Blame src/mkstrtable.awk

Packit fc043f
# mkstrtable.awk
Packit fc043f
# Copyright (C) 2003, 2004, 2008 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 mkstrtable.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 mkstrtable.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 mkstrtable.awk program.
Packit fc043f
#
Packit fc043f
# Certain portions of the mkstrtable.awk source text are designed to be
Packit fc043f
# copied (in certain cases, depending on the input) into the output of
Packit fc043f
# mkstrtable.awk.  We call these the "data" portions.  The rest of the
Packit fc043f
# mkstrtable.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 mkstrtable.awk
Packit fc043f
# released by g10 Code GmbH.  When you make and distribute a modified version
Packit fc043f
# of mkstrtable.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 nr, <tab>, something, <tab>, msg)
Packit fc043f
# CODE2	...	MESSAGE2	(code nr, <tab>, something, <tab>, msg)
Packit fc043f
# ...
Packit fc043f
# CODEn	...	MESSAGEn	(code nr, <tab>, something, <tab>, msg)
Packit fc043f
# 	...	DEFAULT-MESSAGE	(<tab>, something, <tab>, fall-back msg)
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
# The field to be used is specified with the variable "textidx" on
Packit fc043f
# the command line.  It defaults to 2.
Packit fc043f
#
Packit fc043f
# The variable nogettext can be set to 1 to suppress gettext markers.
Packit fc043f
#
Packit fc043f
# The variable prefix can be used to prepend a string to each message.
Packit fc043f
#
Packit fc043f
# The variable namespace can be used to prepend a string to each
Packit fc043f
# variable and macro name.
Packit fc043f
Packit fc043f
BEGIN {
Packit fc043f
  FS = "[\t]+";
Packit fc043f
# cpos holds the current position in the message string.
Packit fc043f
  cpos = 0;
Packit fc043f
# msg holds the number of messages.
Packit fc043f
  msg = 0;
Packit fc043f
  print "/* Output of mkstrtable.awk.  DO NOT EDIT.  */";
Packit fc043f
  print "";
Packit fc043f
  header = 1;
Packit fc043f
  if (textidx == 0)
Packit fc043f
    textidx = 2;
Packit fc043f
# nogettext can be set to 1 to suppress gettext noop markers.
Packit fc043f
}
Packit fc043f
Packit fc043f
/^#/ { next; }
Packit fc043f
Packit fc043f
header {
Packit fc043f
  if ($1 ~ /^[0123456789]+$/)
Packit fc043f
    {
Packit fc043f
      print "/* The purpose of this complex string table is to produce";
Packit fc043f
      print "   optimal code with a minimum of relocations.  */";
Packit fc043f
      print "";
Packit fc043f
      print "static const char " namespace "msgstr[] = ";
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
  if (last_msgstr)
Packit fc043f
    {
Packit fc043f
      if (nogettext)
Packit fc043f
	print "  \"" last_msgstr "\" \"\\0\"";
Packit fc043f
      else
Packit fc043f
	print "  gettext_noop (\"" last_msgstr "\") \"\\0\"";
Packit fc043f
    }
Packit fc043f
  last_msgstr = prefix $textidx;
Packit fc043f
Packit fc043f
# Remember the error code and msgidx of each error message.
Packit fc043f
  code[msg] = $1;
Packit fc043f
  pos[msg] = cpos;
Packit fc043f
  cpos += length (last_msgstr) + 1;
Packit fc043f
  msg++;
Packit fc043f
Packit fc043f
  if ($1 == "")
Packit fc043f
    {
Packit fc043f
      has_default = 1;
Packit fc043f
      exit;
Packit fc043f
    }
Packit fc043f
}
Packit fc043f
END {
Packit fc043f
  if (has_default)
Packit fc043f
    coded_msgs = msg - 1;
Packit fc043f
  else
Packit fc043f
    coded_msgs = msg;
Packit fc043f
Packit fc043f
  if (nogettext)
Packit fc043f
    print "  \"" last_msgstr "\";";
Packit fc043f
  else
Packit fc043f
    print "  gettext_noop (\"" last_msgstr "\");";
Packit fc043f
  print "";
Packit fc043f
  print "static const int " namespace "msgidx[] =";
Packit fc043f
  print "  {";
Packit fc043f
  for (i = 0; i < coded_msgs; i++)
Packit fc043f
    print "    " pos[i] ",";
Packit fc043f
  print "    " pos[coded_msgs];
Packit fc043f
  print "  };";
Packit fc043f
  print "";
Packit fc043f
  print "static GPG_ERR_INLINE int";
Packit fc043f
  print namespace "msgidxof (int code)";
Packit fc043f
  print "{";
Packit fc043f
  print "  return (0 ? 0";
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 < coded_msgs; 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
  if (has_default)
Packit fc043f
    print "  : " stop + 1 " - " skip ");";
Packit fc043f
  else
Packit fc043f
    print "  : -1);";
Packit fc043f
  print "}";
Packit fc043f
}