home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / dev / c / cweb / wmerge.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-14  |  17.5 KB  |  956 lines

  1. #define buf_size 1024 \
  2.  
  3. #define max_include_depth 10 \
  4.  
  5. #define max_file_name_length 256
  6. #define cur_file file[include_depth]
  7. #define cur_file_name file_name[include_depth]
  8. #define cur_line line[include_depth]
  9. #define web_file file[0]
  10. #define web_file_name file_name[0] \
  11.  
  12. #define lines_dont_match (change_limit-change_buffer!=limit-buffer|| \
  13. strncmp(buffer,change_buffer,(size_t) (limit-buffer) ) )  \
  14.  
  15. #define too_long() {include_depth--; \
  16. err_print("! Include file name too long") ;goto restart;} \
  17.  
  18. #define spotless 0
  19. #define harmless_message 1
  20. #define error_message 2
  21. #define fatal_message 3
  22. #define mark_harmless {if(history==spotless) history= harmless_message;}
  23. #define mark_error history= error_message \
  24.  
  25. #define fatal(s,t) { \
  26. fprintf(stderr,s) ;err_print(t) ; \
  27. history= fatal_message;exit(wrap_up() ) ; \
  28. } \
  29.  
  30. #define RETURN_OK 0
  31. #define RETURN_WARN 5
  32. #define RETURN_ERROR 10
  33. #define RETURN_FAIL 20 \
  34.  
  35. #define show_banner flags['b']
  36. #define show_happiness flags['h'] \
  37.  
  38. #define update_terminal fflush(stderr)  \
  39.  
  40. #define max_path_length 4094 \
  41.  
  42. #define alloc_object(object,size,type)  \
  43. if(!(object= (type*) malloc((size) *sizeof(type) ) ) )  \
  44. fatal("","! Memory allocation failure") ;
  45. #define free_object(object)  \
  46. if(object) { \
  47. free(object) ; \
  48. object= NULL; \
  49. } \
  50.  
  51. /*1:*/
  52. #line 13 "wmerge.w"
  53.  
  54. #line 84 "wmerge.ch"
  55. #include <stdio.h>
  56. #include <string.h>
  57. #include <signal.h>
  58.  
  59. #ifdef SEPARATORS
  60. char separators[]= SEPARATORS;
  61. #else
  62. char separators[]= "://";
  63. #endif
  64.  
  65. #define PATH_SEPARATOR   separators[0]
  66. #define DIR_SEPARATOR    separators[1]
  67. #define DEVICE_SEPARATOR separators[2]
  68. #line 15 "wmerge.w"
  69. #include <stdlib.h> 
  70. #include <ctype.h> 
  71. /*2:*/
  72. #line 35 "wmerge.w"
  73.  
  74. typedef short boolean;
  75. typedef unsigned char eight_bits;
  76. typedef char ASCII;
  77.  
  78. /*:2*//*5:*/
  79. #line 68 "wmerge.w"
  80.  
  81. #line 138 "wmerge.ch"
  82. ASCII*buffer;
  83. ASCII*buffer_end;
  84. #line 71 "wmerge.w"
  85. ASCII*limit;
  86. ASCII*loc;
  87.  
  88. /*:5*//*7:*/
  89. #line 134 "wmerge.w"
  90.  
  91. int include_depth;
  92. #line 166 "wmerge.ch"
  93. FILE**file;
  94. FILE*change_file;
  95. char**file_name;
  96. char*change_file_name;
  97. char*alt_web_file_name;
  98. int*line;
  99. #line 143 "wmerge.w"
  100. int change_line;
  101. int change_depth;
  102. boolean input_has_ended;
  103. boolean changing;
  104. boolean web_file_open= 0;
  105.  
  106. /*:7*//*8:*/
  107. #line 160 "wmerge.w"
  108.  
  109. #line 186 "wmerge.ch"
  110. char*change_buffer;
  111. #line 162 "wmerge.w"
  112. char*change_limit;
  113.  
  114. /*:8*//*23:*/
  115. #line 478 "wmerge.w"
  116.  
  117. int history= spotless;
  118.  
  119. /*:23*//*30:*/
  120. #line 575 "wmerge.w"
  121.  
  122. int argc;
  123. char**argv;
  124. #line 444 "wmerge.ch"
  125. char*out_file_name;
  126. char*check_file_name;
  127. boolean*flags;
  128. #line 580 "wmerge.w"
  129.  
  130. /*:30*//*40:*/
  131. #line 692 "wmerge.w"
  132.  
  133. #line 511 "wmerge.ch"
  134. FILE*out_file;
  135. FILE*check_file;
  136. #line 694 "wmerge.w"
  137.  
  138. /*:40*//*44:*/
  139. #line 571 "wmerge.ch"
  140.  
  141. char*include_path;
  142. char*p,*path_prefix,*next_path_prefix;
  143.  
  144. /*:44*//*46:*/
  145. #line 596 "wmerge.ch"
  146.  
  147. #ifdef __SASC
  148. const char Version[]= "$VER: WMerge 3.3 [p11] ("__DATE__", "__TIME__")\n";
  149. #endif
  150.  
  151. /*:46*//*51:*/
  152. #line 690 "wmerge.ch"
  153.  
  154. int i;
  155.  
  156. /*:51*/
  157. #line 17 "wmerge.w"
  158.  
  159. /*4:*/
  160. #line 51 "wmerge.w"
  161.  
  162.  
  163. /*:4*//*24:*/
  164. #line 361 "wmerge.ch"
  165.  
  166. void err_print(char*);
  167.  
  168. /*:24*//*32:*/
  169. #line 468 "wmerge.ch"
  170.  
  171. void scan_args(void);
  172.  
  173. /*:32*//*45:*/
  174. #line 578 "wmerge.ch"
  175.  
  176. int get_line(void);
  177. int input_ln(FILE*);
  178. int main(int,char**);
  179. int wrap_up(void);
  180. void check_change(void);
  181. void check_complete(void);
  182. void err_print(char*);
  183. void prime_the_change_buffer(void);
  184. void put_line(void);
  185. void reset_input(void);
  186. void scan_args(void);
  187. static boolean set_path(char*,char*);
  188.  
  189. /*:45*//*54:*/
  190. #line 716 "wmerge.ch"
  191.  
  192. void catch_break(int);
  193.  
  194. /*:54*/
  195. #line 18 "wmerge.w"
  196.  
  197. /*6:*/
  198. #line 93 "wmerge.w"
  199.  
  200. #line 146 "wmerge.ch"
  201. int input_ln(
  202. FILE*fp)
  203. #line 96 "wmerge.w"
  204. {
  205. register int c= EOF;
  206. register char*k;
  207. if(feof(fp))return(0);
  208. limit= k= buffer;
  209. while(k<=buffer_end&&(c= getc(fp))!=EOF&&c!='\n')
  210. if((*(k++)= c)!=' ')limit= k;
  211. if(k>buffer_end)
  212. if((c= getc(fp))!=EOF&&c!='\n'){
  213. ungetc(c,fp);loc= buffer;err_print("! Input line too long");
  214.  
  215. }
  216. if(c==EOF&&limit==buffer)return(0);
  217.  
  218. return(1);
  219. }
  220.  
  221. /*:6*//*9:*/
  222. #line 171 "wmerge.w"
  223.  
  224. #line 195 "wmerge.ch"
  225. void prime_the_change_buffer(void)
  226. #line 174 "wmerge.w"
  227. {
  228. change_limit= change_buffer;
  229. /*10:*/
  230. #line 185 "wmerge.w"
  231.  
  232. while(1){
  233. change_line++;
  234. if(!input_ln(change_file))return;
  235. if(limit<buffer+2)continue;
  236. if(buffer[0]!='@')continue;
  237. if(isupper(buffer[1]))buffer[1]= tolower(buffer[1]);
  238. if(buffer[1]=='x')break;
  239. if(buffer[1]=='y'||buffer[1]=='z'||buffer[1]=='i'){
  240. loc= buffer+2;
  241. err_print("! Missing @x in change file");
  242.  
  243. }
  244. }
  245.  
  246. /*:10*/
  247. #line 176 "wmerge.w"
  248. ;
  249. /*11:*/
  250. #line 202 "wmerge.w"
  251.  
  252. do{
  253. change_line++;
  254. if(!input_ln(change_file)){
  255. err_print("! Change file ended after @x");
  256.  
  257. return;
  258. }
  259. }while(limit==buffer);
  260.  
  261. /*:11*/
  262. #line 177 "wmerge.w"
  263. ;
  264. /*12:*/
  265. #line 212 "wmerge.w"
  266.  
  267. {
  268. change_limit= change_buffer-buffer+limit;
  269. #line 202 "wmerge.ch"
  270. strncpy(change_buffer,buffer,(size_t)(limit-buffer+1));
  271. #line 216 "wmerge.w"
  272. }
  273.  
  274. /*:12*/
  275. #line 178 "wmerge.w"
  276. ;
  277. }
  278.  
  279. /*:9*//*13:*/
  280. #line 230 "wmerge.w"
  281.  
  282. #line 210 "wmerge.ch"
  283. void check_change(void)
  284. #line 233 "wmerge.w"
  285. {
  286. int n= 0;
  287. if(lines_dont_match)return;
  288. while(1){
  289. changing= 1;change_line++;
  290. if(!input_ln(change_file)){
  291. err_print("! Change file ended before @y");
  292.  
  293. change_limit= change_buffer;changing= 0;
  294. return;
  295. }
  296. if(limit>buffer+1&&buffer[0]=='@'){
  297. if(isupper(buffer[1]))buffer[1]= tolower(buffer[1]);
  298. /*14:*/
  299. #line 263 "wmerge.w"
  300.  
  301. if(buffer[1]=='x'||buffer[1]=='z'){
  302. loc= buffer+2;err_print("! Where is the matching @y?");
  303.  
  304. }
  305. else if(buffer[1]=='y'){
  306. if(n>0){
  307. loc= buffer+2;
  308. fprintf(stderr,"\n! Hmm... %d ",n);
  309. err_print("of the preceding lines failed to match");
  310.  
  311. }
  312. change_depth= include_depth;
  313. return;
  314. }
  315.  
  316. /*:14*/
  317. #line 247 "wmerge.w"
  318. ;
  319. }
  320. /*12:*/
  321. #line 212 "wmerge.w"
  322.  
  323. {
  324. change_limit= change_buffer-buffer+limit;
  325. #line 202 "wmerge.ch"
  326. strncpy(change_buffer,buffer,(size_t)(limit-buffer+1));
  327. #line 216 "wmerge.w"
  328. }
  329.  
  330. /*:12*/
  331. #line 249 "wmerge.w"
  332. ;
  333. changing= 0;cur_line++;
  334. while(!input_ln(cur_file)){
  335. if(include_depth==0){
  336. err_print("! CWEB file ended during a change");
  337.  
  338. input_has_ended= 1;return;
  339. }
  340. include_depth--;cur_line++;
  341. }
  342. if(lines_dont_match)n++;
  343. }
  344. }
  345.  
  346. /*:13*//*15:*/
  347. #line 282 "wmerge.w"
  348.  
  349. #line 218 "wmerge.ch"
  350. void reset_input(void)
  351. #line 285 "wmerge.w"
  352. {
  353. limit= buffer;loc= buffer+1;buffer[0]= ' ';
  354. /*16:*/
  355. #line 297 "wmerge.w"
  356.  
  357. if((web_file= fopen(web_file_name,"r"))==NULL){
  358. strcpy(web_file_name,alt_web_file_name);
  359. if((web_file= fopen(web_file_name,"r"))==NULL)
  360. fatal("! Cannot open input file ",web_file_name);
  361. }
  362.  
  363.  
  364. web_file_open= 1;
  365. if((change_file= fopen(change_file_name,"r"))==NULL)
  366. fatal("! Cannot open change file ",change_file_name);
  367.  
  368. /*:16*/
  369. #line 287 "wmerge.w"
  370. ;
  371. include_depth= 0;cur_line= 0;change_line= 0;
  372. change_depth= include_depth;
  373. changing= 1;prime_the_change_buffer();changing= !changing;
  374. limit= buffer;loc= buffer+1;buffer[0]= ' ';input_has_ended= 0;
  375. }
  376.  
  377. /*:15*//*17:*/
  378. #line 315 "wmerge.w"
  379.  
  380. get_line()
  381. {
  382. restart:
  383. if(changing&&include_depth==change_depth)
  384. /*21:*/
  385. #line 423 "wmerge.w"
  386. {
  387. change_line++;
  388. if(!input_ln(change_file)){
  389. err_print("! Change file ended without @z");
  390.  
  391. buffer[0]= '@';buffer[1]= 'z';limit= buffer+2;
  392. }
  393. if(limit>buffer){
  394. *limit= ' ';
  395. if(buffer[0]=='@'){
  396. if(isupper(buffer[1]))buffer[1]= tolower(buffer[1]);
  397. if(buffer[1]=='x'||buffer[1]=='y'){
  398. loc= buffer+2;
  399. err_print("! Where is the matching @z?");
  400.  
  401. }
  402. else if(buffer[1]=='z'){
  403. prime_the_change_buffer();changing= !changing;
  404. }
  405. }
  406. }
  407. }
  408.  
  409. /*:21*/
  410. #line 320 "wmerge.w"
  411. ;
  412. if(!changing||include_depth>change_depth){
  413. /*20:*/
  414. #line 407 "wmerge.w"
  415. {
  416. cur_line++;
  417. while(!input_ln(cur_file)){
  418. if(include_depth==0){input_has_ended= 1;break;}
  419. else{
  420. fclose(cur_file);include_depth--;
  421. if(changing&&include_depth==change_depth)break;
  422. cur_line++;
  423. }
  424. }
  425. if(!changing&&!input_has_ended)
  426. if(limit-buffer==change_limit-change_buffer)
  427. if(buffer[0]==change_buffer[0])
  428. if(change_limit>change_buffer)check_change();
  429. }
  430.  
  431. /*:20*/
  432. #line 322 "wmerge.w"
  433. ;
  434. if(changing&&include_depth==change_depth)goto restart;
  435. }
  436. loc= buffer;*limit= ' ';
  437. if(*buffer=='@'&&(*(buffer+1)=='i'||*(buffer+1)=='I')){
  438. loc= buffer+2;
  439. while(loc<=limit&&(*loc==' '||*loc=='\t'||*loc=='"'))loc++;
  440. if(loc>=limit){
  441. err_print("! Include file name not given");
  442.  
  443. goto restart;
  444. }
  445. if(include_depth>=max_include_depth-1){
  446. err_print("! Too many nested includes");
  447.  
  448. goto restart;
  449. }
  450. include_depth++;
  451. /*19:*/
  452. #line 366 "wmerge.w"
  453. {
  454. #line 272 "wmerge.ch"
  455. static char*temp_file_name;
  456. #line 368 "wmerge.w"
  457. char*cur_file_name_end= cur_file_name+max_file_name_length-1;
  458. char*k= cur_file_name,*kk;
  459. int l;
  460.  
  461. #line 278 "wmerge.ch"
  462. alloc_object(temp_file_name,max_file_name_length,char);
  463. while(*loc!=' '&&*loc!='\t'&&*loc!='"'&&k<=cur_file_name_end)*k++= *loc++;
  464. #line 373 "wmerge.w"
  465. if(k>cur_file_name_end)too_long();
  466.  
  467. *k= '\0';
  468. if((cur_file= fopen(cur_file_name,"r"))!=NULL){
  469. cur_line= 0;
  470. goto restart;
  471. }
  472. #line 312 "wmerge.ch"
  473. if(0==set_path(include_path,getenv("CWEBINPUTS"))){
  474. include_depth--;goto restart;
  475. }
  476. path_prefix= include_path;
  477. while(path_prefix){
  478. for(kk= temp_file_name,p= path_prefix,l= 0;
  479. p&&*p&&*p!=PATH_SEPARATOR;
  480. *kk++= *p++,l++);
  481. if(path_prefix&&*path_prefix&&*path_prefix!=PATH_SEPARATOR&&
  482. *--p!=DEVICE_SEPARATOR&&*p!=DIR_SEPARATOR){
  483. *kk++= DIR_SEPARATOR;l++;
  484. }
  485. if(k+l+2>=cur_file_name_end)too_long();
  486. strcpy(kk,cur_file_name);
  487. if(cur_file= fopen(temp_file_name,"r")){
  488. cur_line= 0;goto restart;
  489. }
  490. if(next_path_prefix= strchr(path_prefix,PATH_SEPARATOR))
  491. path_prefix= next_path_prefix+1;
  492. else break;
  493. }
  494. #line 404 "wmerge.w"
  495. include_depth--;err_print("! Cannot open include file");goto restart;
  496. }
  497.  
  498. /*:19*/
  499. #line 340 "wmerge.w"
  500. ;
  501. }
  502. return(!input_has_ended);
  503. }
  504.  
  505. #line 233 "wmerge.ch"
  506. void put_line(void)
  507. {
  508. char*ptr= buffer;
  509. while(ptr<limit)
  510. {
  511. putc(*ptr,out_file);
  512. ptr++;
  513. }
  514. putc('\n',out_file);
  515. }
  516. #line 351 "wmerge.w"
  517.  
  518. #line 257 "wmerge.ch"
  519. /*:17*//*22:*/
  520. #line 449 "wmerge.w"
  521.  
  522. #line 340 "wmerge.ch"
  523. void check_complete(void){
  524. #line 452 "wmerge.w"
  525. if(change_limit!=change_buffer){
  526. #line 347 "wmerge.ch"
  527. strncpy(buffer,change_buffer,(size_t)(change_limit-change_buffer+1));
  528. #line 454 "wmerge.w"
  529. limit= buffer+(int)(change_limit-change_buffer);
  530. changing= 1;change_depth= include_depth;loc= buffer;
  531. err_print("! Change file entry did not match");
  532.  
  533. }
  534. }
  535.  
  536. /*:22*//*25:*/
  537. #line 365 "wmerge.ch"
  538.  
  539. void err_print(char*s)
  540. #line 498 "wmerge.w"
  541. {
  542. char*k,*l;
  543. fprintf(stderr,*s=='!'?"\n%s":"%s",s);
  544. if(web_file_open)/*26:*/
  545. #line 514 "wmerge.w"
  546.  
  547. {if(changing&&include_depth==change_depth)
  548. printf(". (l. %d of change file)\n",change_line);
  549. else if(include_depth==0)fprintf(stderr,". (l. %d)\n",cur_line);
  550. else fprintf(stderr,". (l. %d of include file %s)\n",cur_line,cur_file_name);
  551. l= (loc>=limit?limit:loc);
  552. if(l>buffer){
  553. for(k= buffer;k<l;k++)
  554. if(*k=='\t')putc(' ',stderr);
  555. else putc(*k,stderr);
  556. putchar('\n');
  557. for(k= buffer;k<l;k++)putc(' ',stderr);
  558. }
  559. for(k= l;k<limit;k++)putc(*k,stderr);
  560. putc('\n',stderr);
  561. }
  562.  
  563. /*:26*/
  564. #line 501 "wmerge.w"
  565. ;
  566. update_terminal;mark_error;
  567. }
  568.  
  569. /*:25*//*28:*/
  570. #line 403 "wmerge.ch"
  571.  
  572. #ifdef __TURBOC__
  573. int wrap_up(void){
  574. int return_val;
  575.  
  576. putchar('\n');
  577. /*55:*/
  578. #line 719 "wmerge.ch"
  579.  
  580. if(out_file)
  581. fclose(out_file);
  582. if(check_file)
  583. fclose(check_file);
  584. if(check_file_name)
  585. remove(check_file_name);
  586.  
  587. /*:55*/
  588. #line 409 "wmerge.ch"
  589.  
  590. /*29:*/
  591. #line 553 "wmerge.w"
  592.  
  593. switch(history){
  594. case spotless:if(show_happiness)fprintf(stderr,"(No errors were found.)\n");break;
  595. case harmless_message:
  596. fprintf(stderr,"(Did you see the warning message above?)\n");break;
  597. case error_message:
  598. fprintf(stderr,"(Pardon me, but I think I spotted something wrong.)\n");break;
  599. case fatal_message:fprintf(stderr,"(That was a fatal error, my friend.)\n");
  600. }
  601.  
  602. /*:29*/
  603. #line 410 "wmerge.ch"
  604. ;
  605. switch(history){
  606. case harmless_message:return_val= RETURN_WARN;break;
  607. case error_message:return_val= RETURN_ERROR;break;
  608. case fatal_message:return_val= RETURN_FAIL;break;
  609. default:return_val= RETURN_OK;
  610. }
  611. return(return_val);
  612. }
  613. #else
  614. int wrap_up(void){
  615. putchar('\n');
  616. /*55:*/
  617. #line 719 "wmerge.ch"
  618.  
  619. if(out_file)
  620. fclose(out_file);
  621. if(check_file)
  622. fclose(check_file);
  623. if(check_file_name)
  624. remove(check_file_name);
  625.  
  626. /*:55*/
  627. #line 422 "wmerge.ch"
  628.  
  629. /*29:*/
  630. #line 553 "wmerge.w"
  631.  
  632. switch(history){
  633. case spotless:if(show_happiness)fprintf(stderr,"(No errors were found.)\n");break;
  634. case harmless_message:
  635. fprintf(stderr,"(Did you see the warning message above?)\n");break;
  636. case error_message:
  637. fprintf(stderr,"(Pardon me, but I think I spotted something wrong.)\n");break;
  638. case fatal_message:fprintf(stderr,"(That was a fatal error, my friend.)\n");
  639. }
  640.  
  641. /*:29*/
  642. #line 423 "wmerge.ch"
  643. ;
  644. switch(history){
  645. case harmless_message:return(RETURN_WARN);break;
  646. case error_message:return(RETURN_ERROR);break;
  647. case fatal_message:return(RETURN_FAIL);break;
  648. default:return(RETURN_OK);
  649. }
  650. }
  651. #endif
  652. #line 552 "wmerge.w"
  653.  
  654. /*:28*//*33:*/
  655. #line 472 "wmerge.ch"
  656.  
  657. void scan_args(void)
  658. #line 606 "wmerge.w"
  659. {
  660. char*dot_pos;
  661. #line 609 "wmerge.w"
  662. register char*s;
  663. boolean found_web= 0,found_change= 0,found_out= 0;
  664.  
  665. boolean flag_change;
  666.  
  667. while(--argc>0){
  668. if(**(++argv)=='-'||**argv=='+')/*37:*/
  669. #line 674 "wmerge.w"
  670.  
  671. {
  672. if(**argv=='-')flag_change= 0;
  673. else flag_change= 1;
  674. for(dot_pos= *argv+1;*dot_pos>'\0';dot_pos++)
  675. flags[*dot_pos]= flag_change;
  676. }
  677.  
  678. /*:37*/
  679. #line 615 "wmerge.w"
  680.  
  681. else{
  682. #line 489 "wmerge.ch"
  683. s= *argv;dot_pos= NULL;
  684. while(*s){
  685. if(*s=='.')dot_pos= s++;
  686. else if(*s==DIR_SEPARATOR||*s==DEVICE_SEPARATOR||*s=='/')
  687. dot_pos= NULL,++s;
  688. else s++;
  689. }
  690. #line 623 "wmerge.w"
  691. if(!found_web)/*34:*/
  692. #line 640 "wmerge.w"
  693.  
  694. {
  695. if(s-*argv>max_file_name_length-5)
  696. /*39:*/
  697. #line 687 "wmerge.w"
  698. fatal("! Filename too long\n",*argv);
  699.  
  700. /*:39*/
  701. #line 643 "wmerge.w"
  702. ;
  703. if(dot_pos==NULL)
  704. sprintf(web_file_name,"%s.w",*argv);
  705. else{
  706. strcpy(web_file_name,*argv);
  707. *dot_pos= 0;
  708. }
  709. sprintf(alt_web_file_name,"%s.web",*argv);
  710. *out_file_name= '\0';
  711. found_web= 1;
  712. }
  713.  
  714. /*:34*/
  715. #line 623 "wmerge.w"
  716.  
  717. else if(!found_change)/*35:*/
  718. #line 655 "wmerge.w"
  719.  
  720. {
  721. if(s-*argv>max_file_name_length-4)
  722. /*39:*/
  723. #line 687 "wmerge.w"
  724. fatal("! Filename too long\n",*argv);
  725.  
  726. /*:39*/
  727. #line 658 "wmerge.w"
  728. ;
  729. if(dot_pos==NULL)
  730. sprintf(change_file_name,"%s.ch",*argv);
  731. else strcpy(change_file_name,*argv);
  732. found_change= 1;
  733. }
  734.  
  735. /*:35*/
  736. #line 624 "wmerge.w"
  737.  
  738. else if(!found_out)/*36:*/
  739. #line 665 "wmerge.w"
  740.  
  741. {
  742. if(s-*argv>max_file_name_length-5)
  743. /*39:*/
  744. #line 687 "wmerge.w"
  745. fatal("! Filename too long\n",*argv);
  746.  
  747. /*:39*/
  748. #line 668 "wmerge.w"
  749. ;
  750. if(dot_pos==NULL)sprintf(out_file_name,"%s.out",*argv);
  751. else strcpy(out_file_name,*argv);
  752. found_out= 1;
  753. }
  754.  
  755. /*:36*/
  756. #line 625 "wmerge.w"
  757.  
  758. else/*38:*/
  759. #line 682 "wmerge.w"
  760.  
  761. {
  762. fatal("! Usage: wmerge webfile[.w] [changefile[.ch] [outfile[.out]]]\n","")
  763. }
  764.  
  765. /*:38*/
  766. #line 626 "wmerge.w"
  767. ;
  768. }
  769. }
  770. if(!found_web)/*38:*/
  771. #line 682 "wmerge.w"
  772.  
  773. {
  774. fatal("! Usage: wmerge webfile[.w] [changefile[.ch] [outfile[.out]]]\n","")
  775. }
  776.  
  777. /*:38*/
  778. #line 629 "wmerge.w"
  779. ;
  780. #line 501 "wmerge.ch"
  781. #ifdef _DEV_NULL
  782. if(!found_change)strcpy(change_file_name,_DEV_NULL);
  783. #else
  784. if(!found_change)strcpy(change_file_name,"/dev/null");
  785. #endif
  786. #line 631 "wmerge.w"
  787. }
  788.  
  789. /*:33*/
  790. #line 19 "wmerge.w"
  791.  
  792. #line 104 "wmerge.ch"
  793. int main(int ac,char**av)
  794. #line 22 "wmerge.w"
  795. {
  796. argc= ac;argv= av;
  797. #line 110 "wmerge.ch"
  798. /*52:*/
  799. #line 701 "wmerge.ch"
  800.  
  801. if(signal(SIGINT,&catch_break)==SIG_ERR)
  802. exit(1);
  803.  
  804. /*:52*/
  805. #line 110 "wmerge.ch"
  806.  
  807. /*50:*/
  808. #line 671 "wmerge.ch"
  809.  
  810. alloc_object(buffer,buf_size,ASCII);
  811. buffer_end= buffer+buf_size-2;
  812. alloc_object(file,max_include_depth,FILE*);
  813. alloc_object(file_name,max_include_depth,char*);
  814. for(i= 0;i<max_include_depth;i++)
  815. alloc_object(file_name[i],max_file_name_length,char);
  816. alloc_object(change_file_name,max_file_name_length,char);
  817. alloc_object(alt_web_file_name,max_file_name_length,char);
  818. alloc_object(line,max_include_depth,int);
  819. alloc_object(change_buffer,buf_size,char);
  820. alloc_object(out_file_name,max_file_name_length,char);
  821. alloc_object(check_file_name,max_file_name_length,char);
  822. alloc_object(flags,256,boolean);
  823. alloc_object(include_path,max_path_length+2,char);
  824. #ifdef CWEBINPUTS
  825. strcpy(include_path,CWEBINPUTS);
  826. #endif
  827.  
  828. /*:50*/
  829. #line 111 "wmerge.ch"
  830. ;
  831. /*31:*/
  832. #line 583 "wmerge.w"
  833.  
  834. show_banner= show_happiness= 1;
  835.  
  836. /*:31*/
  837. #line 112 "wmerge.ch"
  838. ;
  839. #line 25 "wmerge.w"
  840. /*41:*/
  841. #line 695 "wmerge.w"
  842.  
  843. #line 522 "wmerge.ch"
  844. scan_args();
  845. tmpnam(check_file_name);
  846. if(strrchr(check_file_name,DEVICE_SEPARATOR))
  847. check_file_name= strrchr(check_file_name,DEVICE_SEPARATOR)+1;
  848. if(out_file_name[0]=='\0')out_file= stdout;
  849. else if(!(out_file= fopen(check_file_name,"w")))
  850. fatal("! Cannot open output file ",check_file_name);
  851.  
  852. #line 701 "wmerge.w"
  853.  
  854. /*:41*/
  855. #line 25 "wmerge.w"
  856. ;
  857. reset_input();
  858. while(get_line())
  859. put_line();
  860. fflush(out_file);
  861. check_complete();
  862. fflush(out_file);
  863. #line 118 "wmerge.ch"
  864. if(out_file!=stdout){
  865. fclose(out_file);out_file= NULL;
  866. /*47:*/
  867. #line 620 "wmerge.ch"
  868.  
  869. if(out_file= fopen(out_file_name,"r")){
  870. char*x,*y;
  871. int x_size,y_size;
  872.  
  873. if(!(check_file= fopen(check_file_name,"r")))
  874. fatal("! Cannot open output file",check_file_name);
  875.  
  876. alloc_object(x,BUFSIZ,char);
  877. alloc_object(y,BUFSIZ,char);
  878.  
  879. /*48:*/
  880. #line 648 "wmerge.ch"
  881.  
  882. do{
  883. x_size= fread(x,1,BUFSIZ,out_file);
  884. y_size= fread(y,1,BUFSIZ,check_file);
  885. }while((x_size==y_size)&&!memcmp(x,y,x_size)&&
  886. !feof(out_file)&&!feof(check_file));
  887.  
  888. /*:48*/
  889. #line 631 "wmerge.ch"
  890.  
  891.  
  892. fclose(out_file);out_file= NULL;
  893. fclose(check_file);check_file= NULL;
  894.  
  895. /*49:*/
  896. #line 658 "wmerge.ch"
  897.  
  898. if((x_size!=y_size)||memcmp(x,y,x_size)){
  899. remove(out_file_name);
  900. rename(check_file_name,out_file_name);
  901. }
  902. else
  903. remove(check_file_name);
  904.  
  905. /*:49*/
  906. #line 636 "wmerge.ch"
  907.  
  908.  
  909. free_object(y);
  910. free_object(x);
  911. }
  912. else
  913. rename(check_file_name,out_file_name);
  914.  
  915. check_file_name= NULL;
  916.  
  917. /*:47*/
  918. #line 120 "wmerge.ch"
  919.  
  920. }
  921. return wrap_up();
  922. #line 33 "wmerge.w"
  923. }
  924.  
  925. /*:1*//*43:*/
  926. #line 544 "wmerge.ch"
  927.  
  928. static boolean set_path(char*default_path,char*environment)
  929. {
  930. static char*string;
  931.  
  932. alloc_object(string,max_path_length+2,char);
  933. if(environment){
  934. if(strlen(environment)+strlen(default_path)>=max_path_length){
  935. err_print("! Include path too long");return(0);
  936.  
  937. }
  938. else{
  939. sprintf(string,"%s%c%s",environment,PATH_SEPARATOR,default_path);
  940. strcpy(default_path,string);
  941. }
  942. }
  943. return(1);
  944. }
  945.  
  946. /*:43*//*53:*/
  947. #line 709 "wmerge.ch"
  948.  
  949. void catch_break(int dummy)
  950. {
  951. history= fatal_message;
  952. exit(wrap_up());
  953. }
  954.  
  955. /*:53*/
  956.