OSDN Git Service

updated macros.
[putex/putex.git] / src / texsourc / local.c
index 4273079..364f925 100644 (file)
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301 USA.  */
 
-#ifdef _WINDOWS
-  #define NOCOMM
-  #define NOSOUND
-  #define NODRIVERS
-  #define STRICT
-  #pragma warning(disable:4115) // kill rpcasync.h complaint
-  #include <windows.h>
-  #define MYLIBAPI __declspec(dllexport)
-#endif
-
-#include "texwin.h"
-
-#pragma warning(disable:4996)
-#pragma warning(disable:4131) // old style declarator
-#pragma warning(disable:4135) // conversion between different integral types 
-#pragma warning(disable:4127) // conditional expression is constant
-
-#include <setjmp.h>
-
 #define EXTERN extern
 
 #include "texd.h"
 
 /* Most Y & Y changes are localized here -- init() */
 
-/* Note int main (int ac, char *av[]) is in texmf.c */
-/* and that calls main_program = texbody in itex.c => initialize */
-/* which in turn calls init here in local.c */
-/* which then calls init_commands here in local.c */ 
-
 #define USEOUREALLOC      /* 96/Jan/20 */
 
 #define USEMEMSET       /* 98/Jan/23 */
 
-/* #define PREALLOCHOLE */    /* make hole in heap at bottom debugging */
-
-/* #define CHECKALIGNMENT */  /* reactivated 95/Jan/7 */
-
-/* #define HEAPSHOW */      /* debugging 96/Jan/20 */
-
-/* #ifdef TRYANDOPEN */     /* debugging only */
-
-/* #define SHOWHEAPERROR */   /* debugging 96/Jan/10 */
-
-#ifndef _WINDOWS
-/* #define HEAPWALK */      /* debugging 96/Oct/22 */
-#endif
-
 #ifdef USEOUREALLOC
   #define REALLOC ourrealloc
 #else
   #define REALLOC realloc
 #endif
 
-#include <time.h>
-#include <malloc.h> /* _msize, _expand, HEAPOK et.c */
-#include <direct.h> /* for _getcwd() */
-
-#pragma warning(disable:4032) // different type when promoted
-#ifndef _WINDOWS
-  #include <conio.h>            /* for _getch() */
-#endif
-#pragma warning(default:4032) // different type when promoted
-
-
-/* Argument handling, etc.  */ /* from common.h - setup `main' in texmf.c */
-/* extern int gargc; */
-/* extern char **gargv; */
-
-int wantcopyrght=1;
-
-/* appears in reverse order in EXE file */
+int wantcopyrght = 1;
 
 char *compiletime  =  __TIME__;
 char *compiledate  =  __DATE__;
@@ -100,30 +45,20 @@ char *copyright    = "\nCopyright (C) 1993--2000 Y&Y, Inc.\n"
                      "it under the terms of the GNU General Public License as published by\n"
                      "the Free Software Foundation; either version 2 of the License, or\n"
                      "(at your option) any later version.\n\n  ";
-char *yandyversion = "2.2.3";
-char *application  = "Y&Y TeX"; /* 96/Jan/17 */
+char *yandyversion = "2.2.4";
+char *application  = "Y&Y TeX";
 char *tex_version  = "This is TeX, Version 3.14159265";
 
-/* #define COPYHASH 1890382 */
-/* #define COPYHASH 13862905 */
-/* #define COPYHASH 10558802 */
-/* #define COPYHASH 7254699 */
-/* #define COPYHASH 3950596 */
-/* #define COPYHASH 646493 */
-#define COPYHASH 12905299
-
 clock_t start_time, main_time, finish_time;
 
-char * dvi_directory = ""; /* user specified directory for dvi file */
-char * log_directory = ""; /* user specified directory for log file */
-char * aux_directory = ""; /* user specified directory for aux file */
-char * fmt_directory = ""; /* user specified directory for fmt file */
-char * pdf_directory = ""; /* user specified directory for pdf file */
+char * dvi_directory = "";
+char * log_directory = "";
+char * aux_directory = "";
+char * fmt_directory = "";
+char * pdf_directory = "";
 
 char * texpath = "";   /* path to executable - used if env vars not set */
 
-// #define MAXLINE 256
-
 char log_line[MAXLINE];  // used also in tex9.c
 
 int mem_spec_flag     = 0;    /* non-zero if `-m=...' was used */ 
@@ -134,7 +69,8 @@ bool reorder_arg_flag = true; /* put command line flags/arguments first */
 /* Mapping from Windows ANSI to DOS code page 850 96/Jan/20 */
 /* Used in tex0.c with wintodos[c-128]                      */
 
-unsigned char wintodos[128] = {
+unsigned char wintodos[128] =
+{
     0,   0,   0, 159,   0,   0,   0,   0,
    94,   0,   0,   0,   0,   0,   0,   0,
     0,  96,  39,   0,   0,   7,   0,   0,
@@ -156,13 +92,14 @@ unsigned char wintodos[128] = {
 void show_usage (void)
 {
   char * s = log_line;
+
   sprintf (s, "\n"
       " yandytex [-?ivnwdrzpK] [-m=ini_mem] [-e=hyph_size] [-h=trie_size]\n"
       "          [-x=xchr_file] [-k=key_file] [-o=dvi_dir] [-l=log_dir] [-a=aux_dir]\n"
       "          [+format_file] [tex_file]\n\n"
-      "    -?    show this usage summary\n"
-      "    -i    start up as iniTeX (create format file)\n"
-      "    -v    be verbose (show implementation version number)\n"
+      "    --help    -?    show this usage summary\n"
+      "    --initex  -i    start up as iniTeX (create format file)\n"
+      "    --verbose -v    be verbose (show implementation version number)\n"
       "    -n    do not allow `non ASCII' characters in input files (complain instead)\n"
       "    -w    do not show `non ASCII' characters in hexadecimal (show as is)\n"
       "    -d    do not allow DOS style file names - i.e. do not convert \\ to /\n"
@@ -176,8 +113,7 @@ void show_usage (void)
       "    -k    use `key replacement' defined in file\n"
       "    -o    write DVI file in specified directory (default current directory)\n"
       "    -l    write LOG file in specified directory (default current directory)\n"
-      "    -a    write AUX file in specified directory (default current directory)");
-  strcat(s, "\n");
+      "    -a    write AUX file in specified directory (default current directory)\n");
   show_line(log_line, 1);
 
 #ifndef _WINDOWS
@@ -196,9 +132,8 @@ void show_usage (void)
 
 /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
 
-/* Sep 27 1990 => 1990 Sep 27 */
-/* 012456789      0123456789  */
-
+// Sep 27 1990 => 1990 Sep 27
+// 012456789      0123456789
 void scivilize (char * date)
 {
   int k;
@@ -220,8 +155,7 @@ void scivilize (char * date)
   return;
 }
 
-/* Thu Sep 27 06:26:35 1990 => 1990 Sep 27 06:26:35 */
-
+// Thu Sep 27 06:26:35 1990 => 1990 Sep 27 06:26:35
 void lcivilize (char * date)
 {
   int k;
@@ -242,7 +176,6 @@ void lcivilize (char * date)
   return;
 }
 
-// void stamp_it (FILE *outfile)
 // now writes result into given buffer
 void stamp_it (char *s)
 {
@@ -260,7 +193,7 @@ void stampcopy (char *s)
 {
   if (wantcopyrght)
   {
-    sprintf(s, "%s %s", copyright, www);    /* 99/Oct/25 */
+    sprintf(s, "%s %s", copyright, www);
   }
 }
 
@@ -275,19 +208,8 @@ void read_xchr_sub (FILE * xchr_input)
   int k, from, to, count = 0;
   char *s;
 
-#ifdef USEMEMSET
-  memset (xchr, NOTDEF, MAXCHRS);           /* mark unused */
-#else
-  for (k = 0; k < MAXCHRS; k++)
-    xchr[k] = -1; /* mark unused */
-#endif
-
-#ifdef USEMEMSET
-  memset (xord, NOTDEF, MAXCHRS);           /* mark unused */
-#else
-  for (k = 0; k < MAXCHRS; k++)
-    xord[k] = -1;  /* mark unused */
-#endif
+  memset (xchr, NOTDEF, MAXCHRS);
+  memset (xord, NOTDEF, MAXCHRS);
 
 #ifdef ALLOCATEBUFFER
   while (fgets(buffer, current_buf_size, xchr_input) != NULL)
@@ -303,9 +225,9 @@ void read_xchr_sub (FILE * xchr_input)
 
     if (from >= 0 && from < MAXCHRS && to >= 0 && to < MAXCHRS)
     {
-      if (xchr[from]== (unsigned char) NOTDEF)
+      if (xchr[from] == (unsigned char) NOTDEF)
       {
-        xchr[from]= (unsigned char) to;
+        xchr[from] = (unsigned char) to;
       }
       else
       {
@@ -313,33 +235,33 @@ void read_xchr_sub (FILE * xchr_input)
         show_line(log_line, 0);
       }
 
-      if (xord[to]== NOTDEF)
+      if (xord[to] == NOTDEF)
       {
-        xord[to]= (unsigned char) from;
+        xord[to] = (unsigned char) from;
       }
       else
       {
         sprintf(log_line, "NOTE: %s collision: %d => %d, %d\n", "xord", to, xord[to], from);
         show_line(log_line, 0);
       }
+
       count++;
     }
   }
 
-/*  now fill in the gaps */ /* not clear this is a good idea ... */
   for (k = 0; k < MAXCHRS; k++)
   {
-    if (xchr[k]== NOTDEF)   /* if it has not been filled */
+    if (xchr[k] == NOTDEF)   /* if it has not been filled */
     {
-      if (xord[k]== NOTDEF) /* see whether used already */
+      if (xord[k] == NOTDEF) /* see whether used already */
       {
-        xchr[k]= (unsigned char) k; /* no, so make identity */
-        xord[k]= (unsigned char) k; /* no, so make identity */
+        xchr[k] = (unsigned char) k; /* no, so make identity */
+        xord[k] = (unsigned char) k; /* no, so make identity */
       }
     }
   }
 
-  xchr[NOTDEF]= NOTDEF;         /* fixed point of mapping */
+  xchr[NOTDEF] = NOTDEF;         /* fixed point of mapping */
 
   if (trace_flag)
   {
@@ -348,7 +270,7 @@ void read_xchr_sub (FILE * xchr_input)
 
     for (k = 0; k < MAXCHRS; k++)
     {
-      if (xchr[k]!= NOTDEF)
+      if (xchr[k] != NOTDEF)
       {
         sprintf(log_line, "%d => %d\n", k, xchr[k]);
         show_line(log_line, 0);
@@ -367,34 +289,45 @@ void read_repl_sub (FILE * repl_input)
   int charnum[10];
   char *s, *t;
   
-#ifdef USEMEMSET
   memset(replacement, 0, MAXCHRS * sizeof(replacement[ 0]));
-#else
-  for (k = 0; k < MAXCHRS; k++)
-    replacement[k] = NULL;
-#endif
 
-  while (fgets(buffer, PATH_MAX, repl_input) != NULL) {
+
+  while (fgets(buffer, PATH_MAX, repl_input) != NULL)
+  {
     if (*buffer == '%' || *buffer == ';' || *buffer == '\n')
       continue;
 
     if ((m = sscanf (buffer, "%d%n %s", &chrs, &n, &charname)) == 0)
       continue;
-    else if (m == 2) {
-      if (*charname == '"') {   /* deal with quoted string "..." */
+    else if (m == 2)
+    {
+      if (*charname == '"')   /* deal with quoted string "..." */
+      {
         s = buffer + n;
         t = charname;
-        while (*s != '"' && *s != '\0') s++;  /* step up to " */
-        if (*s++ == '\0') continue;       /* sanity check */
-        while (*s != '\0') {  
-          if (*s == '"') {
+
+        while (*s != '"' && *s != '\0')
+          s++;  /* step up to " */
+
+        if (*s++ == '\0')
+          continue;       /* sanity check */
+
+        while (*s != '\0')
+        {
+          if (*s == '"')
+          {
             s++;            /* is it "" perhaps ? */
-            if (*s != '"') break;   /* no, end of string */
+
+            if (*s != '"')
+              break;   /* no, end of string */
           }
+
           *t++ = *s++;          /* copy over */
         }
+
         *t = '\0';              /* and terminate */
       }
+
       if (chrs >= 0 && chrs < MAXCHRS)
         replacement[chrs] = xstrdup(charname);
     }
@@ -414,10 +347,15 @@ void read_repl_sub (FILE * repl_input)
       show_line(log_line, 1);
     }
   }
-  if (trace_flag) {                  /* debugging output */
+
+  if (trace_flag)
+  {
     show_line("Key replacement table\n", 0);
-    for (k = 0; k < MAXCHRS; k++) {
-      if (replacement[k] != NULL) {
+
+    for (k = 0; k < MAXCHRS; k++)
+    {
+      if (replacement[k] != NULL)
+      {
         sprintf(log_line, "%d\t%s\n", k, replacement[k]);
         show_line(log_line, 0);
       }
@@ -427,7 +365,6 @@ void read_repl_sub (FILE * repl_input)
 
 /* Following used both to read xchr[] file and key replacement file */
 /* the flag is 0 for -x=... and the flag is 1 for -k=... */
-
 int read_xchr_file (char *filename, int flag, char *argv[])
 {
   FILE *pinput;
@@ -612,49 +549,6 @@ void testfloating (void)
 }
 #endif
 
-/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
-
-char *debugfile;      /* NULL or name of file to try and open */
-
-#ifdef SHOWHEAPERROR
- char *heapstrings[] =
- {
-    "", "Empty", "OK", "Bad Begin", "Bad Node", "End", "Bad Pointer"
- };
-#endif
-
-/* Attempt to get at problem with eqtb ... temporarily abandoned */
-#ifdef CHECKEQTB
-void check_eqtb (char *act)
-{
-  int k, count=0;
-  memory_word *eqtb = zeqtb;
-
-  for (k = hash_size + 780 + hash_extra; k < hash_size + 780 + eqtb_extra; k++)
-  {
-    if (eqtb[k].cint != 0)
-    {
-      if (count == 0)
-      {
-        show_char('\n');
-        show_line("EQTB ", 0);
-      }
-
-      sprintf(log_line, "%d ", k);
-      show_line(log_line, 0);
-
-      if (count++ > 256)
-        break;
-    }
-  }
-
-  if (count != 0)
-    show_char('\n');
-}
-#endif
-
-/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
-
 #define MAXSPLITS 3
 
 /* ad hoc default minimum growth in memory realloc is 62% */
@@ -664,84 +558,9 @@ int total_allocated = 0;  /* total memory allocated so far */
 int ini_max_address = 0;  /* maximum address when starting */
 int max_address     = 0;  /* maximum address seen in allocated memory */
 
-/* see texd.h */
-
-// DON'T USE THIS in DLL VERSION
-
-#ifndef _WINDOWS
-#ifdef HEAPWALK
-unsigned int heapthreshold = 0; /* smallest size block interested in ... */
-
-unsigned int heap_dump (FILE *output, int verbose)
-{
-  unsigned int total=0;
-  struct _heapinfo hinfo;
-  int heapstatus;
-  int end_block=0;
-  int n;
-
-  if (verbose)
-    fprintf(output, "HEAP DUMP:\n");
-
-/*  if ((n = _heapchk ()) != _HEAPOK) { */
-  n = _HEAPOK;
-#ifdef SHOWHEAPERROR
-  n = _heapchk();
-#endif
-  if (n != _HEAPOK)
-  {
-    fprintf(stderr, "WARNING: Heap corrupted (%d)\n", n);
-#ifdef SHOWHEAPERROR
-    fprintf(stderr, "HEAP %s (%s)\n", heapstrings[-n], "heap_dump");
-#endif
-  }
-  hinfo._pentry = NULL;
-
-  while ((heapstatus = _heapwalk(&hinfo)) == _HEAPOK)
-  {
-    if (end_block > 0 && (int) hinfo._pentry > end_block + 1024)
-    {
-//      if (verbose) printf("GAP of %d bytes!\n", (int) hinfo._pentry - end_block);
-    }
-    end_block = (int) hinfo._pentry + hinfo._size;
-    if (hinfo._useflag == _USEDENTRY) total += hinfo._size;
-    if (hinfo._size >= heapthreshold && verbose)
-      fprintf(output, "%6s block at %p (%7d) of size %6X (%7d) => (%7d)\n",
-          (hinfo._useflag == _USEDENTRY ? "USED" : "...."),
-          hinfo._pentry, hinfo._pentry, hinfo._size, hinfo._size,
-          end_block);
-  }
-
-  switch (heapstatus)
-  {
-    case _HEAPEMPTY:
-      if (verbose) fprintf(output, "OK - empty heap\n");
-      break;
-    case _HEAPEND:
-      if (verbose) fprintf(output, "OK - end of heap (%u bytes used)\n", total);
-      break;
-    case _HEAPBADPTR:
-      fprintf(output, "ERROR - %s\n", "bad pointer to heap");
-      break;
-    case _HEAPBADBEGIN:
-      fprintf(output, "ERROR - %s\n", "bad start of heap");
-      break;
-    case _HEAPBADNODE:
-      fprintf(output, "ERROR - %s\n", "bad node in heap");
-      break;
-  }
-  return total;
-}
-#endif
-#endif
 
 void show_maximums (FILE *output)
 {
-#ifdef HEAPWALK
-  unsigned heaptotal = 0;           /* no longer used */
-  heaptotal = heap_dump(stdout, 0);      /* 94/Apr/3 */
-#endif
-
   sprintf(log_line, "Max allocated %d --- max address %d\n", total_allocated, max_address);
 //  if (output != NULL) fputs(log_line, output); // log file
 //  else if (flag == 0) show_line(log_line, 0); // informative
@@ -774,16 +593,7 @@ void *ourrealloc (void *old, size_t new_size)
 
   if (old_size >= new_size && old_size < new_size + 4)
     return old;
-/*  _heapmin(); */  /* release unused heap space to the system - no op ? */
-#ifdef HEAPSHOW
-  if (trace_flag)
-  {
-    show_line("BEFORE REALLOC: \n", 0);
-#ifdef HEAPWALK
-    (void) heap_dump(stdout, 1);     /* debugging 96/Jan/18 */
-#endif
-  }
-#endif
+
   mnew = _expand (old, new_size);      /* first try and expand in place */
 
   if (mnew != NULL)
@@ -799,15 +609,7 @@ void *ourrealloc (void *old, size_t new_size)
 /*  *********************************************************************** */
 /*  do this if you want to call the real realloc next -  */
   mnew = realloc (old, new_size);
-#ifdef HEAPSHOW
-  if (trace_flag)
-  {
-    show_line("AFTER REALLOC: \n", 0);
-#ifdef HEAPWALK
-    (void) heap_dump(stdout, 1);     /* debugging 96/Jan/18 */
-#endif
-  }
-#endif
+
   if (mnew != NULL)
     return mnew;
 /*  we are screwed typically if we ever drop through here - no more space */
@@ -834,24 +636,11 @@ void memory_error (char *s, int n)
   {
     fprintf(log_file, "\n! Unable to allocate %d bytes for %s\n", n, s);
     show_maximums(log_file);
-
-#ifdef HEAPWALK
-    if (heap_flag)
-      (void) heap_dump(log_file, 1);
-#endif
   }
 
   sprintf(log_line, "\n! Unable to allocate %d bytes for %s\n", n, s);
   show_line(log_line, 1);
   show_maximums(stderr);
-
-#ifdef HEAPWALK
-  if (heap_flag)
-    (void) heap_dump(stderr, 1);
-#endif
-/*  exit (1); */      /* 94/Jan/22 */
-/*  return to let TeX do its thing (such as complain about runaway) */
-/*  don't set abort_flag here */
 }
 
 void trace_memory (char *s, int n)
@@ -881,11 +670,6 @@ void probe_show (void)
 {
   probe_memory();
   show_maximums(stdout);
-
-#ifdef HEAPWALK
-  if (heap_flag)
-    (void) heap_dump(stdout, 1);
-#endif
 }
 
 size_t roundup (size_t n)
@@ -1117,6 +901,7 @@ memory_word *allocate_main_memory (int size)
 
   if (trace_flag)
     probe_show();     /* 94/Mar/25 */
+
   return zzzaa;             /* same as zmem, mem 94/Jan/24 */
 }
 #endif  /* end of ALLOCATEMAIN */
@@ -1281,7 +1066,7 @@ memory_word *realloc_main (int losize, int hisize)
 #ifdef ALLOCATEFONT
 /* font_mem_size = 10000L ==> font_info array 100k * 8 = 800 kilobytes */
 
-int current_font_mem_size=0;
+int current_font_mem_size = 0;
 
 /* fmemoryword can be either halfword or memory_word */
 fmemoryword * realloc_font_info (int size)
@@ -1367,9 +1152,9 @@ int current_pool_size = 0;
 packed_ASCII_code * realloc_str_pool (int size)
 {
   int k, minsize;
-  int newsize=0;
-  int n=0;
-  packed_ASCII_code *newstrpool=NULL;
+  int newsize = 0;
+  int n = 0;
+  packed_ASCII_code *newstrpool = NULL;
 
   if (trace_flag)
   {
@@ -1400,24 +1185,31 @@ packed_ASCII_code * realloc_str_pool (int size)
     size = size / 2;          /* else can retry smaller */
   }
 
-  if (newstrpool == NULL) {
+  if (newstrpool == NULL)
+  {
     memory_error("string pool", n);
     return str_pool;           /* try and continue !!! */
   }
+
   str_pool = newstrpool;
   update_statistics ((int) str_pool, n, current_pool_size);
   current_pool_size = newsize;
-  if (trace_flag) {
+
+  if (trace_flag)
+  {
     sprintf(log_line, "New Address %s == %d\n", "string pool", str_pool);
     show_line(log_line, 0);
   }
-  if (trace_flag)  probe_show();     /* 94/Mar/25 */
+  
+  if (trace_flag)
+    probe_show();     /* 94/Mar/25 */
+
   return str_pool;
 }
 #endif
 
 #ifdef ALLOCATESTRING
-int current_max_strings=0;
+int current_max_strings = 0;
 
 pool_pointer *realloc_str_start (int size)
 {
@@ -1475,9 +1267,9 @@ pool_pointer *realloc_str_start (int size)
 #ifdef ALLOCATEINI
 
 /* returns -1 if it fails */
-
+/* size == trie_size */
 int allocate_ini (int size)
-{    /* size == trie_size */
+{
   int n, nl, no, nc, nr, nh, nt;
     nh = nr = nl = (size + 1) *  sizeof(trie_pointer);
     no = (size + 1) *  sizeof(trie_op_code);
@@ -1522,7 +1314,7 @@ int allocate_ini (int size)
 #endif
 
 #ifdef ALLOCATESAVESTACK
-int current_save_size=0;
+int current_save_size = 0;
 
 memory_word *realloc_save_stack (int size)
 {
@@ -1530,20 +1322,29 @@ memory_word *realloc_save_stack (int size)
   int n=0, newsize=0;
   memory_word *newsave_stack=NULL;
 
-  if (trace_flag) {
+  if (trace_flag)
+  {
     sprintf(log_line, "Old Address %s == %d\n", "save stack", save_stack);
     show_line(log_line, 0);
   }
-  if (current_save_size == save_size) {  /* arbitrary limit */
+
+  if (current_save_size == save_size)  /* arbitrary limit */
+  {
 /*    memory_error ("save stack", (save_size + 1) * sizeof(memory_word)); */
 /*    exit (1); */
     return save_stack;       /* let TeX handle the error */
   }
+
   minsize =  current_save_size / 100 * percent_grow;
-  if (size < minsize) size = minsize;
-  if (size < initial_save_size) size = initial_save_size;
 
-  for (k = 0; k < MAXSPLITS; k++) {
+  if (size < minsize)
+    size = minsize;
+
+  if (size < initial_save_size)
+    size = initial_save_size;
+
+  for (k = 0; k < MAXSPLITS; k++)
+  {
     newsize = current_save_size + size;
     if (newsize > save_size) newsize = save_size;
     n = (newsize + 1) * sizeof (memory_word); /* save_stack[save_size + 1] */
@@ -1554,26 +1355,31 @@ memory_word *realloc_save_stack (int size)
     size = size / 2;          /* else can retry smaller */
   }
 
-  if (newsave_stack == NULL) {
+  if (newsave_stack == NULL)
+  {
     memory_error("save stack", n);
     return save_stack;           /* try and continue !!! */
   }
+
   save_stack = newsave_stack;
   update_statistics ((int) save_stack, n, current_save_size);
   current_save_size = newsize;
-  if (trace_flag) {
+
+  if (trace_flag)
+  {
     sprintf(log_line, "Current%s %d\n", "save_size", current_save_size);
     show_line(log_line, 0);
     sprintf(log_line, "New Address %s == %d\n", "save stack", save_stack);
     show_line(log_line, 0);
   }
+
   if (trace_flag) probe_show();      /* 94/Mar/25 */
   return save_stack;
 }
 #endif
 
 #ifdef ALLOCATEINPUTSTACK
-int current_stack_size=0;       /* input stack size */
+int current_stack_size = 0;       /* input stack size */
 
 in_state_record *realloc_input_stack (int size)
 {
@@ -1624,7 +1430,7 @@ in_state_record *realloc_input_stack (int size)
 #endif
 
 #ifdef ALLOCATENESTSTACK
-int current_nest_size=0;        /* current nest size */
+int current_nest_size = 0;        /* current nest size */
 
 list_state_record *realloc_nest_stack (int size)
 {
@@ -1726,7 +1532,7 @@ halfword *realloc_param_stack (int size)
 #endif
 
 #ifdef ALLOCATEBUFFER
-int current_buf_size=0;
+int current_buf_size = 0;
 
 ASCII_code *realloc_buffer (int size)
 {
@@ -1781,58 +1587,6 @@ ASCII_code *realloc_buffer (int size)
 }
 #endif
 
-/* we used to allocate this one only to reduce the size of the PE file */
-/* not used anymore - NO advantage */
-
-#ifdef ALLOCATEDVIBUF
-eight_bits *allocatedvibuf (int size)
-{
-  eight_bits *dvi_buf;
-  int n;
-
-  n = (size + 1) * sizeof(eight_bits);
-  if (trace_flag) trace_memory("dvi_buf", n);
-  dvi_buf = (eight_bits *) malloc (roundup(n));
-  if (dvi_buf == NULL) {
-    memory_error("dvi_buf", n);
-    return NULL;
-  }
-  if (trace_flag) {
-    sprintf(log_line, "Address dvi_buf %d\n", dvi_buf);
-    show_line(log_line, 0);
-  }
-  update_statistics ((int) dvi_buf, n, 0);
-  if (trace_flag)  probe_show();     /* 94/Mar/25 */
-  return dvi_buf;
-}
-#endif
-
-/* we used to allocate this one only to reduce the size of the PE file */
-/* it can be done without loss in performance, since register eqtb = zeqtb */
-#ifdef ALLOCATEZEQTB
-memory_word *allocatezeqtb (int k)
-{
-  memory_word *zeqtb;
-  int n;
-
-  n = k * sizeof (memory_word);  /* 13507 * 8 = 108 kilobytes */
-  if (trace_flag)  trace_memory("eqtb", n);
-  zeqtb = (memory_word *) malloc (roundup(n));
-  if (zeqtb == NULL)  {
-    memory_error("eqtb", n);
-//    exit (1);           /* serious error */   
-    return NULL;
-  }
-  if (trace_flag) {
-    sprintf(log_line, "Address zeqtb %d\n", zeqtb);
-    show_line(log_line, 0);
-  }
-  update_statistics ((int) zeqtb, n, 0);
-  if (trace_flag)  probe_show();     /* 94/Mar/25 */
-  return zeqtb;
-}
-#endif
-
 /* here is the main memory allocation routine -- calls the above */
 /* returns -1 if it fails */
 /* allocate rather than static 93/Nov/26 */
@@ -2028,26 +1782,21 @@ int allocate_memory (void)
 int free_memory (void)
 {
   int n;
-  unsigned heaptotal=0;
+  unsigned heaptotal = 0;
 /*  unsigned total; */
 
   if (trace_flag) show_line("free_memory ", 0);
 
-#ifdef CHECKEQTB
-  if (debug_flag) check_eqtb("free_memory");
-#endif
-  if (verbose_flag || trace_flag) show_maximums(stdout); 
-#ifdef HEAPWALK
-  if (heap_flag) (void) heap_dump(stdout, 1);
-#endif
-  if (trace_flag) {
-#ifdef HEAPWALK
-    heaptotal = (void) heap_dump(stdout, 0);
-#endif
+  if (verbose_flag || trace_flag)
+    show_maximums(stdout); 
+
+  if (trace_flag)
+  {
     sprintf(log_line, "Heap total: %u bytes --- max address %u\n", 
         heaptotal, max_address);
     show_line(log_line, 0);
   }
+
   if (trace_flag) {
     sprintf(log_line, "Main Memory: variable node %d (%d - %d) one word %d (%d - %d)\n",
       lo_mem_max - mem_min, mem_min, lo_mem_max, mem_end  - hi_mem_min, hi_mem_min, mem_end);
@@ -2075,7 +1824,8 @@ int free_memory (void)
 #endif
 /*  only free memory if safe ... additional check */
 #ifdef ALLOCATEINI
-  if (is_initex) {
+  if (is_initex)
+  {
     if (trie_taken != NULL) free(trie_taken);
     if (trie_hash  != NULL) free(trie_hash);
     if (trie_r     != NULL) free(trie_r);
@@ -2154,7 +1904,8 @@ int free_memory (void)
   format_file = string_file = source_direct = NULL;
   if (dvi_file_name != NULL) free(dvi_file_name);
   if (log_file_name != NULL) free(log_file_name);
-  log_file_name = dvi_file_name = NULL;       /* 00/Jun/18 */
+  if (pdf_file_name != NULL) free(pdf_file_name);
+  pdf_file_name = log_file_name = dvi_file_name = NULL;       /* 00/Jun/18 */
   return 0;
 }
 
@@ -2172,9 +1923,9 @@ bool prime (int x)
   return true;
 }
 
-int quitflag=0;
-bool show_use=false;
-bool floating=false;
+int quitflag  = 0;
+bool show_use = false;
+bool floating = false;
 
 void complainarg (int c, char *s)
 {
@@ -2189,7 +1940,7 @@ void complainarg (int c, char *s)
 
 /* only  01234567.9 still left to take ... maybe recycle u */
 
-char *allowedargs="+bcdfijnpqrstvwyzABCDFGIJKLMNOPQRSTVWXYZ23456789?a=e=g=h=k=l=m=o=u=x=E=H=P=U=";
+char *allowedargs = "+bcdfijnpqrstvwyzABCDFGIJKLMNOPQRSTVWXYZ023456789?a=e=g=h=k=l=m=o=u=x=E=H=P=U=";
 
 /* char takeargs="gmueoazhluEH"; */ /* subset that takes args! needed here */
 
@@ -2277,11 +2028,14 @@ int test_align (int address, int size, char *name)
 
 void check_fixed_align (int flag)
 {
-  if (test_align ((int) &mem_top, 4, "FIXED ALIGNMENT")) {
+  if (test_align ((int) &mem_top, 4, "FIXED ALIGNMENT"))
+  {
     show_line("PLEASE RECOMPILE ME!\n", 1);
   }
 #ifdef CHECKALIGNMENT
-  if (!flag) return;
+  if (!flag)
+    return;
+
   test_align ((int) &mem_top, 4, "mem_top");
   test_align ((int) &mem_max, 4, "mem_max");
   test_align ((int) &mem_min, 4, "mem_min");
@@ -2402,7 +2156,8 @@ void check_fixed_align (int flag)
 #endif
 }
 
-void check_alloc_align (int flag) {
+void check_alloc_align (int flag)
+{
   if (test_align ((int) eqtb, sizeof(eqtb[0]), "ALLOCATED ALIGNMENT"))
     show_line("PLEASE RECOMPILE ME!\n", 1);
 #ifdef CHECKALIGNMENT
@@ -2431,61 +2186,28 @@ void check_alloc_align (int flag) {
 #endif
 }
 
-#ifdef HEAPSHOW
-void showaddresses (void)
-{         /* 96/Jan/20 */
-  int c;
-  int d;
-  sprintf(log_line, "STACK %d %d (grows %s) ", &c, &d, (&d > &c) ? "upward" : "downward");
-  show_line(log_line, 0);
-  sprintf(log_line, "eqtb %d hash %d ", zeqtb, zzzae);
-  show_line(log_line, 0);
-  sprintf(log_line, "dvi_buf %d xchr %d xord %d nest %d\n", zdvibuf, xchr, xord, nest);
-  show_line(log_line, 0);
-  sprintf(log_line, "save_stack %d input_stack %d line_stack %d param_stack %d\n",
-       save_stack, input_stack, line_stack, param_stack);
-  show_line(log_line, 0);
-  sprintf(log_line, "font_check %d font_size %d font_dsize %d font_params %d font_name %d\n",
-       font_check, font_size, font_dsize, font_params, font_name);
-  show_line(log_line, 0);
-  sprintf(log_line, "main %d font_info %d str_pool %d str_start %d hyph_word %d hyph_list %d\n",
-      zmem, font_info, str_pool, str_start, hyph_word, hyph_list);
-  show_line(log_line, 0);
-}
-#endif
-
-/* *** *** *** *** *** *** *** NEW APPROACH TO `ENV VARS' *** *** *** *** */
-
-/* grab `env var' from `dviwindo.ini' - or from DOS environment 94/May/19  */
-/* controlled by USEDVIWINDOINI environment variable            94/June/19 */
-
-bool usedviwindo        = true;               /* use [Environment] section in `dviwindo.ini' */
 bool backwardflag       = false;              /* don't cripple all advanced features */
 bool shorten_file_name  = false;              /* don't shorten file names to 8+3 for DOS */
-char *inifilename       = "dviwindo.ini";     /* name of ini file we look for */
-char *dviwindo          = "";                 /* full file name for dviwindo.ini with path */
-char *envsection        = "[Environment]";    /* Env var section in `dviwindo.ini' */
-char *wndsection        = "[Window]";         /* Window section in `dviwindo.ini' */
-char *workdirect        = "WorkingDirectory"; /* key in [Window] section */
 bool usesourcedirectory = true;               /* use source file directory as local when WorkingDirectory is set */
 bool workingdirectory   = false;              /* if working directory set in ini */
 
 /* cache to prevent allocating twice in a row */
 
-char *lastname=NULL, *lastvalue=NULL;
+char *lastname = NULL, *lastvalue = NULL;
 
 /* get value of env var - try first in dviwindo.ini then DOS env */
 /* returns allocated string -- these strings are not freed again */
 /* is it safe to do that now ? 98/Jan/31 */
 char *grabenv (char *varname)
 {
-  char line[PATH_MAX];
-  FILE *pinput;
   char *s;
-  int m, n;
 
-  if (varname == NULL) return NULL;    /* sanity check */
-  if (*varname == '\0') return NULL;   /* sanity check */
+  if (varname == NULL)
+    return NULL;
+
+  if (*varname == '\0')
+    return NULL;
+
 /*  speedup to avoid double lookup when called from set_paths in ourpaths.c */
 /*  if (lastname != NULL && strcmp(lastname, varname) == 0) { */
   if (lastname != NULL && _strcmpi(lastname, varname) == 0)
@@ -2495,72 +2217,28 @@ char *grabenv (char *varname)
       sprintf(log_line, "Cache hit: %s=%s\n", lastname, lastvalue);
       show_line(log_line, 0);
     }
-/*    return lastvalue; */        /* save some time here */
+
     return xstrdup(lastvalue);
-/*    duplicate so can free safely 98/Jan/31 */
-  }
-
-/*  hmm, following was not xstrdup(...) */ /* not cached */
-  if (usedviwindo == 0 || *dviwindo == '\0') {
-/*    return getenv(varname); */
-    s = getenv(varname);
-    if (s == NULL) return NULL;
-    else return xstrdup(s);       /* make safe 98/Jan/31 */
-  }
-
-  if (share_flag == 0) pinput = fopen(dviwindo, "r");
-  else pinput = _fsopen(dviwindo, "r", share_flag);
-
-  if (pinput != NULL) {
-    m = strlen(envsection);
-/*    search for [Environment] section */ /* should be case insensitive */
-    while (fgets (line, sizeof(line), pinput) != NULL) {
-      if (*line == ';') continue;
-      if (*line == '\n') continue;
-      if (_strnicmp(line, envsection, m) == 0) {  /* 98/Jan/31 */
-/*        search for varname=... */ /* should be case sensitive ? */
-        n = strlen(varname);
-        while (fgets (line, sizeof(line), pinput) != NULL) {
-          if (*line == ';') continue;
-          if (*line == '[') break;
-/*          if (*line == '\n') break; */  /* ??? */
-          if (*line <= ' ') continue;   /* 95/June/23 */
-/*          if (strncmp(line, varname, n) == 0 && */
-          if (_strnicmp(line, varname, n) == 0 &&
-            *(line+n) == '=') { /* found it ? */
-              (void) fclose (pinput);
-/*              flush trailing white space */
-              s = line + strlen(line) - 1;
-              while (*s <= ' ' && s > line) *s-- = '\0';
-              if (trace_flag) { /* DEBUGGING ONLY */
-                sprintf(log_line, "%s=%s\n", varname, line+n+1);
-                show_line(log_line, 0);
-              }
-              s = line+n+1;
-              if (lastname != NULL) free(lastname);
-              lastname = xstrdup (varname);
-              if (lastvalue != NULL) free(lastvalue);
-              lastvalue = xstrdup(s);
-              return xstrdup(s);    /* 98/Jan/31 */
-          }   /* end of matching varname */
-        }     /* end of while fgets */
-/*        break; */ /* ? not found in designated section */    
-      }       /* end of search for [Environment] section */
-    }
-    (void) fclose (pinput);
-  }           /* end of if fopen */
-  s = getenv(varname);    /* failed, so try and get from environment */
-/*  if (s != NULL) return s;  */
-  if (s != NULL) {
-/*    sdup = xstrdup(s); */   /* just to be safe --- 1995/Jan/31 */
-    if (lastname != NULL) free(lastname);
+  }
+
+  s = getenv(varname);
+
+  if (s != NULL)
+  {
+    if (lastname != NULL)
+      free(lastname);
+
     lastname = xstrdup (varname);
-    if (lastvalue != NULL) free(lastvalue);
-    lastvalue = xstrdup(s);   /* remember in case asked again ... */
-/*    return sdup; */
-    return xstrdup(s);    /* 98/Jan/31 */
+
+    if (lastvalue != NULL)
+      free(lastvalue);
+
+    lastvalue = xstrdup(s);
+
+    return xstrdup(s);
   }
-  else return NULL;   /* return NULL if not found anywhere */
+  else
+    return NULL;
 }
 
 /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
@@ -2569,9 +2247,12 @@ void flush_trailing_slash (char *directory)
 {
   char *s;
 /*  flush trailing \ or / in directory, if any 1993/Dec/12 */
-  if (strcmp(directory, "") != 0) {
+  if (strcmp(directory, "") != 0)
+  {
     s = directory + strlen(directory) - 1;
-    if (*s == '\\' || *s == '/') *s = '\0';
+
+    if (*s == '\\' || *s == '/')
+      *s = '\0';
   }
 }
 
@@ -2613,32 +2294,32 @@ void knuthify (void)
   knuth_flag = true;       /* so other code can know about this */
 } /* end of knuthify */
 
-/* following have already been used up */
-
-/* abcdefghijklmnopqrstuvwxyz */
-
-/* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
-
-/* ........ */
-
-int nohandler = 0;    /* experiment to avoid Ctrl-C interrupt handler */
-
 /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
 
 /* following made global so analyze_flag can be made separate procedure */
 
-// char *xchrfile=""; /* save space use xstrdup */
-char *xchrfile = NULL; /* save space use xstrdup */
-// char *replfile="";/* save space use xstrdup */
-char *replfile = NULL;/* save space use xstrdup */
-
-/* abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ */
-
-/* analyze command line flag or argument */
-/* c is the flag letter itself, while optarg is start of arg if any */
-
-/* when making changes, revise allowedargs */
-
+char * xchrfile = NULL;
+char * replfile = NULL;
+
+char * short_options = "viKLZMdp2t?u";
+
+static struct option long_options[] =
+{
+  //{"interaction",   1, 0, 0},
+  {"verbose",       0, 0, 'v'},
+  {"initex",        0, 0, 'i'},
+  {"knuthify",      0, 0, 'K'},
+  {"cstyle",        0, 0, 'L'},
+  {"showtfm",       0, 0, 'Z'},
+  {"showmissing",   0, 0, 'M'},
+  {"deslash",       0, 0, 'd'},
+  {"patterns",      0, 0, 'p'},
+  {"suppressflig",  0, 0, '2'},
+  {"trace",         0, 0, 't'},
+  {"help",          0, 0, '?'},
+  {"usage",         0, 0, 'u'},
+  {NULL,            0, 0, 0}
+};
 
 int analyze_flag (int c, char *optarg)
 {
@@ -2726,9 +2407,6 @@ int analyze_flag (int c, char *optarg)
       break;
 /*  case 'X':  truncate_long_lines = false; */ /* 98/Feb/2 */
               /* break; */
-    case 'W':
-      usedviwindo = false; /* 94/May/19 */
-      break;
     case 'J':
       show_line_break_stats = false; /* 96/Feb/8 */
       break;
@@ -2744,13 +2422,6 @@ int analyze_flag (int c, char *optarg)
     case '2':
       suppress_f_ligs = true; /* 99/Jan/5 f-lig */
       break;
-/* following are pretty obscure */
-/*  case 'y': cache_file_flag = false; */ /* 96/Nov/16 */
-/*            break; */
-/*  case 'r': return_flag = false; */ /* flipped 93/Nov/18 */
-/*            break; */
-/*  case 'z': trimeof = false; */ /* 93/Nov/24 */
-/*            break; */
     case 'z':
       full_file_name_flag = false; // 00 Jun 18
       break;
@@ -2901,6 +2572,10 @@ int analyze_flag (int c, char *optarg)
       if (strcmp(dvi_directory, "") == 0)
         complainarg(c, optarg);
       break;
+    case '0':
+      if (optarg == 0)
+        pdf_output_flag = true;
+      break;
     case 'l':
       if (optarg == 0)
         log_directory = "";
@@ -2926,30 +2601,6 @@ int analyze_flag (int c, char *optarg)
   return 0;
 }
 
-/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
-//char *yytexcmd="yandytex.cmd";
-char *yytexcmd="YANDYTEX.CMD";    /* name of command line file */
-
-/* Try and read default command file - YANDYTEX.CMD */
-/* in current directory and then in directory of YANDYTEX */
-/* (does not make sense in TeX file directory) */
-/* since we don't yet know where that is ! */
-/* can't conveniently include this in output file either - not open yet */
-
-/* used both for yytex.cmd and @ indirect command line files */
-/* can this be reentered ? */
-
-/* supply extension if none */
-void yy_extension (char *fname, char *ext)
-{
-  char *s, *t;
-  if ((s = strrchr(fname, '.')) == NULL ||
-    ((t = strrchr(fname, '\\')) != NULL && s < t)) {
-      strcat(fname, ".");
-      strcat(fname, ext);
-  }
-}
-
 /* remove file name - keep only path - inserts '\0' to terminate */
 
 void strip_name (char *pathname)
@@ -2969,132 +2620,70 @@ char *programpath = ""; /* pathname of program */
 
 /* The following does not deslashify arguments ? Do we need to ? */
 
-int read_commands (char *filename)
-{
-  char commandfile[PATH_MAX]; 
-  FILE *command;
-  char line[PATH_MAX];
-  char *linedup;      /* need to copy line to preserve args */
-  char *s;
-/*  char *sn; */
-  char *optarg;
-  int c;
-
-/*  Try first in current directory (or use full name as specified) */
-  strcpy(commandfile, filename);
-  yy_extension(commandfile, "cmd");
-  if (share_flag == 0)
-         command = fopen(commandfile, "r");
-  else
-         command = _fsopen(commandfile, "r", share_flag);
-  if (command == NULL) {
-/*    If that fails, try in YANDYTeX program directory */
-    strcpy(commandfile, programpath);
-/*    don't need fancy footwork, since programpath non-empty */
-    strcat(commandfile, "\\");
-    strcat(commandfile, filename);
-    yy_extension(commandfile, "cmd");
-    if (share_flag == 0)
-               command = fopen(commandfile, "r");
-    else
-               command = _fsopen(commandfile, "r", share_flag);
-    if (command == NULL) {
-/*      perrormod(commandfile); */      /* debugging only */
-/*      strcpy(commandfile, ""); */   /* indicate failed */
-      return 0;       /* no command line file YYTEX.CMD */
-    }
-  }
-
-/*  allow for multiple lines --- ignore args that don't start with `-' */
-  while (fgets(line, PATH_MAX, command) != NULL) {
-/*    sprintf(log_line, "From %s:\t%s", commandfile, line); */
-/*    skip over comment lines and blank lines */
-    if (*line == '%' || *line == ';' || *line == '\n') continue;
-    if (strchr(line, '\n') == NULL) strcat(line, "\n");
-/*    sfplogline, rintf("From %s:\t%s", commandfile, line); */
-    linedup = xstrdup (line);         /* 93/Nov/15 */
-    if (linedup == NULL) {
-      show_line("ERROR: out of memory\n", 1);    /* read_commands */
-//      exit(1);
-      return -1;    // failure
-    }
-    s = strtok(linedup, " \t\n\r");       /* 93/Nov/15 */
-    while (s != NULL) {
-      if (*s == '-' || *s == '/') {
-        c = *(s+1);
-        optarg = s+2;
-/*        if (*optarg = '=') optarg++; */
-        if (*optarg == '=') optarg++;
-        if (analyze_flag(c, optarg) < 0) return -1;  // failure ???
-      }
-/*      else break; */              /* ignore non-flag items */
-      s = strtok(NULL, " \t\n\r");      /* go to next token */
-    }
-/*    If you want to see command lines in file - put -v in the file */
-/*    if (verbose_flag != 0) sprintf(log_line, "From %s:\t%s", commandfile, line); */
-  }
-  (void) fclose(command);   /* no longer needed */
-  return 1;       // success
-}
-
 /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
 
 /* try and read commands on command line */
 int read_command_line (int ac, char **av)
 { 
   int c;
-  char *optargnew;  /* equal to optarg, unless that starts with `='      */
-                    /* in which case it is optarg+1 to step over the `=' */
-                    /* if optarg = 0, then optargnew = 0 also            */
+  char *optargnew;
+  int option_idx = 0;
 
-//  show_line("read_command_line\n", 0);
-  if (ac < 2) return 0;     /* no args to analyze ? 94/Apr/10 */
+  if (ac < 2)
+    return 0;
 
-/*  while ((c = getopt(ac, av, "+vitrdczp?m:h:x:E:")) != EOF) {              */
-/*  NOTE: keep `Y' in there for `do not reorder arguments !                  */
-/*  WARNING: if adding flags, change also `allowedargs' and  `takeargs' !!!! */
-  while ((c = getopt(ac, av, allowedargs)) != EOF) {
+  while ((c = getopt_long_only(ac, av, short_options, long_options, &option_idx)) != EOF)
+  {
     if (optarg != 0 && *optarg == '=')
-      optargnew = optarg+1;
+      optargnew = optarg + 1;
     else
       optargnew = optarg;
+
     analyze_flag (c, optargnew);
   }
-  if (show_use || quitflag == 3) {
-//    showversion (stdout);
+
+  if (show_use || quitflag == 3)
+  {
     stamp_it(log_line);
     strcat(log_line, "\n");
     show_line(log_line, 0);
     stampcopy(log_line);
     strcat(log_line, "\n");
     show_line(log_line, 0);
-    if (show_use) show_usage();
-    else if (quitflag == 3) {
+
+    if (show_use)
+      show_usage();
+    else if (quitflag == 3)
+    {
       strcat(log_line, "\n");
       show_line(log_line, 0);
     }
-//    exit (0);
+
     return -1;        // failure
   } 
-#ifdef DEBUG
-  if (floating) testfloating();   /* debugging */
-#endif
 
-  if (replfile != NULL && *replfile != '\0') {  /* read user defined replacement */
-    if (read_xchr_file(replfile, 1, av)) {
-      if (trace_flag) show_line("KEY REPLACE ON\n", 0);
+  if (replfile != NULL && *replfile != '\0')
+  {
+    if (read_xchr_file(replfile, 1, av))
+    {
+      if (trace_flag)
+        show_line("KEY REPLACE ON\n", 0);
+
       key_replace = true;
     }
   } 
-/*  key_replace used in texmf.c (input_line) */
-  if (xchrfile != NULL && *xchrfile != '\0') {  /* read user defined xchr[] */
-    if (read_xchr_file(xchrfile, 0, av)) {
-      if (trace_flag) show_line("NON ASCII ON\n", 0);
+
+  if (xchrfile != NULL && *xchrfile != '\0')
+  {
+    if (read_xchr_file(xchrfile, 0, av))
+    {
+      if (trace_flag)
+        show_line("NON ASCII ON\n", 0);
+
       non_ascii = true;
     }
   } 
-/*  non_ascii used in texmf.c (t_open_in & input_line & call_edit) */
-/*  see also xchr [] & xord [] use in tex3.c and itex.c */
+
   return 0;
 }
 
@@ -3103,6 +2692,7 @@ int init_commands (int ac, char **av)
 /*  NOTE: some defaults changed 1993/Nov/18 */
 /*  want_version = show_use = switchflag = return_flag = false;
   is_initex = trace_flag = deslash = non_ascii = false; */
+  pdf_output_flag   = false;
   is_initex         = false; /* check for dumping format file */
   allow_patterns    = false; /* using \pattern after format file loaded */
   reset_exceptions  = false;
@@ -3171,23 +2761,18 @@ int init_commands (int ac, char **av)
   programpath = xstrdup(av[0]); /* extract path executable */
   strip_name(programpath); /* strip off yandytex.exe */
 
-  //format_name = "yandytex";
   format_name = "plain"; /* format name if specified on command line */
 
   encoding_name = "";
 
-  if (read_commands(yytexcmd) < 0)   /* read yandytex.cmd 1994/July/12 */
-    return -1;            // in case of error
-
   if (read_command_line(ac, av) < 0)  /* move out to subr 94/Apr/10 */
     return -1;            // in case of error
 
-  if (optind == 0) optind = ac;   /* no arg case paranoia 94/Apr/10 */
+  if (optind == 0)
+    optind = ac;
 
-/*  Print version *after* banner ? */ /* does this get in log file ? */
-  if (want_version) {
-//  showversion (stdout);
-//  showversion (log_line);
+  if (want_version)
+  {
     stamp_it(log_line);
     strcat(log_line, "\n");
     show_line(log_line, 0);
@@ -3195,14 +2780,14 @@ int init_commands (int ac, char **av)
     strcat(log_line, "\n");
     show_line(log_line, 0);
   }
-/*  if (show_use) show_usage();   */  /* show usage and quit */
 
 /*  if we aren't including current directory in any directory lists */
 /*  then makes no sense to avoid them separately for TFM files ... */
 /*  (that is, the ./ is already omitted from the dir list in that case */
   if (!current_flag && !current_tfm)
     current_tfm = true; /* 94/Jan/24 */
-  return 0;               // success
+
+  return 0;
 }
 
 /* E sets environment variable ? */
@@ -3212,10 +2797,13 @@ void initial_memory (void)
   /* set initial memory allocations */
   if (mem_extra_high < 0)
     mem_extra_high = 0;
+
   if (mem_extra_low < 0)
     mem_extra_low = 0;
+
   if (mem_initex < 0)
     mem_initex = 0;
+
   if (is_initex)
   {
  #if defined(ALLOCATEHIGH) || defined(ALLOCATELOW)
@@ -3233,6 +2821,7 @@ void initial_memory (void)
       show_line("ERROR: Can only set initial main memory size in iniTeX\n", 1);
       mem_initex = 0;
     }
+
     if (trie_size != 0)
     {
       show_line("ERROR: Need only set hyphenation trie size in iniTeX\n", 1);
@@ -3327,29 +2916,6 @@ void checkpause (int flag)
   }
 }
 
-void check_enter (int argc, char *argv[])
-{/* 95/Oct/28 */
-  int m;
-  char current[FILENAME_MAX];
-  if (grabenv("DEBUGPAUSE") != NULL) {
-    (void) _getcwd(current, sizeof(current));
-    sprintf(log_line, "Current directory: `%s'\n", current);
-    show_line(log_line, 0);
-    for (m = 0; m < argc; m++) {
-      sprintf(log_line, "%2d: `%s'\n", m, argv[m]); 
-      show_line(log_line, 0);
-    }
-    checkpause(-1);
-  }
-}
-
-#ifdef IGNORED
-void checkexit (int n)
-{              /* 95/Oct/28 */
-  checkpause(1);
-  exit(n);
-}
-#endif
 
 /*************************************************************************/
 
@@ -3361,15 +2927,18 @@ void checkexit (int n)
 
 void hidetwiddle (char *name)
 {
-  char *s=name;
+  char *s = name;
+
 #ifdef DEBUGTWIDDLE
-  if (trace_flag) {
+  if (trace_flag)
+  {
     sprintf(log_line, "Hidetwiddle %s", name);
     show_line(log_line, 0);
   }
 #endif
 /*  while (*s != '\0' && *s != ' ') { */
-  while (*s != '\0')  {
+  while (*s != '\0')
+  {
     if (*s == '~' && pseudo_tilde != 0)
       *s = (char) pseudo_tilde;  /* typically 254 */
     else if (*s == ' ' && pseudo_space != 0)
@@ -3377,7 +2946,8 @@ void hidetwiddle (char *name)
     s++;
   }
 #ifdef DEBUGTWIDDLE
-  if (trace_flag) {
+  if (trace_flag)
+  {
     sprintf(log_line, "=> %s\n", name);
     show_line(log_line, 0);
   }
@@ -3389,11 +2959,6 @@ void deslash_all (int ac, char **av)
   char buffer[PATH_MAX];  
   char *s;
 
-  if ((s = getenv("USEDVIWINDOINI")) != NULL) 
-    sscanf(s, "%d", &usedviwindo);      /* 94/June/14 */
-
-  check_enter(ac, av);           /* 95/Oct/28 */
-
 /* environment variables for output directories (as in PC TeX) */
 
   if ((s = grabenv("TEXDVI")) != NULL) dvi_directory = s;
@@ -3403,6 +2968,7 @@ void deslash_all (int ac, char **av)
   if ((s = grabenv("TEXPDF")) != NULL) pdf_directory = s;
 
   strcpy(buffer, av[0]);            /* get path to executable */
+
   if ((s = strrchr(buffer, '\\')) != NULL) *(s+1) = '\0';
   else if ((s = strrchr(buffer, '/')) != NULL) *(s+1) = '\0';
   else if ((s = strrchr(buffer, ':')) != NULL) *(s+1) = '\0';
@@ -3480,32 +3046,22 @@ void deslash_all (int ac, char **av)
 /* note: those optarg == 0 test don't really work ... */
 /* note: optarg starts at = in case of x=... */
 
-int init (int ac, char **av)
+int main_init (int ac, char **av)
 {
   char initbuffer[PATH_MAX];
   int k;
-  
-  debugfile = getenv("TEXDEBUG");     /* 94/March/28 */
 
-  if (debugfile)
-    debug_flag = 1;
-  else
-    debug_flag = 0;
-
-  if (debug_flag)
-  {
-    show_line("TEXDEBUG\n", 0);
-    trace_flag = 1;            /* 94/April/14 */
-  }
+  kpse_set_program_name(av[0], NULL);
 
-  if (sizeof(memory_word) != 8) /* compile time test */
+  // compile time test
+  if (sizeof(memory_word) != 8)
   {
     sprintf(log_line, "ERROR: Bad word size %d!\n", sizeof(memory_word));
     show_line(log_line, 1);
   }
 
-  start_time = clock();    /* get time */
-  main_time = start_time;   /* fill in, in case file never opened */
+  start_time = clock();
+  main_time = start_time;
 
   initbuffer[0] = '\0';         /* paranoia 94/Apr/10 */
 
@@ -3514,26 +3070,21 @@ int init (int ac, char **av)
   font_info = NULL;
   str_pool = NULL;
   str_start = NULL;
-#ifdef ALLOCATEZEQTB
-  zeqtb = NULL;
-#endif
-#ifdef ALLOCATEHASH
-  zzzae = NULL;
-#endif
+
 #ifdef ALLOCATESAVESTACK
   save_stack = NULL; 
 #endif
-#ifdef ALLOCATEDVIBUF
-  zdvibuf = NULL; 
-#endif
+
 #ifdef ALLOCATEBUFFER
   buffer = NULL;        /* new 1999/Jan/7 need to do early */
   current_buf_size = 0;
   buffer = realloc_buffer (initial_buf_size);
-/*  sprintf(log_line, "buffer %x, current_buf_size %d\n", buffer, current_buf_size); */
 #endif
-  hyph_list = NULL; hyph_word = NULL;
-  trie_taken = NULL; trie_hash = NULL;
+
+  hyph_list = NULL;
+  hyph_word = NULL;
+  trie_taken = NULL;
+  trie_hash = NULL;
   trie_r = NULL;
   trie_c = NULL;
   trie_o = NULL;
@@ -3543,28 +3094,30 @@ int init (int ac, char **av)
   trie_trl = NULL;
 
   log_opened = false;       /* so can tell whether opened */
-  interaction = -1;       /* default state => 3 */
-  missing_characters = 0;      /* none yet! */
-  workingdirectory = false;   /* set from dviwindo.ini & command line */
-  font_dimen_zero = true;     /* \fontdimen0 for checksum 98/Oct/5 */
-  ignore_frozen = false;     /* default is not to ignore 98/Oct/5 */
-  suppress_f_ligs = false;      /* default is not to ignore f-ligs */
+  interaction = -1;         /* default state => 3 */
+  missing_characters = 0;   /* none yet! */
+  workingdirectory = false; /* set from dviwindo.ini & command line */
+  font_dimen_zero = true;   /* \fontdimen0 for checksum 98/Oct/5 */
+  ignore_frozen = false;    /* default is not to ignore 98/Oct/5 */
+  suppress_f_ligs = false;  /* default is not to ignore f-ligs */
 
   if (ac > 1 && !strncmp(av[1], "-Y", 2))
     reorder_arg_flag = false;
 
-  if (reorder_arg_flag) reorderargs(ac, av);  
+  if (reorder_arg_flag)
+    reorderargs(ac, av);  
 
   if (init_commands(ac, av))
     return -1;          // failure
 
   check_fixed_align(trace_flag);       /* sanity check 1994/Jan/8 */
 
-  format_file   = NULL;       /* to be set in openinou.c 94/Jun/21 */
-  string_file   = NULL;       /* to be set in openinou.c 96/Jan/15 */
-  source_direct = NULL;       /* to be set in openinou.c 98/Sep/29 */
-  dvi_file_name = NULL;       /* to be set in openinou.c 00/Jun/18 */
-  log_file_name = NULL;       /* to be set in openinou.c 00/Jun/18 */
+  format_file   = NULL;
+  string_file   = NULL;
+  source_direct = NULL;
+  dvi_file_name = NULL;
+  log_file_name = NULL;
+  pdf_file_name = NULL;
 
   first_pass_count  = 0;
   second_pass_count = 0;
@@ -3581,25 +3134,16 @@ int init (int ac, char **av)
   if (trace_flag)
     show_line("Entering init (local)\n", 0);
 
-/*   Print version *after* banner ? */ /* does this get in log file ? */
-
   probe_memory();             /* show top address */
   ini_max_address = max_address;       /* initial max address */
 
   if (trace_flag)
     show_maximums(stdout);
 
-#ifdef HEAPWALK
-  if (heap_flag)
-    (void) heap_dump(stdout, 1);
-#endif
-
   initial_memory();
 
   deslash_all(ac, av);    /* deslash and note if format specified */
 
-/*  sprintf(log_line, "%s\n", initbuffer); */    /* debugging, remove later */
-
   no_interrupts = 0;
 
   if (format_spec && mem_spec_flag)
@@ -3607,28 +3151,20 @@ int init (int ac, char **av)
     show_line("WARNING: Cannot change initial main memory size when format specified", 1);
   }
 
-   if (allocate_memory() != 0)   /* NOW, try and ALLOCATE MEMORY if needed */
-     return -1;         // if failed to allocate
+  if (allocate_memory() != 0)   /* NOW, try and ALLOCATE MEMORY if needed */
+    return -1;         // if failed to allocate
 
 /*   following is more or less useless since most all things not yet alloc */
    check_alloc_align(trace_flag);    /* sanity check 1994/Jan/8 */
 
-#ifdef HEAPSHOW
-   if (trace_flag) showaddresses();  /* debugging only 1996/Jan/20 */
-#endif
-
-#ifdef HEAPWALK
-/*   if (heap_flag) heap_dump(stdout, 1); */  /* redundant ? */
-#endif
+  if (trace_flag)
+    show_line("Leaving init (local)\n", 0);
 
-   if (trace_flag)
-     show_line("Leaving init (local)\n", 0);
-   return 0;         // success
+  return 0;         // success
 }
 
-/* #define CLOCKS_PER_SEC 1000 */ /* #define CLK_TCK  CLOCKS_PER_SEC */
+#define CLK_TCK  CLOCKS_PER_SEC
 
-/* void show_inter_val (clock_t start, clock_t end) { */
 void show_inter_val (clock_t interval)
 {
   int seconds, tenths, hundredth, thousands;
@@ -3708,12 +3244,6 @@ int endit (int flag)
   return flag;
 }
 
-/********************************************************************************/
-
-/* addition 98/Mar/31 print_csnames Frank Mittelbach */
-
-#define MAXCOLUMN 78
-
 void print_cs_name (FILE *output, int h)
 {
   int c, textof, n;
@@ -4099,300 +3629,6 @@ void show_font_info (void)
   free((void *)fnumtable);
 }
 
-////////////////////////////////////////////////////////////////////////////
-
-// Here follows the new stuff for the DLL version
-
-#ifdef _WINDOWS
-
-int showlineinx=0;
-
-#define SHOWLINEBUFLEN 256
-
-char showlinebuf[SHOWLINEBUFLEN];
-
-// char log_line[MAXLINE];
-
-#define WHITESPACE " \t\n\r"
-
-HINSTANCE hInstanceDLL=NULL;    /* remember for this DLL */
-
-/* This is the callback function for the EDITTEXT Control in CONSOLETEXT */
-
-#define GET_WM_COMMAND_CMD(wParam, lParam)  (HIWORD(wParam))
-#define GET_WM_COMMAND_ID(wParam, lParam) (LOWORD(wParam))
-#define GET_WM_COMMAND_HWND(wParam, lParam) ((HWND)lParam)
-
-HWND hConsoleWnd=NULL;    /* Console Text Window Handle passed from DVIWindo */
-
-void ClearShowBuffer (void)
-{
-  showlinebuf[showlineinx++] = '\0';    // clear out accumulated stuff
-  if (hConsoleWnd != NULL)
-    SendMessage(hConsoleWnd, ICN_ADDTEXT, (WPARAM) showlinebuf, 0L);
-  showlineinx = 0;
-}
-
-// communicate with DVIWindo (for yandytex.dll)
-
-void show_line (char *line, int errflag) {     /* 99/June/11 */
-  int ret;
-
-  if (IsWindow(hConsoleWnd) == 0) {   // in case the other end died
-    sprintf(line, "NO CONSOLE WINDOW? %08X %s", hConsoleWnd, line);
-    ret = MessageBox(NULL, line, "YandYTeX", MB_ICONSTOP | MB_OKCANCEL | MB_TASKMODAL);
-    hConsoleWnd = NULL;
-//    abort_flag++;            // kill job in this case ???
-    return;
-  }
-
-  if (showlineinx > 0) ClearShowBuffer();
-
-  if (hConsoleWnd != NULL)
-    SendMessage(hConsoleWnd, ICN_ADDTEXT, (WPARAM) line, 0L);
-
-  if (errflag) {
-    err_level++;
-    ret =  MessageBox(NULL, line, "YandYTeX", MB_ICONSTOP | MB_OKCANCEL | MB_TASKMODAL);
-    if (ret == IDCANCEL) {
-//      abort_flag++;
-      uexit(1);   // dangerous reentry possibility ?
-    }
-  }
-}
-
-//  Provide means for buffering up individual characters
-
-void show_char (int chr) {
-  if (showlineinx +2 >= SHOWLINEBUFLEN) ClearShowBuffer();
-  showlinebuf[showlineinx++] = (char) chr;
-  if (chr == '\n') ClearShowBuffer();
-}
-
-void winshow(char *line) {
-  (void) MessageBox(NULL, line, "YandYTeX", MB_ICONINFORMATION | MB_OK | MB_TASKMODAL);
-}
-
-void winerror (char *line) {
-  int ret;
-  ret = MessageBox(NULL, line, "YandYTeX", MB_ICONSTOP | MB_OKCANCEL | MB_TASKMODAL);
-  if (ret == IDCANCEL) abort_flag++;
-}
-
-// argument info constructed from command line 
-
-int xargc;
-
-char **xargv=NULL;
-
-// need to be careful here because of quoted args with spaces in them
-// e.g. -d="G:\Program Files\Adobe\Acrobat\*.pdf"
-
-int makecommandargs (char *line)
-{
-  int xargc;
-//  char *s, *t;
-  unsigned char *s, *t;       // fix 2000 June 18
-
-  if (line == NULL) return -1;    /* sanity check */
-
-//  winerror(line);           // debugging only
-
-//  s = strtok(line, WHITESPACE);
-//  while (s != NULL) {         /* count arguments */
-//    xargc++;
-//    s = strtok(NULL, WHITESPACE);
-//  }
-
-  xargc = 0;
-  s = line;
-  while (*s != '\0') {
-    while (*s <= 32 && *s > 0) s++;
-    if (*s == '\0') break;
-    t = s;
-    while (*t > 32 && *t != '\"') t++;
-    if (*t == '\"') {
-      t++;
-      while (*t > 0 && *t != '\"') t++;
-      if (*t == '\0') break;
-      t++;
-    }
-//    xargv[xargc] = s;
-    xargc++;
-    if (*t == '\0') break;
-//    *t = '\0';
-    s = t+1;
-  }
-
-  if (xargc == 0) return -1;      /* nothing to do */
-
-  xargv = (char **) malloc(xargc * sizeof(char *));
-  if (xargv == NULL) {
-    sprintf(log_line, "ERROR: Unable to allocate memory for %s\n", "arguments");
-    winerror(log_line);
-    return -1;
-  }
-
-  xargc = 0;
-  s = line;
-  while (*s != '\0') {
-    while (*s <= ' ' && *s > '\0') s++; /* eat white space */
-    if (*s == '\0') break;
-    t = s;
-    while (*t > ' ' && *t != '\"') t++;
-    if (*t == '\"') {
-      t++;
-      while (*t > 0 && *t != '\"') t++;
-      if (*t == '\0') break;
-      t++;
-    }
-//    winerror(s);    // debugging only
-    xargv[xargc] = s;
-    xargc++;
-    if (*t == '\0') break;
-    *t = '\0';
-    s = t+1;
-  }
-
-//  s = line;
-//  for (k = 0; k < xargc; k++) { /* create pointers to args */
-//    while (*s > '\0' && *s <= ' ') s++; /* eat white space */
-//    xargv[k] = s;
-//    s += strlen(s) +1;
-//  }
-
-#ifdef DEBUGGING
-  s = log_line;
-  *s = '\0';
-  for (k = 0; k < xargc; k++) {
-    sprintf(s, "%d\t%s\n", k, xargv[k]);
-    s += strlen(s);
-  }
-  winshow(log_line);
-#endif
-  return xargc;
-}
-
-// refers to TeXAsk in dviwindo.c
-
-// int (* AskUserCall) (char *, char *) = NULL; // callback for user questions
-int (* AskUserCall) (char *, char *, char *) = NULL;  // callback for user questions
-
-// called from tex0.c only  ---  by initterm and term_input
-
-//int ConsoleInput (char *question, char *buffer) 
-int ConsoleInput (char *question, char *help, char *buffer)
-{
-  int ret=0;
-//  char *s;
-  if (AskUserCall == NULL) return 0;
-//  sprintf(log_line, "str_start %x %x\n", str_start, str_start [831]);
-//  show_line(log_line, 1);
-
-  *buffer = '\0';
-  ret = AskUserCall (question, help, buffer);   // value returned by dialogbox
-//  strcpy(buffer, "x");
-//  strcat(buffer, " ");      // ???
-//  sprintf(log_line, "str_start %x %x\n", str_start, str_start[831]);
-//  show_line(log_line, 1);
-//  input_line_finish();      // ???
-//  s = buffer + strlen(buffer);
-//  *s++ = ' ';           // space terminate
-//  *s++ = '\0';          // and null terminate
-//  returning != 0 means EOF or ^Z
-  return ret;
-}
-
-//  This is the new entry point of DLL called from DVIWindo 
-//  ARGS: console window to send messages to, command line, callback fun
-//  no console window output if hConsole is NULL
-//  returns -1 if it fails --- returns 0 if it succeeds
-
-// MYLIBAPI int yandytex (HWND hConsole, char *line, int (* AskUser) (char *, char *)) {
-MYLIBAPI int yandytex (HWND hConsole, char *line, int (* AskUser) (char *, char *, char *))
-{
-  int flag;
-
-  abort_flag = 0;            // redundant
-  hConsoleWnd = NULL;         // redundant
-
-  AskUserCall = AskUser;        // remember callback
-
-  hConsoleWnd = hConsole;       // remember console window handle
-
-//  can't getenv("DEBUGPAUSE") cause setupdviwindo not called yet
-//  if (grabenv("DEBUGPAUSE") != NULL) {
-//    show_line(line, 0);          // debugging - show command line
-//    show_line("\n", 0);
-//  }
-
-  xargc = makecommandargs(line);      // sets up global *xargv[]
-
-  if (xargc < 0) return -1;       // sanity check
-
-  if (hConsoleWnd != NULL) 
-    SendMessage(hConsoleWnd, ICN_SETTITLE, (WPARAM) "YandYTeX", 0L);
-//  SendMessage(hConsoleWnd, ICN_RESET, 0, 0L); // if want to clear window
-
-
-  (void) main(xargc, xargv);  // now run YandYTeX proper in texmf.c 
-
-  if (err_level > 0 || abort_flag > 0) {
-//    sprintf(log_line, "ERRORS in Processing (err %d abort %d)\n",
-//        err_level, abort_flag);
-//    winerror(log_line);
-  }
-
-//  if (psbufpos > 0) sendpsbuffer(output);   // empty out PS buffer
-//  if (psbufpos > 0) PSputs("", output);   // output already closed
-
-  if (hConsoleWnd != NULL) {
-    if (err_level > 0 || abort_flag > 0) flag = 1;
-    else flag = 0;              // pass along error indication
-    SendMessage(hConsoleWnd, ICN_DONE, flag, 0);  // flush out console buffer
-  }
-//  PScallback = NULL;
-  hConsoleWnd = NULL;
-
-  if (xargv != NULL) free(xargv);
-  if (abort_flag) return -1;
-  else return 0;
-}
-
-BOOL WINAPI DllMain (HINSTANCE hInstDll, DWORD fdwReason, LPVOID fImpLoad)
-{
-
-  switch (fdwReason) {
-    case DLL_PROCESS_ATTACH:
-      // The DLL is being mapped into the process's address space
-      // place to allocate memory ???
-      // return FALSE if this fails
-      hInstanceDLL = hInstDll;    /* remember it */
-      break;
-
-    case DLL_THREAD_ATTACH:
-      // A thread is being created
-      break;
-
-    case DLL_THREAD_DETACH:
-      // A thread is exiting cleanly
-      break;
-
-    case DLL_PROCESS_DETACH:
-      // The DLL is being unmapped from the process's address space
-      // place to free any memory allocated
-      // but make sure it in fact *was* allocated
-      hInstanceDLL = NULL;    /* forget it */
-      break;
-  }
-  return(TRUE); // used only for DLL_PROCESS_ATTACH
-}
-#endif  // end of new stuff for DLL version
-
-//////////////////////////////////////////////////////////////////////////////
-
-
-
 /*  NOTE: current_tfm = false (-c)
   not checking for TFM in current directory saves 0.1 sec
   (0.2 if file_method = false (-G) */
@@ -4410,16 +3646,6 @@ BOOL WINAPI DllMain (HINSTANCE hInstDll, DWORD fdwReason, LPVOID fImpLoad)
 
 /*  Fast flag combinations: nothing, bG, bcG, bcDG */
 
-/* constants for _heapchk/_heapset/_heapwalk routines */
-/* #define _HEAPEMPTY (-1) */
-/* #define _HEAPOK  (-2) */
-/* #define _HEAPBADBEGIN  (-3) */
-/* #define _HEAPBADNODE (-4) */
-/* #define _HEAPEND (-5) */
-/* #define _HEAPBADPTR  (-6) */
-
-/* new arg -H=8 for horizontal tab *//* tab_step = 0 means no tab replacement */
-
 /* buffercopy no longer used */
 
 /* To Knuthian reset right when command line interpreted */