home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / CMTEX330 / SOURCE / TEX.C < prev    next >
C/C++ Source or Header  |  1992-02-19  |  5KB  |  322 lines

  1.  
  2. /*
  3.  * %Y%:%M%:%I%:%Q%
  4.  *
  5.  * Copyright 1987,1988,1991,1992 Pat J Monardo
  6.  *
  7.  * Redistribution of this file is permitted through
  8.  * the specifications in the file COPYING.
  9.  *
  10.  *
  11.  */
  12.  
  13. #ifndef lint
  14. static char *sccsid = "%A%";
  15. #endif
  16.  
  17. #include "tex.h"
  18.  
  19. char    *banner = "This is Common TeX, Version 3.3";
  20. str    format_ident;
  21. int    ready_already;
  22.  
  23. main (argc, argv)
  24.     int    argc;
  25.     str     argv[];
  26. {
  27.     signal(SIGINT, handle_int);
  28.     set_paths();
  29.     if (ready_already != 314159)  {
  30.         initialize_once();
  31.     }
  32.     ready_already = 314159;
  33.     initialize();
  34.     if (!(decode_args(argc, argv) || init_terminal())) {
  35.         exit(history);
  36.     }
  37.     if (end_line_char_active) {
  38.         *++limit = end_line_char;
  39.     }
  40.     if (interaction == BATCH_MODE) {
  41.         selector = NO_PRINT;
  42.     } else {
  43.         selector = TERM_ONLY;
  44.     }
  45.     fix_date_and_time();
  46.     if (next <= limit && cat_code(*next) != ESCAPE) {
  47.         start_input();
  48.     }
  49.     history = SPOTLESS;
  50.     main_control();
  51.     final_cleanup();
  52.     /*NOTREACHED*/
  53. }
  54.  
  55. #define USAGE    "usage: %s [ TeX string ]\n"
  56.  
  57. bool
  58. decode_args (argc, argv)
  59.     int    argc;
  60.     str    argv[];
  61. {
  62.     int    i, j;
  63.  
  64.     if (argc == 1) {
  65.         return FALSE;
  66.     }
  67.     for (i = 1; i < argc; i++) {
  68.         j = str_length(argv[i]);
  69.         argv[i][j] = ' ';
  70.         if (limit + j + 1 >= buffer + BUF_SIZE) {
  71.             return FALSE;
  72.         }
  73.         (void) strncpy(limit + 1, argv[i], j + 1);
  74.         limit += j + 1;
  75.         argv[i][j] = '\0';
  76.     }
  77.     return TRUE;
  78. }
  79.  
  80. void
  81. fix_date_and_time ()
  82. {
  83.     long    clock, begintime();
  84.     struct tm    *tmptr, *localtime();
  85.  
  86.     clock = begintime();
  87.     tmptr = localtime(&clock);
  88.     time = 60 * tmptr->tm_hour + tmptr->tm_min;
  89.     day = tmptr->tm_mday;
  90.     month = tmptr->tm_mon + 1;
  91.     year = tmptr->tm_year + 1900;
  92. }
  93.  
  94. #undef time
  95. long
  96. begintime()
  97. {
  98.     return (time((char *) 0));
  99. }
  100.  
  101. void
  102. handle_int ()
  103. {    
  104.     signal(SIGINT, handle_int);
  105.     interrupted = 1;
  106. }
  107.  
  108. void
  109. initialize ()
  110. {
  111.     _alloc_init();
  112.     _chr_init();
  113.     _str_init();
  114.     _arith_init();
  115.     _error_init();
  116.     _sym_init();
  117.     _def_init();
  118.     _scan_init();
  119.     _expand_init();
  120.     _fileio_init();
  121.     _cond_init();
  122.     _toklist_init();
  123.     _eval_init();
  124.     _box_init();
  125.     _boxlist_init();
  126.     _math_init();
  127.     _mathlist_init();
  128.     _hyph_init();
  129.     _pack_init();
  130.     _page_init();
  131.     _par_init();
  132.     _mlst_hlst_init();
  133.     _align_init();
  134.     _dvi_init();
  135.     _cmd_init();
  136.     _tfm_init();
  137.     _texext_init();
  138.     _tex_init();
  139.     _print_init();
  140.     _tok_init();
  141. }
  142.  
  143. void
  144. initialize_once ()
  145. {
  146.     _chr_init_once();
  147.     _print_init_once();
  148.     _error_init_once();
  149.     _alloc_init_once();
  150.     _str_init_once();
  151.     _arith_init_once();
  152.     _sym_init_once();
  153.     _tok_init_once();
  154.     _def_init_once();
  155.     _scan_init_once();
  156.     _expand_init_once();
  157.     _fileio_init_once();
  158.     _cond_init_once();
  159.     _toklist_init_once();
  160.     _eval_init_once();
  161.     _box_init_once();
  162.     _boxlist_init_once();
  163.     _math_init_once();
  164.     _mathlist_init_once();
  165.     _hyph_init_once();
  166.     _pack_init_once();
  167.     _page_init_once();
  168.     _par_init_once();
  169.     _mlst_hlst_init_once();
  170.     _align_init_once();
  171.     _dvi_init_once();
  172.     _cmd_init_once();
  173.     _tfm_init_once();
  174.     _texext_init_once();
  175.     _tex_init_once();
  176. }
  177.  
  178. void
  179. final_cleanup ()
  180. {
  181.     bool dump = cur_chr == 1;
  182.  
  183. #define SEE_TRANS "(see the transcript file for additional information)"
  184.  
  185.     if (job_name == null_str) {
  186.         open_log_file();
  187.     }
  188.     while (open_parens > 0) {
  189.         print(" )");
  190.         decr(open_parens);
  191.     }
  192.     if (cur_level > LEVEL_ONE) {
  193.         print_nl("(");
  194.         print_esc("end occurred ");
  195.         print("inside a group at level ");
  196.         print_int(cur_level - LEVEL_ONE);
  197.         print(")");
  198.     }
  199.     while (cond_ptr != null) {
  200.         print_nl("(");
  201.         print_esc("end occurred ");
  202.         print("when ");
  203.         print_cmd_chr(IF_TEST, cur_if);
  204.         if (if_line != 0) {
  205.             print(" on line ");
  206.             print_int(if_line);
  207.         }
  208.         print(" was incomplete)");
  209.         if_line = if_line_field(cond_ptr); 
  210.         cur_if = subtype(cond_ptr);
  211.         cond_ptr = link(cond_ptr);
  212.     }
  213.     if (history != SPOTLESS
  214.     && (history == WARNING_ISSUED || interaction < ERROR_STOP_MODE)
  215.     && selector == TERM_AND_LOG) {
  216.         selector = TERM_ONLY;
  217.         print_nl(SEE_TRANS);
  218.         selector = TERM_AND_LOG;
  219.     }
  220.     close_files_and_terminate(FALSE, dump);
  221. }
  222.  
  223. void
  224. close_files_and_terminate (edit, dump)
  225.     bool    edit;
  226.     bool    dump;
  227. {
  228.     int    k;
  229.     
  230.     for (k = 0; k < 16; incr(k)) {
  231.         if (write_open[k]) {
  232.             a_close(write_file[k]);
  233.         }
  234.     }
  235.     wake_up_terminal();
  236.     fin_dvi();
  237.     if (log_name != null_str) {
  238.         wlog_cr();
  239.         a_close(log_file);
  240.         selector -= 2;
  241.         if (selector == TERM_ONLY) {
  242.             print_nl("Transcript written on ");
  243.             print(log_name);
  244.             print(".");
  245.         }
  246.     }
  247.     print_ln();
  248.     if (edit) {
  249.         call_edit();
  250.     }
  251.     fflush(stdout);
  252.     fflush(stderr);
  253.     if (dump) {
  254.         if (trie_not_ready) {
  255.             init_trie();
  256.         }
  257.         kill(getpid(), SIGQUIT);
  258.     }
  259.     exit(history);
  260. }
  261.  
  262. void
  263. call_edit ()
  264. {
  265.     str    envedit;
  266.     char    edit[MAX_STR_SIZE + 17];
  267.     str    texedit = "vi +%d %s";
  268.     str    getenv();
  269.  
  270. #define LONG_FORMAT "Edit format must be of reasonable length"
  271. #define EDIT_FORMAT "Edit format consists of 1 filename and 1 linenumber"
  272.  
  273.     if ((envedit = getenv("TEXEDIT")) != null) {
  274.         texedit = envedit;
  275.         if (strlen(texedit) > MAX_STR_SIZE) {
  276.             print_err(LONG_FORMAT);
  277.             return;
  278.         }
  279.     }
  280.     if (!one("%d", texedit) || !one("%s", texedit)) {
  281.         print_err(EDIT_FORMAT);
  282.         return;
  283.     }
  284.     cur_input = *base_ptr;
  285.     sprintf(edit, texedit, line, file_name);
  286.     system(edit);
  287. }
  288.  
  289. one (s, t)
  290.     str    s;
  291.     str    t;
  292. {
  293.     int    i;
  294.     int    j;
  295.     int    one;
  296.  
  297.     one = 0;
  298.     j = strlen(t) - 1;
  299.     for (i = 0; i < j; incr(i)) {
  300.         if (strncmp(s, &t[i], 2) == 0) {
  301.             if (++one > 1) {
  302.                 return FALSE;
  303.             }
  304.         }
  305.     }
  306.     if (one == 0) {
  307.         return FALSE;
  308.     }
  309.     return TRUE;
  310. }
  311.  
  312. void
  313. _tex_init ()
  314. {
  315. }
  316.  
  317. void
  318. _tex_init_once ()
  319. {
  320.     format_ident = " (INITEX)";
  321. }
  322.