spearl.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. struct spearl_object __visible root_object;
  26. #define callfn(fn) if (fn == -1) return -1
  27. int spearl_init(void) {
  28. root_object.type = LIST;
  29. root_object.list = spl_list_alloc();
  30. spl_klist_init(&root_object.node);
  31. spl_klist_init(&root_object.list->objs);
  32. spl_klist_init(&root_object.list->node);
  33. root_object.list->nobjs = 0;
  34. return 0;
  35. }
  36. static char *join_to_spl_input(char *s, int len) {
  37. return strcat(check_alloc((spl_input = realloc(spl_input, (spl_input_size += (len = strlen(s)))))), s);
  38. }
  39. static struct spearl_word *new_word(char *p, char *f) {
  40. struct spearl_word *word;
  41. word = spl_word_alloc();
  42. spl_klist_init(&word->node);
  43. spl_klist_add(&wordlist, &word->node);
  44. word->p = p;
  45. word->len = f - p;
  46. return word;
  47. }
  48. static inline char *skip_spaces(char *p) {
  49. while (*p && isspace(*p))
  50. p++;
  51. if (!*p)
  52. return NULL;
  53. return p;
  54. }
  55. #define notfirstchar() (save < p)
  56. static int read_words(char *s) {
  57. char *p, *save;
  58. spl_make_state();
  59. save = p = s;
  60. while (*p) {
  61. if (*p == lbracket) {
  62. if (notfirstchar()) {
  63. new_word(save, p);
  64. save = p;
  65. }
  66. p++;
  67. new_word(save, p);
  68. save = p;
  69. spl_state_left();
  70. continue;
  71. }
  72. else if (*p == rbracket) {
  73. if (!spl_current_state_level())
  74. return spearl_error(SPL_BAD_BRACKETS);
  75. new_word(save, p);
  76. p++;
  77. save = p;
  78. spl_state_right();
  79. continue;
  80. }
  81. else if (isspace(*p)) {
  82. new_word(save, p);
  83. p = save = skip_spaces(p);
  84. }
  85. else
  86. ++p;
  87. }
  88. if (spl_current_state_level())
  89. return spearl_error(SPL_BAD_BRACKETS);
  90. if (notfirstchar())
  91. new_word(save, p);
  92. return 0;
  93. }
  94. static int join_words(char *s) {
  95. join_to_spl_input(s, strlen(s));
  96. callfn(read_words(s));
  97. return 0;
  98. }
  99. static struct spearl_object *read_word_object(struct spearl_word *word,
  100. struct spl_klist_node **nout) {
  101. struct spearl_object *obj;
  102. obj = spl_object_alloc();
  103. spl_klist_init(&obj->node);
  104. obj->type = WORD;
  105. obj->word = word;
  106. *nout = word->node.next;
  107. spl_klist_del(&wordlist, &word->node);
  108. return obj;
  109. }
  110. static struct spearl_object *read_list_object(struct spearl_word *word,
  111. struct spl_klist_node **nout) {
  112. struct spearl_list *list;
  113. struct spearl_object *object, *obj;
  114. struct spl_klist_node *n, *next;
  115. object = spl_object_alloc();
  116. list = spl_list_alloc();
  117. spl_klist_init(&object->node);
  118. spl_klist_init(&list->objs);
  119. spl_klist_init(&list->node);
  120. object->type = LIST;
  121. object->list = list;
  122. list->nobjs = 0;
  123. spl_klist_del(&wordlist, &word->node);
  124. for (n = word->node.next; n != &wordlist; n = next) {
  125. next = n->next;
  126. word = word_of_node(n);
  127. if (*word->p == rbracket) {
  128. *nout = next;
  129. spl_klist_del(&wordlist, n);
  130. return object;
  131. }
  132. else if (*word->p == lbracket)
  133. obj = read_list_object(word, &next);
  134. else
  135. obj = read_word_object(word, &next);
  136. spl_klist_add(&list->objs, &obj->node);
  137. }
  138. return object;
  139. }
  140. static int build_list(void) {
  141. struct spl_klist_node *n;
  142. struct spearl_word *word;
  143. struct spearl_object *obj;
  144. n = wordlist.next;
  145. while (!spl_klist_empty(&wordlist)) {
  146. word = word_of_node(n);
  147. if (*word->p != lbracket)
  148. return spearl_error(SPL_INVALID_LIST);
  149. if (!(obj = read_list_object(word, &n)))
  150. return -1;
  151. spl_klist_add(&root_object.list->objs, &obj->node);
  152. root_object.list->nobjs++;
  153. }
  154. return 0;
  155. }
  156. struct spearl_mcall *collect_args(struct spearl_list *list) {
  157. struct spearl_mcall *mcall;
  158. struct spearl_object *obj;
  159. struct spearl_word *word;
  160. struct spearl_mcall_arg *arg;
  161. struct spl_klist_node *n;
  162. spl_make_state();
  163. mcall = spl_mcall_alloc();
  164. mcall->list = list;
  165. mcall->dest = NULL;
  166. mcall->mname = NULL;
  167. spl_klist_init(&mcall->args);
  168. for (n = list->objs.next; n != &list->objs; n = n->next) {
  169. obj = object_of_node(n);
  170. if (obj->type == WORD) {
  171. word = obj->word;
  172. if (spl_state_in()) {
  173. (void) spearl_error(SPL_LIST_NFOUND);
  174. return NULL;
  175. }
  176. /* we won't do any check, to make sure we didn't set it. */
  177. switch (*word->p) {
  178. case '#':
  179. mcall->mname = obj;
  180. break;
  181. case '~':
  182. arg = spl_arg_alloc();
  183. spl_klist_init(&arg->node);
  184. spl_klist_add(&mcall->args, &arg->node);
  185. arg->object = obj;
  186. if (word->len == 1) {
  187. arg->type = LIST;
  188. spl_set_state(IN);
  189. }
  190. else {
  191. arg->type = WORD;
  192. arg->p = strndup(word->p + 1, word->len - 1);
  193. }
  194. break;
  195. case '@':
  196. mcall->dest = obj;
  197. break;
  198. default:
  199. (void) spearl_error(SPL_UNEXPECTED_WORD);
  200. return NULL;
  201. }
  202. }
  203. else {
  204. if (spl_state_in()) {
  205. arg->list = obj->list;
  206. spl_set_state(OUT);
  207. }
  208. else {
  209. (void) spearl_error(SPL_UNEXPECTED_LIST);
  210. return NULL;
  211. }
  212. }
  213. }
  214. return mcall;
  215. }
  216. static inline int mcall_valid(struct spearl_mcall *mcall) {
  217. if (mcall->dest && mcall->mname && !spl_klist_empty(&mcall->args))
  218. return 1;
  219. return 0;
  220. }
  221. static int do_expand_macro(struct spearl_list *list) {
  222. struct spearl_mcall *mcall;
  223. if (!(mcall = collect_args(list)))
  224. return -1;
  225. if (!mcall_valid(mcall))
  226. return spearl_error(SPL_INCOMPLETE_CALL);
  227. return 0;
  228. }
  229. static int expand_calls(void) {
  230. struct spl_klist_node *n;
  231. struct spearl_object *obj;
  232. for (n = root_object.list->objs.next; n != &root_object.list->objs; n = n->next) {
  233. obj = object_of_node(n);
  234. if (obj->type != LIST)
  235. return spearl_error(SPL_INVALID_LIST);
  236. callfn(do_expand_macro(obj->list));
  237. }
  238. return 0;
  239. }
  240. int spearl_join(char *s) {
  241. callfn(join_words(s));
  242. callfn(build_list());
  243. callfn(expand_calls());
  244. return 0;
  245. }