chiark / gitweb /
Imported Debian patch 1.0.0-5
[e16] / src / emodule.c
1 /*
2  * Copyright (C) 2003-2007 Kim Woelders
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies of the Software, its documentation and marketing & publicity
13  * materials, and acknowledgment shall be given in the documentation, materials
14  * and software packages that this Software was used.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #include "E.h"
24 #include "emodule.h"
25
26 #if 0                           /* No dynamic registration yet */
27 void
28 EModuleRegister(EModule * em)
29 {
30    p_modules = EREALLOC(EModule *, p_modules, n_modules + 1);
31    p_modules[n_modules++] = em;
32 }
33 #endif
34
35 const EModule     **
36 ModuleListGet(int *num)
37 {
38    *num = n_modules;
39    return p_modules;
40 }
41
42 void
43 ModuleListFree(const EModule ** lst __UNUSED__)
44 {
45 }
46
47 static const EModule *
48 EModuleFind(const char *name)
49 {
50    int                 i;
51    const EModule      *pm;
52
53    for (i = 0; i < n_modules; i++)
54      {
55         pm = p_modules[i];
56         if (!strncmp(name, pm->name, 4) ||
57             (pm->nick && !strcmp(name, pm->nick)))
58            return pm;
59      }
60    return NULL;
61 }
62
63 int
64 ModuleConfigSet(const char *name, const char *item, const char *params)
65 {
66    const EModule      *em;
67
68    em = EModuleFind(name);
69    if (!em)
70       return -1;
71
72    if (em->cfg.num <= 0)
73       return -1;
74
75    CfgItemListNamedItemSet(em->cfg.lst, em->cfg.num, item, params);
76
77    return 0;
78 }
79
80 static void
81 EmodCfgItemShow(const EModule * em, const CfgItem * ci)
82 {
83    char                buf[1024];
84
85    if (!ci)
86       return;
87
88    CfgItemToString(ci, buf, sizeof(buf));
89    IpcPrintf("  %s.%s = %s\n", em->name, ci->name, buf);
90 }
91
92 static void
93 EmodCfgNamedItemShow(const EModule * em, const char *item)
94 {
95    const CfgItem      *ci;
96
97    ci = CfgItemFind(em->cfg.lst, em->cfg.num, item);
98    if (ci)
99       EmodCfgItemShow(em, ci);
100    else
101       IpcPrintf("! %s.%s *** Not found ***\n", em->name, item);
102 }
103
104 int
105 ModuleConfigShow(const char *name, const char *item)
106 {
107    const EModule      *em;
108    int                 i;
109
110 #if 0
111    Eprintf("ModuleConfigShow: %s:%s\n", name, item);
112 #endif
113    em = EModuleFind(name);
114    if (!em)
115       return -1;
116
117    if (item)
118      {
119         EmodCfgNamedItemShow(em, item);
120      }
121    else
122      {
123         for (i = 0; i < em->cfg.num; i++)
124            EmodCfgItemShow(em, em->cfg.lst + i);
125      }
126
127    return 0;
128 }
129
130 void
131 ModulesSignal(int sig, void *prm)
132 {
133    int                 i;
134
135    for (i = 0; i < n_modules; i++)
136       if (p_modules[i]->Signal)
137          p_modules[i]->Signal(sig, prm);
138 }
139
140 #if 0
141 void
142 ModulesGetCfgItems(const CfgItem *** ppi, int *pni)
143 {
144    int                 i, j, k, n;
145    const CfgItem     **pi;
146
147    pi = *ppi;
148    k = *pni;
149    for (i = 0; i < n_modules; i++)
150      {
151         if (p_modules[i]->cfg.lst)
152           {
153              n = p_modules[i]->cfg.num;
154              pi = EREALLOC(CfgItem *, pi, k + n);
155              for (j = 0; j < n; j++)
156                 pi[k++] = &(p_modules[i]->cfg.lst[j]);
157           }
158      }
159    *ppi = pi;
160    *pni = k;
161 }
162 #endif
163
164 void
165 ModulesGetIpcItems(const IpcItem *** ppi, int *pni)
166 {
167    int                 i, j, k, n;
168    const IpcItem     **pi;
169
170    pi = *ppi;
171    k = *pni;
172    for (i = 0; i < n_modules; i++)
173      {
174         if (p_modules[i]->ipc.lst)
175           {
176              n = p_modules[i]->ipc.num;
177              pi = EREALLOC(const IpcItem *, pi, k + n);
178
179              for (j = 0; j < n; j++)
180                 pi[k++] = &(p_modules[i]->ipc.lst[j]);
181           }
182      }
183    *ppi = pi;
184    *pni = k;
185 }
186
187 void
188 ModulesConfigShow(void)
189 {
190    int                 i, nml;
191    const EModule     **pml;
192
193    /* Load module configs */
194    pml = ModuleListGet(&nml);
195    for (i = 0; i < nml; i++)
196      {
197         /* Somewhat inefficient but ... later */
198         ModuleConfigShow(pml[i]->name, NULL);
199      }
200    ModuleListFree(pml);
201 }