115037Sslatteng # include "signal.h"
215037Sslatteng # include "refer..c"
main(argc,argv)315037Sslatteng main(argc,argv)
415037Sslatteng 	char *argv[];
515037Sslatteng {
615037Sslatteng char line[LLINE], *s;
715037Sslatteng int nodeflt =0;
815037Sslatteng 
915037Sslatteng signals();
1015037Sslatteng while (argc>1 &&argv[1][0] == '-')
1115037Sslatteng 	{
1215037Sslatteng 	switch(argv[1][1])
1315037Sslatteng 		{
1415037Sslatteng 		case 'e':
1515037Sslatteng 			endpush++; break;
1615037Sslatteng 		case 'l':
1715037Sslatteng 			labels++;
1815037Sslatteng 			s = argv[1]+2;
1915037Sslatteng 			nmlen = atoi(s);
2015037Sslatteng 			for ( ; *s; s++)
2115037Sslatteng 				if (*s == ',' || *s =='_' || *s =='"')
2215037Sslatteng 					break;
2315037Sslatteng 			if (*s==0) break;
2415037Sslatteng 			switch(*s++)
2515037Sslatteng 				{
2615037Sslatteng 				case ',': labblkflg=0; break;
2715037Sslatteng 				case '_': labblkflg=" "; break;
2815037Sslatteng 				case '"':
2915037Sslatteng 					labblkflg=s;
3015037Sslatteng 					while (*s && *s != '"')
3115037Sslatteng 						s++;
3215037Sslatteng 					*s++= 0; break;
3315037Sslatteng 				}
3415037Sslatteng 			dtlen = atoi(s);
3515037Sslatteng 			break;
3615037Sslatteng 		case 'k':
3715037Sslatteng 			keywant = (argv[1][2] ? argv[1][2] : 'L');
3815037Sslatteng 			labels++;
3915037Sslatteng 			break;
4015037Sslatteng 		case 's':
4115037Sslatteng 			sort++;
4215037Sslatteng 			if (argv[1][2])
4315037Sslatteng 				keystr= argv[1]+2;
4415037Sslatteng 			break;
4515037Sslatteng 		case 'p':
4615037Sslatteng 			argc--; argv++;
4715037Sslatteng 			*search++ = argv[1];
4815037Sslatteng 			if (search-rdata>NSERCH)
4915037Sslatteng 				err("too many -p options (%d)", NSERCH);
5015037Sslatteng 			break;
5115037Sslatteng 		case 'n':
5215037Sslatteng 			nodeflt=1;
5315037Sslatteng 			break;
5415037Sslatteng 		case 'B':
5515037Sslatteng 			nobracket++; /* falls through */
5615037Sslatteng 		case 'b':
5715037Sslatteng 			bare = (argv[1][2] == '1') ? 1 : 2;
5815037Sslatteng 			break;
5915037Sslatteng 		case 'c':
6015037Sslatteng 			smallcaps = argv[1]+2;
6115037Sslatteng 			break;
6215037Sslatteng 		case 'a':
6315037Sslatteng 			authrev = atoi (argv[1]+2);
6415037Sslatteng 			if (authrev<=0)
6515037Sslatteng 				authrev = 1000;
6615037Sslatteng 			for(s=argv[1]+2; isdigit(*s); s++)
6715037Sslatteng 				;
6815037Sslatteng 			if (*s==',') nocomma++;
6915037Sslatteng 			break;
7015037Sslatteng 		case 'd': /* reduce date to year only */
7115037Sslatteng 			yearonly=1;
7215037Sslatteng 			break;
7315037Sslatteng 		case 'A': /* these fields get appended */
7415037Sslatteng 			appfld = argv[1]+2;
7515037Sslatteng 			break;
7615037Sslatteng 		case 'I': /* these fields get ignored */
7715037Sslatteng 			ignfld = argv[1]+2;
7815037Sslatteng 			break;
7915037Sslatteng 		case 'P': /* preserve original: no .ds's */
8015037Sslatteng 			preserve = 1;
8115037Sslatteng 			break;
8215037Sslatteng 		case 'L':
8315037Sslatteng 			labsepstr= argv[1]+2; break;
8415037Sslatteng 		}
8515037Sslatteng 	argc--; argv++;
8615037Sslatteng 	}
8715037Sslatteng if (nodeflt==0)
8815037Sslatteng 	*search++ = "/usr/dict/papers/Ind";
8915037Sslatteng 
9015037Sslatteng if (sort)
9115037Sslatteng 	endpush=1;
9215037Sslatteng 
9315037Sslatteng 
9415037Sslatteng if (endpush)
9515037Sslatteng 	sprintf(tfile, "/tmp/rj%da", getpid());
9615037Sslatteng if (sort)
9715037Sslatteng 	{
9815037Sslatteng 	sprintf(ofile,"/tmp/rj%db", getpid());
9915037Sslatteng 	sprintf(tdfile, "/tmp/rj%de", getpid());
10015037Sslatteng 	ftemp = fopen(ofile, "w");
10115037Sslatteng 	if (ftemp==NULL)
10215037Sslatteng 		{
10315037Sslatteng 		fprintf(stderr, "Can't open scratch file\n");
10415037Sslatteng 		exit(1);
10515037Sslatteng 		}
10615037Sslatteng 	}
10715037Sslatteng 
10815037Sslatteng do
10915037Sslatteng 	{
11015037Sslatteng 	if (argc>1)
11115037Sslatteng 		{
11215037Sslatteng 		if (in!=stdin)
11315037Sslatteng 			fclose(in);
11415037Sslatteng 		Iline=0;
11515037Sslatteng 		if (strcmp(argv[1], "-")==SAME)
11615037Sslatteng 			in = stdin;
11715037Sslatteng 		else
11815037Sslatteng 			in = fopen(Ifile=argv[1], "r");
11915037Sslatteng 		argc--; argv++;
12015037Sslatteng 		if (in==NULL)
12115037Sslatteng 			{
12215037Sslatteng 			err("Can't read %s", Ifile);
12315037Sslatteng 			continue;
12415037Sslatteng 			}
12515037Sslatteng 		}
12615037Sslatteng 	while (input(line))
12715037Sslatteng 		{
12815037Sslatteng 		Iline++;
12915037Sslatteng # ifdef D1
13015037Sslatteng 		fprintf(stderr, "line %.20s\n",line);
13115037Sslatteng # endif
13215037Sslatteng 		if (prefix(".[", line) || (nobracket && line[0]!='\n'))
13315037Sslatteng 			{
13415037Sslatteng 			if (endpush && (fo==NULL || fo == stdout))
13515037Sslatteng 				{
13615037Sslatteng 				fo = fopen(tfile, "w");
13715037Sslatteng # if D1
13815037Sslatteng 				fprintf(stderr, "opened %s as %o\n",tfile,fo);
13915037Sslatteng # endif
14015037Sslatteng 				if (fo==NULL)
14115037Sslatteng 					{
14215037Sslatteng 					fprintf(stderr,"Can't open scratch file");
14315037Sslatteng 					exit(1);
14415037Sslatteng 					}
14515037Sslatteng 				sep = 002; /* separate records without confusing sort..*/
14615037Sslatteng 				}
14715037Sslatteng 			doref(line);
14815037Sslatteng 			}
14915037Sslatteng 		else
15015037Sslatteng 			output(line);
15115037Sslatteng # if D1
15215037Sslatteng 		fprintf(stderr, "past output/doref\n");
15315037Sslatteng # endif
15415037Sslatteng 		}
15515037Sslatteng 	}
15615037Sslatteng 	while (argc>1);
15715037Sslatteng # if D1
15815037Sslatteng fprintf(stderr, "before dumpold, endpush %d fo %o\n",endpush, fo);
15915037Sslatteng # endif
16015037Sslatteng widelab();
16115037Sslatteng if (endpush && (fo!=NULL && fo != stdout))
16215037Sslatteng 	dumpold();
16315037Sslatteng output("");
16415037Sslatteng fflush (ftemp);
16515037Sslatteng if (sort)
16615037Sslatteng 	recopy(ofile);
16715037Sslatteng clfgrep();
16815037Sslatteng # ifndef D1
16915037Sslatteng cleanup();
17015037Sslatteng # endif
17115037Sslatteng exit(0);
17215037Sslatteng }
17315037Sslatteng 
17415037Sslatteng extern int intr();
signals()17515037Sslatteng signals()
17615037Sslatteng {
177*16408Sslatteng signal (SIGINT, intr);
178*16408Sslatteng signal (SIGHUP, intr);
179*16408Sslatteng signal (SIGPIPE, intr);
180*16408Sslatteng signal (SIGTERM, intr);
18115037Sslatteng }
18215037Sslatteng 
intr()18315037Sslatteng intr()
18415037Sslatteng {
18515037Sslatteng signal(SIGINT, 1);
18615037Sslatteng cleanup();
18715037Sslatteng exit(1);
18815037Sslatteng }
cleanup()18915037Sslatteng cleanup()
19015037Sslatteng {
19115037Sslatteng if (tfile[0]) unlink(tfile);
19215037Sslatteng if (gfile[0]) unlink(gfile);
19315037Sslatteng if (ofile[0]) unlink(ofile);
19415037Sslatteng if (hidenam[0]) unlink(hidenam);
19515037Sslatteng }
196