Debian Codegraph @

texk/dvi2tty/dvi2tty-src/dvi2tty.c

raw
1 /*
2 * dvi2tty
3 * Copyright (C) 2003 Marcel J.E. Mol <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 */
20
21 /*
22 * Include files
23 */
24
25 #include "c-auto.h"
26 #include "dvi2tty.h"
27
28 #if defined(WIN32) && defined(KPATHSEA)
29 #undef fopen
30 #define fopen fsyscp_fopen
31 #endif
32
33
34 /*
35 * Constant definitons
36 */
37 /*-----------------------------------------------------------------------*/
38 /* The following constants may be toggled before compilation to */
39 /* customize the default behaviour of the program for your site. */
40 /* Whichever their settings are, the defaults can be overridden at */
41 /* runtime. */
42 /*-----------------------------------------------------------------------*/
43
44 #define DEFSCAND FALSE /* default is not Scandinavian, toggle this if */
45 /* you have terminals with Scand. nat. chars */
46 #define DEFLATIN1 FALSE /* default is no latin1, toggle this if you */
47 /* have terminals with latin1 chars */
48 #define WANTPAGER TRUE /* default: try to pipe through a pager (like */
49 /* more) if stdout is tty and no -o switch */
50 #define DEFPAGER "more" /* CHANGE TO YOUR LOCAL PAGER */
51
52 /*------------------ end of customization constants ---------------------*/
53
54 #define OPTSET "haJweEpPousqlfFtvbcANUC" /* legal options */
55 #define OPTWARG "weEpPoFvb" /* options with argument */
56
57 /*
58 * USAGE CODES
59 */
60
61 #define wrnge 1 /* width switch arg out of range */
62 #define ign 2 /* ignore cause, print 'Usage:..' */
63 #define nan 3 /* not a number where one expected */
64 #define gae 4 /* garbage at end */
65 #define bdlst 5 /* bad page-numberlist */
66 #define onef 6 /* only one dvifile allowed */
67 #define bdopt 7 /* bad option */
68 #define onepp 8 /* only one page list allowed */
69 #define noarg 9 /* argument expected */
70 #define confl 10 /* -J, -N, -A, and -U conflict */
71 #define incone 11 /* inconsistent output encoding */
72
73
74 /*
75 * Variable definitions
76 */
77
78 const char *dvi2tty = "@(#) dvi2tty.c " VERSION " 20220501 M.J.E. Mol (c) 1989-2010, and contributors (c) -2022";
79
80
81 printlisttype * currentpage; /* current page to print */
82 printlisttype * firstpage; /* first page selected */
83 printlisttype * lastpage; /* last page selected */
84
85 FILE * DVIfile;
86 FILE * output;
87 bool outputtofile; /* tells if output goes to file or stdout */
88 int ttywidth; /* max nr of chars per printed line */
89 int espace; /* to fake calcs with ttywidth */
90
91 long foo; /* utility variable, "register" */
92 bool pager; /* tells if output is piped to a pager */
93 const char * path; /* name of the pager to run */
94 char * progname; /* our name */
95 int Argc;
96 char ** Argv;
97 char * DVIfilename;
98 const char * OUTfilename;
99 char optch; /* for option handling */
100
101
102
103 /*
104 * Function declarations
105 */
106
107 void setoption (const char * optarg);
108 void getargs (void);
109 void getpages (int j, const char * str);
110 void plcnxt (int pagnr);
111 void getfname (const char * str);
112 int getinteger(int * dest, int * j, const char * str);
113 int getlong (long * dest, int * j, const char * str);
114 void usage (int uerr);
115
116
117 /****************************************************************************/
118 /* */
119 /* M A I N */
120 /* */
121 /****************************************************************************/
122
123 int main(int argc, char **argv)
124 {
125 #if defined(WIN32) && defined(KPATHSEA)
126 char *enc;
127 #endif
128
129 progname = *argv;
130 Argc = argc;
131 Argv = argv;
132 #if defined(WIN32) && defined(KPATHSEA)
133 kpse_set_program_name(argv[0], "dvi2tty");
134 enc = kpse_var_value("command_line_encoding");
135 get_command_line_args_utf8(enc, &Argc, &Argv);
136 #endif
137
138 #ifdef WIN32
139 set_enc_string ("sjis", "default");
140 _setmode (fileno(stdout), _O_BINARY);
141 #else
142 set_enc_string (NULL, "default");
143 #endif
144 getargs(); /* read command line arguments */
145 #if defined(WIN32)
146 if ((DVIfile = fopen(DVIfilename, "rb")) == NULL)
147 #else
148 if ((DVIfile = fopen(DVIfilename, "r")) == NULL)
149 #endif
150 errorexit(filop); /* can't open dvifile */
151
152 if (outputtofile) { /* open the outfile, if needed */
153 #if defined(WIN32)
154 if ((output = fopen(OUTfilename, "wb")) == NULL)
155 #else
156 if ((output = fopen(OUTfilename, "w")) == NULL)
157 #endif
158 errorexit(filcr);
159
160 pager = FALSE;
161 }
162 else {
163 output = stdout;
164 if (pager && isatty(fileno(output))) { /* try to pipe to a pager */
165 if ((output = popen(path, "w")) == NULL) /* get pipe to pager */
166 errorexit(pipcr); /* make output to output */
167 }
168 else
169 pager = FALSE;
170 }
171
172 dvimain();
173
174 if (pager)
175 pclose(output); /* close pipe to pager */
176
177 exit(0);
178
179 } /* main */
180
181
182
183 /*
184 * GETARGS -- Process options from command line and from environment.
185 */
186
187 void getargs(void)
188 {
189 const char *str;
190 char *envp;
191 bool DVIfound; /* if a dvi filename found */
192
193 if (Argc <= 1)
194 usage(ign);
195
196 pageswitchon = FALSE; /* show all pages */
197 sequenceon = FALSE; /* selected pages are TeX-numbered */
198 outputtofile = FALSE; /* write to stdout */
199 pager = WANTPAGER; /* want paging, compile time option */
200 accent = TRUE; /* show all accent etc. as extra char */
201 ttfont = FALSE; /* assume tt font (verbatim mode) */
202 noffd = FALSE; /* print formfeed between pages */
203 scascii = DEFSCAND; /* scandinavian, compile time option */
204 latin1 = DEFLATIN1; /* latin1 support, compile time option */
205 utf8 = FALSE; /* print by utf encoding */
206 noligaturefi = FALSE; /* do not use ligature for ff,fi,fl,ffi,ffl */
207 ttywidth = 80; /* default terminal width */
208 espace = 0; /* to fake ttywith calcs */
209 DVIfound = FALSE;
210 printfont = FALSE; /* do not print font switches */
211 compose = TRUE; /* try to compose a combining character sequence */
212 allchar = FALSE; /* do not put out all characters */
213
214 if ((path = getenv("PAGER")) == NULL) /* find default pathname of page */
215 path = DEFPAGER; /* program in case paging is wanted */
216
217 /*
218 * First process environment variable.
219 */
220
221 if ((envp = getenv("DVI2TTY")) != NULL) { /* } keep vi happy */
222 while (*envp == ' ')
223 envp++;
224 while (*envp) { /* environment var args */
225 if (strchr(OPTSET, optch = *envp++) != NULL) {
226 /*
227 * we always pass one option, and arrange for optarg ourselfes,
228 * so setoption does not mesh up Argv
229 */
230 if (strchr(OPTWARG, optch) != NULL) {
231 while (*envp == ' ')
232 envp++;
233 if (*envp == '\0')
234 usage(noarg);
235 str = envp; /* str points to optarg */
236 while ((*envp != ' ') && (*envp != '\0'))
237 envp++; /* set envp just after optarg */
238 if (*envp != '\0')
239 *envp++ = '\0'; /* end optarg string */
240 }
241 else
242 str = "";
243 setoption(str);
244 }
245 else
246 usage(bdopt);
247 while (*envp == ' ')
248 envp++;
249 }
250 }
251
252 /*
253 * Now process command line options.
254 */
255
256 while (--Argc > 0) { /* command line args */
257 str = *++Argv;
258 if (*str != '-') { /* argument is not an option */
259 if (DVIfound) /* only one dvi file allowed */
260 usage(onef);
261 getfname(str);
262 DVIfound = TRUE;
263 }
264 else if (strchr(OPTSET, optch = *++str) != NULL) {
265 str++; /* point to rest of argument if any */
266 if ((strchr(OPTWARG, optch) != NULL) && (*str == '\0')) {
267 if (--Argc <= 0)
268 usage(noarg);
269 str = *++Argv;
270 }
271 setoption(str);
272 }
273 else
274 usage(bdopt);
275 }
276
277 if (!DVIfound)
278 usage(ign);
279
280 return;
281
282 } /* getargs */
283
284
285
286 /*
287 * SETOPTION -- Process an option.
288 */
289
290 void setoption(const char *optarg)
291 {
292 int j = 0;
293
294 while (strchr(OPTSET, optch) != NULL) {
295 switch (optch) {
296 case 'h' : usage(ign); break;
297 case 'q' : pager = FALSE; break;
298 case 'f' : pager = TRUE; break;
299 case 'F' : pager = TRUE;
300 path = optarg;
301 j = strlen(optarg);
302 break;
303 case 'J' : jautodetect = TRUE; break;
304 case 'U' : uptex = TRUE; japan = TRUE; /* upTeX */
305 jautodetect = FALSE;
306 enable_UPTEX(true);
307 set_enc_string (NULL, UPTEX_INTERNAL_ENC);
308 break;
309 case 'A' : asciip = TRUE; japan = TRUE; /* ASCII pTeX */
310 jautodetect = FALSE;
311 set_enc_string (NULL, PTEX_INTERNAL_ENC);
312 break;
313 case 'N' : nttj = TRUE; japan = TRUE; /* NTT jTeX */
314 jautodetect = FALSE;
315 set_enc_string (NULL, JTEX_INTERNAL_ENC);
316 break;
317 case 'E' :
318 switch (optarg[0]) {
319 case 'e' :
320 set_enc_string ("euc", NULL); break;
321 case 's' :
322 set_enc_string ("sjis", NULL); break;
323 case 'j' :
324 set_enc_string ("jis", NULL); break;
325 case 'u' :
326 utf8 = TRUE;
327 set_enc_string ("utf8", NULL);
328 if (optarg[1]=='1') {
329 noligaturefi = TRUE; j++;
330 }
331 break;
332 default :
333 usage(noarg);
334 }
335 j++;
336 break;
337 case 't' : ttfont = TRUE; break;
338 case 'l' : noffd = TRUE; break;
339 case 's' : scascii ^= 1; break;
340 case 'u' : latin1 ^= 1; break;
341 case 'a' : accent = FALSE; break;
342 case 'C' : compose = FALSE; break;
343 case 'c' : allchar = TRUE; break;
344 case 'P' : sequenceon = TRUE; /* fall through */
345 case 'p' : if (pageswitchon)
346 usage(onepp);
347 getpages(j, optarg);
348 break;
349 case 'w' : if (getinteger(&ttywidth, &j, optarg))
350 usage(nan);
351 if (optarg[j] != '\0')
352 usage(gae);
353 if ((ttywidth < 16) || (ttywidth > MAXTERMWIDTH))
354 usage(wrnge);
355 break;
356 case 'e' : if (getinteger(&espace, &j, optarg))
357 usage(nan);
358 if (optarg[j] != '\0')
359 usage(gae);
360 break;
361 case 'v' : if (getlong(&lineheight, &j, optarg))
362 usage(nan);
363 if (optarg[j] != '\0')
364 usage(gae);
365 /* lineheight *= 65536L; */
366 /* want to specify in pt, but have no getfloat */
367 break;
368 case 'o' : OUTfilename = optarg;
369 outputtofile = TRUE;
370 j = strlen(optarg);
371 break;
372 case 'b' : printfont = TRUE;
373 delim = optarg;
374 if (!strlen(optarg))
375 printfont = FALSE;
376 break;
377 default : usage(bdopt);
378 }
379 if ((optch = optarg[j++]) == '\0')
380 break;
381 if ( (optarg[j] == '\0') && (strchr(OPTWARG, optch) != NULL) ) {
382 if (--Argc <= 0)
383 usage(noarg);
384 optarg = *++Argv;
385 j = 0;
386 }
387 }
388
389 /* Option conflict */
390 if ((asciip && uptex) ||
391 (nttj && (asciip || uptex)) ||
392 (jautodetect && (nttj || asciip || uptex))) {
393 usage(confl);
394 }
395 if (((jautodetect || asciip || uptex || nttj || utf8)
396 && (scascii || latin1)) ||
397 (scascii && latin1)) {
398 usage(incone);
399 }
400
401 return;
402
403 } /* setoption */
404
405
406
407 /*
408 * GETPAGES -- Getr a list of pages to print.
409 */
410
411 void getpages(int j, const char *str)
412 {
413 int i, c;
414 int num;
415
416 pageswitchon = TRUE;
417 if ((firstpage = (printlisttype *) malloc(sizeof(printlisttype))) == NULL) {
418 perror("firstpage");
419 exit(1);
420 }
421 firstpage->all = FALSE;
422 firstpage->nxt = nil;
423 firstpage->pag = 0;
424 lastpage = firstpage;
425 currentpage = firstpage;
426 if (getinteger(&num, &j, str))
427 usage(nan);
428 plcnxt((int) num);
429 while (str[j]) {
430 c = str[j];
431 if (c == ',' || c == ':') {
432 j++;
433 if (getinteger(&num, &j, str))
434 usage(nan);
435 }
436 else
437 break;
438 if (c == ',')
439 plcnxt(num);
440 else {
441 if (currentpage->pag < 0) {
442 if (num > 0) {
443 currentpage->all = TRUE;
444 plcnxt(num);
445 }
446 else if (num < currentpage->pag)
447 for (i = currentpage->pag - 1; i >= num; i--)
448 plcnxt(i);
449 else
450 usage(bdlst);
451 }
452 else {
453 if (num < currentpage->pag)
454 usage(bdlst);
455 for (i = currentpage->pag + 1; i <= num; i++)
456 plcnxt(i);
457 }
458 }
459 }
460 if ((str[j] != ' ') && (str[j] != '\0')) {
461 usage(gae);
462 }
463 currentpage = firstpage;
464
465 return;
466
467 } /* getpages */
468
469
470
471 /*
472 * PLCNXT -- Place page-nr next in list.
473 */
474
475 void plcnxt(int pagnr)
476 {
477
478 currentpage = lastpage;
479 currentpage->pag = pagnr;
480 if ((lastpage = (printlisttype *) malloc(sizeof(printlisttype))) == NULL) {
481 perror("lastpage");
482 exit(1);
483 }
484 lastpage->all = FALSE;
485 lastpage->nxt = nil;
486 lastpage->pag = 0;
487 currentpage->nxt = lastpage;
488
489 return;
490
491 } /* plcnxt */
492
493
494
495 /*
496 * GETFNAME -- {Make sure we have a .dvi filename.
497 */
498
499 void getfname(const char *str)
500 {
501 int i;
502
503 i = strlen(str);
504 if (i == 0)
505 usage(ign);
506 if ((DVIfilename = (char *) malloc(i+5)) == NULL) {
507 perror("DVIfilename");
508 exit(1);
509 }
510 strcpy(DVIfilename, str);
511 #ifdef KPATHSEA
512 if (!kpse_readable_file(DVIfilename))
513 #else
514 if ((i < 5) || strcmp(str+i-4, ".dvi"))
515 #endif
516 strcat(DVIfilename, ".dvi");
517
518 return;
519
520 } /* getfname */
521
522
523
524
525 /*
526 * GETINTEGER -- Convert ascii to an integer. I'm sure there is a library
527 * call for it.
528 */
529
530 int getinteger(int *dest, int *j, const char *str)
531 {
532 int cum;
533 int sgn;
534 char ch;
535
536 ch = str[*j];
537 if (ch == '-') {
538 sgn = -1;
539 ch = str[++(*j)];
540 }
541 else
542 sgn = 1;
543
544 if ((ch >= '0') && (ch <= '9')) {
545 cum = 0;
546 while ((ch >= '0') && (ch <= '9')) {
547 cum = cum*10 + ch - '0';
548 ch = str[++(*j)];
549 }
550 *dest = sgn * cum;
551
552 return 0; /* return ok */
553 }
554
555 return 1; /* return error */
556
557 } /* getinteger */
558
559
560
561 /*
562 * GETLONG -- Convert ascii to a long. I'm sure there is a library
563 * call for it.
564 */
565
566 int getlong(long *dest, int *j, const char *str)
567 {
568 long cum;
569 int sgn;
570 char ch;
571
572 ch = str[*j];
573 if (ch == '-') {
574 sgn = -1;
575 ch = str[++(*j)];
576 }
577 else
578 sgn = 1;
579
580 if ((ch >= '0') && (ch <= '9')) {
581 cum = 0;
582 while ((ch >= '0') && (ch <= '9')) {
583 cum = cum*10L + ch - '0';
584 ch = str[++(*j)];
585 }
586 *dest = (long) sgn * cum;
587
588 return 0; /* return ok */
589 }
590
591 return 1; /* return error */
592
593 } /* getinteger */
594
595
596
597 /*
598 * ERROREXIT -- Exit program with an erro message.
599 */
600
601 void errorexit(int errorcode)
602 {
603
604 fprintf(stderr, "%s: ", progname);
605 switch (errorcode) {
606 case illop : fprintf(stderr, "Illegal op-code found: %d\n", opcode);
607 break;
608 case stkof : fprintf(stderr, "Stack overflow\n");
609 break;
610 case stkuf : fprintf(stderr, "Stack underflow\n");
611 break;
612 case stkrq : fprintf(stderr, "Cannot create dvi stack\n");
613 break;
614 case lnerq : fprintf(stderr, "Cannot allocate memory\n");
615 break;
616 case badid : fprintf(stderr, "Id-byte is not correct: %d\n ", opcode);
617 break;
618 case bdsgn : fprintf(stderr, "Bad signature: %d (not 223)\n",
619 (int) foo);
620 break;
621 case fwsgn : fprintf(stderr, "%d signature bytes (min. 4)\n",
622 (int) foo);
623 break;
624 case nopre : fprintf(stderr, "Missing preamble\n");
625 break;
626 case nobop : fprintf(stderr, "Missing beginning-of-page command\n");
627 break;
628 case nopp : fprintf(stderr, "Missing post-post command\n");
629 break;
630 case bdpre : fprintf(stderr, "Preamble occured inside a page\n");
631 break;
632 case bdbop : fprintf(stderr, "BOP-command occured inside a page\n");
633 break;
634 case bdpst : fprintf(stderr, "Postamble occured before end-of-page\n");
635 break;
636 case bdpp : fprintf(stderr, "Postpost occured before post-command\n");
637 break;
638 case nopst : fprintf(stderr, "Missing postamble\n");
639 break;
640 case illch : fprintf(stderr, "Character code out of range, 0..127\n");
641 break;
642 case filop : fprintf(stderr, "Cannot open dvifile\n");
643 break;
644 case filcr : fprintf(stderr, "Cannot create outfile\n");
645 break;
646 case pipcr : fprintf(stderr, "Cannot create pipe to pager\n");
647 break;
648 case bdfnt : fprintf(stderr, "Fail to get font information\n");
649 break;
650 default : fprintf(stderr, "Unkown error code\n");
651 break;
652 };
653 if (outputtofile)
654 unlink(OUTfilename);
655
656 exit(errorcode);
657
658 } /* errorexit */
659
660
661
662 /*
663 * USAGE -- Print the usage info. Also print a warning/error message
664 * if needed.
665 */
666
667 void usage(int uerr)
668 {
669
670 if (jautodetect || nttj || asciip || uptex)
671 fprintf(stderr, "%s (%s) %s", Progname, get_enc_string(), Copyright);
672 else
673 fprintf(stderr, "%s %s", Progname, Copyright);
674
675 if (uerr != ign) {
676 fprintf(stderr,"\n%s: ", progname);
677 switch (uerr) {
678 case wrnge : fprintf(stderr, "width arg out of range:16-%d",
679 MAXTERMWIDTH);
680 break;
681 case nan : fprintf(stderr,
682 "numeric argument expected for option %c",
683 optch);
684 break;
685 case gae : fprintf(stderr, "garbage in argument for option %c",
686 optch);
687 break;
688 case bdlst : fprintf(stderr, "mal-formed list of pagenumbers");
689 break;
690 case onef : fprintf(stderr, "only one infile argument allowed");
691 break;
692 case noarg : fprintf(stderr,
693 "option argument expected for option %c",
694 optch);
695 break;
696 case bdopt : fprintf(stderr, "bad option %c", optch);
697 break;
698 case onepp : fprintf(stderr, "only one pagelist allowed");
699 break;
700 case confl : fprintf(stderr, "-J, -N, -A, and -U are mutually exclusive");
701 break;
702 case incone : fprintf(stderr, "output encoding is not consistent");
703 break;
704 default : fprintf(stderr, "unknown usage error");
705 break;
706 }
707 fprintf(stderr, "\n");
708 }
709
710 fprintf(stderr, "\n%s\n\n", dvi2tty);
711 fprintf(stderr, "Usage: %s [ options ] <dvi-file>[.dvi]\n", progname);
712 fprintf(stderr, "Options are:\n");
713 fprintf(stderr,
714 " -ofile Write output to file, else write to stdout.\n");
715 fprintf(stderr,
716 " -plist Print pages whose TeX-page-number are in list.\n");
717 fprintf(stderr,
718 " -Plist Print pages whose sequential number are in list.\n");
719 fprintf(stderr,
720 " -wn Print the lines with width n characters, default 80.\n");
721 fprintf(stderr,
722 " -vn Use n for vertical line height, default 450000.\n");
723 fprintf(stderr,
724 " -evalue Add/Substract this value for spacing (-20..20)\n");
725 fprintf(stderr, " -f Try to pipe to a pager if output is a tty");
726 if (WANTPAGER)
727 fprintf(stderr, " (default).\n");
728 else
729 fprintf(stderr, ".\n");
730 fprintf(stderr, " -q Don't try to pipe to a pager");
731 if (WANTPAGER)
732 fprintf(stderr, ".\n");
733 else
734 fprintf(stderr, " (default).\n");
735 fprintf(stderr, " -Fprog Pipe output to pager prog.\n");
736 fprintf(stderr,
737 " -a Remove accents grave etc. from output: \\'{e} -> e.\n");
738 fprintf(stderr,
739 " -t Assuming that document was made with tt fonts\n");
740 fprintf(stderr,
741 " -l Write ''^L'' instead of formfeed between pages.\n");
742 fprintf(stderr,
743 " -s Toggle National Swedish/Finnish characters printed as aaoAAO (default %s).\n", DEFSCAND ? "off" : "on");
744 fprintf(stderr,
745 " -u Toggle latin1 support (default %s).\n", DEFLATIN1 ? "on" : "off");
746 fprintf(stderr,
747 " -J Enable auto detect for NTT JTeX, ASCII pTeX, and upTeX (japanese fonts).\n");
748 fprintf(stderr,
749 " -N Support NTT JTeX dvi.\n");
750 fprintf(stderr,
751 " -A Support ASCII pTeX dvi.\n");
752 fprintf(stderr,
753 " -U Support upTeX dvi.\n");
754 fprintf(stderr,
755 " -Eenc Output multibyte encoding. u:UTF8, e:EUC-JP s:Shift_JIS j:JIS\n"
756 " u1:UTF8 (do not use ligature for ff,fi,fl,ffi,ffl).\n");
757 fprintf(stderr,
758 " -C Don't try to compose a combining character sequence.\n");
759 fprintf(stderr,
760 " -c Override -a -u -s and print all characters 0-255.\n");
761 fprintf(stderr,
762 " -bdelim Print font switch as text: delimcmr10miled\n");
763 fprintf(stderr, " -h This help message.\n");
764 fprintf(stderr, "\n If you like this code and want to support is feel free\n to donate at Paypal [email protected]. Thanks.\n\n");
765
766 if (uerr != ign)
767 exit(uerr);
768 exit(0);
769
770 } /* usage */
771