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.
35 char pot_etags_version
[] = "@(#) pot revision number is 11.19";
38 #include <sys/param.h>
45 #define MAXPATHLEN _MAX_PATH
50 /* On some systems, Emacs defines static as nothing for the sake
51 of unexec. We don't want that here since we don't use unexec. */
61 #include <sys/types.h>
64 #if !defined (S_ISREG) && defined (S_IFREG)
65 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
72 #endif /* ETAGS_REGEXPS */
77 /* Define CTAGS to make the program "ctags" compatible with the usual one.
78 Let it undefined to make the program "etags", which makes emacs-style
79 tag tables and tags typedefs, #defines and struct/union/enum by default. */
87 /* Exit codes for success and failure. */
97 #define C_PLPL 0x00001 /* C++ */
98 #define C_STAR 0x00003 /* C* */
99 #define YACC 0x10000 /* yacc file */
101 #define streq(s,t) (strcmp (s, t) == 0)
102 #define strneq(s,t,n) (strncmp (s, t, n) == 0)
104 #define iswhite(arg) (_wht[arg]) /* T if char is white */
105 #define begtoken(arg) (_btk[arg]) /* T if char can start token */
106 #define intoken(arg) (_itk[arg]) /* T if char can be in token */
107 #define endtoken(arg) (_etk[arg]) /* T if char ends tokens */
110 * xnew -- allocate storage
112 * SYNOPSIS: Type *xnew (int n, Type);
114 #define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
119 { /* sorting structure */
120 char *name
; /* function or type name */
121 char *file
; /* file name */
122 logical is_func
; /* use pattern or line no */
123 logical named
; /* list name separately */
124 logical been_warned
; /* set if noticed dup */
125 int lno
; /* line number tag is on */
126 long cno
; /* character number line starts on */
127 char *pat
; /* search pattern */
128 struct nd_st
*left
, *right
; /* left and right sons */
131 extern char *getenv ();
134 char *savenstr (), *savestr ();
135 char *etags_strchr (), *etags_strrchr ();
136 char *etags_getcwd ();
137 char *relative_filename (), *absolute_filename (), *absolute_dirname ();
138 char *xmalloc (), *xrealloc ();
140 typedef void Lang_function ();
141 #if FALSE /* many compilers barf on this */
142 Lang_function Asm_labels
;
143 Lang_function default_C_entries
;
144 Lang_function C_entries
;
145 Lang_function Cplusplus_entries
;
146 Lang_function Cstar_entries
;
147 Lang_function Fortran_functions
;
148 Lang_function Yacc_entries
;
149 Lang_function Lisp_functions
;
150 Lang_function Pascal_functions
;
151 Lang_function Prolog_functions
;
152 Lang_function Scheme_functions
;
153 Lang_function TeX_functions
;
154 Lang_function just_read_file
;
155 #else /* so let's write it this way */
157 void default_C_entries ();
159 void Cplusplus_entries ();
160 void Cstar_entries ();
161 void Fortran_functions ();
162 void Yacc_entries ();
163 void Lisp_functions ();
164 void Pascal_functions ();
165 void Prolog_functions ();
166 void Scheme_functions ();
167 void TeX_functions ();
168 void just_read_file ();
171 logical
get_language ();
172 int total_size_of_entries ();
174 long readline_internal ();
180 void fatal (), pfatal ();
181 void find_entries ();
187 void process_file ();
192 char searchar
= '/'; /* use /.../ searches */
194 int lineno
; /* line number of current line */
195 long charno
; /* current character number */
197 long linecharno
; /* charno of start of line; not used by C,
198 but by every other language. */
200 char *curfile
; /* current input file name */
201 char *tagfile
; /* output file */
202 char *progname
; /* name this program was invoked with */
203 char *cwd
; /* current working directory */
204 char *tagfiledir
; /* directory of tagfile */
206 FILE *tagf
; /* ioptr for tags file */
207 NODE
*head
; /* the head of the binary tree of tags */
210 * A `struct linebuffer' is a structure which holds a line of text.
211 * `readline' reads a line from a stream into a linebuffer and works
212 * regardless of the length of the line.
220 struct linebuffer lb
; /* the current line */
224 struct linebuffer lb
; /* used by C_entries instead of lb */
227 /* boolean "functions" (see init) */
228 logical _wht
[0177], _etk
[0177], _itk
[0177], _btk
[0177];
230 *white
= " \f\t\n\013", /* white chars */
231 *endtk
= " \t\n\013\"'#()[]{}=-+%*/&|^~!<>;,.:?", /* token ending chars */
232 /* token starting chars */
233 *begtk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~",
234 /* valid in-token chars */
235 *intk
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
237 logical append_to_tagfile
; /* -a: append to tags */
238 /* The following three default to TRUE for etags, but to FALSE for ctags. */
239 logical typedefs
; /* -t: create tags for typedefs */
240 logical typedefs_and_cplusplus
; /* -T: create tags for typedefs, level */
241 /* 0 struct/enum/union decls, and C++ */
242 /* member functions. */
243 logical constantypedefs
; /* -d: create tags for C #define and enum */
244 /* constants. Enum consts not implemented. */
245 /* -D: opposite of -d. Default under ctags. */
246 logical update
; /* -u: update tags */
247 logical vgrind_style
; /* -v: create vgrind style index output */
248 logical no_warnings
; /* -w: suppress warnings */
249 logical cxref_style
; /* -x: create cxref style output */
250 logical cplusplus
; /* .[hc] means C++, not C */
251 logical noindentypedefs
; /* -I: ignore indentation in C */
252 #define permit_duplicates TRUE /* allow duplicate tags */
254 struct option longopts
[] =
256 { "append", no_argument
, NULL
, 'a' },
257 { "backward-search", no_argument
, NULL
, 'B' },
258 { "c++", no_argument
, NULL
, 'C' },
259 { "cxref", no_argument
, NULL
, 'x' },
260 { "defines", no_argument
, NULL
, 'd' },
261 { "help", no_argument
, NULL
, 'h' },
262 { "help", no_argument
, NULL
, 'H' },
263 { "ignore-indentation", no_argument
, NULL
, 'I' },
264 { "include", required_argument
, NULL
, 'i' },
265 { "language", required_argument
, NULL
, 'l' },
266 { "no-defines", no_argument
, NULL
, 'D' },
267 { "no-regex", no_argument
, NULL
, 'R' },
268 { "no-warn", no_argument
, NULL
, 'w' },
269 { "output", required_argument
, NULL
, 'o' },
270 { "regex", required_argument
, NULL
, 'r' },
271 { "typedefs", no_argument
, NULL
, 't' },
272 { "typedefs-and-c++", no_argument
, NULL
, 'T' },
273 { "update", no_argument
, NULL
, 'u' },
274 { "version", no_argument
, NULL
, 'V' },
275 { "vgrind", no_argument
, NULL
, 'v' },
280 /* Structure defining a regular expression. Elements are
281 the compiled pattern, and the name string. */
284 struct re_pattern_buffer
*pattern
;
285 struct re_registers regs
;
287 logical error_signaled
;
290 /* Number of regexps found. */
291 int num_patterns
= 0;
293 /* Array of all regexps. */
294 struct pattern
*patterns
= NULL
;
295 #endif /* ETAGS_REGEXPS */
297 /* Language stuff. */
301 Lang_function
*function
;
304 /* Table of language names and corresponding functions. */
305 /* It is ok for a given function to be listed under more than one
306 name. I just didn't. */
307 /* "auto" language reverts to default behavior. */
308 struct lang_entry lang_names
[] =
310 { "asm", Asm_labels
},
311 { "c", default_C_entries
},
312 { "c++", Cplusplus_entries
},
313 { "c*", Cstar_entries
},
314 { "fortran", Fortran_functions
},
315 { "lisp", Lisp_functions
},
316 { "none", just_read_file
},
317 { "pascal", Pascal_functions
},
318 { "scheme" , Scheme_functions
},
319 { "tex", TeX_functions
},
324 /* Table of file extensions and corresponding language functions. */
325 struct lang_entry lang_extensions
[] =
327 /* Assume that ".s" or ".a" is assembly code. -wolfgang.
329 { "a", Asm_labels
}, /* Unix assembler */
330 { "asm", Asm_labels
}, /* Microcontroller assembly */
331 { "def", Asm_labels
}, /* BSO/Tasking definition includes */
332 { "inc", Asm_labels
}, /* Microcontroller include files */
333 { "ins", Asm_labels
}, /* Microcontroller include files */
335 { "sa", Asm_labels
}, /* Unix assembler */
336 { "src", Asm_labels
}, /* BSO/Tasking C compiler output */
338 /* .aux, .bbl, .clo, .cls, .dtx or .tex implies LaTeX source code. */
339 { "aux", TeX_functions
},
340 { "bbl", TeX_functions
},
341 { "clo", TeX_functions
},
342 { "cls", TeX_functions
},
343 { "dtx", TeX_functions
},
344 { "sty", TeX_functions
},
345 { "tex", TeX_functions
},
347 /* .l or .el or .lisp (or .cl or .clisp or ...) implies lisp source code */
348 { "cl", Lisp_functions
},
349 { "clisp", Lisp_functions
},
350 { "el", Lisp_functions
},
351 { "l", Lisp_functions
},
352 { "lisp", Lisp_functions
},
353 { "lsp", Lisp_functions
},
355 /* .scm or .sm or .scheme implies scheme source code */
356 { "SCM", Scheme_functions
},
357 { "SM", Scheme_functions
},
358 { "oak", Scheme_functions
},
359 { "sch", Scheme_functions
},
360 { "scheme", Scheme_functions
},
361 { "scm", Scheme_functions
},
362 { "sm", Scheme_functions
},
363 { "t", Scheme_functions
},
364 /* FIXME Can't do the `SCM' or `scm' prefix with a version number */
366 /* Note that ".c" and ".h" can be considered C++, if the --c++
367 flag was given. That is why default_C_entries is called here. */
368 { "c", default_C_entries
},
369 { "h", default_C_entries
},
371 /* .C or .H or .cpp or .cxx or .hxx or .hh or .cc or .cpp: a C++ file */
372 { "C", Cplusplus_entries
},
373 { "H", Cplusplus_entries
},
374 { "cc", Cplusplus_entries
},
375 { "cpp", Cplusplus_entries
},
376 { "cxx", Cplusplus_entries
},
377 { "hh", Cplusplus_entries
},
378 { "hxx", Cplusplus_entries
},
380 /* .y: a yacc file */
381 { "y", Yacc_entries
},
383 /* .cs or .hs: a C* file */
384 { "cs", Cstar_entries
},
385 { "hs", Cstar_entries
},
387 /* .f and .for are FORTRAN. */
388 { "F", Fortran_functions
},
389 { "f", Fortran_functions
},
390 { "for", Fortran_functions
},
392 /* .pl implies prolog source code */
393 { "pl", Prolog_functions
},
395 /* .p or .pas: a Pascal file */
396 { "p", Pascal_functions
},
397 { "pas", Pascal_functions
},
402 /* Non-NULL if language fixed. */
403 Lang_function
*lang_func
= NULL
;
407 print_language_names ()
409 struct lang_entry
*name
, *ext
;
411 puts ("\nThese are the currently supported languages, along with the\n\
412 default extensions for files:");
413 for (name
= lang_names
; name
->extension
; ++name
)
415 printf ("\t%s\t", name
->extension
);
416 for (ext
= lang_extensions
; ext
->extension
; ++ext
)
417 if (name
->function
== ext
->function
)
418 printf (" .%s", ext
->extension
);
421 puts ("Where `auto' means use default language for files based on filename\n\
422 extension, and `none' means only do regexp processing on files.\n\
423 If no language is specified and no extension is found for some file,\n\
424 Fortran is tried first; if no tags are found, C is tried next.");
431 printf ("%s for Emacs version %s.\n", (CTAGS
) ? "CTAGS" : "ETAGS", VERSION
);
433 printf ("%s for Emacs version 19.\n", (CTAGS
) ? "CTAGS" : "ETAGS");
442 printf ("These are the options accepted by %s. You may use unambiguous\n\
443 abbreviations for the long option names. A - as file name means read\n\
444 names from stdin.\n\n", progname
);
446 puts ("-a, --append\n\
447 Append tag entries to existing tags file.");
450 puts ("-B, --backward-search\n\
451 Write the search commands for the tag entries using '?', the\n\
452 backward-search command instead of '/', the forward-search command.");
455 Treat files whose extension defaults to C language as C++ files.");
458 puts ("-d, --defines\n\
459 Create tag entries for constant C #defines, too.");
461 puts ("-D, --no-defines\n\
462 Don't create tag entries for constant C #defines. This makes\n\
463 the tags file smaller.");
467 puts ("-i FILE, --include=FILE\n\
468 Include a note in tag file indicating that, when searching for\n\
469 a tag, one should also consult the tags file FILE after\n\
470 checking the current file.");
471 puts ("-l LANG, --language=LANG\n\
472 Force the following files to be considered as written in the\n\
473 named language up to the next --language=LANG option.");
477 puts ("-r /REGEXP/, --regex=/REGEXP/\n\
478 Make a tag for each line matching pattern REGEXP in the\n\
479 following files. REGEXP is anchored (as if preceded by ^).\n\
480 The form /REGEXP/NAME/ creates a named tag. For example Tcl\n\
481 named tags can be created with:\n\
482 --regex=/proc[ \\t]+\\([^ \\t]+\\)/\\1/.");
483 puts ("-R, --no-regex\n\
484 Don't create tags from regexps for the following files.");
485 #endif /* ETAGS_REGEXPS */
486 puts ("-o FILE, --output=FILE\n\
487 Write the tags to FILE.");
488 puts ("-I, --ignore-indentation\n\
489 Don't rely on indentation quite as much as normal. Currently,\n\
490 this means not to assume that a closing brace in the first\n\
491 column is the final brace of a function or structure\n\
492 definition in C and C++.");
496 puts ("-t, --typedefs\n\
497 Generate tag entries for C typedefs.");
498 puts ("-T, --typedefs-and-c++\n\
499 Generate tag entries for C typedefs, C struct/enum/union tags,\n\
500 and C++ member functions.");
501 puts ("-u, --update\n\
502 Update the tag entries for the given files, leaving tag\n\
503 entries for other files in place. Currently, this is\n\
504 implemented by deleting the existing entries for the given\n\
505 files and then rewriting the new entries at the end of the\n\
506 tags file. It is often faster to simply rebuild the entire\n\
507 tag file than to use this.");
508 puts ("-v, --vgrind\n\
509 Generates an index of items intended for human consumption,\n\
510 similar to the output of vgrind. The index is sorted, and\n\
511 gives the page number of each item.");
512 puts ("-w, --no-warn\n\
513 Suppress warning messages about entries defined in multiple\n\
515 puts ("-x, --cxref\n\
516 Like --vgrind, but in the style of cxref, rather than vgrind.\n\
517 The output uses line numbers instead of page numbers, but\n\
518 beyond that the differences are cosmetic; try both to see\n\
522 puts ("-V, --version\n\
523 Print the version of the program.\n\
525 Print this help message.");
527 print_language_names ();
540 /* This structure helps us allow mixing of --lang and filenames. */
543 enum argument_type arg_type
;
545 Lang_function
*function
;
548 #ifdef VMS /* VMS specific functions */
552 /* This is a BUG! ANY arbitrary limit is a BUG!
553 Won't someone please fix this? */
554 #define MAX_FILE_SPEC_LEN 255
557 char body
[MAX_FILE_SPEC_LEN
+ 1];
561 v1.05 nmm 26-Jun-86 fn_exp - expand specification of list of file names
562 returning in each successive call the next filename matching the input
563 spec. The function expects that each in_spec passed
564 to it will be processed to completion; in particular, up to and
565 including the call following that in which the last matching name
566 is returned, the function ignores the value of in_spec, and will
567 only start processing a new spec with the following call.
568 If an error occurs, on return out_spec contains the value
569 of in_spec when the error occurred.
571 With each successive filename returned in out_spec, the
572 function's return value is one. When there are no more matching
573 names the function returns zero. If on the first call no file
574 matches in_spec, or there is any other error, -1 is returned.
579 #define OUTSIZE MAX_FILE_SPEC_LEN
585 static long context
= 0;
586 static struct dsc$descriptor_s o
;
587 static struct dsc$descriptor_s i
;
588 static logical pass1
= TRUE
;
595 o
.dsc$a_pointer
= (char *) out
;
596 o
.dsc$w_length
= (short)OUTSIZE
;
597 i
.dsc$a_pointer
= in
;
598 i
.dsc$w_length
= (short)strlen(in
);
599 i
.dsc$b_dtype
= DSC$K_DTYPE_T
;
600 i
.dsc$b_class
= DSC$K_CLASS_S
;
601 o
.dsc$b_dtype
= DSC$K_DTYPE_VT
;
602 o
.dsc$b_class
= DSC$K_CLASS_VS
;
604 if ((status
= lib$
find_file(&i
, &o
, &context
, 0, 0)) == RMS$_NORMAL
)
606 out
->body
[out
->curlen
] = EOS
;
609 else if (status
== RMS$_NMF
)
613 strcpy(out
->body
, in
);
616 lib$
find_file_end(&context
);
622 v1.01 nmm 19-Aug-85 gfnames - return in successive calls the
623 name of each file specified by the provided arg expanding wildcards.
626 gfnames (arg
, p_error
)
630 static vspec filename
= {MAX_FILE_SPEC_LEN
, "\0"};
632 switch (fn_exp (&filename
, arg
))
636 return filename
.body
;
642 return filename
.body
;
646 #ifndef OLD /* Newer versions of VMS do provide `system'. */
650 fprintf (stderr
, "system() function not implemented under VMS\n");
654 #define VERSION_DELIM ';'
655 char *massage_name (s
)
661 if (*s
== VERSION_DELIM
)
679 unsigned int nincluded_files
= 0;
680 char **included_files
= xnew (argc
, char *);
683 int current_arg
= 0, file_count
= 0;
684 struct linebuffer filename_lb
;
690 _fmode
= O_BINARY
; /* all of files are treated as binary files */
695 /* Allocate enough no matter what happens. Overkill, but each one
697 argbuffer
= xnew (argc
, ARGUMENT
);
700 /* Set syntax for regular expression routines. */
701 re_set_syntax (RE_SYNTAX_EMACS
);
702 #endif /* ETAGS_REGEXPS */
705 * If etags, always find typedefs and structure tags. Why not?
706 * Also default is to find macro constants.
709 typedefs
= typedefs_and_cplusplus
= constantypedefs
= TRUE
;
713 int opt
= getopt_long (argc
, argv
,
714 "-aCdDf:Il:o:r:RStTi:BuvxwVhH", longopts
, 0);
722 /* If getopt returns 0, then it has already processed a
723 long-named option. We should do nothing. */
727 /* This means that a filename has been seen. Record it. */
728 argbuffer
[current_arg
].arg_type
= at_filename
;
729 argbuffer
[current_arg
].what
= optarg
;
734 /* Common options. */
736 append_to_tagfile
= TRUE
;
742 constantypedefs
= TRUE
;
745 constantypedefs
= FALSE
;
747 case 'f': /* for compatibility with old makefiles */
751 fprintf (stderr
, "%s: -%c option may only be given once.\n",
758 case 'S': /* for backward compatibility */
759 noindentypedefs
= TRUE
;
762 if (!get_language (optarg
, &argbuffer
[current_arg
].function
))
764 fprintf (stderr
, "%s: language \"%s\" not recognized.\n",
768 argbuffer
[current_arg
].arg_type
= at_language
;
773 argbuffer
[current_arg
].arg_type
= at_regexp
;
774 argbuffer
[current_arg
].what
= optarg
;
778 argbuffer
[current_arg
].arg_type
= at_regexp
;
779 argbuffer
[current_arg
].what
= NULL
;
782 #endif /* ETAGS_REGEXPS */
794 typedefs
= typedefs_and_cplusplus
= TRUE
;
799 included_files
[nincluded_files
++] = optarg
;
824 for (; optind
< argc
; ++optind
)
826 argbuffer
[current_arg
].arg_type
= at_filename
;
827 argbuffer
[current_arg
].what
= argv
[optind
];
832 if (nincluded_files
== 0 && file_count
== 0)
834 fprintf (stderr
, "%s: No input files specified.\n", progname
);
837 fprintf (stderr
, "\tTry `%s --help' for a complete list of options.\n",
844 tagfile
= CTAGS
? "tags" : "TAGS";
846 cwd
= etags_getcwd (); /* the current working directory */
848 if (streq (tagfile
, "-"))
854 tagfiledir
= absolute_dirname (tagfile
, cwd
);
857 init (); /* set up boolean "functions" */
860 initbuffer (&lbs
[0].lb
);
861 initbuffer (&lbs
[1].lb
);
862 initbuffer (&filename_lb
);
866 if (streq (tagfile
, "-"))
869 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
875 * Loop through files finding functions.
877 for (i
= 0; i
< current_arg
; ++i
)
879 switch (argbuffer
[i
].arg_type
)
882 lang_func
= argbuffer
[i
].function
;
886 add_regex (argbuffer
[i
].what
);
891 while ((this_file
= gfnames (argbuffer
[i
].what
, &got_err
)) != NULL
)
895 error ("Can't find file %s\n", this_file
);
900 this_file
= massage_name (this_file
);
903 this_file
= argbuffer
[i
].what
;
905 /* Input file named "-" means read file names from stdin
907 if (streq (this_file
, "-"))
909 while (!feof (stdin
))
911 (void) readline_internal (&filename_lb
, stdin
);
912 if (strlen (filename_lb
.buffer
) > 0)
913 process_file (filename_lb
.buffer
);
917 process_file (this_file
);
927 while (nincluded_files
-- > 0)
928 fprintf (tagf
, "\f\n%s,include\n", *included_files
++);
934 /* If CTAGS, we are here. process_file did not write the tags yet,
935 because we want them ordered. Let's do it now. */
938 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
948 for (i
= 0; i
< current_arg
; ++i
)
950 if (argbuffer
[i
].arg_type
!= at_filename
)
953 "mv %s OTAGS;fgrep -v '\t%s\t' OTAGS >%s;rm OTAGS",
954 tagfile
, argbuffer
[i
].what
, tagfile
);
955 if (system (cmd
) != GOOD
)
956 fatal ("failed to execute shell command");
958 append_to_tagfile
= TRUE
;
961 tagf
= fopen (tagfile
, append_to_tagfile
? "a" : "w");
970 sprintf (cmd
, "sort %s -o %s", tagfile
, tagfile
);
978 * Set the language, given the name.
981 get_language (language
, func
)
983 Lang_function
**func
;
985 struct lang_entry
*lang
;
987 for (lang
= lang_names
; lang
->extension
; ++lang
)
989 if (streq (language
, lang
->extension
))
991 *func
= lang
->function
;
1001 * This routine is called on each file argument.
1007 struct stat stat_buf
;
1010 if (stat (file
, &stat_buf
) == 0 && !S_ISREG (stat_buf
.st_mode
))
1012 fprintf (stderr
, "Skipping %s: it is not a regular file.\n", file
);
1015 if (streq (file
, tagfile
) && !streq (tagfile
, "-"))
1017 fprintf (stderr
, "Skipping inclusion of %s in self.\n", file
);
1020 inf
= fopen (file
, "r");
1027 find_entries (file
, inf
);
1035 /* file is an absolute filename. Canonicalise it. */
1036 filename
= absolute_filename (file
, cwd
);
1040 /* file is a filename relative to cwd. Make it relative
1041 to the directory of the tags file. */
1042 filename
= relative_filename (file
, tagfiledir
);
1044 fprintf (tagf
, "\f\n%s,%d\n", filename
, total_size_of_entries (head
));
1052 * This routine sets up the boolean pseudo-functions which work
1053 * by setting boolean flags dependent upon the corresponding character
1054 * Every char which is NOT in that string is not a white char. Therefore,
1055 * all of the array "_wht" is set to FALSE, and then the elements
1056 * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
1057 * of a char is TRUE if it is the string "white", else FALSE.
1065 for (i
= 0; i
< 0177; i
++)
1066 _wht
[i
] = _etk
[i
] = _itk
[i
] = _btk
[i
] = FALSE
;
1067 for (sp
= white
; *sp
; sp
++)
1069 for (sp
= endtk
; *sp
; sp
++)
1071 for (sp
= intk
; *sp
; sp
++)
1073 for (sp
= begtk
; *sp
; sp
++)
1075 _wht
[0] = _wht
['\n'];
1076 _etk
[0] = _etk
['\n'];
1077 _btk
[0] = _btk
['\n'];
1078 _itk
[0] = _itk
['\n'];
1082 * This routine opens the specified file and calls the function
1083 * which finds the function and type definitions.
1086 find_entries (file
, inf
)
1091 struct lang_entry
*lang
;
1092 NODE
*old_last_node
;
1093 extern NODE
*last_node
;
1095 curfile
= savestr (file
);
1096 cp
= etags_strrchr (file
, '.');
1098 /* If user specified a language, use it. */
1099 if (lang_func
!= NULL
)
1109 for (lang
= lang_extensions
; lang
->extension
; ++lang
)
1111 if (streq (cp
, lang
->extension
))
1113 lang
->function (inf
);
1121 old_last_node
= last_node
;
1122 Fortran_functions (inf
);
1124 /* No Fortran entries found. Try C. */
1125 if (old_last_node
== last_node
)
1126 default_C_entries (inf
);
1131 /* Should take a TOKEN* instead!! */
1133 pfnote (name
, is_func
, named
, linestart
, linelen
, lno
, cno
)
1134 char *name
; /* tag name */
1135 logical is_func
; /* tag is a function */
1136 logical named
; /* tag different from text of definition */
1137 char *linestart
; /* start of the line where tag is */
1138 int linelen
; /* length of the line where tag is */
1139 int lno
; /* line number */
1140 long cno
; /* character number */
1142 register NODE
*np
= xnew (1, NODE
);
1145 /* If ctags mode, change name "main" to M<thisfilename>. */
1146 if (CTAGS
&& !cxref_style
&& streq (name
, "main"))
1148 fp
= etags_strrchr (curfile
, '/');
1149 np
->name
= concat ("M", fp
== 0 ? curfile
: fp
+ 1, "");
1150 fp
= etags_strrchr (np
->name
, '.');
1151 if (fp
&& fp
[1] != '\0' && fp
[2] == '\0')
1160 np
->been_warned
= FALSE
;
1162 np
->is_func
= is_func
;
1164 /* Our char numbers are 0-base, because of C language tradition?
1165 ctags compatibility? old versions compatibility? I don't know.
1166 Anyway, since emacs's are 1-base we espect etags.el to take care
1167 of the difference. If we wanted to have 1-based numbers, we would
1168 uncomment the +1 below. */
1169 np
->cno
= cno
/* + 1 */ ;
1170 np
->left
= np
->right
= NULL
;
1171 np
->pat
= savenstr (linestart
, ((CTAGS
&& !cxref_style
) ? 50 : linelen
));
1173 add_node (np
, &head
);
1178 * recurse on left children, iterate on right children.
1182 register NODE
*node
;
1186 register NODE
*node_right
= node
->right
;
1187 free_tree (node
->left
);
1191 free ((char *) node
);
1198 * Adds a node to the tree of nodes. In etags mode, we don't keep
1199 * it sorted; we just keep a linear list. In ctags mode, maintain
1200 * an ordered tree, with no attempt at balancing.
1202 * add_node is the only function allowed to add nodes, so it can
1205 NODE
*last_node
= NULL
;
1207 add_node (node
, cur_node_p
)
1208 NODE
*node
, **cur_node_p
;
1211 register NODE
*cur_node
= *cur_node_p
;
1213 if (cur_node
== NULL
)
1223 if (last_node
== NULL
)
1224 fatal ("internal error in add_node", 0);
1225 last_node
->right
= node
;
1231 dif
= strcmp (node
->name
, cur_node
->name
);
1234 * If this tag name matches an existing one, then
1235 * do not add the node, but maybe print a warning.
1239 if (node
->file
== cur_node
->file
)
1243 fprintf (stderr
, "Duplicate entry in file %s, line %d: %s\n",
1244 node
->file
, lineno
, node
->name
);
1245 fprintf (stderr
, "Second entry ignored\n");
1249 if (!cur_node
->been_warned
&& !no_warnings
)
1252 "Duplicate entry in files %s and %s: %s (Warning only)\n",
1253 node
->file
, cur_node
->file
, node
->name
);
1255 cur_node
->been_warned
= TRUE
;
1259 /* Maybe refuse to add duplicate nodes. */
1260 if (!permit_duplicates
)
1262 if (streq (node
->name
, cur_node
->name
)
1263 && streq (node
->file
, cur_node
->file
))
1267 /* Actually add the node */
1268 add_node (node
, dif
< 0 ? &cur_node
->left
: &cur_node
->right
);
1274 register NODE
*node
;
1281 /* Output subentries that precede this one */
1282 put_entries (node
->left
);
1284 /* Output this entry */
1290 fprintf (tagf
, "%s\177%s\001%d,%d\n",
1291 node
->pat
, node
->name
,
1292 node
->lno
, node
->cno
);
1296 fprintf (tagf
, "%s\177%d,%d\n",
1298 node
->lno
, node
->cno
);
1301 else if (!cxref_style
)
1303 fprintf (tagf
, "%s\t%s\t",
1304 node
->name
, node
->file
);
1308 putc (searchar
, tagf
);
1311 for (sp
= node
->pat
; *sp
; sp
++)
1313 if (*sp
== '\\' || *sp
== searchar
)
1317 putc (searchar
, tagf
);
1320 { /* a typedef; text pattern inadequate */
1321 fprintf (tagf
, "%d", node
->lno
);
1325 else if (vgrind_style
)
1326 fprintf (stdout
, "%s %s %d\n",
1327 node
->name
, node
->file
, (node
->lno
+ 63) / 64);
1329 fprintf (stdout
, "%-16s %3d %-16s %s\n",
1330 node
->name
, node
->lno
, node
->file
, node
->pat
);
1332 /* Output subentries that follow this one */
1333 put_entries (node
->right
);
1336 /* Length of a number's decimal representation. */
1344 for (; num
; num
/= 10)
1350 * Return total number of characters that put_entries will output for
1351 * the nodes in the subtree of the specified node. Works only if
1352 * we are not ctags, but called only in that case. This count
1353 * is irrelevant with the new tags.el, but is still supplied for
1354 * backward compatibility.
1357 total_size_of_entries (node
)
1358 register NODE
*node
;
1366 for (; node
; node
= node
->right
)
1368 /* Count left subentries. */
1369 total
+= total_size_of_entries (node
->left
);
1371 /* Count this entry */
1372 total
+= strlen (node
->pat
) + 1;
1373 total
+= number_len ((long) node
->lno
) + 1 + number_len (node
->cno
) + 1;
1375 total
+= 1 + strlen (node
->name
); /* \001name */
1382 * The C symbol tables.
1386 st_none
, st_C_struct
, st_C_enum
, st_C_define
, st_C_typedef
, st_C_typespec
1389 /* Feed stuff between (but not including) %[ and %] lines to:
1390 gperf -c -k1,3 -o -p -r -t
1392 struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
1394 class, C_PLPL, st_C_struct
1395 domain, C_STAR, st_C_struct
1396 union, 0, st_C_struct
1397 struct, 0, st_C_struct
1399 typedef, 0, st_C_typedef
1400 define, 0, st_C_define
1401 long, 0, st_C_typespec
1402 short, 0, st_C_typespec
1403 int, 0, st_C_typespec
1404 char, 0, st_C_typespec
1405 float, 0, st_C_typespec
1406 double, 0, st_C_typespec
1407 signed, 0, st_C_typespec
1408 unsigned, 0, st_C_typespec
1409 auto, 0, st_C_typespec
1410 void, 0, st_C_typespec
1411 extern, 0, st_C_typespec
1412 static, 0, st_C_typespec
1413 const, 0, st_C_typespec
1414 volatile, 0, st_C_typespec
1416 and replace lines between %< and %> with its output. */
1418 /* C code produced by gperf version 1.8.1 (K&R C version) */
1419 /* Command-line: gperf -c -k1,3 -o -p -r -t */
1422 struct C_stab_entry
{ char *name
; int c_ext
; enum sym_type type
; };
1424 #define MIN_WORD_LENGTH 3
1425 #define MAX_WORD_LENGTH 8
1426 #define MIN_HASH_VALUE 10
1427 #define MAX_HASH_VALUE 62
1430 53 is the maximum key range
1438 static unsigned char hash_table
[] =
1440 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
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, 2, 62, 7,
1450 6, 9, 15, 30, 62, 24, 62, 62, 1, 24,
1451 7, 27, 13, 62, 19, 26, 18, 27, 1, 62,
1452 62, 62, 62, 62, 62, 62, 62, 62,
1454 return len
+ hash_table
[str
[2]] + hash_table
[str
[0]];
1457 struct C_stab_entry
*
1458 in_word_set (str
, len
)
1463 static struct C_stab_entry wordlist
[] =
1465 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
1467 {"volatile", 0, st_C_typespec
},
1469 {"long", 0, st_C_typespec
},
1470 {"char", 0, st_C_typespec
},
1471 {"class", C_PLPL
, st_C_struct
},
1472 {"",}, {"",}, {"",}, {"",},
1473 {"const", 0, st_C_typespec
},
1474 {"",}, {"",}, {"",}, {"",},
1475 {"auto", 0, st_C_typespec
},
1477 {"define", 0, st_C_define
},
1479 {"void", 0, st_C_typespec
},
1480 {"",}, {"",}, {"",},
1481 {"extern", 0, st_C_typespec
},
1482 {"static", 0, st_C_typespec
},
1484 {"domain", C_STAR
, st_C_struct
},
1486 {"typedef", 0, st_C_typedef
},
1487 {"double", 0, st_C_typespec
},
1488 {"enum", 0, st_C_enum
},
1489 {"",}, {"",}, {"",}, {"",},
1490 {"int", 0, st_C_typespec
},
1492 {"float", 0, st_C_typespec
},
1493 {"",}, {"",}, {"",},
1494 {"struct", 0, st_C_struct
},
1495 {"",}, {"",}, {"",}, {"",},
1496 {"union", 0, st_C_struct
},
1498 {"short", 0, st_C_typespec
},
1500 {"unsigned", 0, st_C_typespec
},
1501 {"signed", 0, st_C_typespec
},
1504 if (len
<= MAX_WORD_LENGTH
&& len
>= MIN_WORD_LENGTH
)
1506 register int key
= hash (str
, len
);
1508 if (key
<= MAX_HASH_VALUE
&& key
>= MIN_HASH_VALUE
)
1510 register char *s
= wordlist
[key
].name
;
1512 if (*s
== *str
&& strneq (str
+ 1, s
+ 1, len
- 1))
1513 return &wordlist
[key
];
1521 C_symtype(str
, len
, c_ext
)
1526 register struct C_stab_entry
*se
= in_word_set(str
, len
);
1528 if (se
== NULL
|| (se
->c_ext
&& !(c_ext
& se
->c_ext
)))
1534 * C functions are recognized using a simple finite automaton.
1535 * funcdef is its state variable.
1539 fnone
, /* nothing seen */
1540 ftagseen
, /* function-like tag seen */
1541 fstartlist
, /* just after open parenthesis */
1542 finlist
, /* in parameter list */
1543 flistseen
, /* after parameter list */
1544 fignore
/* before open brace */
1550 * typedefs are recognized using a simple finite automaton.
1551 * typeddef is its state variable.
1555 tnone
, /* nothing seen */
1556 ttypedseen
, /* typedef keyword seen */
1557 tinbody
, /* inside typedef body */
1558 tend
, /* just before typedef tag */
1559 tignore
/* junk after typedef tag */
1565 * struct-like structures (enum, struct and union) are recognized
1566 * using another simple finite automaton. `structdef' is its state
1571 snone
, /* nothing seen yet */
1572 skeyseen
, /* struct-like keyword seen */
1573 stagseen
, /* struct-like tag seen */
1574 scolonseen
, /* colon seen after struct-like tag */
1575 sinbody
/* in struct body: recognize member func defs*/
1580 * When structdef is stagseen, scolonseen, or sinbody, structtag is the
1581 * struct tag, and structtype is the type of the preceding struct-like
1584 char *structtag
= "<uninited>";
1585 enum sym_type structtype
;
1588 * Yet another little state machine to deal with preprocessor lines.
1592 dnone
, /* nothing seen */
1593 dsharpseen
, /* '#' seen as first char on line */
1594 ddefineseen
, /* '#' and 'define' seen */
1595 dignorerest
/* ignore rest of line */
1600 * Set this to TRUE, and the next token considered is called a function.
1601 * Used only for GNU emacs's function-defining macros.
1603 logical next_token_is_func
;
1606 * TRUE in the rules part of a yacc file, FALSE outside (parse as C).
1612 * checks to see if the current token is at the start of a
1613 * function, or corresponds to a typedef, or is a struct/union/enum
1616 * *IS_FUNC gets TRUE iff the token is a function or macro with args.
1617 * C_EXT is which language we are looking at.
1619 * In the future we will need some way to adjust where the end of
1620 * the token is; for instance, implementing the C++ keyword
1621 * `operator' properly will adjust the end of the token to be after
1622 * whatever follows `operator'.
1629 * next_token_is_func IN OUT
1633 consider_token (str
, len
, c
, c_ext
, cblev
, is_func
)
1634 register char *str
; /* IN: token pointer */
1635 register int len
; /* IN: token length */
1636 register char c
; /* IN: first char after the token */
1637 int c_ext
; /* IN: C extensions mask */
1638 int cblev
; /* IN: curly brace level */
1639 logical
*is_func
; /* OUT: function found */
1641 enum sym_type toktype
= C_symtype (str
, len
, c_ext
);
1644 * Advance the definedef state machine.
1649 /* We're not on a preprocessor line. */
1652 if (toktype
== st_C_define
)
1654 definedef
= ddefineseen
;
1658 definedef
= dignorerest
;
1663 * Make a tag for any macro, unless it is a constant
1664 * and constantypedefs is FALSE.
1666 definedef
= dignorerest
;
1667 *is_func
= (c
== '(');
1668 if (!*is_func
&& !constantypedefs
)
1675 error ("internal error: definedef value.", 0);
1684 if (toktype
== st_C_typedef
)
1687 typdef
= ttypedseen
;
1703 /* Do not return here, so the structdef stuff has a chance. */
1717 * This structdef business is currently only invoked when cblev==0.
1718 * It should be recursively invoked whatever the curly brace level,
1719 * and a stack of states kept, to allow for definitions of structs
1722 * This structdef business is NOT invoked when we are ctags and the
1723 * file is plain C. This is because a struct tag may have the same
1724 * name as another tag, and this loses with ctags.
1726 * This if statement deals with the typdef state machine as
1727 * follows: if typdef==ttypedseen and token is struct/union/class/enum,
1728 * return FALSE. All the other code here is for the structdef
1735 if (typdef
== ttypedseen
1736 || (typedefs_and_cplusplus
&& cblev
== 0 && structdef
== snone
))
1738 structdef
= skeyseen
;
1739 structtype
= toktype
;
1743 if (structdef
== skeyseen
)
1745 /* Save the tag for struct/union/class, for functions that may be
1747 if (structtype
== st_C_struct
)
1748 structtag
= savenstr (str
, len
);
1750 structtag
= "<enum>";
1751 structdef
= stagseen
;
1755 /* Avoid entering funcdef stuff if typdef is going on. */
1756 if (typdef
!= tnone
)
1762 /* Detect GNU macros. */
1763 if (definedef
== dnone
)
1764 if (strneq (str
, "DEFUN", 5) /* Used in emacs */
1766 These are defined inside C functions
, so currently they
1768 || strneq (str
, "EXFUN", 5) /* Used in glibc */
1769 || strneq (str
, "DEFVAR_", 7) /* Used in emacs */
1771 || strneq (str
, "SYSCALL", 7) /* Used in glibc (mach) */
1772 || strneq (str
, "ENTRY", 5) /* Used in glibc */
1773 || strneq (str
, "PSEUDO", 6)) /* Used in glibc */
1776 next_token_is_func
= TRUE
;
1779 if (next_token_is_func
)
1781 next_token_is_func
= FALSE
;
1791 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1792 funcdef
= fnone
; /* should be useless */
1795 if (funcdef
== fnone
)
1808 * This routine finds functions, typedefs, #define's and
1809 * struct/union/enum definitions in C syntax and adds them
1820 #define current_lb_is_new (newndx == curndx)
1821 #define switch_line_buffers() (curndx = 1 - curndx)
1823 #define curlb (lbs[curndx].lb)
1824 #define othlb (lbs[1-curndx].lb)
1825 #define newlb (lbs[newndx].lb)
1826 #define curlinepos (lbs[curndx].linepos)
1827 #define othlinepos (lbs[1-curndx].linepos)
1828 #define newlinepos (lbs[newndx].linepos)
1830 #define CNL_SAVE_DEFINEDEF \
1832 curlinepos = charno; \
1834 charno += readline (&curlb, inf); \
1835 lp = curlb.buffer; \
1842 CNL_SAVE_DEFINEDEF; \
1846 token_saved = FALSE; \
1848 definedef = dnone; \
1851 #define make_tag_from_new_lb(isfun) pfnote (tok.str, isfun, tok.named, \
1852 newlb.buffer, tok.linelen, tok.lineno, newlinepos)
1853 #define make_tag_from_oth_lb(isfun) pfnote (tok.str, isfun, tok.named, \
1854 othlb.buffer, tok.linelen, tok.lineno, othlinepos)
1857 C_entries (c_ext
, inf
)
1858 int c_ext
; /* extension of C */
1859 FILE *inf
; /* input file */
1861 register char c
; /* latest char read; '\0' for end of line */
1862 register char *lp
; /* pointer one beyond the character `c' */
1863 int curndx
, newndx
; /* indices for current and new lb */
1864 TOKEN tok
; /* latest token read */
1865 register int tokoff
; /* offset in line of start of current token */
1866 register int toklen
; /* length of current token */
1867 int cblev
; /* current curly brace level */
1868 int parlev
; /* current parenthesis level */
1869 logical incomm
, inquote
, inchar
, quotednl
, midtoken
;
1871 logical token_saved
; /* token saved */
1872 TOKEN savetok
; /* token saved during preprocessor handling */
1874 curndx
= newndx
= 0;
1880 definedef
= dnone
; funcdef
= fnone
; typdef
= tnone
; structdef
= snone
;
1881 next_token_is_func
= yacc_rules
= token_saved
= FALSE
;
1882 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
1885 cplpl
= c_ext
& C_PLPL
;
1892 /* If we're at the end of the line, the next character is a
1893 '\0'; don't skip it, because it's the thing that tells us
1894 to read the next line. */
1915 /* Newlines inside comments do not end macro definitions in
1930 /* Newlines inside strings do not end macro definitions
1931 in traditional cpp, even though compilers don't
1932 usually accept them. */
1943 /* Hmmm, something went wrong. */
1957 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1962 if (funcdef
!= finlist
&& funcdef
!= fignore
)
1972 else if (cplpl
&& *lp
== '/')
1980 if ((c_ext
& YACC
) && *lp
== '%')
1982 /* entering or exiting rules section in yacc file */
1984 definedef
= dnone
; funcdef
= fnone
;
1985 typdef
= tnone
; structdef
= snone
;
1986 next_token_is_func
= FALSE
;
1987 midtoken
= inquote
= inchar
= incomm
= quotednl
= FALSE
;
1989 yacc_rules
= !yacc_rules
;
1995 if (definedef
== dnone
)
1998 logical cpptoken
= TRUE
;
2000 /* Look back on this line. If all blanks, or nonblanks
2001 followed by an end of comment, this is a preprocessor
2003 for (cp
= newlb
.buffer
; cp
< lp
-1; cp
++)
2006 if (*cp
== '*' && *(cp
+1) == '/')
2015 definedef
= dsharpseen
;
2016 } /* if (definedef == dnone) */
2022 /* Consider token only if some complicated conditions are satisfied. */
2023 if ((definedef
!= dnone
2024 || (cblev
== 0 && structdef
!= scolonseen
)
2025 || (cblev
== 1 && cplpl
&& structdef
== sinbody
))
2026 && typdef
!= tignore
2027 && definedef
!= dignorerest
2028 && funcdef
!= finlist
)
2034 if (cplpl
&& c
== ':' && *lp
== ':' && begtoken(*(lp
+ 1)))
2037 * This handles :: in the middle, but not at the
2038 * beginning of an identifier.
2045 logical is_func
= FALSE
;
2048 || consider_token (newlb
.buffer
+ tokoff
, toklen
,
2049 c
, c_ext
, cblev
, &is_func
))
2051 if (structdef
== sinbody
2052 && definedef
== dnone
2054 /* function defined in C++ class body */
2056 char *cp
= newlb
.buffer
+ tokoff
+ toklen
;
2059 tok
.str
= concat (structtag
, "::",
2060 newlb
.buffer
+ tokoff
);
2066 tok
.str
= savenstr (newlb
.buffer
+tokoff
, toklen
);
2067 if (structdef
== stagseen
2070 && definedef
== dignorerest
)) /* macro */
2075 tok
.lineno
= lineno
;
2076 tok
.linelen
= tokoff
+ toklen
+ 1;
2078 if (definedef
== dnone
2079 && (funcdef
== ftagseen
2080 || structdef
== stagseen
2083 if (current_lb_is_new
)
2084 switch_line_buffers ();
2087 make_tag_from_new_lb (is_func
);
2091 } /* if (endtoken (c)) */
2092 else if (intoken (c
))
2097 } /* if (midtoken) */
2098 else if (begtoken (c
))
2109 make_tag_from_oth_lb (TRUE
);
2116 if (structdef
== stagseen
)
2123 if (!yacc_rules
|| lp
== newlb
.buffer
+ 1)
2125 tokoff
= lp
- 1 - newlb
.buffer
;
2130 } /* if (begtoken) */
2131 } /* if must look at token */
2134 /* Detect end of line, colon, comma, semicolon and various braces
2135 after having handled a token.*/
2139 if (definedef
!= dnone
)
2141 if (structdef
== stagseen
)
2142 structdef
= scolonseen
;
2149 make_tag_from_oth_lb (FALSE
);
2159 if (definedef
!= dnone
)
2165 make_tag_from_oth_lb (FALSE
);
2170 if (funcdef
!= fignore
)
2172 if (structdef
== stagseen
)
2176 if (definedef
!= dnone
)
2178 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2180 if (structdef
== stagseen
)
2184 if (definedef
!= dnone
)
2186 if (cblev
== 0 && typdef
== tend
)
2189 make_tag_from_oth_lb (FALSE
);
2192 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2194 if (structdef
== stagseen
)
2198 if (definedef
!= dnone
)
2207 /* Make sure that the next char is not a '*'.
2208 This handles constructs like:
2209 typedef void OperatorFun (int fun); */
2213 make_tag_from_oth_lb (FALSE
);
2216 } /* switch (typdef) */
2219 funcdef
= fstartlist
;
2228 if (definedef
!= dnone
)
2236 funcdef
= flistseen
;
2239 if (cblev
== 0 && typdef
== tend
)
2242 make_tag_from_oth_lb (FALSE
);
2245 else if (parlev
< 0) /* can happen due to ill-conceived #if's. */
2249 if (definedef
!= dnone
)
2251 if (typdef
== ttypedseen
)
2255 case skeyseen
: /* unnamed struct */
2256 structtag
= "_anonymous_";
2257 structdef
= sinbody
;
2260 case scolonseen
: /* named struct */
2261 structdef
= sinbody
;
2262 make_tag_from_oth_lb (FALSE
);
2268 make_tag_from_oth_lb (TRUE
);
2274 /* Neutralize `extern "C" {' grot and look inside structs. */
2275 if (cblev
== 0 && structdef
== snone
&& typdef
== tnone
)
2281 if (definedef
!= dnone
)
2283 if (funcdef
== fstartlist
)
2284 funcdef
= fnone
; /* avoid tagging `foo' in `foo (*bar()) ()' */
2287 if (definedef
!= dnone
)
2289 if (!noindentypedefs
&& lp
== newlb
.buffer
+ 1)
2291 cblev
= 0; /* reset curly brace level if first column */
2292 parlev
= 0; /* also reset paren level, just in case... */
2298 if (typdef
== tinbody
)
2300 #if FALSE /* too risky */
2301 if (structdef
== sinbody
)
2305 structtag
= "<error>";
2309 case '#': case '+': case '-': case '~': case '&': case '%': case '/':
2310 case '|': case '^': case '!': case '<': case '>': case '.': case '?':
2311 if (definedef
!= dnone
)
2313 /* These surely cannot follow a function tag. */
2314 if (funcdef
!= finlist
&& funcdef
!= fignore
)
2318 /* If a macro spans multiple lines don't reset its state. */
2326 } /* while not eof */
2330 * Process either a C++ file or a C file depending on the setting
2334 default_C_entries (inf
)
2337 C_entries (cplusplus
? C_PLPL
: 0, inf
);
2340 /* Always do C++. */
2342 Cplusplus_entries (inf
)
2345 C_entries (C_PLPL
, inf
);
2353 C_entries (C_STAR
, inf
);
2356 /* Always do Yacc. */
2361 C_entries (YACC
, inf
);
2364 /* Fortran parsing */
2372 register int len
= 0;
2374 while (*cp
&& (*cp
| ' ') == (dbp
[len
] | ' '))
2387 while (isspace (*dbp
))
2392 while (isspace (*dbp
))
2396 if (!isdigit (*dbp
))
2398 --dbp
; /* force failure */
2403 while (isdigit (*dbp
));
2412 while (isspace (*dbp
))
2417 linecharno
= charno
;
2418 charno
+= readline (&lb
, inf
);
2423 while (isspace (*dbp
))
2432 && (isalpha (*cp
) || isdigit (*cp
) || (*cp
== '_') || (*cp
== '$')));
2435 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
, FALSE
, lb
.buffer
,
2436 cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2440 Fortran_functions (inf
)
2449 linecharno
= charno
;
2450 charno
+= readline (&lb
, inf
);
2453 dbp
++; /* Ratfor escape to fortran */
2454 while (isspace (*dbp
))
2461 if (tail ("integer"))
2469 if (tail ("logical"))
2473 if (tail ("complex") || tail ("character"))
2477 if (tail ("double"))
2479 while (isspace (*dbp
))
2483 if (tail ("precision"))
2489 while (isspace (*dbp
))
2496 if (tail ("function"))
2500 if (tail ("subroutine"))
2508 if (tail ("program"))
2513 if (tail ("procedure"))
2521 * Bob Weiner, Motorola Inc., 4/3/94
2522 * Unix and microcontroller assembly tag handling
2523 * look for '^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]'
2537 linecharno
= charno
;
2538 charno
+= readline (&lb
, inf
);
2541 /* If first char is alphabetic or one of [_.$], test for colon
2542 following identifier. */
2543 if (isalpha (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2545 /* Read past label. */
2547 while (isalnum (*cp
) || *cp
== '_' || *cp
== '.' || *cp
== '$')
2549 if (*cp
== ':' || isspace (*cp
))
2551 /* Found end of label, so copy it and add it to the table. */
2552 pfnote (savenstr (lb
.buffer
, cp
-lb
.buffer
), TRUE
, FALSE
,
2553 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2559 /* Added by Mosur Mohan, 4/22/88 */
2560 /* Pascal parsing */
2562 #define GET_NEW_LINE \
2564 linecharno = charno; lineno++; \
2565 charno += 1 + readline (&lb, inf); \
2570 * Locates tags for procedures & functions. Doesn't do any type- or
2571 * var-definitions. It does look for the keyword "extern" or
2572 * "forward" immediately following the procedure statement; if found,
2573 * the tag is skipped.
2576 Pascal_functions (inf
)
2579 struct linebuffer tline
; /* mostly copied from C_entries */
2585 logical
/* each of these flags is TRUE iff: */
2586 incomment
, /* point is inside a comment */
2587 inquote
, /* point is inside '..' string */
2588 get_tagname
, /* point is after PROCEDURE/FUNCTION */
2589 /* keyword, so next item = potential tag */
2590 found_tag
, /* point is after a potential tag */
2591 inparms
, /* point is within parameter-list */
2592 verify_tag
; /* point has passed the parm-list, so the */
2593 /* next token will determine whether */
2594 /* this is a FORWARD/EXTERN to be */
2595 /* ignored, or whether it is a real tag */
2601 initbuffer (&tline
);
2603 incomment
= inquote
= FALSE
;
2604 found_tag
= FALSE
; /* have a proc name; check if extern */
2605 get_tagname
= FALSE
; /* have found "procedure" keyword */
2606 inparms
= FALSE
; /* found '(' after "proc" */
2607 verify_tag
= FALSE
; /* check if "extern" is ahead */
2609 /* long main loop to get next char */
2613 if (c
== '\0') /* if end of line */
2618 if (!((found_tag
&& verify_tag
) ||
2620 c
= *dbp
++; /* only if don't need *dbp pointing */
2621 /* to the beginning of the name of */
2622 /* the procedure or function */
2626 if (c
== '}') /* within { - } comments */
2628 else if (c
== '*' && dbp
[1] == ')') /* within (* - *) comments */
2645 inquote
= TRUE
; /* found first quote */
2647 case '{': /* found open-{-comment */
2651 if (*dbp
== '*') /* found open-(*-comment */
2656 else if (found_tag
) /* found '(' after tag, i.e., parm-list */
2659 case ')': /* end of parms list */
2664 if ((found_tag
) && (!inparms
)) /* end of proc or fn stmt */
2671 if ((found_tag
) && (verify_tag
) && (*dbp
!= ' '))
2673 /* check if this is an "extern" declaration */
2676 if ((*dbp
== 'e') || (*dbp
== 'E'))
2678 if (tail ("extern")) /* superfluous, really! */
2684 else if ((*dbp
== 'f') || (*dbp
== 'F'))
2686 if (tail ("forward")) /* check for forward reference */
2692 if ((found_tag
) && (verify_tag
)) /* not external proc, so make tag */
2696 pfnote (nambuf
, TRUE
, FALSE
, tline
.buffer
,
2697 cp
- tline
.buffer
+ 1, save_lineno
, save_lcno
);
2701 if (get_tagname
) /* grab name of proc or fn */
2706 /* save all values for later tagging */
2707 tline
.size
= lb
.size
;
2708 strcpy (tline
.buffer
, lb
.buffer
);
2709 save_lineno
= lineno
;
2710 save_lcno
= linecharno
;
2712 /* grab block name */
2713 for (cp
= dbp
+ 1; *cp
&& (!endtoken (*cp
)); cp
++)
2715 nambuf
= savenstr (dbp
, cp
-dbp
);
2716 dbp
= cp
; /* restore dbp to e-o-token */
2717 get_tagname
= FALSE
;
2721 /* and proceed to check for "extern" */
2723 else if (!incomment
&& !inquote
&& !found_tag
)
2725 /* check for proc/fn keywords */
2729 if (tail ("rocedure")) /* c = 'p', dbp has advanced */
2733 if (tail ("unction"))
2738 } /* while not eof */
2742 * lisp tag functions
2743 * look for (def or (DEF, quote or QUOTE
2747 register char *strp
;
2749 return ((strp
[1] == 'd' || strp
[1] == 'D')
2750 && (strp
[2] == 'e' || strp
[2] == 'E')
2751 && (strp
[3] == 'f' || strp
[3] == 'F'));
2756 register char *strp
;
2758 return ((*(++strp
) == 'q' || *strp
== 'Q')
2759 && (*(++strp
) == 'u' || *strp
== 'U')
2760 && (*(++strp
) == 'o' || *strp
== 'O')
2761 && (*(++strp
) == 't' || *strp
== 'T')
2762 && (*(++strp
) == 'e' || *strp
== 'E')
2763 && isspace(*(++strp
)));
2771 if (*dbp
== '\'') /* Skip prefix quote */
2773 else if (*dbp
== '(' && L_isquote (dbp
)) /* Skip "(quote " */
2776 while (isspace(*dbp
))
2779 for (cp
= dbp
/*+1*/;
2780 *cp
&& *cp
!= '(' && *cp
!= ' ' && *cp
!= ')';
2786 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
, FALSE
, lb
.buffer
,
2787 cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2791 Lisp_functions (inf
)
2800 linecharno
= charno
;
2801 charno
+= readline (&lb
, inf
);
2807 while (!isspace (*dbp
))
2809 while (isspace (*dbp
))
2815 /* Check for (foo::defmumble name-defined ... */
2818 while (*dbp
&& !isspace (*dbp
)
2819 && *dbp
!= ':' && *dbp
!= '(' && *dbp
!= ')');
2824 while (*dbp
== ':');
2826 if (L_isdef (dbp
- 1))
2828 while (!isspace (*dbp
))
2830 while (isspace (*dbp
))
2841 * Scheme tag functions
2842 * look for (def... xyzzy
2843 * look for (def... (xyzzy
2844 * look for (def ... ((...(xyzzy ....
2845 * look for (set! xyzzy
2851 Scheme_functions (inf
)
2860 linecharno
= charno
;
2861 charno
+= readline (&lb
, inf
);
2863 if (dbp
[0] == '(' &&
2864 (dbp
[1] == 'D' || dbp
[1] == 'd') &&
2865 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
2866 (dbp
[3] == 'F' || dbp
[3] == 'f'))
2868 while (!isspace (*dbp
))
2870 /* Skip over open parens and white space */
2871 while (*dbp
&& (isspace (*dbp
) || *dbp
== '('))
2875 if (dbp
[0] == '(' &&
2876 (dbp
[1] == 'S' || dbp
[1] == 's') &&
2877 (dbp
[2] == 'E' || dbp
[2] == 'e') &&
2878 (dbp
[3] == 'T' || dbp
[3] == 't') &&
2879 (dbp
[4] == '!' || dbp
[4] == '!') &&
2882 while (!isspace (*dbp
))
2884 /* Skip over white space */
2885 while (isspace (*dbp
))
2899 /* Go till you get to white space or a syntactic break */
2901 *cp
&& *cp
!= '(' && *cp
!= ')' && !isspace (*cp
);
2904 pfnote (savenstr (dbp
, cp
-dbp
), TRUE
, FALSE
,
2905 lb
.buffer
, cp
- lb
.buffer
+ 1, lineno
, linecharno
);
2908 /* Find tags in TeX and LaTeX input files. */
2910 /* TEX_toktab is a table of TeX control sequences that define tags.
2911 Each TEX_tabent records one such control sequence.
2912 CONVERT THIS TO USE THE Stab TYPE!! */
2919 struct TEX_tabent
*TEX_toktab
= NULL
; /* Table with tag tokens */
2921 /* Default set of control sequences to put into TEX_toktab.
2922 The value of environment var TEXTAGS is prepended to this. */
2924 char *TEX_defenv
= "\
2925 :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem:typeout";
2928 struct TEX_tabent
*TEX_decode_env ();
2932 char TEX_esc
= '\\';
2933 char TEX_opgrp
= '{';
2934 char TEX_clgrp
= '}';
2937 * TeX/LaTeX scanning loop.
2948 /* Select either \ or ! as escape character. */
2951 /* Initialize token table once from environment. */
2953 TEX_toktab
= TEX_decode_env ("TEXTAGS", TEX_defenv
);
2956 { /* Scan each line in file */
2958 linecharno
= charno
;
2959 charno
+= readline (&lb
, inf
);
2962 while (dbp
= etags_strchr (dbp
, TEX_esc
)) /* Look at each esc in line */
2968 linecharno
+= dbp
- lasthit
;
2970 i
= TEX_Token (lasthit
);
2973 TEX_getit (lasthit
, TEX_toktab
[i
].len
);
2974 break; /* We only save a line once */
2980 #define TEX_LESC '\\'
2981 #define TEX_SESC '!'
2984 /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
2985 chars accordingly. */
2992 while ((c
= getc (inf
)) != EOF
)
2994 /* Skip to next line if we hit the TeX comment char. */
2998 else if (c
== TEX_LESC
|| c
== TEX_SESC
)
3017 /* Read environment and prepend it to the default string.
3018 Build token table. */
3020 TEX_decode_env (evarname
, defenv
)
3024 register char *env
, *p
;
3026 struct TEX_tabent
*tab
;
3029 /* Append default string to environment. */
3030 env
= getenv (evarname
);
3034 env
= concat (env
, defenv
, "");
3036 /* Allocate a token table */
3037 for (size
= 1, p
= env
; p
;)
3038 if ((p
= etags_strchr (p
, ':')) && *(++p
))
3040 /* Add 1 to leave room for null terminator. */
3041 tab
= xnew (size
+ 1, struct TEX_tabent
);
3043 /* Unpack environment string into token table. Be careful about */
3044 /* zero-length strings (leading ':', "::" and trailing ':') */
3047 p
= etags_strchr (env
, ':');
3048 if (!p
) /* End of environment string. */
3049 p
= env
+ strlen (env
);
3051 { /* Only non-zero strings. */
3052 tab
[i
].name
= savenstr (env
, p
- env
);
3053 tab
[i
].len
= strlen (tab
[i
].name
);
3060 tab
[i
].name
= NULL
; /* Mark end of table. */
3068 /* Record a tag defined by a TeX command of length LEN and starting at NAME.
3069 The name being defined actually starts at (NAME + LEN + 1).
3070 But we seem to include the TeX command in the tag name. */
3072 TEX_getit (name
, len
)
3076 char *p
= name
+ len
;
3081 /* Let tag name extend to next group close (or end of line) */
3082 while (*p
&& *p
!= TEX_clgrp
)
3084 pfnote (savenstr (name
, p
-name
), TRUE
, FALSE
, lb
.buffer
,
3085 strlen (lb
.buffer
), lineno
, linecharno
);
3088 /* If the text at CP matches one of the tag-defining TeX command names,
3089 return the pointer to the first occurrence of that command in TEX_toktab.
3090 Otherwise return -1.
3091 Keep the capital `T' in `Token' for dumb truncating compilers
3092 (this distinguishes it from `TEX_toktab' */
3099 for (i
= 0; TEX_toktab
[i
].len
> 0; i
++)
3100 if (strneq (TEX_toktab
[i
].name
, cp
, TEX_toktab
[i
].len
))
3105 /* Support for Prolog. */
3107 /* Whole head (not only functor, but also arguments)
3108 is gotten in compound term. */
3121 if (s
[0] == '\0') /* syntax error. */
3123 else if (insquote
&& s
[0] == '\'' && s
[1] == '\'')
3125 else if (s
[0] == '\'')
3127 insquote
= !insquote
;
3130 else if (!insquote
&& s
[0] == '(')
3135 else if (!insquote
&& s
[0] == ')')
3141 else if (npar
< 0) /* syntax error. */
3144 else if (!insquote
&& s
[0] == '.'
3145 && (isspace (s
[1]) || s
[1] == '\0'))
3147 if (npar
!= 0) /* syntax error. */
3155 pfnote (savenstr (save_s
, s
-save_s
), TRUE
, FALSE
,
3156 save_s
, s
-save_s
, lineno
, linecharno
);
3159 /* It is assumed that prolog predicate starts from column 0. */
3161 Prolog_functions (inf
)
3164 void skip_comment (), prolog_getit ();
3166 lineno
= linecharno
= charno
= 0;
3170 linecharno
+= charno
;
3171 charno
= readline (&lb
, inf
) + 1; /* 1 for newline. */
3173 if (isspace (dbp
[0])) /* not predicate header. */
3175 else if (dbp
[0] == '%') /* comment. */
3177 else if (dbp
[0] == '/' && dbp
[1] == '*') /* comment. */
3178 skip_comment (&lb
, inf
, &lineno
, &linecharno
);
3185 skip_comment (plb
, inf
, plineno
, plinecharno
)
3186 struct linebuffer
*plb
;
3188 int *plineno
; /* result */
3189 long *plinecharno
; /* result */
3195 for (cp
= plb
->buffer
; *cp
!= '\0'; cp
++)
3196 if (cp
[0] == '*' && cp
[1] == '/')
3199 *plinecharno
+= readline (plb
, inf
) + 1; /* 1 for newline. */
3204 #ifdef ETAGS_REGEXPS
3205 /* Take a string like "/blah/" and turn it into "blah", making sure
3206 that the first and last characters are the same, and handling
3207 quoted separator characters. Actually, stops on the occurence of
3208 an unquoted separator. Also turns "\t" into a Tab character.
3209 Returns pointer to terminating separator. Works in place. Null
3210 terminates name string. */
3212 scan_separators (name
)
3216 char *copyto
= name
;
3217 logical quoted
= FALSE
;
3219 for (++name
; *name
!= '\0'; ++name
)
3225 else if (*name
== sep
)
3229 /* Something else is quoted, so preserve the quote. */
3235 else if (*name
== '\\')
3237 else if (*name
== sep
)
3243 /* Terminate copied string. */
3248 /* Turn a name, which is an ed-style (but Emacs syntax) regular
3249 expression, into a real regular expression by compiling it. */
3251 add_regex (regexp_pattern
)
3252 char *regexp_pattern
;
3256 struct re_pattern_buffer
*patbuf
;
3258 if (regexp_pattern
== NULL
)
3260 /* Remove existing regexps. */
3266 if (regexp_pattern
[0] == '\0')
3268 error ("missing regexp", 0);
3271 if (regexp_pattern
[strlen(regexp_pattern
)-1] != regexp_pattern
[0])
3273 error ("%s: unterminated regexp", regexp_pattern
);
3276 name
= scan_separators (regexp_pattern
);
3277 if (regexp_pattern
[0] == '\0')
3279 error ("null regexp", 0);
3282 (void) scan_separators (name
);
3284 patbuf
= xnew (1, struct re_pattern_buffer
);
3285 patbuf
->translate
= NULL
;
3286 patbuf
->fastmap
= NULL
;
3287 patbuf
->buffer
= NULL
;
3288 patbuf
->allocated
= 0;
3290 err
= re_compile_pattern (regexp_pattern
, strlen (regexp_pattern
), patbuf
);
3293 error ("%s while compiling pattern", err
);
3298 if (num_patterns
== 1)
3299 patterns
= xnew (1, struct pattern
);
3301 patterns
= ((struct pattern
*)
3303 (num_patterns
* sizeof (struct pattern
))));
3304 patterns
[num_patterns
- 1].pattern
= patbuf
;
3305 patterns
[num_patterns
- 1].name_pattern
= savestr (name
);
3306 patterns
[num_patterns
- 1].error_signaled
= FALSE
;
3310 * Do the subtitutions indicated by the regular expression and
3314 substitute (in
, out
, regs
)
3316 struct re_registers
*regs
;
3318 char *result
= NULL
, *t
;
3321 /* Pass 1: figure out how much size to allocate. */
3322 for (t
= out
; *t
; ++t
)
3329 fprintf (stderr
, "%s: pattern subtitution ends prematurely\n",
3336 size
+= regs
->end
[dig
] - regs
->start
[dig
];
3341 /* Allocate space and do the substitutions. */
3342 result
= xnew (size
+ 1, char);
3351 /* Using "dig2" satisfies my debugger. Bleah. */
3352 int dig2
= *out
- '0';
3353 strncpy (result
+ size
, in
+ regs
->start
[dig2
],
3354 regs
->end
[dig2
] - regs
->start
[dig2
]);
3355 size
+= regs
->end
[dig2
] - regs
->start
[dig2
];
3362 result
[size
++] = '\t';
3368 result
[size
++] = *out
;
3374 result
[size
++] = *out
;
3376 result
[size
] = '\0';
3381 #endif /* ETAGS_REGEXPS */
3382 /* Initialize a linebuffer for use */
3384 initbuffer (linebuffer
)
3385 struct linebuffer
*linebuffer
;
3387 linebuffer
->size
= 200;
3388 linebuffer
->buffer
= xnew (200, char);
3392 * Read a line of text from `stream' into `linebuffer'.
3393 * Return the number of characters read from `stream',
3394 * which is the length of the line including the newline, if any.
3397 readline_internal (linebuffer
, stream
)
3398 struct linebuffer
*linebuffer
;
3399 register FILE *stream
;
3401 char *buffer
= linebuffer
->buffer
;
3402 register char *p
= linebuffer
->buffer
;
3403 register char *pend
;
3406 pend
= p
+ linebuffer
->size
; /* Separate to avoid 386/IX compiler bug. */
3410 register int c
= getc (stream
);
3413 linebuffer
->size
*= 2;
3414 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
3415 p
+= buffer
- linebuffer
->buffer
;
3416 pend
= buffer
+ linebuffer
->size
;
3417 linebuffer
->buffer
= buffer
;
3426 if (p
[-1] == '\r' && p
> buffer
)
3441 return p
- buffer
+ chars_deleted
;
3445 * Like readline_internal, above, but try to match the input
3446 * line against any existing regular expressions.
3449 readline (linebuffer
, stream
)
3450 struct linebuffer
*linebuffer
;
3453 /* Read new line. */
3455 long result
= readline_internal (linebuffer
, stream
);
3457 #ifdef ETAGS_REGEXPS
3458 /* Match against all listed patterns. */
3459 for (i
= 0; i
< num_patterns
; ++i
)
3461 int match
= re_match (patterns
[i
].pattern
, linebuffer
->buffer
,
3462 (int)result
, 0, &patterns
[i
].regs
);
3467 if (!patterns
[i
].error_signaled
)
3469 error ("error while matching pattern %d", i
);
3470 patterns
[i
].error_signaled
= TRUE
;
3477 /* Match occurred. Construct a tag. */
3478 if (patterns
[i
].name_pattern
[0] != '\0')
3480 /* Make a named tag. */
3481 char *name
= substitute (linebuffer
->buffer
,
3482 patterns
[i
].name_pattern
,
3485 pfnote (name
, TRUE
, TRUE
, linebuffer
->buffer
,
3486 match
, lineno
, linecharno
);
3490 /* Make an unnamed tag. */
3491 pfnote (NULL
, TRUE
, FALSE
, linebuffer
->buffer
,
3492 match
, lineno
, linecharno
);
3497 #endif /* ETAGS_REGEXPS */
3503 * Read a file, but do no processing. This is used to do regexp
3504 * matching on files that have no language defined.
3507 just_read_file (inf
)
3513 linecharno
= charno
;
3514 charno
+= readline (&lb
, inf
) + 1;
3520 * Return a pointer to a space of size strlen(cp)+1 allocated
3521 * with xnew where the string CP has been copied.
3527 return savenstr (cp
, strlen (cp
));
3531 * Return a pointer to a space of size LEN+1 allocated with xnew where
3532 * the string CP has been copied for at most the first LEN characters.
3541 dp
= xnew (len
+ 1, char);
3542 strncpy (dp
, cp
, len
);
3548 * Return the ptr in sp at which the character c last
3549 * appears; NULL if not found
3551 * Identical to System V strrchr, included for portability.
3554 etags_strrchr (sp
, c
)
3555 register char *sp
, c
;
3570 * Return the ptr in sp at which the character c first
3571 * appears; NULL if not found
3573 * Identical to System V strchr, included for portability.
3576 etags_strchr (sp
, c
)
3577 register char *sp
, c
;
3587 /* Print error message and exit. */
3604 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
3609 fprintf (stderr
, "%s: ", progname
);
3610 fprintf (stderr
, s1
, s2
);
3611 fprintf (stderr
, "\n");
3614 /* Return a newly-allocated string whose contents
3615 concatenate those of s1, s2, s3. */
3620 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
3621 char *result
= xnew (len1
+ len2
+ len3
+ 1, char);
3623 strcpy (result
, s1
);
3624 strcpy (result
+ len1
, s2
);
3625 strcpy (result
+ len1
+ len2
, s3
);
3626 result
[len1
+ len2
+ len3
] = '\0';
3631 /* Does the same work as the system V getcwd, but does not need to
3632 guess buffer size in advance. */
3637 char *p
, path
[MAXPATHLEN
+ 1]; /* Fixed size is safe on MSDOS. */
3645 *p
++ = tolower (*p
);
3647 return strdup (path
);
3649 #elif HAVE_GETCWD /* not DOS_NT */
3652 char *path
= xnew (bufsize
, char);
3654 while (getcwd (path
, bufsize
) == NULL
)
3656 if (errno
!= ERANGE
)
3659 path
= xnew (bufsize
, char);
3664 #else /* not DOS_NT and not HAVE_GETCWD */
3666 struct linebuffer path
;
3670 pipe
= (FILE *) popen ("pwd 2>/dev/null", "r");
3671 if (pipe
== NULL
|| readline_internal (&path
, pipe
) == 0)
3677 #endif /* not DOS_NT and not HAVE_GETCWD */
3679 /* Return a newly allocated string containing the filename
3680 of FILE relative to the absolute directory DIR (which
3681 should end with a slash). */
3683 relative_filename (file
, dir
)
3686 char *fp
, *dp
, *res
;
3688 /* Find the common root of file and dir. */
3689 fp
= absolute_filename (file
, cwd
);
3691 while (*fp
++ == *dp
++)
3700 /* Build a sequence of "../" strings for the resulting relative filename. */
3701 for (dp
= etags_strchr (dp
+ 1, '/'), res
= "";
3703 dp
= etags_strchr (dp
+ 1, '/'))
3705 res
= concat (res
, "../", "");
3708 /* Add the filename relative to the common root of file and dir. */
3709 res
= concat (res
, fp
+ 1, "");
3711 return res
; /* temporary stub */
3714 /* Return a newly allocated string containing the
3715 absolute filename of FILE given CWD (which should
3716 end with a slash). */
3718 absolute_filename (file
, cwd
)
3721 char *slashp
, *cp
, *res
;
3724 res
= concat (file
, "", "");
3726 res
= concat (cwd
, file
, "");
3728 /* Delete the "/dirname/.." and "/." substrings. */
3729 slashp
= etags_strchr (res
, '/');
3730 while (slashp
!= NULL
&& slashp
[0] != '\0')
3732 if (slashp
[1] == '.')
3734 if (slashp
[2] == '.'
3735 && (slashp
[3] == '/' || slashp
[3] == '\0'))
3740 while (cp
>= res
&& *cp
!= '/');
3743 strcpy (cp
, slashp
+ 3);
3745 else /* else (cp == res) */
3747 if (slashp
[3] != '\0')
3748 strcpy (cp
, slashp
+ 4);
3755 else if (slashp
[2] == '/' || slashp
[2] == '\0')
3757 strcpy (slashp
, slashp
+ 2);
3762 slashp
= etags_strchr (slashp
+ 1, '/');
3768 /* Return a newly allocated string containing the absolute
3769 filename of dir where FILE resides given CWD (which should
3770 end with a slash). */
3772 absolute_dirname (file
, cwd
)
3778 slashp
= etags_strrchr (file
, '/');
3783 res
= absolute_filename (file
, cwd
);
3789 /* Like malloc but get fatal error if memory is exhausted. */
3794 char *result
= (char *) malloc (size
);
3796 fatal ("virtual memory exhausted", 0);
3801 xrealloc (ptr
, size
)
3805 char *result
= (char *) realloc (ptr
, size
);
3807 fatal ("virtual memory exhausted");