📄 main.c
字号:
/*=========================================================================== Copyright (c) 1998-2000, The Santa Cruz Operation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: *Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. *Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. *Neither name of The Santa Cruz Operation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. =========================================================================*//* cscope - interactive C symbol cross-reference * * main functions */#include "global.h"#include "build.h"#include "vp.h"#include "version.h" /* FILEVERSION and FIXVERSION */#include "scanner.h" #include <stdlib.h> /* atoi */#if defined(USE_NCURSES) && !defined(RENAMED_NCURSES)#include <ncurses.h>#else#include <curses.h>#endif#include <sys/types.h> /* needed by stat.h */#include <sys/stat.h> /* stat *//* defaults for unset environment variables */#define EDITOR "vi"#define HOME "/" /* no $HOME --> use root directory */#define SHELL "sh"#define LINEFLAG "+%s" /* default: used by vi and emacs */#define TMPDIR "/tmp"#ifndef DFLT_INCDIR#define DFLT_INCDIR "/usr/include"#endifstatic char const rcsid[] = "$Id: main.c,v 1.31 2003/08/14 14:36:17 broeker Exp $";/* note: these digraph character frequencies were calculated from possible printable digraphs in the cross-reference for the C compiler */char dichar1[] = " teisaprnl(of)=c"; /* 16 most frequent first chars */char dichar2[] = " tnerpla"; /* 8 most frequent second chars using the above as first chars */char dicode1[256]; /* digraph first character code */char dicode2[256]; /* digraph second character code */char *editor, *shell, *lineflag; /* environment variables */char *home; /* Home directory */BOOL lineflagafterfile;char *argv0; /* command name */BOOL compress = YES; /* compress the characters in the crossref */BOOL dbtruncated; /* database symbols are truncated to 8 chars */int dispcomponents = 1; /* file path components to display */#if CCSBOOL displayversion; /* display the C Compilation System version */#endifBOOL editallprompt = YES; /* prompt between editing files */int fileargc; /* file argument count */char **fileargv; /* file argument values */int fileversion; /* cross-reference file version */BOOL incurses = NO; /* in curses */BOOL invertedindex; /* the database has an inverted index */BOOL isuptodate; /* consider the crossref up-to-date */BOOL kernelmode; /* don't use DFLT_INCDIR - bad for kernels */BOOL linemode = NO; /* use line oriented user interface */BOOL verbosemode = NO; /* print extra information on line mode */BOOL recurse_dir = NO; /* recurse dirs when searching for src files */char *namefile; /* file of file names */BOOL ogs; /* display OGS book and subsystem names */char *prependpath; /* prepend path to file names */FILE *refsfound; /* references found file */#if 0 /* HBB 20010705: flag no longer used */BOOL select_large = NO; /* enable more than 9 select lines */#endifchar temp1[PATHLEN + 1]; /* temporary file name */char temp2[PATHLEN + 1]; /* temporary file name */long totalterms; /* total inverted index terms */BOOL trun_syms; /* truncate symbols to 8 characters */char tempstring[8192]; /* use this as a buffer, instead of 'yytext', * which had better be left alone */char *tmpdir; /* temporary directory */static BOOL onesearch; /* one search only in line mode */static char *reflines; /* symbol reference lines file *//* Internal prototypes: */static void initcompress(void);static void longusage(void);static void skiplist(FILE *oldrefs);static void usage(void);#ifdef HAVE_FIXKEYPADvoid fixkeypad();#endifintmain(int argc, char **argv){ FILE *names; /* name file pointer */ int oldnum; /* number in old cross-ref */ char path[PATHLEN + 1]; /* file path */ FILE *oldrefs; /* old cross-reference file */ char *s; int c, i; pid_t pid; struct stat stat_buf; yyin = stdin; yyout = stdout; /* save the command name for messages */ argv0 = argv[0]; /* set the options */ while (--argc > 0 && (*++argv)[0] == '-') { /* HBB 20030814: add GNU-style --help and --version * options */ if (strequal(argv[0], "--help") || strequal(argv[0], "-h")) { longusage(); myexit(0); } if (strequal(argv[0], "--version") || strequal(argv[0], "-V")) {#if CCS displayversion = YES;#else fprintf(stderr, "%s: version %d%s\n", argv0, FILEVERSION, FIXVERSION); myexit(0);#endif } for (s = argv[0] + 1; *s != '\0'; s++) { /* look for an input field number */ if (isdigit((unsigned char)*s)) { field = *s - '0'; if (field > 8) { field = 8; } if (*++s == '\0' && --argc > 0) { s = *++argv; } if (strlen(s) > PATLEN) { (void) fprintf(stderr, "cscope: pattern too long, cannot be > %d characters\n", PATLEN); myexit(1); } (void) strcpy(pattern, s); goto nextarg; } switch (*s) { case '-': /* end of options */ --argc; ++argv; goto lastarg; case 'b': /* only build the cross-reference */ buildonly = YES; linemode = YES; break; case 'c': /* ASCII characters only in crossref */ compress = NO; break; case 'C': /* turn on caseless mode for symbol searches */ caseless = YES; egrepcaseless(caseless); /* 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 'k': /* ignore DFLT_INCDIR */ kernelmode = YES; break; case 'L': onesearch = YES; /* FALLTHROUGH */ case 'l': linemode = YES; break; case 'v': verbosemode = YES; break; case 'o': /* display OGS book and subsystem names */ ogs = YES; break; case 'q': /* quick search */ invertedindex = YES; break; case 'T': /* truncate symbols to 8 characters */ trun_syms = YES; break; case 'u': /* unconditionally build the cross-reference */ unconditional = YES; break; case 'U': /* assume some files have changed */ fileschanged = YES; break; case 'R': recurse_dir = 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': 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': /* alternate cross-reference file */ reffile = s; (void) strcpy(path, s);#ifdef SHORT_NAMES_ONLY /* System V has a 14 character limit */ s = mybasename(path); if (strlen(s) > 11) { s[11] = '\0'; }#endif s = path + strlen(path); (void) strcpy(s, ".in"); invname = stralloc(path); (void) strcpy(s, ".po"); invpost = stralloc(path); 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' ) { (void) fprintf(stderr, "%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': /* additional source directory */ case 'S': sourcedir(s); break; } goto nextarg;#if 0 /* HBB 20010705: unused, now! */ case 't': /* enable more than 9 select lines */ select_large = YES; break;#endif default: (void) fprintf(stderr, "%s: unknown option: -%c\n", argv0, *s); usage: (void) usage(); (void) fprintf(stderr, "Try the -h option for more information.\n"); myexit(1); } }nextarg: ; }lastarg: /* read the environment */ editor = mygetenv("EDITOR", EDITOR); editor = mygetenv("VIEWER", editor); /* use viewer if set */ editor = mygetenv("CSCOPE_EDITOR", editor); /* has last word */ home = mygetenv("HOME", HOME); shell = mygetenv("SHELL", SHELL); lineflag = mygetenv("CSCOPE_LINEFLAG", LINEFLAG); lineflagafterfile = getenv("CSCOPE_LINEFLAG_AFTER_FILE")?1:0; tmpdir = mygetenv("TMPDIR", TMPDIR); /* XXX remove if/when clearerr() in dir.c does the right thing. */ if (namefile && strcmp(namefile, "-") == 0 && !buildonly) { fprintf (stderr, "cscope: Must use -b if file list comes from stdin\n"); myexit(1); } /* make sure that tmpdir exists */ if (lstat (tmpdir, &stat_buf)) { fprintf (stderr, "cscope: Temporary directory %s does not exist or cannot be accessed\n", tmpdir); fprintf (stderr, "cscope: Please create the directory or set the environment variable\ncscope: TMPDIR to a valid directory\n"); myexit(1); } /* create the temporary file names */ pid = getpid(); (void) sprintf(temp1, "%s/cscope%d.1", tmpdir, pid); (void) sprintf(temp2, "%s/cscope%d.2", tmpdir, pid); /* if running in the foreground */ if (signal(SIGINT, SIG_IGN) != SIG_IGN) { /* cleanup on the interrupt and quit signals */ (void) signal(SIGINT, myexit); (void) signal(SIGQUIT, myexit); } /* cleanup on the hangup signal */ (void) signal(SIGHUP, myexit); /* if the database path is relative and it can't be created */ if (reffile[0] != '/' && access(".", WRITE) != 0) { /* put it in the home directory if the database may not be * up-to-date or doesn't exist in the relative directory, * so a database in the current directory will be * used instead of failing to open a non-existant database in * the home directory */ (void) sprintf(path, "%s/%s", home, reffile); if (isuptodate == NO || access(path, 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); } } if (linemode == NO) { (void) signal(SIGINT, SIG_IGN); /* ignore interrupts */ (void) signal(SIGPIPE, SIG_IGN);/* | command can cause pipe signal */ /* initialize the curses display package */ (void) initscr(); /* initialize the screen */ entercurses();#if TERMINFO (void) keypad(stdscr, TRUE); /* enable the keypad */#ifdef HAVE_FIXKEYPAD fixkeypad(); /* fix for getch() intermittently returning garbage */#endif#endif#if UNIXPC standend(); /* turn off reverse video */#endif dispinit(); /* initialize display parameters */ setfield(); /* set the initial cursor position */ postmsg(""); /* clear any build progress message */ display(); /* display the version number and input fields */ } /* if the cross-reference is to be considered up-to-date */ if (isuptodate == YES) { if ((oldrefs = vpfopen(reffile, "rb")) == NULL) { posterr("cscope: cannot open file %s\n", reffile); myexit(1); } /* get the crossref file version but skip the current directory */ if (fscanf(oldrefs, "cscope %d %*s", &fileversion) != 1) { posterr("cscope: cannot read file version from file %s\n", reffile); myexit(1); } if (fileversion >= 8) { /* override these command line options */ compress = YES; invertedindex = NO; /* see if there are options in the database */ for (;;) { (void) getc(oldrefs); /* skip the blank */ if ((c = getc(oldrefs)) != '-') { (void) ungetc(c, oldrefs); break; } switch (c = getc(oldrefs)) { case 'c': /* ASCII characters only */ compress = NO; break; case 'q': /* quick search */ invertedindex = YES; (void) fscanf(oldrefs, "%ld", &totalterms); break; case 'T': /* truncate symbols to 8 characters */ dbtruncated = YES; trun_syms = YES; break; } } initcompress(); seek_to_trailer(oldrefs);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -