1 /* $Id$ */
2 /* Copyright (c) 2013-2015 Pierre Pronchery <khorben@defora.org> */
3 /* This file is part of DeforaOS Desktop Browser */
4 /* Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  *    this list of conditions and the following disclaimer in the documentation
13  *    and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the authors nor the names of the contributors may be
16  *    used to endorse or promote products derived from this software without
17  *    specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY ITS AUTHORS AND CONTRIBUTORS "AS IS" AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 
30 
31 
32 #include <dirent.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <dlfcn.h>
37 #include "Browser/plugin.h"
38 
39 #ifndef PROGNAME
40 # define PROGNAME	"plugins"
41 #endif
42 
43 
44 /* private */
45 /* prototypes */
46 static int _plugins(void);
47 
48 static int _dlerror(char const * message, int ret);
49 static int _error(char const * message, char const * error, int ret);
50 static int _perror(char const * message, int ret);
51 
52 
53 /* functions */
54 /* plugins */
_plugins(void)55 static int _plugins(void)
56 {
57 	int ret = 0;
58 	const char path[] = "../src/plugins"; /* FIXME broken with OBJDIR */
59 #ifdef __APPLE__
60 	const char ext[] = ".dylib";
61 #else
62 	const char ext[] = ".so";
63 #endif
64 	DIR * dir;
65 	struct dirent * de;
66 	size_t len;
67 	char * s;
68 	void * p;
69 	BrowserPluginDefinition * bpd;
70 
71 	if((dir = opendir(path)) == NULL)
72 		return -_perror(path, 1);
73 	while((de = readdir(dir)) != NULL)
74 	{
75 		if((len = strlen(de->d_name)) < sizeof(ext))
76 			continue;
77 		if(strcmp(&de->d_name[len - sizeof(ext) + 1], ext) != 0)
78 			continue;
79 		if((s = malloc(sizeof(path) + len + 1)) == NULL)
80 		{
81 			ret += _perror(de->d_name, 1);
82 			continue;
83 		}
84 		snprintf(s, sizeof(path) + len + 1, "%s/%s", path, de->d_name);
85 		if((p = dlopen(s, RTLD_LAZY)) == NULL)
86 		{
87 			ret += _dlerror(s, 1);
88 			free(s);
89 			continue;
90 		}
91 		if((bpd = dlsym(p, "plugin")) == NULL)
92 			ret += _dlerror(s, 1);
93 		else if(bpd->icon == NULL)
94 			ret += _error(s, "No icon defined", 1);
95 		else
96 			printf("\n%s: %s (%s)\n%s\n", de->d_name, bpd->name,
97 					bpd->icon, (bpd->description != NULL)
98 					? bpd->description
99 					: "(no description)");
100 		free(s);
101 		dlclose(p);
102 	}
103 	closedir(dir);
104 	return ret;
105 }
106 
107 
108 /* dlerror */
_dlerror(char const * message,int ret)109 static int _dlerror(char const * message, int ret)
110 {
111 	fputs(PROGNAME ": ", stderr);
112 	fprintf(stderr, "%s: %s\n", message, dlerror());
113 	return ret;
114 }
115 
116 
117 /* error */
_error(char const * message,char const * error,int ret)118 static int _error(char const * message, char const * error, int ret)
119 {
120 	fputs(PROGNAME ": ", stderr);
121 	fprintf(stderr, "%s: %s\n", message, error);
122 	return ret;
123 }
124 
125 
126 /* perror */
_perror(char const * message,int ret)127 static int _perror(char const * message, int ret)
128 {
129 	fputs(PROGNAME ": ", stderr);
130 	perror(message);
131 	return ret;
132 }
133 
134 
135 /* public */
136 /* functions */
137 /* main */
main(void)138 int main(void)
139 {
140 	return (_plugins() == 0) ? 0 : 2;
141 }
142