Newer
Older
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
/****************************************************************************
* tools/mksyscall.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
/****************************************************************************
* Definitions
****************************************************************************/
#define LINESIZE (PATH_MAX > 256 ? PATH_MAX : 256)
#define MAX_FIELDS 16
#define MAX_PARMSIZE 128
#define NAME_INDEX 0
#define HEADER_INDEX 1
#define RETTYPE_INDEX 2
#define PARM1_INDEX 3
/****************************************************************************
* Private Data
****************************************************************************/
static bool g_debug;
static char g_line[LINESIZE+1];
static char g_parm[MAX_FIELDS][MAX_PARMSIZE];
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
/****************************************************************************
* Private Functions
****************************************************************************/
static char *skip_space(char *ptr)
{
while (*ptr && isspace(*ptr)) ptr++;
return ptr;
}
static char *read_line(FILE *stream)
{
char *ptr;
for (;;)
{
g_line[LINESIZE] = '\0';
if (!fgets(g_line, LINESIZE, stream))
{
return NULL;
}
else
{
if (g_debug)
{
printf("Line: %s\n", g_line);
}
ptr = skip_space(g_line);
if (*ptr && *ptr != '#' && *ptr != '\n')
{
return ptr;
}
}
}
}
static char *copy_parm(char *src, char *dest)
{
char *start = src;
int i;
for (i = 0; i < MAX_PARMSIZE; i++)
{
if (*src == '"')
{
*dest = '\0';
return src;
}
else if (*src == '\n' || *src == '\0')
{
fprintf(stderr, "Unexpected end of line: \"%s\"\n", start);
exit(4);
}
else
{
*dest++ = *src++;
}
}
fprintf(stderr, "Parameter too long: \"%s\"\n", start);
exit(3);
}
static char *find_parm(char *ptr)
{
char *start = ptr;
if (*ptr != '"')
{
fprintf(stderr, "I'm confused: \"%s\"\n", start);
exit(5);
}
ptr++;
ptr = skip_space(ptr);
if (*ptr == '\n' || *ptr == '\0')
{
return NULL;
}
else if (*ptr != ',')
{
fprintf(stderr, "Expected ',': \"%s\"\n", start);
exit(6);
}
ptr++;
ptr = skip_space(ptr);
if (*ptr != '"')
{
fprintf(stderr, "Expected \": \"%s\"\n", start);
exit(7);
}
ptr++;
return ptr;
}
static int parse_csvline(char *ptr)
{
int nparms;
int i;
/* Format "arg1","arg2","arg3",... Spaces will be tolerated outside of the
* quotes. Any initial spaces have already been skipped so the first thing
* should be '"'.
*/
if (*ptr != '"')
{
fprintf(stderr, "Bad line: \"%s\"\n", g_line);
exit(2);
}
ptr++;
nparms = 0;
do
{
ptr = copy_parm(ptr, &g_parm[nparms][0]);
nparms++;
ptr = find_parm(ptr);
}
while (ptr);
if (g_debug)
{
printf("Parameters: %d\n", nparms);
for (i = 0; i < nparms; i++)
{
printf(" Parm%d: \"%s\"\n", i+1, g_parm[i]);
}
}
return nparms;
}
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
static bool is_vararg(const char *type, int index, int nparms)
{
if (strcmp(type,"...") == 0)
{
if (index != (nparms-1))
{
fprintf(stderr, "... is not the last in the argument list\n");
exit(11);
}
return true;
}
return false;
}
static bool is_union(const char *type)
{
return (strncmp(type,"union", 5) == 0);
}
static const char *check_funcptr(const char *type)
{
const char *str = strstr(type,"(*)");
if (str)
{
return str + 2;
}
return NULL;
}
static const char *check_array(const char *type)
{
const char *str = strchr(type, '[');
if (str)
{
return str;
}
return NULL;
}
static void print_formalparm(FILE *stream, const char *argtype, int parmno)
{
const char *part2;
int len;
/* Function pointers and array formal parameter types are a little more work */
if ((part2 = check_funcptr(argtype)) != NULL || (part2 = check_array(argtype)) != NULL)
{
len = part2 - argtype;
(void)fwrite(argtype, 1, len, stream);
fprintf(stream, "parm%d%s", parmno, part2);
}
else
{
fprintf(stream, "%s parm%d", argtype, parmno);
}
}
static void get_formalparmtype(const char *arg, char *formal)
{
char *ptr = strchr(arg,'|');
if (ptr)
{
/* The formal parm type is a pointer to everything up to the '|' */
while (*arg != '|')
{
*formal++ = *arg++;
}
*formal = '\0';
}
else
{
strncpy(formal, arg, MAX_PARMSIZE);
}
}
static void get_actualparmtype(const char *arg, char *actual)
{
char *ptr = strchr(arg,'|');
if (ptr)
{
ptr++;
strncpy(actual, ptr, MAX_PARMSIZE);
}
else
{
strncpy(actual, arg, MAX_PARMSIZE);
}
}
snprintf(filename, MAX_PARMSIZE+9, "PROXY_%s.c", g_parm[NAME_INDEX]);
filename[MAX_PARMSIZE+9] = '\0';
stream = fopen(filename, "w");
if (stream == NULL)
{
fprintf(stderr, "Failed to open %s: %s\n", filename, strerror(errno));
exit(10);
}
return stream;
}
static void generate_proxy(int nparms)
{
FILE *stream = open_proxy();
/* Generate "up-front" information, include correct header files */
fprintf(stream, "/* Auto-generated %s proxy file -- do not edit */\n\n", g_parm[NAME_INDEX]);
fprintf(stream, "#include <%s>\n", g_parm[HEADER_INDEX]);
fprintf(stream, "#include <syscall.h>\n\n");
/* Generate the function definition that matches standard function prototype */
fprintf(stream, "%s %s(", g_parm[RETTYPE_INDEX], g_parm[NAME_INDEX]);
if (nparms <= 0)
{
fprintf(stream, "void");
}
else
{
for (i = 0; i < nparms; i++)
{
get_formalparmtype(g_parm[PARM1_INDEX+i], formal);
/* Generate the system call. Functions that do not return or return void are special cases */
if (strcmp(g_parm[RETTYPE_INDEX], "void") == 0)
{
fprintf(stream, " (void)sys_call%d(", nparms);
}
else
{
fprintf(stream, " return (%s)sys_call%d(", g_parm[RETTYPE_INDEX], nparms);
}
/* Create the parameter list with the matching types. The first parametr is always the syscall number. */
fprintf(stream, "(unsigned int)SYS_%s", g_parm[NAME_INDEX]);
fprintf(stream, ", (uintptr_t)parm%d", i+1);
/* Handle the tail end of the function. */
fprintf(stream, ");\n}\n");
fclose(stream);
}
static FILE *open_stub(void)
{
if (g_inline)
{
if (!g_stubstream)
{
g_stubstream = fopen("STUB.h", "w");
if (g_stubstream == NULL)
{
fprintf(stderr, "Failed to open STUB.h: %s\n", strerror(errno));
exit(9);
}
}
return g_stubstream;
}
else
{
char filename[MAX_PARMSIZE+8];
FILE *stream;
snprintf(filename, MAX_PARMSIZE+7, "STUB_%s.c", g_parm[NAME_INDEX]);
filename[MAX_PARMSIZE+7] = '\0';
stream = fopen(filename, "w");
if (stream == NULL)
{
fprintf(stderr, "Failed to open %s: %s\n", filename, strerror(errno));
exit(9);
}
return stream;
}
}
static void stub_close(FILE *stream)
{
if (!g_inline)
}
}
static void generate_stub(int nparms)
{
FILE *stream = open_stub();
char formal[MAX_PARMSIZE];
char actual[MAX_PARMSIZE];
int j;
/* Generate "up-front" information, include correct header files */
fprintf(stream, "/* Auto-generated %s stub file -- do not edit */\n\n", g_parm[0]);
fprintf(stream, "#include <stdint.h>\n");
fprintf(stream, "#include <%s>\n\n", g_parm[HEADER_INDEX]);
/* Generate the function definition that matches standard function prototype */
fprintf(stream, "uintptr_t STUB_%s(", g_parm[NAME_INDEX]);
/* Generate the formal parameter list. A function received no parameters is a special case. */
if (nparms <= 0)
{
fprintf(stream, "void");
}
else
{
for (i = 0; i < nparms; i++)
{
/* Treat the first argument in the list differently from the others..
* It does not need a comma before it.
*/
/* Check for a variable number of arguments */
if (is_vararg(g_parm[PARM1_INDEX+i], i, nparms))
{
/* Always receive six arguments in this case */
for (j = i+1; j <=6; j++)
{
fprintf(stream, ", uintptr_t parm%d", j);
}
}
else
{
fprintf(stream, ", uintptr_t parm%d", i+1);
}
}
else
{
fprintf(stream, "uintptr_t parm%d", i+1);
}
}
}
fprintf(stream, ")\n{\n");
/* Then call the proxied function. Functions that have no return value are
* a special case.
*/
if (strcmp(g_parm[RETTYPE_INDEX], "void") == 0)
{
fprintf(stream, " %s(", g_parm[NAME_INDEX]);
}
else
{
fprintf(stream, " return (uintptr_t)%s(", g_parm[NAME_INDEX]);
}
/* The pass all of the system call parameters, casting to the correct type
* as necessary.
*/
/* Get the formal type of the parameter, and get the type that we
* actually have to cast to. For example for a formal type like 'int parm[]'
* we have to cast the actual parameter to 'int*'. The worst is a union
* type like 'union sigval' where we have to cast to (union sigval)((FAR void *)parm)
* -- Yech.
*/
get_formalparmtype(g_parm[PARM1_INDEX+i], formal);
get_actualparmtype(g_parm[PARM1_INDEX+i], actual);
/* Treat the first argument in the list differently from the others..
* It does not need a comma before it.
*/
/* Check for a variable number of arguments */
if (is_vararg(actual, i, nparms))
{
/* Always pass six arguments */
for (j = i+1; j <=6; j++)
{
fprintf(stream, ", parm%d", j);
}
}
else
{
if (is_union(formal))
{
fprintf(stream, ", (%s)((%s)parm%d)", formal, actual, i+1);
}
else
{
fprintf(stream, ", (%s)parm%d", actual, i+1);
}
}
if (is_union(formal))
{
fprintf(stream, "(%s)((%s)parm%d)", formal, actual, i+1);
}
else
{
fprintf(stream, "(%s)parm%d",actual, i+1);
}
/* Tail end of the function. The the proxied function has no return
* value, just return zero (OK).
*/
if (strcmp(g_parm[RETTYPE_INDEX], "void") == 0)
{
fprintf(stream, ");\n return 0;\n}\n");
}
else
{
fprintf(stream, ");\n}\n");
}
stub_close(stream);
}
static void show_usage(const char *progname)
{
fprintf(stderr, "USAGE: %s [-p|s|i] <CSV file>\n\n", progname);
fprintf(stderr, "Where:\n\n");
fprintf(stderr, "\t-p : Generate proxies\n");
fprintf(stderr, "\t-s : Generate stubs\n");
fprintf(stderr, "\t-i : Generate proxies as static inline functions\n");
exit(1);
}
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, char **argv, char **envp)
{
char *csvpath;
bool proxies = false;
FILE *stream;
char *ptr;
int ch;
/* Parse command line options */
g_debug = false;
while ((ch = getopt(argc, argv, ":dps")) > 0)
{
switch (ch)
{
case 'd' :
g_debug = true;
break;
case 'p' :
proxies = true;
break;
case 's' :
proxies = false;
break;
case 'i' :
g_inline = true;
break;
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
case '?' :
fprintf(stderr, "Unrecognized option: %c\n", optopt);
show_usage(argv[0]);
case ':' :
fprintf(stderr, "Missing option argument, option: %c\n", optopt);
show_usage(argv[0]);
break;
fprintf(stderr, "Unexpected option: %c\n", ch);
show_usage(argv[0]);
}
}
if (optind >= argc)
{
fprintf(stderr, "Missing <CSV file>\n");
show_usage(argv[0]);
}
csvpath = argv[optind];
if (++optind < argc)
{
fprintf(stderr, "Unexpected garbage at the end of the line\n");
show_usage(argv[0]);
}
/* Open the CSV file */
stream= fopen(csvpath, "r");
if (!stream)
{
fprintf(stderr, "open %s failed: %s\n", csvpath, strerror(errno));
exit(3);
}
/* Process each line in the CVS file */
while ((ptr = read_line(stream)) != NULL)
{
/* Parse the line from the CVS file */
int nargs = parse_csvline(ptr);
if (nargs < 3)
{
fprintf(stderr, "Only %d arguments found: %s\n", nargs, g_line);
exit(8);
}
if (proxies)
{
generate_proxy(nargs-3);
}
else
{
if (g_stubstream != NULL)
{
fclose(g_stubstream);
}