1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
28 #include "libpspp/assertion.h"
29 #include "libpspp/cast.h"
30 #include "libpspp/compiler.h"
31 #include "libpspp/misc.h"
32 #include "language/lexer/segment.h"
35 #include "gl/minmax.h"
36 #include "gl/progname.h"
37 #include "gl/read-file.h"
38 #include "gl/xalloc.h"
40 /* -a/--auto, -b/--batch, -i/--interactive: syntax mode. */
41 static enum segmenter_mode mode = SEG_MODE_AUTO;
43 /* -v, --verbose: Print row and column information. */
46 /* -1, --one-byte: Feed in one byte at a time? */
49 static const char *parse_options (int argc, char **argv);
50 static void usage (void) NO_RETURN;
53 main (int argc, char *argv[])
55 size_t offset, line_number, line_offset;
56 const char *file_name;
62 set_program_name (argv[0]);
63 file_name = parse_options (argc, argv);
65 /* Read from stdin into 'input'. Ensure that 'input' ends in a new-line
66 followed by a null byte. */
67 input = (!strcmp (file_name, "-")
68 ? fread_file (stdin, &length)
69 : read_file (file_name, &length));
71 error (EXIT_FAILURE, errno, "reading %s failed", file_name);
72 input = xrealloc (input, length + 3);
73 if (length == 0 || input[length - 1] != '\n')
74 input[length++] = '\n';
75 input[length++] = '\0';
77 segmenter_init (&s, mode);
82 for (offset = 0; offset < length; )
84 enum segment_type type;
85 const char *type_name, *p;
93 for (i = 0; i <= length - offset; i++)
95 /* Make a copy to ensure that segmenter_push() isn't actually
99 if (i > 0 && input[offset + i - 1] == '\n')
102 copy = xmemdup (input + offset, i);
103 n = segmenter_push (&s, copy, i, &type);
109 assert (n_newlines <= 2);
112 n = segmenter_push (&s, input + offset, length - offset, &type);
115 error (EXIT_FAILURE, 0, "segmenter_push returned -1 at offset %zu",
117 assert (offset + n <= length);
119 if (type == SEG_NEWLINE)
120 assert ((n == 1 && input[offset] == '\n')
122 && input[offset] == '\r' && input[offset + 1] == '\n'));
124 assert (memchr (&input[offset], '\n', n) == NULL);
128 if (prev_type != SEG_SPACES && prev_type != -1
129 && type == SEG_SPACES && n == 1 && input[offset] == ' ')
142 printf ("%2zu:%2zu: ", line_number, offset - line_offset);
144 type_name = segment_type_to_string (type);
145 for (p = type_name; *p != '\0'; p++)
146 putchar (tolower ((unsigned char) *p));
151 for (i = MIN (15, strlen (type_name)); i < 16; i++)
155 const uint8_t *u_input = CHAR_CAST (const uint8_t *, input);
159 mblen = u8_mbtoucr (&uc, u_input + (offset + i), n - i);
164 mblen = u8_mbtouc (&uc, u_input + (offset + i), n - i);
166 for (j = 0; j < mblen; j++)
170 printf ("%02x", input[offset + i + j]);
207 if (uc < 0x20 || uc == 0x00a0)
208 printf ("<U+%04X>", uc);
210 fwrite (input + offset + i, 1, mblen, stdout);
220 if (type == SEG_NEWLINE)
222 enum prompt_style prompt;
225 line_offset = offset;
227 prompt = segmenter_get_prompt (&s);
228 printf (" (%s)\n", prompt_style_to_string (prompt));
239 parse_options (int argc, char **argv)
243 static const struct option options[] =
245 {"one-byte", no_argument, NULL, '1'},
246 {"auto", no_argument, NULL, 'a'},
247 {"batch", no_argument, NULL, 'b'},
248 {"interactive", no_argument, NULL, 'i'},
249 {"verbose", no_argument, NULL, 'v'},
250 {"help", no_argument, NULL, 'h'},
254 int c = getopt_long (argc, argv, "1abivh", options, NULL);
265 mode = SEG_MODE_AUTO;
269 mode = SEG_MODE_BATCH;
273 mode = SEG_MODE_INTERACTIVE;
296 if (optind + 1 != argc)
297 error (1, 0, "exactly one non-option argument required; "
298 "use --help for help");
306 %s, to test breaking PSPP syntax into lexical segments\n\
307 usage: %s [OPTIONS] INPUT\n\
310 -1, --one-byte feed one byte at a time\n\
311 -a, --auto use \"auto\" syntax mode\n\
312 -b, --batch use \"batch\" syntax mode\n\
313 -i, --interactive use \"interactive\" syntax mode (default)\n\
314 -v, --verbose include rows and column numbers in output\n\
315 -h, --help print this help message\n",
316 program_name, program_name);