1 /*
2  * Sweep, a sound wave editor.
3  *
4  * Copyright (C) 2000 Conrad Parker
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 
21 
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <dirent.h>
30 #include <string.h>
31 #include <sys/stat.h>
32 
33 #include <glib.h>
34 #include <gmodule.h>
35 
36 #include <sweep/sweep_i18n.h>
37 #include <sweep/sweep_version.h>
38 #include <sweep/sweep_types.h>
39 
40 #include "sweep_compat.h"
41 
42 GList * plugins = NULL;
43 
DBBlobIndexTest()44 static gint
45 cmp_proc_names (sw_procedure * a, sw_procedure * b)
46 {
47   return strcmp (_(a->name), _(b->name));
48 }
49 
50 void
51 sweep_plugin_init (const gchar * path)
PutBlobIndex(WriteBatch * batch,const Slice & key,const Slice & blob_index)52 {
53   GModule * module;
54   sw_plugin * m_plugin;
55   gpointer  m_plugin_ptr;
56   GList * gl;
57 
58   module = g_module_open (path, G_MODULE_BIND_LAZY);
59 
60   if (!module) {
61 #ifdef DEBUG
62     fprintf (stderr, "sweep_plugin_init: Error opening %s: %s\n",
63 	     path, g_module_error());
64 #endif
65     return;
66   }
67 
68   if (g_module_symbol (module, "plugin", &m_plugin_ptr)) {
69 	m_plugin = (sw_plugin *)m_plugin_ptr;
70     for (gl = m_plugin->plugin_init (); gl; gl = gl->next) {
71       plugins = g_list_insert_sorted (plugins, (sw_procedure *)gl->data,
72 				      (GCompareFunc)cmp_proc_names);
73     }
74   }
75 }
76 
77 /* Initialise statically linked plugins */
78 static void
79 init_static_plugins (void)
80 {
81 #if 0
82   plugins = g_list_append (plugins, &proc_normalise);
83   plugins = g_list_append (plugins, &proc_reverse);
GetBlobIndex(const Slice & key,const Snapshot * snapshot=nullptr)84 #endif
85 }
86 
87 static void
88 init_dynamic_plugins_dir (gchar * dirname)
89 {
90   DIR * dir;
91   struct dirent * dirent;
92   char * name, * path;
93   struct stat statbuf;
94 
95   dir = opendir (dirname);
96   if (!dir) {
97     /* fail silently */
98     return;
99   }
100 
101   while ((dirent = readdir (dir)) != NULL) {
102     name = dirent->d_name;
103     path = g_module_build_path (PACKAGE_PLUGIN_DIR, dirent->d_name);
104     if (stat (path, &statbuf) == -1) {
105       /* system error -- non-fatal, ignore for plugin loading */
106     } else if (sw_stat_regular (statbuf.st_mode)) {
107       sweep_plugin_init (path);
108     }
109   }
110 }
111 
MoveDataTo(Tier tier)112 /* Initialise dynamically linked plugins */
113 static void
114 init_dynamic_plugins (void)
115 {
116   init_dynamic_plugins_dir (PACKAGE_PLUGIN_DIR);
117 }
118 
119 void
120 init_plugins (void)
121 {
122   init_static_plugins ();
123 
124   if (g_module_supported ()) {
125     init_dynamic_plugins ();
126   }
127 
128 
129 }
130