1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1998-2011 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Eclipse Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.eclipse.org/org/documents/epl-v10.html *
11 * (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <glenn.s.fowler@gmail.com> *
18 * *
19 ***********************************************************************/
20 #pragma prototyped
21
22 /*
23 * find and load a pzip dll
24 */
25
26 #include "pzlib.h"
27
28 #include <dlldefs.h>
29
30 int
pzlib(register Pz_t * pz,register const char * name,int ignore)31 pzlib(register Pz_t* pz, register const char* name, int ignore)
32 {
33 register Pzdll_t* dll;
34 register Pzdllpz_t* pzs;
35 char* id;
36 char buf[64];
37 char path[PATH_MAX];
38
39 if (id = strrchr(state.id, ':'))
40 id++;
41 else
42 id = (char*)state.id;
43
44 /*
45 * see if the dll is already loaded
46 */
47
48 for (dll = state.dll; dll && !streq(dll->name, name); dll = dll->next);
49 if (!dll)
50 {
51 /*
52 * add to the list and open
53 */
54
55 if (!(dll = newof(0, Pzdll_t, 1, strlen(name) + 1)))
56 return -1;
57 dll->name = strcpy((char*)(dll + 1), name);
58 dll->next = state.dll;
59 state.dll = dll;
60 if (ignore)
61 return 0;
62 if (!(dll->dll = dllplugin(id, dll->name, NiL, PZ_PLUGIN_VERSION, NiL, RTLD_LAZY, path, sizeof(path))))
63 {
64 if (pz->disc && pz->disc->errorf)
65 (*pz->disc->errorf)(pz, pz->disc, ERROR_SYSTEM|2, "%s: %s", dll->name, dlerror());
66 return -1;
67 }
68
69 /*
70 * get the initialization function
71 */
72
73 sfsprintf(buf, sizeof(buf), "%s_init", id);
74 if (!(dll->initf = (Pzinit_f)dlllook(dll->dll, buf)))
75 {
76 if (pz->disc && pz->disc->errorf)
77 (*pz->disc->errorf)(pz, pz->disc, 2, "%s: %s: initialization function not found in library", path, buf);
78 return -1;
79 }
80 }
81
82 /*
83 * see if pz already initialized
84 */
85
86 if (dll->initf)
87 {
88 for (pzs = dll->pzs; pzs && pzs->pz != pz; pzs = pzs->next);
89 if (!pzs)
90 {
91 if (!(pzs = newof(0, Pzdllpz_t, 1, 0)))
92 return -1;
93 pzs->pz = pz;
94 pzs->next = dll->pzs;
95 dll->pzs = pzs;
96 if (!(dll->usage = pzinit(pz, dll->name, dll->initf)))
97 return -1;
98 }
99 }
100 return 0;
101 }
102