1 /* Copyright 2007 TeX Users Group
2 Copyright 2014 Clerk Ma
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #define REALLOC ourrealloc
29 #define REALLOC realloc
34 char *compiletime = __TIME__;
35 char *compiledate = __DATE__;
36 char *www = "http://www.tug.org/yandy";
37 char *copyright = "\nCopyright (C) 1993--2000 Y&Y, Inc.\n"
38 "Copyright (C) 2007 TeX Users Group.\n"
39 "Copyright (C) 2014 Clerk Ma.\n\n"
40 "This program is free software; you can redistribute it and/or modify\n"
41 "it under the terms of the GNU General Public License as published by\n"
42 "the Free Software Foundation; either version 2 of the License, or\n"
43 "(at your option) any later version.\n\n ";
44 char *yandyversion = "2.3.0";
45 char *application = "Y&Y TeX";
46 char *tex_version = "This is TeX, Version 3.14159265";
48 clock_t start_time, main_time, finish_time;
50 char * dvi_directory = "";
51 char * log_directory = "";
52 char * aux_directory = "";
53 char * fmt_directory = "";
54 char * pdf_directory = "";
58 char log_line[MAXLINE]; // used also in tex9.c
60 int mem_spec_flag = 0; /* non-zero if `-m=...' was used */
61 int format_spec = 0; /* non-zero if a format specified on command line */
62 int closed_already = 0; /* make sure we don't try this more than once */
63 boolean reorder_arg_flag = true; /* put command line flags/arguments first */
65 /* Mapping from Windows ANSI to DOS code page 850 96/Jan/20 */
66 /* Used in tex0.c with wintodos[c-128] */
68 unsigned char wintodos[128] =
70 0, 0, 0, 159, 0, 0, 0, 0,
71 94, 0, 0, 0, 0, 0, 0, 0,
72 0, 96, 39, 0, 0, 7, 0, 0,
73 126, 0, 0, 0, 0, 0, 0, 0,
74 32, 173, 189, 156, 207, 190, 221, 21,
75 0, 184, 166, 174, 170, 45, 169, 0,
76 248, 241, 253, 252, 0, 230, 20, 250,
77 0, 251, 167, 175, 172, 171, 243, 168,
78 183, 181, 182, 199, 142, 143, 146, 128,
79 212, 144, 210, 211, 222, 214, 215, 216,
80 209, 165, 227, 224, 226, 229, 153, 158,
81 157, 235, 233, 234, 154, 237, 232, 225,
82 133, 160, 131, 198, 132, 134, 145, 135,
83 138, 130, 136, 137, 141, 161, 140, 139,
84 208, 164, 149, 162, 147, 228, 148, 246,
85 155, 151, 163, 150, 129, 236, 231, 152
88 void show_usage (void)
91 " Useage: yanytex [OPTION]... [+format_file] [tex_file]\n\n"
93 " show this usage summary\n"
95 " start up as iniTeX (create format file)\n"
97 " be verbose (show implementation version number)\n"
98 " -n do not allow `non ASCII' characters in input files (complain instead)\n"
100 " do not show `non ASCII' characters in hexadecimal (show as is)\n"
101 " -d do not allow DOS style file names - i.e. do not convert \\ to /\n"
102 " -r do not allow Mac style termination - i.e. do not convert \\r to \\n\n"
104 " allow use of \\patterns after loading format (iniTeX only)\n"
106 " disable all extensions to basic TeX\n"
107 " --main-memory -m\n"
108 " initial main memory size in kilo words (iniTeX only)\n"
110 " hyphenation exception dictionary size (iniTeX only)\n"
112 " hyphenation pattern trie size (iniTeX only)\n"
114 " use `non ASCII' character mapping (xchr[]) defined in file\n"
116 " use `key replacement' defined in file\n"
118 " write DVI file in specified directory (default current directory)\n"
120 " write LOG file in specified directory (default current directory)\n"
122 " write AUX file in specified directory (default current directory)\n");
125 uexit(EXIT_FAILURE); // has this been setup yet ???
129 /* -z do not discard control-Z at end of input file (treat as character)\n\ */
131 /* \t-d\tallow DOS style file names - i.e. convert \\ to / \n\ */
132 /* \t\t(applies to file name and format file name, if present)\n\ */
133 /* \t-r\tallow Mac style line termination - i.e. convert \\r to \\n \n\ */
135 // Sep 27 1990 => 1990 Sep 27
136 // 012456789 0123456789
137 void scivilize (char * date)
142 strcpy (pyear, date + 7);
144 for (k = 5; k >= 0; k--)
145 date[k + 5] = date[k];
147 for (k = 0; k < 4; k++)
153 date[9] = '0'; /* replace space by '0' */
156 // Thu Sep 27 06:26:35 1990 => 1990 Sep 27 06:26:35
157 void lcivilize (char * date)
162 strcpy (pyear, date + 20);
164 for (k = 18; k >= 0; k--)
169 for (k = 0; k < 4; k++)
175 // now writes result into given buffer
176 void stamp_it (char *s)
180 strcpy(date, compiledate);
182 sprintf(s, "%s %s ", application, yandyversion);
184 sprintf(s, "(compiled time: %s %s)", date, compiletime);
188 void stampcopy (char *s)
192 sprintf(s, "%s %s", copyright, www);
199 void read_xchr_sub (FILE * xchr_input)
201 char buffer[file_name_size];
202 int k, from, to, count = 0;
205 memset(xchr, NOTDEF, MAXCHRS);
206 memset(xord, NOTDEF, MAXCHRS);
208 #ifdef ALLOCATEBUFFER
209 while (fgets(buffer, current_buf_size, xchr_input) != NULL)
211 while (fgets(buffer, sizeof(buffer), xchr_input) != NULL)
214 if (*buffer == '%' || *buffer == ';' || *buffer == '\n')
217 from = (int) strtol (buffer, &s, 0);
218 to = (int) strtol (s, NULL, 0);
220 if (from >= 0 && from < MAXCHRS && to >= 0 && to < MAXCHRS)
222 if (xchr[from] == (unsigned char) NOTDEF)
224 xchr[from] = (unsigned char) to;
228 sprintf(log_line, "NOTE: %s collision: %d => %d, %d\n", "xchr", from, xchr[from], to);
229 show_line(log_line, 0);
232 if (xord[to] == NOTDEF)
234 xord[to] = (unsigned char) from;
238 sprintf(log_line, "NOTE: %s collision: %d => %d, %d\n", "xord", to, xord[to], from);
239 show_line(log_line, 0);
246 for (k = 0; k < MAXCHRS; k++)
248 if (xchr[k] == NOTDEF) /* if it has not been filled */
250 if (xord[k] == NOTDEF) /* see whether used already */
252 xchr[k] = (unsigned char) k; /* no, so make identity */
253 xord[k] = (unsigned char) k; /* no, so make identity */
258 xchr[NOTDEF] = NOTDEF; /* fixed point of mapping */
262 sprintf(log_line, "Read %d xchr[] pairs:\n", count);
263 show_line(log_line, 0);
265 for (k = 0; k < MAXCHRS; k++)
267 if (xchr[k] != NOTDEF)
269 sprintf(log_line, "%d => %d\n", k, xchr[k]);
270 show_line(log_line, 0);
276 char *replacement[MAXCHRS]; /* pointers to replacement strings */
278 void read_repl_sub (FILE * repl_input)
281 char buffer[file_name_size];
286 memset(replacement, 0, MAXCHRS * sizeof(replacement[ 0]));
289 while (fgets(buffer, file_name_size, repl_input) != NULL)
291 if (*buffer == '%' || *buffer == ';' || *buffer == '\n')
294 if ((m = sscanf (buffer, "%d%n %s", &chrs, &n, &charname)) == 0)
298 if (*charname == '"') /* deal with quoted string "..." */
303 while (*s != '"' && *s != '\0')
304 s++; /* step up to " */
307 continue; /* sanity check */
313 s++; /* is it "" perhaps ? */
316 break; /* no, end of string */
319 *t++ = *s++; /* copy over */
322 *t = '\0'; /* and terminate */
325 if (chrs >= 0 && chrs < MAXCHRS)
326 replacement[chrs] = xstrdup(charname);
328 /* presently the following can never get triggered */
329 /* which is good, because it is perhaps not right ... */
330 else if ((m = sscanf (buffer, "%d %d %d %d %d %d %d %d %d %d %d",
331 &chrs, charnum, charnum+1, charnum+2, charnum+3, charnum+4,
332 charnum+5, charnum+6, charnum+7, charnum+8, charnum+9)) > 1) {
333 /* for (k = 0; k < n-1; k++) charname[k] = (char) charnum; */
334 for (k = 0; k < n-1; k++) charname[k] = (char) charnum[k];
336 if (chrs >= 0 && chrs < MAXCHRS)
337 replacement[chrs] = xstrdup(charname);
340 sprintf(log_line, "ERROR: don't understand %s", buffer);
341 show_line(log_line, 1);
347 puts("Key replacement table\n");
349 for (k = 0; k < MAXCHRS; k++)
351 if (replacement[k] != NULL)
353 sprintf(log_line, "%d\t%s\n", k, replacement[k]);
354 show_line(log_line, 0);
360 /* Following used both to read xchr[] file and key replacement file */
361 /* the flag is 0 for -x=... and the flag is 1 for -k=... */
362 int read_xchr_file (char *filename, int flag, char *argv[])
365 char infile[file_name_size];
368 if (filename == NULL)
373 sprintf(log_line, "Reading xchr/repl %s\n", filename);
374 show_line(log_line, 0);
377 /* first try using file as specified */
378 strcpy(infile, filename);
382 sprintf(log_line, "Trying %s\n", infile);
383 show_line(log_line, 0);
386 pinput = fopen (infile, "r");
390 if (strrchr(infile, '.') == NULL)
393 strcat(infile, ".map");
395 strcat(infile, ".key");
399 sprintf(log_line, "Trying %s\n", infile);
400 show_line(log_line, 0);
403 pinput = fopen (infile, "r");
409 strcpy(infile, argv[0]); /* try TeX program path */
411 if ((s = strrchr (infile, '\\')) != NULL) *(s+1) = '\0';
412 else if ((s = strrchr (infile, '/')) != NULL) *(s+1) = '\0';
413 else if ((s = strrchr (infile, ':')) != NULL) *(s+1) = '\0';
415 strcat (infile, filename);
419 sprintf(log_line, "Trying %s\n", infile);
420 show_line(log_line, 0);
423 pinput = fopen (infile, "r");
427 if (strchr(infile, '.') == NULL)
430 strcat(infile, ".map");
432 strcat(infile, ".key");
436 sprintf(log_line, "Trying %s\n", infile);
437 show_line(log_line, 0);
440 pinput = fopen (infile, "r");
445 if (pinput == NULL) /* 97/July/31 */
447 strcpy (infile, argv[0]); /* try TeX program path */
449 if ((s = strrchr (infile, '\\')) != NULL) *(s+1) = '\0';
450 else if ((s = strrchr (infile, '/')) != NULL) *(s+1) = '\0';
451 else if ((s = strrchr (infile, ':')) != NULL) *(s+1) = '\0';
453 strcat (infile, "keyboard\\");
454 strcat (infile, filename);
458 sprintf(log_line, "Trying %s\n", infile);
459 show_line(log_line, 0);
462 pinput = fopen (infile, "r");
466 if (strchr(infile, '.') == NULL)
469 strcat(infile, ".map");
471 strcat(infile, ".key");
475 sprintf(log_line, "Trying %s\n", infile);
476 show_line(log_line, 0);
479 pinput = fopen (infile, "r");
483 /* Note: can't look in TeX source file dir, since that is not known yet */
486 sprintf(log_line, "ERROR: Sorry, cannot find %s file %s",
487 flag ? " xchr[]" : "key mapping", filename);
488 show_line(log_line, 1);
489 perrormod (filename);
494 read_xchr_sub (pinput);
496 read_repl_sub (pinput);
498 (void) fclose (pinput);
502 /* need to also set `key_replace' here based on command line */
503 /* need to also allocate `buffercopy' here and free at end */
504 /* need to call `readreplace' in appropriate place */
506 /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
510 /* ad hoc default minimum growth in memory realloc is 62% */
511 /* golden ratio (1 + \sqrt{5}) / 2 = 1.618033989... */
512 int percent_grow = 62; /* default minimum growth in memory realloc is 62% */
513 int total_allocated = 0; /* total memory allocated so far */
514 int ini_max_address = 0; /* maximum address when starting */
515 int max_address = 0; /* maximum address seen in allocated memory */
518 void show_maximums (FILE * output)
520 sprintf(log_line, "Max allocated %d --- max address %d\n", total_allocated, max_address);
521 fputs(log_line, output);
524 /* our own version of realloc --- avoid supposed MicroSoft version bug */
525 /* also tries _expand first, which can avoid address growth ... */
528 void *ourrealloc (void *old, size_t new_size)
531 size_t old_size, overlap;
533 /* round up to nearest multiple of four bytes */
534 /* avoid unlikely alignment */
535 if ((new_size % 4) != 0)
536 new_size = ((new_size / 4) + 1) * 4;
539 return malloc (new_size); /* no old block - use malloc */
542 old_size = _msize (old);
544 old_size = malloc_usable_size (old);
547 if (old_size >= new_size && old_size < new_size + 4)
551 mnew = _expand (old, new_size); /* first try and expand in place MSVC */
553 mnew = realloc (old, new_size);
560 sprintf(log_line, "EXPANDED! %d (%d) == %d (%d)\n",
561 mnew, new_size, old, old_size);
562 show_line(log_line, 0);
568 /* do this if you want to call the real realloc next - */
569 mnew = realloc (old, new_size);
574 /* we are screwed typically if we ever drop through here - no more space */
575 mnew = malloc (new_size); /* otherwise find new space */
578 return mnew; /* if unable to allocate */
580 if (old_size < new_size)
585 memcpy (mnew, old, overlap); /* copy old data to new area */
586 free(old); /* free the old area */
592 void memory_error (char *s, int n)
596 fprintf(log_file, "\n! Unable to allocate %d bytes for %s\n", n, s);
597 show_maximums(log_file);
600 sprintf(log_line, "\n! Unable to allocate %d bytes for %s\n", n, s);
601 show_line(log_line, 1);
602 show_maximums(stderr);
605 void trace_memory (char *s, int n)
607 printf("Allocating %d bytes for %s\n", n, s);
610 void update_statistics (int address, int size, int old_size)
612 if (address + size > max_address)
613 max_address = address + size;
615 total_allocated = total_allocated + size - old_size;
618 void probe_memory (void)
620 char *s = (char *) malloc(4); /* get current top address */
622 update_statistics ((int) s, 0, 0); /* show where we are */
625 void probe_show (void)
628 show_maximums(stdout);
631 size_t roundup (size_t n)
636 return ((n / 4) + 1) * 4;
640 /* returns -1 if it fails */
642 int allocate_tries (int trie_max)
646 if (trie_max > 1000000)
649 nl = (trie_max + 1) * sizeof(halfword);
650 no = (trie_max + 1) * sizeof(halfword);
651 nc = (trie_max + 1) * sizeof(quarterword);
655 trace_memory("hyphen trie", n);
657 trie_trl = (halfword *) malloc(roundup(nl));
658 trie_tro = (halfword *) malloc(roundup(no));
659 trie_trc = (quarterword *) malloc(roundup(nc));
661 if (trie_trl == NULL || trie_tro == NULL || trie_trc == NULL)
663 memory_error("hyphen trie", n);
669 sprintf(log_line, "Addresses trie_trl %d trie_tro %d trie_trc %d\n", trie_trl, trie_tro, trie_trc);
670 show_line(log_line, 0);
673 update_statistics((int) trie_trl, nl, 0);
674 update_statistics((int) trie_tro, no, 0);
675 update_statistics((int) trie_trc, nc, 0);
677 trie_size = trie_max;
686 #ifdef ALLOCATEHYPHEN
687 boolean prime(int); /* test function later in this file */
689 int current_prime = 0; /* remember in case reallocated later */
691 /* we don't return an address here, since TWO memory regions allocated */
692 /* plus, we don't really reallocate, we FLUSH the old information totally */
693 /* returns -1 if it fails */
695 int realloc_hyphen (int hyphen_prime)
699 if (!prime(hyphen_prime))
701 sprintf(log_line, "ERROR: non-prime hyphen exception number (%d)\n", hyphen_prime);
702 show_line(log_line, 1);
706 /* need not/cannot preserve old contents when hyphen prime is changed */
707 /* if (hyph_list != NULL) free(hyph_list); */
708 /* if (hyph_word != NULL) free(hyph_word); */
709 /* important + 1 since str_number hyph_word[hyphen_prime + 1] in original etc. */
710 nw = (hyphen_prime + 1) * sizeof(str_number);
711 nl = (hyphen_prime + 1) * sizeof(halfword);
715 trace_memory("hyphen exception", n);
717 hyph_word = (str_number *) REALLOC (hyph_word, nw);
718 hyph_list = (halfword *) REALLOC (hyph_list, nl);
720 if (hyph_word == NULL || hyph_list == NULL)
722 memory_error("hyphen exception", n);
728 sprintf(log_line, "Addresses hyph_word %d hyph_list %d\n", hyph_word, hyph_list);
729 show_line(log_line, 0);
732 /* cannot preserve old contents when hyphen prime is changed */
734 memset(hyph_word, 0, (hyphen_prime + 1) * sizeof (hyph_word[0]));
736 for (k = 0; k <= hyphen_prime; k++)
741 memset(hyph_list, 0, (hyphen_prime + 1) * sizeof (hyph_list[0]));
743 for (k = 0; k <= hyphen_prime; k++)
749 if (current_prime != 0)
751 update_statistics((int) hyph_word, nw, (current_prime + 1) * sizeof(str_number));
752 update_statistics((int) hyph_list, nl, (current_prime + 1) * sizeof(halfword));
756 update_statistics((int) hyph_word, nw, 0);
757 update_statistics((int) hyph_list, nl, 0);
760 current_prime = hyphen_prime;
769 int current_mem_size = 0; /* current total words in main mem allocated -1 */
771 /* this gets called from itex.c when it figures out what mem_top is */
772 /* or gets called from here when in ini_TeX mode */ /* and nowhere else */
773 /* initial allocation only, may get expanded later */
774 /* NOTE: we DON't use ALLOCATEHIGH & ALLOCATELOW anymore */
775 /* returns NULL if it fails */
778 /* initial main memory alloc - mem_top */
779 memory_word *allocate_main_memory (int size)
783 if (main_memory != NULL)
786 puts("Reallocating initial memory allocation");
789 mem_top = mem_bot + size;
791 mem_start = 0; /* bottom of memory allocated by system */
792 mem_min = 0; /* bottom of area made available to TeX */
793 n = (mem_max - mem_start + 1) * sizeof (memory_word); /* 256k * 8 = 2000 k */
796 trace_memory("main memory", n);
798 main_memory = (memory_word *) REALLOC (main_memory, n);
800 if (main_memory == NULL)
802 memory_error("initial main memory", n);
809 sprintf(log_line, "Address main memory == %d\n", main_memory);
810 show_line(log_line, 0);
815 if (mem_start != 0 && !is_initex)
816 mem = main_memory - mem_start;
820 sprintf(log_line, "Offset address main memory == %d\n", mem);
821 show_line(log_line, 0);
824 update_statistics((int) main_memory, n, (current_mem_size + 1) * sizeof (memory_word));
825 /* current_mem_size = (mem_max - mem_start + 1); */
826 current_mem_size = mem_max - mem_start; /* total number of words - 1 */
836 /* int firstallocation = 1; */
838 /* increase main memory allocation at low end and high end */
839 /* called only from tex0.c *//* called with one of lo_size or hi_size == 0 */
840 /* returns NULL if it fails */
842 memory_word * realloc_main (int lo_size, int hi_size)
847 memory_word * new_memory = NULL;
851 sprintf(log_line, "WARNING: Entering realloc_main lo %d hi %d\n", lo_size, hi_size);
852 show_line(log_line, 0);
857 puts("ERROR: Cannot extent main memory in iniTeX\n");
860 puts("Please use `-m=...' on command line");
867 sprintf(log_line, "Old Address %s == %d\n", "main memory", main_memory);
868 show_line(log_line, 0);
871 /* if we REALLY run up to limit ! */
872 if (current_mem_size + 1 == max_mem_size)
874 memory_error("main memory", (max_mem_size + 1) * sizeof(memory_word));
879 /* first allocation should expand *both* lo and hi */
880 if (hi_size == 0 && mem_end == mem_max)
883 if (lo_size == 0 && mem_start == mem_min)
886 /* try and prevent excessive frequent reallocations */
887 /* while avoiding over allocation by too much */
888 min_size = current_mem_size / 100 * percent_grow;
890 if (lo_size + hi_size < min_size)
892 if (lo_size > 0 && hi_size > 0)
894 lo_size = min_size / 2;
895 hi_size = min_size / 2;
897 else if (lo_size > 0)
899 else if (hi_size > 0)
903 if (lo_size > 0 && lo_size < mem_top / 2)
904 lo_size = mem_top / 2;
906 if (hi_size > 0 && hi_size < mem_top / 2)
907 hi_size = mem_top / 2;
909 for (k = 0; k < MAXSPLITS; k++)
911 new_size = current_mem_size + lo_size + hi_size;
913 if (new_size >= max_mem_size) /* bump against limit - ha ha ha */
915 while (new_size >= max_mem_size)
917 lo_size = lo_size / 2;
918 hi_size = hi_size / 2;
919 new_size = current_mem_size + lo_size + hi_size;
923 n = (new_size + 1) * sizeof (memory_word);
926 trace_memory("main memory", n);
928 new_memory = (memory_word *) REALLOC (main_memory, n);
930 if (new_memory != NULL)
931 break; /* did we get it ? */
933 if (current_mem_size == 0)
934 break; /* in case we ever use for initial */
936 lo_size = lo_size / 2; hi_size = hi_size / 2;
939 if (new_memory == NULL)
941 memory_error("main memory", n);
947 sprintf(log_line, "New Address %s == %d\n", "main memory", new_memory);
948 show_line(log_line, 0);
953 /* shift everything upward to make space for new low area */
956 sprintf(log_line, "memmove %d %d %d \n", new_memory + lo_size,
957 new_memory, (current_mem_size + 1) * sizeof(memory_word));
958 show_line(log_line, 0);
961 memmove (new_memory + lo_size, new_memory,
962 (current_mem_size + 1) * sizeof(memory_word));
963 /* could reduce words moved by (mem_max - mem_end) */
966 main_memory = new_memory; /* remember for free later */
969 mem_start = mem_start - lo_size; /* update lower limit */
972 mem_max = mem_max + hi_size; /* update upper limit */
974 update_statistics ((int) main_memory, n,
975 (current_mem_size + 1) * sizeof (memory_word));
976 current_mem_size = new_size;
978 if (current_mem_size != mem_max - mem_start)
980 puts("ERROR: Impossible Memory Error");
984 mem = main_memory - mem_start;
996 int current_font_mem_size = 0;
998 /* fmemoryword can be either halfword or memory_word */
999 memory_word * realloc_font_info (int size)
1001 memory_word * new_font_info = NULL;
1008 sprintf(log_line, "Old Address %s == %d\n", "font_info", font_info);
1009 show_line(log_line, 0);
1011 /* during initial allocation, font_info == NULL - realloc acts like malloc */
1012 /* during initial allocation current_font_mem_size == 0 */
1013 if (current_font_mem_size == font_mem_size) /* if we REALLY run up to limit */
1015 /* memory_error("font", (font_mem_size + 1) * sizeof(memory_word)); */
1016 return font_info; /* pass it back to TeX 99/Fabe/4 */
1018 /* try and prevent excessive frequent reallocations */
1019 /* while avoiding over allocation by too much */
1020 /* min_size = current_font_mem_size / 2; */
1021 min_size = current_font_mem_size / 100 * percent_grow;
1023 if (size < min_size)
1026 if (size < initial_font_mem_size)
1027 size = initial_font_mem_size;
1029 for (k=0; k < MAXSPLITS; k++)
1031 new_size = current_font_mem_size + size;
1033 if (new_size > font_mem_size)
1034 new_size = font_mem_size; /* bump against limit */
1036 /* important + 1 since fmemoryword font_info[font_mem_size + 1] original */
1037 n = (new_size + 1) * sizeof (memory_word);
1040 trace_memory("font_info", n);
1042 new_font_info = (memory_word *) REALLOC (font_info, n);
1044 if (new_font_info != NULL)
1045 break; /* did we get it ? */
1047 if (current_font_mem_size == 0)
1048 break; /* initial allocation must work */
1053 if (new_font_info == NULL)
1055 memory_error("font", n);
1056 return font_info; /* try and continue !!! */
1059 font_info = new_font_info;
1063 sprintf(log_line, "New Address %s == %d\n", "font_info", font_info);
1064 show_line(log_line, 0);
1067 update_statistics ((int) font_info, n, current_font_mem_size * sizeof(memory_word));
1068 current_font_mem_size = new_size;
1077 #ifdef ALLOCATESTRING
1078 int current_pool_size = 0;
1080 packed_ASCII_code * realloc_str_pool (int size)
1085 packed_ASCII_code *newstrpool = NULL;
1089 sprintf(log_line, "Old Address %s == %d\n", "string pool", str_pool);
1090 show_line(log_line, 0);
1093 if (current_pool_size == pool_size)
1095 /* memory_error ("string pool", (pool_size + 1) * sizeof(packed_ASCII_code)); */
1097 return str_pool; /* pass it back to TeX 99/Fabe/4 */
1100 min_size = current_pool_size / 100 * percent_grow;
1102 if (size < min_size)
1105 if (size < initial_pool_size)
1106 size = initial_pool_size;
1108 for (k = 0; k < MAXSPLITS; k++)
1110 new_size = current_pool_size + size;
1112 if (new_size > pool_size)
1113 new_size = pool_size;
1114 /* important + 1 since packed_ASCII_code str_pool[pool_size + 1]; in original */
1115 n = (new_size + 1) * sizeof (packed_ASCII_code);
1118 trace_memory("str_pool", n);
1120 newstrpool = (packed_ASCII_code *) REALLOC (str_pool, n); /* 95/Sep/24 */
1122 if (newstrpool != NULL)
1123 break; /* did we get it ? */
1125 if (current_pool_size == 0)
1126 break; /* initial allocation must work */
1128 size = size / 2; /* else can retry smaller */
1131 if (newstrpool == NULL)
1133 memory_error("string pool", n);
1134 return str_pool; /* try and continue !!! */
1137 str_pool = newstrpool;
1138 update_statistics ((int) str_pool, n, current_pool_size);
1139 current_pool_size = new_size;
1143 sprintf(log_line, "New Address %s == %d\n", "string pool", str_pool);
1144 show_line(log_line, 0);
1154 #ifdef ALLOCATESTRING
1155 int current_max_strings = 0;
1157 pool_pointer *realloc_str_start (int size)
1162 pool_pointer * new_str_start = NULL;
1166 sprintf(log_line, "Old Address %s == %d\n", "string start", str_start);
1167 show_line(log_line, 0);
1170 if (current_max_strings == max_strings)
1172 /* memory_error ("string pointer", (max_strings + 1) * sizeof(pool_pointer)); */
1174 return str_start; /* pass it back to TeX 99/Fabe/4 */
1177 min_size = current_max_strings / 100 * percent_grow;
1179 if (size < min_size)
1182 if (size < initial_max_strings)
1183 size = initial_max_strings;
1185 for (k = 0; k < MAXSPLITS; k++)
1187 new_size = current_max_strings + size;
1189 if (new_size > max_strings)
1190 new_size = max_strings;
1191 /* important + 1 since str_start[maxstring + 1] originally */
1192 n = (new_size + 1) * sizeof (pool_pointer);
1195 trace_memory("str_start", n);
1197 new_str_start = (pool_pointer *) REALLOC (str_start, n);
1199 if (new_str_start != NULL)
1200 break; /* did we get it ? */
1202 if (current_max_strings == 0)
1203 break; /* initial allocation must work */
1205 size = size / 2; /* otherwise can try smaller */
1208 if (new_str_start == NULL)
1210 memory_error("string pointer", n);
1211 return str_start; /* try and continue */
1214 str_start = new_str_start;
1215 update_statistics((int) str_start, n, current_max_strings * sizeof (pool_pointer));
1216 current_max_strings = new_size;
1220 sprintf(log_line, "New Address %s == %d\n", "string start", str_start);
1221 show_line(log_line, 0);
1232 /* returns -1 if it fails */
1233 /* size == trie_size */
1234 int allocate_ini (int size)
1236 int n, nl, no, nc, nr, nh, nt;
1238 nh = nr = nl = (size + 1) * sizeof(trie_pointer);
1239 no = (size + 1) * sizeof(trie_op_code);
1240 nc = (size + 1) * sizeof(packed_ASCII_code);
1241 /* nt = (size + 1) * sizeof(boolean); */
1242 nt = (size + 1) * sizeof(char);
1243 n = nl + no + nc + nr + nh + nt;
1244 /* n = (size + 1) * (sizeof(packed_ASCII_code) + sizeof(trie_op_code) +
1245 3 * sizeof(trie_pointer) + sizeof (char)); */
1247 trace_memory ("iniTeX hyphen trie", n);
1249 trie_l = (trie_pointer *) malloc (roundup(nl));
1250 trie_o = (trie_op_code *) malloc (roundup(no));
1251 trie_c = (packed_ASCII_code *) malloc (roundup(nc));
1252 trie_r = (trie_pointer *) malloc (roundup(nr));
1253 trie_hash = (trie_pointer *) malloc (roundup(nh));
1254 trie_taken = (char *) malloc (roundup(nt));
1256 if (trie_c == NULL || trie_o == NULL || trie_l == NULL || trie_r == NULL ||
1257 trie_hash == NULL || trie_taken == NULL)
1259 memory_error("iniTeX hyphen trie", n);
1260 // exit (1); /* serious error */
1266 sprintf(log_line, "Addresses trie_l %d trie_o %d trie_c %d\n", trie_l, trie_o, trie_c);
1267 show_line(log_line, 0);
1268 sprintf(log_line, "Addresses trie_r %d trie_hash %d trie_taken %d\n",
1269 trie_r, trie_hash, trie_taken);
1270 show_line(log_line, 0);
1273 update_statistics ((int) trie_l, nl, 0);
1274 update_statistics ((int) trie_o, no, 0);
1275 update_statistics ((int) trie_c, nc, 0);
1276 update_statistics ((int) trie_r, nr, 0);
1277 update_statistics ((int) trie_hash, nh, 0);
1278 update_statistics ((int) trie_taken, nt, 0);
1279 /* trie_size = size; */ /* ??? */
1283 return 0; // success
1287 #ifdef ALLOCATESAVESTACK
1288 int current_save_size = 0;
1290 memory_word *realloc_save_stack (int size)
1293 int n = 0, new_size = 0;
1294 memory_word * new_save_stack = NULL;
1298 sprintf(log_line, "Old Address %s == %d\n", "save stack", save_stack);
1299 show_line(log_line, 0);
1302 if (current_save_size == save_size) /* arbitrary limit */
1304 /* memory_error ("save stack", (save_size + 1) * sizeof(memory_word)); */
1306 return save_stack; /* let TeX handle the error */
1309 min_size = current_save_size / 100 * percent_grow;
1311 if (size < min_size)
1314 if (size < initial_save_size)
1315 size = initial_save_size;
1317 for (k = 0; k < MAXSPLITS; k++)
1319 new_size = current_save_size + size;
1321 if (new_size > save_size)
1322 new_size = save_size;
1324 n = (new_size + 1) * sizeof (memory_word); /* save_stack[save_size + 1] */
1327 trace_memory("save_stack", n);
1329 new_save_stack = (memory_word *) REALLOC (save_stack, n);
1331 if (new_save_stack != NULL)
1332 break; /* did we get it ? */
1334 if (current_save_size == 0)
1335 break; /* initial allocation must work */
1337 size = size / 2; /* else can retry smaller */
1340 if (new_save_stack == NULL)
1342 memory_error("save stack", n);
1343 return save_stack; /* try and continue !!! */
1346 save_stack = new_save_stack;
1347 update_statistics ((int) save_stack, n, current_save_size);
1348 current_save_size = new_size;
1352 sprintf(log_line, "Current %s %d\n", "save_size", current_save_size);
1353 show_line(log_line, 0);
1354 sprintf(log_line, "New Address %s == %d\n", "save stack", save_stack);
1355 show_line(log_line, 0);
1365 #ifdef ALLOCATEINPUTSTACK
1366 int current_stack_size = 0; /* input stack size */
1368 in_state_record *realloc_input_stack (int size)
1371 int n = 0, new_size = 0;
1372 in_state_record * new_input_stack = NULL;
1376 sprintf(log_line, "Old Address %s == %d\n", "input stack", input_stack);
1377 show_line(log_line, 0);
1380 if (current_stack_size == stack_size) /* arbitrary limit */
1382 /* memory_error ("input stack", (stack_size + 1) * sizeof(in_state_record)); */
1387 min_size = current_stack_size / 100 * percent_grow;
1389 if (size < min_size)
1392 if (size < initial_stack_size)
1393 size = initial_stack_size;
1395 for (k = 0; k < MAXSPLITS; k++)
1397 new_size = current_stack_size + size;
1399 if (new_size > stack_size)
1400 new_size = stack_size;
1402 n = (new_size + 1) * sizeof (in_state_record); /* input_stack[stack_size + 1] */
1405 trace_memory("input_stack", n);
1407 new_input_stack = (in_state_record *) REALLOC (input_stack, n);
1409 if (new_input_stack != NULL)
1410 break; /* did we get it ? */
1412 if (current_stack_size == 0)
1413 break; /* initial allocation must work */
1415 size = size / 2; /* else can retry smaller */
1418 if (new_input_stack == NULL)
1420 memory_error("input stack", n);
1421 return input_stack; /* try and continue !!! */
1424 input_stack = new_input_stack;
1425 update_statistics ((int) input_stack, n, current_stack_size);
1426 current_stack_size = new_size;
1430 sprintf(log_line, "Current %s %d\n", "stack_size", current_stack_size);
1431 show_line(log_line, 0);
1432 sprintf(log_line, "New Address %s == %d\n", "input stack", input_stack);
1433 show_line(log_line, 0);
1437 probe_show(); /* 94/Mar/25 */
1443 #ifdef ALLOCATENESTSTACK
1444 int current_nest_size = 0; /* current nest size */
1446 list_state_record *realloc_nest_stack (int size)
1449 int n = 0, new_size = 0;
1450 list_state_record * new_nest = NULL;
1454 sprintf(log_line, "Old Address %s == %d\n", "nest stack", nest);
1455 show_line(log_line, 0);
1458 if (current_nest_size == nest_size) /* arbitrary limit */
1460 /* memory_error ("nest stack", (nest_size + 1) * sizeof(list_state_record)); */
1462 return nest; /* let TeX handle the error */
1465 min_size = current_nest_size / 100 * percent_grow;
1467 if (size < min_size)
1470 if (size < initial_nest_size)
1471 size = initial_nest_size;
1473 for (k = 0; k < MAXSPLITS; k++)
1475 new_size = current_nest_size + size;
1477 if (new_size > nest_size)
1478 new_size = nest_size;
1480 n = (new_size + 1) * sizeof (list_state_record); /* nest[nest_size + 1] */
1483 trace_memory("nest stack", n);
1485 new_nest = (list_state_record *) REALLOC (nest, n);
1487 if (new_nest != NULL)
1488 break; /* did we get it ? */
1490 if (current_nest_size == 0)
1491 break; /* initial allocation must work */
1493 size = size / 2; /* else can retry smaller */
1496 if (new_nest == NULL)
1498 memory_error("nest stack", n);
1499 return nest; /* try and continue !!! */
1503 update_statistics ((int) nest, n, current_nest_size);
1504 current_nest_size = new_size;
1508 sprintf(log_line, "Current %s %d\n", "nest_size", current_nest_size);
1509 show_line(log_line, 0);
1510 sprintf(log_line, "New Address %s == %d\n", "nest stack", nest);
1511 show_line(log_line, 0);
1521 #ifdef ALLOCATEPARAMSTACK
1522 int current_param_size = 0;
1524 halfword *realloc_param_stack (int size)
1527 int n = 0, new_size = 0;
1528 halfword * new_param = NULL;
1532 sprintf(log_line, "Old Address %s == %d\n", "param stack", param_stack);
1533 show_line(log_line, 0);
1536 if (current_param_size == param_size) /* arbitrary limit */
1538 /* memory_error ("param stack", (param_size + 1) * sizeof(halfword)); */
1540 return param_stack; /* let TeX handle the error */
1543 min_size = current_param_size / 100 * percent_grow;
1545 if (size < min_size)
1548 if (size < initial_param_size)
1549 size = initial_param_size;
1551 for (k = 0; k < MAXSPLITS; k++)
1553 new_size = current_param_size + size;
1555 if (new_size > param_size)
1556 new_size = param_size;
1558 n = (new_size + 1) * sizeof (halfword); /* param_stack[param_size + 1] */
1561 trace_memory("param stack", n);
1563 new_param = (halfword *) REALLOC (param_stack, n);
1565 if (new_param != NULL)
1566 break; /* did we get it ? */
1568 if (current_param_size == 0)
1569 break; /* initial allocation must work */
1571 size = size / 2; /* else can retry smaller */
1574 if (new_param == NULL)
1576 memory_error("param stack", n);
1577 return param_stack; /* try and continue !!! */
1580 param_stack = new_param;
1581 update_statistics ((int) param_stack, n, current_param_size);
1582 current_param_size = new_size;
1586 sprintf(log_line, "Current %s %d\n", "param_size", current_param_size);
1587 show_line(log_line, 0);
1588 sprintf(log_line, "New Address %s == %d\n", "param stack", param_stack);
1589 show_line(log_line, 0);
1593 probe_show(); /* 94/Mar/25 */
1599 #ifdef ALLOCATEBUFFER
1600 int current_buf_size = 0;
1602 ASCII_code * realloc_buffer (int size)
1605 int n = 0, new_size = 0;
1606 ASCII_code * new_buffer = NULL;
1610 sprintf(log_line, "Old Address %s == %d\n", "buffer", buffer);
1611 show_line(log_line, 0);
1614 if (current_buf_size == buf_size)
1616 /* memory_error ("buffer", buf_size); */
1618 return buffer; /* pass it back to TeX 99/Fabe/4 */
1621 min_size = current_buf_size / 100 * percent_grow;
1623 if (size < min_size)
1626 if (size < initial_buf_size)
1627 size = initial_buf_size;
1629 for (k = 0; k < MAXSPLITS; k++)
1631 new_size = current_buf_size + size;
1633 if (new_size > buf_size)
1634 new_size = buf_size;
1636 n = (new_size + 1) * sizeof(ASCII_code);
1639 trace_memory("buffer", n);
1641 new_buffer = (ASCII_code *) REALLOC (buffer, n);
1643 if (new_buffer != NULL)
1644 break; /* did we get it ? */
1646 if (current_buf_size == 0)
1647 break; /* initial allocation must work */
1652 if (new_buffer == NULL)
1654 memory_error("buffer", n);
1655 return buffer; /* try and continue !!! */
1658 buffer = new_buffer;
1659 update_statistics ((int) buffer, n, current_buf_size);
1662 memset(buffer + current_buf_size, 0, new_size - current_buf_size);
1664 for (k = current_buf_size; k < new_size; k++) buffer[k]= 0;
1667 current_buf_size = new_size;
1671 sprintf(log_line, "Current %s %d\n", "buffer", current_buf_size);
1672 show_line(log_line, 0);
1673 sprintf(log_line, "New Address %s == %d\n", "buffer", buffer);
1674 show_line(log_line, 0);
1684 /* here is the main memory allocation routine -- calls the above */
1685 /* returns -1 if it fails */
1686 /* allocate rather than static 93/Nov/26 */
1687 int allocate_memory (void)
1689 #ifdef ALLOCATEINPUTSTACK
1691 current_stack_size = 0;
1692 input_stack = realloc_input_stack (initial_stack_size); /* + 1 */
1695 #ifdef ALLOCATENESTSTACK
1697 current_nest_size = 0;
1698 nest = realloc_nest_stack (initial_nest_size); /* + 1 */
1701 #ifdef ALLOCATEPARAMSTACK
1703 current_param_size = 0;
1704 param_stack = realloc_param_stack (initial_param_size); /* + 1 */
1707 #ifdef ALLOCATESAVESTACK
1709 current_save_size = 0;
1710 save_stack = realloc_save_stack (initial_save_size);
1714 buffer = NULL; /* need to do earlier */
1715 current_buf_size = 0;
1716 buffer = realloc_buffer (initial_buf_size);
1719 #ifdef ALLOCATESTRING
1721 current_pool_size = 0;
1723 current_max_strings = 0;
1725 /* maybe taylor allocations to actual pool file 1300 strings 27000 bytes ? */
1729 puts("ini TeX pool and string allocation\n");
1731 str_pool = realloc_str_pool(initial_pool_size);
1732 str_start = realloc_str_start(initial_max_strings);
1736 /* the following can save a lot of the usual 800k fixed allocation */
1739 current_font_mem_size = 0;
1740 /* if not iniTeX, then do initial allocation on fmt file read in itex.c */
1741 /* if ini-TeX we need to do it here - no format file read later */
1743 font_info = realloc_font_info (initial_font_mem_size);
1749 mem_min = mem_bot; /* just to avoid complaints in texbody */
1750 mem_top = mem_initex;
1752 /* allocate main memory here if this is iniTeX */
1753 /* otherwise wait for format undumping in itex.c ... */
1756 /* avoid this if format specified on command line ??? */
1757 /* allocate_main_memory(mem_initex); */ /* made variable ! */
1758 mem = allocate_main_memory(mem_initex); /* made variable ! */
1761 return -1; /* serious error */
1765 /* now for the hyphenation exception stuff */
1766 #ifdef ALLOCATEHYPHEN
1769 /* this will be overridden later by what is in format file */
1770 hyphen_prime = default_hyphen_prime;
1771 /* non ini-TeX use assumes format will be read and that specifies size */
1774 if (new_hyphen_prime)
1775 hyphen_prime = new_hyphen_prime;
1777 if (realloc_hyphen(hyphen_prime)) /* allocate just in case no format */
1782 /* now for memory for the part of the hyphenation stuff that always needed */
1783 /* if iniTeX, need to allocate pre-determined fixed amount - trie_size */
1784 /* if iniTeX not selected, allocate only enough later - undump in itex.c ! */
1785 #ifdef ALLOCATETRIES
1788 if (allocate_tries (trie_size))
1793 /* now for memory for hyphenation stuff needed only when running iniTeX */
1797 if (allocate_ini(trie_size))
1802 trie_l = trie_r = trie_o = trie_hash = NULL; /* (trie_size + 1) * integer */
1803 trie_c = NULL; /* (trie_size + 1) * char */
1804 trie_taken = NULL; /* (trie_size + 1) * boolean */
1808 return 0; // success
1811 /* returns non-zero if error - done to test integrity of stack mostly */
1812 int free_memory (void)
1814 unsigned int heap_total = 0;
1817 puts("free_memory ");
1819 if (verbose_flag || trace_flag)
1820 show_maximums(stdout);
1824 sprintf(log_line, "Heap total: %u bytes --- max address %u\n",
1825 heap_total, max_address);
1826 show_line(log_line, 0);
1831 printf("Main Memory: variable node %d (%d - %d);\n"
1832 " one word %d (%d - %d)\n",
1833 lo_mem_max - mem_min, mem_min, lo_mem_max,
1834 mem_end - hi_mem_min, hi_mem_min, mem_end);
1838 puts("Freeing memory again");
1840 /* only free memory if safe ... additional check */
1844 if (trie_taken != NULL)
1847 if (trie_hash != NULL)
1863 trie_hash = trie_l = trie_r = NULL;
1869 #ifdef ALLOCATETRIES
1870 if (trie_trc != NULL)
1873 if (trie_tro != NULL)
1876 if (trie_trl != NULL)
1880 trie_tro = trie_trl = NULL;
1883 #ifdef ALLOCATEHYPHEN
1884 if (hyph_list != NULL)
1887 if (hyph_word != NULL)
1895 if (main_memory != NULL)
1902 if (font_info != NULL)
1908 #ifdef ALLOCATESTRING
1909 if (str_start != NULL)
1912 if (str_pool != NULL)
1919 #ifdef ALLOCATEPARAMSTACK
1920 if (param_stack != NULL)
1926 #ifdef ALLOCATENESTSTACK
1933 #ifdef ALLOCATEINPUTSTACK
1934 if (input_stack != NULL)
1940 #ifdef ALLOCATESAVESTACK
1941 if (save_stack != NULL)
1947 if (format_file != NULL)
1950 if (source_direct != NULL)
1951 free(source_direct);
1953 format_file = source_direct = NULL;
1955 if (dvi_file_name != NULL)
1956 free(dvi_file_name);
1958 if (log_file_name != NULL)
1959 free(log_file_name);
1961 if (pdf_file_name != NULL)
1962 free(pdf_file_name);
1964 pdf_file_name = log_file_name = dvi_file_name = NULL;
1969 boolean prime (int x)
1972 int sum = 1; /* 1 + 3 + 5 + k = (k + 1) * (k + 1) / 4 */
1977 for (k = 3; k < x; k = k + 2)
1992 boolean show_use = false;
1993 boolean floating = false;
1995 void complainarg (int c, char *s)
1997 sprintf(log_line, "ERROR: Do not understand `%c' argument value `%s'\n", c, s);
1998 show_line(log_line, 1);
2002 /* following is list of allowed command line flags and args */
2004 char *allowedargs = "+bcdfijnpqrstvwyzABCDFGIJKLMNOPQRSTVWXYZ023456789?a=e=g=h=k=l=m=o=u=x=E=H=P=U=";
2006 void reorderargs (int ac, char **av)
2010 char takeargs[256]; /* large enough for all command line arg chars */
2014 return; /* no args ! */
2018 t = takeargs; /* list of those that take args */
2020 while (*s != '\0' && *(s + 1) != '\0')
2022 if (*(s + 1) == '=')
2023 *t++ = *s++; /* copy over --- without the = */
2032 show_line(takeargs, 0);
2043 if (n + 1 < ac && *(av[n] + 2) == '\0' &&
2044 strchr(takeargs, *(av[n] + 1)) != NULL)
2045 n += 2; /* step over it */
2060 while (m < ac && *av[m] != '-')
2061 m++; /* first command */
2065 /* does it take an argument ? and is this argument next ? */
2066 /* check first whether the `-x' is isolated, or arg follows directly */
2067 /* then check whether this is one of those that takes an argument */
2068 if (m+1 < ac && *(av[m] + 2) == '\0' &&
2069 strchr(takeargs, *(av[m] + 1)) != NULL)
2071 s = av[m]; /* move command down before non-command */
2075 av[m + 1] = av[m - 1];
2079 n += 2; /* step over moved args */
2083 s = av[m]; /* move command down before non-command */
2089 n++; /* step over moved args */
2094 int test_align (int address, int size, char *name)
2105 sprintf(log_line, "OFFSET %d (ELEMENT %d) in %s\n", n, size, name);
2106 show_line(log_line, 0);
2112 /* activate detailed checking of alignment when trace_flag is set */
2114 void check_fixed_align (int flag)
2118 if (test_align ((int) &mem_top, 4, "FIXED ALIGNMENT"))
2120 puts("PLEASE RECOMPILE ME!\n");
2123 #ifdef CHECKALIGNMENT
2127 test_align ((int) &mem_top, 4, "mem_top");
2128 test_align ((int) &mem_max, 4, "mem_max");
2129 test_align ((int) &mem_min, 4, "mem_min");
2130 test_align ((int) &bad, 4, "bad");
2131 test_align ((int) &trie_size, 4, "trie_size");
2132 test_align ((int) &xord, sizeof(xord[0]), "xord"); /* no op */
2133 test_align ((int) &xchr, sizeof(xchr[0]), "xchr"); /* no op */
2134 test_align ((int) &name_length, 4, "name_length");
2135 test_align ((int) &first, 4, "first");
2136 test_align ((int) &last, 4, "last");
2137 test_align ((int) &max_buf_stack, 4, "max_buf_stack");
2138 test_align ((int) &pool_ptr, 4, "pool_ptr");
2139 test_align ((int) &str_ptr, 4, "str_ptr");
2140 test_align ((int) &init_pool_ptr, 4, "init_pool_ptr");
2141 test_align ((int) &init_str_ptr, 4, "init_str_ptr");
2142 test_align ((int) &log_file, 4, "log_file");
2143 test_align ((int) &tally, 4, "tally");
2144 test_align ((int) &term_offset, 4, "term_offset");
2145 test_align ((int) &file_offset, 4, "file_offset");
2146 test_align ((int) &trick_count, 4, "trick_count");
2147 test_align ((int) &first_count, 4, "first_count");
2148 test_align ((int) &deletions_allowed, 4, "deletions_allowed");
2149 test_align ((int) &set_box_allowed, 4, "set_box_allowed");
2150 test_align ((int) &help_line, sizeof(help_line[0]), "help_line");
2151 test_align ((int) &use_err_help, 4, "use_err_help");
2152 test_align ((int) &interrupt, 4, "interrupt");
2153 test_align ((int) &OK_to_interrupt, 4, "OK_to_interrupt");
2154 test_align ((int) &arith_error, 4, "arith_error");
2155 test_align ((int) &tex_remainder, 4, "tex_remainder");
2156 test_align ((int) &temp_ptr, 4, "temp_ptr");
2157 test_align ((int) &lo_mem_max, 4, "lo_mem_max");
2158 test_align ((int) &hi_mem_min, 4, "hi_mem_min");
2159 test_align ((int) &var_used, 4, "var_used");
2160 test_align ((int) &dyn_used, 4, "dyn_used");
2161 test_align ((int) &avail, 4, "avail");
2162 test_align ((int) &mem_end, 4, "mem_end");
2163 test_align ((int) &mem_start, 4, "mem_start");
2164 test_align ((int) &rover, 4, "rover");
2165 test_align ((int) &font_in_short_display, 4, "font_in_short_display");
2166 test_align ((int) &depth_threshold, 4, "depth_threshold");
2167 test_align ((int) &breadth_max, 4, "breadth_max");
2168 test_align ((int) &nest, sizeof(nest[0]), "nest");
2169 /* test_align ((int) &xeq_level, sizeof(xeq_level[0]), "xeq_level"); */
2170 test_align ((int) &zzzad, sizeof(zzzad[0]), "zzzad");
2171 /* test_align ((int) &hash, sizeof(hash[0]), "hash"); */
2172 test_align ((int) &zzzae, sizeof(zzzae[0]), "zzzae");
2174 test_align ((int) &save_stack, sizeof(save_stack[0]), "save_stack");
2175 test_align ((int) &input_stack, sizeof(input_stack[0]), "input_stack");
2176 test_align ((int) &input_file, sizeof(input_file[0]), "input_file");
2177 test_align ((int) &line_stack, sizeof(line_stack[0]), "line_stack");
2178 test_align ((int) ¶m_stack, sizeof(param_stack[0]), "param_stack");
2179 test_align ((int) &cur_mark, sizeof(cur_mark[0]), "cur_mark");
2180 test_align ((int) &pstack, sizeof(pstack[0]), "pstack");
2181 test_align ((int) &read_file, sizeof(read_file[0]), "read_file");
2183 test_align ((int) &font_check, sizeof(font_check[0]), "font_check");
2184 test_align ((int) &font_size, sizeof(font_size[0]), "font_size");
2185 test_align ((int) &font_dsize, sizeof(font_dsize[0]), "font_dsize");
2186 test_align ((int) &font_params, sizeof(font_params[0]), "font_params");
2187 test_align ((int) &font_name, sizeof(font_name[0]), "font_name");
2188 test_align ((int) &font_area, sizeof(font_area[0]), "font_area");
2189 test_align ((int) &font_bc, sizeof(font_bc[0]), "font_bc");
2190 test_align ((int) &font_ec, sizeof(font_ec[0]), "font_ec");
2191 test_align ((int) &font_glue, sizeof(font_glue[0]), "font_glue");
2192 test_align ((int) &font_used, sizeof(font_used[0]), "font_used");
2193 test_align ((int) &hyphen_char, sizeof(hyphen_char[0]), "hyphen_char");
2194 test_align ((int) &skew_char, sizeof(skew_char[0]), "skew_char");
2195 test_align ((int) &bchar_label, sizeof(bchar_label[0]), "bchar_label");
2196 test_align ((int) &font_bchar, sizeof(font_bchar[0]), "font_bchar");
2197 test_align ((int) &font_false_bchar, sizeof(font_false_bchar[0]), "font_false_bchar");
2198 test_align ((int) &char_base, sizeof(char_base[0]), "char_base");
2199 test_align ((int) &width_base, sizeof(width_base[0]), "width_base");
2200 test_align ((int) &height_base, sizeof(height_base[0]), "height_base");
2201 test_align ((int) &depth_base, sizeof(depth_base[0]), "depth_base");
2202 test_align ((int) &italic_base, sizeof(italic_base[0]), "italic_base");
2203 test_align ((int) &lig_kern_base, sizeof(lig_kern_base[0]), "lig_kern_base");
2204 test_align ((int) &kern_base, sizeof(kern_base[0]), "kern_base");
2205 test_align ((int) &exten_base, sizeof(exten_base[0]), "exten_base");
2206 test_align ((int) ¶m_base, sizeof(param_base[0]), "param_base");
2208 #ifdef ALLOCATEDVIBUF
2209 test_align ((int) &zdvibuf, sizeof(zdvibuf[0]), "zdvibuf"); /* no op */
2212 test_align ((int) &total_stretch, sizeof(total_stretch[0]), "total_stretch");
2213 test_align ((int) &total_shrink, sizeof(total_shrink[0]), "total_shrink");
2214 test_align ((int) &active_width, sizeof(active_width[0]), "active_width");
2215 test_align ((int) &cur_active_width, sizeof(cur_active_width[0]), "cur_active_width");
2216 test_align ((int) &background, sizeof(background[0]), "background");
2217 test_align ((int) &break_width, sizeof(break_width[0]), "break_width");
2218 test_align ((int) &minimal_demerits, sizeof(minimal_demerits[0]), "minimal_demerits");
2219 test_align ((int) &best_place, sizeof(best_place[0]), "best_place");
2220 test_align ((int) &best_pl_line, sizeof(best_pl_line[0]), "best_pl_line");
2221 test_align ((int) &hc, sizeof(hc[0]), "hc");
2222 test_align ((int) &hu, sizeof(hu[0]), "hu");
2223 test_align ((int) &hyf, sizeof(hyf[0]), "hyf");
2224 /* test_align ((int) &x, sizeof(x[0]), "x"); */
2226 test_align ((int) &hyf_distance, sizeof(hyf_distance[0]), "hyf_distance");
2227 test_align ((int) &hyf_num, sizeof(hyf_num[0]), "hyf_num");
2228 test_align ((int) &hyf_next, sizeof(hyf_next[0]), "hyf_next");
2229 test_align ((int) &op_start, sizeof(op_start[0]), "op_start");
2231 /* test_align ((int) &trie_op_hash, sizeof(trie_op_hash[0]), "trie_op_hash"); */
2232 test_align ((int) &zzzaf, sizeof(zzzaf[0]), "zzzaf");
2233 test_align ((int) &trie_used, sizeof(trie_used[0]), "trie_used");
2234 /* test_align ((int) &trie_op_lang, sizeof(trie_op_lang[0]), "trie_op_lang");*/
2235 test_align ((int) &trie_op_val, sizeof(trie_op_val[0]), "trie_op_val");
2237 test_align ((int) &trie_min, sizeof(trie_min[0]), "trie_min");
2238 test_align ((int) &page_so_far, sizeof(page_so_far[0]), "page_so_far");
2239 test_align ((int) &write_file, sizeof(write_file[0]), "write_file");
2240 test_align ((int) &write_open, sizeof(write_open[0]), "write_open");
2244 void check_alloc_align (int flag)
2248 if (test_align((int) eqtb, sizeof(eqtb[0]), "ALLOCATED ALIGNMENT"))
2249 puts("PLEASE RECOMPILE ME!\n");
2251 #ifdef CHECKALIGNMENT
2256 test_align ((int) zeqtb, sizeof(zeqtb[0]), "zeqtb");
2259 #ifndef ALLOCATEDVIBUF
2260 test_align ((int) &zdvibuf, sizeof(zdvibuf[0]), "zdvibuf"); /* no op */
2263 test_align ((int) str_pool, sizeof(str_pool[0]), "str_pool"); /* no op */
2264 test_align ((int) str_start, sizeof(str_start[0]), "str_start");
2265 test_align ((int) zmem, sizeof(zmem[0]), "main memory");
2266 test_align ((int) font_info, sizeof(font_info[0]), "font memory");
2267 test_align ((int) trie_trl, sizeof(trie_trl[0]), "trie_trl");
2268 test_align ((int) trie_tro, sizeof(trie_tro[0]), "trie_tro");
2269 test_align ((int) trie_trc, sizeof(trie_trc[0]), "trie_trc");
2270 test_align ((int) hyph_word, sizeof(hyph_word[0]), "hyph_word");
2271 test_align ((int) hyph_list, sizeof(hyph_list[0]), "hyph_list");
2272 /* test_align ((int) trie_c, sizeof(trie_c[0]), "trie_c"); *//* no op */
2273 test_align ((int) trie_o, sizeof(trie_o[0]), "trie_o");
2274 test_align ((int) trie_l, sizeof(trie_l[0]), "trie_l");
2275 test_align ((int) trie_r, sizeof(trie_r[0]), "trie_r");
2276 test_align ((int) trie_hash, sizeof(trie_hash[0]), "trie_hash");
2277 test_align ((int) trie_taken, sizeof(trie_taken[0]), "trie_taken");
2281 boolean backwardflag = false; /* don't cripple all advanced features */
2282 boolean shorten_file_name = false; /* don't shorten file names to 8+3 for DOS */
2283 boolean usesourcedirectory = true; /* use source file directory as local when WorkingDirectory is set */
2285 /* cache to prevent allocating twice in a row */
2287 char *lastname = NULL, *lastvalue = NULL;
2289 /* returns allocated string -- these strings are not freed again */
2290 /* is it safe to do that now ? 98/Jan/31 */
2291 char * grabenv (char * varname)
2295 if (varname == NULL)
2298 if (*varname == '\0')
2301 if (lastname != NULL && strcasecmp(lastname, varname) == 0)
2305 sprintf(log_line, "Cache hit: %s=%s\n", lastname, lastvalue);
2306 show_line(log_line, 0);
2309 return xstrdup(lastvalue);
2312 s = getenv(varname);
2316 if (lastname != NULL)
2319 lastname = xstrdup(varname);
2321 if (lastvalue != NULL)
2324 lastvalue = xstrdup(s);
2332 void flush_trailing_slash (char *directory)
2336 if (strcmp(directory, "") != 0)
2338 s = directory + strlen(directory) - 1;
2340 if (*s == '\\' || *s == '/')
2345 void knuthify (void)
2347 restrict_to_ascii = false; /* don't complain non ASCII */
2348 allow_patterns = false; /* don't allow pattern redefinition */
2349 show_in_hex = true; /* show character code in hex */
2350 show_in_dos = false; /* redundant with previous */
2351 show_numeric = false; /* don't show character code decimal */
2352 show_missing = false; /* don't show missing characters */
2353 civilize_flag = false; /* don't reorder date fields */
2354 c_style_flag = false; /* don't add file name to error msg */
2355 show_fmt_flag = false; /* don't show format file in log */
2356 show_tfm_flag = false; /* don't show metric file in log */
2358 show_line_break_stats = false; /* do not show line break stats */
2359 show_fonts_used = false;
2360 default_rule = 26214; /* revert to default rule thickness */
2361 pseudo_tilde = false;
2362 pseudo_space = false;
2363 show_texinput_flag = false;
2364 truncate_long_lines = false;
2365 allow_quoted_names = false;
2366 show_cs_names = false;
2367 font_dimen_zero = false;
2368 ignore_frozen = false;
2369 suppress_f_ligs = false;
2370 full_file_name_flag = false;
2371 knuth_flag = true; /* so other code can know about this */
2374 /* following made global so analyze_flag can be made separate procedure */
2376 char * xchr_file = NULL;
2377 char * repl_file = NULL;
2379 char * short_options = "m:e:h:0:H:g:P:o:l:a:wvpiKLZMdp2t?u";
2381 static struct option long_options[] =
2383 {"main-memory", 1, 0, 'm'},
2384 {"hyph-size", 1, 0, 'e'},
2385 {"trie-size", 1, 0, 'h'},
2386 {"backend", 1, 0, '0'},
2387 {"tab-step", 1, 0, 'H'},
2388 {"percent-grow", 1, 0, 'g'},
2389 {"default-rule", 1, 0, 'P'},
2390 {"dvi-dir", 1, 0, 'o'},
2391 {"log-dir", 1, 0, 'l'},
2392 {"aux-dir", 1, 0, 'a'},
2393 {"showhex", 0, 0, 'w'},
2394 {"verbose", 0, 0, 'v'},
2395 {"patterns", 0, 0, 'p'},
2396 {"initex", 0, 0, 'i'},
2397 {"knuthify", 0, 0, 'K'},
2398 {"cstyle", 0, 0, 'L'},
2399 {"showtfm", 0, 0, 'Z'},
2400 {"showmissing", 0, 0, 'M'},
2401 {"deslash", 0, 0, 'd'},
2402 {"patterns", 0, 0, 'p'},
2403 {"suppressflig", 0, 0, '2'},
2404 {"trace", 0, 0, 't'},
2405 {"help", 0, 0, '?'},
2406 {"usage", 0, 0, 'u'},
2410 int analyze_flag (int c, char *optarg)
2415 want_version = true;
2416 verbose_flag = true;
2422 interaction = batch_mode; /* quiet mode */
2425 interaction = nonstop_mode; /* run mode */
2428 interaction = scroll_mode; /* scroll mode */
2431 interaction = error_stop_mode; /* tex mode */
2434 backwardflag = true;
2435 knuthify(); /* revert to `standard' Knuth TeX */
2438 c_style_flag = true; /* C style error msg 94/Mar/21 */
2441 show_tfm_flag = true; /* show TFM in log file 94/Jun/21 */
2444 show_missing = false; /* do not show missing 94/June/10 */
2447 deslash = false; /* flipped 93/Nov/18 */
2448 /* pseudo_tilde = 0; */ /* new 95/Sep/26 */
2451 allow_patterns = true; /* 93/Nov/26 */
2452 /* reset_exceptions = true; */ /* 93/Dec/23 */
2454 /* case 'w': show_in_hex = false; */ /* 94/Jan/26 */
2456 show_in_hex = true; /* flipped 00/Jun/18 */
2459 show_in_dos = true; /* 96/Jan/26 */
2462 restrict_to_ascii = true; /* 0 - 127 1994/Jan/21 */
2465 usesourcedirectory = false; /* use working dir 98/Sep/29 */
2468 show_fonts_used = false; /* 97/Dec/24 */
2471 shorten_file_name = true; /* 95/Feb/20 */
2474 show_cs_names = true; /* 98/Mar/31 */
2477 ignore_frozen = true; /* 98/Oct/5 */
2480 font_dimen_zero = false; /* 98/Oct/5 */
2483 show_texinput_flag = false; /* 98/Jan/28 */
2485 /* case 'X': truncate_long_lines = false; */ /* 98/Feb/2 */
2488 show_line_break_stats = false; /* 96/Feb/8 */
2491 show_fmt_flag = false; /* 94/Jun/21 */
2494 suppress_f_ligs = true; /* 99/Jan/5 f-lig */
2497 full_file_name_flag = false; // 00 Jun 18
2503 quitflag++; /* 93/Dec/16 */
2505 /* The following are really obscure and should not be advertized */
2507 show_current = false; /* tex8 93/Dec/14 */
2510 show_numeric = false; /* 93/Dec/21 */
2513 civilize_flag = false; /* 93/Dec/16 */
2516 open_trace_flag = true; /* openinou 1994/Jan/8 */
2519 reorder_arg_flag = false; /* local */
2524 mem_initex = mem_top;
2526 mem_initex = atoi(optarg) * 1024;
2528 if (mem_initex == 0)
2529 complainarg(c, optarg);
2534 #ifdef VARIABLETRIESIZE
2537 trie_size = default_trie_size;
2539 trie_size = atoi(optarg);
2542 complainarg(c, optarg);
2546 #ifdef ALLOCATEHYPHEN
2549 new_hyphen_prime = hyphen_prime * 2;
2551 new_hyphen_prime = atoi(optarg);
2553 if (new_hyphen_prime == 0)
2554 complainarg(c, optarg);
2562 percent_grow = atoi(optarg);
2564 if (percent_grow == 0)
2565 complainarg(c, optarg);
2573 pseudo_tilde = atoi(optarg);
2575 if (pseudo_tilde > 255)
2577 else if (pseudo_tilde < 128)
2586 tab_step = atoi(optarg);
2588 complainarg(c, optarg);
2592 xchr_file = xstrdup("xchr.map");
2594 xchr_file = xstrdup(optarg);
2596 if (xchr_file == NULL || *xchr_file == '\0')
2597 complainarg(c, optarg);
2601 repl_file = xstrdup("repl.key");
2603 repl_file = xstrdup(optarg);
2605 if (repl_file == NULL || *repl_file == '\0')
2606 complainarg(c, optarg);
2610 default_rule = 26214;
2612 default_rule = atoi(optarg);
2614 if (default_rule == 0)
2615 complainarg(c, optarg);
2621 complainarg(c, optarg);
2627 dvi_directory = xstrdup(optarg);
2629 if (strcmp(dvi_directory, "") == 0)
2630 complainarg(c, optarg);
2635 char * format_spec = NULL;
2638 format_spec = xstrdup(optarg);
2640 if (!strcmp(format_spec, "pdf"))
2641 shipout_flag = out_pdf_flag;
2642 else if (!strcmp(format_spec, "dvi"))
2643 shipout_flag = out_dvi_flag;
2644 else if (!strcmp(format_spec, "xdv"))
2645 shipout_flag = out_xdv_flag;
2648 sprintf(log_line, "ERROR: Do not understand shipout flag `%s'\n", format_spec);
2649 show_line(log_line, 1);
2657 log_directory = xstrdup(optarg);
2659 if (strcmp(log_directory, "") == 0)
2660 complainarg(c, optarg);
2668 aux_directory = xstrdup(optarg);
2670 if (strcmp(aux_directory, "") == 0)
2671 complainarg(c, optarg);
2685 void strip_name (char *pathname)
2689 if ((s = strrchr(pathname, '\\')) != NULL)
2691 else if ((s = strrchr(pathname, '/')) != NULL)
2693 else if ((s = strrchr(pathname, ':')) != NULL)
2701 int read_command_line (int ac, char **av)
2710 while ((c = getopt_long_only(ac, av, short_options, long_options, &option_idx)) != EOF)
2712 if (optarg != 0 && *optarg == '=')
2713 optargnew = optarg + 1;
2717 analyze_flag (c, optargnew);
2720 if (show_use || quitflag == 3)
2723 strcat(log_line, "\n");
2724 show_line(log_line, 0);
2725 //stampcopy(log_line);
2726 //strcat(log_line, "\n");
2727 //show_line(log_line, 0);
2731 else if (quitflag == 3)
2733 strcat(log_line, "\n");
2734 show_line(log_line, 0);
2737 return -1; // failure
2740 if (repl_file != NULL && *repl_file != '\0')
2742 if (read_xchr_file(repl_file, 1, av))
2745 puts("KEY REPLACE ON\n");
2751 if (xchr_file != NULL && *xchr_file != '\0')
2753 if (read_xchr_file(xchr_file, 0, av))
2756 puts("NON ASCII ON\n");
2765 int init_commands (int ac, char **av)
2767 shipout_flag = out_dvi_flag;
2769 allow_patterns = false;
2770 reset_exceptions = false;
2772 key_replace = false;
2773 want_version = false;
2774 open_trace_flag = false;
2776 verbose_flag = false;
2777 restrict_to_ascii = false;
2778 show_in_hex = false; /* default is not to show as hex code ^^ 00/Jun/18 */
2779 show_in_dos = false; /* default is not to translate to DOS 850 */
2780 return_flag = true; // hard wired now
2781 trimeof = true; // hard wired now
2783 pseudo_tilde = 254; /* default '~' replace 95/Sep/26 filledbox DOS 850 */
2784 pseudo_space = 255; /* default ' ' replace 97/June/5 nbspace DOS 850 */
2785 default_rule = 26214; /* default rule variable 95/Oct/9 */
2786 show_current = true;
2787 civilize_flag = true;
2788 show_numeric = true;
2789 show_missing = true;
2790 c_style_flag = false; /* use c-style error output */
2791 show_fmt_flag = true; /* show format file in log */
2792 show_tfm_flag = false; /* don't show metric file in log */
2793 shorten_file_name = false; /* don't shorten file names to 8+3 */
2794 show_texinput_flag = true; /* show TEXINPUTS and TEXFONTS */
2795 truncate_long_lines = true; /* truncate long lines */
2796 tab_step = 0; /* do not replace tabs with spaces */
2797 show_line_break_stats = true; /* show line break statistics 96/Feb/8 */
2798 show_fonts_used = true; /* show fonts used in LOG file 97/Dec/24 */
2799 allow_quoted_names = true; /* allow quoted names with spaces 98/Mar/15 */
2800 show_cs_names = false;
2801 knuth_flag = false; /* allow extensions to TeX */
2802 full_file_name_flag = true; /* new default 2000 June 18 */
2803 errout = stdout; /* as opposed to stderr say --- used ??? */
2804 new_hyphen_prime = 0;
2806 #ifdef VARIABLETRIESIZE
2807 /* trie_size = default_trie_size; */
2815 format_name = "plain";
2819 if (read_command_line(ac, av) < 0)
2829 strcat(log_line, "\n");
2830 show_line(log_line, 0);
2831 stampcopy(log_line);
2832 strcat(log_line, "\n");
2833 show_line(log_line, 0);
2840 void initial_memory (void)
2842 /* set initial memory allocations */
2843 if (mem_extra_high < 0)
2846 if (mem_extra_low < 0)
2854 #if defined(ALLOCATEHIGH) || defined(ALLOCATELOW)
2855 if (mem_extra_high != 0 || mem_extra_low != 0)
2857 puts("ERROR: Cannot extend main memory in iniTeX\n");
2858 mem_extra_high = 0; mem_extra_low = 0;
2864 if (mem_initex != 0)
2866 puts("ERROR: Can only set initial main memory size in iniTeX\n");
2872 puts("ERROR: Need only set hyphenation trie size in iniTeX\n");
2873 /* trie_size = 0; */
2876 if (mem_initex == 0)
2877 mem_initex = default_mem_top;
2880 trie_size = default_trie_size;
2881 /* Just in case user mistakenly specified words instead of kilo words */
2882 if (mem_extra_high > 10000L * 1024L)
2883 mem_extra_high = mem_extra_high / 1024;
2885 if (mem_extra_low > 10000L * 1024L)
2886 mem_extra_low = mem_extra_low / 1024;
2888 if (mem_initex > 10000L * 1024L)
2889 mem_initex = mem_initex / 1024;
2891 if (mem_initex > 2048L * 1024L) /* extend main memory by 16 mega byte! */
2893 puts("WARNING: There may be no benefit to asking for so much memory\n");
2894 /* mem_initex = 2048 * 1024; */
2897 if (new_hyphen_prime < 0)
2898 new_hyphen_prime = 0;
2900 if (new_hyphen_prime > 0)
2903 puts("ERROR: Can only set hyphen prime in iniTeX\n");
2906 if (new_hyphen_prime % 2 == 0)
2909 while (!prime(new_hyphen_prime))
2910 new_hyphen_prime = new_hyphen_prime + 2;
2914 sprintf(log_line, "Using %d as hyphen prime\n", new_hyphen_prime);
2915 show_line(log_line, 0);
2920 if (percent_grow > 100)
2921 percent_grow = percent_grow - 100;
2923 if (percent_grow > 100)
2924 percent_grow = 100; /* upper limit - double */
2926 if (percent_grow < 10)
2927 percent_grow = 10; /* lower limit - 10% */
2930 /**********************************************************************/
2932 void perrormod (char *s)
2934 sprintf(log_line, "`%s': %s\n", s, strerror(errno));
2935 show_line(log_line, 1);
2938 /*************************************************************************/
2940 /* convert tilde to pseudo_tilde to hide it from TeX --- 95/Sep/26 */
2941 /* convert space to pseudo_space to hide it from TeX --- 97/Jun/5 */
2942 /* called only if pseudo_tilde != 0 or pseudo_space != 0 */
2943 /* this is then undone in tex3.c both for fopen input and output */
2944 /* not ideal, since pseudo name appears in log and in error messages ... */
2946 void hidetwiddle (char *name)
2953 sprintf(log_line, "Hidetwiddle %s", name);
2954 show_line(log_line, 0);
2960 if (*s == '~' && pseudo_tilde != 0)
2961 *s = (char) pseudo_tilde; /* typically 254 */
2962 else if (*s == ' ' && pseudo_space != 0)
2963 *s = (char) pseudo_space; /* typically 255 */
2970 sprintf(log_line, "=> %s\n", name);
2971 show_line(log_line, 0);
2976 void deslash_all (int ac, char **av)
2978 char buffer[file_name_size];
2981 if ((s = grabenv("TEXDVI")) != NULL)
2984 if ((s = grabenv("TEXLOG")) != NULL)
2987 if ((s = grabenv("TEXAUX")) != NULL)
2990 if ((s = grabenv("TEXFMT")) != NULL)
2993 if ((s = grabenv("TEXPDF")) != NULL)
2996 strcpy(buffer, av[0]);
2998 if ((s = strrchr(buffer, '\\')) != NULL)
3000 else if ((s = strrchr(buffer, '/')) != NULL)
3002 else if ((s = strrchr(buffer, ':')) != NULL)
3005 s = buffer + strlen(buffer) - 1;
3007 if (*s == '\\' || *s == '/')
3010 texpath = xstrdup(buffer);
3012 if (strcmp(dvi_directory, "") != 0)
3013 flush_trailing_slash(dvi_directory);
3015 if (strcmp(log_directory, "") != 0)
3016 flush_trailing_slash(log_directory);
3018 if (strcmp(aux_directory, "") != 0)
3019 flush_trailing_slash(aux_directory);
3021 if (strcmp(fmt_directory, "") != 0)
3022 flush_trailing_slash(fmt_directory);
3024 if (strcmp(pdf_directory, "") != 0)
3025 flush_trailing_slash(pdf_directory);
3031 if (strcmp(dvi_directory, "") != 0)
3032 unixify(dvi_directory);
3034 if (strcmp(log_directory, "") != 0)
3035 unixify(log_directory);
3037 if (strcmp(aux_directory, "") != 0)
3038 unixify(aux_directory);
3040 if (strcmp(fmt_directory, "") != 0)
3041 unixify(fmt_directory);
3043 if (strcmp(pdf_directory, "") != 0)
3044 unixify(pdf_directory);
3049 if (optind < ac && optind > 0)
3053 if (trace_flag || debug_flag)
3055 sprintf(log_line, "deslash: k %d argv[k] %s (argc %d)\n",
3056 optind, av[optind], ac);
3057 show_line(log_line, 0);
3060 unixify(av[optind]);
3063 if (pseudo_tilde != 0 || pseudo_space != 0)
3064 hidetwiddle (av[optind]);
3066 if (*av[optind] == '&' || *av[optind] == '+')
3069 format_name = xstrdup(av[optind] + 1);
3071 if (optind + 1 < ac)
3075 if (trace_flag || debug_flag)
3077 sprintf(log_line, "deslash: k %d argv[k] %s (argc %d)\n",
3078 optind + 1, av[optind + 1], ac);
3079 show_line(log_line, 0);
3082 unixify(av[optind + 1]);
3085 if (pseudo_tilde != 0 || pseudo_space != 0)
3086 hidetwiddle (av[optind + 1]);
3092 /* note: those optarg == 0 test don't really work ... */
3093 /* note: optarg starts at = in case of x=... */
3095 int main_init (int ac, char **av)
3097 kpse_set_program_name(av[0], NULL);
3098 xputenv("engine", "yandytex");
3100 if (sizeof(memory_word) != sizeof(halfword) * 2)
3101 printf("ERROR: Bad word size %d!\n", sizeof(memory_word));
3103 start_time = clock();
3104 main_time = start_time;
3106 /* reset all allocatable memory pointers to NULL - in case we drop out */
3112 #ifdef ALLOCATESAVESTACK
3116 #ifdef ALLOCATEBUFFER
3118 current_buf_size = 0;
3119 buffer = realloc_buffer (initial_buf_size);
3134 log_opened = false; /* so can tell whether opened */
3135 interaction = -1; /* default state => 3 */
3136 missing_characters = 0; /* none yet! */
3137 font_dimen_zero = true; /* \fontdimen0 for checksum 98/Oct/5 */
3138 ignore_frozen = false; /* default is not to ignore 98/Oct/5 */
3139 suppress_f_ligs = false; /* default is not to ignore f-ligs */
3141 if (ac > 1 && !strncmp(av[1], "-Y", 2))
3142 reorder_arg_flag = false;
3144 if (reorder_arg_flag)
3145 reorderargs(ac, av);
3147 if (init_commands(ac, av))
3150 check_fixed_align(trace_flag);
3153 source_direct = NULL;
3154 dvi_file_name = NULL;
3155 log_file_name = NULL;
3156 pdf_file_name = NULL;
3158 first_pass_count = 0;
3159 second_pass_count = 0;
3160 final_pass_count = 0;
3161 paragraph_failed = 0;
3170 puts("Entering init (local.c)");
3173 ini_max_address = max_address;
3176 show_maximums(stdout);
3179 deslash_all(ac, av); /* deslash and note if format specified */
3182 if (format_spec && mem_spec_flag)
3183 puts("WARNING: Cannot change initial main memory size when format specified");
3185 if (allocate_memory() != 0)
3188 check_alloc_align(trace_flag);
3191 puts("Leaving init (local.c)");
3196 #define CLK_TCK CLOCKS_PER_SEC
3198 void show_inter_val (clock_t inter_val)
3200 int seconds, tenths, hundredth, thousands;
3202 if (inter_val >= CLK_TCK * 10)
3204 tenths = (inter_val * 10 + CLK_TCK / 2) / CLK_TCK;
3205 seconds = tenths / 10;
3206 tenths = tenths % 10;
3207 printf("%d.%d", seconds, tenths);
3209 else if (inter_val >= CLK_TCK)
3211 hundredth = (inter_val * 100 + CLK_TCK / 2) / CLK_TCK;
3212 seconds = hundredth / 100;
3213 hundredth = hundredth % 100;
3214 printf("%d.%02d", seconds, hundredth);
3216 else if (inter_val > 0)
3218 thousands = (inter_val * 1000 + CLK_TCK / 2) / CLK_TCK;
3219 seconds = thousands / 1000;
3220 thousands = thousands % 1000;
3221 printf("%d.%03d", seconds, thousands);
3229 finish_time = clock();
3231 if (missing_characters != 0)
3234 if (missing_characters)
3236 sprintf(log_line, "! There %s %d missing character%s --- see log file\n",
3237 (missing_characters == 1) ? "was" : "were", missing_characters,
3238 (missing_characters == 1) ? "" : "s");
3239 show_line(log_line, 0);
3242 if (free_memory() != 0)
3248 show_inter_val(finish_time - start_time);
3250 show_inter_val(main_time - start_time);
3251 printf(" format load + ");
3252 show_inter_val(finish_time - main_time);
3253 printf(" processing) ");
3255 if (total_pages > 0)
3257 show_inter_val((finish_time - main_time) / total_pages);
3258 printf(" sec per page.");
3264 // printf control sequences' name
3265 void print_cs_name (FILE *output, int h)
3269 memset(log_line, 0, sizeof(log_line));
3271 textof = hash[h].rh;
3276 c = sprintf(log_line, "(%d), ", h);
3279 memmove(log_line + c, str_pool + str_start[textof], n);
3280 memmove(log_line + c + n, "\n", 2);
3282 if (output == stderr)
3284 show_line(log_line, 1);
3288 if (output == stdout)
3289 show_line(log_line, 0);
3291 fprintf(output, log_line);
3296 int compare_strn (int, int, int, int);
3297 /* compare two csnames in qsort */
3298 int compare_cs (const void *cp1, const void *cp2)
3300 int c1, c2, l1, l2, k1, k2, textof1, textof2;
3304 textof1 = hash[c1].rh;
3305 textof2 = hash[c2].rh;
3306 l1 = length(textof1);
3307 l2 = length(textof2);
3308 k1 = str_start[textof1];
3309 k2 = str_start[textof2];
3311 return compare_strn(k1, l1, k2, l2);
3314 char * csused = NULL;
3316 /* Allocate table of indeces to allow sorting on csname */
3317 /* Allocate flags to remember which ones already listed at start */
3318 /* pass = 0 --> fmt */
3319 /* pass = 1 --> after */
3320 void print_cs_names (FILE *output, int pass)
3322 int h, k, ccount, repeatflag;
3324 int nfcs = frozen_control_sequence;
3326 if (pass == 0 && csused == NULL)
3328 csused = (char *) malloc (nfcs);
3334 memset(csused, 0, nfcs);
3336 for (h = 0; h < (hash_size + 780); h++)
3343 for (h = hash_base + 1; h < nfcs; h++)
3345 if (pass == 1 && csused[h])
3348 if (hash[h].rh != 0)
3357 sprintf(log_line, "\n%d %s multiletter control sequences:\n",
3358 ccount, (pass == 1) ? "new" : "");
3360 if (output == stderr)
3362 show_line(log_line, 1);
3366 if (output == stdout)
3367 show_line(log_line, 0);
3369 fprintf(output, log_line);
3374 cnumtable = (int *) malloc (ccount * sizeof(int));
3376 if (cnumtable == NULL)
3381 for (h = hash_base + 1; h < nfcs; h++)
3383 if (pass == 1 && csused[h])
3386 if (hash[h].rh != 0)
3387 cnumtable[ccount++] = h;
3390 //qsort ((void *)cnumtable, ccount, sizeof (int), &compare_cs);
3394 for (k = 0; k < ccount; k++)
3398 if (pass == 1 && csused[h])
3401 print_cs_name(output, h);
3404 sprintf(log_line, "\n");
3406 if (output == stderr)
3408 show_line(log_line, 1);
3412 if (output == stdout)
3413 show_line(log_line, 0);
3415 fprintf(output, log_line);
3418 free((void *)cnumtable);
3421 if (pass == 1 && csused != NULL)
3428 /* k1 and k2 are positions in string pool */
3429 /* l1 and l2 are lengths of strings */
3430 int compare_strn (int k1, int l1, int k2, int l2)
3434 while (l1 > 0 && l2 > 0)
3449 return 1; /* first string longer */
3451 return -1; /* second string longer */
3453 return 0; /* strings match */
3455 /* compare two font names and their at sizes in qsort */
3456 int compare_fnt (const void *fp1, const void *fp2)
3458 int f1, f2, l1, l2, k1, k2, s;
3462 l1 = length(font_name[f1]);
3463 l2 = length(font_name[f2]);
3464 k1 = str_start[font_name[f1]];
3465 k2 = str_start[font_name[f2]];
3467 s = compare_strn (k1, l1, k2, l2);
3472 if (font_size[f1] > font_size[f2])
3474 else if (font_size[f1] < font_size[f2])
3477 return 0; /* should not ever get here */
3479 /* compare two font names */
3480 int compare_fnt_name (int f1, int f2)
3482 int l1, l2, k1, k2, s;
3484 l1 = length(font_name[f1]);
3485 l2 = length(font_name[f2]);
3486 k1 = str_start[font_name[f1]];
3487 k2 = str_start[font_name[f2]];
3489 s = compare_strn (k1, l1, k2, l2);
3493 /* decode checksum information */
3494 unsigned long checkdefault = 0x59265920; /* default signature */
3495 int decode_fourty (unsigned long checksum, char *codingvector)
3499 /* char codingvector[6+1]; */
3503 strcpy(codingvector, "unknwn");
3507 if ((checksum >> 8) == (checkdefault >> 8)) /* last byte random */
3509 /* strcpy (codingvector, "native"); */ /* if not specified ... */
3510 strcpy (codingvector, "fixed "); /* if not specified ... */
3511 return 1; /* no info available */
3515 for (k = 0; k < 6; k++)
3517 c = (int) (checksum % 40);
3518 checksum = checksum / 40;
3519 if (c <= 'z' - 'a') c = c + 'a';
3520 else if (c < 36) c = (c + '0') - ('z' - 'a') - 1;
3521 else if (c == 36) c = '-';
3522 else if (c == 37) c = '&';
3523 else if (c == 38) c = '_';
3524 else c = '.'; /* unknown */
3525 codingvector[5-k] = (char) c;
3527 codingvector[6] = '\0';
3533 double sclpnt (long x)
3537 pt = (double) x / 65536.0;
3538 pt = (double) ((int) (pt * 1000.0 + 0.5)) / 1000.0;
3543 void dvi_font_show(internal_font_number f, int suppressname)
3546 unsigned long checksum;
3547 char checksumvector[8];
3550 putc(' ', log_file);
3552 if (suppressname == 0)
3554 a = length(font_area[f]);
3555 l = length(font_name[f]);
3557 k = str_start[font_area[f]];
3559 memcpy(buffer, str_pool + k, length(font_area[f]));
3560 fwrite(buffer, sizeof(char), length(font_area[f]), log_file);
3562 k = str_start[font_name[f]];
3564 memcpy(buffer, str_pool + k, length(font_name[f]));
3565 fwrite(buffer, sizeof(char), length(font_name[f]), log_file);
3569 for (k = a + l; k < 16; k++)
3570 putc(' ', log_file);
3572 sprintf(buffer, "at %lgpt ", sclpnt(font_size[f]));
3573 fputs(buffer, log_file);
3575 if (suppressname == 0)
3579 for (k = n; k < 16; k++)
3580 putc(' ', log_file);
3582 checksum = (((font_check[f].b0) << 8 | font_check[f].b1) << 8 | font_check[f].b2) << 8 | font_check[f].b3;
3583 decode_fourty(checksum, checksumvector);
3584 fprintf(log_file, "encoding: %s..", checksumvector);
3587 putc('\n', log_file);
3589 /* Allocate table of indeces to allow sorting on font name */
3590 void show_font_info (void)
3592 int k, m, fcount, repeatflag;
3597 for (k = 1; k <= font_ptr; k++)
3604 fnumtable = (short *) malloc(fcount * sizeof(short));
3606 fprintf(log_file, "\nUsed %d font%s:\n", fcount, (fcount == 1) ? "" : "s");
3610 for (k = 1; k <= font_ptr; k++)
3612 fnumtable[fcount++] = (short) k;
3614 qsort ((void *)fnumtable, fcount, sizeof(short), &compare_fnt);
3618 for (m = 0; m < fcount; m++)
3622 if (compare_fnt_name(fnumtable[m-1], fnumtable[m]) == 0)
3628 dvi_font_show(fnumtable[m], repeatflag);
3631 free((void *)fnumtable);