#include <curses.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <unistd.h>
#include <sys/stat.h>
#include <libgen.h>
#include "global.h"
#include "version.h"
#include "vp.h"
#define OPTSEPS " \t"
#define MINHOURS 4
#define EDITOR "vi"
#define SHELL "sh"
#define TMPDIR "/tmp"
char dichar1[] = " teisaprnl(of)=c";
char dichar2[] = " tnerpla";
char dicode1[256];
char dicode2[256];
char *editor, *home, *shell;
BOOL compress = YES;
int cscopedepth;
char currentdir[PATHLEN + 1];
BOOL dbtruncated;
char **dbvpdirs;
int dbvpndirs;
int dispcomponents = 1;
BOOL editallprompt = YES;
int fileargc;
char **fileargv;
int fileversion;
BOOL incurses;
INVCONTROL invcontrol;
BOOL invertedindex;
BOOL isuptodate;
BOOL linemode;
char *namefile;
char *newinvname;
char *newinvpost;
char *newreffile;
FILE *newrefs;
BOOL noacttimeout;
BOOL ogs;
FILE *postings;
char *prependpath;
BOOL returnrequired;
int symrefs = -1;
char temp1[PATHLEN + 1];
char temp2[PATHLEN + 1];
long totalterms;
BOOL truncatesyms;
static BOOL buildonly;
static BOOL fileschanged;
static char *invname = INVNAME;
static char *invpost = INVPOST;
static unsigned noacttime;
static BOOL onesearch;
static char *reffile = REFFILE;
static char *reflines;
static char *tmpdir;
static long traileroffset;
static BOOL unconditional;
static void options(int argc, char **argv);
static void printusage(void);
static void removeindex(void);
static void cannotindex(void);
static void initcompress(void);
static void opendatabase(void);
static void closedatabase(void);
static void build(void);
static int compare(const void *s1, const void *s2);
static char *getoldfile(void);
static void putheader(char *dir);
static void putlist(char **names, int count);
static BOOL samelist(FILE *oldrefs, char **names, int count);
static void skiplist(FILE *oldrefs);
static void copydata(void);
static void copyinverted(void);
static void putinclude(char *s);
static void movefile(char *new, char *old);
static void timedout(int sig);
int
main(int argc, char **argv)
{
int envc;
char **envv;
FILE *names;
int oldnum;
char path[PATHLEN + 1];
FILE *oldrefs;
char *s;
int c, i;
pid_t pid;
argv0 = basename(argv[0]);
if (mygetwd(currentdir) == NULL) {
(void) fprintf(stderr,
"cscope: warning: cannot get current directory name\n");
(void) strcpy(currentdir, "<unknown>");
}
vpinit(currentdir);
dbvpndirs = vpndirs;
dbvpdirs = vpdirs;
sourcedir(".");
editor = mygetenv("EDITOR", EDITOR);
editor = mygetenv("VIEWER", editor);
home = getenv("HOME");
shell = mygetenv("SHELL", SHELL);
tmpdir = mygetenv("TMPDIR", TMPDIR);
cscopedepth = atoi(mygetenv("CSCOPEDEPTH", "0"));
(void) sprintf(path, "CSCOPEDEPTH=%d", ++cscopedepth);
(void) putenv(stralloc(path));
if ((s = getenv("CSCOPEOPTIONS")) != NULL) {
envc = 1;
envv = mymalloc(sizeof (char *));
s = strtok(stralloc(s), OPTSEPS);
while (s != NULL) {
envv = myrealloc(envv, ++envc * sizeof (char *));
envv[envc - 1] = stralloc(s);
s = strtok((char *)NULL, OPTSEPS);
}
options(envc, envv);
}
options(argc, argv);
pid = getpid();
(void) sprintf(temp1, "%s/cscope%d.1", tmpdir, (int)pid);
(void) sprintf(temp2, "%s/cscope%d.2", tmpdir, (int)pid);
if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
(void) signal(SIGINT, myexit);
(void) signal(SIGQUIT, myexit);
}
(void) signal(SIGHUP, myexit);
if (reffile[0] != '/' && access(".", WRITE) != 0) {
(void) sprintf(path, "%s/%s", home, reffile);
if (isuptodate == NO || access(reffile, READ) != 0) {
reffile = stralloc(path);
(void) sprintf(path, "%s/%s", home, invname);
invname = stralloc(path);
(void) sprintf(path, "%s/%s", home, invpost);
invpost = stralloc(path);
(void) fprintf(stderr,
"cscope: symbol database will be %s\n", reffile);
}
}
if (isuptodate == YES) {
if ((oldrefs = vpfopen(reffile, "r")) == NULL) {
cannotopen(reffile);
exit(1);
}
if (fscanf(oldrefs, "cscope %d %*s", &fileversion) != 1) {
(void) fprintf(stderr,
"cscope: cannot read file version from file %s\n",
reffile);
exit(1);
}
if (fileversion >= 8) {
compress = YES;
invertedindex = NO;
for (;;) {
while ((c = getc(oldrefs)) == ' ') {
;
}
if (c != '-') {
(void) ungetc(c, oldrefs);
break;
}
switch (c = getc(oldrefs)) {
case 'c':
compress = NO;
break;
case 'q':
invertedindex = YES;
(void) fscanf(oldrefs,
"%ld", &totalterms);
break;
case 'T':
dbtruncated = YES;
truncatesyms = YES;
break;
}
}
initcompress();
if (fscanf(oldrefs, "%ld", &traileroffset) != 1) {
(void) fprintf(stderr,
"cscope: cannot read trailer offset from "
"file %s\n", reffile);
exit(1);
}
if (fseek(oldrefs, traileroffset, 0) != 0) {
(void) fprintf(stderr,
"cscope: cannot seek to trailer in "
"file %s\n", reffile);
exit(1);
}
}
if (fileversion >= 13) {
if (fscanf(oldrefs, "%d", &dbvpndirs) != 1) {
(void) fprintf(stderr,
"cscope: cannot read view path size from "
"file %s\n", reffile);
exit(1);
}
if (dbvpndirs > 0) {
dbvpdirs = mymalloc(
dbvpndirs * sizeof (char *));
for (i = 0; i < dbvpndirs; ++i) {
if (fscanf(oldrefs, "%s", path) != 1) {
(void) fprintf(stderr,
"cscope: cannot read view "
"path from file %s\n",
reffile);
exit(1);
}
dbvpdirs[i] = stralloc(path);
}
}
}
skiplist(oldrefs);
skiplist(oldrefs);
if (fscanf(oldrefs, "%d", &nsrcfiles) != 1) {
(void) fprintf(stderr,
"cscope: cannot read source file size from "
"file %s\n", reffile);
exit(1);
}
srcfiles = mymalloc(nsrcfiles * sizeof (char *));
if (fileversion >= 9) {
if (fscanf(oldrefs, "%d", &oldnum) != 1) {
(void) fprintf(stderr,
"cscope: cannot read string space size "
"from file %s\n", reffile);
exit(1);
}
s = mymalloc(oldnum);
(void) getc(oldrefs);
if (fread(s, oldnum, 1, oldrefs) != 1) {
(void) fprintf(stderr,
"cscope: cannot read source file names "
"from file %s\n", reffile);
exit(1);
}
for (i = 0; i < nsrcfiles; ++i) {
srcfiles[i] = s;
for (++s; *s != '\n'; ++s) {
;
}
*s = '\0';
++s;
}
if (namefile != NULL &&
(names = vpfopen(namefile, "r")) != NULL ||
(names = vpfopen(NAMEFILE, "r")) != NULL) {
while (fscanf(names, "%s", path) == 1 &&
*path == '-') {
i = path[1];
s = path + 2;
if (*s == '\0') {
(void) fscanf(names,
"%s", path);
s = path;
}
switch (i) {
case 'p':
if (*s < '0' || *s > '9') {
(void) fprintf(stderr,
"cscope: -p option "
"in file %s: "
"missing or "
"invalid numeric "
"value\n",
namefile);
}
dispcomponents = atoi(s);
}
}
(void) fclose(names);
}
} else {
for (i = 0; i < nsrcfiles; ++i) {
if (fscanf(oldrefs, "%s", path) != 1) {
(void) fprintf(stderr,
"cscope: cannot read source file "
"name from file %s\n", reffile);
exit(1);
}
srcfiles[i] = stralloc(path);
}
}
(void) fclose(oldrefs);
} else {
if ((s = getenv("SOURCEDIRS")) != NULL) {
sourcedir(s);
}
srcfiles = mymalloc(msrcfiles * sizeof (char *));
makefilelist();
if (nsrcfiles == 0) {
(void) fprintf(stderr,
"cscope: no source files found\n");
printusage();
exit(1);
}
if ((s = getenv("INCLUDEDIRS")) != NULL) {
includedir(s);
}
includedir("/usr/include");
initsymtab();
(void) strcpy(path, reffile);
s = basename(path);
*s = '\0';
(void) strcat(path, "n");
++s;
(void) strcpy(s, basename(reffile));
newreffile = stralloc(path);
(void) strcpy(s, basename(invname));
newinvname = stralloc(path);
(void) strcpy(s, basename(invpost));
newinvpost = stralloc(path);
initcompress();
build();
if (buildonly == YES) {
exit(0);
}
}
opendatabase();
if (noacttime) {
(void) signal(SIGALRM, timedout);
(void) alarm(noacttime);
}
if (linemode == YES) {
if (*pattern != '\0') {
if (search() == YES) {
while ((c = getc(refsfound)) != EOF) {
(void) putchar(c);
}
}
}
if (onesearch == YES) {
myexit(0);
}
for (;;) {
char buf[PATLEN + 2];
if (noacttime) {
(void) alarm(noacttime);
}
(void) printf(">> ");
(void) fflush(stdout);
if (fgets(buf, sizeof (buf), stdin) == NULL) {
myexit(0);
}
if (*(s = buf + strlen(buf) - 1) == '\n') {
*s = '\0';
}
switch (*buf) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
field = *buf - '0';
(void) strcpy(pattern, buf + 1);
(void) search();
(void) printf("cscope: %d lines\n", totallines);
while ((c = getc(refsfound)) != EOF) {
(void) putchar(c);
}
break;
case 'c':
case ctrl('C'):
if (caseless == NO) {
caseless = YES;
} else {
caseless = NO;
}
egrepcaseless(caseless);
break;
case 'r':
case ctrl('R'):
freefilelist();
makefilelist();
case 'R':
rebuild();
(void) putchar('\n');
break;
case 'C':
freefilelist();
(void) putchar('\n');
break;
case 'F':
(void) strcpy(path, buf + 1);
if (infilelist(path) == NO &&
vpaccess(path, READ) == 0) {
addsrcfile(path);
}
(void) putchar('\n');
break;
case 'P':
if (prependpath != NULL) {
(void) puts(prependpath);
} else {
(void) puts(currentdir);
}
break;
case 'q':
case ctrl('D'):
case ctrl('Z'):
myexit(0);
default:
(void) fprintf(stderr,
"cscope: unknown command '%s'\n", buf);
break;
}
}
}
if (errorsfound == YES) {
errorsfound = NO;
askforreturn();
}
(void) signal(SIGINT, SIG_IGN);
(void) signal(SIGPIPE, SIG_IGN);
(void) initscr();
setfield();
entercurses();
(void) keypad(stdscr, TRUE);
dispinit();
putmsg("");
display();
if (*pattern != '\0') {
atfield();
(void) command(ctrl('A'));
display();
} else if (reflines != NULL) {
(void) readrefs(reflines);
display();
}
for (;;) {
if (noacttime) {
(void) alarm(noacttime);
}
atfield();
if ((c = mygetch()) == EOF || c == ctrl('D') ||
c == ctrl('Z')) {
break;
}
if (command(c) == YES) {
display();
}
}
myexit(0);
return (0);
}
static void
options(int argc, char **argv)
{
char path[PATHLEN + 1];
int c;
char *s;
while (--argc > 0 && (*++argv)[0] == '-') {
for (s = argv[0] + 1; *s != '\0'; s++) {
if (isdigit(*s)) {
field = *s - '0';
if (*++s == '\0' && --argc > 0) {
s = *++argv;
}
if (strlen(s) > PATLEN) {
(void) fprintf(stderr,
"cscope: pattern too long, cannot "
"be > %d characters\n", PATLEN);
exit(1);
}
(void) strcpy(pattern, s);
goto nextarg;
}
switch (*s) {
case '-':
--argc;
++argv;
goto lastarg;
case 'V':
(void) fprintf(stderr,
"%s: version %d%s\n", argv0,
FILEVERSION, FIXVERSION);
exit(0);
case 'b':
buildonly = YES;
break;
case 'c':
compress = NO;
break;
case 'C':
caseless = YES;
egrepcaseless(caseless);
break;
case 'd':
isuptodate = YES;
break;
case 'e':
editallprompt = NO;
break;
case 'L':
onesearch = YES;
case 'l':
linemode = YES;
break;
case 'o':
ogs = YES;
break;
case 'q':
invertedindex = YES;
break;
case 'r':
returnrequired = YES;
break;
case 'T':
truncatesyms = YES;
break;
case 'u':
unconditional = YES;
break;
case 'U':
fileschanged = YES;
break;
case 'f':
case 'F':
case 'i':
case 'I':
case 'p':
case 'P':
case 's':
case 'S':
case 't':
c = *s;
if (*++s == '\0' && --argc > 0) {
s = *++argv;
}
if (*s == '\0') {
(void) fprintf(stderr,
"%s: -%c option: missing or empty "
"value\n", argv0, c);
goto usage;
}
switch (c) {
case 'f':
reffile = s;
(void) strcpy(path, s);
s = basename(path);
if ((int)strlen(s) > 11) {
s[11] = '\0';
}
s = path + strlen(path);
(void) strcpy(s, ".in");
invname = stralloc(path);
(void) strcpy(s, ".po");
invpost = stralloc(path);
break;
case 'F':
reflines = s;
break;
case 'i':
namefile = s;
break;
case 'I':
includedir(s);
break;
case 'p':
if (*s < '0' || *s > '9') {
(void) fprintf(stderr,
"%s: -p option: missing "
"or invalid numeric "
"value\n", argv0);
goto usage;
}
dispcomponents = atoi(s);
break;
case 'P':
prependpath = s;
break;
case 's':
case 'S':
sourcedir(s);
break;
case 't':
if (*s < '1' || *s > '9') {
(void) fprintf(stderr,
"%s: -t option: missing or "
"invalid numeric value\n",
argv0);
goto usage;
}
c = atoi(s);
if (c < MINHOURS) {
(void) fprintf(stderr,
"cscope: minimum timeout "
"is %d hours\n", MINHOURS);
(void) sleep(3);
c = MINHOURS;
}
noacttime = c * 3600;
break;
}
goto nextarg;
default:
(void) fprintf(stderr,
"%s: unknown option: -%c\n", argv0, *s);
usage:
printusage();
exit(1);
}
}
nextarg: continue;
}
lastarg:
fileargc = argc;
fileargv = argv;
}
static void
printusage(void)
{
(void) fprintf(stderr,
"Usage: cscope [-bcdelLoqrtTuUV] [-f file] [-F file] [-i file] "
"[-I dir] [-s dir]\n");
(void) fprintf(stderr,
" [-p number] [-P path] [-[0-8] pattern] "
"[source files]\n");
(void) fprintf(stderr,
"-b Build the database only.\n");
(void) fprintf(stderr,
"-c Use only ASCII characters in the database file, "
"that is,\n");
(void) fprintf(stderr,
" do not compress the data.\n");
(void) fprintf(stderr,
"-d Do not update the database.\n");
(void) fprintf(stderr,
"-f \"file\" Use \"file\" as the database file name "
"instead of\n");
(void) fprintf(stderr,
" the default (cscope.out).\n");
(void) fprintf(stderr,
"-F \"file\" Read symbol reference lines from file, just\n");
(void) fprintf(stderr,
" like the \"<\" command.\n");
(void) fprintf(stderr,
"-i \"file\" Read any -I, -p, -q, and -T options and the\n");
(void) fprintf(stderr,
" list of source files from \"file\" instead of the \n");
(void) fprintf(stderr,
" default (cscope.files).\n");
(void) fprintf(stderr,
"-I \"dir\" Look in \"dir\" for #include files.\n");
(void) fprintf(stderr,
"-q Build an inverted index for quick symbol seaching.\n");
(void) fprintf(stderr,
"-s \"dir\" Look in \"dir\" for additional source files.\n");
}
static void
removeindex(void)
{
(void) fprintf(stderr,
"cscope: removed files %s and %s\n", invname, invpost);
(void) unlink(invname);
(void) unlink(invpost);
}
static void
cannotindex(void)
{
(void) fprintf(stderr,
"cscope: cannot create inverted index; ignoring -q option\n");
invertedindex = NO;
errorsfound = YES;
(void) fprintf(stderr,
"cscope: removed files %s and %s\n", newinvname, newinvpost);
(void) unlink(newinvname);
(void) unlink(newinvpost);
removeindex();
}
void
cannotopen(char *file)
{
char msg[MSGLEN + 1];
(void) sprintf(msg, "Cannot open file %s", file);
putmsg(msg);
}
void
cannotwrite(char *file)
{
char msg[MSGLEN + 1];
(void) sprintf(msg, "Removed file %s because write failed", file);
myperror(msg);
(void) unlink(file);
myexit(1);
}
static void
initcompress(void)
{
int i;
if (compress == YES) {
for (i = 0; i < 16; ++i) {
dicode1[(unsigned)(dichar1[i])] = i * 8 + 1;
}
for (i = 0; i < 8; ++i) {
dicode2[(unsigned)(dichar2[i])] = i + 1;
}
}
}
static void
opendatabase(void)
{
if ((symrefs = vpopen(reffile, O_RDONLY)) == -1) {
cannotopen(reffile);
myexit(1);
}
blocknumber = -1;
if (invertedindex == YES &&
invopen(&invcontrol, invname, invpost, INVAVAIL) == -1) {
askforreturn();
invertedindex = NO;
}
}
static void
closedatabase(void)
{
(void) close(symrefs);
if (invertedindex == YES) {
invclose(&invcontrol);
nsrcoffset = 0;
npostings = 0;
}
}
void
rebuild(void)
{
closedatabase();
build();
opendatabase();
if (refsfound != NULL) {
(void) fclose(refsfound);
refsfound = NULL;
}
*lastfilepath = '\0';
}
static void
build(void)
{
int i;
FILE *oldrefs;
time_t reftime;
char *file;
char *oldfile;
char newdir[PATHLEN + 1];
char olddir[PATHLEN + 1];
char oldname[PATHLEN + 1];
int oldnum;
struct stat statstruct;
int firstfile;
int lastfile;
int built = 0;
int copied = 0;
BOOL interactive = YES;
(void) strcpy(newdir, currentdir);
if (strcmp(currentdir, home) == 0) {
(void) strcpy(newdir, "$HOME");
} else if (strncmp(currentdir, home, strlen(home)) == 0) {
(void) sprintf(newdir, "$HOME%s", currentdir + strlen(home));
}
qsort((char *)srcfiles, (unsigned)nsrcfiles, sizeof (char *), compare);
if ((oldrefs = vpfopen(reffile, "r")) != NULL && unconditional == NO &&
fscanf(oldrefs, "cscope %d %s", &fileversion, olddir) == 2 &&
(strcmp(olddir, currentdir) == 0 ||
strcmp(olddir, newdir) == 0)) {
(void) fstat(fileno(oldrefs), &statstruct);
reftime = statstruct.st_mtime;
if (fileversion >= 8) {
BOOL oldcompress = YES;
BOOL oldinvertedindex = NO;
BOOL oldtruncatesyms = NO;
int c;
for (;;) {
while ((c = getc(oldrefs)) == ' ') {
}
if (c != '-') {
(void) ungetc(c, oldrefs);
break;
}
switch (c = getc(oldrefs)) {
case 'c':
oldcompress = NO;
break;
case 'q':
oldinvertedindex = YES;
(void) fscanf(oldrefs,
"%ld", &totalterms);
break;
case 'T':
oldtruncatesyms = YES;
break;
}
}
if (oldcompress != compress ||
oldtruncatesyms != truncatesyms) {
(void) fprintf(stderr,
"cscope: -c or -T option mismatch between "
"command line and old symbol database\n");
goto force;
}
if (oldinvertedindex != invertedindex) {
(void) fprintf(stderr,
"cscope: -q option mismatch between "
"command line and old symbol database\n");
if (invertedindex == NO) {
removeindex();
}
goto outofdate;
}
if (fscanf(oldrefs, "%ld", &traileroffset) != 1 ||
fseek(oldrefs, traileroffset, 0) == -1) {
(void) fprintf(stderr,
"cscope: incorrect symbol database file "
"format\n");
goto force;
}
}
if (fileschanged == YES) {
goto outofdate;
}
if (fileversion >= 13 &&
samelist(oldrefs, vpdirs, vpndirs) == NO) {
goto outofdate;
}
if (samelist(oldrefs, srcdirs, nsrcdirs) == NO ||
samelist(oldrefs, incdirs, nincdirs) == NO ||
fscanf(oldrefs, "%d", &oldnum) != 1 ||
fileversion >= 9 && fscanf(oldrefs, "%*s") != 0) {
goto outofdate;
}
for (i = 0; i < nsrcfiles; ++i) {
if (fscanf(oldrefs, "%s", oldname) != 1 ||
strnotequal(oldname, srcfiles[i]) ||
vpstat(srcfiles[i], &statstruct) != 0 ||
statstruct.st_mtime > reftime) {
goto outofdate;
}
}
while (i++ < oldnum && fscanf(oldrefs, "%s", oldname) == 1) {
addsrcfile(oldname);
}
(void) fclose(oldrefs);
return;
outofdate:
if (fileversion != FILEVERSION) {
(void) fprintf(stderr,
"cscope: converting to new symbol database file "
"format\n");
goto force;
}
if ((symrefs = vpopen(reffile, O_RDONLY)) == -1) {
cannotopen(reffile);
myexit(1);
}
blocknumber = -1;
(void) readblock();
(void) scanpast('\t');
oldfile = getoldfile();
} else {
force:
reftime = 0;
oldfile = NULL;
}
if ((newrefs = fopen(newreffile, "w")) == NULL) {
cannotopen(newreffile);
myexit(1);
}
if (invertedindex == YES && (postings = fopen(temp1, "w")) == NULL) {
cannotopen(temp1);
cannotindex();
}
(void) fprintf(stderr, "cscope: building symbol database\n");
putheader(newdir);
fileversion = FILEVERSION;
if (buildonly == YES && !isatty(0)) {
interactive = NO;
} else {
initprogress();
}
dbputc('\t');
firstfile = 0;
lastfile = nsrcfiles;
if (invertedindex == YES) {
srcoffset = mymalloc((nsrcfiles + 1) * sizeof (long));
}
for (;;) {
for (fileindex = firstfile; fileindex < lastfile; ++fileindex) {
if (interactive == YES && fileindex % 10 == 0) {
if (copied == 0) {
progress("%ld files built",
(long)built, 0L);
} else {
progress("%ld files built, %ld "
"files copied", (long)built,
(long)copied);
}
}
file = srcfiles[fileindex];
while (oldfile != NULL && strcmp(file, oldfile) > 0) {
oldfile = getoldfile();
}
if (oldfile == NULL || strcmp(file, oldfile) < 0) {
crossref(file);
++built;
} else if (vpstat(file, &statstruct) == 0 &&
statstruct.st_mtime > reftime) {
crossref(file);
++built;
oldfile = getoldfile();
} else {
putfilename(file);
if (invertedindex == YES) {
copyinverted();
} else {
copydata();
}
++copied;
oldfile = getoldfile();
}
}
if (lastfile == nsrcfiles) {
break;
}
firstfile = lastfile;
lastfile = nsrcfiles;
if (invertedindex == YES) {
srcoffset = myrealloc(srcoffset,
(nsrcfiles + 1) * sizeof (long));
}
qsort((char *)&srcfiles[firstfile],
(unsigned)(lastfile - firstfile), sizeof (char *), compare);
}
putfilename("");
dbputc('\n');
traileroffset = dboffset;
putlist(vpdirs, vpndirs);
putlist(srcdirs, nsrcdirs);
putlist(incdirs, nincdirs);
putlist(srcfiles, nsrcfiles);
if (fflush(newrefs) == EOF) {
cannotwrite(newreffile);
}
if (invertedindex == YES) {
char sortcommand[PATHLEN + 1];
if (fflush(postings) == EOF) {
cannotwrite(temp1);
}
(void) fstat(fileno(postings), &statstruct);
(void) fprintf(stderr,
"cscope: building symbol index: temporary file size is "
"%ld bytes\n", statstruct.st_size);
(void) fclose(postings);
(void) sprintf(sortcommand, "LC_ALL=C sort %s", temp1);
if ((postings = popen(sortcommand, "r")) == NULL) {
(void) fprintf(stderr,
"cscope: cannot open pipe to sort command\n");
cannotindex();
} else {
if ((totalterms = invmake(newinvname, newinvpost,
postings)) > 0) {
movefile(newinvname, invname);
movefile(newinvpost, invpost);
} else {
cannotindex();
}
(void) pclose(postings);
}
(void) unlink(temp1);
(void) free(srcoffset);
(void) fprintf(stderr,
"cscope: index has %ld references to %ld symbols\n",
npostings, totalterms);
}
rewind(newrefs);
putheader(newdir);
(void) fclose(newrefs);
if (symrefs >= 0) {
(void) close(symrefs);
}
if (oldrefs != NULL) {
(void) fclose(oldrefs);
}
movefile(newreffile, reffile);
}
static int
compare(const void *s1, const void *s2)
{
return (strcmp((char *)s1, (char *)s2));
}
static char *
getoldfile(void)
{
static char file[PATHLEN + 1];
if (blockp != NULL) {
do {
if (*blockp == NEWFILE) {
skiprefchar();
getstring(file);
if (file[0] != '\0') {
return (file);
}
return (NULL);
}
} while (scanpast('\t') != NULL);
}
return (NULL);
}
static void
putheader(char *dir)
{
dboffset = fprintf(newrefs, "cscope %d %s", FILEVERSION, dir);
if (compress == NO) {
dboffset += fprintf(newrefs, " -c");
}
if (invertedindex == YES) {
dboffset += fprintf(newrefs, " -q %.10ld", totalterms);
} else {
dboffset += fprintf(newrefs, " ");
}
if (truncatesyms == YES) {
dboffset += fprintf(newrefs, " -T");
}
dbfprintf(newrefs, " %.10ld\n", traileroffset);
}
static void
putlist(char **names, int count)
{
int i, size = 0;
(void) fprintf(newrefs, "%d\n", count);
if (names == srcfiles) {
for (i = 0; i < count; ++i) {
size += strlen(names[i]) + 1;
}
(void) fprintf(newrefs, "%d\n", size);
}
for (i = 0; i < count; ++i) {
if (fputs(names[i], newrefs) == EOF ||
putc('\n', newrefs) == EOF) {
cannotwrite(newreffile);
}
}
}
static BOOL
samelist(FILE *oldrefs, char **names, int count)
{
char oldname[PATHLEN + 1];
int oldcount;
int i;
if (fscanf(oldrefs, "%d", &oldcount) != 1 ||
oldcount != count) {
return (NO);
}
for (i = 0; i < count; ++i) {
if (fscanf(oldrefs, "%s", oldname) != 1 ||
strnotequal(oldname, names[i])) {
return (NO);
}
}
return (YES);
}
static void
skiplist(FILE *oldrefs)
{
int i;
if (fscanf(oldrefs, "%d", &i) != 1) {
(void) fprintf(stderr,
"cscope: cannot read list size from file %s\n", reffile);
exit(1);
}
while (--i >= 0) {
if (fscanf(oldrefs, "%*s") != 0) {
(void) fprintf(stderr,
"cscope: cannot read list name from file %s\n",
reffile);
exit(1);
}
}
}
static void
copydata(void)
{
char symbol[PATLEN + 1];
char *cp;
setmark('\t');
cp = blockp;
for (;;) {
do {
while (*cp != '\t') {
dbputc(*cp++);
}
} while (*++cp == '\0' && (cp = readblock()) != NULL);
dbputc('\t');
if (*(cp + 1) == '\0') {
cp = readblock();
}
if (cp == NULL || *cp == NEWFILE) {
break;
}
if (*cp == INCLUDE) {
blockp = cp;
putinclude(symbol);
putstring(symbol);
setmark('\t');
cp = blockp;
}
}
blockp = cp;
}
static void
copyinverted(void)
{
char *cp;
int c;
int type;
char symbol[PATLEN + 1];
cp = blockp;
for (;;) {
setmark('\n');
do {
while (*cp != '\n') {
dbputc(*cp++);
}
} while (*++cp == '\0' && (cp = readblock()) != NULL);
dbputc('\n');
if (*(cp + 1) == '\0') {
cp = readblock();
}
if (cp == NULL) {
break;
}
switch (*cp) {
case '\n':
lineoffset = dboffset + 1;
continue;
case '\t':
dbputc('\t');
blockp = cp;
type = getrefchar();
switch (type) {
case NEWFILE:
return;
case INCLUDE:
putinclude(symbol);
goto output;
}
dbputc(type);
skiprefchar();
getstring(symbol);
goto output;
}
c = *cp;
if (c & 0200) {
c = dichar1[(c & 0177) / 8];
}
if (isalpha(c) || c == '_') {
blockp = cp;
getstring(symbol);
type = ' ';
output:
putposting(symbol, type);
putstring(symbol);
if (blockp == NULL) {
return;
}
cp = blockp;
}
}
blockp = cp;
}
static void
putinclude(char *s)
{
dbputc(INCLUDE);
skiprefchar();
getstring(s);
incfile(s + 1, *s);
}
static void
movefile(char *new, char *old)
{
(void) unlink(old);
if (link(new, old) == -1) {
(void) perror("cscope");
(void) fprintf(stderr,
"cscope: cannot link file %s to file %s\n", new, old);
myexit(1);
}
if (unlink(new) == -1) {
(void) perror("cscope");
(void) fprintf(stderr, "cscope: cannot unlink file %s\n", new);
errorsfound = YES;
}
}
void
entercurses(void)
{
incurses = YES;
(void) nonl();
(void) cbreak();
(void) noecho();
(void) clear();
initmouse();
drawscrollbar(topline, nextline, totallines);
atfield();
}
void
exitcurses(void)
{
(void) move(LINES - 1, 0);
(void) clrtoeol();
(void) refresh();
(void) endwin();
incurses = NO;
cleanupmouse();
(void) fflush(stdout);
}
static void
timedout(int sig)
{
if (childpid) {
closedatabase();
noacttimeout = YES;
return;
}
exitcurses();
(void) fprintf(stderr, "cscope: no activity for %d hours--exiting\n",
noacttime / 3600);
myexit(sig);
}
void
myexit(int sig)
{
(void) signal(SIGHUP, SIG_IGN);
if (temp1[0] != '\0') {
(void) unlink(temp1);
(void) unlink(temp2);
}
if (incurses == YES) {
exitcurses();
}
if (sig == SIGQUIT) {
(void) abort();
}
exit(sig);
}