OSDN Git Service

fixed memory align.
[putex/putex.git] / src / texsourc / yandytex.c
index adeeeff..53a9fef 100644 (file)
 
 #include "texd.h"
 
-#define dump_default_var    TEX_format_default
-#define dump_default        " plain.fmt"
 #define dump_ext_length     4
-#define dump_default_length format_default_length
-#define main_program        texbody
 #define edit_value          tex_edit_value
 
 extern char * replacement[];
-
-/* The main program, etc.  */
-/* What we were invoked as and with. */
-
-static char *program_name = NULL;
 int    gargc;   /* number of args - set to zero after initialization */
-char **gargv;   /* char *gargv[] -- bkph ? */
-
-/* The entry point: set up for reading the command line, which will
-   happen in `t_open_in', then call the main body.  */
-
-int main_init(int, char **);     /* in local.c */
+char **gargv;
 
 int jump_used = 0;
 
@@ -50,22 +36,14 @@ int main (int ac, char *av[])
 {
   int flag = 0, ret = 0;
 
-#ifndef INI
-  char custom_default[PATH_MAX];
-#endif
-
-#ifdef WIN32
-  _setmaxstdio(2048);
-#endif
-
   gargc = ac;
   gargv = av;
 
   if (main_init(gargc, gargv))
-    return -1; // failure
+    return -1;
 
-  dump_default_var = dump_default;
-  dump_default_length = strlen(dump_default_var + 1);
+  TEX_format_default = " plain.fmt";
+  format_default_length = strlen(TEX_format_default + 1);
 
   jump_used = 0;
 
@@ -76,18 +54,12 @@ int main (int ac, char *av[])
     flag = main_program();
 
     if (trace_flag)
-    {
-      sprintf(log_line, "EXITING at %s %d %d %d\n", "MAIN", flag, ret, jump_used);
-      show_line(log_line, 0);
-    }
+      printf("EXITING at %s: flag = %d, ret = %d, jump_used = %d\n", "main", flag, ret, jump_used);
   }
   else
   {
     if (trace_flag)
-    {
-      sprintf(log_line, "EXITING at %s %d %d %d\n", "JUMPOUT", flag, ret, jump_used);
-      show_line(log_line, 0);
-    }
+      printf("EXITING at %s: flag = %d, ret = %d, jump_used =  %d\n", "jump_out", flag, ret, jump_used);
   }
 
   if (endit(flag) != 0)
@@ -95,12 +67,8 @@ int main (int ac, char *av[])
 
   if (flag == 0)
     return 0;
-
-#ifdef _WINDOWS
-  return flag;
-#else
-  else exit (flag);   // avoid circularity!
-#endif
+  else
+    exit (flag);
 }
 
 /* texk/web2c/lib/texmfmp.c */
@@ -153,74 +121,54 @@ void t_open_in (void)
   }
 }
 
-/* All our interrupt handler has to do is set TeX's or Metafont's global
-   variable `interrupt'; then they will do everything needed.  */
-
-static void catch_interrupt (int err)
+static void catch_interrupt(int err)
 {
-  (void) signal (SIGINT, SIG_IGN);
+  (void) err;
+  (void) signal(SIGINT, SIG_IGN);
 
   if (interrupt++ >= 3)
     exit(1);
 
-  (void) signal (SIGINT, catch_interrupt);
+  (void) signal(SIGINT, catch_interrupt);
 }
 
-/* Besides getting the date and time here, we also set up the interrupt
-   handler, for no particularly good reason.  It's just that since the
-   `fix_date_and_time' routine is called early on (section 1337 in TeX,
-   ``Get the first line of input and prepare to start''), this is as
-   good a place as any.  */
-
-void get_date_and_time (integer *sys_minutes,
-                        integer *sys_day,
-                        integer *sys_month,
-                        integer *sys_year)
+void fix_date_and_time (void)
 {
   time_t clock;
   struct tm *tmptr;
 
-  (void) time (&clock);  /* - seconds since 1970 */ 
+  (void) time(&clock);
 
   if (trace_flag)
-  {
-    sprintf(log_line, "The time is %u\n", clock);
-    show_line(log_line, 0);   
-  }
+    printf("The time is %lld\n", (long long)clock);
 
   if (clock < 0)
-  {
-    show_line("Time not available!\n", 1);
-  }
+    puts("Time not available!");
 
   tmptr = localtime (&clock);
 
   if (tmptr == NULL)
   {
-    sprintf(log_line, "Cannot convert time (%0ld)!\n", clock);
-    show_line(log_line, 1);
-    *sys_year    = 2038;
-    *sys_month   = 1;
-    *sys_day     = 18;
-    *sys_minutes = 22 * 60 + 14;
+    printf("Cannot convert time (%0lld)!\n", (long long)clock);
+    year     = 2038;
+    month    = 1;
+    day      = 18;
+    tex_time = 22 * 60 + 14;
   }
   else
   {
-    *sys_minutes = tmptr->tm_hour * 60 + tmptr->tm_min;
-    *sys_day     = tmptr->tm_mday;
-    *sys_month   = tmptr->tm_mon + 1;
-    *sys_year    = tmptr->tm_year + 1900;
+    tex_time = tmptr->tm_hour * 60 + tmptr->tm_min;
+    day      = tmptr->tm_mday;
+    month    = tmptr->tm_mon + 1;
+    year     = tmptr->tm_year + 1900;
 
     if (trace_flag)
-    {
-      sprintf(log_line, "%d-%d-%d %d:%d\n",
+      printf("%d-%d-%d %d:%d\n",
         tmptr->tm_year + 1900,
         tmptr->tm_mon + 1,
         tmptr->tm_mday,
         tmptr->tm_hour,
         tmptr->tm_min);
-      show_line(log_line, 0);
-    }
   }
 
   {
@@ -229,9 +177,9 @@ void get_date_and_time (integer *sys_minutes,
     {
       if (signal(SIGINT, catch_interrupt) == SIG_ERR)
       {
-        show_line(" CTRL-C handler not installed\n", 0);
+        puts(" CTRL-C handler not installed\n");
 #ifndef _WINDOWS
-        uexit(1);  /* do we care when run as DLL ? */
+        uexit(EXIT_FAILURE);  /* do we care when run as DLL ? */
 #endif
       }
     }
@@ -255,15 +203,8 @@ void complain_line (FILE *output)
   sprintf(log_line, "! Unable to read an entire line---buf_size=%d.\n", buf_size);
 #endif
 
-  if (output == stderr)
-    show_line(log_line, 1);
-  else
-    if (output == stdout)
-      show_line(log_line, 0);
-    else
-      fputs(log_line, output); // never
-
-  show_line("  (File may have a line termination problem.)", 0);
+  fputs(log_line, output);
+  puts("  (File may have a line termination problem.)");
 }
 
 void show_bad_line (FILE *output, int first, int last)
@@ -275,134 +216,107 @@ void show_bad_line (FILE *output, int first, int last)
   {
     ch = buffer[i];
 
-    if ((show_in_hex && ch > 127))
+    if (show_in_hex && (ch > 127))
     {
       c = ch >> 4;
-      d = ch & 15; 
+      d = ch & 15;
+
       if (c > 9)
         c = c + 'a' - 10;
       else
         c = c + '0';
+
       if (d > 9)
         d = d + 'a' - 10;
       else
         d = d + '0';
-/* putc('^', output); putc('^', output); */
+
       *s++ = '^';
       *s++ = '^';
-/* putc (c, output); putc (d, output); */
+
       *s++ = (char) c;
       *s++ = (char) d;
     }
+    else if (ch < 32)
+    {
+      *s++ = '^';
+      *s++ = '^';
+      *s++ = (char) (ch + 64);
+    }
+    else if (ch == 127)
+    {
+      *s++ = '^';
+      *s++ = '^';
+      *s++ = (char) (ch - 64);
+    }
     else
-      if (ch < 32)
-      {
-/* putc('^', output); putc('^', output); */
-        *s++ = '^';
-        *s++ = '^';
-/* putc (ch + 64, output); */
-        *s++ = (char) (ch + 64);
-      }
-      else
-        if (ch == 127)
-        {
-/* putc('^', output); putc('^', output); */
-          *s++ = '^';
-          *s++ = '^';
-/* putc (ch - 64, output); */
-          *s++ = (char) (ch - 64);
-        }
-        else
-        {
-/* putc(ch, output); */
-          *s++ = (char) ch;
-        }
+    {
+      *s++ = (char) ch;
+    }
   }
-//  putc(' ', output);    /*  putc('\n', output); */
+
   *s++ = ' ';
   *s++ = '\0';
-  if (output == stderr)
-    show_line(log_line, 1);
-  else
-    if (output == stdout)
-      show_line(log_line, 0);
-    else
-      fputs(log_line, output);   // log_file
+
+  fputs(log_line, output);   // log_file
 }
 
-// split off for convenience and use in ConsoleInput
 boolean input_line_finish (void)
 {
   int i = '\0';
   int ch, flag;
-  
-/*  if last line in file does not end with \n - never happens ? */
-/*  if (i == EOF && buffer[last] != '\n') buffer[last++] = '\n'; */
 
-  buffer[last] = ' ';           /* space terminate */
+  buffer[last] = ' ';
+
   if (last >= max_buf_stack)
-    max_buf_stack = last; /* remember longest line */
+    max_buf_stack = last;
 
-/* Trim trailing whitespace.  */ 
-/* #define isblank(c) ((c) == ' ' || (c) == '\t') */
-/* What about \n ?  Can't get in here ?- bkph */
-/* What about control-Z that gets read in binary mode ? - bkph */
-// #ifdef MYDEBUG
-/*  while (last > first && buffer[last - 1] <= ' ')  --last; */
   while (last > first)
   {
     i = buffer[last - 1];
 
     if (i == ' ' || i == '\t')
       --last;
-/*    else if (trimeof && i == 26) --last;   */   /* 93/Nov/24 */
     else
       break;
   }
-/*  if (trimeof != 0 && i == EOF && last == first)  
-      return false; */              /* EOF and line empty */
-// #else
-//   while (last > first
-//         && isblank (buffer[last - 1]) && buffer[last - 1] != '\r')
-//    --last;
-// #endif
 
-/* following added to check source file integrity ASCII 32 - 127 */
-/* allow space, tab, new-page - also allow return, newline ? */
   if (restrict_to_ascii)
   {
     flag = 0;
+
     for (i = first; i <= last; i++)
     {
       ch = buffer[i];
-/*      if (ch > 127 || (ch < ' ' && ch != '\t' && ch != '\f')) */
-/*      1 -- 8, 11, 14 -- 31 are not good ASCII characters */
+
       if (ch > 126 ||  (ch < ' ' && ch != '\t' && ch != '\f' && ch != '\r' && ch != '\n'))
       {
         sprintf(log_line, "\n! non ASCII char (%d) in line: ", ch);
         show_line(log_line, 1);
+
         if (log_opened)
           fprintf(log_file, "\n! non ASCII char (%d) in line: ", ch);
-/*        buffer[i]= 127; */ /* not defined - invalid char */
+
         flag = 1;
         break;
       }
     }
+
     if (flag)
     {
       show_bad_line(errout, first, last);
+
       if (log_opened)
         show_bad_line(log_file, first, last);
     }
   }
-/* Don't bother using xord if we don't need to. */ /* for input line */
-/* #ifdef NONASCII */ /* has been turned into command line flag - bkph */
+
   if (non_ascii)
   {
     for (i = first; i <= last; i++)
       buffer[i] = xord[buffer[i]];
   }
-/* #endif */
+
   return true;
 }
 
@@ -555,7 +469,7 @@ boolean input_line (FILE *f)
       last--;       /* just in case */
     }
     else
-      uexit(1);      /* line too long */
+      uexit(EXIT_FAILURE);      /* line too long */
   }
 
   return input_line_finish();
@@ -641,10 +555,10 @@ void call_edit (ASCII_code *stringpool, pool_pointer fnstart, integer fnlength,
             sprintf(log_line, "! `%%d' cannot appear twice in editor command.\n");
             show_line(log_line, 1);
 #endif
-            uexit(1); 
+            uexit(EXIT_FAILURE); 
           }
 
-          (void) sprintf (s, "%d", linenumber);
+          (void) sprintf (s, "%lld", linenumber);
 
           while (*s != '\0')
             s++;
@@ -662,7 +576,7 @@ void call_edit (ASCII_code *stringpool, pool_pointer fnstart, integer fnlength,
             sprintf(log_line, "! `%%s' cannot appear twice in editor command.\n");
             show_line(log_line, 1);
 #endif
-            uexit(1); 
+            uexit(EXIT_FAILURE); 
           }
 
           t = (char *) texfilename;
@@ -688,7 +602,7 @@ void call_edit (ASCII_code *stringpool, pool_pointer fnstart, integer fnlength,
             sprintf(log_line, "! `%%l' cannot appear twice in editor command.\n");
             show_line(log_line, 1);
 #endif
-            uexit(1); 
+            uexit(EXIT_FAILURE); 
           }
 
           t = (char *) log_file_name;
@@ -723,9 +637,8 @@ void call_edit (ASCII_code *stringpool, pool_pointer fnstart, integer fnlength,
 
   if (strlen(command) + 1 >= commandlen)
   {
-    sprintf(log_line, "Command too long (%d > %d)\n", strlen(command) + 1, commandlen);
-    show_line(log_line, 1);
-    uexit(1);
+    printf("Command too long (%ld > %d)\n", strlen(command) + 1, commandlen);
+    uexit(EXIT_FAILURE);
   }
 
   //flushall();
@@ -744,22 +657,15 @@ void call_edit (ASCII_code *stringpool, pool_pointer fnstart, integer fnlength,
 
     sprintf(log_line, "  (TEXEDIT=%s)\n", edit_value);
     show_line(log_line, 0);
-    show_line("  (Editor specified may be missing or path may be wrong)\n", 0);
-    show_line("  (or there may be missing -- or extraneous -- quotation signs)\n", 0);
+    puts("  (Editor specified may be missing or path may be wrong)\n");
+    puts("  (or there may be missing -- or extraneous -- quotation signs)\n");
   }
 
-  uexit(1);
+  uexit(EXIT_FAILURE);
 }
 
-/* Read and write format (for TeX) or base (for Metafont) files.  In
-   tex.web, these files are architecture dependent; specifically,
-   BigEndian and LittleEndian architectures produce different files.
-   These routines always output BigEndian files.  This still does not
-   make the dump files architecture-independent, because it is possible
-   to make a format file that dumps a glue ratio, i.e., a floating-point
-   number.  Fortunately, none of the standard formats do that.  */
 
-#if !defined (WORDS_BIGENDIAN) && !defined (NO_FMTBASE_SWAP) /* this fn */
+#if !defined (WORDS_BIGENDIAN) && !defined (NO_FMTBASE_SWAP)
 
 /* We don't REALLY care what `endian' the machine is after all ! */
 /* But we do care about speed - so check exe file for following - bkph */
@@ -821,36 +727,33 @@ static int swap_items (char *p, int nitems, int size)
       show_line("\n", 0);
       sprintf(log_line, "! I can't (un)dump a %d byte item.\n", size);
       show_line(log_line, 1);
-      uexit(1);
+      uexit(EXIT_FAILURE);
   }
   return 0;
 }
 #endif /* not WORDS_BIGENDIAN and not NO_FMTBASE_SWAP */
 
-/* Hmm, this could benefit from some on the fly compression - bkph */
-/* and complementary decompression on input - bkph */
-
-/* Here we write NITEMS items, each item being ITEM_SIZE bytes long.
-   The pointer to the stuff to write is P, and we write to the file
-   OUT_FILE.  */
-
-int do_dump (char *p, int item_size, int nitems, FILE *out_file)
+#ifdef COMPACTFORMAT
+int do_dump(char *p, int item_size, int nitems, gzFile out_file)
+#else
+int do_dump(char *p, int item_size, int nitems, FILE *out_file)
+#endif
 {
 #if !defined (WORDS_BIGENDIAN) && !defined (NO_FMTBASE_SWAP)
   swap_items (p, nitems, item_size);
 #endif
 
 #ifdef COMPACTFORMAT
-  if (gzwrite(gz_fmt_file, p, (item_size * nitems)) != (item_size * nitems))
+  if (gzwrite(out_file, p, (item_size * nitems)) != (item_size * nitems))
 #else
-  if ((int) fwrite (p, item_size, nitems, out_file) != nitems)
+  if ((int) fwrite(p, item_size, nitems, out_file) != nitems)
 #endif
   {
     show_line("\n", 0);
     sprintf(log_line, "! Could not write %d %d-byte item%s.\n",
                nitems, item_size, (nitems > 1) ? "s" : "");
     show_line(log_line, 1);
-    uexit(1);
+    uexit(EXIT_FAILURE);
   }
 
 /* Have to restore the old contents of memory, since some of it might get used again.  */
@@ -861,13 +764,14 @@ int do_dump (char *p, int item_size, int nitems, FILE *out_file)
   return 0;
 }
 
-/* Hmm, this could benefit from some on the fly decompression - bkph */
-
-/* Here is the dual of the writing routine.  */
-int do_undump (char *p, int item_size, int nitems, FILE *in_file)
+#ifdef COMPACTFORMAT
+int do_undump(char *p, int item_size, int nitems, gzFile in_file)
+#else
+int do_undump(char *p, int item_size, int nitems, FILE *in_file)
+#endif
 {
 #ifdef COMPACTFORMAT
-  if (gzread(gz_fmt_file, (void *) p, (unsigned int) (item_size * nitems)) <= 0)
+  if (gzread(in_file, (void *) p, (unsigned int) (item_size * nitems)) <= 0)
 #else
   if ((int) fread((void *) p, item_size, nitems, in_file) != nitems)
 #endif
@@ -876,21 +780,17 @@ int do_undump (char *p, int item_size, int nitems, FILE *in_file)
     sprintf(log_line, "! Could not read %d %d-byte item%s.\n",
                nitems, item_size, (nitems > 1) ? "s" : "");
     show_line(log_line, 1);
-    uexit(1);
+    uexit(EXIT_FAILURE);
   }
 
 #if !defined (WORDS_BIGENDIAN) && !defined (NO_FMTBASE_SWAP)
   swap_items (p, nitems, item_size);
 #endif
+
   return 0;
 }
 
 #ifdef FUNNY_CORE_DUMP
-/* This procedure is due to chris@mimsy.umd.edu.  It makes a core dump
-   without any sort of error status (abort(2) does return an error status,
-   so we don't want to use that).  It is used only when making a preloaded
-   TeX from virtex, and is triggered by a magic file name requested as
-   input (see `open_input', above).  */
 
 void funny_core_dump (void)
 {