4f6c45759ece25aef59b08760b5e27c20d8784e5
[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
164  *      from the current position copying the next string
165  *      into the external string buffer (str).  The string ends when a
166  *      non SPACE type character is found. The maximum number of
167  *      characters copied is PATH_MAX. If the input string is larger than
168  *      PATH_MAX characters then the string is truncated, if the input string
169  *      is shorter than PATH_MAX characters then the string is NULL filled.
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                 if (p < &str[PATH_MAX-1])
199                         *p++ = c;
200                 c = get();
201         } while (c && ((ctype[c] != SPACE)||(c == ':')||(c == '\\')));
202         while (p < &str[PATH_MAX])
203                 *p++ = 0;
204 }
205
206 /*)Function     char    getnb()
207  *
208  *      The function getnb() scans the current input text
209  *      line returning the first character not a SPACE or TAB.
210  *
211  *      local variables:
212  *              int     c               current character from input
213  *
214  *      global variables:
215  *              none
216  *
217  *      called functions:
218  *              char    get()           lklex.c
219  *
220  *      side effects:
221  *              use of get() updates the global pointer ip, the position
222  *              in the current input text line
223  */
224
225 char
226 getnb()
227 {
228         register int c;
229
230         while ((c=get())==' ' || c=='\t')
231                 ;
232         return (c);
233 }
234
235 /*)Function     VOID    skip()
236  *
237  *      The function skip() scans the input text skipping all
238  *      letters and digits.
239  *
240  *      local variables:
241  *              none
242  *
243  *      global variables:
244  *              char    ctype[]         array of character types, one per
245  *                                      ASCII character
246  *              
247  *      functions called:
248  *              char    get()           lklex.c
249  *              char    getnb()         lklex.c
250  *              VOID    unget()         lklex.c
251  *
252  *      side effects:
253  *              Input letters and digits are skipped.
254  */
255
256 VOID
257 skip(c)
258 register int c;
259 {
260         if (c < 0)
261                 c = getnb();
262         while (ctype[c=get()] & (LETTER|DIGIT)) { ; }
263         unget(c);
264 }
265
266 /*)Function     char    get()
267  *
268  *      The function get() returns the next character in the
269  *      input text line, at the end of the line a
270  *      NULL character is returned.
271  *
272  *      local variables:
273  *              int     c               current character from
274  *                                      input text line
275  *
276  *      global variables:
277  *              char *  ip              pointer into the current
278  *                                      input text line
279  *
280  *      called functions:
281  *              none
282  *
283  *      side effects:
284  *              updates ip to the next character position in the
285  *              input text line.  If ip is at the end of the
286  *              line, ip is not updated.
287  */
288
289 char
290 get()
291 {
292         register int c;
293
294         if ((c = *ip) != 0)
295                 ++ip;
296         return (c);
297 }
298
299 /*)Function     VOID    unget(c)
300  *
301  *              int     c               value of last character
302  *                                      read from input text line
303  *
304  *      If (c) is not a NULL character then the global pointer ip
305  *      is updated to point to the preceeding character in the
306  *      input text line.
307  *
308  *      NOTE:   This function does not push the character (c)
309  *              back into the input text line, only
310  *              the pointer ip is changed.
311  *
312  *      local variables:
313  *              int     c               last character read
314  *                                      from input text line
315  *
316  *      global variables:
317  *              char *  ip              position into the current
318  *                                      input text line
319  *
320  *      called functions:
321  *              none
322  *
323  *      side effects:
324  *              ip decremented by 1 character position
325  */
326
327 VOID
328 unget(c)
329 {
330         if (c != 0)
331                 --ip;
332 }
333
334 /*)Function     int     getmap(d)
335  *
336  *              int     d               value to compare with the
337  *                                      input text line character
338  *
339  *      The function getmap() converts the 'C' style characters \b, \f,
340  *      \n, \r, and \t to their equivalent ascii values and also
341  *      converts 'C' style octal constants '\123' to their equivalent
342  *      numeric values.  If the first character is equivalent to (d) then
343  *      a (-1) is returned, if the end of the line is detected then
344  *      a 'q' error terminates the parse for this line, or if the first
345  *      character is not a \ then the character value is returned.
346  *
347  *      local variables:
348  *              int     c               value of character
349  *                                      from input text line
350  *              int     n               looping counter
351  *              int     v               current value of numeric conversion
352  *
353  *      global variables:
354  *              none
355  *
356  *      called functions:
357  *              char    get()           lklex.c
358  *              VOID    unget()         lklex.c
359  *
360  *      side effects:
361  *              use of get() updates the global pointer ip the position
362  *              in the current input text line
363  */
364
365 int
366 getmap(d)
367 {
368         register int c, n, v;
369
370         if ((c = get()) == '\0')
371                 return (-1);
372         if (c == d)
373                 return (-1);
374         if (c == '\\') {
375                 c = get();
376                 switch (c) {
377
378                 case 'b':
379                         c = '\b';
380                         break;
381
382                 case 'f':
383                         c = '\f';
384                         break;
385
386                 case 'n':
387                         c = '\n';
388                         break;
389
390                 case 'r':
391                         c = '\r';
392                         break;
393
394                 case 't':
395                         c = '\t';
396                         break;
397
398                 case '0':
399                 case '1':
400                 case '2':
401                 case '3':
402                 case '4':
403                 case '5':
404                 case '6':
405                 case '7':
406                         n = 0;
407                         v = 0;
408                         while (++n<=3 && c>='0' && c<='7') {
409                                 v = (v<<3) + c - '0';
410                                 c = get();
411                         }
412                         unget(c);
413                         c = v;
414                         break;
415                 }
416         }
417         return (c);
418 }
419
420 /*)Function     int     getline()
421  *
422  *      The function getline() reads a line of input text from a
423  *      .rel source text file, a .lnk command file or from stdin.
424  *      Lines of text are processed from a single .lnk file or
425  *      multiple .rel files until all files have been read.
426  *      The input text line is copied into the global string ib[]
427  *      and converted to a NULL terminated string.  The function
428  *      getline() returns a (1) after succesfully reading a line
429  *      or a (0) if all files have been read.
430  *      This function also opens each input .lst file and output
431  *      .rst file as each .rel file is processed.
432  *
433  *      local variables:
434  *              int     i               string length
435  *              int     ftype           file type
436  *              char *  fid             file name
437  *
438  *      global variables:
439  *              lfile   *cfp            The pointer *cfp points to the
440  *                                      current lfile structure
441  *              lfile   *filep          The pointer *filep points to the
442  *                                      beginning of a linked list of
443  *                                      lfile structures.
444  *              int     gline           get a line from the LST file
445  *                                      to translate for the RST file
446  *              char    ib[NINPUT]      REL file text line
447  *              int     pass            linker pass number
448  *              int     pflag           print linker command file flag
449  *              FILE    *rfp            The file handle to the current
450  *                                      output RST file
451  *              FILE    *sfp            The file handle sfp points to the
452  *                                      currently open file
453  *              FILE *  stdin           c_library
454  *              FILE *  stdout          c_library
455  *              FILE    *tfp            The file handle to the current
456  *                                      LST file being scanned
457  *              int     uflag           update listing flag
458  *
459  *      called functions:
460  *              FILE *  afile()         lkmain.c
461  *              int     fclose()        c_library
462  *              char *  fgets()         c_library
463  *              int     fprintf()       c_library
464  *              VOID    lkulist()       lklist.c
465  *              VOID    lkexit()        lkmain.c
466  *              int     strlen()        c_library
467  *
468  *      side effects:
469  *              The input stream is scanned.  The .rel files will be
470  *              opened and closed sequentially scanning each in turn.
471  */
472
473 int
474 getline()
475 {
476         register int ftype;
477         register char *fid;
478
479 loop:   if (pflag && cfp && cfp->f_type == F_STD)
480                 fprintf(stdout, "ASlink >> ");
481
482         if (sfp == NULL || fgets(ib, sizeof ib, sfp) == NULL) {
483                 if (sfp) {
484                         fclose(sfp);
485                         sfp = NULL;
486                         lkulist(0);
487                 }
488                 if (cfp == NULL) {
489                         cfp = filep;
490                 } else {
491                         cfp = cfp->f_flp;
492                 }
493                 if (cfp) {
494                         ftype = cfp->f_type;
495                         fid = cfp->f_idp;
496                         if (ftype == F_STD) {
497                                 sfp = stdin;
498                         } else
499                         if (ftype == F_LNK) {
500                                 sfp = afile(fid, "lnk", 0);
501                         } else
502                         if (ftype == F_REL) {
503                                 sfp = afile(fid, "rel", 0);
504                                 /* if a .cdb file exists then copy it over */
505                                 if (dflag && sfp && dfp && pass == 0) {
506                                     FILE *xfp = afile(fid,"cdb",0);
507                                     if (xfp) {
508                                         copyfile(dfp,xfp);
509                                         fclose(xfp);
510                                     }
511                                 }
512                                 if (uflag && pass != 0) {
513                                  if ((tfp = afile(fid, "lst", 0)) != NULL) {
514                                   if ((rfp = afile(fid, "rst", 1)) == NULL) {
515                                         fclose(tfp);
516                                         tfp = NULL;
517                                   }
518                                  }
519                                 }
520                                 gline = 1;
521                         } else {
522                                 fprintf(stderr, "Invalid file type\n");
523                                 lkexit(1);
524                         }
525                         if (sfp == NULL) {
526                                 lkexit(1);
527                         }
528                         goto loop;
529                 } else {
530                         filep = NULL;
531                         return(0);
532                 }
533         }
534         chop_crlf(ib);
535         return (1);
536 }
537
538 /*)Function     int     more()
539  *
540  *      The function more() scans the input text line
541  *      skipping white space (SPACES and TABS) and returns a (0)
542  *      if the end of the line or a comment delimeter (;) is found,
543  *      or a (1) if their are additional characters in the line.
544  *
545  *      local variables:
546  *              int     c               next character from
547  *                                      the input text line
548  *
549  *      global variables:
550  *              none
551  *
552  *      called functions:
553  *              char    getnb()         lklex.c
554  *              VOID    unget()         lklex.c
555  *
556  *      side effects:
557  *              use of getnb() and unget() updates the global pointer ip
558  *              the position in the current input text line
559  */
560
561 int
562 more()
563 {
564         register int c;
565
566         c = getnb();
567         unget(c);
568         return( (c == '\0' || c == ';') ? 0 : 1 );
569 }
570
571 /*)Function     char    endline()
572  *
573  *      The function endline() scans the input text line
574  *      skipping white space (SPACES and TABS) and returns the next
575  *      character or a (0) if the end of the line is found or a
576  *      comment delimiter (;) is found.
577  *
578  *      local variables:
579  *              int     c               next character from
580  *                                      the input text line
581  *
582  *      global variables:
583  *              none
584  *
585  *      called functions:
586  *              char    getnb()         lklex.c
587  *
588  *      side effects:
589  *              Use of getnb() updates the global pointer ip the
590  *              position in the current input text line.
591  */
592
593 char
594 endline()
595 {
596         register int c;
597
598         c = getnb();
599         return( (c == '\0' || c == ';') ? 0 : c );
600 }
601
602 /*)Function     VOID    chop_crlf(str)
603  *
604  *              char    *str            string to chop
605  *
606  *      The function chop_crlf() removes trailing LF or CR/LF from
607  *      str, if present.
608  *
609  *      local variables:
610  *              int     i               string length
611  *
612  *      global variables:
613  *              none
614  *
615  *      functions called:
616  *              none
617  *
618  *      side effects:
619  *              none
620  */
621
622 VOID
623 chop_crlf(str)
624 char *str;
625 {
626         register int i;
627
628         i = strlen(str);
629         if (i >= 1 && str[i-1] == '\n') str[i-1] = 0;
630         if (i >= 2 && str[i-2] == '\r') str[i-2] = 0;
631 }