libexplain
1.4.D001
|
00001 /* 00002 * libexplain - Explain errno values returned by libc functions 00003 * Copyright (C) 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/assert.h> 00021 #include <libexplain/ac/errno.h> 00022 #include <libexplain/ac/stdlib.h> 00023 00024 #include <libexplain/output.h> 00025 #include <libexplain/output/stderr.h> 00026 00027 00028 explain_output_t * 00029 explain_output_new(const explain_output_vtable_t *vtable) 00030 { 00031 unsigned int size; 00032 explain_output_t *result; 00033 00034 assert(vtable); 00035 size = vtable->size; 00036 if (size < sizeof(explain_output_t)) 00037 size = sizeof(explain_output_t); 00038 result = malloc(size); 00039 if (!result) 00040 return NULL; 00041 result->vtable = vtable; 00042 result->exit_has_been_used = 0; 00043 return result; 00044 } 00045 00046 00047 void 00048 explain_output_method_destructor(explain_output_t *op) 00049 { 00050 if (!op) 00051 return; 00052 if (op == &explain_output_static_stderr) 00053 return; 00054 assert(op->vtable); 00055 if (op->vtable->destructor) 00056 op->vtable->destructor(op); 00057 free(op); 00058 } 00059 00060 00061 void 00062 explain_output_method_message(explain_output_t *op, const char *text) 00063 { 00064 int hold_errno; 00065 00066 hold_errno = errno; 00067 assert(op); 00068 assert(op->vtable); 00069 assert(op->vtable->message); 00070 assert(text); 00071 op->vtable->message(op, text); 00072 errno = hold_errno; 00073 } 00074 00075 00076 void _exit(int); 00077 00078 00079 void 00080 explain_output_method_exit(explain_output_t *op, int status) 00081 { 00082 assert(op); 00083 assert(op->vtable); 00084 if (op->vtable->exit) 00085 op->vtable->exit(op, status); 00086 00092 if (op->exit_has_been_used) 00093 _exit(status); 00094 00095 /* 00096 * 1. We call ::exit if they didn't supply an exit method. 00097 * 2. We ::exit if their exit method returns, because the rest of 00098 * the libexplain code assumes that exit means ::exit. 00099 */ 00100 exit(status); 00101 op->exit_has_been_used = 1; 00102 } 00103 00104 00105 /* vim: set ts=8 sw=4 et : */