blood-elf.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* -*- c-file-style: "linux";indent-tabs-mode:t -*- */
  2. /* Copyright (C) 2017 Jeremiah Orians
  3. * Copyright (C) 2017 Jan Nieuwenhuizen <janneke@gnu.org>
  4. * This file is part of mescc-tools
  5. *
  6. * mescc-tools is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * mescc-tools is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with mescc-tools. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <getopt.h>
  23. #include <unistd.h>
  24. #include <sys/stat.h>
  25. #define max_string 4096
  26. //CONSTANT max_string 4096
  27. #define TRUE 1
  28. //CONSTANT TRUE 1
  29. #define FALSE 0
  30. //CONSTANT FALSE 0
  31. #define FALSE 0
  32. // CONSTANT FALSE 0
  33. #define TRUE 1
  34. // CONSTANT TRUE 1
  35. int match(char* a, char* b)
  36. {
  37. int i = -1;
  38. do
  39. {
  40. i = i + 1;
  41. if(a[i] != b[i])
  42. {
  43. return FALSE;
  44. }
  45. } while((0 != a[i]) && (0 !=b[i]));
  46. return TRUE;
  47. }
  48. struct entry
  49. {
  50. struct entry* next;
  51. char* name;
  52. };
  53. FILE* output;
  54. struct entry* jump_table;
  55. void consume_token(FILE* source_file, char* s)
  56. {
  57. int i = 0;
  58. int c = fgetc(source_file);
  59. do
  60. {
  61. s[i] = c;
  62. i = i + 1;
  63. c = fgetc(source_file);
  64. } while((' ' != c) && ('\t' != c) && ('\n' != c) && '>' != c);
  65. }
  66. void storeLabel(FILE* source_file)
  67. {
  68. struct entry* entry = calloc(1, sizeof(struct entry));
  69. /* Prepend to list */
  70. entry->next = jump_table;
  71. jump_table = entry;
  72. /* Store string */
  73. entry->name = calloc((max_string + 1), sizeof(char));
  74. consume_token(source_file, entry->name);
  75. /* Remove all entries that start with the forbidden char pattern :_ */
  76. if('_' == entry->name[0])
  77. {
  78. jump_table = jump_table->next;
  79. }
  80. }
  81. void line_Comment(FILE* source_file)
  82. {
  83. int c = fgetc(source_file);
  84. while((10 != c) && (13 != c))
  85. {
  86. c = fgetc(source_file);
  87. }
  88. }
  89. void purge_string(FILE* source_file)
  90. {
  91. int c = fgetc(source_file);
  92. while((EOF != c) && (34 != c))
  93. {
  94. c = fgetc(source_file);
  95. }
  96. }
  97. void first_pass(struct entry* input)
  98. {
  99. if(NULL == input) return;
  100. first_pass(input->next);
  101. FILE* source_file = fopen(input->name, "r");
  102. if(NULL == source_file)
  103. {
  104. fputs("The file: ", stderr);
  105. fputs(input->name, stderr);
  106. fputs(" can not be opened!\n", stderr);
  107. exit(EXIT_FAILURE);
  108. }
  109. int c;
  110. for(c = fgetc(source_file); EOF != c; c = fgetc(source_file))
  111. {
  112. /* Check for and deal with label */
  113. if(58 == c)
  114. {
  115. storeLabel(source_file);
  116. }
  117. /* Check for and deal with line comments */
  118. else if (c == '#' || c == ';')
  119. {
  120. line_Comment(source_file);
  121. }
  122. else if (34 == c)
  123. {
  124. purge_string(source_file);
  125. }
  126. }
  127. fclose(source_file);
  128. }
  129. void output_debug(struct entry* node, int stage)
  130. {
  131. struct entry* i;
  132. for(i = node; NULL != i; i = i->next)
  133. {
  134. if(stage)
  135. {
  136. fputs(":ELF_str_", output);
  137. fputs(i->name, output);
  138. fputs("\n\x22", output);
  139. fputs(i->name, output);
  140. fputs("\x22\n", output);
  141. }
  142. else
  143. {
  144. fputs("%ELF_str_", output);
  145. fputs(i->name, output);
  146. fputs(">ELF_str\n&", output);
  147. fputs(i->name, output);
  148. fputs("\n%10000\n!2\n!0\n@1\n", output);
  149. }
  150. }
  151. }
  152. struct entry* reverse_list(struct entry* head)
  153. {
  154. struct entry* root = NULL;
  155. struct entry* next;
  156. while(NULL != head)
  157. {
  158. next = head->next;
  159. head->next = root;
  160. root = head;
  161. head = next;
  162. }
  163. return root;
  164. }
  165. /* Standard C main program */
  166. int main(int argc, char **argv)
  167. {
  168. jump_table = NULL;
  169. struct entry* input = NULL;
  170. output = stdout;
  171. char* output_file = "";
  172. struct entry* temp;
  173. int option_index = 1;
  174. while(option_index <= argc)
  175. {
  176. if(NULL == argv[option_index])
  177. {
  178. option_index = option_index + 1;
  179. }
  180. else if(match(argv[option_index], "-h") || match(argv[option_index], "--help"))
  181. {
  182. fputs("Usage: ", stderr);
  183. fputs(argv[0], stderr);
  184. fputs(" -f FILENAME1 {-f FILENAME2}\n", stderr);
  185. exit(EXIT_SUCCESS);
  186. }
  187. else if(match(argv[option_index], "-f") || match(argv[option_index], "--file"))
  188. {
  189. temp = calloc(1, sizeof(struct entry));
  190. temp->name = argv[option_index + 1];
  191. temp->next = input;
  192. input = temp;
  193. option_index = option_index + 2;
  194. }
  195. else if(match(argv[option_index], "-o") || match(argv[option_index], "--output"))
  196. {
  197. output_file = argv[option_index + 1];
  198. output = fopen(output_file, "w");
  199. if(NULL == output)
  200. {
  201. fputs("The file: ", stderr);
  202. fputs(input->name, stderr);
  203. fputs(" can not be opened!\n", stderr);
  204. exit(EXIT_FAILURE);
  205. }
  206. option_index = option_index + 2;
  207. }
  208. else if(match(argv[option_index], "-V") || match(argv[option_index], "--version"))
  209. {
  210. fputs("blood-elf 0.1\n(Basically Launches Odd Object Dump ExecutabLe Files\n", stdout);
  211. exit(EXIT_SUCCESS);
  212. }
  213. else
  214. {
  215. fputs("Unknown option\n", stderr);
  216. exit(EXIT_FAILURE);
  217. }
  218. }
  219. /* Make sure we have a program tape to run */
  220. if (NULL == input)
  221. {
  222. return EXIT_FAILURE;
  223. }
  224. /* Get all of the labels */
  225. first_pass(input);
  226. /* Reverse their order */
  227. jump_table = reverse_list(jump_table);
  228. fputs(":ELF_str\n!0\n", output);
  229. output_debug(jump_table, TRUE);
  230. fputs("%0\n:ELF_sym\n%0\n%0\n%0\n!0\n!0\n@1\n", output);
  231. output_debug(jump_table, FALSE);
  232. fputs("\n:ELF_end\n", output);
  233. if (output != stdout) {
  234. fclose(output);
  235. }
  236. return EXIT_SUCCESS;
  237. }