forked from albertw/flash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.c
217 lines (172 loc) · 4.5 KB
/
module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Set up and initialise modules
Copyright (C) Stephen Fegan 1995-1997
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 675 Mass
Ave, Cambridge, MA 02139, USA.
please send patches or advice on flash to: `flash@netsoc.ucd.ie'
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<pwd.h>
#include<sys/types.h>
#include<sys/resource.h>
#include<sys/wait.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<dlfcn.h>
#include<time.h>
#include"set.h"
#include"rc.h"
#include"exec.h"
#include"config.h"
#include"parse.h"
#include"misc.h"
#include"string.h"
#include"parseline.h"
#include"module.h"
extern char *progname;
time_t TimeNow;
typedef void(*INITMOD)(int argc, char **argv, FILE *fp, int *lc);
typedef int(*FUNCTION)(int argc, char **argv);
struct InternalModules
{
char *name;
INITMOD InitModule;
};
struct InternalModules InternalModules[]=
{
{"background",InitBackground},
{"barclock",InitBarClock},
{"mailcheck",InitMailCheck},
{NULL,NULL}
};
void internal_module(int wordc, char **wordv, FILE *fp, int *line_no)
{
char *modname;
struct InternalModules *x=InternalModules;
wordv++,wordc--;
modname=*wordv;
while(x->name!=NULL)
{
if(strcasecmp(modname,x->name)==0)
{
#ifdef DEBUG
fprintf(stderr,"Found INTERNAL module %s, initialising\n",x->name);
#endif
(*x->InitModule)(wordc,wordv,fp,line_no);
return;
}
x++;
}
fprintf(stderr,"Could not find INTERNAL module: %s\n",modname);
return;
}
struct ExternalModule
{
struct ExternalModule *next,*prev;
char *name;
void *handle;
INITMOD InitModule;
};
struct ExternalModule ExtModules={&ExtModules,&ExtModules,NULL,NULL,NULL};
static struct ExternalModule *FindModule(char *modfile)
{
struct ExternalModule *h,*x;
for(h=&ExtModules,x=ExtModules.next;x!=h;x=x->next)if(strcmp(x->name,modfile)==0)break;
if(x==h)return NULL;
else return x;
}
static struct ExternalModule *LoadModule(char *modfile)
{
char *name;
void *handle;
INITMOD InitMod;
struct ExternalModule *x,*h;
const char *error;
name=xmalloc(strlen(modfile)+1);
strcpy(name,modfile);
modfile=stradp(modfile);
handle=dlopen(modfile,RTLD_LAZY);
if(!handle)
{
fprintf(stderr,"module: could not load %s\nmodule: %s\n",modfile,dlerror());
free(name);
return NULL;
}
#ifdef DEBUG
fprintf(stderr,"Loaded module %s\n",modfile);
#endif
InitMod=(INITMOD)dlsym(handle,"ModuleInit");
if((error=dlerror())!=NULL)
{
fprintf(stderr,"module: could not find module initialisation function\nmodule: %s\n",error);
free(name);
return NULL;
}
x=xmalloc(sizeof(*x));
h=&ExtModules;
x->name=name;
x->handle=handle;
x->InitModule=InitMod;
x->next=h;
x->prev=h->prev;
h->prev->next=x;
h->prev=x;
return x;
}
void module(int wordc, char **wordv, FILE *fp, int *line_no)
{
char *modfile;
struct ExternalModule *x;
wordv++,wordc--;
modfile=*wordv;
if(strcasecmp(modfile,"internal")==0)internal_module(wordc,wordv,fp,line_no);
else
{
x=FindModule(modfile);
if(x==NULL)x=LoadModule(modfile);
if(x==NULL)return;
time(&TimeNow);
(*x->InitModule)(wordc,wordv,fp,line_no);
}
return;
}
void RunModuleFunction(int argc, char **argv)
{
char *modfile,*function;
const char *error;
struct ExternalModule *x;
FUNCTION f;
modfile=*argv;
for(function=modfile;(*function!='\0')&&(*function!=':');function++);
if(*function=='\0')function="ModuleMain";
else *(function++)='\0';
x=FindModule(modfile);
if(x==NULL)
{
x=LoadModule(modfile);
if(x==NULL)return;
time(&TimeNow);
(*x->InitModule)(argc,argv,NULL,NULL);
}
f=(FUNCTION)dlsym(x->handle,function);
if((error=dlerror())!=NULL)
{
fprintf(stderr,"%s: could not find function %s\nmodule: %s\n",modfile,function,error);
return;
}
time(&TimeNow);
(*f)(argc,argv);
return;
}