home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / CMTEX330 / SOURCE / PRINT.C < prev    next >
C/C++ Source or Header  |  1992-02-19  |  4KB  |  325 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. file    log_file;
  20. str    log_name;
  21.  
  22. int    selector;
  23. int    term_offset;
  24. int    file_offset;
  25. int    tally;
  26. int    first_count;
  27. int    trick_count;
  28. int    trick_buf[ERROR_LINE];
  29. int    dig[23];
  30.  
  31. void
  32. print_ln ()
  33. {
  34.     switch (selector)
  35.     {
  36.     case TERM_AND_LOG:
  37.         wterm_cr();
  38.         term_offset = 0;
  39.         wlog_cr();
  40.         file_offset = 0;
  41.         break;
  42.  
  43.     case LOG_ONLY:
  44.         wlog_cr();
  45.         file_offset = 0;
  46.         break;
  47.  
  48.     case TERM_ONLY:
  49.         wterm_cr();
  50.         term_offset = 0;
  51.         break;
  52.  
  53.     case NO_PRINT:
  54.     case PSEUDO:
  55.     case NEW_STRING:
  56.         break;
  57.  
  58.     default:
  59.         wfile_cr();
  60.         break;
  61.     }
  62. }    
  63.  
  64. void
  65. print_char (c)
  66.     int    c;
  67. {
  68.     if (c == new_line_char && selector < PSEUDO) {
  69.         print_ln();
  70.         return;
  71.     }
  72.     switch (selector)
  73.     {
  74.     case TERM_AND_LOG:
  75.         wterm(xchr[c]);
  76.         incr(term_offset);
  77.         wlog(xchr[c]);
  78.         incr(file_offset);
  79.         if (term_offset == MAX_PRINT_LINE) {
  80.             print_ln();
  81.         }
  82.         if (file_offset == MAX_PRINT_LINE) {
  83.             print_ln();
  84.         }
  85.         break;
  86.  
  87.     case LOG_ONLY:
  88.         wlog(xchr[c]);
  89.         incr(file_offset);
  90.         if (file_offset == MAX_PRINT_LINE) {
  91.             print_ln();
  92.         }
  93.         break;
  94.  
  95.     case TERM_ONLY:
  96.         wterm(xchr[c]);
  97.         incr(term_offset);
  98.         if (term_offset == MAX_PRINT_LINE) {
  99.             print_ln();
  100.         }
  101.         break;
  102.  
  103.     case NO_PRINT:
  104.         return;
  105.  
  106.     case PSEUDO:
  107.         if (tally < trick_count) {
  108.             trick_buf[tally % ERROR_LINE] = c;
  109.         }
  110.         break;
  111.  
  112.     case NEW_STRING:
  113.         if (cur_str_ptr < cur_str_end) {
  114.             append_char(c);
  115.         }
  116.         break;
  117.  
  118.     default:
  119.         wfile(xchr[c]);
  120.         break;
  121.     }
  122.     incr(tally);
  123. }
  124.  
  125. void
  126. print_ASCII (c)
  127.     int    c;
  128. {
  129.     if (c == new_line_char && selector < PSEUDO) {
  130.         print_ln();
  131.         return;
  132.     }
  133.     print(ASCII[c]);
  134. }
  135.  
  136. void
  137. print (s)
  138.     str    s;
  139. {
  140.     while (*s) {
  141.         print_char(*s++);
  142.     }
  143. }
  144.  
  145. void
  146. print_nl (s)
  147.     str     s;
  148. {
  149.     if (term_offset > 0 && odd(selector)
  150.     || file_offset > 0 && selector >= LOG_ONLY) {
  151.         print_ln();
  152.     }
  153.     print(s);
  154. }
  155.  
  156. void
  157. print_esc (s)
  158.     str    s;
  159. {
  160.     int    c;
  161.  
  162.     c = escape_char;
  163.     if (c >= 0 && c < 256) {
  164.         print_ASCII(c);
  165.     }
  166.     print(s);
  167. }
  168.  
  169. void
  170. print_int (n)
  171.     int    n;
  172. {
  173.     int    m;
  174.     int    k;
  175.  
  176.     k = 0;
  177.     if (n < 0)  {
  178.         print("-");
  179.         if (n > -100000000) {
  180.             negate(n);
  181.         } else {
  182.             m = -1 - n;
  183.             n = m / 10;
  184.             m = m % 10 + 1;
  185.             k = 1;
  186.             if (m < 10) {
  187.                 dig[0] = m;
  188.             } else {
  189.                 dig[0] = 0;
  190.                 incr(n);
  191.             }
  192.         }
  193.     }
  194.     do {
  195.         dig[k] = n % 10;
  196.         n /= 10;
  197.         incr(k);
  198.     } while (n != 0);
  199.     print_the_digs(k);
  200. }
  201.  
  202. void
  203. print_hex (n)
  204.     int    n;
  205. {
  206.     int    k;
  207.  
  208.     k = 0;
  209.     print("\"");
  210.     do {
  211.         dig[k] = n % 16;
  212.         n /= 16;
  213.         incr(k);
  214.     } while (n != 0);
  215.     print_the_digs(k);
  216. }
  217.  
  218. void
  219. print_the_digs (k)
  220.     int    k;
  221. {
  222.     while (k > 0) {
  223.         decr(k);
  224.         if (dig[k] < 10) {
  225.             print_char('0' + dig[k]);
  226.         } else {
  227.             print_char('A' - 10 + dig[k]);
  228.         }
  229.     }
  230. }
  231.  
  232. void
  233. print_two (n)
  234.     int    n;
  235. {
  236.     n = abs(n) % 100;
  237.     print_char('0' + n / 10);
  238.     print_char('0' + n % 10);
  239. }
  240.  
  241. void
  242. print_roman_int (n)
  243.     int    n;
  244. {
  245.     str     s;
  246.     str     t;
  247.     int    u;
  248.     int    v;
  249.  
  250.     s = "m2d5c2l5x2v5i";
  251.     v = 1000;
  252.     loop {
  253.         while (n >= v) {
  254.             print_char(*s);
  255.             n -= v;
  256.         }
  257.         if (n <= 0) {
  258.             return;
  259.         }
  260.         t = s + 2;
  261.         u = v / (t[-1] - '0');
  262.         if (t[-1] == '2')  {
  263.             t += 2;
  264.             u /= t[-1] - '0';
  265.         }
  266.         if (n + u >= v)  {
  267.             print_char(*t);
  268.             n += u;
  269.         } else {
  270.             s += 2;
  271.             v /= s[-1] - '0';
  272.         }
  273.     }
  274. }
  275.  
  276. int
  277. begin_pseudoprint ()
  278. {
  279.     int     t;
  280.  
  281.     t = tally;
  282.     tally = 0;
  283.     selector = PSEUDO;
  284.     trick_count = 1000000;
  285.  
  286.     return t;
  287. }
  288.  
  289. void
  290. set_trick_count ()
  291. {
  292.     first_count = tally;
  293.     trick_count = tally + 1 + ERROR_LINE - HALF_ERROR_LINE;
  294.     if (trick_count < ERROR_LINE) {
  295.         trick_count = ERROR_LINE;
  296.     }
  297. }
  298.  
  299. void
  300. _print_init ()
  301. {
  302.     selector = TERM_ONLY;
  303.     tally = 0;
  304.     term_offset = 0;
  305.     file_offset = 0;
  306.     fputs(banner, term_out);
  307.     if (format_ident == null_str) {
  308.         fputs(" (no format preloaded)", term_out);
  309.     } else {
  310.         fputs(format_ident, term_out);
  311.     }
  312.     fputs("\n", term_out);
  313.     log_name = null_str;
  314. }
  315.  
  316. void
  317. _print_init_once ()
  318. {
  319.     selector = TERM_ONLY;
  320.     tally = 0;
  321.     term_offset = 0;
  322.     file_offset = 0;
  323.     log_name = null_str;
  324. }
  325.