~ubuntu-branches/ubuntu/lucid/codelite/lucid

« back to all changes in this revision

Viewing changes to sdk/ctags/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2009-02-10 02:27:55 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090210022755-m5692nfc1t5uf1w9
Tags: 1.0.2759+dfsg-0ubuntu1
* New upstream release (LP: #327216).
* debian/patches/series, debian/patches/00_fix-ia64-build.patch:
  + Dropped, applied upstream already.
* debian/patches/02_fix-desktop.patch,
  debian/patches/03_fix-sh.patch:
  + Refreshed to patch cleanly.
* debian/rules:
  + Make get-orig-source honour UPSTREAM_VERSION if set.
* debian/ctags-le.1,
  debian/codelite_indexer.1,
  debian/codelite.manpages:
  + Dropped ctags-le manpage, since ctags-le was replaced by
    codelite_indexer.
  + Added codelite_indexer manpage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
*   $Id: main.c,v 1.24 2006/05/30 04:37:12 darren Exp $
3
 
*
4
 
*   Copyright (c) 1996-2003, Darren Hiebert
5
 
*
6
 
*   Author: Darren Hiebert <dhiebert@users.sourceforge.net>
7
 
*           http://ctags.sourceforge.net
8
 
*
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.
12
 
*
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.
16
 
*
17
 
*   This module contains the start-up code and routines to determine the list
18
 
*   of files to parsed for tags.
19
 
*/
20
 
 
21
 
/*
22
 
*   INCLUDE FILES
23
 
*/
24
 
#include "general.h"  /* must always come first */
25
 
 
26
 
#include <string.h>
27
 
 
28
 
/*  To provide timings features if available.
29
 
 */
30
 
#ifdef HAVE_CLOCK
31
 
# ifdef HAVE_TIME_H
32
 
#  include <time.h>
33
 
# endif
34
 
#else
35
 
# ifdef HAVE_TIMES
36
 
#  ifdef HAVE_SYS_TIMES_H
37
 
#   include <sys/times.h>
38
 
#  endif
39
 
# endif
40
 
#endif
41
 
 
42
 
/*  To provide directory searching for recursion feature.
43
 
 */
44
 
#ifdef AMIGA
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)
49
 
# ifdef __SASC
50
 
   extern struct DosLibrary *DOSBase;
51
 
#  include <pragmas/dos_pragmas.h>
52
 
# endif
53
 
#endif
54
 
 
55
 
#ifdef HAVE_DIRENT_H
56
 
# ifdef __BORLANDC__
57
 
#  define boolean BORLAND_boolean
58
 
# endif
59
 
# ifdef HAVE_SYS_TYPES_H
60
 
#  include <sys/types.h>  /* required by dirent.h */
61
 
# endif
62
 
# include <dirent.h>  /* to declare opendir() */
63
 
# undef boolean
64
 
#endif
65
 
#ifdef HAVE_DIRECT_H
66
 
# include <direct.h>  /* to _getcwd() */
67
 
#endif
68
 
#ifdef HAVE_DOS_H
69
 
# include <dos.h>  /* to declare FA_DIREC */
70
 
#endif
71
 
#ifdef HAVE_DIR_H
72
 
# include <dir.h>  /* to declare findfirst() and findnext */
73
 
#endif
74
 
#ifdef HAVE_IO_H
75
 
# include <io.h>  /* to declare _findfirst() */
76
 
#endif
77
 
 
78
 
 
79
 
#include "debug.h"
80
 
#include "keyword.h"
81
 
#include "main.h"
82
 
#include "options.h"
83
 
#include "read.h"
84
 
#include "routines.h"
85
 
 
86
 
/*
87
 
*   MACROS
88
 
*/
89
 
#define plural(value)  (((unsigned long)(value) == 1L) ? "" : "s")
90
 
 
91
 
/*
92
 
*   DATA DEFINITIONS
93
 
*/
94
 
static struct { long files, lines, bytes; } Totals = { 0, 0, 0 };
95
 
 
96
 
#ifdef AMIGA
97
 
# include "ctags.h"
98
 
  static const char *VERsion = "$VER: "PROGRAM_NAME" "PROGRAM_VERSION" "
99
 
# ifdef __SASC
100
 
  __AMIGADATE__
101
 
# else
102
 
  __DATE__
103
 
# endif
104
 
  " "AUTHOR_NAME" $";
105
 
#endif
106
 
 
107
 
/*
108
 
*   FUNCTION PROTOTYPES
109
 
*/
110
 
static boolean createTagsForEntry (const char *const entryName);
111
 
 
112
 
/*
113
 
*   FUNCTION DEFINITIONS
114
 
*/
115
 
 
116
 
extern void addTotals (
117
 
                const unsigned int files, const long unsigned int lines,
118
 
                const long unsigned int bytes)
119
 
{
120
 
        Totals.files += files;
121
 
        Totals.lines += lines;
122
 
        Totals.bytes += bytes;
123
 
}
124
 
 
125
 
extern boolean isDestinationStdout (void)
126
 
{
127
 
        boolean toStdout = FALSE;
128
 
 
129
 
        if (Option.xref  ||  Option.filter  ||
130
 
                (Option.tagFileName != NULL  &&  (strcmp (Option.tagFileName, "-") == 0
131
 
#if defined (VMS)
132
 
        || strcmp (Option.tagFileName, "sys$output") == 0
133
 
#else
134
 
        || strcmp (Option.tagFileName, "/dev/stdout") == 0
135
 
#endif
136
 
                )))
137
 
                toStdout = TRUE;
138
 
        return toStdout;
139
 
}
140
 
 
141
 
#if defined (HAVE_OPENDIR)
142
 
static boolean recurseUsingOpendir (const char *const dirName)
143
 
{
144
 
        boolean resize = FALSE;
145
 
        DIR *const dir = opendir (dirName);
146
 
        if (dir == NULL)
147
 
                error (WARNING | PERROR, "cannot recurse into directory \"%s\"", dirName);
148
 
        else
149
 
        {
150
 
                struct dirent *entry;
151
 
                while ((entry = readdir (dir)) != NULL)
152
 
                {
153
 
                        if (strcmp (entry->d_name, ".") != 0  &&
154
 
                                strcmp (entry->d_name, "..") != 0)
155
 
                        {
156
 
                                vString *filePath;
157
 
                                if (strcmp (dirName, ".") == 0)
158
 
                                        filePath = vStringNewInit (entry->d_name);
159
 
                                else
160
 
                                        filePath = combinePathAndFile (dirName, entry->d_name);
161
 
                                resize |= createTagsForEntry (vStringValue (filePath));
162
 
                                vStringDelete (filePath);
163
 
                        }
164
 
                }
165
 
                closedir (dir);
166
 
        }
167
 
        return resize;
168
 
}
169
 
 
170
 
#elif defined (HAVE_FINDFIRST) || defined (HAVE__FINDFIRST)
171
 
 
172
 
static boolean createTagsForWildcardEntry (
173
 
                const char *const pattern, const size_t dirLength,
174
 
                const char *const entryName)
175
 
{
176
 
        boolean resize = FALSE;
177
 
        /* we must not recurse into the directories "." or ".." */
178
 
        if (strcmp (entryName, ".") != 0  &&  strcmp (entryName, "..") != 0)
179
 
        {
180
 
                vString *const filePath = vStringNew ();
181
 
                vStringNCopyS (filePath, pattern, dirLength);
182
 
                vStringCatS (filePath, entryName);
183
 
                resize = createTagsForEntry (vStringValue (filePath));
184
 
                vStringDelete (filePath);
185
 
        }
186
 
        return resize;
187
 
}
188
 
 
189
 
static boolean createTagsForWildcardUsingFindfirst (const char *const pattern)
190
 
{
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);
196
 
        while (result == 0)
197
 
        {
198
 
                const char *const entry = (const char *) fileInfo.ff_name;
199
 
                resize |= createTagsForWildcardEntry (pattern, dirLength, entry);
200
 
                result = _findnext (&fileInfo);
201
 
        }
202
 
#elif defined (HAVE__FINDFIRST)
203
 
        struct _finddata_t fileInfo;
204
 
        intptr_t /*findfirst_t*/ hFile = _findfirst (pattern, &fileInfo);
205
 
        if (hFile != -1L)
206
 
        {
207
 
                do
208
 
                {
209
 
                        const char *const entry = (const char *) fileInfo.name;
210
 
                        resize |= createTagsForWildcardEntry (pattern, dirLength, entry);
211
 
                } while (_findnext (hFile, &fileInfo) == 0);
212
 
                _findclose (hFile);
213
 
        }
214
 
#endif
215
 
        return resize;
216
 
}
217
 
 
218
 
#elif defined (AMIGA)
219
 
 
220
 
static boolean createTagsForAmigaWildcard (const char *const pattern)
221
 
{
222
 
        boolean resize = FALSE;
223
 
        struct AnchorPath *const anchor =
224
 
                        (struct AnchorPath *) eMalloc ((size_t) ANCHOR_SIZE);
225
 
        LONG result;
226
 
 
227
 
        memset (anchor, 0, (size_t) ANCHOR_SIZE);
228
 
        anchor->ap_Strlen = ANCHOR_BUF_SIZE;
229
 
        /* Allow '.' for current directory */
230
 
#ifdef APF_DODOT
231
 
        anchor->ap_Flags = APF_DODOT | APF_DOWILD;
232
 
#else
233
 
        anchor->ap_Flags = APF_DoDot | APF_DoWild;
234
 
#endif
235
 
        result = MatchFirst ((UBYTE *) pattern, anchor);
236
 
        while (result == 0)
237
 
        {
238
 
                resize |= createTagsForEntry ((char *) anchor->ap_Buf);
239
 
                result = MatchNext (anchor);
240
 
        }
241
 
        MatchEnd (anchor);
242
 
        eFree (anchor);
243
 
        return resize;
244
 
}
245
 
#endif
246
 
 
247
 
static boolean recurseIntoDirectory (const char *const dirName)
248
 
{
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);
254
 
        else
255
 
        {
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)
260
 
                {
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);
267
 
                }
268
 
#elif defined (AMIGA)
269
 
                {
270
 
                        vString *const pattern = vStringNew ();
271
 
                        if (*dirName != '\0'  &&  strcmp (dirName, ".") != 0)
272
 
                        {
273
 
                                vStringCopyS (pattern, dirName);
274
 
                                if (dirName [strlen (dirName) - 1] != '/')
275
 
                                        vStringPut (pattern, '/');
276
 
                        }
277
 
                        vStringCatS (pattern, "#?");
278
 
                        resize = createTagsForAmigaWildcard (vStringValue (pattern));
279
 
                        vStringDelete (pattern);
280
 
                }
281
 
#endif
282
 
        }
283
 
        return resize;
284
 
}
285
 
 
286
 
static boolean createTagsForEntry (const char *const entryName)
287
 
{
288
 
        boolean resize = FALSE;
289
 
        fileStatus *status = eStat (entryName);
290
 
 
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);
302
 
        else
303
 
                resize = parseFile (entryName);
304
 
 
305
 
        return resize;
306
 
}
307
 
 
308
 
#ifdef MANUAL_GLOBBING
309
 
 
310
 
static boolean createTagsForWildcardArg (const char *const arg)
311
 
{
312
 
        boolean resize = FALSE;
313
 
        vString *const pattern = vStringNewInit (arg);
314
 
        char *patternS = vStringValue (pattern);
315
 
 
316
 
#if defined (HAVE_FINDFIRST) || defined (HAVE__FINDFIRST)
317
 
        /*  We must transform the "." and ".." forms into something that can
318
 
         *  be expanded by the findfirst/_findfirst functions.
319
 
         */
320
 
        if (Option.recurse  &&
321
 
                (strcmp (patternS, ".") == 0  ||  strcmp (patternS, "..") == 0))
322
 
        {
323
 
                vStringPut (pattern, OUTPUT_PATH_SEPARATOR);
324
 
                vStringCatS (pattern, "*.*");
325
 
        }
326
 
        resize |= createTagsForWildcardUsingFindfirst (patternS);
327
 
#endif
328
 
        vStringDelete (pattern);
329
 
        return resize;
330
 
}
331
 
 
332
 
#endif
333
 
 
334
 
static boolean createTagsForArgs (cookedArgs *const args)
335
 
{
336
 
        boolean resize = FALSE;
337
 
 
338
 
        /*  Generate tags for each argument on the command line.
339
 
         */
340
 
        while (! cArgOff (args))
341
 
        {
342
 
                const char *const arg = cArgItem (args);
343
 
 
344
 
#ifdef MANUAL_GLOBBING
345
 
                resize |= createTagsForWildcardArg (arg);
346
 
#else
347
 
                resize |= createTagsForEntry (arg);
348
 
#endif
349
 
                cArgForth (args);
350
 
                parseOptions (args);
351
 
        }
352
 
        return resize;
353
 
}
354
 
 
355
 
/*  Read from an opened file a list of file names for which to generate tags.
356
 
 */
357
 
static boolean createTagsFromFileInput (FILE *const fp, const boolean filter)
358
 
{
359
 
        boolean resize = FALSE;
360
 
        if (fp != NULL)
361
 
        {
362
 
                cookedArgs *args = cArgNewFromLineFile (fp);
363
 
                parseOptions (args);
364
 
                while (! cArgOff (args))
365
 
                {
366
 
                        resize |= createTagsForEntry (cArgItem (args));
367
 
                        if (filter)
368
 
                        {
369
 
                                if (Option.filterTerminator != NULL)
370
 
                                {
371
 
                                        fputs (Option.filterTerminator, stdout);
372
 
                                        /** Eran Ifrah **/
373
 
                                        fputs ("\n", stdout);
374
 
                                }
375
 
                                fflush (stdout);
376
 
                        }
377
 
                        cArgForth (args);
378
 
                        parseOptions (args);
379
 
                }
380
 
                cArgDelete (args);
381
 
        }
382
 
        return resize;
383
 
}
384
 
 
385
 
/*  Read from a named file a list of file names for which to generate tags.
386
 
 */
387
 
static boolean createTagsFromListFile (const char *const fileName)
388
 
{
389
 
        boolean resize;
390
 
        Assert (fileName != NULL);
391
 
        if (strcmp (fileName, "-") == 0)
392
 
                resize = createTagsFromFileInput (stdin, FALSE);
393
 
        else
394
 
        {
395
 
                FILE *const fp = fopen (fileName, "r");
396
 
                if (fp == NULL)
397
 
                        error (FATAL | PERROR, "cannot open list file \"%s\"", fileName);
398
 
                resize = createTagsFromFileInput (fp, FALSE);
399
 
                fclose (fp);
400
 
        }
401
 
        return resize;
402
 
}
403
 
 
404
 
#if defined (HAVE_CLOCK)
405
 
# define CLOCK_AVAILABLE
406
 
# ifndef CLOCKS_PER_SEC
407
 
#  define CLOCKS_PER_SEC                1000000
408
 
# endif
409
 
#elif defined (HAVE_TIMES)
410
 
# define CLOCK_AVAILABLE
411
 
# define CLOCKS_PER_SEC 60
412
 
static clock_t clock (void)
413
 
{
414
 
        struct tms buf;
415
 
 
416
 
        times (&buf);
417
 
        return (buf.tms_utime + buf.tms_stime);
418
 
}
419
 
#else
420
 
# define clock()  (clock_t)0
421
 
#endif
422
 
 
423
 
static void printTotals (const clock_t *const timeStamps)
424
 
{
425
 
        const unsigned long totalTags = TagFile.numTags.added +
426
 
                                                                        TagFile.numTags.prev;
427
 
 
428
 
        fprintf (errout, "%ld file%s, %ld line%s (%ld kB) scanned",
429
 
                        Totals.files, plural (Totals.files),
430
 
                        Totals.lines, plural (Totals.lines),
431
 
                        Totals.bytes/1024L);
432
 
#ifdef CLOCK_AVAILABLE
433
 
        {
434
 
                const double interval = ((double) (timeStamps [1] - timeStamps [0])) /
435
 
                                                                CLOCKS_PER_SEC;
436
 
 
437
 
                fprintf (errout, " in %.01f seconds", interval);
438
 
                if (interval != (double) 0.0)
439
 
                        fprintf (errout, " (%lu kB/s)",
440
 
                                        (unsigned long) (Totals.bytes / interval) / 1024L);
441
 
        }
442
 
#endif
443
 
        fputc ('\n', errout);
444
 
 
445
 
        fprintf (errout, "%lu tag%s added to tag file",
446
 
                        TagFile.numTags.added, plural (TagFile.numTags.added));
447
 
        if (Option.append)
448
 
                fprintf (errout, " (now %lu tags)", totalTags);
449
 
        fputc ('\n', errout);
450
 
 
451
 
        if (totalTags > 0  &&  Option.sorted != SO_UNSORTED)
452
 
        {
453
 
                fprintf (errout, "%lu tag%s sorted", totalTags, plural (totalTags));
454
 
#ifdef CLOCK_AVAILABLE
455
 
                fprintf (errout, " in %.02f seconds",
456
 
                                ((double) (timeStamps [2] - timeStamps [1])) / CLOCKS_PER_SEC);
457
 
#endif
458
 
                fputc ('\n', errout);
459
 
        }
460
 
 
461
 
#ifdef DEBUG
462
 
        fprintf (errout, "longest tag line = %lu\n",
463
 
                        (unsigned long) TagFile.max.line);
464
 
#endif
465
 
}
466
 
 
467
 
static boolean etagsInclude (void)
468
 
{
469
 
        return (boolean)(Option.etags && Option.etagsInclude != NULL);
470
 
}
471
 
 
472
 
static void makeTags (cookedArgs *args)
473
 
{
474
 
        clock_t timeStamps [3];
475
 
        boolean resize = FALSE;
476
 
        boolean files = (boolean)(! cArgOff (args) || Option.fileList != NULL
477
 
                                                          || Option.filter);
478
 
 
479
 
        if (! files)
480
 
        {
481
 
                if (filesRequired ())
482
 
                        error (FATAL, "No files specified. Try \"%s --help\".",
483
 
                                getExecutableName ());
484
 
                else if (! Option.recurse && ! etagsInclude ())
485
 
                        return;
486
 
        }
487
 
 
488
 
#define timeStamp(n) timeStamps[(n)]=(Option.printTotals ? clock():(clock_t)0)
489
 
        if (! Option.filter)
490
 
                openTagFile ();
491
 
 
492
 
        timeStamp (0);
493
 
 
494
 
        if (! cArgOff (args))
495
 
        {
496
 
                verbose ("Reading command line arguments\n");
497
 
                resize = createTagsForArgs (args);
498
 
        }
499
 
        if (Option.fileList != NULL)
500
 
        {
501
 
                verbose ("Reading list file\n");
502
 
                resize = (boolean) (createTagsFromListFile (Option.fileList) || resize);
503
 
        }
504
 
        if (Option.filter)
505
 
        {
506
 
                verbose ("Reading filter input\n");
507
 
                resize = (boolean) (createTagsFromFileInput (stdin, TRUE) || resize);
508
 
        }
509
 
        if (! files  &&  Option.recurse)
510
 
                resize = recurseIntoDirectory (".");
511
 
 
512
 
        timeStamp (1);
513
 
 
514
 
        if (! Option.filter)
515
 
                closeTagFile (resize);
516
 
 
517
 
        timeStamp (2);
518
 
 
519
 
        if (Option.printTotals)
520
 
                printTotals (timeStamps);
521
 
#undef timeStamp
522
 
}
523
 
 
524
 
/*
525
 
 *              Start up code
526
 
 */
527
 
 
528
 
extern int main (int __unused__ argc, char **argv)
529
 
{
530
 
        cookedArgs *args;
531
 
#ifdef VMS
532
 
        extern int getredirection (int *ac, char ***av);
533
 
 
534
 
        /* do wildcard expansion and I/O redirection */
535
 
        getredirection (&argc, &argv);
536
 
#endif
537
 
 
538
 
#ifdef AMIGA
539
 
        /* This program doesn't work when started from the Workbench */
540
 
        if (argc == 0)
541
 
                exit (1);
542
 
#endif
543
 
 
544
 
#ifdef __EMX__
545
 
        _wildcard (&argc, &argv);  /* expand wildcards in argument list */
546
 
#endif
547
 
 
548
 
#if defined (macintosh) && BUILD_MPW_TOOL == 0
549
 
        argc = ccommand (&argv);
550
 
#endif
551
 
 
552
 
        setCurrentDirectory ();
553
 
        setExecutableName (*argv++);
554
 
        checkRegex ();
555
 
 
556
 
        args = cArgNewFromArgv (argv);
557
 
        previewFirstOption (args);
558
 
        testEtagsInvocation ();
559
 
        initializeParsing ();
560
 
        initOptions ();
561
 
        readOptionConfiguration ();
562
 
        verbose ("Reading initial options from command line\n");
563
 
        parseOptions (args);
564
 
        checkOptions ();
565
 
        makeTags (args);
566
 
 
567
 
        /*  Clean up.
568
 
         */
569
 
        cArgDelete (args);
570
 
        freeKeywordTable ();
571
 
        freeRoutineResources ();
572
 
        freeSourceFileResources ();
573
 
        freeTagFileResources ();
574
 
        freeOptionResources ();
575
 
        freeParserResources ();
576
 
        freeRegexResources ();
577
 
 
578
 
        exit (0);
579
 
        return 0;
580
 
}
581
 
 
582
 
/* vi:set tabstop=4 shiftwidth=4: */