arch_templ.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /* tripl/arch/arch_templ.c
  2. *
  3. * This file is part of tripl.
  4. *
  5. * tripl 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. * tripl 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 tripl. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <sys/types.h>
  22. #include <sys/wait.h>
  23. #include <string.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include <klist.h>
  27. #include <config.h>
  28. #include <tripl/tripl.h>
  29. #include <tripl/limits.h>
  30. #include <tripl/arch.h>
  31. char *psave;
  32. struct klist_node names_list = KLIST_NODE_INIT(&names_list);
  33. extern char *line_buffer;
  34. extern char *line_eof;
  35. __arch_templ static int new_var(char *p, struct arch *);
  36. __arch_templ static int set_name(char *p, struct arch *, long *out);
  37. __arch_templ static int math_set_name(char *p, struct arch *, long *out);
  38. __arch_templ static int declare_object(char *p, struct arch *);
  39. __arch_templ static int interp_in_interp(char *p, struct arch *, long *out);
  40. __arch_templ int cstyle_declare(char *p, struct arch *);
  41. __arch_templ union math_expr *parse_math_expr(char *p);
  42. __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
  43. long ignored;
  44. psave = p;
  45. if (unlikely(p == line_eof))
  46. return 0;
  47. switch (*p) {
  48. case '$':
  49. /* '$ asdf = 0;' --- asdf = 0
  50. * '$ asdf 0;' --- asdf = 0
  51. * '$ asdf;' --- asdf = 0
  52. * */
  53. p++;
  54. _skip_spaces(p);
  55. new_var(p, arch_fn);
  56. break;
  57. case '!':
  58. p++;
  59. _skip_spaces(p);
  60. if (likely(!arch_fn->arch_dump))
  61. exit(generic_error("unsupported dumping in current mode", psave, lno));
  62. arch_fn->arch_dump(getattrmask(p, dump_attrtab));
  63. break;
  64. case '<':
  65. declare_object(p, arch_fn);
  66. break;
  67. case '`':
  68. /* running code in another interpreter */
  69. interp_in_interp(p, arch_fn, &ignored);
  70. break;
  71. case '+':
  72. case '-':
  73. if (!math_set_name(p, arch_fn, &ignored))
  74. exit(generic_error("unknow keyword", psave, lno));
  75. break;
  76. default:
  77. if (unlikely(!set_name(p, arch_fn, &ignored) &&
  78. !math_set_name(p, arch_fn, &ignored) &&
  79. !cstyle_declare(p, arch_fn)))
  80. exit(generic_error("unknow keyword", psave, lno));
  81. break;
  82. }
  83. return 0;
  84. }
  85. struct klist_node *find_name(char *name) {
  86. struct klist_node *n;
  87. for (n = names_list.next; n != &names_list; n = n->next)
  88. if (!strcmp(n->payload, name))
  89. return n;
  90. return NULL;
  91. }
  92. __arch_templ static int new_var(char *p, struct arch *arch_fn) {
  93. char *name;
  94. int type, out, attr;
  95. struct klist_node *t;
  96. getword(p, &type, &out);
  97. if (unlikely(type != WORDTYPE_NAME))
  98. exit(generic_error("a name for variable required", psave, lno));
  99. name = strndup(p, out);
  100. p += out;
  101. _skip_spaces(p);
  102. getword(p, &type, &out);
  103. switch (type) {
  104. case WORDTYPE_KWORD:
  105. if (unlikely(out != '='))
  106. exit(generic_error("keyword but it's not a '='", psave, lno));
  107. ++p;
  108. _skip_spaces(p);
  109. getword(p, &type, &out);
  110. if (unlikely(type != WORDTYPE_NR))
  111. exit(generic_error("a number required", psave, lno));
  112. _skip_word(p);
  113. break;
  114. case WORDTYPE_NR:
  115. /* to attribute (if have) */
  116. _skip_word(p);
  117. break;
  118. case 0:
  119. out = 0;
  120. break;
  121. default:
  122. exit(generic_error("bad word after variable name", psave, lno));
  123. }
  124. attr = getattrmask(p, vardef_attrtab);
  125. if (unlikely((t = find_name(name)) && !(t->xmask & XMASK_WEAK))) {
  126. error(name);
  127. exit(generic_error("name already defined", psave, lno));
  128. }
  129. arch_fn->arch_new_var(name, out, attr);
  130. /* the name can't be free */
  131. return 0;
  132. }
  133. static int getrightvalue(char *p, struct arch *arch_fn) {
  134. long res;
  135. if (*p == '`') {
  136. if (!interp_in_interp(p, arch_fn, &res))
  137. /* actually, it never be here */
  138. exit(generic_error("invalid right-value", psave, lno));
  139. }
  140. else if (!set_name(p, arch_fn, &res)
  141. && !math_set_name(p, arch_fn, &res))
  142. exit(generic_error("invalid right-value", psave, lno));
  143. return res;
  144. }
  145. static int set_name(char *p, struct arch *arch_fn, long *res) {
  146. char *name;
  147. int type, out;
  148. getword(p, &type, &out);
  149. if (type != WORDTYPE_NAME)
  150. return 0;
  151. name = strndup(p, out);
  152. p += out;
  153. _skip_spaces(p);
  154. getword(p, &type, &out);
  155. if (type != WORDTYPE_KWORD || out != '=')
  156. return 0;
  157. p++;
  158. _skip_spaces(p);
  159. getword(p, &type, &out);
  160. if (type == WORDTYPE_KWORD && out == '$') {
  161. out = arch_fn->arch_math_expr(p, name);
  162. goto skip;
  163. }
  164. else if (type != WORDTYPE_NR)
  165. out = getrightvalue(p, arch_fn);
  166. arch_fn->arch_set_var(name, out, 0);
  167. skip: *res = out;
  168. free(name);
  169. return 1;
  170. }
  171. /* 9 -> a; and warning that math_set_name doesn't support instructions
  172. * such as 'a = 0 -> b' */
  173. static int math_set_name(char *p, struct arch *arch_fn, long *res) {
  174. char *name;
  175. int type, out, nr;
  176. getword(p, &type, &out);
  177. if (type != WORDTYPE_NR)
  178. return 0;
  179. nr = out;
  180. /* this requires a space */
  181. _skip_word(p);
  182. getword(p, &type, &out);
  183. if (unlikely(type != WORDTYPE_KWORD || out != '-'))
  184. exit(generic_error("a '->' required", psave, lno));
  185. p++;
  186. getword(p, &type, &out);
  187. if (unlikely(type != WORDTYPE_KWORD || out != '>'))
  188. exit(generic_error("a '->' required", psave, lno));
  189. p++;
  190. _skip_spaces(p);
  191. getword(p, &type, &out);
  192. if (unlikely(type != WORDTYPE_NAME))
  193. exit(generic_error("a name required", psave, lno));
  194. name = strndup(p, out);
  195. arch_fn->arch_set_var(name, nr, 0);
  196. *res = nr;
  197. free(name);
  198. return 1;
  199. }
  200. /*
  201. * 1.
  202. * < "libfoo.a" @ dynalib;
  203. * 2.
  204. * < fn @ fn;
  205. *
  206. * 3.
  207. * < fn 2 @ fn;
  208. *
  209. * 4.
  210. * < fn (a, b) @ fn;
  211. *
  212. * 5.
  213. * < age @ var;
  214. * */
  215. static int declare_object(char *p, struct arch *arch_fn) {
  216. char *name;
  217. int type, out, attr;
  218. enum { SHARED, NAMES, FUNCTION } guess;
  219. struct klist_node *n, *t;
  220. /* skip '<' */
  221. p++;
  222. _skip_spaces(p);
  223. if (unlikely(!getword(p, &type, &out)))
  224. exit(generic_error("a module name or object name required", psave, lno));
  225. switch (type) {
  226. case WORDTYPE_STR:
  227. /* it must be a dynamic library */
  228. name = strndup(p + 1, out);
  229. guess = SHARED;
  230. if (arch_fn->arch_import_shared)
  231. arch_fn->arch_import_shared(name);
  232. p += out + 2;
  233. /* we will not free the name because interp_libs_dump uses it */
  234. return 0;
  235. case WORDTYPE_NAME:
  236. /* functions or variables */
  237. name = strndup(p, out);
  238. /* join to the name list */
  239. n = malloc(sizeof(struct klist_node));
  240. klist_init(n);
  241. n->payload = name;
  242. guess = NAMES;
  243. p += out;
  244. break;
  245. default:
  246. exit(generic_error("invalid type for '<'", psave, lno));
  247. }
  248. _skip_spaces(p);
  249. /* try to getword */
  250. getword(p, &type, &out);
  251. if (type == WORDTYPE_KWORD && out == '@')
  252. goto skip;
  253. /* can just be a function define */
  254. guess = FUNCTION;
  255. if (type == WORDTYPE_NR) {
  256. /* only number of arguments */
  257. if (!out)
  258. /* special case */
  259. n->value = DECLARE_NOARG;
  260. else
  261. n->value = out;
  262. _skip_word(p);
  263. _skip_spaces(p);
  264. goto skip;
  265. }
  266. /* argument list */
  267. if (type == WORDTYPE_KWORD && out == '(') {
  268. n->value = 0;
  269. p++;
  270. _skip_spaces(p);
  271. for (;;) {
  272. if (unlikely(!getword(p, &type, &out)))
  273. /* end but no ')' */
  274. exit(generic_error("argument list terminated with no ')'", psave, lno));
  275. switch (type) {
  276. case WORDTYPE_KWORD:
  277. if (unlikely(out != ')' && out != ','))
  278. exit(generic_error("invalid operators between arguments", psave, lno));
  279. if (out == ')') {
  280. if (!n->value)
  281. n->value = DECLARE_NOARG;
  282. p++;
  283. _skip_spaces(p);
  284. goto skip;
  285. }
  286. p++;
  287. _skip_spaces(p);
  288. break;
  289. case WORDTYPE_NAME:
  290. n->value++;
  291. p += out;
  292. _skip_spaces(p);
  293. break;
  294. default:
  295. exit(generic_error("invalid type between arguments", psave, lno));
  296. }
  297. }
  298. }
  299. if (!n->value)
  300. n->value = DECLARE_NOARG;
  301. skip: attr = getattrmask(p, declare_attrtab);
  302. if (unlikely(!attr && guess != SHARED))
  303. exit(generic_error("you must give attributes for declares", psave, lno));
  304. if (attr & 2) {
  305. n->value |= DECLARE_FN;
  306. n->xmask = XMASK_FN;
  307. }
  308. else if (attr & 4) {
  309. if (unlikely(guess == FUNCTION))
  310. exit(generic_error("you gave us a function declare but told us this is a variable declare? ",
  311. psave, lno));
  312. /* variable */
  313. n->value = DECLARE_VAR;
  314. }
  315. else
  316. exit(generic_error("one of 'fn' or 'var' must be gave", psave, lno));
  317. if (unlikely((t = find_name(n->payload))) && !(t->xmask & XMASK_WEAK)) {
  318. error((char *) n->payload);
  319. exit(generic_error("name already defined", psave, lno));
  320. }
  321. if (t && t->xmask == XMASK_WEAK)
  322. if (((t->value & DECLARE_FN) && !(n->value & DECLARE_FN)) ||
  323. ((n->value & DECLARE_FN) && !(t->value & DECLARE_FN))) {
  324. error((char *) n->payload);
  325. exit(generic_error("name defined in different type", psave, lno));
  326. }
  327. n->xmask |= XMASK_WEAK;
  328. klist_add(&names_list, n);
  329. return 0;
  330. }
  331. static int iini_run(char *interp_path, char *code) {
  332. int pid, stat;
  333. /* we use the command-line '-c' */
  334. fprintf(stderr, "\nRun %s as interpreter-in-interpreter: \n", interp_path);
  335. if (!(pid = fork())) {
  336. /* child */
  337. #if CONFIG_NODISPLAY_IINI
  338. int fd = open("/dev/null", O_WRONLY);
  339. dup2(fd, 1);
  340. dup2(fd, 2);
  341. #endif
  342. execl(interp_path, interp_path, "-c", code, NULL);
  343. error("execl: %s\n", strerror(errno));
  344. exit(generic_error("interp-in-interp failed", psave, lno));
  345. }
  346. waitpid(pid, &stat, 0);
  347. if (WIFEXITED(stat))
  348. fprintf(stderr, "\n%s exited normally with status %d\n", interp_path, WEXITSTATUS(stat));
  349. else if (WIFSIGNALED(stat)) {
  350. error("%s terminated by signal %d%s\n", interp_path, WTERMSIG(stat),
  351. #ifdef WCOREDUMP
  352. WCOREDUMP(stat) ? " (core dumped)" : ""
  353. #else
  354. ""
  355. #endif
  356. );
  357. exit(generic_error("interp-in-interp failed", psave, lno));
  358. }
  359. return WEXITSTATUS(stat);
  360. }
  361. static int interp_in_interp(char *p, struct arch *arch_fn, long *res) {
  362. int type, out;
  363. char *interp, *code, *end;
  364. /* first get the interpreter name */
  365. p++;
  366. _skip_spaces(p);
  367. if (!getword(p, &type, &out))
  368. exit(generic_error("interpreter name required", psave, lno));
  369. interp = strndup(p + 1, out);
  370. p += out + 2;
  371. _skip_spaces(p);
  372. if (!getword(p, &type, &out) || out != '{')
  373. exit(generic_error("start mark '{' required", psave, lno));
  374. /* get the end mark '}' */
  375. end = read_code_block(p);
  376. _skip_spaces(end);
  377. /* ; -> next_line */
  378. end++;
  379. p++;
  380. code = strndup(p, end - p - 1);
  381. /* now let's do something interesting */
  382. *res = iini_run(interp, code);
  383. free(code);
  384. free(interp);
  385. _skip_spaces(end);
  386. /* this, parse_line will not bark. */
  387. line_buffer = end;
  388. return 1;
  389. }