starter: Use automake LEX/YACC automatisms.
authorTobias Brunner <tobias@strongswan.org>
Mon, 10 Oct 2011 17:31:04 +0000 (19:31 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 10 Oct 2011 17:31:04 +0000 (19:31 +0200)
src/starter/Makefile.am
src/starter/args.c
src/starter/args.h
src/starter/confread.c
src/starter/confread.h
src/starter/ipsec-parser.h [new file with mode: 0644]
src/starter/lexer.l [new file with mode: 0644]
src/starter/parser.h [deleted file]
src/starter/parser.l [deleted file]
src/starter/parser.y

index f05aeca..d676d1d 100644 (file)
@@ -1,9 +1,10 @@
 ipsec_PROGRAMS = starter
-starter_SOURCES = y.tab.c netkey.c y.tab.h parser.h args.h netkey.h \
+starter_SOURCES = \
+netkey.c parser.y lexer.l ipsec-parser.h args.h netkey.h \
 starterwhack.c starterwhack.h starterstroke.c invokepluto.c confread.c \
 starterstroke.h interfaces.c invokepluto.h confread.h interfaces.h args.c \
 keywords.c files.h keywords.h cmp.c starter.c cmp.h exec.c invokecharon.c \
-exec.h invokecharon.h lex.yy.c loglite.c klips.c klips.h
+exec.h invokecharon.h loglite.c klips.c klips.h
 
 INCLUDES = \
 -I${linux_headers} \
@@ -23,9 +24,12 @@ AM_CFLAGS = \
 -DDEV_URANDOM=\"${urandom_device}\" \
 -DDEBUG
 
+AM_YFLAGS = -v -d
+
 starter_LDADD = defs.o $(top_builddir)/src/libfreeswan/libfreeswan.a $(top_builddir)/src/libstrongswan/libstrongswan.la $(SOCKLIB)
-EXTRA_DIST = parser.l parser.y keywords.txt ipsec.conf
-MAINTAINERCLEANFILES = lex.yy.c y.tab.c y.tab.h keywords.c
+EXTRA_DIST = keywords.txt ipsec.conf
+MAINTAINERCLEANFILES = keywords.c
+BUILT_SOURCES = parser.h
 
 PLUTODIR=$(top_srcdir)/src/pluto
 SCEPCLIENTDIR=$(top_srcdir)/src/scepclient
@@ -42,15 +46,6 @@ if USE_LOAD_WARNING
   AM_CFLAGS += -DLOAD_WARNING
 endif
 
-lex.yy.c:      $(srcdir)/parser.l $(srcdir)/parser.y $(srcdir)/parser.h y.tab.h
-               $(LEX) $(srcdir)/parser.l
-
-y.tab.c:       $(srcdir)/parser.y $(srcdir)/parser.l $(srcdir)/parser.h
-               $(YACC) -v -d $(srcdir)/parser.y
-
-y.tab.h:       $(srcdir)/parser.y $(srcdir)/parser.l $(srcdir)/parser.h
-               $(YACC) -v -d $(srcdir)/parser.y
-
 keywords.c:    $(srcdir)/keywords.txt $(srcdir)/keywords.h
                $(GPERF) -m 10 -C -G -D -t < $(srcdir)/keywords.txt > $@
 
index ad5916f..65d0a75 100644 (file)
@@ -24,7 +24,6 @@
 #include "../pluto/log.h"
 
 #include "keywords.h"
-#include "parser.h"
 #include "confread.h"
 #include "args.h"
 
index b003784..f5c13e6 100644 (file)
@@ -17,7 +17,7 @@
 #define _ARGS_H_
 
 #include "keywords.h"
-#include "parser.h"
+#include "ipsec-parser.h"
 
 extern char **new_list(char *value);
 extern bool assign_arg(kw_token_t token, kw_token_t first, kw_list_t *kw
index fa35341..5f96fb1 100644 (file)
@@ -29,7 +29,6 @@
 #include "../pluto/log.h"
 
 #include "keywords.h"
-#include "parser.h"
 #include "confread.h"
 #include "args.h"
 #include "files.h"
index 5e60ba0..988b8ed 100644 (file)
@@ -20,7 +20,7 @@
 #include <freeswan.h>
 #endif
 
-#include "parser.h"
+#include "ipsec-parser.h"
 #include "interfaces.h"
 
 typedef enum {
diff --git a/src/starter/ipsec-parser.h b/src/starter/ipsec-parser.h
new file mode 100644 (file)
index 0000000..1c6cf20
--- /dev/null
@@ -0,0 +1,55 @@
+/* strongSwan config file parser
+ * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
+ *
+ * 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 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * 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.
+ */
+
+#ifndef _IPSEC_PARSER_H_
+#define _IPSEC_PARSER_H_
+
+#include "keywords.h"
+
+typedef struct kw_entry kw_entry_t;
+
+struct kw_entry {
+       char *name;
+       kw_token_t token;
+};
+
+typedef struct kw_list kw_list_t;
+
+struct kw_list {
+               kw_entry_t *entry;
+               char *value;
+               kw_list_t *next;
+};
+
+typedef struct section_list section_list_t;
+
+struct section_list {
+               char *name;
+               kw_list_t *kw;
+               section_list_t *next;
+};
+
+typedef struct config_parsed config_parsed_t;
+
+struct config_parsed {
+               kw_list_t *config_setup;
+               section_list_t *conn_first, *conn_last;
+               section_list_t *ca_first, *ca_last;
+};
+
+config_parsed_t *parser_load_conf (const char *file);
+void parser_free_conf (config_parsed_t *cfg);
+
+#endif /* _IPSEC_PARSER_H_ */
+
diff --git a/src/starter/lexer.l b/src/starter/lexer.l
new file mode 100644 (file)
index 0000000..734776a
--- /dev/null
@@ -0,0 +1,215 @@
+%option noinput
+%option nounput
+%{
+/* FreeS/WAN config file parser (parser.l)
+ * Copyright (C) 2001 Mathieu Lafon - Arkoon Network Security
+ *
+ * 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 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * 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.
+ */
+
+#include <string.h>
+#include <stdlib.h>
+
+#ifdef HAVE_GLOB_H
+#include <glob.h>
+#endif
+
+#include "parser.h"
+
+#define MAX_INCLUDE_DEPTH  20
+
+extern void yyerror(const char *);
+extern int yylex(void);
+extern int yylex_destroy(void);
+
+static struct {
+       int stack_ptr;
+       YY_BUFFER_STATE stack[MAX_INCLUDE_DEPTH];
+       FILE *file[MAX_INCLUDE_DEPTH];
+       unsigned int line[MAX_INCLUDE_DEPTH];
+       char *filename[MAX_INCLUDE_DEPTH];
+} __parser_y_private;
+
+void _parser_y_error(char *b, int size, const char *s);
+void _parser_y_init (const char *f);
+void _parser_y_fini (void);
+int _parser_y_include (const char *filename);
+
+void _parser_y_error(char *b, int size, const char *s)
+{
+       extern char *yytext; // was: char yytext[];
+
+       snprintf(b, size, "%s:%d: %s [%s]",
+                       __parser_y_private.filename[__parser_y_private.stack_ptr],
+                       __parser_y_private.line[__parser_y_private.stack_ptr],
+                       s, yytext);
+}
+
+void _parser_y_init (const char *f)
+{
+       memset(&__parser_y_private, 0, sizeof(__parser_y_private));
+       __parser_y_private.line[0] = 1;
+       __parser_y_private.filename[0] = strdup(f);
+}
+
+void _parser_y_fini (void)
+{
+       unsigned int i;
+
+       for (i = 0; i < MAX_INCLUDE_DEPTH; i++)
+       {
+               if (__parser_y_private.filename[i])
+                       free(__parser_y_private.filename[i]);
+               if (__parser_y_private.file[i])
+                       fclose(__parser_y_private.file[i]);
+       }
+       memset(&__parser_y_private, 0, sizeof(__parser_y_private));
+       yylex_destroy();
+}
+
+/**
+ * parse the file located at filename
+ */
+int include_file(char *filename)
+{
+       unsigned int p = __parser_y_private.stack_ptr + 1;
+       FILE *f;
+
+       if (p >= MAX_INCLUDE_DEPTH)
+       {
+               yyerror("max inclusion depth reached");
+               return 1;
+       }
+
+       f = fopen(filename, "r");
+       if (!f)
+       {
+               yyerror("can't open include filename");
+               return 0; /* ignore this error */
+       }
+
+       __parser_y_private.stack_ptr++;
+       __parser_y_private.file[p] = f;
+       __parser_y_private.stack[p] = YY_CURRENT_BUFFER;
+       __parser_y_private.line[p] = 1;
+       __parser_y_private.filename[p] = strdup(filename);
+
+       yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
+       return 0;
+}
+
+int _parser_y_include (const char *filename)
+{
+       int ret = 0;
+#ifdef HAVE_GLOB_H
+       {
+               glob_t files;
+               int i;
+
+               ret = glob(filename, GLOB_ERR, NULL, &files);
+               if (ret)
+               {
+                       const char *err;
+
+                       switch (ret)
+                       {
+                               case GLOB_NOSPACE:
+                                       err = "include files ran out of memory";
+                                       break;
+                               case GLOB_ABORTED:
+                                       err = "include files aborted due to read error";
+                                       break;
+                               case GLOB_NOMATCH:
+                                       err = "include files found no matches";
+                                       break;
+                               default:
+                                       err = "unknown include files error";
+                       }
+                       globfree(&files);
+                       yyerror(err);
+                       return 1;
+               }
+
+               for (i = 0; i < files.gl_pathc; i++)
+               {
+                       if ((ret = include_file(files.gl_pathv[i])))
+                       {
+                               break;
+                       }
+               }
+               globfree(&files);
+       }
+#else /* HAVE_GLOB_H */
+       /* if glob(3) is not available, try to load pattern directly */
+       ret = include_file(filename);
+#endif /* HAVE_GLOB_H */
+       return ret;
+}
+
+%}
+
+%%
+
+<<EOF>> {
+               if (__parser_y_private.filename[__parser_y_private.stack_ptr]) {
+                               free(__parser_y_private.filename[__parser_y_private.stack_ptr]);
+                               __parser_y_private.filename[__parser_y_private.stack_ptr] = NULL;
+               }
+               if (__parser_y_private.file[__parser_y_private.stack_ptr]) {
+                               fclose(__parser_y_private.file[__parser_y_private.stack_ptr]);
+                               __parser_y_private.file[__parser_y_private.stack_ptr] = NULL;
+                               yy_delete_buffer (YY_CURRENT_BUFFER);
+                               yy_switch_to_buffer
+                                               (__parser_y_private.stack[__parser_y_private.stack_ptr]);
+               }
+               if (--__parser_y_private.stack_ptr < 0) {
+                               yyterminate();
+               }
+}
+
+^[\t ]+                 return FIRST_SPACES;
+
+[\t ]+                  /* ignore spaces in line */ ;
+
+=                               return EQUAL;
+
+\n|#.*\n                {
+                                                                               __parser_y_private.line[__parser_y_private.stack_ptr]++;
+                                                                               return EOL;
+                                                               }
+
+config                  return CONFIG;
+setup                   return SETUP;
+conn                    return CONN;
+ca                      return CA;
+include                 return INCLUDE;
+version                 return FILE_VERSION;
+
+[^\"= \t\n]+    {
+                                                                               yylval.s = strdup(yytext);
+                                                                               return STRING;
+                                                               }
+
+\"[^\"\n]*\"    {
+                                                                               yylval.s = strdup(yytext+1);
+                                                                               if (yylval.s) yylval.s[strlen(yylval.s)-1]='\0';
+                                                                               return STRING;
+                                                               }
+
+.                               yyerror(yytext);
+
+%%
+
+int yywrap(void)
+{
+       return 1;
+}
+
diff --git a/src/starter/parser.h b/src/starter/parser.h
deleted file mode 100644 (file)
index 1c6cf20..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-/* strongSwan config file parser
- * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
- *
- * 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 2 of the License, or (at your
- * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
- *
- * 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.
- */
-
-#ifndef _IPSEC_PARSER_H_
-#define _IPSEC_PARSER_H_
-
-#include "keywords.h"
-
-typedef struct kw_entry kw_entry_t;
-
-struct kw_entry {
-       char *name;
-       kw_token_t token;
-};
-
-typedef struct kw_list kw_list_t;
-
-struct kw_list {
-               kw_entry_t *entry;
-               char *value;
-               kw_list_t *next;
-};
-
-typedef struct section_list section_list_t;
-
-struct section_list {
-               char *name;
-               kw_list_t *kw;
-               section_list_t *next;
-};
-
-typedef struct config_parsed config_parsed_t;
-
-struct config_parsed {
-               kw_list_t *config_setup;
-               section_list_t *conn_first, *conn_last;
-               section_list_t *ca_first, *ca_last;
-};
-
-config_parsed_t *parser_load_conf (const char *file);
-void parser_free_conf (config_parsed_t *cfg);
-
-#endif /* _IPSEC_PARSER_H_ */
-
diff --git a/src/starter/parser.l b/src/starter/parser.l
deleted file mode 100644 (file)
index c30010f..0000000
+++ /dev/null
@@ -1,215 +0,0 @@
-%option noinput
-%option nounput
-%{
-/* FreeS/WAN config file parser (parser.l)
- * Copyright (C) 2001 Mathieu Lafon - Arkoon Network Security
- *
- * 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 2 of the License, or (at your
- * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
- *
- * 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.
- */
-
-#include <string.h>
-#include <stdlib.h>
-
-#ifdef HAVE_GLOB_H
-#include <glob.h>
-#endif
-
-#include "y.tab.h"
-
-#define MAX_INCLUDE_DEPTH  20
-
-extern void yyerror(const char *);
-extern int yylex(void);
-extern int yylex_destroy(void);
-
-static struct {
-       int stack_ptr;
-       YY_BUFFER_STATE stack[MAX_INCLUDE_DEPTH];
-       FILE *file[MAX_INCLUDE_DEPTH];
-       unsigned int line[MAX_INCLUDE_DEPTH];
-       char *filename[MAX_INCLUDE_DEPTH];
-} __parser_y_private;
-
-void _parser_y_error(char *b, int size, const char *s);
-void _parser_y_init (const char *f);
-void _parser_y_fini (void);
-int _parser_y_include (const char *filename);
-
-void _parser_y_error(char *b, int size, const char *s)
-{
-       extern char *yytext; // was: char yytext[];
-
-       snprintf(b, size, "%s:%d: %s [%s]",
-                       __parser_y_private.filename[__parser_y_private.stack_ptr],
-                       __parser_y_private.line[__parser_y_private.stack_ptr],
-                       s, yytext);
-}
-
-void _parser_y_init (const char *f)
-{
-       memset(&__parser_y_private, 0, sizeof(__parser_y_private));
-       __parser_y_private.line[0] = 1;
-       __parser_y_private.filename[0] = strdup(f);
-}
-
-void _parser_y_fini (void)
-{
-       unsigned int i;
-
-       for (i = 0; i < MAX_INCLUDE_DEPTH; i++)
-       {
-               if (__parser_y_private.filename[i])
-                       free(__parser_y_private.filename[i]);
-               if (__parser_y_private.file[i])
-                       fclose(__parser_y_private.file[i]);
-       }
-       memset(&__parser_y_private, 0, sizeof(__parser_y_private));
-       yylex_destroy();
-}
-
-/**
- * parse the file located at filename
- */
-int include_file(char *filename)
-{
-       unsigned int p = __parser_y_private.stack_ptr + 1;
-       FILE *f;
-
-       if (p >= MAX_INCLUDE_DEPTH)
-       {
-               yyerror("max inclusion depth reached");
-               return 1;
-       }
-
-       f = fopen(filename, "r");
-       if (!f)
-       {
-               yyerror("can't open include filename");
-               return 0; /* ignore this error */
-       }
-
-       __parser_y_private.stack_ptr++;
-       __parser_y_private.file[p] = f;
-       __parser_y_private.stack[p] = YY_CURRENT_BUFFER;
-       __parser_y_private.line[p] = 1;
-       __parser_y_private.filename[p] = strdup(filename);
-
-       yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
-       return 0;
-}
-
-int _parser_y_include (const char *filename)
-{
-       int ret = 0;
-#ifdef HAVE_GLOB_H
-       {
-               glob_t files;
-               int i;
-
-               ret = glob(filename, GLOB_ERR, NULL, &files);
-               if (ret)
-               {
-                       const char *err;
-
-                       switch (ret)
-                       {
-                               case GLOB_NOSPACE:
-                                       err = "include files ran out of memory";
-                                       break;
-                               case GLOB_ABORTED:
-                                       err = "include files aborted due to read error";
-                                       break;
-                               case GLOB_NOMATCH:
-                                       err = "include files found no matches";
-                                       break;
-                               default:
-                                       err = "unknown include files error";
-                       }
-                       globfree(&files);
-                       yyerror(err);
-                       return 1;
-               }
-
-               for (i = 0; i < files.gl_pathc; i++)
-               {
-                       if ((ret = include_file(files.gl_pathv[i])))
-                       {
-                               break;
-                       }
-               }
-               globfree(&files);
-       }
-#else /* HAVE_GLOB_H */
-       /* if glob(3) is not available, try to load pattern directly */
-       ret = include_file(filename);
-#endif /* HAVE_GLOB_H */
-       return ret;
-}
-
-%}
-
-%%
-
-<<EOF>> {
-               if (__parser_y_private.filename[__parser_y_private.stack_ptr]) {
-                               free(__parser_y_private.filename[__parser_y_private.stack_ptr]);
-                               __parser_y_private.filename[__parser_y_private.stack_ptr] = NULL;
-               }
-               if (__parser_y_private.file[__parser_y_private.stack_ptr]) {
-                               fclose(__parser_y_private.file[__parser_y_private.stack_ptr]);
-                               __parser_y_private.file[__parser_y_private.stack_ptr] = NULL;
-                               yy_delete_buffer (YY_CURRENT_BUFFER);
-                               yy_switch_to_buffer
-                                               (__parser_y_private.stack[__parser_y_private.stack_ptr]);
-               }
-               if (--__parser_y_private.stack_ptr < 0) {
-                               yyterminate();
-               }
-}
-
-^[\t ]+                 return FIRST_SPACES;
-
-[\t ]+                  /* ignore spaces in line */ ;
-
-=                               return EQUAL;
-
-\n|#.*\n                {
-                                                                               __parser_y_private.line[__parser_y_private.stack_ptr]++;
-                                                                               return EOL;
-                                                               }
-
-config                  return CONFIG;
-setup                   return SETUP;
-conn                    return CONN;
-ca                      return CA;
-include                 return INCLUDE;
-version                 return FILE_VERSION;
-
-[^\"= \t\n]+    {
-                                                                               yylval.s = strdup(yytext);
-                                                                               return STRING;
-                                                               }
-
-\"[^\"\n]*\"    {
-                                                                               yylval.s = strdup(yytext+1);
-                                                                               if (yylval.s) yylval.s[strlen(yylval.s)-1]='\0';
-                                                                               return STRING;
-                                                               }
-
-.                               yyerror(yytext);
-
-%%
-
-int yywrap(void)
-{
-       return 1;
-}
-
index b0b1f6f..dfaec9e 100644 (file)
@@ -22,7 +22,7 @@
 #include "../pluto/constants.h"
 #include "../pluto/defs.h"
 #include "../pluto/log.h"
-#include "parser.h"
+#include "ipsec-parser.h"
 
 #define YYERROR_VERBOSE
 #define ERRSTRING_LEN   256