files.c 4.1 KB

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