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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 /*
34  *
35  * Things used to handle special requests (eg. manual feed) globally or on a per
36  * page basis. Requests are passed through to the translator using the -R option.
37  * The argument to -R can be "request", "request:page", or "request:page:file".
38  * If page is omitted (as in the first form) or set to 0 request will be applied
39  * to the global environment. In all other cases it applies only to the selected
40  * page. If a file is given, page must be supplied, and the lookup is in that file
41  * rather than *requestfile.
42  *
43  */
44 
45 
46 #include <stdio.h>
47 
48 #include "gen.h"			/* general purpose definitions */
49 #include "request.h"			/* a few special definitions */
50 #include "path.h"			/* for the default request file */
51 
52 
53 Request	request[MAXREQUEST];		/* next page or global request */
54 int	nextreq = 0;			/* goes in request[nextreq] */
55 char	*requestfile = REQUESTFILE;	/* default lookup file */
56 
57 void dumprequest(char *, char *, FILE *);
58 
59 /*****************************************************************************/
60 
61 
62 void
63 saverequest(char *want)
64 	/* grab code for this stuff */
65 {
66     char	*page;			/* and save it for this page */
67     char	*strtok();
68 
69 /*
70  *
71  * Save the request until we get to appropriate page - don't even bother with
72  * the lookup right now. Format of *want string is "request", "request:page", or
73  * "request:page:file", and we assume we can change the string here as needed.
74  * If page is omitted or given as 0 the request will be done globally. If *want
75  * includes a file, request and page must also be given, and in that case *file
76  * will be used for the lookup.
77  *
78  */
79 
80 
81     if ( nextreq < MAXREQUEST )  {
82 	request[nextreq].want = strtok(want, ": ");
83 	if ( (page = strtok(NULL, ": ")) == NULL )
84 	    request[nextreq].page = 0;
85 	else request[nextreq].page = atoi(page);
86 	if ( (request[nextreq].file = strtok(NULL, ": ")) == NULL )
87 	    request[nextreq].file = requestfile;
88 	nextreq++;
89     } else error(NON_FATAL, "too many requests - ignoring %s", want);
90 
91 }   /* End of saverequest */
92 
93 
94 /*****************************************************************************/
95 
96 
97 void
98 writerequest(int page, FILE *fp_out)
99 	/* page - write everything for this page */
100 	/* fp_out - to this file */
101 {
102     int		i;			/* loop index */
103 
104 /*
105  *
106  * Writes out all the requests that have been saved for page. Page 0 refers to
107  * the global environment and is done during initial setup.
108  *
109  */
110 
111 
112     for ( i = 0; i < nextreq; i++ )
113 	if ( request[i].page == page )
114 	    dumprequest(request[i].want, request[i].file, fp_out);
115 
116 }   /* End of writerequest */
117 
118 
119 /*****************************************************************************/
120 
121 
122 void
123 dumprequest(char *want, char *file, FILE *fp_out)
124 	/* want - look for this string */
125 	/* file - in this file */
126 	/* fp_out - and write the value out here */
127 {
128     char	buf[100];		/* line buffer for reading *file */
129     FILE	*fp_in;
130 
131 /*
132  *
133  * Looks for *want in the request file and if it's found the associated value
134  * is copied to the output file. Keywords (ie. the *want strings) begin an @ in
135  * the first column of file, while the values (ie. the stuff that's copied to
136  * the output file) starts on the next line and extends to the next keyword or
137  * to the end of file.
138  *
139  */
140 
141 
142     if ( (fp_in = fopen(file, "r")) != NULL )  {
143 	while ( fgets(buf, sizeof(buf), fp_in) != NULL )
144 	    if ( buf[0] == '@' && strncmp(want, &buf[1], strlen(want)) == 0 )
145 		while ( fgets(buf, sizeof(buf), fp_in) != NULL )
146 		    if ( buf[0] == '#' || buf[0] == '%' )
147 			continue;
148 		    else if ( buf[0] != '@' )
149 			fprintf(fp_out, "%s", buf);
150 		    else break;
151 	fclose(fp_in);
152     }	/* End if */
153 
154 }   /* End of dumprequest */
155 
156 
157 /*****************************************************************************/
158 
159