daaa4f32c3dd16f132ca01f1b984f1852ef8ed8d
[fw/sdcc] / as / mcs51 / lklex.c
1 /* lklex.c */
2
3 /*
4  * (C) Copyright 1989-1995
5  * All Rights Reserved
6  *
7  * Alan R. Baldwin
8  * 721 Berkeley St.
9  * Kent, Ohio  44240
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #include "aslink.h"
15
16 /*)Module       lklex.c
17  *
18  *      The module lklex.c contains the general lexical analysis
19  *      functions used to scan the text lines from the .rel files.
20  *
21  *      lklex.c contains the fllowing functions:
22  *              char    endline()
23  *              char    get()
24  *              VOID    getfid()
25  *              VOID    getid()
26  *              VOID    getSid()
27  *              int     getline()
28  *              int     getmap()
29  *              char    getnb()
30  *              int     more()
31  *              VOID    skip()
32  *              VOID    unget()
33  *
34  *      lklex.c contains no local variables.
35  */
36
37 /*)Function     VOID    getid(id,c)
38  *
39  *              char *  id              a pointer to a string of
40  *                                      maximum length NCPS
41  *              int     c               mode flag
42  *                                      >=0     this is first character to
43  *                                              copy to the string buffer
44  *                                      <0      skip white space
45  *
46  *      The function getid() scans the current input text line
47  *      from the current position copying the next LETTER | DIGIT string
48  *      into the external string buffer (id).  The string ends when a non
49  *      LETTER or DIGIT character is found. The maximum number of
50  *      characters copied is NCPS.  If the input string is larger than
51  *      NCPS characters then the string is truncated, if the input string
52  *      is shorter than NCPS characters then the string is NULL filled.
53  *      If the mode argument (c) is >=0 then (c) is the first character
54  *      copied to the string buffer, if (c) is <0 then intervening white
55  *      space (SPACES and TABS) are skipped.
56  *
57  *      local variables:
58  *              char *  p               pointer to external string buffer
59  *              int     c               current character value
60  *
61  *      global variables:
62  *              char    ctype[]         a character array which defines the
63  *                                      type of character being processed.
64  *                                      This index is the character
65  *                                      being processed.
66  *
67  *      called functions:
68  *              char    get()           lklex.c
69  *              char    getnb()         lklex.c
70  *              VOID    unget()         lklex.c
71  *
72  *      side effects:
73  *              use of getnb(), get(), and unget() updates the
74  *              global pointer ip the position in the current
75  *              input text line.
76  */
77
78 VOID
79 getid(id, c)
80 register int c;
81 char *id;
82 {
83         register char *p;
84
85         if (c < 0) {
86                 c = getnb();
87         }
88         p = id;
89         do {
90                 if (p < &id[NCPS])
91                         *p++ = c;
92         } while (ctype[c=get()] & (LETTER|DIGIT));
93         unget(c);
94         while (p < &id[NCPS])
95                 *p++ = 0;
96 }
97
98 /*)Function     VOID    getSid (char *id)
99  *
100  *              char *  id              a pointer to a string of
101  *                                      maximum length NCPS
102  *
103  *  getSid is derived from getid. It is called from newsym()
104  *  in lksym.c, when an S-record has to be scanned. getSid accepts
105  *  much more characters than getid, which is necessary for SDCC.
106  * 
107  *      The function getSid() scans the current input text line
108  *      from the current position copying the next string
109  *      into the external string buffer (id).  The string ends when a space
110  *  character (space, tab, \0) is found. The maximum number of
111  *      characters copied is NCPS.  If the input string is larger than
112  *      NCPS characters then the string is truncated, if the input string
113  *      is shorter than NCPS characters then the string is NULL filled.
114  *      Intervening white space (SPACES and TABS) are skipped.
115  *
116  *      local variables:
117  *              char *  p               pointer to external string buffer
118  *              int     c               current character value
119  *
120  *      global variables:
121  *              char    ctype[]         a character array which defines the
122  *                                      type of character being processed.
123  *                                      This index is the character
124  *                                      being processed.
125  *
126  *      called functions:
127  *              char    get()           lklex.c
128  *              char    getnb()         lklex.c
129  *              VOID    unget()         lklex.c
130  *
131  *      side effects:
132  *              use of getnb(), get(), and unget() updates the
133  *              global pointer ip the position in the current
134  *              input text line.
135  */
136
137 VOID
138 getSid (id)
139 char *id;
140 {
141   register int c;
142         register char *p;
143
144   c = getnb();
145         p = id;
146         do {
147                 if (p < &id[NCPS])
148                         *p++ = c;
149                 c = get();
150         } while (c != '\0' && c != ' ' && c != '\t');
151         unget(c);
152         while (p < &id[NCPS])
153                 *p++ = 0;
154 }
155
156 /*)Function     VOID    getfid(fid,c)
157  *
158  *              char *  str             a pointer to a string of
159  *                                      maximum length PATH_MAX
160  *              int     c               this is first character to
161  *                                      copy to the string buffer
162  *
163  *      The function getfid() scans the current input text line from
164  *      the current position copying the next string into the external
165  *      string buffer (str).  The string ends when end of line is found.
166  *      Trailing spacers are removed. The maximum number of characters
167  *      copied is PATH_MAX. If the input string is larger than PATH_MAX
168  *      characters then the string is truncated. The string is NULL
169  *      terminated.
170  *
171  *      local variables:
172  *              char *  p               pointer to external string buffer
173  *              int     c               current character value
174  *
175  *      global variables:
176  *              char    ctype[]         a character array which defines the
177  *                                      type of character being processed.
178  *                                      This index is the character
179  *                                      being processed.
180  *
181  *      called functions:
182  *              char    get()           lklex.c
183  *
184  *      side effects:
185  *              use of get() updates the global pointer ip
186  *              the position in the current input text line.
187  */
188
189 VOID
190 getfid(str, c)
191 register int c;
192 char *str;
193 {
194         register char *p;
195
196         p = str;
197         do
198         {
199                 if (p < &str[PATH_MAX-1])
200                         *p++ = c;
201                 c = get();
202         } while (c);
203         /* trim trailing spaces */
204         --p;
205         while (p >= str && ctype[(int)*p] == SPACE)
206                 --p;
207         /* terminate the string */
208         *(++p) = '\0';
209 }
210
211 /*)Function     char    getnb()
212  *
213  *      The function getnb() scans the current input text
214  *      line returning the first character not a SPACE or TAB.
215  *
216  *      local variables:
217  *              int     c               current character from input
218  *
219  *      global variables:
220  *              none
221  *
222  *      called functions:
223  *              char    get()           lklex.c
224  *
225  *      side effects:
226  *              use of get() updates the global pointer ip, the position
227  *              in the current input text line
228  */
229
230 char
231 getnb()
232 {
233         register int c;
234
235         while ((c=get())==' ' || c=='\t')
236                 ;
237         return (c);
238 }
239
240 /*)Function     VOID    skip()
241  *
242  *      The function skip() scans the input text skipping all
243  *      letters and digits.
244  *
245  *      local variables:
246  *              none
247  *
248  *      global variables:
249  *              char    ctype[]         array of character types, one per
250  *                                      ASCII character
251  *              
252  *      functions called:
253  *              char    get()           lklex.c
254  *              char    getnb()         lklex.c
255  *              VOID    unget()         lklex.c
256  *
257  *      side effects:
258  *              Input letters and digits are skipped.
259  */
260
261 VOID
262 skip(c)
263 register int c;
264 {
265         if (c < 0)
266                 c = getnb();
267         while (ctype[c=get()] & (LETTER|DIGIT)) { ; }
268         unget(c);
269 }
270
271 /*)Function     char    get()
272  *
273  *      The function get() returns the next character in the
274  *      input text line, at the end of the line a
275  *      NULL character is returned.
276  *
277  *      local variables:
278  *              int     c               current character from
279  *                                      input text line
280  *
281  *      global variables:
282  *              char *  ip              pointer into the current
283  *                                      input text line
284  *
285  *      called functions:
286  *              none
287  *
288  *      side effects:
289  *              updates ip to the next character position in the
290  *              input text line.  If ip is at the end of the
291  *              line, ip is not updated.
292  */
293
294 char
295 get()
296 {
297         register int c;
298
299         if ((c = *ip) != 0)
300                 ++ip;
301         return (c);
302 }
303
304 /*)Function     VOID    unget(c)
305  *
306  *              int     c               value of last character
307  *                                      read from input text line
308  *
309  *      If (c) is not a NULL character then the global pointer ip
310  *      is updated to point to the preceeding character in the
311  *      input text line.
312  *
313  *      NOTE:   This function does not push the character (c)
314  *              back into the input text line, only
315  *              the pointer ip is changed.
316  *
317  *      local variables:
318  *              int     c               last character read
319  *                                      from input text line
320  *
321  *      global variables:
322  *              char *  ip              position into the current
323  *                                      input text line
324  *
325  *      called functions:
326  *              none
327  *
328  *      side effects:
329  *              ip decremented by 1 character position
330  */
331
332 VOID
333 unget(c)
334 {
335         if (c != 0)
336                 --ip;
337 }
338
339 /*)Function     int     getmap(d)
340  *
341  *              int     d               value to compare with the
342  *                                      input text line character
343  *
344  *      The function getmap() converts the 'C' style characters \b, \f,
345  *      \n, \r, and \t to their equivalent ascii values and also
346  *      converts 'C' style octal constants '\123' to their equivalent
347  *      numeric values.  If the first character is equivalent to (d) then
348  *      a (-1) is returned, if the end of the line is detected then
349  *      a 'q' error terminates the parse for this line, or if the first
350  *      character is not a \ then the character value is returned.
351  *
352  *      local variables:
353  *              int     c               value of character
354  *                                      from input text line
355  *              int     n               looping counter
356  *              int     v               current value of numeric conversion
357  *
358  *      global variables:
359  *              none
360  *
361  *      called functions:
362  *              char    get()           lklex.c
363  *              VOID    unget()         lklex.c
364  *
365  *      side effects:
366  *              use of get() updates the global pointer ip the position
367  *              in the current input text line
368  */
369
370 int
371 getmap(d)
372 {
373         register int c, n, v;
374
375         if ((c = get()) == '\0')
376                 return (-1);
377         if (c == d)
378                 return (-1);
379         if (c == '\\') {
380                 c = get();
381                 switch (c) {
382
383                 case 'b':
384                         c = '\b';
385                         break;
386
387                 case 'f':
388                         c = '\f';
389                         break;
390
391                 case 'n':
392                         c = '\n';
393                         break;
394
395                 case 'r':
396                         c = '\r';
397                         break;
398
399                 case 't':
400                         c = '\t';
401                         break;
402
403                 case '0':
404                 case '1':
405                 case '2':
406                 case '3':
407                 case '4':
408                 case '5':
409                 case '6':
410                 case '7':
411                         n = 0;
412                         v = 0;
413                         while (++n<=3 && c>='0' && c<='7') {
414                                 v = (v<<3) + c - '0';
415                                 c = get();
416                         }
417                         unget(c);
418                         c = v;
419                         break;
420                 }
421         }
422         return (c);
423 }
424
425 /*)Function     int     getline()
426  *
427  *      The function getline() reads a line of input text from a
428  *      .rel source text file, a .lnk command file or from stdin.
429  *      Lines of text are processed from a single .lnk file or
430  *      multiple .rel files until all files have been read.
431  *      The input text line is copied into the global string ib[]
432  *      and converted to a NULL terminated string.  The function
433  *      getline() returns a (1) after succesfully reading a line
434  *      or a (0) if all files have been read.
435  *      This function also opens each input .lst file and output
436  *      .rst file as each .rel file is processed.
437  *
438  *      local variables:
439  *              int     i               string length
440  *              int     ftype           file type
441  *              char *  fid             file name
442  *
443  *      global variables:
444  *              lfile   *cfp            The pointer *cfp points to the
445  *                                      current lfile structure
446  *              lfile   *filep          The pointer *filep points to the
447  *                                      beginning of a linked list of
448  *                                      lfile structures.
449  *              int     gline           get a line from the LST file
450  *                                      to translate for the RST file
451  *              char    ib[NINPUT]      REL file text line
452  *              int     pass            linker pass number
453  *              int     pflag           print linker command file flag
454  *              FILE    *rfp            The file handle to the current
455  *                                      output RST file
456  *              FILE    *sfp            The file handle sfp points to the
457  *                                      currently open file
458  *              FILE *  stdin           c_library
459  *              FILE *  stdout          c_library
460  *              FILE    *tfp            The file handle to the current
461  *                                      LST file being scanned
462  *              int     uflag           update listing flag
463  *
464  *      called functions:
465  *              FILE *  afile()         lkmain.c
466  *              int     fclose()        c_library
467  *              char *  fgets()         c_library
468  *              int     fprintf()       c_library
469  *              VOID    lkulist()       lklist.c
470  *              VOID    lkexit()        lkmain.c
471  *              int     strlen()        c_library
472  *
473  *      side effects:
474  *              The input stream is scanned.  The .rel files will be
475  *              opened and closed sequentially scanning each in turn.
476  */
477
478 int
479 getline()
480 {
481         register int ftype;
482         register char *fid;
483
484 loop:   if (pflag && cfp && cfp->f_type == F_STD)
485                 fprintf(stdout, "ASlink >> ");
486
487         if (sfp == NULL || fgets(ib, sizeof ib, sfp) == NULL) {
488                 if (sfp) {
489                         fclose(sfp);
490                         sfp = NULL;
491                         lkulist(0);
492                 }
493                 if (cfp == NULL) {
494                         cfp = filep;
495                 } else {
496                         cfp = cfp->f_flp;
497                 }
498                 if (cfp) {
499                         ftype = cfp->f_type;
500                         fid = cfp->f_idp;
501                         if (ftype == F_STD) {
502                                 sfp = stdin;
503                         } else
504                         if (ftype == F_LNK) {
505                                 sfp = afile(fid, "lnk", 0);
506                         } else
507                         if (ftype == F_REL) {
508                                 sfp = afile(fid, "rel", 0);
509                                 /* if a .cdb file exists then copy it over */
510                                 if (dflag && sfp && dfp && pass == 0) {
511                                     FILE *xfp = afile(fid,"adb",0); //JCF: Nov 30, 2002
512                                     if (xfp) {
513                                         copyfile(dfp,xfp);
514                                         fclose(xfp);
515                                     }
516                                 }
517                                 if (uflag && pass != 0) {
518                                  SaveLinkedFilePath(fid); //Save the linked path for aomf51
519                                  if ((tfp = afile(fid, "lst", 0)) != NULL) {
520                                   if ((rfp = afile(fid, "rst", 1)) == NULL) {
521                                         fclose(tfp);
522                                         tfp = NULL;
523                                   }
524                                  }
525                                 }
526                                 gline = 1;
527                         } else {
528                                 fprintf(stderr, "Invalid file type\n");
529                                 lkexit(1);
530                         }
531                         if (sfp == NULL) {
532                                 lkexit(1);
533                         }
534                         goto loop;
535                 } else {
536                         filep = NULL;
537                         return(0);
538                 }
539         }
540         chop_crlf(ib);
541         return (1);
542 }
543
544 /*)Function     int     more()
545  *
546  *      The function more() scans the input text line
547  *      skipping white space (SPACES and TABS) and returns a (0)
548  *      if the end of the line or a comment delimeter (;) is found,
549  *      or a (1) if their are additional characters in the line.
550  *
551  *      local variables:
552  *              int     c               next character from
553  *                                      the input text line
554  *
555  *      global variables:
556  *              none
557  *
558  *      called functions:
559  *              char    getnb()         lklex.c
560  *              VOID    unget()         lklex.c
561  *
562  *      side effects:
563  *              use of getnb() and unget() updates the global pointer ip
564  *              the position in the current input text line
565  */
566
567 int
568 more()
569 {
570         register int c;
571
572         c = getnb();
573         unget(c);
574         return( (c == '\0' || c == ';') ? 0 : 1 );
575 }
576
577 /*)Function     char    endline()
578  *
579  *      The function endline() scans the input text line
580  *      skipping white space (SPACES and TABS) and returns the next
581  *      character or a (0) if the end of the line is found or a
582  *      comment delimiter (;) is found.
583  *
584  *      local variables:
585  *              int     c               next character from
586  *                                      the input text line
587  *
588  *      global variables:
589  *              none
590  *
591  *      called functions:
592  *              char    getnb()         lklex.c
593  *
594  *      side effects:
595  *              Use of getnb() updates the global pointer ip the
596  *              position in the current input text line.
597  */
598
599 char
600 endline()
601 {
602         register int c;
603
604         c = getnb();
605         return( (c == '\0' || c == ';') ? 0 : c );
606 }
607
608 /*)Function     VOID    chop_crlf(str)
609  *
610  *              char    *str            string to chop
611  *
612  *      The function chop_crlf() removes trailing LF or CR/LF from
613  *      str, if present.
614  *
615  *      local variables:
616  *              int     i               string length
617  *
618  *      global variables:
619  *              none
620  *
621  *      functions called:
622  *              none
623  *
624  *      side effects:
625  *              none
626  */
627
628 VOID
629 chop_crlf(str)
630 char *str;
631 {
632         register int i;
633
634         i = strlen(str);
635         if (i >= 1 && str[i-1] == '\n') str[i-1] = 0;
636         if (i >= 2 && str[i-2] == '\r') str[i-2] = 0;
637 }