]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
43866e37 | 6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
1c79356b | 7 | * |
43866e37 A |
8 | * This file contains Original Code and/or Modifications of Original Code |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
14 | * | |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
43866e37 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
1c79356b A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* | |
26 | * @OSF_COPYRIGHT@ | |
27 | */ | |
28 | /* | |
29 | * HISTORY | |
30 | * | |
31 | * Revision 1.1.1.1 1998/09/22 21:05:48 wsanchez | |
32 | * Import of Mac OS X kernel (~semeria) | |
33 | * | |
34 | * Revision 1.1.1.1 1998/03/07 02:26:09 wsanchez | |
35 | * Import of OSF Mach kernel (~mburg) | |
36 | * | |
37 | * Revision 1.1.11.3 1996/01/09 19:15:49 devrcs | |
38 | * Change 'register foo' to 'register int foo'. | |
39 | * [1995/12/01 21:42:12 jfraser] | |
40 | * | |
41 | * Merged '64-bit safe' changes from DEC alpha port. | |
42 | * [1995/11/21 18:03:11 jfraser] | |
43 | * | |
44 | * Revision 1.1.11.2 1995/01/06 19:10:21 devrcs | |
45 | * mk6 CR668 - 1.3b26 merge | |
46 | * * Revision 1.1.4.6 1994/05/06 18:39:20 tmt | |
47 | * Merged osc1.3dec/shared with osc1.3b19 | |
48 | * Merge Alpha changes into osc1.312b source code. | |
49 | * String protos. | |
50 | * 64bit cleanup. | |
51 | * Cleanup to quiet gcc warnings. | |
52 | * * End1.3merge | |
53 | * [1994/11/04 08:49:35 dwm] | |
54 | * | |
55 | * Revision 1.1.11.1 1994/09/23 01:19:59 ezf | |
56 | * change marker to not FREE | |
57 | * [1994/09/22 21:10:14 ezf] | |
58 | * | |
59 | * Revision 1.1.4.4 1993/08/11 20:37:55 elliston | |
60 | * Add ANSI Prototypes. CR #9523. | |
61 | * [1993/08/11 03:33:26 elliston] | |
62 | * | |
63 | * Revision 1.1.4.3 1993/07/27 18:27:38 elliston | |
64 | * Add ANSI prototypes. CR #9523. | |
65 | * [1993/07/27 18:12:13 elliston] | |
66 | * | |
67 | * Revision 1.1.4.2 1993/06/02 23:11:27 jeffc | |
68 | * Added to OSF/1 R1.3 from NMK15.0. | |
69 | * [1993/06/02 20:56:32 jeffc] | |
70 | * | |
71 | * Revision 1.1 1992/09/30 02:01:10 robert | |
72 | * Initial revision | |
73 | * | |
74 | * $EndLog$ | |
75 | */ | |
76 | /* CMU_HIST */ | |
77 | /* | |
78 | * Revision 2.5 91/10/09 16:00:20 af | |
79 | * Revision 2.4.3.1 91/10/05 13:06:25 jeffreyh | |
80 | * Added relational operator tokens and string constant etc. | |
81 | * Added input switching functions for macro and conditional command. | |
82 | * Moved skip_to_eol() from db_command.c and added db_last_lp to print | |
83 | * skipped input data as a warning message. | |
84 | * Added last input repetition support to db_read_line. | |
85 | * Changed db_lex() to always set db_tok_string for error message. | |
86 | * [91/08/29 tak] | |
87 | * | |
88 | * Revision 2.4.3.1 91/10/05 13:06:25 jeffreyh | |
89 | * Added relational operator tokens and string constant etc. | |
90 | * Added input switching functions for macro and conditional command. | |
91 | * Moved skip_to_eol() from db_command.c and added db_last_lp to print | |
92 | * skipped input data as a warning message. | |
93 | * Added last input repetition support to db_read_line. | |
94 | * Changed db_lex() to always set db_tok_string for error message. | |
95 | * [91/08/29 tak] | |
96 | * | |
97 | * Revision 2.4 91/05/14 15:34:23 mrt | |
98 | * Correcting copyright | |
99 | * | |
100 | * Revision 2.3 91/02/05 17:06:36 mrt | |
101 | * Changed to new Mach copyright | |
102 | * [91/01/31 16:18:20 mrt] | |
103 | * | |
104 | * Revision 2.2 90/08/27 21:51:10 dbg | |
105 | * Add 'dotdot' token. | |
106 | * [90/08/22 dbg] | |
107 | * | |
108 | * Allow backslash to quote any character into an identifier. | |
109 | * Allow colon in identifier for symbol table qualification. | |
110 | * [90/08/16 dbg] | |
111 | * Reduce lint. | |
112 | * [90/08/07 dbg] | |
113 | * Created. | |
114 | * [90/07/25 dbg] | |
115 | * | |
116 | */ | |
117 | /* CMU_ENDHIST */ | |
118 | /* | |
119 | * Mach Operating System | |
120 | * Copyright (c) 1991,1990 Carnegie Mellon University | |
121 | * All Rights Reserved. | |
122 | * | |
123 | * Permission to use, copy, modify and distribute this software and its | |
124 | * documentation is hereby granted, provided that both the copyright | |
125 | * notice and this permission notice appear in all copies of the | |
126 | * software, derivative works or modified versions, and any portions | |
127 | * thereof, and that both notices appear in supporting documentation. | |
128 | * | |
129 | * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" | |
130 | * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR | |
131 | * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
132 | * | |
133 | * Carnegie Mellon requests users of this software to return to | |
134 | * | |
135 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
136 | * School of Computer Science | |
137 | * Carnegie Mellon University | |
138 | * Pittsburgh PA 15213-3890 | |
139 | * | |
140 | * any improvements or extensions that they make and grant Carnegie Mellon | |
141 | * the rights to redistribute these changes. | |
142 | */ | |
143 | /* | |
144 | */ | |
145 | /* | |
146 | * Author: David B. Golub, Carnegie Mellon University | |
147 | * Date: 7/90 | |
148 | */ | |
149 | /* | |
150 | * Lexical analyzer. | |
151 | */ | |
152 | #include <string.h> /* For strcpy(), strncmp(), strlen() */ | |
153 | #include <ddb/db_lex.h> | |
154 | #include <ddb/db_command.h> | |
155 | #include <ddb/db_input.h> | |
156 | #include <ddb/db_output.h> /* For db_printf() */ | |
157 | ||
158 | char db_line[DB_LEX_LINE_SIZE]; | |
159 | char db_last_line[DB_LEX_LINE_SIZE]; | |
160 | char *db_lp, *db_endlp; | |
161 | char *db_last_lp; | |
162 | int db_look_char = 0; | |
163 | db_expr_t db_look_token = 0; | |
164 | ||
165 | ||
166 | /* Prototypes for functions local to this file. XXX -- should be static! | |
167 | */ | |
168 | void db_flush_line(void); | |
169 | void db_unread_char(int c); | |
170 | ||
171 | ||
172 | int | |
173 | db_read_line(char *repeat_last) | |
174 | { | |
175 | int i; | |
176 | ||
177 | i = db_readline(db_line, sizeof(db_line)); | |
178 | if (i == 0) | |
179 | return (0); /* EOI */ | |
180 | if (repeat_last) { | |
181 | if (strncmp(db_line, repeat_last, strlen(repeat_last)) == 0) { | |
182 | strcpy(db_line, db_last_line); | |
183 | db_printf("%s", db_line); | |
184 | i = strlen(db_line); | |
185 | } else if (db_line[0] != '\n' && db_line[0] != 0) | |
186 | strcpy(db_last_line, db_line); | |
187 | } | |
188 | db_lp = db_line; | |
189 | db_endlp = db_lp + i; | |
190 | db_last_lp = db_lp; | |
191 | db_look_char = 0; | |
192 | db_look_token = 0; | |
193 | return (i); | |
194 | } | |
195 | ||
196 | void | |
197 | db_flush_line(void) | |
198 | { | |
199 | db_lp = db_line; | |
200 | db_last_lp = db_lp; | |
201 | db_endlp = db_line; | |
202 | } | |
203 | ||
204 | void | |
205 | db_switch_input( | |
206 | char *buffer, | |
207 | int size) | |
208 | { | |
209 | db_lp = buffer; | |
210 | db_last_lp = db_lp; | |
211 | db_endlp = buffer + size; | |
212 | db_look_char = 0; | |
213 | db_look_token = 0; | |
214 | } | |
215 | ||
216 | void | |
217 | db_save_lex_context(register struct db_lex_context *lp) | |
218 | { | |
219 | lp->l_ptr = db_lp; | |
220 | lp->l_eptr = db_endlp; | |
221 | lp->l_char = db_look_char; | |
222 | lp->l_token = db_look_token; | |
223 | } | |
224 | ||
225 | void | |
226 | db_restore_lex_context(register struct db_lex_context *lp) | |
227 | { | |
228 | db_lp = lp->l_ptr; | |
229 | db_last_lp = db_lp; | |
230 | db_endlp = lp->l_eptr; | |
231 | db_look_char = lp->l_char; | |
232 | db_look_token = lp->l_token; | |
233 | } | |
234 | ||
235 | int | |
236 | db_read_char(void) | |
237 | { | |
238 | int c; | |
239 | ||
240 | if (db_look_char != 0) { | |
241 | c = db_look_char; | |
242 | db_look_char = 0; | |
243 | } | |
244 | else if (db_lp >= db_endlp) | |
245 | c = -1; | |
246 | else | |
247 | c = *db_lp++; | |
248 | return (c); | |
249 | } | |
250 | ||
251 | void | |
252 | db_unread_char(int c) | |
253 | { | |
254 | db_look_char = c; | |
255 | } | |
256 | ||
257 | void | |
258 | db_unread_token(int t) | |
259 | { | |
260 | db_look_token = t; | |
261 | } | |
262 | ||
263 | int | |
264 | db_read_token(void) | |
265 | { | |
266 | int t; | |
267 | ||
268 | if (db_look_token) { | |
269 | t = db_look_token; | |
270 | db_look_token = 0; | |
271 | } | |
272 | else { | |
273 | db_last_lp = db_lp; | |
274 | if (db_look_char) | |
275 | db_last_lp--; | |
276 | t = db_lex(); | |
277 | } | |
278 | return (t); | |
279 | } | |
280 | ||
281 | db_expr_t db_tok_number; | |
282 | char db_tok_string[TOK_STRING_SIZE]; | |
283 | ||
284 | db_expr_t db_radix = 16; | |
285 | ||
286 | void | |
287 | db_flush_lex(void) | |
288 | { | |
289 | db_flush_line(); | |
290 | db_look_char = 0; | |
291 | db_look_token = 0; | |
292 | } | |
293 | ||
294 | #define DB_DISP_SKIP 40 /* number of chars to display skip */ | |
295 | ||
296 | void | |
297 | db_skip_to_eol(void) | |
298 | { | |
299 | register int skip; | |
300 | register int t; | |
301 | register int n; | |
302 | register char *p; | |
303 | ||
304 | t = db_read_token(); | |
305 | p = db_last_lp; | |
306 | for (skip = 0; t != tEOL && t != tSEMI_COLON && t != tEOF; skip++) | |
307 | t = db_read_token(); | |
308 | if (t == tSEMI_COLON) | |
309 | db_unread_token(t); | |
310 | if (skip != 0) { | |
311 | while (p < db_last_lp && (*p == ' ' || *p == '\t')) | |
312 | p++; | |
313 | db_printf("Warning: Skipped input data \""); | |
314 | for (n = 0; n < DB_DISP_SKIP && p < db_last_lp; n++) | |
315 | db_printf("%c", *p++); | |
316 | if (n >= DB_DISP_SKIP) | |
317 | db_printf("...."); | |
318 | db_printf("\"\n"); | |
319 | } | |
320 | } | |
321 | ||
322 | int | |
323 | db_lex(void) | |
324 | { | |
325 | register char *cp; | |
326 | register int c; | |
327 | ||
328 | c = db_read_char(); | |
329 | while (c <= ' ' || c > '~') { | |
330 | if (c == '\n' || c == -1) | |
331 | return (tEOL); | |
332 | c = db_read_char(); | |
333 | } | |
334 | ||
335 | cp = db_tok_string; | |
336 | *cp++ = c; | |
337 | ||
338 | if (c >= '0' && c <= '9') { | |
339 | /* number */ | |
340 | int r, digit; | |
341 | ||
342 | if (c > '0') | |
343 | r = db_radix; | |
344 | else { | |
345 | c = db_read_char(); | |
346 | if (c == 'O' || c == 'o') | |
347 | r = 8; | |
348 | else if (c == 'T' || c == 't') | |
349 | r = 10; | |
350 | else if (c == 'X' || c == 'x') | |
351 | r = 16; | |
352 | else { | |
353 | cp--; | |
354 | r = db_radix; | |
355 | db_unread_char(c); | |
356 | } | |
357 | c = db_read_char(); | |
358 | *cp++ = c; | |
359 | } | |
360 | db_tok_number = 0; | |
361 | for (;;) { | |
362 | if (c >= '0' && c <= ((r == 8) ? '7' : '9')) | |
363 | digit = c - '0'; | |
364 | else if (r == 16 && ((c >= 'A' && c <= 'F') || | |
365 | (c >= 'a' && c <= 'f'))) { | |
366 | if (c >= 'a') | |
367 | digit = c - 'a' + 10; | |
368 | else | |
369 | digit = c - 'A' + 10; | |
370 | } | |
371 | else | |
372 | break; | |
373 | db_tok_number = db_tok_number * r + digit; | |
374 | c = db_read_char(); | |
375 | if (cp < &db_tok_string[sizeof(db_tok_string)-1]) | |
376 | *cp++ = c; | |
377 | } | |
378 | cp[-1] = 0; | |
379 | if ((c >= '0' && c <= '9') || | |
380 | (c >= 'A' && c <= 'Z') || | |
381 | (c >= 'a' && c <= 'z') || | |
382 | (c == '_')) | |
383 | { | |
384 | db_printf("Bad character '%c' after number %s\n", | |
385 | c, db_tok_string); | |
386 | db_error(0); | |
387 | db_flush_lex(); | |
388 | return (tEOF); | |
389 | } | |
390 | db_unread_char(c); | |
391 | return (tNUMBER); | |
392 | } | |
393 | if ((c >= 'A' && c <= 'Z') || | |
394 | (c >= 'a' && c <= 'z') || | |
395 | c == '_' || c == '\\' || c == ':') | |
396 | { | |
397 | /* identifier */ | |
398 | if (c == '\\') { | |
399 | c = db_read_char(); | |
400 | if (c == '\n' || c == -1) | |
401 | db_error("Bad '\\' at the end of line\n"); | |
402 | cp[-1] = c; | |
403 | } | |
404 | while (1) { | |
405 | c = db_read_char(); | |
406 | if ((c >= 'A' && c <= 'Z') || | |
407 | (c >= 'a' && c <= 'z') || | |
408 | (c >= '0' && c <= '9') || | |
409 | c == '_' || c == '\\' || c == ':' || c == '.') | |
410 | { | |
411 | if (c == '\\') { | |
412 | c = db_read_char(); | |
413 | if (c == '\n' || c == -1) | |
414 | db_error("Bad '\\' at the end of line\n"); | |
415 | } | |
416 | *cp++ = c; | |
417 | if (cp == db_tok_string+sizeof(db_tok_string)) { | |
418 | db_error("String too long\n"); | |
419 | db_flush_lex(); | |
420 | return (tEOF); | |
421 | } | |
422 | continue; | |
423 | } | |
424 | else { | |
425 | *cp = '\0'; | |
426 | break; | |
427 | } | |
428 | } | |
429 | db_unread_char(c); | |
430 | return (tIDENT); | |
431 | } | |
432 | ||
433 | *cp = 0; | |
434 | switch (c) { | |
435 | case '+': | |
436 | return (tPLUS); | |
437 | case '-': | |
438 | return (tMINUS); | |
439 | case '.': | |
440 | c = db_read_char(); | |
441 | if (c == '.') { | |
442 | *cp++ = c; | |
443 | *cp = 0; | |
444 | return (tDOTDOT); | |
445 | } | |
446 | db_unread_char(c); | |
447 | return (tDOT); | |
448 | case '*': | |
449 | return (tSTAR); | |
450 | case '/': | |
451 | return (tSLASH); | |
452 | case '=': | |
453 | c = db_read_char(); | |
454 | if (c == '=') { | |
455 | *cp++ = c; | |
456 | *cp = 0; | |
457 | return(tLOG_EQ); | |
458 | } | |
459 | db_unread_char(c); | |
460 | return (tEQ); | |
461 | case '%': | |
462 | return (tPCT); | |
463 | case '#': | |
464 | return (tHASH); | |
465 | case '(': | |
466 | return (tLPAREN); | |
467 | case ')': | |
468 | return (tRPAREN); | |
469 | case ',': | |
470 | return (tCOMMA); | |
471 | case '\'': | |
472 | return (tQUOTE); | |
473 | case '"': | |
474 | /* string */ | |
475 | cp = db_tok_string; | |
476 | c = db_read_char(); | |
477 | while (c != '"' && c > 0 && c != '\n') { | |
478 | if (cp >= &db_tok_string[sizeof(db_tok_string)-1]) { | |
479 | db_error("Too long string\n"); | |
480 | db_flush_lex(); | |
481 | return (tEOF); | |
482 | } | |
483 | if (c == '\\') { | |
484 | c = db_read_char(); | |
485 | switch(c) { | |
486 | case 'n': | |
487 | c = '\n'; break; | |
488 | case 't': | |
489 | c = '\t'; break; | |
490 | case '\\': | |
491 | case '"': | |
492 | break; | |
493 | default: | |
494 | db_printf("Bad escape sequence '\\%c'\n", c); | |
495 | db_error(0); | |
496 | db_flush_lex(); | |
497 | return (tEOF); | |
498 | } | |
499 | } | |
500 | *cp++ = c; | |
501 | c = db_read_char(); | |
502 | } | |
503 | *cp = 0; | |
504 | if (c != '"') { | |
505 | db_error("Non terminated string constant\n"); | |
506 | db_flush_lex(); | |
507 | return (tEOF); | |
508 | } | |
509 | return (tSTRING); | |
510 | case '$': | |
511 | return (tDOLLAR); | |
512 | case '!': | |
513 | c = db_read_char(); | |
514 | if (c == '=') { | |
515 | *cp++ = c; | |
516 | *cp = 0; | |
517 | return(tLOG_NOT_EQ); | |
518 | } | |
519 | db_unread_char(c); | |
520 | return (tEXCL); | |
521 | case '&': | |
522 | c = db_read_char(); | |
523 | if (c == '&') { | |
524 | *cp++ = c; | |
525 | *cp = 0; | |
526 | return(tLOG_AND); | |
527 | } | |
528 | db_unread_char(c); | |
529 | return(tBIT_AND); | |
530 | case '|': | |
531 | c = db_read_char(); | |
532 | if (c == '|') { | |
533 | *cp++ = c; | |
534 | *cp = 0; | |
535 | return(tLOG_OR); | |
536 | } | |
537 | db_unread_char(c); | |
538 | return(tBIT_OR); | |
539 | case '<': | |
540 | c = db_read_char(); | |
541 | *cp++ = c; | |
542 | *cp = 0; | |
543 | if (c == '<') | |
544 | return (tSHIFT_L); | |
545 | if (c == '=') | |
546 | return (tLESS_EQ); | |
547 | cp[-1] = 0; | |
548 | db_unread_char(c); | |
549 | return(tLESS); | |
550 | break; | |
551 | case '>': | |
552 | c = db_read_char(); | |
553 | *cp++ = c; | |
554 | *cp = 0; | |
555 | if (c == '>') | |
556 | return (tSHIFT_R); | |
557 | if (c == '=') | |
558 | return (tGREATER_EQ); | |
559 | cp[-1] = 0; | |
560 | db_unread_char(c); | |
561 | return (tGREATER); | |
562 | break; | |
563 | case ';': | |
564 | return (tSEMI_COLON); | |
565 | case '?': | |
566 | return (tQUESTION); | |
567 | case -1: | |
568 | strcpy(db_tok_string, "<EOL>"); | |
569 | return (tEOF); | |
570 | } | |
571 | db_printf("Bad character '%c'\n", c); | |
572 | db_flush_lex(); | |
573 | return (tEOF); | |
574 | } |