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