1 /* Copyright 1992 Karl Berry
2 Copyright 2007 TeX Users Group
3 Copyright 2014 Clerk Ma
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
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., 51 Franklin Street, Fifth Floor, Boston, MA
25 #define dump_default_var TEX_format_default
26 #define dump_default " plain.fmt"
27 #define dump_ext_length 4
28 #define dump_default_length format_default_length
29 #define main_program texbody
30 #define edit_value tex_edit_value
31 #define edit_var "UFYFEJU"
34 extern char * replacement[];
36 #ifdef FUNNY_CORE_DUMP
37 void funny_core_dump (void);
40 /* The main program, etc. */
42 /* What we were invoked as and with. */
43 static char *program_name = NULL;
44 int gargc; /* number of args - set to zero after initialization */
45 char **gargv; /* char *gargv[] -- bkph ? */
47 /* The entry point: set up for reading the command line, which will
48 happen in `t_open_in', then call the main body. */
50 int main_init(int, char **); /* in local.c */
54 jmp_buf jumpbuffer; // for non-local jumps
56 int main (int ac, char *av[])
58 int flag = 0, ret = 0;
61 char custom_default[PATH_MAX];
71 if (main_init(gargc, gargv))
74 dump_default_var = dump_default;
75 dump_default_length = strlen(dump_default_var + 1);
79 ret = setjmp(jumpbuffer);
83 flag = main_program();
87 sprintf(log_line, "EXITING at %s %d %d %d\n", "MAIN", flag, ret, jump_used);
88 show_line(log_line, 0);
95 sprintf(log_line, "EXITING at %s %d %d %d\n", "JUMPOUT", flag, ret, jump_used);
96 show_line(log_line, 0);
100 if (endit(flag) != 0)
101 flag = 1; /* do final clean up in local.c */
109 else exit (flag); // avoid circularity!
113 /* texk/web2c/lib/texmfmp.c */
114 void t_open_in (void)
118 buffer[first] = 0; /* In case there are no arguments. */
121 if (gargc > optind && optind > 0)
123 for (i = optind; i < gargc; i++)
127 for (i = 1; i < gargc; i++)
130 if (allow_quoted_names && strchr(gargv[i], ' ') != NULL)
132 (void) strcat ((char *) &buffer[first], "\"");
133 (void) strcat ((char *) &buffer[first], gargv[i]);
134 (void) strcat ((char *) &buffer[first], "\"");
137 (void) strcat ((char *) &buffer[first], gargv[i]);
139 (void) strcat ((char *) &buffer[first], " ");
145 /* Find the end of the buffer. */
146 for (last = first; buffer[last]; ++last) ;
148 for (--last; last >= first
149 && ISBLANK (buffer[last]) && buffer[last] != '\r'; --last)
153 /* One more time, this time converting to TeX's internal character
154 representation. */ /* for command line input in this case */
157 for (i = first; i < last; i++)
158 buffer[i] = xord[buffer[i]];
162 /* All our interrupt handler has to do is set TeX's or Metafont's global
163 variable `interrupt'; then they will do everything needed. */
165 static void catch_interrupt (int err)
167 (void) signal (SIGINT, SIG_IGN);
169 if (interrupt++ >= 3)
172 (void) signal (SIGINT, catch_interrupt);
175 /* Besides getting the date and time here, we also set up the interrupt
176 handler, for no particularly good reason. It's just that since the
177 `fix_date_and_time' routine is called early on (section 1337 in TeX,
178 ``Get the first line of input and prepare to start''), this is as
179 good a place as any. */
181 void get_date_and_time (integer *sys_minutes,
189 (void) time (&clock); /* - seconds since 1970 */
193 sprintf(log_line, "The time is %u\n", clock);
194 show_line(log_line, 0);
199 show_line("Time not available!\n", 1);
202 tmptr = localtime (&clock);
203 /* MS C runtime library has trouble for clock >= 2^31 !!! */
204 if (tmptr == NULL) /* debugging 95/Dec/30*/
206 sprintf(log_line, "Cannot convert time (%0ld)!\n", clock);
207 show_line(log_line, 1);
211 *sys_minutes = 22 * 60 + 14;
215 *sys_minutes = tmptr->tm_hour * 60 + tmptr->tm_min;
216 *sys_day = tmptr->tm_mday;
217 *sys_month = tmptr->tm_mon + 1;
218 *sys_year = tmptr->tm_year + 1900;
222 sprintf(log_line, "%d-%d-%d %d:%d\n",
223 tmptr->tm_year + 1900,
228 show_line(log_line, 0);
236 if (signal(SIGINT, catch_interrupt) == SIG_ERR)
238 show_line(" CTRL-C handler not installed\n", 0);
240 uexit(1); /* do we care when run as DLL ? */
245 void (*old_handler)();
247 if ((old_handler = signal (SIGINT, catch_interrupt)) != SIG_DFL)
248 (void) signal (SIGINT, old_handler);
253 /* I/O for TeX and Metafont. */ /* give file name ? */
255 void complain_line (FILE *output)
259 #ifdef ALLOCATEBUFFER
260 sprintf(log_line, "! Unable to read an entire line---buf_size=%d.\n", current_buf_size);
262 sprintf(log_line, "! Unable to read an entire line---buf_size=%d.\n", buf_size);
265 if (output == stderr)
266 show_line(log_line, 1);
268 if (output == stdout)
269 show_line(log_line, 0);
271 fputs(log_line, output); // never
273 show_line(" (File may have a line termination problem.)", 0);
276 void show_bad_line (FILE *output, int first, int last)
281 for (i = first; i <= last; i++)
285 if ((show_in_hex && ch > 127))
297 /* putc('^', output); putc('^', output); */
300 /* putc (c, output); putc (d, output); */
307 /* putc('^', output); putc('^', output); */
310 /* putc (ch + 64, output); */
311 *s++ = (char) (ch + 64);
316 /* putc('^', output); putc('^', output); */
319 /* putc (ch - 64, output); */
320 *s++ = (char) (ch - 64);
324 /* putc(ch, output); */
328 // putc(' ', output); /* putc('\n', output); */
331 if (output == stderr)
332 show_line(log_line, 1);
334 if (output == stdout)
335 show_line(log_line, 0);
337 fputs(log_line, output); // log_file
340 // split off for convenience and use in ConsoleInput
341 bool input_line_finish (void)
346 /* if last line in file does not end with \n - never happens ? */
347 /* if (i == EOF && buffer[last] != '\n') buffer[last++] = '\n'; */
349 buffer[last] = ' '; /* space terminate */
350 if (last >= max_buf_stack)
351 max_buf_stack = last; /* remember longest line */
353 /* Trim trailing whitespace. */
354 /* #define isblank(c) ((c) == ' ' || (c) == '\t') */
355 /* What about \n ? Can't get in here ?- bkph */
356 /* What about control-Z that gets read in binary mode ? - bkph */
358 /* while (last > first && buffer[last - 1] <= ' ') --last; */
359 while (last > first) {
360 i = buffer[last - 1];
361 if (i == ' ' || i == '\t')
363 /* else if (trimeof && i == 26) --last; */ /* 93/Nov/24 */
367 /* if (trimeof != 0 && i == EOF && last == first)
368 return false; */ /* EOF and line empty */
370 // while (last > first
371 // && isblank (buffer[last - 1]) && buffer[last - 1] != '\r')
375 /* following added to check source file integrity ASCII 32 - 127 */
376 /* allow space, tab, new-page - also allow return, newline ? */
377 if (restrict_to_ascii)
380 for (i = first; i <= last; i++)
383 /* if (ch > 127 || (ch < ' ' && ch != '\t' && ch != '\f')) */
384 /* 1 -- 8, 11, 14 -- 31 are not good ASCII characters */
385 if (ch > 126 || (ch < ' ' && ch != '\t' && ch != '\f' && ch != '\r' && ch != '\n'))
387 sprintf(log_line, "\n! non ASCII char (%d) in line: ", ch);
388 show_line(log_line, 1);
390 fprintf(log_file, "\n! non ASCII char (%d) in line: ", ch);
391 /* buffer[i]= 127; */ /* not defined - invalid char */
398 show_bad_line(errout, first, last);
400 show_bad_line(log_file, first, last);
403 /* Don't bother using xord if we don't need to. */ /* for input line */
404 /* #ifdef NONASCII */ /* has been turned into command line flag - bkph */
407 for (i = first; i <= last; i++)
408 buffer[i] = xord[buffer[i]];
414 /* Read a line of input into buffer as efficiently as possible (ha ha)
415 while still looking like Pascal.
416 We set `last' to `first' and return `false' if we get to eof.
417 Otherwise, we return `true' and set last = first +
418 length(line except trailing whitespace). */
421 bool input_line (FILE *f)
423 // int ch, flag; /* for restrict_to_ascii case 94/Jan/21 */
424 char *u; /* 1994/July/3 for key_replace */
427 /* and here is the long way of doing this */
429 /* following is new version with tab expansion and key replacement */
430 /* may want to expand out separately for speed 1994/July/3 */
431 /* different versions depending on return_flag / tabexpand / key_replace */
432 /* while (last < buf_size && (i = getc (f)) != EOF) */
433 #ifdef ALLOCATEBUFFER
436 while (last < buf_size)
443 if (i == EOF || i == '\n' || (i == '\r' && return_flag))
445 else if (i == '\t' && tab_step != 0)
447 buffer[last++] = (ASCII_code) ' ';
449 #ifdef ALLOCATEBUFFER
450 if (last >= current_buf_size)
452 buffer = realloc_buffer(increment_buf_size);
454 if (last >= current_buf_size)
459 #ifdef ALLOCATEBUFFER
460 while ((last - first) % tab_step != 0)
462 while (last < buf_size && (last - first) % tab_step != 0)
466 buffer[last++] = (ASCII_code) ' ';
468 #ifdef ALLOCATEBUFFER
469 if (last >= current_buf_size)
471 buffer = realloc_buffer(increment_buf_size);
473 if (last >= current_buf_size)
483 if (key_replace && (u = replacement[i]) != NULL)
485 #ifdef ALLOCATEBUFFER
488 while (last < buf_size && *u != '\0')
491 buffer[last++] = (ASCII_code) *u++;
492 #ifdef ALLOCATEBUFFER
493 if (last >= current_buf_size)
495 buffer = realloc_buffer(increment_buf_size);
497 if (last >= current_buf_size)
503 else /* normal case */
505 buffer[last++] = (ASCII_code) i;
507 #ifdef ALLOCATEBUFFER
508 if (last >= current_buf_size)
510 buffer = realloc_buffer(increment_buf_size);
512 if (last >= current_buf_size)
519 if (return_flag) /* let return terminate line as well as newline */
521 if (i == '\r') /* see whether return followed by newline */
523 i = getc (f); /* in which case throw away the newline */
530 /* else buffer[last-1] = (ASCII_code) i; */
534 // Turn Ctrl-Z at end of file into newline 2000 June 22
535 if (i == EOF && trimeof && buffer[last - 1] == 26)
540 if (i == EOF && last == first)
543 /* Didn't get the whole line because buffer was too small? */
544 /* This shouldn't happen anymore 99/Jan/23 */
545 if (i != EOF && i != '\n' && i != '\r')
547 complain_line(errout);
550 complain_line(log_file);
552 /* This may no longer be needed ... now that we grow it */
553 if (truncate_long_lines)
555 while (i != EOF && i != '\n' && i != '\r')
557 i = getc (f); // discard rest of line
560 last--; /* just in case */
563 uexit(1); /* line too long */
566 return input_line_finish();
570 /* This string specifies what the `e' option does in response to an
573 static char *edit_value = "c:\\yandy\\WinEdt\\WinEdt.exe [Open('%s');SelLine(%d,7)]";
575 /* This procedure is due to sjc@s1-c. TeX (or Metafont) calls it when
576 the user types `e' in response to an error, invoking a text editor on
577 the erroneous source file. FNSTART is how far into STRINGPOOL the
578 actual filename starts; FNLENGTH is how long the filename is.
580 See ../site.h for how to set the default, and how to override it. */
582 /* called from close_files_and_terminate in tex9.c */
584 void call_edit (ASCII_code *stringpool, pool_pointer fnstart, integer fnlength, integer linenumber)
586 char *command, *s, *t, *u;
588 int sdone, ddone, ldone;
590 unsigned int commandlen;
591 ASCII_code *texfilename;
592 ASCII_code *log_file_name;
593 pool_pointer lgstart;
598 lgstart = str_start[texmf_log_name];
599 lglength = length(texmf_log_name);
600 log_file_name = stringpool + lgstart;
605 log_file_name = (unsigned char *) "";
608 sdone = ddone = ldone = 0;
609 texfilename = stringpool + fnstart;
611 for (i = 1; i <= in_open; i++)
612 (void) fclose (input_file[i]);
616 if (n > 0 && verbose_flag)
618 sprintf(log_line, "Closed %d streams\n", n);
619 show_line(log_line, 0);
622 s = kpse_var_value(edit_var);
627 commandlen = strlen (edit_value) + fnlength + lglength + 10 + 1 + 2;
628 command = (string) xmalloc (commandlen);
634 while ((c = *u++) != 0)
644 sprintf(log_line, "! bad command syntax (%c).\n", 'd');
645 show_line(log_line, 1);
647 sprintf(log_line, "! `%%d' cannot appear twice in editor command.\n");
648 show_line(log_line, 1);
653 (void) sprintf (s, "%d", linenumber);
665 sprintf(log_line, "! bad command syntax (%c).\n", 's');
666 show_line(log_line, 1);
668 sprintf(log_line, "! `%%s' cannot appear twice in editor command.\n");
669 show_line(log_line, 1);
674 t = (char *) texfilename;
678 for (i = 0; i < n; i++)
681 for (i = 0; i < n; i++)
691 sprintf(log_line, "! bad command syntax (%c).\n", 'l');
692 show_line(log_line, 1);
694 sprintf(log_line, "! `%%l' cannot appear twice in editor command.\n");
695 show_line(log_line, 1);
700 t = (char *) log_file_name;
704 for (i = 0; i < n; i++)
707 for (i = 0; i < n; i++)
728 *s = 0; /* terminate the command string */
730 if (strlen(command) + 1 >= commandlen)
732 sprintf(log_line, "Command too long (%d > %d)\n", strlen(command) + 1, commandlen);
733 show_line(log_line, 1);
740 if (system (command) != 0)
743 sprintf(log_line, "! Error in call: %s\n", command);
744 show_line(log_line, 1);
748 perrormod("! DOS says");
751 sprintf(log_line, " (TEXEDIT=%s)\n", edit_value);
752 show_line(log_line, 0);
753 show_line(" (Editor specified may be missing or path may be wrong)\n", 0);
754 show_line(" (or there may be missing -- or extraneous -- quotation signs)\n", 0);
761 /* Read and write format (for TeX) or base (for Metafont) files. In
762 tex.web, these files are architecture dependent; specifically,
763 BigEndian and LittleEndian architectures produce different files.
764 These routines always output BigEndian files. This still does not
765 make the dump files architecture-independent, because it is possible
766 to make a format file that dumps a glue ratio, i.e., a floating-point
767 number. Fortunately, none of the standard formats do that. */
769 #if !defined (WORDS_BIGENDIAN) && !defined (NO_FMTBASE_SWAP) /* this fn */
771 /* We don't REALLY care what `endian' the machine is after all ! */
772 /* But we do care about speed - so check exe file for following - bkph */
775 // char swapmarkerstring="ERROR: SWAPPING - NOT BigEndian AND NOT NoFmtBaseSwap";
778 /* This macro is always invoked as a statement. It assumes a variable
781 #define SWAP(x, y) temp = (x); (x) = (y); (y) = temp;
784 /* Make the NITEMS items pointed at by P, each of size SIZE, be the
785 opposite-endianness of whatever they are now. */
787 static int swap_items (char *p, int nitems, int size)
791 /* Since `size' does not change, we can write a while loop for each
792 case, and avoid testing `size' for each time. */
829 sprintf(log_line, "! I can't (un)dump a %d byte item.\n", size);
830 show_line(log_line, 1);
835 #endif /* not WORDS_BIGENDIAN and not NO_FMTBASE_SWAP */
837 /* Hmm, this could benefit from some on the fly compression - bkph */
838 /* and complementary decompression on input - bkph */
840 /* Here we write NITEMS items, each item being ITEM_SIZE bytes long.
841 The pointer to the stuff to write is P, and we write to the file
844 int do_dump (char *p, int item_size, int nitems, FILE *out_file)
846 #if !defined (WORDS_BIGENDIAN) && !defined (NO_FMTBASE_SWAP)
847 swap_items (p, nitems, item_size);
851 if (gzwrite(gz_fmt_file, p, (item_size * nitems)) != (item_size * nitems))
853 if ((int) fwrite (p, item_size, nitems, out_file) != nitems)
857 sprintf(log_line, "! Could not write %d %d-byte item%s.\n",
858 nitems, item_size, (nitems > 1) ? "s" : "");
859 show_line(log_line, 1);
863 /* Have to restore the old contents of memory, since some of it might get used again. */
864 #if !defined (WORDS_BIGENDIAN) && !defined (NO_FMTBASE_SWAP)
865 swap_items (p, nitems, item_size);
871 /* Hmm, this could benefit from some on the fly decompression - bkph */
873 /* Here is the dual of the writing routine. */
874 int do_undump (char *p, int item_size, int nitems, FILE *in_file)
877 if (gzread(gz_fmt_file, (void *) p, (unsigned int) (item_size * nitems)) <= 0)
879 if ((int) fread((void *) p, item_size, nitems, in_file) != nitems)
883 sprintf(log_line, "! Could not read %d %d-byte item%s.\n",
884 nitems, item_size, (nitems > 1) ? "s" : "");
885 show_line(log_line, 1);
889 #if !defined (WORDS_BIGENDIAN) && !defined (NO_FMTBASE_SWAP)
890 swap_items (p, nitems, item_size);
896 #ifdef FUNNY_CORE_DUMP
897 /* This procedure is due to chris@mimsy.umd.edu. It makes a core dump
898 without any sort of error status (abort(2) does return an error status,
899 so we don't want to use that). It is used only when making a preloaded
900 TeX from virtex, and is triggered by a magic file name requested as
901 input (see `open_input', above). */
903 void funny_core_dump ()
908 switch (pid = vfork ())
910 case -1: /* failed */
912 exit (-1); /* NOTREACHED */
915 (void) signal (SIGQUIT, SIG_DFL);
916 (void) kill (getpid (), SIGQUIT);
917 (void) write (2, "how did we get here?\n", 21);
918 exit (1); /* NOTREACHED */
920 default: /* parent */
921 while ((w = wait (&status)) != pid && w != -1)
923 if (status.w_coredump)
925 (void) write (2, "attempt to dump core failed\n", 28);
929 #endif /* FUNNY_CORE_DUMP */