1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29
30 /*
31 *
32 * Things used to handle special requests (eg. manual feed) globally or on a per
33 * page basis. Requests are passed through to the translator using the -R option.
34 * The argument to -R can be "request", "request:page", or "request:page:file".
35 * If page is omitted (as in the first form) or set to 0 request will be applied
36 * to the global environment. In all other cases it applies only to the selected
37 * page. If a file is given, page must be supplied, and the lookup is in that file
38 * rather than *requestfile.
39 *
40 */
41
42
43 #include <stdio.h>
44
45 #include "gen.h" /* general purpose definitions */
46 #include "request.h" /* a few special definitions */
47 #include "path.h" /* for the default request file */
48
49
50 Request request[MAXREQUEST]; /* next page or global request */
51 int nextreq = 0; /* goes in request[nextreq] */
52 char *requestfile = REQUESTFILE; /* default lookup file */
53
54 void dumprequest(char *, char *, FILE *);
55
56 /*****************************************************************************/
57
58
59 void
saverequest(char * want)60 saverequest(char *want)
61 /* grab code for this stuff */
62 {
63 char *page; /* and save it for this page */
64 char *strtok();
65
66 /*
67 *
68 * Save the request until we get to appropriate page - don't even bother with
69 * the lookup right now. Format of *want string is "request", "request:page", or
70 * "request:page:file", and we assume we can change the string here as needed.
71 * If page is omitted or given as 0 the request will be done globally. If *want
72 * includes a file, request and page must also be given, and in that case *file
73 * will be used for the lookup.
74 *
75 */
76
77
78 if ( nextreq < MAXREQUEST ) {
79 request[nextreq].want = strtok(want, ": ");
80 if ( (page = strtok(NULL, ": ")) == NULL )
81 request[nextreq].page = 0;
82 else request[nextreq].page = atoi(page);
83 if ( (request[nextreq].file = strtok(NULL, ": ")) == NULL )
84 request[nextreq].file = requestfile;
85 nextreq++;
86 } else error(NON_FATAL, "too many requests - ignoring %s", want);
87
88 } /* End of saverequest */
89
90
91 /*****************************************************************************/
92
93
94 void
writerequest(int page,FILE * fp_out)95 writerequest(int page, FILE *fp_out)
96 /* page - write everything for this page */
97 /* fp_out - to this file */
98 {
99 int i; /* loop index */
100
101 /*
102 *
103 * Writes out all the requests that have been saved for page. Page 0 refers to
104 * the global environment and is done during initial setup.
105 *
106 */
107
108
109 for ( i = 0; i < nextreq; i++ )
110 if ( request[i].page == page )
111 dumprequest(request[i].want, request[i].file, fp_out);
112
113 } /* End of writerequest */
114
115
116 /*****************************************************************************/
117
118
119 void
dumprequest(char * want,char * file,FILE * fp_out)120 dumprequest(char *want, char *file, FILE *fp_out)
121 /* want - look for this string */
122 /* file - in this file */
123 /* fp_out - and write the value out here */
124 {
125 char buf[100]; /* line buffer for reading *file */
126 FILE *fp_in;
127
128 /*
129 *
130 * Looks for *want in the request file and if it's found the associated value
131 * is copied to the output file. Keywords (ie. the *want strings) begin an @ in
132 * the first column of file, while the values (ie. the stuff that's copied to
133 * the output file) starts on the next line and extends to the next keyword or
134 * to the end of file.
135 *
136 */
137
138
139 if ( (fp_in = fopen(file, "r")) != NULL ) {
140 while ( fgets(buf, sizeof(buf), fp_in) != NULL )
141 if ( buf[0] == '@' && strncmp(want, &buf[1], strlen(want)) == 0 )
142 while ( fgets(buf, sizeof(buf), fp_in) != NULL )
143 if ( buf[0] == '#' || buf[0] == '%' )
144 continue;
145 else if ( buf[0] != '@' )
146 fprintf(fp_out, "%s", buf);
147 else break;
148 fclose(fp_in);
149 } /* End if */
150
151 } /* End of dumprequest */
152
153
154 /*****************************************************************************/
155
156