OSDN Git Service

update_terminal().
[putex/putex.git] / src / texsourc / tex0.c
index ef20f26..2e17801 100644 (file)
-#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
+/* Copyright 2014 Clerk Ma
 
-#include "texwin.h"
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
 
-#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
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
 
-#include <setjmp.h>
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301 USA.  */
 
 #define EXTERN extern
-
 #include "texd.h"
 
-#ifdef IGNORED
-static void winerror (char * message)
-{
-  (void) MessageBox(NULL, message, "YandYTeX.DLL", MB_ICONSTOP | MB_OK);
-}
-#endif
-
-/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
-void set_cur_lang(void)
-{
-  if (language <= 0)
-    cur_lang = 0;
-  else if (language > 255)
-    cur_lang = 0;
-  else
-    cur_lang = language;
-}
-INLINE void free_avail_(halfword p)
-{
-  link(p) = avail;
-  avail = p;
-#ifdef STAT
-  decr(dyn_used);
-#endif /* STAT */
-}
-INLINE void dvi_out_(ASCII_code op)
-{
-  dvi_buf[dvi_ptr] = op;
-  incr(dvi_ptr);
-  if (dvi_ptr == dvi_limit)
-    dvi_swap();
-}
-INLINE void succumb (void)
-{
-  if (interaction == error_stop_mode)
-    interaction = scroll_mode;
-
-  if (log_opened)
-  {
-    error();
-  }
-#ifdef DEBUG
-  if (interaction > 0)
-    debug_help();
-#endif
-  history = 3;
-  jump_out();
-}
-INLINE void flush_string (void)
-{
-  decr(str_ptr);
-  pool_ptr = str_start[str_ptr];
-}
-INLINE void append_char (ASCII_code c)
-{
-  str_pool[pool_ptr] = c;
-  incr(pool_ptr);
-}
-INLINE void append_lc_hex (ASCII_code c)
-{
-  if (c < 10)
-    append_char(c + '0');
-  else
-    append_char(c - 10 + 'a');
-}
-INLINE void print_err (const char * s)
-{
-  if (interaction == error_stop_mode);
-    print_nl("! ");
-  print_string(s);
-}
-INLINE void tex_help (unsigned int n, ...)
-{
-  int i;
-  va_list help_arg;
-
-  if (n > 6)
-    n = 6;
-
-  help_ptr = n;
-  va_start(help_arg, n);
-
-  for (i = n - 1; i > -1; --i)
-    help_line[i] = va_arg(help_arg, char *);
-
-  va_end(help_arg);
-}
-INLINE void str_room_ (int val)
-{
-#ifdef ALLOCATESTRING
-  if (pool_ptr + val > current_pool_size)
-    str_pool = realloc_str_pool(increment_pool_size);
-
-  if (pool_ptr + val > current_pool_size)
-  {
-    overflow("pool size", current_pool_size - init_pool_ptr);
-  }
-#else
-  if (pool_ptr + val > pool_size)
-  {
-    overflow("pool size", pool_size - init_pool_ptr); /* pool size */
-  }
-#endif
-}
-INLINE void tail_append_ (pointer val)
-{
-  link(tail) = val;
-  tail = link(tail);
-}
-/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
-
-/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
 /* sec 0058 */
 void print_ln (void)
 {
@@ -141,12 +26,12 @@ void print_ln (void)
     case term_and_log:
       show_char('\n');
       term_offset = 0;
-      (void) putc ('\n', log_file);
+      putc('\n', log_file);
       file_offset = 0;
       break;
 
     case log_only:
-      (void) putc ('\n',  log_file);
+      putc('\n', log_file);
       file_offset = 0;
       break;
 
@@ -161,7 +46,7 @@ void print_ln (void)
       break;
 
     default:
-      (void) putc ('\n', write_file[selector]);
+      putc('\n', write_file[selector]);
       break;
   }
 }
@@ -169,20 +54,18 @@ void print_ln (void)
 void print_char_ (ASCII_code s)
 {
   if (s == new_line_char)
-  {
     if (selector < pseudo)
     {
       print_ln();
       return;
     }
-  }
 
   switch (selector)
   {
     case term_and_log:
-      (void) show_char(Xchr(s));
+      show_char(xchr[s]);
       incr(term_offset);
-      (void) putc(Xchr(s), log_file);
+      putc(xchr[s], log_file);
       incr(file_offset);
 
       if (term_offset == max_print_line)
@@ -193,14 +76,14 @@ void print_char_ (ASCII_code s)
       
       if (file_offset == max_print_line)
       {
-        (void) putc ('\n', log_file);
+        putc ('\n', log_file);
         file_offset = 0;
       }
 
       break;
 
     case log_only:
-      (void) putc(Xchr(s), log_file);
+      putc(xchr[s], log_file);
       incr(file_offset);
 
       if (file_offset == max_print_line)
@@ -209,7 +92,7 @@ void print_char_ (ASCII_code s)
       break;
 
     case term_only:
-      (void) show_char(Xchr(s));
+      show_char(xchr[s]);
       incr(term_offset);
 
       if (term_offset == max_print_line)
@@ -229,26 +112,18 @@ void print_char_ (ASCII_code s)
     case new_string:
 #ifdef ALLOCATESTRING
       if (pool_ptr + 1 > current_pool_size)
-      {
         str_pool = realloc_str_pool (increment_pool_size);
-      }
       
       if (pool_ptr < current_pool_size)
-      {
-        str_pool[pool_ptr]= s;
-        incr(pool_ptr);
-      }
+        append_char(s);
 #else
       if (pool_ptr < pool_size)
-      {
-        str_pool[pool_ptr]= s;
-        incr(pool_ptr);
-      }
+        append_char(s);
 #endif
       break;
 
     default:
-      (void) putc(Xchr(s), write_file[selector]);
+      putc(xchr[s], write_file[selector]);
       break;
   }
 
@@ -262,7 +137,7 @@ void print_ (integer s)
 
   if (s >= str_ptr)
     s = 259; /* ??? */
-  else 
+  else
   {
     if (s < 256)
     {
@@ -285,14 +160,15 @@ void print_ (integer s)
           
           nl = new_line_char;
           new_line_char = -1;
-/* translate ansi to dos 850 */
+          
+          /* translate ansi to dos 850 */
           if (!show_in_hex && s < 256 && s >= 32)
           {
             if (show_in_dos && s > 127)
             {
               if (wintodos[s - 128] > 0)
               {
-                print_char (wintodos[s - 128]);
+                print_char(wintodos[s - 128]);
               }
               else
               {
@@ -320,6 +196,7 @@ void print_ (integer s)
               incr(j);
             }
           }
+
           new_line_char = nl; /* restore eol */
           return;
       }
@@ -335,21 +212,18 @@ void print_ (integer s)
   }
 }
 /* string version print. */
-void print_string_ (unsigned char *s)
+void print_string_ (const char *s)
 {
   while (*s > 0)
     print_char(*s++);
 }
 /* sec 0060 */
-// print string number s from string pool by calling print_
 void slow_print_ (integer s)
 {
   pool_pointer j;
 
   if ((s >= str_ptr) || (s < 256))
-  {
     print(s);
-  }
   else
   {
     j = str_start[s];
@@ -362,8 +236,7 @@ void slow_print_ (integer s)
   }
 }
 /* sec 0062 */
-// print newline followed by string number s (unless at start of line)
-void print_nl_ (const char * s)
+void print_nl (const char * s)
 {
   if (((term_offset > 0) && (odd(selector))) ||
       ((file_offset > 0) && (selector >= log_only)))
@@ -372,8 +245,7 @@ void print_nl_ (const char * s)
   print_string(s);
 }
 /* sec 0063 */
-// print string number s preceded by escape character
-void print_esc_ (const char * s)
+void print_esc (const char * s)
 {
   integer c;
 
@@ -466,7 +338,8 @@ void print_cs_ (integer p)
     print_esc("NONEXISTENT.");
   else
   {
-    print_esc(""); print(text(p));
+    print_esc("");
+    print(text(p));
     print_char(' ');
   }
 }
@@ -478,7 +351,8 @@ void sprint_cs_(halfword p)
       print(p - active_base);
     else if (p < null_cs)
     {
-      print_esc(""); print(p - single_base);
+      print_esc("");
+      print(p - single_base);
     }
     else
     {
@@ -491,7 +365,6 @@ void sprint_cs_(halfword p)
   }
 }
 /* sec 0518 */
-/* ! I can't find file `  c:/foo/  accents  .tex  '. */
 void print_file_name_(integer n, integer a, integer e)
 {
   slow_print(a);
@@ -509,9 +382,11 @@ void print_size_ (integer s)
     print_esc("scriptscriptfont");
 } 
 /* sec 1355 */
-void print_write_whatsit_(str_number s, halfword p)
+void print_write_whatsit_(str_number s, pointer p)
 {
-  print_esc(""); print(s);
+  print_esc("");
+  print(s);
+
   if (write_stream(p) < 16)
     print_int(write_stream(p)); 
   else if (write_stream(p) == 16)
@@ -519,8 +394,6 @@ void print_write_whatsit_(str_number s, halfword p)
   else print_char('-');
 }
 /* sec 0081 */
-// called from itex.c and tex0.c only  NASTY NASTY!
-// now uses uses non-local goto (longjmp) 1999/Nov/7
 void jump_out (void) 
 {
   close_files_and_terminate();
@@ -535,7 +408,7 @@ void jump_out (void)
     ready_already = 0;
 
     if (trace_flag)
-      show_line("EXITING at JUMPOUT\n", 0);
+      puts("EXITING at JUMPOUT");
 
     if ((history != 0) && (history != 1))
       code = 1;
@@ -562,21 +435,17 @@ void error (void)
   if (interaction == error_stop_mode)
     while (true)
     {
-lab22:
+continu:
       clear_for_error_prompt();
-
-      { /* prompt_input */
-        print_string("? ");
-        term_input("? ", help_ptr);
-      }
+      prompt_input("? ");
 
       if (last == first)
         return; // no input
 
-      c = buffer[first];   // analyze first letter typed
+      c = buffer[first];
 
-      if (c >= 'a')         // uppercase letter first
-        c = (unsigned char) (c + 'A' - 'a'); 
+      if (c >= 'a')
+        c = (c + 'A' - 'a'); 
 
       switch (c)
       {
@@ -600,9 +469,9 @@ lab22:
             OK_to_interrupt = false;
 
             if ((last > first + 1) && (buffer[first + 1] >= '0') && (buffer[first + 1] <= '9'))
-              c = (unsigned char) (c * 10 + buffer[first + 1] - '0' * 11);
+              c = (c * 10 + buffer[first + 1] - '0' * 11);
             else
-              c = (unsigned char) (c - 48);
+              c = (c - 48);
             
             while (c > 0)
             {
@@ -618,7 +487,7 @@ lab22:
             help2("I have just deleted some text, as you asked.",
                 "You can now delete more, or insert, or whatever.");
             show_context();
-            goto lab22;     /* loop again */
+            goto continu;
           }
           break;
 
@@ -626,10 +495,11 @@ lab22:
         case 'D':
           {
             debug_help();
-            goto lab22;       /* loop again */
+            goto continu;
           }
           break;
-#endif /* DEBUG */
+#endif
+
         case 'E':
           if (base_ptr > 0)
           {
@@ -652,18 +522,20 @@ lab22:
               if (help_ptr == 0)
                 help2("Sorry, I don't know how to help in this situation.",
                     "Maybe you should try asking a human?");
-              do {
-                decr(help_ptr);
-                print_string(help_line[help_ptr]);
-                print_ln();
-              } while (!(help_ptr == 0));
+              do
+                {
+                  decr(help_ptr);
+                  print_string(help_line[help_ptr]);
+                  print_ln();
+                }
+              while (!(help_ptr == 0));
             }
 
             help4("Sorry, I already gave what help I could...",
                 "Maybe you should try asking a human?",
                 "An error might have occurred before I noticed any problems.",
                 "``If all else fails, read the instructions.''");
-            goto lab22; /* loop again */
+            goto continu;
           }
           break;
 
@@ -673,19 +545,18 @@ lab22:
 
             if (last > first + 1)
             {
-              cur_input.loc_field = first + 1;
+              loc = first + 1;
               buffer[first] = 32;
             }
             else
             {
-              { /* prompt_input */
-                print_string("insert>");
-                term_input("insert>", 0);
-              }
-              cur_input.loc_field = first;
+              prompt_input("insert>");
+              loc = first;
             }
+
             first = last;
-            cur_input.limit_field = last - 1;
+            limit = last - 1;
+
             return;
           }
           break;
@@ -704,9 +575,11 @@ lab22:
                 print_esc("batchmode");
                 decr(selector);
                 break;
+
               case 'R':
                 print_esc("nonstopmode");
                 break;
+
               case 'S':
                 print_esc("scrollmode");
                 break;
@@ -714,9 +587,7 @@ lab22:
 
             print_string("...");
             print_ln();
-#ifndef _WINDOWS
-            fflush(stdout);
-#endif
+            update_terminal();
             return;
           }
           break;
@@ -745,7 +616,7 @@ lab22:
 
         print_nl("H for help, X to quit.");
       }
-    } /* end of while(true) loop */
+    }
 
   incr(error_count);
 
@@ -764,7 +635,7 @@ lab22:
     print_ln();
     give_err_help();
   }
-  else while(help_ptr > 0)
+  else while (help_ptr > 0)
   {
     decr(help_ptr);
     print_nl(help_line[help_ptr] == NULL ? "" : help_line[help_ptr]);
@@ -778,7 +649,7 @@ lab22:
   print_ln();
 }
 /* sec 0093 */
-void fatal_error_(char * s)
+void fatal_error(char * s)
 {
   normalize_selector();
   print_err("Emergency stop");
@@ -799,12 +670,12 @@ void overflow_(char * s, integer n)
 
   if (!knuth_flag)
   {
-    if (!strcmp(s, "pattern memory") && n == trie_size)
+    if (!strcmp(s, "pattern memory") && (n == trie_size))
     {
       sprintf(log_line, "\n  (Maybe use -h=... on command line in ini-TeX)\n");
       show_line(log_line, 0);
     }
-    else if (!strcmp(s, "exception dictionary") && n == hyphen_prime)
+    else if (!strcmp(s, "exception dictionary") && (n == hyphen_prime))
     {
       sprintf(log_line, "\n  (Maybe use -e=... on command line in ini-TeX)\n");
       show_line(log_line, 0);
@@ -814,7 +685,7 @@ void overflow_(char * s, integer n)
   succumb();
 }
 /* sec 0095 */
-void confusion_(char * s)
+void confusion_(const char * s)
 {
   normalize_selector();
 
@@ -835,64 +706,50 @@ void confusion_(char * s)
   succumb();
 }
 /* sec 0037 */
-bool init_terminal (void)
+boolean init_terminal (void)
 {
-  register bool Result;
   int flag;
+
   t_open_in();
 
   if (last > first)
   {
-    cur_input.loc_field = first;
+    loc = first;
 
-    while((cur_input.loc_field < last) && (buffer[cur_input.loc_field]== ' '))
-      incr(cur_input.loc_field);    // step over initial white space
+    while ((loc < last) && (buffer[loc]== ' '))
+      incr(loc);    // step over initial white space
 
-    if (cur_input.loc_field < last)
-    {
-      Result = true;
-      return Result;    // there is an input file name
-    }
+    if (loc < last)
+      return true;
   }
 
 // failed to find input file name
   while (true)
   {
-#ifdef _WINDOWS
-    flag = ConsoleInput("**", "Please type a file name or a control sequence\r\n(or ^z to exit)", (char *) &buffer[first]);
-    last = first + strlen((char *) &buffer[first]); /* -1 ? */
-//    may need to be more elaborate see input_line in texmf.c
-#else
-    (void) fputs("**", stdout);
-    fflush(stdout);
+    fputs("**", stdout);
+    update_terminal();
     flag = input_ln(stdin, true);
-#endif
 
     if (!flag)
     {
       show_char('\n');
-      show_line("! End of file on the terminal... why?\n", 1);
-      Result = false;
-      return Result;
+      puts("! End of file on the terminal... why?\n");
+      return false;
     }
 
-    cur_input.loc_field = first;
+    loc = first;
 
-    while ((cur_input.loc_field < last) && (buffer[cur_input.loc_field]== ' '))
-      incr(cur_input.loc_field);    // step over intial white space
+    while ((loc < last) && (buffer[loc]== ' '))
+      incr(loc);    // step over intial white space
 
-    if (cur_input.loc_field < last)
-    {
-      Result = true;
-      return Result;    // there is an input file name
-    }
+    if (loc < last)
+      return true;
 
     sprintf(log_line, "%s\n", "Please type the name of your input file.");
     show_line(log_line, 1);
   }
 }
 /* sec 0043 */
-// Make string from str_start[str_ptr] to pool_ptr
 str_number make_string (void)
 {
 #ifdef ALLOCATESTRING
@@ -902,26 +759,26 @@ str_number make_string (void)
   if (str_ptr == current_max_strings)
   {
     overflow("number of strings", current_max_strings - init_str_ptr); /* 97/Mar/9 */
-    return 0;     // abort_flag set
+    return 0;
   }
 #else
   if (str_ptr == max_strings)
   {
     overflow("number of strings", max_strings - init_str_ptr);
-    return 0;     // abort_flag set
+    return 0;
   }
 #endif
+
   incr(str_ptr);
   str_start[str_ptr] = pool_ptr;
 
   return (str_ptr - 1);
 }
 /* sec 0044 */
-bool str_eq_buf_ (str_number s, integer k)
+boolean str_eq_buf_ (str_number s, integer k)
 {
-  register bool Result;
   pool_pointer j;
-  bool result;
+  boolean result;
 
   j = str_start[s];
 
@@ -930,28 +787,28 @@ bool str_eq_buf_ (str_number s, integer k)
     if (str_pool[j] != buffer[k])
     {
       result = false;
-      goto lab45;
+      goto not_found;
     }
 
     incr(j);
     incr(k);
   }
+
   result = true;
-lab45:
-  Result = result;
-  return Result;
+
+not_found:
+  return result;
 }
 /* sec 0045 */
-bool str_eq_str_ (str_number s, str_number t)
+boolean str_eq_str_ (str_number s, str_number t)
 {
-  register bool Result;
   pool_pointer j, k;
-  bool result;
+  boolean result;
 
   result = false;
 
   if (length(s) != length(t))
-    goto lab45;
+    goto not_found;
 
   j = str_start[s];
   k = str_start[t];
@@ -959,15 +816,16 @@ bool str_eq_str_ (str_number s, str_number t)
   while (j < str_start[s + 1])
   {
     if (str_pool[j] != str_pool[k])
-      goto lab45;
+      goto not_found;
 
     incr(j);
     incr(k);
   }
+
   result = true;
-lab45:
-  Result = result;
-  return Result;
+
+not_found:
+  return result;
 }
 /* sec 0066 */
 void print_two_(integer n)
@@ -1049,177 +907,25 @@ void print_current_string (void)
   }
 }
 
-int stringlength (int str_ptr)
-{
-  return (str_start[str_ptr + 1] - str_start[str_ptr]) + 2;
-}
-
-char * add_string (char *s, char * str_string)
-{
-  int n;
-
-  n = strlen(str_string);
-  memcpy(s, &str_string, n);
-  s += n;
-  strcpy(s, "\r\n");
-  s += 2;
-  return s;
-}
-
-int addextrahelp = 1;
-
-// make one long \r\n separated string out of help lines 
-// str_pool is packed_ASCII_code *
-
-char * make_up_help_string (int nhelplines)
-{
-  char * helpstring, *s;
-  int k, nlen = 0;
-  
-//  get length of help for this specific message
-  for (k = nhelplines - 1; k >= 0; k--)
-  {
-    nlen += strlen(help_line[k]);
-  }
-
-  nlen += 2; // for blank line separator: "\r\n"
-
-  if (addextrahelp)
-  {
-    nlen += stringlength(265);
-    nlen += stringlength(266);
-    nlen += stringlength(267);
-
-    if (base_ptr > 0)
-      nlen += stringlength(268);
-
-    if (deletions_allowed)
-      nlen += stringlength(269);
-
-    nlen += stringlength(270);
-  }
-
-  helpstring = (char *) malloc(nlen + 1); // +1 = '\0'
-  s = helpstring;
-
-  for (k = nhelplines-1; k >= 0; k--)
-  {
-    s = add_string(s, help_line[k]);
-  }
-
-  if (addextrahelp)
-  {
-    strcpy(s, "\r\n");
-    s += 2;
-    s = add_string(s, "Type <return> to proceed, S to scroll future error messages,");
-    s = add_string(s, "R to run without stopping, Q to run quietly,");
-    s = add_string(s, "I to insert something, ");
-
-    if (base_ptr > 0)
-      s = add_string(s, "E to edit your file, ");
-
-    if (deletions_allowed)
-      s = add_string(s, "1 or ... or 9 to ignore the next 1 to 9 tokens of input,");
-
-    s = add_string(s, "H for help, X to quit.");
-  }
-
-  return helpstring;
-}
-
-char * make_up_query_string (int promptstr)
-{
-  char *querystr;
-  int nstart, nnext, n;
-  char *s;
-
-  nstart = str_start[ promptstr];
-  nnext = str_start[ promptstr + 1];
-  n = nnext - nstart;
-  querystr = (char *) malloc(n + 1);
-  s = querystr;
-  memcpy(s, &str_pool[nstart], n);  
-  s += n;
-  *s = '\0';
-
-  return querystr;
-}
-
-// abort_flag set if input_line / ConsoleInput returns non-zero
-// should set interrupt instead ???
-// called from tex0.c, tex2.c, tex3.c
 /* sec 0071 */
-// void term_input(void)
-void term_input (char * term_str, int term_help_lines)
+void term_input(void)
 { 
   integer k;
   int flag;
-  char * helpstring = NULL;
-  char * querystring = NULL;
-//  if (nhelplines != 0) {
-//    helpstring = make_up_help_string (nhelplines);
-//    printf(helpstring);
-//    free(helpstring);
-//  }
-  show_line("\n", 0);    // force it to show what may be buffered up ???
-  helpstring = NULL;
-
-#ifdef _WINDOWS
-  if (term_str != NULL)
-    querystring = term_str;
-
-  if (term_help_lines != NULL)
-    helpstring = make_up_help_string(term_help_lines);
-
-  if (helpstring == NULL && querystring != NULL)
-  {
-    if (strcmp(querystring, ": ") == 0)
-      helpstring = xstrdup("Please type another file name (or ^z to exit):");
-    else if (strcmp(querystring, "=>") == 0)    // from firm_up_the_line
-      helpstring = xstrdup("Please type <enter> to accept this line\r\nor type a replacement line");
-    else if (strcmp(querystring, "insert>") == 0) // from error() after "I"
-      helpstring = xstrdup("Please type something to insert here");
-    else if (strcmp(querystring, "") == 0)      // from read_toks
-      helpstring = xstrdup("Please type a control sequence");
-    else if (strcmp(querystring, "= ") == 0)    // from read_toks
-      helpstring = xstrdup("Please type a token");
-    else if (strcmp(querystring, "*") == 0)   // get_next
-      helpstring = xstrdup("Please type a control sequence\r\n(or ^z to exit)");
-  }
-
-  flag = ConsoleInput(querystring, helpstring, (char *) &buffer[first]);  // ???
-//  flag == 0 means trouble --- EOF on terminal
-  if (querystring != NULL)
-    free(querystring);
-
-  if (helpstring != NULL)
-    free(helpstring);
-
-  helpstring = querystring = NULL;
+  
+  if (!knuth_flag)
+    show_line("\n", 0);
 
-  last = first + strlen((char *) &buffer[first]); /* -1 ? */
-//  flag = (last > first);
-//  may need to be more elaborate see input_line in texmf.c ???
-//  sprintf(log_line, "first %d last %d flag %d - %s",
-//      first, last, flag, (char *) &buffer[first]);
-//  winerror(log_line);
-#else
-  fflush(stdout);
+  update_terminal();
   flag = input_ln(stdin, true);
-#endif
+
   if (!flag)
   {
     fatal_error("End of file on the terminal!");
-    return;         // abort_flag set
+    return;
   }
+
   term_offset = 0;
-#ifdef _WINDOWS
-// echo what was typed into Console buffer also
-  if (last != first)
-    for (k = first; k <= last - 1; k++)
-      print(buffer[k]);
-  print_ln();
-#else
   decr(selector);     // shut off echo
 
   if (last != first)
@@ -1228,7 +934,6 @@ void term_input (char * term_str, int term_help_lines)
 
   print_ln();
   incr(selector);     // reset selector again
-#endif
 }
 /* sec 0091 */
 void int_error_ (integer n)
@@ -1296,7 +1001,6 @@ scaled round_decimals_(small_number k)
   return ((a + 1) / 2);
 }
 /* sec 0103 */
-/* This has some minor speedup changes - no real advantage probably ... */
 void print_scaled_(scaled s)
 {
   scaled delta;
@@ -1316,6 +1020,7 @@ void print_scaled_(scaled s)
     {
       if (delta > 65536L)
         s = s - 17232; /* 2^15 - 50000 - rounding */
+
       print_char('0' + (s / 65536L));
       s = 10 * (s % 65536L);
       delta = delta * 10;
@@ -1345,7 +1050,7 @@ scaled mult_and_add_(integer n, scaled x, scaled y, scaled maxanswer)
 scaled x_over_n_(scaled x, integer n)
 {
   register scaled Result;
-  bool negative;
+  boolean negative;
 
   negative = false;
 
@@ -1385,7 +1090,7 @@ scaled x_over_n_(scaled x, integer n)
 scaled xn_over_d_(scaled x, integer n, integer d)
 {
   register scaled Result;
-  bool positive;
+  boolean positive;
   nonnegative_integer t, u, v;
 
   if (x >= 0)
@@ -1396,9 +1101,9 @@ scaled xn_over_d_(scaled x, integer n, integer d)
     positive = false;
   }
 
-  t =(x % 32767L) * n;
-  u =(x / 32768L)* n +(t / 32768L);
-  v =(u % d)* 32768L +(t % 32768L); 
+  t = (x % 32767L) * n;
+  u = (x / 32768L) * n + (t / 32768L);
+  v = (u % d) * 32768L + (t % 32768L); 
 
   if (u / d >= 32768L)
     arith_error = true; 
@@ -1452,13 +1157,13 @@ void print_word_(memory_word w)
   print_char(' ');
   print_scaled(round(65536L * w.gr));
   print_ln();
-  print_int(w.hh.v.LH);
+  print_int(w.hh.lh);
   print_char('=');
   print_int(w.hh.b0);
   print_char(':');
   print_int(w.hh.b1);
   print_char(';');
-  print_int(w.hh.v.RH);
+  print_int(w.hh.rh);
   print_char(' ');
   print_int(w.qqqq.b0); 
   print_char(':');
@@ -1469,7 +1174,7 @@ void print_word_(memory_word w)
   print_int(w.qqqq.b3);
 } 
 /* need this version only if SHORTFONTINFO defined */
-void zprintfword(fmemoryword w)
+void zprintfword(memory_word w)
 {
   print_int(w.cint);
   print_char(' ');
@@ -1477,13 +1182,13 @@ void zprintfword(fmemoryword w)
   print_char(' ');
   print_scaled(round(65536L * w.gr));
   print_ln();
-  print_int(w.hh.v.LH);
+  print_int(w.hh.lh);
   print_char('=');
   print_int(w.hh.b0);
   print_char(':');
   print_int(w .hh.b1);
   print_char(';');
-  print_int(w.hh.v.RH);
+  print_int(w.hh.rh);
   print_char(' ');
   print_int(w.qqqq.b0);
   print_char(':');
@@ -1531,56 +1236,55 @@ void show_token_list_(integer p, integer q, integer l)
 
       if (info(p) < 0)
         print_esc("BAD.");
-      else
-        switch (m)
-        {
-          case left_brace:
-          case right_brace:
-          case math_shift:
-          case tab_mark:
-          case sup_mark:
-          case sub_mark:
-          case spacer:
-          case letter:
-          case other_char:
-            print(c);
-            break;
-
-          case mac_param:
-            print(c);
-            print(c);
-            break;
-
-          case out_param:
-            print(match_chr);
-
-            if (c <= 9)
-              print_char(c + '0');
-            else
-            {
-              print_char('!');
-              return;
-            }
-            break;
-
-          case match:
-            match_chr = (ASCII_code) c;
-            print(c);
-            incr(n);
-            print_char(n);
-
-            if (n > '9')
-              return;
-            break;
-
-          case end_match:
-            print_string("->");
-            break;
-
-          default:
-            print_esc("BAD.");
-            break;
-        }
+      else switch (m)
+      {
+        case left_brace:
+        case right_brace:
+        case math_shift:
+        case tab_mark:
+        case sup_mark:
+        case sub_mark:
+        case spacer:
+        case letter:
+        case other_char:
+          print(c);
+          break;
+        
+        case mac_param:
+          print(c);
+          print(c);
+          break;
+        
+        case out_param:
+          print(match_chr);
+          
+          if (c <= 9)
+            print_char(c + '0');
+          else
+          {
+            print_char('!');
+            return;
+          }
+          break;
+        
+        case match:
+          match_chr = (ASCII_code) c;
+          print(c);
+          incr(n);
+          print_char(n);
+          
+          if (n > '9')
+            return;
+          break;
+        
+        case end_match:
+          print_string("->");
+          break;
+        
+        default:
+          print_esc("BAD.");
+          break;
+      }
     }
     p = link(p);
   }
@@ -1635,7 +1339,7 @@ void runaway (void)
 /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
 halfword get_avail (void)
 {
-  halfword p;
+  pointer p;
 
   p = avail;
 
@@ -1663,8 +1367,9 @@ halfword get_avail (void)
       {
         runaway();
         overflow("main memory size", mem_max + 1 - mem_min);
-        return 0;           // abort_flag set
+        return 0;
       }
+
       incr(mem_end);        /* then grab from new area */
       p = mem_end;          /* 1993/Dec/14 */
     }
@@ -1679,9 +1384,9 @@ halfword get_avail (void)
   return p; 
 } 
 /* sec 0123 */
-void flush_list_(halfword p)          /* paragraph 123 */
+void flush_list_(pointer p)
 { 
-  halfword q, r;
+  pointer q, r;
 
   if (p != 0)              /* null !!! */
   {
@@ -1702,14 +1407,13 @@ void flush_list_(halfword p)          /* paragraph 123 */
   }
 }
 /* sec 0125 */
-halfword get_node_(integer s)
+pointer get_node_(integer s)
 {
-  register halfword Result;
-  halfword p;
-  halfword q;
+  pointer p;
+  pointer q;
   integer r;
   integer t;
-lab20:
+restart:
 
   p = rover;
 
@@ -1717,7 +1421,7 @@ lab20:
     {
       q = p + node_size(p);
 
-      while ((mem[q].hh.v.RH == empty_flag))
+      while ((mem[q].hh.rh == empty_flag))
       {
         t = rlink(q);
 
@@ -1735,7 +1439,7 @@ lab20:
       {
         node_size(p) = r - p;
         rover = p;
-        goto lab40;
+        goto found;
       }
 
       if (r == p)
@@ -1745,7 +1449,7 @@ lab20:
           t = llink(p);
           llink(rover) = t;
           rlink(t) = rover;
-          goto lab40;
+          goto found;
         }
 
       node_size(p) = q - p;
@@ -1755,12 +1459,10 @@ lab20:
 
   if (s == 1073741824L)    /* 2^30 - special case - merge adjacent */
   {
-    Result = max_halfword;
-
     if (trace_flag)
-      show_line("Merged adjacent multi-word nodes\n", 0);
+      puts("Merged adjacent multi-word nodes\n");
 
-    return Result;
+    return max_halfword;
   }
 
 /*  maybe try downward epxansion first instead ? */
@@ -1790,7 +1492,7 @@ lab20:
       link(lo_mem_max) = 0;
       info(lo_mem_max) = 0;
       rover = q;
-      goto lab20;
+      goto restart;
     }
 
 /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
@@ -1815,26 +1517,25 @@ lab20:
     }
 
     overflow("main memory size", mem_max + 1 - mem_min); /* darn: allocation failed ! */
-    return 0;     // abort_flag set
+    return 0;
   }
-/* avoid function call in following ? */
+
   add_variable_space (block_size); /* now to be found in itex.c */
-  goto lab20;         /* go try get_node again */
+  goto restart;         /* go try get_node again */
 
-lab40:
+found:
   link(r) = 0;
 
 #ifdef STAT
   var_used = var_used + s; 
 #endif /* STAT */
 
-  Result = r; 
-  return Result; 
+  return r; 
 } 
 /* sec 0130 */
 void free_node_(halfword p, halfword s)
 { 
-  halfword q;
+  pointer q;
 
   node_size(p) = s;
   link(p) = empty_flag;
@@ -1843,14 +1544,15 @@ void free_node_(halfword p, halfword s)
   rlink(p) = rover;
   llink(rover) = p;
   rlink(q) = p;
+
 #ifdef STAT
   var_used = var_used - s; 
 #endif /* STAT */
 }
 /* sec 0136 */
-halfword new_null_box (void) 
+pointer new_null_box (void) 
 {
-  halfword p;
+  pointer p;
 
   p = get_node(box_node_size);
   type(p) = hlist_node;
@@ -1867,9 +1569,9 @@ halfword new_null_box (void)
   return p;
 }
 /* sec 0139 */
-halfword new_rule (void) 
+pointer new_rule (void) 
 {
-  halfword p;
+  pointer p;
 
   p = get_node(rule_node_size);
   type(p) = rule_node;
@@ -1881,9 +1583,9 @@ halfword new_rule (void)
   return p;
 }
 /* sec 0144 */
-halfword new_ligature_(quarterword f, quarterword c, halfword q)
+pointer new_ligature_(quarterword f, quarterword c, pointer q)
 {
-  halfword p;
+  pointer p;
 
   p = get_node(small_node_size);
   type(p) = ligature_node;
@@ -1895,9 +1597,9 @@ halfword new_ligature_(quarterword f, quarterword c, halfword q)
   return p;
 }
 /* sec 0144 */
-halfword new_lig_item_(quarterword c)
+pointer new_lig_item_(quarterword c)
 {
-  halfword p;
+  pointer p;
 
   p = get_node(small_node_size);
   character(p) = c;
@@ -1906,9 +1608,9 @@ halfword new_lig_item_(quarterword c)
   return p;
 }
 /* sec 0145 */
-halfword new_disc (void) 
+pointer new_disc (void) 
 {
-  halfword p;
+  pointer p;
 
   p = get_node(small_node_size);
   type(p) = disc_node;
@@ -1919,9 +1621,9 @@ halfword new_disc (void)
   return p;
 }
 /* sec 0147 */
-halfword new_math_(scaled w, small_number s)
+pointer new_math_(scaled w, small_number s)
 {
-  halfword p;
+  pointer p;
 
   p = get_node(small_node_size);
   type(p) = math_node;
@@ -1931,9 +1633,9 @@ halfword new_math_(scaled w, small_number s)
   return p;
 }
 /* sec 0151 */
-halfword new_spec_(halfword p)
+pointer new_spec_(pointer p)
 {
-  halfword q;
+  pointer q;
 
   q = get_node(glue_spec_size);
   mem[q] = mem[p];
@@ -1945,10 +1647,10 @@ halfword new_spec_(halfword p)
   return q;
 }
 /* se 0152 */
-halfword new_param_glue_(small_number n)
+pointer new_param_glue_(small_number n)
 {
-  halfword p;
-  halfword q;
+  pointer p;
+  pointer q;
 
   p = get_node(small_node_size);
   type(p) = glue_node;
@@ -1961,9 +1663,9 @@ halfword new_param_glue_(small_number n)
   return p;
 }
 /* sec 0153 */
-halfword new_glue_(halfword q)
+pointer new_glue_(pointer q)
 {
-  halfword p;
+  pointer p;
 
   p = get_node(small_node_size);
   type(p) = glue_node;
@@ -1975,9 +1677,9 @@ halfword new_glue_(halfword q)
   return p;
 }
 /* sec 0154 */
-halfword new_skip_param_(small_number n)
+pointer new_skip_param_(small_number n)
 {
-  halfword p;
+  pointer p;
 
   temp_ptr = new_spec(glue_par(n));
   p = new_glue(temp_ptr); 
@@ -1987,9 +1689,9 @@ halfword new_skip_param_(small_number n)
   return p;
 }
 /* sec 0155 */
-halfword new_kern_(scaled w)
+pointer new_kern(scaled w)
 {
-  halfword p;
+  pointer p;
 
   p = get_node(small_node_size);
   type(p) = kern_node;
@@ -1999,9 +1701,9 @@ halfword new_kern_(scaled w)
   return p;
 }
 /* sec 0158 */
-halfword new_penalty_(integer m)
+pointer new_penalty(integer m)
 {
-  halfword p;
+  pointer p;
 
   p = get_node(small_node_size);
   type(p) = penalty_node;
@@ -2013,10 +1715,10 @@ halfword new_penalty_(integer m)
 
 #ifdef DEBUG
 /* sec 0167 */
-void check_mem_(bool printlocs)
+void check_mem(boolean printlocs)
 {
-  halfword p, q;
-  bool clobbered;
+  pointer p, q;
+  boolean clobbered;
 
   for (p = mem_min; p <= lo_mem_max; p++) freearr[p] = false;
   for (p = hi_mem_min; p <= mem_end; p++) freearr[p] = false;
@@ -2033,13 +1735,13 @@ void check_mem_(bool printlocs)
     {
       print_nl("AVAIL list clobbered at ");
       print_int(q);
-      goto lab31;
+      goto done1;
     }
     freearr[p] = true;
     q = p;
     p = link(q);
   }
-lab31:;
+done1:;
   p = rover;
   q = 0;
   clobbered = false;
@@ -2056,7 +1758,7 @@ lab31:;
       {
         print_nl("Double-AVAIL list clobbered at ");
         print_int(q);
-        goto lab32;
+        goto done2;
       }
 
       for (q = p; q <= p + node_size(p) - 1; q++)
@@ -2065,14 +1767,14 @@ lab31:;
         {
           print_nl("Doubly free location at ");
           print_int(q);
-          goto lab32;
+          goto done2;
         }
         freearr[q]= true;
       }
       q = p;
       p = rlink(p);
   } while (!(p == rover));
-lab32:;
+done2:;
   p = mem_min;
   while (p <= lo_mem_max) {
     if (is_empty(p))
@@ -2183,7 +1885,8 @@ void short_display_(integer p)
 {
   integer n; 
 
-  while (p != 0) {      /* want p != null here bkph 93/Dec/15 !!! NOTE: still not fixed in 3.14159 ! */
+  while (p != 0) /* want p != null here ! */
+  {
      if (is_char_node(p))
      {
        if (p <= mem_end)
@@ -2197,6 +1900,7 @@ void short_display_(integer p)
              print_esc("");
              print(font_id_text(font(p)));
            }
+
            print_char(' ');
            font_in_short_display = font(p);
          }
@@ -2232,9 +1936,11 @@ void short_display_(integer p)
         short_display(post_break(p));
         n = replace_count(p);
 
-        while (n > 0) {
-          if (link(p) != 0) /* if link(p)<>null then */
+        while (n > 0)
+        {
+          if (link(p) != 0)
             p = link(p);
+
           decr(n);
         }
         break;
@@ -2245,7 +1951,7 @@ void short_display_(integer p)
   }
 }
 /* sec 0176 */
-void print_font_and_char_ (integer p)
+void print_font_and_char (integer p)
 {
   if (p > mem_end)
     print_esc("CLOBBERED.");
@@ -2268,7 +1974,7 @@ void print_mark_ (integer p)
 { 
   print_char('{');
 
-  if ((p < hi_mem_min)||(p > mem_end))
+  if ((p < hi_mem_min) || (p > mem_end))
     print_esc("CLOBBERED.");
   else
     show_token_list(link(p), 0, max_print_line - 10);
@@ -2276,7 +1982,7 @@ void print_mark_ (integer p)
   print_char('}');
 }
 /* sec 0176 */
-void print_rule_dimen(scaled d)
+void print_rule_dimen(scaled d)
 {
   if ((d == -1073741824L)) /* - 2^30 */
     print_char('*');
@@ -2294,17 +2000,19 @@ void print_glue_(scaled d, integer order, char * s)
   {
     print_string("fil");
 
-    while (order > 1) {
+    while (order > 1)
+    {
       print_char('l');
       decr(order);
     }
-  } else if (*s != '\0')
+  }
+  else if (*s != '\0')
     print_string(s);
 }
 /* sec 0178 */
 void print_spec_(integer p, char * s)
 {
-  if ((p < mem_min)||(p >= lo_mem_max)) 
+  if ((p < mem_min) || (p >= lo_mem_max)) 
     print_char('*');
   else
   {
@@ -2340,7 +2048,7 @@ void print_delimiter_(halfword p)
   integer a;
 
   a = small_fam(p) * 256 + small_char(p);
-  a = a * 4096 + large_fam(p) * 256 + large_char(p);
+  a = a * 0x1000 + large_fam(p) * 256 + large_char(p);
 
   if (a < 0)
     print_int(a);
@@ -2502,9 +2210,9 @@ void show_node_list_(integer p)
 
   if (cur_length > depth_threshold)
   {
-/*  if (p > 0) */  /* was p>null !!! line 3662 in tex.web */
     if (p != 0)    /* fixed 94/Mar/23 BUG FIX NOTE: still not fixed in 3.14159 ! */
-    print_string(" []");
+      print_string(" []");
+
     return; 
   }
 
@@ -2528,7 +2236,7 @@ void show_node_list_(integer p)
       return;
     }
 
-    if ((p >= hi_mem_min))
+    if (is_char_node(p))
       print_font_and_char(p);
     else switch (type(p))
     {
@@ -2588,10 +2296,10 @@ void show_node_list_(integer p)
                 else
                   print_string("< -");
 
-                print_glue(20000 * 65536L, glue_order(p), "");
+                print_glue(20000 * unity, glue_order(p), "");
               }
               else
-                print_glue(round(65536L * g), glue_order(p), "");
+                print_glue(round(unity * g), glue_order(p), "");
             }
 
             if (shift_amount(p) != 0)
@@ -2606,7 +2314,7 @@ void show_node_list_(integer p)
               str_pool[pool_ptr] = 46;
               incr(pool_ptr);
             }
-            show_node_list(mem[p + 5].hh.v.RH);
+            show_node_list(mem[p + 5].hh.rh);
             decr(pool_ptr);
           }
         }
@@ -2640,7 +2348,7 @@ void show_node_list_(integer p)
               str_pool[pool_ptr] = 46;
               incr(pool_ptr);
             }
-            show_node_list(mem[p + 4].hh.v.LH);
+            show_node_list(mem[p + 4].hh.lh);
             decr(pool_ptr);
           }
         }
@@ -2710,7 +2418,7 @@ void show_node_list_(integer p)
               str_pool[pool_ptr] = 46;
               incr(pool_ptr);
             }
-            show_node_list(mem[p + 1].hh.v.RH);
+            show_node_list(mem[p + 1].hh.rh);
             decr(pool_ptr);
           }
         }
@@ -2821,14 +2529,14 @@ void show_node_list_(integer p)
               str_pool[pool_ptr] = 46;
               incr(pool_ptr);
             }
-            show_node_list(mem[p + 1].hh.v.LH);
+            show_node_list(mem[p + 1].hh.lh);
             decr(pool_ptr);
           }
           {
             str_pool[pool_ptr]= 124;
             incr(pool_ptr);
           }
-          show_node_list(mem[p + 1].hh.v.RH);
+          show_node_list(mem[p + 1].hh.rh);
           decr(pool_ptr);
         }
         break;
@@ -2998,7 +2706,7 @@ void show_node_list_(integer p)
           }
 
           if ((small_fam(right_delimiter(p)) != 0) || (small_char(right_delimiter(p)) != 0) ||
-              (large_fam(right_delimiter(p)) != 0)||(large_char(right_delimiter(p)) != 0))
+              (large_fam(right_delimiter(p)) != 0) || (large_char(right_delimiter(p)) != 0))
           {
             print_string(", right-delimiter ");
             print_delimiter(right_delimiter(p));