1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011, 2013, 2014 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/>. */
29 #include "libpspp/assertion.h"
30 #include "libpspp/compiler.h"
31 #include "libpspp/float-format.h"
32 #include "libpspp/integer-format.h"
34 #include "gl/c-ctype.h"
36 #include "gl/intprops.h"
37 #include "gl/progname.h"
38 #include "gl/xalloc.h"
47 static void buffer_put (struct buffer *, const void *, size_t);
48 static void *buffer_put_uninit (struct buffer *, size_t);
67 static enum token_type token;
68 static unsigned long long int tok_integer;
69 static double tok_float;
70 static char *tok_string;
71 static size_t tok_strlen, tok_allocated;
73 /* --be, --le: Integer and floating-point formats. */
74 static enum float_format float_format = FLOAT_IEEE_DOUBLE_BE;
75 static enum integer_format integer_format = INTEGER_MSB_FIRST;
77 /* Input file and current position. */
79 static const char *input_file_name;
80 static int line_number;
82 static void PRINTF_FORMAT (1, 2)
83 fatal (const char *message, ...)
87 fprintf (stderr, "%s:%d: ", input_file_name, line_number);
88 va_start (args, message);
89 vfprintf (stderr, message, args);
99 if (tok_strlen >= tok_allocated)
100 tok_string = x2realloc (tok_string, &tok_allocated);
102 tok_string[tok_strlen] = c;
122 while ((c = getc (input)) != '\n' && c != EOF)
128 while (isspace (c) || c == '<' || c == '>');
134 fatal ("unexpected end of input");
137 else if (isdigit (c) || c == '-')
146 while (isdigit (c) || isalpha (c) || c == '.');
151 if (strchr (tok_string, '.') == NULL)
154 tok_integer = strtoull (tok_string, &tail, 0);
159 tok_float = strtod (tok_string, &tail);
162 fatal ("invalid numeric syntax");
167 while ((c = getc (input)) != '"')
170 fatal ("new-line inside string");
183 else if (isalpha (c))
190 while (isdigit (c) || isalpha (c) || c == '.');
194 if (!strcmp (tok_string, "i8"))
196 else if (!strcmp (tok_string, "i64"))
198 else if (tok_string[0] == 's')
201 tok_integer = atoi (tok_string + 1);
203 else if (!strcmp (tok_string, "SYSMIS"))
206 tok_float = -DBL_MAX;
208 else if (!strcmp (tok_string, "LOWEST"))
211 tok_float = float_get_lowest ();
213 else if (!strcmp (tok_string, "HIGHEST"))
218 else if (!strcmp (tok_string, "ENDIAN"))
221 tok_integer = integer_format == INTEGER_MSB_FIRST ? 1 : 2;
223 else if (!strcmp (tok_string, "COUNT"))
225 else if (!strcmp (tok_string, "hex"))
228 fatal ("invalid token `%s'", tok_string);
231 fatal ("invalid input byte `%c'", c);
235 buffer_put (struct buffer *buffer, const void *data, size_t n)
237 memcpy (buffer_put_uninit (buffer, n), data, n);
241 buffer_put_uninit (struct buffer *buffer, size_t n)
244 if (buffer->size > buffer->allocated)
246 buffer->allocated = buffer->size * 2;
247 buffer->data = xrealloc (buffer->data, buffer->allocated);
249 return &buffer->data[buffer->size - n];
252 /* Returns the integer value of hex digit C. */
256 const char s[] = "0123456789abcdef";
257 const char *cp = strchr (s, c_tolower ((unsigned char) c));
267 %s, SAv Construction Kit\n\
268 usage: %s [OPTIONS] INPUT\n\
270 --be big-endian output format (default)\n\
271 --le little-endian output format\n\
272 --help print this help message and exit\n\
274 The input is a sequence of data items, each followed by a semicolon.\n\
275 Each data item is converted to the output format and written on\n\
276 stdout. A data item is one of the following\n\
278 - An integer in decimal, in hexadecimal prefixed by 0x, or in octal\n\
279 prefixed by 0. Output as a 32-bit binary integer.\n\
281 - A floating-point number. Output in 64-bit IEEE 754 format.\n\
283 - A string enclosed in double quotes. Output literally. There is\n\
284 no syntax for \"escapes\". Strings may not contain new-lines.\n\
286 - A literal of the form s<number> followed by a quoted string as\n\
287 above. Output as the string's contents followed by enough spaces\n\
288 to fill up <number> bytes. For example, s8 \"foo\" is output as\n\
289 the \"foo\" followed by 5 spaces.\n\
291 - The literal \"i8\" followed by an integer. Output as a single\n\
292 byte with the specified value.\n\
294 - The literal \"i64\" followed by an integer. Output as a 64-bit\n\
297 - One of the literals SYSMIS, LOWEST, or HIGHEST. Output as a\n\
298 64-bit IEEE 754 float of the appropriate PSPP value.\n\
300 - The literal ENDIAN. Output as a 32-bit binary integer, either\n\
301 with value 1 if --be is in effect or 2 if --le is in effect.\n\
303 - A pair of parentheses enclosing a sequence of data items, each\n\
304 followed by a semicolon (the last semicolon is optional).\n\
305 Output as the enclosed data items in sequence.\n\
307 - The literal COUNT followed by a sequence of parenthesized data\n\
308 items, as above. Output as a 32-bit binary integer whose value\n\
309 is the number of bytes enclosed within the parentheses, followed\n\
310 by the enclosed data items themselves.\n\
312 optionally followed by an asterisk and a positive integer, which\n\
313 specifies a repeat count for the data item.\n",
314 program_name, program_name);
319 parse_options (int argc, char **argv)
324 OPT_BE = UCHAR_MAX + 1,
328 static const struct option options[] =
330 {"be", no_argument, NULL, OPT_BE},
331 {"le", no_argument, NULL, OPT_LE},
332 {"help", no_argument, NULL, OPT_HELP},
336 int c = getopt_long (argc, argv, "", options, NULL);
343 float_format = FLOAT_IEEE_DOUBLE_BE;
344 integer_format = INTEGER_MSB_FIRST;
348 float_format = FLOAT_IEEE_DOUBLE_LE;
349 integer_format = INTEGER_LSB_FIRST;
368 if (optind + 1 != argc)
369 error (1, 0, "exactly one non-option argument required; "
370 "use --help for help");
375 parse_data_item (struct buffer *output)
377 size_t old_size = output->size;
379 if (token == T_INTEGER)
381 integer_put (tok_integer, integer_format,
382 buffer_put_uninit (output, 4), 4);
385 else if (token == T_FLOAT)
387 float_convert (FLOAT_NATIVE_DOUBLE, &tok_float,
388 float_format, buffer_put_uninit (output, 8));
391 else if (token == T_I8)
398 if (token != T_INTEGER)
399 fatal ("integer expected after `i8'");
401 buffer_put (output, &byte, 1);
404 while (token == T_INTEGER);
406 else if (token == T_I64)
411 if (token != T_INTEGER)
412 fatal ("integer expected after `i64'");
413 integer_put (tok_integer, integer_format,
414 buffer_put_uninit (output, 8), 8);
417 while (token == T_INTEGER);
419 else if (token == T_STRING)
421 buffer_put (output, tok_string, tok_strlen);
424 else if (token == T_S)
431 if (token != T_STRING)
432 fatal ("string expected");
434 fatal ("%zu-byte string is longer than pad length %d",
437 buffer_put (output, tok_string, tok_strlen);
438 memset (buffer_put_uninit (output, n - tok_strlen), ' ',
442 else if (token == T_LPAREN)
446 while (token != T_RPAREN)
447 parse_data_item (output);
451 else if (token == T_COUNT)
453 buffer_put_uninit (output, 4);
456 if (token != T_LPAREN)
457 fatal ("`(' expected after COUNT");
460 while (token != T_RPAREN)
461 parse_data_item (output);
464 integer_put (output->size - old_size - 4, integer_format,
465 output->data + old_size, 4);
467 else if (token == T_HEX)
473 if (token != T_STRING)
474 fatal ("string expected");
476 for (p = tok_string; *p; p++)
478 if (isspace ((unsigned char) *p))
480 else if (isxdigit ((unsigned char) p[0])
481 && isxdigit ((unsigned char) p[1]))
483 int high = hexit_value (p[0]);
484 int low = hexit_value (p[1]);
485 uint8_t byte = high * 16 + low;
486 buffer_put (output, &byte, 1);
490 fatal ("invalid format in hex string");
495 fatal ("syntax error");
497 if (token == T_ASTERISK)
499 size_t n = output->size - old_size;
504 if (token != T_INTEGER || tok_integer < 1)
505 fatal ("positive integer expected after `*'");
506 p = buffer_put_uninit (output, (tok_integer - 1) * n);
507 while (--tok_integer > 0)
509 memcpy (p, output->data + old_size, n);
516 if (token == T_SEMICOLON)
518 else if (token != T_RPAREN)
519 fatal ("`;' expected");
523 main (int argc, char **argv)
525 struct buffer output;
527 set_program_name (argv[0]);
528 input_file_name = parse_options (argc, argv);
530 if (!strcmp (input_file_name, "-"))
534 input = fopen (input_file_name, "r");
536 error (1, errno, "%s: open failed", input_file_name);
539 if (isatty (STDOUT_FILENO))
540 error (1, 0, "not writing binary data to a terminal; redirect to a file");
544 output.allocated = 0;
548 while (token != T_EOF)
549 parse_data_item (&output);
554 fwrite (output.data, output.size, 1, stdout);