|
|
1.1 root 1: /* library:option2.c 1.30 */
2: #include "sccsid.h"
3: VERSION(@(#)library:option2.c 1.30)
4:
5: /***************** option2.c *************/
6:
7: /* this package handles option 2 of library. Basically this
8: * option consists of requests that can be formatted as forms
9: * to be filled in. The forms all have the name 2.*.
10: * Every form must have a dashed line in it (a line starting with at least 10
11: * dashes) which separates control info from the actual form part.
12: * Some conventions:
13: * - lines starting with a # are comments - above and below the dash line.
14: * - If one of the first two lines starts with a P (capital p) this is
15: * what the user is shown to select from. Otherwise the file name.
16: * - lines starting with a ? above the dash line are general comments.
17: * - lines with a ? below the dash line are help for the previous query.
18: *
19: * Each line ends with either
20: * *number saying a response is optional. max number lines is number.
21: * +number saying a response is manditory. max number lines is number.
22: */
23: #include "gnamef.h"
24: #include <stdio.h>
25: #include <ctype.h>
26: #include <string.h>
27: char *malloc(), *realloc();
28: char *ctime(), *getenv();
29: void put_respond(), put_lib_file();
30:
31: option2(logfile, argc, argv, optnum)
32: FILE *logfile;
33: int argc;
34: char *argv[];
35: int optnum;
36: {
37: char *response, filename[80], hold_where[100], what_avail[50],
38: format[150], tempname[60], instr[150], templine[200], query[80];
39: char *ptr, *eptr, *tptr, tchar, tchar2, *endptr, this_char, *dateptr,
40: *clptr, *beginptr, *editor, *tptr2;
41: char *libptr, *get_lib_file(), *find_respond();
42: extern char *Cmd, *whereto, libcntl[], pan[], req_name[], libfile[];
43: int i, j, k, ans, avail, num, numreq, resp_size, first, max, nolog,
44: multi_req, nodata, level, said_blank,
45: save_respond, respond_flag, use_respond;
46: int used_arg; /* flag that arguments used in selection */
47: #ifdef SVR4
48: time_t today;
49: #else
50: long today;
51: #endif
52: FILE *tfile, *popen(), *efile, *sendfile;
53: DIR *tdir;
54:
55: time(&today);
56: used_arg = respond_flag = level = 0;
57: strcpy(format, "Your %d requests have been sent\n");
58: dateptr = ctime(&today);
59: /* get initial space for responses */
60: nolog = said_blank = first = 0;
61: resp_size = 5000;
62: if ((response = malloc(resp_size)) == NULL) {
63: fprintf(stderr, "library: Couldn't get space for request (%d). No requests sent!\n", resp_size);
64: exit(1);
65: }
66: /* first count what is available - If only one say nothing */
67: tdir = opendir(WHERE);
68: avail = 0;
69: ptr = response;
70: while (gnamef(tdir, ptr) == TRUE)
71: if ((atoi(ptr) == optnum) && (*(ptr+1) == '.')) {
72: avail++;
73: ptr += strlen(ptr) + 1;
74: }
75: closedir(tdir);
76: if (avail == 0) {
77: fprintf(stderr, "Nothing available via this option! No requests sent\n");
78: exit(1);
79: }
80: /* see if they already said what they want */
81: if (argc) {
82: tptr = argv[0];
83: if ((*tptr != '-') || ! isalnum(*(tptr+1))) {
84: fprintf(stderr, "Confused arguments for option %d (%s)\?\? No requests sent\n", optnum, tptr);
85: exit(1);
86: }
87: ans = *(tptr+1);
88: argc--;
89: argv++;
90: }
91: else ans = 0;
92: eptr = ptr;
93: if (avail == 1) ans = -1;
94: else {
95: if (ans == 0) {
96: printf("The following types of material are available.\n");
97: printf("For more details on a given type do <option number> ?. E.g. p?\n\n");
98: }
99: /* use the file names - or D line in the library directory */
100: ptr = response;
101: for (this_char='1', j=1; j <= avail; j++, ptr += strlen(ptr) + 1) {
102: /* check if they say special words what to call it */
103: sprintf(eptr, "%s/%s", WHERE, ptr);
104: if ((tfile = fopen(eptr, "r")) == NULL) {
105: fprintf(stderr, "Can't read option file for %s\n", ptr+2);
106: continue;
107: }
108: /* scan to dash line for D line */
109: while (fgets(eptr, 150, tfile) != NULL) {
110: if (strncmp(eptr, "-----", 5) == 0) break;
111: if (*eptr == 'D') break;
112: }
113: tptr = strchr(eptr, '\n');
114: fclose(tfile);
115: if (tptr != NULL) *tptr = '\0';
116: tptr = eptr + 1;
117: while ((*tptr == ' ') || (*tptr == '\t')) tptr++;
118: if ((*eptr != 'D') || ! *tptr) {
119: printf(" %c) %s\n\n", this_char, ptr+2);
120: if (ans == 0) what_avail[j-1] = this_char++;
121: continue;
122: }
123: if ((*(tptr+1) == '\0') || (*(tptr+1) == ' ') || (*(tptr+1) == '\t')) {
124: what_avail[j-1] = *tptr;
125: tptr++;
126: while ((*tptr == ' ') || (*tptr == '\t')) tptr++;
127: }
128: else {
129: what_avail[j-1] = this_char++;
130: }
131: if (ans != 0) continue;
132: if (!strlen(tptr))
133: printf(" %c) %s\n\n", what_avail[j-1], ptr+2);
134: else
135: printf(" %c) %s\n\n", what_avail[j-1], tptr);
136: }
137: } /* display of available options */
138: what_avail[j-1] = 0;
139: if (ans == -1) ans = 1;
140: else if (ans > 0) {
141: for (i=0; i < avail; i++)
142: if (ans == what_avail[i]) break;
143: i++;
144: if ((i < 1) || (i > avail)) {
145: fprintf(stderr, "Illegal option selected (%c). available are [%s]\n",
146: ans, what_avail);
147: exit(1);
148: }
149: ans = i;
150: }
151: while (ans == 0) {
152: printf("Type desired: ");
153: if ((fgets(eptr, 80, stdin) == NULL) || (*eptr == '.')) {
154: fprintf(stderr, "No requests sent!!\n");
155: exit(1);
156: }
157: if ((tptr = strchr(eptr, '\n')) != NULL) *tptr = '\0';
158: if (!strlen(eptr)) {
159: printf("Please select from available options, . to exit, ? for help\n");
160: continue;
161: }
162: ptr = eptr + 1;
163: while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
164: if ((*eptr == '?') && !isalnum(*ptr)) {
165: sprintf(eptr, "%s/option%d.help", WHERE, optnum);
166: if ((tfile = fopen(eptr, "r")) == NULL) {
167: printf("Sorry. General help on option %d not on this machine!\n",
168: optnum);
169: continue;
170: }
171: while (fgets(instr, 150, tfile) != NULL)
172: fputs(instr, stdout);
173: fclose(tfile);
174: continue;
175: }
176: if (*eptr == '?') {
177: tptr = ptr;
178: ptr = eptr;
179: eptr = tptr;
180: }
181: for (i=0; i < avail; i++)
182: if (*eptr == what_avail[i]) break;
183: i++;
184: if ((i < 1) || (i > avail)) {
185: fprintf(stderr, "Illegal option. Must be from list above (%s)\n", what_avail);
186: continue;
187: }
188: /* see if they want help */
189: if (*ptr == '?') {
190: ptr = response;
191: for (j = 1; j < i; j++) ptr += strlen(ptr) + 1;
192: sprintf(eptr, "%s/%s", WHERE, ptr);
193: if ((tfile = fopen(eptr, "r")) == NULL) {
194: fprintf(stderr, "Can't read option file for %s\n", ptr+2);
195: continue;
196: }
197: j = 0;
198: while (fgets(eptr, 150, tfile) != NULL) {
199: if (strncmp(eptr, "-----", 5) == 0) break;
200: if (*eptr == '?') {
201: fputs(eptr+1, stdout);
202: j++;
203: }
204: }
205: fclose(tfile);
206: if (!j) printf("Sorry - no help available about %s\n", ptr+2);
207: continue;
208: } /* end giving of help */
209: ans = i;
210: } /* end more than one thype thing to choose from */
211: ptr = eptr = response;
212: numreq = 0;
213: for (j = 1; j < ans; j++) ptr += strlen(ptr) + 1;
214: sprintf(filename, "%s/%s", WHERE, ptr);
215: if ((tfile = fopen(filename, "r")) == NULL) {
216: fprintf(stderr, "library: Can't read selected option file for %s\n",
217: ptr+2);
218: exit(1);
219: }
220: /* we may need the base of filename for saved responses below */
221: strcpy(filename, ptr);
222:
223: nodata = 0;
224: multi_req = 1;
225: while (1) {
226: endptr = eptr;
227: rewind(tfile);
228: while (fgets(eptr, 150, tfile) != NULL) {
229: if (strncmp(eptr, "-----", 5) == 0) break;
230: /* all set up already */
231: if (numreq) continue;
232: if (*eptr == 'S') {
233: eptr++;
234: while (*eptr == ' ' || (*eptr == '\t')) eptr++;
235: strcpy(response, eptr);
236: eptr = response + strlen(response);
237: }
238: else if (strncmp(eptr, "NODATA", 6) == 0) {
239: nodata = 1;
240: }
241: else if (strncmp(eptr, "FORMAT", 6) == 0) {
242: tptr = eptr + 6;
243: while (*tptr && isspace(*tptr)) tptr++;
244: if (*tptr) strcpy(format, tptr);
245: }
246: else if (strncmp(eptr, "NOBLANKMSG", 10) == 0) {
247: said_blank = -1;
248: }
249: else if (strncmp(eptr, "NOLOG", 5) == 0) {
250: nolog = 1;
251: }
252: else if (strncmp(eptr, "LEVEL", 5) == 0) {
253: level = 1;
254: }
255: else if (strncmp(eptr, "ONE", 3) == 0) {
256: multi_req = 0;
257: }
258: else if (*eptr == 'M') {
259: tptr = eptr+1;
260: while (*tptr == ' ' || (*tptr == '\t')) tptr++;
261: if (strlen(tptr) > sizeof(hold_where)) continue;
262: strcpy(hold_where, tptr);
263: ptr = hold_where + strlen(hold_where);
264: while (!isalnum(*--ptr));
265: *++ptr = '\0';
266: if (strlen(hold_where)) whereto = hold_where;
267: }
268: else if (!first && (*eptr == 'H') && !argc) {
269: fputs(eptr+1, stdout);
270: }
271: }
272: if (strncmp(eptr, "-----", 5) != 0) {
273: fprintf(stderr, "library: No dash list for %s option form! No requests sent\n", ptr + 2);
274: exit(1);
275: }
276: if ((eptr == endptr) && !numreq) {
277: fprintf(stderr, "library: No Send line for this option! No requests sent\n");
278: exit(1);
279: }
280: /* don't just duplicate this if user wiped out last being entered */
281: if ((eptr < (response + 10)) || strncmp(eptr-6, "**-**", 5)) {
282: strcpy(eptr, "**-**\n");
283: eptr += strlen(eptr);
284: }
285: endptr = eptr;
286: if ((said_blank == 0) && !argc) {
287: fprintf(stdout, "Use blank lines to end multi-line inputs\n");
288: said_blank++;
289: }
290: first++;
291: /* now read in an utilize the form */
292: while (fgets(eptr, 150, tfile) != NULL) {
293: /* Level handling */
294: if (level && (*eptr == '@')) {
295: if ((*(eptr+1)-'0') != level) continue;
296: strcpy(eptr, eptr+2);
297: }
298: /* skip comment and help lines */
299: if ((*eptr == '#') || (*eptr == '?')) continue;
300: /* check if getting low on space */
301: if ((resp_size - strlen(response)) < 500) {
302: resp_size += 5000;
303: ptr = response;
304: if ((response = realloc(response, resp_size)) == NULL) {
305: fprintf(stderr, "library: Couldn't get space for this much request (%d). No requests sent!\n", resp_size);
306: exit(1);
307: }
308: eptr += (response - ptr);
309: endptr += (response - ptr);
310: }
311: /* find how much the user can respond */
312: ptr = eptr + strlen(eptr) - 1;
313: if (*ptr == '\n') *ptr-- = '\0';
314: while (isdigit(*ptr) && (ptr > eptr)) ptr--;
315: num = atoi(ptr+1);
316: save_respond = 0;
317: if (*ptr == 'S') {
318: if (respond_flag == 0) {
319: libptr = get_lib_file(libfile);
320: if (libptr == NULL)
321: respond_flag = -1;
322: else
323: respond_flag = 1;
324: }
325: if ((respond_flag > 0) && (num == 1)) save_respond = 1;
326: ptr--;
327: }
328: if ((*ptr != '+') && (*ptr != '*')) {
329: if (!argc) printf("%s\n", eptr);
330: continue;
331: }
332: tchar = *ptr--;
333: while ((*ptr == ' ') || (*ptr == '\t')) ptr--;
334: *++ptr = '\0';
335: if (num == 0) num = 1;
336: /* save the query in case we need it later */
337: strcpy(query, eptr);
338: /* now get in a maximum of num lines */
339: strcat(eptr, ": ");
340: if (strlen(eptr) > 30) strcat(eptr, "\n\t");
341: beginptr = ptr = eptr + strlen(eptr);
342: j = 0; /* use j to count the lines */
343: max = num;
344: use_respond = 0;
345: while (num-- > 0) {
346: /* check if saved response to this */
347: tptr = NULL;
348: if (!j && !argc && save_respond) {
349: /* remove () expressions off query */
350: tptr = query + strlen(query) - 1;
351: if (*tptr == ')') {
352: tptr = strrchr(query, '(');
353: if (tptr != NULL) {
354: while ((*tptr == ' ') || (*tptr == '\t') || (*tptr == '(')) tptr--;
355: *++tptr = '\0';
356: }
357: }
358: tptr = find_respond(libptr, filename, query);
359: if (tptr != NULL) {
360: printf("\nUse response \"%s\" for \"%s\"?\n Enter y (or return) if okay, n to give new response: ",
361: tptr, query);
362: if (fgets(ptr, 80, stdin) == NULL) {
363: fprintf(stderr, "Input ended in middle? No requests sent\n");
364: exit(1);
365: }
366: if ((clptr = strchr(ptr, '\n')) != NULL) *clptr = '\0';
367: if ((*ptr == 'y') || (*ptr == 'Y') || !strlen(ptr)) {
368: use_respond = 1;
369: strcpy(ptr, tptr);
370: num = 0;
371: }
372: else if (*ptr == '.') {
373: use_respond = 1;
374: strcpy(ptr, ".");
375: }
376: else
377: *ptr = '\0';
378: } /* end of response found in file */
379: } /* deal with saved response */
380: if (!use_respond && !j && !argc) {
381: printf("Enter %s", eptr);
382: if (max >1)
383: printf("(Allowed a maximum %d lines)\n==> ", max);
384: else if (strlen(eptr) > 30)
385: printf("\n==> ");
386: }
387: else if (!argc && !use_respond) printf("==> ");
388: if (argc) {
389: tptr = argv[0];
390: if (*tptr == '-') tptr++;
391: strcpy(ptr, tptr);
392: argc--;
393: argv++;
394: used_arg = 1;
395: }
396: else if (!use_respond && (fgets(ptr,500, stdin) == NULL)) {
397: fprintf(stderr, "Input ended in middle? No requests sent\n");
398: exit(1);
399: }
400: if ((clptr = strchr(ptr, '\n')) != NULL) *clptr = '\0';
401: if (*ptr == '?') {
402: /* give any help lines - make it so we can get back */
403: i = ftell(tfile);
404: k = 0;
405: while (fgets(templine, 200, tfile) != NULL) {
406: tptr = templine;
407: if (*tptr == '#') continue;
408: /* Level handling */
409: if (level && (*tptr == '@')) {
410: if ((*(tptr+1)-'0') != level) continue;
411: tptr += 2;
412: }
413: if (*tptr == '#') continue;
414: if (*tptr == '?') {
415: if ((*(tptr+1) == '!') || (*(tptr+1) == '@'))
416: fputs(tptr+2, stdout);
417: else
418: fputs(tptr+1, stdout);
419: k++;
420: }
421: else break;
422: }
423: if (!k) printf("Sorry - no help for this prompt!\n");
424: fseek(tfile, (long)i, 0);
425: num++;
426: *ptr = '\0';
427: continue;
428: }
429: else if (*ptr == '.') {
430: printf("A period wipes out a request. Use return (a blank line) to end input.\n");
431: printf(" Wipe out request? (n): ");
432: if (fgets(ptr, 80, stdin) == NULL) {
433: fprintf(stderr, "Input ended in middle? No requests sent\n");
434: exit(1);
435: }
436: if ((*ptr == 'y') || (*ptr == 'Y')) {
437: j = -1;
438: *eptr = '\0';
439: break;
440: }
441: *ptr = '\0';
442: num++;
443: continue;
444: }
445: else if (strncmp(ptr, "~e", 2) == 0) {
446: if (max < 20) {
447: fprintf(stderr, "Sorry - library() doesn't allow editer use on responses of %d lines\n", max);
448: *ptr = '\0';
449: num++;
450: continue;
451: }
452: editor = getenv("EDITOR");
453: if (editor == NULL) editor = "ed";
454: *ptr = '\0';
455: sprintf(tempname, "/tmp/Lo25.%d", getpid());
456: if ((efile = fopen(tempname, "w")) == NULL) {
457: fprintf(stderr, "Couldn't open tmp file %s for edit\n", tempname);
458: *ptr = '\0';
459: num++;
460: continue;
461: }
462: if (strlen(beginptr)) {
463: /* write out what is there - clean up start of lines */
464: tptr = beginptr;
465: while ((tptr != NULL) && *tptr) {
466: while (*tptr && (*tptr == ' ') || (*tptr == '\t')) tptr++;
467: tptr2 = strchr(tptr, '\n');
468: if (tptr2 != NULL) *tptr2++ = '\0';
469: fputs(tptr, efile);
470: fputc('\n', efile);
471: tptr = tptr2;
472: }
473: }
474: fclose(efile);
475: sprintf(ptr+1, "%s %s", editor, tempname);
476: system(ptr+1);
477: if ((efile = fopen(tempname, "r")) == NULL) {
478: fprintf(stderr, "Couldn't open tmp file %s after edit\n", tempname);
479: num++;
480: *ptr = '\0';
481: continue;
482: }
483: ptr = beginptr;
484: while (fgets(ptr, 1000, efile) != NULL) {
485: ptr += strlen(ptr);
486: *ptr++ = '\t';
487: j++;
488: }
489: *ptr = '\0';
490: fclose(efile);
491: unlink(tempname);
492: printf("(Continue entry)\n");
493: continue;
494: }
495: else if (strncmp(ptr, "~r", 2) == 0) {
496: if (max < 20) {
497: fprintf(stderr, "Sorry - library() doesn't allow file reading on responses of %d lines\n", max);
498: *ptr = '\0';
499: num++;
500: continue;
501: }
502: tptr = ptr;
503: ptr += 2;
504: while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
505: if ((efile = fopen(ptr, "r")) == NULL) {
506: fprintf(stderr, "Couldn't open file %s for reading\n", ptr);
507: num++;
508: ptr = tptr;
509: *ptr = '\0';
510: continue;
511: }
512: ptr = tptr;
513: while (fgets(ptr, 1000, efile) != NULL) {
514: ptr += strlen(ptr);
515: /* check if getting low on space */
516: if ((resp_size - strlen(response)) < 500) {
517: resp_size += 5000;
518: tptr = response;
519: if ((response = realloc(response, resp_size)) == NULL) {
520: fprintf(stderr, "library: Couldn't get space for this much request (%d). No requests sent!\n", resp_size);
521: exit(1);
522: }
523: ptr += (response - tptr);
524: eptr += (response - tptr);
525: endptr += (response - tptr);
526: }
527: *ptr++ = '\t';
528: j++;
529: }
530: *ptr = '\0';
531: fclose(efile);
532: printf("(Continue entry)\n");
533: continue;
534: }
535: if (!strlen(ptr)) {
536: if (!j && tchar == '+') {
537: if (argc) {
538: printf("Attempted to skip required input. Parameter handling stopped!\n");
539: argc = 0;
540: }
541: printf("This input is required. Please enter a question mark (?) for help\n");
542: num++;
543: continue;
544: }
545: break;
546: }
547: #ifndef MSDOS
548: /* User did some input here - check if allowed is restricted */
549: i = ftell(tfile);
550: k = 0;
551: while (fgets(templine, 200, tfile) != NULL) {
552: tptr = templine;
553: if (*(tptr) == '#') continue;
554: /* Level handling */
555: if (level && (*tptr == '@')) {
556: if ((*(tptr+1)-'0') != level) continue;
557: tptr += 2;
558: }
559: if (*(tptr) != '?') break;
560: tchar2 = *(tptr+1);
561: if ((tchar2 != '!') && (tchar2 != '@')) continue;
562: k = -1; /* flag that have option list */
563: tptr += 2;
564: while (*tptr == ' ') tptr++;
565: tptr2 = strchr(tptr, '\n');
566: if (tptr2 != NULL) *tptr2 = '\0';
567: tptr2 = strchr(tptr, '\t');
568: if (tptr2 != NULL) *tptr2++ = '\0';
569: if (strcmp(ptr, tptr) == 0) {
570: k = 1;
571: if (tchar2 == '@') k++;
572: if (tptr2 != NULL) {
573: strcpy(ptr, tptr2);
574: if (!argc) printf("Requested: %s\n", ptr);
575: }
576: break;
577: }
578: }
579:
580: /* see if just jumping a level */
581: if (k == 2) {
582: level++;
583: *ptr = '\0';
584: continue;
585: }
586: fseek(tfile, (long)i, 0);
587: if (k < 0) {
588: printf("Request must be from those listed. Do a question mark (?) for list\n");
589: num++;
590: *ptr = '\0';
591: continue;
592: }
593: #endif /* Got rid of all this cause DOS rouble */
594: /* have real result at ptr */
595: ptr += strlen(ptr);
596: strcpy(ptr, "\n\t");
597: ptr += 2;
598: j++;
599: if (level) level = 1;
600: }
601: if (j > 0) {
602: if (save_respond && !use_respond) {
603: tptr = strchr(eptr, ':');
604: if (tptr++ != NULL) {
605: while ((*tptr == ' ') || (*tptr == '\t') || (*tptr == '\n')) tptr++;
606: put_respond(libptr, filename, query, tptr);
607: respond_flag++;
608: }
609: }
610: *--ptr = '\0';
611: eptr += strlen(eptr);
612: }
613: else if (j < 0) {
614: if (eptr != endptr) {
615: fprintf(stderr, "Request being entered is removed!\n");
616: eptr = endptr;
617: j = -5;
618: }
619: break;
620: }
621: } /* end of dealing with this form */
622: /* See if we need to loop */
623: if (j >= 0) {
624: if ((eptr == endptr) && !nodata)
625: fprintf(stderr, "Request without data skipped!\n");
626: else
627: numreq++;
628: if (!multi_req) break;
629: }
630: /* If they invoked with arguments, no pause here */
631: if (used_arg) break;
632: printf("\nHave entered %d request%s. Enter another (n/y): ",
633: numreq, ((numreq != 1) ? "s" : ""));
634: if (fgets(instr, 80, stdin) == NULL) break;
635: if ((clptr = strchr(instr, '\n')) != NULL) *clptr = '\0';
636: if ((*instr != 'y') && (*instr != 'Y')) break;
637: } /* loop for multiple requests */
638: *eptr = '\0';
639: fclose(tfile);
640: /* output the record */
641: if (numreq == 0) {
642: printf("No requests sent!\n");
643: return;
644: }
645: #ifdef MSDOS
646: if ((sendfile = fopen("\\pipe.tmp", "w")) == NULL) {
647: #else
648: sprintf(instr, "%s %s", Cmd, whereto);
649: if ((sendfile = popen(instr, "w")) == NULL) {
650: #endif
651: fprintf(stderr, "Couldn't open mail pipe to send requests! No requests sent\n");
652: exit(1);
653: }
654: fprintf(sendfile, "* *#OPTION2/5-%s\n%s\n%s\n", libcntl, pan, req_name);
655: fputs(response, sendfile);
656: #ifdef MSDOS
657: fclose(sendfile);
658: sprintf(instr, "%s -f \\pipe.tmp -slibRequest %s", Cmd, whereto);
659: system(instr);
660: unlink("\\pipe.tmp");
661: #else
662: pclose(sendfile);
663: #endif
664: if (logfile != NULL && !nolog) {
665: ptr = strchr(response, '\n')+1; /* skip send info */
666: while (ptr != NULL) {
667: ptr = strchr(ptr, '\n')+1; /* skip **-** line */
668: if (strlen(ptr) < 5) break;
669: fprintf(logfile, "From %s %sSubject: option %d request\n",
670: req_name, dateptr, optnum);
671: tptr = ptr;
672: while ((tptr = strchr(tptr, '\n')) != NULL)
673: if (strncmp(++tptr, "**-**", 5) == 0) break;
674: if (tptr != NULL) *tptr++ = '\0';
675: fputs(ptr, logfile);
676: fputc('\n', logfile);
677: ptr = tptr;
678: }
679: fclose(logfile);
680: }
681: fprintf(stdout, format, numreq);
682: /* save new responses if any */
683: if (respond_flag > 1) put_lib_file(libfile, libptr);
684: return;
685: }
686:
687: char *get_lib_file(filename)
688: char *filename;
689: {
690: char *ptr, *ptr2;
691: FILE *file;
692: struct stat stbuf;
693: int st_rtn;
694:
695: /* get the displays if there are any */
696: st_rtn = stat(filename, &stbuf);
697: if (st_rtn != 0) stbuf.st_size = 10;
698: ptr = malloc(stbuf.st_size + 2000);
699: if (ptr == NULL) {
700: fprintf(stderr, "library: Couldn't get system space for buffer!\n");
701: exit(1);
702: }
703: /* leave emptr char before just to simplify stuff later */
704: *ptr++ = '\0';
705: *ptr = '\0';
706: if (st_rtn) return(ptr);
707: if ((file = fopen(filename, "r")) == NULL) return(ptr);
708: ptr2 = ptr;
709: while (fgets(ptr2, 1000, file) != NULL)
710: ptr2 += strlen(ptr2);
711: *ptr2++ = '\0';
712: *ptr2 = '\0';
713: fclose(file);
714: return(ptr);
715: }
716:
717: void put_lib_file(filename, ptr)
718: char *filename; /* the name of the lib prompt file */
719: char *ptr;
720: {
721: FILE *file;
722:
723: if ((file = fopen(filename, "w")) != NULL) {
724: fputs(ptr, file);
725: fclose(file);
726: }
727: return;
728: }
729:
730: char *find_respond(ptr, prefix, lookfor)
731: register char *ptr;
732: char *prefix;
733: char *lookfor;
734: {
735: static char buf[200];
736: int len;
737: char *ptr2;
738:
739: /* don't even attempt on long strings */
740: if ((strlen(prefix)+strlen(lookfor)) >= 200) return(NULL);
741: sprintf(buf, "%s/%s", prefix, lookfor);
742: ptr2 = buf + strlen(buf) - 1;
743: while ((*ptr2 == '\t') || (*ptr2 == '\n') || (*ptr2 == ' ')) ptr2--;
744: if (*ptr2 != ':') *++ptr2 = ':';
745: *++ptr2 = '\0';
746: len = strlen(buf);
747:
748: while (*ptr) {
749: if (!strncmp(ptr, buf, len)) {
750: ptr += len; /* point it after the : */
751: while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
752: ptr2 = strchr(ptr, '\n');
753: if (ptr2 != NULL) *ptr2 = '\0';
754: if (strlen(ptr) > 200) {
755: if (ptr2 != NULL) *ptr2 = '\n';
756: return(NULL);
757: }
758: strcpy(buf, ptr);
759: if (ptr2 != NULL) *ptr2 = '\n';
760: ptr2 = strchr(buf, '\n');
761: if (ptr2 != NULL) *ptr2 = '\0';
762: return(buf);
763: }
764: ptr = strchr(ptr, '\n');
765: if (ptr++ == NULL) return(NULL);
766: }
767: return(NULL);
768: }
769:
770: void put_respond(fullptr, prefix, newstuff, response)
771: register char *fullptr;
772: char *prefix;
773: char *newstuff;
774: char *response;
775: {
776: char buf[200];
777: int len;
778: register char *ptr2;
779:
780: /* don't even attempt on long strings */
781: if ((strlen(prefix)+strlen(newstuff)) >= 200) return;
782:
783: /* remove any existing response */
784: sprintf(buf, "%s/%s", prefix, newstuff);
785: ptr2 = buf + strlen(buf) - 1;
786: while ((*ptr2 == '\t') || (*ptr2 == '\n') || (*ptr2 == ' ')) ptr2--;
787: if (*ptr2 != ':') *++ptr2 = ':';
788: *++ptr2 = '\0';
789: len = strlen(buf);
790:
791: while (*fullptr) {
792: ptr2 = strchr(fullptr, '\n');
793: if (ptr2 == NULL)
794: ptr2 = fullptr + strlen(fullptr);
795: else
796: ptr2++;
797: if (!strncmp(fullptr, buf, len)) {
798: strcpy(fullptr, ptr2);
799: break;
800: }
801: fullptr = ptr2;
802: }
803:
804: fullptr += strlen(fullptr);
805: if (*(fullptr-1) != '\n') {
806: *fullptr++ = '\n';
807: *fullptr = '\0';
808: }
809: sprintf(fullptr, "%s %s\n", buf, response);
810: fullptr = strchr(fullptr, '\n');
811: if (fullptr != NULL)
812: *++fullptr = '\0';
813: return;
814: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.