trans.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. *
  5. */
  6. #include "ack.h"
  7. #include "list.h"
  8. #include "trans.h"
  9. #include "grows.h"
  10. #include "data.h"
  11. #ifndef NORCSID
  12. static char rcs_id[] = "$Id$" ;
  13. static char rcs_trans[] = RCS_TRANS ;
  14. #endif
  15. /****************************************************************************/
  16. /* Routines for transforming from one file type to another */
  17. /****************************************************************************/
  18. static growstring head ;
  19. static int touch_head= NO ;
  20. static growstring tail ;
  21. static int touch_tail= NO ;
  22. char *headvar(),*tailvar() ;
  23. int transform(phase) register trf *phase ; {
  24. int ok ;
  25. if ( !setfiles(phase) ) {
  26. disc_files(phase) ;
  27. return 0 ;
  28. }
  29. getcallargs(phase) ;
  30. ok= runphase(phase) ;
  31. if ( !ok ) rmfile(&out) ;
  32. /* Free the space occupied by the arguments,
  33. except for the linker, since we are bound to exit soon
  34. and do not foresee further need of memory space */
  35. if ( !phase->t_linker ) discardargs(phase) ;
  36. disc_files(phase) ;
  37. return ok ;
  38. }
  39. getmapflags(phase) register trf *phase ; {
  40. register path *l_in ;
  41. register list_elem *elem ;
  42. int scanned ;
  43. register char *ptr ;
  44. scanlist(l_first(flags),elem) {
  45. scanned= *(l_content(*elem))&NO_SCAN ;
  46. *(l_content(*elem)) &= ~NO_SCAN ;
  47. if ( mapflag(&(phase->t_mapf),l_content(*elem)) ) {
  48. scanned=NO_SCAN ;
  49. #ifdef DEBUG
  50. if ( debug >=4 ) {
  51. vprint("phase %s, added mapflag for %s\n",
  52. phase->t_name,
  53. l_content(*elem) ) ;
  54. }
  55. #endif
  56. }
  57. *(l_content(*elem)) |= scanned ;
  58. }
  59. if ( phase->t_linker ) {
  60. scanlist(l_first(phase->t_inputs),elem) {
  61. l_in = p_cont(*elem) ;
  62. if ( mapflag(&(phase->t_mapf),l_in->p_path) ) {
  63. ptr= keeps(getvar(LIBVAR)) ;
  64. clr_noscan(ptr) ;
  65. #ifdef DEBUG
  66. if ( debug >=4 ) {
  67. vprint("phase %s, library %s(%s)\n",
  68. phase->t_name,l_in->p_path,ptr) ;
  69. }
  70. #endif
  71. if ( l_in->p_keeps) throws(l_in->p_path) ;
  72. l_in->p_path= ptr ;
  73. l_in->p_keeps=YES ;
  74. }
  75. }
  76. scanlist(l_first(flags),elem) {
  77. /* Get the flags remaining for the loader,
  78. That is: all the flags neither eaten by ack nor
  79. one of the subprograms called so-far.
  80. The last fact is indicated by the NO_SCAN bit
  81. in the first character of the flag.
  82. */
  83. if ( !( *(l_content(*elem))&NO_SCAN ) ) {
  84. l_add(&(phase->t_flags),l_content(*elem)) ;
  85. #ifdef DEBUG
  86. if ( debug >=4 ) {
  87. vprint("phase %s, added flag %s\n",
  88. phase->t_name,
  89. l_content(*elem) ) ;
  90. }
  91. #endif
  92. }
  93. }
  94. }
  95. }
  96. do_Rflag(argp) char *argp ; {
  97. l_add(&R_list,argp) ;
  98. }
  99. char *headvar() {
  100. if ( !touch_head) return "" ;
  101. return gr_start(head) ;
  102. }
  103. add_head(str) char *str; {
  104. if ( !touch_head) {
  105. gr_init(&head) ;
  106. touch_head=YES ;
  107. }
  108. gr_cat(&head,str) ;
  109. }
  110. char *tailvar() {
  111. if ( !touch_tail ) return "" ;
  112. return gr_start(tail) ;
  113. }
  114. add_tail(str) char *str ; {
  115. if ( !touch_tail ) {
  116. gr_init(&tail) ;
  117. touch_tail=YES ;
  118. }
  119. gr_cat(&tail,str) ;
  120. }
  121. transini() {
  122. register list_elem *elem ;
  123. register trf *phase ;
  124. scanlist(l_first(tr_list), elem) {
  125. phase = t_cont(*elem) ;
  126. if ( !phase->t_linker ) getmapflags(phase);
  127. }
  128. scanlist(l_first(R_list), elem) {
  129. set_Rflag(l_content(*elem)) ;
  130. }
  131. l_clear(&R_list) ;
  132. setpvar(keeps(HEAD),headvar) ;
  133. setpvar(keeps(TAIL),tailvar) ;
  134. }
  135. set_Rflag(argp) register char *argp ; {
  136. register char *eos ;
  137. register list_elem *prog ;
  138. register int length ;
  139. char *eq, *colon ;
  140. eos= strindex(&argp[2],'-');
  141. eq= strindex(&argp[2],EQUAL) ;
  142. colon= strindex(&argp[2],':');
  143. if ( !eos ) {
  144. eos= eq ;
  145. } else {
  146. if ( eq && eq<eos ) eos= eq ;
  147. }
  148. if ( colon && ( !eos || eos>colon ) ) eos= colon ;
  149. if ( !eos ) {
  150. if ( !(argp[0]&NO_SCAN) ) werror("Incorrect use of -R flag") ;
  151. return ;
  152. }
  153. length= eos - &argp[2] ;
  154. scanlist(l_first(tr_list), prog) {
  155. if ( strncmp(t_cont(*prog)->t_name, &argp[2], length )==0 &&
  156. t_cont(*prog)->t_name[length]==0 /* Same name length */) {
  157. if ( *eos=='-' ) {
  158. if ( !(argp[0]&NO_SCAN) ) {
  159. /* If not already taken by a mapflag */
  160. l_add(&(t_cont(*prog)->t_flags),eos) ;
  161. }
  162. } else
  163. if ( *eos=='=' ) {
  164. t_cont(*prog)->t_prog= eos+1 ;
  165. } else {
  166. t_cont(*prog)->t_priority= atoi(eos+1) ;
  167. }
  168. argp[0] |= NO_SCAN ;
  169. return ;
  170. }
  171. }
  172. if ( !(argp[0]&NO_SCAN) ) werror("Cannot find program for %s",argp) ;
  173. return ;
  174. }
  175. /**************************************************************************/
  176. /* */
  177. /* The creation of arguments for exec for a transformation */
  178. /* */
  179. /**************************************************************************/
  180. growstring scanb(line) char *line ; {
  181. /* Scan a line for backslashes, setting the NO_SCAN bit in characters
  182. preceded by a backslash.
  183. */
  184. register char *in_c ;
  185. register int token ;
  186. growstring result ;
  187. enum { TEXT, ESCAPED } state = TEXT ;
  188. gr_init(&result) ;
  189. for ( in_c= line ; *in_c ; in_c++ ) {
  190. token= *in_c&0377 ;
  191. switch( state ) {
  192. case TEXT :
  193. if ( token==BSLASH ) {
  194. state= ESCAPED ;
  195. } else {
  196. gr_add(&result,token) ;
  197. }
  198. break ;
  199. case ESCAPED :
  200. gr_add(&result,token|NO_SCAN) ;
  201. state=TEXT ;
  202. break ;
  203. }
  204. }
  205. gr_add(&result,0) ;
  206. if ( state!=TEXT ) werror("flag line ends with %c",BSLASH) ;
  207. return result ;
  208. }
  209. growstring scanvars(line) char *line ; {
  210. /* Scan a line variable replacements started by S_VAR.
  211. Two sequences exist: S_VAR name E_VAR, S_VAR name A_VAR text E_VAR.
  212. neither name nor text may contain further replacements.
  213. In the first form an error message is issued if the name is not
  214. present in the variables, the second form produces text
  215. in that case.
  216. The sequence S_VAR S_VAR is transformed into S_VAR.
  217. This to allow later recognition in mapflags, where B_SLASH
  218. would be preventing any recognition.
  219. */
  220. register char *in_c ;
  221. register int token ;
  222. growstring result ;
  223. growstring name ;
  224. register char *tr ;
  225. enum { TEXT, FIRST, NAME, SKIP, COPY } state = TEXT ;
  226. gr_init(&result) ; gr_init(&name) ;
  227. for ( in_c= line ; *in_c ; in_c++ ) {
  228. token= *in_c&0377 ;
  229. switch( state ) {
  230. case TEXT :
  231. if ( token==S_VAR ) {
  232. state= FIRST ;
  233. } else {
  234. gr_add(&result,token) ;
  235. }
  236. break ;
  237. case FIRST :
  238. switch ( token ) {
  239. case S_VAR :
  240. state= TEXT ;
  241. gr_add(&result,token) ;
  242. break ;
  243. case A_VAR :
  244. case C_VAR :
  245. fatal("empty string variable name") ;
  246. default :
  247. state=NAME ;
  248. gr_add(&name,token) ;
  249. break ;
  250. }
  251. break ;
  252. case NAME:
  253. switch ( token ) {
  254. case A_VAR :
  255. gr_add(&name,0) ;
  256. if ( tr=getvar(gr_start(name)) ) {
  257. while ( *tr ) {
  258. gr_add(&result,*tr++) ;
  259. }
  260. state=SKIP ;
  261. } else {
  262. state=COPY ;
  263. }
  264. gr_throw(&name) ;
  265. break ;
  266. case C_VAR :
  267. gr_add(&name,0) ;
  268. if ( tr=getvar(gr_start(name)) ) {
  269. while ( *tr ) {
  270. gr_add(&result,*tr++);
  271. }
  272. } else {
  273. werror("No definition for %s",
  274. gr_start(name)) ;
  275. }
  276. state=TEXT ;
  277. gr_throw(&name) ;
  278. break ;
  279. default:
  280. gr_add(&name,token) ;
  281. break ;
  282. }
  283. break ;
  284. case SKIP :
  285. if ( token==C_VAR ) state= TEXT ;
  286. break ;
  287. case COPY :
  288. if ( token==C_VAR ) state= TEXT ; else {
  289. gr_add(&result,token) ;
  290. }
  291. break ;
  292. }
  293. }
  294. gr_add(&result,0) ;
  295. if ( state!=TEXT ) {
  296. werror("flag line misses %c",C_VAR) ;
  297. gr_throw(&name) ;
  298. }
  299. return result ;
  300. }
  301. growstring scanexpr(line) char *line ; {
  302. /* Scan a line for conditional or flag expressions,
  303. dependent on the type. The format is
  304. S_EXPR suflist M_EXPR suflist T_EXPR tail C_EXPR
  305. the head and tail are passed to treat, together with the
  306. growstring for futher treatment.
  307. Nesting is not allowed.
  308. */
  309. register char *in_c ;
  310. char *heads ;
  311. register int token ;
  312. growstring sufs, tailval ;
  313. growstring result ;
  314. static list_head fsuff, lsuff ;
  315. enum { TEXT, FDOT, FSUF, LDOT, LSUF, FTAIL } state = TEXT ;
  316. gr_init(&result) ; gr_init(&sufs) ; gr_init(&tailval) ;
  317. for ( in_c= line ; *in_c ; in_c++ ) {
  318. token= *in_c&0377 ;
  319. switch( state ) {
  320. case TEXT :
  321. if ( token==S_EXPR ) {
  322. state= FDOT ;
  323. heads=in_c ;
  324. } else gr_add(&result,token) ;
  325. break ;
  326. case FDOT :
  327. if ( token==M_EXPR ) {
  328. state=LDOT ;
  329. break ;
  330. }
  331. token &= ~NO_SCAN ;
  332. if ( token!=SUFCHAR ) {
  333. error("Missing %c in expression",SUFCHAR) ;
  334. }
  335. gr_add(&sufs,token) ; state=FSUF ;
  336. break ;
  337. case FSUF :
  338. if ( token==M_EXPR || (token&~NO_SCAN)==SUFCHAR) {
  339. gr_add(&sufs,0) ;
  340. l_add(&fsuff,gr_final(&sufs)) ;
  341. }
  342. if ( token==M_EXPR ) {
  343. state=LDOT ;
  344. } else gr_add(&sufs,token&~NO_SCAN) ;
  345. break ;
  346. case LDOT :
  347. if ( token==T_EXPR ) {
  348. state=FTAIL ;
  349. break ;
  350. }
  351. token &= ~NO_SCAN ;
  352. if ( token!=SUFCHAR ) {
  353. error("Missing %c in expression",SUFCHAR) ;
  354. }
  355. gr_add(&sufs,token) ; state=LSUF ;
  356. break ;
  357. case LSUF :
  358. if ( token==T_EXPR || (token&~NO_SCAN)==SUFCHAR) {
  359. gr_add(&sufs,0) ;
  360. l_add(&lsuff,gr_final(&sufs)) ;
  361. }
  362. if ( token==T_EXPR ) {
  363. state=FTAIL ;
  364. } else gr_add(&sufs,token&~NO_SCAN) ;
  365. break ;
  366. case FTAIL :
  367. if ( token==C_EXPR ) {
  368. /* Found one !! */
  369. gr_add(&tailval,0) ;
  370. condit(&result,&fsuff,&lsuff,gr_start(tailval)) ;
  371. l_throw(&fsuff) ; l_throw(&lsuff) ;
  372. gr_throw(&tailval) ;
  373. state=TEXT ;
  374. } else gr_add(&tailval,token) ;
  375. break ;
  376. }
  377. }
  378. gr_add(&result,0) ;
  379. if ( state!=TEXT ) {
  380. l_throw(&fsuff) ; l_throw(&lsuff) ; gr_throw(&tailval) ;
  381. werror("flag line has unclosed expression starting with %6s",
  382. heads) ;
  383. }
  384. return result ;
  385. }
  386. condit(line,fsuff,lsuff,tailval) growstring *line ;
  387. list_head *fsuff, *lsuff;
  388. char *tailval ;
  389. {
  390. register list_elem *first ;
  391. register list_elem *last ;
  392. #ifdef DEBUG
  393. if ( debug>=4 ) vprint("Conditional for %s, ",tailval) ;
  394. #endif
  395. scanlist( l_first(*fsuff), first ) {
  396. scanlist( l_first(*lsuff), last ) {
  397. if ( strcmp(l_content(*first),l_content(*last))==0 ) {
  398. /* Found */
  399. #ifdef DEBUG
  400. if ( debug>=4 ) vprint(" matched\n") ;
  401. #endif
  402. while ( *tailval) gr_add(line,*tailval++ ) ;
  403. return ;
  404. }
  405. }
  406. }
  407. #ifdef DEBUG
  408. if ( debug>=4) vprint(" non-matched\n") ;
  409. #endif
  410. }
  411. int mapflag(maplist,cflag) list_head *maplist ; char *cflag ; {
  412. /* Expand a flag expression */
  413. /* The flag "cflag" is checked for each of the mapflags.
  414. A mapflag entry has the form
  415. -text NAME=replacement or -text*text NAME=replacement
  416. The star matches anything as in the shell.
  417. If the entry matches the assignment will take place
  418. This replacement is subjected to argument matching only.
  419. When a match took place the replacement is returned
  420. when not, (char *)0.
  421. The replacement sits in stable storage.
  422. */
  423. register list_elem *elem ;
  424. scanlist(l_first(*maplist),elem) {
  425. if ( mapexpand(l_content(*elem),cflag) ) {
  426. return 1 ;
  427. }
  428. }
  429. return 0 ;
  430. }
  431. int mapexpand(mapentry,cflag)
  432. char *mapentry, *cflag ;
  433. {
  434. register char *star ;
  435. register char *ptr ;
  436. register char *space ;
  437. int length ;
  438. star=strindex(mapentry,STAR) ;
  439. space=firstblank(mapentry) ;
  440. if ( star >space ) star= (char *)0 ;
  441. if ( star ) {
  442. length= space-star-1 ;
  443. if ( strncmp(mapentry,cflag,star-mapentry) ||
  444. strncmp(star+1,cflag+strlen(cflag)-length,length) ) {
  445. return 0 ;
  446. }
  447. /* Match */
  448. /* Now set star to the first char of the star
  449. replacement and length to its length
  450. */
  451. length=strlen(cflag)-(star-mapentry)-length ;
  452. if ( length<0 ) return 0 ;
  453. star=cflag+(star-mapentry) ;
  454. #ifdef DEBUG
  455. if ( debug>=6 ) {
  456. vprint("Starmatch (%s,%s) %.*s\n",
  457. mapentry,cflag,length,star) ;
  458. }
  459. #endif
  460. } else {
  461. if ( strncmp(mapentry,cflag,space-mapentry)!=0 ||
  462. cflag[space-mapentry] ) {
  463. return 0 ;
  464. }
  465. }
  466. ptr= skipblank(space) ;
  467. if ( *ptr==0 ) return 1 ;
  468. doassign(ptr,star,length) ;
  469. return 1 ;
  470. }
  471. doassign(line,star,length) char *line, *star ; {
  472. growstring varval, name, temp ;
  473. register char *ptr ;
  474. gr_init(&varval) ;
  475. gr_init(&name) ;
  476. ptr= line ;
  477. for ( ; *ptr && *ptr!=SPACE && *ptr!=TAB && *ptr!=EQUAL ; ptr++ ) {
  478. gr_add(&name,*ptr) ;
  479. }
  480. ptr= strindex(ptr,EQUAL) ;
  481. if ( !ptr ) {
  482. error("Missing %c in assignment %s",EQUAL,line);
  483. return ;
  484. }
  485. temp= scanvars(ptr+1) ;
  486. for ( ptr=gr_start(temp); *ptr; ptr++ ) switch ( *ptr ) {
  487. case STAR :
  488. if ( star ) {
  489. while ( length-- ) gr_add(&varval,*star++|NO_SCAN) ;
  490. break ;
  491. }
  492. default :
  493. gr_add(&varval,*ptr) ;
  494. break ;
  495. }
  496. gr_throw(&temp) ;
  497. gr_add(&name,0) ; gr_add(&varval,0) ;
  498. setsvar(gr_final(&name),gr_final(&varval)) ;
  499. }
  500. #define ISBLANK(c) ( (c)==SPACE || (c)==TAB )
  501. unravel(line,action) char *line ; int (*action)() ; {
  502. /* Unravel the line, get arguments a la shell */
  503. /* each argument is handled to action */
  504. /* The input string is left intact */
  505. register char *in_c ;
  506. register int token ;
  507. enum { BLANK, ARG } state = BLANK ;
  508. growstring argum ;
  509. in_c=line ;
  510. for (;;) {
  511. token= *in_c&0377 ;
  512. switch ( state ) {
  513. case BLANK :
  514. if ( token==0 ) break ;
  515. if ( !ISBLANK(token) ) {
  516. state= ARG ;
  517. gr_init(&argum) ;
  518. gr_add(&argum,token&~NO_SCAN) ;
  519. }
  520. break ;
  521. case ARG :
  522. if ( ISBLANK(token) || token==0 ) {
  523. gr_add(&argum,0) ;
  524. (*action)(gr_start(argum)) ;
  525. gr_throw(&argum) ;
  526. state=BLANK ;
  527. } else {
  528. gr_add(&argum,token&~NO_SCAN) ;
  529. }
  530. break ;
  531. }
  532. if ( token == 0 ) break ;
  533. in_c++ ;
  534. }
  535. }
  536. char *c_rep(string,place,rep) char *string, *place, *rep ; {
  537. /* Produce a string in stable storage produced from 'string'
  538. with the character at place replaced by rep
  539. */
  540. growstring name ;
  541. register char *nc ;
  542. register char *xc ;
  543. gr_init(&name) ;
  544. for ( nc=string ; *nc && nc<place ; nc++ ) {
  545. gr_add(&name,*nc) ;
  546. }
  547. #ifdef DEBUG
  548. if ( *nc==0 ) fatal("Place is not in string") ;
  549. #endif
  550. for ( xc=rep ; *xc ; xc++ ) gr_add(&name,*xc|NO_SCAN) ;
  551. gr_add(&name,0) ;
  552. gr_cat(&name,nc+1) ;
  553. return gr_final(&name) ;
  554. }
  555. static list_head *curargs ;
  556. static list_head *comb_args ;
  557. addargs(string) char *string ; {
  558. register char *temp, *repc ;
  559. register list_elem *elem ;
  560. repc=strindex(string,C_IN) ;
  561. if ( repc ) {
  562. /* INPUT FILE TOKEN seen, replace it and scan further */
  563. if ( repc==string && string[1]==0 ) {
  564. if ( in.p_path ) { /* All but combiner */
  565. l_add(curargs,keeps(in.p_path)) ;
  566. } else {
  567. scanlist( l_first(*comb_args), elem ) {
  568. l_add(curargs,
  569. keeps(p_cont(*elem)->p_path)) ;
  570. }
  571. }
  572. return ;
  573. }
  574. if ( in.p_path ) { /* Not for the combiners */
  575. temp=c_rep(string,repc,in.p_path) ;
  576. addargs(temp) ;
  577. throws(temp) ;
  578. } else { /* For the combiners */
  579. scanlist( l_first(*comb_args), elem ) {
  580. temp=c_rep(string,repc,p_cont(*elem)->p_path);
  581. addargs(temp) ;
  582. throws(temp) ;
  583. }
  584. }
  585. return ;
  586. }
  587. repc=strindex(string,C_OUT) ;
  588. if ( repc ) {
  589. /* replace the outfile token as with the infile token */
  590. #ifdef DEBUG
  591. if ( !out.p_path ) fatal("missing output filename") ;
  592. #endif
  593. temp=c_rep(string,repc,out.p_path) ;
  594. addargs(temp) ;
  595. throws(temp) ;
  596. return ;
  597. }
  598. temp= keeps(string) ;
  599. clr_noscan(temp) ;
  600. l_add(curargs,temp) ;
  601. }
  602. getcallargs(phase) register trf *phase ; {
  603. growstring arg1, arg2 ;
  604. arg1= scanvars(phase->t_argd) ;
  605. #ifdef DEBUG
  606. if ( debug>=3 ) { vprint("\tvars: ") ; prns(gr_start(arg1)) ; }
  607. #endif
  608. arg2= scanexpr(gr_start(arg1)) ;
  609. #ifdef DEBUG
  610. if ( debug>=3 ) { vprint("\texpr: ") ; prns(gr_start(arg2)) ; }
  611. #endif
  612. gr_throw(&arg1) ;
  613. curargs= &phase->t_args ;
  614. if (phase->t_combine) comb_args = &phase->t_inputs ;
  615. unravel( gr_start(arg2), addargs ) ;
  616. gr_throw(&arg2) ;
  617. }
  618. discardargs(phase) register trf *phase ; {
  619. l_throw(&phase->t_args) ;
  620. }