2 * $Id: main.c 536 2007-06-02 06:09:00Z elliotth $
4 * Copyright (c) 1996-2003, Darren Hiebert
6 * Author: Darren Hiebert <dhiebert@users.sourceforge.net>
7 * http://ctags.sourceforge.net
9 * This source code is released for free distribution under the terms of the
10 * GNU General Public License. It is provided on an as-is basis and no
11 * responsibility is accepted for its failure to perform as expected.
13 * This is a reimplementation of the ctags (1) program. It is an attempt to
14 * provide a fully featured ctags program which is free of the limitations
15 * which most (all?) others are subject to.
17 * This module contains the start-up code and routines to determine the list
18 * of files to parsed for tags.
24 #include "general.h" /* must always come first */
28 /* To provide timings features if available.
36 # ifdef HAVE_SYS_TIMES_H
37 # include <sys/times.h>
42 /* To provide directory searching for recursion feature.
45 # include <dos/dosasl.h> /* for struct AnchorPath */
46 # include <clib/dos_protos.h> /* function prototypes */
47 # define ANCHOR_BUF_SIZE 512
48 # define ANCHOR_SIZE (sizeof (struct AnchorPath) + ANCHOR_BUF_SIZE)
50 extern struct DosLibrary
*DOSBase
;
51 # include <pragmas/dos_pragmas.h>
57 # define boolean BORLAND_boolean
59 # ifdef HAVE_SYS_TYPES_H
60 # include <sys/types.h> /* required by dirent.h */
62 # include <dirent.h> /* to declare opendir() */
66 # include <direct.h> /* to _getcwd() */
69 # include <dos.h> /* to declare FA_DIREC */
72 # include <dir.h> /* to declare findfirst() and findnext */
75 # include <io.h> /* to declare _findfirst() */
89 #define plural(value) (((unsigned long)(value) == 1L) ? "" : "s")
94 static struct { long files
, lines
, bytes
; } Totals
= { 0, 0, 0 };
98 static const char *VERsion
= "$VER: "PROGRAM_NAME
" "PROGRAM_VERSION
" "
108 * FUNCTION PROTOTYPES
110 static boolean
createTagsForEntry (const char *const entryName
);
113 * FUNCTION DEFINITIONS
116 extern void addTotals (
117 const unsigned int files
, const long unsigned int lines
,
118 const long unsigned int bytes
)
120 Totals
.files
+= files
;
121 Totals
.lines
+= lines
;
122 Totals
.bytes
+= bytes
;
125 extern boolean
isDestinationStdout (void)
127 boolean toStdout
= FALSE
;
129 if (Option
.xref
|| Option
.filter
||
130 (Option
.tagFileName
!= NULL
&& (strcmp (Option
.tagFileName
, "-") == 0
132 || strcmp (Option
.tagFileName
, "sys$output") == 0
134 || strcmp (Option
.tagFileName
, "/dev/stdout") == 0
141 #if defined (HAVE_OPENDIR)
142 static boolean
recurseUsingOpendir (const char *const dirName
)
144 boolean resize
= FALSE
;
145 DIR *const dir
= opendir (dirName
);
147 error (WARNING
| PERROR
, "cannot recurse into directory \"%s\"", dirName
);
150 struct dirent
*entry
;
151 while ((entry
= readdir (dir
)) != NULL
)
153 if (strcmp (entry
->d_name
, ".") != 0 &&
154 strcmp (entry
->d_name
, "..") != 0)
157 if (strcmp (dirName
, ".") == 0)
158 filePath
= vStringNewInit (entry
->d_name
);
160 filePath
= combinePathAndFile (dirName
, entry
->d_name
);
161 resize
|= createTagsForEntry (vStringValue (filePath
));
162 vStringDelete (filePath
);
170 #elif defined (HAVE_FINDFIRST) || defined (HAVE__FINDFIRST)
172 static boolean
createTagsForWildcardEntry (
173 const char *const pattern
, const size_t dirLength
,
174 const char *const entryName
)
176 boolean resize
= FALSE
;
177 /* we must not recurse into the directories "." or ".." */
178 if (strcmp (entryName
, ".") != 0 && strcmp (entryName
, "..") != 0)
180 vString
*const filePath
= vStringNew ();
181 vStringNCopyS (filePath
, pattern
, dirLength
);
182 vStringCatS (filePath
, entryName
);
183 resize
= createTagsForEntry (vStringValue (filePath
));
184 vStringDelete (filePath
);
189 static boolean
createTagsForWildcardUsingFindfirst (const char *const pattern
)
191 boolean resize
= FALSE
;
192 const size_t dirLength
= baseFilename (pattern
) - pattern
;
193 #if defined (HAVE_FINDFIRST)
194 struct ffblk fileInfo
;
195 int result
= findfirst (pattern
, &fileInfo
, FA_DIREC
);
198 const char *const entry
= (const char *) fileInfo
.ff_name
;
199 resize
|= createTagsForWildcardEntry (pattern
, dirLength
, entry
);
200 result
= findnext (&fileInfo
);
202 #elif defined (HAVE__FINDFIRST)
203 struct _finddata_t fileInfo
;
204 findfirst_t hFile
= _findfirst (pattern
, &fileInfo
);
209 const char *const entry
= (const char *) fileInfo
.name
;
210 resize
|= createTagsForWildcardEntry (pattern
, dirLength
, entry
);
211 } while (_findnext (hFile
, &fileInfo
) == 0);
218 #elif defined (AMIGA)
220 static boolean
createTagsForAmigaWildcard (const char *const pattern
)
222 boolean resize
= FALSE
;
223 struct AnchorPath
*const anchor
=
224 (struct AnchorPath
*) eMalloc ((size_t) ANCHOR_SIZE
);
227 memset (anchor
, 0, (size_t) ANCHOR_SIZE
);
228 anchor
->ap_Strlen
= ANCHOR_BUF_SIZE
;
229 /* Allow '.' for current directory */
231 anchor
->ap_Flags
= APF_DODOT
| APF_DOWILD
;
233 anchor
->ap_Flags
= APF_DoDot
| APF_DoWild
;
235 result
= MatchFirst ((UBYTE
*) pattern
, anchor
);
238 resize
|= createTagsForEntry ((char *) anchor
->ap_Buf
);
239 result
= MatchNext (anchor
);
247 static boolean
recurseIntoDirectory (const char *const dirName
)
249 boolean resize
= FALSE
;
250 if (isRecursiveLink (dirName
))
251 verbose ("ignoring \"%s\" (recursive link)\n", dirName
);
252 else if (! Option
.recurse
)
253 verbose ("ignoring \"%s\" (directory)\n", dirName
);
256 verbose ("RECURSING into directory \"%s\"\n", dirName
);
257 #if defined (HAVE_OPENDIR)
258 resize
= recurseUsingOpendir (dirName
);
259 #elif defined (HAVE_FINDFIRST) || defined (HAVE__FINDFIRST)
261 vString
*const pattern
= vStringNew ();
262 vStringCopyS (pattern
, dirName
);
263 vStringPut (pattern
, OUTPUT_PATH_SEPARATOR
);
264 vStringCatS (pattern
, "*.*");
265 resize
= createTagsForWildcardUsingFindfirst (vStringValue (pattern
));
266 vStringDelete (pattern
);
268 #elif defined (AMIGA)
270 vString
*const pattern
= vStringNew ();
271 if (*dirName
!= '\0' && strcmp (dirName
, ".") != 0)
273 vStringCopyS (pattern
, dirName
);
274 if (dirName
[strlen (dirName
) - 1] != '/')
275 vStringPut (pattern
, '/');
277 vStringCatS (pattern
, "#?");
278 resize
= createTagsForAmigaWildcard (vStringValue (pattern
));
279 vStringDelete (pattern
);
286 static boolean
createTagsForEntry (const char *const entryName
)
288 boolean resize
= FALSE
;
289 fileStatus
*status
= eStat (entryName
);
291 Assert (entryName
!= NULL
);
292 if (isExcludedFile (entryName
))
293 verbose ("excluding \"%s\"\n", entryName
);
294 else if (status
->isSymbolicLink
&& ! Option
.followLinks
)
295 verbose ("ignoring \"%s\" (symbolic link)\n", entryName
);
296 else if (! status
->exists
)
297 error (WARNING
| PERROR
, "cannot open source file \"%s\"", entryName
);
298 else if (status
->isDirectory
)
299 resize
= recurseIntoDirectory (entryName
);
300 else if (! status
->isNormalFile
)
301 verbose ("ignoring \"%s\" (special file)\n", entryName
);
303 resize
= parseFile (entryName
);
309 #ifdef MANUAL_GLOBBING
311 static boolean
createTagsForWildcardArg (const char *const arg
)
313 boolean resize
= FALSE
;
314 vString
*const pattern
= vStringNewInit (arg
);
315 char *patternS
= vStringValue (pattern
);
317 #if defined (HAVE_FINDFIRST) || defined (HAVE__FINDFIRST)
318 /* We must transform the "." and ".." forms into something that can
319 * be expanded by the findfirst/_findfirst functions.
321 if (Option
.recurse
&&
322 (strcmp (patternS
, ".") == 0 || strcmp (patternS
, "..") == 0))
324 vStringPut (pattern
, OUTPUT_PATH_SEPARATOR
);
325 vStringCatS (pattern
, "*.*");
327 resize
|= createTagsForWildcardUsingFindfirst (patternS
);
329 vStringDelete (pattern
);
335 static boolean
createTagsForArgs (cookedArgs
*const args
)
337 boolean resize
= FALSE
;
339 /* Generate tags for each argument on the command line.
341 while (! cArgOff (args
))
343 const char *const arg
= cArgItem (args
);
345 #ifdef MANUAL_GLOBBING
346 resize
|= createTagsForWildcardArg (arg
);
348 resize
|= createTagsForEntry (arg
);
356 /* Read from an opened file a list of file names for which to generate tags.
358 static boolean
createTagsFromFileInput (FILE *const fp
, const boolean filter
)
360 boolean resize
= FALSE
;
363 cookedArgs
*args
= cArgNewFromLineFile (fp
);
365 while (! cArgOff (args
))
367 resize
|= createTagsForEntry (cArgItem (args
));
370 if (Option
.filterTerminator
!= NULL
)
371 fputs (Option
.filterTerminator
, stdout
);
382 /* Read from a named file a list of file names for which to generate tags.
384 static boolean
createTagsFromListFile (const char *const fileName
)
387 Assert (fileName
!= NULL
);
388 if (strcmp (fileName
, "-") == 0)
389 resize
= createTagsFromFileInput (stdin
, FALSE
);
392 FILE *const fp
= fopen (fileName
, "r");
394 error (FATAL
| PERROR
, "cannot open list file \"%s\"", fileName
);
395 resize
= createTagsFromFileInput (fp
, FALSE
);
401 #if defined (HAVE_CLOCK)
402 # define CLOCK_AVAILABLE
403 # ifndef CLOCKS_PER_SEC
404 # define CLOCKS_PER_SEC 1000000
406 #elif defined (HAVE_TIMES)
407 # define CLOCK_AVAILABLE
408 # define CLOCKS_PER_SEC 60
409 static clock_t clock (void)
414 return (buf
.tms_utime
+ buf
.tms_stime
);
417 # define clock() (clock_t)0
420 static void printTotals (const clock_t *const timeStamps
)
422 const unsigned long totalTags
= TagFile
.numTags
.added
+
423 TagFile
.numTags
.prev
;
425 fprintf (errout
, "%ld file%s, %ld line%s (%ld kB) scanned",
426 Totals
.files
, plural (Totals
.files
),
427 Totals
.lines
, plural (Totals
.lines
),
429 #ifdef CLOCK_AVAILABLE
431 const double interval
= ((double) (timeStamps
[1] - timeStamps
[0])) /
434 fprintf (errout
, " in %.01f seconds", interval
);
435 if (interval
!= (double) 0.0)
436 fprintf (errout
, " (%lu kB/s)",
437 (unsigned long) (Totals
.bytes
/ interval
) / 1024L);
440 fputc ('\n', errout
);
442 fprintf (errout
, "%lu tag%s added to tag file",
443 TagFile
.numTags
.added
, plural (TagFile
.numTags
.added
));
445 fprintf (errout
, " (now %lu tags)", totalTags
);
446 fputc ('\n', errout
);
448 if (totalTags
> 0 && Option
.sorted
!= SO_UNSORTED
)
450 fprintf (errout
, "%lu tag%s sorted", totalTags
, plural (totalTags
));
451 #ifdef CLOCK_AVAILABLE
452 fprintf (errout
, " in %.02f seconds",
453 ((double) (timeStamps
[2] - timeStamps
[1])) / CLOCKS_PER_SEC
);
455 fputc ('\n', errout
);
459 fprintf (errout
, "longest tag line = %lu\n",
460 (unsigned long) TagFile
.max
.line
);
464 static boolean
etagsInclude (void)
466 return (boolean
)(Option
.etags
&& Option
.etagsInclude
!= NULL
);
469 static void makeTags (cookedArgs
*args
)
471 clock_t timeStamps
[3];
472 boolean resize
= FALSE
;
473 boolean files
= (boolean
)(! cArgOff (args
) || Option
.fileList
!= NULL
478 if (filesRequired ())
479 error (FATAL
, "No files specified. Try \"%s --help\".",
480 getExecutableName ());
481 else if (! Option
.recurse
&& ! etagsInclude ())
485 #define timeStamp(n) timeStamps[(n)]=(Option.printTotals ? clock():(clock_t)0)
491 if (! cArgOff (args
))
493 verbose ("Reading command line arguments\n");
494 resize
= createTagsForArgs (args
);
496 if (Option
.fileList
!= NULL
)
498 verbose ("Reading list file\n");
499 resize
= (boolean
) (createTagsFromListFile (Option
.fileList
) || resize
);
503 verbose ("Reading filter input\n");
504 resize
= (boolean
) (createTagsFromFileInput (stdin
, TRUE
) || resize
);
506 if (! files
&& Option
.recurse
)
507 resize
= recurseIntoDirectory (".");
512 closeTagFile (resize
);
516 if (Option
.printTotals
)
517 printTotals (timeStamps
);
525 extern int main (int __unused__ argc
, char **argv
)
529 extern int getredirection (int *ac
, char ***av
);
531 /* do wildcard expansion and I/O redirection */
532 getredirection (&argc
, &argv
);
536 /* This program doesn't work when started from the Workbench */
542 _wildcard (&argc
, &argv
); /* expand wildcards in argument list */
545 #if defined (macintosh) && BUILD_MPW_TOOL == 0
546 argc
= ccommand (&argv
);
549 setCurrentDirectory ();
550 setExecutableName (*argv
++);
553 args
= cArgNewFromArgv (argv
);
554 previewFirstOption (args
);
555 testEtagsInvocation ();
556 initializeParsing ();
558 readOptionConfiguration ();
559 verbose ("Reading initial options from command line\n");
568 freeRoutineResources ();
569 freeSourceFileResources ();
570 freeTagFileResources ();
571 freeOptionResources ();
572 freeParserResources ();
573 freeRegexResources ();
579 /* vi:set tabstop=4 shiftwidth=4: */