tripl.c 6.8 KB

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