xref: /illumos-gate/usr/src/lib/fm/topo/libtopo/common/mkerror.sh (revision 150d2c5288c645a1c1a7d2bee61199a3729406c7)
1#!/bin/sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22#
23# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26#ident	"@(#)mkerror.sh	1.1	06/02/11 SMI"
27
28#pragma ident	"%Z%%M%	%I%	%E% SMI"
29
30
31input="`cat`"
32[ -z "$input" ] && exit 1
33
34if [ $1 = "liberrors" ] ; then
35echo "\
36/*\n\
37 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.\n\
38 * Use is subject to license terms.\n\
39 */\n\
40\n\
41#pragma ident\t\"@(#)mkerror.sh\t1.2\t05/06/08 SMI\"\n\
42\n\
43#include <strings.h>
44#include <topo_error.h>
45#include <topo_mod.h>
46
47\n\
48static const char *const _topo_errstrs[] = {"
49
50pattern='^[ ]*ETOPO_[A-Z0-9_]*.*\* \(.*\) \*.*'
51replace='	"\1",'
52
53echo "$input" | sed -n "s/$pattern/$replace/p" || exit 1
54
55echo "\
56};\n\
57\n\
58static const int _topo_nerrstrs =\n\
59    sizeof (_topo_errstrs) / sizeof (_topo_errstrs[0]);\n\
60\n\
61
62int
63topo_hdl_errno(topo_hdl_t *thp)
64{
65	return (thp->th_errno);
66}
67
68int
69topo_hdl_seterrno(topo_hdl_t *thp, int err)
70{
71	thp->th_errno = err;
72	return (-1);
73}
74
75const char *
76topo_hdl_errmsg(topo_hdl_t *thp)
77{
78	return (topo_strerror(thp->th_errno));
79}"
80
81elif [ $1 = "properrors" ] ; then
82
83echo "\
84\n\
85static const char *const _topo_properrstrs[] = {"
86
87pattern='^[ ]*ETOPO_PROP_[A-Z0-9_]*.*\* \(.*\) \*.*'
88replace='	"\1",'
89
90echo "$input" | sed -n "s/$pattern/$replace/p" || exit 1
91
92echo "\
93};\n\
94\n\
95static const int _topo_nproperrstrs =\n\
96    sizeof (_topo_properrstrs) / sizeof (_topo_properrstrs[0]);"
97
98else
99
100echo "\
101\n\
102static const char *const _topo_moderrstrs[] = {"
103
104pattern='^[ ]*EMOD_[A-Z0-9_]*.*\* \(.*\) \*.*'
105replace='	"\1",'
106
107echo "$input" | sed -n "s/$pattern/$replace/p" || exit 1
108
109echo "\
110};\n\
111static const int _topo_nmoderrstrs =\n\
112    sizeof (_topo_moderrstrs) / sizeof (_topo_moderrstrs[0]);\n\
113\n\
114
115int
116topo_mod_errno(topo_mod_t *mp)
117{
118	return (mp->tm_errno);
119}
120
121int
122topo_mod_seterrno(topo_mod_t *mp, int err)
123{
124	mp->tm_errno = err;
125	return (-1);
126}
127
128const char *
129topo_mod_errmsg(topo_mod_t *mp)
130{
131	return (topo_strerror(mp->tm_errno));
132}
133
134const char *
135topo_strerror(int err)
136{
137	const char *s;
138
139	if (err >= ETOPO_UNKNOWN && (err - ETOPO_UNKNOWN) < _topo_nerrstrs)
140		s = _topo_errstrs[err - ETOPO_UNKNOWN];
141	else if (err >= EMOD_UNKNOWN && (err - EMOD_UNKNOWN) <
142	    _topo_nmoderrstrs)
143		s = _topo_moderrstrs[err - EMOD_UNKNOWN];
144	else if (err >= ETOPO_PROP_UNKNOWN && (err - ETOPO_PROP_UNKNOWN) <
145	    _topo_nproperrstrs)
146		s = _topo_properrstrs[err - ETOPO_PROP_UNKNOWN];
147	else
148		s = _topo_errstrs[0];
149
150	return (s);
151}"
152
153fi
154
155exit 0
156