tripl.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* tripl/tripl.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 <unistd.h>
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #include <string.h>
  23. #include <klist.h>
  24. #include <tripl/tripl.h>
  25. #include <tripl/limits.h>
  26. #include <tripl/arch.h>
  27. char *linebuffer_save = NULL;
  28. char *line_buffer = NULL;
  29. int lno = 0;
  30. char *eof;
  31. char *line_eof;
  32. static int next_line(char *),
  33. parse_line(void);
  34. struct arch *arch_fn;
  35. extern struct arch interp_arch_fn,
  36. x86_64_arch_fn;
  37. struct context *current;
  38. struct context main_context = {
  39. .funcname = "main",
  40. .link = NULL,
  41. .private_data = NULL
  42. };
  43. const static struct {
  44. char *name;
  45. struct arch *arch;
  46. } arch_list[] = {
  47. { "interp", &interp_arch_fn},
  48. { "x86_64", &x86_64_arch_fn},
  49. { NULL, NULL }
  50. };
  51. const struct attr_table vardef_attrtab[] = {
  52. { "global", 1 },
  53. { "rdonly", 4 },
  54. { "ptr", 8 },
  55. { NULL, 0 }
  56. };
  57. int do_tripl_compile(char *fbuffer, off_t fsize) {
  58. char *archname = getenv("TRIPL_ARCH");
  59. int i;
  60. current = &main_context;
  61. if (!archname)
  62. arch_fn = &interp_arch_fn;
  63. else {
  64. for (i = 0; arch_list[i].name; i++)
  65. if (!strcmp(arch_list[i].name, archname)) {
  66. arch_fn = arch_list[i].arch;
  67. goto ok;
  68. }
  69. fprintf(stderr, "*** unsupported arch %s (got from $TRIPL_ARCH). \n", archname);
  70. return 1;
  71. }
  72. ok: klist_init(&stack);
  73. eof = fbuffer + fsize;
  74. arch_fn->arch_init();
  75. while (next_line(fbuffer))
  76. parse_line();
  77. arch_fn->arch_fin ? arch_fn->arch_fin() : 0;
  78. return 0;
  79. }
  80. /* skip the comment, from '# asdf\nasdf2' to 'asdf2' */
  81. static char *__skip_comment(char *fbuffer) {
  82. if (*fbuffer != '#')
  83. return fbuffer;
  84. while (*fbuffer != '\n')
  85. fbuffer++;
  86. _skip_spaces(fbuffer);
  87. if (*fbuffer == '#')
  88. return __skip_comment(fbuffer);
  89. return fbuffer;
  90. }
  91. static int next_line(char *fbuffer) {
  92. char *p;
  93. ++lno;
  94. /* first time we call this, initial */
  95. if (!line_buffer) {
  96. p = line_eof = line_buffer = fbuffer = __skip_comment(fbuffer);
  97. if (fbuffer >= eof)
  98. /* only comment in this file */
  99. return 0;
  100. /* got line_eof */
  101. while (*p != ';')
  102. p++, line_eof++;
  103. linebuffer_save = line_buffer;
  104. return 1;
  105. }
  106. /* move line_buffer to ';' + 1 and line_eof to line_buffer -> ';' */
  107. line_buffer = fbuffer = __skip_comment(line_buffer);
  108. if (fbuffer >= eof)
  109. return 0;
  110. while (*line_buffer != ';')
  111. line_buffer++;
  112. line_buffer++;
  113. /* \n */
  114. _skip_spaces(line_buffer)
  115. line_buffer = __skip_comment(line_buffer);
  116. line_eof = line_buffer;
  117. while (*line_eof != ';')
  118. line_eof++;
  119. linebuffer_save = line_buffer;
  120. if (line_buffer >= eof)
  121. return 0;
  122. return 1;
  123. }
  124. static int getnumber(char *p) {
  125. int dec = 0, log = 1, neg = 0;
  126. if (!isnr(*p)) {
  127. if (*p == '+')
  128. neg = 0;
  129. else if (*p == '-')
  130. neg = 1;
  131. else
  132. exit(generic_error("a dec number required, but: ", linebuffer_save, lno));
  133. p++;
  134. }
  135. if (*p == '0' && isnr(p[1]))
  136. exit(generic_error("number starting with '0'", linebuffer_save, lno));
  137. /* never use eof because this is calling from getword() */
  138. while (isnr(*p)) p++;
  139. --p;
  140. while (p >= line_buffer) {
  141. dec += itoa(*p) * log;
  142. log *= 10;
  143. p--;
  144. if (!isnr(*p))
  145. /* for - and + */
  146. break;
  147. }
  148. return neg ? -dec : dec;
  149. }
  150. /* NEVER MODIFY THIS! MAGIC!
  151. * THIS IS REALLY THE UGLY CODE, MOST UGLY CODE ALL OVER THE WORLD.
  152. *
  153. * BUT IT WORKS NOW, SO WE DO NOT TO CHANGE THIS AND WE WILL CHANGE
  154. * A LOT BECAUSE ALL OF THE TRIPL REQUIRES THIS FUNCTION.
  155. * */
  156. #define isfirstchr() (!i)
  157. #define notfirstchr() i
  158. char *getword(char *fbuffer, int *type, int *out) {
  159. *type = WORDTYPE_INVAL;
  160. char *p;
  161. int i, in = 0;
  162. _skip_spaces(fbuffer);
  163. for (i = 0, p = fbuffer; p < line_eof; i++, p++) {
  164. if (*p == '\"') {
  165. if (in) {
  166. /* end */
  167. *out = i - 1;
  168. return fbuffer;
  169. }
  170. in = 1;
  171. *type = WORDTYPE_STR;
  172. }
  173. if (!in) {
  174. if (isnr(*p) || *p == '-' || *p == '+') {
  175. if (notfirstchr())
  176. /* name, but not a number */
  177. continue;
  178. /* get the number, and save to *out */
  179. *out = getnumber(p);
  180. /* line finished. */
  181. *type = WORDTYPE_NR;
  182. return p;
  183. }
  184. if (i == 1 && isspace(*p)) {
  185. if (ischr(p[-1])) {
  186. /* name */
  187. *type = WORDTYPE_NAME;
  188. *out = 1;
  189. return --p;
  190. }
  191. /* keyword */
  192. *type = WORDTYPE_KWORD;
  193. *out = *--p;
  194. return p;
  195. }
  196. /* it can only be a name */
  197. if (isspace(*p)) {
  198. /* end */
  199. *type = WORDTYPE_NAME;
  200. *out = i;
  201. return fbuffer;
  202. }
  203. if (!ischr(*p)) {
  204. if (!i) {
  205. *type = WORDTYPE_KWORD;
  206. *out = *p;
  207. return fbuffer;
  208. }
  209. if (ischr(p[-1])) {
  210. *type = WORDTYPE_NAME;
  211. *out = i;
  212. return p - i;
  213. }
  214. }
  215. }
  216. }
  217. /* there isn't a space at end of the line */
  218. if (in)
  219. exit(generic_error("no '\"' found", linebuffer_save, lno));
  220. if (i == 1) {
  221. if (ischr(p[-1])) {
  222. *type = WORDTYPE_NAME;
  223. *out = 1;
  224. return p;
  225. }
  226. *type = WORDTYPE_KWORD;
  227. *out = *--p;
  228. return p;
  229. }
  230. if (!i)
  231. return NULL;
  232. /* only can be a name */
  233. *type = WORDTYPE_NAME;
  234. *out = i;
  235. return fbuffer;
  236. }
  237. static inline int find_attr(char *attrname, const struct attr_table *tab) {
  238. int i;
  239. for (i = 0; tab[i].attrname; i++)
  240. if (!strcmp(tab[i].attrname, attrname))
  241. return tab[i].attrmask;
  242. return 0;
  243. }
  244. int getattrmask(char *p, const struct attr_table *tab) {
  245. char *attrname;
  246. int type, out, mask = 0, t;
  247. if (!getword(p, &type, &out))
  248. return 0;
  249. if (type != WORDTYPE_KWORD)
  250. exit(generic_error("non-attribute ('@') character after that", linebuffer_save, lno));
  251. if (out != '@')
  252. exit(generic_error("not a '@' after string", linebuffer_save, lno));
  253. ++p;
  254. _skip_spaces(p);
  255. while (getword(p, &type, &out)) {
  256. if (type != WORDTYPE_NAME)
  257. exit(generic_error("an attribute name required", linebuffer_save, lno));
  258. attrname = strndup(p, out);
  259. if (!(t = find_attr(attrname, tab)))
  260. exit(generic_error("unknow attribute", linebuffer_save, lno));
  261. mask |= t;
  262. free(attrname);
  263. _skip_word(p);
  264. }
  265. return mask;
  266. }
  267. static int parse_line(void) {
  268. char *p;
  269. int type, out;
  270. if ((p = getword(line_buffer, &type, &out)) >= line_eof)
  271. /* empty line */
  272. return 0;
  273. if (type != WORDTYPE_KWORD && type != WORDTYPE_NAME)
  274. exit(generic_error("line starts with a non-keyword", linebuffer_save, lno));
  275. return arch_do_keyword(p, arch_fn);
  276. }