File: matcher.c

package info (click to toggle)
grep-dctrl 1.3a
  • links: PTS
  • area: main
  • in suites: potato
  • size: 660 kB
  • ctags: 403
  • sloc: ansic: 4,004; sh: 358; makefile: 226; sed: 93
file content (343 lines) | stat: -rw-r--r-- 8,059 bytes parent folder | download
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

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "buffer.h"
#include "matcher.h"
#include "msg.h"
#include "strutil.h"

static void
str_tolower (char * target, const char * source)
{
  size_t i;

  for (i = 0; i == 0 || source [i-1] != 0; i++)
    {
      unsigned char c = source [i];

      c = isupper (c) ? tolower (c) : c;
      target [i] = c;
    }
}

/* strstr ignoring case */
static char *
strcasestr (const char * haystack, const char * needle)
     /* This implemetation is suboptimal.  Ugh. */
{
  char * lower_hs;  /* haystack in lower case */
  char * lower_ne;  /* needle in lower case */
  char * rv;

  debug_message ("strcasestr", 0);

  lower_hs = malloc (strlen (haystack) + 1);
  lower_ne = malloc (strlen (needle) + 1);
  if (lower_hs == 0 || lower_ne == 0)
    {
      enomem (0);
      return 0;
    }

  str_tolower (lower_hs, haystack);
  str_tolower (lower_ne, needle);

  rv = strstr (lower_hs, lower_ne);
  if (rv != 0)
    rv = (char *) ( (rv - lower_hs) + haystack);

  free (lower_hs);
  free (lower_ne);

  return rv;
}

/* Copy the body of the field "field" from "s" to "body". */
static void
find_field (buffer body, const char * field, const char *s, const char * fname)
{
  const char * p;

  p = strcasestr (s, field);
  while (p != 0 && !((p == s || *(p-1) == '\n')
                     && (p [strlen (field)] == ':')))
    p = strcasestr (p + 1, field);

  if (p == 0)
    return;

  /* Skip whitespace at start of the field body. */
  for (p += strlen (field) + 1; *p == ' ' || *p == '\t'; p++);

  for (/* p is inherited */;
       *p != 0 && ( (p [0] == '\n') ? (p [1] == ' ' || p [1] == '\t') : 1);
       p++)
    {
      debug_message (_("find_field: copying a char"), fname);
      if (!buffer_append (body, *p))
        {
          buffer_free (body);
          fatal_enomem (fname);
        }          
    }
}


static void
show (const char * para, char * fieldv[],
      int show_fieldname, const char * fname)
{
  int i;
  buffer body;
  static int separatorp = 0;

  if (separatorp)
    puts ("");

  separatorp = fieldv [1] != 0;

  if (fieldv [0] == 0)
    {
      puts (para);
      return;
    }

  body = new_buffer ();
  if (body == buffer_nomem)
    fatal_enomem (fname);

  for (i = 0; fieldv [i] != 0; i++)
    {
      find_field (body, fieldv [i], para, fname);

      if (show_fieldname)
        printf ("%s: %s\n", fieldv[i], left_trimmed (buffer_c_str (body)));
      else
        printf ("%s\n", left_trimmed (buffer_c_str (body)));

      buffer_clear (body);
    }
  buffer_free (body);
}

char *
get_regerror (int errcode, regex_t *compiled)
{
  size_t length = regerror (errcode, compiled, NULL, 0);
  char * buffer = malloc (length);

  if (buffer == 0)
    return 0;

  (void) regerror (errcode, compiled, buffer, length);
  return buffer;
}

static int
grep_regex (regex_t * regex, const char * para, const char * fname)
{
  int regex_errcode;
  char * s;

  s = strdup (para);
  if (s == 0)
    {
      enomem (fname);
      return 0;
    }
  
  regex_errcode = regexec (regex, s, 0, 0, 0);
  free (s);
  if (regex_errcode == 0 || regex_errcode == REG_NOMATCH)
    return (regex_errcode == 0);
  
  /* Error handling be here. */
  assert (regex_errcode != 0 && regex_errcode != REG_NOMATCH);
  s = get_regerror (regex_errcode, regex);
  if (s == 0)
    {
      enomem (fname);
      return 0;
    }
  message (L_IMPORTANT, s, fname);
  free (s);
  return 0;
}

static int
grep_all (enum match_type_t type, int case_sensitive,  int exact,
          union pattern_t * pattern, const char * para,
          const char * fname)
{
  switch (type)
    {
    case MATCH_EXACT:
      {
        int (*matcher) (const char *, const char *);
        
        if (case_sensitive)
          matcher = strcmp;
        else
          matcher = strcasecmp;
        
        return matcher (para, pattern->fixed) == 0;
      }
      
    case MATCH_FIXED:
      {
        char * (*substring) (const char *, const char *);
        
        if (case_sensitive)
          substring = strstr;
        else
          substring = strcasestr;
        
        return substring (para, pattern->fixed) != 0;
      }

    case MATCH_REGEX:
      return grep_regex (pattern->regex, para, fname);
    }
  message (L_FATAL, _("I'm broken - please report this to <gaia@iki.fi>"), "matcher.c");
  abort ();
}

static int
grep (struct matcher_t * matcher, const char * para,
      const char * fname)
{
  buffer body;
  int rv;

  assert (matcher != 0);
  assert (para != 0);

  /* Handle case 1. */
  if (matcher->field == 0 || *matcher->field == 0)
    return grep_all (matcher->type, matcher->case_sensitive,
                     matcher->exact_match,
                     &(matcher->pattern), para, fname);

  /* Now handle case 2. */

  assert (matcher->field != 0);
  assert (strlen (matcher->field) > 0);

  body = new_buffer ();
  if (body == buffer_nomem)
    fatal_enomem (fname);

  debug_message (_("grep: finding field"), fname);  
  find_field (body, matcher->field, para, fname);
  
  /* Grep there. */
  rv = grep_all (matcher->type, matcher->case_sensitive, 
                 matcher->exact_match,                 
                 &(matcher->pattern), buffer_c_str (body),
                 fname);
  buffer_free (body);
  return rv;
}

/* Return true on success. */
static int
slurp_paragraph (buffer buf, FILE * f, const char * fname)
{
  int c;
  int eol = 0;

  while ( (c = getc (f)) != EOF)
    {
      debug_message (_("slurp_paragraph: read a character"), fname);
      if (c == '\n')
        {
          debug_message (_("slurp_paragraph: hit end of line"), fname);
          if (eol == 1)
            {
              debug_message (_("slurp_paragraph: end of paragraph"), fname);
              break;
            }
          else
            eol = 1;
        }
      else 
        if (eol == 1)
          {
            if (c == ' ' || c == '\t')
              debug_message (_("slurp_paragraph: got blank, looking for eol"),
                             fname);
            else
              {
                debug_message (_("slurp_paragraph: false alarm"), fname);
                eol = 0;
              }
          }
        
      if (!buffer_append (buf, c))
        {
          debug_message ( _("slurp_paragraph: buffer is full"), fname);
          buffer_free (buf);
          enomem (fname);
          return 0;
        }
    }
  debug_message (_("slurp_paragraph: out of loop"), fname);

  if (ferror (f))
    {
      message (L_INFORMATIONAL, _("slurp_paragraph: error reading file"), fname);
      message (L_IMPORTANT, strerror (errno), fname);
      return 0;
    }
  if (feof (f))
    debug_message (_("slurp_paragraph: hit end of file"), fname);

  return 1;
}

/* Return true if all went well. */
int
grep_control (struct matcher_t * matcher, FILE * f, char * show_fields[],
              const char * fname)
{
  buffer paragraph;

  message (L_INFORMATIONAL, _("grep_control: processing new file"), fname);
  debug_message (_("grep_control: allocating new buffer"), fname);
  paragraph = new_buffer ();
  if (paragraph == buffer_nomem)
    {
      enomem (fname);
      return 0;
    }

  while (1)
    {
      int grep_result;

      debug_message (_("grep_control: slurping a new paragraph"), fname);
      if (!slurp_paragraph (paragraph, f, fname))
        return 0;
      
      if (feof(f) && buffer_len(paragraph) == 0)
        return 1;

      debug_message (_("grep_control: grepping the paragraph"), fname);
      grep_result = grep (matcher, buffer_c_str (paragraph), fname);
      if (matcher->inverse ? !grep_result : grep_result)
        show (buffer_c_str (paragraph), show_fields,
              matcher->show_fieldnames, fname);
      
      debug_message (_("grep_control: trimming and clearing the buffer"),
                     fname);
      buffer_trim (paragraph);
      buffer_clear (paragraph);
    }

  message (L_INFORMATIONAL, _("grep_control: done with file"), fname);
  return 1;
}