URI:
       trequest.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       trequest.c (3930B)
       ---
            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 extern        void        dumprequest();
           67 
           68 void
           69 writerequest(page, fp_out)
           70 
           71     int                page;                        /* write everything for this page */
           72     FILE        *fp_out;                /* to this file */
           73 
           74 {
           75 
           76     int                i;                        /* loop index */
           77 
           78 /*
           79  *
           80  * Writes out all the requests that have been saved for page. Page 0 refers to
           81  * the global environment and is done during initial setup.
           82  *
           83  */
           84 
           85     for ( i = 0; i < nextreq; i++ )
           86         if ( request[i].page == page )
           87             dumprequest(request[i].want, request[i].file, fp_out);
           88 
           89 }   /* End of writerequest */
           90 
           91 /*****************************************************************************/
           92 
           93 void
           94 dumprequest(want, file, fp_out)
           95 
           96     char        *want;                        /* look for this string */
           97     char        *file;                        /* in this file */
           98     FILE        *fp_out;                /* and write the value out here */
           99 
          100 {
          101 
          102     char        buf[100];                /* line buffer for reading *file */
          103     FILE        *fp_in;
          104 
          105 /*
          106  *
          107  * Looks for *want in the request file and if it's found the associated value
          108  * is copied to the output file. Keywords (ie. the *want strings) begin an @ in
          109  * the first column of file, while the values (ie. the stuff that's copied to
          110  * the output file) starts on the next line and extends to the next keyword or
          111  * to the end of file.
          112  *
          113  */
          114 
          115     if ( (fp_in = fopen(file, "r")) != NULL )  {
          116         while ( fgets(buf, sizeof(buf), fp_in) != NULL )
          117             if ( buf[0] == '@' && strncmp(want, &buf[1], strlen(want)) == 0 )
          118                 while ( fgets(buf, sizeof(buf), fp_in) != NULL )
          119                     if ( buf[0] == '#' || buf[0] == '%' )
          120                         continue;
          121                     else if ( buf[0] != '@' )
          122                         fprintf(fp_out, "%s", buf);
          123                     else break;
          124         fclose(fp_in);
          125     }        /* End if */
          126 
          127 }   /* End of dumprequest */
          128 
          129 /*****************************************************************************/