libexplain  1.4.D001
libexplain/buffer/hstrerror.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - a library of system-call-specific strerror replacements
00003  * Copyright (C) 2013 Peter Miller
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU Lesser General Public License as
00007  * published by the Free Software Foundation; either version 3 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include <libexplain/ac/netdb.h>
00020 
00021 #include <libexplain/buffer/gettext.h>
00022 #include <libexplain/buffer/hstrerror.h>
00023 #include <libexplain/buffer/int.h>
00024 #include <libexplain/errno_info.h>
00025 #include <libexplain/option.h>
00026 #include <libexplain/parse_bits.h>
00027 
00028 static const explain_parse_bits_table_t table[] =
00029 {
00030 #ifdef HOST_NOT_FOUND
00031     { "HOST_NOT_FOUND", HOST_NOT_FOUND },
00032 #endif
00033 #ifdef TRY_AGAIN
00034     { "TRY_AGAIN", TRY_AGAIN },
00035 #endif
00036 #ifdef NO_RECOVERY
00037     { "NO_RECOVERY", NO_RECOVERY },
00038 #endif
00039 #ifdef NO_DATA
00040     { "NO_DATA", NO_DATA },
00041 #endif
00042 #ifdef NETDB_INTERNAL
00043     { "NETDB_INTERNAL", NETDB_INTERNAL },
00044 #endif
00045 #ifdef NETDB_SUCCESS
00046     { "NETDB_SUCCESS", NETDB_SUCCESS },
00047 #endif
00048 #ifdef NO_ADDRESS
00049     { "NO_ADDRESS", NO_ADDRESS },
00050 #endif
00051 };
00052 
00053 
00054 static void
00055 print_h_errno_name(explain_string_buffer_t *sb, int h_errno_value,
00056     int errno_value)
00057 {
00058     const explain_parse_bits_table_t *tp;
00059 
00060     for (tp = table; tp < ENDOF(table); ++tp)
00061     {
00062         if (tp->value == h_errno_value)
00063         {
00064             explain_string_buffer_puts(sb, tp->name);
00065 #ifdef NETDB_INTERNAL
00066             if (tp->value == NETDB_INTERNAL)
00067             {
00068                 const explain_errno_info_t *eip;
00069 
00070                 eip = explain_errno_info_by_number(errno_value);
00071                 if (eip)
00072                 {
00073                     explain_string_buffer_putc(sb, ' ');
00074                     explain_string_buffer_puts(sb, eip->name);
00075                 }
00076             }
00077 #endif
00078             return;
00079         }
00080     }
00081 }
00082 
00083 
00084 static void
00085 print_h_errno_description(explain_string_buffer_t *sb, int h_errno_value,
00086     int errno_value)
00087 {
00088     const explain_parse_bits_table_t *tp;
00089 
00090     for (tp = table; tp < ENDOF(table); ++tp)
00091     {
00092         if (tp->value == h_errno_value)
00093         {
00094             explain_string_buffer_puts(sb, hstrerror(h_errno_value));
00095 #ifdef NETDB_INTERNAL
00096             if (tp->value == NETDB_INTERNAL)
00097             {
00098                 const explain_errno_info_t *eip;
00099 
00100                 eip = explain_errno_info_by_number(errno_value);
00101                 if (eip)
00102                 {
00103                     explain_string_buffer_putc(sb, ' ');
00104                     explain_string_buffer_puts(sb, eip->description);
00105                 }
00106             }
00107 #endif
00108             return;
00109         }
00110     }
00111 
00112     /* no idea */
00113     explain_buffer_gettext
00114     (
00115         sb,
00116 
00117         /*
00118          * xgettext: This message is used when hstreror is unable to translate
00119          * an h_errno value, in which causes this fall-back message to be used.
00120          */
00121         i18n("unknown <netdb.h> error")
00122     );
00123 }
00124 
00125 
00126 void
00127 explain_buffer_hstrerror(explain_string_buffer_t *sb, int h_errno_value,
00128     int errno_value)
00129 {
00130     print_h_errno_description(sb, h_errno_value, errno_value);
00131     explain_string_buffer_puts(sb, " (");
00132     if (explain_option_numeric_errno())
00133     {
00134         explain_buffer_int(sb, h_errno_value);
00135 #ifdef NETDB_INTERNAL
00136         if (h_errno_value == NETDB_INTERNAL)
00137         {
00138             explain_string_buffer_putc(sb, ' ');
00139             explain_buffer_int(sb, errno_value);
00140         }
00141 #endif
00142         explain_string_buffer_puts(sb, ", ");
00143     }
00144     print_h_errno_name(sb, h_errno_value, errno_value);
00145     explain_string_buffer_putc(sb, ')');
00146 }
00147 
00148 
00149 /* vim: set ts=8 sw=4 et : */