source: trunk/athena/bin/lpr/psrv.c @ 12350

Revision 12350, 3.6 KB checked in by ghudson, 26 years ago (diff)
Some RCS ID cleanup: delete $Log$ and replace other RCS keywords with $Id$.
  • Property svn:executable set to *
Line 
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[]="$Id: psrv.c,v 1.2 1999-01-22 23:10:45 ghudson 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 */
17
18#include <stdio.h>
19#ifdef SYSV
20#include <fcntl.h>
21#else
22#include <sys/file.h>
23#endif
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <signal.h>
27#include "psspool.h"
28#include "transcript.h"
29
30private char buf[BUFSIZ];
31private char *prog;
32private int fdin, fdout;
33private char TempName[100];
34
35
36private VOID writesection(first,last)
37        register long first, last;
38{
39    register long len;
40    register int pcnt;
41    register unsigned nb;
42
43    len = last - first;
44    VOIDC lseek(fdin,first,0);
45    while (len > 0) {
46        nb = (unsigned) (len > sizeof(buf)) ? sizeof(buf) : len;
47        len -= pcnt = read(fdin, buf, nb);
48        if (write(fdout, buf, (unsigned) pcnt) != pcnt)
49            pexit2(prog,"write",2);
50    }
51}
52
53
54/* page-reverse stdin to stdout -- errors to stderr */
55main(argc, argv)
56int argc;
57char **argv;
58{
59    struct stat statb;
60    long ptable[2000];
61    int page = 0;
62    long endpro = -1;
63    long trailer = -1;
64    long lastchar;
65    int isregular;
66    register int i;
67    register int cnt, c;
68    register long linestart;
69    register char *bp;
70    FILE *stmin;
71    char *tempdir;
72
73    fdin = fileno(stdin);
74    fdout = fileno(stdout);
75    stmin = stdin;
76
77    prog = *argv;
78
79    if (fstat(fdin, &statb) == -1) pexit2(prog,"fstat", THROW_AWAY);
80    isregular = ((statb.st_mode & S_IFMT) == S_IFREG);
81
82    if (!isregular) {
83        /* copy stdin to a temp file */
84        if ((tempdir = envget("PSTEMPDIR")) == NULL) tempdir = TempDir;
85        VOIDC mktemp(mstrcat(TempName, tempdir, REVTEMP,sizeof TempName));
86       
87        if ((i = open(TempName,O_WRONLY|O_CREAT,0600)) == -1) {
88            pexit2(prog,"open temp file",THROW_AWAY);
89        }
90        while ((cnt = read(fdin, buf, sizeof buf)) > 0) {
91            if (write(i,buf,cnt) != cnt) {
92                perror(prog);
93            }
94        }
95        if (cnt < 0) {
96            pexit2(prog,"copying file",THROW_AWAY);
97        }
98        VOIDC close(i);
99        VOIDC close(fdin);
100        stmin = freopen(TempName,"r",stdin);
101        VOIDC unlink(TempName);
102    }
103
104    while (1) {
105        cnt = BUFSIZ - 1;
106        bp = buf;
107        if ((linestart = ftell(stmin)) == -1) pexit2(prog,"ftell",2);
108        while ((--cnt > 0) && ((*bp++ = (c = getchar())) != '\n')) {
109            if (c == EOF) {
110                *--bp = '\0';
111                break;
112            }
113        }
114        if (c != EOF) {
115            while (c != '\n') {
116                if ((c = getchar()) == EOF) break;
117            }
118        }
119        *bp = '\0';
120        if ((c == EOF) && (bp == buf)) break;
121        else if (*buf != '%') continue;
122        else if (strncmp(buf,"%%Page:",7) == 0) {
123            ptable[page++] = linestart;
124            if (endpro == -1) endpro = linestart;
125        }
126        else if (strncmp(buf,"%%Trailer",9) == 0) {
127            trailer = linestart;
128            ptable[page] = linestart;
129        }
130    }
131    lastchar = ftell(stmin);
132    if (trailer == -1) ptable[page] = lastchar;
133
134    VOIDC fseek(stmin,0L,0); VOIDC lseek(fdin, 0L, 0);
135
136#ifdef BSD
137    VOIDC kill(getppid(),SIGEMT);
138#endif
139
140    if ((endpro == -1) || (page == 0)) {
141        fprintf(stderr,"%s: file not reversible\n",prog);
142        writesection(0L,lastchar);
143    }
144    else {
145        /* do prologue */
146        writesection(0L, endpro);
147
148        /* do pages */
149        for (i = page; i > 0; i--) {
150            writesection(ptable[i-1], ptable[i]);
151        }
152
153        /* do trailer */
154        if (trailer != -1) writesection(trailer, lastchar);
155    }
156    exit(0);
157}
Note: See TracBrowser for help on using the repository browser.