#include <stdlib.h>
#include <string.h>
+struct str {
+ int str_len;
+ char *str_data;
+};
+
+struct str keywords[1024] = {
+ {2, "X_"},
+ {5, "break"},
+ {4, "case"},
+ {4, "char"},
+ {8, "continue"},
+ {7, "default"},
+ {7, "defined"},
+ {2, "do"},
+ {6, "double"},
+ {4, "else"},
+ {4, "enum"},
+ {6, "extern"},
+ {5, "float"},
+ {2, "if"},
+ {3, "for"},
+ {4, "goto"},
+ {6, "return"},
+ {8, "register"},
+ {6, "sizeof"},
+ {6, "static"},
+ {6, "struct"},
+ {6, "switch"},
+ {7, "typedef"},
+ {5, "union"},
+ {4, "void"},
+ {5, "while"},
+ {8, "__FILE__"},
+ {8, "__GNUC__"},
+ {8, "__LINE__"},
+ {3, "__P"},
+ {8, "__STDC__"},
+ {13, "__attribute__"},
+ {12, "__noreturn__"}
+};
+int n_keywords = 33;
+
+struct str typedefs[1024] = {
+ {4, "char"},
+ {6, "double"},
+ {4, "enum"},
+ {5, "float"},
+ {3, "int"},
+ {4, "long"},
+ {5, "short"},
+ {6, "struct"},
+ {5, "union"},
+ {8, "unsigned"},
+ {4, "void"}
+};
+int n_typedefs = 11;
+
char buf[1024];
-int main(int argc, char **argv) {
+int main(argc, argv) int argc; char **argv; {
char *p, *q, *r;
int l, m, n;
- int e;
+ int e, i;
while (fgets(buf, sizeof(buf), stdin)) {
p = buf;
++n;
while (isalnum(r[n]) || r[n] == '_')
++n;
- if (n == 7 && memcmp(r, "typedef", 7) == 0) {
+ if (n == 7 && memcmp(r, "keyword", 7) == 0) {
while (isblank(r[n]))
++n;
p = r + n;
while (isalpha(p[l]) || p[l] == '_') {
while (isalnum(p[l]) || p[l] == '_')
++l;
- fprintf(stderr, "typedef name: ");
+#if 0
+ fwrite("keyword: ", 9, 1, stderr);
fwrite(p, l, 1, stderr);
putc('\n', stderr);
+#endif
+ if ((q = malloc(l)) == 0)
+ abort();
+ memcpy(q, p, l);
+ if (n_keywords >= sizeof(keywords) / sizeof(struct str))
+ abort();
+ keywords[n_keywords].str_len = l;
+ keywords[n_keywords].str_data = q;
+ ++n_keywords;
while (isblank(p[l]))
++l;
p += l;
l = 0;
}
if (p[l] != '\n') {
- fprintf(stderr, "%s: warning: garbage after #pragma typedef: ", argv[0]);
+ fprintf(stderr, "%s: warning: garbage after #pragma keyword: ", argv[0]);
while (p[l] && p[l] != '\n')
putc(p[l++], stderr);
putc('\n', stderr);
}
continue;
}
- else if (n == 7 && memcmp(r, "keyword", 7) == 0) {
+ else if (n == 7 && memcmp(r, "typedef", 7) == 0) {
while (isblank(r[n]))
++n;
p = r + n;
while (isalpha(p[l]) || p[l] == '_') {
while (isalnum(p[l]) || p[l] == '_')
++l;
- fprintf(stderr, "keyword name: ");
+#if 0
+ fwrite("typedef: ", 9, 1, stderr);
fwrite(p, l, 1, stderr);
putc('\n', stderr);
+#endif
+ if ((q = malloc(l)) == 0)
+ abort();
+ memcpy(q, p, l);
+ /* typedefs notified through #pragma are also keywords */
+ /* this isn't the case for typedefs we notice ourselves */
+ if (n_keywords >= sizeof(keywords) / sizeof(struct str))
+ abort();
+ keywords[n_keywords].str_len = l;
+ keywords[n_keywords].str_data = q;
+ ++n_keywords;
+ if (n_typedefs >= sizeof(typedefs) / sizeof(struct str))
+ abort();
+ typedefs[n_typedefs].str_len = l;
+ typedefs[n_typedefs].str_data = q;
+ ++n_typedefs;
while (isblank(p[l]))
++l;
p += l;
l = 0;
}
if (p[l] != '\n') {
- fprintf(stderr, "%s: warning: garbage after #pragma keyword: ", argv[0]);
+ fprintf(stderr, "%s: warning: garbage after #pragma typedef: ", argv[0]);
while (p[l] && p[l] != '\n')
putc(p[l++], stderr);
putc('\n', stderr);
++m;
while (isalnum(q[m]) || q[m] == '_')
++m;
- if (
- (m != 4 || memcmp(q, "char", 4) != 0) &&
- (m != 6 || memcmp(q, "double", 6) != 0) &&
- (m != 4 || memcmp(q, "enum", 4) != 0) &&
- (m != 5 || memcmp(q, "float", 5) != 0) &&
- (m != 3 || memcmp(q, "int", 3) != 0) &&
- (m != 6 || memcmp(q, "int8_t", 6) != 0) &&
- (m != 7 || memcmp(q, "int16_t", 7) != 0) &&
- (m != 7 || memcmp(q, "int32_t", 7) != 0) &&
- (m != 7 || memcmp(q, "int64_t", 7) != 0) &&
- (m != 8 || memcmp(q, "intptr_t", 8) != 0) &&
- (m != 7 || memcmp(q, "jmp_buf", 7) != 0) &&
- (m != 4 || memcmp(q, "long", 4) != 0) &&
- (m != 5 || memcmp(q, "short", 5) != 0) &&
- (m != 6 || memcmp(q, "size_t", 6) != 0) &&
- (m != 7 || memcmp(q, "ssize_t", 7) != 0) &&
- (m != 6 || memcmp(q, "struct", 6) != 0) &&
- (m != 7 || memcmp(q, "uint8_t", 7) != 0) &&
- (m != 8 || memcmp(q, "uint16_t", 8) != 0) &&
- (m != 8 || memcmp(q, "uint32_t", 8) != 0) &&
- (m != 8 || memcmp(q, "uint64_t", 8) != 0) &&
- (m != 9 || memcmp(q, "uintptr_t", 9) != 0) &&
- (m != 5 || memcmp(q, "union", 5) != 0) &&
- (m != 8 || memcmp(q, "unsigned", 8) != 0) &&
- (m != 7 || memcmp(q, "va_list", 7) != 0) &&
- (m != 4 || memcmp(q, "void", 4) != 0)
- ) {
- q += m;
- m = 0;
- while (isblank(q[m]))
- ++m;
- if (q[m] != '*') /* avoid something like: register FILE *fp; */
- fwrite(" x_int", 6, 1, stdout);
- }
+ for (i = 0; i < n_typedefs; ++i)
+ if (
+ m == typedefs[i].str_len &&
+ memcmp(q, typedefs[i].str_data, m) == 0
+ )
+ goto found_typedef;
+ q += m;
+ m = 0;
+ while (isblank(q[m]))
+ ++m;
+ if (q[m] != '*') /* avoid something like: register FILE *fp; */
+ fwrite(" x_int", 6, 1, stdout);
+ found_typedef:
+ ;
}
else /* maybe something like: register *p; */
fwrite(" x_int", 6, 1, stdout);
p += 4;
l -= 4;
}
- else if (
- (l != 2 || memcmp(p, "X_", 2) != 0) &&
- (l != 5 || memcmp(p, "break", 5) != 0) &&
- (l != 4 || memcmp(p, "case", 4) != 0) &&
- (l != 4 || memcmp(p, "char", 4) != 0) &&
- (l != 8 || memcmp(p, "continue", 8) != 0) &&
- (l != 7 || memcmp(p, "default", 7) != 0 /*|| buf[0] != '#'*/) &&
- (l != 7 || memcmp(p, "defined", 7) != 0 /*|| buf[0] != '#'*/) &&
- (l != 2 || memcmp(p, "do", 2) != 0) &&
- (l != 6 || memcmp(p, "double", 6) != 0) &&
- (l != 4 || memcmp(p, "else", 4) != 0) &&
- (l != 4 || memcmp(p, "enum", 4) != 0) &&
- (l != 6 || memcmp(p, "extern", 6) != 0) &&
- (l != 5 || memcmp(p, "float", 5) != 0) &&
- (l != 2 || memcmp(p, "if", 2) != 0) &&
- (l != 6 || memcmp(p, "int8_t", 6) != 0) &&
- (l != 7 || memcmp(p, "int16_t", 7) != 0) &&
- (l != 7 || memcmp(p, "int32_t", 7) != 0) &&
- (l != 7 || memcmp(p, "int64_t", 7) != 0) &&
- (l != 8 || memcmp(p, "intptr_t", 8) != 0) &&
- (l != 3 || memcmp(p, "for", 3) != 0) &&
- (l != 4 || memcmp(p, "goto", 4) != 0) &&
- (l != 7 || memcmp(p, "jmp_buf", 7) != 0) &&
- (l != 7 || memcmp(p, "longjmp", 7) != 0) &&
- (l != 6 || memcmp(p, "return", 6) != 0) &&
- (l != 8 || memcmp(p, "register", 8) != 0) &&
- (l != 6 || memcmp(p, "setjmp", 6) != 0) &&
- (l != 6 || memcmp(p, "sizeof", 6) != 0) &&
- /*(l != 6 || memcmp(p, "size_t", 6) != 0) &&
- (l != 7 || memcmp(p, "ssize_t", 7) != 0) &&*/
- (l != 6 || memcmp(p, "static", 6) != 0) &&
- (l != 6 || memcmp(p, "struct", 6) != 0) &&
- (l != 6 || memcmp(p, "switch", 6) != 0) &&
- (l != 7 || memcmp(p, "typedef", 7) != 0) &&
- (l != 7 || memcmp(p, "uint8_t", 7) != 0) &&
- (l != 8 || memcmp(p, "uint16_t", 8) != 0) &&
- (l != 8 || memcmp(p, "uint32_t", 8) != 0) &&
- (l != 8 || memcmp(p, "uint64_t", 8) != 0) &&
- (l != 9 || memcmp(p, "uintptr_t", 9) != 0) &&
- (l != 5 || memcmp(p, "union", 5) != 0) &&
- (l != 8 || memcmp(p, "va_alist", 8) != 0) &&
- (l != 6 || memcmp(p, "va_arg", 6) != 0) &&
- (l != 6 || memcmp(p, "va_dcl", 6) != 0) &&
- (l != 6 || memcmp(p, "va_end", 6) != 0) &&
- (l != 7 || memcmp(p, "va_list", 7) != 0) &&
- (l != 8 || memcmp(p, "va_start", 8) != 0) &&
- (l != 4 || memcmp(p, "void", 4) != 0) &&
- (l != 5 || memcmp(p, "while", 5) != 0) &&
- (l != 8 || memcmp(p, "__FILE__", 8) != 0) &&
- (l != 8 || memcmp(p, "__GNUC__", 8) != 0) &&
- (l != 8 || memcmp(p, "__LINE__", 8) != 0) &&
- (l != 3 || memcmp(p, "__P", 3) != 0) &&
- (l != 8 || memcmp(p, "__STDC__", 8) != 0) &&
- (l != 13 || memcmp(p, "__attribute__", 13) != 0) &&
- (l < 10 || memcmp(p, "__builtin_", 10) != 0) &&
- (l != 12 || memcmp(p, "__noreturn__", 12) != 0)
- )
+ else {
+ for (i = 0; i < n_keywords; ++i)
+ if (
+ l == keywords[i].str_len &&
+ memcmp(p, keywords[i].str_data, l) == 0
+ )
+ goto found_keyword;
fwrite("x_", 2, 1, stdout);
+ found_keyword:
+ ;
+ }
}
else if (p[l] == '.' && isdigit(p[l + 1])) {
l += 2;