2011-03-31 06:23:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
2011-04-02 01:07:51 +02:00
|
|
|
#define COND_INDEX 2
|
|
|
|
#define RETTYPE_INDEX 3
|
|
|
|
#define PARM1_INDEX 4
|
2011-03-31 06:23:17 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool g_debug;
|
2011-04-01 23:36:17 +02:00
|
|
|
static bool g_inline;
|
2011-03-31 06:23:17 +02:00
|
|
|
static char g_line[LINESIZE+1];
|
|
|
|
static char g_parm[MAX_FIELDS][MAX_PARMSIZE];
|
2011-04-01 23:36:17 +02:00
|
|
|
static FILE *g_stubstream;
|
2011-04-02 01:07:51 +02:00
|
|
|
static int g_lineno;
|
2011-03-31 06:23:17 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
|
{
|
2011-04-02 01:07:51 +02:00
|
|
|
g_lineno++;
|
2011-03-31 06:23:17 +02:00
|
|
|
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')
|
|
|
|
{
|
2011-04-02 01:07:51 +02:00
|
|
|
fprintf(stderr, "%d: Unexpected end of line: \"%s\"\n", g_lineno, start);
|
2011-03-31 06:23:17 +02:00
|
|
|
exit(4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*dest++ = *src++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-02 01:07:51 +02:00
|
|
|
fprintf(stderr, "%d: Parameter too long: \"%s\"\n", g_lineno, start);
|
2011-03-31 06:23:17 +02:00
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *find_parm(char *ptr)
|
|
|
|
{
|
|
|
|
char *start = ptr;
|
|
|
|
|
|
|
|
if (*ptr != '"')
|
|
|
|
{
|
2011-04-02 01:07:51 +02:00
|
|
|
fprintf(stderr, "%d: I'm confused: \"%s\"\n", g_lineno, start);
|
2011-03-31 06:23:17 +02:00
|
|
|
exit(5);
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
ptr = skip_space(ptr);
|
|
|
|
if (*ptr == '\n' || *ptr == '\0')
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (*ptr != ',')
|
|
|
|
{
|
2011-04-02 01:07:51 +02:00
|
|
|
fprintf(stderr, "%d: Expected ',': \"%s\"\n", g_lineno, start);
|
2011-03-31 06:23:17 +02:00
|
|
|
exit(6);
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
ptr = skip_space(ptr);
|
|
|
|
if (*ptr != '"')
|
|
|
|
{
|
2011-04-02 01:07:51 +02:00
|
|
|
fprintf(stderr, "%d: Expected \": \"%s\"\n", g_lineno, start);
|
2011-03-31 06:23:17 +02:00
|
|
|
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 != '"')
|
|
|
|
{
|
2011-04-02 01:07:51 +02:00
|
|
|
fprintf(stderr, "%d: Bad line: \"%s\"\n", g_lineno, g_line);
|
2011-03-31 06:23:17 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-04-01 23:36:17 +02:00
|
|
|
static bool is_vararg(const char *type, int index, int nparms)
|
|
|
|
{
|
|
|
|
if (strcmp(type,"...") == 0)
|
|
|
|
{
|
|
|
|
if (index != (nparms-1))
|
|
|
|
{
|
2011-04-02 01:07:51 +02:00
|
|
|
fprintf(stderr, "%d: ... is not the last in the argument list\n", g_lineno);
|
2011-04-01 23:36:17 +02:00
|
|
|
exit(11);
|
|
|
|
}
|
2011-04-02 03:23:08 +02:00
|
|
|
else if (nparms < 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%d: Need one parameter before ...\n", g_lineno);
|
|
|
|
exit(14);
|
|
|
|
}
|
2011-04-01 23:36:17 +02:00
|
|
|
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)
|
|
|
|
{
|
2011-04-02 17:25:22 +02:00
|
|
|
/* The formal parm type is a pointer to everything up to the first'|' (or
|
|
|
|
* the end of the string if there is no '|' in the type description).
|
|
|
|
*/
|
2011-04-01 23:36:17 +02:00
|
|
|
|
2011-04-02 17:25:22 +02:00
|
|
|
while (*arg != '|' && *arg != '\0')
|
2011-04-01 23:36:17 +02:00
|
|
|
{
|
2011-04-02 17:25:22 +02:00
|
|
|
*formal++ = *arg++;
|
2011-04-01 23:36:17 +02:00
|
|
|
}
|
2011-04-02 17:25:22 +02:00
|
|
|
*formal = '\0';
|
2011-04-01 23:36:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void get_actualparmtype(const char *arg, char *actual)
|
|
|
|
{
|
2011-04-02 17:25:22 +02:00
|
|
|
const char *pstart = strchr(arg,'|');
|
|
|
|
if (pstart)
|
2011-04-01 23:36:17 +02:00
|
|
|
{
|
2011-04-02 17:25:22 +02:00
|
|
|
/* The actual parameter type starts after the '|' */
|
|
|
|
|
|
|
|
pstart++;
|
2011-04-01 23:36:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-02 17:25:22 +02:00
|
|
|
/* The actual parameter is the same as the formal parameter
|
|
|
|
* at starts at the beginning of the type string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pstart = arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The actual parm type is a pointer to everything up to the next '|' (or
|
|
|
|
* the end of the string if there is no '|' in the type description).
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (*pstart != '|' && *pstart != '\0')
|
|
|
|
{
|
|
|
|
*actual++ = *pstart++;
|
|
|
|
}
|
|
|
|
*actual = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_fieldname(const char *arg, char *fieldname)
|
|
|
|
{
|
|
|
|
char *pactual = strchr(arg,'|');
|
|
|
|
char *pstart;
|
|
|
|
|
|
|
|
if (pactual)
|
|
|
|
{
|
|
|
|
/* The actual parameter type starts after the '|' */
|
|
|
|
|
|
|
|
pactual++;
|
|
|
|
pstart = strchr(pactual,'|');
|
|
|
|
if (pstart)
|
|
|
|
{
|
|
|
|
/* The fieldname is everything past the second '|' to the end of the string */
|
|
|
|
|
|
|
|
pstart++;
|
|
|
|
strncpy(fieldname, pstart, MAX_PARMSIZE);
|
|
|
|
return;
|
|
|
|
}
|
2011-04-01 23:36:17 +02:00
|
|
|
}
|
2011-04-02 17:25:22 +02:00
|
|
|
|
|
|
|
fprintf(stderr, "%d: Missing union fieldname: %s\n", g_lineno, arg);
|
|
|
|
exit(15);
|
2011-04-01 23:36:17 +02:00
|
|
|
}
|
|
|
|
|
2011-03-31 06:23:17 +02:00
|
|
|
static FILE *open_proxy(void)
|
|
|
|
{
|
2011-04-01 23:36:17 +02:00
|
|
|
char filename[MAX_PARMSIZE+10];
|
2011-03-31 06:23:17 +02:00
|
|
|
FILE *stream;
|
|
|
|
|
2011-04-01 23:36:17 +02:00
|
|
|
snprintf(filename, MAX_PARMSIZE+9, "PROXY_%s.c", g_parm[NAME_INDEX]);
|
|
|
|
filename[MAX_PARMSIZE+9] = '\0';
|
2011-03-31 06:23:17 +02:00
|
|
|
|
|
|
|
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();
|
2011-04-01 23:36:17 +02:00
|
|
|
char formal[MAX_PARMSIZE];
|
2011-04-02 17:25:22 +02:00
|
|
|
char fieldname[MAX_PARMSIZE];
|
2011-04-02 03:23:08 +02:00
|
|
|
bool bvarargs = false;
|
|
|
|
int nformal;
|
|
|
|
int nactual;
|
2011-03-31 06:23:17 +02:00
|
|
|
int i;
|
|
|
|
|
2011-04-01 23:36:17 +02:00
|
|
|
/* Generate "up-front" information, include correct header files */
|
|
|
|
|
2011-03-31 06:23:17 +02:00
|
|
|
fprintf(stream, "/* Auto-generated %s proxy file -- do not edit */\n\n", g_parm[NAME_INDEX]);
|
2011-04-02 01:07:51 +02:00
|
|
|
fprintf(stream, "#include <nuttx/config.h>\n");
|
2011-04-02 03:23:08 +02:00
|
|
|
|
|
|
|
/* Does this function have a variable number of parameters? If so then the
|
|
|
|
* final parameter type will be encoded as "..."
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (is_vararg(g_parm[PARM1_INDEX+nparms-1], nparms-1, nparms))
|
|
|
|
{
|
|
|
|
nformal = nparms-1;
|
|
|
|
bvarargs = true;
|
|
|
|
fprintf(stream, "#include <stdarg.h>\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nformal = nparms;
|
|
|
|
}
|
|
|
|
|
2011-03-31 06:59:29 +02:00
|
|
|
fprintf(stream, "#include <%s>\n", g_parm[HEADER_INDEX]);
|
|
|
|
fprintf(stream, "#include <syscall.h>\n\n");
|
2011-03-31 06:23:17 +02:00
|
|
|
|
2011-04-02 01:07:51 +02:00
|
|
|
if (g_parm[COND_INDEX][0] != '\0')
|
|
|
|
{
|
|
|
|
fprintf(stream, "#if %s\n\n", g_parm[COND_INDEX]);
|
|
|
|
}
|
|
|
|
|
2011-04-01 23:36:17 +02:00
|
|
|
/* Generate the function definition that matches standard function prototype */
|
|
|
|
|
2011-03-31 06:23:17 +02:00
|
|
|
fprintf(stream, "%s %s(", g_parm[RETTYPE_INDEX], g_parm[NAME_INDEX]);
|
|
|
|
|
2011-04-02 03:23:08 +02:00
|
|
|
/* Generate the formal parameter list */
|
|
|
|
|
|
|
|
if (nformal <= 0)
|
2011-03-31 06:23:17 +02:00
|
|
|
{
|
|
|
|
fprintf(stream, "void");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-02 03:23:08 +02:00
|
|
|
for (i = 0; i < nformal; i++)
|
2011-03-31 06:23:17 +02:00
|
|
|
{
|
2011-04-02 03:23:08 +02:00
|
|
|
/* The formal and actual parameter types may be encoded.. extra the
|
|
|
|
* formal parameter type.
|
|
|
|
*/
|
|
|
|
|
2011-04-01 23:36:17 +02:00
|
|
|
get_formalparmtype(g_parm[PARM1_INDEX+i], formal);
|
2011-04-02 03:23:08 +02:00
|
|
|
|
|
|
|
/* Arguments after the first must be separated from the preceding
|
|
|
|
* parameter with a comma.
|
|
|
|
*/
|
|
|
|
|
2011-03-31 06:23:17 +02:00
|
|
|
if (i > 0)
|
|
|
|
{
|
2011-04-01 23:36:17 +02:00
|
|
|
fprintf(stream, ", ");
|
2011-03-31 06:23:17 +02:00
|
|
|
}
|
2011-04-01 23:36:17 +02:00
|
|
|
print_formalparm(stream, formal, i+1);
|
2011-03-31 06:23:17 +02:00
|
|
|
}
|
|
|
|
}
|
2011-04-01 23:36:17 +02:00
|
|
|
|
2011-04-02 03:23:08 +02:00
|
|
|
/* Handle the end of the formal parameter list */
|
|
|
|
|
|
|
|
if (bvarargs)
|
|
|
|
{
|
|
|
|
fprintf(stream, ", ...)\n{\n");
|
|
|
|
|
|
|
|
/* Get parm variables .. some from the parameter list and others from
|
|
|
|
* the varargs.
|
|
|
|
*/
|
2011-04-01 23:36:17 +02:00
|
|
|
|
2011-04-02 03:23:08 +02:00
|
|
|
if (nparms < 7)
|
|
|
|
{
|
|
|
|
fprintf(stream, " va_list ap;\n");
|
|
|
|
for (i = nparms; i < 7; i++)
|
|
|
|
{
|
|
|
|
fprintf(stream, " uintptr_t parm%d;\n", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stream, "\n va_start(ap, parm%d);\n", nparms-1);
|
|
|
|
for (i = nparms; i < 7; i++)
|
|
|
|
{
|
|
|
|
fprintf(stream, " parm%d = va_arg(ap, uintptr_t);\n", i);
|
|
|
|
}
|
|
|
|
fprintf(stream, " va_end(ap);\n\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stream, ")\n{\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate the system call. Functions that do not return or return void
|
|
|
|
* are special cases.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nactual = bvarargs ? 6 : nparms;
|
2011-04-01 23:36:17 +02:00
|
|
|
if (strcmp(g_parm[RETTYPE_INDEX], "void") == 0)
|
|
|
|
{
|
2011-04-02 03:23:08 +02:00
|
|
|
fprintf(stream, " (void)sys_call%d(", nactual);
|
2011-04-01 23:36:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-02 03:23:08 +02:00
|
|
|
fprintf(stream, " return (%s)sys_call%d(", g_parm[RETTYPE_INDEX], nactual);
|
2011-04-01 23:36:17 +02:00
|
|
|
}
|
|
|
|
|
2011-04-02 03:23:08 +02:00
|
|
|
/* Create the parameter list with the matching types. The first parameter
|
|
|
|
* is always the syscall number.
|
|
|
|
*/
|
2011-04-01 23:36:17 +02:00
|
|
|
|
|
|
|
fprintf(stream, "(unsigned int)SYS_%s", g_parm[NAME_INDEX]);
|
|
|
|
|
2011-04-02 03:23:08 +02:00
|
|
|
for (i = 0; i < nactual; i++)
|
2011-03-31 06:23:17 +02:00
|
|
|
{
|
2011-04-02 17:25:22 +02:00
|
|
|
/* Is the parameter a union member */
|
|
|
|
|
|
|
|
if (i < nparms && is_union(g_parm[PARM1_INDEX+i]))
|
|
|
|
{
|
|
|
|
/* Then we will have to pick a field name that can be cast to a
|
|
|
|
* uintptr_t. There probably should be some error handling here
|
|
|
|
* to catch the case where the fieldname was not supplied.
|
|
|
|
*/
|
|
|
|
|
|
|
|
get_fieldname(g_parm[PARM1_INDEX+i], fieldname);
|
|
|
|
fprintf(stream, ", (uintptr_t)parm%d.%s", i+1, fieldname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stream, ", (uintptr_t)parm%d", i+1);
|
|
|
|
}
|
2011-03-31 06:23:17 +02:00
|
|
|
}
|
2011-04-01 23:36:17 +02:00
|
|
|
|
|
|
|
/* Handle the tail end of the function. */
|
|
|
|
|
2011-04-02 01:07:51 +02:00
|
|
|
fprintf(stream, ");\n}\n\n");
|
|
|
|
if (g_parm[COND_INDEX][0] != '\0')
|
|
|
|
{
|
|
|
|
fprintf(stream, "#endif /* %s */\n", g_parm[COND_INDEX]);
|
|
|
|
}
|
|
|
|
|
2011-03-31 06:23:17 +02:00
|
|
|
fclose(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
static FILE *open_stub(void)
|
|
|
|
{
|
2011-04-01 23:36:17 +02:00
|
|
|
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);
|
|
|
|
}
|
2011-04-02 01:07:51 +02:00
|
|
|
fprintf(g_stubstream, "/* Autogenerated STUB header file */\n\n");
|
|
|
|
fprintf(g_stubstream, "#ifndef __STUB_H\n");
|
|
|
|
fprintf(g_stubstream, "#define __STUB_H\n\n");
|
2011-04-01 23:36:17 +02:00
|
|
|
}
|
2011-03-31 06:23:17 +02:00
|
|
|
|
2011-04-01 23:36:17 +02:00
|
|
|
return g_stubstream;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char filename[MAX_PARMSIZE+8];
|
|
|
|
FILE *stream;
|
2011-03-31 06:23:17 +02:00
|
|
|
|
2011-04-01 23:36:17 +02:00
|
|
|
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)
|
2011-03-31 06:23:17 +02:00
|
|
|
{
|
2011-04-01 23:36:17 +02:00
|
|
|
fclose(stream);
|
2011-03-31 06:23:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void generate_stub(int nparms)
|
|
|
|
{
|
|
|
|
FILE *stream = open_stub();
|
2011-04-01 23:36:17 +02:00
|
|
|
char formal[MAX_PARMSIZE];
|
|
|
|
char actual[MAX_PARMSIZE];
|
2011-03-31 06:23:17 +02:00
|
|
|
int i;
|
2011-04-01 23:36:17 +02:00
|
|
|
int j;
|
|
|
|
|
|
|
|
/* Generate "up-front" information, include correct header files */
|
2011-03-31 06:23:17 +02:00
|
|
|
|
|
|
|
fprintf(stream, "/* Auto-generated %s stub file -- do not edit */\n\n", g_parm[0]);
|
2011-04-02 01:07:51 +02:00
|
|
|
fprintf(stream, "#include <nuttx/config.h>\n");
|
2011-04-01 23:36:17 +02:00
|
|
|
fprintf(stream, "#include <stdint.h>\n");
|
2011-03-31 06:23:17 +02:00
|
|
|
fprintf(stream, "#include <%s>\n\n", g_parm[HEADER_INDEX]);
|
2011-04-01 23:36:17 +02:00
|
|
|
|
2011-04-02 01:07:51 +02:00
|
|
|
if (g_parm[COND_INDEX][0] != '\0')
|
|
|
|
{
|
|
|
|
fprintf(stream, "#if %s\n\n", g_parm[COND_INDEX]);
|
|
|
|
}
|
|
|
|
|
2011-04-01 23:36:17 +02:00
|
|
|
/* Generate the function definition that matches standard function prototype */
|
|
|
|
|
2011-04-02 03:23:08 +02:00
|
|
|
if (g_inline)
|
|
|
|
{
|
|
|
|
fprintf(stream, "static inline ");
|
|
|
|
}
|
2011-03-31 06:23:17 +02:00
|
|
|
fprintf(stream, "uintptr_t STUB_%s(", g_parm[NAME_INDEX]);
|
|
|
|
|
2011-04-01 23:36:17 +02:00
|
|
|
/* Generate the formal parameter list. A function received no parameters is a special case. */
|
|
|
|
|
2011-03-31 06:23:17 +02:00
|
|
|
if (nparms <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stream, "void");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < nparms; i++)
|
|
|
|
{
|
2011-04-01 23:36:17 +02:00
|
|
|
/* Treat the first argument in the list differently from the others..
|
|
|
|
* It does not need a comma before it.
|
|
|
|
*/
|
|
|
|
|
2011-03-31 06:23:17 +02:00
|
|
|
if (i > 0)
|
|
|
|
{
|
2011-04-01 23:36:17 +02:00
|
|
|
/* Check for a variable number of arguments */
|
|
|
|
|
|
|
|
if (is_vararg(g_parm[PARM1_INDEX+i], i, nparms))
|
|
|
|
{
|
|
|
|
/* Always receive six arguments in this case */
|
|
|
|
|
2011-04-02 03:23:08 +02:00
|
|
|
for (j = i+1; j <= 6; j++)
|
2011-04-01 23:36:17 +02:00
|
|
|
{
|
|
|
|
fprintf(stream, ", uintptr_t parm%d", j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stream, ", uintptr_t parm%d", i+1);
|
|
|
|
}
|
2011-03-31 06:23:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stream, "uintptr_t parm%d", i+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stream, ")\n{\n");
|
2011-04-01 23:36:17 +02:00
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2011-03-31 06:23:17 +02:00
|
|
|
for (i = 0; i < nparms; i++)
|
|
|
|
{
|
2011-04-01 23:36:17 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2011-03-31 06:23:17 +02:00
|
|
|
if (i > 0)
|
|
|
|
{
|
2011-04-01 23:36:17 +02:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|
2011-03-31 06:23:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-01 23:36:17 +02:00
|
|
|
if (is_union(formal))
|
|
|
|
{
|
|
|
|
fprintf(stream, "(%s)((%s)parm%d)", formal, actual, i+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stream, "(%s)parm%d",actual, i+1);
|
|
|
|
}
|
2011-03-31 06:23:17 +02:00
|
|
|
}
|
|
|
|
}
|
2011-04-01 23:36:17 +02:00
|
|
|
|
2011-04-02 01:07:51 +02:00
|
|
|
/* Tail end of the function. If the proxied function has no return
|
2011-04-01 23:36:17 +02:00
|
|
|
* value, just return zero (OK).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (strcmp(g_parm[RETTYPE_INDEX], "void") == 0)
|
|
|
|
{
|
2011-04-02 01:07:51 +02:00
|
|
|
fprintf(stream, ");\n return 0;\n}\n\n");
|
2011-04-01 23:36:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-02 01:07:51 +02:00
|
|
|
fprintf(stream, ");\n}\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_parm[COND_INDEX][0] != '\0')
|
|
|
|
{
|
|
|
|
fprintf(stream, "#endif /* %s */\n", g_parm[COND_INDEX]);
|
2011-04-01 23:36:17 +02:00
|
|
|
}
|
|
|
|
stub_close(stream);
|
2011-03-31 06:23:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void show_usage(const char *progname)
|
|
|
|
{
|
2011-04-01 23:36:17 +02:00
|
|
|
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");
|
2011-03-31 06:23:17 +02:00
|
|
|
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;
|
2011-04-01 23:36:17 +02:00
|
|
|
g_inline = false;
|
2011-03-31 06:23:17 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-04-01 23:36:17 +02:00
|
|
|
case 'i' :
|
|
|
|
g_inline = true;
|
|
|
|
break;
|
|
|
|
|
2011-03-31 06:23:17 +02:00
|
|
|
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);
|
2011-04-02 01:07:51 +02:00
|
|
|
if (nargs < PARM1_INDEX)
|
2011-03-31 06:23:17 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Only %d arguments found: %s\n", nargs, g_line);
|
|
|
|
exit(8);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proxies)
|
|
|
|
{
|
2011-04-02 01:07:51 +02:00
|
|
|
generate_proxy(nargs - PARM1_INDEX);
|
2011-03-31 06:23:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-01 23:36:17 +02:00
|
|
|
g_stubstream = NULL;
|
2011-04-02 01:07:51 +02:00
|
|
|
generate_stub(nargs - PARM1_INDEX);
|
2011-04-01 23:36:17 +02:00
|
|
|
if (g_stubstream != NULL)
|
|
|
|
{
|
2011-04-02 01:07:51 +02:00
|
|
|
fprintf(g_stubstream, "\n#endif /* __STUB_H */\n");
|
2011-04-01 23:36:17 +02:00
|
|
|
fclose(g_stubstream);
|
|
|
|
}
|
2011-03-31 06:23:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the CSV file */
|
|
|
|
|
|
|
|
fclose(stream);
|
|
|
|
return 0;
|
|
|
|
}
|