~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to plug-ins/script-fu/re/re.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2008-10-06 13:30:41 UTC
  • mto: This revision was merged to the branch mainline in revision 35.
  • Revision ID: james.westby@ubuntu.com-20081006133041-3panbkcanaymfsmp
Tags: upstream-2.6.0
ImportĀ upstreamĀ versionĀ 2.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* re.c */
2
 
/* Henry Spencer's implementation of Regular Expressions,
3
 
   used for TinyScheme */
4
 
/* Refurbished by Stephen Gildea */
5
 
#include "regex.h"
6
 
#include "tinyscheme/scheme-private.h"
7
 
 
8
 
#if defined(_WIN32)
9
 
#define EXPORT __declspec( dllexport )
10
 
#else
11
 
#define EXPORT
12
 
#endif
13
 
 
14
 
/* Since not exported */
15
 
#define T_STRING 1
16
 
 
17
 
pointer     foreign_re_match(scheme *sc, pointer args);
18
 
EXPORT void init_re(scheme *sc);
19
 
 
20
 
 
21
 
static void set_vector_elem(pointer vec, int ielem, pointer newel) {
22
 
 int n=ielem/2;
23
 
 if(ielem%2==0) {
24
 
     vec[1+n]._object._cons._car=newel;
25
 
 } else {
26
 
     vec[1+n]._object._cons._cdr=newel;
27
 
 }
28
 
}
29
 
 
30
 
pointer foreign_re_match(scheme *sc, pointer args) {
31
 
  pointer retval=sc->F;
32
 
  int retcode;
33
 
  regex_t rt;
34
 
  pointer first_arg, second_arg;
35
 
  pointer third_arg=sc->NIL;
36
 
  char *string;
37
 
  char *pattern;
38
 
  int num=0;
39
 
 
40
 
  if(!((args != sc->NIL) && sc->vptr->is_string((first_arg = sc->vptr->pair_car(args)))
41
 
       && (args=sc->vptr->pair_cdr(args))
42
 
       && sc->vptr->is_pair(args) && sc->vptr->is_string((second_arg = sc->vptr->pair_car(args))))) {
43
 
    return sc->F;
44
 
  }
45
 
  pattern = sc->vptr->string_value(first_arg);
46
 
  string = sc->vptr->string_value(second_arg);
47
 
 
48
 
  args=sc->vptr->pair_cdr(args);
49
 
  if(args!=sc->NIL) {
50
 
    if(!(sc->vptr->is_pair(args) && sc->vptr->is_vector((third_arg = sc->vptr->pair_car(args))))) {
51
 
      return sc->F;
52
 
    } else {
53
 
      num=third_arg->_object._number.value.ivalue;
54
 
    }
55
 
  }
56
 
 
57
 
 
58
 
  if(regcomp(&rt,pattern,REG_EXTENDED)!=0) {
59
 
    return sc->F;
60
 
  }
61
 
 
62
 
  if(num==0) {
63
 
    retcode=regexec(&rt,string,0,0,0);
64
 
  } else {
65
 
    regmatch_t *pmatch=malloc((num+1)*sizeof(regmatch_t));
66
 
    if(pmatch!=0) {
67
 
      retcode=regexec(&rt,string,num+1,pmatch,0);
68
 
      if(retcode==0) {
69
 
       int i;
70
 
       for(i=0; i<num; i++) {
71
 
#undef cons
72
 
         set_vector_elem(third_arg, i,
73
 
                         sc->vptr->cons(sc, sc->vptr->mk_integer(sc, pmatch[i].rm_so),
74
 
                                             sc->vptr->mk_integer(sc, pmatch[i].rm_eo)));
75
 
        }
76
 
      }
77
 
      free(pmatch);
78
 
    } else {
79
 
      sc->no_memory=1;
80
 
      retcode=-1;
81
 
    }
82
 
  }
83
 
 
84
 
  if(retcode==0) {
85
 
    retval=sc->T;
86
 
  }
87
 
 
88
 
  regfree(&rt);
89
 
 
90
 
  return(retval);
91
 
}
92
 
 
93
 
#if 0
94
 
static char* utilities=";; return the substring of STRING matched in MATCH-VECTOR, \n"
95
 
";; the Nth subexpression match (default 0).\n"
96
 
"(define (re-match-nth string match-vector . n)\n"
97
 
"  (let ((n (if (pair? n) (car n) 0)))\n"
98
 
"    (substring string (car (vector-ref match-vector n))\n"
99
 
"                    (cdr (vector-ref match-vector n)))))\n"
100
 
"(define (re-before-nth string match-vector . n)\n"
101
 
"  (let ((n (if (pair? n) (car n) 0)))\n"
102
 
"    (substring string 0 (car (vector-ref match-vector n)))))\n"
103
 
"(define (re-after-nth string match-vector . n)\n"
104
 
"  (let ((n (if (pair? n) (car n) 0)))\n"
105
 
"    (substring string (cdr (vector-ref match-vector n))\n"
106
 
"             (string-length string))))\n";
107
 
#endif
108
 
 
109
 
void init_re(scheme *sc) {
110
 
  sc->vptr->scheme_define(sc,sc->global_env,sc->vptr->mk_symbol(sc,"re-match"),sc->vptr->mk_foreign_func(sc, foreign_re_match));
111
 
  /*    sc->vptr->load_string(sc,utilities);*/
112
 
}