PluginManager.C 50.7 KB
Newer Older
hrchilds's avatar
hrchilds committed
1
2
/*****************************************************************************
*
brugger's avatar
 
brugger committed
3
* Copyright (c) 2000 - 2009, Lawrence Livermore National Security, LLC
hrchilds's avatar
hrchilds committed
4
* Produced at the Lawrence Livermore National Laboratory
brugger's avatar
 
brugger committed
5
* LLNL-CODE-400124
hrchilds's avatar
hrchilds committed
6
7
* All rights reserved.
*
brugger's avatar
 
brugger committed
8
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
hrchilds's avatar
hrchilds committed
9
10
11
12
13
14
15
16
17
18
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
brugger's avatar
 
brugger committed
19
20
21
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
hrchilds's avatar
hrchilds committed
22
23
24
25
26
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
brugger's avatar
 
brugger committed
27
28
29
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
hrchilds's avatar
hrchilds committed
30
31
32
33
34
35
36
37
38
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/

hrchilds's avatar
hrchilds committed
39
40
41
42
// ************************************************************************* //
//                              PluginManager.C                              //
// ************************************************************************* //
#include <PluginManager.h>
fogal1's avatar
fogal1 committed
43
44
45
46
47
48
49
50
51
#include <visit-config.h>

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <vector>

hrchilds's avatar
hrchilds committed
52
53
54
#include <DebugStream.h>
#include <InvalidDirectoryException.h>
#include <InvalidPluginException.h>
55
#include <InstallationFunctions.h>
whitlocb's avatar
whitlocb committed
56
#include <PluginBroadcaster.h>
hrchilds's avatar
hrchilds committed
57
#include <visitstream.h>
hrchilds's avatar
hrchilds committed
58

cyrush's avatar
cyrush committed
59
#if __APPLE__
treadway's avatar
treadway committed
60
61
#include <AvailabilityMacros.h>
#endif
hrchilds's avatar
hrchilds committed
62
63
64
#if defined(_WIN32)
#include <windows.h>
#include <direct.h>
whitlocb's avatar
whitlocb committed
65
66
#include <shlobj.h>
#include <shlwapi.h>
cyrush's avatar
cyrush committed
67
#elif defined(__APPLE__) && ( MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_2 )
hrchilds's avatar
hrchilds committed
68
69
#include <mach-o/dyld.h>
#include <dirent.h>
hrchilds's avatar
hrchilds committed
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
#else
#include <dlfcn.h>
#include <dirent.h>
#endif

using std::string;
using std::vector;
using std::map;
using std::pair;
using std::sort;

#define MAX_PLUGINERROR 500

// ****************************************************************************
//  Method: PluginManager constructor
//
//  Programmer: Jeremy Meredith
//  Creation:   September 26, 2001
//
//  Modifications:
//    Brad Whitlock, Thu Jul 26 12:57:46 PDT 2001
//    Added scripting plugins.
//
//    Brad Whitlock, Fri Mar 29 09:29:50 PDT 2002
//    Moved the plugin dir code to here.
//
//    Jeremy Meredith, Fri Feb 28 12:33:52 PST 2003
//    Added initialization of loadOnDemand.
//
hrchilds's avatar
hrchilds committed
99
100
101
//    Jeremy Meredith, Tue Feb  8 08:40:57 PST 2005
//    Added initialization of pluginInitErrors.
//
hrchilds's avatar
hrchilds committed
102
103
104
//    Brad Whitlock, Wed Nov 22 16:26:20 PST 2006
//    I removed the call to SetPluginDir.
//
hrchilds's avatar
hrchilds committed
105
106
// ****************************************************************************

107
108
109
110
111
112
PluginManager::PluginManager(const string &mgr) : pluginDirs(), openPlugin(),
    handle(0), pluginError(0), category(no_category), parallel(false),
    managerName(mgr), loadOnDemand(false), 
    ids(), names(), versions(), libfiles(), enabled(),
    allindexmap(), loadedindexmap(), loadedhandles(), loadedids(),
    pluginInitErrors()
hrchilds's avatar
hrchilds committed
113
114
115
116
117
118
119
120
121
122
123
{
    pluginError = new char[MAX_PLUGINERROR];
}

// ****************************************************************************
//  Method: PluginManager destructor
//
//  Programmer: Jeremy Meredith
//  Creation:   September 26, 2001
//
//  Modifications:
124
125
126
127
//    Brad Whitlock, Wed Jun 25 10:28:07 PDT 2008
//    Removed call to UnloadPlugins because it can't call back up to derived
//    classes since they've been destructed. That means that some of the 
//    methods called were back to pure virtual by the time we got here.
hrchilds's avatar
hrchilds committed
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
//
// ****************************************************************************

PluginManager::~PluginManager()
{
    delete [] pluginError;
}

// ****************************************************************************
//  Method:  PluginManager::PluginExists
//
//  Purpose:
//    Return true if the plugin exists -- this means it can be enabled and
//    it can attempt to be loaded
//
//  Arguments:
//    id         the id to check
//
//  Programmer:  Jeremy Meredith
//  Creation:    September 26, 2001
//
// ****************************************************************************

bool
PluginManager::PluginExists(const string &id)
{
    return (allindexmap.count(id) > 0);
}

hrchilds's avatar
hrchilds committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// ****************************************************************************
//  Method:  PluginManager::PluginEnabled
//
//  Purpose:
//    Returns true if the plugin indexed by the given ID is currently enabled.
//
//  Arguments:
//    id         the id to check
//
//  Programmer:  Jeremy Meredith
//  Creation:    November  5, 2003
//
// ****************************************************************************
bool
PluginManager::PluginEnabled(const string &id)
{
    return PluginExists(id) && enabled[allindexmap[id]];
}


hrchilds's avatar
hrchilds committed
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// ****************************************************************************
//  Method:  PluginManager::PluginAvailable
//
//  Purpose:
//    Return true if the plugin is available -- this means it exists, it
//    is enabled, and it is successfully loaded
//
//  Arguments:
//    id         the id to check
//
//  Programmer:  Jeremy Meredith
//  Creation:    February 27, 2003
//
// ****************************************************************************

bool
PluginManager::PluginAvailable(const string &id)
{
    if (!PluginExists(id))
        return false;

    if (loadOnDemand)
    {
        int index = GetAllIndex(id);
        LoadSinglePlugin(index);
    }

    return PluginLoaded(id);
}

// ****************************************************************************
//  Method:  PluginManager::PluginLoaded
//
//  Purpose:
//    Return true if the plugin was succesfully loaded
//
//  Arguments:
//    id         the id to check
//
//  Programmer:  Jeremy Meredith
//  Creation:    September 26, 2001
//
// ****************************************************************************

bool
PluginManager::PluginLoaded(const string &id)
{
    return (loadedindexmap.count(id) > 0);
}

// ****************************************************************************
//  Method:  PluginManager::GetPluginName
//
//  Purpose:
//    Get the name of a plugin by its id
//
//  Arguments:
//    id         the plugin id
//
//  Programmer:  Jeremy Meredith
//  Creation:    September 26, 2001
//
hrchilds's avatar
hrchilds committed
239
240
241
242
//  Modfications:
//    Brad Whitlock, Thu Jul 17 09:56:19 PDT 2003
//    Prevented case where a bad id could add itself to the allindexmap.
//
hrchilds's avatar
hrchilds committed
243
244
245
246
247
// ****************************************************************************

string
PluginManager::GetPluginName(const string &id)
{
hrchilds's avatar
hrchilds committed
248
249
250
251
252
253
254
255
256
    string retval;
    if(allindexmap.find(id) != allindexmap.end())
    {
        int index = allindexmap[id];
        if(index < names.size())
            retval = names[index];
    }

    return retval;
hrchilds's avatar
hrchilds committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
}

// ****************************************************************************
//  Method:  PluginManager::GetPluginVersion
//
//  Purpose:
//    Get the version of a plugin by its id
//
//  Arguments:
//    id         the plugin id
//
//  Programmer:  Jeremy Meredith
//  Creation:    September 26, 2001
//
hrchilds's avatar
hrchilds committed
271
272
273
274
//  Modifications:
//    Brad Whitlock, Thu Jul 17 09:56:19 PDT 2003
//    Prevented case where a bad id could add itself to the allindexmap.
//
hrchilds's avatar
hrchilds committed
275
276
277
278
279
// ****************************************************************************

string
PluginManager::GetPluginVersion(const string &id)
{
hrchilds's avatar
hrchilds committed
280
281
282
283
284
285
286
287
288
    string retval;
    if(allindexmap.find(id) != allindexmap.end())
    {
        int index = allindexmap[id];
        if(index < versions.size())
            retval = versions[index];
    }

    return retval;
hrchilds's avatar
hrchilds committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
}

// ****************************************************************************
//  Method: PluginManager::GetNAllPlugins
//
//  Purpose:
//    Return the number of all plugins.
//
//  Returns:    The number of all plugins.
//
//  Programmer: Jeremy Meredith
//  Creation:   September 26, 2001
//
// ****************************************************************************

int
PluginManager::GetNAllPlugins() const
{
    return ids.size();
}

// ****************************************************************************
//  Method: PluginManager::GetAllID
//
//  Purpose:
//    Return the name id of a plugin by its overall index.
//
//  Arguments:
//    index     The index of a plugin by its overall index
//
//  Returns:    The id of a plugin.
//
//  Programmer: Jeremy Meredith
//  Creation:   September 26, 2001
//
// ****************************************************************************

string
PluginManager::GetAllID(int index) const
{
    return ids[index];
}

// ****************************************************************************
//  Method: PluginManager::GetNEnabledPlugins
//
//  Purpose:
//    Return the number of loaded plugins.
//
//  Returns:    The number of loaded plugins.
//
//  Programmer: Jeremy Meredith
//  Creation:   September 26, 2001
//
// ****************************************************************************

int
PluginManager::GetNEnabledPlugins() const
{
    return loadedhandles.size();
}

// ****************************************************************************
//  Method: PluginManager::GetEnabledID
//
//  Purpose:
//    Return the name id of a loaded plugin.
//
//  Arguments:
//    index     The index of a loaded plugin id
//
//  Returns:    The id of a plugin.
//
//  Programmer: Jeremy Meredith
//  Creation:   September 26, 2001
//
// ****************************************************************************

string
PluginManager::GetEnabledID(int index) const
{
    return loadedids[index];
}

hrchilds's avatar
hrchilds committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// ****************************************************************************
//  Method: PluginManager::GetEnabledIndex
//
//  Purpose:
//    Return the enabled-index of a loaded plugin.
//
//  Arguments:
//    id     The id of a plugin.
//
//  Returns:    The index of a loaded plugin id.
//
//  Programmer: Jeremy Meredith
//  Creation:   June 17, 2003
//
hrchilds's avatar
hrchilds committed
387
388
389
390
391
//  Modifications:
//    Brad Whitlock, Thu Jul 17 09:51:19 PDT 2003
//    Added a check to prevent the case where a bad id could add itself to
//    the map.
//
hrchilds's avatar
hrchilds committed
392
393
394
395
396
// ****************************************************************************

int
PluginManager::GetEnabledIndex(const std::string &id)
{
hrchilds's avatar
hrchilds committed
397
398
399
400
401
    int retval = -1;
    if(loadedindexmap.find(id) != loadedindexmap.end())
        retval = loadedindexmap[id];

    return retval;
hrchilds's avatar
hrchilds committed
402
403
}

hrchilds's avatar
hrchilds committed
404
405
406
407
408
409
410
411
412
413
414
415
// ****************************************************************************
//  Method: PluginManager::DisablePlugin
//
//  Purpose:
//    Disable a plugin if it is enabled
//
//  Arguments:
//    id        The id of a plugin
//
//  Programmer: Jeremy Meredith
//  Creation:   September 26, 2001
//
hrchilds's avatar
hrchilds committed
416
417
418
419
420
//  Modifications:
//    Brad Whitlock, Thu Jul 17 09:51:19 PDT 2003
//    Added a check to prevent the case where a bad id could add itself to
//    the map.
//
hrchilds's avatar
hrchilds committed
421
422
423
424
425
// ****************************************************************************

void
PluginManager::DisablePlugin(const string &id)
{
hrchilds's avatar
hrchilds committed
426
427
428
429
430
431
    if(allindexmap.find(id) != allindexmap.end())
    {
        int index = allindexmap[id];
        if(index < enabled.size())
            enabled[index] = false;
    }
hrchilds's avatar
hrchilds committed
432
433
434
435
436
437
438
439
440
441
442
443
444
445
}

// ****************************************************************************
//  Method: PluginManager::EnablePlugin
//
//  Purpose:
//    Enable a plugin if it is disabled
//
//  Arguments:
//    id        The id of a plugin
//
//  Programmer: Jeremy Meredith
//  Creation:   September 26, 2001
//
hrchilds's avatar
hrchilds committed
446
447
448
449
450
//  Modifications:
//    Brad Whitlock, Thu Jul 17 09:51:19 PDT 2003
//    Added a check to prevent the case where a bad id could add itself to
//    the map.
//
hrchilds's avatar
hrchilds committed
451
452
453
454
455
// ****************************************************************************

void
PluginManager::EnablePlugin(const string &id)
{
hrchilds's avatar
hrchilds committed
456
457
458
459
460
461
    if(allindexmap.find(id) != allindexmap.end())
    {
        int index = allindexmap[id];
        if(index < enabled.size())
            enabled[index] = true;
    }
hrchilds's avatar
hrchilds committed
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
}

// ****************************************************************************
// Method: PluginManager::GetPluginList
//
// Purpose: 
//   Reads the list of files that should be checked to see if they are 
//   plugins.
//
// Arguments:
//   libs : The list of filenames that we're returning.
//
// Programmer: Jeremy Meredith, Brad Whitlock
// Creation:   Fri Mar 29 09:09:16 PDT 2002
//
// Modifications:
//    Jeremy Meredith, Fri Jul  5 17:36:23 PDT 2002
//    Made it work on plugins from multiple directories.
//
hrchilds's avatar
hrchilds committed
481
482
483
484
//    Brad Whitlock, Thu Aug 21 14:10:51 PST 2003
//    Added code to prevent the same plugin from being in the list multiple
//    times.
//
hrchilds's avatar
hrchilds committed
485
486
487
//    Jeremy Meredith, Thu Mar 25 14:05:21 PST 2004
//    Use only the first plugin with the same name, independent of directory.
//
488
489
490
491
//    Hank Childs, Fri Jul  6 15:04:56 PDT 2007
//    Allow for duplicates.  Throwing out duplicates means that a bad private
//    plugin disallows the public one from getting loaded.
//
492
493
494
495
//    Hank Childs, Fri Oct  5 15:42:58 PDT 2007
//    Honor the precedence of the plugin directories, rather than just throwing
//    them in a giant list.
//
hrchilds's avatar
hrchilds committed
496
497
498
//    Hank Childs, Fri Nov 16 15:24:16 PST 2007
//    Fix bug pointed out by Paul Selby of AWE.
//
hrchilds's avatar
hrchilds committed
499
500
501
502
503
504
// ****************************************************************************

void
PluginManager::GetPluginList(vector<pair<string,string> > &libs)
{
    // Read the files in the plugin directory.
505
    vector< vector<pair<string,string> > > files;
hrchilds's avatar
hrchilds committed
506
507
508
    ReadPluginDir(files);

    // Add each file that is a library to the list.
509
    string ext(VISIT_PLUGIN_EXTENSION);
hrchilds's avatar
hrchilds committed
510
    int extLen = ext.size();
hrchilds's avatar
hrchilds committed
511

512
513
    vector< vector<pair<string,string> > > tmp;
    tmp.resize(files.size());
514
    for (size_t dir = 0 ; dir < files.size() ; dir++)
hrchilds's avatar
hrchilds committed
515
    {
516
        for (size_t f = 0; f < files[dir].size(); f++)
517
518
        {
            const string &filename = files[dir][f].second;
hrchilds's avatar
hrchilds committed
519

520
521
522
            // Ignore dot directories
            if (filename == "." || filename == "..")
                continue;
hrchilds's avatar
hrchilds committed
523
524
525

#define PLUGIN_MAX(A,B) (((A) < (B)) ? (B) : (A))

526
527
528
529
530
531
            // Ignore it if it does not end in the correct extension
            if (filename.length() < PLUGIN_MAX(5,extLen) ||
                !(filename.substr(filename.length()-extLen,extLen) == ext))
            {
                continue;
            }
hrchilds's avatar
hrchilds committed
532
533

#undef PLUGIN_MAX
534
535
536
            // It is a valid library name so add it to the list.
            tmp[dir].push_back(files[dir][f]);
        }
hrchilds's avatar
hrchilds committed
537
538
    }

539
540
    // Sort the file names, but keep the precedence of the directory
    // they came from preserved.
541
    for (size_t dir = 0 ; dir < tmp.size() ; dir++)
542
    {
hrchilds's avatar
hrchilds committed
543
        sort(tmp[dir].begin(), tmp[dir].end());
544
        for (size_t f = 0 ; f < tmp[dir].size() ; f++)
hrchilds's avatar
hrchilds committed
545
            libs.push_back(tmp[dir][f]);
546
    }
hrchilds's avatar
hrchilds committed
547
548
}

549

hrchilds's avatar
hrchilds committed
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
// ****************************************************************************
//  Method: PluginManager::ReadPluginInfo
//
//  Purpose:
//    Read general info about plugins from the shared libraries.
//    Skip plugins which do not have libraries for the current VisIt component.
//
//  Programmer: Jeremy Meredith
//  Creation:   September 26, 2001
//
//  Modifications:
//    Brad Whitlock, Tue Nov 6 14:31:11 PST 2001
//    Added scripting plugins.
//
//    Jeremy Meredith, Thu Dec 13 11:40:28 PST 2001
//    Added error check for length of possible library name.
//    Closed the unused libI* libraries to free the file descriptors.
//
//    Jeremy Meredith, Wed Jan 16 09:48:16 PST 2002
//    Made the plugin extension not include _ser or _par.  Made the
//    engine add the _ser/_par itself based on the new "parallel" flag.
//
//    Jeremy Meredith, Fri Jul  5 17:36:23 PDT 2002
//    Made it work on plugins from multiple directories.
//
//    Jeremy Meredith, Tue Jul  9 17:43:45 PDT 2002
//    Made it only match I and GSVE plugins from the same directory.
//    Made it refuse to load more than one plugin with the same id.
//
hrchilds's avatar
hrchilds committed
579
580
581
582
583
//    Jeremy Meredith, Tue Feb  8 09:02:32 PST 2005
//    Added logic to confirm that the version the plugin was generated
//    with matches the current version of VisIt.  Added code to store
//    the appropriate list of errors.
//
hrchilds's avatar
hrchilds committed
584
585
586
587
//    Jeremy Meredith, Thu Mar  3 11:46:05 PST 2005
//    Track the missing vs old plugin version separately, because solving
//    them correctly requires a different action in each case.
//
588
589
590
591
592
593
594
595
//    Hank Childs, Fri Jul  6 14:44:55 PDT 2007
//    Look through the libraries for duplicates.  Previously, duplicates were
//    sorted out by GetPluginList.  But this made it difficult to load a 
//    public one if the private one was bad.  Now the duplicates get sent to
//    this function and we can load public plugins if the private ones fail to
//    load.
//    Also, dump warning messages into the debug logs as well.
//
596
597
598
//    Hank Childs, Thu Aug 30 08:45:02 PDT 2007
//    Clean up debug statements.
//
599
600
601
//    Jeremy Meredith, Wed Dec 12 16:09:38 EST 2007
//    Allow plugins to be compatible across point releases.
//
602
603
604
//    Kathleen Bonnell, Wed May 21 08:12:16 PDT 2008 
//    Fix libs indexing when searching for match.
//
whitlocb's avatar
whitlocb committed
605
606
607
//    Brad Whitlock, Thu Apr 23 11:55:41 PDT 2009
//    Ignore any SimV plugin if we're not in the engine and not a simulation.
//
brugger's avatar
 
brugger committed
608
609
610
//    Eric Brugger, Wed Sep  2 08:06:13 PDT 2009
//    I corrected a typo in a debug log message.
//
hrchilds's avatar
hrchilds committed
611
612
613
614
615
616
617
618
619
// ****************************************************************************

void
PluginManager::ReadPluginInfo()
{
    // Get the list of plugins to load.
    vector<pair<string,string> > libs;
    GetPluginList(libs);

hrchilds's avatar
hrchilds committed
620
621
    // Keep track of plugin version errors
    vector<string> pluginsWithWrongVersion;
hrchilds's avatar
hrchilds committed
622
    vector<string> pluginsWithNoVersion;
hrchilds's avatar
hrchilds committed
623

hrchilds's avatar
hrchilds committed
624
    // Read the plugin info for each plugin in the libs list.
625
    string ext(VISIT_PLUGIN_EXTENSION);
626
627
    vector<string> alreadyLoaded;
    vector<string> alreadyLoadedDir;
628
    for (size_t i=0; i<libs.size(); i++)
hrchilds's avatar
hrchilds committed
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
    {
        const string &dirname  = libs[i].first;
        const string &filename = libs[i].second;
        // only try for general info libraries
        if (!IsGeneralPlugin(filename))
            continue;

        // make sure there's a match for our library
        string str;
        switch (category)
        {
          case GUI:       str = string("libG") + filename.substr(4); break;
          case Scripting: str = string("libS") + filename.substr(4); break;
          case Viewer:    str = string("libV") + filename.substr(4); break;
          case MDServer:  str = string("libM") + filename.substr(4); break;
whitlocb's avatar
whitlocb committed
644
645
646
647
648
649
650
651
652
          case Engine:    if(filename.substr(0,8) == "libISimV")
                          {
                              debug1 << "Skipping plugin " << filename
                                     << " because it is a simulation plugin."
                                     << endl;
                              continue;
                          }
                          // Fall through to Simulation
          case Simulation:str = string("libE") +
whitlocb's avatar
whitlocb committed
653
654
655
                          filename.substr(4, filename.length() - 4 - ext.size())
                          + (parallel ? string("_par") : string("_ser"))
                          + ext;
hrchilds's avatar
hrchilds committed
656
657
658
                          break;
        }
        bool match = false;
659
        for (size_t j=0; j<libs.size() && !match; j++)
hrchilds's avatar
hrchilds committed
660
        {
661
            if (libs[j].first  == dirname &&
hrchilds's avatar
hrchilds committed
662
663
664
665
666
667
                libs[j].second == str)
                match = true;
        }
        if (!match)
            continue;

668
669
670
        // see if this plugin has already been loaded, presumably because
        // we loaded a private one and now we're considering the installed
        // version.
671
        for (size_t j = 0 ; j < alreadyLoaded.size() ; j++)
672
673
674
675
        {
            if (filename == alreadyLoaded[j])
            {
                debug1 << "Skipping plugin " << filename << " in " << dirname 
whitlocb's avatar
whitlocb committed
676
677
                       << ", since a plugin by that name was already loaded "
                       << "from " << alreadyLoadedDir[j] << endl;
678
679
680
681
                continue;
            }
        }

hrchilds's avatar
hrchilds committed
682
        // We're okay, now try to open the plugin info.
683
        string pluginFile(dirname + VISIT_SLASH_STRING + filename);
hrchilds's avatar
hrchilds committed
684
        PluginOpen(pluginFile);
hrchilds's avatar
hrchilds committed
685
686
687
        const char **VisItPluginVersion =
                              (const char**)PluginSymbol("VisItPluginVersion");
        bool success;
hrchilds's avatar
hrchilds committed
688
689
690
691
692
        if (!VisItPluginVersion)
        {
            pluginsWithNoVersion.push_back(pluginFile);
            success = false; 
        }
693
        else if (!VisItVersionsCompatible(*VisItPluginVersion, VISIT_VERSION))
hrchilds's avatar
hrchilds committed
694
695
696
697
698
699
700
701
        {
            pluginsWithWrongVersion.push_back(pluginFile);
            success = false; 
        }
        else
        {
            success = LoadGeneralPluginInfo();
        }
hrchilds's avatar
hrchilds committed
702
703
704
705
706
707
        PluginClose();

        if (success)
        {
            // Add the name of the category plugin to the list of plugins
            // that will be loaded later.
708
709
            alreadyLoaded.push_back(filename);
            alreadyLoadedDir.push_back(dirname);
710
            libfiles.push_back(dirname + VISIT_SLASH_STRING + str);
hrchilds's avatar
hrchilds committed
711
712
713
        }
    }

brugger's avatar
 
brugger committed
714
    debug1 << "Successfully loaded info about " 
hrchilds's avatar
hrchilds committed
715
           << ids.size() << " " << managerName.c_str() << " plugins.\n";
hrchilds's avatar
hrchilds committed
716
717
718
719
720

    if (pluginsWithWrongVersion.size() != 0)
    {
        pluginInitErrors += "The following plugins were built with an old "
            "version of VisIt.  Please either rebuild the plugins associated "
hrchilds's avatar
hrchilds committed
721
            "with these files or delete them:\n";
722
        for (size_t i=0; i<pluginsWithWrongVersion.size(); i++)
hrchilds's avatar
hrchilds committed
723
724
        {
            string pluginFile(pluginsWithWrongVersion[i]);
725
            string ext(VISIT_PLUGIN_EXTENSION);
hrchilds's avatar
hrchilds committed
726
727
728
729
730
731
732
            int slashPos = pluginFile.rfind("/");
            string dirname = pluginFile.substr(0, slashPos);
            int suffixLen = (pluginFile.find("_ser") != -1 ||
                             pluginFile.find("_par") != -1) ? 4 : 0;
            int len = pluginFile.size() - slashPos - suffixLen - 5 -
                managerName.size() - ext.size();
            string pluginPrefix(pluginFile.substr(slashPos + 5, len));
733
            string pluginlib(pluginFile.substr(slashPos + 1, 
whitlocb's avatar
whitlocb committed
734
                                             pluginFile.size() - (slashPos+1)));
hrchilds's avatar
hrchilds committed
735
736
737

            pluginInitErrors += string("   the ")+pluginPrefix+
                                " plugin in the directory "+dirname+"\n";
738
            for (size_t j = 0 ; j < alreadyLoaded.size() ; j++)
739
740
741
742
743
744
            {
                if (alreadyLoaded[j] == pluginlib)
                    pluginInitErrors += string("\t(Note that the plugin from ")
                                     + alreadyLoadedDir[j] 
                                     + " is being used in its place.)\n";
            }
hrchilds's avatar
hrchilds committed
745
        }
hrchilds's avatar
hrchilds committed
746
747
748
749
750
751
752
753
754
755
        pluginInitErrors += "\n";
    }

    if (pluginsWithNoVersion.size() != 0)
    {
        pluginInitErrors += "The following plugins are missing version "
            "information;  this means that their source code was generated "
            "with an old version of VisIt.  Please either regenerate the "
            "plugin info files by running xml2info and rebuild them, or else "
            "simply delete them:\n";
756
        for (size_t i=0; i<pluginsWithNoVersion.size(); i++)
hrchilds's avatar
hrchilds committed
757
758
        {
            string pluginFile(pluginsWithNoVersion[i]);
759
            string ext(VISIT_PLUGIN_EXTENSION);
hrchilds's avatar
hrchilds committed
760
761
762
763
764
765
766
767
768
769
770
771
            int slashPos = pluginFile.rfind("/");
            string dirname = pluginFile.substr(0, slashPos);
            int suffixLen = (pluginFile.find("_ser") != -1 ||
                             pluginFile.find("_par") != -1) ? 4 : 0;
            int len = pluginFile.size() - slashPos - suffixLen - 5 -
                managerName.size() - ext.size();
            string pluginPrefix(pluginFile.substr(slashPos + 5, len));

            pluginInitErrors += string("   the ")+pluginPrefix+
                                " plugin in the directory "+dirname+"\n";
        }
        pluginInitErrors += "\n";
hrchilds's avatar
hrchilds committed
772
773
    }

774
775
776
777
778
    if (pluginInitErrors != "")
    {
         debug1 << "Going to print the following message to the user: " << endl;
         debug1 << pluginInitErrors;
    }
hrchilds's avatar
hrchilds committed
779
780
}

whitlocb's avatar
whitlocb committed
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
// ****************************************************************************
// Method: PluginManager::BroadcastGeneralInfo
//
// Purpose: 
//   This method broadcasts the general libI information to other processors
//   using a PluginBroadcaster object.
//
// Arguments:
//   broadcaster : The broadcaster to use.
//
// Programmer: Brad Whitlock
// Creation:   Thu Jun 18 11:22:52 PDT 2009
//
// Modifications:
//   
// ****************************************************************************

void
PluginManager::BroadcastGeneralInfo(PluginBroadcaster *broadcaster)
{
    broadcaster->BroadcastStringVector(ids);
    broadcaster->BroadcastStringVector(names);
    broadcaster->BroadcastStringVector(versions);
    broadcaster->BroadcastStringVector(libfiles);
    broadcaster->BroadcastBoolVector(enabled);
}

// ****************************************************************************
// Method: PluginManager::ObtainPluginInfo
//
// Purpose: 
//   This method gets the plugin info by calling ReadPluginInfo or from the
//   rpiCallback function.
//
// Arguments:
//   readInfo    : Whether to read the plugin info directly.
//   broadcaster : The broadcaster to use for sending plugin info to other procs.
//
// Returns:    
//
// Note:       In most cases, ReadPluginInfo will be called. However, in parallel
//             we can install a callback function lets us share the plugin info
//             read by processor 0 with the other processors via broadcasts.
//
// Programmer: Brad Whitlock
// Creation:   Wed Jun 17 10:15:11 PDT 2009
//
// Modifications:
//   
// ****************************************************************************

void
PluginManager::ObtainPluginInfo(bool readInfo, PluginBroadcaster *broadcaster)
{
    if(readInfo || broadcaster == 0)
        ReadPluginInfo();

    if(broadcaster != 0)
    {
        BroadcastGeneralInfo(broadcaster);

        // If we used the broadcaster to populate ids, names, etc. then that would
        // not have set up the appropriate items in allindexmap. Do that now.
        if(allindexmap.size() == 0)
        {
            for(size_t i = 0; i < ids.size(); ++i)
                allindexmap[ids[i]] = i;
        }

        debug5 << "Shared information about " << ids.size() << " " << managerName
               << " plugins." << endl;
    }

#if 0
    // Keep this for debugging
    for(size_t i = 0; i < ids.size(); ++i)
    {
        debug1 << "plugin " << i << ":\n";
        debug1 << "\tid      = " << ids[i] << endl;
        debug1 << "\tname    = " << names[i] << endl;
        debug1 << "\tversion = " << versions[i] << endl;
        debug1 << "\tlibfile = " << libfiles[i] << endl;
        debug1 << "\tenabled = " << (enabled[i] ? "true" : "false") << endl;
        debug1 << "\tallindexmap[id] = " << allindexmap[ids[i]] << endl;
    }
#endif
}

hrchilds's avatar
hrchilds committed
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
// ****************************************************************************
// Method: PluginManager::IsGeneralPlugin
//
// Purpose: 
//   Returns whether or not the file is a common plugin.
//
// Arguments:
//   pluginFile : The name of the plugin.
//
// Returns:    Whether or not the file is a common plugin.
//
// Programmer: Brad Whitlock
// Creation:   Fri Mar 29 13:29:13 PST 2002
//
// Modifications:
//   
// ****************************************************************************

bool
PluginManager::IsGeneralPlugin(const string &pluginFile) const
{
     return (pluginFile.substr(0,4) == "libI");
}

// ****************************************************************************
//  Method: PluginManager::LoadPluginsNow
//
//  Purpose:
//    Read the plugins from the shared library.
//
//  Programmer: Jeremy Meredith
//  Creation:   February 27, 2003
//
//  Modifications:
//
// ****************************************************************************

void
PluginManager::LoadPluginsNow()
{
909
    for (size_t i=0; i<libfiles.size(); i++)
hrchilds's avatar
hrchilds committed
910
911
912
913
914
    {
        LoadSinglePlugin(i);
    }
}

915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
// ****************************************************************************
//  Method: PluginManager::LoadSinglePluginNow
//
//  Purpose: Load a specific plugin 
//
//  Programmer: Mark C. Miller
//  Creation:   August 5, 2007 
//
// ****************************************************************************

bool
PluginManager::LoadSinglePluginNow(const std::string& id)
{
    int index = GetAllIndex(id);
    return LoadSinglePlugin(index);
}

hrchilds's avatar
hrchilds committed
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
// ****************************************************************************
//  Method: PluginManager::LoadSinglePlugin
//
//  Purpose:
//    Read a single plugin from the shared library.
//
//  Programmer: Jeremy Meredith
//  Creation:   September 26, 2001
//
//  Modifications:
//    Brad Whitlock, Tue Nov 6 14:29:42 PST 2001
//    Added scripting plugins.
//
//    Brad Whitlock, Fri Mar 29 12:24:28 PDT 2002
//    Moved most of the code to other methods so it is easier to port.
//
hrchilds's avatar
hrchilds committed
948
949
950
951
952
//    Jeremy Meredith, Wed Oct 29 13:16:48 PST 2003
//    Added code to catch the PluginOpen call and simply print an error
//    and fail to load the plugin, as long as we're not the viewer or
//    the gui.
//
hrchilds's avatar
hrchilds committed
953
954
955
//    Hank Childs, Fri Jan 28 13:19:33 PST 2005
//    Use catch return.
//
956
957
958
//    Mark C. Miller, Mon Aug  6 13:36:16 PDT 2007
//    Changed return value to bool to indicate if it actually loaded
//    the plugin.
whitlocb's avatar
whitlocb committed
959
960
961
962
//
//    Brad Whitlock, Thu Apr 23 11:56:46 PDT 2009
//    Added Simulation case.
//
963
964
//    Mark C. Miller, Wed Jun 17 14:27:08 PDT 2009
//    Replaced CATCHALL(...) with CATCHALL.
hrchilds's avatar
hrchilds committed
965
966
// ****************************************************************************

967
bool
hrchilds's avatar
hrchilds committed
968
969
970
971
972
973
974
PluginManager::LoadSinglePlugin(int index)
{
    if (!enabled[index])
    {
        debug1 << "Skipping disabled "<<managerName.c_str()<<" plugin "
               << names[index].c_str() << " version " << versions[index].c_str()
               << endl;
975
        return false;
hrchilds's avatar
hrchilds committed
976
977
978
979
980
981
982
    }

    if (PluginLoaded(ids[index]))
    {
        debug1 << "Skipping already loaded "<<managerName.c_str()<<" plugin "
               << names[index].c_str() << " version " << versions[index].c_str()
               << endl;
983
        return false;
hrchilds's avatar
hrchilds committed
984
985
986
    }

    // Open the plugin
hrchilds's avatar
hrchilds committed
987
988
989
990
    TRY
    {
        PluginOpen(libfiles[index]);
    }
991
    CATCHALL
hrchilds's avatar
hrchilds committed
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
    {
        if (category==GUI || category==Viewer)
        {
            // These guys can't handle failing to load a plugin
            // once they decided to load it
            RETHROW;
        }
        else
        {
            // Everyone else can!
            debug1 << "Skipping " << managerName.c_str() << " plugin "
                   << names[index].c_str()
                   << " version " << versions[index].c_str()
                   << " because it failed to open." << endl;
1006
            CATCH_RETURN2(1, false);
hrchilds's avatar
hrchilds committed
1007
1008
1009
        }
    }
    ENDTRY
hrchilds's avatar
hrchilds committed
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029

    // Success so far -- add the handle and the info to the list
    int loadedindex = loadedhandles.size();
    loadedindexmap[ids[index]] = loadedindex;
    loadedhandles.push_back(handle);
    loadedids.push_back(ids[index]);
    switch (category)
    {
      case GUI:
        LoadGUIPluginInfo();
        break;
      case Viewer:
        LoadViewerPluginInfo();
        break;
      case MDServer:
        LoadMDServerPluginInfo();
        break;
      case Engine:
        LoadEnginePluginInfo();
        break;
whitlocb's avatar
whitlocb committed
1030
1031
1032
      case Simulation:
        LoadEnginePluginInfo();
        break;
hrchilds's avatar
hrchilds committed
1033
1034
1035
1036
1037
      case Scripting:
        LoadScriptingPluginInfo();
        break;
    }

hrchilds's avatar
hrchilds committed
1038
    debug1 << "Loaded full "<<managerName.c_str()<<" plugin "
hrchilds's avatar
hrchilds committed
1039
1040
           << names[index].c_str() << " version " << versions[index].c_str()
           << endl;
1041
1042

    return true;
hrchilds's avatar
hrchilds committed
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
}

// ****************************************************************************
//  Method: PluginManager::LoadPluginsOnDemand
//
//  Purpose:
//    Tell the plugin manager that actual plugin data should be loaded on
//    demand, not right now.
//
//  Programmer: Jeremy Meredith
//  Creation:   February  6, 2003
//
//  Modifications:
hrchilds's avatar
hrchilds committed
1056
1057
1058
1059
1060
1061
//    Jeremy Meredith, Tue Aug 16 11:25:07 PDT 2005
//    Enable all plugins when loading on demand.  There is currently no path
//    to inform the Engine which plugins are actually enabled (see '6489), but
//    since we are loading plugins on demand, it is simpler and perfectly
//    correct to blindly enable all plugins in this case.
//
hrchilds's avatar
hrchilds committed
1062
1063
1064
1065
1066
1067
// ****************************************************************************

void
PluginManager::LoadPluginsOnDemand()
{
    loadOnDemand = true;
hrchilds's avatar
hrchilds committed
1068
1069
1070
1071
1072
1073
1074
1075

    // If we are loading plugins on demand, there is no
    // reason to leave any plugin disabled.
    int nAllPlugins = ids.size();
    for (int i=0; i<nAllPlugins; i++)
    {
        EnablePlugin(ids[i]);
    }
hrchilds's avatar
hrchilds committed
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
}

// ****************************************************************************
//  Method: PluginManager::UnloadPlugins
//
//  Purpose:
//    Free the loaded plugins.
//
//  Programmer: Jeremy Meredith
//  Creation:   September 26, 2001
//
//  Modifications:
//    Brad Whitlock, Tue Nov 6 14:32:29 PST 2001
//    Added scripting plugins.
//
//    Brad Whitlock, Fri Mar 29 09:04:03 PDT 2002
//    Changed how plugins are closed.
//
// ****************************************************************************

void
PluginManager::UnloadPlugins()
{
    FreeCommonPluginInfo();
    FreeGUIPluginInfo();
    FreeViewerPluginInfo();
    FreeMDServerPluginInfo();
    FreeEnginePluginInfo();
    FreeScriptingPluginInfo();

    // Close the plugins.
1107
    for (size_t i=0; i < loadedhandles.size(); i++)
hrchilds's avatar
hrchilds committed
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
    {
        handle = loadedhandles[i];
        PluginClose();
    }
    loadedhandles.clear();

    loadedindexmap.clear();
}

// ****************************************************************************
//  Method: PluginManager::ReloadPlugins
//
//  Purpose:
//    Free the loaded plugins.
//
//  Programmer: Jeremy Meredith
//  Creation:   August 20, 2002
//
//  Modifications:
//    Jeremy Meredith, Fri Feb 28 12:25:37 PST 2003
//    Made it use LoadPluginsNow or LoadPluginsOnDemand as appropriate.
//
// ****************************************************************************

void
PluginManager::ReloadPlugins()
{
    UnloadPlugins();

    if (loadOnDemand)
        LoadPluginsOnDemand();
    else
        LoadPluginsNow();
}

// ****************************************************************************
// Method: PluginManager::GetAllIndex
//
// Purpose: 
//   Returns the index of the specified plugin within the plugin manager.
//
// Arguments:
//   id : The id of the plugin that we're looking for.
//
// Returns:    The index of the specified plugin or -1 if the lookup fails.
//
// Note:       This is useful for determining if a plugin id exists in the
//             plugin list.
//
// Programmer: Brad Whitlock
// Creation:   Tue Jul 24 11:30:49 PDT 2001
//
// Modifications:
//   
// ****************************************************************************

int
PluginManager::GetAllIndex(const string &id) const
{
    //
    // Look through the entire list of plugins for the specified name.
    //
    int pluginTypeIndex = -1;
1171
    for(size_t i = 0; i < ids.size(); ++i)
hrchilds's avatar
hrchilds committed
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
    {
        if(id == ids[i])
        {
            pluginTypeIndex = i;
            break;
        }
    }

    return pluginTypeIndex;
}

// ****************************************************************************
// Method: PluginManager::GetAllIndexFromName
//
// Purpose: 
//   Returns the index of the specified plugin within the plugin manager.
//
// Arguments:
//   name : The name of the plugin that we're looking for.
//
// Returns:    The index of the specified plugin or -1 if the lookup fails.
//
// Note:       This is useful for determining if a plugin id exists in the
//             plugin list.
//
// Programmer: Kathleen Bonnell 
// Creation:   April 25, 2002 
//
// Modifications:
//   
// ****************************************************************************

int
PluginManager::GetAllIndexFromName(const string &name) const
{
    //
    // Look through the entire list of plugins for the specified name.
    //
    int pluginTypeIndex = -1;
1211
    for(size_t i = 0; i < names.size(); ++i)
hrchilds's avatar
hrchilds committed
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
    {
        if(name == names[i])
        {
            pluginTypeIndex = i;
            break;
        }
    }

    return pluginTypeIndex;
}

hrchilds's avatar
hrchilds committed
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
// ****************************************************************************
//  Method:  PluginManager::GetPluginInitializationErrors
//
//  Purpose:
//    Return errors generated by plugin initialization, then clear
//    them out so we don't wind up reporting errors twice.
//
//  Arguments:
//    none
//
//  Programmer:  Jeremy Meredith
//  Creation:    February  7, 2005
//
// ****************************************************************************

std::string
PluginManager::GetPluginInitializationErrors()
{
    std::string ret = pluginInitErrors;
    pluginInitErrors = "";
    return ret;
}

hrchilds's avatar
hrchilds committed
1246
1247
1248
1249
1250
1251
1252
1253
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///
/// Methods that have conditionally compiled code for multiple platforms.
///
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

whitlocb's avatar
whitlocb committed
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
// ****************************************************************************
// Method: AddUniquePluginDir
//
// Purpose: 
//   Adds a plugin directory to the pluginDirs vector if it has not been added
//   yet. This prevents a plugin directory from being added more than once
//   as can happen when the user provides a redundant -plugindir command line
//   option.
//
// Arguments:
//   path : The path to add.
//
// Programmer: Brad Whitlock
// Creation:   Wed Jun 17 16:26:31 PDT 2009
//
// Modifications:
//   
// ****************************************************************************

void
AddUniquePluginDir(stringVector &pluginDirs, const std::string &path)
{
    if(std::find(pluginDirs.begin(), pluginDirs.end(), path) == pluginDirs.end())
        pluginDirs.push_back(path);
}

hrchilds's avatar
hrchilds committed
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
// ****************************************************************************
// Method: PluginManager::SetPluginDir
//
// Purpose: 
//   Sets the plugin directory.
//
// Programmer: Brad Whitlock
// Creation:   Fri Mar 29 13:50:50 PST 2002
//
// Modifications:
//    Jeremy Meredith, Fri Jul  5 17:36:23 PDT 2002
//    Made it work on plugins from multiple directories.
//   
//    Brad Whitlock, Wed Jul 24 12:27:04 PDT 2002
//    I made it only use a single plugin directory on Windows since the
//    parsing algorithm for UNIX directories is inadequate for Windows
//    directory names.
//
hrchilds's avatar
hrchilds committed
1298
1299
1300
//    Brad Whitlock, Wed Nov 22 16:25:19 PST 2006
//    I made it possible to pass in the plugin directory. If it does not
//    get passed in (the default) then we use VISITPLUGINDIR.
1301
1302
1303
1304
//    
//    Sean Ahern, Thu Jan 17 15:52:42 EST 2008
//    Only fail the environment exception if the plugin directory hasn't been
//    set in a prior call to SetPluginDir.
hrchilds's avatar
hrchilds committed
1305
//
1306
1307
1308
1309
//    Kathleen Bonnell, Wed May 21 08:12:16 PDT 2008 
//    Modified path-parsing for Windows.  ';' is the only valid separator
//    between paths since ':' could indicate a drive.
//
whitlocb's avatar
whitlocb committed
1310
1311
1312
//    Brad Whitlock, Wed Jun 17 16:28:14 PDT 2009
//    I made it use AddUniquePluginDir so we don't add a path multiple times.
//
hrchilds's avatar
hrchilds committed
1313
1314
1315
// ****************************************************************************

void
hrchilds's avatar
hrchilds committed
1316
PluginManager::SetPluginDir(const char *PluginDir)
hrchilds's avatar
hrchilds committed
1317
{
hrchilds's avatar
hrchilds committed
1318
    const char *plugindir = 0;
1319
    if (PluginDir == 0)
hrchilds's avatar
hrchilds committed
1320
    {
1321
        // Get the plugin directory from the environment.
hrchilds's avatar
hrchilds committed
1322
1323
1324
        plugindir = getenv("VISITPLUGINDIR");
        if (!plugindir)
        {
1325
1326
1327
1328
            // No environment variable found.  If we have directories in the
            // list already, this isn't a problem.
            if (pluginDirs.empty())
            {
whitlocb's avatar
whitlocb committed
1329
                debug4 << "No environment variable!" << endl;
hrchilds's avatar
hrchilds committed
1330
#if defined(_WIN32)
whitlocb's avatar
whitlocb committed
1331
1332
1333
1334
1335
1336
                char *tmp = new char[MAX_PATH];
                if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL,
                                  SHGFP_TYPE_CURRENT, tmp)))
                {
                    PathAppend(tmp, "LLNL");
                    PathAppend(tmp, "VisIt");
whitlocb's avatar
whitlocb committed
1337
1338
                    AddUniquePluginDir(pluginDirs, string(tmp) + VISIT_SLASH_STRING + 
                                       managerName + "s");
whitlocb's avatar
whitlocb committed
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
                    delete [] tmp;
                    return;
                }
                else
                {
                    delete [] tmp;
                    EXCEPTION1(VisItException,
                        "The path to AppData variable could not be found.");
                }
 #else
1349
1350
                EXCEPTION1(VisItException,
                    "The environment variable VISITPLUGINDIR must be defined.");
hrchilds's avatar
hrchilds committed
1351
#endif
1352
1353
1354
1355
1356
1357
1358
1359
            }
            else
            {
                // The pluginDirs list is already populated.  So not having an
                // environment variable is okay.  We got the list from somewhere
                // else (-plugindir option?).
                return;
            }
hrchilds's avatar
hrchilds committed
1360
        }
hrchilds's avatar
hrchilds committed
1361
    }
hrchilds's avatar
hrchilds committed
1362
    else
1363
1364
    {
        // Manually set the plugin directory.
hrchilds's avatar
hrchilds committed
1365
        plugindir = PluginDir;
1366
    }
hrchilds's avatar
hrchilds committed
1367
1368

    const char *c = plugindir;
hrchilds's avatar
hrchilds committed
1369
1370
1371
    while (*c)
    {
        string dir;
1372
#ifndef _WIN32
hrchilds's avatar
hrchilds committed
1373
        while (*c && *c!=':' && *c!=';')
1374
1375
1376
#else
        while (*c && *c!=';')
#endif
hrchilds's avatar
hrchilds committed
1377
1378
1379
1380
1381
1382
        {
            dir += *c;
            c++;
        }
        if (!dir.empty())
        {
whitlocb's avatar
whitlocb committed
1383
            AddUniquePluginDir(pluginDirs, string(dir) + VISIT_SLASH_STRING + managerName + "s");
hrchilds's avatar
hrchilds committed
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
        }
        dir = "";
        if (*c)
            c++;
    }
}

// ****************************************************************************
// Method: PluginManager::ReadPluginDir
//
// Purpose: 
//   Reads the files in the plugin directory.
//
// Arguments:
//   files : The return list of files.
//
// Programmer: Brad Whitlock
// Creation:   Fri Mar 29 09:32:57 PDT 2002
//
// Modifications:
//    Jeremy Meredith, Fri Jul  5 17:36:23 PDT 2002
//    Made it work on plugins from multiple directories.
//
//    Brad Whitlock, Mon Jul 15 16:41:59 PST 2002
//    I fixed the win32 implementation.
//
1410
1411
1412
//    Hank Childs, Fri Oct  5 13:08:55 PDT 2007
//    Changed argument to make separation of directories easier.
//
hrchilds's avatar
hrchilds committed
1413
1414
1415
// ****************************************************************************

void
1416
PluginManager::ReadPluginDir(vector< vector<pair<string,string> > > &files)
hrchilds's avatar
hrchilds committed
1417
{
1418
    files.resize(pluginDirs.size());
1419
    for (size_t i=0; i<pluginDirs.size(); i++)
hrchilds's avatar
hrchilds committed
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
    {
        string pluginDir(pluginDirs[i]);

#if defined(_WIN32)
        string searchPath(pluginDir + string("\\*"));
        WIN32_FIND_DATA fd;
        HANDLE dirHandle = FindFirstFile(searchPath.c_str(), &fd);
        if(dirHandle != INVALID_HANDLE_VALUE)
        {
            while(FindNextFile(dirHandle, &fd))
            {
bonnell's avatar
bonnell committed
1431
                files[i].push_back(pair<string,string>(pluginDir,fd.cFileName));
hrchilds's avatar
hrchilds committed
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
            }
            FindClose(dirHandle);
        }
#else
        // Open the directory
        DIR *dir = opendir(pluginDir.c_str());
        if (dir)
        {
            // Add each file in the directory to the list.
            dirent  *ent;
            while ((ent = readdir(dir)) != NULL)
1443
                files[i].push_back(pair<string,string>(pluginDir,ent->d_name));
hrchilds's avatar
hrchilds committed
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
            closedir(dir);
        }
        //else
        //    EXCEPTION1(InvalidDirectoryException, pluginDir.c_str());
#endif
    }
}

// ****************************************************************************
// Method: PluginManager::PluginOpen
//
// Purpose: 
//   Opens a plugin file and keeps a handle to it.
//
// Arguments:
//   pluginFile : The plugin filename.
//
// Programmer: Jeremy Meredith, Brad Whitlock
// Creation:   Fri Mar 29 11:58:42 PDT 2002
//
// Modifications:
hrchilds's avatar
hrchilds committed
1465
1466
1467
1468
//    Jeremy Meredith, Tue Feb  8 09:06:53 PST 2005
//    Told PluginSymbol not to complain if it could not find
//    the g++ initialization code.
//
hrchilds's avatar
hrchilds committed
1469
1470
1471
//    Hank Childs, Thu Jan 19 17:05:49 PST 2006
//    Print out plugin errors to the screen.  ['6629]
//
1472
1473
//    Mark C. Miller, Thu Aug 14 01:22:59 PDT 2008
//    Made messages reported to screen include plugin name and error msg.
hrchilds's avatar
hrchilds committed
1474
1475
1476
1477
1478
1479
1480
1481
1482
// ****************************************************************************

void
PluginManager::PluginOpen(const string &pluginFile)
{
#if defined(_WIN32)
    HINSTANCE lib = LoadLibrary(pluginFile.c_str());
    if(!lib)
    {
hrchilds's avatar
hrchilds committed
1483
        const char *pluginError = PluginError();
1484
1485
        cerr << "Error opening plugin file: " << pluginFile
             << " (" << pluginError << ")" << endl;
hrchilds's avatar
hrchilds committed
1486
        EXCEPTION3(InvalidPluginException, "Error opening plugin file",
hrchilds's avatar
hrchilds committed
1487
                   pluginFile.c_str(), pluginError);
hrchilds's avatar
hrchilds committed
1488
1489
1490
1491
1492
1493
1494
1495
    }

    handle = (void *)lib;
#else
    // dlopen the plugin
    handle = dlopen(pluginFile.c_str(), RTLD_LAZY);
    if (!handle)
    {
hrchilds's avatar
hrchilds committed
1496
        const char *pluginError = PluginError();
1497
1498
        cerr << "Error opening plugin file: " << pluginFile
             << " (" << pluginError << ")" << endl;
hrchilds's avatar
hrchilds committed
1499
        EXCEPTION3(InvalidPluginException, "Error opening plugin file",
hrchilds's avatar
hrchilds committed
1500
                   pluginFile.c_str(), pluginError);
hrchilds's avatar
hrchilds committed
1501
1502
1503
    }

    // Try to initialize static constructors.  This is a g++ism.
hrchilds's avatar
hrchilds committed
1504
    void (*init)(void) = (void(*)(void))PluginSymbol("_GLOBAL__DI", true);
hrchilds's avatar
hrchilds committed
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
    if (init)
        init();
#endif

    openPlugin = pluginFile;
}

// ****************************************************************************
// Method: PluginManager::PluginSymbol
//
// Purpose: 
//   Reads a symbol from a shared library and returns a pointer to it.
//
// Arguments:
//   symbol : The symbol to find.
//
// Programmer: Brad Whitlock
// Creation:   Fri Mar 29 12:03:46 PDT 2002
//
// Modifications:
hrchilds's avatar
hrchilds committed
1525
1526
1527
//   Brad Whitlock, Thu Aug 21 14:14:40 PST 2003
//   Added a special implementation for MacOS X.
//
hrchilds's avatar
hrchilds committed
1528
1529
1530
1531
1532
1533
//   Brad Whitlock, Wed Mar 10 09:36:16 PDT 2004
//   Added code to print the error message if the symbol could not be found.
//   I did this here because most functions that call PluginSymbol use the
//   return value to throw an exception without calling PluginError themselves.
//   That prevents useful information from being printed to the debug logs.
//
hrchilds's avatar
hrchilds committed
1534
1535
1536
//   Brad Whitlock, Tue Sep 14 13:27:35 PST 2004
//   I made the debug output go to debug5.
//
hrchilds's avatar
hrchilds committed
1537
1538
1539
1540
1541
1542
//   Brad Whitlock, Mon Mar 7 12:23:06 PDT 2005
//   I made the plugin version be handled a little differently on the Mac
//   because the names that were given to the plugin versions for the Mac
//   in all of the plugins don't have underscores in them and I don't want
//   to change them in all of the plugins. Someday we should though.
//
cyrush's avatar
cyrush committed
1543
1544
1545
//   Cyrus Harrison, Wed Jun 20 14:05:58 PDT 2007
//   Changed __DARWIN__ back to __APPLE__
//
hrchilds's avatar
hrchilds committed
1546
1547
1548
// ****************************************************************************

void *
hrchilds's avatar
hrchilds committed
1549
PluginManager::PluginSymbol(const string &symbol, bool noError)
hrchilds's avatar
hrchilds committed
1550
1551
1552
1553
{
    void *retval;
#if defined(_WIN32)
    retval = (void *)GetProcAddress((HMODULE)handle, symbol.c_str());
cyrush's avatar
cyrush committed
1554
#elif defined(__APPLE__)
hrchilds's avatar
hrchilds committed
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
    string symbolName(symbol);

    //
    // If the symbol that we want begins with "Get" then we're most likely
    // trying to access a plugin and in order to get plugins to work on MacOS X
    // in a flat namespace, I changed the name of the access function so that
    // it has the name of the plugin prepended to it in order to create a unique
    // function name. Here, we use the name of the plugin file to determine the
    // name of the plugin so we can create the symbol that we're really after.
    //
hrchilds's avatar
hrchilds committed
1565
1566
    bool pluginVersion = (symbol == "VisItPluginVersion");
    if(pluginVersion || symbol.substr(0,3) == "Get")
hrchilds's avatar
hrchilds committed
1567
    {
1568
        string ext(VISIT_PLUGIN_EXTENSION);
hrchilds's avatar
hrchilds committed
1569
1570
1571
1572
1573
1574
        int slashPos = openPlugin.rfind("/");
        int suffixLen = (openPlugin.find("_ser") != -1 ||
                         openPlugin.find("_par") != -1) ? 4 : 0;
        int len = openPlugin.size() - slashPos - suffixLen - 5 -
                  managerName.size() - ext.size();
        string pluginPrefix(openPlugin.substr(slashPos + 5, len));
whitlocb's avatar
whitlocb committed
1575
//        debug4 << "PluginSymbol: prefix: " << pluginPrefix << endl;
hrchilds's avatar
hrchilds committed
1576
1577
1578
1579
        if(pluginVersion)
            symbolName = string(pluginPrefix + symbol);
        else
            symbolName = string(pluginPrefix + "_" + symbol);
whitlocb's avatar
whitlocb committed
1580
//        debug4 << "PluginSymbol: sym: " << symbolName << endl;
hrchilds's avatar
hrchilds committed
1581
1582
1583
    }
    
    retval = dlsym(handle, symbolName.c_str());
hrchilds's avatar
hrchilds committed
1584
1585
1586
#else
    retval = dlsym(handle, symbol.c_str());
#endif
hrchilds's avatar
hrchilds committed
1587

hrchilds's avatar
hrchilds committed
1588
1589
    // If the symbol was not found, print the error message if appropriate.
    if (retval == 0 && !noError)
hrchilds's avatar
hrchilds committed
1590
        debug5 << PluginError() << endl;
hrchilds's avatar
hrchilds committed
1591

hrchilds's avatar
hrchilds committed
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
    return retval;
}

// ****************************************************************************
// Method: PluginManager::PluginError
//
// Purpose: 
//   Returns the last plugin error message.
//
// Returns:    The last plugin error message.
//
// Programmer: Brad Whitlock
// Creation:   Tue Apr 2 09:56:41 PDT 2002
//
// Modifications:
//   
// ****************************************************************************

char *
PluginManager::PluginError() const
{
#if defined(_WIN32)
    va_list *va = 0;
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), 0,
                  pluginError, MAX_PLUGINERROR, va);
#else
    strncpy(pluginError, dlerror(), MAX_PLUGINERROR);
#endif
    return pluginError;
}

// ****************************************************************************
// Method: PluginManager::PluginClose
//
// Purpose: 
//   Closes the open plugin.
//
// Programmer: Brad Whitlock
// Creation:   Fri Mar 29 10:00:07 PDT 2002
//
// Modifications:
//    Jeremy Meredith, Tue Mar 18 13:24:05 PST 2003
//    Added destruction of static objects.
//
hrchilds's avatar
hrchilds committed
1636
1637
1638
1639
//    Jeremy Meredith, Tue Feb  8 09:06:53 PST 2005
//    Told PluginSymbol not to complain if it could not find
//    the g++ destruction code.
//
hrchilds's avatar
hrchilds committed
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
// ****************************************************************************

void
PluginManager::PluginClose()
{
#if defined(_WIN32)
    if(handle)
    {
        FreeLibrary((HMODULE)handle);
        handle = 0;
    }
#else
hrchilds's avatar
hrchilds committed
1652
    // Try to destruct static objects.  This is a g++ism.
hrchilds's avatar
hrchilds committed
1653
    void (*fini)(void) = (void(*)(void))PluginSymbol("_GLOBAL__DD", true);
hrchilds's avatar
hrchilds committed
1654
1655
1656
    if (fini)
        fini();
        
hrchilds's avatar
hrchilds committed
1657
1658
    if(handle)
    {
1659
1660
1661
1662
1663
1664
        if (dlclose(handle) != 0)
        {
            const char *pluginError = PluginError();
            cerr << "Error closing plugin file: " << openPlugin 
                 << " (" << pluginError << ")" << endl;
        }
hrchilds's avatar
hrchilds committed
1665
1666
1667
        handle = 0;
    }
#endif
1668
1669

    openPlugin = "";
hrchilds's avatar
hrchilds committed
1670
}