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
24 #define dump_ext_length 4
25 #define edit_value tex_edit_value
27 extern char * replacement[];
28 static char * program_name = NULL;
29 int gargc; /* number of args - set to zero after initialization */
32 /* The entry point: set up for reading the command line, which will
33 happen in `t_open_in', then call the main body. */
39 int main (int ac, char *av[])
41 int flag = 0, ret = 0;
46 if (main_init(gargc, gargv))
49 TEX_format_default = " plain.fmt";
50 format_default_length = strlen(TEX_format_default + 1);
54 ret = setjmp(jumpbuffer);
58 flag = main_program();
61 printf("EXITING at %s: flag = %d, ret = %d, jump_used = %d\n", "main", flag, ret, jump_used);
66 printf("EXITING at %s: flag = %d, ret = %d, jump_used = %d\n", "jump_out", flag, ret, jump_used);
70 flag = 1; /* do final clean up in local.c */
78 /* texk/web2c/lib/texmfmp.c */
83 buffer[first] = 0; /* In case there are no arguments. */
86 if (gargc > optind && optind > 0)
88 for (i = optind; i < gargc; i++)
92 for (i = 1; i < gargc; i++)
95 if (allow_quoted_names && strchr(gargv[i], ' ') != NULL)
97 (void) strcat ((char *) &buffer[first], "\"");
98 (void) strcat ((char *) &buffer[first], gargv[i]);
99 (void) strcat ((char *) &buffer[first], "\"");
102 (void) strcat ((char *) &buffer[first], gargv[i]);
104 (void) strcat ((char *) &buffer[first], " ");
110 /* Find the end of the buffer. */
111 for (last = first; buffer[last]; ++last)
114 for (--last; last >= first && ISBLANK (buffer[last]) && buffer[last] != '\r'; --last)
119 /* One more time, this time converting to TeX's internal character
120 representation. */ /* for command line input in this case */
123 for (i = first; i < last; i++)
124 buffer[i] = xord[buffer[i]];
128 static void catch_interrupt(int err)
130 (void) signal(SIGINT, SIG_IGN);
132 if (interrupt++ >= 3)
135 (void) signal(SIGINT, catch_interrupt);
138 void fix_date_and_time (void)
146 printf("The time is %u\n", clock);
149 puts("Time not available!\n");
151 tmptr = localtime (&clock);
155 printf("Cannot convert time (%0ld)!\n", clock);
159 tex_time = 22 * 60 + 14;
163 tex_time = tmptr->tm_hour * 60 + tmptr->tm_min;
164 day = tmptr->tm_mday;
165 month = tmptr->tm_mon + 1;
166 year = tmptr->tm_year + 1900;
169 printf("%d-%d-%d %d:%d\n",
170 tmptr->tm_year + 1900,
181 if (signal(SIGINT, catch_interrupt) == SIG_ERR)
183 puts(" CTRL-C handler not installed\n");
185 uexit(EXIT_FAILURE); /* do we care when run as DLL ? */
190 void (*old_handler)();
192 if ((old_handler = signal (SIGINT, catch_interrupt)) != SIG_DFL)
193 (void) signal (SIGINT, old_handler);
198 /* I/O for TeX and Metafont. */
199 void complain_line (FILE *output)
203 #ifdef ALLOCATEBUFFER
204 sprintf(log_line, "! Unable to read an entire line---buf_size=%d.\n", current_buf_size);
206 sprintf(log_line, "! Unable to read an entire line---buf_size=%d.\n", buf_size);
209 fputs(log_line, output);
210 puts(" (File may have a line termination problem.)");
213 void show_bad_line (FILE *output, int first, int last)
218 for (i = first; i <= last; i++)
222 if ((show_in_hex && ch > 127))
234 /* putc('^', output); putc('^', output); */
237 /* putc (c, output); putc (d, output); */
244 /* putc('^', output); putc('^', output); */
247 /* putc (ch + 64, output); */
248 *s++ = (char) (ch + 64);
253 /* putc('^', output); putc('^', output); */
256 /* putc (ch - 64, output); */
257 *s++ = (char) (ch - 64);
261 /* putc(ch, output); */
265 // putc(' ', output); /* putc('\n', output); */
269 fputs(log_line, output); // log_file
272 // split off for convenience and use in ConsoleInput
273 boolean input_line_finish (void)
278 /* if last line in file does not end with \n - never happens ? */
279 /* if (i == EOF && buffer[last] != '\n') buffer[last++] = '\n'; */
281 buffer[last] = ' '; /* space terminate */
283 if (last >= max_buf_stack)
284 max_buf_stack = last; /* remember longest line */
286 /* Trim trailing whitespace. */
287 /* #define isblank(c) ((c) == ' ' || (c) == '\t') */
288 /* What about \n ? Can't get in here ?- bkph */
289 /* What about control-Z that gets read in binary mode ? - bkph */
291 /* while (last > first && buffer[last - 1] <= ' ') --last; */
294 i = buffer[last - 1];
296 if (i == ' ' || i == '\t')
302 /* following added to check source file integrity ASCII 32 - 127 */
303 /* allow space, tab, new-page - also allow return, newline ? */
304 if (restrict_to_ascii)
307 for (i = first; i <= last; i++)
310 /* if (ch > 127 || (ch < ' ' && ch != '\t' && ch != '\f')) */
311 /* 1 -- 8, 11, 14 -- 31 are not good ASCII characters */
312 if (ch > 126 || (ch < ' ' && ch != '\t' && ch != '\f' && ch != '\r' && ch != '\n'))
314 sprintf(log_line, "\n! non ASCII char (%d) in line: ", ch);
315 show_line(log_line, 1);
317 fprintf(log_file, "\n! non ASCII char (%d) in line: ", ch);
318 /* buffer[i]= 127; */ /* not defined - invalid char */
325 show_bad_line(errout, first, last);
327 show_bad_line(log_file, first, last);
330 /* Don't bother using xord if we don't need to. */ /* for input line */
331 /* #ifdef NONASCII */ /* has been turned into command line flag - bkph */
334 for (i = first; i <= last; i++)
335 buffer[i] = xord[buffer[i]];
341 /* Read a line of input into buffer as efficiently as possible (ha ha)
342 while still looking like Pascal.
343 We set `last' to `first' and return `false' if we get to eof.
344 Otherwise, we return `true' and set last = first +
345 length(line except trailing whitespace). */
348 boolean input_line (FILE *f)
350 // int ch, flag; /* for restrict_to_ascii case 94/Jan/21 */
351 char *u; /* 1994/July/3 for key_replace */
354 /* and here is the long way of doing this */
356 /* following is new version with tab expansion and key replacement */
357 /* may want to expand out separately for speed 1994/July/3 */
358 /* different versions depending on return_flag / tabexpand / key_replace */
359 /* while (last < buf_size && (i = getc (f)) != EOF) */
360 #ifdef ALLOCATEBUFFER
363 while (last < buf_size)
370 if (i == EOF || i == '\n' || (i == '\r' && return_flag))
372 else if (i == '\t' && tab_step != 0)
374 buffer[last++] = (ASCII_code) ' ';
376 #ifdef ALLOCATEBUFFER
377 if (last >= current_buf_size)
379 buffer = realloc_buffer(increment_buf_size);
381 if (last >= current_buf_size)
386 #ifdef ALLOCATEBUFFER
387 while ((last - first) % tab_step != 0)
389 while (last < buf_size && (last - first) % tab_step != 0)
393 buffer[last++] = (ASCII_code) ' ';
395 #ifdef ALLOCATEBUFFER
396 if (last >= current_buf_size)
398 buffer = realloc_buffer(increment_buf_size);
400 if (last >= current_buf_size)
410 if (key_replace && (u = replacement[i]) != NULL)
412 #ifdef ALLOCATEBUFFER
415 while (last < buf_size && *u != '\0')
418 buffer[last++] = (ASCII_code) *u++;
419 #ifdef ALLOCATEBUFFER
420 if (last >= current_buf_size)
422 buffer = realloc_buffer(increment_buf_size);
424 if (last >= current_buf_size)
430 else /* normal case */
432 buffer[last++] = (ASCII_code) i;
434 #ifdef ALLOCATEBUFFER
435 if (last >= current_buf_size)
437 buffer = realloc_buffer(increment_buf_size);
439 if (last >= current_buf_size)
446 if (return_flag) /* let return terminate line as well as newline */
448 if (i == '\r') /* see whether return followed by newline */
450 i = getc (f); /* in which case throw away the newline */
457 /* else buffer[last-1] = (ASCII_code) i; */
461 // Turn Ctrl-Z at end of file into newline 2000 June 22
462 if (i == EOF && trimeof && buffer[last - 1] == 26)
467 if (i == EOF && last == first)
470 /* Didn't get the whole line because buffer was too small? */
471 /* This shouldn't happen anymore 99/Jan/23 */
472 if (i != EOF && i != '\n' && i != '\r')
474 complain_line(errout);
477 complain_line(log_file);
479 /* This may no longer be needed ... now that we grow it */
480 if (truncate_long_lines)
482 while (i != EOF && i != '\n' && i != '\r')
484 i = getc (f); // discard rest of line
487 last--; /* just in case */
490 uexit(EXIT_FAILURE); /* line too long */
493 return input_line_finish();
496 /* This string specifies what the `e' option does in response to an
499 static char *edit_value = "c:\\yandy\\WinEdt\\WinEdt.exe [Open('%s');SelLine(%d,7)]";
501 /* This procedure is due to sjc@s1-c. TeX (or Metafont) calls it when
502 the user types `e' in response to an error, invoking a text editor on
503 the erroneous source file. FNSTART is how far into STRINGPOOL the
504 actual filename starts; FNLENGTH is how long the filename is.
506 See ../site.h for how to set the default, and how to override it. */
508 /* called from close_files_and_terminate in tex9.c */
510 void call_edit (ASCII_code *stringpool, pool_pointer fnstart, integer fnlength, integer linenumber)
512 char *command, *s, *t, *u;
514 int sdone, ddone, ldone;
516 unsigned int commandlen;
517 ASCII_code *texfilename;
518 ASCII_code *log_file_name;
519 pool_pointer lgstart;
524 lgstart = str_start[texmf_log_name];
525 lglength = length(texmf_log_name);
526 log_file_name = stringpool + lgstart;
531 log_file_name = (unsigned char *) "";
534 sdone = ddone = ldone = 0;
535 texfilename = stringpool + fnstart;
537 for (i = 1; i <= in_open; i++)
538 (void) fclose (input_file[i]);
542 if (n > 0 && verbose_flag)
544 sprintf(log_line, "Closed %d streams\n", n);
545 show_line(log_line, 0);
548 s = kpse_var_value(edit_value);
553 commandlen = strlen (edit_value) + fnlength + lglength + 10 + 1 + 2;
554 command = (string) xmalloc (commandlen);
560 while ((c = *u++) != 0)
570 sprintf(log_line, "! bad command syntax (%c).\n", 'd');
571 show_line(log_line, 1);
573 sprintf(log_line, "! `%%d' cannot appear twice in editor command.\n");
574 show_line(log_line, 1);
579 (void) sprintf (s, "%d", linenumber);
591 sprintf(log_line, "! bad command syntax (%c).\n", 's');
592 show_line(log_line, 1);
594 sprintf(log_line, "! `%%s' cannot appear twice in editor command.\n");
595 show_line(log_line, 1);
600 t = (char *) texfilename;
604 for (i = 0; i < n; i++)
607 for (i = 0; i < n; i++)
617 sprintf(log_line, "! bad command syntax (%c).\n", 'l');
618 show_line(log_line, 1);
620 sprintf(log_line, "! `%%l' cannot appear twice in editor command.\n");
621 show_line(log_line, 1);
626 t = (char *) log_file_name;
630 for (i = 0; i < n; i++)
633 for (i = 0; i < n; i++)
654 *s = 0; /* terminate the command string */
656 if (strlen(command) + 1 >= commandlen)
658 sprintf(log_line, "Command too long (%d > %d)\n", strlen(command) + 1, commandlen);
659 show_line(log_line, 1);
666 if (system (command) != 0)
669 sprintf(log_line, "! Error in call: %s\n", command);
670 show_line(log_line, 1);
674 perrormod("! DOS says");
677 sprintf(log_line, " (TEXEDIT=%s)\n", edit_value);
678 show_line(log_line, 0);
679 puts(" (Editor specified may be missing or path may be wrong)\n");
680 puts(" (or there may be missing -- or extraneous -- quotation signs)\n");
686 /* Read and write format (for TeX) or base (for Metafont) files. In
687 tex.web, these files are architecture dependent; specifically,
688 BigEndian and LittleEndian architectures produce different files.
689 These routines always output BigEndian files. This still does not
690 make the dump files architecture-independent, because it is possible
691 to make a format file that dumps a glue ratio, i.e., a floating-point
692 number. Fortunately, none of the standard formats do that. */
694 #if !defined (WORDS_BIGENDIAN) && !defined (NO_FMTBASE_SWAP) /* this fn */
696 /* We don't REALLY care what `endian' the machine is after all ! */
697 /* But we do care about speed - so check exe file for following - bkph */
700 // char swapmarkerstring="ERROR: SWAPPING - NOT BigEndian AND NOT NoFmtBaseSwap";
703 /* This macro is always invoked as a statement. It assumes a variable
706 #define SWAP(x, y) temp = (x); (x) = (y); (y) = temp;
709 /* Make the NITEMS items pointed at by P, each of size SIZE, be the
710 opposite-endianness of whatever they are now. */
712 static int swap_items (char *p, int nitems, int size)
716 /* Since `size' does not change, we can write a while loop for each
717 case, and avoid testing `size' for each time. */
754 sprintf(log_line, "! I can't (un)dump a %d byte item.\n", size);
755 show_line(log_line, 1);
760 #endif /* not WORDS_BIGENDIAN and not NO_FMTBASE_SWAP */
763 int do_dump(char *p, int item_size, int nitems, gzFile out_file)
765 int do_dump(char *p, int item_size, int nitems, FILE *out_file)
768 #if !defined (WORDS_BIGENDIAN) && !defined (NO_FMTBASE_SWAP)
769 swap_items (p, nitems, item_size);
773 if (gzwrite(out_file, p, (item_size * nitems)) != (item_size * nitems))
775 if ((int) fwrite(p, item_size, nitems, out_file) != nitems)
779 sprintf(log_line, "! Could not write %d %d-byte item%s.\n",
780 nitems, item_size, (nitems > 1) ? "s" : "");
781 show_line(log_line, 1);
785 /* Have to restore the old contents of memory, since some of it might get used again. */
786 #if !defined (WORDS_BIGENDIAN) && !defined (NO_FMTBASE_SWAP)
787 swap_items (p, nitems, item_size);
794 int do_undump(char *p, int item_size, int nitems, gzFile in_file)
796 int do_undump(char *p, int item_size, int nitems, FILE *in_file)
800 if (gzread(in_file, (void *) p, (unsigned int) (item_size * nitems)) <= 0)
802 if ((int) fread((void *) p, item_size, nitems, in_file) != nitems)
806 sprintf(log_line, "! Could not read %d %d-byte item%s.\n",
807 nitems, item_size, (nitems > 1) ? "s" : "");
808 show_line(log_line, 1);
812 #if !defined (WORDS_BIGENDIAN) && !defined (NO_FMTBASE_SWAP)
813 swap_items (p, nitems, item_size);
819 #ifdef FUNNY_CORE_DUMP
820 /* This procedure is due to chris@mimsy.umd.edu. It makes a core dump
821 without any sort of error status (abort(2) does return an error status,
822 so we don't want to use that). It is used only when making a preloaded
823 TeX from virtex, and is triggered by a magic file name requested as
824 input (see `open_input', above). */
826 void funny_core_dump (void)
831 switch (pid = vfork ())
838 (void) signal (SIGQUIT, SIG_DFL);
839 (void) kill (getpid (), SIGQUIT);
840 (void) write (2, "how did we get here?\n", 21);
844 while ((w = wait (&status)) != pid && w != -1)
847 if (status.w_coredump)
850 (void) write (2, "attempt to dump core failed\n", 28);
854 #endif /* FUNNY_CORE_DUMP */