ctags/main.c

580 lines
14 KiB
C
Raw Normal View History

/*
* $Id$
*
* Copyright (c) 1996-2003, Darren Hiebert
*
* Author: Darren Hiebert <dhiebert@users.sourceforge.net>
* http://ctags.sourceforge.net
*
* This source code is released for free distribution under the terms of the
* GNU General Public License. It is provided on an as-is basis and no
* responsibility is accepted for its failure to perform as expected.
*
* This is a reimplementation of the ctags (1) program. It is an attempt to
* provide a fully featured ctags program which is free of the limitations
* which most (all?) others are subject to.
*
* This module contains the start-up code and routines to determine the list
* of files to parsed for tags.
*/
/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */
#include <string.h>
/* To provide timings features if available.
*/
#ifdef HAVE_CLOCK
# ifdef HAVE_TIME_H
# include <time.h>
# endif
#else
# ifdef HAVE_TIMES
# ifdef HAVE_SYS_TIMES_H
# include <sys/times.h>
# endif
# endif
#endif
/* To provide directory searching for recursion feature.
*/
#ifdef AMIGA
# include <dos/dosasl.h> /* for struct AnchorPath */
# include <clib/dos_protos.h> /* function prototypes */
# define ANCHOR_BUF_SIZE 512
# define ANCHOR_SIZE (sizeof (struct AnchorPath) + ANCHOR_BUF_SIZE)
# ifdef __SASC
extern struct DosLibrary *DOSBase;
# include <pragmas/dos_pragmas.h>
# endif
#endif
#ifdef HAVE_DIRENT_H
# ifdef __BORLANDC__
# define boolean BORLAND_boolean
# endif
# ifdef HAVE_SYS_TYPES_H
# include <sys/types.h> /* required by dirent.h */
# endif
# include <dirent.h> /* to declare opendir() */
# undef boolean
#endif
#ifdef HAVE_DIRECT_H
# include <direct.h> /* to _getcwd() */
#endif
#ifdef HAVE_DOS_H
# include <dos.h> /* to declare FA_DIREC */
#endif
#ifdef HAVE_DIR_H
# include <dir.h> /* to declare findfirst() and findnext */
#endif
#ifdef HAVE_IO_H
# include <io.h> /* to declare _findfirst() */
#endif
#include "debug.h"
#include "keyword.h"
#include "main.h"
#include "options.h"
#include "read.h"
#include "routines.h"
/*
* MACROS
*/
#define plural(value) (((unsigned long)(value) == 1L) ? "" : "s")
/*
* DATA DEFINITIONS
*/
static struct { long files, lines, bytes; } Totals = { 0, 0, 0 };
#ifdef AMIGA
# include "ctags.h"
static const char *VERsion = "$VER: "PROGRAM_NAME" "PROGRAM_VERSION" "
# ifdef __SASC
__AMIGADATE__
# else
__DATE__
# endif
" "AUTHOR_NAME" $";
#endif
/*
* FUNCTION PROTOTYPES
*/
static boolean createTagsForEntry (const char *const entryName);
/*
* FUNCTION DEFINITIONS
*/
extern void addTotals (
const unsigned int files, const long unsigned int lines,
const long unsigned int bytes)
{
Totals.files += files;
Totals.lines += lines;
Totals.bytes += bytes;
}
extern boolean isDestinationStdout (void)
{
boolean toStdout = FALSE;
if (Option.xref || Option.filter ||
(Option.tagFileName != NULL && (strcmp (Option.tagFileName, "-") == 0
#if defined (VMS)
|| strcmp (Option.tagFileName, "sys$output") == 0
#else
|| strcmp (Option.tagFileName, "/dev/stdout") == 0
#endif
)))
toStdout = TRUE;
return toStdout;
}
#if defined (HAVE_OPENDIR)
static boolean recurseUsingOpendir (const char *const dirName)
{
boolean resize = FALSE;
DIR *const dir = opendir (dirName);
if (dir == NULL)
error (WARNING | PERROR, "cannot recurse into directory \"%s\"", dirName);
else
{
struct dirent *entry;
while ((entry = readdir (dir)) != NULL)
{
if (strcmp (entry->d_name, ".") != 0 &&
strcmp (entry->d_name, "..") != 0)
{
vString *filePath;
if (strcmp (dirName, ".") == 0)
filePath = vStringNewInit (entry->d_name);
else
filePath = combinePathAndFile (dirName, entry->d_name);
resize |= createTagsForEntry (vStringValue (filePath));
vStringDelete (filePath);
}
}
closedir (dir);
}
return resize;
}
#elif defined (HAVE_FINDFIRST) || defined (HAVE__FINDFIRST)
static boolean createTagsForWildcardEntry (
const char *const pattern, const size_t dirLength,
const char *const entryName)
{
boolean resize = FALSE;
/* we must not recurse into the directories "." or ".." */
if (strcmp (entryName, ".") != 0 && strcmp (entryName, "..") != 0)
{
vString *const filePath = vStringNew ();
vStringNCopyS (filePath, pattern, dirLength);
vStringCatS (filePath, entryName);
resize = createTagsForEntry (vStringValue (filePath));
vStringDelete (filePath);
}
return resize;
}
static boolean createTagsForWildcardUsingFindfirst (const char *const pattern)
{
boolean resize = FALSE;
const size_t dirLength = baseFilename (pattern) - pattern;
#if defined (HAVE_FINDFIRST)
struct ffblk fileInfo;
int result = findfirst (pattern, &fileInfo, FA_DIREC);
while (result == 0)
{
const char *const entry = (const char *) fileInfo.ff_name;
resize |= createTagsForWildcardEntry (pattern, dirLength, entry);
result = findnext (&fileInfo);
}
#elif defined (HAVE__FINDFIRST)
struct _finddata_t fileInfo;
findfirst_t hFile = _findfirst (pattern, &fileInfo);
if (hFile != -1L)
{
do
{
const char *const entry = (const char *) fileInfo.name;
resize |= createTagsForWildcardEntry (pattern, dirLength, entry);
} while (_findnext (hFile, &fileInfo) == 0);
_findclose (hFile);
}
#endif
return resize;
}
#elif defined (AMIGA)
static boolean createTagsForAmigaWildcard (const char *const pattern)
{
boolean resize = FALSE;
struct AnchorPath *const anchor =
(struct AnchorPath *) eMalloc ((size_t) ANCHOR_SIZE);
LONG result;
memset (anchor, 0, (size_t) ANCHOR_SIZE);
anchor->ap_Strlen = ANCHOR_BUF_SIZE;
/* Allow '.' for current directory */
#ifdef APF_DODOT
anchor->ap_Flags = APF_DODOT | APF_DOWILD;
#else
anchor->ap_Flags = APF_DoDot | APF_DoWild;
#endif
result = MatchFirst ((UBYTE *) pattern, anchor);
while (result == 0)
{
resize |= createTagsForEntry ((char *) anchor->ap_Buf);
result = MatchNext (anchor);
}
MatchEnd (anchor);
eFree (anchor);
return resize;
}
#endif
static boolean recurseIntoDirectory (const char *const dirName)
{
boolean resize = FALSE;
if (isRecursiveLink (dirName))
verbose ("ignoring \"%s\" (recursive link)\n", dirName);
else if (! Option.recurse)
verbose ("ignoring \"%s\" (directory)\n", dirName);
else
{
verbose ("RECURSING into directory \"%s\"\n", dirName);
#if defined (HAVE_OPENDIR)
resize = recurseUsingOpendir (dirName);
#elif defined (HAVE_FINDFIRST) || defined (HAVE__FINDFIRST)
{
vString *const pattern = vStringNew ();
vStringCopyS (pattern, dirName);
vStringPut (pattern, OUTPUT_PATH_SEPARATOR);
vStringCatS (pattern, "*.*");
resize = createTagsForWildcardUsingFindfirst (vStringValue (pattern));
vStringDelete (pattern);
}
#elif defined (AMIGA)
{
vString *const pattern = vStringNew ();
if (*dirName != '\0' && strcmp (dirName, ".") != 0)
{
vStringCopyS (pattern, dirName);
if (dirName [strlen (dirName) - 1] != '/')
vStringPut (pattern, '/');
}
vStringCatS (pattern, "#?");
resize = createTagsForAmigaWildcard (vStringValue (pattern));
vStringDelete (pattern);
}
#endif
}
return resize;
}
static boolean createTagsForEntry (const char *const entryName)
{
boolean resize = FALSE;
fileStatus *status = eStat (entryName);
Assert (entryName != NULL);
if (isExcludedFile (entryName))
verbose ("excluding \"%s\"\n", entryName);
else if (status->isSymbolicLink && ! Option.followLinks)
verbose ("ignoring \"%s\" (symbolic link)\n", entryName);
else if (! status->exists)
error (WARNING | PERROR, "cannot open source file \"%s\"", entryName);
else if (status->isDirectory)
resize = recurseIntoDirectory (entryName);
else if (! status->isNormalFile)
verbose ("ignoring \"%s\" (special file)\n", entryName);
else
resize = parseFile (entryName);
trunk/args.c: trunk/asm.c: trunk/beta.c: trunk/eiffel.c: trunk/fortran.c: trunk/jscript.c: trunk/lregex.c: trunk/main.c: trunk/options.c: trunk/pascal.c: trunk/read.c: trunk/routines.c: trunk/routines.h: trunk/sml.c: trunk/sql.c: fix almost all our current memory leaks. Based on a patch from Dmitry Antipov, but also using his vString leak detector and valgrind(1) to find new ones and ones he missed. Three known leaks remain. The first is in parseLongOption. There's also one in "fortran.c" and another in "sql.c": helium:~/Projects/ctags/trunk$ valgrind --leak-check=full --show-reachable=yes ./dctags -f - Test/* ==3056== Memcheck, a memory error detector. ==3056== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al. ==3056== Using LibVEX rev 1658, a library for dynamic binary translation. ==3056== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP. ==3056== Using valgrind-3.2.1-Debian, a dynamic binary instrumentation framework. ==3056== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al. ==3056== For more details, rerun with: -v ==3056== . . . ==3056== ==3056== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1) ==3056== malloc/free: in use at exit: 708 bytes in 22 blocks. ==3056== malloc/free: 36,126 allocs, 36,104 frees, 1,584,216 bytes allocated. ==3056== For counts of detected errors, rerun with: -v ==3056== searching for pointers to 22 not-freed blocks. ==3056== checked 68,184 bytes. ==3056== ==3056== 68 bytes in 2 blocks are definitely lost in loss record 1 of 2 ==3056== at 0x4021620: malloc (vg_replace_malloc.c:149) ==3056== by 0x806347E: eMalloc (routines.c:238) ==3056== by 0x8065B68: newToken (sql.c:347) ==3056== by 0x80662BB: parseSubProgram (sql.c:688) ==3056== by 0x8067867: parseSqlFile (sql.c:1782) ==3056== by 0x8067934: findSqlTags (sql.c:1810) ==3056== by 0x8060760: createTagsForFile (parse.c:618) ==3056== by 0x8060810: createTagsWithFallback (parse.c:640) ==3056== by 0x80608DC: parseFile (parse.c:667) ==3056== by 0x805B7D6: createTagsForEntry (main.c:303) ==3056== by 0x805B811: createTagsForArgs (main.c:348) ==3056== by 0x805BD6F: makeTags (main.c:494) ==3056== ==3056== ==3056== 640 bytes in 20 blocks are still reachable in loss record 2 of 2 ==3056== at 0x4021620: malloc (vg_replace_malloc.c:149) ==3056== by 0x806347E: eMalloc (routines.c:238) ==3056== by 0x806AEE2: vStringNew (vstring.c:116) ==3056== by 0x80542C0: newToken (fortran.c:419) ==3056== by 0x8054309: newTokenFrom (fortran.c:429) ==3056== by 0x80562E5: parseInterfaceBlock (fortran.c:1709) ==3056== by 0x805661D: parseDeclarationConstruct (fortran.c:1834) ==3056== by 0x805679F: parseSpecificationPart (fortran.c:1901) ==3056== by 0x80569F5: parseModule (fortran.c:1990) ==3056== by 0x8056E05: parseProgramUnit (fortran.c:2142) ==3056== by 0x8056F37: findFortranTags (fortran.c:2183) ==3056== by 0x806077A: createTagsForFile (parse.c:620) ==3056== ==3056== LEAK SUMMARY: ==3056== definitely lost: 68 bytes in 2 blocks. ==3056== possibly lost: 0 bytes in 0 blocks. ==3056== still reachable: 640 bytes in 20 blocks. ==3056== suppressed: 0 bytes in 0 blocks. I think they're both awkward longjmp(3)/setjmp(3)-related leaks, and I don't currently have a good solution. ("eiffel.c" cunningly only calls newToken once, before calling setjmp(3).) git-svn-id: svn://svn.code.sf.net/p/ctags/code/trunk@536 c5d04d22-be80-434c-894e-aa346cc9e8e8
2007-06-02 06:09:00 +00:00
eStatFree (status);
return resize;
}
#ifdef MANUAL_GLOBBING
static boolean createTagsForWildcardArg (const char *const arg)
{
boolean resize = FALSE;
vString *const pattern = vStringNewInit (arg);
char *patternS = vStringValue (pattern);
#if defined (HAVE_FINDFIRST) || defined (HAVE__FINDFIRST)
/* We must transform the "." and ".." forms into something that can
* be expanded by the findfirst/_findfirst functions.
*/
if (Option.recurse &&
(strcmp (patternS, ".") == 0 || strcmp (patternS, "..") == 0))
{
vStringPut (pattern, OUTPUT_PATH_SEPARATOR);
vStringCatS (pattern, "*.*");
}
resize |= createTagsForWildcardUsingFindfirst (patternS);
#endif
vStringDelete (pattern);
return resize;
}
#endif
static boolean createTagsForArgs (cookedArgs *const args)
{
boolean resize = FALSE;
/* Generate tags for each argument on the command line.
*/
while (! cArgOff (args))
{
const char *const arg = cArgItem (args);
#ifdef MANUAL_GLOBBING
resize |= createTagsForWildcardArg (arg);
#else
resize |= createTagsForEntry (arg);
#endif
cArgForth (args);
parseOptions (args);
}
return resize;
}
/* Read from an opened file a list of file names for which to generate tags.
*/
static boolean createTagsFromFileInput (FILE *const fp, const boolean filter)
{
boolean resize = FALSE;
if (fp != NULL)
{
cookedArgs *args = cArgNewFromLineFile (fp);
parseOptions (args);
while (! cArgOff (args))
{
resize |= createTagsForEntry (cArgItem (args));
if (filter)
{
if (Option.filterTerminator != NULL)
fputs (Option.filterTerminator, stdout);
fflush (stdout);
}
cArgForth (args);
parseOptions (args);
}
cArgDelete (args);
}
return resize;
}
/* Read from a named file a list of file names for which to generate tags.
*/
static boolean createTagsFromListFile (const char *const fileName)
{
boolean resize;
Assert (fileName != NULL);
if (strcmp (fileName, "-") == 0)
resize = createTagsFromFileInput (stdin, FALSE);
else
{
FILE *const fp = fopen (fileName, "r");
if (fp == NULL)
error (FATAL | PERROR, "cannot open list file \"%s\"", fileName);
resize = createTagsFromFileInput (fp, FALSE);
fclose (fp);
}
return resize;
}
#if defined (HAVE_CLOCK)
# define CLOCK_AVAILABLE
# ifndef CLOCKS_PER_SEC
# define CLOCKS_PER_SEC 1000000
# endif
#elif defined (HAVE_TIMES)
# define CLOCK_AVAILABLE
# define CLOCKS_PER_SEC 60
static clock_t clock (void)
{
struct tms buf;
times (&buf);
return (buf.tms_utime + buf.tms_stime);
}
#else
# define clock() (clock_t)0
#endif
static void printTotals (const clock_t *const timeStamps)
{
const unsigned long totalTags = TagFile.numTags.added +
TagFile.numTags.prev;
fprintf (errout, "%ld file%s, %ld line%s (%ld kB) scanned",
Totals.files, plural (Totals.files),
Totals.lines, plural (Totals.lines),
Totals.bytes/1024L);
#ifdef CLOCK_AVAILABLE
{
const double interval = ((double) (timeStamps [1] - timeStamps [0])) /
CLOCKS_PER_SEC;
fprintf (errout, " in %.01f seconds", interval);
if (interval != (double) 0.0)
fprintf (errout, " (%lu kB/s)",
(unsigned long) (Totals.bytes / interval) / 1024L);
}
#endif
fputc ('\n', errout);
fprintf (errout, "%lu tag%s added to tag file",
TagFile.numTags.added, plural (TagFile.numTags.added));
if (Option.append)
fprintf (errout, " (now %lu tags)", totalTags);
fputc ('\n', errout);
if (totalTags > 0 && Option.sorted != SO_UNSORTED)
{
fprintf (errout, "%lu tag%s sorted", totalTags, plural (totalTags));
#ifdef CLOCK_AVAILABLE
fprintf (errout, " in %.02f seconds",
((double) (timeStamps [2] - timeStamps [1])) / CLOCKS_PER_SEC);
#endif
fputc ('\n', errout);
}
#ifdef DEBUG
fprintf (errout, "longest tag line = %lu\n",
(unsigned long) TagFile.max.line);
#endif
}
static boolean etagsInclude (void)
{
return (boolean)(Option.etags && Option.etagsInclude != NULL);
}
static void makeTags (cookedArgs *args)
{
clock_t timeStamps [3];
boolean resize = FALSE;
boolean files = (boolean)(! cArgOff (args) || Option.fileList != NULL
|| Option.filter);
if (! files)
{
if (filesRequired ())
error (FATAL, "No files specified. Try \"%s --help\".",
getExecutableName ());
else if (! Option.recurse && ! etagsInclude ())
return;
}
#define timeStamp(n) timeStamps[(n)]=(Option.printTotals ? clock():(clock_t)0)
if (! Option.filter)
openTagFile ();
timeStamp (0);
if (! cArgOff (args))
{
verbose ("Reading command line arguments\n");
resize = createTagsForArgs (args);
}
if (Option.fileList != NULL)
{
verbose ("Reading list file\n");
resize = (boolean) (createTagsFromListFile (Option.fileList) || resize);
}
if (Option.filter)
{
verbose ("Reading filter input\n");
resize = (boolean) (createTagsFromFileInput (stdin, TRUE) || resize);
}
if (! files && Option.recurse)
resize = recurseIntoDirectory (".");
timeStamp (1);
if (! Option.filter)
closeTagFile (resize);
timeStamp (2);
if (Option.printTotals)
printTotals (timeStamps);
#undef timeStamp
}
/*
* Start up code
*/
extern int main (int __unused__ argc, char **argv)
{
cookedArgs *args;
#ifdef VMS
extern int getredirection (int *ac, char ***av);
/* do wildcard expansion and I/O redirection */
getredirection (&argc, &argv);
#endif
#ifdef AMIGA
/* This program doesn't work when started from the Workbench */
if (argc == 0)
exit (1);
#endif
#ifdef __EMX__
_wildcard (&argc, &argv); /* expand wildcards in argument list */
#endif
#if defined (macintosh) && BUILD_MPW_TOOL == 0
argc = ccommand (&argv);
#endif
setCurrentDirectory ();
setExecutableName (*argv++);
checkRegex ();
args = cArgNewFromArgv (argv);
previewFirstOption (args);
testEtagsInvocation ();
initializeParsing ();
initOptions ();
readOptionConfiguration ();
verbose ("Reading initial options from command line\n");
parseOptions (args);
checkOptions ();
makeTags (args);
/* Clean up.
*/
cArgDelete (args);
freeKeywordTable ();
freeRoutineResources ();
freeSourceFileResources ();
freeTagFileResources ();
freeOptionResources ();
freeParserResources ();
freeRegexResources ();
exit (0);
return 0;
}
/* vi:set tabstop=4 shiftwidth=4: */