OSDN Git Service

clean
[putex/putex.git] / src / texsourc / tex9.c
1 #ifdef _WINDOWS
2   #define NOCOMM
3   #define NOSOUND
4   #define NODRIVERS
5   #define STRICT
6   #pragma warning(disable:4115) // kill rpcasync.h complaint
7   #include <windows.h>
8   #define MYLIBAPI __declspec(dllexport)
9 #endif
10
11 #include "texwin.h"
12
13 #pragma warning(disable:4996)
14 #pragma warning(disable:4131) // old style declarator
15 #pragma warning(disable:4135) // conversion between different integral types 
16 #pragma warning(disable:4127) // conditional expression is constant
17
18 #include <setjmp.h>
19
20 #define EXTERN extern
21
22 #include "texd.h"
23
24 #pragma warning(disable:4244)       /* 96/Jan/10 */
25
26 /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
27
28 /* end of the old tex8.c */
29 /* sec 1284 */
30 void give_err_help (void)
31 {
32   token_show(err_help);
33 }
34 /* sec 0524 */
35 bool open_fmt_file (void)
36 {
37   register bool Result;
38   integer j;
39   j = cur_input.loc_field;
40
41 /* For Windows NT, lets allow + instead of & for format specification */
42 /*  User specified a format name on the command line                  */
43   if (buffer[cur_input.loc_field]== '&' || buffer[cur_input.loc_field]== '+')
44   {
45     incr(cur_input.loc_field);
46     j = cur_input.loc_field;
47     buffer[last] = ' ';
48     while (buffer[j] != ' ')
49       incr(j);
50     pack_buffered_name(0, cur_input.loc_field, j - 1);
51
52     if (w_open_in(fmt_file))
53       goto lab40;  /* format file opened OK */
54   
55 /* format file open failed */
56   if (knuth_flag)
57   {
58     (void) sprintf(log_line, "%s;%s\n", "Sorry, I can't find that format",
59       " will try the default.");
60     show_line(log_line, 1);
61   }
62   else
63   {
64     char *s = log_line;
65     name_of_file[name_length + 1] = '\0';
66     (void) sprintf(s, "%s (%s);%s\n", "Sorry, I can't find that format",
67       name_of_file + 1, " will try the default."); 
68     name_of_file[name_length + 1] = ' ';
69     s += strlen(s);
70     (void) sprintf(s, "(Perhaps your %s environment variable is not set correctly)\n",
71       "TEXFORMATS");
72     s += strlen(s);
73     {
74       char *t;            /* extra info 97/June/13 */
75       if ((t = grabenv("TEXFORMATS")) != NULL)
76       {
77         sprintf(s, "(%s=%s)\n", "TEXFORMATS", t);
78       }
79       else
80       {
81         sprintf(s, "%s environment variable not set\n", "TEXFORMATS");
82       }
83     }
84     show_line(log_line, 1); // show all three lines at once
85   }
86 #ifndef _WINDOWS
87     fflush(stdout);
88 #endif
89   }
90 /* Try the default format (either because no format specified or failed) */
91   pack_buffered_name(format_default_length - 4, 1, 0);
92
93   if (!w_open_in(fmt_file))
94   {
95     if (knuth_flag)
96     {
97       (void) sprintf(log_line, "%s!\n", "I can't find the default format file");
98       show_line(log_line, 1);
99     }
100     else
101     {
102       char *s = log_line;
103       name_of_file[name_length + 1] = '\0';
104       (void) sprintf(s, "%s (%s)!\n", "I can't find the default format file", name_of_file + 1);
105       name_of_file[name_length + 1] = ' ';
106       s += strlen(s);
107       (void) sprintf(s, "(Perhaps your %s environment variable is not set correctly)\n", "TEXFORMATS");
108       s += strlen(s);
109       {
110         char *t;            /* extra info 97/June/13 */
111         if ((t = grabenv("TEXFORMATS")) != NULL)
112         {
113           sprintf(s, "(%s=%s)\n", "TEXFORMATS", t);
114         }
115         else
116         {
117           sprintf(s, "%s environment variable not set\n", "TEXFORMATS");
118         }
119       }
120       show_line(log_line, 1);   // show all three lines at once
121     }
122     Result = false;
123     return(Result);
124   }
125 lab40:
126   cur_input.loc_field = j;
127   Result = true;
128   return Result;
129 }
130 /**************************************************************************/
131 void print_char_string (unsigned char *s)
132 {
133   while (*s > 0)
134     print_char(*s++);
135 }
136 void show_font_info (void);   // now in local.c
137 extern int closed_already;     // make sure we don't try this more than once
138 /* The following needs access to zdvibuf of ALLOCATEDVIBUF 94/Mar/24 */
139 /* done in closefilesandterminate_regmem  in coerce.h */
140 /* sec 1333 */
141 void close_files_and_terminate (void)
142 {
143   integer k; 
144
145   if (closed_already++)
146   {
147     show_line("close_files_and_terminated already ", 0);
148     return;     // sanity check
149   }
150
151   if (trace_flag)
152     show_line("\nclose_files_and_terminate ", 0);
153 /* close all open files */
154   for (k = 0; k <= 15; k++)
155     if (write_open[k])
156     {
157       (void) a_close(write_file[k]);
158     }
159
160 #ifdef STAT
161 /* if tracing_stats>0 then @<Output statistics about this job@>; */
162 /*  if (eqtb[(hash_size + 3194)].cint > 0) */
163   if (tracing_stats > 0 || verbose_flag != 0)  /* 93/Nov/30 - bkph */
164     if (log_opened)
165     {
166 /* used to output paragraph breaking statistics here */
167       (void) fprintf(log_file, "%c\n", ' ');
168       (void) fprintf(log_file, "\n");
169       (void) fprintf(log_file, "%s%s\n", "Here is how much of TeX's memory", " you used:");
170       (void) fprintf(log_file, "%c%ld%s", ' ', (long)str_ptr - init_str_ptr, " string");
171       if (str_ptr != init_str_ptr + 1)
172         (void) putc('s',  log_file);
173 #ifdef ALLOCATESTRING
174       if (show_current)
175         (void) fprintf(log_file, "%s%ld\n", " out of ", (long) current_max_strings - init_str_ptr);
176       else
177 #endif
178         (void) fprintf(log_file, "%s%ld\n", " out of ", (long) max_strings - init_str_ptr);
179
180 #ifdef ALLOCATESTRING
181       if (show_current)
182         (void) fprintf(log_file, "%c%ld%s%ld\n", ' ', (long) pool_ptr - init_pool_ptr, " string characters out of ", (long) current_pool_size - init_pool_ptr);
183       else
184 #endif
185         (void) fprintf(log_file, "%c%ld%s%ld\n", ' ', (long) pool_ptr - init_pool_ptr, " string characters out of ", (long) pool_size - init_pool_ptr);
186
187 #ifdef ALLOCATEMAIN
188       if (show_current)
189         (void) fprintf(log_file, "%c%ld%s%ld\n", ' ', (long)lo_mem_max - mem_min + mem_end - hi_mem_min + 2, " words of memory out of ", (long)current_mem_size);
190       else
191 #endif
192         (void) fprintf(log_file, "%c%ld%s%ld\n", ' ', (long)lo_mem_max - mem_min + mem_end - hi_mem_min + 2, " words of memory out of ", (long)mem_end + 1 - mem_min);
193       (void) fprintf(log_file, "%c%ld%s%ld\n", ' ', (long)cs_count, " multiletter control sequences out of ", (long)(hash_size + hash_extra));
194       (void) fprintf(log_file, "%c%ld%s%ld%s", ' ', (long)fmem_ptr, " words of font info for ", (long)font_ptr - 0, " font");
195       if (font_ptr != 1)
196         (void) putc('s',  log_file);
197
198 #ifdef ALLOCATEFONT
199       if (show_current)
200         (void) fprintf(log_file, "%s%ld%s%ld\n", ", out of ", (long)current_font_mem_size, " for ", (long)font_max - 0);
201       else
202 #endif
203         (void) fprintf(log_file, "%s%ld%s%ld\n", ", out of ", (long)font_mem_size, " for ", (long)font_max - 0);
204       (void) fprintf(log_file, "%c%ld%s", ' ', (long)hyph_count, " hyphenation exception");
205       if (hyph_count != 1)
206         (void) putc('s',  log_file);
207       (void) fprintf(log_file, "%s%ld\n",  " out of ", (long) hyphen_prime);
208       (void) fprintf(log_file, " ");
209       (void) fprintf(log_file, "%ld%s", (long)max_in_stack, "i,");
210       (void) fprintf(log_file, "%ld%s", (long)max_nest_stack, "n,");
211       (void) fprintf(log_file, "%ld%s", (long)max_param_stack, "p,");
212       (void) fprintf(log_file, "%ld%s", (long)max_buf_stack + 1, "b,");
213       (void) fprintf(log_file, "%ld%s", (long)max_save_stack + 6, "s");
214       (void) fprintf(log_file, " stack positions out of ");
215
216 #ifdef ALLOCATESAVESTACK
217       if (show_current)
218         (void) fprintf(log_file, "%ld%s", (long)current_stack_size, "i,");
219       else
220 #endif
221         (void) fprintf(log_file, "%ld%s", (long)stack_size, "i,");
222
223 #ifdef ALLOCATENESTSTACK
224       if (show_current)
225         (void) fprintf(log_file, "%ld%s", (long)current_nest_size, "n,");
226       else
227 #endif
228         (void) fprintf(log_file, "%ld%s", (long)nest_size, "n,");
229
230 #ifdef ALLOCATEPARAMSTACK
231       if (show_current)
232         (void) fprintf(log_file, "%ld%s", (long)current_param_size, "p,");
233       else
234 #endif
235         (void) fprintf(log_file, "%ld%s", (long)param_size, "p,");
236
237 #ifdef ALLOCATEBUFFER
238       if (show_current)
239         (void) fprintf(log_file, "%ld%s", (long)current_buf_size, "b,");
240       else
241 #endif
242         (void) fprintf(log_file, "%ld%s", (long)buf_size, "b,");
243
244 #ifdef ALLOCATESAVESTACK
245       if (show_current)
246         (void) fprintf(log_file, "%ld%s", (long)current_save_size, "s");
247       else
248 #endif
249         (void) fprintf(log_file, "%ld%s", (long)save_size, "s");
250       (void) fprintf(log_file, "\n");
251
252       if (!knuth_flag)
253         fprintf(log_file, " (i = in_stack, n = nest_stack, p = param_stack, b = buf_stack, s = save_stack)\n");
254
255       if (! knuth_flag)          /* 1999/Jan/17 */
256         fprintf(log_file, " %d inputs open max out of %d\n", high_in_open, max_in_open);
257       /* (%d max parens open) */ /* max_open_parens */
258
259 /*  Modified 98/Jan/14 to leave out lines with zero counts */
260       if (show_line_break_stats && first_pass_count > 0) /* 96/Feb/8 */
261       {
262         int first_count, secondcount, thirdcount;
263         (void) fprintf(log_file, "\nSuccess at breaking %d paragraph%s:", first_pass_count, (first_pass_count == 1) ? "" : "s");
264         if (single_line > 0)
265           (void) fprintf(log_file, "\n %d single line `paragraph%s'", single_line, (single_line == 1) ? "" : "s");  /* 96/Apr/23 */
266         first_count = first_pass_count-single_line-second_pass_count;
267         if (first_count < 0)
268           first_count = 0;       /* sanity check */
269         secondcount = second_pass_count - final_pass_count;
270         thirdcount = final_pass_count - paragraph_failed;
271         if (first_count != 0 || secondcount != 0 || thirdcount != 0)
272           (void) fprintf(log_file, "\n %d first pass (\\pretolerance = %d)", first_count, pretolerance);
273         if (secondcount != 0 || thirdcount != 0)
274           (void) fprintf(log_file, "\n %d second pass (\\tolerance = %d)", secondcount, tolerance);
275         if (final_pass_count > 0 || emergency_stretch > 0)
276         {
277           (void) fprintf(log_file, "\n %d third pass (\\emergencystretch = %lgpt)", thirdcount, (double) emergency_stretch / 65536.0);
278           /* above converted from scaled points to printer's points */
279         }
280         if (paragraph_failed > 0)
281           (void) fprintf(log_file, "\n %d failed", paragraph_failed);
282         (void) putc('\n', log_file);
283         if (overfull_hbox > 0)
284           (void) fprintf(log_file, "\n %d overfull \\hbox%s", overfull_hbox, (overfull_hbox > 1) ? "es" : "");
285         if (underfull_hbox > 0)
286           (void) fprintf(log_file, "\n %d underfull \\hbox%s", underfull_hbox, (underfull_hbox > 1) ? "es" : "");
287         if (overfull_vbox > 0)
288           (void) fprintf(log_file, "\n %d overfull \\vbox%s", overfull_vbox, (overfull_vbox > 1) ? "es" : "");
289         if (underfull_vbox > 0)
290           (void) fprintf(log_file, "\n %d underfull \\vbox%s", underfull_vbox, (underfull_vbox > 1) ? "es" : "");
291         if (overfull_hbox || underfull_hbox || overfull_vbox || underfull_vbox)
292           (void) putc('\n', log_file);
293       }
294   } /* end of if (log_opened) */ 
295 #endif /* STAT */
296   while (cur_s > -1) {
297     if (cur_s > 0) 
298       dvi_out(142);
299     else
300     {
301       dvi_out(140);
302       incr(total_pages);
303     }
304     decr(cur_s);
305   }
306
307   if (total_pages == 0)
308     print_nl("No pages of output.");
309   else {
310     dvi_out(248); /* post - start of postamble */
311     dvi_four(last_bop);
312     last_bop = dvi_offset + dvi_ptr - 5;
313     dvi_four(25400000L);     /* magic DVI scale factor */ 
314     dvi_four(473628672L);    /* 7227 * 65536 */
315     prepare_mag();           /* in tex2.c */
316     dvi_four(mag);           /* mag */
317     dvi_four(max_v);         /* max height + depth */
318     dvi_four(max_h);         /* max width */
319     dvi_out(max_push / 256);
320     dvi_out(max_push % 256);
321     if (total_pages >= 65536)    // 99/Oct/10 dvi_t 16 bit problem
322     {
323       sprintf(log_line, "\nWARNING: page count (dvi_t) in DVI file will be %ld not %ld\n",
324         (total_pages % 65536), total_pages);
325       if (log_opened)
326         (void) fputs (log_line, log_file);
327       show_line(log_line, 1);
328     }
329     dvi_out((total_pages / 256) % 256);
330     dvi_out(total_pages % 256);
331     if (show_fonts_used && log_opened)     /* 97/Dec/24 */
332       show_font_info();           // now in local.c
333     while (font_ptr > 0) {
334       if (font_used[font_ptr])
335         dvi_font_def(font_ptr);
336       decr(font_ptr);
337     }
338     dvi_out(249); /* post_post end of postamble */
339     dvi_four(last_bop);
340     dvi_out(2);
341     k = 4 + ((dvi_buf_size - dvi_ptr) % 4);
342     while (k > 0) {
343       dvi_out(223); /* four to seven bytes of 223 */
344       decr(k);
345     }
346
347     if (trace_flag)         /* 93/Dec/28 - bkph */
348     {
349       sprintf(log_line, "\ndviwrite %d", dvi_gone);
350       show_line(log_line, 0);
351     }
352
353     if (dvi_limit == half_buf)
354       writedvi(half_buf, dvi_buf_size - 1); 
355     if (dvi_ptr > 0)
356       writedvi(0, dvi_ptr - 1); 
357     print_nl("Output written on ");
358     if (full_file_name_flag && dvi_file_name != NULL)
359       print_char_string((unsigned char *) dvi_file_name);
360     else
361       slow_print(output_file_name);
362     print_string(" (");
363     print_int(total_pages);
364     print_string(" page");
365     if (total_pages != 1)
366       print_char('s');
367     print_string(", ");
368     print_int(dvi_offset + dvi_ptr);
369     print_string(" bytes).");
370     b_close(dvi_file);
371   }
372
373   if (log_opened)
374   {
375     (void) putc('\n', log_file);
376     (void) a_close(log_file); 
377     selector = selector - 2; 
378     if (selector == term_only)
379     {
380       print_nl("Transcript written on ");
381       if (full_file_name_flag && log_file_name != NULL)
382         print_char_string((unsigned char *) log_file_name);
383       else
384         slow_print(texmf_log_name);
385       print_char('.');
386     }
387   }
388   print_ln();
389
390   if ((edit_name_start != 0) && (interaction > 0))
391   {
392     call_edit(str_pool, edit_name_start, edit_name_length, edit_line);
393   }
394 } /* end of close_files_and_terminate */
395 #ifdef DEBUG
396 /* sec 1338 */
397 void debug_help (void) 
398 {/* 888 10 */ 
399   integer k, l, m, n; 
400   while (true) {
401  ; 
402     print_nl(" debug # (-1 to exit):");
403 #ifndef _WINDOWS
404     fflush(stdout); 
405 #endif
406     read(stdin, m);  // ???
407     if (m < 0)return; 
408     else if (m == 0)
409     dumpcore(); 
410     else {
411       read(stdin, n);  // ???
412       switch(m)
413       {case 1 : 
414   print_word(mem[n]); 
415   break; 
416       case 2 : 
417   print_int(mem[n].hh.v.LH); 
418   break; 
419       case 3 : 
420   print_int(mem[n].hh.v.RH); 
421   break; 
422       case 4 : 
423   print_word(eqtb[n]); 
424   break; 
425       case 5 : 
426 #ifdef SHORTFONTINFO
427   print_scaled(font_info[n].sc);  print_char(' ');
428   print_int(font_info[n].qqq.b0);  print_char(':');
429   print_int(font_info[n].qqq.b1);  print_char(':');
430   print_int(font_info[n].qqq.b2);  print_char(':');
431   print_int(font_info[n].qqq.b3);  
432 #else
433   print_word(font_info[n]); 
434 #endif
435   break; 
436       case 6 : 
437   print_word(save_stack[n]); 
438   break; 
439       case 7 : 
440   show_box(n); 
441   break; 
442       case 8 : 
443   {
444     breadth_max = 10000; 
445 /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
446 #ifdef ALLOCATESTRING
447 /* About to output node list make some space in string pool 97/Mar/9 */
448   if (pool_ptr + 32000 > current_pool_size)
449     str_pool = realloc_str_pool (increment_pool_size);
450 /* We don't bother to check whether this worked */
451 #endif
452 #ifdef ALLOCATESTRING
453     depth_threshold = current_pool_size - pool_ptr - 10; 
454 #else
455 /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
456     depth_threshold = pool_size - pool_ptr - 10; 
457 #endif
458     show_node_list(n); 
459   } 
460   break; 
461       case 9 : 
462   show_token_list(n, 0, 1000); 
463   break; 
464       case 10 : 
465   slow_print(n); 
466   break; 
467       case 11 : 
468   check_mem(n > 0); 
469   break; 
470       case 12 : 
471   search_mem(n); 
472   break; 
473       case 13 : 
474   {
475     read(stdin, l);  // ???
476     print_cmd_chr(n, l); 
477   } 
478   break; 
479       case 14 : 
480   {
481     register integer for_end; 
482     k = 0; 
483     for_end = n; 
484     if (k <= for_end) 
485       do print(buffer[k]); 
486     while(k++ < for_end);
487   } 
488   break; 
489       case 15 : 
490   {
491     font_in_short_display = 0; 
492     short_display(n); 
493   } 
494   break; 
495       case 16 : 
496   panicking = !panicking; 
497   break; 
498   default: 
499   print(63);    /* ? */
500   break; 
501       } 
502     } 
503   } 
504
505 #endif /* DEBUG */