summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 70b1eb963b01a64518c6f4d979567d8e18ac88be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/*===========================================================================
 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 "alloc.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 */
#include <signal.h>

/* 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"
#endif

static char const rcsid[] = "$Id: main.c,v 1.46 2009/04/10 13:39:23 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 CCS
BOOL	displayversion;		/* display the C Compilation System version */
#endif
BOOL	editallprompt = YES;	/* prompt between editing files */
unsigned 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 */
char	temp1[PATHLEN + 1];	/* temporary file name */
char	temp2[PATHLEN + 1];	/* temporary file name */
char	tempdirpv[PATHLEN + 1];	/* private temp directory */
long	totalterms;		/* total inverted index terms */
BOOL	trun_syms;		/* truncate symbols to 8 characters */
char	tempstring[TEMPSTRING_LEN + 1]; /* 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_FIXKEYPAD
void	fixkeypad();
#endif

#if defined(KEY_RESIZE) && !defined(__DJGPP__)
void 
sigwinch_handler(int sig, siginfo_t *info, void *unused)
{
    (void) sig;
    (void) info;
    (void) unused;
    if(incurses == YES)
        ungetch(KEY_RESIZE);
}
#endif

int
main(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;
    unsigned int i;
    pid_t pid;
    struct stat	stat_buf;
#if defined(KEY_RESIZE) && !defined(__DJGPP__)
    struct sigaction winch_action;
#endif
    mode_t orig_umask;
	
    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) {
		    postfatal("\
cscope: pattern too long, cannot be > %d characters\n", PATLEN);
		    /* NOTREACHED */
		}
		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') {
		    fprintf(stderr, "%s: -%c option: missing or empty value\n", 
			    argv0, c);
		    goto usage;
		}
		switch (c) {
		case 'f':	/* alternate cross-reference file */
		    reffile = s;
		    if (strlen(reffile) > sizeof(path) - 1) {
			  postfatal("\
cscope: reffile too long, cannot be > %d characters\n", sizeof(path) - 1);
			  /* NOTREACHED */
		    }
		    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);
		    strcpy(s, ".in");
		    invname = my_strdup(path);
		    strcpy(s, ".po");
		    invpost = my_strdup(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' ) {
			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;
	    default:
		fprintf(stderr, "%s: unknown option: -%c\n", argv0, 
			*s);
	    usage:
		usage();
		fprintf(stderr, "Try the -h option for more information.\n");
		myexit(1);
	    } /* switch(option letter) */
	} /* for(option) */
    nextarg:	
	;
    } /* while(argv) */

 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) {
	postfatal("cscope: Must use -b if file list comes from stdin\n");
	/* NOTREACHED */
    }

    /* 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\n\
cscope: TMPDIR to a valid directory\n");
	myexit(1);
    }

    /* create the temporary file names */
    orig_umask = umask(S_IRWXG|S_IRWXO);
    pid = getpid();
    snprintf(tempdirpv, sizeof(tempdirpv), "%s/cscope.%d", tmpdir, pid);
    if(mkdir(tempdirpv,S_IRWXU)) {
	fprintf(stderr, "\
cscope: Could not create private temp dir %s\n",
		tempdirpv);
	myexit(1);
    }
    umask(orig_umask);

    snprintf(temp1, sizeof(temp1), "%s/cscope.1", tempdirpv);
    snprintf(temp2, sizeof(temp2), "%s/cscope.2", tempdirpv);

    /* if running in the foreground */
    if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
	/* cleanup on the interrupt and quit signals */
	signal(SIGINT, myexit);
	signal(SIGQUIT, myexit);
    }
    /* cleanup on the hangup signal */
    signal(SIGHUP, myexit);

    /* ditto the TERM signal */
    signal(SIGTERM, 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
	 */
	snprintf(path, sizeof(path), "%s/%s", home, reffile);
	if (isuptodate == NO || access(path, READ) == 0) {
	    reffile = my_strdup(path);
	    snprintf(path, sizeof(path), "%s/%s", home, invname);
	    invname = my_strdup(path);
	    snprintf(path, sizeof(path), "%s/%s", home, invpost);
	    invpost = my_strdup(path);
	}
    }

    if (linemode == NO) {
	signal(SIGINT, SIG_IGN);	/* ignore interrupts */
	signal(SIGPIPE, SIG_IGN);/* | command can cause pipe signal */

#if defined(KEY_RESIZE) && !defined(__DJGPP__)
	winch_action.sa_sigaction = sigwinch_handler;
	sigemptyset(&winch_action.sa_mask);
	winch_action.sa_flags = SA_SIGINFO;
	sigaction(SIGWINCH,&winch_action,NULL);
#endif

	/* initialize the curses display package */
	initscr();	/* initialize the screen */
	entercurses();
#if TERMINFO
	keypad(stdscr, TRUE);	/* enable the keypad */
# ifdef HAVE_FIXKEYPAD
	fixkeypad();	/* fix for getch() intermittently returning garbage */
# endif
#endif /* TERMINFO */
#if UNIXPC
	standend();	/* turn off reverse video */
#endif
	dispinit();	/* initialize display parameters */
	setfield();	/* set the initial cursor position */
	clearmsg();	/* 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) {
	    postfatal("cscope: cannot open file %s\n", reffile);
	    /* NOTREACHED */
	}
	/* get the crossref file version but skip the current directory */
	if (fscanf(oldrefs, "cscope %d %*s", &fileversion) != 1) {
	    postfatal("cscope: cannot read file version from file %s\n", 
		      reffile);
	    /* NOTREACHED */
	}
	if (fileversion >= 8) {

	    /* override these command line options */
	    compress = YES;
	    invertedindex = NO;

	    /* see if there are options in the database */
	    for (;;) {
		getc(oldrefs);	/* skip the blank */
		if ((c = getc(oldrefs)) != '-') {
		    ungetc(c, oldrefs);
		    break;
		}
		switch (c = getc(oldrefs)) {
		case 'c':	/* ASCII characters only */
		    compress = NO;
		    break;
		case 'q':	/* quick search */
		    invertedindex = YES;
		    fscanf(oldrefs, "%ld", &totalterms);
		    break;
		case 'T':	/* truncate symbols to 8 characters */
		    dbtruncated = YES;
		    trun_syms = YES;
		    break;
		}
	    }
	    initcompress();
	    seek_to_trailer(oldrefs);
	}
	/* skip the source and include directory lists */
	skiplist(oldrefs);
	skiplist(oldrefs);

	/* get the number of source files */
	if (fscanf(oldrefs, "%lu", &nsrcfiles) != 1) {
	    postfatal("\
cscope: cannot read source file size from file %s\n", reffile);
	    /* NOTREACHED */
	}
	/* get the source file list */
	srcfiles = mymalloc(nsrcfiles * sizeof(char *));
	if (fileversion >= 9) {

	    /* allocate the string space */
	    if (fscanf(oldrefs, "%d", &oldnum) != 1) {
		postfatal("\
cscope: cannot read string space size from file %s\n", reffile);
		/* NOTREACHED */
	    }
	    s = mymalloc(oldnum);
	    getc(oldrefs);	/* skip the newline */
			
	    /* read the strings */
	    if (fread(s, oldnum, 1, oldrefs) != 1) {
		postfatal("\
cscope: cannot read source file names from file %s\n", reffile);
		/* NOTREACHED */
	    }
	    /* 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 */
	    if ((namefile != NULL && (names = vpfopen(namefile, "r")) != NULL)
		|| (names = vpfopen(NAMEFILE, "r")) != NULL) {
	
		/* read any -p option from it */
		while (fgets(path, sizeof(path), names) != NULL && *path == '-') {
		    i = path[1];
		    s = path + 2;		/* for "-Ipath" */
		    if (*s == '\0') {	/* if "-I path" */
			fgets(path, sizeof(path), names);
			s = path;
		    }
		    switch (i) {
		    case 'p':	/* file path components to display */
			if (*s < '0' || *s > '9') {
			    posterr("cscope: -p option in file %s: missing or invalid numeric value\n", 								namefile);

			}
			dispcomponents = atoi(s);
		    }
		}
		fclose(names);
	    }
	} else {
	    for (i = 0; i < nsrcfiles; ++i) {
		if (!fgets(path, sizeof(path), oldrefs) ) {
		    postfatal("\
cscope: cannot read source file name from file %s\n", 
			      reffile);
		    /* NOTREACHED */
		}
		srcfiles[i] = my_strdup(path);
	    }
	}
	fclose(oldrefs);
    } else {
	/* save the file arguments */
	fileargc = argc;
	fileargv = argv;
	
	/* get source directories from the environment */
	if ((s = getenv("SOURCEDIRS")) != NULL) {
	    sourcedir(s);
	}
	/* make the source file list */
	srcfiles = mymalloc(msrcfiles * sizeof(char *));
	makefilelist();
	if (nsrcfiles == 0) {
	    postfatal("cscope: no source files found\n");
	    /* NOTREACHED */
	}
	/* get include directories from the environment */
	if ((s = getenv("INCLUDEDIRS")) != NULL) {
	    includedir(s);
	}
	/* add /usr/include to the #include directory list,
	   but not in kernelmode... kernels tend not to use it. */
	if (kernelmode == NO) {
	    includedir(DFLT_INCDIR);
	}

	/* initialize the C keyword table */
	initsymtab();

	/* Tell build.c about the filenames to create: */
	setup_build_filenames(reffile);

	/* build the cross-reference */
	initcompress();
	if (linemode == NO || verbosemode == YES)    /* display if verbose as well */
	    postmsg("Building cross-reference...");    		    
	build();
	if (linemode == NO )
	    clearmsg();	/* clear any build progress message */
	if (buildonly == YES) {
	    myexit(0);
	}
    }
    opendatabase();

    /* if using the line oriented user interface so cscope can be a 
       subprocess to emacs or samuel */
    if (linemode == YES) {
	if (*Pattern != '\0') {		/* do any optional search */
	    if (search() == YES) {
		/* print the total number of lines in
		 * verbose mode */
		if (verbosemode == YES)
		    printf("cscope: %d lines\n",
			   totallines);

		while ((c = getc(refsfound)) != EOF)
		    putchar(c);
	    }
	}
	if (onesearch == YES)
	    myexit(0);
		
	for (;;) {
	    char buf[PATLEN + 2];
			
	    printf(">> ");
	    fflush(stdout);
	    if (fgets(buf, sizeof(buf), stdin) == NULL) {
		myexit(0);
	    }
	    /* remove any trailing newline character */
	    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':	/* samuel only */
		field = *buf - '0';
		strcpy(Pattern, buf + 1);
		search();
		printf("cscope: %d lines\n", totallines);
		while ((c = getc(refsfound)) != EOF) {
		    putchar(c);
		}
		break;

	    case 'c':	/* toggle caseless mode */
	    case ctrl('C'):
		if (caseless == NO) {
		    caseless = YES;
		} else {
		    caseless = NO;
		}
		egrepcaseless(caseless);
		break;

	    case 'r':	/* rebuild database cscope style */
	    case ctrl('R'):
		freefilelist();
		makefilelist();
		/* FALLTHROUGH */

	    case 'R':	/* rebuild database samuel style */
		rebuild();
		putchar('\n');
		break;

	    case 'C':	/* clear file names */
		freefilelist();
		putchar('\n');
		break;

	    case 'F':	/* add a file name */
		strcpy(path, buf + 1);
		if (infilelist(path) == NO &&
		    (s = inviewpath(path)) != NULL) {
		    addsrcfile(s);
		}
		putchar('\n');
		break;

	    case 'q':	/* quit */
	    case ctrl('D'):
	    case ctrl('Z'):
		myexit(0);

	    default:
		fprintf(stderr, "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();
    }
    /* do any optional search */
    if (*Pattern != '\0') {
	atfield();		/* move to the input field */
	command(ctrl('Y'));	/* search */
    } else if (reflines != NULL) {
	/* read any symbol reference lines file */
	readrefs(reflines);
    }
    display();		/* update the display */

    for (;;) {
	if (!selecting)
	    atfield();	/* move to the input field */

	/* exit if the quit command is entered */
	if ((c = mygetch()) == EOF || c == ctrl('D') || c == ctrl('Z')) {
	    break;
	}
	/* execute the commmand, updating the display if necessary */
	if (command(c) == YES) {
	    display();
	}

	if (selecting) {
	    move(displine[curdispline], 0);
	    refresh();
	}
    }
    /* cleanup and exit */
    myexit(0);
    /* NOTREACHED */
    return 0;		/* avoid warning... */
}

void
cannotopen(char *file)
{
    posterr("Cannot open file %s", file);
}

/* FIXME MTE - should use postfatal here */
void
cannotwrite(char *file)
{
    char	msg[MSGLEN + 1];

    snprintf(msg, sizeof(msg), "Removed file %s because write failed", file);

    myperror(msg);	/* display the reason */

    unlink(file);
    myexit(1);	/* calls exit(2), which closes files */
}


/* set up the digraph character tables for text compression */
static void
initcompress(void)
{
    int	i;
	
    if (compress == YES) {
	for (i = 0; i < 16; ++i) {
	    dicode1[(unsigned char) (dichar1[i])] = i * 8 + 1;
	}
	for (i = 0; i < 8; ++i) {
	    dicode2[(unsigned char) (dichar2[i])] = i + 1;
	}
    }
}

/* skip the list in the cross-reference file */

static void
skiplist(FILE *oldrefs)
{
    int	i;
	
    if (fscanf(oldrefs, "%d", &i) != 1) {
	postfatal("cscope: cannot read list size from file %s\n", reffile);
	/* NOTREACHED */
    }
    while (--i >= 0) {
	if (fscanf(oldrefs, "%*s") != 0) {
	    postfatal("cscope: cannot read list name from file %s\n", reffile);
	    /* NOTREACHED */
	}
    }
}


/* enter curses mode */
void
entercurses(void)
{
    incurses = YES;
#ifndef __MSDOS__ /* HBB 20010313 */
    nonl();		    /* don't translate an output \n to \n\r */
#endif
    raw();			/* single character input */
    noecho();			/* don't echo input characters */
    clear();			/* clear the screen */
    mouseinit();		/* initialize any mouse interface */
    drawscrollbar(topline, nextline);
}


/* exit curses mode */
void
exitcurses(void)
{
	/* clear the bottom line */
	move(LINES - 1, 0);
	clrtoeol();
	refresh();

	/* exit curses and restore the terminal modes */
	endwin();
	incurses = NO;

	/* restore the mouse */
	mousecleanup();
	fflush(stdout);
}


/* normal usage message */
static void
usage(void)
{
	fprintf(stderr, "Usage: cscope [-bcCdehklLqRTuUvV] [-f file] [-F file] [-i file] [-I dir] [-s dir]\n");
	fprintf(stderr, "              [-p number] [-P path] [-[0-8] pattern] [source files]\n");
}


/* long usage message */
static void
longusage(void)
{
	usage();
	fprintf(stderr, "\
\n\
-b            Build the cross-reference only.\n\
-C            Ignore letter case when searching.\n\
-c            Use only ASCII characters in the cross-ref file (don't compress).\n\
-d            Do not update the cross-reference.\n\
-e            Suppress the <Ctrl>-e command prompt between files.\n\
-F symfile    Read symbol reference lines from symfile.\n\
-f reffile    Use reffile as cross-ref file name instead of %s.\n",
		REFFILE);
	fprintf(stderr, "\
-h            This help screen.\n\
-I incdir     Look in incdir for any #include files.\n\
-i namefile   Browse through files listed in namefile, instead of %s\n",
		NAMEFILE);
	fprintf(stderr, "\
-k            Kernel Mode - don't use %s for #include files.\n",
		DFLT_INCDIR);
	fputs("\
-L            Do a single search with line-oriented output.\n\
-l            Line-oriented interface.\n\
-num pattern  Go to input field num (counting from 0) and find pattern.\n\
-P path       Prepend path to relative file names in pre-built cross-ref file.\n\
-p n          Display the last n file path components.\n\
-q            Build an inverted index for quick symbol searching.\n\
-R            Recurse directories for files.\n\
-s dir        Look in dir for additional source  files.\n\
-T            Use only the first eight characters to match against C symbols.\n\
-U            Check file time stamps.\n\
-u            Unconditionally build the cross-reference file.\n\
-v            Be more verbose in line mode.\n\
-V            Print the version number.\n\
\n\
Please see the manpage for more information.\n",
	      stderr);
}

/* cleanup and exit */

void
myexit(int sig)
{
	/* HBB 20010313; close file before unlinking it. Unix may not care
	 * about that, but DOS absolutely needs it */
	if (refsfound != NULL)
		fclose(refsfound);
	
	/* remove any temporary files */
	if (temp1[0] != '\0') {
		unlink(temp1);
		unlink(temp2);
		rmdir(tempdirpv);		
	}
	/* restore the terminal to its original mode */
	if (incurses == YES) {
		exitcurses();
	}
	/* dump core for debugging on the quit signal */
	if (sig == SIGQUIT) {
		abort();
	}
	/* HBB 20000421: be nice: free allocated data */
	freefilelist();
	freeinclist();
	freesrclist();
	freecrossref();
	free_newbuildfiles();

	exit(sig);
}