#!/bin/sh
# mkerrors - Extract error strings from assuan.h
#            and create C source for assuan_strerror
#	Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
#
# This file is part of Assuan.
#
# Assuan is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.
#
# Assuan is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 

cat <<EOF
/* Generated automatically by mkerrors */
/* Do not edit!  See mkerrors for copyright notice. */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <assert.h>
#include <errno.h>

#undef _ASSUAN_IN_LIBASSUAN /* undef to get all error codes. */
#include "assuan.h"

/* If true the modern gpg-error style error codes are used in the
   API. */
static unsigned int err_source;

/* Enable gpg-error style error codes.  ERRSOURCE is one of gpg-error
   sources.  Note, that this function is not thread-safe and should be
   used right at startup. Switching back to the old style mode is not
   supported. */
void
assuan_set_assuan_err_source (int errsource)
{
  errsource &= 0xff;
  err_source = errsource? errsource : 31 /*GPG_ERR_SOURCE_ANY*/;
}


/* Helper to map old style Assuan error codes to gpg-error codes.
   This is used internally to keep an compatible ABI. */
assuan_error_t
_assuan_error (int oldcode)
{
  unsigned int n;

  if (!err_source)
    return (oldcode & 0x00ffffff); /* Make sure that the gpg-error
                                      source part is cleared. */

  switch (oldcode)
    {
    case ASSUAN_General_Error:           n = 257; break;
    case ASSUAN_Accept_Failed:           n = 258; break;
    case ASSUAN_Connect_Failed:          n = 259; break;
    case ASSUAN_Invalid_Response:        n = 260; break;
    case ASSUAN_Invalid_Value:           n = 261; break;
    case ASSUAN_Line_Not_Terminated:     n = 262; break;
    case ASSUAN_Line_Too_Long:           n = 263; break;
    case ASSUAN_Nested_Commands:         n = 264; break;
    case ASSUAN_No_Data_Callback:        n = 265; break;
    case ASSUAN_No_Inquire_Callback:     n = 266; break;
    case ASSUAN_Not_A_Server:            n = 267; break;
    case ASSUAN_Not_Implemented:         n =  69; break;
    case ASSUAN_Parameter_Conflict:      n = 280; break;
    case ASSUAN_Problem_Starting_Server: n = 269; break;
    case ASSUAN_Server_Fault:            n =  80; break;
    case ASSUAN_Syntax_Error:            n = 276; break;
    case ASSUAN_Too_Much_Data:           n = 273; break;
    case ASSUAN_Unexpected_Command:      n = 274; break;
    case ASSUAN_Unknown_Command:         n = 275; break;
    case ASSUAN_Canceled:                n = 277; break;
    case ASSUAN_No_Secret_Key:           n =  17; break;
    case ASSUAN_Not_Confirmed:           n = 114; break;

    case ASSUAN_Read_Error:
      switch (errno)
        {
        case 0: n = 16381; /*GPG_ERR_MISSING_ERRNO*/  break;
        default: n = 270;  /*GPG_ERR_ASS_READ_ERROR*/ break;
        }
      break;

    case ASSUAN_Write_Error:
      switch (errno)
        {
        case 0: n = 16381; /*GPG_ERR_MISSING_ERRNO*/  break;
        default: n = 271;  /*GPG_ERR_ASS_WRITE_ERROR*/ break;
        }
      break;
      
    case ASSUAN_Out_Of_Core:
      switch (errno)
        {
        case 0:  /* Should not happen but a user might have provided
                    an incomplete implemented malloc function.  Give
                    him a chance to correct this fault but make sure
                    an error is indeed returned. */
          n = 16381; /*GPG_ERR_MISSING_ERRNO*/
          break;
        case ENOMEM: n = (1 << 15) | 86; break;
        default:  
          n = 16382; /*GPG_ERR_UNKNOWN_ERRNO*/
          break;
        }
      break;

    case -1: n = 16383 /*GPG_ERR_EOF*/; break;

    default:
      n = 257; 
      break;
    }

  return ((err_source << 24) | (n & 0x00ffffff));

}


/**
 * assuan_strerror:
 * @err:  Error code 
 * 
 * This function returns a textual representaion of the given
 * errorcode. If this is an unknown value, a string with the value
 * is returned (Beware: it is hold in a static buffer).
 * 
 * Return value: String with the error description.
 **/
const char *
assuan_strerror (assuan_error_t err)
{
  const char *s;
  static char buf[50];

  switch (err)
    {
EOF

awk '
/ASSUAN_No_Error/        { okay=1 }
!okay                    {next}
/^#define[ ]+ASSUAN_[A-Za-z_]*/ { print_code($2) }
/ASSUAN_USER_ERROR_LAST/ { exit 0 }


function print_code( s )
{
printf "    case %s: s=\"", s ;
gsub(/_/, " ", s );
printf "%s\"; break;\n", tolower(substr(s,8));
}
'

cat <<EOF
  case -1: s = "EOF (-1)"; break;
    default: 
      {
        unsigned int source, code, n;

        source = ((err >> 24) & 0xff);
        code = (err & 0x00ffffff);
        if (source) 
          {
            /* Assume this is an libgpg-error and try to map the codes
               back. */
            switch (code)
              {
              case 257: n = ASSUAN_General_Error          ; break;
              case 258: n = ASSUAN_Accept_Failed          ; break;
              case 259: n = ASSUAN_Connect_Failed         ; break;
              case 260: n = ASSUAN_Invalid_Response       ; break;
              case 261: n = ASSUAN_Invalid_Value          ; break;
              case 262: n = ASSUAN_Line_Not_Terminated    ; break;
              case 263: n = ASSUAN_Line_Too_Long          ; break;
              case 264: n = ASSUAN_Nested_Commands        ; break;
              case 265: n = ASSUAN_No_Data_Callback       ; break;
              case 266: n = ASSUAN_No_Inquire_Callback    ; break;
              case 267: n = ASSUAN_Not_A_Server           ; break;
              case  69: n = ASSUAN_Not_Implemented        ; break;
              case 280: n = ASSUAN_Parameter_Conflict     ; break;
              case 269: n = ASSUAN_Problem_Starting_Server; break;
              case 270: n = ASSUAN_Read_Error             ; break;
              case 271: n = ASSUAN_Write_Error            ; break;
              case  80: n = ASSUAN_Server_Fault           ; break;
              case 276: n = ASSUAN_Syntax_Error           ; break;
              case 273: n = ASSUAN_Too_Much_Data          ; break;
              case 274: n = ASSUAN_Unexpected_Command     ; break;
              case 275: n = ASSUAN_Unknown_Command        ; break;
              case 277: n = ASSUAN_Canceled               ; break;
              case 114: n = ASSUAN_Not_Confirmed          ; break;
              case ((1<<15)|86): n = ASSUAN_Out_Of_Core   ; break;
              default:  n = 0; break;
              }
            if (n)
              s = assuan_strerror (n);
            else
              {
                sprintf (buf, "ec=%u.%u", source, code ); 
                s=buf;
              }
          }
        else
          {
            sprintf (buf, "ec=%d", err ); 
            s=buf;
          }
      }
      break;
    }

  return s;
}

EOF