forked from nickanderson/copbl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_library.c
308 lines (240 loc) · 6.36 KB
/
build_library.c
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*****************************************************************************/
/* */
/* File: build_procedures.c */
/* */
/* Created: Fri Dec 4 18:30:52 2009 */
/* */
/*****************************************************************************/
#define true 1
#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct Item
{
char done;
char *name;
char *classes;
int counter;
struct Item *next;
};
void PrependItem(struct Item **liststart,char *itemstring,char *classes);
struct Item *SortItemListNames(struct Item *list);
main()
{ FILE *fin,*fout = NULL;
struct Item *ip,*contents = NULL;
char buffer[1024],type[1024],control[1024],data[1024],name[1024];
if ((fin = fopen("cfengine_stdlib.cf","r")) == NULL)
{
printf("Could not open the cfengine_stdlib.cf file\n");
return;
}
while(!feof(fin))
{
buffer[0] = '\0';
control[0] = '\0';
data[0] = '\0';
type[0] = '\0';
fgets(buffer,1023,fin);
if (strncmp(buffer,"##",2) == 0)
{
continue;
}
sscanf(buffer,"%s %s %[^(\n]",type,data,control);
if (strncmp(type,"body",4) == 0 || strncmp(type,"bundle",6) == 0)
{
if (fout)
{
fclose(fout);
fout = NULL;
}
else
{
control[0] = '\0';
data[0] = '\0';
type[0] = '\0';
}
snprintf(name,1024,"%s_%s_%s.tmp",type,data,control);
if ((fout = fopen(name,"w")) == NULL)
{
printf("Could not open output file %s\n",name);
return;
}
fprintf(fout,"%s",buffer);
PrependItem(&contents,buffer,"");
}
else if (fout)
{
fprintf(fout,"%s",buffer);
}
else
{
printf("XXXX %s",buffer);
}
}
if (fout)
{
fclose(fout);
fout = NULL;
}
if ((fout = fopen("stdlib.texinfo","w")) == NULL)
{
printf("Could not open the stdlib.texinfo file\n");
return;
}
IncludeManualFile(fout,"preamble.texinfo");
contents = SortItemListNames(contents);
for (ip = contents; ip != NULL; ip=ip->next)
{
sscanf(ip->name,"%s %s %[^(\n]",type,data,control);
fprintf(fout,"@node %s %s %s\n",type,data,control);
fprintf(fout,"@section %s\n",ip->name);
fprintf(fout,"@verbatim\n");
snprintf(name,1024,"%s_%s_%s.tmp",type,data,control);
IncludeManualFile(fout,name);
fprintf(fout,"@end verbatim\n\n");
}
IncludeManualFile(fout,"postamble.texinfo");
fclose(fin);
fclose(fout);
}
/*****************************************************************************/
/*****************************************************************************/
int IncludeManualFile(FILE *fout,char *file)
{ char buffer[1024],filename[1024];
FILE *fp;
if ((fp = fopen(file,"r")) == NULL)
{
printf("Could not read %s\n",file);
return;
}
while(!feof(fp))
{
buffer[0] = '\0';
fgets(buffer,1023,fp);
fprintf(fout,"%s",buffer);
}
fclose(fp);
}
/* CODE FROM CFENGINE .....*/
/*********************************************************************/
void PrependItem(struct Item **liststart,char *itemstring,char *classes)
{ struct Item *ip;
char *sp,*spe = NULL;
if ((ip = (struct Item *)malloc(sizeof(struct Item))) == NULL)
{
return;
}
if ((sp = malloc(strlen(itemstring)+2)) == NULL)
{
return;
}
if ((classes != NULL) && (spe = malloc(strlen(classes)+2)) == NULL)
{
return;
}
strcpy(sp,itemstring);
ip->name = sp;
ip->next = *liststart;
ip->counter = 0;
*liststart = ip;
if (classes != NULL)
{
strcpy(spe,classes);
ip->classes = spe;
}
else
{
ip->classes = NULL;
}
}
/*******************************************************************/
/* Borrowed this algorithm from merge-sort implementation */
struct Item *SortItemListNames(struct Item *list) /* Alphabetical */
{ struct Item *p, *q, *e, *tail, *oldhead;
int insize, nmerges, psize, qsize, i;
if (list == NULL)
{
return NULL;
}
insize = 1;
while (true)
{
p = list;
oldhead = list; /* only used for circular linkage */
list = NULL;
tail = NULL;
nmerges = 0; /* count number of merges we do in this pass */
while (p)
{
nmerges++; /* there exists a merge to be done */
/* step `insize' places along from p */
q = p;
psize = 0;
for (i = 0; i < insize; i++)
{
psize++;
q = q->next;
if (!q)
{
break;
}
}
/* if q hasn't fallen off end, we have two lists to merge */
qsize = insize;
/* now we have two lists; merge them */
while (psize > 0 || (qsize > 0 && q))
{
/* decide whether next element of merge comes from p or q */
if (psize == 0)
{
/* p is empty; e must come from q. */
e = q;
q = q->next;
qsize--;
}
else if (qsize == 0 || !q)
{
/* q is empty; e must come from p. */
e = p;
p = p->next;
psize--;
}
else if (strcmp(p->name, q->name) <= 0)
{
/* First element of p is lower (or same);
* e must come from p. */
e = p;
p = p->next;
psize--;
}
else
{
/* First element of q is lower; e must come from q. */
e = q;
q = q->next;
qsize--;
}
/* add the next element to the merged list */
if (tail)
{
tail->next = e;
}
else
{
list = e;
}
tail = e;
}
/* now p has stepped `insize' places along, and q has too */
p = q;
}
tail->next = NULL;
/* If we have done only one merge, we're finished. */
if (nmerges <= 1) /* allow for nmerges==0, the empty list case */
{
return list;
}
/* Otherwise repeat, merging lists twice the size */
insize *= 2;
}
}