| 1 |
786 |
skrzyp |
//==========================================================================
|
| 2 |
|
|
//
|
| 3 |
|
|
// alias.c
|
| 4 |
|
|
//
|
| 5 |
|
|
// RedBoot - alias support
|
| 6 |
|
|
//
|
| 7 |
|
|
//==========================================================================
|
| 8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
| 9 |
|
|
// -------------------------------------------
|
| 10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
| 11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
|
| 12 |
|
|
//
|
| 13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
| 14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
| 15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
| 16 |
|
|
// version.
|
| 17 |
|
|
//
|
| 18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
| 19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
| 20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
| 21 |
|
|
// for more details.
|
| 22 |
|
|
//
|
| 23 |
|
|
// You should have received a copy of the GNU General Public License
|
| 24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
| 25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
| 26 |
|
|
//
|
| 27 |
|
|
// As a special exception, if other files instantiate templates or use
|
| 28 |
|
|
// macros or inline functions from this file, or you compile this file
|
| 29 |
|
|
// and link it with other works to produce a work based on this file,
|
| 30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
| 31 |
|
|
// the GNU General Public License. However the source code for this file
|
| 32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
| 33 |
|
|
// General Public License v2.
|
| 34 |
|
|
//
|
| 35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
| 36 |
|
|
// on this file might be covered by the GNU General Public License.
|
| 37 |
|
|
// -------------------------------------------
|
| 38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
| 39 |
|
|
//==========================================================================
|
| 40 |
|
|
//#####DESCRIPTIONBEGIN####
|
| 41 |
|
|
//
|
| 42 |
|
|
// Author(s): gthomas
|
| 43 |
|
|
// Contributors: gthomas, jskov
|
| 44 |
|
|
// Date: 2002-05-15
|
| 45 |
|
|
// Purpose:
|
| 46 |
|
|
// Description:
|
| 47 |
|
|
//
|
| 48 |
|
|
// This code is part of RedBoot (tm).
|
| 49 |
|
|
//
|
| 50 |
|
|
//####DESCRIPTIONEND####
|
| 51 |
|
|
//
|
| 52 |
|
|
//==========================================================================
|
| 53 |
|
|
|
| 54 |
|
|
#include <redboot.h>
|
| 55 |
|
|
|
| 56 |
|
|
#ifdef CYGNUM_REDBOOT_FLASH_STRING_SIZE
|
| 57 |
|
|
# define MAX_STRING_LENGTH CYGNUM_REDBOOT_FLASH_STRING_SIZE
|
| 58 |
|
|
#else
|
| 59 |
|
|
# define MAX_STRING_LENGTH 32
|
| 60 |
|
|
#endif
|
| 61 |
|
|
|
| 62 |
|
|
// Lookup an alias. First try plain string aliases. If that fails try
|
| 63 |
|
|
// other types so allowing access to all configured values. This allows
|
| 64 |
|
|
// for alias (macro) expansion of normal 'fconfig' data, such as the
|
| 65 |
|
|
// board IP address.
|
| 66 |
|
|
static char *
|
| 67 |
|
|
lookup_alias(char *alias, char *alias_buf)
|
| 68 |
|
|
{
|
| 69 |
|
|
if (0 == strcasecmp("FREEMEMLO", alias)) {
|
| 70 |
|
|
diag_sprintf(alias_buf,"0x%x", ((CYG_ADDRWORD)mem_segments[0].start + 0x03ff) & ~0x03ff);
|
| 71 |
|
|
return alias_buf;
|
| 72 |
|
|
} else if (0 == strcasecmp("FREEMEMHI", alias)) {
|
| 73 |
|
|
diag_sprintf(alias_buf,"0x%x", ((CYG_ADDRWORD)mem_segments[0].end) & ~0x03ff);
|
| 74 |
|
|
return alias_buf;
|
| 75 |
|
|
}
|
| 76 |
|
|
|
| 77 |
|
|
#ifdef CYGSEM_REDBOOT_FLASH_ALIASES
|
| 78 |
|
|
return flash_lookup_alias(alias, alias_buf);
|
| 79 |
|
|
#else
|
| 80 |
|
|
return NULL;
|
| 81 |
|
|
#endif
|
| 82 |
|
|
}
|
| 83 |
|
|
|
| 84 |
|
|
// Expand aliases, this is recursive. ie if one alias contains other
|
| 85 |
|
|
// aliases, these will also be expanded from the insertion point
|
| 86 |
|
|
// onwards.
|
| 87 |
|
|
//
|
| 88 |
|
|
// If 'iter' is zero, then quoted strings are not expanded
|
| 89 |
|
|
//
|
| 90 |
|
|
static bool
|
| 91 |
|
|
_expand_aliases(char *line, int len, int iter)
|
| 92 |
|
|
{
|
| 93 |
|
|
char *lp = line;
|
| 94 |
|
|
char *ms, *me, *ep;
|
| 95 |
|
|
char *alias;
|
| 96 |
|
|
char c;
|
| 97 |
|
|
int offset, line_len, alias_len;
|
| 98 |
|
|
char alias_buf[MAX_STRING_LENGTH];
|
| 99 |
|
|
bool macro_found = false;
|
| 100 |
|
|
|
| 101 |
|
|
if ((line_len = strlen(line)) != 0) {
|
| 102 |
|
|
while (*lp) {
|
| 103 |
|
|
c = *lp++;
|
| 104 |
|
|
if ((c == '%') && (*lp == '{')) {
|
| 105 |
|
|
// Found a macro/alias to expand
|
| 106 |
|
|
ms = lp+1;
|
| 107 |
|
|
lp += 2;
|
| 108 |
|
|
while (*lp && (*lp != '}')) lp++;
|
| 109 |
|
|
if (!*lp) {
|
| 110 |
|
|
diag_printf("Invalid macro/alias '%s'\n", ms);
|
| 111 |
|
|
line[0] = '\0'; // Destroy line
|
| 112 |
|
|
return false;
|
| 113 |
|
|
}
|
| 114 |
|
|
me = lp;
|
| 115 |
|
|
*me = '\0';
|
| 116 |
|
|
lp++;
|
| 117 |
|
|
if ((alias = lookup_alias(ms,alias_buf)) != (char *)NULL) {
|
| 118 |
|
|
alias_len = strlen(alias);
|
| 119 |
|
|
// See if there is room in the line to expand this macro/alias
|
| 120 |
|
|
if ((line_len+alias_len) < len) {
|
| 121 |
|
|
// Make a hole by moving data within the line
|
| 122 |
|
|
offset = alias_len-strlen(ms)-2; // Number of bytes being inserted
|
| 123 |
|
|
ep = &lp[strlen(lp)-1];
|
| 124 |
|
|
if (offset > 1) {
|
| 125 |
|
|
ep[offset] = '\0';
|
| 126 |
|
|
while (ep != (lp-1)) {
|
| 127 |
|
|
ep[offset-1] = *ep;
|
| 128 |
|
|
ep--;
|
| 129 |
|
|
}
|
| 130 |
|
|
} else {
|
| 131 |
|
|
if (offset <=0) {
|
| 132 |
|
|
while ((lp-1) != ep) {
|
| 133 |
|
|
lp[offset-1] = *lp;
|
| 134 |
|
|
lp++;
|
| 135 |
|
|
}
|
| 136 |
|
|
lp[offset-1]='\0';
|
| 137 |
|
|
}
|
| 138 |
|
|
}
|
| 139 |
|
|
// Insert the macro/alias data
|
| 140 |
|
|
lp = ms-2;
|
| 141 |
|
|
while (*alias) {
|
| 142 |
|
|
if ((alias[0] == '%') && (alias[1] == '{')) macro_found = true;
|
| 143 |
|
|
*lp++ = *alias++;
|
| 144 |
|
|
}
|
| 145 |
|
|
line_len = strlen(line);
|
| 146 |
|
|
lp = lp - alias_len;
|
| 147 |
|
|
} else {
|
| 148 |
|
|
diag_printf("No room to expand '%s'\n", ms);
|
| 149 |
|
|
line[0] = '\0'; // Destroy line
|
| 150 |
|
|
return false;
|
| 151 |
|
|
}
|
| 152 |
|
|
} else {
|
| 153 |
|
|
diag_printf("Alias '%s' not defined\n", ms);
|
| 154 |
|
|
*me = '|';
|
| 155 |
|
|
}
|
| 156 |
|
|
} else if ((c == '"') && (iter == 0)) {
|
| 157 |
|
|
// Skip quoted strings
|
| 158 |
|
|
while (*lp && (*lp != '"')) lp++;
|
| 159 |
|
|
}
|
| 160 |
|
|
}
|
| 161 |
|
|
}
|
| 162 |
|
|
return macro_found;
|
| 163 |
|
|
}
|
| 164 |
|
|
|
| 165 |
|
|
void
|
| 166 |
|
|
expand_aliases(char *line, int len)
|
| 167 |
|
|
{
|
| 168 |
|
|
int iter = 0;
|
| 169 |
|
|
|
| 170 |
|
|
while (_expand_aliases(line, len, iter++)) {
|
| 171 |
|
|
}
|
| 172 |
|
|
}
|