tripl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. static char *__skip_comment(char *fbuffer) {
  80. if (*fbuffer != '#')
  81. return fbuffer;
  82. while (*fbuffer != '\n')
  83. fbuffer++;
  84. _skip_spaces(fbuffer);
  85. return fbuffer;
  86. }
  87. /* move line pointer to next line */
  88. static int next_line(char *fbuffer) {
  89. char *p;
  90. ++lno;
  91. if (!line_buffer) {
  92. p = line_eof = line_buffer = fbuffer = __skip_comment(fbuffer);
  93. if (fbuffer >= eof)
  94. return 0;
  95. while (*p != ';')
  96. line_eof++, p++;
  97. linebuffer_save = line_buffer;
  98. return 1;
  99. }
  100. fbuffer = __skip_comment(fbuffer);
  101. for (; (*line_buffer != ';') && (line_buffer < eof); ++line_buffer);
  102. if (line_buffer >= eof)
  103. return 0;
  104. p = ++line_buffer;
  105. while (*p != ';' && p < eof)
  106. p++, line_eof++;
  107. if (p == eof)
  108. /* eof */
  109. return 0;
  110. linebuffer_save = line_buffer;
  111. return line_eof++, 1;
  112. }
  113. static int getnumber(char *p) {
  114. int dec = 0, log = 1, neg = 0;
  115. if (!isnr(*p)) {
  116. if (*p == '+')
  117. neg = 0;
  118. else if (*p == '-')
  119. neg = 1;
  120. else
  121. exit(generic_error("a dec number required, but: ", linebuffer_save, lno));
  122. p++;
  123. }
  124. if (*p == '0' && isnr(p[1]))
  125. exit(generic_error("number starting with '0'", linebuffer_save, lno));
  126. /* never use eof because this is calling from getword() */
  127. while (isnr(*p)) p++;
  128. --p;
  129. while (p >= line_buffer) {
  130. dec += itoa(*p) * log;
  131. log *= 10;
  132. p--;
  133. if (!isnr(*p))
  134. /* for - and + */
  135. break;
  136. }
  137. return neg ? -dec : dec;
  138. }
  139. #define isfirstchr() (!i)
  140. #define notfirstchr() i
  141. char *getword(char *fbuffer, int *type, int *out) {
  142. *type = WORDTYPE_INVAL;
  143. char *p;
  144. int i, in = 0;
  145. _skip_spaces(fbuffer);
  146. for (i = 0, p = fbuffer; p < line_eof; i++, p++) {
  147. if (*p == '\"') {
  148. if (in) {
  149. /* end */
  150. *out = i - 1;
  151. return fbuffer;
  152. }
  153. in = 1;
  154. *type = WORDTYPE_STR;
  155. }
  156. if (!in) {
  157. if (isnr(*p) || *p == '-' || *p == '+') {
  158. if (notfirstchr())
  159. /* name, but not a number */
  160. continue;
  161. /* get the number, and save to *out */
  162. *out = getnumber(p);
  163. /* line finished. */
  164. *type = WORDTYPE_NR;
  165. return p;
  166. }
  167. /* eg. "# " */
  168. if (i == 1 && isspace(*p)) {
  169. if (ischr(p[-1])) {
  170. /* name */
  171. *type = WORDTYPE_NAME;
  172. *out = 1;
  173. return --p;
  174. }
  175. /* keyword */
  176. *type = WORDTYPE_KWORD;
  177. *out = *--p;
  178. return p;
  179. }
  180. /* it can only be a name */
  181. if (isspace(*p)) {
  182. /* end */
  183. *type = WORDTYPE_NAME;
  184. *out = i;
  185. return fbuffer;
  186. }
  187. }
  188. }
  189. /* there isn't a space at end of the line */
  190. if (in)
  191. exit(generic_error("no '\"' found", linebuffer_save, lno));
  192. if (i == 1) {
  193. if (ischr(p[-1])) {
  194. *type = WORDTYPE_NAME;
  195. *out = 1;
  196. return p;
  197. }
  198. *type = WORDTYPE_KWORD;
  199. *out = *--p;
  200. return p;
  201. }
  202. if (!i)
  203. return NULL;
  204. /* only can be a name */
  205. *type = WORDTYPE_NAME;
  206. *out = i;
  207. return fbuffer;
  208. }
  209. static inline int find_attr(char *attrname, const struct attr_table *tab) {
  210. int i;
  211. for (i = 0; tab[i].attrname; i++)
  212. if (!strcmp(tab[i].attrname, attrname))
  213. return tab[i].attrmask;
  214. return 0;
  215. }
  216. int getattrmask(char *p, const struct attr_table *tab) {
  217. char *attrname;
  218. int type, out, mask = 0, t;
  219. if (!getword(p, &type, &out))
  220. return 0;
  221. if (type != WORDTYPE_KWORD)
  222. exit(generic_error("non-attribute ('@') character after that", linebuffer_save, lno));
  223. if (out != '@')
  224. exit(generic_error("not a '@' after string", linebuffer_save, lno));
  225. ++p;
  226. _skip_spaces(p);
  227. while (getword(p, &type, &out)) {
  228. if (type != WORDTYPE_NAME)
  229. exit(generic_error("an attribute name required", linebuffer_save, lno));
  230. attrname = strndup(p, out);
  231. if (!(t = find_attr(attrname, tab)))
  232. exit(generic_error("unknow attribute", linebuffer_save, lno));
  233. mask |= t;
  234. free(attrname);
  235. _skip_word(p);
  236. }
  237. return mask;
  238. }
  239. static int parse_line(void) {
  240. char *p;
  241. int type, out;
  242. if ((p = getword(line_buffer, &type, &out)) >= line_eof)
  243. /* empty line */
  244. return 0;
  245. if (type != WORDTYPE_KWORD && type != WORDTYPE_NAME)
  246. exit(generic_error("line starts with a non-keyword", linebuffer_save, lno));
  247. return do_keyword(p, *p);
  248. }