File: loaddata.cc

package info (click to toggle)
intel2gas 1.3.3-15
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 472 kB
  • ctags: 132
  • sloc: cpp: 1,286; makefile: 87
file content (368 lines) | stat: -rw-r--r-- 7,587 bytes parent folder | download | duplicates (11)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
    loaddata.cc    parses syntax and list files
    Copyright (c) 1999 Mikko Tiihonen (mikko.tiihonen@hut.fi)
    This source code is licensed under the GNU LGPL
  
    Please refer to the file COPYING.LIB contained in the distribution for
    licensing conditions
*/

#include "intel2gas.h"
#include "loaddata.h"
#include "strhandle.h"

#include <stdlib.h>
#include <string.h>
#ifdef USE_GLOB
#include <glob.h>         //  Find files
#else
#include <errno.h>
#include <dirent.h>
#endif


int current_line; // global variable holds current line number in file


/* Reads the next line from f. 
 * - Converts tabs to whitespace.
 * - Strips comments.
 * Data is stored in global buffer tempstr */
void ReadNextLine(FILE *f, bool strip_comments)
{
    if (feof(f)) {
        tempstr[0] = '\0';
	return;
    }
    char temp[TEMP_SIZE];
    temp[0] = '\0';
    fgets(temp,TEMP_SIZE,f);
    current_line ++;

    char *s1 = temp,
	 *s2 = tempstr;
    while (*s1 != '\0') {
	// convert tabs to spaces
	if (*s1 == '\t') {
	    s1++;
	    do {
	        *s2++ = ' ';
	    } while ((s2-tempstr)%TAB_SIZE != 0);
	    continue;
	}
	if (strip_comments) {
	    if (*s1 == '\\') {
	        if (s1[1] == '#') {
		    *s2++ = '#';
		    s1 += 2;
		    continue;
		}
	    }
	    if (*s1 == '#')
	        break;
	}
	if (*s1 == '\n' || *s1 == '\r')
	    break;
	*s2++ = *s1++;
    }
    *s2 = '\0';
}


/* If str is a valid assignment does it */
static void HandleVarSet(char const * const str)
{
    if (str[0] != '@') return;
    char name[STR_LEN+1], value[STR_LEN+1];
    str_splitstrat(str+1,name,'=',value);
    str_cutat(name,' ');
    setVar(name, str_skipspace(value));
}


/* Allocates a new empty structure for .list file and
 * adds it to linked list called list */
static list_t *newList()
{
    list_t *l = new list_t;
    l->words[0] = NULL;
    l->bit = 0;
    l->assignments = NULL;

    l->next = list;
    list = l;

    return l;
}


/* Allocates a new structure to .syntax file.
 * It's added to linked list called syntaxlist */
static syntaxlist_t *newSyntaxList(char const *name)
{
    syntaxlist_t *s = new syntaxlist_t;
    strcpy(s->name,name);
    s->syntax = NULL;
    s->var = NULL;

    s->next = syntaxlist;
    syntaxlist = s;

    return s;
}


/* Allocates a new empty structure for a single parse syntax.
 * Adds it last to linked list called in current syntaxlist. */
static syntax_t *newSyntax()
{
    syntax_t *s = new syntax_t;
    s->parseline[0] = '\0';
    s->output[0] = '\0';
    s->assignments = NULL;
    s->assignments_after = NULL;

    s->next = NULL;
    if (syntaxlist->syntax == NULL) {
	syntaxlist->syntax = s;
	return s;
    }

    // put last in list
    syntax_t *ss = syntaxlist->syntax;
    while (ss->next)
	ss = ss->next;
    ss->next = s;
    
    return s;
}


/* Deletes all varibles in syntax's scope */
void deleteVars(syntaxlist_t *syntaxlist)
{
    variable_t *v = syntaxlist->var;
    syntaxlist->var = NULL;
    while (v) {
	variable_t *delme = v;
	v = v->next;
	delete delme;
    }
}


static void LoadWordList(char const filename[])
// where filename is listname.nn.list
{
    FILE *f;
    f = fopen(filename,"r");
    current_line = 0;
    
    list_t *l = newList();
    
    // get the nn letters of filename
    int strl = strlen(filename);
    l->bit = 0x10*(filename[strl-7]-'0') +
             0x01*(filename[strl-6]-'0');
    strcpy(l->name,strrchr(filename,'/')+1);
    str_cutat(l->name,'.');

    newSyntaxList("dummy"); // all variables go here
    
    int mode = 0;
    int count = 0;
    while (!feof(f)) {
	ReadNextLine(f, true);
	char *str = tempstr;
       	str = str_skipspace(str);
	// do not allow empty lines (should we?)
	if (str_empty(str)) continue;
	if (mode == 0) {
	    if (!strcmp(str,"-")) {
		// steal the variables from dummy syntax
		l->assignments = syntaxlist->var;
		syntaxlist->var = NULL;
		mode++;
		continue;
	    }
	    if (str[0] == '@') {
	        HandleVarSet(str);	        
		continue;
	    }
	    printf("Error while reading %s: Unknown meaning (%s)\n",filename,str);
	    exit(1);
	}
	if (count == WORDS_PER_FILE-1) {
	    printf("Error while readomg %s: Too many words in file\n",filename);
	    exit(1);
	}
	l->words[count] = strdup(str);
	count++;
    }
    fclose(f);
    l->words[count] = NULL;
    
    // free dummy syntax
    deleteVars(syntaxlist);
    delete syntaxlist;
    syntaxlist = NULL;
}


static void LoadSyntax(char const *filename)
// where filename is syntaxname.syntax
{
    FILE *f;
    f = fopen(filename,"r");
    current_line = 0;
    
    char name[STR_LEN+1];
    strcpy(name,strrchr(filename,'/')+1);
    str_cutat(name,'.');
    newSyntaxList(name);
    
    int mode = 0;
    syntax_t *s = NULL;

    while (!feof(f)) {
	ReadNextLine(f, true);
	char *str = tempstr;
	str = str_skipspace(str);
	if (str_empty(str)) continue;
	if (!strcmp(tempstr,"-")) {
	    if (s == NULL) continue;
	    // move variables to this syntax
	    s->assignments_after = syntaxlist->var;
	    syntaxlist->var = NULL;

	    s = NULL;
	    mode = 0;
	    continue;
	}
	if (str[0] == '@') {
	    HandleVarSet(str);
	    continue;
	}
	if (mode == 0) {
	    s = newSyntax();
	    strcpy(s->parseline,str);
	    str_catchar(s->parseline,' '); // to allow spaces after parsed line
	    mode = 1;
	    continue;
	}
	if (mode == 2) {
	    printf("In %s(%d): Only one result line allowed\n%s\n",filename,current_line,str);
	    exit(1);
	}
	mode = 2;
	if (!strcmp(str,"SKIPLINE"))
	    s->output[0] = '\0';
	else
	    strcpy(s->output,str);

	// steal variables
	s->assignments = syntaxlist->var;
	syntaxlist->var = NULL;
    }
    fclose(f);
    deleteVars(syntaxlist);
}



/* Loads all .list files in specified directory */
void LoadWordLists(const char *resource_path)
{
#ifdef USE_GLOB
    
    glob_t globbuf;
  
    strcpy(tempstr,resource_path);
    strcat(tempstr,"*.??.list");
    if (glob(tempstr,GLOB_NOSORT,NULL,&globbuf) < 0) {
	perror("glob");
	exit(1);
    }

    for (unsigned i=0;i<globbuf.gl_pathc; i++) {
	LoadWordList(globbuf.gl_pathv[i]);
    }
    globfree(&globbuf);
    
#else // USE_GLOB
    
    DIR *dp;
    dirent *entry;
	
    dp=opendir(resource_path);
    if(errno!=0) {
      perror("opendir");
      exit(1);
    }
    do {
      errno=0;
      if((entry=readdir(dp))!=NULL) {
	if(strstr(entry->d_name,".list"))
	  if(strlen(strstr(entry->d_name,".list"))==strlen(".list")) {
	    strcpy(tempstr,resource_path);
	    strcat(tempstr,entry->d_name);
	    LoadWordList(tempstr);
	  }
      } else if(errno!=0) {
	perror("readdir");
	exit(1);
      }
    } while(entry);
    closedir(dp);
#endif // !USE_GLOB
}


/* Loads all .syntax files in specified directory */
void LoadSyntaxis(const char *resource_path)
{
#ifdef USE_GLOB
    
  glob_t globbuf;
  
  strcpy(tempstr,resource_path);
  strcat(tempstr,"*.syntax");
  if (glob(tempstr,GLOB_NOSORT,NULL,&globbuf) < 0) {
    perror("glob");
    exit(1);
  }

  for (unsigned i=0;i<globbuf.gl_pathc; i++) {
    LoadSyntax(globbuf.gl_pathv[i]);
  }
  globfree(&globbuf);
    
#else // USE_GLOB
    
  DIR *dp;
  dirent *entry;
	
  dp=opendir(resource_path);
  if(errno!=0) {
    perror("opendir");
    exit(1);
  }
  do {
    errno=0;
    if((entry=readdir(dp))!=NULL) {
      if(strstr(entry->d_name,".syntax"))
	if(strlen(strstr(entry->d_name,".syntax"))==strlen(".syntax")) {
	  strcpy(tempstr,resource_path);
	  strcat(tempstr,entry->d_name);
	  LoadSyntax(tempstr);
	}
    } else if(errno!=0) {
      perror("readdir");
      exit(1);
    }
  } while(entry);
  closedir(dp);
    
#endif // !USE_GLOB
}