-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathctang-output.ch
More file actions
193 lines (164 loc) · 6.41 KB
/
ctang-output.ch
File metadata and controls
193 lines (164 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
Changes for CTANGLE.W by Andreas Scherer, October 2021.
This set of changes modifies the output behaviour of the CWEB system.
Instead of writing directly to the C or TeX file as described in the
manual, the current run is documented in a temporary output file which is
copied to the expected file in the last moment. In case of an user abort,
previous results are not destroyed.
This change file requires CTANG-PATCH.CH to be applied as well.
For a complete history of the changes made to CTANGLE.W see CTANG-PATCH.CH.
Section 50.
@x l.556
@<Write all the named output files@>=
for (an_output_file=end_output_files; an_output_file>cur_out_file;) {
an_output_file--;
sprint_section_name(output_file_name,*an_output_file);
fclose(C_file);
if ((C_file=fopen(output_file_name,"wb"))==NULL)
fatal("! Cannot open output file ",output_file_name);
@.Cannot open output file@>
if (show_progress) { printf("\n(%s)",output_file_name); update_terminal(); }
cur_line=1;
@<Initialize the secondary output@>@;
@<Output material...@>@;
}
@y
@<Write all the named output files@>=
if (check_for_change) {
fclose(C_file); C_file=NULL;
@<Update the primary result when it has changed@>@;
}
for (an_output_file=end_output_files; an_output_file>cur_out_file;) {
an_output_file--;
sprint_section_name(output_file_name,*an_output_file);
if (check_for_change) @<Open the intermediate output file@>@;
else {
fclose(C_file);
if ((C_file=fopen(output_file_name,"wb"))==NULL)
fatal("! Cannot open output file ",output_file_name);
@.Cannot open output file@>
}
if (show_progress) { printf("\n(%s)",output_file_name); update_terminal(); }
cur_line=1;
@<Initialize the secondary output@>@;
@<Output material...@>@;
if (check_for_change) {
fclose(C_file); C_file=NULL;
@<Update the secondary results when they have changed@>@;
}
}
if (check_for_change)
strcpy(check_file_name,""); /* We want to get rid of the temporary file */
@z
Additional material.
@x l.1551
@** Index.
@y
@* Output file update. Most \CEE/ projects are controlled by a \.{Makefile}
that automatically takes care of the temporal dependencies between the different
source modules. It may be convenient that \.{CWEB} doesn't create new output
for all existing files, when there are only changes to some of them. Thus the
\.{make} process will only recompile those modules where necessary. You can
activate this feature with the `\.{+c}' command-line option. The idea and basic
implementation of this mechanism can be found in the program \.{NUWEB} by
Preston Briggs, to whom credit is due.
@<Open the intermediate output file@>= {
if ((C_file=fopen(output_file_name,"a"))==NULL)
fatal("! Cannot open output file ",output_file_name);
@.Cannot open output file@>
else fclose(C_file); /* Test accessability */
if((C_file=fopen(check_file_name,"wb"))==NULL)
fatal("! Cannot open output file ",check_file_name);
}
@ @<Update the primary result...@>=
if((C_file=fopen(C_file_name,"r"))!=NULL) {
@<Set up the comparison of temporary output@>@;
@<Create the primary output depending on the comparison@>@;
} else
rename(check_file_name,C_file_name); /* This was the first run */
@ @<Set up the comparison of temporary output@>=
bool comparison=false;
if((check_file=fopen(check_file_name,"r"))==NULL)
fatal("! Cannot open output file ",check_file_name);
@.Cannot open output file@>
@<Compare the temporary output...@>@;
fclose(C_file); C_file=NULL;
fclose(check_file); check_file=NULL;
@ We hope that this runs fast on most systems.
@<Compare the temporary output to the previous output@>=
do {
char x[BUFSIZ],y[BUFSIZ];
int x_size = fread(x,sizeof(char),BUFSIZ,C_file);
int y_size = fread(y,sizeof(char),BUFSIZ,check_file);
comparison = (x_size == y_size) && !memcmp(x,y,x_size);
} while(comparison && !feof(C_file) && !feof(check_file));
@ Note the superfluous call to |remove| before |rename|. We're using it to
get around a bug in some implementations of |rename|.@^system dependencies@>
@<Create the primary output...@>=
if(comparison)
remove(check_file_name); /* The output remains untouched */
else {
remove(C_file_name);
rename(check_file_name,C_file_name);
}
@ The author of a \.{CWEB} program may want to write the \\{secondary} output
instead of to a file (in \.{@@(...@@>}) to \.{/dev/null} or \.{/dev/stdout} or
\.{/dev/stderr}. We must take care of the \\{temporary} output already written
to a file and finally get rid of that file.
@<Update the secondary results...@>=
if(0==strcmp("/dev/stdout",output_file_name))@/
@<Redirect temporary output to \.{/dev/stdout}@>@;
else if(0==strcmp("/dev/stderr",output_file_name))@/
@<Redirect temporary output to \.{/dev/stderr}@>@;
else if(0==strcmp("/dev/null",output_file_name))@/
@<Redirect temporary output to \.{/dev/null}@>@;
else { /* Hopefully a regular output file */
if((C_file=fopen(output_file_name,"r"))!=NULL) {
@<Set up the comparison of temporary output@>@;
@<Create the secondary output depending on the comparison@>@;
} else
rename(check_file_name,output_file_name); /* This was the first run */
}
@ Again, we use a call to |remove| before |rename|.
@<Create the secondary output...@>=
if(comparison)
remove(check_file_name); /* The output remains untouched */
else {
remove(output_file_name);
rename(check_file_name,output_file_name);
}
@ Copy secondary output to |stdout|.
@<Redirect temporary output to \.{/dev/stdout}@>={
@<Setup system redirection@>@;
do {
in_size = fread(in_buf,sizeof(char),BUFSIZ,check_file);
in_buf[in_size]='\0';
fprintf(stdout,"%s",in_buf);
} while(!feof(check_file));@/
fclose(check_file); check_file=NULL;
@<Create the secondary output...@>@;
}
@ Copy secondary output to |stderr|.
@<Redirect temporary output to \.{/dev/stderr}@>={
@<Setup system redirection@>@;
do {
in_size = fread(in_buf,sizeof(char),BUFSIZ,check_file);
in_buf[in_size]='\0';
fprintf(stderr,"%s",in_buf);
} while(!feof(check_file));@/
fclose(check_file); check_file=NULL;
@<Create the secondary output...@>@;
}
@ No copying necessary, just remove the temporary output file.
@<Redirect temporary output to \.{/dev/null}@>={
bool comparison=true;
@<Create the secondary output...@>@;
}
@ @<Setup system redirection@>=
char in_buf[BUFSIZ+1];
int in_size;
bool comparison=true;
if((check_file=fopen(check_file_name,"r"))==NULL)
fatal("! Cannot open output file ",check_file_name);
@.Cannot open output file@>
@** Index.
@z