2 Copyright © 2019, The AROS Development Team. All rights reserved.
12 #include "parsendkoffsets.h"
15 static char *skipspaces(char *start
)
17 while (start
[0] == ' ')
22 static inline char *escapematch(char *p
)
38 static void escapeheader(char *buffer
)
41 for (p
= buffer
; (p
= escapematch(p
)); ++p
) {
46 void makefullpath(char *path
)
52 while ((str
= strtok (s
, "/")) != NULL
) {
56 if (stat (path
, &statBuf
) == -1) {
57 mkdir (path
, S_IRWXU
| S_IRWXG
| S_IROTH
| S_IXOTH
);
59 if (! S_ISDIR (statBuf
.st_mode
)) {
60 fprintf (stderr
, "cannot create directory %s\n", path
);
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");
83 parsendkoffsets (char *offfile
, char *sdkdir
, char *gendir
, char *bindir
)
86 FILE *structfile
= NULL
;
87 char *structName
, *structFileName
, *headerName
, *headerTestName
, *line
= NULL
;
93 srcfile
= fopen(offfile
, "r");
98 line
= malloc(LINELENGTH
);
99 structName
= malloc(LINELENGTH
);
100 structFileName
= malloc(LINELENGTH
);
102 while (fgets(line
, LINELENGTH
, srcfile
) != NULL
)
107 line
[strlen(line
) - 2] = '\0';
108 sprintf(structName
, "%s", line
);
111 writeEpilogue(structfile
);
115 headerTestName
= NULL
;
120 headerName
= findStructHeader(sdkdir
, structName
);
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
);
132 printf ("Creating '%s'\n", structFileName
);
134 structfile
= fopen(structFileName
, "w");
137 printf ("Failed to open '%s'\n", offfile
);
139 free(structFileName
);
142 writePrologue(structfile
, headerName
, structName
);
143 addSDKHeaderStruct(headerTestName
, structName
);
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)))
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
);
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
);
170 writeEpilogue(structfile
);
173 writeHeaderMakefile(gendir
, bindir
);
176 free(structFileName
);
180 printf ("Failed to open '%s'\n", offfile
);