Blame src/mkerrcodes2.awk

Packit fc043f
# mkstrtable.awk
Packit fc043f
# Copyright (C) 2003 g10 Code GmbH
Packit fc043f
# 
Packit fc043f
# This program is free software; you can redistribute it and/or
Packit fc043f
# modify it under the terms of the GNU General Public License as
Packit fc043f
# published by the Free Software Foundation; either version 2 of
Packit fc043f
# the License, or (at your option) any later version.
Packit fc043f
#
Packit fc043f
# This program is distributed in the hope that it will be useful,
Packit fc043f
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit fc043f
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit fc043f
# General Public License for more details.
Packit fc043f
#
Packit fc043f
# You should have received a copy of the GNU General Public License
Packit fc043f
# along with this program; if not, write to the Free Software
Packit fc043f
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
Packit fc043f
#
Packit fc043f
# As a special exception, g10 Code GmbH gives unlimited permission to
Packit fc043f
# copy, distribute and modify the C source files that are the output
Packit fc043f
# of mkerrcodes2.awk.  You need not follow the terms of the GNU General
Packit fc043f
# Public License when using or distributing such scripts, even though
Packit fc043f
# portions of the text of mkerrcodes2.awk appear in them.  The GNU
Packit fc043f
# General Public License (GPL) does govern all other use of the material
Packit fc043f
# that constitutes the mkerrcodes2.awk program.
Packit fc043f
#
Packit fc043f
# Certain portions of the mkerrcodes2.awk source text are designed to be
Packit fc043f
# copied (in certain cases, depending on the input) into the output of
Packit fc043f
# mkerrcodes2.awk.  We call these the "data" portions.  The rest of the
Packit fc043f
# mkerrcodes2.awk source text consists of comments plus executable code
Packit fc043f
# that decides which of the data portions to output in any given case.
Packit fc043f
# We call these comments and executable code the "non-data" portions.
Packit fc043f
# mkstrtable.h never copies any of the non-data portions into its output.
Packit fc043f
#
Packit fc043f
# This special exception to the GPL applies to versions of mkerrcodes2.awk
Packit fc043f
# released by g10 Code GmbH.  When you make and distribute a modified version
Packit fc043f
# of mkerrcodes2.awk, you may extend this special exception to the GPL to
Packit fc043f
# apply to your modified version as well, *unless* your modified version
Packit fc043f
# has the potential to copy into its output some of the text that was the
Packit fc043f
# non-data portion of the version that you started with.  (In other words,
Packit fc043f
# unless your change moves or copies text from the non-data portions to the
Packit fc043f
# data portions.)  If your modification has such potential, you must delete
Packit fc043f
# any notice of this special exception to the GPL from your modified version.
Packit fc043f
Packit fc043f
# This script outputs a source file that does define the following
Packit fc043f
# symbols:
Packit fc043f
#
Packit fc043f
# static const char msgstr[];
Packit fc043f
# A string containing all messages in the list.
Packit fc043f
#
Packit fc043f
# static const int msgidx[];
Packit fc043f
# A list of index numbers, one for each message, that points to the
Packit fc043f
# beginning of the string in msgstr.
Packit fc043f
#
Packit fc043f
# msgidxof (code);
Packit fc043f
# A macro that maps code numbers to idx numbers.  If a DEFAULT MESSAGE
Packit fc043f
# is provided (see below), its index will be returned for unknown codes.
Packit fc043f
# Otherwise -1 is returned for codes that do not appear in the list.
Packit fc043f
# You can lookup the message with code CODE with:
Packit fc043f
# msgstr + msgidx[msgidxof (code)].
Packit fc043f
#
Packit fc043f
# The input file has the following format:
Packit fc043f
# CODE1	MESSAGE1		(Code number, <tab>, message string)
Packit fc043f
# CODE2	MESSAGE2		(Code number, <tab>, message string)
Packit fc043f
# ...
Packit fc043f
# CODEn	MESSAGEn		(Code number, <tab>, message string)
Packit fc043f
# 	DEFAULT MESSAGE		(<tab>, fall-back message string)
Packit fc043f
#
Packit fc043f
# Comments (starting with # and ending at the end of the line) are removed,
Packit fc043f
# as is trailing whitespace.  The last line is optional; if no DEFAULT
Packit fc043f
# MESSAGE is given, msgidxof will return the number -1 for unknown
Packit fc043f
# index numbers.
Packit fc043f
Packit fc043f
BEGIN {
Packit fc043f
# msg holds the number of messages.
Packit fc043f
  msg = 0;
Packit fc043f
  print "/* Output of mkerrcodes2.awk.  DO NOT EDIT.  */";
Packit fc043f
  print "";
Packit fc043f
  header = 1;
Packit fc043f
}
Packit fc043f
Packit fc043f
/^#/ { next; }
Packit fc043f
Packit fc043f
header {
Packit fc043f
  if ($1 ~ /^[0123456789]+$/)
Packit fc043f
    {
Packit fc043f
      print "static const int err_code_from_index[] = {";
Packit fc043f
      header = 0;
Packit fc043f
    }
Packit fc043f
  else
Packit fc043f
    print;
Packit fc043f
}
Packit fc043f
Packit fc043f
!header {
Packit fc043f
  sub (/\#.+/, "");
Packit fc043f
  sub (/[ 	]+$/, ""); # Strip trailing space and tab characters.
Packit fc043f
Packit fc043f
  if (/^$/)
Packit fc043f
    next;
Packit fc043f
Packit fc043f
# Print the string msgstr line by line.  We delay output by one line to be able
Packit fc043f
# to treat the last line differently (see END).
Packit fc043f
  print "  " $2 ",";
Packit fc043f
Packit fc043f
# Remember the error value and index of each error code.
Packit fc043f
  code[msg] = $1;
Packit fc043f
  pos[msg] = $2;
Packit fc043f
  msg++;
Packit fc043f
}
Packit fc043f
END {
Packit fc043f
  print "};";
Packit fc043f
  print "";
Packit fc043f
  print "#define errno_to_idx(code) (0 ? -1 \\";
Packit fc043f
Packit fc043f
# Gather the ranges.
Packit fc043f
  skip = code[0];
Packit fc043f
  start = code[0];
Packit fc043f
  stop = code[0];
Packit fc043f
  for (i = 1; i < msg; i++)
Packit fc043f
    {
Packit fc043f
      if (code[i] == stop + 1)
Packit fc043f
	stop++;
Packit fc043f
      else
Packit fc043f
	{
Packit fc043f
	  print "  : ((code >= " start ") && (code <= " stop ")) ? (code - " \
Packit fc043f
            skip ") \\";
Packit fc043f
	  skip += code[i] - stop - 1;
Packit fc043f
	  start = code[i];
Packit fc043f
	  stop = code[i];
Packit fc043f
	}
Packit fc043f
    }
Packit fc043f
  print "  : ((code >= " start ") && (code <= " stop ")) ? (code - " \
Packit fc043f
    skip ") \\";
Packit fc043f
  print "  : -1)";
Packit fc043f
}