File: new_filter.cpp

package info (click to toggle)
aspell 0.60.6-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 10,000 kB
  • ctags: 4,862
  • sloc: sh: 48,145; cpp: 22,153; perl: 1,546; ansic: 1,535; makefile: 684; sed: 16
file content (540 lines) | stat: -rw-r--r-- 14,850 bytes parent folder | download | duplicates (9)
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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// This file is part of The New Aspell
// Copyright (C) 2002 by Kevin Atkinson and Christoph Hintermller
// under the GNU LGPL license version 2.0 or 2.1.  You should have
// received a copy of the LGPL license along with this library if you
// did not you can find it at http://www.gnu.org/.
//
// Expansion for loading filter libraries and collections upon startup
// was added by Christoph Hintermller

#include "settings.h"

#include "cache-t.hpp"
#include "asc_ctype.hpp"
#include "config.hpp"
#include "enumeration.hpp"
#include "errors.hpp"
#include "filter.hpp"
#include "filter_entry.hpp"
#include "fstream.hpp"
#include "getdata.hpp"
#include "indiv_filter.hpp"
#include "iostream.hpp"
#include "itemize.hpp"
#include "key_info.hpp"
#include "parm_string.hpp"
#include "posib_err.hpp"
#include "stack_ptr.hpp"
#include "string_enumeration.hpp"
#include "string_list.hpp"
#include "string_map.hpp"
#include "strtonum.hpp"
#include "file_util.hpp"

#include <stdio.h>

#ifdef HAVE_LIBDL
#  include <dlfcn.h>
#endif

namespace acommon
{
#include "static_filters.src.cpp"

  //////////////////////////////////////////////////////////////////////////
  //
  // setup static filters
  //

  PosibErr<const ConfigModule *> get_dynamic_filter(Config * config, ParmStr value);
  extern void activate_filter_modes(Config *config);

  void setup_static_filters(Config * config)
  {
    config->set_filter_modules(filter_modules_begin, filter_modules_end);
    activate_filter_modes(config);
#ifdef HAVE_LIBDL
    config->load_filter_hook = get_dynamic_filter;
#endif
  }

  //////////////////////////////////////////////////////////////////////////
  //
  // 
  //

#ifdef HAVE_LIBDL

  struct ConfigFilterModule : public Cacheable {
    String name; 
    String file; // path of shared object or dll
    String desc; // description of module
    Vector<KeyInfo> options;
    typedef Config CacheConfig;
    typedef String CacheKey;
    static PosibErr<ConfigFilterModule *> get_new(const String & key, const Config *);
    bool cache_key_eq(const String & okey) const {
      return name == okey;
    }
    ConfigFilterModule() : in_option(0) {}
    ~ConfigFilterModule();
    bool in_option;
    KeyInfo * new_option() {
      options.push_back(KeyInfo()); 
      in_option = true; 
      return &options.back();}
    PosibErr<void> end_option();
  };

  static GlobalCache<ConfigFilterModule> filter_module_cache("filters");

  ConfigFilterModule::~ConfigFilterModule()
  {
    for (Vector<KeyInfo>::iterator i = options.begin();
         i != options.end();
         ++i)
    {
      free(const_cast<char *>(i->name));
      free(const_cast<char *>(i->def));
      free(const_cast<char *>(i->desc));
    }
  }

#endif

  class IndividualFilter;

  //
  // actual code
  //

  FilterEntry * get_standard_filter(ParmStr);

  //////////////////////////////////////////////////////////////////////////
  //
  // setup filter
  //

  PosibErr<void> setup_filter(Filter & filter, 
			      Config * config, 
			      bool use_decoder, bool use_filter, bool use_encoder)
  {
    StringList sl;
    config->retrieve_list("filter", &sl);
    StringListEnumeration els = sl.elements_obj();
    const char * filter_name;
    String fun;

    StackPtr<IndividualFilter> ifilter;

    filter.clear();

    while ((filter_name = els.next()) != 0) {
      //fprintf(stderr, "Loading %s ... \n", filter_name);
      FilterEntry * f = get_standard_filter(filter_name);
      // In case libdl is not available a filter is only available if made
      // one of the standard filters. This is done by statically linking
      // the filter sources.
      // On systems providing libdl or in case libtool mimics libdl 
      // The following code parts assure that all filters needed and requested
      // by user are loaded properly or be reported to be missing.
      // 
      FilterHandle decoder_handle, filter_handle, encoder_handle;
#ifdef HAVE_LIBDL
      FilterEntry dynamic_filter;
      if (!f) {

        RET_ON_ERR_SET(get_dynamic_filter(config, filter_name),
                       const ConfigModule *, module);

        if (!(decoder_handle = dlopen(module->file,RTLD_NOW)) ||
            !(encoder_handle = dlopen(module->file,RTLD_NOW)) ||
            !(filter_handle  = dlopen(module->file,RTLD_NOW)))
          return make_err(cant_dlopen_file,dlerror()).with_file(filter_name);

        fun = "new_aspell_";
        fun += filter_name;
        fun += "_decoder";
        dynamic_filter.decoder = (FilterFun *)dlsym(decoder_handle.get(), fun.str());

        fun = "new_aspell_";
        fun += filter_name;
        fun += "_encoder";
        dynamic_filter.encoder = (FilterFun *)dlsym(encoder_handle.get(), fun.str());

        fun = "new_aspell_";
        fun += filter_name;
        fun += "_filter";
        dynamic_filter.filter = (FilterFun *)dlsym(filter_handle.get(), fun.str());

        if (!dynamic_filter.decoder && 
	    !dynamic_filter.encoder &&
	    !dynamic_filter.filter)
          return make_err(empty_filter,filter_name);
        dynamic_filter.name = filter_name;
        f = &dynamic_filter;
      } 
#else
      if (!f)
        return make_err(no_such_filter, filter_name);
#endif
      if (use_decoder && f->decoder && (ifilter = f->decoder())) {
        RET_ON_ERR_SET(ifilter->setup(config), bool, keep);
        ifilter->handle = decoder_handle.release();
	if (!keep) {
	  ifilter.del();
	} else {
          filter.add_filter(ifilter.release());
        }
      } 
      if (use_filter && f->filter && (ifilter = f->filter())) {
        RET_ON_ERR_SET(ifilter->setup(config), bool, keep);
        ifilter->handle = filter_handle.release();
        if (!keep) {
          ifilter.del();
        } else {
          filter.add_filter(ifilter.release());
        }
      }
      if (use_encoder && f->encoder && (ifilter = f->encoder())) {
        RET_ON_ERR_SET(ifilter->setup(config), bool, keep);
        ifilter->handle = encoder_handle.release();
        if (!keep) {
          ifilter.del();
        } else {
          filter.add_filter(ifilter.release());
        }
      }
    }
    return no_err;
  }

  //////////////////////////////////////////////////////////////////////////
  //
  // get filter
  //

  FilterEntry * get_standard_filter(ParmStr filter_name) {
    unsigned int i = 0;
    while (i != standard_filters_size) {
      if (standard_filters[i].name == filter_name) {
	return (FilterEntry *) standard_filters + i;
      }
      ++i;
    }
    return 0;
  }

#ifdef HAVE_LIBDL

  PosibErr<const ConfigModule *> get_dynamic_filter(Config * config, ParmStr filter_name) 
  {
    for (const ConfigModule * cur = config->filter_modules.pbegin();
         cur != config->filter_modules.pend();
         ++cur)
    {
      if (strcmp(cur->name,filter_name) == 0) return cur;
    }

    RET_ON_ERR_SET(get_cache_data(&filter_module_cache, config, filter_name), 
                   ConfigFilterModule *, module);

    ConfigModule m = {
      module->name.str(), module->file.str(), module->desc.str(),
      module->options.pbegin(), module->options.pend()
    };

    config->filter_modules_ptrs.push_back(module);
    config->filter_modules.push_back(m);

    return &config->filter_modules.back();
  }

  PosibErr<ConfigFilterModule *> ConfigFilterModule::get_new(const String & filter_name, 
                                                             const Config * config)
  {
    StackPtr<ConfigFilterModule> module(new ConfigFilterModule);
    module->name = filter_name;
    KeyInfo * cur_opt = NULL;

    String option_file = filter_name;
    option_file += "-filter.info";
    if (!find_file(config, "filter-path", option_file))
      return make_err(no_such_filter, filter_name);

    FStream options;
    RET_ON_ERR(options.open(option_file,"r"));

    String buf; DataPair d;
    while (getdata_pair(options,d,buf))
    {
      to_lower(d.key);

      //
      // key == aspell
      //
      if (d.key == "aspell") 
      {
        if ( d.value == NULL || *d.value == '\0' )
          return make_err(confusing_version).with_file(option_file,d.line_num);
#ifdef FILTER_VERSION_CONTROL 
       PosibErr<void> peb = check_version(d.value.str);
        if (peb.has_err()) return peb.with_file(option_file,d.line_num);
#endif
        continue;
      }
	  
      //
      // key == option
      //
      if (d.key == "option" ) {

        RET_ON_ERR(module->end_option());

        to_lower(d.value.str);

        cur_opt = module->new_option();
        
        char * s = (char *)malloc(2 + filter_name.size() + 1 + d.value.size + 1);
        cur_opt->name = s;
        memcpy(s, "f-", 2); 
        s+= 2;
        memcpy(s, filter_name.str(), filter_name.size());
        s += filter_name.size();
        *s++ = '-';
        memcpy(s, d.value.str, d.value.size);
        s += d.value.size;
        *s = '\0';
        for (Vector<KeyInfo>::iterator cur = module->options.begin();
             cur != module->options.end() - 1; // avoid checking the one just inserted
             ++cur) {
          if (strcmp(cur_opt->name,cur->name) == 0)
            return make_err(identical_option).with_file(option_file,d.line_num);
        }

        continue;
      }

      //
      // key == static
      //
      if (d.key == "static") {
        RET_ON_ERR(module->end_option());
        continue;
      }

      //
      // key == description
      //
      if ((d.key == "desc") ||
          (d.key == "description")) {

        unescape(d.value);

        //
        // filter description
        // 
        if (!module->in_option) {
          module->desc = d.value;
        } 
        //
        //option description
        //
        else {
          //avoid memory leak;
          if (cur_opt->desc) free((char *)cur_opt->desc);
          cur_opt->desc = strdup(d.value.str);
        }
        continue;
      }

      //
      // key == lib-file
      //
      if (d.key == "lib-file")
      {
        module->file = d.value;
        continue;
      }
	  
      //
      // !active_option
      //
      if (!module->in_option) {
        return make_err(options_only).with_file(option_file,d.line_num);
      }

      //
      // key == type
      //
      if (d.key == "type") {
        to_lower(d.value); // This is safe since normally option_value is used
        if (d.value == "list") 
          cur_opt->type = KeyInfoList;
        else if (d.value == "int" || d.value == "integer") 
          cur_opt->type = KeyInfoInt;
        else if (d.value == "string")
          cur_opt->type = KeyInfoString;
        //FIXME why not force user to ommit type specifier or explicitly say bool ???
        else
          cur_opt->type = KeyInfoBool;
        continue;
      }

      //
      // key == default
      //
      if (d.key == "def" || d.key == "default") {
            
        if (cur_opt->type == KeyInfoList) {

          int new_len = 0;
          int orig_len = 0;
          if (cur_opt->def) {
            orig_len = strlen(cur_opt->def);
            new_len += orig_len + 1;
          }
          for (const char * s = d.value.str; *s; ++s) {
            if (*s == ':') ++new_len;
            ++new_len;
          }
          new_len += 1;
          char * x = (char *)realloc((char *)cur_opt->def, new_len);
          cur_opt->def = x;
          if (orig_len > 0) {
            x += orig_len;
            *x++ = ':';
          }
          for (const char * s = d.value.str; *s; ++s) {
            if (*s == ':') *x++ = ':';
            *x++ = *s;
          }
          *x = '\0';

        } else {

          // FIXME
          //may try some syntax checking
          //if ( cur_opt->type == KeyInfoBool ) {
          //  check for valid bool values true false 0 1 on off ...
          //  and issue error if wrong or assume false ??
          //}
          //if ( cur_opt->type == KeyInfoInt ) {
          //  check for valid integer or double and issue error if not
          //}
          unescape(d.value);
          cur_opt->def = strdup(d.value.str);
        }
        continue;
      }
          
      //
      // key == flags
      //
      if (d.key == "flags") {
        if (d.value == "utf-8" || d.value == "UTF-8")
          cur_opt->flags = KEYINFO_UTF8;
        continue;
      }
           
      //
      // key == endoption
      //
      if (d.key=="endoption") {
        RET_ON_ERR(module->end_option());
        continue;
      }

      // 
      // error
      // 
      return make_err(invalid_option_modifier).with_file(option_file,d.line_num);
        
    } // end while getdata_pair_c
    RET_ON_ERR(module->end_option());
    const char * slash = strrchr(option_file.str(), '/');
    assert(slash);
    if (module->file.empty()) {
      module->file.assign(option_file.str(), slash + 1 - option_file.str());
      //module->file += "lib";
      module->file += filter_name;
      module->file += "-filter.so";
    } else {
      if (module->file[0] != '/')
        module->file.insert(0, option_file.str(), slash + 1 - option_file.str());
      module->file += ".so";
    }

    return module.release();
  }

  PosibErr<void>  ConfigFilterModule::end_option()
  {
    if (in_option) 
    {
      // FIXME: Check to make sure there is a name and desc.
      KeyInfo * cur_opt = &options.back();
      if (!cur_opt->def) cur_opt->def = strdup("");
    }
    in_option = false;
    return no_err;
  }

#endif

  void load_all_filters(Config * config) {
#ifdef HAVE_LIBDL
    StringList filter_path;
    String toload;
    
    config->retrieve_list("filter-path", &filter_path);
    PathBrowser els(filter_path, "-filter.info");
    
    const char * file;
    while ((file = els.next()) != NULL) {
      
      const char * name = strrchr(file, '/');
      if (!name) name = file;
      else name++;
      unsigned len = strlen(name) - 12;
      
      toload.assign(name, len);
      
      get_dynamic_filter(config, toload);
    }
#endif
  }


  class FiltersEnumeration : public StringPairEnumeration
  {
  public:
    typedef Vector<ConfigModule>::const_iterator Itr;
  private:
    Itr it;
    Itr end;
  public:
    FiltersEnumeration(Itr i, Itr e) : it(i), end(e) {}
    bool at_end() const {return it == end;}
    StringPair next()
    {
      if (it == end) return StringPair();
      StringPair res = StringPair(it->name, it->desc);
      ++it;
      return res;
    }
    StringPairEnumeration * clone() const {return new FiltersEnumeration(*this);}
    void assign(const StringPairEnumeration * other0)
    {
      const FiltersEnumeration * other = (const FiltersEnumeration *)other0;
      *this = *other;
    }
  };

  PosibErr<StringPairEnumeration *> available_filters(Config * config)
  {
    return new FiltersEnumeration(config->filter_modules.begin(),
                                  config->filter_modules.end());
  }

}