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