kernel - Video - Add suppor for Intel IGD chipsets (netbook / N450 etc)
[dragonfly.git] / lib / libc / citrus / citrus_module.c
blob672e43a585ca6a7da1e48a8618c2e3bcae6ecac9
1 /* $NetBSD: citrus_module.c,v 1.5 2005/11/29 03:11:58 christos Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/citrus_module.c,v 1.5 2008/04/10 10:21:01 hasso Exp $ */
4 /*-
5 * Copyright (c)1999, 2000, 2001, 2002 Citrus Project,
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 /*-
31 * Copyright (c) 1998 The NetBSD Foundation, Inc.
32 * All rights reserved.
34 * This code is derived from software contributed to The NetBSD Foundation
35 * by Paul Kranenburg.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the NetBSD
48 * Foundation, Inc. and its contributors.
49 * 4. Neither the name of The NetBSD Foundation nor the names of its
50 * contributors may be used to endorse or promote products derived
51 * from this software without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 * POSSIBILITY OF SUCH DAMAGE.
66 /*-
67 * Copyright (c) 1993
68 * The Regents of the University of California. All rights reserved.
70 * This code is derived from software contributed to Berkeley by
71 * Paul Borman at Krystal Technologies.
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. Neither the name of the University nor the names of its contributors
82 * may be used to endorse or promote products derived from this software
83 * without specific prior written permission.
85 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95 * SUCH DAMAGE.
98 #include <assert.h>
99 #include <errno.h>
100 #include <limits.h>
101 #include <string.h>
102 #include <stdio.h>
103 #include <stdlib.h>
104 #include <unistd.h>
105 #include <locale.h>
106 #include <stddef.h>
107 #include <paths.h>
108 #include <wchar.h>
109 #include "citrus_module.h"
111 #include <sys/types.h>
112 #include <dirent.h>
113 #include <dlfcn.h>
115 #ifdef __PIC__
117 static int _getdewey(int [], char *);
118 static int _cmpndewey(int [], int, int [], int);
119 static const char *_findshlib(char *, int *, int *);
121 static const char *_pathI18nModule = NULL;
123 /* from libexec/ld.aout_so/shlib.c */
124 #undef major
125 #undef minor
126 #define MAXDEWEY 3 /*ELF*/
128 static int
129 _getdewey(int dewey[], char *cp)
131 int i, n;
133 _DIAGASSERT(dewey != NULL);
134 _DIAGASSERT(cp != NULL);
136 for (n = 0, i = 0; i < MAXDEWEY; i++) {
137 if (*cp == '\0')
138 break;
140 if (*cp == '.') cp++;
141 if (*cp < '0' || '9' < *cp)
142 return 0;
144 dewey[n++] = (int)strtol(cp, &cp, 10);
147 return n;
151 * Compare two dewey arrays.
152 * Return -1 if `d1' represents a smaller value than `d2'.
153 * Return 1 if `d1' represents a greater value than `d2'.
154 * Return 0 if equal.
156 static int
157 _cmpndewey(int d1[], int n1, int d2[], int n2)
159 int i;
161 _DIAGASSERT(d1 != NULL);
162 _DIAGASSERT(d2 != NULL);
164 for (i = 0; i < n1 && i < n2; i++) {
165 if (d1[i] < d2[i])
166 return -1;
167 if (d1[i] > d2[i])
168 return 1;
171 if (n1 == n2)
172 return 0;
174 if (i == n1)
175 return -1;
177 if (i == n2)
178 return 1;
180 /* XXX cannot happen */
181 return 0;
184 static const char *
185 _findshlib(char *name, int *majorp, int *minorp)
187 int dewey[MAXDEWEY];
188 int ndewey;
189 int tmp[MAXDEWEY];
190 int i;
191 int len;
192 char *lname;
193 static char path[PATH_MAX];
194 int major, minor;
195 const char *search_dirs[1];
196 const int n_search_dirs = 1;
198 _DIAGASSERT(name != NULL);
199 _DIAGASSERT(majorp != NULL);
200 _DIAGASSERT(minorp != NULL);
202 major = *majorp;
203 minor = *minorp;
204 path[0] = '\0';
205 search_dirs[0] = _pathI18nModule;
206 len = strlen(name);
207 lname = name;
209 ndewey = 0;
211 for (i = 0; i < n_search_dirs; i++) {
212 DIR *dd = opendir(search_dirs[i]);
213 struct dirent *dp;
214 int found_dot_a = 0;
215 int found_dot_so = 0;
217 if (dd == NULL)
218 continue;
220 while ((dp = readdir(dd)) != NULL) {
221 int n;
223 if (dp->d_namlen < len + 4)
224 continue;
225 if (strncmp(dp->d_name, lname, (size_t)len) != 0)
226 continue;
227 if (strncmp(dp->d_name+len, ".so.", 4) != 0)
228 continue;
230 if ((n = _getdewey(tmp, dp->d_name+len+4)) == 0)
231 continue;
233 if (major != -1 && found_dot_a)
234 found_dot_a = 0;
236 /* XXX should verify the library is a.out/ELF? */
238 if (major == -1 && minor == -1) {
239 goto compare_version;
240 } else if (major != -1 && minor == -1) {
241 if (tmp[0] == major)
242 goto compare_version;
243 } else if (major != -1 && minor != -1) {
244 if (tmp[0] == major) {
245 if (n == 1 || tmp[1] >= minor)
246 goto compare_version;
250 /* else, this file does not qualify */
251 continue;
253 compare_version:
254 if (_cmpndewey(tmp, n, dewey, ndewey) <= 0)
255 continue;
257 /* We have a better version */
258 found_dot_so = 1;
259 snprintf(path, sizeof(path), "%s/%s", search_dirs[i],
260 dp->d_name);
261 found_dot_a = 0;
262 bcopy(tmp, dewey, sizeof(dewey));
263 ndewey = n;
264 *majorp = dewey[0];
265 *minorp = dewey[1];
267 closedir(dd);
269 if (found_dot_a || found_dot_so)
271 * There's a lib in this dir; take it.
273 return path[0] ? path : NULL;
276 return path[0] ? path : NULL;
279 void *
280 _citrus_find_getops(_citrus_module_t handle, const char *modname,
281 const char *ifname)
283 char name[PATH_MAX];
284 void *p;
286 _DIAGASSERT(handle != NULL);
287 _DIAGASSERT(modname != NULL);
288 _DIAGASSERT(ifname != NULL);
290 snprintf(name, sizeof(name), "_citrus_%s_%s_getops", modname, ifname);
291 p = dlsym((void *)handle, name);
292 return p;
296 _citrus_load_module(_citrus_module_t *rhandle, const char *encname)
298 const char *p;
299 char path[PATH_MAX];
300 int maj, min;
301 void *handle;
303 _DIAGASSERT(rhandle != NULL);
305 if (_pathI18nModule == NULL) {
306 p = getenv("PATH_I18NMODULE");
307 if (p != NULL && !issetugid()) {
308 _pathI18nModule = strdup(p);
309 if (_pathI18nModule == NULL)
310 return ENOMEM;
311 } else
312 _pathI18nModule = _PATH_I18NMODULE;
315 snprintf(path, sizeof(path), "lib%s", encname);
316 maj = I18NMODULE_MAJOR;
317 min = -1;
318 p = _findshlib(path, &maj, &min);
319 if (!p)
320 return (EINVAL);
321 handle = dlopen(p, RTLD_LAZY);
322 if (!handle)
323 return (EINVAL);
325 *rhandle = (_citrus_module_t)handle;
327 return (0);
330 void
331 _citrus_unload_module(_citrus_module_t handle)
333 if (handle)
334 dlclose((void *)handle);
336 #elif defined(_I18N_STATIC)
338 * Compiled-in multibyte locale support for statically linked programs.
340 #include "citrus_ctype.h"
341 #include "sys/queue.h"
342 #include "citrus_types.h"
343 #include "citrus_hash.h"
344 #include "citrus_namespace.h"
345 #include "citrus_region.h"
346 #include "citrus_iconv_local.h"
347 #include "citrus_mapper_local.h"
348 #include "citrus_stdenc_local.h"
349 #include "modules/citrus_mapper_serial.h"
350 #include "modules/citrus_mapper_std.h"
351 #include "modules/citrus_mapper_none.h"
352 #include "modules/citrus_iconv_std.h"
353 #include "modules/citrus_utf1632.h"
355 #ifdef _I18N_STATIC_BIG5
356 #include "modules/citrus_big5.h"
357 #endif
358 #ifdef _I18N_STATIC_EUC
359 #include "modules/citrus_euc.h"
360 #endif
361 #ifdef _I18N_STATIC_EUCTW
362 #include "modules/citrus_euctw.h"
363 #endif
364 #ifdef _I18N_STATIC_ISO2022
365 #include "modules/citrus_iso2022.h"
366 #endif
367 #ifdef _I18N_STATIC_MSKanji
368 #include "modules/citrus_mskanji.h"
369 #endif
370 #ifdef _I18N_STATIC_UTF8
371 #include "modules/citrus_utf8.h"
372 #endif
374 #define _CITRUS_GETOPS_FUNC(_m_, _if_) _citrus_##_m_##_##_if_##_getops
375 /* only ctype is supported */
376 #define _CITRUS_LOCALE_TABLE_ENTRY(_n_) \
377 { #_n_, "ctype", _CITRUS_GETOPS_FUNC(_n_, ctype) }
379 #define _CITRUS_MODULE_TABLE_ENTRY(_n_, _if_) \
380 { #_n_, #_if_, _CITRUS_GETOPS_FUNC(_n_, _if_) }
382 * Table of compiled-in modules.
384 struct citrus_metadata module_table[] = {
385 _CITRUS_MODULE_TABLE_ENTRY(iconv_std, iconv),
386 _CITRUS_MODULE_TABLE_ENTRY(mapper_std, mapper),
387 _CITRUS_MODULE_TABLE_ENTRY(mapper_serial, mapper),
388 _CITRUS_MODULE_TABLE_ENTRY(mapper_none, mapper),
389 _CITRUS_MODULE_TABLE_ENTRY(UTF1632, stdenc),
390 #ifdef _I18N_STATIC_BIG5
391 _CITRUS_LOCALE_TABLE_ENTRY(BIG5),
392 #endif
393 #ifdef _I18N_STATIC_EUC
394 _CITRUS_LOCALE_TABLE_ENTRY(EUC),
395 #endif
396 #ifdef _I18N_STATIC_EUCTW
397 _CITRUS_LOCALE_TABLE_ENTRY(EUCTW),
398 #endif
399 #ifdef _I18N_STATIC_ISO2022
400 _CITRUS_LOCALE_TABLE_ENTRY(ISO2022),
401 #endif
402 #ifdef _I18N_STATIC_MSKanji
403 _CITRUS_LOCALE_TABLE_ENTRY(MSKanji),
404 #endif
405 #ifdef _I18N_STATIC_UTF8
406 _CITRUS_LOCALE_TABLE_ENTRY(UTF8),
407 #endif
408 { NULL, NULL, NULL },
411 SET_DECLARE(citrus_set, struct citrus_metadata);
413 DATA_SET(citrus_set, module_table);
415 #define MAGIC_HANDLE (void *)(0xC178C178)
417 void *
418 /*ARGSUSED*/
419 _citrus_find_getops(_citrus_module_t handle __unused, const char *modname,
420 const char *ifname)
422 struct citrus_metadata **mdp, *mod;
424 SET_FOREACH(mdp, citrus_set) {
425 mod = *mdp;
426 if (mod == NULL || mod->module_name == NULL || mod->interface_name == NULL)
427 continue;
428 if (strcmp(mod->module_name, modname) != 0)
429 continue;
430 if (strcmp(mod->interface_name, ifname) != 0)
431 continue;
432 return(mod->module_ops);
434 return (NULL);
438 /*ARGSUSED*/
439 _citrus_load_module(_citrus_module_t *rhandle, char const *modname)
441 struct citrus_metadata **mdp, *mod;
443 SET_FOREACH(mdp, citrus_set) {
444 mod = *mdp;
445 if (mod == NULL || mod->module_name == NULL)
446 continue;
447 if (strcmp(mod->module_name, modname) != 0)
448 continue;
449 *rhandle = (_citrus_module_t)mod;
450 return(0);
452 return (EINVAL);
455 void
456 /*ARGSUSED*/
457 _citrus_unload_module(_citrus_module_t handle __unused)
460 #else
461 SET_DECLARE(citrus_set, struct citrus_metadata);
463 struct citrus_metadata empty = {
464 NULL, NULL, NULL
467 DATA_SET(citrus_set, empty);
469 #define MAGIC_HANDLE (void *)(0xC178C178)
471 void *
472 /*ARGSUSED*/
473 _citrus_find_getops(_citrus_module_t handle __unused, const char *modname,
474 const char *ifname)
476 struct citrus_metadata **mdp, *mod;
478 _DIAGASSERT(handle == MAGIC_HANDLE);
480 SET_FOREACH(mdp, citrus_set) {
481 mod = *mdp;
482 if (mod == NULL || mod->module_name == NULL || mod->interface_name == NULL)
483 continue;
484 if (strcmp(mod->module_name, modname) != 0)
485 continue;
486 if (strcmp(mod->interface_name, ifname) != 0)
487 continue;
488 return(mod->module_ops);
490 return (NULL);
494 /*ARGSUSED*/
495 _citrus_load_module(_citrus_module_t *rhandle, char const *modname)
497 struct citrus_metadata **mdp, *mod;
499 SET_FOREACH(mdp, citrus_set) {
500 mod = *mdp;
501 if (mod == NULL || mod->module_name == NULL)
502 continue;
503 if (strcmp(mod->module_name, modname) != 0)
504 continue;
505 *rhandle = MAGIC_HANDLE;
506 return(0);
508 return (EINVAL);
511 void
512 /*ARGSUSED*/
513 _citrus_unload_module(_citrus_module_t handle __unused)
516 #endif