xref: /titanic_44/usr/src/lib/libast/common/string/ccmapid.c (revision bd28a477274db2b836577dfd6f223c696ad00720)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 
24 /*
25  * Glenn Fowler
26  * AT&T Research
27  *
28  * 8 bit character code map name/id lookup support
29  */
30 
31 #include <ast.h>
32 #include <ccode.h>
33 #include <ctype.h>
34 
35 static const Ccmap_t	maps[] =
36 {
37 	{
38 	"ascii",
39 	"a|ascii|?(iso)?(-)646|?(iso)?(-)8859|latin",
40 	"8 bit ascii",
41 	"ISO-8859-%s",
42 	"1",
43 	CC_ASCII,
44 	},
45 
46 	{
47 	"ebcdic",
48 	"e|ebcdic?(-)?([1e])",
49 	"X/Open ebcdic",
50 	"EBCDIC",
51 	0,
52 	CC_EBCDIC_E,
53 	},
54 
55 	{
56 	"ebcdic-o",
57 	"o|ebcdic?(-)[3o]|?(cp|ibm)1047|open?(-)edition",
58 	"mvs OpenEdition ebcdic",
59 	"EBCDIC-O",
60 	0,
61 	CC_EBCDIC_O,
62 	},
63 
64 	{
65 	"ebcdic-h",
66 	"h|ebcdic?(-)h|?(cp|ibm)?(00)37|[oa]s?(/-)400",
67 	"ibm OS/400 AS/400 ebcdic",
68 	"EBCDIC-H",
69 	0,
70 	CC_EBCDIC_H,
71 	},
72 
73 	{
74 	"ebcdic-s",
75 	"s|ebcdic?(-)s|siemens|posix-bc",
76 	"siemens posix-bc ebcdic",
77 	"EBCDIC-S",
78 	0,
79 	CC_EBCDIC_S,
80 	},
81 
82 	{
83 	"ebcdic-i",
84 	"i|ebcdic?(-)[2i]|ibm",
85 	"X/Open ibm ebcdic (not idempotent)",
86 	"EBCDIC-I",
87 	0,
88 	CC_EBCDIC_I,
89 	},
90 
91 	{
92 	"ebcdic-m",
93 	"m|ebcdic?(-)m|mvs",
94 	"mvs ebcdic",
95 	"EBCDIC-M",
96 	0,
97 	CC_EBCDIC_M,
98 	},
99 
100 	{
101 	"ebcdic-u",
102 	"u|ebcdic?(-)(u|mf)|microfocus",
103 	"microfocus cobol ebcdic",
104 	"EBCDIC-U",
105 	0,
106 	CC_EBCDIC_U,
107 	},
108 
109 	{
110 	"native",
111 	"n|native|local",
112 	"native code set",
113 	0,
114 	0,
115 	CC_NATIVE,
116 	},
117 
118 	{ 0 },
119 };
120 
121 /*
122  * ccode map list iterator
123  */
124 
125 Ccmap_t*
126 ccmaplist(Ccmap_t* mp)
127 {
128 	return !mp ? (Ccmap_t*)maps : (++mp)->name ? mp : (Ccmap_t*)0;
129 }
130 
131 /*
132  * return ccode map id given name
133  */
134 
135 int
136 ccmapid(const char* name)
137 {
138 	register const Ccmap_t*	mp;
139 	register int		c;
140 	const Ccmap_t*		bp;
141 	int			n;
142 	int			sub[2];
143 
144 	bp = 0;
145 	n = 0;
146 	for (mp = maps; mp->name; mp++)
147 		if (strgrpmatch(name, mp->match, sub, elementsof(sub) / 2, STR_MAXIMAL|STR_LEFT|STR_ICASE))
148 		{
149 			if (!(c = name[sub[1]]))
150 				return mp->ccode;
151 			if (sub[1] > n && !isalpha(c))
152 			{
153 				n = sub[1];
154 				bp = mp;
155 			}
156 		}
157 	return bp ? bp->ccode : -1;
158 }
159 
160 /*
161  * return ccode map name given id
162  */
163 
164 char*
165 ccmapname(register int id)
166 {
167 	register const Ccmap_t*	mp;
168 
169 	for (mp = maps; mp->name; mp++)
170 		if (id == mp->ccode)
171 			return (char*)mp->name;
172 	return 0;
173 }
174