source: roaraudio/roarclients/roarpluginrunner.c @ 5347:1d76e45ebfd1

Last change on this file since 5347:1d76e45ebfd1 was 5347:1d76e45ebfd1, checked in by phi, 12 years ago

added some support and handling code for libdeps. currently it can be parsed and if there is libdep info in a file loading is canceled because we can not load additional stuff at the moment

File size: 9.1 KB
Line 
1//roarpluginrunner.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011
5 *
6 *  This file is part of roarclients a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26int g_verbose = 0;
27#define ROAR_DBG_INFOVAR g_verbose
28
29#include <roaraudio.h>
30
31enum action {
32 RUN,
33 EXPLAIN
34};
35
36static struct roar_dl_librarypara * g_para = NULL;
37
38void usage (const char * progname) {
39 fprintf(stderr, "Usage: %s [OPTIONS]... PLUGIN\n", progname);
40
41 fprintf(stderr, "\nOptions:\n\n");
42
43 fprintf(stderr, " -h --help         - This help.\n"
44                 "    --verbose      - Be verbose. Can be used multiple times.\n"
45                 "    --run          - Run plugin.\n"
46                 "    --explain      - Explain plugin.\n");
47}
48
49static int do_run(const char * name) {
50 struct roar_dl_lhandle * lhandle = roar_dl_open(name, ROAR_DL_FLAG_DEFAULTS, 1, g_para);
51
52 if ( lhandle == NULL )
53  return -1;
54
55 roar_dl_appsched_trigger(lhandle, ROAR_DL_APPSCHED_INIT);
56
57 while (roar_dl_appsched_trigger(lhandle, ROAR_DL_APPSCHED_WAIT) == 0)
58  roar_dl_appsched_trigger(lhandle, ROAR_DL_APPSCHED_UPDATE);
59
60 roar_dl_appsched_trigger(lhandle, ROAR_DL_APPSCHED_UPDATE);
61 roar_dl_appsched_trigger(lhandle, ROAR_DL_APPSCHED_FREE);
62
63 roar_dl_unref(lhandle);
64 return 0;
65}
66
67static const char * _ptr2str(const void * p) {
68 static char buf[24];
69
70 if ( p == NULL )
71  return "<not set>";
72
73 snprintf(buf, sizeof(buf), "%p", p);
74
75 return buf;
76}
77
78static const char * _ptrrange2str(const void * p, size_t len) {
79 static char buf[48];
80
81 if ( p == NULL )
82  return "<not set>";
83
84 if ( len == 0 )
85  return _ptr2str(p);
86
87 snprintf(buf, sizeof(buf), "%p-%p", p, p + len);
88
89 return buf;
90}
91
92static int do_explain(const char * name) {
93 struct roar_dl_lhandle * lhandle = roar_dl_open(name, ROAR_DL_FLAG_LAZY, 0, NULL);
94 struct roar_dl_libraryinst * (*func)(struct roar_dl_librarypara * para);
95 struct roar_dl_libraryinst * lib;
96 int libok = 0, libnameok = 0, libdepok = 0;
97 int tmp;
98 int i;
99 size_t iter;
100 char c;
101
102 if ( lhandle == NULL )
103  return -1;
104
105 func = roar_dl_getsym(lhandle, "_roaraudio_library_init", -1);
106 if ( func == NULL ) {
107  fprintf(stderr, "Warning: Not a RA lib: %s\n", name);
108  roar_dl_unref(lhandle);
109  return 0;
110 }
111
112 lib = func(g_para);
113 if ( lib == NULL ) {
114  fprintf(stderr, "Warning: Can not RA init: %s\n", name);
115  roar_dl_unref(lhandle);
116  return 0;
117 }
118
119 if ( lib->version == ROAR_DL_LIBINST_VERSION && lib->len == sizeof(*lib) ) {
120  libok = 1;
121 }
122
123 printf("lib                     = %s\n", _ptr2str(lib));
124 printf("|-> version             = %i (%smatch)\n", lib->version, lib->version == ROAR_DL_LIBINST_VERSION ? "" : "no ");
125 printf("%c-> len                 = %zu (%smatch)\n", libok ? '|' : '\\', lib->len, lib->len == sizeof(*lib) ? "" : "no ");
126
127 if ( libok ) {
128  printf("|-> unload              = %s\n", _ptr2str(lib->unload));
129  printf("|-> func                = {");
130  i = 0;
131  while (i < ROAR_DL_FN_MAX) {
132   for (tmp = 0; i < ROAR_DL_FN_MAX; i++) {
133    if ( lib->func[i] != NULL ) {
134     break;
135    }
136    tmp++;
137   }
138   if (tmp)
139    printf("%i x <not set>%s", tmp, i < (ROAR_DL_FN_MAX-1) ? ", " : "");
140
141   if ( i < ROAR_DL_FN_MAX ) {
142    printf("[%i] = %s%s", i, _ptr2str(lib->func[i]), i < (ROAR_DL_FN_MAX-1) ? ", " : "");
143    i++;
144   }
145  }
146
147  printf("}\n");
148  printf("|-> libname             = %s\n", _ptr2str(lib->libname));
149  if ( lib->libname != NULL ) {
150   if ( lib->libname->version == ROAR_DL_LIBNAME_VERSION && lib->libname->len == sizeof(*(lib->libname)) ) {
151    libnameok = 1;
152   }
153   printf("|   |-> version         = %i (%smatch)\n", lib->libname->version,
154                                                      lib->libname->version == ROAR_DL_LIBNAME_VERSION ? "" : "no ");
155   printf("|   %c-> len             = %zu (%smatch)\n", libnameok ? '|' : '\\', lib->libname->len,
156                                                      lib->libname->len == sizeof(*(lib->libname)) ? "" : "no ");
157   if ( libnameok ) {
158#define _ps(k,name) \
159    tmp = (lib->libname->name) != NULL; \
160    printf("|   %c-> %-15s = %s%s%s\n", (k), #name, tmp ? "\"" : "", \
161                                    tmp ? (lib->libname->name) : "<not set>", tmp ? "\"" : "");
162
163    _ps('|', name);
164    _ps('|', libname);
165    _ps('|', libversion);
166    _ps('|', abiversion);
167    _ps('|', description);
168    _ps('|', contact);
169    _ps('|', authors);
170    _ps('\\', license);
171#undef _ps
172   }
173  }
174  printf("|-> global_data_len     = %zu\n", lib->global_data_len);
175  printf("|-> global_data_init    = %s\n", _ptrrange2str(lib->global_data_init, lib->global_data_len));
176  printf("|-> global_data_pointer = %s\n", _ptr2str(lib->global_data_pointer));
177  if ( lib->libdep != NULL && lib->libdep_len ) {
178   printf("|-> libdep              = %s\n", _ptr2str(lib->libdep));
179   for (iter = 0; iter < lib->libdep_len; iter++) {
180    printf("|   %c-> Table entry %zu   = %p\n", iter == (lib->libdep_len-1) ? '\\' : '|', iter, &(lib->libdep[iter]));
181    c = iter == (lib->libdep_len-1) ? ' ' : '|';
182    if ( lib->libdep[iter].version == ROAR_DL_LIBDEP_VERSION &&
183         lib->libdep[iter].len     == sizeof(struct roar_dl_librarydep) ) {
184     libdepok = 1;
185    } else {
186     libdepok = 0;
187    }
188    printf("|   %c   |-> version     = %i (%smatch)\n", c, lib->libdep[iter].version,
189                                                        lib->libdep[iter].version == ROAR_DL_LIBDEP_VERSION ? "" : "no ");
190    printf("|   %c   %c-> len         = %zu (%smatch)\n", c, libdepok ? '|' : '\\',
191                                                       lib->libdep[iter].len,
192                                                       lib->libdep[iter].len == sizeof(struct roar_dl_librarydep) ?
193                                                       "" : "no ");
194    if ( libdepok ) {
195     printf("|   %c   |-> flags       = 0x%.8lX\n", c, (unsigned long int)lib->libdep[iter].flags);
196#define _ps(k,name) \
197    tmp = (lib->libdep[iter].name) != NULL; \
198    printf("|   %c   %c-> %-11s = %s%s%s\n", c, (k), #name, tmp ? "\"" : "", \
199                                    tmp ? (lib->libdep[iter].name) : "<not set>", tmp ? "\"" : "");
200     _ps('|', name);
201     _ps('|', libname);
202     _ps('\\', abiversion);
203#undef _ps
204    }
205   }
206   printf("|-> libdep_len          = %zu\n", lib->libdep_len);
207  } else if ( (lib->libdep == NULL && lib->libdep_len) || (lib->libdep != NULL && !lib->libdep_len) ) {
208   printf("|-> libdep              = %s (invalid)\n", _ptr2str(lib->libdep));
209   printf("|-> libdep_len          = %zu (invalid)\n", lib->libdep_len);
210  }
211  printf("\\-> appsched            = %s\n", _ptr2str(lib->appsched));
212  if ( lib->appsched != NULL ) {
213   printf("    |-> init            = %s\n", _ptr2str(lib->appsched->init));
214   printf("    |-> free            = %s\n", _ptr2str(lib->appsched->free));
215   printf("    |-> update          = %s\n", _ptr2str(lib->appsched->update));
216   printf("    |-> tick            = %s\n", _ptr2str(lib->appsched->tick));
217   printf("    \\-> wait            = %s\n", _ptr2str(lib->appsched->wait));
218  }
219 }
220
221 roar_dl_unref(lhandle);
222 return 0;
223}
224
225static int do_plugin(enum action action, const char * name) {
226 switch (action) {
227  case EXPLAIN:
228    return do_explain(name);
229   break;
230  case RUN:
231    return do_run(name);
232   break;
233  default:
234    roar_err_set(ROAR_ERROR_BADRQC);
235    return -1;
236   break;
237 }
238}
239
240static inline void _clear_para(void) {
241 if ( g_para == NULL )
242  return;
243
244 roar_dl_para_unref(g_para);
245 g_para = NULL;
246}
247
248int main (int argc, char * argv[]) {
249 const char * appname    = "roarpluginrunner";
250 const char * abiversion = NULL;
251 const char * pluginargs = NULL;
252 enum action action = RUN;
253 int ret = 0;
254 int i;
255 char * k;
256
257 for (i = 1; i < argc; i++) {
258  k = argv[i];
259
260  if ( !strcmp(k, "-h") || !strcmp(k, "--help") ) {
261   usage(argv[0]);
262   return 0;
263  } else if ( !strcmp(k, "--run") ) {
264   action = RUN;
265  } else if ( !strcmp(k, "--explain") ) {
266   action = EXPLAIN;
267  } else if ( !strcmp(k, "--verbose") ) {
268   g_verbose++;
269  } else if ( !strcmp(k, "--appname") ) {
270   appname = argv[++i];
271   _clear_para();
272  } else if ( !strcmp(k, "--abiversion") ) {
273   abiversion = argv[++i];
274   _clear_para();
275  } else if ( !strcmp(k, "--args") ) {
276   pluginargs = argv[++i];
277   _clear_para();
278  } else {
279   if ( g_para == NULL )
280    g_para = roar_dl_para_new(pluginargs, NULL, appname, abiversion);
281   roar_err_set(ROAR_ERROR_NONE);
282   if ( do_plugin(action, k) == -1 ) {
283    fprintf(stderr, "Error loading plugin: %s\n",
284                    roar_error != ROAR_ERROR_NONE ? roar_error2str(roar_error) : roar_dl_errstr(NULL));
285    ret = 1;
286   }
287  }
288 }
289
290 _clear_para();
291
292 return ret;
293}
294
295//ll
Note: See TracBrowser for help on using the repository browser.