A buffer variable wasn't dereferenced in two similar comparisons in ResList
[AROS.git] / tools / genmodule / writeincdefines.c
blob1cf330cce231a4f2a11aa75a2c279a8ff9843f0b
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
5 Function to write defines/modulename.h. Part of genmodule.
6 */
7 #include "genmodule.h"
9 static void writedefineregister(FILE *, struct functionhead *, struct config *, char);
10 static void writedefinevararg(FILE *, struct functionhead *, struct config *, char, char *);
11 static void writealiases(FILE *, struct functionhead *, struct config *);
13 void writeincdefines(struct config *cfg)
15 FILE *out;
16 char line[256], *banner;
17 struct functionhead *funclistit;
19 snprintf(line, 255, "%s/defines/%s.h", cfg->gendir, cfg->includename);
20 out = fopen(line, "w");
22 if (out == NULL)
24 perror(line);
25 exit(20);
28 banner = getBanner(cfg);
29 fprintf(out,
30 "#ifndef DEFINES_%s_H\n"
31 "#define DEFINES_%s_H\n"
32 "\n"
33 "%s"
34 "\n"
35 "/*\n"
36 " Desc: Defines for %s\n"
37 "*/\n"
38 "\n"
39 "#include <aros/libcall.h>\n"
40 "#include <exec/types.h>\n"
41 "#include <aros/symbolsets.h>\n"
42 "#include <aros/preprocessor/variadic/cast2iptr.hpp>\n"
43 "\n"
44 "__BEGIN_DECLS\n"
45 "\n",
46 cfg->includenameupper, cfg->includenameupper, banner, cfg->modulename
48 freeBanner(banner);
50 for (funclistit = cfg->funclist; funclistit!=NULL; funclistit = funclistit->next)
52 if (!funclistit->priv && (funclistit->lvo >= cfg->firstlvo) && funclistit->libcall != STACK)
54 char isvararg = 0, *varargname = NULL, *lastname;
56 fprintf(out,
57 "\n"
58 "#if !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__)"
59 "\n",
60 cfg->includenameupper,
61 funclistit->version,
62 cfg->includenameupper
65 if ((!funclistit->novararg) && (funclistit->arguments))
67 struct functionarg *arglistit = funclistit->arguments;
69 while (arglistit->next != NULL) arglistit = arglistit->next;
71 lastname = getargname(arglistit);
72 assert(lastname != NULL);
74 if (*(funclistit->name + strlen(funclistit->name) - 1) == 'A')
76 isvararg = 1;
77 varargname = strdup(funclistit->name);
78 varargname[strlen(funclistit->name)-1] = '\0';
80 else if (strcmp(funclistit->name + strlen(funclistit->name) - 7, "TagList") == 0)
82 isvararg = 1;
83 /* TagList has to be changed in Tags at the end of the functionname */
84 varargname = strdup(funclistit->name);
85 varargname[strlen(funclistit->name)-4] = 's';
86 varargname[strlen(funclistit->name)-3] = '\0';
88 else if (strcmp(funclistit->name + strlen(funclistit->name) - 4, "Args") == 0
89 && (strcasecmp(lastname, "args") == 0 || strcasecmp(lastname, "arglist") == 0)
92 isvararg = 1;
93 varargname = strdup(funclistit->name);
94 varargname[strlen(funclistit->name)-4] = '\0';
96 else if ((funclistit->name[0] == 'V') && (strncmp(arglistit->arg, "va_list", 7) == 0))
98 isvararg = 2;
99 varargname = malloc(strlen(funclistit->name));
100 strcpy(varargname, &funclistit->name[1]);
102 else
104 char *p;
106 if (strncmp(arglistit->arg, "const", 5) == 0) {
107 p = arglistit->arg + 5;
108 while (isspace(*p)) p++;
109 } else
110 p = arglistit->arg;
111 if (strncmp(p, "struct", 6)==0)
113 p += 6;
114 while (isspace(*p)) p++;
115 if (strncmp(p, "TagItem", 7) == 0)
117 p += 7;
118 while (isspace(*p)) p++;
120 if (*p == '*')
122 isvararg = 1;
123 varargname = malloc(strlen(funclistit->name) + 5);
124 strcpy(varargname, funclistit->name);
125 strcat(varargname, "Tags");
132 writedefineregister(out, funclistit, cfg, isvararg);
133 if (!funclistit->novararg && isvararg)
135 writedefinevararg(out, funclistit, cfg, isvararg, varargname);
136 free(varargname);
138 writealiases(out, funclistit, cfg);
140 fprintf(out,
141 "\n"
142 "#endif /* !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__) */"
143 "\n",
144 cfg->includenameupper,
145 funclistit->version,
146 cfg->includenameupper
151 fprintf(out,
152 "\n"
153 "__END_DECLS\n"
154 "\n"
155 "#endif /* DEFINES_%s_H*/\n",
156 cfg->includenameupper
158 fclose(out);
161 void
162 writedefineregister(FILE *out, struct functionhead *funclistit, struct config *cfg, char isvararg)
164 struct functionarg *arglistit;
165 int count, isvoid, nquad = 0, narg = 0;
166 char *type;
168 isvoid = strcmp(funclistit->type, "void") == 0
169 || strcmp(funclistit->type, "VOID") == 0;
171 fprintf(out,
172 "\n"
173 "#define __%s_WB(__%s",
174 funclistit->name, cfg->libbase
176 for (arglistit = funclistit->arguments, count = 1;
177 arglistit!=NULL;
178 arglistit = arglistit->next, count++
181 fprintf(out, ", __arg%d", count);
182 if (strchr(arglistit->reg, '/') != NULL) {
183 nquad++;
184 } else {
185 narg++;
188 fprintf(out,
189 ") ({\\\n"
191 fprintf(out,
192 " AROS_LIBREQ(%s,%d)\\\n",
193 cfg->libbase, funclistit->version
195 if (nquad == 0)
197 fprintf(out,
198 " AROS_LC%d%s%s(%s, %s, \\\n",
199 funclistit->argcount, funclistit->unusedlibbase ? "I" : "",
200 (isvoid) ? "NR" : "",
201 funclistit->type, funclistit->name
204 for (arglistit = funclistit->arguments, count = 1;
205 arglistit!=NULL;
206 arglistit = arglistit->next, count++
209 type = getargtype(arglistit);
210 assert(type != NULL);
211 fprintf(out,
212 " AROS_LCA(%s%s,(__arg%d),%s), \\\n",
213 ((isvararg) && (!arglistit->next)) ? "const " : "",
214 type, count, arglistit->reg
216 free(type);
219 else
221 if (narg) {
222 fprintf(out,
223 " AROS_LC%dQUAD%d%s(%s, %s,\\\n",
224 narg, nquad, (isvoid) ? "NR" : "",
225 funclistit->type, funclistit->name
227 } else {
228 fprintf(out,
229 " AROS_LCQUAD%d%s(%s, %s,\\\n",
230 nquad, (isvoid) ? "NR" : "",
231 funclistit->type, funclistit->name
235 for (arglistit = funclistit->arguments, count = 1;
236 arglistit != NULL;
237 arglistit = arglistit->next, count++
240 char *quad2 = strchr(arglistit->reg, '/');
242 arglistit->reg[2] = 0;
243 type = getargtype(arglistit);
244 assert(type != NULL);
246 if (quad2 != NULL) {
247 *quad2 = 0;
248 fprintf(out,
249 " AROS_LCAQUAD(%s%s, (__arg%d), %s, %s), \\\n",
250 ((isvararg) && (!arglistit->next)) ? "const " : "",
251 type, count, arglistit->reg, quad2+1
253 *quad2 = '/';
254 } else {
255 fprintf(out,
256 " AROS_LCA(%s%s, (__arg%d), %s), \\\n",
257 ((isvararg) && (!arglistit->next)) ? "const " : "",
258 type, count, arglistit->reg
261 free(type);
264 fprintf(out,
265 " %s, (__%s), %u, %s);\\\n})\n\n",
266 cfg->libbasetypeptrextern, cfg->libbase, funclistit->lvo, cfg->basename
269 fprintf(out, "#define %s(", funclistit->name);
270 for (arglistit = funclistit->arguments, count = 1;
271 arglistit != NULL;
272 arglistit = arglistit->next, count++
275 if (arglistit != funclistit->arguments)
276 fprintf(out, ", ");
277 fprintf(out, "arg%d", count);
279 fprintf(out, ") \\\n __%s_WB(__aros_getbase_%s()",
280 funclistit->name, cfg->libbase
282 for (arglistit = funclistit->arguments, count = 1;
283 arglistit != NULL;
284 arglistit = arglistit->next, count++
286 fprintf(out, ", (arg%d)", count);
287 fprintf(out, ")\n");
290 void
291 writedefinevararg(FILE *out, struct functionhead *funclistit, struct config *cfg, char isvararg, char *varargname)
293 struct functionarg *arglistit = funclistit->arguments;
295 if (isvararg == 1)
297 int count;
298 char *type;
300 fprintf(out,
301 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
302 "#define %s(",
303 cfg->includenameupper, varargname
305 for (arglistit = funclistit->arguments, count = 1;
306 arglistit != NULL && arglistit->next != NULL;
307 arglistit = arglistit->next, count++
310 fprintf(out, "arg%d, ", count);
312 fprintf(out,
313 "...) \\\n"
314 "({ \\\n"
315 " %s(",
316 funclistit->name
318 for (arglistit = funclistit->arguments, count = 1;
319 arglistit != NULL;
320 arglistit = arglistit->next, count++
323 if (arglistit != funclistit->arguments)
324 fprintf(out, ", ");
326 if (arglistit->next == NULL)
328 type = getargtype(arglistit);
329 assert(type != NULL);
330 fprintf(out, "(const %s)(const IPTR []){ AROS_PP_VARIADIC_CAST2IPTR(__VA_ARGS__) }", type);
331 free(type);
333 else
334 fprintf(out, "(arg%d)", count);
336 fprintf(out,
337 "); \\\n"
338 "})\n"
339 "#endif /* !NO_INLINE_STDARG */\n"
342 else if (isvararg == 2)
344 int count;
345 struct functionarg *lastarg;
347 fprintf(out,
348 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
349 "static inline %s __%s_WB(%s __%s",
350 cfg->includenameupper,
351 funclistit->type, varargname, cfg->libbasetypeptrextern, cfg->libbase
353 for (arglistit = funclistit->arguments;
354 arglistit != NULL && arglistit->next != NULL;
355 arglistit = arglistit->next
358 fprintf(out, ", %s", arglistit->arg);
359 lastarg = arglistit;
361 fprintf(out, ", ...)\n");
363 fprintf(out,
364 "{\n"
365 " %s retval;\n"
366 " va_list args;\n"
367 "\n"
368 " va_start(args, %s);\n"
369 " retval = __%s_WB(__%s, ",
370 funclistit->type,
371 getargname(lastarg),
372 funclistit->name, cfg->libbase
374 for (arglistit = funclistit->arguments;
375 arglistit != NULL && arglistit->next != NULL;
376 arglistit = arglistit->next
379 fprintf(out, "%s, ", getargname(arglistit));
381 fprintf(out,
382 "args);\n"
383 " va_end(args);\n"
384 " return retval;\n"
385 "}\n"
386 "#define %s(",
387 varargname
389 for (arglistit = funclistit->arguments, count = 1;
390 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
391 arglistit = arglistit->next, count++
394 fprintf(out, "arg%d, ", count);
396 fprintf(out,
397 "...) __%s_WB(%s, ",
398 varargname, cfg->libbase
400 for (arglistit = funclistit->arguments, count = 1;
401 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
402 arglistit = arglistit->next, count++
405 fprintf(out, "(arg%d), ", count);
407 fprintf(out,
408 "__VA_ARGS__)\n"
409 "#endif /* !NO_INLINE_STDARG */\n"
414 void
415 writealiases(FILE *out, struct functionhead *funclistit, struct config *cfg)
417 struct stringlist *aliasesit;
419 for (aliasesit = funclistit->aliases;
420 aliasesit != NULL;
421 aliasesit = aliasesit->next
424 fprintf(out, "#define %s %s\n", aliasesit->s, funclistit->name);