xref: /freebsd/contrib/com_err/error.c (revision 847a74626bc1d2da493acbc0a57765d7e5b21f5f)
1847a7462SMark Murray /*
2847a7462SMark Murray  * Copyright (c) 1997, 1998 Kungliga Tekniska H�gskolan
3847a7462SMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4847a7462SMark Murray  * All rights reserved.
5847a7462SMark Murray  *
6847a7462SMark Murray  * Redistribution and use in source and binary forms, with or without
7847a7462SMark Murray  * modification, are permitted provided that the following conditions
8847a7462SMark Murray  * are met:
9847a7462SMark Murray  *
10847a7462SMark Murray  * 1. Redistributions of source code must retain the above copyright
11847a7462SMark Murray  *    notice, this list of conditions and the following disclaimer.
12847a7462SMark Murray  *
13847a7462SMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14847a7462SMark Murray  *    notice, this list of conditions and the following disclaimer in the
15847a7462SMark Murray  *    documentation and/or other materials provided with the distribution.
16847a7462SMark Murray  *
17847a7462SMark Murray  * 3. All advertising materials mentioning features or use of this software
18847a7462SMark Murray  *    must display the following acknowledgement:
19847a7462SMark Murray  *      This product includes software developed by Kungliga Tekniska
20847a7462SMark Murray  *      H�gskolan and its contributors.
21847a7462SMark Murray  *
22847a7462SMark Murray  * 4. Neither the name of the Institute nor the names of its contributors
23847a7462SMark Murray  *    may be used to endorse or promote products derived from this software
24847a7462SMark Murray  *    without specific prior written permission.
25847a7462SMark Murray  *
26847a7462SMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27847a7462SMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28847a7462SMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29847a7462SMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30847a7462SMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31847a7462SMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32847a7462SMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33847a7462SMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34847a7462SMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35847a7462SMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36847a7462SMark Murray  * SUCH DAMAGE.
37847a7462SMark Murray  */
38847a7462SMark Murray 
39847a7462SMark Murray #ifdef HAVE_CONFIG_H
40847a7462SMark Murray #include <config.h>
41847a7462SMark Murray RCSID("$Id: error.c,v 1.13 1998/02/17 21:19:44 bg Exp $");
42847a7462SMark Murray #endif
43847a7462SMark Murray #include <stdio.h>
44847a7462SMark Murray #include <stdlib.h>
45847a7462SMark Murray #include <string.h>
46847a7462SMark Murray #include <com_right.h>
47847a7462SMark Murray 
48847a7462SMark Murray const char *
49847a7462SMark Murray com_right(struct et_list *list, long code)
50847a7462SMark Murray {
51847a7462SMark Murray     struct et_list *p;
52847a7462SMark Murray     for (p = list; p; p = p->next) {
53847a7462SMark Murray 	if (code >= p->table->base && code < p->table->base + p->table->n_msgs)
54847a7462SMark Murray 	    return p->table->msgs[code - p->table->base];
55847a7462SMark Murray     }
56847a7462SMark Murray     return NULL;
57847a7462SMark Murray }
58847a7462SMark Murray 
59847a7462SMark Murray struct foobar {
60847a7462SMark Murray     struct et_list etl;
61847a7462SMark Murray     struct error_table et;
62847a7462SMark Murray };
63847a7462SMark Murray 
64847a7462SMark Murray void
65847a7462SMark Murray initialize_error_table_r(struct et_list **list,
66847a7462SMark Murray 			 const char **messages,
67847a7462SMark Murray 			 int num_errors,
68847a7462SMark Murray 			 long base)
69847a7462SMark Murray {
70847a7462SMark Murray     struct et_list *et;
71847a7462SMark Murray     struct foobar *f;
72847a7462SMark Murray     for (et = *list; et; et = et->next)
73847a7462SMark Murray         if (et->table->msgs == messages)
74847a7462SMark Murray             return;
75847a7462SMark Murray     f = malloc(sizeof(*f));
76847a7462SMark Murray     if (f == NULL)
77847a7462SMark Murray         return;
78847a7462SMark Murray     et = &f->etl;
79847a7462SMark Murray     et->table = &f->et;
80847a7462SMark Murray     et->table->msgs = messages;
81847a7462SMark Murray     et->table->n_msgs = num_errors;
82847a7462SMark Murray     et->table->base = base;
83847a7462SMark Murray     et->next = *list;
84847a7462SMark Murray     *list = et;
85847a7462SMark Murray }
86847a7462SMark Murray 
87847a7462SMark Murray 
88847a7462SMark Murray void
89847a7462SMark Murray free_error_table(struct et_list *et)
90847a7462SMark Murray {
91847a7462SMark Murray     while(et){
92847a7462SMark Murray 	struct et_list *p = et;
93847a7462SMark Murray 	et = et->next;
94847a7462SMark Murray 	free(p);
95847a7462SMark Murray     }
96847a7462SMark Murray }
97