trans.c 16 KB

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