OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [ecos-2.0/] [packages/] [redboot/] [v2_0/] [src/] [alias.c] - Blame information for rev 1773

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.