OSDN Git Service

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