File: post.c

package info (click to toggle)
mpage 2.5.1pre-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 348 kB
  • ctags: 293
  • sloc: ansic: 2,864; makefile: 79; sh: 68
file content (839 lines) | stat: -rw-r--r-- 24,443 bytes parent folder | download
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*
 * post.c
 */

/*
 * mpage:    a program to reduce pages of print so that several pages
 *           of output appear on one printed page.
 *
 * Written by:
 *   ...!uunet!\                       Mark Hahn, Sr Systems Engineer
 *              >pyrdc!mark            Pyramid Technology Corporation
 * ...!pyramid!/                       Vienna, Va    (703)848-2050
 *
 *
 * Copyright (c) 1988 Mark P. Hahn, Herndon, Virginia
 * Copyright (c) 1994-1997 Marcel J.E. Mol, The Netherlands
 *                    marcel@mesa.nl
 *  
 *     Permission is granted to anyone to make or distribute verbatim
 *     copies of this document as received, in any medium, provided
 *     that this copyright notice is preserved, and that the
 *     distributor grants the recipient permission for further
 *     redistribution as permitted by this notice.
 *
 */


#include "mpage.h"
#include <string.h>


/*
 * character spaces used throughout for holding the current line from
 * the input file
 */
static char currline[LINESIZE];
static char *file_name;

/*
 * for ps documents, used to remember if we have come across the
 * tailer section.  reset at the beginning of processing for each file
 */
static int ps_at_trailer;

/*
 * this is the type of postscript document we are processing
 */
static int ps_posttype;

/*
 * set to one if we have a inputline that must be preceded by something...
 * used for dvips output without PS comments.
 */
static int have_line = 0;

/*
 * number of output lines on current logical page
 */
static plcnt;


int have_showsheet = 0;

char ps_roff_xi [16]; /* to hold the DITROFF xi line... */

static char * tex1;   /* to capture important dvi2ps lines... */
static char * tex2;

/*
 * Function declarations 
 */
static int   ps_gettype();
static void  do_post_doc();
#if 0
static void  do_other_doc();
#endif
static void  ps_copyprolog();
static void  ps_roff_copyprolog();
static void  ps_mpage_copyprolog();
static void  ps_skip_to_page();
static int   do_post_sheet();
static void  ps_sheetsetup();
static int   post_onepage();
static void  do_roff_tailer();
int   ps_check();
void  do_ps_doc();

/*
 * Peek at the first two chacters on the open file and check for the
 * two character postscript flag "%!".  If the file is not postscript
 * then the characters are pushed back into the input stream (hopefully).
 */
int
ps_check(infd)
 FILE *infd;
{
    int firstchar;
    int secondchar;
    
    Debug(DB_PSCHECK, "%%ps_check: in ps_check\n", 0);

    /*
     * eliminate blank files
     */
    if ((firstchar = fgetc(infd)) == EOF) {
        Debug(DB_PSCHECK, "%%ps_check: file is blank\n", 0);
        return 0;
    }

    /*
     * Skip any CTRL-D chars
     * Hope there are no text files starting with ctrl-d's
     */
    while (firstchar == 4)
        firstchar = fgetc(infd);

    /*
     * eliminate non-postscript files
     */
    if (firstchar != '%') {
        Debug(DB_PSCHECK, "%ps_check: 1st char is '%c' not '%'\n", firstchar);
        if (ungetc(firstchar, infd) == EOF) {
            fprintf(stderr, "%s: Lost first character of file ", MPAGE);
            fprintf(stderr, "while checking for postscript\n.");
        }
        return 0;
    }
    Debug(DB_PSCHECK, "%%ps_check: 1st char is '%c'\n", firstchar);
    /*
     * eliminate one character files (containing only a %)
     */
    if ((secondchar = fgetc(infd)) == EOF) {
        Debug(DB_PSCHECK, "%%ps_check: no second char\n", 0);
        if (ungetc(firstchar, infd) == EOF) {
            fprintf(stderr, "%s: Lost first character of file ", MPAGE);
            fprintf(stderr, "while checking for postscript\n.");
        }
        return 0;
    }
    /*
     * eliminate files that don't have the full two character
     * sequence of "%!".
     */
    if (secondchar != '!') {
        Debug(DB_PSCHECK, "%%ps_check: 2nd char is '%c' not '!'\n", secondchar);
        if (ungetc(secondchar, infd) == EOF) {
            fprintf(stderr, "%s: Lost first two characters of ", MPAGE);
            fprintf(stderr, "file while checking for postscript\n.");
            return 0;
        }
        if (ungetc(firstchar, infd) == EOF) {
            fprintf(stderr, "%s: Lost first character of file ", MPAGE);
            fprintf(stderr, "while checking for postscript.\n");
        }
        return 0;
    }
    /*
     * for post script files the first two characters (the "%!") are
     * digested by this routine.  It's just easier than dealing
     * with the problems encounted if the characters can't be ungetc'ed.
     */
    Debug(DB_PSCHECK, "%%ps_check: 2nd char is '%c'\n", secondchar);
    Debug(DB_PSCHECK, "%%ps_check: input is postscript\n", 0);

    return 1;

} /* ps_check */



static int
ps_gettype(fd, outfd)
 FILE *fd;
 FILE *outfd;
{
    int ps_type, end_comments;

    Debug(DB_PSDOC, "%%ps_gettype: in ps_gettype\n", 0);
    /*
     * error check for truncated files
     */
    if (fgets(currline, LINESIZE-1, fd) == NULL) {
        Debug(DB_PSDOC, "%%ps_gettype: got eof on first line\n", 0);
        return PS_NONE;
    }
    /*
     * check for non-conforming postscript
     * Note that %! is already gone down the drain...
     */
    if (strncmp(currline, PS_FLAG, strlen(PS_FLAG)) != 0) {
        Debug(DB_PSDOC, "%%ps_gettype: no match PS_FLAG \"%s\"\n", currline);
        return PS_OTHER;
    }
    /*
     * we have some form of conforming postscript, try to identify the
     * type
     */
    Debug(DB_PSDOC, "%%ps_gettype: conforming postscript\n", 0);
    end_comments = 0;
    ps_type = PS_CONFORM;
    while (!end_comments) {
        /*
         * if we get end of file then we assume non-conforming PS
         */
        if (fgets(currline, LINESIZE-1, fd) == NULL) {
            Debug(DB_PSDOC, "%%ps_gettype: eof in comments\n", 0);
            return PS_OTHER;
        }
        /*
         * if we have run out of leading comments then assume 
         * conforming PS (because we had a valid "%!" line)
         */
        if (currline[0] != '%') {
            Debug(DB_PSDOC, "%%ps_gettype: out off comments\n", 0);
            fprintf(outfd, "%s", currline);
            return PS_CONFORM;
        }
        /*
         * print out the comment line with an extra % to disguise the comment
         */
        fprintf(outfd, "%%%s", currline);
        /*
         * check for the end of the leading comments section
         */
        if (strncmp(currline, "%%EndComments", 13) == 0)
            end_comments = 1;
        /*
         * Some tricky way to handle MS-windows postscript files...
         * probably doesn't work.
        */
        if (strncmp(currline, "%%Pages:", 8) == 0 &&
            ps_type == PS_MSWINDOWS)
            return ps_type;
        /*
         * once we know the type of PS, we no longer need to keep
         * checking.
         */
        if (ps_type != PS_CONFORM)
            continue;
        /*
         * check for mpage output
         */
        if (!strncmp(currline, "%%Creator: ", 11)) {
            if (!strncmp(currline+11, MPAGE, strlen(MPAGE))) {
                Debug(DB_PSDOC, "%%ps_gettype: mpage document\n", 0);
                ps_type = PS_MPAGE;
            }
            else if (!strncmp(currline+11, "Windows PSCRIPT", 15)) {
                Debug(DB_PSDOC, "%%ps_gettype: windows document\n", 0);
                ps_type = PS_MSWINDOWS;
            }
            else if (!strncmp(currline+11, "dvips", 5)) {
                Debug(DB_PSDOC, "%%ps_gettype: idvips\n", 0);
            }
        }
        /*
         * check for psroff/tex output
         */
        if (strncmp(currline, "%%Title: ", 9) == 0) {
            if (strstr(currline, "ditroff")) {
                Debug(DB_PSDOC, "%%ps_gettype: psroff\n", 0);
                ps_type = PS_PSROFF;
            }
            else if (strstr(currline, ".dvi")) {
                Debug(DB_PSDOC, "%%ps_gettype: dvi2ps\n", 0);
                ps_type = PS_TEX;
            }
        }
        if (strncmp(currline, "%DVIPS", 6) == 0) {
            Debug(DB_PSDOC, "%%ps_gettype: dvips\n", 0);
            if (ps_type != PS_TEX)
                ps_type = PS_TEX2;
            return ps_type; 
        }
    }
#ifdef DEBUG
    if (ps_type == PS_CONFORM) {
        Debug(DB_PSDOC, "%%ps_gettype: unknown type, conforming PS\n", 0);
    }
#endif

    return ps_type;

} /* ps_gettype */



void
do_ps_doc(fd, asheet, outfd, fname)
 FILE *fd;
 struct sheet *asheet;
 FILE *outfd;
 char * fname;
{

    Debug(DB_PSDOC, "%%do_ps_doc: postscript document\n", 0);

    file_name = fname;
    ps_posttype = ps_gettype(fd,outfd);
    Debug(DB_PSDOC, "%%do_ps_doc: document type is %d\n", ps_posttype);
    if (ps_posttype != PS_NONE)
        do_post_doc(fd, asheet, outfd);

    return;

} /* do_ps_doc */



static void
do_post_doc(fd, asheet, outfd)
 FILE *fd;
 struct sheet *asheet;
 FILE *outfd;
{

    ps_at_trailer = FALSE;
    Debug(DB_POST, "%%do_post_doc: prolog\n", 0);
    ps_copyprolog(fd, outfd);
    /*
     * while there is still input, print pages
     */
    Debug(DB_POST, "%%do_post_doc: pages\n", 0);
    do_sheets(do_post_sheet, fd, asheet, outfd);
    Debug(DB_POST, "%%do_post_doc: trailer\n", 0);
    do_roff_tailer(fd, outfd);

    return;

} /* do_post_doc */


#if 0
/* not used yet... */
static void
do_other_doc(fd, asheet, outfd)
 FILE *fd;
 struct sheet *asheet;
 FILE *outfd;
{

    ps_at_trailer = FALSE;
    ps_copyprolog(fd, outfd);

    return;

} /* do_other_doc */
#endif



static void
ps_copyprolog(fd, outfd)
 FILE *fd;
 FILE *outfd;
{

    Debug(DB_PSDOC, "%%ps_copyprolog: adding mpage prolog\n", 0);
    if (!have_showsheet) {
        fprintf(outfd, "/showsheet { showpage } bind def\n");
        fprintf(outfd, "/showpage { } def\n");
        have_showsheet = 1;
    }
    had_ps = 1;
    Debug(DB_PSDOC, "%%ps_copyprolog: copying prolog\n", 0);
    if (ps_posttype == PS_PSROFF) {
        Debug(DB_PSDOC, "%%ps_copyprolog: calling ps_roff_copyprolog\n",0);
        ps_roff_copyprolog(fd, outfd);
        return;
    }
    if (ps_posttype == PS_MPAGE) {
        Debug(DB_PSDOC, "%%ps_copyprolog: calling ps_mpage_copyprolog\n",0);
        ps_mpage_copyprolog(fd, outfd);
        return;
    }
    while (fgets(currline, LINESIZE-1, fd) != NULL) {
        if (strncmp(currline, "%%Page:", 7) == 0) {
            fprintf(outfd, "%% %s", currline);
            return;
        }
        if (ps_posttype == PS_TEX || ps_posttype == PS_TEX2) { 
            if (ps_posttype == PS_TEX2 && strstr(currline, " bop ")) {
                /* dvips output without comments... */
                have_line = 1;
                return;
            }
            if (strncmp(currline, "TeXDict", 7) == 0) {
            /* SHOULD PROBABLY REMOVE THIS. SEE BELOW  /dictionarystack */
                /*
                 * Hope all dvi2ps progs work the same:
                 * capture the TeX init code so we can run it 'manually' for
                 * every page. This is needed as this code sets up a gstate 
                 * that conflicts with mpage...
                 * This trick seems to work for text, but figures within the dvi
                 * file seem to have a mind of their own...
                 */
                if (tex1)
                    free(tex1);
                tex1 = malloc(strlen(currline)+1);
                strcpy(tex1, currline);
                fprintf(outfd, "%s", currline);

                fgets(currline, LINESIZE-1, fd);
                if (tex2)
                    free(tex2);
                tex2 = malloc(strlen(currline)+1);
                strcpy(tex2, currline);
            }
        }
        fprintf(outfd, "%s", currline);
    }
    Debug(DB_PSDOC, "%%ps_copyprolog: eof before %%%%EndProlog\n", 0);
    fprintf(outfd, "%%%%EndProlog\n");

    return;

} /* ps_copyprolog */



static void
ps_roff_copyprolog(fd, outfd)
 FILE *fd;
 FILE *outfd;
{

    Debug(DB_PSDOC, "%%ps_roff_copyprolog: copying psroff prolog\n", 0);
    while(fgets(currline, LINESIZE-1, fd) != NULL) {
 /*       if (strcmp(currline, "xi\n") == 0) */
        if (strstr(currline, "xi\n")) {
            fprintf(outfd, "%%%s", currline); 
            strcpy(ps_roff_xi, currline);
        }
        else if (strncmp(currline, "%%Page:", 7) == 0) {
            fprintf(outfd, "/p { } def\n");
            fprintf(outfd, "/xt { } def\n");
            fprintf(outfd, "/xs { } def\n");
            fprintf(outfd, "%% %s", currline);
            Debug(DB_PSDOC, "%%ps_copyprolog: Done\n", 0);
            return;
        }
        else
            fprintf(outfd, "%s", currline);
    }
    Debug(DB_PSDOC, "%%ps_copyprolog: eof before %%%%EndProlog\n", 0);
    fprintf(outfd, "/p { } def\n");
    fprintf(outfd, "/xt { } def\n");
    fprintf(outfd, "/xs { } def\n");
    fprintf(outfd, "%%%%EndProlog\n");

    return;

} /* ps_roff_copyprolog */



static void
ps_mpage_copyprolog(fd, outfd)
 FILE *fd;
 FILE *outfd;
{

    Debug(DB_PSDOC, "%%ps_mpage_copyprolog: skipping mpage prolog\n", 0);
    while(fgets(currline, LINESIZE-1, fd) != NULL) {
        if (strncmp(currline, "%%Page:", 7) == 0)  {
            fprintf(outfd, "%% %s", currline);
            Debug(DB_PSDOC, "%%ps_copyprolog: Done\n", 0);
            return;
        }
    }
} /* ps_mpage_copyprolog */



static void
ps_skip_to_page(fd)
 FILE *fd;
{

    Debug(DB_PSDOC, "%%ps_skip to page: reading until %%%%Page:\n", 0);
    while(fgets(currline, LINESIZE-1, fd) != NULL) {
        Debug(DB_PSDOC, "%% %s", currline);
        if (strncmp(currline, "%%Page:", 7) == 0)
            return;
    }
    Debug(DB_PSDOC, "%%ps_skip_to_page: eof before %%%%Page:\n", 0);

    return;

} /* ps_skip_to_page */

/* GPN */
/* #define NSCALE	to take care of previous scaling */
#ifdef NSCALE
char *NScale =  "/gpnsavematrix {orgmatrix currentmatrix pop} bind def\n"
                "/gpnrestorematrix {orgmatrix setmatrix} bind def\n"
                "/orgmatrix matrix def\n"
                "gpnsavematrix\n"
                "orgmatrix orgmatrix invertmatrix pop\n"
                "/gpnxs\n"
                "    orgmatrix 0 get 0.0000 eq\n"
                "     {orgmatrix 1 get abs}\n"
                "     {orgmatrix 0 get abs}\n"
                "    ifelse def\n"
                "/gpnys\n"
                "    orgmatrix 3 get 0.0000 eq\n"
                "     {orgmatrix 2 get abs}\n"
                "     {orgmatrix 3 get abs}\n"
                "    ifelse def\n"
                "/gpnxs gpnxs gscurrentresolution 0 get 72 div mul def\n"
                "/gpnys gpnys gscurrentresolution 1 get 72 div mul def\n";
#endif /* NSCALE */

static int
do_post_sheet(fd, asheet, outfd)
 FILE *fd;
 struct sheet *asheet;
 FILE *outfd;
{
    int rtn_val = FILE_MORE;
    int sh_high, sh_wide;

    if ((points->pp_origin_x == 0 && !points->skip) || opt_file) {
        /*
         * keep track of the pages printed
         */
        ps_pagenum++;
        fprintf(outfd, "%%%%Page: %d %d\n", ps_pagenum, ps_pagenum);
# ifdef DEBUG
        if (Debug_flag & DB_PSMPAGE)
            fprintf(outfd, "(Page: %d\\n) print flush\n", ps_pagenum);
# endif /* DEBUG */
#if 0
     /* seems to give memory problems... */
        fprintf(outfd, "/sheetsave save def\n");
#endif

        /*
         * Now is the time to print a sheet header...
         * for now, this has to be done before outline...
         */
        sheetheader(outfd, file_name);

        /*
         * print the page outline
         */
        mp_outline(outfd, asheet);

        /*
         * run through the list of base points for putting reduced pages
         * on the printed page
         */
        points = asheet->sh_pagepoints;
    }
    while ((points->pp_origin_x!=0 || points->skip) && rtn_val == FILE_MORE) {

        /* GPN. skip this page ?*/
     	 if (points->skip!=0)   {
             ps_skip_to_page(fd);
             points++;
             continue;
     	 }

        /*
         * Save current graphics context so we can scale/translate
         * from known position and size.
         */
        fprintf(outfd, "gsave\n");

        /*
         * print one reduced page by moving to the proper point,
         * turning to the proper aspect, scaling to the proper
         * size, and setting up a clip path to prevent overwritting;
         * then print a reduced page of output.
         */
        Debug(DB_PSMPAGE, "%%%% %%%%ReducedPageStartsHere\n", outfd);
# ifdef DEBUG
        if (Debug_flag & DB_PSMPAGE) {
            fprintf(outfd, "(    %d %d translate %d rotate\\n)",
                            points->pp_origin_x(), points->pp_origin_y(),
                            asheet->sh_rotate);
            fprintf(outfd, " print flush\n");
        }
# endif /* DEBUG */

#ifdef  NSCALE /*GPN*/
        fprintf (outfd, NScale);
        fprintf(outfd, "%d gpnxs mul %d gpnxs mul translate %d rotate\n",
                      points->pp_origin_x(), points->pp_origin_y(),
   	     	      	                           asheet->sh_rotate);

        sh_wide = (*asheet->sh_width)();
        sh_high = (*asheet->sh_height)();
#else /* NSCALE */
        fprintf(outfd, "%d %d translate\n",
                       points->pp_origin_x(), points->pp_origin_y());
   	if (asheet->sh_rotate)
            fprintf(outfd, "%d rotate\n", asheet->sh_rotate);

        /*
         * Take extra page margins into account
         */
        sh_wide = (*asheet->sh_width)() - pagemargin_left - pagemargin_right;
        sh_high = (*asheet->sh_height)() - pagemargin_top - pagemargin_bottom;
#endif

        if (opt_square) {
            int newhigh = sh_high, newwide = sh_wide;

            if (sh_wide * ps_height > sh_high * ps_width)
                newwide = (sh_high * ps_width) / ps_height;
            else
                newhigh = (sh_wide * ps_height) / ps_width;

#ifdef  NSCALE /*GPN*/
    	    fprintf(outfd, "%d gpnxs mul %d gpnys mul translate\n",
#else
            fprintf(outfd, "%d %d translate\n",
#endif

                           (sh_wide - newwide) / 2, (sh_high - newhigh) / 2);
            sh_wide = newwide;
            sh_high = newhigh;
   	}

#ifndef NSCALE
        fprintf(outfd, "%d %d translate\n", pagemargin_left, pagemargin_bottom);
#endif

        fprintf(outfd, "%d %d div %d %d div scale\n",
                       sh_wide, ps_width, sh_high, ps_height);
        /* output the clip path */

#ifdef  NSCALE /*GPN*/
        fprintf(outfd, "0 0 moveto 0 %d gpnys mul lineto %d gpnxs mul"
                       " %d gpnys mul lineto ",
       	       	    ps_height, ps_width, ps_height);
        fprintf(outfd, "%d gpnxs mul 0 lineto\n", ps_width);
#else /* NSCALE */
        fprintf(outfd, "0 0 moveto 0 %d lineto %d %d lineto ",
                       ps_height, ps_width, ps_height);
        fprintf(outfd, "%d 0 lineto\n", ps_width);
#endif

#ifdef  NSCALE /*GPN*/
   	fprintf(outfd, "closepath clip\n");
#else
        fprintf(outfd, "closepath clip newpath\n");
#endif /* NSCALE */
#if 0
   /* does not seem to be neccessary. seems to create dictionary
      stack underflows...
    */
        if ((ps_posttype == PS_TEX || ps_posttype == PS_TEX2) && tex1)
            /* start dvi2ps init every page */
	    fprintf(outfd, "%s%s", tex1, tex2);
#endif

        /*
         * do the individual sheet setup
         */
        ps_sheetsetup(outfd);
        /*
         * place one reduce page on the printed page
         */
        plcnt = 0;
        rtn_val = post_onepage(fd, asheet, outfd);
        /*
         * clean up after mpage as drawn its page
         */
        fprintf(outfd, "grestore\n");
        points++;
    }
#if 0
    fprintf(outfd, "sheetsave restore\n");
#endif

    /*
     * print the sheet
     */
    if (points->pp_origin_x == 0 || (rtn_val == FILE_EOF && opt_file))
        fprintf(outfd, "showsheet\n");

    /*
     * let the upper level know about the status of possible EOF
     */
    return rtn_val;

} /* do_post_sheet */



static void
ps_sheetsetup(outfd)
 FILE *outfd;
{

    switch (ps_posttype) {
        case PS_PSROFF: fprintf(outfd, "%s", ps_roff_xi);
                        fprintf(outfd, "/p {} def\n");
                        break;
/*
        case PS_MPAGE:  fprintf(outfd, "/showpage {} def\n");
                        break;
*/
    }

        return;

} /* ps_sheetsetup */



static int
post_onepage(fd, asheet, outfd)
 FILE *fd;
 struct sheet *asheet;
 FILE *outfd;
{

    Debug(DB_PSROFF, "%%post_onepage: Begin page\n", 0);
    if (ps_at_trailer) {
        Debug(DB_PSROFF, "%%post_onepage: still at trailer\n", 0);
        return FILE_EOF;
    }

    if (have_line) {
        fprintf(outfd, "%s", currline);
        have_line = 0;
    }
        
    while(fgets(currline, LINESIZE-1, fd) != NULL) {
#ifndef DONT_SKIP_EPS
        /* this piece of code will skip over the EPS files included
           in the postscript, without messing
           inside them.   Why? If you include
           pictures, done with xfig, in your tex file, they contain
           the %%Page  lines... mpage would otherwise
           force a page break there!!
        */
        if (strncmp(currline, "%%BeginDocument:",
                    strlen("%%BeginDocument:")) == 0) {
            while (strncmp(currline, "%%EndDocument",
                           strlen("%%EndDocument")) != 0) {
                plcnt++;
                fprintf(outfd, "%s", currline);
                fgets(currline, LINESIZE-1, fd);
                if (feof(fd) ) {
                    Debug(DB_PSROFF, "%%post_onepage: eof\n", 0);
                    return FILE_EOF;
                }
            }
        }
#endif
        if (strncmp(currline, "%%Page:", 7) == 0) {
            fprintf(outfd, "%% %s", currline);
            /* only if there is already output to this logical page */
            if (!plcnt)
                continue;
            Debug(DB_PSROFF, "%%post_onepage: next page\n", 0);
            return FILE_MORE;
        }
        if (opt_killtrail && (strncmp(currline, "%%Trailer", 9) == 0 ||
            strncmp(currline, "%%PSTrailer", 11) == 0)) {
            fprintf(outfd, "%% %s", currline);
            Debug(DB_PSROFF, "%%post_onepage: found trailer\n", 0);
            ps_at_trailer = TRUE;
            return FILE_EOF;
        }
        /* For netscape output */
        if (strncmp(currline, "%%EOF", 5) == 0) {
            fprintf(outfd, "%% %s", currline);
            Debug(DB_PSROFF, "%%post_onepage: EOF\n", 0);
            return FILE_EOF;
        }
        if (ps_posttype == PS_MPAGE && strncmp(currline, "showsheet", 9) == 0)
            continue;
        if (ps_posttype == PS_TEX || ps_posttype == PS_TEX2) {
            if (ps_posttype == PS_TEX2 &&
                (strstr(currline, "eop\n") || strstr(currline, "eop end\n"))) {
                fprintf(outfd, "%s", currline);
                Debug(DB_PSROFF, "%%post_onepage: found TEX eop\n", 0);
                return FILE_MORE;
            }
            if (strncmp(currline, "TeXDict", 7) == 0) {
                /*
                 * Hope all dvi2ps progs work the same:
                 * capture the TeX init code so we can run it 'manually' for
                 * every page. This is needed as this code sets up a gstate 
                 * that conflicts with mpage...
                 * This trick seems to work for text, but figures within the dvi
                 * file seem to have a mind of their own...
                 */
                if (tex1)
                    free(tex1);
                tex1 = malloc(strlen(currline)+1);
                strcpy(tex1, currline);
                fprintf(outfd, "%s", currline);

                fgets(currline, LINESIZE-1, fd);
                if (tex2)
                    free(tex2);
                tex2 = malloc(strlen(currline)+1);
                strcpy(tex2, currline);
            }
        }
        fprintf(outfd, "%s", currline);
        plcnt++;
    }
    Debug(DB_PSROFF, "%%post_onepage: eof\n", 0);

    return FILE_EOF;

} /* post_onepage */



static void
do_roff_tailer(fd, outfd)
 FILE *fd, *outfd;
{
#ifdef DEBUG
        int i = 0;
#endif

    Debug(DB_PSDOC, "%%do_roff_trailer: looking for eof\n", 0);
    while(fgets(currline, LINESIZE-1, fd) != NULL) {
#ifdef DEBUG
        i++;
        Debug(DB_PSDOC, "%%%s", currline);
#endif
        ;
    }
    Debug(DB_PSDOC, "%%do_roff_trailer: tailer of %d lines\n", i);

    return;

} /* do_roff_tailer */