]> diplodocus.org Git - nmh/blob - docs/historical/mh-jun-1982/progs/news.c
Fixed inc(1) and %(me) to not obey Local-Mailbox profile component.
[nmh] / docs / historical / mh-jun-1982 / progs / news.c
1 #ifdef COMMENT
2 Proprietary Rand Corporation, 1981.
3 Further distribution of this software
4 subject to the terms of the Rand
5 license agreement.
6 #endif
7
8 #include "../mh.h"
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <sys/dir.h>
13 #include <strings.h>
14
15 extern char *sprintf();
16
17 #define max(a,b) (a > b ? a : b)
18 #define YES 1
19 #define NO 0
20
21 #define NEWSP "/usr/news" /* MUST be same as login News's login dir */
22 #define MAXTOPICS 50 /* Enough for a while */
23
24 int vecp, topicp;
25 int topicspec; /* topic argument(s) given */
26 char *topics[MAXTOPICS+1];
27 char *vec[MAXARGS];
28 int fdisplay, fcheck, fupdate, fsend, freview, fbody; /* flags */
29 int frevback, fverbose, fhelp, ftopics; /* flags */
30 int hit;
31
32 struct nts {
33 char t_name[16];
34 int t_num;
35 } nts[MAXTOPICS+1], *check();
36 struct nts *ntps[MAXTOPICS];
37 int nnts; /* number of actual topics */
38
39 struct swit switches[] = {
40 "add", -1, /* 0 */
41 "body", -1, /* 1 */
42 "check", 0, /* 2 */
43 "display", 0, /* 3 */
44 "review [#]", 0, /* 4 */
45 "send topic ...",0, /* 5 */
46 "topics", 0, /* 6 */
47 "update", 0, /* 7 */
48 "help", 4, /* 8 */
49 0, 0,
50 };
51
52 extern char _sobuf[]; /* MLW standard out buffer */
53
54 /*ARGSUSED*/
55 main(argc, argv)
56 int argc;
57 char *argv[];
58 {
59 register int i;
60 register char *cp, **ap;
61 char *arguments[50], **argp;
62
63 invo_name = argv[0];
64 setbuf(stdout, _sobuf);
65 #ifdef NEWS
66 m_news();
67 #endif
68 if (chdir (NEWSP) < 0) { /* <-- N.B. */
69 fprintf(stderr, "Can't change directory to ");
70 perror(NEWSP);
71 done(1);
72 }
73 vecp = 2;
74 cp = r1bindex(argv[0], '/');
75 if((cp = m_find(cp)) != NULL) {
76 ap = brkstring(cp = getcpy(cp), " ", "\n");
77 ap = copyip(ap, arguments);
78 } else
79 ap = arguments;
80 VOID copyip(argv+1, ap);
81 argp = arguments;
82 while(cp = *argp++) {
83 if(*cp == '-')
84 switch(smatch(++cp, switches)) {
85 case -2:ambigsw(cp, switches); /* ambiguous */
86 done(0);
87 /* unknown */
88 case -1:vec[vecp++] = --cp; continue;
89 case 0:
90 case 5: fsend = YES; continue;
91 case 1: fbody = YES;
92 vec[vecp++] = --cp; continue;
93 case 2: fcheck = YES; continue;
94 case 3: fdisplay = YES; continue;
95 case 4: freview = YES;
96 if(**argp >= '0' && **argp <= '9')
97 frevback = atoi(*argp++);
98 continue;
99 case 7: fupdate = YES; continue;
100
101 case 8:
102 fhelp = YES;
103 case 6:
104 ftopics = YES;
105 continue;
106 }
107 else
108 if(vecp == 2) {
109 VOID addtopic (cp, YES);
110 topicspec = YES;
111 }
112 else
113 vec[vecp++] = cp;
114 }
115 if (!topicspec)
116 /* user didn't specify topics so give him his defaults */
117 gettopics();
118 getnts();
119 if (fhelp) {
120 help(
121 "news [topic ...] [switches] [switches for \"c\" or \"mail\" ]", switches);
122 putchar('\n');
123 }
124 if (ftopics) {
125 showtopics();
126 done(0);
127 }
128 if(fsend) {
129 if(!topicspec) {
130 fprintf(stderr, "Usage: news -send topic [mail switches]\n");
131 done(1);
132 }
133 for(i = 0; i < topicp; i++)
134 if(check(topics[i]))
135 send(topics[i]);
136 else
137 printf("Unknown topic: %s\n", topics[i]);
138 done(0);
139 }
140 for(i = 0; i < topicp; i++)
141 if(check(topics[i]) == NULL)
142 printf("Topic: %s unknown.\n", topics[i]);
143 else
144 disp(topics[i], fverbose || topicspec);
145 if(fcheck) {
146 if(hit)
147 printf(".\n");
148 } else if(!hit && !topicspec && !fupdate && !fdisplay)
149 nonews();
150 m_update();
151 done(0);
152 }
153
154
155 struct nts *
156 check(topic)
157 char *topic;
158 {
159 register struct nts *t;
160
161 for(t = nts; t->t_name[0]; t++)
162 if(strncmp(topic, t->t_name, DIRSIZ) == 0)
163 return t;
164 return 0;
165 }
166
167
168 disp(topic, argflg)
169 register char *topic;
170 int argflg;
171 {
172 register struct nts *t;
173 register char *cp, *np;
174 register int msgnum;
175 int high;
176 char buf[128];
177
178 if((t = check(topic)) == 0)
179 fprintf(stderr, "HUH?\n");
180 if((cp = m_find(np = concat("news-", topic, NULLCP))) == NULL)
181 cp = "0";
182 high = atoi(cp);
183 if(fcheck) {
184 if(t->t_num > high) {
185 if(!hit++)
186 printf("Unread news in");
187 if(hit > 1)
188 printf(",");
189 printf(" '%s'", topic);
190 }
191 return;
192 }
193 if(fupdate) {
194 if(t->t_num > high) {
195 m_replace(np, getcpy(m_name(t->t_num)));
196 printf("Skipping %d items in %s.\n",
197 t->t_num - high, topic);
198 }
199 return;
200 }
201 if(freview)
202 if(frevback)
203 msgnum = max(high - frevback, 0);
204 else
205 msgnum = 0;
206 else
207 msgnum = high;
208 /*** msgnum = freview? frevback? high - frevback : 0 : high; */
209 if(msgnum >= t->t_num) {
210 if(argflg)
211 printf("%s: no new news.\n", topic);
212 return;
213 }
214 VOID sprintf(buf, "%s/%s", NEWSP, topic);
215 if(chdir(buf) == -1) {
216 perror(buf);
217 return;
218 }
219 vec[1] = showproc;
220 for( ; msgnum < t->t_num;) {
221 cp = m_name(++msgnum);
222 if(hit) {
223 printf("\nPress <return> for %s:%s...", topic, cp);
224 VOID fflush(stdout);
225 VOID read(0, buf, sizeof buf);
226 } else
227 printf("News item %s:%s\n", topic, cp);
228 if(msgnum > high) {
229 m_replace(np, getcpy(cp));
230 m_update();
231 }
232 putenv("mhfolder", topic);
233 vec[vecp] = cp;
234 putchar('\n');
235 call(vec + 1);
236 hit = 1;
237 }
238 }
239
240
241 call(vector)
242 char **vector;
243 {
244 register int pid, child;
245 int status;
246
247 VOID fflush(stdout);
248 while((child = fork()) == -1) {
249 printf("No forks...\n"); VOID fflush(stdout);
250 sleep(2);
251 }
252 if(child == 0) {
253 execv(vector[0], vector);
254 perror(vector[0]);
255 done(1);
256 }
257 while((pid = wait(&status)) != -1 && pid != child) ;
258 if(pid == -1 || status) {
259 fprintf(stderr, "Abnormal termination from %s\n", vector[0]);
260 done(1);
261 }
262 }
263
264
265 send(topic)
266 register char *topic;
267 {
268 vec[0] = mailproc;
269 vec[1] = concat("news.", topic, NULLCP);
270 if(!fbody)
271 printf("Enter text for %s\n", topic);
272 call(vec);
273 free(vec[1]);
274 }
275
276
277 #ifdef CUTE
278 char *nons[] = {
279 "No new news",
280 "No news to peruse",
281 "Only old news",
282 "News shortage",
283 "News reporters on strike",
284 "Report your own news",
285 "News presses broken"
286 };
287 #define NONS (sizeof nons/ sizeof nons[0])
288 #endif
289
290 nonews()
291 {
292 #ifdef CUTE
293 #include <sys/timeb.h>
294 struct timeb tb;
295
296 ftime(&tb);
297 printf("%s.\n", nons[tb.millitm % NONS]);
298 #else
299 printf("No new news.\n");
300 #endif
301 }
302
303 showtopics()
304 {
305 register int i;
306
307 sortnts();
308 printf("Chk Items Topics\n");
309 printf("--- ----- ------\n");
310 for(i = 0; i < nnts; i++)
311 printf(" %s %4d %s\n",
312 addtopic (ntps[i]->t_name, NO) ? "*" : " ",
313 ntps[i]->t_num, ntps[i]->t_name);
314 return;
315 }
316
317 gettopics ()
318 {
319 register char **ap;
320 register char *cp;
321
322 VOID addtopic ("everyone", YES);
323 if((cp = m_find("news-topics")) != NULL) {
324 for (ap = brkstring (cp = getcpy(cp), " ", "\n")
325 ; *ap; ap++)
326 VOID addtopic (*ap, YES);
327 }
328 return;
329 }
330
331 addtopic (cp, real)
332 char *cp;
333 int real;
334 {
335 register char **cpp;
336
337 for (cpp = topics; *cpp; cpp++)
338 if (!strcmp (*cpp, cp))
339 return YES; /* We have it already */
340 if (real) {
341 if (!strcmp ("*", cp)) {
342 register int i;
343 getnts ();
344 for(i = 0; i < nnts; i++)
345 VOID addtopic (nts[i].t_name, YES);
346 }
347 else if (topicp < MAXTOPICS - 1)
348 topics[topicp++] = cp;
349 }
350 return NO; /* We don't have it already */
351 }
352
353 getnts()
354 {
355 /* shouldn't ALWAYS look up all news folders.
356 * Should only do that for a -topics or -help.
357 */
358 struct direct dir;
359 struct stat st;
360 register struct nts *t;
361 register FILE *d;
362 char tbuf[DIRSIZ + 2];
363
364 if (nnts > 0)
365 /* we've done it already */
366 return;
367 t = nts;
368 if((d = fopen(".", "r")) == NULL) {
369 fprintf(stderr, "Can't open ");
370 perror(NEWSP);
371 done(1);
372 }
373 tbuf[0] = '.';
374 while(fread((char *)&dir, sizeof dir, 1, d)) {
375 if(dir.d_ino && dir.d_name[0] != '.') {
376 strncpy(t->t_name, dir.d_name, DIRSIZ);
377 strcpy(&tbuf[1], t->t_name);
378 if(stat(tbuf, &st) != -1)
379 t->t_num = st.st_size;
380 nnts++;
381 t++;
382 }
383 }
384 VOID fclose(d);
385 return;
386 }
387
388 sortnts()
389 {
390 register int i;
391 extern int ntcmp();
392
393 for (i = 0; i < nnts; i++)
394 ntps[i] = &nts[i];
395 qsort ((char *) ntps, nnts, sizeof ntps[0], ntcmp);
396 return;
397 }
398
399 ntcmp (n1, n2)
400 struct nts **n1;
401 struct nts **n2;
402 {
403 return strcmp ((*n1)->t_name, (*n2)->t_name);
404 }