xref: /illumos-gate/usr/src/lib/fm/topo/libtopo/common/mkerror.sh (revision 34a0f871d192b33b865455a8812a3d34c1866315)
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 2007 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 2007 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
98elif [ $1 = "methoderrors" ] ; then
99
100echo "\
101\n\
102static const char *const _topo_methoderrstrs[] = {"
103
104pattern='^[ ]*ETOPO_METHOD_[A-Z0-9_]*.*\* \(.*\) \*.*'
105replace='	"\1",'
106
107echo "$input" | sed -n "s/$pattern/$replace/p" || exit 1
108
109echo "\
110};\n\
111\n\
112static const int _topo_nmethoderrstrs =\n\
113    sizeof (_topo_methoderrstrs) / sizeof (_topo_methoderrstrs[0]);"
114else
115
116echo "\
117\n\
118static const char *const _topo_moderrstrs[] = {"
119
120pattern='^[ ]*EMOD_[A-Z0-9_]*.*\* \(.*\) \*.*'
121replace='	"\1",'
122
123echo "$input" | sed -n "s/$pattern/$replace/p" || exit 1
124
125echo "\
126};\n\
127static const int _topo_nmoderrstrs =\n\
128    sizeof (_topo_moderrstrs) / sizeof (_topo_moderrstrs[0]);\n\
129\n\
130
131int
132topo_mod_errno(topo_mod_t *mp)
133{
134	return (mp->tm_errno);
135}
136
137int
138topo_mod_seterrno(topo_mod_t *mp, int err)
139{
140	mp->tm_errno = err;
141	return (-1);
142}
143
144const char *
145topo_mod_errmsg(topo_mod_t *mp)
146{
147	return (topo_strerror(mp->tm_errno));
148}
149
150const char *
151topo_strerror(int err)
152{
153	const char *s;
154
155	if (err >= ETOPO_UNKNOWN && (err - ETOPO_UNKNOWN) < _topo_nerrstrs)
156		s = _topo_errstrs[err - ETOPO_UNKNOWN];
157	else if (err >= EMOD_UNKNOWN && (err - EMOD_UNKNOWN) <
158	    _topo_nmoderrstrs)
159		s = _topo_moderrstrs[err - EMOD_UNKNOWN];
160	else if (err >= ETOPO_PROP_UNKNOWN && (err - ETOPO_PROP_UNKNOWN) <
161	    _topo_nproperrstrs)
162		s = _topo_properrstrs[err - ETOPO_PROP_UNKNOWN];
163	else if (err >= ETOPO_METHOD_UNKNOWN && (err - ETOPO_METHOD_UNKNOWN) <
164	    _topo_nmethoderrstrs)
165		s = _topo_methoderrstrs[err - ETOPO_METHOD_UNKNOWN];
166	else
167		s = _topo_errstrs[0];
168
169	return (s);
170}"
171
172fi
173
174exit 0
175