Debian Codegraph @

libidu/scanners.c

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