File: psorder.c

package info (click to toggle)
netatalk 2.0.3-4%2Betch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 9,012 kB
  • ctags: 6,109
  • sloc: ansic: 67,633; sh: 8,424; perl: 1,187; makefile: 1,001
file content (569 lines) | stat: -rw-r--r-- 13,689 bytes parent folder | download | duplicates (4)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
 * $Id: psorder.c,v 1.5 2001/06/29 14:14:46 rufustfirefly Exp $
 *
 * Copyright (c) 1990,1991 Regents of The University of Michigan.
 * All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appears in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation, and that the name of The University
 * of Michigan not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. This software is supplied as is without expressed or
 * implied warranties of any kind.
 *
 *	Research Systems Unix Group
 *	The University of Michigan
 *	c/o Mike Clark
 *	535 W. William Street
 *	Ann Arbor, Michigan
 *	+1-313-763-0525
 *	netatalk@itd.umich.edu
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdlib.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/file.h>
#include <ctype.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif /* HAVE_FCNTL_H */
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include "pa.h"
#include "psorder.h"

#include <atalk/paths.h>

/*
 *			Global Variables
 */

u_char			psbuf[ 8192 ];
struct psinfo_st	psinfo;
int			orderflag, forceflag;

int main( argc, argv )
    int		argc;
    char	**argv;
{
    extern int	optind;
    char	*progname;
    int		errflag = 0;
    int		c;

    while (( c = getopt( argc, argv, OPTSTR )) != -1 ) {
	switch ( c ) {
	case REVCHAR:
	    if ( orderflag ) errflag++;
	    else orderflag = REVERSE;
	    break;
	case FORWCHAR:
	    if ( orderflag ) errflag++;
	    else orderflag = FORWARD;
	    break;
	case FORCECHAR:
	    if ( forceflag ) errflag++;
	    else forceflag++;
	    break;
	}
    }
    if ( errflag ) {
	if (( progname = strrchr( argv[ 0 ], '/' )) == NULL ) {
	    progname = argv[ 0 ];
	} else progname++;
	fprintf( stderr, "usage: %s [-duf] [sourcefile]\n", progname );
	return( -1 );
    } else if ( !orderflag ) orderflag = FORWARD;

    if ( optind >= argc ) {
	return( psorder( STDIN ));
    }
    return( psorder( argv[ optind ] ));
}

int
psorder( path )
    char	*path;
{
    int			tempfd;
    int			inputfd;
    char		tempfile[MAXNAMLEN];

    filesetup( path, &inputfd, tempfile, &tempfd );
    readps( inputfd, tempfd, tempfile );
    if ( lseek( tempfd, REWIND, SEEK_SET ) < 0 ) {
	perror( tempfile );
	filecleanup( -1, tempfd, tempfile );
    }
    writeps( tempfd, tempfile );
    filecleanup( 0, tempfd, tempfile );
    return( 0 );
}

void
filesetup( inputfile, infd, tfile, tfd )
    char	*inputfile;
    int		*infd;
    char	*tfile;
    int		*tfd;
{
    struct stat		st;
    char		*template = _PATH_TMPPAGEORDER;

    if ( strcmp( inputfile, STDIN ) != 0 ) {
	if ( stat( inputfile, &st ) < 0 ) {
	    perror( inputfile );
	    filecleanup( -1, -1, "" );
	}
	if ( st.st_mode & S_IFMT & S_IFDIR ) {
	    fprintf( stderr, "%s is a directory.\n", inputfile );
	    filecleanup( 0, -1, "" );
	}
	if (( *infd = open( inputfile, O_RDONLY, 0600 )) < 0 ) {
	    perror( inputfile );
	    filecleanup( -1, -1, "" );
	}
    } else {
	*infd = 0;
    }

#if DEBUG
    fprintf( stderr, "Input file or stdin and stdout opened.\n" );
    fprintf( stderr, "Input file descriptor is %d .\n", *infd );
#endif /* DEBUG */

/*
	make temporary file
 */

    (void *)strncpy( tfile, template, MAXNAMLEN );
    if (( *tfd = mkstemp( tfile )) == -1 ) {
	fprintf( stderr, "can't create temporary file %s\n", tfile );
	filecleanup( -1, -1, "" );
    }

#if DEBUG
    fprintf( stderr, "Temporary file %s created and opened.\n", tfile );
    fprintf( stderr, "Temporary file descriptor is %d .\n", *tfd );
#endif /* DEBUG */

    psinfo.firstpage = NULL;
    psinfo.lastpage = NULL;
    psinfo.trailer = 0;
    psinfo.pages.offset = 0;
    psinfo.pages.end = 0;
    psinfo.pages.num[0] = '\0';
    psinfo.pages.order[0] = '\0';

    return;
}

void
readps( inputfd, tempfd, tempfile )
    int			inputfd;
    int			tempfd;
    char		*tempfile;
{
    off_t		ccread = 0;
    off_t		ccmatch;
    char		*curtok = 0;
    FILE		*tempstream;
    pa_buf_t		*pb;
    int			n;
    char		c = -1;
    char		pc, cc = 0;

    pb = pa_init( inputfd );
    if (( tempstream = fdopen( tempfd, "w" )) == NULL ) {
	perror( "fdopen fails for tempfile" );
	filecleanup( -1, tempfd, tempfile );
    }

    if (( c = pa_getchar( pb )) != 0 ) {
	ccread++;
	(void)putc( c, tempstream );
	pc = 0;
	cc = c;
	pa_match( pb );
	n = strlen( PPSADOBE );
	for ( ; ( n > 0 ) && (( c = pa_getchar( pb )) != 0 ) ; n-- ) {
	    ccread++ ;
	    (void)putc( c, tempstream );
	    pc = cc;
	    cc = c;
	}
	curtok = pa_gettok( pb );
    }
#if DEBUG
    fprintf( stderr, "%s\n", curtok );
#endif /* DEBUG */

/*
 * not postscript
 */
    if ( strcmp( curtok, PPSADOBE ) != 0 ) {
#if DEBUG
    fprintf( stderr, "in the not postscript section of readps\n" );
#endif /* DEBUG */
	while (( c = pa_getchar( pb )) != 0 ) {
	    ccread++;
	    (void)putc( c, tempstream );
	    pc = cc;
	    cc = c;
	}

	(void)fflush( tempstream );
	return;
    }

/*
 * postscript
 */
#if DEBUG
    fprintf( stderr, "in the postscript section of readps\n" );
#endif /* DEBUG */
    while (( c = pa_getchar( pb )) != 0 ) {
	ccread++;
	(void)putc( c, tempstream );
	pc = cc;
	cc = c;
	if ((( pc == '\r' ) || ( pc == '\n' )) && ( cc == '%' )) {
#if DEBUG
	    fprintf( stderr, "supposed start of match, cc = %c\n", cc );
#endif /* DEBUG */
	    pa_match( pb );
	    ccmatch = ccread - 1;
	    while ( ( c = pa_getchar( pb ) ) ) {
		if ( c != 0 ) {
		    ccread++;
		    (void)putc( c, tempstream );
		    pc = cc;
		    cc = c;
		}
		if (( c == '\r' ) || ( c == '\n' ) || ( cc == '\0' )) {
		    curtok = pa_gettok( pb );
#if DEBUG
		    fprintf( stderr, "%s\n", curtok );
#endif /* DEBUG */
		    if ( handletok( ccmatch, curtok ) < 0 ) {
			perror( "malloc died" );
			filecleanup( -1, tempfd, tempfile );
		    }
		    break;
		}
		if ( c == 0 ) break;
	    }
	    if ( c == 0 ) break;
	}
    }

    (void)fflush( tempstream );
    return;
}

int
handletok( count, token )
    off_t		count;
    char		*token;
{
    int			incdoc = 0;
    struct pspage_st	*newpage;
    char		*tmp;

    if (( strncmp( PENDDOC, token, strlen( PENDDOC )) == 0 ) && incdoc ) {
	incdoc--;
#if DEBUG
	fprintf( stderr, "found an EndDoc\n" );
#endif /* DEBUG */

    } else if ( strncmp( PBEGINDOC, token, strlen( PBEGINDOC )) == 0 ) {
	incdoc++;
#if DEBUG
	fprintf( stderr, "found a BeginDoc\n" );
#endif /* DEBUG */

    } else if ( !incdoc && 
	    ( strncmp( PPAGE, token, strlen( PPAGE )) == 0 )) {
#if DEBUG
	fprintf( stderr, "found a Page\n" );
#endif /* DEBUG */
	if (( newpage = getpspage( count )) == NULL ) {
	    return( -1 );
	}
	if ( psinfo.firstpage == NULL ) {
	    newpage->prevpage = NULL;
	    psinfo.firstpage = newpage;
	} else {
	    newpage->prevpage = psinfo.lastpage;
	    psinfo.lastpage->nextpage = newpage;
	}
	psinfo.lastpage = newpage;
	while ( *token++ != ':' );
	if (( tmp = strtok( token, WHITESPACE )) != NULL ) {
	    (void)strncpy( newpage->lable, tmp, NUMLEN );
	    if (( tmp = strtok( NULL, WHITESPACE )) != NULL ) {
		(void)strncpy( newpage->ord, tmp, ORDLEN );
	    }
	}
#if DEBUG
	fprintf( stderr, "page lable %s, page ord %s\n", newpage->lable,
		newpage->ord );
#endif /* DEBUG */

    } else if ( !incdoc && 
	    ( strncmp( PPAGES, token, strlen( PPAGES )) == 0 )) {
#if DEBUG
	fprintf( stderr, "found a Pages\n" );
#endif /* DEBUG */
	psinfo.pages.offset = count;
	psinfo.pages.end = strlen( token ) + count;
	while ( *token++ != ':' );
	while ( isspace( *token )) token++;
	if ( strncmp( ATEND, token, strlen( ATEND )) == 0 ) {
#if DEBUG
	    fprintf( stderr, "it is a Pages: (atend)\n" );
#endif /* DEBUG */
	    psinfo.pages.offset = 0;
	    psinfo.pages.end = 0;
	} else {
	    if (( tmp = strtok( token, WHITESPACE )) != NULL ) {
		(void)strncpy( psinfo.pages.num, tmp, NUMLEN );
		if (( tmp = strtok( NULL, WHITESPACE )) != NULL ) {
		    (void)strncpy( psinfo.pages.order, tmp, ORDERLEN );
		}
	    }
#if DEBUG
	    fprintf( stderr, "number of pages %s\n", psinfo.pages.num );
	    fprintf( stderr, "order control number %s\n", psinfo.pages.order );
#endif /* DEBUG */
	}

    } else if ( !incdoc && 
	    ( strncmp( PTRAILER, token, strlen( PTRAILER )) == 0 )) {
#if DEBUG
	fprintf( stderr, "found the Trailer\n" );
#endif /* DEBUG */
	if  ( psinfo.trailer == 0 ) {
	    psinfo.trailer = count;
	}
    }

    return( 0 );
}

void
writeps( tempfd, tempfile )
    int			tempfd;
    char		*tempfile;
{
    struct stat		st;
    off_t		endofpage;
    int			order;

    if ( stat( tempfile, &st ) < 0 ) {
	perror( "stat failed" );
	filecleanup( -1, tempfd, tempfile );
    }
    if ( psinfo.trailer == 0 ) {
	endofpage = st.st_size;
    } else endofpage = psinfo.trailer;

    if (( psinfo.firstpage == NULL ) || 
	    ( psinfo.firstpage == psinfo.lastpage )) {
	order = FORWARD;
    } else if ( psinfo.pages.offset == 0 ) {
	order = orderflag;
    } else if (( strncmp( psinfo.pages.order, "", ORDERLEN ) == 0 ) ||
	    ( strncmp( psinfo.pages.order, "1", ORDERLEN ) == 0 )) {
	order = orderflag;
	if ( order == REVERSE ) strcpy( psinfo.pages.order, "-1" );
    } else if ( strncmp( psinfo.pages.order, "-1", ORDERLEN ) == 0 ) {
	if ( orderflag == FORWARD ) {
	    order = REVERSE;
	    strcpy( psinfo.pages.order, "1" );
	} else order = FORWARD;
    } else if (( strncmp( psinfo.pages.order, "0", ORDERLEN ) == 0 ) &&
	    forceflag ) {
	order = orderflag;
    } else order = FORWARD;

    if ( order == FORWARD ) {
	temp2out( tempfd, tempfile, st.st_size );
    } else {
/*
 *	output the header stuff and rewrite the $$Pages line
 *	if it is in the header and not %%Pages: (atend)
 */
	if ( psinfo.firstpage->offset > 0 ) {
	    if (( psinfo.firstpage->offset > psinfo.pages.offset ) &&
		    ( psinfo.pages.offset != 0 )) {
		temp2out( tempfd, tempfile, psinfo.pages.offset );
		writelable( tempfd, tempfile, PPAGES );
		if ( lseek( tempfd, psinfo.pages.end, SEEK_SET ) < 0 ) {
		    perror( tempfile );
		    filecleanup( -1, tempfd, tempfile );
		}
		temp2out( tempfd, tempfile, 
			psinfo.firstpage->offset - psinfo.pages.end );
	    } else temp2out( tempfd, tempfile, psinfo.firstpage->offset );
	}
/*
 *	output the pages, last to first
 */
	while ( psinfo.lastpage != NULL ) {
	    if ( lseek( tempfd, psinfo.lastpage->offset, SEEK_SET ) < 0 ) {
		perror( tempfile );
		filecleanup( -1, tempfd, tempfile );
	    }
	    temp2out( tempfd, tempfile, endofpage - psinfo.lastpage->offset );
	    endofpage = psinfo.lastpage->offset;
	    psinfo.lastpage = psinfo.lastpage->prevpage;
	    if ( psinfo.lastpage != NULL ) {
		(void)free( psinfo.lastpage->nextpage );
		psinfo.lastpage->nextpage = NULL;
	    }
	}
/*
 *	output the trailer stuff and rewrite the $$Pages line
 *	if it is in the trailer
 */
	if ( psinfo.trailer != 0 ) {
	    if ( lseek( tempfd, psinfo.trailer, SEEK_SET ) < 0 ) {
		perror( tempfile );
		filecleanup( -1, tempfd, tempfile );
	    }
	    if ( psinfo.trailer < psinfo.pages.offset ) {
		temp2out( tempfd, tempfile,
			psinfo.pages.offset - psinfo.trailer );
		writelable( tempfd, tempfile, PPAGES );
		if ( lseek( tempfd, psinfo.pages.end, SEEK_SET ) < 0 ) {
		    perror( tempfile );
		    filecleanup( -1, tempfd, tempfile );
		}
		temp2out( tempfd, tempfile, st.st_size - psinfo.pages.end );
	    } else temp2out( tempfd, tempfile, st.st_size - psinfo.trailer );
	}
    }

    return;
}

void
writelable( tempfd, tempfile, lable )
    int			tempfd;
    char		*tempfile;
    char		*lable;
{
    char		line[256];
    int			ccwrite;
    int			linelen;
    char		*argone;
    char		*argtwo;

    if ( strcmp( lable, PPAGES ) == 0 ) {
	argone = psinfo.pages.num;
	argtwo = psinfo.pages.order;
    } else {
	argone = argtwo = NULL;
    }
    (void)sprintf( line, "%s %s %s", lable, argone, argtwo );
    linelen = strlen( line );

    ccwrite = write( 1, line, linelen );
    if ( ccwrite < 0 ) {
	perror( "stdout" );
	filecleanup( ccwrite, tempfd, tempfile );
    } else {
	linelen -= ccwrite;
    }
}

void
temp2out( tempfd, tempfile, length )
    int			tempfd;
    char		*tempfile;
    off_t		length;
{
    int			ccread;
    int			ccwrite;
    int			size;

    while ( length > 0 ) {
	if ( length > sizeof( psbuf )) {
	    size = sizeof( psbuf );
	} else size = length;
	if (( ccread = read( tempfd, psbuf, size )) > 0 ) {
	    size = ccread;
	    while ( ccread > 0 ) {
		ccwrite = write( 1, psbuf, ccread );
		if ( ccwrite < 0 ) {
		    perror( "stdout" );
		    filecleanup( ccwrite, tempfd, tempfile );
		} else {
		    ccread -= ccwrite;
		}
	    }
	}
	if ( ccread < 0 ) {
	    perror( "temporary file" );
	    filecleanup( ccread, tempfd, tempfile );
	}
	length -= size;
    }
}

struct pspage_st
*getpspage( off )
    off_t		off;
{
    struct pspage_st	*newpspage;

    newpspage = (struct pspage_st *)malloc( sizeof( struct pspage_st )); 
    if ( newpspage != NULL ) {
	newpspage->offset = off;
	newpspage->nextpage = NULL;
	*newpspage->lable = '\0';
	*newpspage->ord = '\0';
    }
    return( newpspage );
}

void
filecleanup( errorcode, tfd, tfile )
    int			errorcode;
    int			tfd;
    char		*tfile;
{

/*
	Close and unlink the temporary file.
 */

    if ( tfd != 0 ) {
	if ( close( tfd ) != 0 ) {
	    perror( tfile );
	    exit( errorcode );
	}
	if ( unlink( tfile ) != 0 ) {
	    perror( tfile );
	    exit( errorcode );
	}
    }

    exit( errorcode );
}