files.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #include <em_path.h>
  12. #ifndef NORCSID
  13. static char rcs_id[] = "$Id$" ;
  14. #endif
  15. char *add_u(part,ptr) char *ptr ; {
  16. if ( part>=26 ) {
  17. ptr=add_u(part/26-1,ptr) ;
  18. }
  19. *ptr= part%26 + 'a' ;
  20. return ptr+1 ;
  21. }
  22. char *unique() {
  23. /* Get the next unique part of the internal filename */
  24. static int u_next = 0 ;
  25. static char buf[10] ;
  26. register char *ptr ;
  27. ptr=add_u(u_next,buf) ;
  28. *ptr=0 ;
  29. u_next++ ;
  30. return buf ;
  31. }
  32. setfiles(phase) register trf *phase ; {
  33. /* Set the out structure according to the in structure,
  34. the transformation and some global data */
  35. growstring pathname ;
  36. register list_elem *elem ;
  37. static int out_used= 0 ;
  38. if ( !phase->t_next && !phase->t_isprep && outfile ) {
  39. if ( out_used ) {
  40. fuerror("only one output file allowed when using the -o flag") ;
  41. } else {
  42. if ( !phase->t_keep ) fatal("Removing result file") ;
  43. phase->t_outfile=outfile ;
  44. out_used++ ;
  45. }
  46. }
  47. if ( phase->t_combine ) {
  48. in.p_path= (char *)0 ;
  49. in.p_keep=YES ;
  50. in.p_keeps=NO ;
  51. }
  52. if ( phase->t_outfile && phase->t_keep ) {
  53. out.p_path=phase->t_outfile ;
  54. out.p_keeps=NO ;
  55. out.p_keep=YES ;
  56. } else {
  57. gr_init(&pathname) ;
  58. if ( !phase->t_keep && !t_flag ) {
  59. gr_cat(&pathname,TMP_DIR) ;
  60. gr_cat(&pathname,"/") ;
  61. gr_cat(&pathname,template) ;
  62. gr_cat(&pathname,unique()) ;
  63. out.p_keep=NO ;
  64. } else {
  65. if ( !p_basename ) {
  66. gr_cat(&pathname,"Ack") ;
  67. gr_cat(&pathname,unique()) ;
  68. p_basename=keeps(gr_start(pathname)) ;
  69. werror("Output written on %s%s",
  70. p_basename,phase->t_out) ;
  71. } else {
  72. gr_cat(&pathname,p_basename) ;
  73. }
  74. out.p_keep=YES ;
  75. }
  76. gr_cat(&pathname,phase->t_out) ;
  77. out.p_path= gr_final(&pathname) ;
  78. out.p_keeps= YES ;
  79. }
  80. scanlist( l_first(arguments), elem) {
  81. if ( strcmp(l_content(*elem),out.p_path)==0 ) {
  82. error("attempt to overwrite %s",out.p_path) ;
  83. return 0 ;
  84. }
  85. }
  86. return 1 ;
  87. }
  88. disc_files(phase) trf *phase ; {
  89. path temp ;
  90. if ( !phase->t_combine ) {
  91. file_final(&in) ;
  92. } else {
  93. disc_inputs(phase) ;
  94. }
  95. temp=in ; in=out ; out=temp ;
  96. }
  97. file_final(file) path *file ; {
  98. if ( file->p_path ) {
  99. if ( !file->p_keep && t_flag<=1 ) {
  100. if ( unlink(file->p_path)!=0 ) {
  101. werror("couldn't unlink %s",file->p_path);
  102. }
  103. }
  104. if ( file->p_keeps ) throws(file->p_path) ;
  105. }
  106. file->p_path= (char *)0 ;
  107. file->p_keeps=NO ;
  108. file->p_keep=NO ;
  109. }
  110. disc_inputs(phase) trf *phase ; {
  111. /* Remove all the input files of this phase */
  112. /* Only for combiners */
  113. register path *l_in ;
  114. register list_elem *elem ;
  115. scanlist( l_first(phase->t_inputs), elem) {
  116. l_in= p_cont(*elem) ;
  117. file_final(l_in) ;
  118. freecore((char *)l_in) ;
  119. }
  120. l_clear(&phase->t_inputs) ;
  121. }
  122. rmfile(file) path *file ; {
  123. /* Remove a file, do not complain when is does not exist */
  124. if ( file->p_path ) {
  125. if ( t_flag<=1 ) unlink(file->p_path) ;
  126. if ( file->p_keeps ) throws(file->p_path) ;
  127. file->p_path= (char *)0 ;
  128. file->p_keeps=NO ;
  129. file->p_keep=NO ;
  130. }
  131. }
  132. rmtemps() {
  133. /* Called in case of disaster, always remove the current output file!
  134. */
  135. register list_elem *elem ;
  136. if ( t_flag>1 ) return ;
  137. rmfile(&out) ;
  138. file_final(&in) ;
  139. scanlist(l_first(tr_list),elem) {
  140. if ( t_cont(*elem)->t_combine && t_cont(*elem)->t_do ) {
  141. disc_inputs(t_cont(*elem)) ;
  142. }
  143. }
  144. }
  145. add_input(file,phase) path *file ; trf *phase ; {
  146. register path *store ;
  147. #ifdef DEBUG
  148. if ( debug ) {
  149. vprint("Adding %s to inputs of %s\n",
  150. file->p_path,phase->t_name) ;
  151. }
  152. #endif
  153. phase->t_do=YES ;
  154. if ( !phase->t_origname && orig.p_path[0]!='-' ) {
  155. /* This entry decides the name of the result */
  156. phase->t_origname= orig.p_path ;
  157. }
  158. store= (path *) getcore(sizeof (path)) ;
  159. *store = *file ;
  160. l_add(&phase->t_inputs,(char *)store) ;
  161. /* The task of getting rid of the string is passed to 'phase',
  162. as is the task to get rid of the file itself.
  163. */
  164. file->p_keeps=NO ; file->p_keep=YES ;
  165. }