aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/postscript/common/request.c
blob: 2e96093ba65cc70ea9d0b585e5649b38a7b6319f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 *
 * Things used to handle special requests (eg. manual feed) globally or on a per
 * page basis. Requests are passed through to the translator using the -R option.
 * The argument to -R can be "request", "request:page", or "request:page:file".
 * If page is omitted (as in the first form) or set to 0 request will be applied
 * to the global environment. In all other cases it applies only to the selected
 * page. If a file is given, page must be supplied, and the lookup is in that file
 * rather than *requestfile.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "gen.h"			/* general purpose definitions */
#include "ext.h"
#include "request.h"			/* a few special definitions */
#include "path.h"			/* for the default request file */

Request	request[MAXREQUEST];		/* next page or global request */
int	nextreq = 0;			/* goes in request[nextreq] */
char	*requestfile = REQUESTFILE;	/* default lookup file */

void dumprequest(char *want, char *file, FILE *fp_out);
extern void error(int errtype, char *fmt, ...);

/*****************************************************************************/

void
saverequest(want)

    char	*want;			/* grab code for this stuff */

{

    char	*page;			/* and save it for this page */
    char	*strtok();

/*
 *
 * Save the request until we get to appropriate page - don't even bother with
 * the lookup right now. Format of *want string is "request", "request:page", or
 * "request:page:file", and we assume we can change the string here as needed.
 * If page is omitted or given as 0 the request will be done globally. If *want
 * includes a file, request and page must also be given, and in that case *file
 * will be used for the lookup.
 *
 */

    if ( nextreq < MAXREQUEST )  {
	request[nextreq].want = strtok(want, ": ");
	if ( (page = strtok(NULL, ": ")) == NULL )
	    request[nextreq].page = 0;
	else request[nextreq].page = atoi(page);
	if ( (request[nextreq].file = strtok(NULL, ": ")) == NULL )
	    request[nextreq].file = requestfile;
	nextreq++;
    } else error(NON_FATAL, "too many requests - ignoring %s", want);

}   /* End of saverequest */

/*****************************************************************************/
extern	void	dumprequest();

void
writerequest(page, fp_out)

    int		page;			/* write everything for this page */
    FILE	*fp_out;		/* to this file */

{

    int		i;			/* loop index */

/*
 *
 * Writes out all the requests that have been saved for page. Page 0 refers to
 * the global environment and is done during initial setup.
 *
 */

    for ( i = 0; i < nextreq; i++ )
	if ( request[i].page == page )
	    dumprequest(request[i].want, request[i].file, fp_out);

}   /* End of writerequest */

/*****************************************************************************/

void
dumprequest(want, file, fp_out)

    char	*want;			/* look for this string */
    char	*file;			/* in this file */
    FILE	*fp_out;		/* and write the value out here */

{

    char	buf[100];		/* line buffer for reading *file */
    FILE	*fp_in;

/*
 *
 * Looks for *want in the request file and if it's found the associated value
 * is copied to the output file. Keywords (ie. the *want strings) begin an @ in
 * the first column of file, while the values (ie. the stuff that's copied to
 * the output file) starts on the next line and extends to the next keyword or
 * to the end of file.
 *
 */

    if ( (fp_in = fopen(file, "r")) != NULL )  {
	while ( fgets(buf, sizeof(buf), fp_in) != NULL )
	    if ( buf[0] == '@' && strncmp(want, &buf[1], strlen(want)) == 0 )
		while ( fgets(buf, sizeof(buf), fp_in) != NULL )
		    if ( buf[0] == '#' || buf[0] == '%' )
			continue;
		    else if ( buf[0] != '@' )
			fprintf(fp_out, "%s", buf);
		    else break;
	fclose(fp_in);
    }	/* End if */

}   /* End of dumprequest */

/*****************************************************************************/