static void redirect(int pg) /* next page we're printing */ { static FILE *fp_null = NULL; /* if output is turned off */ /* * * If we're not supposed to print page pg, fp_out will be directed to /dev/null, * otherwise output goes to stdout. * */ if ( pg >= 0 && in_olist(pg) == ON ) fp_out = stdout; else if ( (fp_out = fp_null) == NULL ) fp_out = fp_null = fopen("/dev/null", "w"); } /* End of redirect */
static void readpages(void) { int endpagelen; /* length of ENDPAGE */ int pagelen; /* and PAGE strings */ int sawendpage = TRUE; /* ENDPAGE equivalent marked last page */ int gotpage = FALSE; /* TRUE disables BEGINSETUP stuff */ /* * * Records starting and ending positions of the requested pages (usually all of * them), puts global definitions in the prologue, and remembers where the TRAILER * was found. * * Page boundaries are marked by the strings PAGE, ENDPAGE, or perhaps both. * Application programs will normally find one or the other more convenient, so * in most cases only one kind of page delimiter will be found in a particular * document. * */ pages[0].start = ftell(fp_in); /* first page starts after ENDPROLOG */ endprolog = ENDPROLOG; endpagelen = strlen(ENDPAGE); pagelen = strlen(PAGE); while ( fgets(buf, sizeof(buf), fp_in) != NULL ) if ( buf[0] != '%' ) continue; else if ( strncmp(buf, ENDPAGE, endpagelen) == 0 ) { if ( in_olist(page++) == ON ) { pages[next_page].empty = FALSE; pages[next_page++].stop = ftell(fp_in); } /* End if */ pages[next_page].start = ftell(fp_in); sawendpage = TRUE; gotpage = TRUE; } else if ( strncmp(buf, PAGE, pagelen) == 0 ) { if ( sawendpage == FALSE && in_olist(page++) == ON ) { pages[next_page].empty = FALSE; pages[next_page++].stop = ftell(fp_in) - strlen(buf); } /* End if */ pages[next_page].start = ftell(fp_in) - strlen(buf); sawendpage = FALSE; gotpage = TRUE; } else if ( gotpage == FALSE && strcmp(buf, BEGINSETUP) == 0 ) { fprintf(fp_out, "%s", endprolog); fprintf(fp_out, "%s", BEGINSETUP); moreprolog(ENDSETUP); endprolog = ENDSETUP; } else if ( strcmp(buf, BEGINGLOBAL) == 0 ) { moreprolog(ENDGLOBAL); } else if ( strcmp(buf, TRAILER) == 0 ) { if ( sawendpage == FALSE ) pages[next_page++].stop = ftell(fp_in) - strlen(buf); endoff = ftell(fp_in); break; } /* End if */ } /* End of readpages */
void t_page(int n) /* do whatever new page functions */ { int m, i; char buf[1024], *bp; pgnum[np++] = n; pgadr[np] = ftell(fp); if (np > npmax) npmax = np; if (output == 0) { output = in_olist(n); t_init(1); return; } /* have just printed something, and seen p<n> for next one */ putpage(); fflush(stdout); if (nowait) return; next: for (bp = buf; (*bp = readch()); ) if (*bp++ == '\n' || bp >= &buf[sizeof buf - 1]) break; *bp = 0; switch (buf[0]) { case 0: done(); break; case '\n': output = in_olist(n); t_init(1); return; case '!': callunix(&buf[1]); fputs("!\n", stderr); break; case 'e': erase = 1 - erase; break; case 'w': wflag = 1 - wflag; break; case 'a': aspect = atof(&buf[1]); break; case '-': case 'p': m = atoi(&buf[1]) + 1; if (fp == stdin) { fputs("you can't; it's not a file\n", stderr); break; } if (np - m <= 0) { fputs("too far back\n", stderr); break; } np -= m; fseek(fp, pgadr[np], 0); output = 1; t_init(1); return; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': m = atoi(&buf[0]); for (i = 0; i < npmax; i++) if (m == pgnum[i]) break; if (i >= npmax || fp == stdin) { fputs("you can't\n", stderr); break; } np = i + 1; fseek(fp, pgadr[np], 0); output = 1; t_init(1); return; case 'o': outlist(&buf[1]); output = 0; t_init(1); return; case '?': fputs("!cmd unix cmd\n", stderr); fputs("p print this page again\n", stderr); fputs("-n go back n pages\n", stderr); fputs("n print page n (previously printed)\n", stderr); fputs("o... set the -o output list to ...\n", stderr); fputs("en n=0 -> don't erase; n=1 -> erase\n", stderr); fputs("an sets aspect ratio to n\n", stderr); break; default: fputs("?\n", stderr); break; } goto next; }