home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Tex / Tex29 / StTeXsrc.zoo / src / fmt.c < prev    next >
C/C++ Source or Header  |  1989-07-13  |  14KB  |  558 lines

  1.  
  2. /*
  3.  * @(#)fmt.c 2.5 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 "token.h"
  16. #include "box.h"
  17. #include "eqstack.h"
  18. #include "file.h"
  19. #include "tfm.h"
  20. #include "hyph.h"
  21. #include "fmt.h"
  22.  
  23. word_file   fmt_file;
  24.  
  25. str     format_ident;
  26.  
  27. #ifdef atarist
  28. #ifndef INIT
  29. #include <osbind.h>
  30. #define _BSIZE 16384
  31. static int _st_fmt_file;
  32. static unsigned char *_st_b, *_st_ptr;
  33. static long _st_cnt = 0;
  34. static char _st_eof = 0;
  35. #ifdef feof
  36. #undef  feof
  37. #define feof(X) (_st_eof)
  38. #endif
  39.  
  40. #ifdef __GNUC__
  41. inline
  42. #endif
  43. static int _stgetc()
  44. {
  45.     if(_st_cnt == 0)
  46.     {
  47.         if((_st_cnt = Fread(_st_fmt_file, (long)_BSIZE,
  48.                     _st_b)) < 0)
  49.         {
  50.             _st_eof = 1;
  51.             return -1;
  52.         }
  53.         else
  54.         {
  55.             _st_ptr = _st_b;
  56.         }
  57.     }
  58.     _st_cnt--;
  59.     return *_st_ptr++;
  60. }
  61.  
  62. void _stfread(b, size)
  63. register unsigned char *b;
  64. register long size;
  65. {
  66.     while(size--)
  67.         *b++ = _stgetc();
  68. }
  69. #ifdef __GNUC__
  70. inline
  71. #endif
  72. static int _stgetw()
  73. {
  74.     int i;
  75.     _stfread(&i, sizeof(int));
  76.     return i;
  77. }
  78.  
  79. #else /* INIT */
  80.  
  81. int Getw(f)
  82. FILE *f;
  83. {
  84.     int i;
  85.     return (fread(&i, sizeof(int), 1, f) == 1)? i : EOF ;
  86. }
  87.  
  88. int Putw(w, f)
  89. int w;
  90. FILE *f;
  91. {
  92.     return (fwrite(&w,sizeof(int), 1, f) == 1)? w : EOF ;
  93. }
  94. #endif
  95. #endif
  96.  
  97. #if (defined(atarist) && (!(defined(INIT))))
  98. #define undump_ascii()      (str_pool[k] = _stgetc())
  99. #define undump_int(M)       M = _stgetw()
  100. #define undump_wd(M)        _stfread(&M, sizeof(M));
  101. #define undump_hh(M)        _stfread(&M, sizeof(M));
  102. #define undump_qqqq(M)      _stfread(&M, sizeof(M));
  103. #else
  104. #define undump_ascii()      (str_pool[k] = getc(fmt_file))
  105. #define undump_int(M)       M = Getw(fmt_file)
  106. #define undump_wd(M)        fread(&M, sizeof(M), 1, fmt_file);
  107. #define undump_hh(M)        fread(&M, sizeof(M), 1, fmt_file);
  108. #define undump_qqqq(M)      fread(&M, sizeof(M), 1, fmt_file);
  109. #endif
  110. #define too_small(S) \
  111.     {wake_up_terminal(); \
  112.     printf("---! Must increase the %s\n", S); \
  113.     goto bad_fmt;}
  114.  
  115. #define undump(MIN, MAX, X) \
  116.     {undump_int(x); \
  117.     if (x < MIN || x > MAX) \
  118.         goto bad_fmt; \
  119.     else X = x;}
  120.  
  121. #define undump_size(MIN, MAX, TABLE, X) \
  122.     {undump_int(x); \
  123.     if (x < MIN) goto bad_fmt; \
  124.     if (x > MAX) {too_small(TABLE);} \
  125.     else {X = x;}}
  126.  
  127. bool
  128. load_fmt_file ()
  129. {
  130.     int     j;
  131.     int     k;
  132.     ptr     p;
  133.     ptr     q;
  134.     int     x;
  135. #if (defined(atarist) && (!(defined(INIT))))
  136.     if((_st_b = (unsigned char *)Malloc((long)_BSIZE)) == (unsigned
  137.                                char *)NULL)
  138.     {
  139.         puts("(Not enough memory; I'm stymied)");
  140.         return FALSE;
  141.     }
  142.     _st_fmt_file = fileno(fmt_file);
  143. #endif
  144.  
  145.     undump_int(x);
  146.     if (x != MEM_BOT) goto bad_fmt;
  147.     undump_int(x);
  148.     if (x != MEM_TOP) goto bad_fmt;
  149.     undump_int(x);
  150.     if (x != TOK_BOT) goto bad_fmt;
  151.     undump_int(x);
  152.     if (x != TOK_TOP) goto bad_fmt;
  153.     undump_int(x);
  154.     if (x != EQTB_SIZE) goto bad_fmt;
  155.     undump_int(x);
  156.     if (x != HASH_PRIME) goto bad_fmt;
  157.     undump_int(x);
  158.     if (x != HYPH_SIZE) goto bad_fmt;
  159.  
  160.     undump_size(0, POOL_SIZE, "string pool size", pool_ptr);
  161.     undump_size(0, MAX_STRINGS, "max strings", str_ptr);
  162.     undump_int(null_str);
  163.     for (k = 0; k <= str_ptr; incr(k))
  164.         undump(0, pool_ptr, str_start[k]);
  165.     for (k = 0; k <= pool_ptr; incr(k))
  166.         undump_ascii();
  167.  
  168.     undump(TOK_BOT, tok_high, tok_low);
  169.     undump(NULL, TOK_TOP, tok_head);
  170.     tok_end = TOK_TOP;
  171.     for (k = tok_low; k <= tok_end; incr(k)) {
  172.         undump_wd(tok_link[k]);
  173.         undump_wd(tok_mem[k]);
  174.     }
  175.  
  176.     undump(LO_MEM_STAT_MAX + 1000, HI_MEM_STAT_MIN - 1, lo_mem_max);
  177.     undump(LO_MEM_STAT_MAX  + 1, lo_mem_max, rover);
  178.     p = MEM_BOT;
  179.     q = rover;
  180.     x = 0;
  181.     do {
  182.         for (k = p; k <= q + 1; incr(k))
  183.             undump_wd(mem[k]);
  184.         p = q + node_size(q);
  185.         if (p > lo_mem_max || q >= rlink(q) && rlink(q) != rover)
  186.                 goto bad_fmt;
  187.         q = rlink(q);
  188.     } while (q != rover);
  189.     for (k = p; k <= lo_mem_max; incr(k))
  190.         undump_wd(mem[k]);
  191.  
  192.     undump(lo_mem_max + 1, HI_MEM_STAT_MIN, hi_mem_min);
  193.     undump(NULL, MEM_TOP, avail);
  194.     mem_end = MEM_TOP;
  195.     for (k = hi_mem_min; k <= mem_end; incr(k))
  196.         undump_wd(mem[k]);
  197.  
  198.     undump_int(var_used);
  199.     undump_int(dyn_used);
  200.     undump_int(tok_used);
  201.  
  202.     k = ACTIVE_BASE;
  203.     do {
  204.         undump_int(x);
  205.         if (x < 1 || k + x > EQTB_SIZE + 1)
  206.             goto bad_fmt;
  207.         for (j = k; j < k + x; incr(j))
  208.             undump_wd(eqtb[j]);
  209.         k += x;
  210.         undump_int(x);
  211.         if (x < 0 || k + x > EQTB_SIZE + 1)
  212.             goto bad_fmt;
  213.         for (j = k; j < k + x; incr(j)) 
  214.             eqtb[j] = eqtb[k - 1];
  215.         k += x;
  216.     } while (k <= EQTB_SIZE);
  217.  
  218.     undump(HASH_BASE, FROZEN_CONTROL_SEQUENCE, par_loc);
  219.     par_token = CS_TOKEN_FLAG + par_loc;
  220.     undump(HASH_BASE, FROZEN_CONTROL_SEQUENCE, write_loc);
  221.  
  222.     undump(HASH_BASE, FROZEN_CONTROL_SEQUENCE, hash_used);
  223.     p = HASH_BASE - 1;
  224.     do {
  225.         undump(p + 1, hash_used, p);
  226.         undump_hh(hash[p]);
  227.     } while (p != hash_used);
  228.     for (p = hash_used + 1; p < UNDEFINED_CONTROL_SEQUENCE; incr(p))
  229.         undump_hh(hash[p]);
  230.     undump_int(cs_count);
  231.  
  232.     undump_size(7, FONT_MEM_SIZE, "font mem size", fmem_ptr);
  233.     for (k = 0; k < fmem_ptr; incr(k))
  234.         undump_wd(font_info[k]);
  235.     undump_size(FONT_BASE, FONT_MAX, "font max", font_ptr);
  236.     for (k = null_font; k <= font_ptr; incr(k)) {
  237.         undump_qqqq(font_check[k]);
  238.         undump_wd(font_size[k]);
  239.         undump_wd(font_dsize[k]);
  240.         undump(MIN_HALFWORD, MAX_HALFWORD, font_params[k]);
  241.         undump_int(hyphen_char[k]);
  242.         undump_int(skew_char[k]);
  243.         undump(0, str_ptr, font_name[k]);
  244.         undump(0, str_ptr, font_area[k]);
  245.         undump(0, 255, font_bc[k]);
  246.         undump(0, 255, font_ec[k]);
  247.         undump_int(char_base[k]);
  248.         undump_int(width_base[k]);
  249.         undump_int(height_base[k]);
  250.         undump_int(depth_base[k]);
  251.         undump_int(italic_base[k]);
  252.         undump_int(lig_kern_base[k]);
  253.         undump_int(kern_base[k]);
  254.         undump_int(exten_base[k]);
  255.         undump_int(param_base[k]);
  256.         undump(MIN_HALFWORD, hi_mem_min, font_glue[k]);
  257.     }
  258.  
  259.     undump(0, HYPH_SIZE, hyph_count);
  260.     for (k = 1; k <= hyph_count; incr(k)) {
  261.         undump(0, HYPH_SIZE, j);
  262.         undump(0, str_ptr, hyph_word[j]);
  263.         undump(MIN_HALFWORD, MAX_HALFWORD, hyph_list[j]);
  264.     }
  265.     undump_size(0, TRIE_SIZE, "trie size", trie_max);
  266.     for (k = 0; k <= trie_max; incr(k))
  267.         undump_hh(trie[k]);
  268.     undump(MIN_QUARTERWORD, MAX_QUARTERWORD, trie_op_ptr);
  269.     for (k = MIN_QUARTERWORD + 1; k <= trie_op_ptr; incr(k))  {
  270.         undump(0, 63, hyf_distance[k]);
  271.         undump(0, 63, hyf_num[k]);
  272.         undump(MIN_QUARTERWORD, MAX_QUARTERWORD, hyf_next[k]);
  273.     }
  274.  
  275.     undump(BATCH_MODE, ERROR_STOP_MODE, interaction);
  276.     undump(0, str_ptr, format_ident);
  277.     undump_int(x);
  278.  
  279.     if (x != 6969 || feof(fmt_file)) goto bad_fmt;
  280.  
  281. #if (defined(atarist) && (!(defined(INIT))))
  282.     Mfree(_st_b);
  283. #endif
  284.     return TRUE;
  285.  
  286. bad_fmt:
  287.     puts("(Fatal format file error; I'm stymied)");
  288. #if (defined(atarist) && (!(defined(INIT))))
  289.     Mfree(_st_b);
  290. #endif    
  291.     return FALSE;
  292. }
  293.  
  294. #ifdef INIT
  295.  
  296. #define dump_ascii()    putc(str_pool[k], fmt_file)
  297. #define dump_int(W)     Putw(W, fmt_file)
  298. #define dump_hh(W)      fwrite(&W, sizeof(W), 1, fmt_file);
  299. #define dump_wd(W)      fwrite(&W, sizeof(W), 1, fmt_file);
  300. #define dump_qqqq(W)    fwrite(&W, sizeof(W), 1, fmt_file);
  301.  
  302. store_fmt_file ()
  303. {
  304.     int     j;
  305.     int     k;
  306.     int     l;
  307.     ptr     p;
  308.     ptr     q;
  309.     val     x;
  310.  
  311.     if (save_ptr != 0) {
  312.         print_nl("! You can't \\dump inside a group");
  313.         help1("`{...\\dump}' is a no-no.");
  314.         succumb();
  315.     }
  316.     selector = NEW_STRING;
  317.     print(" (preloaded format=");
  318.     print_str(job_name);
  319.     print_char(' ');
  320.     print_val(year % 100);
  321.     print_char('.');
  322.     print_val(month);
  323.     print_char('.');
  324.     print_val(day);
  325.     print_char(')');
  326.     if (interaction == BATCH_MODE)
  327.         selector = LOG_ONLY;
  328.     else selector = TERM_AND_LOG;
  329.     str_room(1);
  330.     format_ident = make_str();
  331.     pack_job_name(str_fmt);
  332.     while ((fmt_file = w_open_out()) == NULL)
  333.         prompt_file_name("format file name", str_fmt);
  334.     print_nl("Beginning to dump on file ");
  335.     print_str(w_make_name_string(fmt_file));
  336.     flush_string();
  337.     print_nl("");
  338.     print_str(format_ident);
  339.  
  340.     dump_int(MEM_BOT);
  341.     dump_int(MEM_TOP);
  342.     dump_int(TOK_BOT);
  343.     dump_int(TOK_TOP);
  344.     dump_int(EQTB_SIZE);
  345.     dump_int(HASH_PRIME);
  346.     dump_int(HYPH_SIZE);
  347.  
  348.     dump_int(pool_ptr);
  349.     dump_int(str_ptr);
  350.     dump_int(null_str);
  351.     for (k = 0; k <= str_ptr; incr(k))
  352.         dump_int(str_start[k]);
  353.     for (k = 0; k <= pool_ptr; incr(k))
  354.         dump_ascii();
  355.     print_ln();
  356.     print_int(str_ptr);
  357.     print(" strings of total length ");
  358.     print_int(pool_ptr);
  359.  
  360.     x = 0;
  361.     dump_int(tok_low);
  362.     dump_int(tok_head);
  363.     tok_used = tok_end + 1 - tok_low;
  364.     for (k = tok_low; k <= tok_end; incr(k)) {
  365.         dump_wd(tok_link[k]);
  366.         dump_wd(tok_mem[k]);
  367.     }
  368.     for (p = tok_head; p != NULL; p = token_link(p))
  369.         decr(tok_used);
  370.     x += tok_end + 1 - tok_low;
  371.  
  372.     sort_avail();
  373.     dump_int(lo_mem_max);
  374.     dump_int(rover);
  375.     var_used = 0;
  376.     p = MEM_BOT; q = rover;
  377.     do {
  378.         for (k = p; k <= q + 1; incr(k))
  379.             dump_wd(mem[k]);
  380.         x += q + 2 - p;
  381.         var_used += q - p;
  382.         p = q + node_size(q);
  383.         q = rlink(q);
  384.     } while (q != rover);
  385.     var_used += lo_mem_max - p;
  386.     for (k = p; k <= lo_mem_max; incr(k))
  387.         dump_wd(mem[k]);
  388.     x += lo_mem_max + 1 - p;
  389.  
  390.     dyn_used = mem_end + 1 - hi_mem_min;
  391.     dump_int(hi_mem_min);
  392.     dump_int(avail);
  393.     for (k = hi_mem_min; k <= mem_end; incr(k))
  394.         dump_wd(mem[k]);
  395.     for (p = avail; p != NULL; p = link(p))
  396.         decr(dyn_used);
  397.     x += mem_end + 1 - hi_mem_min;
  398.  
  399.     dump_int(var_used);
  400.     dump_int(dyn_used);
  401.     dump_int(tok_used);
  402.     print_ln();
  403.     print_val(x);
  404.     print(" memory locations dumped; current usage is ");
  405.     print_int(var_used);
  406.     print_char(',');
  407.     print_int(dyn_used);
  408.     print_char(',');
  409.     print_int(tok_used);
  410.  
  411.     k = ACTIVE_BASE;
  412.     do {
  413.         for (j = k; j < INT_BASE - 1; incr(j))
  414.             if (equiv(j) == equiv(j + 1) &&
  415.                 eq_type(j) == eq_type(j + 1) &&
  416.                 eq_level(j) == eq_level(j + 1))
  417.                 goto found1;
  418.         l = INT_BASE;
  419.         goto done1;
  420.     
  421.     found1:
  422.         incr(j);
  423.         l = j;
  424.         for (; j < INT_BASE - 1; incr(j))
  425.             if (equiv(j) != equiv(j + 1) ||
  426.                 eq_type(j) != eq_type(j + 1) ||
  427.                 eq_level(j) != eq_level(j + 1))
  428.                 goto done1;
  429.     
  430.     done1:
  431.         dump_int(l - k);
  432.         for (; k < l; incr(k))
  433.             dump_wd(eqtb[k]);
  434.         k = j + 1;
  435.         dump_int(k - l);
  436.     } while (k != INT_BASE);
  437.  
  438.     do {
  439.         for (j = k; j < EQTB_SIZE; incr(j))
  440.             if (eqtb[j].i == eqtb[j + 1].i)
  441.                 goto found2;
  442.         l = EQTB_SIZE + 1;
  443.         goto done2;
  444.  
  445.     found2:
  446.         incr(j);
  447.         l = j;
  448.         for (; j < EQTB_SIZE; incr(j))
  449.             if (eqtb[j].i != eqtb[j + 1].i)
  450.                 goto done2;
  451.     
  452.     done2:
  453.         dump_int(l - k);
  454.         for (; k < l; incr(k))
  455.             dump_wd(eqtb[k]);
  456.         k = j + 1;
  457.         dump_int(k - l);
  458.     } while (k <= EQTB_SIZE);
  459.     dump_int(par_loc);
  460.     dump_int(write_loc);
  461.     
  462.     dump_int(hash_used);
  463.     cs_count = FROZEN_CONTROL_SEQUENCE - 1 - hash_used;
  464.     for (p = HASH_BASE; p <= hash_used; incr(p))
  465.         if (text(p) != 0) {
  466.             dump_int(p);
  467.             dump_hh(hash[p]);
  468.             incr(cs_count);
  469.         }
  470.     for (p = hash_used + 1; p < UNDEFINED_CONTROL_SEQUENCE; incr(p))
  471.         dump_hh(hash[p]);
  472.     dump_int(cs_count);
  473.     print_ln();
  474.     print_int(cs_count);
  475.     print(" multiletter control sequences");
  476.  
  477.     dump_int(fmem_ptr);
  478.     for (k = 0; k < fmem_ptr; incr(k))
  479.         dump_wd(font_info[k]);
  480.     dump_int(font_ptr);
  481.     for (k = null_font; k <= font_ptr; incr(k)) {
  482.         dump_qqqq(font_check[k]);
  483.         dump_wd(font_size[k]);
  484.         dump_wd(font_dsize[k]);
  485.         dump_int(font_params[k]);
  486.         dump_int(hyphen_char[k]);
  487.         dump_int(skew_char[k]);
  488.         dump_int(font_name[k]);
  489.         dump_int(font_area[k]);
  490.         dump_int(font_bc[k]);
  491.         dump_int(font_ec[k]);
  492.         dump_int(char_base[k]);
  493.         dump_int(width_base[k]);
  494.         dump_int(height_base[k]);
  495.         dump_int(depth_base[k]);
  496.         dump_int(italic_base[k]);
  497.         dump_int(lig_kern_base[k]);
  498.         dump_int(kern_base[k]);
  499.         dump_int(exten_base[k]);
  500.         dump_int(param_base[k]);
  501.         dump_int(font_glue[k]);
  502.         print_nl("\\font");
  503.         print_esc("");
  504.         print_str(font_id_text(k));
  505.         print_char('=');
  506.         print_file_name(font_name[k], font_area[k], null_str);
  507.         if (font_size[k] != font_dsize[k]) {
  508.             print(" at ");
  509.             print_scaled(font_size[k]);
  510.             print("pt");
  511.         }
  512.     }
  513.     print_ln();
  514.     print_int(fmem_ptr - 7);
  515.     print(" words of font info for ");
  516.     print_int(font_ptr - FONT_BASE);
  517.     print(" preloaded font");
  518.     if (font_ptr != FONT_BASE + 1)
  519.         print_char('s');
  520.  
  521.     dump_int(hyph_count);
  522.     for (k = 0; k <= HYPH_SIZE; incr(k)) {
  523.         if (hyph_word[k] != 0) {
  524.             dump_int(k);
  525.             dump_int(hyph_word[k]);
  526.             dump_int(hyph_list[k]);
  527.         }
  528.     }
  529.     dump_int(trie_max);
  530.     for (k = 0; k <= trie_max; incr(k))
  531.         dump_hh(trie[k]);
  532.     dump_int(trie_op_ptr);
  533.     for (k = MIN_QUARTERWORD + 1; k <= trie_op_ptr; incr(k)) {
  534.         dump_int(hyf_distance[k]);
  535.         dump_int(hyf_num[k]);
  536.         dump_int(hyf_next[k]);
  537.     }
  538.     print_ln();
  539.     print_int(hyph_count);
  540.     print(" hyphenation exception");
  541.     if (hyph_count != 1)
  542.         print_char('s');
  543.     print_nl("Hyphenation trie of length ");
  544.     print_int(trie_max);
  545.     print(" has ");
  546.     print_int(qo(trie_op_ptr));
  547.     print(" op");
  548.     if (trie_op_ptr != MIN_QUARTERWORD + 1)
  549.         print_char('s');
  550.  
  551.     dump_int(interaction);
  552.     dump_int(format_ident);
  553.     dump_int(6969);
  554.     w_close(fmt_file);
  555.     tracing_stats = 0;
  556. }
  557. #endif
  558.