/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* cscope - interactive C symbol cross-reference
*
* main functions
*/
#include <curses.h> /* stdscr and TRUE */
#include <fcntl.h> /* O_RDONLY */
#include <unistd.h> /* O_RDONLY */
#include <unistd.h> /* O_RDONLY */
#include <libgen.h> /* O_RDONLY */
#include "global.h"
#include "version.h" /* FILEVERSION and FIXVERSION */
#include "vp.h" /* vpdirs and vpndirs */
/* defaults for unset environment variables */
/*
* note: these digraph character frequencies were calculated from possible
* printable digraphs in the cross-reference for the C compiler
*/
/* using the above as first chars */
/* database view path */
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 char *getoldfile(void);
static void copydata(void);
static void copyinverted(void);
static void putinclude(char *s);
int
{
char *s;
int c, i;
/* save the command name for messages */
/* get the current directory for build() and line-oriented P command */
"cscope: warning: cannot get current directory name\n");
}
/* initialize any view path; (saves time since currendir is known) */
/* directories (including current) in database view path */
/* the first source directory is the current directory */
sourcedir(".");
/* read the environment */
/* increment nesting depth */
/* parse the environment option string */
envc = 1;
while (s != NULL) {
}
/* set the environment options */
}
/* set the command line options */
/* create the temporary file names */
/* if running in the foreground */
/* cleanup on the interrupt and quit signals */
}
/* cleanup on the hangup signal */
/* if the database path is relative and it can't be created */
/* if the database may not be up-to-date or can't be read */
/* put it in the home directory */
"cscope: symbol database will be %s\n", reffile);
}
}
/* if the cross-reference is to be considered up-to-date */
if (isuptodate == YES) {
exit(1);
}
/*
* get the crossref file version but skip the current
* directory
*/
"cscope: cannot read file version from file %s\n",
reffile);
exit(1);
}
if (fileversion >= 8) {
/* override these command line options */
invertedindex = NO;
/* see if there are options in the database */
for (;;) {
/* no -q leaves multiple blanks */
;
}
if (c != '-') {
break;
}
case 'c': /* ASCII characters only */
break;
case 'q': /* quick search */
invertedindex = YES;
"%ld", &totalterms);
break;
case 'T':
/* truncate symbols to 8 characters */
dbtruncated = YES;
truncatesyms = YES;
break;
}
}
initcompress();
/* seek to the trailer */
"cscope: cannot read trailer offset from "
"file %s\n", reffile);
exit(1);
}
"cscope: cannot seek to trailer in "
"file %s\n", reffile);
exit(1);
}
}
/*
* read the view path for use in converting relative paths to
* full paths
*
* note: don't overwrite vp[n]dirs because this can cause
* the wrong database index files to be found in the viewpath
*/
if (fileversion >= 13) {
"cscope: cannot read view path size from "
"file %s\n", reffile);
exit(1);
}
if (dbvpndirs > 0) {
dbvpndirs * sizeof (char *));
for (i = 0; i < dbvpndirs; ++i) {
"cscope: cannot read view "
"path from file %s\n",
reffile);
exit(1);
}
}
}
}
/* skip the source and include directory lists */
/* get the number of source files */
"cscope: cannot read source file size from "
"file %s\n", reffile);
exit(1);
}
/* get the source file list */
if (fileversion >= 9) {
/* allocate the string space */
"cscope: cannot read string space size "
"from file %s\n", reffile);
exit(1);
}
/* read the strings */
"cscope: cannot read source file names "
"from file %s\n", reffile);
exit(1);
}
/* change newlines to nulls */
for (i = 0; i < nsrcfiles; ++i) {
srcfiles[i] = s;
for (++s; *s != '\n'; ++s) {
;
}
*s = '\0';
++s;
}
/* if there is a file of source file names */
/* read any -p option from it */
*path == '-') {
i = path[1];
if (*s == '\0') {
/* if "-I path" */
"%s", path);
s = path;
}
switch (i) {
case 'p':
/* file path components */
/* to display */
if (*s < '0' || *s > '9') {
"cscope: -p option "
"in file %s: "
"missing or "
"invalid numeric "
"value\n",
namefile);
}
dispcomponents = atoi(s);
}
}
}
} else {
for (i = 0; i < nsrcfiles; ++i) {
"cscope: cannot read source file "
"name from file %s\n", reffile);
exit(1);
}
}
}
} else {
/* get source directories from the environment */
sourcedir(s);
}
/* make the source file list */
makefilelist();
if (nsrcfiles == 0) {
"cscope: no source files found\n");
printusage();
exit(1);
}
/* get include directories from the environment */
includedir(s);
}
includedir("/usr/include");
/* initialize the C keyword table */
initsymtab();
/* create the file name(s) used for a new cross-reference */
*s = '\0';
++s;
/* build the cross-reference */
initcompress();
build();
exit(0);
}
}
opendatabase();
/*
* removing a database will not release the disk space if a cscope
* process has the file open, so a project may want unattended cscope
* processes to exit overnight, including their subshells and editors
*/
if (noacttime) {
}
/*
* if using the line oriented user interface so cscope can be a
* subprocess to emacs or samuel
*/
(void) putchar(c);
}
}
}
myexit(0);
}
for (;;) {
if (noacttime) {
}
(void) printf(">> ");
myexit(0);
}
/* remove any trailing newline character */
*s = '\0';
}
switch (*buf) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': /* samuel only */
(void) search();
(void) putchar(c);
}
break;
case 'c': /* toggle caseless mode */
case ctrl('C'):
} else {
}
break;
case 'r': /* rebuild database cscope style */
case ctrl('R'):
freefilelist();
makefilelist();
/* FALLTHROUGH */
case 'R': /* rebuild database samuel style */
rebuild();
(void) putchar('\n');
break;
case 'C': /* clear file names */
freefilelist();
(void) putchar('\n');
break;
case 'F': /* add a file name */
}
(void) putchar('\n');
break;
case 'P': /* print the path to the files */
if (prependpath != NULL) {
(void) puts(prependpath);
} else {
(void) puts(currentdir);
}
break;
case 'q': /* quit */
case ctrl('D'):
case ctrl('Z'):
myexit(0);
default:
"cscope: unknown command '%s'\n", buf);
break;
}
}
/* NOTREACHED */
}
/* pause before clearing the screen if there have been error messages */
if (errorsfound == YES) {
errorsfound = NO;
askforreturn();
}
/* initialize the curses display package */
(void) initscr(); /* initialize the screen */
setfield(); /* set the initial cursor position */
entercurses();
dispinit(); /* initialize display parameters */
display(); /* display the version number and input fields */
/* do any optional search */
if (*pattern != '\0') {
atfield(); /* move to the input field */
display(); /* update the display */
/* read any symbol reference lines file */
display(); /* update the display */
}
for (;;) {
if (noacttime) {
}
atfield(); /* move to the input field */
/* exit if the quit command is entered */
c == ctrl('Z')) {
break;
}
/* execute the commmand, updating the display if necessary */
display();
}
}
/* cleanup and exit */
myexit(0);
/* NOTREACHED */
return (0);
}
static void
{
int c;
char *s;
/* look for an input field number */
if (isdigit(*s)) {
field = *s - '0';
if (*++s == '\0' && --argc > 0) {
s = *++argv;
}
"cscope: pattern too long, cannot "
"be > %d characters\n", PATLEN);
exit(1);
}
goto nextarg;
}
switch (*s) {
case '-': /* end of options */
--argc;
++argv;
goto lastarg;
case 'V': /* print the version number */
"%s: version %d%s\n", argv0,
exit(0);
/*NOTREACHED*/
case 'b': /* only build the cross-reference */
break;
case 'c': /* ASCII characters only in crossref */
break;
case 'C':
/* turn on caseless mode for symbol searches */
/* simulate egrep -i flag */
break;
case 'd': /* consider crossref up-to-date */
isuptodate = YES;
break;
case 'e': /* suppress ^E prompt between files */
editallprompt = NO;
break;
case 'L':
/* FALLTHROUGH */
case 'l':
break;
case 'o':
/* display OGS book and subsystem names */
break;
case 'q': /* quick search */
invertedindex = YES;
break;
case 'r': /* display as many lines as possible */
break;
case 'T': /* truncate symbols to 8 characters */
truncatesyms = YES;
break;
case 'u':
/* unconditionally build the cross-reference */
unconditional = YES;
break;
case 'U': /* assume some files have changed */
fileschanged = YES;
break;
case 'f': /* alternate cross-reference file */
case 'F': /* symbol reference lines file */
case 'i': /* file containing file names */
case 'I': /* #include file directory */
case 'p': /* file path components to display */
case 'P': /* prepend path to file names */
case 's': /* additional source file directory */
case 'S':
case 't': /* no activity timeout in hours */
c = *s;
if (*++s == '\0' && --argc > 0) {
s = *++argv;
}
if (*s == '\0') {
"%s: -%c option: missing or empty "
"value\n", argv0, c);
goto usage;
}
switch (c) {
case 'f':
/* alternate cross-reference file */
reffile = s;
/* System V has a 14 character limit */
if ((int)strlen(s) > 11) {
s[11] = '\0';
}
(void) strcpy(s, ".in");
(void) strcpy(s, ".po");
break;
case 'F':
/* symbol reference lines file */
reflines = s;
break;
case 'i': /* file containing file names */
namefile = s;
break;
case 'I': /* #include file directory */
includedir(s);
break;
case 'p':
/* file path components to display */
if (*s < '0' || *s > '9') {
"%s: -p option: missing "
"or invalid numeric "
"value\n", argv0);
goto usage;
}
dispcomponents = atoi(s);
break;
case 'P': /* prepend path to file names */
prependpath = s;
break;
case 's':
case 'S':
/* additional source directory */
sourcedir(s);
break;
case 't':
/* no activity timeout in hours */
if (*s < '1' || *s > '9') {
"%s: -t option: missing or "
"invalid numeric value\n",
argv0);
goto usage;
}
c = atoi(s);
if (c < MINHOURS) {
"cscope: minimum timeout "
"is %d hours\n", MINHOURS);
(void) sleep(3);
c = MINHOURS;
}
noacttime = c * 3600;
break;
}
goto nextarg;
default:
"%s: unknown option: -%c\n", argv0, *s);
printusage();
exit(1);
}
}
nextarg: continue;
}
/* save the file arguments */
}
static void
printusage(void)
{
"Usage: cscope [-bcdelLoqrtTuUV] [-f file] [-F file] [-i file] "
"[-I dir] [-s dir]\n");
" [-p number] [-P path] [-[0-8] pattern] "
"[source files]\n");
"-b Build the database only.\n");
"-c Use only ASCII characters in the database file, "
"that is,\n");
" do not compress the data.\n");
"-d Do not update the database.\n");
"-f \"file\" Use \"file\" as the database file name "
"instead of\n");
" the default (cscope.out).\n");
"-F \"file\" Read symbol reference lines from file, just\n");
/* BEGIN CSTYLED */
" like the \"<\" command.\n");
/* END CSTYLED */
"-i \"file\" Read any -I, -p, -q, and -T options and the\n");
" list of source files from \"file\" instead of the \n");
" default (cscope.files).\n");
"-I \"dir\" Look in \"dir\" for #include files.\n");
"-q Build an inverted index for quick symbol seaching.\n");
"-s \"dir\" Look in \"dir\" for additional source files.\n");
}
static void
removeindex(void)
{
}
static void
cannotindex(void)
{
"cscope: cannot create inverted index; ignoring -q option\n");
invertedindex = NO;
errorsfound = YES;
(void) unlink(newinvname);
(void) unlink(newinvpost);
removeindex(); /* remove any existing index to prevent confusion */
}
void
{
}
void
{
}
/* set up the digraph character tables for text compression */
static void
initcompress(void)
{
int i;
for (i = 0; i < 16; ++i) {
}
for (i = 0; i < 8; ++i) {
}
}
}
/* open the database */
static void
opendatabase(void)
{
myexit(1);
}
/* open any inverted index */
if (invertedindex == YES &&
askforreturn(); /* so user sees message */
invertedindex = NO;
}
}
/* close the database */
static void
closedatabase(void)
{
if (invertedindex == YES) {
nsrcoffset = 0;
npostings = 0;
}
}
/* rebuild the database */
void
rebuild(void)
{
build();
opendatabase();
/* revert to the initial display */
}
}
/* build the cross-reference */
static void
build(void)
{
int i;
/*
* normalize the current directory relative to the home directory so
* the cross-reference is not rebuilt when the user's login is moved
*/
}
/* sort the source file names (needed for rebuilding) */
/*
* if there is an old cross-reference and its current directory
* matches or this is an unconditional build
*/
/* get the cross-reference file's modification time */
if (fileversion >= 8) {
int c;
/* see if there are options in the database */
for (;;) {
}
if (c != '-') {
break;
}
case 'c': /* ASCII characters only */
oldcompress = NO;
break;
case 'q': /* quick search */
"%ld", &totalterms);
break;
case 'T':
/* truncate symbols to 8 characters */
break;
}
}
/* check the old and new option settings */
if (oldcompress != compress ||
oldtruncatesyms != truncatesyms) {
"cscope: -c or -T option mismatch between "
"command line and old symbol database\n");
goto force;
}
if (oldinvertedindex != invertedindex) {
"cscope: -q option mismatch between "
"command line and old symbol database\n");
if (invertedindex == NO) {
removeindex();
}
goto outofdate;
}
/* seek to the trailer */
"cscope: incorrect symbol database file "
"format\n");
goto force;
}
}
/* if assuming that some files have changed */
if (fileschanged == YES) {
goto outofdate;
}
/* see if the view path is the same */
if (fileversion >= 13 &&
goto outofdate;
}
/* see if the directory lists are the same */
/* skip the string space size */
goto outofdate;
}
/*
* see if the list of source files is the same and
* none have been changed up to the included files
*/
for (i = 0; i < nsrcfiles; ++i) {
goto outofdate;
}
}
/* the old cross-reference is up-to-date */
/* so get the list of included files */
}
return;
/* if the database format has changed, rebuild it all */
if (fileversion != FILEVERSION) {
"cscope: converting to new symbol database file "
"format\n");
goto force;
}
/* reopen the old cross-reference file for fast scanning */
myexit(1);
}
/* get the first file name in the old cross-reference */
blocknumber = -1;
(void) readblock(); /* read the first cross-ref block */
oldfile = getoldfile();
} else { /* force cross-referencing of all the source files */
reftime = 0;
}
/* open the new cross-reference file */
myexit(1);
}
cannotindex();
}
interactive = NO;
} else {
initprogress();
}
/* output the leading tab expected by crossref() */
dbputc('\t');
/*
* make passes through the source file list until the last level of
* included files is processed
*/
firstfile = 0;
if (invertedindex == YES) {
}
for (;;) {
/* get the next source file name */
/* display the progress about every three seconds */
if (copied == 0) {
progress("%ld files built",
(long)built, 0L);
} else {
progress("%ld files built, %ld "
"files copied", (long)built,
(long)copied);
}
}
/* if the old file has been deleted get the next one */
oldfile = getoldfile();
}
/*
* if there isn't an old database or this is
* a new file
*/
++built;
/* if this file was modified */
++built;
/*
* skip its old crossref so modifying the last
* source file does not cause all included files
* to be built. Unfortunately a new file that
* is alphabetically last will cause all
* included files to be built, but this is
* less likely
*/
oldfile = getoldfile();
} else { /* copy its cross-reference */
if (invertedindex == YES) {
copyinverted();
} else {
copydata();
}
++copied;
oldfile = getoldfile();
}
}
/* see if any included files were found */
break;
}
if (invertedindex == YES) {
(nsrcfiles + 1) * sizeof (long));
}
/* sort the included file names */
}
/* add a null file name to the trailing tab */
putfilename("");
dbputc('\n');
/* get the file trailer offset */
/*
* output the view path and source and include directory and
* file lists
*/
/* rewind doesn't check for write failure */
/* NOTREACHED */
}
/* create the inverted index if requested */
if (invertedindex == YES) {
/* NOTREACHED */
}
"cscope: building symbol index: temporary file size is "
/*
* sort -T is broken until it is fixed we don't have too much choice
*/
/*
* (void) sprintf(sortcommand, "sort -y -T %s %s", tmpdir, temp1);
*/
"cscope: cannot open pipe to sort command\n");
cannotindex();
} else {
postings)) > 0) {
} else {
cannotindex();
}
}
"cscope: index has %ld references to %ld symbols\n",
}
/* rewrite the header with the trailer offset and final option list */
/* close the old database file */
if (symrefs >= 0) {
}
}
/* replace it with the new database file */
}
/* string comparison function for qsort */
static int
{
}
/* get the next file name in the old cross-reference */
static char *
getoldfile(void)
{
do {
skiprefchar();
if (file[0] != '\0') {
/* if not end-of-crossref */
return (file);
}
return (NULL);
}
}
return (NULL);
}
/*
* output the cscope version, current directory, database format options, and
* the database trailer offset
*/
static void
{
}
if (invertedindex == YES) {
} else {
/*
* leave space so if the header is overwritten without -q
* because writing the inverted index failed, the header is
* the same length
*/
}
if (truncatesyms == YES) {
}
}
/* put the name list into the cross-reference file */
static void
{
int i, size = 0;
/* calculate the string space needed */
for (i = 0; i < count; ++i) {
}
}
for (i = 0; i < count; ++i) {
/* NOTREACHED */
}
}
}
/* see if the name list is the same in the cross-reference file */
static BOOL
{
int oldcount;
int i;
/* see if the number of names is the same */
return (NO);
}
/* see if the name list is the same */
for (i = 0; i < count; ++i) {
return (NO);
}
}
return (YES);
}
/* skip the list in the cross-reference file */
static void
{
int i;
"cscope: cannot read list size from file %s\n", reffile);
exit(1);
}
while (--i >= 0) {
"cscope: cannot read list name from file %s\n",
reffile);
exit(1);
}
}
}
/* copy this file's symbol data */
static void
copydata(void)
{
char *cp;
setmark('\t');
for (;;) {
/* copy up to the next \t */
do { /* innermost loop optimized to only one test */
while (*cp != '\t') {
}
/* get the next character */
}
/* exit if at the end of this file's data */
break;
}
/* look for an #included file */
setmark('\t');
}
}
}
/* copy this file's symbol data and output the inverted index postings */
static void
copyinverted(void)
{
char *cp;
int c;
/* note: this code was expanded in-line for speed */
/* while (scanpast('\n') != NULL) { */
/* other macros were replaced by code using cp instead of blockp */
for (;;) {
setmark('\n');
do { /* innermost loop optimized to only one test */
while (*cp != '\n') {
}
/* get the next character */
}
/* exit if at the end of this file's data */
break;
}
switch (*cp) {
case '\n':
continue;
case '\t':
dbputc('\t');
type = getrefchar();
switch (type) {
case NEWFILE: /* file name */
return;
case INCLUDE: /* #included file */
goto output;
}
skiprefchar();
goto output;
}
c = *cp;
if (c & 0200) { /* digraph char? */
}
/* if this is a symbol */
if (isalpha(c) || c == '_') {
type = ' ';
return;
}
}
}
}
/* process the #included file in the old database */
static void
putinclude(char *s)
{
skiprefchar();
getstring(s);
incfile(s + 1, *s);
}
/* replace the old file with the new file */
static void
{
(void) perror("cscope");
myexit(1);
}
(void) perror("cscope");
errorsfound = YES;
}
}
/* enter curses mode */
void
entercurses(void)
{
(void) nonl(); /* don't translate an output \n to \n\r */
(void) cbreak(); /* single character input */
(void) noecho(); /* don't echo input characters */
(void) clear(); /* clear the screen */
initmouse(); /* initialize any mouse interface */
atfield();
}
/* exit curses mode */
void
exitcurses(void)
{
/* clear the bottom line */
(void) clrtoeol();
(void) refresh();
/* exit curses and restore the terminal modes */
(void) endwin();
/* restore the mouse */
cleanupmouse();
}
/* no activity timeout occurred */
static void
{
/* if there is a child process, don't exit until it does */
if (childpid) {
noacttimeout = YES;
return;
}
exitcurses();
noacttime / 3600);
}
/* cleanup and exit */
void
{
/* deleted layer causes multiple signals */
/* remove any temporary files */
if (temp1[0] != '\0') {
}
/* restore the terminal to its original mode */
exitcurses();
}
/* dump core for debugging on the quit signal */
(void) abort();
}
}