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