tripl.c 5.8 KB

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