xref: /freebsd/crypto/heimdal/lib/com_err/com_err.c (revision c19800e8cd5640693f36f2040db4ab5e8d738146)
15e9cd1aeSAssar Westerlund /*
28373020dSJacques Vidrine  * Copyright (c) 1997 - 2002 Kungliga Tekniska H�gskolan
35e9cd1aeSAssar Westerlund  * (Royal Institute of Technology, Stockholm, Sweden).
45e9cd1aeSAssar Westerlund  * All rights reserved.
55e9cd1aeSAssar Westerlund  *
65e9cd1aeSAssar Westerlund  * Redistribution and use in source and binary forms, with or without
75e9cd1aeSAssar Westerlund  * modification, are permitted provided that the following conditions
85e9cd1aeSAssar Westerlund  * are met:
95e9cd1aeSAssar Westerlund  *
105e9cd1aeSAssar Westerlund  * 1. Redistributions of source code must retain the above copyright
115e9cd1aeSAssar Westerlund  *    notice, this list of conditions and the following disclaimer.
125e9cd1aeSAssar Westerlund  *
135e9cd1aeSAssar Westerlund  * 2. Redistributions in binary form must reproduce the above copyright
145e9cd1aeSAssar Westerlund  *    notice, this list of conditions and the following disclaimer in the
155e9cd1aeSAssar Westerlund  *    documentation and/or other materials provided with the distribution.
165e9cd1aeSAssar Westerlund  *
175e9cd1aeSAssar Westerlund  * 3. Neither the name of the Institute nor the names of its contributors
185e9cd1aeSAssar Westerlund  *    may be used to endorse or promote products derived from this software
195e9cd1aeSAssar Westerlund  *    without specific prior written permission.
205e9cd1aeSAssar Westerlund  *
215e9cd1aeSAssar Westerlund  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
225e9cd1aeSAssar Westerlund  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235e9cd1aeSAssar Westerlund  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245e9cd1aeSAssar Westerlund  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
255e9cd1aeSAssar Westerlund  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265e9cd1aeSAssar Westerlund  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275e9cd1aeSAssar Westerlund  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285e9cd1aeSAssar Westerlund  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295e9cd1aeSAssar Westerlund  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305e9cd1aeSAssar Westerlund  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315e9cd1aeSAssar Westerlund  * SUCH DAMAGE.
325e9cd1aeSAssar Westerlund  */
335e9cd1aeSAssar Westerlund 
345e9cd1aeSAssar Westerlund #ifdef HAVE_CONFIG_H
355e9cd1aeSAssar Westerlund #include <config.h>
36c19800e8SDoug Rabson RCSID("$Id: com_err.c 14930 2005-04-24 19:43:06Z lha $");
375e9cd1aeSAssar Westerlund #endif
385e9cd1aeSAssar Westerlund #include <stdio.h>
395e9cd1aeSAssar Westerlund #include <stdlib.h>
405e9cd1aeSAssar Westerlund #include <string.h>
415e9cd1aeSAssar Westerlund #include <roken.h>
425e9cd1aeSAssar Westerlund #include "com_err.h"
435e9cd1aeSAssar Westerlund 
445e9cd1aeSAssar Westerlund struct et_list *_et_list = NULL;
455e9cd1aeSAssar Westerlund 
465e9cd1aeSAssar Westerlund 
475e9cd1aeSAssar Westerlund const char *
485e9cd1aeSAssar Westerlund error_message (long code)
495e9cd1aeSAssar Westerlund {
505e9cd1aeSAssar Westerlund     static char msg[128];
515e9cd1aeSAssar Westerlund     const char *p = com_right(_et_list, code);
528373020dSJacques Vidrine     if (p == NULL) {
538373020dSJacques Vidrine 	if (code < 0)
54c19800e8SDoug Rabson 	    snprintf(msg, sizeof(msg), "Unknown error %ld", code);
558373020dSJacques Vidrine 	else
565e9cd1aeSAssar Westerlund 	    p = strerror(code);
578373020dSJacques Vidrine     }
585e9cd1aeSAssar Westerlund     if (p != NULL && *p != '\0') {
59c19800e8SDoug Rabson 	strlcpy(msg, p, sizeof(msg));
605e9cd1aeSAssar Westerlund     } else
61c19800e8SDoug Rabson 	snprintf(msg, sizeof(msg), "Unknown error %ld", code);
625e9cd1aeSAssar Westerlund     return msg;
635e9cd1aeSAssar Westerlund }
645e9cd1aeSAssar Westerlund 
655e9cd1aeSAssar Westerlund int
665e9cd1aeSAssar Westerlund init_error_table(const char **msgs, long base, int count)
675e9cd1aeSAssar Westerlund {
685e9cd1aeSAssar Westerlund     initialize_error_table_r(&_et_list, msgs, count, base);
695e9cd1aeSAssar Westerlund     return 0;
705e9cd1aeSAssar Westerlund }
715e9cd1aeSAssar Westerlund 
725e9cd1aeSAssar Westerlund static void
735e9cd1aeSAssar Westerlund default_proc (const char *whoami, long code, const char *fmt, va_list args)
74adb0ddaeSAssar Westerlund     __attribute__((__format__(__printf__, 3, 0)));
75adb0ddaeSAssar Westerlund 
76adb0ddaeSAssar Westerlund static void
77adb0ddaeSAssar Westerlund default_proc (const char *whoami, long code, const char *fmt, va_list args)
785e9cd1aeSAssar Westerlund {
795e9cd1aeSAssar Westerlund     if (whoami)
805e9cd1aeSAssar Westerlund       fprintf(stderr, "%s: ", whoami);
815e9cd1aeSAssar Westerlund     if (code)
825e9cd1aeSAssar Westerlund       fprintf(stderr, "%s ", error_message(code));
835e9cd1aeSAssar Westerlund     if (fmt)
845e9cd1aeSAssar Westerlund       vfprintf(stderr, fmt, args);
855e9cd1aeSAssar Westerlund     fprintf(stderr, "\r\n");	/* ??? */
865e9cd1aeSAssar Westerlund }
875e9cd1aeSAssar Westerlund 
885e9cd1aeSAssar Westerlund static errf com_err_hook = default_proc;
895e9cd1aeSAssar Westerlund 
905e9cd1aeSAssar Westerlund void
915e9cd1aeSAssar Westerlund com_err_va (const char *whoami,
925e9cd1aeSAssar Westerlund 	    long code,
935e9cd1aeSAssar Westerlund 	    const char *fmt,
945e9cd1aeSAssar Westerlund 	    va_list args)
955e9cd1aeSAssar Westerlund {
965e9cd1aeSAssar Westerlund     (*com_err_hook) (whoami, code, fmt, args);
975e9cd1aeSAssar Westerlund }
985e9cd1aeSAssar Westerlund 
995e9cd1aeSAssar Westerlund void
1005e9cd1aeSAssar Westerlund com_err (const char *whoami,
1015e9cd1aeSAssar Westerlund 	 long code,
1025e9cd1aeSAssar Westerlund 	 const char *fmt,
1035e9cd1aeSAssar Westerlund 	 ...)
1045e9cd1aeSAssar Westerlund {
1055e9cd1aeSAssar Westerlund     va_list ap;
1065e9cd1aeSAssar Westerlund     va_start(ap, fmt);
1075e9cd1aeSAssar Westerlund     com_err_va (whoami, code, fmt, ap);
1085e9cd1aeSAssar Westerlund     va_end(ap);
1095e9cd1aeSAssar Westerlund }
1105e9cd1aeSAssar Westerlund 
1115e9cd1aeSAssar Westerlund errf
1125e9cd1aeSAssar Westerlund set_com_err_hook (errf new)
1135e9cd1aeSAssar Westerlund {
1145e9cd1aeSAssar Westerlund     errf old = com_err_hook;
1155e9cd1aeSAssar Westerlund 
1165e9cd1aeSAssar Westerlund     if (new)
1175e9cd1aeSAssar Westerlund 	com_err_hook = new;
1185e9cd1aeSAssar Westerlund     else
1195e9cd1aeSAssar Westerlund 	com_err_hook = default_proc;
1205e9cd1aeSAssar Westerlund 
1215e9cd1aeSAssar Westerlund     return old;
1225e9cd1aeSAssar Westerlund }
1235e9cd1aeSAssar Westerlund 
1245e9cd1aeSAssar Westerlund errf
1255e9cd1aeSAssar Westerlund reset_com_err_hook (void)
1265e9cd1aeSAssar Westerlund {
1275e9cd1aeSAssar Westerlund     return set_com_err_hook(NULL);
1285e9cd1aeSAssar Westerlund }
1295e9cd1aeSAssar Westerlund 
1305e9cd1aeSAssar Westerlund #define ERRCODE_RANGE   8       /* # of bits to shift table number */
1315e9cd1aeSAssar Westerlund #define BITS_PER_CHAR   6       /* # bits to shift per character in name */
1325e9cd1aeSAssar Westerlund 
1335e9cd1aeSAssar Westerlund static const char char_set[] =
1345e9cd1aeSAssar Westerlund         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
1355e9cd1aeSAssar Westerlund 
1365e9cd1aeSAssar Westerlund static char buf[6];
1375e9cd1aeSAssar Westerlund 
1385e9cd1aeSAssar Westerlund const char *
1395e9cd1aeSAssar Westerlund error_table_name(int num)
1405e9cd1aeSAssar Westerlund {
1415e9cd1aeSAssar Westerlund     int ch;
1425e9cd1aeSAssar Westerlund     int i;
1435e9cd1aeSAssar Westerlund     char *p;
1445e9cd1aeSAssar Westerlund 
1455e9cd1aeSAssar Westerlund     /* num = aa aaa abb bbb bcc ccc cdd ddd d?? ??? ??? */
1465e9cd1aeSAssar Westerlund     p = buf;
1475e9cd1aeSAssar Westerlund     num >>= ERRCODE_RANGE;
1485e9cd1aeSAssar Westerlund     /* num = ?? ??? ??? aaa aaa bbb bbb ccc ccc ddd ddd */
1495e9cd1aeSAssar Westerlund     num &= 077777777;
1505e9cd1aeSAssar Westerlund     /* num = 00 000 000 aaa aaa bbb bbb ccc ccc ddd ddd */
1515e9cd1aeSAssar Westerlund     for (i = 4; i >= 0; i--) {
1525e9cd1aeSAssar Westerlund         ch = (num >> BITS_PER_CHAR * i) & ((1 << BITS_PER_CHAR) - 1);
1535e9cd1aeSAssar Westerlund         if (ch != 0)
1545e9cd1aeSAssar Westerlund             *p++ = char_set[ch-1];
1555e9cd1aeSAssar Westerlund     }
1565e9cd1aeSAssar Westerlund     *p = '\0';
1575e9cd1aeSAssar Westerlund     return(buf);
1585e9cd1aeSAssar Westerlund }
159adb0ddaeSAssar Westerlund 
160adb0ddaeSAssar Westerlund void
161adb0ddaeSAssar Westerlund add_to_error_table(struct et_list *new_table)
162adb0ddaeSAssar Westerlund {
163adb0ddaeSAssar Westerlund     struct et_list *et;
164adb0ddaeSAssar Westerlund 
165adb0ddaeSAssar Westerlund     for (et = _et_list; et; et = et->next) {
166adb0ddaeSAssar Westerlund 	if (et->table->base == new_table->table->base)
167adb0ddaeSAssar Westerlund 	    return;
168adb0ddaeSAssar Westerlund     }
169adb0ddaeSAssar Westerlund 
170adb0ddaeSAssar Westerlund     new_table->next = _et_list;
171adb0ddaeSAssar Westerlund     _et_list = new_table;
172adb0ddaeSAssar Westerlund }
173