main.c 11 KB

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