summaryrefslogtreecommitdiff
path: root/m4/resyntax.c
blob: 9ab66a91dfb224ac23c83abc1fad7923bf3d3c57 (plain)
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
/* GNU m4 -- A simple macro processor
   Copyright (C) 2006-2008, 2010, 2013-2014 Free Software Foundation,
   Inc.

   This file is part of GNU M4.

   GNU M4 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.

   GNU M4 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 <regex.h>
#include <string.h>

#include "m4private.h"

typedef struct {
  const char    *spec;
  const int     code;
} m4_resyntax;

/* The syntaxes named in this table are saved into frozen files.  Changing
   the mappings will break programs that load a frozen file made before
   such a change...  */

static m4_resyntax const m4_resyntax_map[] =
{
  /* First, the canonical definitions for reverse lookups:  */

  { "AWK",                      RE_SYNTAX_AWK },
  { "ED",                       RE_SYNTAX_ED },
  { "EGREP",                    RE_SYNTAX_EGREP },
  { "EMACS",                    RE_SYNTAX_EMACS },
  { "GNU_AWK",                  RE_SYNTAX_GNU_AWK },
  { "GREP",                     RE_SYNTAX_GREP },
  { "POSIX_AWK",                RE_SYNTAX_POSIX_AWK },
  { "POSIX_BASIC",              RE_SYNTAX_POSIX_BASIC },
  { "POSIX_EGREP",              RE_SYNTAX_POSIX_EGREP },
  { "POSIX_EXTENDED",           RE_SYNTAX_POSIX_EXTENDED },
  { "POSIX_MINIMAL_BASIC",      RE_SYNTAX_POSIX_MINIMAL_BASIC },
  { "SED",                      RE_SYNTAX_SED },

  /* The rest are aliases, for forward lookups only:  */

  { "",                         RE_SYNTAX_EMACS },
  { "BASIC",                    RE_SYNTAX_POSIX_BASIC },
  { "BSD_M4",                   RE_SYNTAX_POSIX_EXTENDED },
  { "EXTENDED",                 RE_SYNTAX_POSIX_EXTENDED },
  { "GAWK",                     RE_SYNTAX_GNU_AWK },
  { "GNU_EGREP",                RE_SYNTAX_EGREP },
  { "GNU_EMACS",                RE_SYNTAX_EMACS },
  { "GNU_M4",                   RE_SYNTAX_EMACS },
  { "MINIMAL",                  RE_SYNTAX_POSIX_MINIMAL_BASIC },
  { "MINIMAL_BASIC",            RE_SYNTAX_POSIX_MINIMAL_BASIC },
  { "POSIX_MINIMAL",            RE_SYNTAX_POSIX_MINIMAL_BASIC },

  /* End marker:  */

  { NULL,                       -1 }
};


/* Return the internal code representing the syntax SPEC, or -1 if
   SPEC is invalid.  The `m4_syntax_map' table is searched case
   insensitively, after replacing any spaces or dashes in SPEC with
   underscore characters.  Possible matches for the "GNU_M4" element
   then, are "gnu m4", "GNU-m4" or "Gnu_M4".  */
int
m4_regexp_syntax_encode (const char *spec)
{
  const m4_resyntax *resyntax;
  char *canonical;
  char *p;

  /* Unless specified otherwise, return the historical GNU M4 default.  */
  if (!spec)
    return RE_SYNTAX_EMACS;

  canonical = xstrdup (spec);

  /* Canonicalise SPEC.  */
  for (p = canonical; *p != '\0'; ++p)
    {
      if ((*p == ' ') || (*p == '-'))
        *p = '_';
      else if (islower (to_uchar (*p)))
        *p = toupper (to_uchar (*p));
    }

  for (resyntax = m4_resyntax_map; resyntax->spec != NULL; ++resyntax)
    {
      if (STREQ (resyntax->spec, canonical))
        break;
    }

  free (canonical);

  return resyntax->code;
}


/* Return the syntax specifier that matches CODE, or NULL if there is
   no match.  */
const char *
m4_regexp_syntax_decode (int code)
{
  const m4_resyntax *resyntax;

  for (resyntax = m4_resyntax_map; resyntax->spec != NULL; ++resyntax)
    {
      if (resyntax->code == code)
        break;
    }

  return resyntax->spec;
}