2 * Copyright (c) 1987, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)C.c 8.4 (Berkeley) 4/2/94
30 * $FreeBSD: head/usr.bin/ctags/C.c 299355 2016-05-10 11:11:23Z bapt $
39 static int func_entry(void);
40 static void hash_entry(void);
41 static void skip_string(int);
42 static int str_entry(int);
46 * read .c and .h files and call appropriate routines
51 int c
; /* current character */
52 int level
; /* brace level */
53 int token
; /* if reading a token */
54 int t_def
; /* if reading a typedef */
55 int t_level
; /* typedef's brace level */
56 char *sp
; /* buffer pointer */
57 char tok
[MAXTOKEN
]; /* token buffer */
59 lineftell
= ftell(inf
);
60 sp
= tok
; token
= t_def
= NO
; t_level
= -1; level
= 0; lineno
= 1;
61 while (GETC(!=, EOF
)) {
64 * Here's where it DOESN'T handle: {
71 * puts("hello, world");
79 * if level goes below zero, try and fix
80 * it, even though we've already messed up
89 * the above 3 cases are similar in that they
90 * are special characters that also end tokens.
92 endtok
: if (sp
> tok
) {
102 * We ignore quoted strings and character constants
111 * comments can be fun; note the state is unchanged after
112 * return, in case we found:
113 * "foo() XX comment XX { int bar; }"
116 if (GETC(==, '*') || c
== '/') {
124 /* hash marks flag #define's. */
133 * if we have a current token, parenthesis on
134 * level zero indicates a function.
137 if (!level
&& token
) {
143 * grab the line immediately, we may
144 * already be wrong, for example,
152 pfnote(tok
, curline
);
159 * semi-colons indicate the end of a typedef; if we find a
160 * typedef we search for the next semi-colon of the same
161 * level as the typedef. Ignoring "structs", they are
162 * tricky, since you can find:
164 * "typedef long time_t;"
165 * "typedef unsigned int u_int;"
166 * "typedef unsigned int u_int [10];"
168 * If looking at a typedef, we save a copy of the last token
169 * found. Then, when we find the ';' we take the current
170 * token if it starts with a valid token name, else we take
171 * the one we saved. There's probably some reasonable
172 * alternative to this...
175 if (t_def
&& level
== t_level
) {
186 * store characters until one that can't be part of a token
187 * comes along; check the current token against certain
191 /* ignore whitespace */
192 if (c
== ' ' || c
== '\t') {
194 while (GETC(!=, EOF
) && (c
== ' ' || c
== '\t'))
201 storec
: if (!intoken(c
)) {
206 /* no typedefs inside typedefs */
208 !memcmp(tok
, "typedef",8)) {
213 /* catch "typedef struct" */
214 if ((!t_def
|| t_level
< level
)
215 && (!memcmp(tok
, "struct", 7)
216 || !memcmp(tok
, "union", 6)
217 || !memcmp(tok
, "enum", 5))) {
219 * get line immediately;
220 * may change before '{'
231 else if (sp
!= tok
|| begtoken(c
)) {
232 if (sp
== tok
+ sizeof tok
- 1)
233 /* Too long -- truncate it */
249 * handle a function reference
254 int c
; /* current character */
255 int level
= 0; /* for matching '()' */
258 * Find the end of the assumed function declaration.
259 * Note that ANSI C functions can have type definitions so keep
260 * track of the parentheses nesting level.
262 while (GETC(!=, EOF
)) {
266 /* skip strings and character constants */
271 if (GETC(==, '*') || c
== '/')
289 * we assume that the character after a function's right paren
290 * is a token character if it's a function and a non-token
291 * character if it's a declaration. Comments don't count...
294 while (GETC(!=, EOF
) && iswhite(c
))
297 if (intoken(c
) || c
== '{')
299 if (c
== '/' && (GETC(==, '*') || c
== '/'))
301 else { /* don't ever "read" '/' */
313 * handle a line starting with a '#'
318 int c
; /* character read */
319 int curline
; /* line started on */
320 char *sp
; /* buffer pointer */
321 char tok
[MAXTOKEN
]; /* storage buffer */
323 /* ignore leading whitespace */
324 while (GETC(!=, EOF
) && (c
== ' ' || c
== '\t'))
329 for (sp
= tok
;;) { /* get next token */
334 if (sp
== tok
+ sizeof tok
- 1)
335 /* Too long -- truncate it */
341 if (memcmp(tok
, "define", 6)) /* only interested in #define's */
343 for (;;) { /* this doesn't handle "#define \n" */
349 for (sp
= tok
;;) { /* get next token */
350 if (sp
== tok
+ sizeof tok
- 1)
351 /* Too long -- truncate it */
358 * this is where it DOESN'T handle
365 if (dflag
|| c
== '(') { /* only want macros */
367 pfnote(tok
, curline
);
369 skip
: if (c
== '\n') { /* get rid of rest of define */
371 if (*(sp
- 1) != '\\')
379 * handle a struct, union or enum entry
382 str_entry(int c
) /* c is current character */
384 int curline
; /* line started on */
385 char *sp
; /* buffer pointer */
386 char tok
[LINE_MAX
]; /* storage buffer */
392 if (c
== '{') /* it was "struct {" */
394 for (sp
= tok
;;) { /* get next token */
395 if (sp
== tok
+ sizeof tok
- 1)
396 /* Too long -- truncate it */
406 case '{': /* it was "struct foo{" */
409 case '\n': /* it was "struct foo\n" */
412 default: /* probably "struct foo " */
413 while (GETC(!=, EOF
))
422 pfnote(tok
, curline
);
431 skip_comment(int t
) /* t is comment character */
433 int c
; /* character read */
434 int star
; /* '*' flag */
436 for (star
= 0; GETC(!=, EOF
);)
438 /* comments don't nest, nor can they be escaped. */
443 if (star
&& t
== '*')
459 * skip to the end of a string or character constant.
467 for (skip
= NO
; GETC(!=, EOF
); )
469 case '\\': /* a backslash escapes anything */
470 skip
= !skip
; /* we toggle in case it's "\\" */
476 if (c
== key
&& !skip
)
484 * skip to next char "key"
493 for (skip
= retval
= NO
; GETC(!=, EOF
);)
495 case '\\': /* a backslash escapes anything */
496 skip
= !skip
; /* we toggle in case it's "\\" */
498 case ';': /* special case for yacc; if one */
499 case '|': /* of these chars occurs, we may */
500 retval
= YES
; /* have moved out of the rule */
501 break; /* not used by C */
504 /* skip strings and character constants */
509 if (GETC(==, '*') || c
== '/') {
521 if (c
== key
&& !skip
)