home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Tex / Tex29 / StTeXsrc.zoo / src / tex.c < prev    next >
C/C++ Source or Header  |  1989-09-21  |  10KB  |  388 lines

  1.  
  2. /*
  3.  * @(#)tex.c 2.9 EPA
  4.  *
  5.  * Copyright 1987,1988 Pat J Monardo
  6.  *
  7.  * Redistribution of this file is permitted through
  8.  * the specifications in the file COPYING.
  9.  *
  10.  * 
  11.  */
  12.  
  13. #include "tex.h"
  14. #include "texext.h"
  15. #include "evalstack.h"
  16. #include "eqstack.h"
  17. #include "tokenstack.h"
  18. #include "token.h"
  19. #include "box.h"
  20. #include "pack.h"
  21. #include "cond.h"
  22. #include "file.h"
  23. #include "tfm.h"
  24. #include "hyph.h"
  25. #include "dvi.h"
  26. #include "fmt.h"
  27. #include "page.h"
  28.  
  29. char        banner[] = "This is Common TeX, Version 2.9";
  30. int         ready_already;
  31.  
  32. #ifdef atarist
  33. long _stksize = 16384;
  34. #endif
  35.  
  36. main (argc, argv)
  37.     int     argc;
  38.     char    **argv;
  39. {
  40. #ifdef atarist
  41.     extern size_t __DEFAULT_BUFSIZ__;
  42.     __DEFAULT_BUFSIZ__ = 4096;
  43.     _binmode(1);
  44. #endif
  45.     job_name = 0;
  46.     history = FATAL_ERROR_STOP;
  47.     signal(SIGINT, handle_int);
  48.     set_paths();
  49.     adjust_tail = NULL;
  50.     page_tail = page_head;
  51.     mode = VMODE;
  52.     head = tail = contrib_head;
  53.     prev_depth = IGNORE_DEPTH;
  54.     mode_line = 0;
  55.     t_open_out();
  56.     term_offset = file_offset = 0;
  57.     for (first = 0; first < BUF_SIZE; incr(first))
  58.         buffer[first] = 0;
  59.     first = 1;
  60.     state = NEW_LINE;
  61.     start = 1;
  62.     if (ready_already != 314159) 
  63.         initialize();
  64.     ready_already = 314159;
  65.     selector = TERM_ONLY;
  66.     tally = 0; term_offset = 0; file_offset = 0;
  67.     print(banner);
  68.     if (format_ident == 0)
  69.         print(" (no format preloaded)");
  70.     else print_str(format_ident);
  71.     print_ln();
  72.     if (!decode_args(argc, argv))
  73.         if (!init_terminal())
  74.             exit(history);
  75.     limit = last;
  76.     first = last + 1;
  77.     if (format_ident == 0 || buffer[loc] == '&') {
  78.         if (!open_fmt_file()) exit(history);
  79.         if (!load_fmt_file()) exit(history);
  80.         w_close(fmt_file);
  81.         while (loc < limit && buffer[loc] == ' ')
  82.             incr(loc);
  83.     }
  84.     if (end_line_char < 0 || end_line_char > 127)
  85.         decr(limit);
  86.     else buffer[limit] = end_line_char;
  87.     if (interaction == BATCH_MODE)
  88.         selector = NO_PRINT;
  89.     else selector = TERM_ONLY;
  90.     fix_date_and_time();
  91.     if (loc < limit && cat_code(buffer[loc]) != ESCAPE)
  92.         start_input();
  93.     history = SPOTLESS;
  94.     main_control();
  95.     final_cleanup();
  96.     close_files_and_terminate(FALSE);
  97. }
  98.  
  99. #define USAGE   "usage: %s [ -d dir ] [ file ]\n"
  100.  
  101. bool
  102. decode_args (argc, argv)
  103.     int     argc;
  104.     char*   *argv;
  105. {
  106.     int     j;
  107.     char*   ap;
  108.     char*   pgm;
  109.     char    dir[MAX_PATH_CHARS];
  110.  
  111.     ap = pgm = argv[0];
  112.     while (*pgm++ != NUL)
  113.         if (*pgm == '/' || *pgm == '\\')
  114.             ap = pgm, ap++;
  115.     pgm = ap;
  116.  
  117.     decr(argc), incr(argv);
  118.     if (argc) {
  119.         last = first;
  120.         while (argc) {
  121.             if (argv[0][0] == '-') {
  122.                 for (ap = *argv + 1; *ap != NUL; incr(ap)) {
  123.                     switch (*ap)
  124.                     {
  125.                     case 'd':
  126.                         decr(argc), incr(argv);
  127.                         strcpy(dir, *argv);
  128. #ifdef atarist
  129.                         strcat(dir, ";");
  130. #else
  131.                         strcat(dir, ":");
  132. #endif
  133.                         strcat(dir, input_path);
  134.                         strcpy(input_path, dir);
  135.                         break;
  136.  
  137.                     default:
  138.                         fprintf(stderr, USAGE, pgm);
  139.                         exit(history);
  140.                     }
  141.                 }
  142.             } else {
  143.                 j = 0;
  144.                 while (j <= FILE_NAME_SIZE && argv[0][j] != NUL) {
  145.                     buffer[last] = argv[0][j];
  146.                     incr(last), incr(j);
  147.                 }
  148.                 if (j > 0)
  149.                     buffer[last++] = ' ';
  150.             }
  151.             decr(argc), incr(argv);
  152.         }
  153.         if (last > first) {
  154.             loc = first;
  155.             return TRUE;
  156.         }
  157.     }
  158.     return FALSE;
  159. }
  160.  
  161. fix_date_and_time ()
  162. {
  163.     val        clock;
  164.     struct tm   *tmptr, *localtime();
  165.     
  166.     clock = begintime();
  167.     tmptr = localtime(&clock);
  168.     time = 60 * tmptr->tm_hour + tmptr->tm_min;
  169.     day = tmptr->tm_mday;
  170.     month = tmptr->tm_mon + 1;
  171.     year = tmptr->tm_year + 1900;
  172. }
  173.  
  174. #undef time
  175. begintime()
  176. {
  177.     return (time(0));
  178. }
  179.  
  180. final_cleanup ()
  181. {
  182.     int     c;
  183.     
  184.     c = cur_chr;
  185.     if (job_name == 0)
  186.         open_log_file();
  187.     if (cur_level > LEVEL_ONE) {
  188.         print_nl("(");
  189.         print_esc("end occurred ");
  190.         print("inside a group at level ");
  191.         print_int(cur_level - LEVEL_ONE);
  192.         print_char(')');
  193.     }
  194.     while (cond_ptr != NULL) {
  195.         print_nl("(");
  196.         print_esc("end occurred ");
  197.         print("when ");
  198.         print_cmd_chr(IF_TEST, cur_if);
  199.         if (if_line != 0) {
  200.             print(" on line ");
  201.             print_val(if_line);
  202.         }
  203.         print(" was incomplete)");
  204.         if_line = if_line_field(cond_ptr); 
  205.         cur_if = subtype(cond_ptr);
  206.         cond_ptr = link(cond_ptr);
  207.     }
  208.     if (history != SPOTLESS && 
  209.         (history == WARNING_ISSUED || interaction < ERROR_STOP_MODE) &&
  210.         selector == TERM_AND_LOG) {
  211.         selector = TERM_ONLY;
  212.         print_nl("(see the transcript file for additional information)");
  213.         selector = TERM_AND_LOG;
  214.     }
  215.     if (c == 1)
  216. #ifdef INIT
  217.         store_fmt_file();
  218. #else
  219.         print_nl("(\\dump is performed only by INITEX)");
  220. #endif
  221. }
  222.  
  223. close_files_and_terminate (edit)
  224.     bool    edit;
  225. {
  226.     int     k;
  227.     
  228.     for (k = 0; k < 16; incr(k))
  229.         if (write_open[k])
  230.             a_close(write_file[k]);
  231. #ifdef STAT
  232.     if (tracing_stats > 0 && job_name > 0)  {
  233.         int save_selector = selector;
  234.         selector = LOG_ONLY;
  235.         print_ln();
  236.         print(" Here is how much of TeX's memory you used:");
  237.         print_ln();
  238.         print_int(str_ptr);
  239.         print(" strings out of ");
  240.         print_int(MAX_STRINGS);
  241.         print_ln();
  242.         print_int(pool_ptr);
  243.         print(" string characters out of ");
  244.         print_int(POOL_SIZE);
  245.         print_ln();
  246.         print_int(lo_mem_max - MEM_MIN + mem_end - hi_mem_min);
  247.         print(" words of memory out of ");
  248.         print_int(mem_end + 1 - MEM_MIN);
  249.         print_ln();
  250.         print_int(tok_end + 1 - tok_low);
  251.         print(" words of token memory out of ");
  252.         print_int(TOK_MAX + 1 - TOK_MIN);
  253.         print_ln();
  254.         print_int(cs_count);
  255.         print(" multiletter control sequences out of ");
  256.         print_int(HASH_SIZE);
  257.         print_ln();
  258.         print_int(fmem_ptr);
  259.         print(" words of font info for ");
  260.         print_int(font_ptr - FONT_BASE);
  261.         print(" font");
  262.         if (font_ptr != FONT_BASE + 1)
  263.             print_char('s'); 
  264.         print(", out of ");
  265.         print_int(FONT_MEM_SIZE);
  266.         print(" for ");
  267.         print_int(FONT_MAX - FONT_BASE);
  268.         print_ln();
  269.         print_int(hyph_count);
  270.         print(" hyphenation exception");
  271.         if (hyph_count != 1) print_char('s');
  272.         print(" out of ");
  273.         print_int(HYPH_SIZE);
  274.         print_ln();
  275.         print_int(max_in_stack); print("i,");
  276.         print_int(max_nest_stack); print("n,");
  277.         print_int(max_param_stack); print("p,");
  278.         print_int(max_buf_stack + 1); print("b,");
  279.         print_int(max_save_stack + 6); print("s ");
  280.         print("stack positions out of ");
  281.         print_int(STACK_SIZE); print("i,");
  282.         print_int(NEST_SIZE); print("n,");
  283.         print_int(PARAM_SIZE); print("p,");
  284.         print_int(BUF_SIZE); print("b,");
  285.         print_int(SAVE_SIZE); print("s");
  286.         print_ln();
  287.         selector = save_selector;
  288.     }
  289. #endif
  290.     wake_up_terminal();
  291.     fin_dvi();
  292.     if (job_name > 0) {
  293.         wlog_cr();
  294.         a_close(log_file);
  295.         selector -= 2;
  296.         if (selector == TERM_ONLY) {
  297.             print_nl("Transcript written on ");
  298.             print_str(log_name);
  299.             print_char('.');
  300.         }
  301.     }
  302.     print_ln();
  303.     if (edit) call_edit();
  304.     if (history == SPOTLESS || history == WARNING_ISSUED) {
  305.         exit(SPOTLESS);
  306.     } else {
  307.     exit(history);
  308.     }
  309. }
  310.  
  311. initialize ()
  312. {
  313.     init_char();
  314.     init_strings();
  315.     init_file();
  316.     init_error();
  317.     init_mem();
  318.     init_tok_mem();
  319.     init_eq();
  320.     init_cmds();
  321.     init_hyph();
  322.     init_tfm();
  323.     init_dvi();
  324. #ifdef INIT
  325.     format_ident = make_str_given(" (INITEX)");
  326. #endif
  327. }
  328.  
  329. #ifdef atarist
  330. void handle_int ()
  331. #else
  332. handle_int ()
  333. #endif
  334. {   
  335.     signal(SIGINT, handle_int);
  336.     interrupt = 1;
  337. }
  338.  
  339. one (s, t)
  340.     char*   s;
  341.     char*   t;
  342. {   
  343.     int     i;
  344.     int     one;
  345.  
  346.     one = 0;
  347.     for (i = 0; i < strlen(t); incr(i)) {
  348.         if (strncmp(s, &t[i], 2) == 0) {
  349.             incr(one);
  350.             if (one > 1)
  351.                 return FALSE;
  352.         }
  353.     }
  354.     if (one == 0)
  355.         return FALSE;
  356.     return TRUE;
  357. }
  358.  
  359. call_edit ()
  360. {
  361. #ifndef atarist
  362.     char*   file;
  363.     char*   envedit;
  364.     int     old_setting;
  365.     char    edit[FILE_NAME_SIZE + 17];
  366.     char*   texedit = "/usr/ucb/vi +%d %s &";
  367.     val getenv();
  368.  
  369.     if ((envedit = (char*) getenv("TEXEDIT")) != NULL)
  370.         texedit = envedit;
  371.     if (!one("%d", texedit) || !one("%s", texedit)) {
  372.         print_err("Edit format consists of 1 filename and 1 linenumber");
  373.         return;
  374.     }
  375.     file = (char*) &str_pool[str_start[str_ptr]];
  376.     old_setting = selector;
  377.     selector = NEW_STRING;
  378.     print_str(input_stack[base_ptr].name_field);
  379.     selector = old_setting;
  380.     str_pool[pool_ptr] = NUL;
  381.     sprintf(edit, texedit, line, file);
  382.     if (system(edit) != 0)
  383.         print_err("Trouble executing editor");
  384. #else
  385.         print_err("Sorry! Edit File not implemented");
  386. #endif
  387. }
  388.