home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / cweb / cwebsrc / CWEB / src / c / common next >
Encoding:
Text File  |  1996-10-30  |  21.7 KB  |  1,241 lines

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