1 # include "signal.h"
2 # include "refer..c"
3 main(argc,argv)
4 	char *argv[];
5 {
6 char line[LLINE], *s;
7 int nodeflt =0;
8 
9 signals();
10 while (argc>1 &&argv[1][0] == '-')
11 	{
12 	switch(argv[1][1])
13 		{
14 		case 'e':
15 			endpush++; break;
16 		case 'l':
17 			labels++;
18 			s = argv[1]+2;
19 			nmlen = atoi(s);
20 			for ( ; *s; s++)
21 				if (*s == ',' || *s =='_' || *s =='"')
22 					break;
23 			if (*s==0) break;
24 			switch(*s++)
25 				{
26 				case ',': labblkflg=0; break;
27 				case '_': labblkflg=" "; break;
28 				case '"':
29 					labblkflg=s;
30 					while (*s && *s != '"')
31 						s++;
32 					*s++= 0; break;
33 				}
34 			dtlen = atoi(s);
35 			break;
36 		case 'k':
37 			keywant = (argv[1][2] ? argv[1][2] : 'L');
38 			labels++;
39 			break;
40 		case 's':
41 			sort++;
42 			if (argv[1][2])
43 				keystr= argv[1]+2;
44 			break;
45 		case 'p':
46 			argc--; argv++;
47 			*search++ = argv[1];
48 			if (search-rdata>NSERCH)
49 				err("too many -p options (%d)", NSERCH);
50 			break;
51 		case 'n':
52 			nodeflt=1;
53 			break;
54 		case 'B':
55 			nobracket++; /* falls through */
56 		case 'b':
57 			bare = (argv[1][2] == '1') ? 1 : 2;
58 			break;
59 		case 'c':
60 			smallcaps = argv[1]+2;
61 			break;
62 		case 'a':
63 			authrev = atoi (argv[1]+2);
64 			if (authrev<=0)
65 				authrev = 1000;
66 			for(s=argv[1]+2; isdigit(*s); s++)
67 				;
68 			if (*s==',') nocomma++;
69 			break;
70 		case 'd': /* reduce date to year only */
71 			yearonly=1;
72 			break;
73 		case 'A': /* these fields get appended */
74 			appfld = argv[1]+2;
75 			break;
76 		case 'I': /* these fields get ignored */
77 			ignfld = argv[1]+2;
78 			break;
79 		case 'P': /* preserve original: no .ds's */
80 			preserve = 1;
81 			break;
82 		case 'L':
83 			labsepstr= argv[1]+2; break;
84 		}
85 	argc--; argv++;
86 	}
87 if (nodeflt==0)
88 	*search++ = "/usr/dict/papers/Ind";
89 
90 if (sort)
91 	endpush=1;
92 
93 
94 if (endpush)
95 	sprintf(tfile, "/tmp/rj%da", getpid());
96 if (sort)
97 	{
98 	sprintf(ofile,"/tmp/rj%db", getpid());
99 	sprintf(tdfile, "/tmp/rj%de", getpid());
100 	ftemp = fopen(ofile, "w");
101 	if (ftemp==NULL)
102 		{
103 		fprintf(stderr, "Can't open scratch file\n");
104 		exit(1);
105 		}
106 	}
107 
108 do
109 	{
110 	if (argc>1)
111 		{
112 		if (in!=stdin)
113 			fclose(in);
114 		Iline=0;
115 		if (strcmp(argv[1], "-")==SAME)
116 			in = stdin;
117 		else
118 			in = fopen(Ifile=argv[1], "r");
119 		argc--; argv++;
120 		if (in==NULL)
121 			{
122 			err("Can't read %s", Ifile);
123 			continue;
124 			}
125 		}
126 	while (input(line))
127 		{
128 		Iline++;
129 # ifdef D1
130 		fprintf(stderr, "line %.20s\n",line);
131 # endif
132 		if (prefix(".[", line) || (nobracket && line[0]!='\n'))
133 			{
134 			if (endpush && (fo==NULL || fo == stdout))
135 				{
136 				fo = fopen(tfile, "w");
137 # if D1
138 				fprintf(stderr, "opened %s as %o\n",tfile,fo);
139 # endif
140 				if (fo==NULL)
141 					{
142 					fprintf(stderr,"Can't open scratch file");
143 					exit(1);
144 					}
145 				sep = 002; /* separate records without confusing sort..*/
146 				}
147 			doref(line);
148 			}
149 		else
150 			output(line);
151 # if D1
152 		fprintf(stderr, "past output/doref\n");
153 # endif
154 		}
155 	}
156 	while (argc>1);
157 # if D1
158 fprintf(stderr, "before dumpold, endpush %d fo %o\n",endpush, fo);
159 # endif
160 widelab();
161 if (endpush && (fo!=NULL && fo != stdout))
162 	dumpold();
163 output("");
164 fflush (ftemp);
165 if (sort)
166 	recopy(ofile);
167 clfgrep();
168 # ifndef D1
169 cleanup();
170 # endif
171 exit(0);
172 }
173 
174 extern int intr();
175 signals()
176 {
177 signal (SIGINT, intr);
178 signal (SIGHUP, intr);
179 signal (SIGPIPE, intr);
180 signal (SIGTERM, intr);
181 }
182 
183 intr()
184 {
185 signal(SIGINT, 1);
186 cleanup();
187 exit(1);
188 }
189 cleanup()
190 {
191 if (tfile[0]) unlink(tfile);
192 if (gfile[0]) unlink(gfile);
193 if (ofile[0]) unlink(ofile);
194 if (hidenam[0]) unlink(hidenam);
195 }
196