#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994, 1995\n\
The Regents of the University of California. All rights reserved.\n");
#endif
#ifndef lint
#if 0
static char sccsid[] = "@(#)ctags.c 8.4 (Berkeley) 2/7/95";
#endif
__RCSID("$NetBSD: ctags.c,v 1.6 1998/08/25 20:59:36 ross Exp $");
#endif
#include <err.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "ctags.h"
NODE *head;
bool _wht[256], _etk[256], _itk[256], _btk[256], _gd[256];
FILE *inf;
FILE *outf;
long lineftell;
int lineno;
int dflag;
int tflag=1;
int vflag;
int wflag;
int xflag;
char *curfile;
char searchar = '/';
char lbuf[LINE_MAX];
void init __P((void));
int main __P((int, char **));
void find_entries __P((char *));
int
main(argc, argv)
int argc;
char **argv;
{
static char *outfile = "tags";
int aflag;
int uflag;
int exit_val;
int step;
int ch;
char cmd[100];
aflag = uflag = NO;
while ((ch = getopt(argc, argv, "BFadf:tuwvx")) != -1)
switch(ch) {
case 'B':
searchar = '?';
break;
case 'F':
searchar = '/';
break;
case 'a':
aflag++;
break;
case 'd':
dflag++;
break;
case 'f':
outfile = optarg;
break;
case 't':
tflag++;
break;
case 'T':
tflag--;
break;
case 'u':
uflag++;
break;
case 'w':
wflag++;
break;
case 'v':
vflag++;
case 'x':
xflag++;
break;
case '?':
default:
goto usage;
}
argv += optind;
argc -= optind;
if (!argc) {
usage: (void)fprintf(stderr,
"usage: ctags [-BFadtuwvx] [-f tagsfile] file ...\n");
exit(1);
}
init();
for (exit_val = step = 0; step < argc; ++step)
if (!(inf = fopen(argv[step], "r"))) {
warn("%s", argv[step]);
exit_val = 1;
}
else {
curfile = argv[step];
find_entries(argv[step]);
(void)fclose(inf);
}
if (head) {
if (xflag)
put_entries(head);
else {
if (uflag) {
for (step = 0; step < argc; step++) {
(void)sprintf(cmd,
"mv %s OTAGS; fgrep -v '\t%s\t' OTAGS >%s; rm OTAGS",
outfile, argv[step],
outfile);
system(cmd);
}
++aflag;
}
if (!(outf = fopen(outfile, aflag ? "a" : "w")))
err(exit_val ? exit_val : 1, "%s", outfile);
put_entries(head);
(void)fclose(outf);
if (uflag) {
(void)sprintf(cmd, "sort -o %s %s",
outfile, outfile);
system(cmd);
}
}
}
exit(exit_val);
}
void
init()
{
int i;
unsigned char *sp;
for (i = 0; i < 256; i++) {
_wht[i] = _etk[i] = _itk[i] = _btk[i] = NO;
_gd[i] = YES;
}
#define CWHITE (unsigned char *)" \f\t\n"
for (sp = CWHITE; *sp; sp++)
_wht[*sp] = YES;
#define CTOKEN (unsigned char *)" \t\n\"'#()[]{}=-+%*/&|^~!<>;,.:?"
for (sp = CTOKEN; *sp; sp++)
_etk[*sp] = YES;
#define CINTOK (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz0123456789"
for (sp = CINTOK; *sp; sp++)
_itk[*sp] = YES;
#define CBEGIN (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
for (sp = CBEGIN; *sp; sp++)
_btk[*sp] = YES;
#define CNOTGD (unsigned char *)",;"
for (sp = CNOTGD; *sp; sp++)
_gd[*sp] = NO;
}
void
find_entries(file)
char *file;
{
char *cp;
lineno = 0;
if ((cp = strrchr(file, '.')) != NULL) {
if (cp[1] == 'l' && !cp[2]) {
int c;
for (;;) {
if (GETC(==, EOF))
return;
if (!iswhite(c)) {
rewind(inf);
break;
}
}
#define LISPCHR ";(["
if (strchr(LISPCHR, c)) {
l_entries();
return;
}
else {
toss_yysec();
(void)strcpy(lbuf, "%%$");
pfnote("yylex", lineno);
rewind(inf);
}
}
else if (cp[1] == 'y' && !cp[2]) {
toss_yysec();
(void)strcpy(lbuf, "%%$");
pfnote("yyparse", lineno);
y_entries();
}
else if ((cp[1] != 'c' && cp[1] != 'h') && !cp[2]) {
if (PF_funcs())
return;
rewind(inf);
}
}
c_entries();
}