3 # Copyright © 2021, 2023 Nick Bowler
5 # Generate a C string table based on an input string specification file.
7 # A string table is a single large char single array containing all of
8 # the specified (0-terminated) strings, which is then offset to obtain
9 # the desired string. By storing these offsets instead of string pointers
10 # into read-only data structures, this can reduce the need for relocation
11 # processing at startup when programs are built in PIC mode.
13 # The string specification file is processed line by line. Comment
14 # lines may be included by beginning the line with a # character, which
15 # must be the very first character on the line. If a comment is encountered,
16 # processing immediately moves on to the next line and the result is as if
17 # the comment line were omitted from the input.
19 # Options may be used to alter the normal behaviour. An option is placed
20 # on a line by itself beginning with an @ character, and may appear anywhere
21 # in the input file. The following options are defined:
24 # All strings will have a non-zero offset in the strtab.
26 # A string is defined by beginning a line with an & character, which must
27 # be immediately followed by a C identifier. A nonempty sequence of
28 # whitespace (with at most one newline) separates the identifier from the
29 # beginning of the string itself. This whitespace is never included in the
32 # The string is then interpreted as follows:
34 # - Leading blanks on each line are ignored.
35 # - The sequences \\, \a, \b, \t, \n, \v, \f and \r can be entered and
36 # mean the same as they do in C string literals. The "\\" sequence
37 # prevents any special interpretation of the second backslash.
38 # - Newlines in the input are included in the output, except for the
39 # where the entire string (including its identifier) are on one line.
40 # - If this is not desired, a newline which is immediately preceded by an
41 # unescaped backslash will deleted, along with the backslash.
42 # - All other backslashes are deleted. This can be used to prevent special
43 # handling of whitespace, # or & characters at the beginning of a line.
45 # The output defines a variable, strtab, which contains all of the strings,
46 # and each identifier in the input is declared as an emumeration constant
47 # whose value is the offset of the associated string within strtab.
49 # The object-like macro STRTAB_MAX_OFFSET is defined and expands to the
50 # greatest string offset, suitable for use in #if preprocessing directives.
52 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
53 # This is free software: you are free to do what the fuck you want to.
54 # There is NO WARRANTY, to the extent permitted by law.
59 print " * Automatically generated by gen-strtab.awk from " FILENAME
61 print " * Automatically generated by gen-strtab.awk"
63 print " * Do not edit."
69 collected = ident = ""
70 startline = endline = 0
75 NF == 0 || $0 ~ /^[#]/ { next }
82 val = !sub(/^no_?/, "", $1);
86 print "error: unrecognized option: @" orig | "cat 1>&2"
95 finish_string_input(strings, ident, collected)
96 vars[num_vars++] = ident
110 collected = collected "\n" $0
120 finish_string_input(strings, ident, collected)
121 vars[num_vars++] = ident
128 count = bucketsort(sorted_strings, strings)
131 print "\n#define STR_L10N_(x)"
133 print "# define N_(x) x"
135 print "\nstatic const char strtab[] ="
140 for (i = 0; i < count; i++) {
141 s = sorted_strings[i]
142 gsub(/\\\\/, "\2", s)
143 if ((n = index(strtab "\1", s "\1")) > 0) {
144 offsets[sorted_strings[i]] = real_length(substr(strtab, 1, n-1))
145 print "\tSTR_L10N_(N_(\"" sorted_strings[i] "\"))"
147 strtab = strtab "\1" s
148 offsets[sorted_strings[i]] = strtab_len + 1
149 strtab_len += real_length(s) + 1
152 offsets[sorted_strings[i]] = 0
153 strtab_len += real_length(s)
157 gsub(/\2/, "\\\\", strtab)
158 gsub(/\1/, "\")\"\\0\"\n\tN_(\"", strtab)
159 print "\tN_(\"" strtab "\")"
163 for (i = 0; i < num_vars; i++) {
164 sep = (i+1) != num_vars ? "," : ""
166 o = offsets[strings[s]] + !opts["zero"]
167 print "\t" s " = " o sep
173 print "\n#define STRTAB_MAX_OFFSET " max
176 # finish_input_string(strings, ident, val)
178 # Deal with backslash-escapes and special characters in val, then set
179 # strings[ident] = val.
180 function finish_string_input(strings, ident, val, n, tmpval)
182 gsub(/\\\\/, "\1", val)
183 val = val (endline > startline ? "\n" : "")
184 gsub(/\\\n/, "", val)
187 while ((n = match(val, /\\[^abtnvfr]/)) > 0) {
188 tmpval = tmpval substr(val, 1, n-1)
189 val = substr(val, n+1)
193 # Escape special characters
194 gsub(/"/, "\\\"", tmpval)
195 gsub(/\t/, "\\t", tmpval)
196 gsub(/\n/, "\\n", tmpval)
197 gsub(/\1/, "\\\\", tmpval)
199 strings[ident] = tmpval
202 function real_length(s, t)
205 return t - gsub(/\\./, "&", s)
208 # bucketsort(dst, src)
210 # Sort the elements of src by descending string length,
211 # placing them into dst[0] ... dst[n].
213 # Returns the number of elements.
214 function bucketsort(dst, src, buckets, max, count, i, t)
218 if (i > max) { max = i }
222 for (i = max; i > 0; i--) {
231 i = length(t = src[t])
232 dst[buckets[i]++] = t