xref: /plan9/sys/src/cmd/postscript/common/request.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
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 
15 #include "gen.h"			/* general purpose definitions */
16 #include "request.h"			/* a few special definitions */
17 #include "path.h"			/* for the default request file */
18 
19 Request	request[MAXREQUEST];		/* next page or global request */
20 int	nextreq = 0;			/* goes in request[nextreq] */
21 char	*requestfile = REQUESTFILE;	/* default lookup file */
22 
23 /*****************************************************************************/
24 
25 saverequest(want)
26 
27     char	*want;			/* grab code for this stuff */
28 
29 {
30 
31     char	*page;			/* and save it for this page */
32     char	*strtok();
33 
34 /*
35  *
36  * Save the request until we get to appropriate page - don't even bother with
37  * the lookup right now. Format of *want string is "request", "request:page", or
38  * "request:page:file", and we assume we can change the string here as needed.
39  * If page is omitted or given as 0 the request will be done globally. If *want
40  * includes a file, request and page must also be given, and in that case *file
41  * will be used for the lookup.
42  *
43  */
44 
45     if ( nextreq < MAXREQUEST )  {
46 	request[nextreq].want = strtok(want, ": ");
47 	if ( (page = strtok(NULL, ": ")) == NULL )
48 	    request[nextreq].page = 0;
49 	else request[nextreq].page = atoi(page);
50 	if ( (request[nextreq].file = strtok(NULL, ": ")) == NULL )
51 	    request[nextreq].file = requestfile;
52 	nextreq++;
53     } else error(NON_FATAL, "too many requests - ignoring %s", want);
54 
55 }   /* End of saverequest */
56 
57 /*****************************************************************************/
58 
59 writerequest(page, fp_out)
60 
61     int		page;			/* write everything for this page */
62     FILE	*fp_out;		/* to this file */
63 
64 {
65 
66     int		i;			/* loop index */
67 
68 /*
69  *
70  * Writes out all the requests that have been saved for page. Page 0 refers to
71  * the global environment and is done during initial setup.
72  *
73  */
74 
75     for ( i = 0; i < nextreq; i++ )
76 	if ( request[i].page == page )
77 	    dumprequest(request[i].want, request[i].file, fp_out);
78 
79 }   /* End of writerequest */
80 
81 /*****************************************************************************/
82 
83 dumprequest(want, file, fp_out)
84 
85     char	*want;			/* look for this string */
86     char	*file;			/* in this file */
87     FILE	*fp_out;		/* and write the value out here */
88 
89 {
90 
91     char	buf[100];		/* line buffer for reading *file */
92     FILE	*fp_in;
93 
94 /*
95  *
96  * Looks for *want in the request file and if it's found the associated value
97  * is copied to the output file. Keywords (ie. the *want strings) begin an @ in
98  * the first column of file, while the values (ie. the stuff that's copied to
99  * the output file) starts on the next line and extends to the next keyword or
100  * to the end of file.
101  *
102  */
103 
104     if ( (fp_in = fopen(file, "r")) != NULL )  {
105 	while ( fgets(buf, sizeof(buf), fp_in) != NULL )
106 	    if ( buf[0] == '@' && strncmp(want, &buf[1], strlen(want)) == 0 )
107 		while ( fgets(buf, sizeof(buf), fp_in) != NULL )
108 		    if ( buf[0] == '#' || buf[0] == '%' )
109 			continue;
110 		    else if ( buf[0] != '@' )
111 			fprintf(fp_out, "%s", buf);
112 		    else break;
113 	fclose(fp_in);
114     }	/* End if */
115 
116 }   /* End of dumprequest */
117 
118 /*****************************************************************************/
119 
120