1# Copyright 2010-2021 Free Software Foundation, Inc.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License,
6# or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15#
16
17#######################################################
18# From gawk manual
19# ord.awk --- do ord and chr
20
21# Global identifiers:
22#    _ord_:        numerical values indexed by characters
23#    _ord_init:    function to initialize _ord_
24
25BEGIN    { _ord_init() }
26
27function _ord_init(    low, high, i, t)
28{
29    low = sprintf("%c", 7) # BEL is ascii 7
30    if (low == "\a") {    # regular ascii
31        low = 0
32        high = 127
33    } else if (sprintf("%c", 128 + 7) == "\a") {
34        # ascii, mark parity
35        low = 128
36        high = 255
37    } else {        # ebcdic(!)
38        low = 0
39        high = 255
40    }
41
42    for (i = low; i <= high; i++) {
43        t = sprintf("%c", i)
44        _ord_[t] = i
45    }
46}
47
48function ord(str,    c)
49{
50    # only first character is of interest
51    c = substr(str, 1, 1)
52    return _ord_[c]
53}
54
55#######################################################
56
57
58BEGIN {
59    bs_escapes["\\n"] = "\n"
60    bs_escapes["\\f"] = "\f"
61    bs_escapes["\\t"] = "\t"
62    bs_escapes["\\\\"] = "\\"
63    bs_escapes["\\\""] = "\""
64    bs_escapes["\\x20"] = " "
65
66    for (v in bs_escapes) {
67        inv_bs_escapes[bs_escapes[v]] = v
68    }
69
70    if (srcdir == "") {
71        srcdir = "."
72    }
73    CD = srcdir "/command_data.c"
74    CI = srcdir "/command_ids.h"
75
76    print "/* This file automatically generated by command_data.awk */" > CI
77    print "#ifndef COMMAND_IDS_H"                 > CI
78    print "#define COMMAND_IDS_H"                 > CI
79    print                                         > CI
80    print "/* Useful aliases */"                  > CI
81    print "#define CM_hex_09 CM_TAB"              > CI
82    print "#define CM_hex_0a CM_NEWLINE"          > CI
83    print "#define CM_hex_20 CM_SPACE"            > CI
84    print "#define CM_hex_21 CM_EXCLAMATION_MARK" > CI
85    print "#define CM_hex_22 CM_POUND_SIGN"       > CI
86    print "#define CM_hex_27 CM_APOSTROPHE"       > CI
87    print "#define CM_hex_2a CM_ASTERISK"         > CI
88    print "#define CM_hex_2c CM_COMMA"            > CI
89    print "#define CM_hex_2d CM_HYPHEN"           > CI
90    print "#define CM_hex_2e CM_FULL_STOP"        > CI
91    print "#define CM_hex_2f CM_SLASH"            > CI
92    print "#define CM_hex_3a CM_COLON"            > CI
93    print "#define CM_hex_3d CM_EQUALS"           > CI
94    print "#define CM_hex_3f CM_QUESTION_MARK"    > CI
95    print "#define CM_hex_40 CM_AT_SIGN"          > CI
96    print "#define CM_hex_5c CM_BACKSLASH"        > CI
97    print "#define CM_hex_5e CM_CIRCUMFLEX"       > CI
98    print "#define CM_hex_60 CM_BACKQUOTE"        > CI
99    print "#define CM_hex_7b CM_OPEN_BRACE"       > CI
100    print "#define CM_hex_7c CM_VERTICAL_BAR"     > CI
101    print "#define CM_hex_7d CM_CLOSE_BRACE"      > CI
102    print "#define CM_hex_7e CM_TILDE"            > CI
103    print                                         > CI
104    print "/* Defined on MS-Windows */"           > CI
105    print "#undef CM_NONE"                        > CI
106    print                                         > CI
107    print "enum command_id {"                     > CI
108    print "CM_NONE,"                              > CI
109    print                                         > CI
110
111}
112
113!/^$/ && !/^#/ {
114    if ($1 in bs_escapes) {
115        c = bs_escapes[$1]
116    } else {
117        c = $1
118    }
119    commands[c] = $2
120    data[c] = $3
121}
122
123END {
124    print "COMMAND builtin_command_data[] = {" > CD
125
126    print "0, 0, 0," > CD
127
128    # We want the output sorted so we can use bsearch
129    PROCINFO["sorted_in"]="@ind_str_asc"
130    for (c in commands) {
131        # Single character commands with unusual names
132        if (c ~ /^[^[:alpha:]]$/) {
133                if (c in inv_bs_escapes) {
134                    c2 = inv_bs_escapes[c]
135                } else
136                    c2 = c
137                printf "CM_hex_%02x,\n", ord(c) > CI
138        } else {
139                c2 = c
140                print "CM_" c "," > CI
141        }
142
143        if (commands[c] != "") {
144            flags = "CF_" commands[c]
145            gsub (/,/, " | CF_", flags)
146        } else {
147            flags = "0"
148        }
149
150        if (data[c] != "") {
151            command_data = data[c]
152        } else {
153            command_data = "0"
154        }
155        print "\"" c2 "\", " flags ", " command_data "," > CD
156    }
157    print "};" > CD
158    print "};" > CI
159    print "#endif" > CI
160}
161
162
163