File: vars.c

package info (click to toggle)
elilo 3.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,356 kB
  • ctags: 2,507
  • sloc: ansic: 9,556; sh: 639; asm: 532; makefile: 196
file content (125 lines) | stat: -rw-r--r-- 2,788 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 *  Copyright (C) 2001-2002 Hewlett-Packard Co.
 *	Contributed by Stephane Eranian <eranian@hpl.hp.com>
 *
 *  Copyright (C) 2001 Silicon Graphics, Inc.
 *	Contributed by Brent Casavant <bcasavan@sgi.com>
 *
 * This file is part of the ELILO, the EFI Linux boot loader.
 *
 *  ELILO is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  ELILO is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with ELILO; see the file COPYING.  If not, write to the Free
 *  Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 *  02111-1307, USA.
 *
 * Please check out the elilo.txt for complete documentation on how
 * to use this program.
 */
#include <efi.h>
#include <efilib.h>

/*
 * A variable name is 1 character long and case sensitive. So 
 * we actually have 52 (26*2) possible variables.
 */
#define MAX_VARIABLES		(26<<1)
#define MAX_VARIABLE_LENGTH	128
#define VAR_IDX(a)		(((a) >= 'a' && (a) <= 'z') ? 26-'a'+(a) : (a)-'A')
#define IDX_VAR(i)		((i) < 26 ? 'A'+(i) : 'a'+ ((i)-26))

typedef struct {
	CHAR16	value[MAX_VARIABLE_LENGTH];
} elilo_var_t;

static elilo_var_t vars[MAX_VARIABLES];	/* set of variables */

INTN
set_var(CHAR16 v, CHAR16 *value)
{
	/* invalid variable name */
	if (v < 'A' || (v > 'Z' && v < 'a') || v > 'z') return -1;

	StrCpy(vars[VAR_IDX(v)].value, value);
	return 0;
}

CHAR16 *
get_var(CHAR16 v)
{
	/* invalid variable name */
	if (v < L'A' || (v > L'Z' && v < L'a') || v > L'z') return NULL;

	return vars[VAR_IDX(v)].value;
}


VOID
print_vars(VOID)
{
	INTN i;
	UINTN cnt = 0;

	for(i=0; i < MAX_VARIABLES; i++) {
		if (vars[i].value[0]) {
			cnt++;
			Print(L"%c = \"%s\"\n", IDX_VAR(i), vars[i].value);
		}
	}
	if (cnt == 0) Print(L"no variable defined\n");
}


INTN
subst_vars(CHAR16 *in, CHAR16 *out, INTN maxlen)
{
	INTN i, l, j, cnt;
	INTN m = 0, d = 0;
	CHAR16 *val;

	if (in == NULL || out == NULL || maxlen <= 1) return -1;

	l = StrLen(in);

	maxlen--;

	for (i=0, j=0;i < l; i++) {
		cnt = 1;
		val = in+i;

		if (*val == L'\\'  && d == 0) {
			d = 1; 
			continue;
		}
		if(m == 1) {
		    m = 0;
		    val = get_var(*val);

		    if (val == NULL) continue;

		    cnt = StrLen(val);

		} else if (*val == L'%' && d == 0) {
			m = 1;
			continue;
		}
		d = 0;
		while (j < maxlen && cnt) {
			out[j++] = *val++;
			cnt--;
		}
		if (j == maxlen) break;
	}
	out[j] = CHAR_NULL;

	return 0;
}