lexer: Reimplement for better testability and internationalization.
[pspp-builds.git] / src / ui / source-init-opts.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2010  Free Software Foundation
3
4    This program 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
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "source-init-opts.h"
20
21 #include <stdbool.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "data/file-name.h"
26 #include "data/por-file-reader.h"
27 #include "data/settings.h"
28 #include "data/sys-file-reader.h"
29 #include "language/lexer/include-path.h"
30 #include "language/lexer/lexer.h"
31 #include "libpspp/assertion.h"
32 #include "libpspp/argv-parser.h"
33 #include "libpspp/llx.h"
34 #include "libpspp/message.h"
35 #include "ui/syntax-gen.h"
36
37 #include "gl/error.h"
38 #include "gl/xalloc.h"
39
40 #include "gettext.h"
41 #define _(msgid) gettext (msgid)
42 #define N_(msgid) msgid
43
44 enum
45   {
46     OPT_ALGORITHM,
47     OPT_INCLUDE,
48     OPT_NO_INCLUDE,
49     OPT_SAFER,
50     OPT_SYNTAX,
51     N_SOURCE_INIT_OPTIONS
52   };
53
54 static const struct argv_option source_init_options[N_SOURCE_INIT_OPTIONS] =
55   {
56     {"algorithm", 'a', required_argument, OPT_ALGORITHM},
57     {"include", 'I', required_argument, OPT_INCLUDE},
58     {"no-include", 0, no_argument, OPT_NO_INCLUDE},
59     {"safer", 's', no_argument, OPT_SAFER},
60     {"syntax", 'x', required_argument, OPT_SYNTAX},
61   };
62
63 static void
64 source_init_option_callback (int id, void *aux UNUSED)
65 {
66   switch (id)
67     {
68     case OPT_ALGORITHM:
69       if (!strcmp (optarg, "compatible"))
70         settings_set_algorithm (COMPATIBLE);
71       else if (!strcmp (optarg, "enhanced"))
72         settings_set_algorithm (ENHANCED);
73       else
74         /* TRANSLATORS: Leave the words `compatible' and `enhanced' in their
75         original English. */
76         error (1, 0,
77                _("Algorithm must be either `compatible' or `enhanced'."));
78       break;
79
80     case OPT_INCLUDE:
81       if (!strcmp (optarg, "-"))
82         include_path_clear ();
83       else
84         include_path_add (optarg);
85       break;
86
87     case OPT_NO_INCLUDE:
88       include_path_clear ();
89       break;
90
91     case OPT_SAFER:
92       settings_set_safer_mode ();
93       break;
94
95     case OPT_SYNTAX:
96       if (!strcmp (optarg, "compatible") )
97         settings_set_syntax (COMPATIBLE);
98       else if (!strcmp (optarg, "enhanced"))
99         settings_set_syntax (ENHANCED);
100       else
101         /* TRANSLATORS: Leave the words `compatible' and `enhanced' in their
102         original English. */
103         error (1, 0,
104                _("Syntax must be either `compatible' or `enhanced'."));
105       break;
106
107     default:
108       NOT_REACHED ();
109     }
110 }
111
112 void
113 source_init_register_argv_parser (struct argv_parser *ap)
114 {
115   argv_parser_add_options (ap, source_init_options, N_SOURCE_INIT_OPTIONS,
116                            source_init_option_callback, NULL);
117 }