forked from mailru/confetti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
confetti.c
159 lines (130 loc) · 2.77 KB
/
confetti.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
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <prscfl.h>
static void
usage() {
fputs(
"Copyright (c) 2010 Teodor Sigaev <[email protected]>. All rights reserved.\n"
"Usage:\n"
"confetti -i INPUTFILE [-D] (-c COUTFILE -n NAME | -h HOUTFILE -n NAME | -f CFGOUTFILE | -p PARSEROUTFILE | -H HPARSEOUTFILE)\n",
stdout
);
exit(1);
}
extern char *optarg;
extern int opterr;
int
main(int argc, char* argv[]) {
char *ifn = NULL, *cfn = NULL, *hfn = NULL, *cfgfn = NULL, *pfn = NULL, *Hfn = NULL;
FILE *fh;
char *name = NULL;
int i, debug = 0;;
ParamDef *def;
opterr=0;
while((i=getopt(argc,argv,"i:c:h:f:n:p:H:D")) != EOF) {
switch(i) {
case 'i':
ifn = strdup(optarg);
break;
case 'c':
cfn = strdup(optarg);
break;
case 'h':
hfn = strdup(optarg);
break;
case 'f':
cfgfn = strdup(optarg);
break;
case 'p':
pfn = strdup(optarg);
break;
case 'H':
Hfn = strdup(optarg);
break;
case 'n':
name = strdup(optarg);
break;
case 'D':
debug = 1;
break;
default:
usage();
}
}
if ( !ifn )
usage();
if ( (fh = fopen(ifn, "r")) == NULL ) {
fprintf(stderr, "Could not open file '%s': %s\n", ifn, strerror(errno));
exit(1);
}
if ( (def = parseCfgDef(fh)) == NULL ) {
fprintf(stderr, "Could not parse file '%s'\n", ifn);
exit(1);
}
fclose(fh);
if (debug)
dDump(def);
if ( hfn ) {
if (!name)
usage();
if (strcmp(hfn, "-") == 0) {
fh = stdout;
} else if ((fh = fopen(hfn, "w")) == NULL ) {
fprintf(stderr, "Could not open file '%s': %s\n", hfn, strerror(errno));
exit(1);
}
hDump(fh, name, def);
if (fh != stdout)
fclose(fh);
}
if ( cfn ) {
if (!name)
usage();
if (strcmp(cfn, "-") == 0) {
fh = stdout;
} else if ((fh = fopen(cfn, "w")) == NULL ) {
fprintf(stderr, "Could not open file '%s': %s\n", cfn, strerror(errno));
exit(1);
}
cDump(fh, name, def);
if (fh != stdout)
fclose(fh);
}
if ( cfgfn ) {
if (strcmp(cfgfn, "-") == 0) {
fh = stdout;
} else if ((fh = fopen(cfgfn, "w")) == NULL ) {
fprintf(stderr, "Could not open file '%s': %s\n", cfgfn, strerror(errno));
exit(1);
}
fDump(fh, def);
if (fh != stdout)
fclose(fh);
}
if ( pfn ) {
if (strcmp(pfn, "-") == 0) {
fh = stdout;
} else if ((fh = fopen(pfn, "w")) == NULL ) {
fprintf(stderr, "Could not open file '%s': %s\n", pfn, strerror(errno));
exit(1);
}
pDump(fh, def);
if (fh != stdout)
fclose(fh);
}
if ( Hfn ) {
if (strcmp(Hfn, "-") == 0) {
fh = stdout;
} else if ((fh = fopen(Hfn, "w")) == NULL ) {
fprintf(stderr, "Could not open file '%s': %s\n", Hfn, strerror(errno));
exit(1);
}
HDump(fh);
if (fh != stdout)
fclose(fh);
}
exit(0);
}