~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to plug-ins/script-fu/siod/regex.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "config.h"
 
2
 
 
3
#include <sys/types.h>
 
4
#include <stdio.h>
 
5
#include <stdlib.h>
 
6
#include <string.h>
 
7
 
 
8
#ifndef HAVE_GLIBC_REGEX
 
9
#include "regexrepl/regex.h"
 
10
#else
 
11
#include <regex.h>
 
12
#endif
 
13
 
 
14
#include "siod.h"
 
15
 
 
16
/* OSF/1 doc says that POSIX and XPG4 include regcomp in libc.
 
17
   So we might as well set ourselves up to take advantage of it.
 
18
   This functionality is also available in hpux, and is also provided
 
19
   by the FSF's librx package, so if you can use that if your
 
20
   operating system vendor doesn't supply it.
 
21
 */
 
22
 
 
23
static void
 
24
init_regex_version (void)
 
25
{
 
26
  setvar (cintern ("*regex-version*"),
 
27
          cintern ("$Id: regex.c,v 1.12 2003/01/05 20:51:39 yosh Exp $"),
 
28
          NIL);
 
29
}
 
30
 
 
31
long tc_regex = 0;
 
32
 
 
33
struct tc_regex
 
34
  {
 
35
    int compflag;
 
36
    size_t nmatch;
 
37
    regex_t *r;
 
38
    regmatch_t *m;
 
39
  };
 
40
 
 
41
struct tc_regex *
 
42
get_tc_regex (LISP ptr)
 
43
{
 
44
  if NTYPEP
 
45
    (ptr, tc_regex) my_err ("not a regular expression", ptr);
 
46
  return ((struct tc_regex *) ptr->storage_as.string.data);
 
47
}
 
48
 
 
49
LISP
 
50
regcomp_l (LISP pattern, LISP flags)
 
51
{
 
52
  long iflag, iflags;
 
53
  char *str, errbuff[1024];
 
54
  int error;
 
55
  LISP result;
 
56
  struct tc_regex *h;
 
57
  iflags = NNULLP (flags) ? get_c_long (flags) : 0;
 
58
  str = get_c_string (pattern);
 
59
  iflag = no_interrupt (1);
 
60
  result = cons (NIL, NIL);
 
61
  h = (struct tc_regex *) must_malloc (sizeof (struct tc_regex));
 
62
  h->compflag = 0;
 
63
  h->nmatch = 0;
 
64
  h->r = NULL;
 
65
  h->m = NULL;
 
66
  result->type = tc_regex;
 
67
  result->storage_as.string.data = (char *) h;
 
68
  h->r = (regex_t *) must_malloc (sizeof (regex_t));
 
69
  if ((error = regcomp (h->r, str, iflags)))
 
70
    {
 
71
      regerror (error, h->r, errbuff, sizeof (errbuff));
 
72
      return (my_err (errbuff, pattern));
 
73
    }
 
74
  h->compflag = 1;
 
75
  if (iflags & REG_NOSUB)
 
76
    {
 
77
      no_interrupt (iflag);
 
78
      return (result);
 
79
    }
 
80
  h->nmatch = h->r->re_nsub + 1;
 
81
  h->m = (regmatch_t *) must_malloc (sizeof (regmatch_t) * h->nmatch);
 
82
  no_interrupt (iflag);
 
83
  return (result);
 
84
}
 
85
 
 
86
LISP
 
87
regerror_l (LISP code, LISP ptr)
 
88
{
 
89
  char errbuff[1024];
 
90
  regerror (get_c_long (code), get_tc_regex (ptr)->r, errbuff, sizeof (errbuff));
 
91
  return (strcons (strlen (errbuff), errbuff));
 
92
}
 
93
 
 
94
LISP
 
95
regexec_l (LISP ptr, LISP str, LISP eflags)
 
96
{
 
97
  size_t j;
 
98
  int error;
 
99
  LISP result;
 
100
  struct tc_regex *h;
 
101
  h = get_tc_regex (ptr);
 
102
  if ((error = regexec (h->r,
 
103
                       get_c_string (str),
 
104
                       h->nmatch,
 
105
                       h->m,
 
106
                       NNULLP (eflags) ? get_c_long (eflags) : 0)))
 
107
    return (flocons (error));
 
108
  for (j = 0, result = NIL; j < h->nmatch; ++j)
 
109
    result = cons (cons (flocons (h->m[j].rm_so),
 
110
                         flocons (h->m[j].rm_eo)),
 
111
                   result);
 
112
  return (nreverse (result));
 
113
}
 
114
 
 
115
void
 
116
regex_gc_free (LISP ptr)
 
117
{
 
118
  struct tc_regex *h;
 
119
  if ((h = (struct tc_regex *) ptr->storage_as.string.data))
 
120
    {
 
121
      if ((h->compflag) && h->r)
 
122
        regfree (h->r);
 
123
      if (h->r)
 
124
        {
 
125
          free (h->r);
 
126
          h->r = NULL;
 
127
        }
 
128
      if (h->m)
 
129
        {
 
130
          free (h->m);
 
131
          h->m = NULL;
 
132
        }
 
133
      free (h);
 
134
      ptr->storage_as.string.data = NULL;
 
135
    }
 
136
}
 
137
 
 
138
void
 
139
regex_prin1 (LISP ptr, struct gen_printio *f)
 
140
{
 
141
  char buffer[256];
 
142
  regex_t *p;
 
143
  p = get_tc_regex (ptr)->r;
 
144
  sprintf (buffer, "#<REGEX %p nsub=%d",
 
145
           p, (int) p->re_nsub);
 
146
  gput_st (f, buffer);
 
147
  gput_st (f, ">");
 
148
}
 
149
 
 
150
void
 
151
init_regex (void)
 
152
{
 
153
  long j;
 
154
  tc_regex = allocate_user_tc ();
 
155
  set_gc_hooks (tc_regex,
 
156
                NULL,
 
157
                NULL,
 
158
                NULL,
 
159
                regex_gc_free,
 
160
                &j);
 
161
  set_print_hooks (tc_regex, regex_prin1);
 
162
  init_subr_2 ("regcomp", regcomp_l);
 
163
  init_subr_2 ("regerror", regerror_l);
 
164
  init_subr_3 ("regexec", regexec_l);
 
165
  setvar (cintern ("REG_EXTENDED"), flocons (REG_EXTENDED), NIL);
 
166
  setvar (cintern ("REG_ICASE"), flocons (REG_ICASE), NIL);
 
167
  setvar (cintern ("REG_NOSUB"), flocons (REG_NOSUB), NIL);
 
168
  setvar (cintern ("REG_NEWLINE"), flocons (REG_NEWLINE), NIL);
 
169
 
 
170
  setvar (cintern ("REG_NOTBOL"), flocons (REG_NOTBOL), NIL);
 
171
  setvar (cintern ("REG_NOTEOL"), flocons (REG_NOTEOL), NIL);
 
172
 
 
173
  setvar (cintern ("REG_NOMATCH"), flocons (REG_NOMATCH), NIL);
 
174
  setvar (cintern ("REG_BADPAT"), flocons (REG_BADPAT), NIL);
 
175
  setvar (cintern ("REG_ECOLLATE"), flocons (REG_ECOLLATE), NIL);
 
176
  setvar (cintern ("REG_ECTYPE"), flocons (REG_ECTYPE), NIL);
 
177
  setvar (cintern ("REG_EESCAPE"), flocons (REG_EESCAPE), NIL);
 
178
  setvar (cintern ("REG_ESUBREG"), flocons (REG_ESUBREG), NIL);
 
179
  setvar (cintern ("REG_EBRACK"), flocons (REG_EBRACK), NIL);
 
180
  setvar (cintern ("REG_EPAREN"), flocons (REG_EPAREN), NIL);
 
181
  setvar (cintern ("REG_EBRACE"), flocons (REG_EBRACE), NIL);
 
182
  setvar (cintern ("REG_BADBR"), flocons (REG_BADBR), NIL);
 
183
  setvar (cintern ("REG_ERANGE"), flocons (REG_ERANGE), NIL);
 
184
  setvar (cintern ("REG_ESPACE"), flocons (REG_ESPACE), NIL);
 
185
  setvar (cintern ("REG_BADRPT"), flocons (REG_BADRPT), NIL);
 
186
#ifdef REG_ECHAR
 
187
  setvar (cintern ("REG_ECHAR"), flocons (REG_ECHAR), NIL);
 
188
#endif
 
189
  init_regex_version ();
 
190
}