File: rec-fex.c

package info (click to toggle)
recutils 1.7-1.1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 12,276 kB
  • ctags: 7,034
  • sloc: ansic: 64,860; sh: 18,180; lisp: 1,840; yacc: 1,381; makefile: 331; lex: 241; sed: 16
file content (854 lines) | stat: -rw-r--r-- 19,307 bytes parent folder | download | duplicates (3)
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
541
542
543
544
545
546
547
548
549
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
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
/* -*- mode: C -*-
 *
 *       File:         rec-fex.c
 *       Date:         Sun Apr 11 21:31:32 2010
 *
 *       GNU recutils - Field Expressions
 *
 */

/* Copyright (C) 2010, 2011, 2012 Jose E. Marchesi */

/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <stdlib.h>
#include <string.h>
#include <gettext.h>
#define _(str) dgettext (PACKAGE, str)

#include <rec-utils.h>
#include <rec.h>

/*
 * Data types.
 */

struct rec_fex_elem_s
{
  char *str;

  char *field_name;
  char *rewrite_to;
  int max;
  int min;

  char *function_name;
  void *function_data;
};

#define REC_FEX_MAX_ELEMS 256

struct rec_fex_s
{
  int num_elems;
  char *str;
  rec_fex_elem_t elems[REC_FEX_MAX_ELEMS];
};

/*
 * Static function declarations.
 */

static void rec_fex_init (rec_fex_t fex);

static bool rec_fex_parse_str_simple     (rec_fex_t new,       const char *str, const char *sep);
static bool rec_fex_parse_str_subscripts (rec_fex_t new,       const char *str);
static bool rec_fex_parse_elem           (rec_fex_elem_t elem, const char *str);

/*
 * Public functions.
 */

rec_fex_t
rec_fex_new (const char *str,
             enum rec_fex_kind_e kind)
{
  rec_fex_t new;
  int i;

  new = malloc (sizeof (struct rec_fex_s));
  if (new)
    {
      rec_fex_init (new);

      new->num_elems = 0;
      new->str = NULL;
      for (i = 0; i < REC_FEX_MAX_ELEMS; i++)
        {
          new->elems[i] = 0;
        }

      if (str != NULL)
        {
          /* Parse the string, using the proper parsing routine
             depending on the kind of field expression requested by
             the user.  */

          if (kind == REC_FEX_SUBSCRIPTS)
            {
              if (!rec_fex_parse_str_subscripts (new, str))
                {
                  /* Out of memory or parse error.  */
                  free (new);
                  return NULL;
                }
            }
          else if (kind == REC_FEX_SIMPLE)
            {
              if (!rec_fex_parse_str_simple (new, str, " \t\n"))
                {
                  /* Out of memory or parse error.  */
                  free (new);
                  return NULL;
                }
            }
          else /* REC_FEX_CSV */
            {
              if (!rec_fex_parse_str_simple (new, str, ","))
                {
                  /* Out of memory or parse error.  */
                  free (new);
                  return NULL;
                }
            }
        }
    }
  
  return new;
}

void
rec_fex_destroy (rec_fex_t fex)
{
  int i;
  
  if (fex)
    {
      for (i = 0; i < fex->num_elems; i++)
        {
          free (fex->elems[i]->rewrite_to);
          free (fex->elems[i]->field_name);
          free (fex->elems[i]->str);
          free (fex->elems[i]);
        }
      
      free (fex->str);
      free (fex);
    }
}

rec_fex_t
rec_fex_dup (rec_fex_t fex)
{
  rec_fex_t copy = NULL;
  size_t i = 0;

  copy = malloc (sizeof (struct rec_fex_s));
  if (copy)
    {
      rec_fex_init (copy);
  
      copy->num_elems = fex->num_elems;
      copy->str = strdup (fex->str);
      if (!copy->str)
        {
          /* Out of memory.  */
          rec_fex_destroy (copy);
          return NULL;
        }

      for (i = 0; i < fex->num_elems; i++)
        {
          if (fex->elems[i] == NULL)
            {
              copy->elems[i] = NULL;
              continue;
            }

          copy->elems[i] = malloc (sizeof (struct rec_fex_elem_s));
          if (!copy->elems[i])
            {
              /* Out of memory.  */
              rec_fex_destroy (copy);
              return NULL;
            }

          copy->elems[i]->max = fex->elems[i]->max;
          copy->elems[i]->min = fex->elems[i]->min;

#define REC_COPY_STR_MAYBE_RETURN(FNAME)                                \
          do                                                            \
            {                                                           \
               if (!fex->elems[i]->FNAME)                               \
                 {                                                      \
                   copy->elems[i]->FNAME = NULL;                        \
                 }                                                      \
               else                                                     \
                 {                                                      \
                   copy->elems[i]->FNAME = strdup (fex->elems[i]->FNAME); \
                   if (!copy->elems[i]->FNAME)                          \
                     {                                                  \
                       /* Out of memory.  */                            \
                       rec_fex_destroy (copy);                          \
                       return NULL;                                     \
                     }                                                  \
                 }                                                      \
            } while (0)

          REC_COPY_STR_MAYBE_RETURN (str);
          REC_COPY_STR_MAYBE_RETURN (field_name);
          REC_COPY_STR_MAYBE_RETURN (rewrite_to);
          REC_COPY_STR_MAYBE_RETURN (function_name);
        }
    }

  return copy;
}

bool
rec_fex_check (const char *str, enum rec_fex_kind_e kind)
{
  char *regexp_str;

  switch (kind)
    {
    case REC_FEX_SIMPLE:
      {
        regexp_str = "^" REC_FNAME_LIST_RE "$";
        break;
      }
    case REC_FEX_CSV:
      {
        regexp_str = "^" REC_FNAME_LIST_CS_RE "$";
        break;
      }
    case REC_FEX_SUBSCRIPTS:
      {
        regexp_str = "^" REC_FNAME_LIST_SUB_RE "$";
        break;
      }
    default:
      {
        regexp_str = NULL;
        break;
      }
    }

  return rec_match (str, regexp_str);
}

size_t
rec_fex_size (rec_fex_t fex)
{
  return fex->num_elems;
}

rec_fex_elem_t
rec_fex_get (rec_fex_t fex,
             size_t position)
{
  if ((position < 0) || (position >= fex->num_elems))
    {
      return NULL;
    }

  return fex->elems[position];
}

const char *
rec_fex_elem_field_name (rec_fex_elem_t elem)
{
  return elem->field_name;
}

bool
rec_fex_elem_set_field_name (rec_fex_elem_t elem,
                             const char *fname)
{
  free (elem->field_name);
  elem->field_name = strdup (fname);
  return (elem->field_name != NULL);
}

int
rec_fex_elem_min (rec_fex_elem_t elem)
{
  return elem->min;
}

int
rec_fex_elem_max (rec_fex_elem_t elem)
{
  return elem->max;
}

const char *
rec_fex_elem_rewrite_to (rec_fex_elem_t elem)
{
  return elem->rewrite_to;
}

void
rec_fex_sort (rec_fex_t fex)
{
  bool done;
  rec_fex_elem_t aux;
  int i, j;

  /* XXX: this code only works when 'max' is not specified.  */

  for (i = 1; i < fex->num_elems; i++)
    {
      aux = fex->elems[i];
      j = i - 1;
      done = false;

      while (!done)
       {
         /* If elems[j] > aux  */
         if ((fex->elems[j]->min == -1) || (fex->elems[j]->min > aux->min))
           {
             fex->elems[j + 1] = fex->elems[j];
             j = j - 1;
             if (j < 0)
               {
                 done = true;
               }
           }
         else
           {
             done = true;
           }
       }

      fex->elems[j + 1] = aux;
    }
}

char *
rec_fex_str (rec_fex_t fex,
             enum rec_fex_kind_e kind)
{
  char *result;
  size_t result_size;
  rec_buf_t buf;
  size_t i;
  char *tmp;

  result = NULL;
  buf = rec_buf_new (&result, &result_size);
  if (buf)
    {
      char *field_str = NULL;

      for (i = 0; i < fex->num_elems; i++)
        {
          if (i != 0)
            {
              if (kind == REC_FEX_SIMPLE)
                {
                  rec_buf_putc (' ', buf);
                }
              else
                {
                  rec_buf_putc (',', buf);
                }
            }
          
          field_str = strdup (fex->elems[i]->field_name);
          if (!field_str)
            {
              rec_buf_close (buf);
              free (result);
              return NULL;
            }

          rec_buf_puts (field_str, buf);
          free (field_str);

          if (kind == REC_FEX_SUBSCRIPTS)
            {
              if ((fex->elems[i]->min != -1)
                  || (fex->elems[i]->max != -1))
                {
                  rec_buf_putc ('[', buf);
                  if (fex->elems[i]->min != -1)
                    {
                      if (asprintf (&tmp, "%d", fex->elems[i]->min) != -1)
                        {
                          rec_buf_puts (tmp, buf);
                          free (tmp);
                        }
                    }
                  if (fex->elems[i]->max != -1)
                    {
                      if (asprintf (&tmp, "-%d", fex->elems[i]->max) != -1)
                        {
                          rec_buf_puts (tmp, buf);
                          free (tmp);
                        }
                    }

                  rec_buf_putc (']', buf);
                }
            }
        }
    }

  rec_buf_close (buf);

  return result;
}

bool
rec_fex_member_p (rec_fex_t fex,
                  const char *fname,
                  int min,
                  int max)
{
  bool res = false;
  int i;
  
  for (i = 0; i < fex->num_elems; i++)
    {
      if (rec_field_name_equal_p (fname,
                                  fex->elems[i]->field_name)
          && ((min == -1) || (fex->elems[i]->min == min))
          && ((max == -1) || (fex->elems[i]->max == max)))
        {
          res = true;
          break;
        }
    }

  return res;
}

rec_fex_elem_t
rec_fex_append (rec_fex_t fex,
                const char *fname,
                int min,
                int max)
{
  rec_fex_elem_t new_elem;

  if (fex->num_elems >= REC_FEX_MAX_ELEMS)
    {
      fprintf (stderr, _("internal error: REC_FEX_MAX_ELEMS exceeded.  Please report this.\n"));
      return NULL;
    }

  new_elem = malloc (sizeof (struct rec_fex_elem_s));
  if (new_elem)
    {
      memset (new_elem, 0, sizeof (*new_elem));
      new_elem->field_name = strdup (fname);
      if (!new_elem->field_name)
        {
          /* Out of memory.  */
          free (new_elem);
          return NULL;
        }

      new_elem->str = strdup (fname);
      if (!new_elem->str)
        {
          /* Out of memory.  */
          free (new_elem->field_name);
          free (new_elem);
          return NULL;
        }

      new_elem->min = min;
      new_elem->max = max;
      fex->elems[fex->num_elems++] = new_elem;
    }

  return new_elem;
}

const char *
rec_fex_elem_function_name (rec_fex_elem_t elem)
{
  return elem->function_name;
}

void **
rec_fex_elem_function_data (rec_fex_elem_t elem)
{
  return elem->function_data;
}

bool
rec_fex_all_calls_p (rec_fex_t fex)
{
  bool result = true;
  size_t i = 0;

  for (i = 0; i < fex->num_elems; i++)
    {
      if (fex->elems[i]->function_name == NULL)
        {
          result = false;
          break;
        }
    }

  return result;
}

/*
 * Private functions.
 */

static void
rec_fex_init (rec_fex_t fex)
{
  /* Initialize the field expression structure so it can be safely
     passed to rec_fex_destroy even if its contents are not completely
     initialized with real values.  */

  memset (fex, 0 /* NULL */, sizeof (struct rec_fex_s));
}

static bool
rec_fex_parse_str_simple (rec_fex_t new,
                          const char *str,
                          const char *sep)
{
  bool res;
  rec_fex_elem_t elem;
  char *fex_str, *fex_str_orig;
  char *elem_str;
  size_t i;

  if (!str)
    {
      return false;
    }

  fex_str = strdup (str);
  if (!fex_str)
    {
      return false;
    }
  fex_str_orig = fex_str;

  res = true;

  elem_str = strsep (&fex_str, sep);
  do
    {
      if (strlen (elem_str) > 0)
        {
          if ((elem = malloc (sizeof (struct rec_fex_elem_s))))
            {
              const char *p = elem_str;

              /* Get the field name.  */

              if (!rec_parse_regexp (&p,
                                     "^" REC_FNAME_RE,
                                     &(elem->field_name)))
                {
                  /* Parse error. */
                  free (elem);
                  res = false;
                  break;
                }

              /* Get the subname, if any, and modify the name
                 accordingly.  */

              if (*p == '.')
                {
                  char *subname = NULL;

                  p++;
                  if (!rec_parse_regexp (&p,
                                         "^" REC_FNAME_RE,
                                         &subname))
                    {
                      /* Parse error.  */
                      free (elem->field_name);
                      free (elem);
                      res = false;
                      break;
                    }

                  /* Concatenate the field name and the subname.  */
                  elem->field_name = rec_concat_strings (elem->field_name, "_", subname);
                }

              /* Check that there are no extra stuff at the end of the
                 string.  */

              if (*p != '\0')
                {
                  free (elem->field_name);
                  free (elem);
                  res = false;
                  break;
                }

              /* Initialize other attributes of the fex entry.  */

              elem->function_name = NULL;
              elem->function_data = NULL;
              elem->rewrite_to = NULL;
              elem->str = strdup (elem_str);
              elem->min = -1;
              elem->max = -1;
              new->elems[new->num_elems++] = elem;
            }
          else
            {
              res = false;
              break;
            }
        }
    }
  while ((elem_str = strsep (&fex_str, sep)));

  if (new->num_elems == 0)
    {
      /* No elements were recognized.  */
      res = false;
    }

  if (res)
    {
      new->str = strdup (str);
    }
  else
    {
      /* Destroy parsed elements.  */
      for (i = 0; i < new->num_elems; i++)
        {
          free (new->elems[i]->rewrite_to);
          free (new->elems[i]->field_name);
          free (new->elems[i]->str);
          free (new->elems[i]);
        }
    }

  free (fex_str_orig);
  return res;
}

static bool
rec_fex_parse_str_subscripts (rec_fex_t new,
                              const char *str)
{
  bool res;
  char *elem_str;
  char *fex_str, *fex_str_orig;
  rec_fex_elem_t elem;
  int i;

  res = true;

  fex_str = strdup (str);
  if (!fex_str)
    {
      return false;
    }
  fex_str_orig = fex_str;

  elem_str = strsep (&fex_str, ",");
  do
    {
      elem = malloc (sizeof (struct rec_fex_elem_s));
      if (!elem)
        {
          /* Out of memory.  */
          res = false;
          break;
        }

      if (!rec_fex_parse_elem (elem, elem_str))
        {
          /* Parse error.  */
          for (i = 0; i < new->num_elems; i++)
            {
              free (new->elems[i]->field_name);
              free (new->elems[i]->str);
              free (new->elems[i]);
            }

          free (elem);
          res = false;
          break;
        }

      /* Add the elem to the FEX.  */
      new->elems[new->num_elems++] = elem;
    }
  while ((elem_str = strsep (&fex_str, ",")));

  if (res)
    {
      new->str = strdup (str);
    }

  free (fex_str_orig);
  return res;
}

static bool
rec_fex_parse_elem (rec_fex_elem_t elem,
                    const char *str)
{
  bool ret;
  const char *p;

  ret = true;
  p = str;

  /* 'Empty' part.  */
  elem->field_name = NULL;
  elem->function_name = NULL;
  elem->function_data = NULL;
  elem->str = NULL;
  elem->rewrite_to = NULL;
  elem->min = -1;
  elem->max = -1;

  /* The 'str' field keeps a copy of the textual entry.  */

  elem->str = strdup (str);

  /* Each FEX element can be either a function call or a field name
     with an optional subscript.  */

  if (rec_match (p, "^" REC_FEX_CALL))
    {
      /* Get the function name and the field argument and store them
         in the FEX element.  */

      if (!rec_parse_regexp (&p,
                             "^" REC_FEX_FUNCTION_NAME,
                             &(elem->function_name)))
        {
          /* Parse error.  */
          free (elem->str);
          return false;
        }

      p++; /* Skip the ( */
    }

  /* Get the field name.  */
      
  if (!rec_parse_regexp (&p,
                         "^" REC_FNAME_RE,
                         &(elem->field_name)))
    {
      /* Parse error.  */
      free (elem->str);
      return false;
    }
  
  /* Get the subname and modify the name accordingly, if it
     exists.  */
  
  if (*p == '.')
    {
      char *subname = NULL;
      
      p++;
      if (!rec_parse_regexp (&p,
                             "^" REC_FNAME_RE,
                             &subname))
        {
          /* Parse error.  */
          free (elem->str);
          return false;
        }
      
      /* Concatenate the field_name and the subname.  */
      elem->field_name = rec_concat_strings (elem->field_name, "_", subname);
    }
  
  /* Get the subscripts if they are present.  */
  if (*p == '[')
    {
      p++;
      /* First subscript in range.  */
      if (!rec_parse_int (&p, &(elem->min)))
        {
          /* Parse error.  */
          free (elem->str);
          free (elem->field_name);
          return false;
        }
      
      if (*p == '-')
        {
          p++;
          /* Second subscript in range.  */
          if (!rec_parse_int (&p, &(elem->max)))
            {
              /* Parse error.  */
              free (elem->str);
              free (elem->field_name);
              return false;
            }
        }
      
      if (*p != ']')
        {
          /* Parse error.  */
          free (elem->str);
          free (elem->field_name);
          return false;
        }
      p++; /* Skip the ]  */
    }

  if (elem->function_name)
    {
      p++; /* Skip the ) */
    }
      
  /* Get the rewrite rule if it is present.  */
  if (*p == ':')
    {
      p++;
      if (!rec_parse_regexp (&p,
                             "^" REC_FNAME_RE,
                             &(elem->rewrite_to)))
        {
          /* Parse error.  */
          free (elem->str);
          free (elem->field_name);
          return false;
        }
    }

  if (*p != '\0')
    {
      free (elem->str);
      free (elem->field_name);
      free (elem->rewrite_to);
      return false;
    }

  return ret;
}

/* End of rec-fex.c */