2
* $Id: psorder.c,v 1.5 2001/06/29 14:14:46 rufustfirefly Exp $
4
* Copyright (c) 1990,1991 Regents of The University of Michigan.
7
* Permission to use, copy, modify, and distribute this software and
8
* its documentation for any purpose and without fee is hereby granted,
9
* provided that the above copyright notice appears in all copies and
10
* that both that copyright notice and this permission notice appear
11
* in supporting documentation, and that the name of The University
12
* of Michigan not be used in advertising or publicity pertaining to
13
* distribution of the software without specific, written prior
14
* permission. This software is supplied as is without expressed or
15
* implied warranties of any kind.
17
* Research Systems Unix Group
18
* The University of Michigan
20
* 535 W. William Street
23
* netatalk@itd.umich.edu
28
#endif /* HAVE_CONFIG_H */
31
#include <sys/types.h>
32
#include <sys/param.h>
40
#endif /* HAVE_FCNTL_H */
46
#endif /* HAVE_UNISTD_H */
50
#include <atalk/paths.h>
57
struct psinfo_st psinfo;
58
int orderflag, forceflag;
60
int main( argc, argv )
69
while (( c = getopt( argc, argv, OPTSTR )) != -1 ) {
72
if ( orderflag ) errflag++;
73
else orderflag = REVERSE;
76
if ( orderflag ) errflag++;
77
else orderflag = FORWARD;
80
if ( forceflag ) errflag++;
86
if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
89
fprintf( stderr, "usage: %s [-duf] [sourcefile]\n", progname );
91
} else if ( !orderflag ) orderflag = FORWARD;
93
if ( optind >= argc ) {
94
return( psorder( STDIN ));
96
return( psorder( argv[ optind ] ));
105
char tempfile[MAXNAMLEN];
107
filesetup( path, &inputfd, tempfile, &tempfd );
108
readps( inputfd, tempfd, tempfile );
109
if ( lseek( tempfd, REWIND, SEEK_SET ) < 0 ) {
111
filecleanup( -1, tempfd, tempfile );
113
writeps( tempfd, tempfile );
114
filecleanup( 0, tempfd, tempfile );
119
filesetup( inputfile, infd, tfile, tfd )
126
char *template = _PATH_TMPPAGEORDER;
128
if ( strcmp( inputfile, STDIN ) != 0 ) {
129
if ( stat( inputfile, &st ) < 0 ) {
131
filecleanup( -1, -1, "" );
133
if ( st.st_mode & S_IFMT & S_IFDIR ) {
134
fprintf( stderr, "%s is a directory.\n", inputfile );
135
filecleanup( 0, -1, "" );
137
if (( *infd = open( inputfile, O_RDONLY, 0600 )) < 0 ) {
139
filecleanup( -1, -1, "" );
146
fprintf( stderr, "Input file or stdin and stdout opened.\n" );
147
fprintf( stderr, "Input file descriptor is %d .\n", *infd );
154
(void *)strncpy( tfile, template, MAXNAMLEN );
155
if (( *tfd = mkstemp( tfile )) == -1 ) {
156
fprintf( stderr, "can't create temporary file %s\n", tfile );
157
filecleanup( -1, -1, "" );
161
fprintf( stderr, "Temporary file %s created and opened.\n", tfile );
162
fprintf( stderr, "Temporary file descriptor is %d .\n", *tfd );
165
psinfo.firstpage = NULL;
166
psinfo.lastpage = NULL;
168
psinfo.pages.offset = 0;
169
psinfo.pages.end = 0;
170
psinfo.pages.num[0] = '\0';
171
psinfo.pages.order[0] = '\0';
177
readps( inputfd, tempfd, tempfile )
191
pb = pa_init( inputfd );
192
if (( tempstream = fdopen( tempfd, "w" )) == NULL ) {
193
perror( "fdopen fails for tempfile" );
194
filecleanup( -1, tempfd, tempfile );
197
if (( c = pa_getchar( pb )) != 0 ) {
199
(void)putc( c, tempstream );
203
n = strlen( PPSADOBE );
204
for ( ; ( n > 0 ) && (( c = pa_getchar( pb )) != 0 ) ; n-- ) {
206
(void)putc( c, tempstream );
210
curtok = pa_gettok( pb );
213
fprintf( stderr, "%s\n", curtok );
219
if ( strcmp( curtok, PPSADOBE ) != 0 ) {
221
fprintf( stderr, "in the not postscript section of readps\n" );
223
while (( c = pa_getchar( pb )) != 0 ) {
225
(void)putc( c, tempstream );
230
(void)fflush( tempstream );
238
fprintf( stderr, "in the postscript section of readps\n" );
240
while (( c = pa_getchar( pb )) != 0 ) {
242
(void)putc( c, tempstream );
245
if ((( pc == '\r' ) || ( pc == '\n' )) && ( cc == '%' )) {
247
fprintf( stderr, "supposed start of match, cc = %c\n", cc );
250
ccmatch = ccread - 1;
251
while ( ( c = pa_getchar( pb ) ) ) {
254
(void)putc( c, tempstream );
258
if (( c == '\r' ) || ( c == '\n' ) || ( cc == '\0' )) {
259
curtok = pa_gettok( pb );
261
fprintf( stderr, "%s\n", curtok );
263
if ( handletok( ccmatch, curtok ) < 0 ) {
264
perror( "malloc died" );
265
filecleanup( -1, tempfd, tempfile );
275
(void)fflush( tempstream );
280
handletok( count, token )
285
struct pspage_st *newpage;
288
if (( strncmp( PENDDOC, token, strlen( PENDDOC )) == 0 ) && incdoc ) {
291
fprintf( stderr, "found an EndDoc\n" );
294
} else if ( strncmp( PBEGINDOC, token, strlen( PBEGINDOC )) == 0 ) {
297
fprintf( stderr, "found a BeginDoc\n" );
300
} else if ( !incdoc &&
301
( strncmp( PPAGE, token, strlen( PPAGE )) == 0 )) {
303
fprintf( stderr, "found a Page\n" );
305
if (( newpage = getpspage( count )) == NULL ) {
308
if ( psinfo.firstpage == NULL ) {
309
newpage->prevpage = NULL;
310
psinfo.firstpage = newpage;
312
newpage->prevpage = psinfo.lastpage;
313
psinfo.lastpage->nextpage = newpage;
315
psinfo.lastpage = newpage;
316
while ( *token++ != ':' );
317
if (( tmp = strtok( token, WHITESPACE )) != NULL ) {
318
(void)strncpy( newpage->lable, tmp, NUMLEN );
319
if (( tmp = strtok( NULL, WHITESPACE )) != NULL ) {
320
(void)strncpy( newpage->ord, tmp, ORDLEN );
324
fprintf( stderr, "page lable %s, page ord %s\n", newpage->lable,
328
} else if ( !incdoc &&
329
( strncmp( PPAGES, token, strlen( PPAGES )) == 0 )) {
331
fprintf( stderr, "found a Pages\n" );
333
psinfo.pages.offset = count;
334
psinfo.pages.end = strlen( token ) + count;
335
while ( *token++ != ':' );
336
while ( isspace( *token )) token++;
337
if ( strncmp( ATEND, token, strlen( ATEND )) == 0 ) {
339
fprintf( stderr, "it is a Pages: (atend)\n" );
341
psinfo.pages.offset = 0;
342
psinfo.pages.end = 0;
344
if (( tmp = strtok( token, WHITESPACE )) != NULL ) {
345
(void)strncpy( psinfo.pages.num, tmp, NUMLEN );
346
if (( tmp = strtok( NULL, WHITESPACE )) != NULL ) {
347
(void)strncpy( psinfo.pages.order, tmp, ORDERLEN );
351
fprintf( stderr, "number of pages %s\n", psinfo.pages.num );
352
fprintf( stderr, "order control number %s\n", psinfo.pages.order );
356
} else if ( !incdoc &&
357
( strncmp( PTRAILER, token, strlen( PTRAILER )) == 0 )) {
359
fprintf( stderr, "found the Trailer\n" );
361
if ( psinfo.trailer == 0 ) {
362
psinfo.trailer = count;
370
writeps( tempfd, tempfile )
378
if ( stat( tempfile, &st ) < 0 ) {
379
perror( "stat failed" );
380
filecleanup( -1, tempfd, tempfile );
382
if ( psinfo.trailer == 0 ) {
383
endofpage = st.st_size;
384
} else endofpage = psinfo.trailer;
386
if (( psinfo.firstpage == NULL ) ||
387
( psinfo.firstpage == psinfo.lastpage )) {
389
} else if ( psinfo.pages.offset == 0 ) {
391
} else if (( strncmp( psinfo.pages.order, "", ORDERLEN ) == 0 ) ||
392
( strncmp( psinfo.pages.order, "1", ORDERLEN ) == 0 )) {
394
if ( order == REVERSE ) strcpy( psinfo.pages.order, "-1" );
395
} else if ( strncmp( psinfo.pages.order, "-1", ORDERLEN ) == 0 ) {
396
if ( orderflag == FORWARD ) {
398
strcpy( psinfo.pages.order, "1" );
399
} else order = FORWARD;
400
} else if (( strncmp( psinfo.pages.order, "0", ORDERLEN ) == 0 ) &&
403
} else order = FORWARD;
405
if ( order == FORWARD ) {
406
temp2out( tempfd, tempfile, st.st_size );
409
* output the header stuff and rewrite the $$Pages line
410
* if it is in the header and not %%Pages: (atend)
412
if ( psinfo.firstpage->offset > 0 ) {
413
if (( psinfo.firstpage->offset > psinfo.pages.offset ) &&
414
( psinfo.pages.offset != 0 )) {
415
temp2out( tempfd, tempfile, psinfo.pages.offset );
416
writelable( tempfd, tempfile, PPAGES );
417
if ( lseek( tempfd, psinfo.pages.end, SEEK_SET ) < 0 ) {
419
filecleanup( -1, tempfd, tempfile );
421
temp2out( tempfd, tempfile,
422
psinfo.firstpage->offset - psinfo.pages.end );
423
} else temp2out( tempfd, tempfile, psinfo.firstpage->offset );
426
* output the pages, last to first
428
while ( psinfo.lastpage != NULL ) {
429
if ( lseek( tempfd, psinfo.lastpage->offset, SEEK_SET ) < 0 ) {
431
filecleanup( -1, tempfd, tempfile );
433
temp2out( tempfd, tempfile, endofpage - psinfo.lastpage->offset );
434
endofpage = psinfo.lastpage->offset;
435
psinfo.lastpage = psinfo.lastpage->prevpage;
436
if ( psinfo.lastpage != NULL ) {
437
(void)free( psinfo.lastpage->nextpage );
438
psinfo.lastpage->nextpage = NULL;
442
* output the trailer stuff and rewrite the $$Pages line
443
* if it is in the trailer
445
if ( psinfo.trailer != 0 ) {
446
if ( lseek( tempfd, psinfo.trailer, SEEK_SET ) < 0 ) {
448
filecleanup( -1, tempfd, tempfile );
450
if ( psinfo.trailer < psinfo.pages.offset ) {
451
temp2out( tempfd, tempfile,
452
psinfo.pages.offset - psinfo.trailer );
453
writelable( tempfd, tempfile, PPAGES );
454
if ( lseek( tempfd, psinfo.pages.end, SEEK_SET ) < 0 ) {
456
filecleanup( -1, tempfd, tempfile );
458
temp2out( tempfd, tempfile, st.st_size - psinfo.pages.end );
459
} else temp2out( tempfd, tempfile, st.st_size - psinfo.trailer );
467
writelable( tempfd, tempfile, lable )
478
if ( strcmp( lable, PPAGES ) == 0 ) {
479
argone = psinfo.pages.num;
480
argtwo = psinfo.pages.order;
482
argone = argtwo = NULL;
484
(void)sprintf( line, "%s %s %s", lable, argone, argtwo );
485
linelen = strlen( line );
487
ccwrite = write( 1, line, linelen );
490
filecleanup( ccwrite, tempfd, tempfile );
497
temp2out( tempfd, tempfile, length )
506
while ( length > 0 ) {
507
if ( length > sizeof( psbuf )) {
508
size = sizeof( psbuf );
509
} else size = length;
510
if (( ccread = read( tempfd, psbuf, size )) > 0 ) {
512
while ( ccread > 0 ) {
513
ccwrite = write( 1, psbuf, ccread );
516
filecleanup( ccwrite, tempfd, tempfile );
523
perror( "temporary file" );
524
filecleanup( ccread, tempfd, tempfile );
534
struct pspage_st *newpspage;
536
newpspage = (struct pspage_st *)malloc( sizeof( struct pspage_st ));
537
if ( newpspage != NULL ) {
538
newpspage->offset = off;
539
newpspage->nextpage = NULL;
540
*newpspage->lable = '\0';
541
*newpspage->ord = '\0';
547
filecleanup( errorcode, tfd, tfile )
554
Close and unlink the temporary file.
558
if ( close( tfd ) != 0 ) {
562
if ( unlink( tfile ) != 0 ) {