Create generated/tags.h with sed and bash instead of C.

This commit is contained in:
Rob Landley 2024-04-30 15:18:24 -05:00
parent 41e7186b01
commit d52e93c947
2 changed files with 12 additions and 64 deletions

View File

@ -212,12 +212,18 @@ fi
echo "} this;"
} > "$GENDIR"/globals.h || exit 1
hostcomp mktags
if isnewer tags.h toys
then
$SED -n '/TAGGED_ARRAY(/,/^)/{s/.*TAGGED_ARRAY[(]\([^,]*\),/\1/;p}' \
toys/*/*.c lib/*.c | "$UNSTRIPPED"/mktags > "$GENDIR"/tags.h
fi
# Recreate tags.h
$SED -ne '/TAGGED_ARRAY(/,/^)/{s/.*TAGGED_ARRAY[(]\([^,]*\),/\1/p' \
-e 's/[^{]*{"\([^"]*\)"[^{]*/ _\1/gp}' toys/*/*.c | tr '[:punct:]' _ | \
while read i; do
[ "$i" = "${i#_}" ] && { HEAD="$i"; X=0; LL=; continue;}
for j in $i; do
[ $X -eq 32 ] && LL=LL
NAME="$HEAD$j"
printf "#define $NAME %*s%s\n#define _$NAME %*s%s\n" \
$((32-${#NAME})) "" "$X" $((31-${#NAME})) "" "(1$LL<<$((X++)))" || exit 1
done
done > "$GENDIR"/tags.h || exit 1
# Create help.h, and zhelp.h if zcat enabled
hostcomp config2help

View File

@ -1,58 +0,0 @@
// Process TAGGED_ARRAY() macros to emit TAG_STRING index macros.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
char *tag = 0;
int idx = 0;
for (;;) {
char *line = 0, *s;
ssize_t len;
len = getline(&line, (void *)&len, stdin);
if (len<0) break;
while (len && isspace(line[len-1])) line[--len]=0;
// Very simple parser: If we haven't got a TAG then first line is TAG.
// Then look for { followed by "str" (must be on same line, may have
// more than one per line), for each one emit #define. Current TAG ended
// by ) at start of line.
if (!tag) {
if (!isalpha(*line)) {
fprintf(stderr, "bad tag %s\n", line);
exit(1);
}
tag = strdup(line);
idx = 0;
continue;
}
for (s = line; isspace(*s); s++);
if (*s == ')') tag = 0;
else for (;;) {
char *start;
while (*s && *s != '{') s++;
while (*s && *s != '"') s++;
if (!*s) break;
start = ++s;
while (*s && *s != '"') {
if (!isalpha(*s) && !isdigit(*s)) *s = '_';
s++;
}
printf("#define %s_%*.*s %d\n", tag, -40, (int)(s-start), start, idx);
printf("#define _%s_%*.*s (1%s<<%d)\n", tag, -39, (int)(s-start), start,
idx>31 ? "LL": "", idx);
idx++;
}
free(line);
}
}