request.c (3903B)
1 /* 2 * 3 * Things used to handle special requests (eg. manual feed) globally or on a per 4 * page basis. Requests are passed through to the translator using the -R option. 5 * The argument to -R can be "request", "request:page", or "request:page:file". 6 * If page is omitted (as in the first form) or set to 0 request will be applied 7 * to the global environment. In all other cases it applies only to the selected 8 * page. If a file is given, page must be supplied, and the lookup is in that file 9 * rather than *requestfile. 10 * 11 */ 12 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include <string.h> 16 #include <unistd.h> 17 18 #include "gen.h" /* general purpose definitions */ 19 #include "ext.h" 20 #include "request.h" /* a few special definitions */ 21 #include "path.h" /* for the default request file */ 22 23 Request request[MAXREQUEST]; /* next page or global request */ 24 int nextreq = 0; /* goes in request[nextreq] */ 25 char *requestfile = REQUESTFILE; /* default lookup file */ 26 27 void dumprequest(char *want, char *file, FILE *fp_out); 28 extern void error(int errtype, char *fmt, ...); 29 30 /*****************************************************************************/ 31 32 void 33 saverequest(want) 34 35 char *want; /* grab code for this stuff */ 36 37 { 38 39 char *page; /* and save it for this page */ 40 char *strtok(); 41 42 /* 43 * 44 * Save the request until we get to appropriate page - don't even bother with 45 * the lookup right now. Format of *want string is "request", "request:page", or 46 * "request:page:file", and we assume we can change the string here as needed. 47 * If page is omitted or given as 0 the request will be done globally. If *want 48 * includes a file, request and page must also be given, and in that case *file 49 * will be used for the lookup. 50 * 51 */ 52 53 if ( nextreq < MAXREQUEST ) { 54 request[nextreq].want = strtok(want, ": "); 55 if ( (page = strtok(NULL, ": ")) == NULL ) 56 request[nextreq].page = 0; 57 else request[nextreq].page = atoi(page); 58 if ( (request[nextreq].file = strtok(NULL, ": ")) == NULL ) 59 request[nextreq].file = requestfile; 60 nextreq++; 61 } else error(NON_FATAL, "too many requests - ignoring %s", want); 62 63 } /* End of saverequest */ 64 65 /*****************************************************************************/ 66 67 void 68 writerequest(page, fp_out) 69 70 int page; /* write everything for this page */ 71 FILE *fp_out; /* to this file */ 72 73 { 74 75 int i; /* loop index */ 76 77 /* 78 * 79 * Writes out all the requests that have been saved for page. Page 0 refers to 80 * the global environment and is done during initial setup. 81 * 82 */ 83 84 for ( i = 0; i < nextreq; i++ ) 85 if ( request[i].page == page ) 86 dumprequest(request[i].want, request[i].file, fp_out); 87 88 } /* End of writerequest */ 89 90 /*****************************************************************************/ 91 92 void 93 dumprequest(want, file, fp_out) 94 95 char *want; /* look for this string */ 96 char *file; /* in this file */ 97 FILE *fp_out; /* and write the value out here */ 98 99 { 100 101 char buf[100]; /* line buffer for reading *file */ 102 FILE *fp_in; 103 104 /* 105 * 106 * Looks for *want in the request file and if it's found the associated value 107 * is copied to the output file. Keywords (ie. the *want strings) begin an @ in 108 * the first column of file, while the values (ie. the stuff that's copied to 109 * the output file) starts on the next line and extends to the next keyword or 110 * to the end of file. 111 * 112 */ 113 114 if ( (fp_in = fopen(file, "r")) != NULL ) { 115 while ( fgets(buf, sizeof(buf), fp_in) != NULL ) 116 if ( buf[0] == '@' && strncmp(want, &buf[1], strlen(want)) == 0 ) 117 while ( fgets(buf, sizeof(buf), fp_in) != NULL ) 118 if ( buf[0] == '#' || buf[0] == '%' ) 119 continue; 120 else if ( buf[0] != '@' ) 121 fprintf(fp_out, "%s", buf); 122 else break; 123 fclose(fp_in); 124 } /* End if */ 125 126 } /* End of dumprequest */ 127 128 /*****************************************************************************/