1 /* Tags file maker to go with GNU Emacs
2 Copyright (C) 1984, 87, 88, 89, 93, 94, 95
3 Free Software Foundation, Inc. and Ken Arnold
4 This file is not considered part of GNU Emacs.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 * Ctags originally by Ken Arnold.
23 * Fortran added by Jim Kleckner.
24 * Ed Pelegri-Llopart added C typedefs.
25 * Gnu Emacs TAGS format and modifications by RMS?
26 * Sam Kendall added C++.
27 * Francesco Potorti` reorganised C and C++ based on work by Joe Wells.
29 * Regexp tags by Tom Tromey.
32 * Francesco Potorti` (pot@cnuce.cnr.it) is the current maintainer.
35 char pot_etags_version
[] = "@(#) pot revision number is 11.26";
45 #include <sys/param.h>
52 #define MAXPATHLEN _MAX_PATH
57 /* On some systems, Emacs defines static as nothing for the sake
58 of unexec. We don't want that here since we don't use unexec. */
68 #include <sys/types.h>
71 #if !defined (S_ISREG) && defined (S_IFREG)
72 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
79 #endif /* ETAGS_REGEXPS */
81 /* Define CTAGS to make the program "ctags" compatible with the usual one.
82 Let it undefined to make the program "etags", which makes emacs-style
83 tag tables and tags typedefs, #defines and struct/union/enum by default. */
91 /* Exit codes for success and failure. */
101 #define C_PLPL 0x00001 /* C++ */
102 #define C_STAR 0x00003 /* C* */
103 #define YACC 0x10000 /* yacc file */
105 #define streq(s,t) (strcmp (s, t) == 0)
106 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
108 #define iswhite(arg) (_wht[arg]) /* T if char is white */
109 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
110 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
111 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
114 * xnew -- allocate storage
116 * SYNOPSIS: Type *xnew (int n, Type);
118 #define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
123 { /* sorting structure */
124 char *name
; /* function or type name */
125 char *file
; /* file name */
126 logical is_func
; /* use pattern or line no */
127 logical named
; /* list name separately */
128 logical been_warned
; /* set if noticed dup */
129 int lno
; /* line number tag is on */
130 long cno
; /* character number line starts on */
131 char *pat
; /* search pattern */
132 struct nd_st
*left
, *right
; /* left and right sons */
135 extern char *getenv ();
138 char *savenstr (), *savestr ();
139 char *etags_strchr (), *etags_strrchr ();
140 char *etags_getcwd ();
141 char *relative_filename (), *absolute_filename (), *absolute_dirname ();
142 char *xmalloc (), *xrealloc ();
144 typedef void Lang_function ();
145 #if FALSE /* many compilers barf on this */
146 Lang_function Asm_labels
;
147 Lang_function default_C_entries
;
148 Lang_function C_entries
;
149 Lang_function Cplusplus_entries
;
150 Lang_function Cstar_entries
;
151 Lang_function Fortran_functions
;
152 Lang_function Yacc_entries
;
153 Lang_function Lisp_functions
;
154 Lang_function Pascal_functions
;
155 Lang_function Prolog_functions
;
156 Lang_function Scheme_functions
;
157 Lang_function TeX_functions
;
158 Lang_function just_read_file
;
159 #else /* so let's write it this way */
161 void default_C_entries ();
163 void Cplusplus_entries ();
164 void Cstar_entries ();
165 void Fortran_functions ();
166 void Yacc_entries ();
167 void Lisp_functions ();
168 void Pascal_functions ();
169 void Prolog_functions ();
170 void Scheme_functions ();
171 void TeX_functions ();
172 void just_read_file ();
175 logical
get_language ();
176 int total_size_of_entries ();
178 long readline_internal ();
184 void fatal (), pfatal ();
185 void find_entries ();
191 void process_file ();
196 char searchar
= '/'; /* use /.../ searches */
198 int lineno
; /* line number of current line */
199 long charno
; /* current character number */
201 long linecharno
; /* charno of start of line; not used by C,
202 but by every other language. */
204 char *curfile
; /* current input file name */
205 char *tagfile
; /* output file */
206 char *progname
; /* name this program was invoked with */
207 char *cwd
; /* current working directory */
208 char *tagfiledir
; /* directory of tagfile */
210 FILE *tagf
; /* ioptr for tags file */
211 NODE
*head
; /* the head of the binary tree of tags */
214 * A `struct linebuffer' is a structure which holds a line of text.
215 * `readline' reads a line from a stream into a linebuffer and works
216 * regardless of the length of the line.
224 struct linebuffer lb
; /* the current line */
225 struct linebuffer token_name
; /* used by C_entries as temporary area */
229 struct linebuffer lb
; /* used by C_entries instead of lb */
232 /* boolean "functions" (see init) */
233 logical _wht
[0177], _etk
[0177], _itk
[0177], _btk
[0177];
235 *white
= " \f\t\n\013", /* white chars */
236 *endtk
= " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?", /* token ending chars */
237 /* token starting chars */
238 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~",
239 /* valid in-token chars */
240 *intk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
242 logical append_to_tagfile
; /* -a: append to tags */
243 /* The following three default to TRUE for etags, but to FALSE for ctags. */
244 logical typedefs
; /* -t: create tags for typedefs */
245 logical typedefs_and_cplusplus
; /* -T: create tags for typedefs, level */
246 /* 0 struct/enum/union decls, and C++ */
247 /* member functions. */
248 logical constantypedefs
; /* -d: create tags for C #define and enum */
249 /* constants. Enum consts not implemented. */
250 /* -D: opposite of -d. Default under ctags. */
251 logical update
; /* -u: update tags */
252 logical vgrind_style
; /* -v: create vgrind style index output */
253 logical no_warnings
; /* -w: suppress warnings */
254 logical cxref_style
; /* -x: create cxref style output */
255 logical cplusplus
; /* .[hc] means C++, not C */
256 logical noindentypedefs
; /* -I: ignore indentation in C */
257 #define permit_duplicates TRUE /* allow duplicate tags */
259 struct option longopts
[] =
261 { "append", no_argument
, NULL
, 'a' },
262 { "backward-search", no_argument
, NULL
, 'B' },
263 { "c++", no_argument
, NULL
, 'C' },
264 { "cxref", no_argument
, NULL
, 'x' },
265 { "defines", no_argument
, NULL
, 'd' },
266 { "help", no_argument
, NULL
, 'h' },
267 { "help", no_argument
, NULL
, 'H' },
268 { "ignore-indentation", no_argument
, NULL
, 'I' },
269 { "include", required_argument
, NULL
, 'i' },
270 { "language", required_argument
, NULL
, 'l' },
271 { "no-defines", no_argument
, NULL
, 'D' },
272 { "no-regex", no_argument
, NULL
, 'R' },
273 { "no-warn", no_argument
, NULL
, 'w' },
274 { "output", required_argument
, NULL
, 'o' },
275 { "regex", required_argument
, NULL
, 'r' },
276 { "typedefs", no_argument
, NULL
, 't' },
277 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
278 { "update", no_argument
, NULL
, 'u' },
279 { "version", no_argument
, NULL
, 'V' },
280 { "vgrind", no_argument
, NULL
, 'v' },
285 /* Structure defining a regular expression. Elements are
286 the compiled pattern, and the name string. */
289 struct re_pattern_buffer
*pattern
;
290 struct re_registers regs
;
292 logical error_signaled
;
295 /* Number of regexps found. */
296 int num_patterns
= 0;
298 /* Array of all regexps. */
299 struct pattern
*patterns
= NULL
;
300 #endif /* ETAGS_REGEXPS */
302 /* Language stuff. */
306 Lang_function
*function
;
309 /* Table of language names and corresponding functions. */
310 /* It is ok for a given function to be listed under more than one
311 name. I just didn't. */
312 /* "auto" language reverts to default behavior. */
313 struct lang_entry lang_names
[] =
315 { "asm", Asm_labels
},
316 { "c", default_C_entries
},
317 { "c++", Cplusplus_entries
},
318 { "c*", Cstar_entries
},
319 { "fortran", Fortran_functions
},
320 { "lisp", Lisp_functions
},
321 { "none", just_read_file
},
322 { "pascal", Pascal_functions
},
323 { "scheme" , Scheme_functions
},
324 { "tex", TeX_functions
},
329 /* Table of file extensions and corresponding language functions. */
330 struct lang_entry lang_extensions
[] =
332 /* Assume that ".s" or ".a" is assembly code. -wolfgang.
334 { "a", Asm_labels
}, /* Unix assembler */
335 { "asm", Asm_labels
}, /* Microcontroller assembly */
336 { "def", Asm_labels
}, /* BSO/Tasking definition includes */
337 { "inc", Asm_labels
}, /* Microcontroller include files */
338 { "ins", Asm_labels
}, /* Microcontroller include files */
340 { "sa", Asm_labels
}, /* Unix assembler */
341 { "src", Asm_labels
}, /* BSO/Tasking C compiler output */
343 /* .aux, .bbl, .clo, .cls, .dtx or .tex implies LaTeX source code. */
344 { "aux", TeX_functions
},
345 { "bbl", TeX_functions
},
346 { "clo", TeX_functions
},
347 { "cls", TeX_functions
},
348 { "dtx", TeX_functions
},
349 { "sty", TeX_functions
},
350 { "tex", TeX_functions
},
352 /* .l or .el or .lisp (or .cl or .clisp or ...) implies lisp source code */
353 { "cl", Lisp_functions
},
354 { "clisp", Lisp_functions
},
355 { "el", Lisp_functions
},
356 { "l", Lisp_functions
},
357 { "lisp", Lisp_functions
},
358 { "lsp", Lisp_functions
},
360 /* .scm or .sm or .scheme implies scheme source code */
361 { "SCM", Scheme_functions
},
362 { "SM", Scheme_functions
},
363 { "oak", Scheme_functions
},
364 { "sch", Scheme_functions
},
365 { "scheme", Scheme_functions
},
366 { "scm", Scheme_functions
},
367 { "sm", Scheme_functions
},
368 { "t", Scheme_functions
},
369 /* FIXME Can't do the `SCM' or `scm' prefix with a version number */
371 /* Note that ".c" and ".h" can be considered C++, if the --c++
372 flag was given. That is why default_C_entries is called here. */
373 { "c", default_C_entries
},
374 { "h", default_C_entries
},
376 /* .C or .H or .c++ or .cc or .cpp or .cxx or .h++ or .hh or .hxx:
378 { "C", Cplusplus_entries
},
379 { "H", Cplusplus_entries
},
380 { "c++", Cplusplus_entries
},
381 { "cc", Cplusplus_entries
},
382 { "cpp", Cplusplus_entries
},
383 { "cxx", Cplusplus_entries
},
384 { "h++", Cplusplus_entries
},
385 { "hh", Cplusplus_entries
},
386 { "hxx", Cplusplus_entries
},
388 /* .y: a yacc file */
389 { "y", Yacc_entries
},
391 /* .cs or .hs: a C* file */
392 { "cs", Cstar_entries
},
393 { "hs", Cstar_entries
},
395 /* .f and .for are FORTRAN. */
396 { "F", Fortran_functions
},
397 { "f", Fortran_functions
},
398 { "for", Fortran_functions
},
400 /* .pl implies prolog source code */
401 { "pl", Prolog_functions
},
403 /* .p or .pas: a Pascal file */
404 { "p", Pascal_functions
},
405 { "pas", Pascal_functions
},
410 /* Non-NULL if language fixed. */
411 Lang_function
*lang_func
= NULL
;
415 print_language_names ()
417 struct lang_entry
*name
, *ext
;
419 puts ("\nThese are the currently supported languages, along with the\n\
420 default extensions for files:");
421 for (name
= lang_names
; name
->extension
; ++name
)
423 printf ("\t%s\t", name
->extension
);
424 for (ext
= lang_extensions
; ext
->extension
; ++ext
)
425 if (name
->function
== ext
->function
)
426 printf (" .%s", ext
->extension
);
429 puts ("Where `auto' means use default language for files based on filename\n\
430 extension, and `none' means only do regexp processing on files.\n\
431 If no language is specified and no extension is found for some file,\n\
432 Fortran is tried first; if no tags are found, C is tried next.");
439 printf ("%s for Emacs version %s.\n", (CTAGS
) ? "CTAGS" : "ETAGS", VERSION
);
441 printf ("%s for Emacs version 19.\n", (CTAGS
) ? "CTAGS" : "ETAGS");
450 printf ("These are the options accepted by %s. You may use unambiguous\n\
451 abbreviations for the long option names. A - as file name means read\n\
452 names from stdin.\n\n", progname
);
454 puts ("-a, --append\n\
455 Append tag entries to existing tags file.");
458 puts ("-B, --backward-search\n\
459 Write the search commands for the tag entries using '?', the\n\
460 backward-search command instead of '/', the forward-search command.");
463 Treat files whose extension defaults to C language as C++ files.");
466 puts ("-d, --defines\n\
467 Create tag entries for constant C #defines, too.");
469 puts ("-D, --no-defines\n\
470 Don't create tag entries for constant C #defines. This makes\n\
471 the tags file smaller.");
475 puts ("-i FILE, --include=FILE\n\
476 Include a note in tag file indicating that, when searching for\n\
477 a tag, one should also consult the tags file FILE after\n\
478 checking the current file.");
479 puts ("-l LANG, --language=LANG\n\
480 Force the following files to be considered as written in the\n\
481 named language up to the next --language=LANG option.");
485 puts ("-r /REGEXP/, --regex=/REGEXP/\n\
486 Make a tag for each line matching pattern REGEXP in the\n\
487 following files. REGEXP is anchored (as if preceded by ^).\n\
488 The form /REGEXP/NAME/ creates a named tag. For example Tcl\n\
489 named tags can be created with:\n\
490 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
491 puts ("-R, --no-regex\n\
492 Don't create tags from regexps for the following files.");
493 #endif /* ETAGS_REGEXPS */
494 puts ("-o FILE, --output=FILE\n\
495 Write the tags to FILE.");
496 puts ("-I, --ignore-indentation\n\
497 Don't rely on indentation quite as much as normal. Currently,\n\
498 this means not to assume that a closing brace in the first\n\
499 column is the final brace of a function or structure\n\
500 definition in C and C++.");
504 puts ("-t, --typedefs\n\
505 Generate tag entries for C typedefs.");
506 puts ("-T, --typedefs-and-c++\n\
507 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
508 and C++ member functions.");
509 puts ("-u, --update\n\
510 Update the tag entries for the given files, leaving tag\n\
511 entries for other files in place. Currently, this is\n\
512 implemented by deleting the existing entries for the given\n\
513 files and then rewriting the new entries at the end of the\n\
514 tags file. It is often faster to simply rebuild the entire\n\
515 tag file than to use this.");
516 puts ("-v, --vgrind\n\
517 Generates an index of items intended for human consumption,\n\
518 similar to the output of vgrind. The index is sorted, and\n\
519 gives the page number of each item.");
520 puts ("-w, --no-warn\n\
521 Suppress warning messages about entries defined in multiple\n\
523 puts ("-x, --cxref\n\
524 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
525 The output uses line numbers instead of page numbers, but\n\
526 beyond that the differences are cosmetic; try both to see\n\
530 puts ("-V, --version\n\
531 Print the version of the program.\n\
533 Print this help message.");
535 print_language_names ();
548 /* This structure helps us allow mixing of --lang and filenames. */
551 enum argument_type arg_type
;
553 Lang_function
*function
;
556 #ifdef VMS /* VMS specific functions */
560 /* This is a BUG! ANY arbitrary limit is a BUG!
561 Won't someone please fix this? */
562 #define MAX_FILE_SPEC_LEN 255
565 char body
[MAX_FILE_SPEC_LEN
+ 1];
569 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
570 returning in each successive call the next filename matching the input
571 spec. The function expects that each in_spec passed
572 to it will be processed to completion; in particular, up to and
573 including the call following that in which the last matching name
574 is returned, the function ignores the value of in_spec, and will
575 only start processing a new spec with the following call.
576 If an error occurs, on return out_spec contains the value
577 of in_spec when the error occurred.
579 With each successive filename returned in out_spec, the
580 function's return value is one. When there are no more matching
581 names the function returns zero. If on the first call no file
582 matches in_spec, or there is any other error, -1 is returned.
587 #define OUTSIZE MAX_FILE_SPEC_LEN
593 static long context
= 0;
594 static struct dsc$descriptor_s o
;
595 static struct dsc$descriptor_s i
;
596 static logical pass1
= TRUE
;
603 o
.dsc$a_pointer
= (char *) out
;
604 o
.dsc$w_length
= (short)OUTSIZE
;
605 i
.dsc$a_pointer
= in
;
606 i
.dsc$w_length
= (short)strlen(in
);
607 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
608 i
.dsc$b_class
= DSC$K_CLASS_S
;
609 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
610 o
.dsc$b_class
= DSC$K_CLASS_VS
;
612 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
614 out
->body
[out
->curlen
] = EOS
;
617 else if (status
== RMS$_NMF
)
621 strcpy(out
->body
, in
);
624 lib$
find_file_end(&context
);
630 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
631 name of each file specified by the provided arg expanding wildcards.
634 gfnames (arg
, p_error
)
638 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
640 switch (fn_exp (&filename
, arg
))
644 return filename
.body
;
650 return filename
.body
;
654 #ifndef OLD /* Newer versions of VMS do provide `system'. */
658 fprintf (stderr
, "system() function not implemented under VMS\n");
662 #define VERSION_DELIM ';'
663 char *massage_name (s
)
669 if (*s
== VERSION_DELIM
)
687 unsigned int nincluded_files
= 0;
688 char **included_files
= xnew (argc
, char *);
691 int current_arg
= 0, file_count
= 0;
692 struct linebuffer filename_lb
;
698 _fmode
= O_BINARY
; /* all of files are treated as binary files */
703 /* Allocate enough no matter what happens. Overkill, but each one
705 argbuffer
= xnew (argc
, ARGUMENT
);
708 /* Set syntax for regular expression routines. */
709 re_set_syntax (RE_SYNTAX_EMACS
);
710 #endif /* ETAGS_REGEXPS */
713 * If etags, always find typedefs and structure tags. Why not?
714 * Also default is to find macro constants.
717 typedefs
= typedefs_and_cplusplus
= constantypedefs
= TRUE
;
721 int opt
= getopt_long (argc
, argv
,
722 "-aCdDf:Il:o:r:RStTi:BuvxwVhH", longopts
, 0);
730 /* If getopt returns 0, then it has already processed a
731 long-named option. We should do nothing. */
735 /* This means that a filename has been seen. Record it. */
736 argbuffer
[current_arg
].arg_type
= at_filename
;
737 argbuffer
[current_arg
].what
= optarg
;
742 /* Common options. */
744 append_to_tagfile
= TRUE
;
750 constantypedefs
= TRUE
;
753 constantypedefs
= FALSE
;
755 case 'f': /* for compatibility with old makefiles */
759 fprintf (stderr
, "%s: -%c option may only be given once.\n",
766 case 'S': /* for backward compatibility */
767 noindentypedefs
= TRUE
;
770 if (!get_language (optarg
, &argbuffer
[current_arg
].function
))
772 fprintf (stderr
, "%s: language \"%s\" not recognized.\n",
776 argbuffer
[current_arg
].arg_type
= at_language
;
781 argbuffer
[current_arg
].arg_type
= at_regexp
;
782 argbuffer
[current_arg
].what
= optarg
;
786 argbuffer
[current_arg
].arg_type
= at_regexp
;
787 argbuffer
[current_arg
].what
= NULL
;
790 #endif /* ETAGS_REGEXPS */
802 typedefs
= typedefs_and_cplusplus
= TRUE
;
807 included_files
[nincluded_files
++] = optarg
;
832 for (; optind
< argc
; ++optind
)
834 argbuffer
[current_arg
].arg_type
= at_filename
;
835 argbuffer
[current_arg
].what
= argv
[optind
];
840 if (nincluded_files
== 0 && file_count
== 0)
842 fprintf (stderr
, "%s: No input files specified.\n", progname
);
845 fprintf (stderr
, "\tTry `%s --help' for a complete list of options.\n",
852 tagfile
= CTAGS
? "tags" : "TAGS";
854 cwd
= etags_getcwd (); /* the current working directory */
856 if (streq (tagfile
, "-"))
862 tagfiledir
= absolute_dirname (tagfile
, cwd
);
865 init (); /* set up boolean "functions" */
868 initbuffer (&token_name
);
869 initbuffer (&lbs
[0].lb
);
870 initbuffer (&lbs
[1].lb
);
871 initbuffer (&filename_lb
);
875 if (streq (tagfile
, "-"))
878 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
884 * Loop through files finding functions.
886 for (i
= 0; i
< current_arg
; ++i
)
888 switch (argbuffer
[i
].arg_type
)
891 lang_func
= argbuffer
[i
].function
;
895 add_regex (argbuffer
[i
].what
);
900 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
904 error ("Can't find file %s\n", this_file
);
909 this_file
= massage_name (this_file
);
912 this_file
= argbuffer
[i
].what
;
914 /* Input file named "-" means read file names from stdin
916 if (streq (this_file
, "-"))
917 while (readline_internal (&filename_lb
, stdin
) > 0)
918 process_file (filename_lb
.buffer
);
920 process_file (this_file
);
930 while (nincluded_files
-- > 0)
931 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
937 /* If CTAGS, we are here. process_file did not write the tags yet,
938 because we want them ordered. Let's do it now. */
941 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
951 for (i
= 0; i
< current_arg
; ++i
)
953 if (argbuffer
[i
].arg_type
!= at_filename
)
956 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
957 tagfile
, argbuffer
[i
].what
, tagfile
);
958 if (system (cmd
) != GOOD
)
959 fatal ("failed to execute shell command");
961 append_to_tagfile
= TRUE
;
964 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
973 sprintf (cmd
, "sort %s -o %s", tagfile
, tagfile
);
981 * Set the language, given the name.
984 get_language (language
, func
)
986 Lang_function
**func
;
988 struct lang_entry
*lang
;
990 for (lang
= lang_names
; lang
->extension
; ++lang
)
992 if (streq (language
, lang
->extension
))
994 *func
= lang
->function
;
1004 * This routine is called on each file argument.
1010 struct stat stat_buf
;
1013 if (stat (file
, &stat_buf
) == 0 && !S_ISREG (stat_buf
.st_mode
))
1015 fprintf (stderr
, "Skipping %s: it is not a regular file.\n", file
);
1018 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1020 fprintf (stderr
, "Skipping inclusion of %s in self.\n", file
);
1023 inf
= fopen (file
, "r");
1030 find_entries (file
, inf
);
1038 /* file is an absolute filename. Canonicalise it. */
1039 filename
= absolute_filename (file
, cwd
);
1043 /* file is a filename relative to cwd. Make it relative
1044 to the directory of the tags file. */
1045 filename
= relative_filename (file
, tagfiledir
);
1047 fprintf (tagf
, "\f\n%s,%d\n", filename
, total_size_of_entries (head
));
1056 * This routine sets up the boolean pseudo-functions which work
1057 * by setting boolean flags dependent upon the corresponding character
1058 * Every char which is NOT in that string is not a white char. Therefore,
1059 * all of the array "_wht" is set to FALSE, and then the elements
1060 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1061 * of a char is TRUE if it is the string "white", else FALSE.
1069 for (i
= 0; i
< 0177; i
++)
1070 _wht
[i
] = _etk
[i
] = _itk
[i
] = _btk
[i
] = FALSE
;
1071 for (sp
= white
; *sp
; sp
++)
1073 for (sp
= endtk
; *sp
; sp
++)
1075 for (sp
= intk
; *sp
; sp
++)
1077 for (sp
= begtk
; *sp
; sp
++)
1079 _wht
[0] = _wht
['\n'];
1080 _etk
[0] = _etk
['\n'];
1081 _btk
[0] = _btk
['\n'];
1082 _itk
[0] = _itk
['\n'];
1086 * This routine opens the specified file and calls the function
1087 * which finds the function and type definitions.
1090 find_entries (file
, inf
)
1095 struct lang_entry
*lang
;
1096 NODE
*old_last_node
;
1097 extern NODE
*last_node
;
1099 curfile
= savestr (file
);
1100 cp
= etags_strrchr (file
, '.');
1102 /* If user specified a language, use it. */
1103 if (lang_func
!= NULL
)
1113 for (lang
= lang_extensions
; lang
->extension
; ++lang
)
1115 if (streq (cp
, lang
->extension
))
1117 lang
->function (inf
);
1125 old_last_node
= last_node
;
1126 Fortran_functions (inf
);
1128 /* No Fortran entries found. Try C. */
1129 if (old_last_node
== last_node
)
1130 default_C_entries (inf
);
1136 pfnote (name
, is_func
, named
, linestart
, linelen
, lno
, cno
)
1137 char *name
; /* tag name */
1138 logical is_func
; /* tag is a function */
1139 logical named
; /* tag different from text of definition */
1140 char *linestart
; /* start of the line where tag is */
1141 int linelen
; /* length of the line where tag is */
1142 int lno
; /* line number */
1143 long cno
; /* character number */
1145 register NODE
*np
= xnew (1, NODE
);
1148 /* If ctags mode, change name "main" to M<thisfilename>. */
1149 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
1151 fp
= etags_strrchr (curfile
, '/');
1152 np
->name
= concat ("M", fp
== 0 ? curfile
: fp
+ 1, "");
1153 fp
= etags_strrchr (np
->name
, '.');
1154 if (fp
&& fp
[1] != '\0' && fp
[2] == '\0')
1163 np
->been_warned
= FALSE
;
1165 np
->is_func
= is_func
;
1167 /* Our char numbers are 0-base, because of C language tradition?
1168 ctags compatibility? old versions compatibility? I don't know.
1169 Anyway, since emacs's are 1-base we espect etags.el to take care
1170 of the difference. If we wanted to have 1-based numbers, we would
1171 uncomment the +1 below. */
1172 np
->cno
= cno
/* + 1 */ ;
1173 np
->left
= np
->right
= NULL
;
1174 np
->pat
= savenstr (linestart
, ((CTAGS
&& !cxref_style
) ? 50 : linelen
));
1176 add_node (np
, &head
);
1181 * recurse on left children, iterate on right children.
1185 register NODE
*node
;
1189 register NODE
*node_right
= node
->right
;
1190 free_tree (node
->left
);
1194 free ((char *) node
);
1201 * Adds a node to the tree of nodes. In etags mode, we don't keep
1202 * it sorted; we just keep a linear list. In ctags mode, maintain
1203 * an ordered tree, with no attempt at balancing.
1205 * add_node is the only function allowed to add nodes, so it can
1208 NODE
*last_node
= NULL
;
1210 add_node (node
, cur_node_p
)
1211 NODE
*node
, **cur_node_p
;
1214 register NODE
*cur_node
= *cur_node_p
;
1216 if (cur_node
== NULL
)
1226 if (last_node
== NULL
)
1227 fatal ("internal error in add_node", 0);
1228 last_node
->right
= node
;
1234 dif
= strcmp (node
->name
, cur_node
->name
);
1237 * If this tag name matches an existing one, then
1238 * do not add the node, but maybe print a warning.
1242 if (node
->file
== cur_node
->file
)
1246 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
1247 node
->file
, lineno
, node
->name
);
1248 fprintf (stderr
, "Second entry ignored\n");
1252 if (!cur_node
->been_warned
&& !no_warnings
)
1255 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1256 node
->file
, cur_node
->file
, node
->name
);
1258 cur_node
->been_warned
= TRUE
;
1262 /* Maybe refuse to add duplicate nodes. */
1263 if (!permit_duplicates
)
1265 if (streq (node
->name
, cur_node
->name
)
1266 && streq (node
->file
, cur_node
->file
))
1270 /* Actually add the node */
1271 add_node (node
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1277 register NODE
*node
;
1284 /* Output subentries that precede this one */
1285 put_entries (node
->left
);
1287 /* Output this entry */
1293 fprintf (tagf
, "%s\177%s\001%d,%d\n",
1294 node
->pat
, node
->name
,
1295 node
->lno
, node
->cno
);
1299 fprintf (tagf
, "%s\177%d,%d\n",
1301 node
->lno
, node
->cno
);
1304 else if (!cxref_style
)
1306 fprintf (tagf
, "%s\t%s\t",
1307 node
->name
, node
->file
);
1311 putc (searchar
, tagf
);
1314 for (sp
= node
->pat
; *sp
; sp
++)
1316 if (*sp
== '\\' || *sp
== searchar
)
1320 putc (searchar
, tagf
);
1323 { /* a typedef; text pattern inadequate */
1324 fprintf (tagf
, "%d", node
->lno
);
1328 else if (vgrind_style
)
1329 fprintf (stdout
, "%s %s %d\n",
1330 node
->name
, node
->file
, (node
->lno
+ 63) / 64);
1332 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1333 node
->name
, node
->lno
, node
->file
, node
->pat
);
1335 /* Output subentries that follow this one */
1336 put_entries (node
->right
);
1339 /* Length of a number's decimal representation. */
1347 for (; num
; num
/= 10)
1353 * Return total number of characters that put_entries will output for
1354 * the nodes in the subtree of the specified node. Works only if
1355 * we are not ctags, but called only in that case. This count
1356 * is irrelevant with the new tags.el, but is still supplied for
1357 * backward compatibility.
1360 total_size_of_entries (node
)
1361 register NODE
*node
;
1369 for (; node
; node
= node
->right
)
1371 /* Count left subentries. */
1372 total
+= total_size_of_entries (node
->left
);
1374 /* Count this entry */
1375 total
+= strlen (node
->pat
) + 1;
1376 total
+= number_len ((long) node
->lno
) + 1 + number_len (node
->cno
) + 1;
1378 total
+= 1 + strlen (node
->name
); /* \001name */
1385 * The C symbol tables.
1389 st_none
, st_C_struct
, st_C_enum
, st_C_define
, st_C_typedef
, st_C_typespec
1392 /* Feed stuff between (but not including) %[ and %] lines to:
1393 gperf -c -k1,3 -o -p -r -t
1395 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1397 class, C_PLPL, st_C_struct
1398 domain, C_STAR, st_C_struct
1399 union, 0, st_C_struct
1400 struct, 0, st_C_struct
1402 typedef, 0, st_C_typedef
1403 define, 0, st_C_define
1404 long, 0, st_C_typespec
1405 short, 0, st_C_typespec
1406 int, 0, st_C_typespec
1407 char, 0, st_C_typespec
1408 float, 0, st_C_typespec
1409 double, 0, st_C_typespec
1410 signed, 0, st_C_typespec
1411 unsigned, 0, st_C_typespec
1412 auto, 0, st_C_typespec
1413 void, 0, st_C_typespec
1414 extern, 0, st_C_typespec
1415 static, 0, st_C_typespec
1416 const, 0, st_C_typespec
1417 volatile, 0, st_C_typespec
1419 and replace lines between %< and %> with its output. */
1421 /* C code produced by gperf version 1.8.1 (K&R C version) */
1422 /* Command-line: gperf -c -k1,3 -o -p -r -t */
1425 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
1427 #define MIN_WORD_LENGTH 3
1428 #define MAX_WORD_LENGTH 8
1429 #define MIN_HASH_VALUE 10
1430 #define MAX_HASH_VALUE 62
1433 53 is the maximum key range
1441 static unsigned char hash_table
[] =
1443 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1444 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1445 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1446 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1447 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1448 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1449 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1450 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1451 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1452 62, 62, 62, 62, 62, 62, 62, 2, 62, 7,
1453 6, 9, 15, 30, 62, 24, 62, 62, 1, 24,
1454 7, 27, 13, 62, 19, 26, 18, 27, 1, 62,
1455 62, 62, 62, 62, 62, 62, 62, 62,
1457 return len
+ hash_table
[str
[2]] + hash_table
[str
[0]];
1460 struct C_stab_entry
*
1461 in_word_set (str
, len
)
1466 static struct C_stab_entry wordlist
[] =
1468 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1470 {"volatile", 0, st_C_typespec
},
1472 {"long", 0, st_C_typespec
},
1473 {"char", 0, st_C_typespec
},
1474 {"class", C_PLPL
, st_C_struct
},
1475 {"",}, {"",}, {"",}, {"",},
1476 {"const", 0, st_C_typespec
},
1477 {"",}, {"",}, {"",}, {"",},
1478 {"auto", 0, st_C_typespec
},
1480 {"define", 0, st_C_define
},
1482 {"void", 0, st_C_typespec
},
1483 {"",}, {"",}, {"",},
1484 {"extern", 0, st_C_typespec
},
1485 {"static", 0, st_C_typespec
},
1487 {"domain", C_STAR
, st_C_struct
},
1489 {"typedef", 0, st_C_typedef
},
1490 {"double", 0, st_C_typespec
},
1491 {"enum", 0, st_C_enum
},
1492 {"",}, {"",}, {"",}, {"",},
1493 {"int", 0, st_C_typespec
},
1495 {"float", 0, st_C_typespec
},
1496 {"",}, {"",}, {"",},
1497 {"struct", 0, st_C_struct
},
1498 {"",}, {"",}, {"",}, {"",},
1499 {"union", 0, st_C_struct
},
1501 {"short", 0, st_C_typespec
},
1503 {"unsigned", 0, st_C_typespec
},
1504 {"signed", 0, st_C_typespec
},
1507 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
1509 register int key
= hash (str
, len
);
1511 if (key
<= MAX_HASH_VALUE
&& key
>= MIN_HASH_VALUE
)
1513 register char *s
= wordlist
[key
].name
;
1515 if (*s
== *str
&& strneq (str
+ 1, s
+ 1, len
- 1))
1516 return &wordlist
[key
];
1524 C_symtype(str
, len
, c_ext
)
1529 register struct C_stab_entry
*se
= in_word_set(str
, len
);
1531 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
1537 * C functions are recognized using a simple finite automaton.
1538 * funcdef is its state variable.
1542 fnone
, /* nothing seen */
1543 ftagseen
, /* function-like tag seen */
1544 fstartlist
, /* just after open parenthesis */
1545 finlist
, /* in parameter list */
1546 flistseen
, /* after parameter list */
1547 fignore
/* before open brace */
1553 * typedefs are recognized using a simple finite automaton.
1554 * typeddef is its state variable.
1558 tnone
, /* nothing seen */
1559 ttypedseen
, /* typedef keyword seen */
1560 tinbody
, /* inside typedef body */
1561 tend
, /* just before typedef tag */
1562 tignore
/* junk after typedef tag */
1568 * struct-like structures (enum, struct and union) are recognized
1569 * using another simple finite automaton. `structdef' is its state
1574 snone
, /* nothing seen yet */
1575 skeyseen
, /* struct-like keyword seen */
1576 stagseen
, /* struct-like tag seen */
1577 scolonseen
, /* colon seen after struct-like tag */
1578 sinbody
/* in struct body: recognize member func defs*/
1583 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1584 * struct tag, and structtype is the type of the preceding struct-like
1587 char *structtag
= "<uninited>";
1588 enum sym_type structtype
;
1591 * Yet another little state machine to deal with preprocessor lines.
1595 dnone
, /* nothing seen */
1596 dsharpseen
, /* '#' seen as first char on line */
1597 ddefineseen
, /* '#' and 'define' seen */
1598 dignorerest
/* ignore rest of line */
1603 * Set this to TRUE, and the next token considered is called a function.
1604 * Used only for GNU emacs's function-defining macros.
1606 logical next_token_is_func
;
1609 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1615 * checks to see if the current token is at the start of a
1616 * function, or corresponds to a typedef, or is a struct/union/enum
1619 * *IS_FUNC gets TRUE iff the token is a function or macro with args.
1620 * C_EXT is which language we are looking at.
1622 * In the future we will need some way to adjust where the end of
1623 * the token is; for instance, implementing the C++ keyword
1624 * `operator' properly will adjust the end of the token to be after
1625 * whatever follows `operator'.
1632 * next_token_is_func IN OUT
1636 consider_token (str
, len
, c
, c_ext
, cblev
, is_func
)
1637 register char *str
; /* IN: token pointer */
1638 register int len
; /* IN: token length */
1639 register char c
; /* IN: first char after the token */
1640 int c_ext
; /* IN: C extensions mask */
1641 int cblev
; /* IN: curly brace level */
1642 logical
*is_func
; /* OUT: function found */
1644 enum sym_type toktype
= C_symtype (str
, len
, c_ext
);
1647 * Advance the definedef state machine.
1652 /* We're not on a preprocessor line. */
1655 if (toktype
== st_C_define
)
1657 definedef
= ddefineseen
;
1661 definedef
= dignorerest
;
1666 * Make a tag for any macro, unless it is a constant
1667 * and constantypedefs is FALSE.
1669 definedef
= dignorerest
;
1670 *is_func
= (c
== '(');
1671 if (!*is_func
&& !constantypedefs
)
1678 error ("internal error: definedef value.", 0);
1687 if (toktype
== st_C_typedef
)
1690 typdef
= ttypedseen
;
1706 /* Do not return here, so the structdef stuff has a chance. */
1720 * This structdef business is currently only invoked when cblev==0.
1721 * It should be recursively invoked whatever the curly brace level,
1722 * and a stack of states kept, to allow for definitions of structs
1725 * This structdef business is NOT invoked when we are ctags and the
1726 * file is plain C. This is because a struct tag may have the same
1727 * name as another tag, and this loses with ctags.
1729 * This if statement deals with the typdef state machine as
1730 * follows: if typdef==ttypedseen and token is struct/union/class/enum,
1731 * return FALSE. All the other code here is for the structdef
1738 if (typdef
== ttypedseen
1739 || (typedefs_and_cplusplus
&& cblev
== 0 && structdef
== snone
))
1741 structdef
= skeyseen
;
1742 structtype
= toktype
;
1746 if (structdef
== skeyseen
)
1748 /* Save the tag for struct/union/class, for functions that may be
1750 if (structtype
== st_C_struct
)
1751 structtag
= savenstr (str
, len
);
1753 structtag
= "<enum>";
1754 structdef
= stagseen
;
1758 /* Avoid entering funcdef stuff if typdef is going on. */
1759 if (typdef
!= tnone
)
1765 /* Detect GNU macros. */
1766 if (definedef
== dnone
)
1767 if (strneq (str
, "DEFUN", 5) /* Used in emacs */
1769 These are defined inside C functions
, so currently they
1771 || strneq (str
, "EXFUN", 5) /* Used in glibc */
1772 || strneq (str
, "DEFVAR_", 7) /* Used in emacs */
1774 || strneq (str
, "SYSCALL", 7) /* Used in glibc (mach) */
1775 || strneq (str
, "ENTRY", 5) /* Used in glibc */
1776 || strneq (str
, "PSEUDO", 6)) /* Used in glibc */
1779 next_token_is_func
= TRUE
;
1782 if (next_token_is_func
)
1784 next_token_is_func
= FALSE
;
1794 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1795 funcdef
= fnone
; /* should be useless */
1798 if (funcdef
== fnone
)
1811 * This routine finds functions, typedefs, #define's and
1812 * struct/union/enum definitions in C syntax and adds them
1826 #define current_lb_is_new (newndx == curndx)
1827 #define switch_line_buffers() (curndx = 1 - curndx)
1829 #define curlb (lbs[curndx].lb)
1830 #define othlb (lbs[1-curndx].lb)
1831 #define newlb (lbs[newndx].lb)
1832 #define curlinepos (lbs[curndx].linepos)
1833 #define othlinepos (lbs[1-curndx].linepos)
1834 #define newlinepos (lbs[newndx].linepos)
1836 #define CNL_SAVE_DEFINEDEF \
1838 curlinepos = charno; \
1840 charno += readline (&curlb, inf); \
1841 lp = curlb.buffer; \
1848 CNL_SAVE_DEFINEDEF; \
1849 if (savetok.valid) \
1852 savetok.valid = FALSE; \
1854 definedef = dnone; \
1857 #define make_tag(isfun) do \
1860 pfnote (savestr (token_name.buffer), isfun, tok.named, \
1861 tok.buffer, tok.linelen, tok.lineno, tok.linepos); \
1862 else if (DEBUG) abort (); \
1863 tok.valid = FALSE; \
1867 C_entries (c_ext
, inf
)
1868 int c_ext
; /* extension of C */
1869 FILE *inf
; /* input file */
1871 register char c
; /* latest char read; '\0' for end of line */
1872 register char *lp
; /* pointer one beyond the character `c' */
1873 int curndx
, newndx
; /* indices for current and new lb */
1874 TOKEN tok
; /* latest token read */
1875 register int tokoff
; /* offset in line of start of current token */
1876 register int toklen
; /* length of current token */
1877 int cblev
; /* current curly brace level */
1878 int parlev
; /* current parenthesis level */
1879 logical incomm
, inquote
, inchar
, quotednl
, midtoken
;
1881 TOKEN savetok
; /* token saved during preprocessor handling */
1884 curndx
= newndx
= 0;
1890 definedef
= dnone
; funcdef
= fnone
; typdef
= tnone
; structdef
= snone
;
1891 next_token_is_func
= yacc_rules
= FALSE
;
1892 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
1893 tok
.valid
= savetok
.valid
= FALSE
;
1896 cplpl
= c_ext
& C_PLPL
;
1903 /* If we're at the end of the line, the next character is a
1904 '\0'; don't skip it, because it's the thing that tells us
1905 to read the next line. */
1926 /* Newlines inside comments do not end macro definitions in
1941 /* Newlines inside strings do not end macro definitions
1942 in traditional cpp, even though compilers don't
1943 usually accept them. */
1954 /* Hmmm, something went wrong. */
1968 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1973 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1983 else if (cplpl
&& *lp
== '/')
1991 if ((c_ext
& YACC
) && *lp
== '%')
1993 /* entering or exiting rules section in yacc file */
1995 definedef
= dnone
; funcdef
= fnone
;
1996 typdef
= tnone
; structdef
= snone
;
1997 next_token_is_func
= FALSE
;
1998 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
2000 yacc_rules
= !yacc_rules
;
2006 if (definedef
== dnone
)
2009 logical cpptoken
= TRUE
;
2011 /* Look back on this line. If all blanks, or nonblanks
2012 followed by an end of comment, this is a preprocessor
2014 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
2017 if (*cp
== '*' && *(cp
+1) == '/')
2026 definedef
= dsharpseen
;
2027 } /* if (definedef == dnone) */
2033 /* Consider token only if some complicated conditions are satisfied. */
2034 if ((definedef
!= dnone
2035 || (cblev
== 0 && structdef
!= scolonseen
)
2036 || (cblev
== 1 && cplpl
&& structdef
== sinbody
))
2037 && typdef
!= tignore
2038 && definedef
!= dignorerest
2039 && funcdef
!= finlist
)
2045 if (cplpl
&& c
== ':' && *lp
== ':' && begtoken(*(lp
+ 1)))
2048 * This handles :: in the middle, but not at the
2049 * beginning of an identifier.
2056 logical is_func
= FALSE
;
2059 || consider_token (newlb
.buffer
+ tokoff
, toklen
,
2060 c
, c_ext
, cblev
, &is_func
))
2062 if (structdef
== sinbody
2063 && definedef
== dnone
2065 /* function defined in C++ class body */
2067 int strsize
= strlen(structtag
) + 2 + toklen
+ 1;
2068 while (token_name
.size
< strsize
)
2070 token_name
.size
*= 2;
2071 token_name
.buffer
=xrealloc(token_name
.buffer
,
2074 strcpy (token_name
.buffer
, structtag
);
2075 strcat (token_name
.buffer
, "::");
2076 strncat (token_name
.buffer
,
2077 newlb
.buffer
+tokoff
, toklen
);
2082 while (token_name
.size
< toklen
+ 1)
2084 token_name
.size
*= 2;
2085 token_name
.buffer
=xrealloc(token_name
.buffer
,
2088 strncpy (token_name
.buffer
,
2089 newlb
.buffer
+tokoff
, toklen
);
2090 token_name
.buffer
[toklen
] = '\0';
2091 if (structdef
== stagseen
2094 && definedef
== dignorerest
)) /* macro */
2099 tok
.lineno
= lineno
;
2100 tok
.linelen
= tokoff
+ toklen
+ 1;
2101 tok
.buffer
= newlb
.buffer
;
2102 tok
.linepos
= newlinepos
;
2105 if (definedef
== dnone
2106 && (funcdef
== ftagseen
2107 || structdef
== stagseen
2110 if (current_lb_is_new
)
2111 switch_line_buffers ();
2118 } /* if (endtoken (c)) */
2119 else if (intoken (c
))
2124 } /* if (midtoken) */
2125 else if (begtoken (c
))
2143 if (structdef
== stagseen
)
2149 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
2151 tokoff
= lp
- 1 - newlb
.buffer
;
2156 } /* if (begtoken) */
2157 } /* if must look at token */
2160 /* Detect end of line, colon, comma, semicolon and various braces
2161 after having handled a token.*/
2165 if (definedef
!= dnone
)
2167 if (structdef
== stagseen
)
2168 structdef
= scolonseen
;
2185 if (definedef
!= dnone
)
2196 if (funcdef
!= fignore
)
2198 if (structdef
== stagseen
)
2202 if (definedef
!= dnone
)
2204 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2206 if (structdef
== stagseen
)
2210 if (definedef
!= dnone
)
2212 if (cblev
== 0 && typdef
== tend
)
2218 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2220 if (structdef
== stagseen
)
2224 if (definedef
!= dnone
)
2233 /* Make sure that the next char is not a '*'.
2234 This handles constructs like:
2235 typedef void OperatorFun (int fun); */
2242 } /* switch (typdef) */
2245 funcdef
= fstartlist
;
2254 if (definedef
!= dnone
)
2262 funcdef
= flistseen
;
2265 if (cblev
== 0 && typdef
== tend
)
2271 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
2275 if (definedef
!= dnone
)
2277 if (typdef
== ttypedseen
)
2281 case skeyseen
: /* unnamed struct */
2282 structtag
= "_anonymous_";
2283 structdef
= sinbody
;
2286 case scolonseen
: /* named struct */
2287 structdef
= sinbody
;
2300 /* Neutralize `extern "C" {' grot and look inside structs. */
2301 if (cblev
== 0 && structdef
== snone
&& typdef
== tnone
)
2307 if (definedef
!= dnone
)
2309 if (funcdef
== fstartlist
)
2310 funcdef
= fnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
2313 if (definedef
!= dnone
)
2315 if (!noindentypedefs
&& lp
== newlb
.buffer
+ 1)
2317 cblev
= 0; /* reset curly brace level if first column */
2318 parlev
= 0; /* also reset paren level, just in case... */
2324 if (typdef
== tinbody
)
2326 #if FALSE /* too risky */
2327 if (structdef
== sinbody
)
2332 structtag
= "<error>";
2336 case '#': case '+': case '-': case '~': case '&': case '%': case '/':
2337 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
2338 if (definedef
!= dnone
)
2340 /* These surely cannot follow a function tag. */
2341 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2345 /* If a macro spans multiple lines don't reset its state. */
2353 } /* while not eof */
2357 * Process either a C++ file or a C file depending on the setting
2361 default_C_entries (inf
)
2364 C_entries (cplusplus
? C_PLPL
: 0, inf
);
2367 /* Always do C++. */
2369 Cplusplus_entries (inf
)
2372 C_entries (C_PLPL
, inf
);
2380 C_entries (C_STAR
, inf
);
2383 /* Always do Yacc. */
2388 C_entries (YACC
, inf
);
2391 /* Fortran parsing */
2399 register int len
= 0;
2401 while (*cp
&& (*cp
| ' ') == (dbp
[len
] | ' '))
2414 while (isspace (*dbp
))
2419 while (isspace (*dbp
))
2423 if (!isdigit (*dbp
))
2425 --dbp
; /* force failure */
2430 while (isdigit (*dbp
));
2439 while (isspace (*dbp
))
2444 linecharno
= charno
;
2445 charno
+= readline (&lb
, inf
);
2450 while (isspace (*dbp
))
2459 && (isalpha (*cp
) || isdigit (*cp
) || (*cp
== '_') || (*cp
== '$')));
2462 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
, FALSE
, lb
.buffer
,
2463 cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2467 Fortran_functions (inf
)
2476 linecharno
= charno
;
2477 charno
+= readline (&lb
, inf
);
2480 dbp
++; /* Ratfor escape to fortran */
2481 while (isspace (*dbp
))
2488 if (tail ("integer"))
2496 if (tail ("logical"))
2500 if (tail ("complex") || tail ("character"))
2504 if (tail ("double"))
2506 while (isspace (*dbp
))
2510 if (tail ("precision"))
2516 while (isspace (*dbp
))
2523 if (tail ("function"))
2527 if (tail ("subroutine"))
2535 if (tail ("program"))
2540 if (tail ("procedure"))
2548 * Bob Weiner, Motorola Inc., 4/3/94
2549 * Unix and microcontroller assembly tag handling
2550 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
2564 linecharno
= charno
;
2565 charno
+= readline (&lb
, inf
);
2568 /* If first char is alphabetic or one of [_.$], test for colon
2569 following identifier. */
2570 if (isalpha (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2572 /* Read past label. */
2574 while (isalnum (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2576 if (*cp
== ':' || isspace (*cp
))
2578 /* Found end of label, so copy it and add it to the table. */
2579 pfnote (savenstr (lb
.buffer
, cp
-lb
.buffer
), TRUE
, FALSE
,
2580 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2586 /* Added by Mosur Mohan, 4/22/88 */
2587 /* Pascal parsing */
2589 #define GET_NEW_LINE \
2591 linecharno = charno; lineno++; \
2592 charno += 1 + readline (&lb, inf); \
2597 * Locates tags for procedures & functions. Doesn't do any type- or
2598 * var-definitions. It does look for the keyword "extern" or
2599 * "forward" immediately following the procedure statement; if found,
2600 * the tag is skipped.
2603 Pascal_functions (inf
)
2606 struct linebuffer tline
; /* mostly copied from C_entries */
2612 logical
/* each of these flags is TRUE iff: */
2613 incomment
, /* point is inside a comment */
2614 inquote
, /* point is inside '..' string */
2615 get_tagname
, /* point is after PROCEDURE/FUNCTION */
2616 /* keyword, so next item = potential tag */
2617 found_tag
, /* point is after a potential tag */
2618 inparms
, /* point is within parameter-list */
2619 verify_tag
; /* point has passed the parm-list, so the */
2620 /* next token will determine whether */
2621 /* this is a FORWARD/EXTERN to be */
2622 /* ignored, or whether it is a real tag */
2628 initbuffer (&tline
);
2630 incomment
= inquote
= FALSE
;
2631 found_tag
= FALSE
; /* have a proc name; check if extern */
2632 get_tagname
= FALSE
; /* have found "procedure" keyword */
2633 inparms
= FALSE
; /* found '(' after "proc" */
2634 verify_tag
= FALSE
; /* check if "extern" is ahead */
2636 /* long main loop to get next char */
2640 if (c
== '\0') /* if end of line */
2645 if (!((found_tag
&& verify_tag
) ||
2647 c
= *dbp
++; /* only if don't need *dbp pointing */
2648 /* to the beginning of the name of */
2649 /* the procedure or function */
2653 if (c
== '}') /* within { - } comments */
2655 else if (c
== '*' && dbp
[1] == ')') /* within (* - *) comments */
2672 inquote
= TRUE
; /* found first quote */
2674 case '{': /* found open-{-comment */
2678 if (*dbp
== '*') /* found open-(*-comment */
2683 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
2686 case ')': /* end of parms list */
2691 if ((found_tag
) && (!inparms
)) /* end of proc or fn stmt */
2698 if ((found_tag
) && (verify_tag
) && (*dbp
!= ' '))
2700 /* check if this is an "extern" declaration */
2703 if ((*dbp
== 'e') || (*dbp
== 'E'))
2705 if (tail ("extern")) /* superfluous, really! */
2711 else if ((*dbp
== 'f') || (*dbp
== 'F'))
2713 if (tail ("forward")) /* check for forward reference */
2719 if ((found_tag
) && (verify_tag
)) /* not external proc, so make tag */
2723 pfnote (nambuf
, TRUE
, FALSE
, tline
.buffer
,
2724 cp
- tline
.buffer
+ 1, save_lineno
, save_lcno
);
2728 if (get_tagname
) /* grab name of proc or fn */
2733 /* save all values for later tagging */
2734 tline
.size
= lb
.size
;
2735 strcpy (tline
.buffer
, lb
.buffer
);
2736 save_lineno
= lineno
;
2737 save_lcno
= linecharno
;
2739 /* grab block name */
2740 for (cp
= dbp
+ 1; *cp
&& (!endtoken (*cp
)); cp
++)
2742 nambuf
= savenstr (dbp
, cp
-dbp
);
2743 dbp
= cp
; /* restore dbp to e-o-token */
2744 get_tagname
= FALSE
;
2748 /* and proceed to check for "extern" */
2750 else if (!incomment
&& !inquote
&& !found_tag
)
2752 /* check for proc/fn keywords */
2756 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
2760 if (tail ("unction"))
2765 } /* while not eof */
2769 * lisp tag functions
2770 * look for (def or (DEF, quote or QUOTE
2774 register char *strp
;
2776 return ((strp
[1] == 'd' || strp
[1] == 'D')
2777 && (strp
[2] == 'e' || strp
[2] == 'E')
2778 && (strp
[3] == 'f' || strp
[3] == 'F'));
2783 register char *strp
;
2785 return ((*(++strp
) == 'q' || *strp
== 'Q')
2786 && (*(++strp
) == 'u' || *strp
== 'U')
2787 && (*(++strp
) == 'o' || *strp
== 'O')
2788 && (*(++strp
) == 't' || *strp
== 'T')
2789 && (*(++strp
) == 'e' || *strp
== 'E')
2790 && isspace(*(++strp
)));
2798 if (*dbp
== '\'') /* Skip prefix quote */
2800 else if (*dbp
== '(' && L_isquote (dbp
)) /* Skip "(quote " */
2803 while (isspace(*dbp
))
2806 for (cp
= dbp
/*+1*/;
2807 *cp
&& *cp
!= '(' && *cp
!= ' ' && *cp
!= ')';
2813 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
, FALSE
, lb
.buffer
,
2814 cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2818 Lisp_functions (inf
)
2827 linecharno
= charno
;
2828 charno
+= readline (&lb
, inf
);
2834 while (!isspace (*dbp
))
2836 while (isspace (*dbp
))
2842 /* Check for (foo::defmumble name-defined ... */
2845 while (*dbp
&& !isspace (*dbp
)
2846 && *dbp
!= ':' && *dbp
!= '(' && *dbp
!= ')');
2851 while (*dbp
== ':');
2853 if (L_isdef (dbp
- 1))
2855 while (!isspace (*dbp
))
2857 while (isspace (*dbp
))
2868 * Scheme tag functions
2869 * look for (def... xyzzy
2870 * look for (def... (xyzzy
2871 * look for (def ... ((...(xyzzy ....
2872 * look for (set! xyzzy
2878 Scheme_functions (inf
)
2887 linecharno
= charno
;
2888 charno
+= readline (&lb
, inf
);
2890 if (dbp
[0] == '(' &&
2891 (dbp
[1] == 'D' || dbp
[1] == 'd') &&
2892 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
2893 (dbp
[3] == 'F' || dbp
[3] == 'f'))
2895 while (!isspace (*dbp
))
2897 /* Skip over open parens and white space */
2898 while (*dbp
&& (isspace (*dbp
) || *dbp
== '('))
2902 if (dbp
[0] == '(' &&
2903 (dbp
[1] == 'S' || dbp
[1] == 's') &&
2904 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
2905 (dbp
[3] == 'T' || dbp
[3] == 't') &&
2906 (dbp
[4] == '!' || dbp
[4] == '!') &&
2909 while (!isspace (*dbp
))
2911 /* Skip over white space */
2912 while (isspace (*dbp
))
2926 /* Go till you get to white space or a syntactic break */
2928 *cp
&& *cp
!= '(' && *cp
!= ')' && !isspace (*cp
);
2931 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
, FALSE
,
2932 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2935 /* Find tags in TeX and LaTeX input files. */
2937 /* TEX_toktab is a table of TeX control sequences that define tags.
2938 Each TEX_tabent records one such control sequence.
2939 CONVERT THIS TO USE THE Stab TYPE!! */
2946 struct TEX_tabent
*TEX_toktab
= NULL
; /* Table with tag tokens */
2948 /* Default set of control sequences to put into TEX_toktab.
2949 The value of environment var TEXTAGS is prepended to this. */
2951 char *TEX_defenv
= "\
2952 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem:typeout";
2955 struct TEX_tabent
*TEX_decode_env ();
2959 char TEX_esc
= '\\';
2960 char TEX_opgrp
= '{';
2961 char TEX_clgrp
= '}';
2964 * TeX/LaTeX scanning loop.
2975 /* Select either \ or ! as escape character. */
2978 /* Initialize token table once from environment. */
2980 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
2983 { /* Scan each line in file */
2985 linecharno
= charno
;
2986 charno
+= readline (&lb
, inf
);
2989 while (dbp
= etags_strchr (dbp
, TEX_esc
)) /* Look at each esc in line */
2995 linecharno
+= dbp
- lasthit
;
2997 i
= TEX_Token (lasthit
);
3000 TEX_getit (lasthit
, TEX_toktab
[i
].len
);
3001 break; /* We only save a line once */
3007 #define TEX_LESC '\\'
3008 #define TEX_SESC '!'
3011 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
3012 chars accordingly. */
3019 while ((c
= getc (inf
)) != EOF
)
3021 /* Skip to next line if we hit the TeX comment char. */
3025 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
3044 /* Read environment and prepend it to the default string.
3045 Build token table. */
3047 TEX_decode_env (evarname
, defenv
)
3051 register char *env
, *p
;
3053 struct TEX_tabent
*tab
;
3056 /* Append default string to environment. */
3057 env
= getenv (evarname
);
3061 env
= concat (env
, defenv
, "");
3063 /* Allocate a token table */
3064 for (size
= 1, p
= env
; p
;)
3065 if ((p
= etags_strchr (p
, ':')) && *(++p
))
3067 /* Add 1 to leave room for null terminator. */
3068 tab
= xnew (size
+ 1, struct TEX_tabent
);
3070 /* Unpack environment string into token table. Be careful about */
3071 /* zero-length strings (leading ':', "::" and trailing ':') */
3074 p
= etags_strchr (env
, ':');
3075 if (!p
) /* End of environment string. */
3076 p
= env
+ strlen (env
);
3078 { /* Only non-zero strings. */
3079 tab
[i
].name
= savenstr (env
, p
- env
);
3080 tab
[i
].len
= strlen (tab
[i
].name
);
3087 tab
[i
].name
= NULL
; /* Mark end of table. */
3095 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
3096 The name being defined actually starts at (NAME + LEN + 1).
3097 But we seem to include the TeX command in the tag name. */
3099 TEX_getit (name
, len
)
3103 char *p
= name
+ len
;
3108 /* Let tag name extend to next group close (or end of line) */
3109 while (*p
&& *p
!= TEX_clgrp
)
3111 pfnote (savenstr (name
, p
-name
), TRUE
, FALSE
, lb
.buffer
,
3112 strlen (lb
.buffer
), lineno
, linecharno
);
3115 /* If the text at CP matches one of the tag-defining TeX command names,
3116 return the pointer to the first occurrence of that command in TEX_toktab.
3117 Otherwise return -1.
3118 Keep the capital `T' in `Token' for dumb truncating compilers
3119 (this distinguishes it from `TEX_toktab' */
3126 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
3127 if (strneq (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
))
3132 /* Support for Prolog. */
3134 /* Whole head (not only functor, but also arguments)
3135 is gotten in compound term. */
3148 if (s
[0] == '\0') /* syntax error. */
3150 else if (insquote
&& s
[0] == '\'' && s
[1] == '\'')
3152 else if (s
[0] == '\'')
3154 insquote
= !insquote
;
3157 else if (!insquote
&& s
[0] == '(')
3162 else if (!insquote
&& s
[0] == ')')
3168 else if (npar
< 0) /* syntax error. */
3171 else if (!insquote
&& s
[0] == '.'
3172 && (isspace (s
[1]) || s
[1] == '\0'))
3174 if (npar
!= 0) /* syntax error. */
3182 pfnote (savenstr (save_s
, s
-save_s
), TRUE
, FALSE
,
3183 save_s
, s
-save_s
, lineno
, linecharno
);
3186 /* It is assumed that prolog predicate starts from column 0. */
3188 Prolog_functions (inf
)
3191 void skip_comment (), prolog_getit ();
3193 lineno
= linecharno
= charno
= 0;
3197 linecharno
+= charno
;
3198 charno
= readline (&lb
, inf
) + 1; /* 1 for newline. */
3200 if (isspace (dbp
[0])) /* not predicate header. */
3202 else if (dbp
[0] == '%') /* comment. */
3204 else if (dbp
[0] == '/' && dbp
[1] == '*') /* comment. */
3205 skip_comment (&lb
, inf
, &lineno
, &linecharno
);
3212 skip_comment (plb
, inf
, plineno
, plinecharno
)
3213 struct linebuffer
*plb
;
3215 int *plineno
; /* result */
3216 long *plinecharno
; /* result */
3222 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
3223 if (cp
[0] == '*' && cp
[1] == '/')
3226 *plinecharno
+= readline (plb
, inf
) + 1; /* 1 for newline. */
3231 #ifdef ETAGS_REGEXPS
3232 /* Take a string like "/blah/" and turn it into "blah", making sure
3233 that the first and last characters are the same, and handling
3234 quoted separator characters. Actually, stops on the occurence of
3235 an unquoted separator. Also turns "\t" into a Tab character.
3236 Returns pointer to terminating separator. Works in place. Null
3237 terminates name string. */
3239 scan_separators (name
)
3243 char *copyto
= name
;
3244 logical quoted
= FALSE
;
3246 for (++name
; *name
!= '\0'; ++name
)
3252 else if (*name
== sep
)
3256 /* Something else is quoted, so preserve the quote. */
3262 else if (*name
== '\\')
3264 else if (*name
== sep
)
3270 /* Terminate copied string. */
3275 /* Turn a name, which is an ed-style (but Emacs syntax) regular
3276 expression, into a real regular expression by compiling it. */
3278 add_regex (regexp_pattern
)
3279 char *regexp_pattern
;
3283 struct re_pattern_buffer
*patbuf
;
3285 if (regexp_pattern
== NULL
)
3287 /* Remove existing regexps. */
3293 if (regexp_pattern
[0] == '\0')
3295 error ("missing regexp", 0);
3298 if (regexp_pattern
[strlen(regexp_pattern
)-1] != regexp_pattern
[0])
3300 error ("%s: unterminated regexp", regexp_pattern
);
3303 name
= scan_separators (regexp_pattern
);
3304 if (regexp_pattern
[0] == '\0')
3306 error ("null regexp", 0);
3309 (void) scan_separators (name
);
3311 patbuf
= xnew (1, struct re_pattern_buffer
);
3312 patbuf
->translate
= NULL
;
3313 patbuf
->fastmap
= NULL
;
3314 patbuf
->buffer
= NULL
;
3315 patbuf
->allocated
= 0;
3317 err
= re_compile_pattern (regexp_pattern
, strlen (regexp_pattern
), patbuf
);
3320 error ("%s while compiling pattern", err
);
3325 if (num_patterns
== 1)
3326 patterns
= xnew (1, struct pattern
);
3328 patterns
= ((struct pattern
*)
3330 (num_patterns
* sizeof (struct pattern
))));
3331 patterns
[num_patterns
- 1].pattern
= patbuf
;
3332 patterns
[num_patterns
- 1].name_pattern
= savestr (name
);
3333 patterns
[num_patterns
- 1].error_signaled
= FALSE
;
3337 * Do the subtitutions indicated by the regular expression and
3341 substitute (in
, out
, regs
)
3343 struct re_registers
*regs
;
3345 char *result
= NULL
, *t
;
3348 /* Pass 1: figure out how much size to allocate. */
3349 for (t
= out
; *t
; ++t
)
3356 fprintf (stderr
, "%s: pattern subtitution ends prematurely\n",
3363 size
+= regs
->end
[dig
] - regs
->start
[dig
];
3368 /* Allocate space and do the substitutions. */
3369 result
= xnew (size
+ 1, char);
3378 /* Using "dig2" satisfies my debugger. Bleah. */
3379 int dig2
= *out
- '0';
3380 strncpy (result
+ size
, in
+ regs
->start
[dig2
],
3381 regs
->end
[dig2
] - regs
->start
[dig2
]);
3382 size
+= regs
->end
[dig2
] - regs
->start
[dig2
];
3389 result
[size
++] = '\t';
3395 result
[size
++] = *out
;
3401 result
[size
++] = *out
;
3403 result
[size
] = '\0';
3408 #endif /* ETAGS_REGEXPS */
3409 /* Initialize a linebuffer for use */
3411 initbuffer (linebuffer
)
3412 struct linebuffer
*linebuffer
;
3414 linebuffer
->size
= 200;
3415 linebuffer
->buffer
= xnew (200, char);
3419 * Read a line of text from `stream' into `linebuffer'.
3420 * Return the number of characters read from `stream',
3421 * which is the length of the line including the newline, if any.
3424 readline_internal (linebuffer
, stream
)
3425 struct linebuffer
*linebuffer
;
3426 register FILE *stream
;
3428 char *buffer
= linebuffer
->buffer
;
3429 register char *p
= linebuffer
->buffer
;
3430 register char *pend
;
3433 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
3437 register int c
= getc (stream
);
3440 linebuffer
->size
*= 2;
3441 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
3442 p
+= buffer
- linebuffer
->buffer
;
3443 pend
= buffer
+ linebuffer
->size
;
3444 linebuffer
->buffer
= buffer
;
3453 if (p
> buffer
&& p
[-1] == '\r')
3468 return p
- buffer
+ chars_deleted
;
3472 * Like readline_internal, above, but try to match the input
3473 * line against any existing regular expressions.
3476 readline (linebuffer
, stream
)
3477 struct linebuffer
*linebuffer
;
3480 /* Read new line. */
3482 long result
= readline_internal (linebuffer
, stream
);
3484 #ifdef ETAGS_REGEXPS
3485 /* Match against all listed patterns. */
3486 for (i
= 0; i
< num_patterns
; ++i
)
3488 int match
= re_match (patterns
[i
].pattern
, linebuffer
->buffer
,
3489 (int)result
, 0, &patterns
[i
].regs
);
3494 if (!patterns
[i
].error_signaled
)
3496 error ("error while matching pattern %d", i
);
3497 patterns
[i
].error_signaled
= TRUE
;
3504 /* Match occurred. Construct a tag. */
3505 if (patterns
[i
].name_pattern
[0] != '\0')
3507 /* Make a named tag. */
3508 char *name
= substitute (linebuffer
->buffer
,
3509 patterns
[i
].name_pattern
,
3512 pfnote (name
, TRUE
, TRUE
, linebuffer
->buffer
,
3513 match
, lineno
, linecharno
);
3517 /* Make an unnamed tag. */
3518 pfnote (NULL
, TRUE
, FALSE
, linebuffer
->buffer
,
3519 match
, lineno
, linecharno
);
3524 #endif /* ETAGS_REGEXPS */
3530 * Read a file, but do no processing. This is used to do regexp
3531 * matching on files that have no language defined.
3534 just_read_file (inf
)
3540 linecharno
= charno
;
3541 charno
+= readline (&lb
, inf
) + 1;
3547 * Return a pointer to a space of size strlen(cp)+1 allocated
3548 * with xnew where the string CP has been copied.
3554 return savenstr (cp
, strlen (cp
));
3558 * Return a pointer to a space of size LEN+1 allocated with xnew where
3559 * the string CP has been copied for at most the first LEN characters.
3568 dp
= xnew (len
+ 1, char);
3569 strncpy (dp
, cp
, len
);
3575 * Return the ptr in sp at which the character c last
3576 * appears; NULL if not found
3578 * Identical to System V strrchr, included for portability.
3581 etags_strrchr (sp
, c
)
3582 register char *sp
, c
;
3597 * Return the ptr in sp at which the character c first
3598 * appears; NULL if not found
3600 * Identical to System V strchr, included for portability.
3603 etags_strchr (sp
, c
)
3604 register char *sp
, c
;
3614 /* Print error message and exit. */
3631 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
3636 fprintf (stderr
, "%s: ", progname
);
3637 fprintf (stderr
, s1
, s2
);
3638 fprintf (stderr
, "\n");
3641 /* Return a newly-allocated string whose contents
3642 concatenate those of s1, s2, s3. */
3647 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
3648 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
3650 strcpy (result
, s1
);
3651 strcpy (result
+ len1
, s2
);
3652 strcpy (result
+ len1
+ len2
, s3
);
3653 result
[len1
+ len2
+ len3
] = '\0';
3658 /* Does the same work as the system V getcwd, but does not need to
3659 guess buffer size in advance. */
3664 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
3672 *p
++ = tolower (*p
);
3674 return strdup (path
);
3676 #elif HAVE_GETCWD /* not DOS_NT */
3679 char *path
= xnew (bufsize
, char);
3681 while (getcwd (path
, bufsize
) == NULL
)
3683 if (errno
!= ERANGE
)
3686 path
= xnew (bufsize
, char);
3691 #else /* not DOS_NT and not HAVE_GETCWD */
3693 struct linebuffer path
;
3697 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
3698 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
3704 #endif /* not DOS_NT and not HAVE_GETCWD */
3706 /* Return a newly allocated string containing the filename
3707 of FILE relative to the absolute directory DIR (which
3708 should end with a slash). */
3710 relative_filename (file
, dir
)
3713 char *fp
, *dp
, *res
;
3715 /* Find the common root of file and dir. */
3716 fp
= absolute_filename (file
, cwd
);
3718 while (*fp
++ == *dp
++)
3727 /* Build a sequence of "../" strings for the resulting relative filename. */
3728 for (dp
= etags_strchr (dp
+ 1, '/'), res
= "";
3730 dp
= etags_strchr (dp
+ 1, '/'))
3732 res
= concat (res
, "../", "");
3735 /* Add the filename relative to the common root of file and dir. */
3736 res
= concat (res
, fp
+ 1, "");
3738 return res
; /* temporary stub */
3741 /* Return a newly allocated string containing the
3742 absolute filename of FILE given CWD (which should
3743 end with a slash). */
3745 absolute_filename (file
, cwd
)
3748 char *slashp
, *cp
, *res
;
3751 res
= concat (file
, "", "");
3753 res
= concat (cwd
, file
, "");
3755 /* Delete the "/dirname/.." and "/." substrings. */
3756 slashp
= etags_strchr (res
, '/');
3757 while (slashp
!= NULL
&& slashp
[0] != '\0')
3759 if (slashp
[1] == '.')
3761 if (slashp
[2] == '.'
3762 && (slashp
[3] == '/' || slashp
[3] == '\0'))
3767 while (cp
>= res
&& *cp
!= '/');
3770 strcpy (cp
, slashp
+ 3);
3772 else /* else (cp == res) */
3774 if (slashp
[3] != '\0')
3775 strcpy (cp
, slashp
+ 4);
3782 else if (slashp
[2] == '/' || slashp
[2] == '\0')
3784 strcpy (slashp
, slashp
+ 2);
3789 slashp
= etags_strchr (slashp
+ 1, '/');
3795 /* Return a newly allocated string containing the absolute
3796 filename of dir where FILE resides given CWD (which should
3797 end with a slash). */
3799 absolute_dirname (file
, cwd
)
3805 slashp
= etags_strrchr (file
, '/');
3810 res
= absolute_filename (file
, cwd
);
3816 /* Like malloc but get fatal error if memory is exhausted. */
3821 char *result
= (char *) malloc (size
);
3823 fatal ("virtual memory exhausted", 0);
3828 xrealloc (ptr
, size
)
3832 char *result
= (char *) realloc (ptr
, size
);
3834 fatal ("virtual memory exhausted");