spearl.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* spearl/spearl.c
  2. *
  3. * This file is part of spearl.
  4. *
  5. * Spearl is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Spearl is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with spearl. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <spearl/spearl.h>
  19. #include <spearl/error.h>
  20. #include <spearl/sys.h>
  21. int __locally spl_input_size;
  22. char __locally *spl_input;
  23. char __visible lbracket = '(', rbracket = ')';
  24. struct spl_klist_node __visible wordlist = SPL_KLIST_NODE_INIT(&wordlist);
  25. int spearl_init(void) {
  26. /* dummy */
  27. return 0;
  28. }
  29. static char *join_to_spl_input(char *s, int len) {
  30. return strcat(check_alloc((spl_input = realloc(spl_input, (spl_input_size += (len = strlen(s)))))), s);
  31. }
  32. static struct spearl_word *new_word(char *p, char *f) {
  33. struct spearl_word *word;
  34. word = spl_word_alloc();
  35. spl_klist_init(&word->node);
  36. spl_klist_add(&wordlist, &word->node);
  37. word->p = p;
  38. word->len = f - p;
  39. return word;
  40. }
  41. static inline char *skip_spaces(char *p) {
  42. while (*p && isspace(*p))
  43. p++;
  44. if (!*p)
  45. return NULL;
  46. return p;
  47. }
  48. #define isfirstchar() (save == p + 1)
  49. static int read_words(char *s) {
  50. char *p, *save;
  51. spl_make_state();
  52. save = p = s;
  53. while (*p) {
  54. if (*p == lbracket) {
  55. if (!isfirstchar()) {
  56. new_word(save, p);
  57. save = p;
  58. }
  59. p++;
  60. new_word(save, p);
  61. save = p;
  62. spl_state_left();
  63. continue;
  64. }
  65. else if (*p == rbracket) {
  66. if (!spl_current_state_level())
  67. return spearl_error(SPL_BAD_BRACKET);
  68. p++;
  69. new_word(save, p);
  70. save = p;
  71. spl_state_right();
  72. continue;
  73. }
  74. else if (isspace(*p)) {
  75. new_word(save, p);
  76. p = save = skip_spaces(p);
  77. }
  78. else
  79. ++p;
  80. }
  81. if (spl_current_state_level())
  82. return spearl_error(SPL_BAD_BRACKET);
  83. if (!isfirstchar())
  84. new_word(save, p);
  85. return 0;
  86. }
  87. static int join_words(char *s) {
  88. join_to_spl_input(s, strlen(s));
  89. if (read_words(s) == -1)
  90. return -1;
  91. return 0;
  92. }
  93. int spearl_join(char *s) {
  94. if (join_words(s) == -1)
  95. return -1;
  96. return 0;
  97. }