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

  1.  
  2. /*
  3.  * %Y%:%M%:%I%:%Q%
  4.  *
  5.  * Copyright 1987,1988,1991 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. ptr
  20. str_toks ()
  21. {
  22.     str    s;
  23.     tok    t;
  24.     ptr    p, q;
  25.  
  26.     p = q = new_token();
  27.     token_link(p) = null;
  28.     token_ref_count(p) = 0;
  29.     for (s = cur_str; s < cur_str_ptr; incr(s)) {
  30.         t = *s;
  31.         if (t == ' ') {
  32.             t = SPACE_TOKEN;
  33.         } else {
  34.             t += OTHER_TOKEN;
  35.         }
  36.         q = token_link(q) = new_token();
  37.         token(q) = t;
  38.     }
  39.     flush_str();
  40.     return p;
  41. }
  42.  
  43. ptr
  44. the_toks ()
  45. {
  46.     ptr    p, q, r;
  47.     int    old_setting;
  48.  
  49.     get_x_token();
  50.     scan_something_internal(TOK_VAL, FALSE);
  51.     if (cur_val_level >= IDENT_VAL) {
  52.         p = q = new_token();
  53.         token_link(q) = null;
  54.         token_ref_count(q) = 0;
  55.         if (cur_val_level == IDENT_VAL) {
  56.             q = token_link(q) = new_token();
  57.             token(q) = sym2tok(cur_val);
  58.         } else if ((ptr)cur_val != null) {
  59.             r = token_link(cur_val);
  60.             while (r != null ) {
  61.                 q = token_link(q) = new_token();
  62.                 token(q) = token(r);
  63.                 r = token_link(r);
  64.             }
  65.         }
  66.         return p;
  67.     } else {
  68.         old_setting = selector;
  69.         selector = NEW_STRING;
  70.         switch (cur_val_level)
  71.         {
  72.         case INT_VAL:
  73.             print_int(cur_val);
  74.             break;
  75.  
  76.         case DIMEN_VAL:
  77.             print_scaled(cur_val);
  78.             print("pt");
  79.             break;
  80.  
  81.         case GLUE_VAL:
  82.             print_spec(cur_val, "pt");
  83.             delete_glue_ref(cur_val);
  84.             break;
  85.  
  86.         case MU_VAL:
  87.             print_spec(cur_val,"mu");
  88.             delete_glue_ref(cur_val);
  89.             break;
  90.         }
  91.         selector = old_setting;
  92.         return (str_toks());
  93.     }
  94. }
  95.  
  96. void
  97. conv_toks ()
  98. {
  99.     int    c;
  100.     ptr    p;
  101.     int    old_setting;
  102.     int    save_scanner_status;
  103.  
  104.     c = cur_chr;
  105.     switch (c)
  106.     {
  107.     case NUMBER_CODE: 
  108.     case ROMAN_NUMERAL_CODE:
  109.         scan_int();
  110.         break;
  111.  
  112.     case STRING_CODE:
  113.     case MEANING_CODE:
  114.         save_scanner_status = scanner_status;
  115.         scanner_status = NORMAL;
  116.         get_token();
  117.         scanner_status = save_scanner_status;
  118.         break;
  119.     
  120.     case FONT_NAME_CODE:
  121.         scan_font_ident();
  122.         break;
  123.  
  124.     case JOB_NAME_CODE:
  125.         if (job_name == null_str) {
  126.             open_log_file();
  127.         }
  128.         break;
  129.     }
  130.     old_setting = selector;
  131.     selector = NEW_STRING;
  132.     switch (c)
  133.     {
  134.     case NUMBER_CODE:
  135.         print_int(cur_val);
  136.         break;
  137.     
  138.     case ROMAN_NUMERAL_CODE:
  139.         print_roman_int(cur_val);
  140.         break;
  141.  
  142.     case STRING_CODE:
  143.         if (cur_cs != 0) {
  144.             sprint_cs(cur_cs);
  145.         } else  {
  146.             print_char(cur_chr);
  147.         }
  148.         break;
  149.     
  150.     case MEANING_CODE:
  151.         print_meaning();
  152.         break;
  153.  
  154.     case FONT_NAME_CODE:
  155.         print(font_name(cur_val));
  156.         if (font_size(cur_val) != font_dsize(cur_val)) {
  157.             print(" at ");
  158.             print_scaled(font_size(cur_val));
  159.             print("pt");
  160.         }
  161.         break;
  162.  
  163.     case JOB_NAME_CODE:
  164.         print(job_name);
  165.         break;
  166.     }
  167.     selector = old_setting; 
  168.     p = str_toks();
  169.     ins_list(token_link(p));
  170.     free_token(p);
  171. }
  172.  
  173. void
  174. scan_toks (macro_def, xpand)
  175.     bool    macro_def;
  176.     bool    xpand;
  177. {
  178.     ptr    p, q;
  179.     tok    s, t;
  180.     int    unbalance;
  181.     tok    hash_brace;
  182.  
  183. #define BAD_PARAM_NO "Illegal parameter number in definition of "
  184. #define ONLY_9_PARAMS "You already have nine parameters"
  185. #define CONSEC_PARAMS "Parameters must be numbered consecutively"
  186.  
  187.     if (macro_def) {
  188.         scanner_status = DEFINING;
  189.     } else {
  190.         scanner_status = ABSORBING;
  191.     }
  192.     warning_cs = cur_cs;
  193.     def_ref = new_token();
  194.     token_ref_count(def_ref) = 0;
  195.     p = def_ref;
  196.     hash_brace = 0;
  197.     t = ZERO_TOKEN;
  198.     if (macro_def) {
  199.         loop {
  200.             get_token();
  201.             if (cur_tok < RIGHT_BRACE_LIMIT) {
  202.                 break;
  203.             }
  204.             if (cur_cmd == MAC_PARAM) {
  205.                 s = MATCH_TOKEN + cur_chr;
  206.                 get_token();
  207.                 if (cur_cmd == LEFT_BRACE) {
  208.                     hash_brace = cur_tok; 
  209.                     p = token_link(p) = new_token();
  210.                     token(p) = cur_tok;
  211.                     p = token_link(p) = new_token();
  212.                     token(p) = END_MATCH_TOKEN;
  213.                     goto done;
  214.                 }
  215.                 if (t == ZERO_TOKEN + 9) {
  216.                     print_err(ONLY_9_PARAMS);
  217.                     help_param_count();
  218.                     error();
  219.                 } else {
  220.                     incr(t);
  221.                     if (cur_tok != t) {
  222.                         print_err(CONSEC_PARAMS);
  223.                         help_param_num();
  224.                         back_error();
  225.                     }
  226.                     cur_tok = s;
  227.                 }
  228.             }
  229.             p = token_link(p) = new_token();
  230.             token(p) = cur_tok;
  231.         }
  232.         p = token_link(p) = new_token();
  233.         token(p) = END_MATCH_TOKEN;
  234.         if (cur_cmd == RIGHT_BRACE) {
  235.             print_err("Missing { inserted");
  236.             incr(align_state); 
  237.             help_left_brace();
  238.             error();
  239.             goto found;
  240.         }
  241.     } else {
  242.         scan_left_brace();
  243.     }
  244.  
  245. done:
  246.     unbalance = 1;
  247.     loop {
  248.         if (xpand) {
  249.             loop {
  250.                 get_next();
  251.                 if (cur_cmd <= MAX_COMMAND) {
  252.                     break;
  253.                 }
  254.                 if (cur_cmd != THE) {
  255.                     expand();
  256.                 } else {
  257.                     q = token_link(the_toks()); 
  258.                     if (q != null) {
  259.                         p = token_link(p) = q;
  260.                         while (token_link(q)) {
  261.                             p = q = token_link(q);
  262.                         }
  263.                     }
  264.                 }
  265.             }
  266.             x_token();
  267.         } else {
  268.             get_token();
  269.         }
  270.         if (cur_tok < RIGHT_BRACE_LIMIT) {
  271.             if (cur_cmd < RIGHT_BRACE) {
  272.                 incr(unbalance);
  273.             } else {
  274.                 decr(unbalance);
  275.                 if (unbalance == 0) {
  276.                     break;
  277.                 }
  278.             }
  279.         } else if (cur_cmd == MAC_PARAM && macro_def) {
  280.             s = cur_tok;
  281.             if (xpand) {
  282.                 get_x_token();
  283.             } else {
  284.                 get_token();
  285.             }
  286.             if (cur_cmd != MAC_PARAM) {
  287.                 if (cur_tok <= ZERO_TOKEN || cur_tok > t) {
  288.                     print_err(BAD_PARAM_NO);
  289.                     sprint_cs(warning_cs);
  290.                     help_param_use();
  291.                     back_error(); 
  292.                     cur_tok = s;
  293.                 } else {
  294.                     cur_tok = OUT_PARAM_TOKEN +
  295.                         cur_chr - '0';
  296.                 }
  297.             }
  298.         }
  299.         p = token_link(p) = new_token();
  300.         token(p) = cur_tok;
  301.     }
  302.  
  303. found:
  304.     scanner_status = NORMAL;
  305.     if (hash_brace != 0) {
  306.         p = token_link(p) = new_token();
  307.         token(p) = hash_brace;
  308.     }
  309. }
  310.  
  311. void
  312. read_toks (n, s)
  313.     int    n;
  314.     sym    s;
  315. {
  316.     ptr    p;
  317.     int    m;
  318.     int    a;
  319.  
  320. #define NO_READ "*** (cannot \\read from terminal in nonstop modes)"
  321.  
  322.     scanner_status = DEFINING;
  323.     warning_cs = s;
  324.     p = def_ref = new_token();
  325.     token_ref_count(def_ref) = 0;
  326.     p = token_link(p) = new_token();
  327.     token(p) = END_MATCH_TOKEN;
  328.     if (n < 0 || n > 15) {
  329.         m = 16;
  330.     } else {
  331.         m = n;
  332.     }
  333.     a = align_state;
  334.     align_state = 1000000;
  335.     do {
  336.         begin_file_reading();
  337.         index = m + 1;
  338.         if (read_open[m] == CLOSED) {
  339.             if (interaction > NONSTOP_MODE) {
  340.                 if (n < 0) {
  341.                     prompt_input(null_str);
  342.                 } else {
  343.                     wake_up_terminal();
  344.                     print_ln();
  345.                     sprint_cs(s);
  346.                     prompt_input("=");
  347.                     n = -1;
  348.                 }
  349.                 memcpy(buffer, cur_str, cur_length());
  350.                 next = buffer;
  351.                 limit = buffer + cur_length() - 1;
  352.                 flush_str();
  353.             } else {
  354.                 fatal_error(NO_READ);
  355.             }
  356.         } else if (read_open[m] == OPENED) {
  357.             if (input_ln(read_file[m])) {
  358.                 read_open[m] = NORMAL;
  359.             } else {
  360.                 a_close(read_file[m]);
  361.                 read_open[m] = CLOSED;
  362.             }
  363.         } else {
  364.             if (!input_ln(read_file[m])) {
  365.                 a_close(read_file[m]);
  366.                 read_open[m] = CLOSED;
  367.                 if (align_state != 1000000) {
  368.                     runaway();
  369.                     print_err("File ended within ");
  370.                     print_esc("read");
  371.                     help_read();
  372.                     align_state = 1000000;
  373.                     error();
  374.                 }
  375.             }
  376.         }
  377.         if (end_line_char_active) {
  378.             *++limit = end_line_char;
  379.         }
  380.         state = NEW_LINE;
  381.         loop {
  382.             get_token();
  383.             if (cur_tok == 0) {
  384.                 break; 
  385.             }
  386.             p = token_link(p) = new_token();
  387.             token(p) = cur_tok;
  388.         }
  389.         end_file_reading();
  390.     } while (align_state != 1000000);
  391.     cur_val = def_ref;
  392.     scanner_status = NORMAL; 
  393.     align_state = a;
  394. }
  395.  
  396. void
  397. show_token_list    (p, q, l)
  398.     ptr    p;
  399.     ptr    q;
  400.     int    l;
  401. {
  402.     int    c;
  403.     int    m;
  404.     int    n;
  405.     int    match_chr;
  406.  
  407.     match_chr = '#';
  408.     n = '0';
  409.     for (tally = 0; p != null && tally < l; p = token_link(p)) {
  410.         if (p == q) {
  411.             set_trick_count();
  412.         }
  413.         if (token(p) >= CS_TOKEN_FLAG) {
  414.             print_cs(tok2sym(token(p)));
  415.         } else {
  416.             m = token(p) / 0400;
  417.             c = token(p) % 0400;
  418.             if (token(p) < 0 || c > 255) {
  419.                 print_esc("BAD.");
  420.             } else {
  421.                 switch (m)
  422.                 {
  423.                 case LEFT_BRACE:
  424.                 case RIGHT_BRACE:
  425.                 case MATH_SHIFT:
  426.                 case TAB_MARK:
  427.                 case SUP_MARK:
  428.                 case SUB_MARK:
  429.                 case SPACER:
  430.                 case LETTER:
  431.                 case OTHER_CHAR:
  432.                     print_ASCII(c); 
  433.                     break;
  434.                 
  435.                 case MAC_PARAM:
  436.                     print_ASCII(c);
  437.                     print_ASCII(c);
  438.                     break;
  439.                 
  440.                 case OUT_PARAM:
  441.                     print_ASCII(match_chr);
  442.                     if (c <= 9) {
  443.                         print_char(c + '0');
  444.                     } else {
  445.                         print("!");
  446.                         return;
  447.                     }
  448.                     break;
  449.                 
  450.                 case MATCH:
  451.                     match_chr = c;
  452.                     print_ASCII(c);
  453.                     incr(n);
  454.                     print_char(n);
  455.                     if (n > '9') {
  456.                         return;
  457.                     }
  458.                     break;
  459.                 
  460.                 case END_MATCH:
  461.                     print("->");
  462.                     break;
  463.                 
  464.                 default:
  465.                     print_err("BAD.");
  466.                     break;
  467.                 }
  468.             }
  469.         }
  470.     }
  471.     if (p != null) {
  472.         print_esc("ETC.");
  473.     }
  474. }
  475.  
  476. void
  477. token_show (p)
  478.     ptr    p;
  479.     if (p != null) {
  480.         show_token_list(token_link(p), null, 10000000);
  481.     }
  482. }
  483.  
  484. void
  485. print_meaning ()
  486. {
  487.     print_cmd_chr(cur_cmd, cur_chr);
  488.     if (cur_cmd >= CALL) {
  489.         print(":");
  490.         print_ln();
  491.         token_show(cur_chr);
  492.     } else if (cur_cmd == TOP_BOT_MARK) {
  493.         print(":");
  494.         print_ln();
  495.         token_show(cur_mark[cur_chr]);
  496.     }
  497. }
  498.  
  499. void
  500. flush_list (p)
  501.     ptr    p;
  502. {
  503.     ptr    q;
  504.  
  505.     while (p != null) {
  506.         q = token_link(p);
  507.         free_token(p);
  508.         p = q;
  509.     }
  510. }
  511.  
  512. void
  513. _toklist_init ()
  514. {
  515. }
  516.  
  517. void
  518. _toklist_init_once ()
  519. {
  520. }
  521.  
  522. /*
  523. **    Help text
  524. */
  525.  
  526. help_param_num ()
  527. {
  528.     help2("I've inserted the digit you should have used after the #.",
  529.     "Type `1' to delete what you did use.");
  530. }
  531.  
  532. help_param_count ()
  533. {
  534.     help1("I'm going to ignore the # sign you just used.");
  535. }
  536.  
  537. help_left_brace ()
  538. {
  539.     help2("Where was the left brace? You said something like `\\def\\a}',",
  540.     "which I'm going to interpret as `\\def\\a{}'.");
  541. }
  542.  
  543. help_param_use ()
  544. {
  545.     help3("You meant to type ## instead of #, right?",
  546.     "Or maybe a } was forgotten somewhere earlier, and things",
  547.     "are all screwed up? I'm going to assume that you meant ##.");
  548. }
  549.  
  550. help_read ()
  551. {
  552.     help1("This \\read has unbalanced braces.");
  553. }
  554.