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