Debian Codegraph @

texk/web2c/cwebdir/common.c

raw
1 /*1:*/
2 #line 62 "common.w"
3
4 /*4:*/
5 #line 47 "common.h"
6
7 #include <ctype.h>
8 #include <stdbool.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 /*:4*/
16 #line 63 "common.w"
17
18 #define ctangle false
19 #define cweave true
20 #define and_and 04
21 #define lt_lt 020
22 #define gt_gt 021
23 #define plus_plus 013
24 #define minus_minus 01
25 #define minus_gt 031
26 #define non_eq 032
27 #define lt_eq 034
28 #define gt_eq 035
29 #define eq_eq 036
30 #define or_or 037
31 #define dot_dot_dot 016
32 #define colon_colon 06
33 #define period_ast 026
34 #define minus_gt_ast 027
35 #define compress(c) if(loc++<=limit)return c
36 #define xisalpha(c) (isalpha((int)(c))&&!ishigh(c))
37 #define xisdigit(c) (isdigit((int)(c))&&!ishigh(c))
38 #define xisspace(c) (isspace((int)(c))&&!ishigh(c))
39 #define xislower(c) (islower((int)(c))&&!ishigh(c))
40 #define xisupper(c) (isupper((int)(c))&&!ishigh(c))
41 #define xisxdigit(c) (isxdigit((int)(c))&&!ishigh(c))
42 #define isxalpha(c) ((c)=='_'||(c)=='$')
43 #define ishigh(c) ((eight_bits)(c)> 0177)
44 #define max_include_depth 10
45 #define max_file_name_length 60
46 #define cur_file file[include_depth]
47 #define cur_file_name file_name[include_depth]
48 #define cur_line line[include_depth]
49 #define web_file file[0]
50 #define web_file_name file_name[0]
51 #define length(c) (size_t)((c+1)->byte_start-(c)->byte_start)
52 #define print_id(c) term_write((c)->byte_start,length(c))
53 #define llink link
54 #define rlink dummy.Rlink
55 #define root name_dir->rlink
56 #define ilk dummy.Ilk
57 #define spotless 0
58 #define harmless_message 1
59 #define error_message 2
60 #define fatal_message 3
61 #define mark_harmless() if(history==spotless)history= harmless_message
62 #define mark_error() history= error_message
63 #define confusion(s) fatal("! This can't happen: ",s)
64 #define show_banner flags['b']
65 #define show_progress flags['p']
66 #define show_happiness flags['h']
67 #define show_stats flags['s']
68 #define make_xrefs flags['x']
69 #define update_terminal() fflush(stdout)
70 #define new_line() putchar('\n')
71 #define term_write(a,b) fflush(stdout),fwrite(a,sizeof(char),b,stdout)
72 #define buf_size 200
73 #define longest_name 10000
74 #define long_buf_size (buf_size+longest_name)
75 #define max_bytes 100000
76 #define max_names 5000
77 #define max_sections 2000
78 #define lines_dont_match (change_limit-change_buffer!=limit-buffer|| \
79 strncmp(buffer,change_buffer,(size_t) (limit-buffer))!=0)
80 #define if_section_start_make_pending(b) \
81 *limit= '!'; \
82 for(loc= buffer;xisspace(*loc);loc++); \
83 *limit= ' '; \
84 if(*loc=='@'&&(xisspace(*(loc+1))||*(loc+1)=='*'))change_pending= b
85 #define too_long() {include_depth--; \
86 err_print("! Include file name too long");goto restart;}
87 #define hash_size 353
88 #define first_chunk(p) ((p)->byte_start+2)
89 #define prefix_length(p) (size_t)((eight_bits)*((p)->byte_start)*256+ \
90 (eight_bits)*((p)->byte_start+1))
91 #define set_prefix_length(p,m) (*((p)->byte_start)= (char)((m)/256), \
92 *((p)->byte_start+1)= (char)((m)%256))
93 #define less 0
94 #define equal 1
95 #define greater 2
96 #define prefix 3
97 #define extension 4
98 #define bad_extension 5
99 #define flag_change (**argv!='-')
100
101 #line 64 "common.w"
102
103 /*2:*/
104 #line 35 "common.h"
105
106 typedef uint8_t eight_bits;
107 typedef uint16_t sixteen_bits;
108 extern bool program;
109 extern int phase;
110
111 /*:2*//*5:*/
112 #line 92 "common.h"
113
114 extern char section_text[];
115 extern char*section_text_end;
116 extern char*id_first;
117 extern char*id_loc;
118
119 /*:5*//*6:*/
120 #line 110 "common.h"
121
122 extern char buffer[];
123 extern char*buffer_end;
124 extern char*loc;
125 extern char*limit;
126
127 /*:6*//*7:*/
128 #line 127 "common.h"
129
130 extern int include_depth;
131 extern FILE*file[];
132 extern FILE*change_file;
133 extern char file_name[][max_file_name_length];
134
135 extern char change_file_name[];
136 extern int line[];
137 extern int change_line;
138 extern int change_depth;
139 extern bool input_has_ended;
140 extern bool changing;
141 extern bool web_file_open;
142
143 /*:7*//*9:*/
144 #line 147 "common.h"
145
146 extern sixteen_bits section_count;
147 extern bool changed_section[];
148 extern bool change_pending;
149 extern bool print_where;
150
151 /*:9*//*10:*/
152 #line 162 "common.h"
153
154 typedef struct name_info{
155 char*byte_start;
156 struct name_info*link;
157 union{
158 struct name_info*Rlink;
159
160 eight_bits Ilk;
161 }dummy;
162 void*equiv_or_xref;
163 }name_info;
164 typedef name_info*name_pointer;
165 typedef name_pointer*hash_pointer;
166 extern char byte_mem[];
167 extern char*byte_mem_end;
168 extern char*byte_ptr;
169 extern name_info name_dir[];
170 extern name_pointer name_dir_end;
171 extern name_pointer name_ptr;
172 extern name_pointer hash[];
173 extern hash_pointer hash_end;
174 extern hash_pointer hash_ptr;
175
176 /*:10*//*12:*/
177 #line 207 "common.h"
178
179 extern int history;
180
181 /*:12*//*14:*/
182 #line 225 "common.h"
183
184 extern int argc;
185 extern char**argv;
186 extern char C_file_name[];
187 extern char tex_file_name[];
188 extern char idx_file_name[];
189 extern char scn_file_name[];
190 extern bool flags[];
191
192 /*:14*//*15:*/
193 #line 239 "common.h"
194
195 extern FILE*C_file;
196 extern FILE*tex_file;
197 extern FILE*idx_file;
198 extern FILE*scn_file;
199 extern FILE*active_file;
200
201 /*:15*/
202 #line 65 "common.w"
203
204 /*18:*/
205 #line 79 "common.w"
206
207 bool program;
208
209 /*:18*//*19:*/
210 #line 88 "common.w"
211
212 int phase;
213
214 /*:19*//*21:*/
215 #line 126 "common.w"
216
217 char section_text[longest_name+1];
218 char*section_text_end= section_text+longest_name;
219 char*id_first;
220 char*id_loc;
221
222 /*:21*//*22:*/
223 #line 146 "common.w"
224
225 char buffer[long_buf_size];
226 char*buffer_end= buffer+buf_size-2;
227 char*loc= buffer;
228 char*limit= buffer;
229
230 /*:22*//*25:*/
231 #line 190 "common.w"
232
233 int include_depth;
234 FILE*file[max_include_depth];
235 FILE*change_file;
236 char file_name[max_include_depth][max_file_name_length];
237
238 char change_file_name[max_file_name_length];
239 static char alt_web_file_name[max_file_name_length];
240 int line[max_include_depth];
241 int change_line;
242 int change_depth;
243 bool input_has_ended;
244 bool changing;
245 bool web_file_open= false;
246
247 /*:25*//*26:*/
248 #line 216 "common.w"
249
250 static char change_buffer[buf_size];
251 static char*change_limit;
252
253 /*:26*//*42:*/
254 #line 545 "common.w"
255
256 sixteen_bits section_count;
257 bool changed_section[max_sections];
258 bool change_pending;
259
260 bool print_where= false;
261
262 /*:42*//*43:*/
263 #line 590 "common.w"
264
265 char byte_mem[max_bytes];
266 char*byte_mem_end= byte_mem+max_bytes-1;
267 name_info name_dir[max_names];
268 name_pointer name_dir_end= name_dir+max_names-1;
269
270 /*:43*//*44:*/
271 #line 601 "common.w"
272
273 char*byte_ptr;
274 name_pointer name_ptr;
275
276 /*:44*//*46:*/
277 #line 621 "common.w"
278
279 name_pointer hash[hash_size]= {NULL};
280 hash_pointer hash_end= hash+hash_size-1;
281 hash_pointer hash_ptr;
282
283 /*:46*//*65:*/
284 #line 999 "common.w"
285
286 int history= spotless;
287
288 /*:65*//*73:*/
289 #line 1124 "common.w"
290
291 int argc;
292 char**argv;
293 char C_file_name[max_file_name_length];
294 char tex_file_name[max_file_name_length];
295 char idx_file_name[max_file_name_length];
296 char scn_file_name[max_file_name_length];
297 bool flags[128];
298
299 /*:73*//*83:*/
300 #line 1267 "common.w"
301
302 FILE*C_file;
303 FILE*tex_file;
304 FILE*idx_file;
305 FILE*scn_file;
306 FILE*active_file;
307
308 /*:83*/
309 #line 66 "common.w"
310
311 /*3:*/
312 #line 42 "common.h"
313
314 extern void common_init(void);
315
316 /*:3*//*8:*/
317 #line 141 "common.h"
318
319 extern bool get_line(void);
320 extern void check_complete(void);
321 extern void reset_input(void);
322
323 /*:8*//*11:*/
324 #line 185 "common.h"
325
326 extern name_pointer id_lookup(const char*,const char*,eight_bits);
327
328 extern name_pointer section_lookup(char*,char*,bool);
329 extern void print_prefix_name(name_pointer);
330 extern void print_section_name(name_pointer);
331 extern void sprint_section_name(char*,name_pointer);
332
333 extern bool names_match(name_pointer,const char*,size_t,eight_bits);
334
335 extern void init_node(name_pointer);
336
337 /*:11*//*13:*/
338 #line 210 "common.h"
339
340 extern int wrap_up(void);
341 extern void err_print(const char*);
342 extern void fatal(const char*,const char*);
343 extern void overflow(const char*);
344
345 extern void print_stats(void);
346
347 /*:13*//*24:*/
348 #line 176 "common.w"
349 static bool input_ln(FILE*);
350
351 /*:24*//*28:*/
352 #line 237 "common.w"
353 static void prime_the_change_buffer(void);
354
355 /*:28*//*33:*/
356 #line 334 "common.w"
357 static void check_change(void);
358
359 /*:33*//*55:*/
360 #line 760 "common.w"
361
362 static int web_strcmp(char*,size_t,char*,size_t);
363 static name_pointer add_section_name(name_pointer,int,char*,char*,bool);
364 static void extend_section_name(name_pointer,char*,char*,bool);
365
366 /*:55*//*64:*/
367 #line 987 "common.w"
368 static int section_name_cmp(char**,size_t,name_pointer);
369
370 /*:64*//*76:*/
371 #line 1184 "common.w"
372 static void scan_args(void);
373
374 /*:76*/
375 #line 67 "common.w"
376
377
378 /*:1*//*20:*/
379 #line 95 "common.w"
380
381 void
382 common_init(void)
383 {
384 /*45:*/
385 #line 605 "common.w"
386
387 name_dir->byte_start= byte_ptr= byte_mem;
388 name_ptr= name_dir+1;
389 name_ptr->byte_start= byte_mem;
390 root= NULL;
391
392 /*:45*/
393 #line 99 "common.w"
394
395 /*74:*/
396 #line 1137 "common.w"
397
398 show_banner= show_happiness= show_progress= make_xrefs= true;
399
400 /*:74*/
401 #line 100 "common.w"
402
403 /*84:*/
404 #line 1274 "common.w"
405
406 scan_args();
407 if(program==ctangle){
408 if((C_file= fopen(C_file_name,"wb"))==NULL)
409 fatal("! Cannot open output file ",C_file_name);
410
411 }
412 else{
413 if((tex_file= fopen(tex_file_name,"wb"))==NULL)
414 fatal("! Cannot open output file ",tex_file_name);
415 }
416
417 /*:84*/
418 #line 101 "common.w"
419
420 }
421
422 /*:20*//*23:*/
423 #line 156 "common.w"
424
425 static bool input_ln(
426 FILE*fp)
427 {
428 int c= EOF;
429 char*k;
430 if(feof(fp))return false;
431 limit= k= buffer;
432 while(k<=buffer_end&&(c= getc(fp))!=EOF&&c!='\n')
433 if((*(k++)= c)!=' ')limit= k;
434 if(k> buffer_end){
435 while((c= getc(fp))!=EOF&&c!='\n');
436 loc= buffer;err_print("! Input line too long");
437
438 }
439 if(c==EOF&&limit==buffer)return false;
440
441 return true;
442 }
443
444 /*:23*//*27:*/
445 #line 227 "common.w"
446
447 static void
448 prime_the_change_buffer(void)
449 {
450 change_limit= change_buffer;
451 /*29:*/
452 #line 243 "common.w"
453
454 while(true){
455 change_line++;
456 if(!input_ln(change_file))return;
457 if(limit<buffer+2)continue;
458 if(buffer[0]!='@')continue;
459 if(xisupper(buffer[1]))buffer[1]= tolower((int)buffer[1]);
460 if(buffer[1]=='x')break;
461 if(buffer[1]=='y'||buffer[1]=='z'||buffer[1]=='i'){
462 loc= buffer+2;
463 err_print("! Missing @x in change file");
464
465 }
466 }
467
468 /*:29*/
469 #line 232 "common.w"
470
471 /*30:*/
472 #line 260 "common.w"
473
474 do{
475 change_line++;
476 if(!input_ln(change_file)){
477 err_print("! Change file ended after @x");
478
479 return;
480 }
481 }while(limit==buffer);
482
483 /*:30*/
484 #line 233 "common.w"
485
486 /*31:*/
487 #line 270 "common.w"
488
489 change_limit= change_buffer+(ptrdiff_t)(limit-buffer);
490 strncpy(change_buffer,buffer,(size_t)(limit-buffer+1));
491
492 /*:31*/
493 #line 234 "common.w"
494
495 }
496
497 /*:27*//*32:*/
498 #line 296 "common.w"
499
500 static void
501 check_change(void)
502 {
503 int n= 0;
504 if(lines_dont_match)return;
505 change_pending= false;
506 if(!changed_section[section_count]){
507 if_section_start_make_pending(true);
508 if(!change_pending)changed_section[section_count]= true;
509 }
510 while(true){
511 changing= print_where= true;change_line++;
512 if(!input_ln(change_file)){
513 err_print("! Change file ended before @y");
514
515 change_limit= change_buffer;changing= false;
516 return;
517 }
518 if(limit> buffer+1&&buffer[0]=='@'){
519 if(xisupper(buffer[1]))buffer[1]= tolower((int)buffer[1]);
520 /*34:*/
521 #line 336 "common.w"
522
523 if(buffer[1]=='x'||buffer[1]=='z'){
524 loc= buffer+2;err_print("! Where is the matching @y?");
525
526 }
527 else if(buffer[1]=='y'){
528 if(n> 0){
529 loc= buffer+2;
530 printf("\n! Hmm... %d ",n);
531 err_print("of the preceding lines failed to match");
532
533 }
534 change_depth= include_depth;
535 return;
536 }
537
538 /*:34*/
539 #line 318 "common.w"
540
541 }
542 /*31:*/
543 #line 270 "common.w"
544
545 change_limit= change_buffer+(ptrdiff_t)(limit-buffer);
546 strncpy(change_buffer,buffer,(size_t)(limit-buffer+1));
547
548 /*:31*/
549 #line 320 "common.w"
550
551 changing= false;cur_line++;
552 while(!input_ln(cur_file)){
553 if(include_depth==0){
554 err_print("! CWEB file ended during a change");
555
556 input_has_ended= true;return;
557 }
558 include_depth--;cur_line++;
559 }
560 if(lines_dont_match)n++;
561 }
562 }
563
564 /*:32*//*35:*/
565 #line 362 "common.w"
566
567 bool get_line(void)
568 {
569 restart:
570 if(changing&&include_depth==change_depth)
571 /*38:*/
572 #line 470 "common.w"
573 {
574 change_line++;
575 if(!input_ln(change_file)){
576 err_print("! Change file ended without @z");
577
578 buffer[0]= '@';buffer[1]= 'z';limit= buffer+2;
579 }
580 if(limit> buffer){
581 if(change_pending){
582 if_section_start_make_pending(false);
583 if(change_pending){
584 changed_section[section_count]= true;change_pending= false;
585 }
586 }
587 *limit= ' ';
588 if(buffer[0]=='@'){
589 if(xisupper(buffer[1]))buffer[1]= tolower((int)buffer[1]);
590 if(buffer[1]=='x'||buffer[1]=='y'){
591 loc= buffer+2;
592 err_print("! Where is the matching @z?");
593
594 }
595 else if(buffer[1]=='z'){
596 prime_the_change_buffer();changing= !changing;print_where= true;
597 }
598 }
599 }
600 }
601
602 /*:38*/
603 #line 367 "common.w"
604
605 if(!changing||include_depth> change_depth){
606 /*37:*/
607 #line 453 "common.w"
608 {
609 cur_line++;
610 while(!input_ln(cur_file)){
611 print_where= true;
612 if(include_depth==0){input_has_ended= true;break;}
613 else{
614 fclose(cur_file);include_depth--;
615 if(changing&&include_depth==change_depth)break;
616 cur_line++;
617 }
618 }
619 if(!changing&&!input_has_ended)
620 if(limit-buffer==change_limit-change_buffer)
621 if(buffer[0]==change_buffer[0])
622 if(change_limit> change_buffer)check_change();
623 }
624
625 /*:37*/
626 #line 369 "common.w"
627
628 if(changing&&include_depth==change_depth)goto restart;
629 }
630 if(input_has_ended)return false;
631 loc= buffer;*limit= ' ';
632 if(buffer[0]=='@'&&(buffer[1]=='i'||buffer[1]=='I')){
633 loc= buffer+2;*limit= '"';
634 while(*loc==' '||*loc=='\t')loc++;
635 if(loc>=limit){
636 err_print("! Include file name not given");
637
638 goto restart;
639 }
640 if(include_depth>=max_include_depth-1){
641 err_print("! Too many nested includes");
642
643 goto restart;
644 }
645 include_depth++;
646 /*36:*/
647 #line 407 "common.w"
648 {
649 char temp_file_name[max_file_name_length];
650 char*cur_file_name_end= cur_file_name+max_file_name_length-1;
651 char*kk,*k= cur_file_name;
652 size_t l;
653
654 if(*loc=='"'){
655 loc++;
656 while(*loc!='"'&&k<=cur_file_name_end)*k++= *loc++;
657 if(loc==limit)k= cur_file_name_end+1;
658 }else
659 while(*loc!=' '&&*loc!='\t'&&*loc!='"'&&k<=cur_file_name_end)*k++= *loc++;
660 if(k> cur_file_name_end)too_long();
661
662 *k= '\0';
663 if((cur_file= fopen(cur_file_name,"r"))!=NULL){
664 cur_line= 0;print_where= true;
665 goto restart;
666 }
667 if((kk= getenv("CWEBINPUTS"))!=NULL){
668 if((l= strlen(kk))> max_file_name_length-2)too_long();
669 strcpy(temp_file_name,kk);
670 }
671 else{
672 #ifdef CWEBINPUTS
673 if((l= strlen(CWEBINPUTS))> max_file_name_length-2)too_long();
674 strcpy(temp_file_name,CWEBINPUTS);
675 #else
676 #line 435 "common.w"
677 l= 0;
678 #endif
679 #line 437 "common.w"
680 }
681 if(l> 0){
682 if(k+l+2>=cur_file_name_end)too_long();
683
684 for(;k>=cur_file_name;k--)*(k+l+1)= *k;
685 strcpy(cur_file_name,temp_file_name);
686 cur_file_name[l]= '/';
687
688 if((cur_file= fopen(cur_file_name,"r"))!=NULL){
689 cur_line= 0;print_where= true;
690 goto restart;
691 }
692 }
693 include_depth--;err_print("! Cannot open include file");goto restart;
694 }
695
696 /*:36*/
697 #line 388 "common.w"
698
699 }
700 return true;
701 }
702
703 /*:35*//*39:*/
704 #line 502 "common.w"
705
706 void
707 check_complete(void){
708 if(change_limit!=change_buffer){
709 strncpy(buffer,change_buffer,(size_t)(change_limit-change_buffer+1));
710 limit= buffer+(ptrdiff_t)(change_limit-change_buffer);
711 changing= true;change_depth= include_depth;loc= buffer;
712 err_print("! Change file entry did not match");
713
714 }
715 }
716
717 /*:39*//*40:*/
718 #line 518 "common.w"
719
720 void
721 reset_input(void)
722 {
723 limit= buffer;loc= buffer+1;buffer[0]= ' ';
724 /*41:*/
725 #line 533 "common.w"
726
727 if((web_file= fopen(web_file_name,"r"))==NULL){
728 strcpy(web_file_name,alt_web_file_name);
729 if((web_file= fopen(web_file_name,"r"))==NULL)
730 fatal("! Cannot open input file ",web_file_name);
731 }
732
733
734 web_file_open= true;
735 if((change_file= fopen(change_file_name,"r"))==NULL)
736 fatal("! Cannot open change file ",change_file_name);
737
738 /*:41*/
739 #line 523 "common.w"
740
741 include_depth= cur_line= change_line= 0;
742 change_depth= include_depth;
743 changing= true;prime_the_change_buffer();changing= !changing;
744 limit= buffer;loc= buffer+1;buffer[0]= ' ';input_has_ended= false;
745 }
746
747 /*:40*//*48:*/
748 #line 630 "common.w"
749
750 name_pointer
751 id_lookup(
752 const char*first,
753 const char*last,
754 eight_bits t)
755 {
756 const char*i= first;
757 int h;
758 size_t l;
759 name_pointer p;
760 if(last==NULL)for(last= first;*last!='\0';last++);
761 l= (size_t)(last-first);
762 /*49:*/
763 #line 653 "common.w"
764
765 h= (int)((eight_bits)*i);
766 while(++i<last)h= (h+h+(int)((eight_bits)*i))%hash_size;
767
768
769 /*:49*/
770 #line 643 "common.w"
771
772 /*50:*/
773 #line 661 "common.w"
774
775 p= hash[h];
776 while(p&&!names_match(p,first,l,t))p= p->link;
777 if(p==NULL){
778 p= name_ptr;
779 p->link= hash[h];hash[h]= p;
780 }
781
782 /*:50*/
783 #line 644 "common.w"
784
785 if(p==name_ptr)/*51:*/
786 #line 672 "common.w"
787 {
788 if(byte_ptr+l> byte_mem_end)overflow("byte memory");
789 if(name_ptr>=name_dir_end)overflow("name");
790 strncpy(byte_ptr,first,l);
791 (++name_ptr)->byte_start= byte_ptr+= l;
792 if(program==cweave)p->ilk= t,init_node(p);
793 }
794
795 /*:51*/
796 #line 645 "common.w"
797
798 return p;
799 }
800
801 /*:48*//*52:*/
802 #line 704 "common.w"
803
804 void
805 print_section_name(
806 name_pointer p)
807 {
808 char*ss,*s= first_chunk(p);
809 name_pointer q= p+1;
810 while(p!=name_dir){
811 ss= (p+1)->byte_start-1;
812 if(*ss==' '&&ss>=s)p= q->link,q= p;
813 else ss++,p= name_dir,q= NULL;
814 term_write(s,(size_t)(ss-s));
815 s= p->byte_start;
816 }
817 if(q)term_write("...",3);
818 }
819
820 /*:52*//*53:*/
821 #line 721 "common.w"
822
823 void
824 sprint_section_name(
825 char*dest,
826 name_pointer p)
827 {
828 char*ss,*s= first_chunk(p);
829 name_pointer q= p+1;
830 while(p!=name_dir){
831 ss= (p+1)->byte_start-1;
832 if(*ss==' '&&ss>=s)p= q->link,q= p;
833 else ss++,p= name_dir;
834 strncpy(dest,s,(size_t)(ss-s)),dest+= ss-s;
835 s= p->byte_start;
836 }
837 *dest= '\0';
838 }
839
840 /*:53*//*54:*/
841 #line 739 "common.w"
842
843 void
844 print_prefix_name(
845 name_pointer p)
846 {
847 char*s= first_chunk(p);
848 size_t l= prefix_length(p);
849 term_write(s,l);
850 if(s+l<(p+1)->byte_start)term_write("...",3);
851 }
852
853 /*:54*//*56:*/
854 #line 765 "common.w"
855
856 static int web_strcmp(
857 char*j,
858 size_t j_len,
859 char*k,
860 size_t k_len)
861 {
862 char*j1= j+j_len,*k1= k+k_len;
863 while(k<k1&&j<j1&&*j==*k)k++,j++;
864 if(k==k1)if(j==j1)return equal;
865 else return extension;
866 else if(j==j1)return prefix;
867 else if(*j<*k)return less;
868 else return greater;
869 }
870
871 /*:56*//*57:*/
872 #line 794 "common.w"
873
874 static name_pointer
875 add_section_name(
876 name_pointer par,
877 int c,
878 char*first,
879 char*last,
880 bool ispref)
881 {
882 name_pointer p= name_ptr;
883 char*s= first_chunk(p);
884 size_t name_len= (size_t)(last-first+(int)ispref);
885 if(s+name_len> byte_mem_end)overflow("byte memory");
886 if(name_ptr+1>=name_dir_end)overflow("name");
887 (++name_ptr)->byte_start= byte_ptr= s+name_len;
888 if(ispref){
889 *(byte_ptr-1)= ' ';
890 name_len--;
891 name_ptr->link= name_dir;
892 (++name_ptr)->byte_start= byte_ptr;
893 }
894 set_prefix_length(p,name_len);
895 strncpy(s,first,name_len);
896 p->llink= p->rlink= NULL;
897 init_node(p);
898 return par==NULL?(root= p):c==less?(par->llink= p):(par->rlink= p);
899 }
900
901 /*:57*//*58:*/
902 #line 822 "common.w"
903
904 static void
905 extend_section_name(
906 name_pointer p,
907 char*first,
908 char*last,
909 bool ispref)
910 {
911 char*s;
912 name_pointer q= p+1;
913 size_t name_len= (size_t)(last-first+(int)ispref);
914 if(name_ptr>=name_dir_end)overflow("name");
915 while(q->link!=name_dir)q= q->link;
916 q->link= name_ptr;
917 s= name_ptr->byte_start;
918 name_ptr->link= name_dir;
919 if(s+name_len> byte_mem_end)overflow("byte memory");
920 (++name_ptr)->byte_start= byte_ptr= s+name_len;
921 strncpy(s,first,name_len);
922 if(ispref)*(byte_ptr-1)= ' ';
923 }
924
925 /*:58*//*59:*/
926 #line 850 "common.w"
927
928 name_pointer
929 section_lookup(
930 char*first,char*last,
931 bool ispref)
932 {
933 int c= less;
934 name_pointer p= root;
935 name_pointer q= NULL;
936 name_pointer r= NULL;
937 name_pointer par= NULL;
938
939 size_t name_len= (size_t)(last-first+1);
940 /*60:*/
941 #line 874 "common.w"
942
943 while(p){
944 c= web_strcmp(first,name_len,first_chunk(p),prefix_length(p));
945 if(c==less||c==greater){
946 if(r==NULL)
947 par= p;
948 p= (c==less?p->llink:p->rlink);
949 }else{
950 if(r!=NULL){
951 printf("%s","\n! Ambiguous prefix: matches <");
952
953 print_prefix_name(p);
954 printf("%s",">\n and <");
955 print_prefix_name(r);
956 err_print(">");
957 return name_dir;
958 }
959 r= p;
960 p= p->llink;
961 q= r->rlink;
962 }
963 if(p==NULL)
964 p= q,q= NULL;
965 }
966
967 /*:60*/
968 #line 864 "common.w"
969
970 /*61:*/
971 #line 899 "common.w"
972
973 if(r==NULL)
974 return add_section_name(par,c,first,last+1,ispref);
975
976 /*:61*/
977 #line 865 "common.w"
978
979 /*62:*/
980 #line 907 "common.w"
981
982 switch(section_name_cmp(&first,name_len,r)){
983
984 case prefix:
985 if(!ispref){
986 printf("%s","\n! New name is a prefix of <");
987
988 print_section_name(r);
989 err_print(">");
990 }
991 else if(name_len<prefix_length(r))set_prefix_length(r,name_len);
992 /* fall through */
993 case equal:break;
994 case extension:if(!ispref||first<=last)
995 extend_section_name(r,first,last+1,ispref);
996 break;
997 case bad_extension:
998 printf("%s","\n! New name extends <");
999
1000 print_section_name(r);
1001 err_print(">");
1002 break;
1003 default:
1004 printf("%s","\n! Section name incompatible with <");
1005
1006 print_prefix_name(r);
1007 printf("%s",">,\n which abbreviates <");
1008 print_section_name(r);
1009 err_print(">");
1010 }
1011 return r;
1012
1013 /*:62*/
1014 #line 866 "common.w"
1015
1016 }
1017
1018 /*:59*//*63:*/
1019 #line 955 "common.w"
1020
1021 static int section_name_cmp(
1022 char**pfirst,
1023 size_t len,
1024 name_pointer r)
1025 {
1026 char*first= *pfirst;
1027 name_pointer q= r+1;
1028 char*ss,*s= first_chunk(r);
1029 int c= less;
1030 bool ispref;
1031 while(true){
1032 ss= (r+1)->byte_start-1;
1033 if(*ss==' '&&ss>=r->byte_start)ispref= true,q= q->link;
1034 else ispref= false,ss++,q= name_dir;
1035 switch(c= web_strcmp(first,len,s,(size_t)(ss-s))){
1036 case equal:if(q==name_dir)
1037 if(ispref){
1038 *pfirst= first+(ptrdiff_t)(ss-s);
1039 return extension;
1040 }else return equal;
1041 else return length(q)==0?equal:prefix;
1042 case extension:
1043 if(!ispref)return bad_extension;
1044 first+= ss-s;
1045 if(q!=name_dir){len-= (int)(ss-s);s= q->byte_start;r= q;continue;}
1046 *pfirst= first;return extension;
1047 default:return c;
1048 }
1049 }
1050 }
1051
1052 /*:63*//*66:*/
1053 #line 1009 "common.w"
1054
1055 void
1056 err_print(
1057 const char*s)
1058 {
1059 printf(*s=='!'?"\n%s":"%s",s);
1060 if(web_file_open)/*67:*/
1061 #line 1028 "common.w"
1062
1063 {char*k,*l;
1064 if(changing&&include_depth==change_depth&&change_line> 0)
1065 printf(". (l. %d of change file)\n",change_line);
1066 else if(cur_line> 0){
1067 if(include_depth==0)printf(". (l. %d)\n",cur_line);
1068 else printf(". (l. %d of include file %s)\n",cur_line,cur_file_name);
1069 }
1070 l= (loc>=limit?limit:loc);
1071 if(l> buffer){
1072 for(k= buffer;k<l;k++)
1073 if(*k=='\t')putchar(' ');
1074 else putchar(*k);
1075 new_line();
1076 for(k= buffer;k<l;k++)putchar(' ');
1077 }
1078 for(k= l;k<limit;k++)putchar(*k);
1079 if(*limit=='|')putchar('|');
1080 putchar(' ');
1081 }
1082
1083 /*:67*/
1084 #line 1015 "common.w"
1085
1086 update_terminal();mark_error();
1087 }
1088
1089 /*:66*//*68:*/
1090 #line 1063 "common.w"
1091
1092 int wrap_up(void){
1093 if(show_progress||show_happiness||history!=spotless)new_line();
1094 if(show_stats)
1095 print_stats();
1096 /*69:*/
1097 #line 1073 "common.w"
1098
1099 switch(history){
1100 case spotless:
1101 if(show_happiness)puts("(No errors were found.)");break;
1102 case harmless_message:
1103 puts("(Did you see the warning message above?)");break;
1104 case error_message:
1105 puts("(Pardon me, but I think I spotted something wrong.)");break;
1106 case fatal_message:default:
1107 puts("(That was a fatal error, my friend.)");
1108 }
1109
1110 /*:69*/
1111 #line 1068 "common.w"
1112
1113 if(history> harmless_message)return EXIT_FAILURE;
1114 else return EXIT_SUCCESS;
1115 }
1116
1117 /*:68*//*70:*/
1118 #line 1091 "common.w"
1119 void
1120 fatal(
1121 const char*s,const char*t)
1122 {
1123 if(*s)printf("%s",s);
1124 err_print(t);
1125 history= fatal_message;exit(wrap_up());
1126 }
1127
1128 /*:70*//*71:*/
1129 #line 1102 "common.w"
1130 void
1131 overflow(
1132 const char*t)
1133 {
1134 printf("\n! Sorry, %s capacity exceeded",t);fatal("","");
1135 }
1136
1137
1138 /*:71*//*75:*/
1139 #line 1155 "common.w"
1140
1141 static void
1142 scan_args(void)
1143 {
1144 char*dot_pos;
1145 char*name_pos;
1146 char*s;
1147 bool found_web= false,found_change= false,found_out= false;
1148
1149
1150 strcpy(change_file_name,"/dev/null");
1151 while(--argc> 0){
1152 if((**(++argv)=='-'||**argv=='+')&&*(*argv+1))/*80:*/
1153 #line 1245 "common.w"
1154
1155 for(dot_pos= *argv+1;*dot_pos> '\0';dot_pos++)
1156 flags[(eight_bits)*dot_pos]= flag_change;
1157
1158 /*:80*/
1159 #line 1167 "common.w"
1160
1161 else{
1162 s= name_pos= *argv;dot_pos= NULL;
1163 while(*s)
1164 if(*s=='.')dot_pos= s++;
1165 else if(*s=='/')dot_pos= NULL,name_pos= ++s;
1166 else s++;
1167 if(!found_web)/*77:*/
1168 #line 1193 "common.w"
1169
1170 {
1171 if(s-*argv> max_file_name_length-5)
1172 /*82:*/
1173 #line 1261 "common.w"
1174 fatal("! Filename too long\n",*argv);
1175
1176
1177 /*:82*/
1178 #line 1196 "common.w"
1179
1180 if(dot_pos==NULL)
1181 sprintf(web_file_name,"%s.w",*argv);
1182 else{
1183 strcpy(web_file_name,*argv);
1184 *dot_pos= '\0';
1185 }
1186 sprintf(alt_web_file_name,"%s.web",*argv);
1187 sprintf(tex_file_name,"%s.tex",name_pos);
1188 if(make_xrefs){
1189 sprintf(idx_file_name,"%s.idx",name_pos);
1190 sprintf(scn_file_name,"%s.scn",name_pos);
1191 }
1192 sprintf(C_file_name,"%s.c",name_pos);
1193 found_web= true;
1194 }
1195
1196 /*:77*/
1197 #line 1175 "common.w"
1198
1199 else if(!found_change)/*78:*/
1200 #line 1213 "common.w"
1201
1202 {
1203 if(strcmp(*argv,"-")!=0){
1204 if(s-*argv> max_file_name_length-4)
1205 /*82:*/
1206 #line 1261 "common.w"
1207 fatal("! Filename too long\n",*argv);
1208
1209
1210 /*:82*/
1211 #line 1217 "common.w"
1212
1213 if(dot_pos==NULL)
1214 sprintf(change_file_name,"%s.ch",*argv);
1215 else strcpy(change_file_name,*argv);
1216 }
1217 found_change= true;
1218 }
1219
1220 /*:78*/
1221 #line 1176 "common.w"
1222
1223 else if(!found_out)/*79:*/
1224 #line 1225 "common.w"
1225
1226 {
1227 if(s-*argv> max_file_name_length-5)
1228 /*82:*/
1229 #line 1261 "common.w"
1230 fatal("! Filename too long\n",*argv);
1231
1232
1233 /*:82*/
1234 #line 1228 "common.w"
1235
1236 if(dot_pos==NULL){
1237 sprintf(tex_file_name,"%s.tex",*argv);
1238 sprintf(C_file_name,"%s.c",*argv);
1239 }else{
1240 strcpy(tex_file_name,*argv);
1241 strcpy(C_file_name,*argv);
1242 *dot_pos= '\0';
1243 }
1244 if(make_xrefs){
1245 sprintf(idx_file_name,"%s.idx",*argv);
1246 sprintf(scn_file_name,"%s.scn",*argv);
1247 }
1248 found_out= true;
1249 }
1250
1251 /*:79*/
1252 #line 1177 "common.w"
1253
1254 else/*81:*/
1255 #line 1249 "common.w"
1256
1257 {
1258 if(program==ctangle)
1259 fatal(
1260 "! Usage: ctangle [options] webfile[.w] [{changefile[.ch]|-} [outfile[.c]]]\n"
1261 ,"");
1262
1263 else fatal(
1264 "! Usage: cweave [options] webfile[.w] [{changefile[.ch]|-} [outfile[.tex]]]\n"
1265 ,"");
1266 }
1267
1268 /*:81*/
1269 #line 1178 "common.w"
1270
1271 }
1272 }
1273 if(!found_web)/*81:*/
1274 #line 1249 "common.w"
1275
1276 {
1277 if(program==ctangle)
1278 fatal(
1279 "! Usage: ctangle [options] webfile[.w] [{changefile[.ch]|-} [outfile[.c]]]\n"
1280 ,"");
1281
1282 else fatal(
1283 "! Usage: cweave [options] webfile[.w] [{changefile[.ch]|-} [outfile[.tex]]]\n"
1284 ,"");
1285 }
1286
1287 /*:81*/
1288 #line 1181 "common.w"
1289
1290 }
1291
1292 /*:75*/