/* parser.c - the part of the parser that can return partial tokens */ /* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2005,2007,2009,2021 Free Software Foundation, Inc. * * VAS_EBOOT 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 3 of the License, or * (at your option) any later version. * * VAS_EBOOT 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. * * You should have received a copy of the GNU General Public License * along with VAS_EBOOT. If not, see . */ #include #include #include #include #include /* All the possible state transitions on the command line. If a transition can not be found, it is assumed that there is no transition and keep_value is assumed to be 1. */ static struct VasEBoot_parser_state_transition state_transitions[] = { {VAS_EBOOT_PARSER_STATE_TEXT, VAS_EBOOT_PARSER_STATE_QUOTE, '\'', 0}, {VAS_EBOOT_PARSER_STATE_TEXT, VAS_EBOOT_PARSER_STATE_DQUOTE, '\"', 0}, {VAS_EBOOT_PARSER_STATE_TEXT, VAS_EBOOT_PARSER_STATE_VAR, '$', 0}, {VAS_EBOOT_PARSER_STATE_TEXT, VAS_EBOOT_PARSER_STATE_ESC, '\\', 0}, {VAS_EBOOT_PARSER_STATE_ESC, VAS_EBOOT_PARSER_STATE_TEXT, 0, 1}, {VAS_EBOOT_PARSER_STATE_QUOTE, VAS_EBOOT_PARSER_STATE_TEXT, '\'', 0}, {VAS_EBOOT_PARSER_STATE_DQUOTE, VAS_EBOOT_PARSER_STATE_TEXT, '\"', 0}, {VAS_EBOOT_PARSER_STATE_DQUOTE, VAS_EBOOT_PARSER_STATE_QVAR, '$', 0}, {VAS_EBOOT_PARSER_STATE_VAR, VAS_EBOOT_PARSER_STATE_VARNAME2, '{', 0}, {VAS_EBOOT_PARSER_STATE_VAR, VAS_EBOOT_PARSER_STATE_VARNAME, 0, 1}, {VAS_EBOOT_PARSER_STATE_VARNAME, VAS_EBOOT_PARSER_STATE_TEXT, ' ', 1}, {VAS_EBOOT_PARSER_STATE_VARNAME, VAS_EBOOT_PARSER_STATE_TEXT, '\t', 1}, {VAS_EBOOT_PARSER_STATE_VARNAME2, VAS_EBOOT_PARSER_STATE_TEXT, '}', 0}, {VAS_EBOOT_PARSER_STATE_QVAR, VAS_EBOOT_PARSER_STATE_QVARNAME2, '{', 0}, {VAS_EBOOT_PARSER_STATE_QVAR, VAS_EBOOT_PARSER_STATE_QVARNAME, 0, 1}, {VAS_EBOOT_PARSER_STATE_QVARNAME, VAS_EBOOT_PARSER_STATE_TEXT, '\"', 0}, {VAS_EBOOT_PARSER_STATE_QVARNAME, VAS_EBOOT_PARSER_STATE_DQUOTE, ' ', 1}, {VAS_EBOOT_PARSER_STATE_QVARNAME, VAS_EBOOT_PARSER_STATE_DQUOTE, '\t', 1}, {VAS_EBOOT_PARSER_STATE_QVARNAME2, VAS_EBOOT_PARSER_STATE_DQUOTE, '}', 0}, {0, 0, 0, 0} }; /* Determines the state following STATE, determined by C. */ VasEBoot_parser_state_t VasEBoot_parser_cmdline_state (VasEBoot_parser_state_t state, char c, char *result) { struct VasEBoot_parser_state_transition *transition; struct VasEBoot_parser_state_transition default_transition; default_transition.to_state = state; default_transition.keep_value = 1; /* Look for a good translation. */ for (transition = state_transitions; transition->from_state; transition++) { if (transition->from_state != state) continue; /* An exact match was found, use it. */ if (transition->input == c) break; if (VasEBoot_isspace (transition->input) && !VasEBoot_isalpha (c) && !VasEBoot_isdigit (c) && c != '_') break; /* A less perfect match was found, use this one if no exact match can be found. */ if (transition->input == 0) break; } if (!transition->from_state) transition = &default_transition; if (transition->keep_value) *result = c; else *result = 0; return transition->to_state; } /* Helper for VasEBoot_parser_split_cmdline. */ static inline int check_varstate (VasEBoot_parser_state_t s) { return (s == VAS_EBOOT_PARSER_STATE_VARNAME || s == VAS_EBOOT_PARSER_STATE_VARNAME2 || s == VAS_EBOOT_PARSER_STATE_QVARNAME || s == VAS_EBOOT_PARSER_STATE_QVARNAME2); } static VasEBoot_err_t add_var (VasEBoot_buffer_t varname, VasEBoot_buffer_t buf, VasEBoot_parser_state_t state, VasEBoot_parser_state_t newstate) { const char *val; /* Check if a variable was being read in and the end of the name was reached. */ if (!(check_varstate (state) && !check_varstate (newstate))) return VAS_EBOOT_ERR_NONE; if (VasEBoot_buffer_append_char (varname, '\0') != VAS_EBOOT_ERR_NONE) return VasEBoot_errno; val = VasEBoot_env_get ((const char *) VasEBoot_buffer_peek_data (varname)); VasEBoot_buffer_reset (varname); if (!val) return VAS_EBOOT_ERR_NONE; /* Insert the contents of the variable in the buffer. */ return VasEBoot_buffer_append_data (buf, val, VasEBoot_strlen (val)); } static VasEBoot_err_t terminate_arg (VasEBoot_buffer_t buffer, int *argc) { VasEBoot_size_t unread = VasEBoot_buffer_get_unread_bytes (buffer); if (unread == 0) return VAS_EBOOT_ERR_NONE; if (*(const char *) VasEBoot_buffer_peek_data_at (buffer, unread - 1) == '\0') return VAS_EBOOT_ERR_NONE; if (VasEBoot_buffer_append_char (buffer, '\0') != VAS_EBOOT_ERR_NONE) return VasEBoot_errno; (*argc)++; return VAS_EBOOT_ERR_NONE; } static VasEBoot_err_t process_char (char c, VasEBoot_buffer_t buffer, VasEBoot_buffer_t varname, VasEBoot_parser_state_t state, int *argc, VasEBoot_parser_state_t *newstate) { char use; *newstate = VasEBoot_parser_cmdline_state (state, c, &use); /* * If a variable was being processed and this character does * not describe the variable anymore, write the variable to * the buffer. */ if (add_var (varname, buffer, state, *newstate) != VAS_EBOOT_ERR_NONE) return VasEBoot_errno; if (check_varstate (*newstate)) { if (use) return VasEBoot_buffer_append_char (varname, use); } else if (*newstate == VAS_EBOOT_PARSER_STATE_TEXT && state != VAS_EBOOT_PARSER_STATE_ESC && VasEBoot_isspace (use)) { /* * Don't add more than one argument if multiple * spaces are used. */ return terminate_arg (buffer, argc); } else if (use) return VasEBoot_buffer_append_char (buffer, use); return VAS_EBOOT_ERR_NONE; } VasEBoot_err_t VasEBoot_parser_split_cmdline (const char *cmdline, VasEBoot_reader_getline_t getline, void *getline_data, int *argc, char ***argv) { VasEBoot_parser_state_t state = VAS_EBOOT_PARSER_STATE_TEXT; VasEBoot_buffer_t buffer, varname; char *rd = (char *) cmdline; char *rp = rd; int i; *argc = 0; *argv = NULL; buffer = VasEBoot_buffer_new (1024); if (buffer == NULL) return VasEBoot_errno; varname = VasEBoot_buffer_new (200); if (varname == NULL) goto fail; do { if (rp == NULL || *rp == '\0') { if (rd != cmdline) { VasEBoot_free (rd); rd = rp = NULL; } if (getline) { getline (&rd, 1, getline_data); rp = rd; } else break; } if (!rd) break; for (; *rp != '\0'; rp++) { VasEBoot_parser_state_t newstate; if (process_char (*rp, buffer, varname, state, argc, &newstate) != VAS_EBOOT_ERR_NONE) goto fail; state = newstate; } } while (state != VAS_EBOOT_PARSER_STATE_TEXT && !check_varstate (state)); /* A special case for when the last character was part of a variable. */ if (add_var (varname, buffer, state, VAS_EBOOT_PARSER_STATE_TEXT) != VAS_EBOOT_ERR_NONE) goto fail; /* Ensure that the last argument is terminated. */ if (terminate_arg (buffer, argc) != VAS_EBOOT_ERR_NONE) goto fail; /* If there are no args, then we're done. */ if (!*argc) { VasEBoot_errno = VAS_EBOOT_ERR_NONE; goto out; } *argv = VasEBoot_calloc (*argc + 1, sizeof (char *)); if (!*argv) goto fail; /* The arguments are separated with 0's, setup argv so it points to the right values. */ for (i = 0; i < *argc; i++) { char *arg; if (i > 0) { if (VasEBoot_buffer_advance_read_pos (buffer, 1) != VAS_EBOOT_ERR_NONE) goto fail; } arg = (char *) VasEBoot_buffer_peek_data (buffer); if (arg == NULL || VasEBoot_buffer_advance_read_pos (buffer, VasEBoot_strlen (arg)) != VAS_EBOOT_ERR_NONE) goto fail; (*argv)[i] = arg; } /* Keep memory for the return values. */ VasEBoot_buffer_take_data (buffer); VasEBoot_errno = VAS_EBOOT_ERR_NONE; out: if (rd != cmdline) VasEBoot_free (rd); VasEBoot_buffer_free (buffer); VasEBoot_buffer_free (varname); return VasEBoot_errno; fail: VasEBoot_free (*argv); *argv = NULL; *argc = 0; goto out; } /* Helper for VasEBoot_parser_execute. */ static VasEBoot_err_t VasEBoot_parser_execute_getline (char **line, int cont __attribute__ ((unused)), void *data) { char **source = data; char *p; if (!*source) { *line = 0; return 0; } p = VasEBoot_strchr (*source, '\n'); if (p) *line = VasEBoot_strndup (*source, p - *source); else *line = VasEBoot_strdup (*source); *source = p ? p + 1 : 0; return 0; } VasEBoot_err_t VasEBoot_parser_execute (char *source) { while (source) { char *line; VasEBoot_parser_execute_getline (&line, 0, &source); VasEBoot_rescue_parse_line (line, VasEBoot_parser_execute_getline, &source); VasEBoot_free (line); VasEBoot_print_error (); } return VasEBoot_errno; }