blood-elf.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. int BITSIZE;
  32. void file_print(char* s, FILE* f);
  33. int match(char* a, char* b);
  34. struct entry
  35. {
  36. struct entry* next;
  37. char* name;
  38. };
  39. FILE* output;
  40. struct entry* jump_table;
  41. void consume_token(FILE* source_file, char* s)
  42. {
  43. int i = 0;
  44. int c = fgetc(source_file);
  45. do
  46. {
  47. s[i] = c;
  48. i = i + 1;
  49. c = fgetc(source_file);
  50. } while((' ' != c) && ('\t' != c) && ('\n' != c) && '>' != c);
  51. }
  52. void storeLabel(FILE* source_file)
  53. {
  54. struct entry* entry = calloc(1, sizeof(struct entry));
  55. /* Prepend to list */
  56. entry->next = jump_table;
  57. jump_table = entry;
  58. /* Store string */
  59. entry->name = calloc((max_string + 1), sizeof(char));
  60. consume_token(source_file, entry->name);
  61. /* Remove all entries that start with the forbidden char pattern :_ */
  62. if('_' == entry->name[0])
  63. {
  64. jump_table = jump_table->next;
  65. }
  66. }
  67. void line_Comment(FILE* source_file)
  68. {
  69. int c = fgetc(source_file);
  70. while(('\n' != c) && ('\r' != c) && (EOF != c))
  71. {
  72. c = fgetc(source_file);
  73. }
  74. }
  75. void purge_string(FILE* source_file)
  76. {
  77. int c = fgetc(source_file);
  78. while((EOF != c) && ('"' != c))
  79. {
  80. c = fgetc(source_file);
  81. }
  82. }
  83. void first_pass(struct entry* input)
  84. {
  85. if(NULL == input) return;
  86. first_pass(input->next);
  87. FILE* source_file = fopen(input->name, "r");
  88. if(NULL == source_file)
  89. {
  90. file_print("The file: ", stderr);
  91. file_print(input->name, stderr);
  92. file_print(" can not be opened!\n", stderr);
  93. exit(EXIT_FAILURE);
  94. }
  95. int c;
  96. for(c = fgetc(source_file); EOF != c; c = fgetc(source_file))
  97. {
  98. /* Check for and deal with label */
  99. if(58 == c)
  100. {
  101. storeLabel(source_file);
  102. }
  103. /* Check for and deal with line comments */
  104. else if (c == '#' || c == ';')
  105. {
  106. line_Comment(source_file);
  107. }
  108. else if ('"' == c)
  109. {
  110. purge_string(source_file);
  111. }
  112. }
  113. fclose(source_file);
  114. }
  115. void output_debug(struct entry* node, int stage)
  116. {
  117. struct entry* i;
  118. for(i = node; NULL != i; i = i->next)
  119. {
  120. if(stage)
  121. {
  122. file_print(":ELF_str_", output);
  123. file_print(i->name, output);
  124. file_print("\n\"", output);
  125. file_print(i->name, output);
  126. file_print("\"\n", output);
  127. }
  128. else if(64 == BITSIZE)
  129. {
  130. file_print("%ELF_str_", output);
  131. file_print(i->name, output);
  132. file_print(">ELF_str\n!2\n!0\n@1\n&", output);
  133. file_print(i->name, output);
  134. file_print(" %0\n%10000\n%0\n", output);
  135. }
  136. else
  137. {
  138. file_print("%ELF_str_", output);
  139. file_print(i->name, output);
  140. file_print(">ELF_str\n&", output);
  141. file_print(i->name, output);
  142. file_print("\n%10000\n!2\n!0\n@1\n", output);
  143. }
  144. }
  145. }
  146. struct entry* reverse_list(struct entry* head)
  147. {
  148. struct entry* root = NULL;
  149. struct entry* next;
  150. while(NULL != head)
  151. {
  152. next = head->next;
  153. head->next = root;
  154. root = head;
  155. head = next;
  156. }
  157. return root;
  158. }
  159. /* Standard C main program */
  160. int main(int argc, char **argv)
  161. {
  162. jump_table = NULL;
  163. struct entry* input = NULL;
  164. output = stdout;
  165. char* output_file = "";
  166. BITSIZE = 32;
  167. int option_index = 1;
  168. while(option_index <= argc)
  169. {
  170. if(NULL == argv[option_index])
  171. {
  172. option_index = option_index + 1;
  173. }
  174. else if(match(argv[option_index], "-h") || match(argv[option_index], "--help"))
  175. {
  176. file_print("Usage: ", stderr);
  177. file_print(argv[0], stderr);
  178. file_print(" --file FILENAME1 {--file FILENAME2} --output FILENAME\n", stderr);
  179. exit(EXIT_SUCCESS);
  180. }
  181. else if(match(argv[option_index], "--64"))
  182. {
  183. BITSIZE = 64;
  184. option_index = option_index + 1;
  185. }
  186. else if(match(argv[option_index], "-f") || match(argv[option_index], "--file"))
  187. {
  188. struct entry* temp = calloc(1, sizeof(struct entry));
  189. temp->name = argv[option_index + 1];
  190. temp->next = input;
  191. input = temp;
  192. option_index = option_index + 2;
  193. }
  194. else if(match(argv[option_index], "-o") || match(argv[option_index], "--output"))
  195. {
  196. output_file = argv[option_index + 1];
  197. output = fopen(output_file, "w");
  198. if(NULL == output)
  199. {
  200. file_print("The file: ", stderr);
  201. file_print(input->name, stderr);
  202. file_print(" can not be opened!\n", stderr);
  203. exit(EXIT_FAILURE);
  204. }
  205. option_index = option_index + 2;
  206. }
  207. else if(match(argv[option_index], "-V") || match(argv[option_index], "--version"))
  208. {
  209. file_print("blood-elf 0.6.0\n(Basically Launches Odd Object Dump ExecutabLe Files\n", stdout);
  210. exit(EXIT_SUCCESS);
  211. }
  212. else
  213. {
  214. file_print("Unknown option\n", stderr);
  215. exit(EXIT_FAILURE);
  216. }
  217. }
  218. /* Make sure we have a program tape to run */
  219. if (NULL == input)
  220. {
  221. return EXIT_FAILURE;
  222. }
  223. /* Get all of the labels */
  224. first_pass(input);
  225. /* Reverse their order */
  226. jump_table = reverse_list(jump_table);
  227. file_print(":ELF_str\n!0\n", output);
  228. output_debug(jump_table, TRUE);
  229. if(64 == BITSIZE) file_print("%0\n:ELF_sym\n%0\n!0\n!0\n@1\n%0 %0\n%0 %0\n", output);
  230. else file_print("%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. file_print("\n:ELF_end\n", output);
  233. return EXIT_SUCCESS;
  234. }