blood-elf.c 5.4 KB

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