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