OSDN Git Service

chr_cmd().
[putex/putex.git] / src / texsourc / tex9.c
1 /* Copyright 2014 Clerk Ma
2
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; either version 2 of the License, or
6    (at your option) any later version.
7
8    This program is distributed in the hope that it will be useful, but
9    WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11    General Public License for more details.
12
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16    02110-1301 USA.  */
17
18 #define EXTERN extern
19
20 #include "texd.h"
21
22 /* end of the old tex8.c */
23 /* sec 1284 */
24 void give_err_help (void)
25 {
26   token_show(err_help);
27 }
28 /* sec 0524 */
29 boolean open_fmt_file (void)
30 {
31   integer j;
32
33   j = loc;
34
35   if (buffer[loc] == '&' || buffer[loc] == '+')
36   {
37     incr(loc);
38     j = loc;
39     buffer[last] = ' ';
40
41     while (buffer[j] != ' ')
42       incr(j);
43
44     pack_buffered_name(0, loc, j - 1);
45
46     if (w_open_in(fmt_file))
47       goto lab40;
48   
49     if (knuth_flag)
50     {
51       sprintf(log_line, "%s;%s\n", "Sorry, I can't find that format",
52         " will try the default.");
53       show_line(log_line, 1);
54     }
55     else
56     {
57       char *s = log_line;
58
59       name_of_file[name_length + 1] = '\0';
60       sprintf(s, "%s (%s);%s\n", "Sorry, I can't find that format",
61         name_of_file + 1, " will try the default."); 
62       name_of_file[name_length + 1] = ' ';
63       s += strlen(s);
64       sprintf(s, "(Perhaps your %s environment variable is not set correctly)\n",
65         "TEXFORMATS");
66       s += strlen(s);
67       show_line(log_line, 1);
68     }
69
70 #ifndef _WINDOWS
71     fflush(stdout);
72 #endif
73   }
74
75   pack_buffered_name(format_default_length - 4, 1, 0);
76
77   if (!w_open_in(fmt_file))
78   {
79     if (knuth_flag)
80     {
81       sprintf(log_line, "%s!\n", "I can't find the default format file");
82       show_line(log_line, 1);
83     }
84     else
85     {
86       char *s = log_line;
87
88       name_of_file[name_length + 1] = '\0';
89       sprintf(s, "%s (%s)!\n", "I can't find the default format file", name_of_file + 1);
90       name_of_file[name_length + 1] = ' ';
91       s += strlen(s);
92       sprintf(s, "(Perhaps your %s environment variable is not set correctly)\n", "TEXFORMATS");
93       s += strlen(s);
94       show_line(log_line, 1);
95     }
96
97     return false;
98   }
99
100 lab40:
101   loc = j;
102
103   return true;
104 }
105 /**************************************************************************/
106 void print_char_string (unsigned char *s)
107 {
108   while (*s > 0)
109     print_char(*s++);
110 }
111 void show_font_info (void); // now in local.c
112 extern int closed_already;  // make sure we don't try this more than once
113 /* sec 1333 */
114 void close_files_and_terminate (void)
115 {
116   integer k; 
117
118   if (closed_already++)
119   {
120     puts("close_files_and_terminated already ");
121     return;     // sanity check
122   }
123
124   if (trace_flag)
125     puts("\nclose_files_and_terminate ");
126
127   for (k = 0; k <= 15; k++)
128     if (write_open[k])
129     {
130       (void) a_close(write_file[k]);
131     }
132
133 #ifdef STAT
134   if (tracing_stats > 0 || verbose_flag != 0)
135     if (log_opened)
136     {
137       fprintf(log_file, "%c\n", ' ');
138       fprintf(log_file, "\n");
139       fprintf(log_file, "%s%s\n", "Here is how much of TeX's memory", " you used:");
140       fprintf(log_file, "%c%ld%s", ' ', str_ptr - init_str_ptr, " string");
141
142       if (str_ptr != init_str_ptr + 1)
143         putc('s',  log_file);
144
145 #ifdef ALLOCATESTRING
146       if (show_current)
147         fprintf(log_file, "%s%ld\n", " out of ", current_max_strings - init_str_ptr);
148       else
149 #endif
150         fprintf(log_file, "%s%ld\n", " out of ", max_strings - init_str_ptr);
151
152 #ifdef ALLOCATESTRING
153       if (show_current)
154         fprintf(log_file, "%c%ld%s%ld\n", ' ', pool_ptr - init_pool_ptr, " string characters out of ", current_pool_size - init_pool_ptr);
155       else
156 #endif
157         fprintf(log_file, "%c%ld%s%ld\n", ' ', pool_ptr - init_pool_ptr, " string characters out of ", pool_size - init_pool_ptr);
158
159 #ifdef ALLOCATEMAIN
160       if (show_current)
161         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 ", current_mem_size);
162       else
163 #endif
164         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 ", mem_end + 1 - mem_min);
165
166       fprintf(log_file, "%c%ld%s%ld\n", ' ', cs_count, " multiletter control sequences out of ", (hash_size + hash_extra));
167       fprintf(log_file, "%c%ld%s%ld%s", ' ', (long)fmem_ptr, " words of font info for ", (long)font_ptr - font_base, " font");
168
169       if (font_ptr != 1)
170         putc('s',  log_file);
171
172 #ifdef ALLOCATEFONT
173       if (show_current)
174         fprintf(log_file, "%s%ld%s%ld\n", ", out of ", current_font_mem_size, " for ", font_max - font_base);
175       else
176 #endif
177         fprintf(log_file, "%s%ld%s%ld\n", ", out of ", font_mem_size, " for ", font_max - font_base);
178
179       fprintf(log_file, "%c%ld%s", ' ', hyph_count, " hyphenation exception");
180
181       if (hyph_count != 1)
182         putc('s',  log_file);
183
184       fprintf(log_file, "%s%ld\n",  " out of ", hyphen_prime);
185       fprintf(log_file, " ");
186       fprintf(log_file, "%ld%s", max_in_stack, "i,");
187       fprintf(log_file, "%ld%s", max_nest_stack, "n,");
188       fprintf(log_file, "%ld%s", max_param_stack, "p,");
189       fprintf(log_file, "%ld%s", max_buf_stack + 1, "b,");
190       fprintf(log_file, "%ld%s", max_save_stack + 6, "s");
191       fprintf(log_file, " stack positions out of ");
192
193 #ifdef ALLOCATESAVESTACK
194       if (show_current)
195         fprintf(log_file, "%ld%s", current_stack_size, "i,");
196       else
197 #endif
198         fprintf(log_file, "%ld%s", stack_size, "i,");
199
200 #ifdef ALLOCATENESTSTACK
201       if (show_current)
202         fprintf(log_file, "%ld%s", current_nest_size, "n,");
203       else
204 #endif
205         fprintf(log_file, "%ld%s", nest_size, "n,");
206
207 #ifdef ALLOCATEPARAMSTACK
208       if (show_current)
209         fprintf(log_file, "%ld%s", current_param_size, "p,");
210       else
211 #endif
212         fprintf(log_file, "%ld%s", param_size, "p,");
213
214 #ifdef ALLOCATEBUFFER
215       if (show_current)
216         fprintf(log_file, "%ld%s", current_buf_size, "b,");
217       else
218 #endif
219         fprintf(log_file, "%ld%s", buf_size, "b,");
220
221 #ifdef ALLOCATESAVESTACK
222       if (show_current)
223         fprintf(log_file, "%ld%s", current_save_size, "s");
224       else
225 #endif
226         fprintf(log_file, "%ld%s", save_size, "s");
227
228       fprintf(log_file, "\n");
229
230       if (!knuth_flag)
231         fprintf(log_file, " (i = in_stack, n = nest_stack, p = param_stack, b = buf_stack, s = save_stack)\n");
232
233       if (!knuth_flag)
234         fprintf(log_file, " %d inputs open max out of %d\n", high_in_open, max_in_open);
235
236       if (show_line_break_stats && first_pass_count > 0)
237       {
238         int first_count, second_count, third_count;
239
240         fprintf(log_file, "\nSuccess at breaking %d paragraph%s:", first_pass_count, (first_pass_count == 1) ? "" : "s");
241
242         if (single_line > 0)
243           fprintf(log_file, "\n %d single line `paragraph%s'", single_line, (single_line == 1) ? "" : "s");
244
245         first_count = first_pass_count - single_line - second_pass_count;
246
247         if (first_count < 0)
248           first_count = 0;
249
250         second_count = second_pass_count - final_pass_count;
251         third_count = final_pass_count - paragraph_failed;
252
253         if (first_pass_count > 0)
254           fprintf(log_file, "\n %d first pass (\\pretolerance = %d)", first_pass_count, pretolerance);
255
256         if (second_pass_count > 0)
257           fprintf(log_file, "\n %d second pass (\\tolerance = %d)", second_pass_count, tolerance);
258
259         if (final_pass_count > 0 || emergency_stretch > 0)
260         {
261           fprintf(log_file, "\n %d third pass (\\emergencystretch = %lgpt)", final_pass_count, (double) emergency_stretch / 65536.0);
262         }
263
264         if (paragraph_failed > 0)
265           fprintf(log_file, "\n %d failed", paragraph_failed);
266
267         putc('\n', log_file);
268
269         if (overfull_hbox > 0)
270           fprintf(log_file, "\n %d overfull \\hbox%s", overfull_hbox, (overfull_hbox > 1) ? "es" : "");
271
272         if (underfull_hbox > 0)
273           fprintf(log_file, "\n %d underfull \\hbox%s", underfull_hbox, (underfull_hbox > 1) ? "es" : "");
274
275         if (overfull_vbox > 0)
276           fprintf(log_file, "\n %d overfull \\vbox%s", overfull_vbox, (overfull_vbox > 1) ? "es" : "");
277
278         if (underfull_vbox > 0)
279           fprintf(log_file, "\n %d underfull \\vbox%s", underfull_vbox, (underfull_vbox > 1) ? "es" : "");
280
281         if (overfull_hbox || underfull_hbox || overfull_vbox || underfull_vbox)
282           putc('\n', log_file);
283       }
284   }
285 #endif
286
287   switch (shipout_flag)
288   {
289     case out_dvi_flag:
290     case out_xdv_flag:
291       {
292         while (cur_s > -1)
293         {
294           if (cur_s > 0) 
295             dvi_out(142);
296           else
297           {
298             dvi_out(eop);
299             incr(total_pages);
300           }
301
302           decr(cur_s);
303         }
304
305         if (total_pages == 0)
306           print_nl("No pages of output.");
307         else
308         {
309           dvi_out(post);
310           dvi_four(last_bop);
311           last_bop = dvi_offset + dvi_ptr - 5;
312           dvi_four(25400000L);
313           dvi_four(473628672L);
314           prepare_mag();
315           dvi_four(mag);
316           dvi_four(max_v);
317           dvi_four(max_h);
318           dvi_out(max_push / 256);
319           dvi_out(max_push % 256);
320
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
326             if (log_opened)
327               (void) fputs (log_line, log_file);
328
329             show_line(log_line, 1);
330           }
331
332           dvi_out((total_pages / 256) % 256);
333           dvi_out(total_pages % 256);
334
335           if (show_fonts_used && log_opened)     /* 97/Dec/24 */
336             show_font_info();           // now in local.c
337
338           while (font_ptr > 0)
339           {
340             if (font_used[font_ptr])
341               dvi_font_def(font_ptr);
342
343             decr(font_ptr);
344           }
345
346           dvi_out(post_post);
347           dvi_four(last_bop);
348           dvi_out(id_byte);
349           k = 4 + ((dvi_buf_size - dvi_ptr) % 4);
350
351           while (k > 0)
352           {
353             dvi_out(223);
354             decr(k);
355           }
356
357           if (trace_flag) /* 93/Dec/28 - bkph */
358           {
359             sprintf(log_line, "\ndviwrite %d", dvi_gone);
360             show_line(log_line, 0);
361           }
362
363           if (dvi_limit == half_buf)
364             write_dvi(half_buf, dvi_buf_size - 1);
365
366           if (dvi_ptr > 0)
367             write_dvi(0, dvi_ptr - 1); 
368
369           print_nl("Output written on ");
370
371           if (full_file_name_flag && dvi_file_name != NULL)
372             print_char_string((unsigned char *) dvi_file_name);
373           else
374             slow_print(output_file_name);
375
376           print_string(" (");
377           print_int(total_pages);
378           print_string(" page");
379
380           if (total_pages != 1)
381             print_char('s');
382
383           print_string(", ");
384           print_int(dvi_offset + dvi_ptr);
385           print_string(" bytes).");
386           b_close(dvi_file);
387         }
388       }
389       break;
390   }
391
392   if (log_opened)
393   {
394     putc('\n', log_file);
395     a_close(log_file);
396     selector = selector - 2;
397
398     if (selector == term_only)
399     {
400       print_nl("Transcript written on ");
401
402       if (full_file_name_flag && log_file_name != NULL)
403         print_char_string((unsigned char *) log_file_name);
404       else
405         slow_print(texmf_log_name);
406
407       print_char('.');
408     }
409   }
410
411   print_ln();
412
413   if ((edit_name_start != 0) && (interaction > 0))
414     call_edit(str_pool, edit_name_start, edit_name_length, edit_line);
415 }
416 #ifdef DEBUG
417 /* sec 1338 */
418 void debug_help (void) 
419 {
420   integer k, l, m, n;
421
422   while (true)
423   { 
424     print_nl(" debug # (-1 to exit):");
425
426 #ifndef _WINDOWS
427     fflush(stdout); 
428 #endif
429
430     read(stdin, m);  // ???
431
432     if (m < 0)
433       return;
434     else if (m == 0)
435       dumpcore();
436     else
437     {
438       read(stdin, n);
439
440       switch(m)
441       {
442         case 1:
443           print_word(mem[n]);
444           break;
445
446         case 2:
447           print_int(mem[n].hh.lh);
448           break;
449           
450         case 3:
451           print_int(mem[n].hh.rh);
452           break;
453         
454         case 4:
455           print_word(eqtb[n]);
456           break;
457
458         case 5:
459 #ifdef SHORTFONTINFO
460           print_scaled(font_info[n].sc);
461           print_char(' ');
462           print_int(font_info[n].b0);
463           print_char(':');
464           print_int(font_info[n].b1);
465           print_char(':');
466           print_int(font_info[n].b2);
467           print_char(':');
468           print_int(font_info[n].b3);
469 #else
470           print_word(font_info[n]); 
471 #endif
472           break;
473         
474         case 6:
475           print_word(save_stack[n]);
476           break;
477           
478         case 7:
479           show_box(n);
480           break;
481         
482         case 8:
483           {
484             breadth_max = 10000;
485 #ifdef ALLOCATESTRING
486             if (pool_ptr + 32000 > current_pool_size)
487               str_pool = realloc_str_pool (increment_pool_size);
488 #endif
489 #ifdef ALLOCATESTRING
490             depth_threshold = current_pool_size - pool_ptr - 10;
491 #else
492             depth_threshold = pool_size - pool_ptr - 10;
493 #endif
494             show_node_list(n);
495           }
496           break;
497         
498         case 9:
499           show_token_list(n, 0, 1000);
500           break;
501         
502         case 10:
503           slow_print(n);
504           break;
505         
506         case 11:
507           check_mem(n > 0);
508           break;
509         
510         case 12:
511           search_mem(n);
512           break;
513         
514         case 13:
515           {
516             read(stdin, l);
517             print_cmd_chr(n, l);
518           }
519           break;
520         
521         case 14:
522           {
523             for (k = 0; k <= n; k++)
524               print(buffer[k]);
525           }
526           break;
527         
528         case 15:
529           {
530             font_in_short_display = 0;
531             short_display(n);
532           }
533           break;
534         
535         case 16:
536           panicking = !panicking;
537           break;
538         
539         default:
540           print('?');
541           break;
542       }
543     }
544   }
545 }
546 #endif /* DEBUG */