4 * (C) Copyright 1989-1995
11 * 28-Oct-97 JLH bug in getst(): sign extend on ~(SPACE|ILL)
12 * causes infinite loop
16 * Extensions: P. Felber, M. Hope
26 * The module aslex.c includes the general lexical
27 * analysis routines for the assembler.
29 * aslex.c contains the following functions:
40 * aslex.c contains no local/static variables
43 /*)Function VOID getid(id,c)
45 * char * id a pointer to a string of
48 * >=0 this is first character to
49 * copy to the string buffer
50 * <0 skip white space, first
51 * character must be a LETTER
53 * The function getid() scans the current assembler-source text line
54 * from the current position copying the next LETTER | DIGIT string
55 * into the external string buffer (id). The string ends when a non
56 * LETTER or DIGIT character is found. The maximum number of
57 * characters copied is NCPS. If the input string is larger than
58 * NCPS characters then the string is truncated, if the input string
59 * is shorter than NCPS characters then the string is NULL filled.
60 * If the mode argument (c) is >=0 then (c) is the first character
61 * copied to the string buffer, if (c) is <0 then intervening white
62 * space (SPACES and TABS) are skipped and the first character found
63 * must be a LETTER else a 'q' error terminates the parse of this
64 * assembler-source text line.
67 * char * p pointer to external string buffer
68 * int c current character value
71 * char ctype[] a character array which defines the
72 * type of character being processed.
73 * This index is the character
78 * char getnb() aslex.c
79 * VOID unget() aslex.c
82 * use of getnb(), get(), and unget() updates the
83 * global pointer ip, the position in the current
84 * assembler-source text line.
96 if ((ctype[c] & LETTER) == 0)
103 } while (ctype[c=get()] & (LETTER|DIGIT));
105 while (p < &id[NCPS])
109 /*)Function VOID getst(id,c)
111 * char * id a pointer to a string of
112 * maximum length NCPS
114 * >=0 this is first character to
115 * copy to the string buffer
116 * <0 skip white space, first
117 * character must be a LETTER
119 * The function getnbid() scans the current assembler-source text line
120 * from the current position copying the next character string into
121 * the external string buffer (id). The string ends when a SPACE or
122 * ILL character is found. The maximum number of
123 * characters copied is NCPS. If the input string is larger than
124 * NCPS characters then the string is truncated, if the input string
125 * is shorter than NCPS characters then the string is NULL filled.
126 * If the mode argument (c) is >=0 then (c) is the first character
127 * copied to the string buffer, if (c) is <0 then intervening white
128 * space (SPACES and TABS) are skipped and the first character found
129 * must be a LETTER else a 'q' error terminates the parse of this
130 * assembler-source text line.
133 * char * p pointer to external string buffer
134 * int c current character value
137 * char ctype[] a character array which defines the
138 * type of character being processed.
139 * This index is the character
144 * char getnb() aslex.c
145 * VOID unget() aslex.c
148 * use of getnb(), get(), and unget() updates the
149 * global pointer ip, the position in the current
150 * assembler-source text line.
162 if ((ctype[c] & LETTER) == 0)
169 } while (ctype[c=get()] & (0xFF - (SPACE|ILL)));
171 while (p < &id[NCPS])
175 /*)Function char getnb()
177 * The function getnb() scans the current assembler-source
178 * text line returning the first character not a SPACE or TAB.
181 * int c current character from
182 * assembler-source text line
191 * use of get() updates the global pointer ip, the position
192 * in the current assembler-source text line
200 while ((c=get()) == ' ' || c == '\t')
205 /*)Function char get()
207 * The function get() returns the next character in the
208 * assembler-source text line, at the end of the line a
209 * NULL character is returned.
212 * int c current character from
213 * assembler-source text line
216 * char * ip pointer into the current
217 * assembler-source text line
223 * updates ip to the next character position in the
224 * assembler-source text line. If ip is at the end of the
225 * line, ip is not updated.
238 /*)Function VOID unget(c)
240 * int c value of last character read from
241 * assembler-source text line
243 * If (c) is not a NULL character then the global pointer ip
244 * is updated to point to the preceeding character in the
245 * assembler-source text line.
247 * NOTE: This function does not push the character (c)
248 * back into the assembler-source text line, only
249 * the pointer ip is changed.
252 * int c last character read from
253 * assembler-source text line
256 * char * ip position into the current
257 * assembler-source text line
263 * ip decremented by 1 character position
274 /*)Function int getmap(d)
276 * int d value to compare with the
277 * assembler-source text line character
279 * The function getmap() converts the 'C' style characters \b, \f,
280 * \n, \r, and \t to their equivalent ascii values and also
281 * converts 'C' style octal constants '\123' to their equivalent
282 * numeric values. If the first character is equivalent to (d) then
283 * a (-1) is returned, if the end of the line is detected then
284 * a 'q' error terminates the parse for this line, or if the first
285 * character is not a \ then the character value is returned.
288 * int c value of character from the
289 * assembler-source text line
290 * int n looping counter
291 * int v current value of numeric conversion
300 * use of get() updates the global pointer ip the position
301 * in the current assembler-source text line
307 register int c, n, v;
309 if ((c=get()) == '\0')
347 while (++n<=3 && c>='0' && c<='7') {
348 v = (v<<3) + c - '0';
359 /*)Function char * readlin()
361 * char * str poinetr to beginning of the buffer
362 * size_t n size of the buffer
363 * FILE * infp input file pointer
365 * The function readlin() reads a line from a file to the buffer s.
366 * The buffer length is n. If the line is truncterd to n -1 caharecters
367 * if it is longer the n - 1 characters.
368 * Trailing LF or CR/LF are removed from str, if present.
371 * int c character read from the file
372 * char *s buffer pointer
373 * satic char eof_f EOF flag
383 readlin(char *str, size_t n, FILE *infp)
387 static char eof_f = 0;
400 while (--n && (c = getc(infp)) != '\n' && c != EOF)
404 c = ' '; /* initialize it to something for the caharacter eating step */
407 if (s > str && *(s - 1) == '\r')
410 /* terminate the line */
413 /* eat characters until the end of line */
414 while (c != '\n' && c != EOF)
417 /* if the EOF is not at the beginning of the line, return the line;
418 return NULL at the next call of redlin */
430 /*)Function int as_getline()
432 * The function as_getline() reads a line of assembler-source text
433 * from an assembly source text file or an include file.
434 * Lines of text are processed from assembler-source files until
435 * all files have been read. If an include file is opened then
436 * lines of text are read from the include file (or nested
437 * include file) until the end of the include file is found.
438 * The input text line is copied into the global string ib[]
439 * and converted to a NULL terminated string. The function
440 * as_getline() returns a (1) after succesfully reading a line
441 * or a (0) if all files have been read.
444 * int i string length
447 * char ib[] string buffer containing
448 * assembler-source text line
449 * char ifp[] array of file handles for
451 * int incfil index for ifp[] specifies
452 * active include file
453 * int incline[] array of include file
455 * char sfp[] array of file handles for
456 * assembler source files
457 * int cfile index for sfp[] specifies
459 * int srcline[] array of source file
461 * int inpfil maximum input file index
464 * int fclose() c-library
465 * char * fgets() c-library
466 * int strlen() c-library
469 * include file will be closed at detection of end of file.
470 * the next sequential source file may be selected.
471 * the global file indexes incfil or cfile may be changed.
472 * The respective source line or include line counter
479 loop: if (incfil >= 0) {
480 if (readlin(ib, sizeof ib, ifp[incfil]) == NULL) {
482 ifp[incfil--] = NULL;
489 if (readlin(ib, sizeof ib, sfp[cfile]) == NULL) {
490 if (++cfile <= inpfil) {
502 /*)Function int more()
504 * The function more() scans the assembler-source text line
505 * skipping white space (SPACES and TABS) and returns a (0)
506 * if the end of the line or a comment delimeter (;) is found,
507 * or a (1) if their are additional characters in the line.
510 * int c next character from the
511 * assembler-source text line
517 * char getnb() aslex.c
518 * VOID unget() aslex.c
521 * use of getnb() and unget() updates the global pointer ip
522 * the position in the current assembler-source text line
532 return( (c == '\0' || c == ';') ? 0 : 1 );
535 /*)Function char endline()
537 * The function endline() scans the assembler-source text line
538 * skipping white space (SPACES and TABS) and returns the next
539 * character or a (0) if the end of the line is found or a
540 * comment delimiter (;) is found.
543 * int c next character from the
544 * assembler-source text line
550 * char getnb() aslex.c
553 * use of getnb() updates the global pointer ip the
554 * position in the current assembler-source text line
563 return( (c == '\0' || c == ';') ? 0 : c );