1# mkstrtable.awk 2# Copyright (C) 2003 g10 Code GmbH 3# 4# This program is free software; you can redistribute it and/or 5# modify it under the terms of the GNU General Public License as 6# published by the Free Software Foundation; either version 2 of 7# the License, or (at your option) any later version. 8# 9# This program is distributed in the hope that it will be useful, 10# but WITHOUT ANY WARRANTY; without even the implied warranty of 11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12# General Public License for more details. 13# 14# You should have received a copy of the GNU General Public License 15# along with this program; if not, write to the Free Software 16# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 17# 18# As a special exception, g10 Code GmbH gives unlimited permission to 19# copy, distribute and modify the C source files that are the output 20# of mkerrcodes2.awk. You need not follow the terms of the GNU General 21# Public License when using or distributing such scripts, even though 22# portions of the text of mkerrcodes2.awk appear in them. The GNU 23# General Public License (GPL) does govern all other use of the material 24# that constitutes the mkerrcodes2.awk program. 25# 26# Certain portions of the mkerrcodes2.awk source text are designed to be 27# copied (in certain cases, depending on the input) into the output of 28# mkerrcodes2.awk. We call these the "data" portions. The rest of the 29# mkerrcodes2.awk source text consists of comments plus executable code 30# that decides which of the data portions to output in any given case. 31# We call these comments and executable code the "non-data" portions. 32# mkstrtable.h never copies any of the non-data portions into its output. 33# 34# This special exception to the GPL applies to versions of mkerrcodes2.awk 35# released by g10 Code GmbH. When you make and distribute a modified version 36# of mkerrcodes2.awk, you may extend this special exception to the GPL to 37# apply to your modified version as well, *unless* your modified version 38# has the potential to copy into its output some of the text that was the 39# non-data portion of the version that you started with. (In other words, 40# unless your change moves or copies text from the non-data portions to the 41# data portions.) If your modification has such potential, you must delete 42# any notice of this special exception to the GPL from your modified version. 43 44# This script outputs a source file that does define the following 45# symbols: 46# 47# static const char msgstr[]; 48# A string containing all messages in the list. 49# 50# static const int msgidx[]; 51# A list of index numbers, one for each message, that points to the 52# beginning of the string in msgstr. 53# 54# msgidxof (code); 55# A macro that maps code numbers to idx numbers. If a DEFAULT MESSAGE 56# is provided (see below), its index will be returned for unknown codes. 57# Otherwise -1 is returned for codes that do not appear in the list. 58# You can lookup the message with code CODE with: 59# msgstr + msgidx[msgidxof (code)]. 60# 61# The input file has the following format: 62# CODE1 MESSAGE1 (Code number, <tab>, message string) 63# CODE2 MESSAGE2 (Code number, <tab>, message string) 64# ... 65# CODEn MESSAGEn (Code number, <tab>, message string) 66# DEFAULT MESSAGE (<tab>, fall-back message string) 67# 68# Comments (starting with # and ending at the end of the line) are removed, 69# as is trailing whitespace. The last line is optional; if no DEFAULT 70# MESSAGE is given, msgidxof will return the number -1 for unknown 71# index numbers. 72 73BEGIN { 74# msg holds the number of messages. 75 msg = 0; 76 print "/* Output of mkerrcodes2.awk. DO NOT EDIT. */"; 77 print ""; 78 header = 1; 79} 80 81/^#/ { next; } 82 83header { 84 if ($1 ~ /^[0123456789]+$/) 85 { 86 print "static const int err_code_from_index[] = {"; 87 header = 0; 88 } 89 else 90 print; 91} 92 93!header { 94 sub (/\#.+/, ""); 95 sub (/[ ]+$/, ""); # Strip trailing space and tab characters. 96 97 if (/^$/) 98 next; 99 100# Print the string msgstr line by line. We delay output by one line to be able 101# to treat the last line differently (see END). 102 print " " $2 ","; 103 104# Remember the error value and index of each error code. 105 code[msg] = $1; 106 pos[msg] = $2; 107 msg++; 108} 109END { 110 print "};"; 111 print ""; 112 print "#define errno_to_idx(code) (0 ? -1 \\"; 113 114# Gather the ranges. 115 skip = code[0]; 116 start = code[0]; 117 stop = code[0]; 118 for (i = 1; i < msg; i++) 119 { 120 if (code[i] == stop + 1) 121 stop++; 122 else 123 { 124 print " : ((code >= " start ") && (code <= " stop ")) ? (code - " \ 125 skip ") \\"; 126 skip += code[i] - stop - 1; 127 start = code[i]; 128 stop = code[i]; 129 } 130 } 131 print " : ((code >= " start ") && (code <= " stop ")) ? (code - " \ 132 skip ") \\"; 133 print " : -1)"; 134} 135