2 * Copyright (c) 1990,1991 Regents of The University of Michigan.
5 * Permission to use, copy, modify, and distribute this software and
6 * its documentation for any purpose and without fee is hereby granted,
7 * provided that the above copyright notice appears in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation, and that the name of The University
10 * of Michigan not be used in advertising or publicity pertaining to
11 * distribution of the software without specific, written prior
12 * permission. This software is supplied as is without expressed or
13 * implied warranties of any kind.
15 * Research Systems Unix Group
16 * The University of Michigan
18 * 535 W. William Street
21 * netatalk@itd.umich.edu
29 #include <sys/types.h>
30 #include <sys/param.h>
44 #include <atalk/paths.h>
51 struct psinfo_st psinfo;
52 int orderflag, forceflag;
54 int main( argc, argv )
63 while (( c = getopt( argc, argv, OPTSTR )) != -1 ) {
66 if ( orderflag ) errflag++;
67 else orderflag = REVERSE;
70 if ( orderflag ) errflag++;
71 else orderflag = FORWARD;
74 if ( forceflag ) errflag++;
80 if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
83 fprintf( stderr, "usage: %s [-duf] [sourcefile]\n", progname );
85 } else if ( !orderflag ) orderflag = FORWARD;
87 if ( optind >= argc ) {
88 return( psorder( STDIN ));
90 return( psorder( argv[ optind ] ));
99 char tempfile[MAXNAMLEN];
101 filesetup( path, &inputfd, tempfile, &tempfd );
102 readps( inputfd, tempfd, tempfile );
103 if ( lseek( tempfd, REWIND, SEEK_SET ) < 0 ) {
105 filecleanup( -1, tempfd, tempfile );
107 writeps( tempfd, tempfile );
108 filecleanup( 0, tempfd, tempfile );
113 filesetup( inputfile, infd, tfile, tfd )
120 char *template = _PATH_TMPPAGEORDER;
122 if ( strcmp( inputfile, STDIN ) != 0 ) {
123 if ( stat( inputfile, &st ) < 0 ) {
125 filecleanup( -1, -1, "" );
127 if ( st.st_mode & S_IFMT & S_IFDIR ) {
128 fprintf( stderr, "%s is a directory.\n", inputfile );
129 filecleanup( 0, -1, "" );
131 if (( *infd = open( inputfile, O_RDONLY, 0600 )) < 0 ) {
133 filecleanup( -1, -1, "" );
140 fprintf( stderr, "Input file or stdin and stdout opened.\n" );
141 fprintf( stderr, "Input file descriptor is %d .\n", *infd );
148 (void *)strncpy( tfile, template, MAXNAMLEN );
149 if (( *tfd = mkstemp( tfile )) == -1 ) {
150 fprintf( stderr, "can't create temporary file %s\n", tfile );
151 filecleanup( -1, -1, "" );
155 fprintf( stderr, "Temporary file %s created and opened.\n", tfile );
156 fprintf( stderr, "Temporary file descriptor is %d .\n", *tfd );
159 psinfo.firstpage = NULL;
160 psinfo.lastpage = NULL;
162 psinfo.pages.offset = 0;
163 psinfo.pages.end = 0;
164 psinfo.pages.num[0] = '\0';
165 psinfo.pages.order[0] = '\0';
171 readps( inputfd, tempfd, tempfile )
185 pb = pa_init( inputfd );
186 if (( tempstream = fdopen( tempfd, "w" )) == NULL ) {
187 perror( "fdopen fails for tempfile" );
188 filecleanup( -1, tempfd, tempfile );
191 if (( c = pa_getchar( pb )) != 0 ) {
193 (void)putc( c, tempstream );
197 n = strlen( PPSADOBE );
198 for ( ; ( n > 0 ) && (( c = pa_getchar( pb )) != 0 ) ; n-- ) {
200 (void)putc( c, tempstream );
204 curtok = pa_gettok( pb );
207 fprintf( stderr, "%s\n", curtok );
213 if ( strcmp( curtok, PPSADOBE ) != 0 ) {
215 fprintf( stderr, "in the not postscript section of readps\n" );
217 while (( c = pa_getchar( pb )) != 0 ) {
219 (void)putc( c, tempstream );
224 (void)fflush( tempstream );
232 fprintf( stderr, "in the postscript section of readps\n" );
234 while (( c = pa_getchar( pb )) != 0 ) {
236 (void)putc( c, tempstream );
239 if ((( pc == '\r' ) || ( pc == '\n' )) && ( cc == '%' )) {
241 fprintf( stderr, "supposed start of match, cc = %c\n", cc );
244 ccmatch = ccread - 1;
245 while ( ( c = pa_getchar( pb ) ) ) {
248 (void)putc( c, tempstream );
252 if (( c == '\r' ) || ( c == '\n' ) || ( cc == '\0' )) {
253 curtok = pa_gettok( pb );
255 fprintf( stderr, "%s\n", curtok );
257 if ( handletok( ccmatch, curtok ) < 0 ) {
258 perror( "malloc died" );
259 filecleanup( -1, tempfd, tempfile );
269 (void)fflush( tempstream );
274 handletok( count, token )
279 struct pspage_st *newpage;
282 if (( strncmp( PENDDOC, token, strlen( PENDDOC )) == 0 ) && incdoc ) {
285 fprintf( stderr, "found an EndDoc\n" );
288 } else if ( strncmp( PBEGINDOC, token, strlen( PBEGINDOC )) == 0 ) {
291 fprintf( stderr, "found a BeginDoc\n" );
294 } else if ( !incdoc &&
295 ( strncmp( PPAGE, token, strlen( PPAGE )) == 0 )) {
297 fprintf( stderr, "found a Page\n" );
299 if (( newpage = getpspage( count )) == NULL ) {
302 if ( psinfo.firstpage == NULL ) {
303 newpage->prevpage = NULL;
304 psinfo.firstpage = newpage;
306 newpage->prevpage = psinfo.lastpage;
307 psinfo.lastpage->nextpage = newpage;
309 psinfo.lastpage = newpage;
310 while ( *token++ != ':' );
311 if (( tmp = strtok( token, WHITESPACE )) != NULL ) {
312 (void)strncpy( newpage->lable, tmp, NUMLEN );
313 if (( tmp = strtok( NULL, WHITESPACE )) != NULL ) {
314 (void)strncpy( newpage->ord, tmp, ORDLEN );
318 fprintf( stderr, "page lable %s, page ord %s\n", newpage->lable,
322 } else if ( !incdoc &&
323 ( strncmp( PPAGES, token, strlen( PPAGES )) == 0 )) {
325 fprintf( stderr, "found a Pages\n" );
327 psinfo.pages.offset = count;
328 psinfo.pages.end = strlen( token ) + count;
329 while ( *token++ != ':' );
330 while ( isspace( *token )) token++;
331 if ( strncmp( ATEND, token, strlen( ATEND )) == 0 ) {
333 fprintf( stderr, "it is a Pages: (atend)\n" );
335 psinfo.pages.offset = 0;
336 psinfo.pages.end = 0;
338 if (( tmp = strtok( token, WHITESPACE )) != NULL ) {
339 (void)strncpy( psinfo.pages.num, tmp, NUMLEN );
340 if (( tmp = strtok( NULL, WHITESPACE )) != NULL ) {
341 (void)strncpy( psinfo.pages.order, tmp, ORDERLEN );
345 fprintf( stderr, "number of pages %s\n", psinfo.pages.num );
346 fprintf( stderr, "order control number %s\n", psinfo.pages.order );
350 } else if ( !incdoc &&
351 ( strncmp( PTRAILER, token, strlen( PTRAILER )) == 0 )) {
353 fprintf( stderr, "found the Trailer\n" );
355 if ( psinfo.trailer == 0 ) {
356 psinfo.trailer = count;
364 writeps( tempfd, tempfile )
372 if ( stat( tempfile, &st ) < 0 ) {
373 perror( "stat failed" );
374 filecleanup( -1, tempfd, tempfile );
376 if ( psinfo.trailer == 0 ) {
377 endofpage = st.st_size;
378 } else endofpage = psinfo.trailer;
380 if (( psinfo.firstpage == NULL ) ||
381 ( psinfo.firstpage == psinfo.lastpage )) {
383 } else if ( psinfo.pages.offset == 0 ) {
385 } else if (( strncmp( psinfo.pages.order, "", ORDERLEN ) == 0 ) ||
386 ( strncmp( psinfo.pages.order, "1", ORDERLEN ) == 0 )) {
388 if ( order == REVERSE ) strcpy( psinfo.pages.order, "-1" );
389 } else if ( strncmp( psinfo.pages.order, "-1", ORDERLEN ) == 0 ) {
390 if ( orderflag == FORWARD ) {
392 strcpy( psinfo.pages.order, "1" );
393 } else order = FORWARD;
394 } else if (( strncmp( psinfo.pages.order, "0", ORDERLEN ) == 0 ) &&
397 } else order = FORWARD;
399 if ( order == FORWARD ) {
400 temp2out( tempfd, tempfile, st.st_size );
403 * output the header stuff and rewrite the $$Pages line
404 * if it is in the header and not %%Pages: (atend)
406 if ( psinfo.firstpage->offset > 0 ) {
407 if (( psinfo.firstpage->offset > psinfo.pages.offset ) &&
408 ( psinfo.pages.offset != 0 )) {
409 temp2out( tempfd, tempfile, psinfo.pages.offset );
410 writelable( tempfd, tempfile, PPAGES );
411 if ( lseek( tempfd, psinfo.pages.end, SEEK_SET ) < 0 ) {
413 filecleanup( -1, tempfd, tempfile );
415 temp2out( tempfd, tempfile,
416 psinfo.firstpage->offset - psinfo.pages.end );
417 } else temp2out( tempfd, tempfile, psinfo.firstpage->offset );
420 * output the pages, last to first
422 while ( psinfo.lastpage != NULL ) {
423 if ( lseek( tempfd, psinfo.lastpage->offset, SEEK_SET ) < 0 ) {
425 filecleanup( -1, tempfd, tempfile );
427 temp2out( tempfd, tempfile, endofpage - psinfo.lastpage->offset );
428 endofpage = psinfo.lastpage->offset;
429 psinfo.lastpage = psinfo.lastpage->prevpage;
430 if ( psinfo.lastpage != NULL ) {
431 (void)free( psinfo.lastpage->nextpage );
432 psinfo.lastpage->nextpage = NULL;
436 * output the trailer stuff and rewrite the $$Pages line
437 * if it is in the trailer
439 if ( psinfo.trailer != 0 ) {
440 if ( lseek( tempfd, psinfo.trailer, SEEK_SET ) < 0 ) {
442 filecleanup( -1, tempfd, tempfile );
444 if ( psinfo.trailer < psinfo.pages.offset ) {
445 temp2out( tempfd, tempfile,
446 psinfo.pages.offset - psinfo.trailer );
447 writelable( tempfd, tempfile, PPAGES );
448 if ( lseek( tempfd, psinfo.pages.end, SEEK_SET ) < 0 ) {
450 filecleanup( -1, tempfd, tempfile );
452 temp2out( tempfd, tempfile, st.st_size - psinfo.pages.end );
453 } else temp2out( tempfd, tempfile, st.st_size - psinfo.trailer );
461 writelable( tempfd, tempfile, lable )
472 if ( strcmp( lable, PPAGES ) == 0 ) {
473 argone = psinfo.pages.num;
474 argtwo = psinfo.pages.order;
476 argone = argtwo = NULL;
478 (void)sprintf( line, "%s %s %s", lable, argone, argtwo );
479 linelen = strlen( line );
481 ccwrite = write( 1, line, linelen );
484 filecleanup( ccwrite, tempfd, tempfile );
491 temp2out( tempfd, tempfile, length )
500 while ( length > 0 ) {
501 if ( length > sizeof( psbuf )) {
502 size = sizeof( psbuf );
503 } else size = length;
504 if (( ccread = read( tempfd, psbuf, size )) > 0 ) {
506 while ( ccread > 0 ) {
507 ccwrite = write( 1, psbuf, ccread );
510 filecleanup( ccwrite, tempfd, tempfile );
517 perror( "temporary file" );
518 filecleanup( ccread, tempfd, tempfile );
528 struct pspage_st *newpspage;
530 newpspage = (struct pspage_st *)malloc( sizeof( struct pspage_st ));
531 if ( newpspage != NULL ) {
532 newpspage->offset = off;
533 newpspage->nextpage = NULL;
534 *newpspage->lable = '\0';
535 *newpspage->ord = '\0';
541 filecleanup( errorcode, tfd, tfile )
548 Close and unlink the temporary file.
552 if ( close( tfd ) != 0 ) {
556 if ( unlink( tfile ) != 0 ) {