1 /* Tags file maker to go with GNU Emacs
2 Copyright (C) 1984,87,88,89,93,94 Free Software Foundation, Inc. and Ken Arnold
3 This file is not considered part of GNU Emacs.
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 2 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 * Ctags originally by Ken Arnold.
22 * Fortran added by Jim Kleckner.
23 * Ed Pelegri-Llopart added C typedefs.
24 * Gnu Emacs TAGS format and modifications by RMS?
25 * Sam Kendall added C++.
26 * Francesco Potorti` reorganised C and C++ based on work by Joe Wells.
28 * Regexp tags by Tom Tromey.
31 * Francesco Potorti` (pot@cnuce.cnr.it) is the current maintainer.
34 char pot_etags_version
[] = "@(#) pot revision number is 11.25";
44 #include <sys/param.h>
51 #define MAXPATHLEN _MAX_PATH
56 /* On some systems, Emacs defines static as nothing for the sake
57 of unexec. We don't want that here since we don't use unexec. */
67 #include <sys/types.h>
70 #if !defined (S_ISREG) && defined (S_IFREG)
71 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
78 #endif /* ETAGS_REGEXPS */
80 /* Define CTAGS to make the program "ctags" compatible with the usual one.
81 Let it undefined to make the program "etags", which makes emacs-style
82 tag tables and tags typedefs, #defines and struct/union/enum by default. */
90 /* Exit codes for success and failure. */
100 #define C_PLPL 0x00001 /* C++ */
101 #define C_STAR 0x00003 /* C* */
102 #define YACC 0x10000 /* yacc file */
104 #define streq(s,t) (strcmp (s, t) == 0)
105 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
107 #define iswhite(arg) (_wht[arg]) /* T if char is white */
108 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
109 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
110 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
113 * xnew -- allocate storage
115 * SYNOPSIS: Type *xnew (int n, Type);
117 #define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
122 { /* sorting structure */
123 char *name
; /* function or type name */
124 char *file
; /* file name */
125 logical is_func
; /* use pattern or line no */
126 logical named
; /* list name separately */
127 logical been_warned
; /* set if noticed dup */
128 int lno
; /* line number tag is on */
129 long cno
; /* character number line starts on */
130 char *pat
; /* search pattern */
131 struct nd_st
*left
, *right
; /* left and right sons */
134 extern char *getenv ();
137 char *savenstr (), *savestr ();
138 char *etags_strchr (), *etags_strrchr ();
139 char *etags_getcwd ();
140 char *relative_filename (), *absolute_filename (), *absolute_dirname ();
141 char *xmalloc (), *xrealloc ();
143 typedef void Lang_function ();
144 #if FALSE /* many compilers barf on this */
145 Lang_function Asm_labels
;
146 Lang_function default_C_entries
;
147 Lang_function C_entries
;
148 Lang_function Cplusplus_entries
;
149 Lang_function Cstar_entries
;
150 Lang_function Fortran_functions
;
151 Lang_function Yacc_entries
;
152 Lang_function Lisp_functions
;
153 Lang_function Pascal_functions
;
154 Lang_function Prolog_functions
;
155 Lang_function Scheme_functions
;
156 Lang_function TeX_functions
;
157 Lang_function just_read_file
;
158 #else /* so let's write it this way */
160 void default_C_entries ();
162 void Cplusplus_entries ();
163 void Cstar_entries ();
164 void Fortran_functions ();
165 void Yacc_entries ();
166 void Lisp_functions ();
167 void Pascal_functions ();
168 void Prolog_functions ();
169 void Scheme_functions ();
170 void TeX_functions ();
171 void just_read_file ();
174 logical
get_language ();
175 int total_size_of_entries ();
177 long readline_internal ();
183 void fatal (), pfatal ();
184 void find_entries ();
190 void process_file ();
195 char searchar
= '/'; /* use /.../ searches */
197 int lineno
; /* line number of current line */
198 long charno
; /* current character number */
200 long linecharno
; /* charno of start of line; not used by C,
201 but by every other language. */
203 char *curfile
; /* current input file name */
204 char *tagfile
; /* output file */
205 char *progname
; /* name this program was invoked with */
206 char *cwd
; /* current working directory */
207 char *tagfiledir
; /* directory of tagfile */
209 FILE *tagf
; /* ioptr for tags file */
210 NODE
*head
; /* the head of the binary tree of tags */
213 * A `struct linebuffer' is a structure which holds a line of text.
214 * `readline' reads a line from a stream into a linebuffer and works
215 * regardless of the length of the line.
223 struct linebuffer lb
; /* the current line */
224 struct linebuffer token_name
; /* used by C_entries as temporary area */
228 struct linebuffer lb
; /* used by C_entries instead of lb */
231 /* boolean "functions" (see init) */
232 logical _wht
[0177], _etk
[0177], _itk
[0177], _btk
[0177];
234 *white
= " \f\t\n\013", /* white chars */
235 *endtk
= " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?", /* token ending chars */
236 /* token starting chars */
237 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~",
238 /* valid in-token chars */
239 *intk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
241 logical append_to_tagfile
; /* -a: append to tags */
242 /* The following three default to TRUE for etags, but to FALSE for ctags. */
243 logical typedefs
; /* -t: create tags for typedefs */
244 logical typedefs_and_cplusplus
; /* -T: create tags for typedefs, level */
245 /* 0 struct/enum/union decls, and C++ */
246 /* member functions. */
247 logical constantypedefs
; /* -d: create tags for C #define and enum */
248 /* constants. Enum consts not implemented. */
249 /* -D: opposite of -d. Default under ctags. */
250 logical update
; /* -u: update tags */
251 logical vgrind_style
; /* -v: create vgrind style index output */
252 logical no_warnings
; /* -w: suppress warnings */
253 logical cxref_style
; /* -x: create cxref style output */
254 logical cplusplus
; /* .[hc] means C++, not C */
255 logical noindentypedefs
; /* -I: ignore indentation in C */
256 #define permit_duplicates TRUE /* allow duplicate tags */
258 struct option longopts
[] =
260 { "append", no_argument
, NULL
, 'a' },
261 { "backward-search", no_argument
, NULL
, 'B' },
262 { "c++", no_argument
, NULL
, 'C' },
263 { "cxref", no_argument
, NULL
, 'x' },
264 { "defines", no_argument
, NULL
, 'd' },
265 { "help", no_argument
, NULL
, 'h' },
266 { "help", no_argument
, NULL
, 'H' },
267 { "ignore-indentation", no_argument
, NULL
, 'I' },
268 { "include", required_argument
, NULL
, 'i' },
269 { "language", required_argument
, NULL
, 'l' },
270 { "no-defines", no_argument
, NULL
, 'D' },
271 { "no-regex", no_argument
, NULL
, 'R' },
272 { "no-warn", no_argument
, NULL
, 'w' },
273 { "output", required_argument
, NULL
, 'o' },
274 { "regex", required_argument
, NULL
, 'r' },
275 { "typedefs", no_argument
, NULL
, 't' },
276 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
277 { "update", no_argument
, NULL
, 'u' },
278 { "version", no_argument
, NULL
, 'V' },
279 { "vgrind", no_argument
, NULL
, 'v' },
284 /* Structure defining a regular expression. Elements are
285 the compiled pattern, and the name string. */
288 struct re_pattern_buffer
*pattern
;
289 struct re_registers regs
;
291 logical error_signaled
;
294 /* Number of regexps found. */
295 int num_patterns
= 0;
297 /* Array of all regexps. */
298 struct pattern
*patterns
= NULL
;
299 #endif /* ETAGS_REGEXPS */
301 /* Language stuff. */
305 Lang_function
*function
;
308 /* Table of language names and corresponding functions. */
309 /* It is ok for a given function to be listed under more than one
310 name. I just didn't. */
311 /* "auto" language reverts to default behavior. */
312 struct lang_entry lang_names
[] =
314 { "asm", Asm_labels
},
315 { "c", default_C_entries
},
316 { "c++", Cplusplus_entries
},
317 { "c*", Cstar_entries
},
318 { "fortran", Fortran_functions
},
319 { "lisp", Lisp_functions
},
320 { "none", just_read_file
},
321 { "pascal", Pascal_functions
},
322 { "scheme" , Scheme_functions
},
323 { "tex", TeX_functions
},
328 /* Table of file extensions and corresponding language functions. */
329 struct lang_entry lang_extensions
[] =
331 /* Assume that ".s" or ".a" is assembly code. -wolfgang.
333 { "a", Asm_labels
}, /* Unix assembler */
334 { "asm", Asm_labels
}, /* Microcontroller assembly */
335 { "def", Asm_labels
}, /* BSO/Tasking definition includes */
336 { "inc", Asm_labels
}, /* Microcontroller include files */
337 { "ins", Asm_labels
}, /* Microcontroller include files */
339 { "sa", Asm_labels
}, /* Unix assembler */
340 { "src", Asm_labels
}, /* BSO/Tasking C compiler output */
342 /* .aux, .bbl, .clo, .cls, .dtx or .tex implies LaTeX source code. */
343 { "aux", TeX_functions
},
344 { "bbl", TeX_functions
},
345 { "clo", TeX_functions
},
346 { "cls", TeX_functions
},
347 { "dtx", TeX_functions
},
348 { "sty", TeX_functions
},
349 { "tex", TeX_functions
},
351 /* .l or .el or .lisp (or .cl or .clisp or ...) implies lisp source code */
352 { "cl", Lisp_functions
},
353 { "clisp", Lisp_functions
},
354 { "el", Lisp_functions
},
355 { "l", Lisp_functions
},
356 { "lisp", Lisp_functions
},
357 { "lsp", Lisp_functions
},
359 /* .scm or .sm or .scheme implies scheme source code */
360 { "SCM", Scheme_functions
},
361 { "SM", Scheme_functions
},
362 { "oak", Scheme_functions
},
363 { "sch", Scheme_functions
},
364 { "scheme", Scheme_functions
},
365 { "scm", Scheme_functions
},
366 { "sm", Scheme_functions
},
367 { "t", Scheme_functions
},
368 /* FIXME Can't do the `SCM' or `scm' prefix with a version number */
370 /* Note that ".c" and ".h" can be considered C++, if the --c++
371 flag was given. That is why default_C_entries is called here. */
372 { "c", default_C_entries
},
373 { "h", default_C_entries
},
375 /* .C or .H or .c++ or .cc or .cpp or .cxx or .h++ or .hh or .hxx:
377 { "C", Cplusplus_entries
},
378 { "H", Cplusplus_entries
},
379 { "c++", Cplusplus_entries
},
380 { "cc", Cplusplus_entries
},
381 { "cpp", Cplusplus_entries
},
382 { "cxx", Cplusplus_entries
},
383 { "h++", Cplusplus_entries
},
384 { "hh", Cplusplus_entries
},
385 { "hxx", Cplusplus_entries
},
387 /* .y: a yacc file */
388 { "y", Yacc_entries
},
390 /* .cs or .hs: a C* file */
391 { "cs", Cstar_entries
},
392 { "hs", Cstar_entries
},
394 /* .f and .for are FORTRAN. */
395 { "F", Fortran_functions
},
396 { "f", Fortran_functions
},
397 { "for", Fortran_functions
},
399 /* .pl implies prolog source code */
400 { "pl", Prolog_functions
},
402 /* .p or .pas: a Pascal file */
403 { "p", Pascal_functions
},
404 { "pas", Pascal_functions
},
409 /* Non-NULL if language fixed. */
410 Lang_function
*lang_func
= NULL
;
414 print_language_names ()
416 struct lang_entry
*name
, *ext
;
418 puts ("\nThese are the currently supported languages, along with the\n\
419 default extensions for files:");
420 for (name
= lang_names
; name
->extension
; ++name
)
422 printf ("\t%s\t", name
->extension
);
423 for (ext
= lang_extensions
; ext
->extension
; ++ext
)
424 if (name
->function
== ext
->function
)
425 printf (" .%s", ext
->extension
);
428 puts ("Where `auto' means use default language for files based on filename\n\
429 extension, and `none' means only do regexp processing on files.\n\
430 If no language is specified and no extension is found for some file,\n\
431 Fortran is tried first; if no tags are found, C is tried next.");
438 printf ("%s for Emacs version %s.\n", (CTAGS
) ? "CTAGS" : "ETAGS", VERSION
);
440 printf ("%s for Emacs version 19.\n", (CTAGS
) ? "CTAGS" : "ETAGS");
449 printf ("These are the options accepted by %s. You may use unambiguous\n\
450 abbreviations for the long option names. A - as file name means read\n\
451 names from stdin.\n\n", progname
);
453 puts ("-a, --append\n\
454 Append tag entries to existing tags file.");
457 puts ("-B, --backward-search\n\
458 Write the search commands for the tag entries using '?', the\n\
459 backward-search command instead of '/', the forward-search command.");
462 Treat files whose extension defaults to C language as C++ files.");
465 puts ("-d, --defines\n\
466 Create tag entries for constant C #defines, too.");
468 puts ("-D, --no-defines\n\
469 Don't create tag entries for constant C #defines. This makes\n\
470 the tags file smaller.");
474 puts ("-i FILE, --include=FILE\n\
475 Include a note in tag file indicating that, when searching for\n\
476 a tag, one should also consult the tags file FILE after\n\
477 checking the current file.");
478 puts ("-l LANG, --language=LANG\n\
479 Force the following files to be considered as written in the\n\
480 named language up to the next --language=LANG option.");
484 puts ("-r /REGEXP/, --regex=/REGEXP/\n\
485 Make a tag for each line matching pattern REGEXP in the\n\
486 following files. REGEXP is anchored (as if preceded by ^).\n\
487 The form /REGEXP/NAME/ creates a named tag. For example Tcl\n\
488 named tags can be created with:\n\
489 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
490 puts ("-R, --no-regex\n\
491 Don't create tags from regexps for the following files.");
492 #endif /* ETAGS_REGEXPS */
493 puts ("-o FILE, --output=FILE\n\
494 Write the tags to FILE.");
495 puts ("-I, --ignore-indentation\n\
496 Don't rely on indentation quite as much as normal. Currently,\n\
497 this means not to assume that a closing brace in the first\n\
498 column is the final brace of a function or structure\n\
499 definition in C and C++.");
503 puts ("-t, --typedefs\n\
504 Generate tag entries for C typedefs.");
505 puts ("-T, --typedefs-and-c++\n\
506 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
507 and C++ member functions.");
508 puts ("-u, --update\n\
509 Update the tag entries for the given files, leaving tag\n\
510 entries for other files in place. Currently, this is\n\
511 implemented by deleting the existing entries for the given\n\
512 files and then rewriting the new entries at the end of the\n\
513 tags file. It is often faster to simply rebuild the entire\n\
514 tag file than to use this.");
515 puts ("-v, --vgrind\n\
516 Generates an index of items intended for human consumption,\n\
517 similar to the output of vgrind. The index is sorted, and\n\
518 gives the page number of each item.");
519 puts ("-w, --no-warn\n\
520 Suppress warning messages about entries defined in multiple\n\
522 puts ("-x, --cxref\n\
523 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
524 The output uses line numbers instead of page numbers, but\n\
525 beyond that the differences are cosmetic; try both to see\n\
529 puts ("-V, --version\n\
530 Print the version of the program.\n\
532 Print this help message.");
534 print_language_names ();
547 /* This structure helps us allow mixing of --lang and filenames. */
550 enum argument_type arg_type
;
552 Lang_function
*function
;
555 #ifdef VMS /* VMS specific functions */
559 /* This is a BUG! ANY arbitrary limit is a BUG!
560 Won't someone please fix this? */
561 #define MAX_FILE_SPEC_LEN 255
564 char body
[MAX_FILE_SPEC_LEN
+ 1];
568 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
569 returning in each successive call the next filename matching the input
570 spec. The function expects that each in_spec passed
571 to it will be processed to completion; in particular, up to and
572 including the call following that in which the last matching name
573 is returned, the function ignores the value of in_spec, and will
574 only start processing a new spec with the following call.
575 If an error occurs, on return out_spec contains the value
576 of in_spec when the error occurred.
578 With each successive filename returned in out_spec, the
579 function's return value is one. When there are no more matching
580 names the function returns zero. If on the first call no file
581 matches in_spec, or there is any other error, -1 is returned.
586 #define OUTSIZE MAX_FILE_SPEC_LEN
592 static long context
= 0;
593 static struct dsc$descriptor_s o
;
594 static struct dsc$descriptor_s i
;
595 static logical pass1
= TRUE
;
602 o
.dsc$a_pointer
= (char *) out
;
603 o
.dsc$w_length
= (short)OUTSIZE
;
604 i
.dsc$a_pointer
= in
;
605 i
.dsc$w_length
= (short)strlen(in
);
606 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
607 i
.dsc$b_class
= DSC$K_CLASS_S
;
608 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
609 o
.dsc$b_class
= DSC$K_CLASS_VS
;
611 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
613 out
->body
[out
->curlen
] = EOS
;
616 else if (status
== RMS$_NMF
)
620 strcpy(out
->body
, in
);
623 lib$
find_file_end(&context
);
629 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
630 name of each file specified by the provided arg expanding wildcards.
633 gfnames (arg
, p_error
)
637 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
639 switch (fn_exp (&filename
, arg
))
643 return filename
.body
;
649 return filename
.body
;
653 #ifndef OLD /* Newer versions of VMS do provide `system'. */
657 fprintf (stderr
, "system() function not implemented under VMS\n");
661 #define VERSION_DELIM ';'
662 char *massage_name (s
)
668 if (*s
== VERSION_DELIM
)
686 unsigned int nincluded_files
= 0;
687 char **included_files
= xnew (argc
, char *);
690 int current_arg
= 0, file_count
= 0;
691 struct linebuffer filename_lb
;
697 _fmode
= O_BINARY
; /* all of files are treated as binary files */
702 /* Allocate enough no matter what happens. Overkill, but each one
704 argbuffer
= xnew (argc
, ARGUMENT
);
707 /* Set syntax for regular expression routines. */
708 re_set_syntax (RE_SYNTAX_EMACS
);
709 #endif /* ETAGS_REGEXPS */
712 * If etags, always find typedefs and structure tags. Why not?
713 * Also default is to find macro constants.
716 typedefs
= typedefs_and_cplusplus
= constantypedefs
= TRUE
;
720 int opt
= getopt_long (argc
, argv
,
721 "-aCdDf:Il:o:r:RStTi:BuvxwVhH", longopts
, 0);
729 /* If getopt returns 0, then it has already processed a
730 long-named option. We should do nothing. */
734 /* This means that a filename has been seen. Record it. */
735 argbuffer
[current_arg
].arg_type
= at_filename
;
736 argbuffer
[current_arg
].what
= optarg
;
741 /* Common options. */
743 append_to_tagfile
= TRUE
;
749 constantypedefs
= TRUE
;
752 constantypedefs
= FALSE
;
754 case 'f': /* for compatibility with old makefiles */
758 fprintf (stderr
, "%s: -%c option may only be given once.\n",
765 case 'S': /* for backward compatibility */
766 noindentypedefs
= TRUE
;
769 if (!get_language (optarg
, &argbuffer
[current_arg
].function
))
771 fprintf (stderr
, "%s: language \"%s\" not recognized.\n",
775 argbuffer
[current_arg
].arg_type
= at_language
;
780 argbuffer
[current_arg
].arg_type
= at_regexp
;
781 argbuffer
[current_arg
].what
= optarg
;
785 argbuffer
[current_arg
].arg_type
= at_regexp
;
786 argbuffer
[current_arg
].what
= NULL
;
789 #endif /* ETAGS_REGEXPS */
801 typedefs
= typedefs_and_cplusplus
= TRUE
;
806 included_files
[nincluded_files
++] = optarg
;
831 for (; optind
< argc
; ++optind
)
833 argbuffer
[current_arg
].arg_type
= at_filename
;
834 argbuffer
[current_arg
].what
= argv
[optind
];
839 if (nincluded_files
== 0 && file_count
== 0)
841 fprintf (stderr
, "%s: No input files specified.\n", progname
);
844 fprintf (stderr
, "\tTry `%s --help' for a complete list of options.\n",
851 tagfile
= CTAGS
? "tags" : "TAGS";
853 cwd
= etags_getcwd (); /* the current working directory */
855 if (streq (tagfile
, "-"))
861 tagfiledir
= absolute_dirname (tagfile
, cwd
);
864 init (); /* set up boolean "functions" */
867 initbuffer (&token_name
);
868 initbuffer (&lbs
[0].lb
);
869 initbuffer (&lbs
[1].lb
);
870 initbuffer (&filename_lb
);
874 if (streq (tagfile
, "-"))
877 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
883 * Loop through files finding functions.
885 for (i
= 0; i
< current_arg
; ++i
)
887 switch (argbuffer
[i
].arg_type
)
890 lang_func
= argbuffer
[i
].function
;
894 add_regex (argbuffer
[i
].what
);
899 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
903 error ("Can't find file %s\n", this_file
);
908 this_file
= massage_name (this_file
);
911 this_file
= argbuffer
[i
].what
;
913 /* Input file named "-" means read file names from stdin
915 if (streq (this_file
, "-"))
916 while (readline_internal (&filename_lb
, stdin
) > 0)
917 process_file (filename_lb
.buffer
);
919 process_file (this_file
);
929 while (nincluded_files
-- > 0)
930 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
936 /* If CTAGS, we are here. process_file did not write the tags yet,
937 because we want them ordered. Let's do it now. */
940 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
950 for (i
= 0; i
< current_arg
; ++i
)
952 if (argbuffer
[i
].arg_type
!= at_filename
)
955 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
956 tagfile
, argbuffer
[i
].what
, tagfile
);
957 if (system (cmd
) != GOOD
)
958 fatal ("failed to execute shell command");
960 append_to_tagfile
= TRUE
;
963 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
972 sprintf (cmd
, "sort %s -o %s", tagfile
, tagfile
);
980 * Set the language, given the name.
983 get_language (language
, func
)
985 Lang_function
**func
;
987 struct lang_entry
*lang
;
989 for (lang
= lang_names
; lang
->extension
; ++lang
)
991 if (streq (language
, lang
->extension
))
993 *func
= lang
->function
;
1003 * This routine is called on each file argument.
1009 struct stat stat_buf
;
1012 if (stat (file
, &stat_buf
) == 0 && !S_ISREG (stat_buf
.st_mode
))
1014 fprintf (stderr
, "Skipping %s: it is not a regular file.\n", file
);
1017 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1019 fprintf (stderr
, "Skipping inclusion of %s in self.\n", file
);
1022 inf
= fopen (file
, "r");
1029 find_entries (file
, inf
);
1037 /* file is an absolute filename. Canonicalise it. */
1038 filename
= absolute_filename (file
, cwd
);
1042 /* file is a filename relative to cwd. Make it relative
1043 to the directory of the tags file. */
1044 filename
= relative_filename (file
, tagfiledir
);
1046 fprintf (tagf
, "\f\n%s,%d\n", filename
, total_size_of_entries (head
));
1054 * This routine sets up the boolean pseudo-functions which work
1055 * by setting boolean flags dependent upon the corresponding character
1056 * Every char which is NOT in that string is not a white char. Therefore,
1057 * all of the array "_wht" is set to FALSE, and then the elements
1058 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1059 * of a char is TRUE if it is the string "white", else FALSE.
1067 for (i
= 0; i
< 0177; i
++)
1068 _wht
[i
] = _etk
[i
] = _itk
[i
] = _btk
[i
] = FALSE
;
1069 for (sp
= white
; *sp
; sp
++)
1071 for (sp
= endtk
; *sp
; sp
++)
1073 for (sp
= intk
; *sp
; sp
++)
1075 for (sp
= begtk
; *sp
; sp
++)
1077 _wht
[0] = _wht
['\n'];
1078 _etk
[0] = _etk
['\n'];
1079 _btk
[0] = _btk
['\n'];
1080 _itk
[0] = _itk
['\n'];
1084 * This routine opens the specified file and calls the function
1085 * which finds the function and type definitions.
1088 find_entries (file
, inf
)
1093 struct lang_entry
*lang
;
1094 NODE
*old_last_node
;
1095 extern NODE
*last_node
;
1097 curfile
= savestr (file
);
1098 cp
= etags_strrchr (file
, '.');
1100 /* If user specified a language, use it. */
1101 if (lang_func
!= NULL
)
1111 for (lang
= lang_extensions
; lang
->extension
; ++lang
)
1113 if (streq (cp
, lang
->extension
))
1115 lang
->function (inf
);
1123 old_last_node
= last_node
;
1124 Fortran_functions (inf
);
1126 /* No Fortran entries found. Try C. */
1127 if (old_last_node
== last_node
)
1128 default_C_entries (inf
);
1134 pfnote (name
, is_func
, named
, linestart
, linelen
, lno
, cno
)
1135 char *name
; /* tag name */
1136 logical is_func
; /* tag is a function */
1137 logical named
; /* tag different from text of definition */
1138 char *linestart
; /* start of the line where tag is */
1139 int linelen
; /* length of the line where tag is */
1140 int lno
; /* line number */
1141 long cno
; /* character number */
1143 register NODE
*np
= xnew (1, NODE
);
1146 /* If ctags mode, change name "main" to M<thisfilename>. */
1147 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
1149 fp
= etags_strrchr (curfile
, '/');
1150 np
->name
= concat ("M", fp
== 0 ? curfile
: fp
+ 1, "");
1151 fp
= etags_strrchr (np
->name
, '.');
1152 if (fp
&& fp
[1] != '\0' && fp
[2] == '\0')
1161 np
->been_warned
= FALSE
;
1163 np
->is_func
= is_func
;
1165 /* Our char numbers are 0-base, because of C language tradition?
1166 ctags compatibility? old versions compatibility? I don't know.
1167 Anyway, since emacs's are 1-base we espect etags.el to take care
1168 of the difference. If we wanted to have 1-based numbers, we would
1169 uncomment the +1 below. */
1170 np
->cno
= cno
/* + 1 */ ;
1171 np
->left
= np
->right
= NULL
;
1172 np
->pat
= savenstr (linestart
, ((CTAGS
&& !cxref_style
) ? 50 : linelen
));
1174 add_node (np
, &head
);
1179 * recurse on left children, iterate on right children.
1183 register NODE
*node
;
1187 register NODE
*node_right
= node
->right
;
1188 free_tree (node
->left
);
1192 free ((char *) node
);
1199 * Adds a node to the tree of nodes. In etags mode, we don't keep
1200 * it sorted; we just keep a linear list. In ctags mode, maintain
1201 * an ordered tree, with no attempt at balancing.
1203 * add_node is the only function allowed to add nodes, so it can
1206 NODE
*last_node
= NULL
;
1208 add_node (node
, cur_node_p
)
1209 NODE
*node
, **cur_node_p
;
1212 register NODE
*cur_node
= *cur_node_p
;
1214 if (cur_node
== NULL
)
1224 if (last_node
== NULL
)
1225 fatal ("internal error in add_node", 0);
1226 last_node
->right
= node
;
1232 dif
= strcmp (node
->name
, cur_node
->name
);
1235 * If this tag name matches an existing one, then
1236 * do not add the node, but maybe print a warning.
1240 if (node
->file
== cur_node
->file
)
1244 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
1245 node
->file
, lineno
, node
->name
);
1246 fprintf (stderr
, "Second entry ignored\n");
1250 if (!cur_node
->been_warned
&& !no_warnings
)
1253 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1254 node
->file
, cur_node
->file
, node
->name
);
1256 cur_node
->been_warned
= TRUE
;
1260 /* Maybe refuse to add duplicate nodes. */
1261 if (!permit_duplicates
)
1263 if (streq (node
->name
, cur_node
->name
)
1264 && streq (node
->file
, cur_node
->file
))
1268 /* Actually add the node */
1269 add_node (node
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1275 register NODE
*node
;
1282 /* Output subentries that precede this one */
1283 put_entries (node
->left
);
1285 /* Output this entry */
1291 fprintf (tagf
, "%s\177%s\001%d,%d\n",
1292 node
->pat
, node
->name
,
1293 node
->lno
, node
->cno
);
1297 fprintf (tagf
, "%s\177%d,%d\n",
1299 node
->lno
, node
->cno
);
1302 else if (!cxref_style
)
1304 fprintf (tagf
, "%s\t%s\t",
1305 node
->name
, node
->file
);
1309 putc (searchar
, tagf
);
1312 for (sp
= node
->pat
; *sp
; sp
++)
1314 if (*sp
== '\\' || *sp
== searchar
)
1318 putc (searchar
, tagf
);
1321 { /* a typedef; text pattern inadequate */
1322 fprintf (tagf
, "%d", node
->lno
);
1326 else if (vgrind_style
)
1327 fprintf (stdout
, "%s %s %d\n",
1328 node
->name
, node
->file
, (node
->lno
+ 63) / 64);
1330 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1331 node
->name
, node
->lno
, node
->file
, node
->pat
);
1333 /* Output subentries that follow this one */
1334 put_entries (node
->right
);
1337 /* Length of a number's decimal representation. */
1345 for (; num
; num
/= 10)
1351 * Return total number of characters that put_entries will output for
1352 * the nodes in the subtree of the specified node. Works only if
1353 * we are not ctags, but called only in that case. This count
1354 * is irrelevant with the new tags.el, but is still supplied for
1355 * backward compatibility.
1358 total_size_of_entries (node
)
1359 register NODE
*node
;
1367 for (; node
; node
= node
->right
)
1369 /* Count left subentries. */
1370 total
+= total_size_of_entries (node
->left
);
1372 /* Count this entry */
1373 total
+= strlen (node
->pat
) + 1;
1374 total
+= number_len ((long) node
->lno
) + 1 + number_len (node
->cno
) + 1;
1376 total
+= 1 + strlen (node
->name
); /* \001name */
1383 * The C symbol tables.
1387 st_none
, st_C_struct
, st_C_enum
, st_C_define
, st_C_typedef
, st_C_typespec
1390 /* Feed stuff between (but not including) %[ and %] lines to:
1391 gperf -c -k1,3 -o -p -r -t
1393 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1395 class, C_PLPL, st_C_struct
1396 domain, C_STAR, st_C_struct
1397 union, 0, st_C_struct
1398 struct, 0, st_C_struct
1400 typedef, 0, st_C_typedef
1401 define, 0, st_C_define
1402 long, 0, st_C_typespec
1403 short, 0, st_C_typespec
1404 int, 0, st_C_typespec
1405 char, 0, st_C_typespec
1406 float, 0, st_C_typespec
1407 double, 0, st_C_typespec
1408 signed, 0, st_C_typespec
1409 unsigned, 0, st_C_typespec
1410 auto, 0, st_C_typespec
1411 void, 0, st_C_typespec
1412 extern, 0, st_C_typespec
1413 static, 0, st_C_typespec
1414 const, 0, st_C_typespec
1415 volatile, 0, st_C_typespec
1417 and replace lines between %< and %> with its output. */
1419 /* C code produced by gperf version 1.8.1 (K&R C version) */
1420 /* Command-line: gperf -c -k1,3 -o -p -r -t */
1423 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
1425 #define MIN_WORD_LENGTH 3
1426 #define MAX_WORD_LENGTH 8
1427 #define MIN_HASH_VALUE 10
1428 #define MAX_HASH_VALUE 62
1431 53 is the maximum key range
1439 static unsigned char hash_table
[] =
1441 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
1442 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
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, 2, 62, 7,
1451 6, 9, 15, 30, 62, 24, 62, 62, 1, 24,
1452 7, 27, 13, 62, 19, 26, 18, 27, 1, 62,
1453 62, 62, 62, 62, 62, 62, 62, 62,
1455 return len
+ hash_table
[str
[2]] + hash_table
[str
[0]];
1458 struct C_stab_entry
*
1459 in_word_set (str
, len
)
1464 static struct C_stab_entry wordlist
[] =
1466 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1468 {"volatile", 0, st_C_typespec
},
1470 {"long", 0, st_C_typespec
},
1471 {"char", 0, st_C_typespec
},
1472 {"class", C_PLPL
, st_C_struct
},
1473 {"",}, {"",}, {"",}, {"",},
1474 {"const", 0, st_C_typespec
},
1475 {"",}, {"",}, {"",}, {"",},
1476 {"auto", 0, st_C_typespec
},
1478 {"define", 0, st_C_define
},
1480 {"void", 0, st_C_typespec
},
1481 {"",}, {"",}, {"",},
1482 {"extern", 0, st_C_typespec
},
1483 {"static", 0, st_C_typespec
},
1485 {"domain", C_STAR
, st_C_struct
},
1487 {"typedef", 0, st_C_typedef
},
1488 {"double", 0, st_C_typespec
},
1489 {"enum", 0, st_C_enum
},
1490 {"",}, {"",}, {"",}, {"",},
1491 {"int", 0, st_C_typespec
},
1493 {"float", 0, st_C_typespec
},
1494 {"",}, {"",}, {"",},
1495 {"struct", 0, st_C_struct
},
1496 {"",}, {"",}, {"",}, {"",},
1497 {"union", 0, st_C_struct
},
1499 {"short", 0, st_C_typespec
},
1501 {"unsigned", 0, st_C_typespec
},
1502 {"signed", 0, st_C_typespec
},
1505 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
1507 register int key
= hash (str
, len
);
1509 if (key
<= MAX_HASH_VALUE
&& key
>= MIN_HASH_VALUE
)
1511 register char *s
= wordlist
[key
].name
;
1513 if (*s
== *str
&& strneq (str
+ 1, s
+ 1, len
- 1))
1514 return &wordlist
[key
];
1522 C_symtype(str
, len
, c_ext
)
1527 register struct C_stab_entry
*se
= in_word_set(str
, len
);
1529 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
1535 * C functions are recognized using a simple finite automaton.
1536 * funcdef is its state variable.
1540 fnone
, /* nothing seen */
1541 ftagseen
, /* function-like tag seen */
1542 fstartlist
, /* just after open parenthesis */
1543 finlist
, /* in parameter list */
1544 flistseen
, /* after parameter list */
1545 fignore
/* before open brace */
1551 * typedefs are recognized using a simple finite automaton.
1552 * typeddef is its state variable.
1556 tnone
, /* nothing seen */
1557 ttypedseen
, /* typedef keyword seen */
1558 tinbody
, /* inside typedef body */
1559 tend
, /* just before typedef tag */
1560 tignore
/* junk after typedef tag */
1566 * struct-like structures (enum, struct and union) are recognized
1567 * using another simple finite automaton. `structdef' is its state
1572 snone
, /* nothing seen yet */
1573 skeyseen
, /* struct-like keyword seen */
1574 stagseen
, /* struct-like tag seen */
1575 scolonseen
, /* colon seen after struct-like tag */
1576 sinbody
/* in struct body: recognize member func defs*/
1581 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1582 * struct tag, and structtype is the type of the preceding struct-like
1585 char *structtag
= "<uninited>";
1586 enum sym_type structtype
;
1589 * Yet another little state machine to deal with preprocessor lines.
1593 dnone
, /* nothing seen */
1594 dsharpseen
, /* '#' seen as first char on line */
1595 ddefineseen
, /* '#' and 'define' seen */
1596 dignorerest
/* ignore rest of line */
1601 * Set this to TRUE, and the next token considered is called a function.
1602 * Used only for GNU emacs's function-defining macros.
1604 logical next_token_is_func
;
1607 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1613 * checks to see if the current token is at the start of a
1614 * function, or corresponds to a typedef, or is a struct/union/enum
1617 * *IS_FUNC gets TRUE iff the token is a function or macro with args.
1618 * C_EXT is which language we are looking at.
1620 * In the future we will need some way to adjust where the end of
1621 * the token is; for instance, implementing the C++ keyword
1622 * `operator' properly will adjust the end of the token to be after
1623 * whatever follows `operator'.
1630 * next_token_is_func IN OUT
1634 consider_token (str
, len
, c
, c_ext
, cblev
, is_func
)
1635 register char *str
; /* IN: token pointer */
1636 register int len
; /* IN: token length */
1637 register char c
; /* IN: first char after the token */
1638 int c_ext
; /* IN: C extensions mask */
1639 int cblev
; /* IN: curly brace level */
1640 logical
*is_func
; /* OUT: function found */
1642 enum sym_type toktype
= C_symtype (str
, len
, c_ext
);
1645 * Advance the definedef state machine.
1650 /* We're not on a preprocessor line. */
1653 if (toktype
== st_C_define
)
1655 definedef
= ddefineseen
;
1659 definedef
= dignorerest
;
1664 * Make a tag for any macro, unless it is a constant
1665 * and constantypedefs is FALSE.
1667 definedef
= dignorerest
;
1668 *is_func
= (c
== '(');
1669 if (!*is_func
&& !constantypedefs
)
1676 error ("internal error: definedef value.", 0);
1685 if (toktype
== st_C_typedef
)
1688 typdef
= ttypedseen
;
1704 /* Do not return here, so the structdef stuff has a chance. */
1718 * This structdef business is currently only invoked when cblev==0.
1719 * It should be recursively invoked whatever the curly brace level,
1720 * and a stack of states kept, to allow for definitions of structs
1723 * This structdef business is NOT invoked when we are ctags and the
1724 * file is plain C. This is because a struct tag may have the same
1725 * name as another tag, and this loses with ctags.
1727 * This if statement deals with the typdef state machine as
1728 * follows: if typdef==ttypedseen and token is struct/union/class/enum,
1729 * return FALSE. All the other code here is for the structdef
1736 if (typdef
== ttypedseen
1737 || (typedefs_and_cplusplus
&& cblev
== 0 && structdef
== snone
))
1739 structdef
= skeyseen
;
1740 structtype
= toktype
;
1744 if (structdef
== skeyseen
)
1746 /* Save the tag for struct/union/class, for functions that may be
1748 if (structtype
== st_C_struct
)
1749 structtag
= savenstr (str
, len
);
1751 structtag
= "<enum>";
1752 structdef
= stagseen
;
1756 /* Avoid entering funcdef stuff if typdef is going on. */
1757 if (typdef
!= tnone
)
1763 /* Detect GNU macros. */
1764 if (definedef
== dnone
)
1765 if (strneq (str
, "DEFUN", 5) /* Used in emacs */
1767 These are defined inside C functions
, so currently they
1769 || strneq (str
, "EXFUN", 5) /* Used in glibc */
1770 || strneq (str
, "DEFVAR_", 7) /* Used in emacs */
1772 || strneq (str
, "SYSCALL", 7) /* Used in glibc (mach) */
1773 || strneq (str
, "ENTRY", 5) /* Used in glibc */
1774 || strneq (str
, "PSEUDO", 6)) /* Used in glibc */
1777 next_token_is_func
= TRUE
;
1780 if (next_token_is_func
)
1782 next_token_is_func
= FALSE
;
1792 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1793 funcdef
= fnone
; /* should be useless */
1796 if (funcdef
== fnone
)
1809 * This routine finds functions, typedefs, #define's and
1810 * struct/union/enum definitions in C syntax and adds them
1824 #define current_lb_is_new (newndx == curndx)
1825 #define switch_line_buffers() (curndx = 1 - curndx)
1827 #define curlb (lbs[curndx].lb)
1828 #define othlb (lbs[1-curndx].lb)
1829 #define newlb (lbs[newndx].lb)
1830 #define curlinepos (lbs[curndx].linepos)
1831 #define othlinepos (lbs[1-curndx].linepos)
1832 #define newlinepos (lbs[newndx].linepos)
1834 #define CNL_SAVE_DEFINEDEF \
1836 curlinepos = charno; \
1838 charno += readline (&curlb, inf); \
1839 lp = curlb.buffer; \
1846 CNL_SAVE_DEFINEDEF; \
1847 if (savetok.valid) \
1850 savetok.valid = FALSE; \
1852 definedef = dnone; \
1855 #define make_tag(isfun) do \
1858 pfnote (savestr (token_name.buffer), isfun, tok.named, \
1859 tok.buffer, tok.linelen, tok.lineno, tok.linepos); \
1860 else if (DEBUG) abort (); \
1861 tok.valid = FALSE; \
1865 C_entries (c_ext
, inf
)
1866 int c_ext
; /* extension of C */
1867 FILE *inf
; /* input file */
1869 register char c
; /* latest char read; '\0' for end of line */
1870 register char *lp
; /* pointer one beyond the character `c' */
1871 int curndx
, newndx
; /* indices for current and new lb */
1872 TOKEN tok
; /* latest token read */
1873 register int tokoff
; /* offset in line of start of current token */
1874 register int toklen
; /* length of current token */
1875 int cblev
; /* current curly brace level */
1876 int parlev
; /* current parenthesis level */
1877 logical incomm
, inquote
, inchar
, quotednl
, midtoken
;
1879 TOKEN savetok
; /* token saved during preprocessor handling */
1882 curndx
= newndx
= 0;
1888 definedef
= dnone
; funcdef
= fnone
; typdef
= tnone
; structdef
= snone
;
1889 next_token_is_func
= yacc_rules
= FALSE
;
1890 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
1891 tok
.valid
= savetok
.valid
= FALSE
;
1894 cplpl
= c_ext
& C_PLPL
;
1901 /* If we're at the end of the line, the next character is a
1902 '\0'; don't skip it, because it's the thing that tells us
1903 to read the next line. */
1924 /* Newlines inside comments do not end macro definitions in
1939 /* Newlines inside strings do not end macro definitions
1940 in traditional cpp, even though compilers don't
1941 usually accept them. */
1952 /* Hmmm, something went wrong. */
1966 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1971 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1981 else if (cplpl
&& *lp
== '/')
1989 if ((c_ext
& YACC
) && *lp
== '%')
1991 /* entering or exiting rules section in yacc file */
1993 definedef
= dnone
; funcdef
= fnone
;
1994 typdef
= tnone
; structdef
= snone
;
1995 next_token_is_func
= FALSE
;
1996 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
1998 yacc_rules
= !yacc_rules
;
2004 if (definedef
== dnone
)
2007 logical cpptoken
= TRUE
;
2009 /* Look back on this line. If all blanks, or nonblanks
2010 followed by an end of comment, this is a preprocessor
2012 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
2015 if (*cp
== '*' && *(cp
+1) == '/')
2024 definedef
= dsharpseen
;
2025 } /* if (definedef == dnone) */
2031 /* Consider token only if some complicated conditions are satisfied. */
2032 if ((definedef
!= dnone
2033 || (cblev
== 0 && structdef
!= scolonseen
)
2034 || (cblev
== 1 && cplpl
&& structdef
== sinbody
))
2035 && typdef
!= tignore
2036 && definedef
!= dignorerest
2037 && funcdef
!= finlist
)
2043 if (cplpl
&& c
== ':' && *lp
== ':' && begtoken(*(lp
+ 1)))
2046 * This handles :: in the middle, but not at the
2047 * beginning of an identifier.
2054 logical is_func
= FALSE
;
2057 || consider_token (newlb
.buffer
+ tokoff
, toklen
,
2058 c
, c_ext
, cblev
, &is_func
))
2060 if (structdef
== sinbody
2061 && definedef
== dnone
2063 /* function defined in C++ class body */
2065 int strsize
= strlen(structtag
) + 2 + toklen
+ 1;
2066 while (token_name
.size
< strsize
)
2068 token_name
.size
*= 2;
2069 token_name
.buffer
=xrealloc(token_name
.buffer
,
2072 strcpy (token_name
.buffer
, structtag
);
2073 strcat (token_name
.buffer
, "::");
2074 strncat (token_name
.buffer
,
2075 newlb
.buffer
+tokoff
, toklen
);
2080 while (token_name
.size
< toklen
+ 1)
2082 token_name
.size
*= 2;
2083 token_name
.buffer
=xrealloc(token_name
.buffer
,
2086 strncpy (token_name
.buffer
,
2087 newlb
.buffer
+tokoff
, toklen
);
2088 token_name
.buffer
[toklen
] = '\0';
2089 if (structdef
== stagseen
2092 && definedef
== dignorerest
)) /* macro */
2097 tok
.lineno
= lineno
;
2098 tok
.linelen
= tokoff
+ toklen
+ 1;
2099 tok
.buffer
= newlb
.buffer
;
2100 tok
.linepos
= newlinepos
;
2103 if (definedef
== dnone
2104 && (funcdef
== ftagseen
2105 || structdef
== stagseen
2108 if (current_lb_is_new
)
2109 switch_line_buffers ();
2116 } /* if (endtoken (c)) */
2117 else if (intoken (c
))
2122 } /* if (midtoken) */
2123 else if (begtoken (c
))
2141 if (structdef
== stagseen
)
2147 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
2149 tokoff
= lp
- 1 - newlb
.buffer
;
2154 } /* if (begtoken) */
2155 } /* if must look at token */
2158 /* Detect end of line, colon, comma, semicolon and various braces
2159 after having handled a token.*/
2163 if (definedef
!= dnone
)
2165 if (structdef
== stagseen
)
2166 structdef
= scolonseen
;
2183 if (definedef
!= dnone
)
2194 if (funcdef
!= fignore
)
2196 if (structdef
== stagseen
)
2200 if (definedef
!= dnone
)
2202 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2204 if (structdef
== stagseen
)
2208 if (definedef
!= dnone
)
2210 if (cblev
== 0 && typdef
== tend
)
2216 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2218 if (structdef
== stagseen
)
2222 if (definedef
!= dnone
)
2231 /* Make sure that the next char is not a '*'.
2232 This handles constructs like:
2233 typedef void OperatorFun (int fun); */
2240 } /* switch (typdef) */
2243 funcdef
= fstartlist
;
2252 if (definedef
!= dnone
)
2260 funcdef
= flistseen
;
2263 if (cblev
== 0 && typdef
== tend
)
2269 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
2273 if (definedef
!= dnone
)
2275 if (typdef
== ttypedseen
)
2279 case skeyseen
: /* unnamed struct */
2280 structtag
= "_anonymous_";
2281 structdef
= sinbody
;
2284 case scolonseen
: /* named struct */
2285 structdef
= sinbody
;
2298 /* Neutralize `extern "C" {' grot and look inside structs. */
2299 if (cblev
== 0 && structdef
== snone
&& typdef
== tnone
)
2305 if (definedef
!= dnone
)
2307 if (funcdef
== fstartlist
)
2308 funcdef
= fnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
2311 if (definedef
!= dnone
)
2313 if (!noindentypedefs
&& lp
== newlb
.buffer
+ 1)
2315 cblev
= 0; /* reset curly brace level if first column */
2316 parlev
= 0; /* also reset paren level, just in case... */
2322 if (typdef
== tinbody
)
2324 #if FALSE /* too risky */
2325 if (structdef
== sinbody
)
2330 structtag
= "<error>";
2334 case '#': case '+': case '-': case '~': case '&': case '%': case '/':
2335 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
2336 if (definedef
!= dnone
)
2338 /* These surely cannot follow a function tag. */
2339 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2343 /* If a macro spans multiple lines don't reset its state. */
2351 } /* while not eof */
2355 * Process either a C++ file or a C file depending on the setting
2359 default_C_entries (inf
)
2362 C_entries (cplusplus
? C_PLPL
: 0, inf
);
2365 /* Always do C++. */
2367 Cplusplus_entries (inf
)
2370 C_entries (C_PLPL
, inf
);
2378 C_entries (C_STAR
, inf
);
2381 /* Always do Yacc. */
2386 C_entries (YACC
, inf
);
2389 /* Fortran parsing */
2397 register int len
= 0;
2399 while (*cp
&& (*cp
| ' ') == (dbp
[len
] | ' '))
2412 while (isspace (*dbp
))
2417 while (isspace (*dbp
))
2421 if (!isdigit (*dbp
))
2423 --dbp
; /* force failure */
2428 while (isdigit (*dbp
));
2437 while (isspace (*dbp
))
2442 linecharno
= charno
;
2443 charno
+= readline (&lb
, inf
);
2448 while (isspace (*dbp
))
2457 && (isalpha (*cp
) || isdigit (*cp
) || (*cp
== '_') || (*cp
== '$')));
2460 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
, FALSE
, lb
.buffer
,
2461 cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2465 Fortran_functions (inf
)
2474 linecharno
= charno
;
2475 charno
+= readline (&lb
, inf
);
2478 dbp
++; /* Ratfor escape to fortran */
2479 while (isspace (*dbp
))
2486 if (tail ("integer"))
2494 if (tail ("logical"))
2498 if (tail ("complex") || tail ("character"))
2502 if (tail ("double"))
2504 while (isspace (*dbp
))
2508 if (tail ("precision"))
2514 while (isspace (*dbp
))
2521 if (tail ("function"))
2525 if (tail ("subroutine"))
2533 if (tail ("program"))
2538 if (tail ("procedure"))
2546 * Bob Weiner, Motorola Inc., 4/3/94
2547 * Unix and microcontroller assembly tag handling
2548 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
2562 linecharno
= charno
;
2563 charno
+= readline (&lb
, inf
);
2566 /* If first char is alphabetic or one of [_.$], test for colon
2567 following identifier. */
2568 if (isalpha (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2570 /* Read past label. */
2572 while (isalnum (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2574 if (*cp
== ':' || isspace (*cp
))
2576 /* Found end of label, so copy it and add it to the table. */
2577 pfnote (savenstr (lb
.buffer
, cp
-lb
.buffer
), TRUE
, FALSE
,
2578 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2584 /* Added by Mosur Mohan, 4/22/88 */
2585 /* Pascal parsing */
2587 #define GET_NEW_LINE \
2589 linecharno = charno; lineno++; \
2590 charno += 1 + readline (&lb, inf); \
2595 * Locates tags for procedures & functions. Doesn't do any type- or
2596 * var-definitions. It does look for the keyword "extern" or
2597 * "forward" immediately following the procedure statement; if found,
2598 * the tag is skipped.
2601 Pascal_functions (inf
)
2604 struct linebuffer tline
; /* mostly copied from C_entries */
2610 logical
/* each of these flags is TRUE iff: */
2611 incomment
, /* point is inside a comment */
2612 inquote
, /* point is inside '..' string */
2613 get_tagname
, /* point is after PROCEDURE/FUNCTION */
2614 /* keyword, so next item = potential tag */
2615 found_tag
, /* point is after a potential tag */
2616 inparms
, /* point is within parameter-list */
2617 verify_tag
; /* point has passed the parm-list, so the */
2618 /* next token will determine whether */
2619 /* this is a FORWARD/EXTERN to be */
2620 /* ignored, or whether it is a real tag */
2626 initbuffer (&tline
);
2628 incomment
= inquote
= FALSE
;
2629 found_tag
= FALSE
; /* have a proc name; check if extern */
2630 get_tagname
= FALSE
; /* have found "procedure" keyword */
2631 inparms
= FALSE
; /* found '(' after "proc" */
2632 verify_tag
= FALSE
; /* check if "extern" is ahead */
2634 /* long main loop to get next char */
2638 if (c
== '\0') /* if end of line */
2643 if (!((found_tag
&& verify_tag
) ||
2645 c
= *dbp
++; /* only if don't need *dbp pointing */
2646 /* to the beginning of the name of */
2647 /* the procedure or function */
2651 if (c
== '}') /* within { - } comments */
2653 else if (c
== '*' && dbp
[1] == ')') /* within (* - *) comments */
2670 inquote
= TRUE
; /* found first quote */
2672 case '{': /* found open-{-comment */
2676 if (*dbp
== '*') /* found open-(*-comment */
2681 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
2684 case ')': /* end of parms list */
2689 if ((found_tag
) && (!inparms
)) /* end of proc or fn stmt */
2696 if ((found_tag
) && (verify_tag
) && (*dbp
!= ' '))
2698 /* check if this is an "extern" declaration */
2701 if ((*dbp
== 'e') || (*dbp
== 'E'))
2703 if (tail ("extern")) /* superfluous, really! */
2709 else if ((*dbp
== 'f') || (*dbp
== 'F'))
2711 if (tail ("forward")) /* check for forward reference */
2717 if ((found_tag
) && (verify_tag
)) /* not external proc, so make tag */
2721 pfnote (nambuf
, TRUE
, FALSE
, tline
.buffer
,
2722 cp
- tline
.buffer
+ 1, save_lineno
, save_lcno
);
2726 if (get_tagname
) /* grab name of proc or fn */
2731 /* save all values for later tagging */
2732 tline
.size
= lb
.size
;
2733 strcpy (tline
.buffer
, lb
.buffer
);
2734 save_lineno
= lineno
;
2735 save_lcno
= linecharno
;
2737 /* grab block name */
2738 for (cp
= dbp
+ 1; *cp
&& (!endtoken (*cp
)); cp
++)
2740 nambuf
= savenstr (dbp
, cp
-dbp
);
2741 dbp
= cp
; /* restore dbp to e-o-token */
2742 get_tagname
= FALSE
;
2746 /* and proceed to check for "extern" */
2748 else if (!incomment
&& !inquote
&& !found_tag
)
2750 /* check for proc/fn keywords */
2754 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
2758 if (tail ("unction"))
2763 } /* while not eof */
2767 * lisp tag functions
2768 * look for (def or (DEF, quote or QUOTE
2772 register char *strp
;
2774 return ((strp
[1] == 'd' || strp
[1] == 'D')
2775 && (strp
[2] == 'e' || strp
[2] == 'E')
2776 && (strp
[3] == 'f' || strp
[3] == 'F'));
2781 register char *strp
;
2783 return ((*(++strp
) == 'q' || *strp
== 'Q')
2784 && (*(++strp
) == 'u' || *strp
== 'U')
2785 && (*(++strp
) == 'o' || *strp
== 'O')
2786 && (*(++strp
) == 't' || *strp
== 'T')
2787 && (*(++strp
) == 'e' || *strp
== 'E')
2788 && isspace(*(++strp
)));
2796 if (*dbp
== '\'') /* Skip prefix quote */
2798 else if (*dbp
== '(' && L_isquote (dbp
)) /* Skip "(quote " */
2801 while (isspace(*dbp
))
2804 for (cp
= dbp
/*+1*/;
2805 *cp
&& *cp
!= '(' && *cp
!= ' ' && *cp
!= ')';
2811 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
, FALSE
, lb
.buffer
,
2812 cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2816 Lisp_functions (inf
)
2825 linecharno
= charno
;
2826 charno
+= readline (&lb
, inf
);
2832 while (!isspace (*dbp
))
2834 while (isspace (*dbp
))
2840 /* Check for (foo::defmumble name-defined ... */
2843 while (*dbp
&& !isspace (*dbp
)
2844 && *dbp
!= ':' && *dbp
!= '(' && *dbp
!= ')');
2849 while (*dbp
== ':');
2851 if (L_isdef (dbp
- 1))
2853 while (!isspace (*dbp
))
2855 while (isspace (*dbp
))
2866 * Scheme tag functions
2867 * look for (def... xyzzy
2868 * look for (def... (xyzzy
2869 * look for (def ... ((...(xyzzy ....
2870 * look for (set! xyzzy
2876 Scheme_functions (inf
)
2885 linecharno
= charno
;
2886 charno
+= readline (&lb
, inf
);
2888 if (dbp
[0] == '(' &&
2889 (dbp
[1] == 'D' || dbp
[1] == 'd') &&
2890 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
2891 (dbp
[3] == 'F' || dbp
[3] == 'f'))
2893 while (!isspace (*dbp
))
2895 /* Skip over open parens and white space */
2896 while (*dbp
&& (isspace (*dbp
) || *dbp
== '('))
2900 if (dbp
[0] == '(' &&
2901 (dbp
[1] == 'S' || dbp
[1] == 's') &&
2902 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
2903 (dbp
[3] == 'T' || dbp
[3] == 't') &&
2904 (dbp
[4] == '!' || dbp
[4] == '!') &&
2907 while (!isspace (*dbp
))
2909 /* Skip over white space */
2910 while (isspace (*dbp
))
2924 /* Go till you get to white space or a syntactic break */
2926 *cp
&& *cp
!= '(' && *cp
!= ')' && !isspace (*cp
);
2929 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
, FALSE
,
2930 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2933 /* Find tags in TeX and LaTeX input files. */
2935 /* TEX_toktab is a table of TeX control sequences that define tags.
2936 Each TEX_tabent records one such control sequence.
2937 CONVERT THIS TO USE THE Stab TYPE!! */
2944 struct TEX_tabent
*TEX_toktab
= NULL
; /* Table with tag tokens */
2946 /* Default set of control sequences to put into TEX_toktab.
2947 The value of environment var TEXTAGS is prepended to this. */
2949 char *TEX_defenv
= "\
2950 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem:typeout";
2953 struct TEX_tabent
*TEX_decode_env ();
2957 char TEX_esc
= '\\';
2958 char TEX_opgrp
= '{';
2959 char TEX_clgrp
= '}';
2962 * TeX/LaTeX scanning loop.
2973 /* Select either \ or ! as escape character. */
2976 /* Initialize token table once from environment. */
2978 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
2981 { /* Scan each line in file */
2983 linecharno
= charno
;
2984 charno
+= readline (&lb
, inf
);
2987 while (dbp
= etags_strchr (dbp
, TEX_esc
)) /* Look at each esc in line */
2993 linecharno
+= dbp
- lasthit
;
2995 i
= TEX_Token (lasthit
);
2998 TEX_getit (lasthit
, TEX_toktab
[i
].len
);
2999 break; /* We only save a line once */
3005 #define TEX_LESC '\\'
3006 #define TEX_SESC '!'
3009 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
3010 chars accordingly. */
3017 while ((c
= getc (inf
)) != EOF
)
3019 /* Skip to next line if we hit the TeX comment char. */
3023 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
3042 /* Read environment and prepend it to the default string.
3043 Build token table. */
3045 TEX_decode_env (evarname
, defenv
)
3049 register char *env
, *p
;
3051 struct TEX_tabent
*tab
;
3054 /* Append default string to environment. */
3055 env
= getenv (evarname
);
3059 env
= concat (env
, defenv
, "");
3061 /* Allocate a token table */
3062 for (size
= 1, p
= env
; p
;)
3063 if ((p
= etags_strchr (p
, ':')) && *(++p
))
3065 /* Add 1 to leave room for null terminator. */
3066 tab
= xnew (size
+ 1, struct TEX_tabent
);
3068 /* Unpack environment string into token table. Be careful about */
3069 /* zero-length strings (leading ':', "::" and trailing ':') */
3072 p
= etags_strchr (env
, ':');
3073 if (!p
) /* End of environment string. */
3074 p
= env
+ strlen (env
);
3076 { /* Only non-zero strings. */
3077 tab
[i
].name
= savenstr (env
, p
- env
);
3078 tab
[i
].len
= strlen (tab
[i
].name
);
3085 tab
[i
].name
= NULL
; /* Mark end of table. */
3093 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
3094 The name being defined actually starts at (NAME + LEN + 1).
3095 But we seem to include the TeX command in the tag name. */
3097 TEX_getit (name
, len
)
3101 char *p
= name
+ len
;
3106 /* Let tag name extend to next group close (or end of line) */
3107 while (*p
&& *p
!= TEX_clgrp
)
3109 pfnote (savenstr (name
, p
-name
), TRUE
, FALSE
, lb
.buffer
,
3110 strlen (lb
.buffer
), lineno
, linecharno
);
3113 /* If the text at CP matches one of the tag-defining TeX command names,
3114 return the pointer to the first occurrence of that command in TEX_toktab.
3115 Otherwise return -1.
3116 Keep the capital `T' in `Token' for dumb truncating compilers
3117 (this distinguishes it from `TEX_toktab' */
3124 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
3125 if (strneq (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
))
3130 /* Support for Prolog. */
3132 /* Whole head (not only functor, but also arguments)
3133 is gotten in compound term. */
3146 if (s
[0] == '\0') /* syntax error. */
3148 else if (insquote
&& s
[0] == '\'' && s
[1] == '\'')
3150 else if (s
[0] == '\'')
3152 insquote
= !insquote
;
3155 else if (!insquote
&& s
[0] == '(')
3160 else if (!insquote
&& s
[0] == ')')
3166 else if (npar
< 0) /* syntax error. */
3169 else if (!insquote
&& s
[0] == '.'
3170 && (isspace (s
[1]) || s
[1] == '\0'))
3172 if (npar
!= 0) /* syntax error. */
3180 pfnote (savenstr (save_s
, s
-save_s
), TRUE
, FALSE
,
3181 save_s
, s
-save_s
, lineno
, linecharno
);
3184 /* It is assumed that prolog predicate starts from column 0. */
3186 Prolog_functions (inf
)
3189 void skip_comment (), prolog_getit ();
3191 lineno
= linecharno
= charno
= 0;
3195 linecharno
+= charno
;
3196 charno
= readline (&lb
, inf
) + 1; /* 1 for newline. */
3198 if (isspace (dbp
[0])) /* not predicate header. */
3200 else if (dbp
[0] == '%') /* comment. */
3202 else if (dbp
[0] == '/' && dbp
[1] == '*') /* comment. */
3203 skip_comment (&lb
, inf
, &lineno
, &linecharno
);
3210 skip_comment (plb
, inf
, plineno
, plinecharno
)
3211 struct linebuffer
*plb
;
3213 int *plineno
; /* result */
3214 long *plinecharno
; /* result */
3220 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
3221 if (cp
[0] == '*' && cp
[1] == '/')
3224 *plinecharno
+= readline (plb
, inf
) + 1; /* 1 for newline. */
3229 #ifdef ETAGS_REGEXPS
3230 /* Take a string like "/blah/" and turn it into "blah", making sure
3231 that the first and last characters are the same, and handling
3232 quoted separator characters. Actually, stops on the occurence of
3233 an unquoted separator. Also turns "\t" into a Tab character.
3234 Returns pointer to terminating separator. Works in place. Null
3235 terminates name string. */
3237 scan_separators (name
)
3241 char *copyto
= name
;
3242 logical quoted
= FALSE
;
3244 for (++name
; *name
!= '\0'; ++name
)
3250 else if (*name
== sep
)
3254 /* Something else is quoted, so preserve the quote. */
3260 else if (*name
== '\\')
3262 else if (*name
== sep
)
3268 /* Terminate copied string. */
3273 /* Turn a name, which is an ed-style (but Emacs syntax) regular
3274 expression, into a real regular expression by compiling it. */
3276 add_regex (regexp_pattern
)
3277 char *regexp_pattern
;
3281 struct re_pattern_buffer
*patbuf
;
3283 if (regexp_pattern
== NULL
)
3285 /* Remove existing regexps. */
3291 if (regexp_pattern
[0] == '\0')
3293 error ("missing regexp", 0);
3296 if (regexp_pattern
[strlen(regexp_pattern
)-1] != regexp_pattern
[0])
3298 error ("%s: unterminated regexp", regexp_pattern
);
3301 name
= scan_separators (regexp_pattern
);
3302 if (regexp_pattern
[0] == '\0')
3304 error ("null regexp", 0);
3307 (void) scan_separators (name
);
3309 patbuf
= xnew (1, struct re_pattern_buffer
);
3310 patbuf
->translate
= NULL
;
3311 patbuf
->fastmap
= NULL
;
3312 patbuf
->buffer
= NULL
;
3313 patbuf
->allocated
= 0;
3315 err
= re_compile_pattern (regexp_pattern
, strlen (regexp_pattern
), patbuf
);
3318 error ("%s while compiling pattern", err
);
3323 if (num_patterns
== 1)
3324 patterns
= xnew (1, struct pattern
);
3326 patterns
= ((struct pattern
*)
3328 (num_patterns
* sizeof (struct pattern
))));
3329 patterns
[num_patterns
- 1].pattern
= patbuf
;
3330 patterns
[num_patterns
- 1].name_pattern
= savestr (name
);
3331 patterns
[num_patterns
- 1].error_signaled
= FALSE
;
3335 * Do the subtitutions indicated by the regular expression and
3339 substitute (in
, out
, regs
)
3341 struct re_registers
*regs
;
3343 char *result
= NULL
, *t
;
3346 /* Pass 1: figure out how much size to allocate. */
3347 for (t
= out
; *t
; ++t
)
3354 fprintf (stderr
, "%s: pattern subtitution ends prematurely\n",
3361 size
+= regs
->end
[dig
] - regs
->start
[dig
];
3366 /* Allocate space and do the substitutions. */
3367 result
= xnew (size
+ 1, char);
3376 /* Using "dig2" satisfies my debugger. Bleah. */
3377 int dig2
= *out
- '0';
3378 strncpy (result
+ size
, in
+ regs
->start
[dig2
],
3379 regs
->end
[dig2
] - regs
->start
[dig2
]);
3380 size
+= regs
->end
[dig2
] - regs
->start
[dig2
];
3387 result
[size
++] = '\t';
3393 result
[size
++] = *out
;
3399 result
[size
++] = *out
;
3401 result
[size
] = '\0';
3406 #endif /* ETAGS_REGEXPS */
3407 /* Initialize a linebuffer for use */
3409 initbuffer (linebuffer
)
3410 struct linebuffer
*linebuffer
;
3412 linebuffer
->size
= 200;
3413 linebuffer
->buffer
= xnew (200, char);
3417 * Read a line of text from `stream' into `linebuffer'.
3418 * Return the number of characters read from `stream',
3419 * which is the length of the line including the newline, if any.
3422 readline_internal (linebuffer
, stream
)
3423 struct linebuffer
*linebuffer
;
3424 register FILE *stream
;
3426 char *buffer
= linebuffer
->buffer
;
3427 register char *p
= linebuffer
->buffer
;
3428 register char *pend
;
3431 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
3435 register int c
= getc (stream
);
3438 linebuffer
->size
*= 2;
3439 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
3440 p
+= buffer
- linebuffer
->buffer
;
3441 pend
= buffer
+ linebuffer
->size
;
3442 linebuffer
->buffer
= buffer
;
3451 if (p
[-1] == '\r' && p
> buffer
)
3466 return p
- buffer
+ chars_deleted
;
3470 * Like readline_internal, above, but try to match the input
3471 * line against any existing regular expressions.
3474 readline (linebuffer
, stream
)
3475 struct linebuffer
*linebuffer
;
3478 /* Read new line. */
3480 long result
= readline_internal (linebuffer
, stream
);
3482 #ifdef ETAGS_REGEXPS
3483 /* Match against all listed patterns. */
3484 for (i
= 0; i
< num_patterns
; ++i
)
3486 int match
= re_match (patterns
[i
].pattern
, linebuffer
->buffer
,
3487 (int)result
, 0, &patterns
[i
].regs
);
3492 if (!patterns
[i
].error_signaled
)
3494 error ("error while matching pattern %d", i
);
3495 patterns
[i
].error_signaled
= TRUE
;
3502 /* Match occurred. Construct a tag. */
3503 if (patterns
[i
].name_pattern
[0] != '\0')
3505 /* Make a named tag. */
3506 char *name
= substitute (linebuffer
->buffer
,
3507 patterns
[i
].name_pattern
,
3510 pfnote (name
, TRUE
, TRUE
, linebuffer
->buffer
,
3511 match
, lineno
, linecharno
);
3515 /* Make an unnamed tag. */
3516 pfnote (NULL
, TRUE
, FALSE
, linebuffer
->buffer
,
3517 match
, lineno
, linecharno
);
3522 #endif /* ETAGS_REGEXPS */
3528 * Read a file, but do no processing. This is used to do regexp
3529 * matching on files that have no language defined.
3532 just_read_file (inf
)
3538 linecharno
= charno
;
3539 charno
+= readline (&lb
, inf
) + 1;
3545 * Return a pointer to a space of size strlen(cp)+1 allocated
3546 * with xnew where the string CP has been copied.
3552 return savenstr (cp
, strlen (cp
));
3556 * Return a pointer to a space of size LEN+1 allocated with xnew where
3557 * the string CP has been copied for at most the first LEN characters.
3566 dp
= xnew (len
+ 1, char);
3567 strncpy (dp
, cp
, len
);
3573 * Return the ptr in sp at which the character c last
3574 * appears; NULL if not found
3576 * Identical to System V strrchr, included for portability.
3579 etags_strrchr (sp
, c
)
3580 register char *sp
, c
;
3595 * Return the ptr in sp at which the character c first
3596 * appears; NULL if not found
3598 * Identical to System V strchr, included for portability.
3601 etags_strchr (sp
, c
)
3602 register char *sp
, c
;
3612 /* Print error message and exit. */
3629 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
3634 fprintf (stderr
, "%s: ", progname
);
3635 fprintf (stderr
, s1
, s2
);
3636 fprintf (stderr
, "\n");
3639 /* Return a newly-allocated string whose contents
3640 concatenate those of s1, s2, s3. */
3645 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
3646 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
3648 strcpy (result
, s1
);
3649 strcpy (result
+ len1
, s2
);
3650 strcpy (result
+ len1
+ len2
, s3
);
3651 result
[len1
+ len2
+ len3
] = '\0';
3656 /* Does the same work as the system V getcwd, but does not need to
3657 guess buffer size in advance. */
3662 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
3670 *p
++ = tolower (*p
);
3672 return strdup (path
);
3674 #elif HAVE_GETCWD /* not DOS_NT */
3677 char *path
= xnew (bufsize
, char);
3679 while (getcwd (path
, bufsize
) == NULL
)
3681 if (errno
!= ERANGE
)
3684 path
= xnew (bufsize
, char);
3689 #else /* not DOS_NT and not HAVE_GETCWD */
3691 struct linebuffer path
;
3695 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
3696 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
3702 #endif /* not DOS_NT and not HAVE_GETCWD */
3704 /* Return a newly allocated string containing the filename
3705 of FILE relative to the absolute directory DIR (which
3706 should end with a slash). */
3708 relative_filename (file
, dir
)
3711 char *fp
, *dp
, *res
;
3713 /* Find the common root of file and dir. */
3714 fp
= absolute_filename (file
, cwd
);
3716 while (*fp
++ == *dp
++)
3725 /* Build a sequence of "../" strings for the resulting relative filename. */
3726 for (dp
= etags_strchr (dp
+ 1, '/'), res
= "";
3728 dp
= etags_strchr (dp
+ 1, '/'))
3730 res
= concat (res
, "../", "");
3733 /* Add the filename relative to the common root of file and dir. */
3734 res
= concat (res
, fp
+ 1, "");
3736 return res
; /* temporary stub */
3739 /* Return a newly allocated string containing the
3740 absolute filename of FILE given CWD (which should
3741 end with a slash). */
3743 absolute_filename (file
, cwd
)
3746 char *slashp
, *cp
, *res
;
3749 res
= concat (file
, "", "");
3751 res
= concat (cwd
, file
, "");
3753 /* Delete the "/dirname/.." and "/." substrings. */
3754 slashp
= etags_strchr (res
, '/');
3755 while (slashp
!= NULL
&& slashp
[0] != '\0')
3757 if (slashp
[1] == '.')
3759 if (slashp
[2] == '.'
3760 && (slashp
[3] == '/' || slashp
[3] == '\0'))
3765 while (cp
>= res
&& *cp
!= '/');
3768 strcpy (cp
, slashp
+ 3);
3770 else /* else (cp == res) */
3772 if (slashp
[3] != '\0')
3773 strcpy (cp
, slashp
+ 4);
3780 else if (slashp
[2] == '/' || slashp
[2] == '\0')
3782 strcpy (slashp
, slashp
+ 2);
3787 slashp
= etags_strchr (slashp
+ 1, '/');
3793 /* Return a newly allocated string containing the absolute
3794 filename of dir where FILE resides given CWD (which should
3795 end with a slash). */
3797 absolute_dirname (file
, cwd
)
3803 slashp
= etags_strrchr (file
, '/');
3808 res
= absolute_filename (file
, cwd
);
3814 /* Like malloc but get fatal error if memory is exhausted. */
3819 char *result
= (char *) malloc (size
);
3821 fatal ("virtual memory exhausted", 0);
3826 xrealloc (ptr
, size
)
3830 char *result
= (char *) realloc (ptr
, size
);
3832 fatal ("virtual memory exhausted");