00001 /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> 00002 * All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions are met: 00006 * 00007 * Redistributions of source code must retain the above copyright notice, this 00008 * list of conditions and the following disclaimer. 00009 * 00010 * Redistributions in binary form must reproduce the above copyright notice, 00011 * this list of conditions and the following disclaimer in the documentation 00012 * and/or other materials provided with the distribution. 00013 * 00014 * Neither the name of the original program's authors nor the names of its 00015 * contributors may be used to endorse or promote products derived from this 00016 * software without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00021 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00022 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00028 * POSSIBILITY OF SUCH DAMAGE. 00029 */ 00030 00031 #include <stdio.h> 00032 #include <str.h> 00033 #include <stdlib.h> 00034 #include <unistd.h> 00035 #include <stdarg.h> 00036 00037 #include "config.h" 00038 #include "errors.h" 00039 #include "errstr.h" 00040 00041 volatile int cli_errno = CL_EOK; 00042 extern volatile unsigned int cli_quit; 00043 00044 /* Error printing, translation and handling functions */ 00045 00046 00047 /* Look up errno in cl_errors and return the corresponding string. 00048 * Return NULL if not found */ 00049 static const char *err2str(int err) 00050 { 00051 00052 if (NULL != cl_errors[err]) 00053 return cl_errors[err]; 00054 00055 return (char *)NULL; 00056 } 00057 00058 /* Print an error report signifying errno, which is translated to 00059 * its corresponding human readable string. If errno > 0, raise the 00060 * cli_quit int that tells the main program loop to exit immediately */ 00061 00062 void cli_error(int err, const char *fmt, ...) 00063 { 00064 va_list vargs; 00065 va_start(vargs, fmt); 00066 vprintf(fmt, vargs); 00067 va_end(vargs); 00068 00069 if (NULL != err2str(err)) 00070 printf(" (%s)\n", err2str(err)); 00071 else 00072 printf(" (Unknown Error %d)\n", err); 00073 00074 /* If fatal, raise cli_quit so that we try to exit 00075 * gracefully. This will break the main loop and 00076 * invoke the destructor */ 00077 if (err == CL_EFATAL) 00078 cli_quit = 1; 00079 00080 return; 00081 00082 } 00083 00084 00085 00086 00087