PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/m4/resyntax.c

#
C | 126 lines | 70 code | 23 blank | 33 comment | 16 complexity | c4b10e915a74f01d3e01e49144dd291a MD5 | raw file
  1. /* GNU m4 -- A simple macro processor
  2. Copyright (C) 2006, 2007, 2008, 2010 Free Software Foundation, Inc.
  3. This file is part of GNU M4.
  4. GNU M4 is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. GNU M4 is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <config.h>
  16. #include <regex.h>
  17. #include <string.h>
  18. #include "m4private.h"
  19. typedef struct {
  20. const char *spec;
  21. const int code;
  22. } m4_resyntax;
  23. /* The syntaxes named in this table are saved into frozen files. Changing
  24. the mappings will break programs that load a frozen file made before
  25. such a change... */
  26. static m4_resyntax const m4_resyntax_map[] =
  27. {
  28. /* First, the canonical definitions for reverse lookups: */
  29. { "AWK", RE_SYNTAX_AWK },
  30. { "ED", RE_SYNTAX_ED },
  31. { "EGREP", RE_SYNTAX_EGREP },
  32. { "EMACS", RE_SYNTAX_EMACS },
  33. { "GNU_AWK", RE_SYNTAX_GNU_AWK },
  34. { "GREP", RE_SYNTAX_GREP },
  35. { "POSIX_AWK", RE_SYNTAX_POSIX_AWK },
  36. { "POSIX_BASIC", RE_SYNTAX_POSIX_BASIC },
  37. { "POSIX_EGREP", RE_SYNTAX_POSIX_EGREP },
  38. { "POSIX_EXTENDED", RE_SYNTAX_POSIX_EXTENDED },
  39. { "POSIX_MINIMAL_BASIC", RE_SYNTAX_POSIX_MINIMAL_BASIC },
  40. { "SED", RE_SYNTAX_SED },
  41. /* The rest are aliases, for forward lookups only: */
  42. { "", RE_SYNTAX_EMACS },
  43. { "BASIC", RE_SYNTAX_POSIX_BASIC },
  44. { "BSD_M4", RE_SYNTAX_POSIX_EXTENDED },
  45. { "EXTENDED", RE_SYNTAX_POSIX_EXTENDED },
  46. { "GAWK", RE_SYNTAX_GNU_AWK },
  47. { "GNU_EGREP", RE_SYNTAX_EGREP },
  48. { "GNU_EMACS", RE_SYNTAX_EMACS },
  49. { "GNU_M4", RE_SYNTAX_EMACS },
  50. { "MINIMAL", RE_SYNTAX_POSIX_MINIMAL_BASIC },
  51. { "MINIMAL_BASIC", RE_SYNTAX_POSIX_MINIMAL_BASIC },
  52. { "POSIX_MINIMAL", RE_SYNTAX_POSIX_MINIMAL_BASIC },
  53. /* End marker: */
  54. { NULL, -1 }
  55. };
  56. /* Return the internal code representing the syntax SPEC, or -1 if
  57. SPEC is invalid. The `m4_syntax_map' table is searched case
  58. insensitively, after replacing any spaces or dashes in SPEC with
  59. underscore characters. Possible matches for the "GNU_M4" element
  60. then, are "gnu m4", "GNU-m4" or "Gnu_M4". */
  61. int
  62. m4_regexp_syntax_encode (const char *spec)
  63. {
  64. const m4_resyntax *resyntax;
  65. char *canonical;
  66. char *p;
  67. /* Unless specified otherwise, return the historical GNU M4 default. */
  68. if (!spec)
  69. return RE_SYNTAX_EMACS;
  70. canonical = xstrdup (spec);
  71. /* Canonicalise SPEC. */
  72. for (p = canonical; *p != '\0'; ++p)
  73. {
  74. if ((*p == ' ') || (*p == '-'))
  75. *p = '_';
  76. else if (islower (to_uchar (*p)))
  77. *p = toupper (to_uchar (*p));
  78. }
  79. for (resyntax = m4_resyntax_map; resyntax->spec != NULL; ++resyntax)
  80. {
  81. if (STREQ (resyntax->spec, canonical))
  82. break;
  83. }
  84. free (canonical);
  85. return resyntax->code;
  86. }
  87. /* Return the syntax specifier that matches CODE, or NULL if there is
  88. no match. */
  89. const char *
  90. m4_regexp_syntax_decode (int code)
  91. {
  92. const m4_resyntax *resyntax;
  93. for (resyntax = m4_resyntax_map; resyntax->spec != NULL; ++resyntax)
  94. {
  95. if (resyntax->code == code)
  96. break;
  97. }
  98. return resyntax->spec;
  99. }