arch_templ.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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, comma = 0;
  95. struct klist_node *t;
  96. while (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 (out == ',') {
  106. comma = 1;
  107. p++;
  108. _skip_spaces(p);
  109. break;
  110. }
  111. if (out == '@') {
  112. out = 0;
  113. break;
  114. }
  115. if (unlikely(out != '=' ))
  116. exit(generic_error("keyword but it's not a '='", psave, lno));
  117. ++p;
  118. _skip_spaces(p);
  119. getword(p, &type, &out);
  120. if (unlikely(type != WORDTYPE_NR))
  121. exit(generic_error("a number required", psave, lno));
  122. _skip_word(p);
  123. break;
  124. case WORDTYPE_NR:
  125. /* to attribute (if have) */
  126. _skip_word(p);
  127. break;
  128. case 0:
  129. out = 0;
  130. break;
  131. default:
  132. exit(generic_error("bad word after variable name", psave, lno));
  133. }
  134. if (!comma)
  135. attr = getattrmask(p, vardef_attrtab);
  136. if (unlikely((t = find_name(name)) && !(t->xmask & XMASK_WEAK))) {
  137. error(name);
  138. exit(generic_error("name already defined", psave, lno));
  139. }
  140. arch_fn->arch_new_var(name, out, attr);
  141. if (!comma)
  142. break;
  143. comma = 0;
  144. _skip_spaces(p);
  145. }
  146. return 0;
  147. }
  148. static int getrightvalue(char *p, struct arch *arch_fn) {
  149. long res;
  150. if (*p == '`') {
  151. if (!interp_in_interp(p, arch_fn, &res))
  152. /* actually, it never be here */
  153. exit(generic_error("invalid right-value", psave, lno));
  154. }
  155. else if (!set_name(p, arch_fn, &res)
  156. && !math_set_name(p, arch_fn, &res))
  157. exit(generic_error("invalid right-value", psave, lno));
  158. return res;
  159. }
  160. static int set_name(char *p, struct arch *arch_fn, long *res) {
  161. char *name;
  162. int type, out;
  163. getword(p, &type, &out);
  164. if (type != WORDTYPE_NAME)
  165. return 0;
  166. name = strndup(p, out);
  167. p += out;
  168. _skip_spaces(p);
  169. getword(p, &type, &out);
  170. if (type != WORDTYPE_KWORD || out != '=')
  171. return 0;
  172. p++;
  173. _skip_spaces(p);
  174. getword(p, &type, &out);
  175. if (type == WORDTYPE_KWORD && out == '$') {
  176. out = arch_fn->arch_math_expr(p, name);
  177. goto skip;
  178. }
  179. else if (type != WORDTYPE_NR)
  180. out = getrightvalue(p, arch_fn);
  181. arch_fn->arch_set_var(name, out, 0);
  182. skip: *res = out;
  183. free(name);
  184. return 1;
  185. }
  186. /* 9 -> a; and warning that math_set_name doesn't support instructions
  187. * such as 'a = 0 -> b' */
  188. static int math_set_name(char *p, struct arch *arch_fn, long *res) {
  189. char *name;
  190. int type, out, nr;
  191. getword(p, &type, &out);
  192. if (type != WORDTYPE_NR)
  193. return 0;
  194. nr = out;
  195. /* this requires a space */
  196. _skip_word(p);
  197. getword(p, &type, &out);
  198. if (unlikely(type != WORDTYPE_KWORD || out != '-'))
  199. exit(generic_error("a '->' required", psave, lno));
  200. p++;
  201. getword(p, &type, &out);
  202. if (unlikely(type != WORDTYPE_KWORD || out != '>'))
  203. exit(generic_error("a '->' required", psave, lno));
  204. p++;
  205. _skip_spaces(p);
  206. getword(p, &type, &out);
  207. if (unlikely(type != WORDTYPE_NAME))
  208. exit(generic_error("a name required", psave, lno));
  209. name = strndup(p, out);
  210. arch_fn->arch_set_var(name, nr, 0);
  211. *res = nr;
  212. free(name);
  213. return 1;
  214. }
  215. /*
  216. * 1.
  217. * < "libfoo.a" @ dynalib;
  218. * 2.
  219. * < fn @ fn;
  220. *
  221. * 3.
  222. * < fn 2 @ fn;
  223. *
  224. * 4.
  225. * < fn (a, b) @ fn;
  226. *
  227. * 5.
  228. * < age @ var;
  229. * */
  230. static int declare_object(char *p, struct arch *arch_fn) {
  231. char *name;
  232. int type, out, attr;
  233. enum { SHARED, NAMES, FUNCTION } guess;
  234. struct klist_node *n, *t;
  235. /* skip '<' */
  236. p++;
  237. _skip_spaces(p);
  238. if (unlikely(!getword(p, &type, &out)))
  239. exit(generic_error("a module name or object name required", psave, lno));
  240. switch (type) {
  241. case WORDTYPE_STR:
  242. /* it must be a dynamic library */
  243. name = strndup(p + 1, out);
  244. guess = SHARED;
  245. if (arch_fn->arch_import_shared)
  246. arch_fn->arch_import_shared(name);
  247. p += out + 2;
  248. /* we will not free the name because interp_libs_dump uses it */
  249. return 0;
  250. case WORDTYPE_NAME:
  251. /* functions or variables */
  252. name = strndup(p, out);
  253. /* join to the name list */
  254. n = malloc(sizeof(struct klist_node));
  255. klist_init(n);
  256. n->payload = name;
  257. guess = NAMES;
  258. p += out;
  259. break;
  260. default:
  261. exit(generic_error("invalid type for '<'", psave, lno));
  262. }
  263. _skip_spaces(p);
  264. /* try to getword */
  265. getword(p, &type, &out);
  266. if (type == WORDTYPE_KWORD && out == '@')
  267. goto skip;
  268. /* can just be a function define */
  269. guess = FUNCTION;
  270. if (type == WORDTYPE_NR) {
  271. /* only number of arguments */
  272. if (!out)
  273. /* special case */
  274. n->value = DECLARE_NOARG;
  275. else
  276. n->value = out;
  277. _skip_word(p);
  278. _skip_spaces(p);
  279. goto skip;
  280. }
  281. /* argument list */
  282. if (type == WORDTYPE_KWORD && out == '(') {
  283. n->value = 0;
  284. p++;
  285. _skip_spaces(p);
  286. for (;;) {
  287. if (unlikely(!getword(p, &type, &out)))
  288. /* end but no ')' */
  289. exit(generic_error("argument list terminated with no ')'", psave, lno));
  290. switch (type) {
  291. case WORDTYPE_KWORD:
  292. if (unlikely(out != ')' && out != ','))
  293. exit(generic_error("invalid operators between arguments", psave, lno));
  294. if (out == ')') {
  295. if (!n->value)
  296. n->value = DECLARE_NOARG;
  297. p++;
  298. _skip_spaces(p);
  299. goto skip;
  300. }
  301. p++;
  302. _skip_spaces(p);
  303. break;
  304. case WORDTYPE_NAME:
  305. n->value++;
  306. p += out;
  307. _skip_spaces(p);
  308. break;
  309. default:
  310. exit(generic_error("invalid type between arguments", psave, lno));
  311. }
  312. }
  313. }
  314. if (!n->value)
  315. n->value = DECLARE_NOARG;
  316. skip: attr = getattrmask(p, declare_attrtab);
  317. if (unlikely(!attr && guess != SHARED))
  318. exit(generic_error("you must give attributes for declares", psave, lno));
  319. if (attr & 2) {
  320. n->value |= DECLARE_FN;
  321. n->xmask = XMASK_FN;
  322. }
  323. else if (attr & 4) {
  324. if (unlikely(guess == FUNCTION))
  325. exit(generic_error("you gave us a function declare but told us this is a variable declare? ",
  326. psave, lno));
  327. /* variable */
  328. n->value = DECLARE_VAR;
  329. }
  330. else
  331. exit(generic_error("one of 'fn' or 'var' must be gave", psave, lno));
  332. if (unlikely((t = find_name(n->payload))) && !(t->xmask & XMASK_WEAK)) {
  333. error((char *) n->payload);
  334. exit(generic_error("name already defined", psave, lno));
  335. }
  336. if (t && t->xmask == XMASK_WEAK)
  337. if (((t->value & DECLARE_FN) && !(n->value & DECLARE_FN)) ||
  338. ((n->value & DECLARE_FN) && !(t->value & DECLARE_FN))) {
  339. error((char *) n->payload);
  340. exit(generic_error("name defined in different type", psave, lno));
  341. }
  342. n->xmask |= XMASK_WEAK;
  343. klist_add(&names_list, n);
  344. return 0;
  345. }
  346. static int iini_run(char *interp_path, char *code) {
  347. int pid, stat;
  348. /* we use the command-line '-c' */
  349. fprintf(stderr, "\nRun %s as interpreter-in-interpreter: \n", interp_path);
  350. if (!(pid = fork())) {
  351. /* child */
  352. #if CONFIG_NODISPLAY_IINI
  353. int fd = open("/dev/null", O_WRONLY);
  354. dup2(fd, 1);
  355. dup2(fd, 2);
  356. #endif
  357. execl(interp_path, interp_path, "-c", code, NULL);
  358. error("execl: %s\n", strerror(errno));
  359. exit(generic_error("interp-in-interp failed", psave, lno));
  360. }
  361. waitpid(pid, &stat, 0);
  362. if (WIFEXITED(stat))
  363. fprintf(stderr, "\n%s exited normally with status %d\n", interp_path, WEXITSTATUS(stat));
  364. else if (WIFSIGNALED(stat)) {
  365. error("%s terminated by signal %d%s\n", interp_path, WTERMSIG(stat),
  366. #ifdef WCOREDUMP
  367. WCOREDUMP(stat) ? " (core dumped)" : ""
  368. #else
  369. ""
  370. #endif
  371. );
  372. exit(generic_error("interp-in-interp failed", psave, lno));
  373. }
  374. return WEXITSTATUS(stat);
  375. }
  376. static int interp_in_interp(char *p, struct arch *arch_fn, long *res) {
  377. int type, out;
  378. char *interp, *code, *end;
  379. /* first get the interpreter name */
  380. p++;
  381. _skip_spaces(p);
  382. if (!getword(p, &type, &out))
  383. exit(generic_error("interpreter name required", psave, lno));
  384. interp = strndup(p + 1, out);
  385. p += out + 2;
  386. _skip_spaces(p);
  387. if (!getword(p, &type, &out) || out != '{')
  388. exit(generic_error("start mark '{' required", psave, lno));
  389. /* get the end mark '}' */
  390. end = read_code_block(p);
  391. _skip_spaces(end);
  392. /* ; -> next_line */
  393. end++;
  394. p++;
  395. code = strndup(p, end - p - 1);
  396. /* now let's do something interesting */
  397. *res = iini_run(interp, code);
  398. free(code);
  399. free(interp);
  400. _skip_spaces(end);
  401. /* this, parse_line will not bark. */
  402. line_buffer = end;
  403. return 1;
  404. }