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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/* pstops.c
* Copyright (C) Angus J. C. Duggan 1991-1995
* See file LICENSE for details.
*
* rearrange pages in conforming PS file for printing in signatures
*
* Usage:
* pstops [-q] [-b] [-d] [-w<dim>] [-h<dim>] [-ppaper] <pagespecs> [infile [outfile]]
*/
#include <unistd.h>
#include <string.h>
#include "psutil.h"
#include "psspec.h"
#include "pserror.h"
#include "patchlev.h"
#ifdef DEBIAN
#include <paper.h>
#endif
char *program ;
int pages ;
int verbose ;
FILE *infile ;
FILE *outfile ;
char pagelabel[BUFSIZ] ;
int pageno ;
void usage(void)
{
fprintf(stderr, "%s release %d patchlevel %d\n", program, RELEASE, PATCHLEVEL);
fprintf(stderr, "Copyright (C) Angus J. C. Duggan, 1991-1995. See file LICENSE for details.\n");
fprintf(stderr, "Usage: %s [-q] [-b] [-wwidth] [-hheight] [-dlwidth] [-ppaper] <pagespecs> [infile [outfile]]\n",
program);
fflush(stderr);
exit(1);
}
static void argerror(void)
{
fprintf(stderr, "%s: page specification error:\n", program);
fprintf(stderr, " <pagespecs> = [modulo:]<spec>\n");
fprintf(stderr, " <spec> = [-]pageno[@scale][L|R|U|H|V][(xoff,yoff)][,spec|+spec]\n");
fprintf(stderr, " modulo>=1, 0<=pageno<modulo\n");
fflush(stderr);
exit(1);
}
static int modulo = 1;
static int pagesperspec = 1;
static PageSpec *parsespecs(char *str)
{
PageSpec *head, *tail;
unsigned long spec_count = 0;
long num = -1;
head = tail = newspec();
while (*str) {
if (isdigit(*str)) {
num = parseint(&str, argerror);
} else {
switch (*str++) {
case ':':
if (spec_count || head != tail || num < 1) argerror();
modulo = num;
num = -1;
break;
case '-':
tail->reversed = !tail->reversed;
break;
case '@':
tail->scale *= parsedouble(&str, argerror);
tail->flags |= SCALE;
break;
case 'l': case 'L':
tail->rotate += 90;
tail->flags |= ROTATE;
break;
case 'r': case 'R':
tail->rotate -= 90;
tail->flags |= ROTATE;
break;
case 'u': case 'U':
tail->rotate += 180;
tail->flags |= ROTATE;
break;
case 'h': case 'H':
tail->hflip += 1;
tail->flags |= HFLIP;
break;
case 'v': case 'V':
tail->vflip += 1;
tail->flags |= VFLIP;
break;
case '(':
tail->xoff += parsedimen(&str, argerror);
if (*str++ != ',') argerror();
tail->yoff += parsedimen(&str, argerror);
if (*str++ != ')') argerror();
tail->flags |= OFFSET;
break;
case '+':
tail->flags |= ADD_NEXT;
case ',':
if (num < 0 || num >= modulo) argerror();
if ((tail->flags & ADD_NEXT) == 0)
pagesperspec++;
tail->pageno = num;
tail->next = newspec();
tail = tail->next;
num = -1;
break;
default:
argerror();
}
spec_count++;
}
}
if (num >= modulo)
argerror();
else if (num >= 0)
tail->pageno = num;
return (head);
}
int
main(int argc, char *argv[])
{
PageSpec *specs = NULL;
int nobinding = 0;
double draw = 0;
Paper *paper = NULL;
int opt;
#ifdef DEBIAN
paperinit();
{
const char *default_size = systempapername();
if (!default_size) default_size = defaultpapername ();
if (default_size) paper = findpaper(default_size);
if (paper) {
width = (double)PaperWidth(paper);
height = (double)PaperHeight(paper);
}
}
paperdone();
#elif defined(PAPER)
if ( (paper = findpaper(PAPER)) != (Paper *)0 ) {
width = (double)PaperWidth(paper);
height = (double)PaperHeight(paper);
}
#endif
verbose = 1;
program = *argv;
while((opt = getopt(argc, argv, "qd::bw:h:p:v0123456789")) != EOF) {
switch(opt) {
case 'q': /* quiet */
verbose = 0;
break;
case 'd': /* draw borders */
if(optarg)
draw = singledimen(optarg, argerror, usage);
else
draw = 1;
break;
case 'b': /* no bind operator */
nobinding = 1;
break;
case 'w': /* page width */
width = singledimen(optarg, argerror, usage);
break;
case 'h': /* page height */
height = singledimen(optarg, argerror, usage);
break;
case 'p': /* paper type */
if ( (paper = findpaper(optarg)) != (Paper *)0 ) {
width = (double)PaperWidth(paper);
height = (double)PaperHeight(paper);
} else
message(FATAL, "paper size '%s' not recognised\n", optarg);
break;
case 'v': /* version */
usage();
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (specs == NULL) {
char *spec_txt = alloca((optarg ? strlen(optarg) : 0) + 3);
if(!spec_txt) message(FATAL, "no memory for spec allocation\n");
spec_txt[0] = '-';
spec_txt[1] = opt;
spec_txt[2] = 0;
if (optarg) strcat(spec_txt, optarg);
specs = parsespecs(spec_txt);
} else {
usage();
}
break;
default:
usage();
break;
}
}
if (specs == NULL) {
if(optind == argc) usage();
specs = parsespecs(argv[optind]);
optind++;
}
infile = stdin;
outfile = stdout;
/* Be defensive */
if((argc - optind) < 0 || (argc - optind) > 2) usage();
if (optind != argc) {
/* User specified an input file */
if ((infile = fopen(argv[optind], OPEN_READ)) == NULL)
message(FATAL, "can't open input file %s\n", argv[optind]);
optind++;
}
if (optind != argc) {
/* User specified an output file */
if ((outfile = fopen(argv[optind], OPEN_WRITE)) == NULL)
message(FATAL, "can't open output file %s\n", argv[optind]);
optind++;
}
if (optind != argc) usage();
if (specs == NULL) usage();
#if defined(MSDOS) || defined(WINNT)
if ( infile == stdin ) {
int fd = fileno(stdin) ;
if ( setmode(fd, O_BINARY) < 0 )
message(FATAL, "can't open input file %s\n", argv[4]);
}
if ( outfile == stdout ) {
int fd = fileno(stdout) ;
if ( setmode(fd, O_BINARY) < 0 )
message(FATAL, "can't reset stdout to binary mode\n");
}
#endif
if ((infile=seekable(infile))==NULL)
message(FATAL, "can't seek input\n");
pstops(modulo, pagesperspec, nobinding, specs, draw);
exit(0);
}
|