-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathqmic.c
121 lines (100 loc) · 2.01 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
#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 "list.h"
#include "qmic.h"
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 %llu\n", qc->name, qc->value);
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]\n", __progname);
exit(1);
}
int main(int argc, char **argv)
{
char fname[256];
FILE *hfp;
FILE *sfp;
int method = 0;
int opt;
while ((opt = getopt(argc, argv, "ak")) != -1) {
switch (opt) {
case 'a':
method = 0;
break;
case 'k':
method = 1;
break;
default:
usage();
}
}
qmi_parse();
snprintf(fname, sizeof(fname), "qmi_%s.c", qmi_package);
sfp = fopen(fname, "w");
if (!sfp)
err(1, "failed to open %s", fname);
snprintf(fname, sizeof(fname), "qmi_%s.h", qmi_package);
hfp = fopen(fname, "w");
if (!hfp)
err(1, "failed to open %s", fname);
switch (method) {
case 0:
accessor_emit_c(sfp, qmi_package);
accessor_emit_h(hfp, qmi_package);
break;
case 1:
kernel_emit_c(sfp, qmi_package);
kernel_emit_h(hfp, qmi_package);
break;
}
fclose(hfp);
fclose(sfp);
return 0;
}