re-order some parts of the code so that the msg and rect are only allocated once.
[AROS.git] / tools / parseoffsets / parsendkoffsets.c
blobdd40877c46a0f281aae2f9f7f6ea24a70e1a9222
1 /*
2 Copyright © 2019, The AROS Development Team. All rights reserved.
3 */
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdarg.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/stat.h>
12 #include "parsendkoffsets.h"
13 #include "header.h"
15 static char *skipspaces(char *start)
17 while (start[0] == ' ')
18 start++;
19 return start;
22 static inline char *escapematch(char *p)
24 while (p[0] != '\0')
26 if (p[0] == ':')
27 return p;
28 else if (p[0] == '/')
29 return p;
30 else if (p[0] == '.')
31 return p;
32 else
33 p++;
35 return NULL;
38 static void escapeheader(char *buffer)
40 char* p;
41 for (p = buffer; (p = escapematch(p)); ++p) {
42 *p = '_';
46 void makefullpath(char *path)
48 struct stat statBuf;
49 char *str, *s;
51 s = path;
52 while ((str = strtok (s, "/")) != NULL) {
53 if (str != s) {
54 str[-1] = '/';
56 if (stat (path, &statBuf) == -1) {
57 mkdir (path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
58 } else {
59 if (! S_ISDIR (statBuf.st_mode)) {
60 fprintf (stderr, "cannot create directory %s\n", path);
61 exit (1);
64 s = NULL;
68 static void writePrologue(FILE *structfile, char *header, char *structname)
70 printBanner(structfile, "//");
71 fprintf(structfile, "\n#include <stddef.h>\n#include <stdio.h>\n");
72 fprintf(structfile, "#include <%s>\n", header);
73 fprintf(structfile, "\nint\nmain (int argc, char ** argv)\n{\n\tstruct %s strc_%s;\n\tint retval = 0;\n", structname, structname);
74 fprintf(structfile, "\n\t\tprintf(\"Validating 'struct %s'\\n\");\n", structname);
77 static void writeEpilogue(FILE *structfile)
79 fprintf(structfile, "\n\treturn retval;\n}\n");
82 int
83 parsendkoffsets (char *offfile, char *sdkdir, char *gendir, char *bindir)
85 FILE *srcfile = NULL;
86 FILE *structfile = NULL;
87 char *structName, *structFileName, *headerName, *headerTestName, *line = NULL;
88 int retval = 0;
90 if (offfile == NULL)
91 return retval;
93 srcfile = fopen(offfile, "r");
94 if (srcfile != NULL)
96 makefullpath(gendir);
98 line = malloc(LINELENGTH);
99 structName = malloc(LINELENGTH);
100 structFileName = malloc(LINELENGTH);
102 while (fgets(line, LINELENGTH, srcfile) != NULL)
104 if (line[0] != ' ')
106 // new struct
107 line[strlen(line) - 2] = '\0';
108 sprintf(structName, "%s", line);
109 if (structfile)
111 writeEpilogue(structfile);
112 fclose(structfile);
113 if (headerTestName)
115 headerTestName = NULL;
117 structfile = NULL;
118 headerName = NULL;
120 headerName = findStructHeader(sdkdir, structName);
121 if (headerName)
123 headerTestName = malloc(strlen(headerName) + 1);
124 memcpy(headerTestName, headerName, strlen(headerName));
125 headerTestName[strlen(headerName)] = '\0';
126 escapeheader(headerTestName);
128 sprintf(structFileName, "%s/test-struct_%s.c", gendir, structName);
130 if (verbose)
132 printf ("Creating '%s'\n", structFileName);
134 structfile = fopen(structFileName, "w");
135 if (!structfile)
137 printf ("Failed to open '%s'\n", offfile);
138 free(structName);
139 free(structFileName);
140 return 1;
142 writePrologue(structfile, headerName, structName);
143 addSDKHeaderStruct(headerTestName, structName);
145 else if (verbose)
147 // Skip the structure
148 printf ("Failed to locate header file for struct %s\n", structName);
151 else if (structfile && (!strncmp(&line[15], "sizeof(", 7)))
153 // struct size
154 int structSize = atoi(skipspaces(&line[7]));
155 line[strlen(line) - 2] = '\0';
156 fprintf(structfile, "\n\tif (sizeof(strc_%s) != %d)\n\t{\n\t\tprintf(\"%s->%s: sizeof(struct %s) != %d\\n\");\n\t}\n", &line[22], structSize, headerName, structName, &line[22], structSize);
158 else if (structfile)
160 // offset line
161 char *elementName = skipspaces(&line[19]);
162 int elementOffset = atoi(skipspaces(&line[7])), elementSize = atoi(skipspaces(&line[13]));
163 elementName[strlen(elementName) - 1] = '\0';
164 fprintf(structfile, "\n\tif (offsetof(struct %s,%s) != %d)\n\t{\n\t\tprintf(\"%s->%s: - offsetof(struct %s,%s) != %d\\n\");\n\t}\n", structName, elementName, elementOffset, headerName, structName, structName, elementName, elementOffset);
165 fprintf(structfile, "\n\tif (sizeof(strc_%s.%s) != %d)\n\t{\n\t\tprintf(\"%s->%s: - sizeof(%s.%s) != %d\\n\");\n\t}\n", structName, elementName, elementSize, headerName, structName, structName, elementName, elementSize);
168 if (structfile)
170 writeEpilogue(structfile);
171 fclose(structfile);
173 writeHeaderMakefile(gendir, bindir);
174 fclose(srcfile);
175 free(structName);
176 free(structFileName);
178 else
180 printf ("Failed to open '%s'\n", offfile);
181 retval = 1;
184 return retval;