files.c 4.0 KB

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