1 | #ifndef lint |
---|
2 | #define _NOTICE static char |
---|
3 | _NOTICE N1[] = "Copyright (c) 1985,1987 Adobe Systems Incorporated"; |
---|
4 | _NOTICE N2[] = "GOVERNMENT END USERS: See Notice file in TranScript library directory"; |
---|
5 | _NOTICE N3[] = "-- probably /usr/lib/ps/Notice"; |
---|
6 | _NOTICE RCSID[]="$Header: /afs/dev.mit.edu/source/repository/athena/bin/lpr/psrv.c,v 1.1 1993-10-12 05:18:00 probe Exp $"; |
---|
7 | #endif |
---|
8 | /* psrv.c |
---|
9 | * |
---|
10 | * Copyright (C) 1985,1987 Adobe Systems Incorporated. All rights reserved. |
---|
11 | * GOVERNMENT END USERS: See Notice file in TranScript library directory |
---|
12 | * -- probably /usr/lib/ps/Notice |
---|
13 | * |
---|
14 | * page-reversal (only) filter to be invoked by spooler communications filter |
---|
15 | * |
---|
16 | * RCSLOG: |
---|
17 | * $Log: not supported by cvs2svn $ |
---|
18 | * Revision 2.2 87/11/17 16:52:26 byron |
---|
19 | * Release 2.1 |
---|
20 | * |
---|
21 | * Revision 2.1.1.4 87/11/12 13:41:58 byron |
---|
22 | * Changed Government user's notice. |
---|
23 | * |
---|
24 | * Revision 2.1.1.3 87/04/23 10:26:46 byron |
---|
25 | * Copyright notice. |
---|
26 | * |
---|
27 | * Revision 2.1.1.2 87/03/24 16:50:26 shore |
---|
28 | * deleted %%EndProlog case as it was deemed to be a bug |
---|
29 | * now, the first %%Page comment marks the end of the prolog |
---|
30 | * |
---|
31 | * Revision 2.1.1.1 86/02/26 10:38:53 shore |
---|
32 | * fixed vanishing file problem for non-conforming input |
---|
33 | * |
---|
34 | * Revision 2.1 85/11/24 11:51:09 shore |
---|
35 | * Product Release 2.0 |
---|
36 | * |
---|
37 | * Revision 1.1 85/11/20 00:53:48 shore |
---|
38 | * Initial revision |
---|
39 | * |
---|
40 | * |
---|
41 | */ |
---|
42 | |
---|
43 | #include <stdio.h> |
---|
44 | #ifdef SYSV |
---|
45 | #include <fcntl.h> |
---|
46 | #else |
---|
47 | #include <sys/file.h> |
---|
48 | #endif |
---|
49 | #include <sys/types.h> |
---|
50 | #include <sys/stat.h> |
---|
51 | #include <signal.h> |
---|
52 | #include "psspool.h" |
---|
53 | #include "transcript.h" |
---|
54 | |
---|
55 | private char buf[BUFSIZ]; |
---|
56 | private char *prog; |
---|
57 | private int fdin, fdout; |
---|
58 | private char TempName[100]; |
---|
59 | |
---|
60 | |
---|
61 | private VOID writesection(first,last) |
---|
62 | register long first, last; |
---|
63 | { |
---|
64 | register long len; |
---|
65 | register int pcnt; |
---|
66 | register unsigned nb; |
---|
67 | |
---|
68 | len = last - first; |
---|
69 | VOIDC lseek(fdin,first,0); |
---|
70 | while (len > 0) { |
---|
71 | nb = (unsigned) (len > sizeof(buf)) ? sizeof(buf) : len; |
---|
72 | len -= pcnt = read(fdin, buf, nb); |
---|
73 | if (write(fdout, buf, (unsigned) pcnt) != pcnt) |
---|
74 | pexit2(prog,"write",2); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | /* page-reverse stdin to stdout -- errors to stderr */ |
---|
80 | main(argc, argv) |
---|
81 | int argc; |
---|
82 | char **argv; |
---|
83 | { |
---|
84 | struct stat statb; |
---|
85 | long ptable[2000]; |
---|
86 | int page = 0; |
---|
87 | long endpro = -1; |
---|
88 | long trailer = -1; |
---|
89 | long lastchar; |
---|
90 | int isregular; |
---|
91 | register int i; |
---|
92 | register int cnt, c; |
---|
93 | register long linestart; |
---|
94 | register char *bp; |
---|
95 | FILE *stmin; |
---|
96 | char *tempdir; |
---|
97 | |
---|
98 | fdin = fileno(stdin); |
---|
99 | fdout = fileno(stdout); |
---|
100 | stmin = stdin; |
---|
101 | |
---|
102 | prog = *argv; |
---|
103 | |
---|
104 | if (fstat(fdin, &statb) == -1) pexit2(prog,"fstat", THROW_AWAY); |
---|
105 | isregular = ((statb.st_mode & S_IFMT) == S_IFREG); |
---|
106 | |
---|
107 | if (!isregular) { |
---|
108 | /* copy stdin to a temp file */ |
---|
109 | if ((tempdir = envget("PSTEMPDIR")) == NULL) tempdir = TempDir; |
---|
110 | VOIDC mktemp(mstrcat(TempName, tempdir, REVTEMP,sizeof TempName)); |
---|
111 | |
---|
112 | if ((i = open(TempName,O_WRONLY|O_CREAT,0600)) == -1) { |
---|
113 | pexit2(prog,"open temp file",THROW_AWAY); |
---|
114 | } |
---|
115 | while ((cnt = read(fdin, buf, sizeof buf)) > 0) { |
---|
116 | if (write(i,buf,cnt) != cnt) { |
---|
117 | perror(prog); |
---|
118 | } |
---|
119 | } |
---|
120 | if (cnt < 0) { |
---|
121 | pexit2(prog,"copying file",THROW_AWAY); |
---|
122 | } |
---|
123 | VOIDC close(i); |
---|
124 | VOIDC close(fdin); |
---|
125 | stmin = freopen(TempName,"r",stdin); |
---|
126 | VOIDC unlink(TempName); |
---|
127 | } |
---|
128 | |
---|
129 | while (1) { |
---|
130 | cnt = BUFSIZ - 1; |
---|
131 | bp = buf; |
---|
132 | if ((linestart = ftell(stmin)) == -1) pexit2(prog,"ftell",2); |
---|
133 | while ((--cnt > 0) && ((*bp++ = (c = getchar())) != '\n')) { |
---|
134 | if (c == EOF) { |
---|
135 | *--bp = '\0'; |
---|
136 | break; |
---|
137 | } |
---|
138 | } |
---|
139 | if (c != EOF) { |
---|
140 | while (c != '\n') { |
---|
141 | if ((c = getchar()) == EOF) break; |
---|
142 | } |
---|
143 | } |
---|
144 | *bp = '\0'; |
---|
145 | if ((c == EOF) && (bp == buf)) break; |
---|
146 | else if (*buf != '%') continue; |
---|
147 | else if (strncmp(buf,"%%Page:",7) == 0) { |
---|
148 | ptable[page++] = linestart; |
---|
149 | if (endpro == -1) endpro = linestart; |
---|
150 | } |
---|
151 | else if (strncmp(buf,"%%Trailer",9) == 0) { |
---|
152 | trailer = linestart; |
---|
153 | ptable[page] = linestart; |
---|
154 | } |
---|
155 | } |
---|
156 | lastchar = ftell(stmin); |
---|
157 | if (trailer == -1) ptable[page] = lastchar; |
---|
158 | |
---|
159 | VOIDC fseek(stmin,0L,0); VOIDC lseek(fdin, 0L, 0); |
---|
160 | |
---|
161 | #ifdef BSD |
---|
162 | VOIDC kill(getppid(),SIGEMT); |
---|
163 | #endif |
---|
164 | |
---|
165 | if ((endpro == -1) || (page == 0)) { |
---|
166 | fprintf(stderr,"%s: file not reversible\n",prog); |
---|
167 | writesection(0L,lastchar); |
---|
168 | } |
---|
169 | else { |
---|
170 | /* do prologue */ |
---|
171 | writesection(0L, endpro); |
---|
172 | |
---|
173 | /* do pages */ |
---|
174 | for (i = page; i > 0; i--) { |
---|
175 | writesection(ptable[i-1], ptable[i]); |
---|
176 | } |
---|
177 | |
---|
178 | /* do trailer */ |
---|
179 | if (trailer != -1) writesection(trailer, lastchar); |
---|
180 | } |
---|
181 | exit(0); |
---|
182 | } |
---|