Debian Codegraph @

libidu/scanners.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* scanners.c -- file & directory name manipulations
2 Copyright (C) 1986-2020 Free Software Foundation, Inc.
3 Written by Greg McGary <[email protected]>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <fcntl.h>
23 #include <getopt.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <xalloc.h>
29 #include <error.h>
30 #include <sys/stat.h>
31
32 #include "xnls.h"
33 #include "scanners.h"
34 #include "tokflags.h"
35 #include "iduglobal.h"
36
37 static struct obstack lang_args_obstack;
38 struct lang_args *lang_args_default = 0;
39 struct lang_args *lang_args_list = 0;
40 struct obstack tokens_obstack;
41 size_t log_8_member_files = 0;
42
43 extern void usage (void) __attribute__((__noreturn__));
44 extern char *program_name;
45
46 /****************************************************************************/
47
48 static struct lang_args **parse_language_map_file (char const *file_name,
49 struct lang_args **next_ptr);
50 static char *read_language_map_file (char const *file_name);
51 static void tokenize_args_string (char *args_string, int *argcp, char ***argvp);
52
53 static struct token *get_token_c (FILE *in_FILE, void const *args, int *flags);
54 static void *parse_args_c (char **argv, int argc);
55 static void help_me_c (void);
56 static void help_me_cpp (void);
57 static void help_me_java (void);
58
59 static struct token *get_token_asm (FILE *in_FILE, void const *args, int *flags);
60 static void *parse_args_asm (char **argv, int argc);
61 static void help_me_asm (void);
62
63 static struct token *get_token_text (FILE *in_FILE, void const *args, int *flags);
64 static void *parse_args_text (char **argv, int argc);
65 static void help_me_text (void);
66
67 static struct token *get_token_perl (FILE *in_FILE, void const *args, int *flags);
68 static void *parse_args_perl (char **argv, int argc);
69 static void help_me_perl (void);
70
71 static struct token *get_token_lisp (FILE *in_FILE, void const *args, int *flags);
72 static void *parse_args_lisp (char **argv, int argc);
73 static void help_me_lisp (void);
74
75 static struct language languages_0[] =
76 {
77 { "C", parse_args_c, get_token_c, help_me_c },
78 { "C++", parse_args_c, get_token_c, help_me_cpp },
79 { "Java", parse_args_c, get_token_c, help_me_java },
80 { "asm", parse_args_asm, get_token_asm, help_me_asm },
81 { "text", parse_args_text, get_token_text, help_me_text },
82 { "perl", parse_args_perl, get_token_perl, help_me_perl },
83 { "lisp", parse_args_lisp, get_token_lisp, help_me_lisp }
84 };
85 static struct language const *languages_N
86 = &languages_0[cardinalityof (languages_0)];
87
88 void
89 language_help_me (void)
90 {
91 const struct language *lang;
92 for (lang = languages_0; lang < languages_N; lang++)
93 {
94 putchar ('\n');
95 (*lang->lg_help_me) ();
96 }
97 }
98
99 void
100 language_save_arg (char *arg)
101 {
102 char *lang_name = strsep (&arg, ":");
103 struct language *lang = get_language (lang_name);
104
105 if (lang == NULL)
106 {
107 error (0, 0, _("unrecognized language: `%s'"), lang_name);
108 usage ();
109 }
•110 if (arg == NULL)
111 {
112 error (0, 0, _("missing language OPT for `%s'"), lang_name);
113 usage ();
114 }
115 if (lang->lg_argc == 0)
116 lang->lg_argv[lang->lg_argc++] = program_name;
117 lang->lg_argv[lang->lg_argc++] = arg;
118 }
119
120 void
121 language_getopt (void)
122 {
123 const struct language *lang;
124 for (lang = languages_0; lang < languages_N; lang++)
125 if (lang->lg_argc)
126 lang->lg_parse_args ((char**)(lang->lg_argv), lang->lg_argc);
127 }
128
129 struct language *
130 get_language (char const *lang_name)
131 {
132 const struct language *lang;
133 for (lang = languages_0; lang < languages_N; lang++)
134 if (strequ (lang_name, lang->lg_name))
135 {
136 DEBUG (("lang=%s", lang_name));
137 return (struct language *) lang;
138 }
139 DEBUG (("!lang=%s", lang_name));
140 return 0;
141 }
142
143 /****************************************************************************/
144
145 static int lang_args_index = 0;
146
147 void
148 set_default_language (char const *lang_name)
149 {
150 struct lang_args *new_args;
151 const struct language *lang;
152
153 obstack_init (&lang_args_obstack);
154
155 new_args = obstack_alloc (&lang_args_obstack, sizeof *new_args);
156
157 new_args->la_pattern = obstack_copy0 (&lang_args_obstack, lang_name, strlen(lang_name)+1);
158 new_args->la_args_string = 0;
159 new_args->la_next = 0;
160 lang = new_args->la_language = get_language (lang_name);
161 new_args->la_args_digested = (lang
162 ? lang->lg_parse_args (&new_args->la_args_string, 0)
163 : 0);
164 lang_args_default = new_args;
165 DEBUG ((", <default>\n"));
166 }
167
168 void
169 parse_language_map (char const *file_name)
170 {
171 if (obstack_init (&lang_args_obstack) == 0)
172 error (EXIT_FAILURE, 0, _("can't allocate language args obstack: memory exhausted"));
173 if (file_name == 0)
174 file_name = LANGUAGE_MAP_FILE;
175 parse_language_map_file (file_name, &lang_args_list);
176 }
177
178 static struct lang_args **
179 parse_language_map_file (char const *file_name, struct lang_args **next_ptr)
180 {
181 static char white_space[] = " \t\r\n\v\f";
182 static char horizontal_space[] = " \t";
183 static char vertical_space[] = "\r\n\v\f";
184 char *lang_map_buffer;
185 char *lmp;
186
187 lmp = lang_map_buffer = read_language_map_file (file_name);
188 for (;;)
189 {
190 struct lang_args *new_args;
191 struct language const *lang;
192 int pattern_size;
193 char *lang_name;
194 int space;
195
196 /* Skip leading white space and full-line comments */
197 while (*lmp)
198 {
199 lmp += strspn (lmp, white_space);
200 if (*lmp != '#')
201 break;
202 lmp += strcspn (lmp, vertical_space);
203 }
204 if (*lmp == '\0')
205 break;
206
207 pattern_size = strcspn (lmp, white_space);
208 if (pattern_size == 3 && strnequ (lmp, "***", 3))
209 {
210 lmp += pattern_size;
211 lmp += strspn (lmp, horizontal_space);
212 if (isspace (*lmp))
213 next_ptr = parse_language_map_file (LANGUAGE_MAP_FILE, next_ptr);
214 else
215 {
216 char *end = lmp + strcspn (lmp, white_space);
217 *end = '\0';
218 next_ptr = parse_language_map_file (lmp, next_ptr);
219 lmp = end + 1;
220 }
221 continue;
222 }
223
224 new_args = obstack_alloc (&lang_args_obstack, sizeof *new_args);
225 if (new_args == 0)
226 error (EXIT_FAILURE, 0, _("can't allocate language args: memory exhausted"));
227 new_args->la_pattern = obstack_copy0 (&lang_args_obstack, lmp, pattern_size);
228 new_args->la_args_string = 0;
229 new_args->la_next = 0;
230 lmp += pattern_size;
231 lmp += strspn (lmp, horizontal_space);
232 if (isspace (*lmp))
233 {
234 error (0, 0, _("language name expected following `%s' in file `%s'"),
235 new_args->la_pattern, file_name);
236 obstack_free (&lang_args_obstack, new_args);
237 continue;
238 }
239 lang_name = lmp;
240 lmp += strcspn (lmp, white_space);
241 space = *lmp;
242 *lmp++ = '\0';
243 lmp += strspn (lmp, horizontal_space);
244 lang = new_args->la_language = get_language (lang_name);
245
246 if (*lmp == '#')
247 lmp += strcspn (lmp, vertical_space);
248 else if (!isspace (*lmp) && (space == ' ' || space == '\t'))
249 {
250 int args_size = strcspn (lmp, vertical_space);
251 new_args->la_args_string = obstack_copy0 (&lang_args_obstack, lmp, args_size);
252 lmp += args_size;
253 }
254 new_args->la_args_digested = (lang
255 ? lang->lg_parse_args (&new_args->la_args_string, 0)
256 : 0);
257 if (pattern_size == 2 && strnequ (new_args->la_pattern, "**", 2))
258 {
259 if (lang_args_default)
260 {
261 obstack_free (&lang_args_obstack, new_args);
262 continue;
263 }
264 lang_args_default = new_args;
265 DEBUG ((", <default>"));
266 }
267 else
268 {
269 new_args->la_index = lang_args_index++;
270 *next_ptr = new_args;
271 next_ptr = &new_args->la_next;
272 }
273 DEBUG ((", pat=%s\n", new_args->la_pattern));
274 }
275 free (lang_map_buffer);
276 return next_ptr;
277 }
278
279 static char *
280 read_language_map_file (char const *file_name)
281 {
282 int map_fd;
283 char *lang_map_buffer;
284 struct stat st;
285 int bytes;
286
287 map_fd = open (file_name, O_RDONLY);
288 if (map_fd < 0)
289 error (EXIT_FAILURE, errno, _("can't open language map file `%s'"), file_name);
290 if (fstat (map_fd, &st) < 0)
291 error (EXIT_FAILURE, errno, _("can't get size of map file `%s'"), file_name);
292
293 lang_map_buffer = xmalloc (sizeof(char) * (st.st_size + 2));
294 if (lang_map_buffer == 0)
295 error (EXIT_FAILURE, 0, _("can't allocate language args: memory exhausted"));
296 lang_map_buffer[st.st_size] = '\n';
297 lang_map_buffer[st.st_size+1] = '\0';
298
299 bytes = read (map_fd, lang_map_buffer, st.st_size);
300 if (bytes < 0)
301 error (EXIT_FAILURE, errno, _("can't read language map file `%s'"), file_name);
302 /* FIXME: handle interrupted & partial reads */
303 if (bytes != st.st_size)
304 error (EXIT_FAILURE, errno, _("can't read entire language map file `%s'"), file_name);
305
306 close (map_fd);
307 return lang_map_buffer;
308 }
309
310 /****************************************************************************/
311
312 static void
313 tokenize_args_string (char *args_string, int *argcp, char ***argvp)
314 {
315 static char horizontal_space[] = " \t";
316 char **argv_0 = xmalloc (sizeof(char *) * strlen (args_string) / 2);
317 char **argv = argv_0;
318 char *arg;
319
320 *argv++ = program_name;
321 arg = strsep (&args_string, horizontal_space);
322 while (arg)
323 {
324 *argv++ = arg;
325 arg = strsep (&args_string, horizontal_space);
326 }
327 *argcp = argv - argv_0;
328 *argvp = xnrealloc (argv_0, *argcp, sizeof **argvp);
329 }
330
331 static void
332 set_ushort_ctype (unsigned short *ctype, char const *chars, int type)
333 {
334 unsigned short *rct = &ctype[1];
335 unsigned char const *uc = (unsigned char const *) chars;
336
337 while (*uc)
338 rct[*uc++] |= type;
339 }
340
341 static void
342 clear_ushort_ctype (unsigned short *ctype, char const *chars, int type)
343 {
344 unsigned short *rct = &ctype[1];
345 unsigned char const *uc = (unsigned char const *) chars;
346
347 while (*uc)
348 rct[*uc++] &= ~type;
349 }
350
351 static void
352 set_uchar_ctype (unsigned char *ctype, char const *chars, int type)
353 {
354 unsigned char *rct;
355 unsigned char const *uc;
356
357 rct = &ctype[1];
358 uc = (unsigned char const *) chars;
359
360 while (*uc)
361 rct[*uc++] |= type;
362 }
363
364 static void
365 clear_uchar_ctype (unsigned char *ctype, char const *chars, int type)
366 {
367 unsigned char *rct;
368 unsigned char const *uc;
369
370 rct = &ctype[1];
371 uc = (unsigned char const *) chars;
372
373 while (*uc)
374 rct[*uc++] &= ~type;
375 }
376
377 /*************** C, C++ & Java ***********************************************/
378
379 #define I1 0x0001 /* 1st char of an identifier [a-zA-Z_] */
380 #define DG 0x0002 /* decimal digit [0-9] */
381 #define NM 0x0004 /* extra chars in a hex or long number [a-fA-FxXlLuU.] */
382 #define C1 0x0008 /* C comment introduction char: / */
383 #define C2 0x0010 /* C comment termination char: * */
384 #define Q1 0x0020 /* single quote: ' */
385 #define Q2 0x0040 /* double quote: " */
386 #define ES 0x0080 /* escape char: \ */
387 #define NL 0x0100 /* newline: \n */
388 #define EF 0x0200 /* EOF */
389 #define SK 0x0400 /* Make these chars valid for names within strings */
390 #define VH 0x0800 /* VHIL comment introduction char: # */
391
392 /* character class membership macros: */
393
394 #define ISDIGIT(c) ((rct)[c] & (DG)) /* digit */
395 #define ISNUMBER(c) ((rct)[c] & (DG|NM)) /* legal in a number */
396 #define ISEOF(c) ((rct)[c] & (EF)) /* EOF */
397 #define ISID1ST(c) ((rct)[c] & (I1)) /* 1st char of an identifier */
398 #define ISIDREST(c) ((rct)[c] & (I1|DG)) /* rest of an identifier */
399 #define ISSTRKEEP(c) ((rct)[c] & (I1|DG|SK)) /* keep contents of string */
400
401 /* The `BORING' classes should be skipped over until something
402 interesting comes along... */
403
404 #define ISBORING(c) (!((rct)[c] & (EF|NL|I1|DG|Q1|Q2|C1|VH))) /* fluff */
405 #define ISCBORING(c) (!((rct)[c] & (EF|C2))) /* comment fluff */
406 #define ISCCBORING(c) (!((rct)[c] & (EF|NL))) /* C++ // comment fluff */
407 #define ISQ1BORING(c) (!((rct)[c] & (EF|NL|Q1|ES))) /* char const fluff */
408 #define ISQ2BORING(c) (!((rct)[c] & (EF|NL|Q2|ES))) /* quoted str fluff */
409
410 static unsigned short ctype_c[257] =
411 {
412 EF,
413 /* 0 1 2 3 4 5 6 7 */
414 /* ----- ----- ----- ----- ----- ----- ----- ----- */
415 /*000*/ 0, 0, 0, 0, 0, 0, 0, 0,
416 /*010*/ 0, 0, NL, 0, 0, 0, 0, 0,
417 /*020*/ 0, 0, 0, 0, 0, 0, 0, 0,
418 /*030*/ 0, 0, 0, 0, 0, 0, 0, 0,
419 /*040*/ 0, 0, Q2, 0, 0, 0, 0, Q1,
420 /*050*/ 0, 0, C2, 0, 0, 0, NM, C1,
421 /*060*/ DG, DG, DG, DG, DG, DG, DG, DG,
422 /*070*/ DG, DG, 0, 0, 0, 0, 0, 0,
423 /*100*/ 0, I1|NM, I1|NM, I1|NM, I1|NM, I1|NM, I1|NM, I1,
424 /*110*/ I1, I1, I1, I1, I1|NM, I1, I1, I1,
425 /*120*/ I1, I1, I1, I1, I1, I1|NM, I1, I1,
426 /*130*/ I1|NM, I1, I1, 0, ES, 0, 0, I1,
427 /*140*/ 0, I1|NM, I1|NM, I1|NM, I1|NM, I1|NM, I1|NM, I1,
428 /*150*/ I1, I1, I1, I1, I1|NM, I1, I1, I1,
429 /*160*/ I1, I1, I1, I1, I1, I1|NM, I1, I1,
430 /*170*/ I1|NM, I1, I1, 0, 0, 0, 0, 0,
431 /* FIXME: latin-1 */
432 };
433
434 struct args_c
435 {
436 int strip_underscore;
437 unsigned short *ctype;
438 };
439
440 static struct args_c args_c = { 0, ctype_c };
441
442 static struct option const long_options_c[] =
443 {
444 { "keep", required_argument, 0, 'k' },
445 { "ignore", required_argument, 0, 'i' },
446 { "strip-underscore", no_argument, 0, 'u' },
447 {NULL, 0, NULL, 0}
448 };
449
450 static void
451 help_me_c (void)
452 {
453 printf (_("\
454 C language:\n\
455 -k,--keep=CHARS Allow CHARS in single-token strings, keep the result\n\
456 -i,--ignore=CHARS Allow CHARS in single-token strings, toss the result\n\
457 -u,--strip-underscore Strip a leading underscore from single-token strings\n\
458 "));
459 }
460
461 static void
462 help_me_cpp (void)
463 {
464 printf (_("\
465 C++ language:\n\
466 -k,--keep=CHARS Allow CHARS in single-token strings, keep the result\n\
467 -i,--ignore=CHARS Allow CHARS in single-token strings, toss the result\n\
468 -u,--strip-underscore Strip a leading underscore from single-token strings\n\
469 "));
470 }
471
472 static void
473 help_me_java (void)
474 {
475 printf (_("\
476 Java language:\n\
477 -k,--keep=CHARS Allow CHARS in single-token strings, keep the result\n\
478 -i,--ignore=CHARS Allow CHARS in single-token strings, toss the result\n\
479 -u,--strip-underscore Strip a leading underscore from single-token strings\n\
480 "));
481 }
482
483 static void *
484 parse_args_c (char **argv, int argc)
485 {
486 char *tmp_string = 0;
487 struct args_c *args;
488
489 if (argv == 0 || *argv == 0)
490 return &args_c;
491
492 if (argc)
493 args = &args_c;
494 else
495 {
496 tmp_string = xstrdup (*argv);
497 tokenize_args_string (tmp_string, &argc, &argv);
498 args = xmalloc (sizeof *args);
499 args->strip_underscore = 0;
500 args->ctype = ctype_c;
501 }
502
503 optind = 0;
504 for (;;)
505 {
506 int optc = getopt_long (argc, argv, "k:i:u",
507 long_options_c, (int *) 0);
508 if (optc < 0)
509 break;
510 if ((optc == 'k' || optc == 'i') && args->ctype == ctype_c)
511 args->ctype = CLONE (ctype_c, unsigned short, cardinalityof (ctype_c));
512 switch (optc)
513 {
514 case 'k':
515 set_ushort_ctype (args->ctype, optarg, SK);
516 break;
517
518 case 'i':
519 clear_ushort_ctype (args->ctype, optarg, SK);
520 break;
521
522 case 'u':
523 args->strip_underscore = 1;
524 break;
525
526 default:
527 usage ();
528 }
529 }
530 if (tmp_string)
531 {
532 free (argv);
533 free (tmp_string);
534 }
535 return args;
536 }
537
538 unsigned char *scanner_buffer;
539
540 #define SCAN_CPP_DIRECTIVE \
541 do \
542 { \
543 new_line = 0; \
544 /* Cope with leading whitespace before CPP lines */ \
545 while (c == ' ' || c == '\t') \
546 c = getc (in_FILE); \
547 if (c == '\n') \
548 { \
549 new_line = 1; \
550 goto top; \
551 } \
552 else if (c != '#') \
553 goto next; \
554 c = getc (in_FILE); \
555 while (ISBORING (c)) \
556 c = getc (in_FILE); \
557 if (!ISID1ST (c)) \
558 goto next; \
559 id = scanner_buffer; \
560 *id++ = c; \
561 while (ISIDREST (c = getc (in_FILE))) \
562 *id++ = c; \
563 *id = '\0'; \
564 if (strequ (scanner_buffer, "include")) \
565 { \
566 while (c == ' ' || c == '\t') \
567 c = getc (in_FILE); \
568 if (c == '\n') \
569 { \
570 new_line = 1; \
571 goto top; \
572 } \
573 id = scanner_buffer; \
574 if (c == '"') \
575 { \
576 c = getc (in_FILE); \
577 while (c != '\n' && c != '"' && c != EOF) \
578 { \
579 *id++ = c; \
580 c = getc (in_FILE); \
581 } \
582 *flags = TOK_STRING; \
583 } \
584 else if (c == '<') \
585 { \
586 c = getc (in_FILE); \
587 while (c != '\n' && c != '>' && c != EOF) \
588 { \
589 *id++ = c; \
590 c = getc (in_FILE); \
591 } \
592 *flags = TOK_STRING; \
593 } \
594 else if (ISID1ST (c)) \
595 { \
596 *id++ = c; \
597 while (ISIDREST (c = getc (in_FILE))) \
598 *id++ = c; \
599 *flags = TOK_NAME; \
600 } \
601 else \
602 { \
603 while (c != '\n' && c != EOF) \
604 c = getc (in_FILE); \
605 new_line = 1; \
606 goto top; \
607 } \
608 while (c != '\n' && c != EOF) \
609 c = getc (in_FILE); \
610 new_line = 1; \
611 obstack_grow0 (&tokens_obstack, scanner_buffer, \
612 id - scanner_buffer); \
613 return (struct token *) obstack_finish (&tokens_obstack); \
614 } \
615 if (strnequ (scanner_buffer, "if", 2) \
616 || strequ (scanner_buffer, "define") \
617 || strequ (scanner_buffer, "elif") /* ansi C */ \
618 || strequ (scanner_buffer, "undef")) \
619 goto next; \
620 while ((c != '\n') && (c != EOF)) \
621 c = getc (in_FILE); \
622 new_line = 1; \
623 goto top; \
624 } while (0)
625
626
627 /* Grab the next identifier from the C source file. This state
628 machine is built for speed, not elegance. */
629
630 static struct token *
631 get_token_c (FILE *in_FILE, void const *args, int *flags)
632 {
633 #define ARGS ((struct args_c const *) args)
634 static int new_line = 1;
635 unsigned short const *rct = &ARGS->ctype[1];
636 unsigned char *id = scanner_buffer;
637 int c; int d;
638
639 obstack_blank (&tokens_obstack, OFFSETOF_TOKEN_NAME);
640
641 top:
642 c = getc (in_FILE);
643 if (new_line)
644 SCAN_CPP_DIRECTIVE;
645
646 next:
647 while (ISBORING (c))
648 c = getc (in_FILE);
649
650 switch (c)
651 {
652 case '"':
653 id = scanner_buffer;
654 *id++ = c = getc (in_FILE);
655 for (;;)
656 {
657 while (ISQ2BORING (c))
658 *id++ = c = getc (in_FILE);
659 if (c == '\\')
660 {
661 *id++ = c = getc (in_FILE);
662 continue;
663 }
664 else if (c != '"')
665 goto next;
666 break;
667 }
668 *--id = '\0';
669 id = scanner_buffer;
670 while (ISSTRKEEP (d = *id))
671 id++;
672 if (*id || id == scanner_buffer)
673 {
674 c = getc (in_FILE);
675 goto next;
676 }
677 *flags = TOK_STRING;
678 if (ARGS->strip_underscore && scanner_buffer[0] == '_' && scanner_buffer[1])
679 obstack_grow0 (&tokens_obstack, scanner_buffer + 1, id - scanner_buffer - 1);
680 else
681 obstack_grow0 (&tokens_obstack, scanner_buffer, id - scanner_buffer);
682 return (struct token *) obstack_finish (&tokens_obstack);
683
684 case '\'':
685 c = getc (in_FILE);
686 for (;;)
687 {
688 while (ISQ1BORING (c))
689 c = getc (in_FILE);
690 if (c == '\\')
691 {
692 c = getc (in_FILE);
693 continue;
694 }
695 else if (c == '\'')
696 c = getc (in_FILE);
697 goto next;
698 }
699
700 case '/':
701 c = getc (in_FILE);
702 if (c == '/')
703 { /* Cope with C++ comment */
704 while (ISCCBORING (c))
705 c = getc (in_FILE);
706 new_line = 1;
707 goto top;
708 }
709 else if (c != '*')
710 goto next;
711 c = getc (in_FILE);
712 for (;;)
713 {
714 while (ISCBORING (c))
715 c = getc (in_FILE);
716 c = getc (in_FILE);
717 if (c == '/')
718 {
719 c = getc (in_FILE);
720 goto next;
721 }
722 else if (ISEOF (c))
723 {
724 new_line = 1;
725 obstack_free (&tokens_obstack, obstack_finish (&tokens_obstack));
726 return 0;
727 }
728 }
729
730 case '\n':
731 new_line = 1;
732 goto top;
733
734 default:
735 if (ISEOF (c))
736 {
737 new_line = 1;
738 obstack_free (&tokens_obstack, obstack_finish (&tokens_obstack));
739 return 0;
740 }
741 id = scanner_buffer;
742 *id++ = c;
743 if (ISID1ST (c))
744 {
745 *flags = TOK_NAME;
746 while (ISIDREST (c = getc (in_FILE)))
747 *id++ = c;
748 }
749 else if (ISDIGIT (c))
750 {
751 *flags = TOK_NUMBER;
752 while (ISNUMBER (c = getc (in_FILE)))
753 *id++ = c;
754 }
755 else
756 {
757 if (isprint (c))
758 fprintf (stderr, _("junk: `%c'"), c);
759 else
760 fprintf (stderr, _("junk: `\\%03o'"), (unsigned) c);
761 }
762 ungetc (c, in_FILE);
763 *flags |= TOK_LITERAL;
764 obstack_grow0 (&tokens_obstack, scanner_buffer, id - scanner_buffer);
765 return (struct token *) obstack_finish (&tokens_obstack);
766 }
767 #undef ARGS
768 }
769
770 #undef I1
771 #undef DG
772 #undef NM
773 #undef C1
774 #undef C2
775 #undef Q1
776 #undef Q2
777 #undef ES
778 #undef NL
779 #undef EF
780 #undef SK
781 #undef VH
782 #undef ISDIGIT
783 #undef ISNUMBER
784 #undef ISEOF
785 #undef ISID1ST
786 #undef ISIDREST
787 #undef ISSTRKEEP
788 #undef ISBORING
789 #undef ISCBORING
790 #undef ISCCBORING
791 #undef ISQ1BORING
792 #undef ISQ2BORING
793
794 /*************** Assembly ***************************************************/
795
796 #define I1 0x01 /* 1st char of an identifier [a-zA-Z_] */
797 #define NM 0x02 /* digit [0-9a-fA-FxX] */
798 #define NL 0x04 /* newline: \n */
799 #define CM 0x08 /* assembler comment char: usually # or | */
800 #define IG 0x10 /* ignore `identifiers' with these chars in them */
801 #define C1 0x20 /* C comment introduction char: / */
802 #define C2 0x40 /* C comment termination char: * */
803 #define EF 0x80 /* EOF */
804
805 /* Assembly Language character classes */
806 #define ISID1ST(c) ((rct)[c] & (I1))
807 #define ISIDREST(c) ((rct)[c] & (I1|NM))
808 #define ISNUMBER(c) ((rct)[c] & (NM))
809 #define ISEOF(c) ((rct)[c] & (EF))
810 #define ISCOMMENT(c) ((rct)[c] & (CM))
811 #define ISBORING(c) (!((rct)[c] & (EF|NL|I1|NM|CM|C1)))
812 #define ISCBORING(c) (!((rct)[c] & (EF|NL)))
813 #define ISCCBORING(c) (!((rct)[c] & (EF|C2)))
814 #define ISIGNORE(c) ((rct)[c] & (IG))
815
816 static unsigned char ctype_asm[257] =
817 {
818 EF,
819 /* 0 1 2 3 4 5 6 7 */
820 /* ----- ----- ----- ----- ----- ----- ----- ----- */
821 /*000*/ 0, 0, 0, 0, 0, 0, 0, 0,
822 /*010*/ 0, 0, NL, 0, 0, 0, 0, 0,
823 /*020*/ 0, 0, 0, 0, 0, 0, 0, 0,
824 /*030*/ 0, 0, 0, 0, 0, 0, 0, 0,
825 /*040*/ 0, 0, 0, 0, 0, 0, 0, 0,
826 /*050*/ 0, 0, C2, 0, 0, 0, 0, C1,
827 /*060*/ NM, NM, NM, NM, NM, NM, NM, NM,
828 /*070*/ NM, NM, 0, 0, 0, 0, 0, 0,
829 /*100*/ 0, I1|NM, I1|NM, I1|NM, I1|NM, I1|NM, I1|NM, I1,
830 /*110*/ I1, I1, I1, I1, I1|NM, I1, I1, I1,
831 /*120*/ I1, I1, I1, I1, I1, I1, I1, I1,
832 /*130*/ I1|NM, I1, I1, 0, 0, 0, 0, I1,
833 /*140*/ 0, I1|NM, I1|NM, I1|NM, I1|NM, I1|NM, I1|NM, I1,
834 /*150*/ I1, I1, I1, I1, I1|NM, I1, I1, I1,
835 /*160*/ I1, I1, I1, I1, I1, I1, I1, I1,
836 /*170*/ I1|NM, I1, I1, 0, 0, 0, 0, 0,
837
838 };
839
840 struct args_asm
841 {
842 int handle_cpp;
843 int strip_underscore;
844 unsigned char *ctype;
845 };
846
847 static struct args_asm args_asm = { 1, 0, ctype_asm };
848
849 static struct option const long_options_asm[] =
850 {
851 { "comment", required_argument, 0, 'c' },
852 { "keep", required_argument, 0, 'k' },
853 { "ignore", required_argument, 0, 'i' },
854 { "strip-underscore", no_argument, 0, 'u' },
855 { "no-cpp", no_argument, 0, 'n' },
856 {NULL, 0, NULL, 0}
857 };
858
859 static void
860 help_me_asm (void)
861 {
862 printf (_("\
863 Assembly language:\n\
864 -c,--comment=CHARS Any of CHARS starts a comment until end-of-line\n\
865 -k,--keep=CHARS Allow CHARS in tokens, and keep the result\n\
866 -i,--ignore=CHARS Allow CHARS in tokens, and toss the result\n\
867 -u,--strip-underscore Strip a leading underscore from tokens\n\
868 -n,--no-cpp Don't handle C pre-processor directives\n\
869 "));
870 }
871
872 static void *
873 parse_args_asm (char **argv, int argc)
874 {
875 char *tmp_string = 0;
876 struct args_asm *args;
877
878 if (argv == 0 || *argv == 0)
879 return &args_asm;
880
881 if (argc)
882 args = &args_asm;
883 else
884 {
885 tmp_string = xstrdup (*argv);
886 tokenize_args_string (tmp_string, &argc, &argv);
887 args = xmalloc (sizeof *args);
888 args->strip_underscore = 0;
889 args->ctype = ctype_asm;
890 args->handle_cpp = 1;
891 }
892
893 optind = 0;
894 for (;;)
895 {
896 int optc = getopt_long (argc, argv, "c:k:i:un",
897 long_options_asm, (int *) 0);
898 if (optc < 0)
899 break;
900 if ((optc == 'k' || optc == 'i' || optc == 'c')
901 && args->ctype == ctype_asm)
902 args->ctype = CLONE (ctype_asm, unsigned char, cardinalityof (ctype_asm));
903 switch (optc)
904 {
905 case 'c':
906 set_uchar_ctype (args->ctype, optarg, CM);
907 break;
908
909 case 'k':
910 set_uchar_ctype (args->ctype, optarg, I1);
911 break;
912
913 case 'i':
914 set_uchar_ctype (args->ctype, optarg, I1 | IG);
915 break;
916
917 case 'u':
918 args->strip_underscore = 1;
919 break;
920
921 case 'n':
922 args->handle_cpp = 0;
923 break;
924
925 default:
926 usage ();
927 }
928 }
929 if (tmp_string)
930 {
931 free (argv);
932 free (tmp_string);
933 }
934 return args;
935 }
936
937 /* Grab the next identifier the assembly language source file. This
938 state machine is built for speed, not elegance. */
939
940 static struct token *
941 get_token_asm (FILE *in_FILE, void const *args, int *flags)
942 {
943 #define ARGS ((struct args_asm const *) args)
944 static int new_line = 1;
945 unsigned char const *rct = &ARGS->ctype[1];
946 unsigned char *id = scanner_buffer;
947 int c, d;
948
949 obstack_blank (&tokens_obstack, OFFSETOF_TOKEN_NAME);
950
951 top:
952 c = getc (in_FILE);
953 if (ARGS->handle_cpp > 0 && new_line)
954 SCAN_CPP_DIRECTIVE;
955
956 next:
957 while (ISBORING (c))
958 c = getc (in_FILE);
959
960 if (ISCOMMENT (c))
961 {
962 while (ISCBORING (c))
963 c = getc (in_FILE);
964 new_line = 1;
965 }
966
967 if (ISEOF (c))
968 {
969 new_line = 1;
970 obstack_free (&tokens_obstack, obstack_finish (&tokens_obstack));
971 return 0;
972 }
973
974 if (c == '\n')
975 {
976 new_line = 1;
977 goto top;
978 }
979
980 if (c == '/')
981 {
982 if ((c = getc (in_FILE)) != '*')
983 goto next;
984 c = getc (in_FILE);
985 for (;;)
986 {
987 while (ISCCBORING (c))
988 c = getc (in_FILE);
989 c = getc (in_FILE);
990 if (c == '/')
991 {
992 c = getc (in_FILE);
993 break;
994 }
995 else if (ISEOF (c))
996 {
997 new_line = 1;
998 obstack_free (&tokens_obstack, obstack_finish (&tokens_obstack));
999 return 0;
1000 }
1001 }
1002 goto next;
1003 }
1004
1005 id = scanner_buffer;
1006 if (ARGS->strip_underscore && c == '_' && !ISID1ST (c = getc (in_FILE)))
1007 {
1008 obstack_grow0 (&tokens_obstack, "_", 1);
1009 return (struct token *) obstack_finish (&tokens_obstack);
1010 }
1011 *id++ = c;
1012 if (ISID1ST (c))
1013 {
1014 *flags = TOK_NAME;
1015 while (ISIDREST (c = getc (in_FILE)))
1016 *id++ = c;
1017 }
1018 else if (ISNUMBER (c))
1019 {
1020 *flags = TOK_NUMBER;
1021 while (ISNUMBER (c = getc (in_FILE)))
1022 *id++ = c;
1023 }
1024 else
1025 {
1026 if (isprint (c))
1027 fprintf (stderr, _("junk: `%c'"), c);
1028 else
1029 fprintf (stderr, _("junk: `\\%03o'"), (unsigned) c);
1030 goto next;
1031 }
1032
1033 *id = '\0';
1034 for (id = scanner_buffer; *id; id++)
1035 if (ISIGNORE (d = *id))
1036 goto next;
1037 ungetc (c, in_FILE);
1038 *flags |= TOK_LITERAL;
1039 obstack_grow0 (&tokens_obstack, scanner_buffer, id - scanner_buffer);
1040 return (struct token *) obstack_finish (&tokens_obstack);
1041 #undef ARGS
1042 }
1043
1044 #undef I1
1045 #undef NM
1046 #undef NL
1047 #undef CM
1048 #undef IG
1049 #undef C1
1050 #undef C2
1051 #undef EF
1052 #undef ISID1ST
1053 #undef ISIDREST
1054 #undef ISNUMBER
1055 #undef ISEOF
1056 #undef ISCOMMENT
1057 #undef ISBORING
1058 #undef ISCBORING
1059 #undef ISCCBORING
1060 #undef ISIGNORE
1061
1062 /*************** Text *******************************************************/
1063
1064 #define I1 0x01 /* 1st char of an identifier [a-zA-Z_] */
1065 #define NM 0x02 /* digit [0-9a-fA-FxX] */
1066 #define SQ 0x04 /* squeeze these out (.,',-) */
1067 #define EF 0x80 /* EOF */
1068
1069 /* Text character classes */
1070 #define ISID1ST(c) ((rct)[c] & (I1))
1071 #define ISIDREST(c) ((rct)[c] & (I1|NM|SQ))
1072 #define ISNUMBER(c) ((rct)[c] & (NM))
1073 #define ISEOF(c) ((rct)[c] & (EF))
1074 #define ISBORING(c) (!((rct)[c] & (I1|NM|EF)))
1075 #define ISIDSQUEEZE(c) ((rct)[c] & (SQ))
1076
1077 static unsigned char ctype_text[257] =
1078 {
1079 EF,
1080 /* 0 1 2 3 4 5 6 7 */
1081 /* ----- ----- ----- ----- ----- ----- ----- ----- */
1082 /*000*/ 0, 0, 0, 0, 0, 0, 0, 0,
1083 /*010*/ 0, 0, 0, 0, 0, 0, 0, 0,
1084 /*020*/ 0, 0, 0, 0, 0, 0, 0, 0,
1085 /*030*/ 0, 0, 0, 0, 0, 0, 0, 0,
1086 /*040*/ 0, 0, 0, 0, 0, 0, 0, 0,
1087 /*050*/ 0, 0, 0, 0, 0, 0, 0, 0,
1088 /*060*/ NM, NM, NM, NM, NM, NM, NM, NM,
1089 /*070*/ NM, NM, 0, 0, 0, 0, 0, 0,
1090 /*100*/ 0, I1|NM, I1|NM, I1|NM, I1|NM, I1|NM, I1|NM, I1,
1091 /*110*/ I1, I1, I1, I1, I1|NM, I1, I1, I1,
1092 /*120*/ I1, I1, I1, I1, I1, I1, I1, I1,
1093 /*130*/ I1|NM, I1, I1, 0, 0, 0, 0, I1,
1094 /*140*/ 0, I1|NM, I1|NM, I1|NM, I1|NM, I1|NM, I1|NM, I1,
1095 /*150*/ I1, I1, I1, I1, I1|NM, I1, I1, I1,
1096 /*160*/ I1, I1, I1, I1, I1, I1, I1, I1,
1097 /*170*/ I1|NM, I1, I1, 0, 0, 0, 0, 0,
1098 /*200*/ 0, 0, 0, 0, 0, 0, 0, 0,
1099 /*210*/ 0, 0, 0, 0, 0, 0, 0, 0,
1100 /*220*/ 0, 0, 0, 0, 0, 0, 0, 0,
1101 /*230*/ 0, 0, 0, 0, 0, 0, 0, 0,
1102 /*240*/ 0, 0, 0, 0, 0, 0, 0, 0,
1103 /*250*/ 0, 0, 0, 0, 0, 0, 0, 0,
1104 /*260*/ 0, 0, 0, 0, 0, 0, 0, 0,
1105 /*270*/ 0, 0, 0, 0, 0, 0, 0, 0,
1106 /*300*/ I1, I1, I1, I1, I1, I1, I1, I1,
1107 /*310*/ I1, I1, I1, I1, I1, I1, I1, I1,
1108 /*320*/ I1, I1, I1, I1, I1, I1, I1, 0,
1109 /*330*/ I1, I1, I1, I1, I1, I1, I1, I1,
1110 /*340*/ I1, I1, I1, I1, I1, I1, I1, I1,
1111 /*350*/ I1, I1, I1, I1, I1, I1, I1, I1,
1112 /*360*/ I1, I1, I1, I1, I1, I1, I1, 0,
1113 /*370*/ I1, I1, I1, I1, I1, I1, I1, I1,
1114 };
1115
1116 struct args_text
1117 {
1118 unsigned char *ctype;
1119 };
1120
1121 static struct args_text args_text = { ctype_text };
1122
1123 static struct option const long_options_text[] =
1124 {
1125 { "include", required_argument, 0, 'i' },
1126 { "exclude", required_argument, 0, 'x' },
1127 {NULL, 0, NULL, 0}
1128 };
1129
1130 static void
1131 help_me_text (void)
1132 {
1133 printf (_("\
1134 Text language:\n\
1135 -i,--include=CHAR-CLASS Treat characters of CHAR-CLASS as token constituents\n\
1136 -x,--exclude=CHAR-CLASS Treat characters of CHAR-CLASS as token delimiters\n\
1137 "));
1138 }
1139
1140 static void *
1141 parse_args_text (char **argv, int argc)
1142 {
1143 char *tmp_string = 0;
1144 struct args_text *args;
1145
1146 if (argv == 0 || *argv == 0)
1147 return &args_text;
1148
1149 if (argc)
1150 args = &args_text;
1151 else
1152 {
1153 tmp_string = xstrdup (*argv);
1154 tokenize_args_string (tmp_string, &argc, &argv);
1155 args = xmalloc (sizeof *args);
1156 args->ctype = ctype_text;
1157 }
1158
1159 optind = 0;
1160 for (;;)
1161 {
1162 int optc = getopt_long (argc, argv, "i:x:",
1163 long_options_text, (int *) 0);
1164 if (optc < 0)
1165 break;
1166 if ((optc == 'x' || optc == 'i') && args->ctype == ctype_text)
1167 args->ctype = CLONE (ctype_text, unsigned char, cardinalityof (ctype_text));
1168 switch (optc)
1169 {
1170 case 'i':
1171 set_uchar_ctype (args->ctype, optarg, I1);
1172 break;
1173
1174 case 'x':
1175 clear_uchar_ctype (args->ctype, optarg, I1);
1176 break;
1177
1178 default:
1179 usage ();
1180 }
1181 }
1182 if (tmp_string)
1183 {
1184 free (argv);
1185 free (tmp_string);
1186 }
1187 return args;
1188 }
1189
1190 /* Grab the next identifier the text source file. This state machine
1191 is built for speed, not elegance. */
1192
1193 static struct token *
1194 get_token_text (FILE *in_FILE, void const *args, int *flags)
1195 {
1196 #define ARGS ((struct args_text const *) args)
1197 unsigned char const *rct = &ARGS->ctype[1];
1198 int c;
1199 unsigned char *id = scanner_buffer;
1200
1201 obstack_blank (&tokens_obstack, OFFSETOF_TOKEN_NAME);
1202
1203 top:
1204 c = getc (in_FILE);
1205 while (ISBORING (c))
1206 c = getc (in_FILE);
1207 if (ISEOF (c))
1208 {
1209 obstack_free (&tokens_obstack, obstack_finish (&tokens_obstack));
1210 return 0;
1211 }
1212 id = scanner_buffer;
1213 *id++ = c;
1214 if (ISID1ST (c))
1215 {
1216 *flags = TOK_NAME;
1217 while (ISIDREST (c = getc (in_FILE)))
1218 if (!ISIDSQUEEZE (c))
1219 *id++ = c;
1220 }
1221 else if (ISNUMBER (c))
1222 {
1223 *flags = TOK_NUMBER;
1224 while (ISNUMBER (c = getc (in_FILE)))
1225 *id++ = c;
1226 }
1227 else
1228 {
1229 if (isprint (c))
1230 fprintf (stderr, _("junk: `%c'"), c);
1231 else
1232 fprintf (stderr, _("junk: `\\%03o'"), (unsigned) c);
1233 goto top;
1234 }
1235
1236 ungetc (c, in_FILE);
1237 *flags |= TOK_LITERAL;
1238 obstack_grow0 (&tokens_obstack, scanner_buffer, id - scanner_buffer);
1239 return (struct token *) obstack_finish (&tokens_obstack);
1240 #undef ARGS
1241 }
1242
1243 #undef I1
1244 #undef NM
1245 #undef SQ
1246 #undef EF
1247 #undef ISID1ST
1248 #undef ISIDREST
1249 #undef ISNUMBER
1250 #undef ISEOF
1251 #undef ISBORING
1252 #undef ISIDSQUEEZE
1253
1254 /*************** Perl *******************************************************/
1255
1256 #define I1 0x01 /* 1st char of an identifier [a-zA-Z_] */
1257 #define NM 0x02 /* digit [0-9a-fA-FxX] */
1258 #define SQ 0x04 /* squeeze these out (.,',-) */
1259 #define CM 0x08 /* Comment Character '#' */
1260 #define EQ 0x10 /* Start of Documentation Character '=' */
1261 #define Q1 0x20 /* Start of Single Quote ''' */
1262 #define Q2 0x40 /* Start of Double Quote '"' */
1263 #define EF 0x80 /* EOF */
1264 #define NL 0x100 /* '\n' character */
1265 #define ES 0x200 /* Escape '\\' character */
1266
1267 /* Text character classes */
1268 #define ISID1ST(c) ((rct)[c] & (I1))
1269 #define ISIDREST(c) ((rct)[c] & (I1|NM|SQ))
1270 #define ISNUMBER(c) ((rct)[c] & (NM))
1271 #define ISIDSQUEEZE(c) ((rct)[c] & (SQ))
1272 #define ISCOMMENT(c) ((rct)[c] & (CM))
1273 #define ISEQUALS(c) ((rct)[c] & (EQ))
1274 #define ISQUOTE1(c) ((rct)[c] & (Q1))
1275 #define ISQUOTE2(c) ((rct)[c] & (Q2))
1276 #define ISNEWLINE(c) ((rct)[c] & (NL))
1277 #define ISEOF(c) ((rct)[c] & (EF))
1278 #define ISBORING(c) (!((rct)[c] & (I1|NL|NM|CM|EQ|Q1|Q2|ES|EF)))
1279
1280 static unsigned short ctype_perl[257] =
1281 {
1282 EF,
1283 /* 0 1 2 3 4 5 6 7 */
1284 /* ----- ----- ----- ----- ----- ----- ----- ----- */
1285 /*000*/ 0, 0, 0, 0, 0, 0, 0, 0,
1286 /*010*/ 0, 0, NL, 0, 0, 0, 0, 0,
1287 /*020*/ 0, 0, 0, 0, 0, 0, 0, 0,
1288 /*030*/ 0, 0, 0, 0, 0, 0, 0, 0,
1289 /*040*/ 0, 0, Q2, CM, 0, 0, 0, Q1,
1290 /*050*/ 0, 0, 0, 0, 0, 0, 0, 0,
1291 /*060*/ NM, NM, NM, NM, NM, NM, NM, NM,
1292 /*070*/ NM, NM, 0, 0, 0, EQ, 0, 0,
1293 /*100*/ 0, I1|NM, I1|NM, I1|NM, I1|NM, I1|NM, I1|NM, I1,
1294 /*110*/ I1, I1, I1, I1, I1|NM, I1, I1, I1,
1295 /*120*/ I1, I1, I1, I1, I1, I1, I1, I1,
1296 /*130*/ I1|NM, I1, I1, 0, ES, 0, 0, I1,
1297 /*140*/ 0, I1|NM, I1|NM, I1|NM, I1|NM, I1|NM, I1|NM, I1,
1298 /*150*/ I1, I1, I1, I1, I1|NM, I1, I1, I1,
1299 /*160*/ I1, I1, I1, I1, I1, I1, I1, I1,
1300 /*170*/ I1|NM, I1, I1, 0, 0, 0, 0, 0,
1301 /*200*/ 0, 0, 0, 0, 0, 0, 0, 0,
1302 /*210*/ 0, 0, 0, 0, 0, 0, 0, 0,
1303 /*220*/ 0, 0, 0, 0, 0, 0, 0, 0,
1304 /*230*/ 0, 0, 0, 0, 0, 0, 0, 0,
1305 /*240*/ 0, 0, 0, 0, 0, 0, 0, 0,
1306 /*250*/ 0, 0, 0, 0, 0, 0, 0, 0,
1307 /*260*/ 0, 0, 0, 0, 0, 0, 0, 0,
1308 /*270*/ 0, 0, 0, 0, 0, 0, 0, 0,
1309 /*300*/ I1, I1, I1, I1, I1, I1, I1, I1,
1310 /*310*/ I1, I1, I1, I1, I1, I1, I1, I1,
1311 /*320*/ I1, I1, I1, I1, I1, I1, I1, 0,
1312 /*330*/ I1, I1, I1, I1, I1, I1, I1, I1,
1313 /*340*/ I1, I1, I1, I1, I1, I1, I1, I1,
1314 /*350*/ I1, I1, I1, I1, I1, I1, I1, I1,
1315 /*360*/ I1, I1, I1, I1, I1, I1, I1, 0,
1316 /*370*/ I1, I1, I1, I1, I1, I1, I1, I1,
1317 };
1318
1319 struct args_perl
1320 {
1321 int exclude_dtags;
1322 unsigned short *ctype;
1323 };
1324
1325 static struct args_perl args_perl = { 1, ctype_perl };
1326
1327 static struct option const long_options_perl[] =
1328 {
1329 { "include", required_argument, 0, 'i' },
1330 { "exclude", required_argument, 0, 'x' },
1331 { "dtags", no_argument, 0, 'd' },
1332 {NULL, 0, NULL, 0}
1333 };
1334
1335 static void
1336 help_me_perl (void)
1337 {
1338 printf (_("\
1339 Perl language:\n\
1340 -i,--include=CHAR-CLASS Treat characters of CHAR-CLASS as token constituents\n\
1341 -x,--exclude=CHAR-CLASS Treat characters of CHAR-CLASS as token delimiters\n\
1342 -d,--dtags Include documentation tags\n\
1343 "));
1344 }
1345
1346 static void *
1347 parse_args_perl (char **argv, int argc)
1348 {
1349 char *tmp_string = 0;
1350 struct args_perl *args;
1351
1352 if (argv == 0 || *argv == 0)
1353 return &args_perl;
1354
1355 if (argc)
1356 args = &args_perl;
1357 else
1358 {
1359 tmp_string = xstrdup (*argv);
1360 tokenize_args_string (tmp_string, &argc, &argv);
1361 args = xmalloc (sizeof(struct args_perl));
1362 args->exclude_dtags = 1;
1363 args->ctype = ctype_perl;
1364 }
1365
1366 optind = 0;
1367 for (;;)
1368 {
1369 int optc = getopt_long (argc, argv, "i:x:d",
1370 long_options_perl, (int *) 0);
1371 if (optc < 0)
1372 break;
1373 if ((optc == 'x' || optc == 'i') && args->ctype == ctype_perl)
1374 args->ctype = CLONE (ctype_perl, unsigned short, cardinalityof (ctype_perl));
1375 switch (optc)
1376 {
1377 case 'd':
1378 args->exclude_dtags = 0;
1379 break;
1380
1381 case 'i':
1382 set_ushort_ctype (args->ctype, optarg, I1);
1383 break;
1384
1385 case 'x':
1386 clear_ushort_ctype (args->ctype, optarg, I1);
1387 break;
1388
1389 default:
1390 usage ();
1391 }
1392 }
1393 if (tmp_string)
1394 {
1395 free (argv);
1396 free (tmp_string);
1397 }
1398 return args;
1399 }
1400
1401 /* Grab the next identifier the text source file. This state machine
1402 is built for speed, not elegance. */
1403
1404 static struct token *
1405 get_token_perl (FILE *in_FILE, void const *args, int *flags)
1406 {
1407 #define ARGS ((struct args_perl const *) args)
1408 static int new_line = 1;
1409 /* static char id_0[BUFSIZ]; */
1410 unsigned short const *rct = &ARGS->ctype[1];
1411 int c, state = 0, skip_doc = 0;
1412 /* int comment = 0, d_quote = 0, s_quote = 0, equals = 0; */
1413 unsigned char *id = scanner_buffer;
1414
1415 obstack_blank (&tokens_obstack, OFFSETOF_TOKEN_NAME);
1416
1417 top:
1418 c = getc (in_FILE);
1419 while (ISBORING (c))
1420 c = getc (in_FILE);
1421
1422 switch (c)
1423 {
1424 case '\n':
1425 state &= ~CM; /* comment = 0; */
1426 new_line = 1;
1427 goto top;
1428 break;
1429
1430 case '\\':
1431 c = getc (in_FILE); /* Skip next character */
1432 new_line = 0;
1433 goto top;
1434 break;
1435
1436 case '#':
1437 /* Skip the # if it is inside single quotes or double quotes */
1438 if (!(state & Q1) && !(state & Q2))
1439 state |= CM; /* comment = 1 */
1440 break;
1441
1442 case '\'':
1443 /* Skip the single quote if it is inside a comment or double quotes */
1444 if (!skip_doc)
1445 if (!(state & CM) && !(state & Q2))
1446 state ^= Q1; /* s_quote = ((s_quote) ? 0 : 1); */
1447 break;
1448
1449 case '\"':
1450 /* Skip the double quote if it is inside a comment or single quotes */
1451 if (!skip_doc)
1452 if (!(state & CM) && !(state & Q1))
1453 state ^= Q2; /* d_quote = ((d_quote == 1) ? 0 : 1); */
1454 break;
1455
1456 case '=':
1457 if (new_line && ARGS->exclude_dtags)
1458 {
1459 skip_doc = 1;
1460 state &= ~EQ; /* documentation = 0; */
1461 }
1462 break;
1463
1464 default:
1465 if (ISEOF (c))
1466 {
1467 new_line = 1;
1468 obstack_free (&tokens_obstack, obstack_finish (&tokens_obstack));
1469 return 0;
1470 }
1471 break;
1472 }
1473
1474 new_line = 0;
1475 if (ISCOMMENT(c) || ISQUOTE1(c) || ISQUOTE2(c) ||
1476 ISNEWLINE(c) || ISEQUALS(c) || state)
1477 goto top;
1478
1479 id = scanner_buffer;
1480 *id++ = c;
1481 if (ISID1ST (c))
1482 {
1483 *flags = TOK_NAME;
1484 while (ISIDREST (c = getc (in_FILE)))
1485 if (!ISIDSQUEEZE (c))
1486 *id++ = c;
1487 }
1488 else if (ISNUMBER (c))
1489 {
1490 *flags = TOK_NUMBER;
1491 while (ISNUMBER (c = getc (in_FILE)))
1492 *id++ = c;
1493
1494 ungetc (c, in_FILE);
1495 goto top; /* skip all numbers */
1496 }
1497 else
1498 {
1499 if (isprint (c))
1500 fprintf (stderr, _("junk: `%c'"), c);
1501 else
1502 fprintf (stderr, _("junk: `\\%03o'"), (unsigned) c);
1503 goto top;
1504 }
1505
1506 ungetc (c, in_FILE);
1507
1508 *id = '\0';
1509 if (skip_doc)
1510 {
1511 /* perl documentation 'perlpod' processing is done here */
1512
1513 if (strequ (scanner_buffer, "cut"))
1514 {
1515 skip_doc = 0;
1516 }
1517 else
1518 {
1519 state |= EQ; /* documentation = 1; */
1520 }
1521 goto top;
1522 }
1523
1524 if (strequ (scanner_buffer, "_"))
1525 {
1526 goto top;
1527 }
1528
1529 *flags |= TOK_LITERAL;
1530 obstack_grow0 (&tokens_obstack, scanner_buffer, id - scanner_buffer);
1531 return (struct token *) obstack_finish (&tokens_obstack);
1532 #undef ARGS
1533 }
1534
1535 #undef I1
1536 #undef NM
1537 #undef SQ
1538 #undef CM
1539 #undef EQ
1540 #undef Q1
1541 #undef Q2
1542 #undef EF
1543 #undef NL
1544 #undef ES
1545 #undef ISID1ST
1546 #undef ISIDREST
1547 #undef ISNUMBER
1548 #undef ISIDSQUEEZE
1549 #undef ISCOMMENT
1550 #undef ISEQUALS
1551 #undef ISQUOTE1
1552 #undef ISQUOTE2
1553 #undef ISNEWLINE
1554 #undef ISEOF
1555 #undef ISBORING
1556
1557 /*************** Lisp *******************************************************/
1558
1559 #define DG 0x01 /* [0-9] */
1560 #define LT 0x02 /* [a-zA-Z] */
1561 #define I1 0x04 /* LT | [!$%&*:/<=>?^_~] */
1562 #define Id 0x08 /* LT | I1 | DG | [-+.@] */
1563 #define RA 0x10 /* [bodxieBODXIE] */
1564 #define NM 0x20 /* [-@#.0-9a-fA-FesfdlESFDL] */
1565 #define EF 0x40 /* (eof) */
1566
1567 #define is_DIGIT(c) ((rct)[c] & DG)
1568 #define is_LETTER(c) ((rct)[c] & LT)
1569 #define is_IDENT1(c) ((rct)[c] & (LT | I1))
1570 #define is_IDENT(c) ((rct)[c] & (LT | I1 | DG | Id))
1571 #define is_RADIX(c) ((rct)[c] & RA)
1572 #define is_NUMBER(c) ((rct)[c] & (DG | RA | NM))
1573
1574 static unsigned char ctype_lisp[257] =
1575 {
1576 EF,
1577 /* 0 1 2 3 4 5 6 7 */
1578 /* ----- ----- ----- ----- ----- ----- ----- ----- */
1579 /*000*/ 0, 0, 0, 0, 0, 0, 0, 0,
1580 /*010*/ 0, 0, 0, 0, 0, 0, 0, 0,
1581 /*020*/ 0, 0, 0, 0, 0, 0, 0, 0,
1582 /*030*/ 0, 0, 0, 0, 0, 0, 0, 0,
1583 /*040*/ 0, I1, 0, NM, I1, I1, I1, 0,
1584 /*050*/ 0, 0, I1, Id, 0, Id|NM, Id|NM, I1,
1585 /*060*/ DG, DG, DG, DG, DG, DG, DG, DG,
1586 /*070*/ DG, DG, I1, 0, I1, I1, I1, I1,
1587 /*100*/ Id|NM, LT|NM, LT|RA, LT|NM, LT|RA, LT|RA, LT|NM, LT,
1588 /*110*/ LT, LT|RA, LT, LT, LT|NM, LT, LT, LT|RA,
1589 /*120*/ LT, LT, LT, LT|NM, LT, LT, LT, LT,
1590 /*130*/ LT|RA, LT, LT, 0, 0, 0, I1, I1,
1591 /*140*/ 0, LT|NM, LT|RA, LT|NM, LT|RA, LT|RA, LT|NM, LT,
1592 /*150*/ LT, LT|RA, LT, LT, LT|NM, LT, LT, LT|RA,
1593 /*160*/ LT, LT, LT, LT|NM, LT, LT, LT, LT,
1594 /*170*/ LT|RA, LT, LT, 0, 0, 0, I1, 0
1595 /* FIXME: latin-1 */
1596 };
1597
1598 static void
1599 help_me_lisp (void)
1600 {
1601 printf (_("\
1602 Lisp language:\n\
1603 "));
1604 }
1605
1606 static void *
1607 parse_args_lisp (char **argv, int argc)
1608 {
1609 return NULL;
1610 }
1611
1612 /* Grab the next identifier from the lisp source file. This
1613 state machine is built for speed, not elegance. */
1614
1615 static struct token *
1616 get_token_lisp (FILE *in_FILE, void const *args, int *flags)
1617 {
1618 unsigned char const *rct = &ctype_lisp[1];
1619 unsigned char *id = scanner_buffer;
1620 int c;
1621
1622 obstack_blank (&tokens_obstack, OFFSETOF_TOKEN_NAME);
1623
1624 top:
1625 c = getc (in_FILE);
1626 recheck:
1627 switch (c)
1628 {
1629 case EOF:
1630 obstack_free (&tokens_obstack, obstack_finish (&tokens_obstack));
1631 return 0;
1632
1633 case '(': case ')':
1634 case '\'': case '`': /* quote, quasiquote */
1635 goto top;
1636
1637 case ',': /* unquote */
1638 c = getc (in_FILE);
1639 if (c == '@') /* unquote-splicing */
1640 goto top;
1641 goto recheck;
1642
1643 case ';': /* comment */
1644 do {
1645 c = getc (in_FILE);
1646 } while ( (c != EOF) && (c != '\n'));
1647 goto top;
1648
1649 case '"': /* string with/without ansi-C escapes*/
1650 string:
1651 do {
1652 c = getc (in_FILE);
1653 if (c == '\\')
1654 {
1655 c = getc (in_FILE);
1656 goto string;
1657 }
1658 } while ( (c != EOF) && (c != '"'));
1659 goto top;
1660
1661 case '?': /* character constant */
1662 cconstant:
1663 do {
1664 c = getc(in_FILE);
1665 if (c == '\\')
1666 {
1667 c = getc(in_FILE);
1668 goto cconstant;
1669 }
1670 } while (c != EOF && is_IDENT(c));
1671 goto top;
1672
1673 case '.':
1674 case '+': case '-':
1675 id = scanner_buffer;
1676 *id++ = c;
1677 c = getc (in_FILE);
1678 if (is_DIGIT (c) ||
1679 (scanner_buffer[0] != '.' && (c == '.' || c == 'i' || c == 'I')))
1680 goto number;
1681 if (c != EOF)
1682 ungetc (c, in_FILE);
1683 goto ident;
1684
1685 case '#':
1686 id = scanner_buffer;
1687 *id++ = c;
1688
1689 c = getc (in_FILE);
1690 if (c == EOF)
1691 goto top;
1692 else if (is_RADIX (c))
1693 goto number;
1694 else if (c == '\\') /* #\... literal Character */
1695 {
1696 *id++ = c;
1697 c = getc (in_FILE);
1698 *id++ = c;
1699 if (is_LETTER (c))
1700 {
1701 while (is_LETTER (c = getc (in_FILE)))
1702 *id++ = c;
1703 if (c != EOF)
1704 ungetc (c, in_FILE);
1705 }
1706 *flags = TOK_LITERAL;
1707 obstack_grow0 (&tokens_obstack, scanner_buffer, id - scanner_buffer);
1708 return (struct token *) obstack_finish (&tokens_obstack);
1709 }
1710 else if (c == '(') /* # (...) vector vi%) */
1711 goto top;
1712 else if (c == '"') /* #"..." Bigloo: string with ansi-C escape */
1713 goto string;
1714 else if (c == '!') /* #!... Kawa key/eof/null/... */
1715 {
1716 while (is_LETTER (c = getc (in_FILE)))
1717 *id++ = c;
1718 if (c != EOF)
1719 ungetc (c, in_FILE);
1720 *flags = TOK_LITERAL;
1721 obstack_grow0 (&tokens_obstack, scanner_buffer, id - scanner_buffer);
1722 return (struct token *) obstack_finish (&tokens_obstack);
1723 }
1724 else if (c == '|') /* #|...|# Guile/Kawa multi-lines comment */
1725 {
1726 do {
1727 c = getc (in_FILE);
1728 if (c == '|')
1729 {
1730 while ( (c = getc (in_FILE)) == '|')
1731 ;
1732 if (c == '#')
1733 break;
1734 }
1735 } while (c != EOF);
1736 goto top;
1737 }
1738 else if (c == '@') /* #@LENGTH ...^_ EMACS byte-code comment */
1739 {
1740 do {
1741 c = getc (in_FILE);
1742 } while ( (c != EOF) && (c != '\037'));
1743 goto top;
1744 }
1745 else if (c == '[') /* #[ ... ] EMACS byte-code object */
1746 goto top;
1747 /* Ignore invalide #-construct */
1748 goto top;
1749
1750 case '[': case ']': /* EMACS vector aka # (...) in Scheme */
1751 /* EMACS vector object vs Kawa ident */
1752 /* rational: Kawa ident could not start with [ nor with ] */
1753 goto top;
1754
1755 default:
1756 if (is_IDENT1 (c))
1757 {
1758 id = scanner_buffer;
1759 *id++ = c;
1760 ident:
1761 /* Emacs end-of-vector vs Kawa ident: allow [] as a part of an ident. */
1762 for (;;)
1763 {
1764 while (is_IDENT (c = getc (in_FILE)))
1765 *id++ = c;
1766 if (0 /* c == '[' */)
1767 {
1768 c = getc (in_FILE);
1769 if (c == ']')
1770 {
1771 *id++ = '[';
1772 *id++ = ']';
1773 continue;
1774 }
1775 if (c != EOF)
1776 ungetc (c, in_FILE);
1777 ungetc ('[', in_FILE);
1778 }
1779 break;
1780 }
1781 if (c != EOF)
1782 ungetc (c, in_FILE);
1783 *flags = TOK_NAME | TOK_LITERAL;
1784 obstack_grow0 (&tokens_obstack, scanner_buffer, id - scanner_buffer);
1785 return (struct token *) obstack_finish (&tokens_obstack);
1786 }
1787 else if (is_DIGIT (c))
1788 {
1789 id = scanner_buffer;
1790 number:
1791 *id++ = c;
1792 while (is_NUMBER (c = getc (in_FILE)))
1793 *id++ = c;
1794 if (c != EOF)
1795 ungetc (c, in_FILE);
1796 *flags = TOK_NUMBER | TOK_LITERAL;
1797 obstack_grow0 (&tokens_obstack, scanner_buffer, id - scanner_buffer);
1798 return (struct token *) obstack_finish (&tokens_obstack);
1799 }
1800 }
1801 goto top;
1802
1803 }
1804 #undef DG
1805 #undef LT
1806 #undef I1
1807 #undef Id
1808
1809 #undef RA
1810 #undef NM
1811
1812 #undef is_DIGIT
1813 #undef is_LETTER
1814 #undef is_IDENT1
1815 #undef is_IDENT
1816 #undef is_RADIX
1817 #undef is_NUMBER