source: branches/port5/AISC/aisc.c

Last change on this file was 5968, checked in by westram, 16 years ago
  • new flag -w to aisc_mkpt (add include wrapper)
  • uniform style for several include wrappers
  • removed duplicated includes
  • removed useless nt_concatenate.hxx
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.7 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3/* #include <malloc.h> */
4#include <string.h>
5#include "aisc.h"
6
7const int linebufsize = 200000;
8char      string_buf[256]; 
9
10struct global_struct *gl;
11
12AD *read_aisc(char ** in);
13
14#define get_byte(io)  do { gl->lastchar = *((*io)++); if (gl->lastchar == '\n') gl->line_cnt++; } while(0)
15
16char *read_aisc_file(char *path)
17{
18    FILE *input;
19    int   data_size;
20    char *buffer = 0;
21    if ((input = fopen(path, "r")) == 0) {
22        fprintf(stderr, " file %s not found\n", path);
23    }else{
24        if (fseek(input,0,2)==-1){
25            fprintf(stderr, "file %s not seekable\n",path);
26        }
27        else {
28            data_size = (int)ftell(input);
29
30            if (data_size == 0) {
31                fprintf(stderr, "%s:0: file is empty\n", path);
32            }
33            else {
34                data_size++;
35                rewind(input);
36                buffer =  (char *)malloc(data_size+1);
37                data_size = fread(buffer,1,data_size,input);
38                buffer[data_size] = 0;
39            }
40        }
41        fclose(input);
42    }
43    return buffer;
44}
45
46
47static void aisc_init()
48{
49    int             i;
50    gl = (struct global_struct *)calloc(sizeof(struct global_struct),1);
51    gl->linebuf = (char *)calloc(sizeof(char),linebufsize+2);
52    gl->line_cnt = 1;
53    gl->error_flag = 0;
54    gl->lastchar = ' ';
55    gl->bufsize = linebufsize;
56    gl->out = stdout;
57    gl->outs[0] = stdout;
58    gl->fouts[0] = strdup("stdout");
59    gl->outs[1] = stdout;
60    gl->fouts[1] = strdup("*");
61    gl->tabstop = 8;
62    do_com_push("");
63    gl->fns = create_hash(HASHSIZE);
64    for (i = 0; i < 256; i++) {
65        if ((i == (int) ' ') ||
66            (i == (int) '\t') ||
67            (i == (int) '\n') ||
68            (i == (int) ',') ||
69            (i == (int) ';') ||
70            (i == EOSTR)
71            ) {
72            gl->b_tab[i] = 1;
73        } else {
74            gl->b_tab[i] = 0;
75        }
76        if ( (i == (int) '\n') ||
77             (i == (int) ',') ||
78             (i == (int) ';') ||
79             (i == EOSTR)
80             ) {
81            gl->c_tab[i] = 1;
82        } else {
83            gl->c_tab[i] = 0;
84        }
85        if ((i == (int) ' ') ||
86            (i == (int) '\t') ||
87            (i == (int) '\n') ||
88            (i == EOSTR)
89            ) {
90            gl->s2_tab[i] = 1;
91        } else {
92            gl->s2_tab[i] = 0;
93        }
94        if ((i == (int) ' ') ||
95            (i == (int) '\t') ||
96            (i == (int) '\n')
97            ) {
98            gl->s3_tab[i] = 1;
99        } else {
100            gl->s3_tab[i] = 0;
101        }
102
103        if ((i == (int) ' ') ||
104            (i == (int) '\t') ||
105            (i == EOSTR)
106            ) {
107            gl->s_tab[i] = 1;
108        } else {
109            gl->s_tab[i] = 0;
110        }
111
112        gl->outtab[i] = i;
113    }
114    gl->outtab[(unsigned char)'n'] = '\n';
115    gl->outtab[(unsigned char)'t'] = '\t';
116    gl->outtab[(unsigned char)'0'] = 0;
117    gl->outtab[(unsigned char)'1'] = 0;
118    gl->outtab[(unsigned char)'2'] = 0;
119    gl->outtab[(unsigned char)'3'] = 0;
120    gl->outtab[(unsigned char)'4'] = 0;
121    gl->outtab[(unsigned char)'5'] = 0;
122    gl->outtab[(unsigned char)'6'] = 0;
123    gl->outtab[(unsigned char)'7'] = 0;
124    gl->outtab[(unsigned char)'8'] = 0;
125    gl->outtab[(unsigned char)'9'] = 0;
126    gl->outtab[(unsigned char)'\\'] = 0;
127}
128
129static void p_err(const char *error) {
130    fprintf(stderr, "%s:%i: Error: %s\n", gl->line_path, gl->line_cnt, error);
131    gl->error_flag = 1;
132}
133
134static void p_err_eof         (void){ p_err("Unexpected end of file seen"); }
135/* static void p_error_brih      (void){ p_err("You tried to insert a bracket in a named field"); } */
136static void p_error_nobr      (void){ p_err("{} found, missing contents"); }
137static void p_error_nocbr     (void){ p_err("missing '}'"); }
138static void p_error_emwbr     (void){ p_err("string expected, ',' found"); }
139static void p_error_hewnoid   (void){ p_err("string expected, ';' found"); }
140static void p_error_mixhnh    (void){ p_err("you cannot use the symbol '@' in this line (or it must be the third symbol in the line)"); }
141static void p_error_misscom   (void){ p_err("missing ';'"); }
142static void p_error_missco    (void){ p_err("missing ',' or ';' or 'newline'"); }
143static void p_error_exp_string(void){ p_err("string expected"); }
144
145static char *read_aisc_string(char ** in, int *is_m) {
146    char           *cp;
147    char            buf[1024];
148    cp = &buf[0];
149 read_spaces:
150    while ((gl->lastchar != EOSTR) && (gl->s2_tab[gl->lastchar]))
151        get_byte(in);   /* spaces */
152    if (gl->lastchar == '#') {
153        while ((gl->lastchar != EOSTR) && (gl->lastchar != '\n'))
154            get_byte(in);       /* comment */
155        goto read_spaces;
156    }
157    if (gl->lastchar == EOSTR) {
158        *is_m = 5;
159        return 0;
160    }
161    if (gl->lastchar == '}') {
162        *is_m = 2;
163        return 0;
164    }
165    if (gl->lastchar == '{') {
166        *is_m = 3;
167        return 0;
168    }
169    if (gl->lastchar == ',') {
170        *is_m = 4;
171        return 0;
172    }
173    if (gl->lastchar == ';') {
174        *is_m = 5;
175        return 0;
176    }
177    if (gl->lastchar == '@') {
178        if (strncmp(*in, "SETSOURCE", 9) == 0) {
179            char *space = (*in)+9;
180            char *file;
181            char *comma;
182            char *end;
183            if (*space != ' ') {
184                p_err("space expected after '@SETSOURCE' (injected code)");
185                return 0;
186            }
187            *in   = space;
188            get_byte(in);
189            while ((gl->lastchar != EOSTR) && (gl->s_tab[gl->lastchar])) get_byte(in);
190            file  = (*in)-1;
191            comma = strchr(file, ',');
192            if (!comma) {
193                p_err("comma expected after '@SETSOURCE filename' (injected code)");
194                return 0;
195            }
196            end = strchr(comma, '@');
197            if (!end) {
198                p_err("'@' expected after '@SETSOURCE filename,line' (injected code)");
199                return 0;
200            }
201
202            comma[0]      = 0;
203            gl->line_path = file;
204            gl->line_cnt  = atoi(comma+1);
205
206            *in = end+1;
207            get_byte(in);
208            return read_aisc_string(in, is_m);
209        }
210        else {
211            *is_m = 1;
212            get_byte(in);
213            while ((gl->lastchar != EOSTR) && (gl->s_tab[gl->lastchar])) get_byte(in);
214            if (gl->lastchar == EOSTR) {
215                p_err_eof();
216                return 0;
217            }
218        }
219    } else {
220        *is_m = 0;
221    }
222    if (gl->lastchar == BEG_STR1) {
223        get_byte(in);
224        if (gl->lastchar == BEG_STR2) {
225            get_byte(in);
226        read_next_char:
227            while ( (gl->lastchar != EOSTR) && (gl->lastchar != END_STR1)) {
228                *(cp++) = gl->lastchar;
229                get_byte(in);
230            }
231            if (gl->lastchar == END_STR1) {
232                get_byte(in);
233                if (gl->lastchar == END_STR2 ) {
234                    get_byte(in);
235                }else{
236                    *(cp++) = END_STR1;
237                    goto read_next_char;
238                }
239            }
240
241        } else {
242            *(cp++) = BEG_STR1;
243            while ((!gl->b_tab[gl->lastchar]) && (gl->lastchar != EOSTR) ) {
244                *(cp++) = gl->lastchar;
245                get_byte(in);
246            }
247        }
248    }else{
249        while ((!gl->b_tab[gl->lastchar]) && (gl->lastchar != EOSTR) ) {
250            *(cp++) = gl->lastchar;
251            get_byte(in);
252        }
253    }
254    if (gl->lastchar == EOSTR) {
255        p_err_eof();
256        return 0;
257    }
258
259 read_spaces2:
260    while ((gl->lastchar != EOSTR) && (gl->s_tab[gl->lastchar]))
261        get_byte(in);
262    if (gl->lastchar == '#') {
263        while ((gl->lastchar != EOSTR) && (gl->lastchar != '\n'))
264            get_byte(in);       /* comment */
265        goto read_spaces2;
266    }
267
268    if (gl->lastchar == EOSTR) {
269        p_err_eof();
270        return 0;
271    }
272    *cp = 0;
273    if (!buf[0])
274        return 0;       /* emtpy string */
275    return (char *) strdup(buf);
276}
277
278static AD *make_AD(void) { return (AD *) calloc(sizeof(AD), 1); }
279static HS *make_HS(void) { return (HS *) calloc(sizeof(HS), 1); }
280CL *make_CL(void) { return (CL *) calloc(sizeof(CL), 1); }
281
282#define ITEM_MAKE       hitem = make_AD();if (item) {   \
283        item->next_item = hitem;                        \
284    }else{                                              \
285        first_item = hitem;                             \
286    }; item  = hitem; item->first_item = first_item;
287
288static AD *read_aisc_line(char ** in, HS ** hs)
289{                               /* lese bis zum ;/} */
290    static int      is_m;
291    char           *str;
292    AD             *first_item, *item, *hitem;
293    HS             *first_header, *header, *hheader;
294    first_item = item = 0;
295    get_byte(in);
296    first_header = header = *hs;
297    while (1) {
298        str = read_aisc_string(in, &is_m);
299        if (gl->error_flag)
300            return 0;
301        switch (is_m) {
302            case 2:
303                return first_item;      /* } */
304            case 3:     /* { */
305                ITEM_MAKE;
306                hitem = read_aisc(in);
307                if (gl->error_flag)
308                    return 0;
309                if ((header) && (header->key)) {
310                    item->key = header->key;
311                }else{
312                    item->key = "{";
313                }
314                item->sub = hitem;
315                if (hitem)      hitem->father = item;
316                if (gl->lastchar != '}') {
317                    p_error_nocbr();
318                    return 0;
319                }
320                get_byte(in);
321                break;
322            case 4:     /* , */
323                if ((header) && (header->key)) {
324                } else {
325                    p_error_emwbr();
326                    fprintf(stderr,"There is a header line: %s\n",header->key);
327                    return 0;
328                };
329                get_byte(in);
330                break;
331            case 5:     /* ; */
332                /*if (header) {
333                  p_error_hewnoid();
334                  return 0;
335                  }
336                */
337                return first_item;
338            case 1:     /* @ */
339                if (header != first_header) {
340                    p_error_mixhnh();
341                    return 0;
342                }
343                first_header = header = make_HS();
344                header->key = str;
345                while (gl->lastchar == ',') {
346                    get_byte(in);
347                    str = read_aisc_string(in, &is_m);
348                    if (is_m != 1) {
349                        p_error_mixhnh();
350                        return 0;
351                    };
352                    hheader = make_HS();
353                    header->next = hheader;
354                    header = hheader;
355                    header->key = str;
356                }
357                if (gl->lastchar != ';') {
358                    p_error_misscom();
359                    return 0;
360                };
361                if (first_header->key) {
362                    *hs = first_header;
363                } else {
364                    *hs = 0;
365                }
366                get_byte(in);
367                return 0;
368            case 0:
369                ITEM_MAKE
370                    if (header && header->key) {
371                        item->key = header->key;
372                        item->val = str;
373                        if (!gl->c_tab[gl->lastchar]) {
374                            p_error_missco();
375                            return 0;
376                        }
377                    } else {
378                        item->key = str;
379                        str = read_aisc_string(in, &is_m);
380                        switch (is_m) {
381                            case 3:
382                                hitem = read_aisc(in);
383                                if (gl->error_flag)
384                                    return 0;
385                                if (!hitem) {
386                                    p_error_nobr();
387                                    return 0;
388                                }
389                                item->sub = hitem;
390                                hitem->father = item;
391                                if (gl->lastchar != '}') {
392                                    p_error_nocbr();
393                                    return 0;
394                                }
395                                get_byte(in);
396                                break;
397                            case 0:
398                                item->val = str;
399                                if (!gl->c_tab[gl->lastchar]) {
400                                    p_error_missco();
401                                    return 0;
402                                }
403                                break;
404                            case 4:
405                            case 5:
406                                item->val = strdup("");
407                                break;
408                            default:
409                                p_error_exp_string();
410                                return 0;
411                        }
412                    };
413                if (gl->lastchar == ';') {
414                    get_byte(in);
415                    if (header && header->next) {
416                        p_error_hewnoid();
417                        fprintf(stderr,"There is a header line: %s; %s\n",header->next->key, str);
418                        return 0;
419                    }
420                    return first_item;
421                };
422                get_byte(in);
423                break;
424        }
425        if (header)
426            header = header->next;
427    }
428
429}
430
431AD             *
432read_aisc(char ** in)
433{
434    AD             *first, *a, *data = 0;
435    HS             *header;
436    first = 0;
437    header = 0;
438    while ((EOSTR != gl->lastchar) && (gl->lastchar != '}')) {
439        a = read_aisc_line(in, &header);
440        if (gl->error_flag)
441            return 0;
442        if (a) {
443            if (data) {
444                data->next_line = a;
445            } else {
446                first = a;
447            };
448            data = a;
449            data->first_line = first;
450        }
451    };
452    return first;
453}
454
455static CL *read_prog(char ** in,char *file)
456{
457    char *p;
458    CL *hcl,*cl,*first_cl;
459    first_cl = cl = 0;
460    gl->line_cnt = 0;
461    while (gl->lastchar != EOSTR) {
462    read_spaces3:
463        while ((gl->lastchar != EOSTR) && (gl->s2_tab[gl->lastchar]))
464            get_byte(in);
465        if (gl->lastchar == '#') {
466            while ((gl->lastchar != EOSTR) && (gl->lastchar != '\n'))
467                get_byte(in);   /* comment */
468            goto read_spaces3;
469        }
470        if (gl->lastchar == EOSTR) break;
471        p = (*in)-1;
472        while ((gl->lastchar != EOSTR) && (gl->lastchar != '\n'))
473            get_byte(in);
474        (*in)[-1] = 0;
475        hcl = make_CL();
476        if (!cl) first_cl = cl = hcl;
477        else{
478            cl->next = hcl;
479            cl = hcl;
480        }
481        cl->str = (char *)strdup(p);
482        cl->path = file;
483        cl->linenr = gl->line_cnt;
484    }
485    return first_cl;
486}
487
488
489
490int main(int argc,char ** argv)
491{
492    char                *buf;
493    CL          *co;
494    int         i;
495    char                abuf[20];
496    int erg;
497    if (argc < 2) {
498        fprintf(stderr, "Specify File name\n");
499        exit(-1);
500    }
501    aisc_init();
502    for (i=0;i<argc;i++) {
503        sprintf(abuf,"argv[%i]",i);
504        write_hash(gl->st->hs,abuf,argv[i]);
505    }
506    sprintf(abuf,"%i",argc);
507    write_hash(gl->st->hs,"argc",abuf);
508
509    buf = read_aisc_file(argv[1]);
510    if (!buf) exit(-1);
511    gl->prg = read_prog(&buf,argv[1]);
512    if (gl->prg) {
513        aisc_calc_special_commands();
514        co = aisc_calc_blocks(gl->prg,0,0,0);
515        if (co) {
516            print_error("unexpected end of file");
517            return 1;
518        }
519        erg = run_prg();
520        /* fprintf(stderr, "run_prg() returns %i\n", erg); */
521        if (erg) {
522            aisc_remove_files();
523            fflush(stdout);
524            exit (-1);
525        }
526    }
527    return 0;
528}
Note: See TracBrowser for help on using the repository browser.