main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. #define FILE_NAME_SIZE (20)
  9. static bf_env *global_env;
  10. static struct option long_options[] = {
  11. {"version", no_argument, 0, 'v'},
  12. {"help", no_argument, 0, 'h'},
  13. {"eval", required_argument, 0, 'e'},
  14. {"print", no_argument, 0, 'p'},
  15. {"step", no_argument, 0, 's'},
  16. {"information", no_argument, 0, 'i'},
  17. {"noninit", no_argument, 0, 'n'},
  18. {0, 0, 0, 0}
  19. };
  20. char *program_name;
  21. bool print_code = false;
  22. bool initEnv = true; // 每次执行命令行参数指定的文件时都重置读取头位置
  23. void printUsage(void);
  24. void printVersion(void);
  25. void printMenu(void);
  26. int stepModeFunc(bf_env *env);
  27. int runFile(int argc, char **argv, bf_env *env);
  28. int runCommandLine_(bf_env *env);
  29. int runCommandLine(bf_env *env);
  30. void free_env_at_exit(void);
  31. int clInformation(int ch, bf_env *env);
  32. int main(int argc, char **argv){
  33. int option_index = 0;
  34. int status;
  35. program_name = *argv;
  36. global_env = bf_setEnv();
  37. atexit(free_env_at_exit);
  38. bf_setEnvStepFunc(global_env, stepModeFunc);
  39. while (1) {
  40. option_index = 0;
  41. int c = getopt_long (argc, argv, "vhsie:", long_options, &option_index);
  42. if (c == -1)
  43. break;
  44. switch (c) {
  45. case 0:
  46. break;
  47. case 'h':
  48. printUsage();
  49. goto end;
  50. case 'v':
  51. printVersion();
  52. goto end;
  53. case 'e': {
  54. bf_code code;
  55. code = bf_parserBrainFuck_Str((char *) optarg); // 不会回到头部重新执行
  56. if (print_code) {
  57. printf("code: ");
  58. bf_printBrainFuck(code);
  59. printf("\n");
  60. }
  61. bf_runBrainFuck(code, global_env);
  62. bf_printError("eval error", global_env);
  63. bf_freeBrainFuck(code);
  64. printf("\n");
  65. break;
  66. }
  67. case 's':
  68. bf_setEnvMode(global_env, step, 1);
  69. break;
  70. case 'i':
  71. bf_setEnvMode(global_env, information, 1);
  72. break;
  73. case 'p':
  74. print_code = true;
  75. break;
  76. case 'n':
  77. initEnv = false;
  78. break;
  79. default:
  80. case '?':
  81. printUsage();
  82. return 1;
  83. }
  84. }
  85. status = runFile(argc, argv, global_env);
  86. if (status != 0)
  87. return status;
  88. status = runCommandLine(global_env);
  89. if (status == 1) {
  90. printf("stdin error\n");
  91. return status;
  92. }
  93. printf("BrainFuckPro: bye~\n");
  94. return 0;
  95. end: // 从-v和-h中退出
  96. if (argc > 2) {
  97. printf("Too many argument\n");
  98. return 1;
  99. }
  100. return 0;
  101. }
  102. void printUsage(void) {
  103. printf("Usage: %s[options] file..\n", program_name);
  104. printf("Options: \n");
  105. printf(" -e --eval\t\t\tRun code in a string\n");
  106. printf(" -v --version\t\t\tShow version\n");
  107. printf(" -h --help\t\t\tShow help\n");
  108. printf(" -s --step\t\t\tRun with step mode\n");
  109. printf(" -i --information\t\tRun with information mode\n");
  110. printf(" -n --noninit\t\t\tDon't init env after run file\n\n");
  111. printf(" -p --print\t\t\tPrint code after parser\n\n");
  112. printf("CommandLine Options: \n");
  113. printf(" v show version\n");
  114. printf(" h show help\n");
  115. printf(" q quit\n");
  116. printf(" m show CommandLine Menu\n");
  117. printf("CommandLine Menu: \n");
  118. printMenu();
  119. printf("Note: CommandLine does not support entering multiple lines of code\n");
  120. printf("\n");
  121. printf("Step Options: \n");
  122. printf(" n continue\n");
  123. printf(" m show step menu\n");
  124. printf(" j (Only debug) Jump to the next debug point\n");
  125. printf(" g (Only debug) Ignore all debug point (just in this code)\n");
  126. printf("Step Menu: \n");
  127. printMenu();
  128. printf("\n");
  129. printf(" For more information, please see: \n");
  130. printf(" github.com/SuperH-0630/BrainFuckPro\n");
  131. }
  132. void printVersion(void) {
  133. printf("%s", bf_getVersionInfo());
  134. }
  135. void printMenu(void) {
  136. printf("+---+----------------------------+\n");
  137. printf("+ v + show version +\n");
  138. printf("+ h + show help +\n");
  139. printf("+ m + show menu +\n");
  140. printf("+ d + print code after parser +\n");
  141. printf("+ w + print env information +\n");
  142. printf("+ p + print paper tape +\n");
  143. printf("+ t + print paper tape little +\n");
  144. printf("+ r + print read head +\n");
  145. printf("+ s + run in step model +\n");
  146. printf("+ i + run with information +\n");
  147. printf("+ n + set new env +\n");
  148. printf("+ f + run file +\n");
  149. printf("+ c + clear screen(May not work) +\n");
  150. printf("+ q + quit +\n");
  151. printf("+ e + exit the menu +\n");
  152. printf("+---+----------------------------+\n");
  153. }
  154. int runFile(int argc, char **argv, bf_env *env) {
  155. while (optind < argc) {
  156. FILE *file = fopen(argv[optind], "r");
  157. if (file == NULL) {
  158. perror("read file error");
  159. return 1;
  160. }
  161. bf_code code;
  162. if (initEnv)
  163. bf_initEnv(env); // 每次执行文件都回到头部重新执行
  164. code = bf_parserBrainFuck_File(file);
  165. if (print_code) {
  166. printf("code: ");
  167. bf_printBrainFuck(code);
  168. printf("\n");
  169. }
  170. bf_runBrainFuck(code, env);
  171. bf_printError("run error", env);
  172. bf_freeBrainFuck(code);
  173. fclose(file);
  174. printf("\n");
  175. optind++;
  176. }
  177. return 0;
  178. }
  179. int stepModeFunc(bf_env *env) {
  180. return clInformation('m', env);
  181. }
  182. int clInformation(int ch, bf_env *env) {
  183. int return_ = 0;
  184. switch (ch) {
  185. case 'v':
  186. printVersion();
  187. break;
  188. case 'h':
  189. printUsage();
  190. break;
  191. case 'm': {
  192. printMenu();
  193. printf("Enter the operation:");
  194. int del_ch;
  195. while ((del_ch = getc(stdin) != '\n') && del_ch != EOF)
  196. continue;
  197. ch = getc(stdin);
  198. return_ = clInformation(ch, env);
  199. goto NOT_CLEAR; // 不用清除stdin
  200. }
  201. case 'q': // 退出菜单
  202. return_ = 1;
  203. break;
  204. case 'd':
  205. print_code = !print_code;
  206. if (print_code)
  207. printf("print code mode on\n");
  208. else
  209. printf("print code mode off\n");
  210. break;
  211. case 'w':
  212. bf_printEnvWithMode(env);
  213. break;
  214. case 'p':
  215. bf_printPaperTape(env);
  216. printf("\n");
  217. break;
  218. case 't':
  219. bf_printPaperTapeNear(env);
  220. printf("\n");
  221. break;
  222. case 'r':
  223. bf_printHead(env);
  224. printf("\n");
  225. break;
  226. case 's':
  227. if (bf_setEnvMode(env, step, -1))
  228. printf("step mode on\n");
  229. else
  230. printf("step mode off\n");
  231. break;
  232. case 'i':
  233. if (bf_setEnvMode(env, information, -1))
  234. printf("information mode on\n");
  235. else
  236. printf("information mode off\n");
  237. break;
  238. case 'n':
  239. bf_resetEnv(global_env);
  240. break;
  241. case 'f': {
  242. int size = FILE_NAME_SIZE;
  243. char *str = calloc(size + 1, sizeof(char ));
  244. int del_ch;
  245. while ((del_ch = getc(stdin) != '\n') && del_ch != EOF)
  246. continue;
  247. printf("Enter file address:");
  248. fgets(str, FILE_NAME_SIZE + 1, stdin);
  249. while (!strchr(str, '\n') && !feof(stdin) && !ferror(stdin)) {
  250. char *new = calloc(size + FILE_NAME_SIZE + 1, sizeof(char ));
  251. strcpy(new, str);
  252. fgets(new + size, FILE_NAME_SIZE + 1, stdin);
  253. free(str);
  254. str = new;
  255. size += FILE_NAME_SIZE;
  256. }
  257. *strchr(str, '\n') = '\0'; // 去掉回车
  258. FILE *file = fopen(str, "r");
  259. if (file == NULL) {
  260. printf("file = '%s'\n", str);
  261. perror("read file error222");
  262. return -1;
  263. }
  264. bf_code code;
  265. code = bf_parserBrainFuck_File(file);
  266. if (print_code) {
  267. printf("code: ");
  268. bf_printBrainFuck(code);
  269. printf("\n");
  270. }
  271. return_ = bf_runBrainFuck(code, env);
  272. bf_printError("run error", env);
  273. bf_freeBrainFuck(code);
  274. fclose(file);
  275. free(str);
  276. printf("\n");
  277. goto NOT_CLEAR;
  278. }
  279. case 'c':
  280. system("clear"); // 清空
  281. break;
  282. case 'e': // 退出菜单
  283. break;
  284. default:
  285. printf("Unsupported menu option\n");
  286. break;
  287. }
  288. int del_ch;
  289. while ((del_ch = getc(stdin) != '\n') && del_ch != EOF)
  290. continue;
  291. NOT_CLEAR:
  292. return return_;
  293. }
  294. int runCommandLine(bf_env *env) {
  295. int ch;
  296. printf("BrainFuck %s (" __DATE__ ", " __TIME__ ")\n", bf_getVersion());
  297. printf("Welcome to ues BrainFuck CommandLine (Type 'q' to quit)\n");
  298. printf("Type v(version), h(help) and m(menu) for more information\n");
  299. for (unsigned count = 0; true;count++) {
  300. if (feof(stdin) || ferror(stdin))
  301. return 1;
  302. printf("[%d] >", count);
  303. ch = getc(stdin);
  304. if (ch == 'q')
  305. return 0;
  306. else if (ch == 'v' || ch == 'h' || ch == 'm') {
  307. if(clInformation(ch, env) == 1)
  308. return 0; // quit
  309. continue;
  310. } else
  311. ungetc(ch, stdin);
  312. if (runCommandLine_(env) == -2)
  313. return 0; // 退出
  314. }
  315. }
  316. int runCommandLine_(bf_env *env) {
  317. int size = COMMAND_LINE_STR_SIZE;
  318. int status;
  319. char *str = calloc(size + 1, sizeof(char ));
  320. fgets(str, COMMAND_LINE_STR_SIZE + 1, stdin);
  321. while (!strchr(str, '\n') && !feof(stdin) && !ferror(stdin)) {
  322. char *new = calloc(size + COMMAND_LINE_STR_SIZE + 1, sizeof(char ));
  323. strcpy(new, str);
  324. fgets(new + size, COMMAND_LINE_STR_SIZE + 1, stdin);
  325. free(str);
  326. str = new;
  327. size += COMMAND_LINE_STR_SIZE;
  328. }
  329. bf_code code;
  330. code = bf_parserBrainFuck_Str(str);
  331. if (print_code) {
  332. printf("code: ");
  333. bf_printBrainFuck(code);
  334. printf("\n");
  335. }
  336. status = bf_runBrainFuck(code, env);
  337. bf_printError("command line error", env);
  338. bf_freeBrainFuck(code);
  339. free(str);
  340. printf("\n");
  341. return status;
  342. }
  343. void free_env_at_exit(void) {
  344. bf_freeEnv(global_env);
  345. }