libexplain
1.4.D001
|
00001 /* 00002 * libexplain - Explain errno values returned by libc functions 00003 * Copyright (C) 2008-2010, 2013 Peter Miller 00004 * Written by Peter Miller <pmiller@opensource.org.au> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as 00008 * published by the Free Software Foundation; either version 3 of the 00009 * License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include <libexplain/ac/stdio.h> 00021 00022 #include <libexplain/common_message_buffer.h> 00023 #include <libexplain/parse_bits.h> 00024 #include <libexplain/program_name.h> 00025 #include <libexplain/string_buffer.h> 00026 #include <libexplain/option.h> 00027 #include <libexplain/output.h> 00028 00029 00030 int 00031 explain_parse_bits_or_die(const char *text, 00032 const explain_parse_bits_table_t *table, size_t table_size, 00033 const char *caption) 00034 { 00035 int result; 00036 00037 result = -1; 00038 if (explain_parse_bits(text, table, table_size, &result) < 0) 00039 { 00040 explain_string_buffer_t sb; 00041 char errstr[500]; 00042 00043 snprintf(errstr, sizeof(errstr), "%s", explain_parse_bits_get_error()); 00044 explain_string_buffer_init 00045 ( 00046 &sb, 00047 explain_common_message_buffer, 00048 explain_common_message_buffer_size 00049 ); 00050 if (explain_option_assemble_program_name()) 00051 { 00052 const char *prog; 00053 00054 prog = explain_program_name_get(); 00055 if (prog && *prog) 00056 { 00057 explain_string_buffer_puts(&sb, prog); 00058 explain_string_buffer_puts(&sb, ": "); 00059 } 00060 } 00061 if (caption) 00062 { 00063 explain_string_buffer_puts(&sb, caption); 00064 explain_string_buffer_puts(&sb, ": "); 00065 } 00066 explain_string_buffer_puts(&sb, errstr); 00067 explain_output_message(explain_common_message_buffer); 00068 explain_output_exit_failure(); 00069 } 00070 return result; 00071 } 00072 00073 00074 /* vim: set ts=8 sw=4 et : */