arch_templ.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <sys/types.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <klist.h>
  24. #include <tripl/tripl.h>
  25. #include <tripl/limits.h>
  26. #include <tripl/arch.h>
  27. char *psave;
  28. __arch_templ static int new_var(char *p, struct arch *);
  29. __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
  30. psave = p;
  31. switch (*p) {
  32. case '$':
  33. /* '$ asdf = 0;' --- asdf = 0
  34. * '$ asdf 0;' --- asdf = 0
  35. * '$ asdf;' --- asdf = 0
  36. * */
  37. p++;
  38. _skip_spaces(p);
  39. new_var(p, arch_fn);
  40. break;
  41. case '!':
  42. if (!arch_fn->arch_stack_dump)
  43. exit(generic_error("unsupported 'stack-dump' in current mode", psave, lno));
  44. arch_fn->arch_stack_dump();
  45. break;
  46. default:
  47. exit(generic_error("unknown keyword", psave, lno));
  48. break;
  49. }
  50. return 0;
  51. }
  52. __arch_templ static int new_var(char *p, struct arch *arch_fn) {
  53. char *name;
  54. int type, out, attr;
  55. getword(p, &type, &out);
  56. if (type != WORDTYPE_NAME)
  57. exit(generic_error("a name for variable required", psave, lno));
  58. name = strndup(p, out);
  59. p += out;
  60. _skip_spaces(p);
  61. getword(p, &type, &out);
  62. switch (type) {
  63. case WORDTYPE_KWORD:
  64. if (out != '=')
  65. exit(generic_error("keyword but it's not a '='", psave, lno));
  66. ++p;
  67. _skip_spaces(p);
  68. getword(p, &type, &out);
  69. if (type != WORDTYPE_NR)
  70. exit(generic_error("a number required", psave, lno));
  71. _skip_word(p);
  72. break;
  73. case WORDTYPE_NR:
  74. /* to attribute (if have) */
  75. _skip_word(p);
  76. break;
  77. case 0:
  78. out = 0;
  79. break;
  80. default:
  81. exit(generic_error("bad word after variable name", psave, lno));
  82. }
  83. attr = getattrmask(p, vardef_attrtab);
  84. arch_fn->arch_new_var(name, out, attr);
  85. /* the name can't be free */
  86. return 0;
  87. }