/* spearl/spearl.c * * This file is part of spearl. * * Spearl 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. * * Spearl 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 spearl. If not, see */ #include #include #include int __locally spl_input_size; char __locally *spl_input; char __visible lbracket = '(', rbracket = ')'; struct spl_klist_node __visible wordlist = SPL_KLIST_NODE_INIT(&wordlist); int spearl_init(void) { /* dummy */ return 0; } static char *join_to_spl_input(char *s, int len) { return strcat(check_alloc((spl_input = realloc(spl_input, (spl_input_size += (len = strlen(s)))))), s); } static struct spearl_word *new_word(char *p, char *f) { struct spearl_word *word; word = spl_word_alloc(); spl_klist_init(&word->node); spl_klist_add(&wordlist, &word->node); word->p = p; word->len = f - p; return word; } static inline char *skip_spaces(char *p) { while (*p && isspace(*p)) p++; if (!*p) return NULL; return p; } #define isfirstchar() (save == p + 1) static int read_words(char *s) { char *p, *save; spl_make_state(); save = p = s; while (*p) { if (*p == lbracket) { if (!isfirstchar()) { new_word(save, p); save = p; } p++; new_word(save, p); save = p; spl_state_left(); continue; } else if (*p == rbracket) { if (!spl_current_state_level()) return spearl_error(SPL_BAD_BRACKET); p++; new_word(save, p); save = p; spl_state_right(); continue; } else if (isspace(*p)) { new_word(save, p); p = save = skip_spaces(p); } else ++p; } if (spl_current_state_level()) return spearl_error(SPL_BAD_BRACKET); if (!isfirstchar()) new_word(save, p); return 0; } static int join_words(char *s) { join_to_spl_input(s, strlen(s)); if (read_words(s) == -1) return -1; return 0; } int spearl_join(char *s) { if (join_words(s) == -1) return -1; return 0; }