summaryrefslogtreecommitdiff
path: root/support/pssplit/pssplit.c
blob: 3baab432d65f10c22a1d9198e9c708451da69f78 (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
130
131
132
133
/*
 * File: pssplit.c
 *
 * (c) Peter Kleiweg 1998
 *
 *  This is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2,
 *  or (at your option) any later version.
 *
 */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#ifdef __MSDOS__
#define strcasecmp(A, B) (stricmp(A, B))
#endif

#define BUFSIZE 2048

char
    buffer [BUFSIZE + 1],
    *programname;

void
    syntax (void),
    errit (char const *format, ...);

int main (int argc, char *argv [])
{
    FILE
        *fp;
    int
        page = 0,
        prpage = 0,
        doclevel = 0,
        trailer = 0,
        even = 0,
        odd = 0,
        beginpage,
        endpage,
        retval = 1;

    programname = argv [0];

    if (argc != 5)
        syntax ();

    beginpage = atoi (argv [2]);
    endpage = atoi (argv [3]);
    if (beginpage < 1 || endpage < beginpage)
        syntax ();

    if (! strcasecmp (argv [4], "all")) {
        even = 1;
        odd = 1;
    } else if (! strcasecmp (argv [4], "even"))
        even = 1;
    else if (! strcasecmp (argv [4], "odd"))
        odd = 1;
    else
        syntax ();

    fp = fopen (argv [1], "r");
    if (! fp)
	errit ("Opening file \"%s\": %s", argv [1], strerror (errno));

    while (fgets (buffer, BUFSIZE, fp)) {
        if (! memcmp (buffer, "%%Page:", 7)) {
            if (! doclevel) {
                page++;
                prpage = (
                    page >= beginpage && page <= endpage
                 && ((odd && (page % 2)) || (even && ! (page % 2)))
                );
                if (prpage) {
                    retval = 0;
                    fputs (buffer, stderr);
                }
            }
        } else if (! memcmp (buffer, "%%BeginDocument", 15))
            doclevel++;
        else if (! memcmp (buffer, "%%EndDocument", 13))
            doclevel--;
        else if (! memcmp (buffer, "%%Trailer", 9)) {
            if (! doclevel)
                trailer = 1;
        }

	if ((page == 0) || prpage || trailer)
            fputs (buffer, stdout);
    }

    fclose (fp);

    if (retval)
        fprintf (stderr, "\n%s: No pages found\n\n", programname);
    
    return retval;
}

void errit (char const *format, ...)
{
    va_list
	list;

    fprintf (stderr, "\nError %s: ", programname);

    va_start (list, format);
    vfprintf (stderr, format, list);

    fprintf (stderr, "\n\n");

    exit (2);
}

void syntax ()
{
    fprintf (
	stderr,
	"\nUsage: %s file.ps beginpage endpage all|even|odd\n"
	"Return value:\n"
	"\t0 - ok\n"
	"\t1 - no pages found\n"
	"\t2 - error\n",
	programname
    );
    exit (2);
}