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