forked from calebccff/qmic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqmic.c
177 lines (148 loc) · 3.35 KB
/
qmic.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
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include "list.h"
#include "qmic.h"
FILE *sourcefile;
const char *sz_simple_types[] = {
[TYPE_U8] = "uint8_t",
[TYPE_U16] = "uint16_t",
[TYPE_U32] = "uint32_t",
[TYPE_U64] = "uint64_t",
[TYPE_STRING] = "char *",
};
void qmi_const_header(FILE *fp)
{
struct qmi_const *qc;
if (list_empty(&qmi_consts))
return;
list_for_each_entry(qc, &qmi_consts, node)
fprintf(fp, "#define %s %lld\n", qc->name, qc->value);
fprintf(fp, "\n");
}
void qmi_enum_header(FILE *fp)
{
struct qmi_enum *qe;
struct qmi_const *qc;
if (list_empty(&qmi_enums))
return;
list_for_each_entry(qe, &qmi_enums, node) {
fprintf(fp, "enum %s {\n", qe->name);
list_for_each_entry(qc, &qe->members, node) {
fprintf(fp, "\t%s = %lld,\n", qc->name, qc->value);
}
fprintf(fp, "};\n\n");
}
fprintf(fp, "\n");
}
void emit_source_includes(FILE *fp, const char *package)
{
fprintf(fp, "#include <errno.h>\n"
"#include <string.h>\n"
"#include \"qmi_%1$s.h\"\n\n",
package);
}
void guard_header(FILE *fp, const char *package)
{
char *upper;
char *p;
upper = p = strdup(package);
while (*p) {
*p = toupper(*p);
p++;
}
fprintf(fp, "#ifndef __QMI_%s_H__\n", upper);
fprintf(fp, "#define __QMI_%s_H__\n", upper);
fprintf(fp, "\n");
}
void guard_footer(FILE *fp)
{
fprintf(fp, "#endif\n");
}
static void usage(void)
{
extern const char *__progname;
fprintf(stderr, "Usage: %s [-ak] [-f FILE] [-o dir]\n", __progname);
fprintf(stderr, " -a Emit accessor style sources for use with qmi_tlv\n");
fprintf(stderr, " -k Emit kernel style sources\n");
fprintf(stderr, " -f FILE Read from file (defaults to stdin)\n");
fprintf(stderr, " -o DIR Output directory to write to\n");
exit(1);
}
int main(int argc, char **argv)
{
char fname[256];
const char* source = NULL;
const char* outdir = NULL;
struct stat sb;
FILE *hfp;
FILE *sfp;
int method = 0;
int opt;
while ((opt = getopt(argc, argv, "akf:o:")) != -1) {
switch (opt) {
case 'a':
method = 0;
break;
case 'k':
method = 1;
break;
case 'f':
source = optarg;
break;
case 'o':
outdir = optarg;
break;
default:
usage();
}
}
if (source) {
sourcefile = fopen(source, "r");
if (!sourcefile) {
fprintf(stderr, "Failed to open '%s' (%d: %s)\n", source,
errno, strerror(errno));
return EXIT_FAILURE;
}
} else {
sourcefile = stdin;
}
if (outdir && !(stat(outdir, &sb) == 0 && S_ISDIR(sb.st_mode))) {
fprintf(stderr, "Specified output directory '%s' either doesn't"
" exist or isn't a directory\n", outdir);
return EXIT_FAILURE;
}
if (!outdir)
outdir = ".";
qmi_parse();
snprintf(fname, sizeof(fname), "%s/qmi_%s.c", outdir, qmi_package.name);
sfp = fopen(fname, "w");
if (!sfp)
err(1, "failed to open %s", fname);
snprintf(fname, sizeof(fname), "%s/qmi_%s.h", outdir, qmi_package.name);
hfp = fopen(fname, "w");
if (!hfp)
err(1, "failed to open %s", fname);
switch (method) {
case 0:
accessor_emit_c(sfp, qmi_package.name);
accessor_emit_h(hfp, qmi_package.name);
break;
case 1:
kernel_emit_c(sfp);
kernel_emit_h(hfp);
break;
}
fclose(hfp);
fclose(sfp);
return 0;
}