main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <getopt.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6. #include "brainfuck.h"
  7. #define COMMAND_LINE_STR_SIZE (20)
  8. static bf_env *global_env;
  9. static struct option long_options[] = {
  10. {"version", no_argument, 0, 'v'},
  11. {"help", no_argument, 0, 'h'},
  12. {"eval", required_argument, 0, 'e'},
  13. {0, 0, 0, 0}
  14. };
  15. char *program_name;
  16. void printUsage(void);
  17. void printVersion(void);
  18. void printMenu(void);
  19. int stepModeFunc(bf_env *env);
  20. int runFile(int argc, char **argv, bf_env *env);
  21. int runCommandLine_(bf_env *env);
  22. int runCommandLine(bf_env *env);
  23. void free_env_at_exit(void);
  24. int clInformation(int ch, bf_env *env);
  25. int main(int argc, char **argv){
  26. int option_index = 0;
  27. int status;
  28. program_name = *argv;
  29. global_env = bf_setEnv();
  30. atexit(free_env_at_exit);
  31. bf_setEnvStepFunc(global_env, stepModeFunc);
  32. while (1) {
  33. option_index = 0;
  34. int c = getopt_long (argc, argv, "vhe:", long_options, &option_index);
  35. if (c == -1)
  36. break;
  37. switch (c) {
  38. case 0:
  39. break;
  40. case 'h':
  41. printUsage();
  42. goto end;
  43. case 'v':
  44. printVersion();
  45. goto end;
  46. case 'e': {
  47. bf_code code;
  48. code = bf_parserBrainFuck_Str((char *) optarg);
  49. bf_runBrainFuck(code, global_env);
  50. bf_printError("eval error", global_env);
  51. bf_freeBrainFuck(code);
  52. break;
  53. }
  54. default:
  55. case '?':
  56. printUsage();
  57. return 1;
  58. }
  59. }
  60. status = runFile(argc, argv, global_env);
  61. if (status != 0)
  62. return status;
  63. status = runCommandLine(global_env);
  64. if (status == 1) {
  65. printf("stdin error\n");
  66. return status;
  67. }
  68. printf("BrainFuckPro: bye~\n");
  69. return 0;
  70. end: // 从-v和-h中退出
  71. if (argc > 2) {
  72. printf("Too many argument\n");
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. void printUsage(void) {
  78. printf("Usage: %s[options] file..\n", program_name);
  79. printf("Options: \n");
  80. printf(" -e --eval\t\tRun code in a string\n");
  81. printf(" -v --version\t\tshow version\n");
  82. printf(" -h --help\t\tshow help\n\n");
  83. printf("CommandLine Options: \n");
  84. printf(" v show version\n");
  85. printf(" h show help\n");
  86. printf(" q quit\n");
  87. printf(" m show CommandLine Menu\n");
  88. printf("CommandLine Menu: \n");
  89. printMenu();
  90. printf("\n");
  91. printf("Step Options: \n");
  92. printf(" n continue\n");
  93. printf(" m show step menu\n");
  94. printf("Step Menu: \n");
  95. printMenu();
  96. printf("\n");
  97. printf(" For more information, please see: \n");
  98. printf(" github.com/SuperH-0630/BrainFuckPro\n");
  99. }
  100. void printVersion(void) {
  101. printf("%s", bf_getVersionInfo());
  102. }
  103. void printMenu(void) {
  104. printf("+---+----------------------------+\n");
  105. printf("+ v + show version +\n");
  106. printf("+ h + show help +\n");
  107. printf("+ m + show menu +\n");
  108. printf("+ w + print env information +\n");
  109. printf("+ p + print paper tape +\n");
  110. printf("+ r + print read head +\n");
  111. printf("+ s + run in step model +\n");
  112. printf("+ i + run with information +\n");
  113. printf("+ c + clear screen(May not work) +\n");
  114. printf("+ q + quit +\n");
  115. printf("+ e + exit the menu +\n");
  116. printf("+---+----------------------------+\n");
  117. }
  118. int runFile(int argc, char **argv, bf_env *env) {
  119. while (optind < argc) {
  120. FILE *file = fopen(argv[optind], "r");
  121. if (file == NULL) {
  122. perror("read file error");
  123. return 1;
  124. }
  125. bf_code code;
  126. code = bf_parserBrainFuck_File(file);
  127. bf_runBrainFuck(code, env);
  128. bf_printError("run error", env);
  129. bf_initEnv(env);
  130. bf_freeBrainFuck(code);
  131. fclose(file);
  132. optind++;
  133. }
  134. return 0;
  135. }
  136. int stepModeFunc(bf_env *env) {
  137. return clInformation('m', env);
  138. }
  139. int clInformation(int ch, bf_env *env) {
  140. int return_ = 0;
  141. switch (ch) {
  142. case 'v':
  143. printVersion();
  144. break;
  145. case 'h':
  146. printUsage();
  147. break;
  148. case 'm':
  149. printMenu();
  150. printf("Enter the operation:");
  151. int del_ch;
  152. while ((del_ch = getc(stdin) != '\n') && del_ch != EOF)
  153. continue;
  154. ch = getc(stdin);
  155. return_ = clInformation(ch, env);
  156. goto NOT_CLEAR; // 不用清除stdin
  157. case 'q': // 退出菜单
  158. break;
  159. case 'w':
  160. bf_printEnvWithMode(env);
  161. break;
  162. case 'p':
  163. bf_printPaperTape(env);
  164. printf("\n");
  165. break;
  166. case 'r':
  167. bf_printHead(env);
  168. printf("\n");
  169. break;
  170. case 's':
  171. if (bf_setEnvMode(env, step, -1))
  172. printf("step mode on\n");
  173. else
  174. printf("step mode off\n");
  175. break;
  176. case 'i':
  177. if (bf_setEnvMode(env, information, -1))
  178. printf("information mode on\n");
  179. else
  180. printf("information mode off\n");
  181. break;
  182. case 'c':
  183. system("clear"); // 清空
  184. break;
  185. case 'e': // 退出菜单
  186. break;
  187. default:
  188. printf("Unsupported menu option\n");
  189. break;
  190. }
  191. int del_ch;
  192. while ((del_ch = getc(stdin) != '\n') && del_ch != EOF)
  193. continue;
  194. NOT_CLEAR:
  195. return return_;
  196. }
  197. int runCommandLine(bf_env *env) {
  198. int ch;
  199. printf("BrainFuck %s (" __DATE__ ", " __TIME__ ")\n", bf_getVersion());
  200. printf("Welcome to ues BrainFuck CommandLine (Type 'q' to quit)\n");
  201. printf("Type v(version), h(help) and m(menu) for more information\n");
  202. for (unsigned count = 0; true;count++) {
  203. if (feof(stdin) || ferror(stdin))
  204. return 1;
  205. printf("[%d] >", count);
  206. ch = getc(stdin);
  207. if (ch == 'q')
  208. return 0;
  209. else if (ch == 'v' || ch == 'h' || ch == 'm') {
  210. if(clInformation(ch, env) == 1)
  211. return 0; // quit
  212. continue;
  213. } else
  214. ungetc(ch, stdin);
  215. runCommandLine_(env);
  216. }
  217. }
  218. int runCommandLine_(bf_env *env) {
  219. int size = COMMAND_LINE_STR_SIZE;
  220. int status;
  221. char *str = calloc(size + 1, sizeof(char ));
  222. fgets(str, COMMAND_LINE_STR_SIZE + 1, stdin);
  223. while (!strchr(str, '\n') && !feof(stdin) && !ferror(stdin)) {
  224. char *new = calloc(size + COMMAND_LINE_STR_SIZE + 1, sizeof(char ));
  225. strcpy(new, str);
  226. fgets(new + size, COMMAND_LINE_STR_SIZE + 1, stdin);
  227. free(str);
  228. str = new;
  229. size += COMMAND_LINE_STR_SIZE;
  230. }
  231. bf_code code;
  232. code = bf_parserBrainFuck_Str(str);
  233. status = bf_runBrainFuck(code, env);
  234. bf_printError("command line error", env);
  235. bf_freeBrainFuck(code);
  236. free(str);
  237. return status;
  238. }
  239. void free_env_at_exit(void) {
  240. bf_freeEnv(global_env);
  241. }