-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.c
257 lines (208 loc) · 5.25 KB
/
token.c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// a tokening system which allows replacing of certain "variables"
// by their values.
// used to handle configuration of different compiler systems.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "makegen.h"
static void addtoken(token **last, token *t);
static uchar GetVarType(char *vname);
// splits up a line into tokens and returns the first token.
// this return value can then be passed to assemble_token().
token *tokenize(char *line)
{
int len = strlen(line);
int asize = len + 1;
token *firsttoken, *lasttoken;
token *t;
int i, j;
uchar ch;
char *ptr;
//printf("tokenize: '%s'\n", line);
if (!(t = malloc(sizeof(token))))
{
printf("tokenize: out of memory!\n");
return NULL;
}
if (!(t->str = malloc(asize)))
{
printf("tokenize: out of memory(2)!\n");
free(t);
return NULL;
}
t->type = TKN_STRING;
firsttoken = lasttoken = t;
for(i=j=0;i<len;i++)
{
topofloop:
ch = line[i];
if (ch=='%')
{
if (line[i+1]=='%') // %% is delimiter for %
{
t->str[j++] = '%';
}
else // it's a variable!!
{
// end the current string token
t->str[j] = 0; j = 0;
addtoken(&lasttoken, t);
// retrieve the variable name
if (!(ptr = strchr(&line[++i], '%')))
{
printf("tokenize: odd number of '%%' on '%s'\n", line);
goto failure;
}
*ptr = 0;
//printf("varname: '%s'\n", &line[i]);
// create a variable token and add it
if (!(t = malloc(sizeof(token)))) goto allocfail;
t->type = GetVarType(&line[i]);
if (!t->type) goto failure;
addtoken(&lasttoken, t);
i += (ptr - &line[i]) + 1;
//printf("rest of str: '%s'\n", &line[i]);
// create a new string token and we'll continue parsing
if (line[i])
{
if (!(t = malloc(sizeof(token)))) goto allocfail;
if (!(t->str = malloc(asize))) goto allocfail;
t->type = TKN_STRING;
goto topofloop;
}
}
}
else
{
t->str[j++] = ch;
}
}
if (j || !len)
{
t->str[j] = 0;
addtoken(&lasttoken, t);
}
return firsttoken;
allocfail:
printf("tokenize: Memory allocation failure!\n");
failure:
// we failed--free up everything we created before exiting
printf("tokenize: operation failed; on string beginning with:\n'%s'\n", line);
token_cleanup(firsttoken);
return NULL;
}
// tokenize a const char * without segfaulting
token *tokenize_const(const char *line)
{
char *tempbuf;
token *t = NULL;
tempbuf = strdup(line);
if (!tempbuf)
{
printf("tokenize_const: memory allocation error");
return NULL;
}
t = tokenize(tempbuf);
free(tempbuf);
return t;
}
// creates a string from a tokenlist previously created by tokenize(),
// with the variables replaced by their current values.
// returns nonzero on error.
char assemble_token(token *firsttoken, char *str, ml_header *header, int src, const char *output_prefix_path)
{
token *t;
int i;
char *catstr;
char prefix_binpath;
i = str[0] = 0;
t = firsttoken;
while(t)
{
prefix_binpath = 0;
switch(t->type)
{
case TKN_STRING:
catstr = t->str;
break;
case TKN_SRCFILE:
if (src==-1) goto badvar;
catstr = sources[src].name;
break;
case TKN_MODULE:
if (src == -1) goto badvar;
catstr = sources[src].modulename;
prefix_binpath = 1;
break;
case TKN_OUTPUT:
catstr = header->defsec->exe;
prefix_binpath = 1;
break;
case TKN_OUTPUTMODULE:
catstr = header->defsec->exemodule;
prefix_binpath = 1;
break;
case TKN_CMDFILE:
catstr = curcmdfilename;
break;
case TKN_OBJ_EXT: catstr = sources[src].hdrsec->obj_ext; break;
case TKN_H_EXT: catstr = h_ext; break;
case TKN_FDH_EXT: catstr = fdh_ext; break;
case TKN_MLNAME: catstr = mlname; break;
case TKN_MAKEFILE: catstr = makefile; break;
default:
printf("assemble_token: bad token type %d\n", t->type);
return 1;
}
if (prefix_binpath && output_prefix_path != NULL)
{
strcpy(&str[i], output_prefix_path);
i += strlen(output_prefix_path);
}
strcpy(&str[i], catstr);
i += strlen(catstr);
t = t->next;
}
return 0;
badvar:
printf("assemble_token: specified variable makes no sense in this context!\n");
printf("line up to now: '%s'\n", str);
return 1;
}
// deallocate a tokenlist
void token_cleanup(token *firsttoken)
{
token *t, *next;
t = firsttoken;
while(t)
{
next = t->next;
free(t);
t = next;
}
}
static void addtoken(token **last, token *t)
{
/*printf("adding token %d",t->type);
if (!t->type) printf(" str '%s'", t->str);
printf("\n");*/
(*last)->next = t;
t->next = NULL;
*last = t;
}
// returns a Token Type from a varname, or 0.
static uchar GetVarType(char *vname)
{
if (!strcasecmp(vname, "SRCFILE")) return TKN_SRCFILE;
if (!strcasecmp(vname, "MODULE")) return TKN_MODULE;
if (!strcasecmp(vname, "OUTPUT")) return TKN_OUTPUT;
if (!strcasecmp(vname, "OUTPUTMODULE")) return TKN_OUTPUTMODULE;
if (!strcasecmp(vname, "CMDFILE")) return TKN_CMDFILE;
if (!strcasecmp(vname, "OBJ_EXT")) return TKN_OBJ_EXT;
if (!strcasecmp(vname, "H_EXT")) return TKN_H_EXT;
if (!strcasecmp(vname, "FDH_EXT")) return TKN_FDH_EXT;
if (!strcasecmp(vname, "MLNAME")) return TKN_MLNAME;
if (!strcasecmp(vname, "MAKEFILE")) return TKN_MAKEFILE;
printf(" ** unknown token variable '%s'\n", vname);
return 0;
}