main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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("\n");
  120. printf("Step Options: \n");
  121. printf(" n continue\n");
  122. printf(" m show step menu\n");
  123. printf("Step Menu: \n");
  124. printMenu();
  125. printf("\n");
  126. printf(" For more information, please see: \n");
  127. printf(" github.com/SuperH-0630/BrainFuckPro\n");
  128. }
  129. void printVersion(void) {
  130. printf("%s", bf_getVersionInfo());
  131. }
  132. void printMenu(void) {
  133. printf("+---+----------------------------+\n");
  134. printf("+ v + show version +\n");
  135. printf("+ h + show help +\n");
  136. printf("+ m + show menu +\n");
  137. printf("+ d + print code after parser +\n");
  138. printf("+ w + print env information +\n");
  139. printf("+ p + print paper tape +\n");
  140. printf("+ t + print paper tape little +\n");
  141. printf("+ r + print read head +\n");
  142. printf("+ s + run in step model +\n");
  143. printf("+ i + run with information +\n");
  144. printf("+ n + set new env +\n");
  145. printf("+ f + run file +\n");
  146. printf("+ c + clear screen(May not work) +\n");
  147. printf("+ q + quit +\n");
  148. printf("+ e + exit the menu +\n");
  149. printf("+---+----------------------------+\n");
  150. }
  151. int runFile(int argc, char **argv, bf_env *env) {
  152. while (optind < argc) {
  153. FILE *file = fopen(argv[optind], "r");
  154. if (file == NULL) {
  155. perror("read file error");
  156. return 1;
  157. }
  158. bf_code code;
  159. if (initEnv)
  160. bf_initEnv(env); // 每次执行文件都回到头部重新执行
  161. code = bf_parserBrainFuck_File(file);
  162. if (print_code) {
  163. printf("code: ");
  164. bf_printBrainFuck(code);
  165. printf("\n");
  166. }
  167. bf_runBrainFuck(code, env);
  168. bf_printError("run error", env);
  169. bf_freeBrainFuck(code);
  170. fclose(file);
  171. printf("\n");
  172. optind++;
  173. }
  174. return 0;
  175. }
  176. int stepModeFunc(bf_env *env) {
  177. return clInformation('m', env);
  178. }
  179. int clInformation(int ch, bf_env *env) {
  180. int return_ = 0;
  181. switch (ch) {
  182. case 'v':
  183. printVersion();
  184. break;
  185. case 'h':
  186. printUsage();
  187. break;
  188. case 'm': {
  189. printMenu();
  190. printf("Enter the operation:");
  191. int del_ch;
  192. while ((del_ch = getc(stdin) != '\n') && del_ch != EOF)
  193. continue;
  194. ch = getc(stdin);
  195. return_ = clInformation(ch, env);
  196. goto NOT_CLEAR; // 不用清除stdin
  197. }
  198. case 'q': // 退出菜单
  199. return_ = 1;
  200. break;
  201. case 'd':
  202. print_code = !print_code;
  203. if (print_code)
  204. printf("print code mode on\n");
  205. else
  206. printf("print code mode off\n");
  207. break;
  208. case 'w':
  209. bf_printEnvWithMode(env);
  210. break;
  211. case 'p':
  212. bf_printPaperTape(env);
  213. printf("\n");
  214. break;
  215. case 't':
  216. bf_printPaperTapeNear(env);
  217. printf("\n");
  218. break;
  219. case 'r':
  220. bf_printHead(env);
  221. printf("\n");
  222. break;
  223. case 's':
  224. if (bf_setEnvMode(env, step, -1))
  225. printf("step mode on\n");
  226. else
  227. printf("step mode off\n");
  228. break;
  229. case 'i':
  230. if (bf_setEnvMode(env, information, -1))
  231. printf("information mode on\n");
  232. else
  233. printf("information mode off\n");
  234. break;
  235. case 'n':
  236. bf_resetEnv(global_env);
  237. break;
  238. case 'f': {
  239. int size = FILE_NAME_SIZE;
  240. char *str = calloc(size + 1, sizeof(char ));
  241. int del_ch;
  242. while ((del_ch = getc(stdin) != '\n') && del_ch != EOF)
  243. continue;
  244. printf("Enter file address:");
  245. fgets(str, FILE_NAME_SIZE + 1, stdin);
  246. while (!strchr(str, '\n') && !feof(stdin) && !ferror(stdin)) {
  247. char *new = calloc(size + FILE_NAME_SIZE + 1, sizeof(char ));
  248. strcpy(new, str);
  249. fgets(new + size, FILE_NAME_SIZE + 1, stdin);
  250. free(str);
  251. str = new;
  252. size += FILE_NAME_SIZE;
  253. }
  254. *strchr(str, '\n') = '\0'; // 去掉回车
  255. FILE *file = fopen(str, "r");
  256. if (file == NULL) {
  257. printf("file = '%s'\n", str);
  258. perror("read file error222");
  259. return -1;
  260. }
  261. bf_code code;
  262. code = bf_parserBrainFuck_File(file);
  263. if (print_code) {
  264. printf("code: ");
  265. bf_printBrainFuck(code);
  266. printf("\n");
  267. }
  268. return_ = bf_runBrainFuck(code, env);
  269. bf_printError("run error", env);
  270. bf_freeBrainFuck(code);
  271. fclose(file);
  272. free(str);
  273. printf("\n");
  274. goto NOT_CLEAR;
  275. }
  276. case 'c':
  277. system("clear"); // 清空
  278. break;
  279. case 'e': // 退出菜单
  280. break;
  281. default:
  282. printf("Unsupported menu option\n");
  283. break;
  284. }
  285. int del_ch;
  286. while ((del_ch = getc(stdin) != '\n') && del_ch != EOF)
  287. continue;
  288. NOT_CLEAR:
  289. return return_;
  290. }
  291. int runCommandLine(bf_env *env) {
  292. int ch;
  293. printf("BrainFuck %s (" __DATE__ ", " __TIME__ ")\n", bf_getVersion());
  294. printf("Welcome to ues BrainFuck CommandLine (Type 'q' to quit)\n");
  295. printf("Type v(version), h(help) and m(menu) for more information\n");
  296. for (unsigned count = 0; true;count++) {
  297. if (feof(stdin) || ferror(stdin))
  298. return 1;
  299. printf("[%d] >", count);
  300. ch = getc(stdin);
  301. if (ch == 'q')
  302. return 0;
  303. else if (ch == 'v' || ch == 'h' || ch == 'm') {
  304. if(clInformation(ch, env) == 1)
  305. return 0; // quit
  306. continue;
  307. } else
  308. ungetc(ch, stdin);
  309. if (runCommandLine_(env) == -2)
  310. return 0; // 退出
  311. }
  312. }
  313. int runCommandLine_(bf_env *env) {
  314. int size = COMMAND_LINE_STR_SIZE;
  315. int status;
  316. char *str = calloc(size + 1, sizeof(char ));
  317. fgets(str, COMMAND_LINE_STR_SIZE + 1, stdin);
  318. while (!strchr(str, '\n') && !feof(stdin) && !ferror(stdin)) {
  319. char *new = calloc(size + COMMAND_LINE_STR_SIZE + 1, sizeof(char ));
  320. strcpy(new, str);
  321. fgets(new + size, COMMAND_LINE_STR_SIZE + 1, stdin);
  322. free(str);
  323. str = new;
  324. size += COMMAND_LINE_STR_SIZE;
  325. }
  326. bf_code code;
  327. code = bf_parserBrainFuck_Str(str);
  328. if (print_code) {
  329. printf("code: ");
  330. bf_printBrainFuck(code);
  331. printf("\n");
  332. }
  333. status = bf_runBrainFuck(code, env);
  334. bf_printError("command line error", env);
  335. bf_freeBrainFuck(code);
  336. free(str);
  337. printf("\n");
  338. return status;
  339. }
  340. void free_env_at_exit(void) {
  341. bf_freeEnv(global_env);
  342. }