runfile.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include "__run.h"
  2. bool importRunParser(ParserMessage *pm, fline line, char *file, Statement *run_st, INTER_FUNCTIONSIG_NOT_ST) {
  3. setResultCore(result);
  4. parserCommandList(pm, inter, true, false, run_st);
  5. if (pm->status == int_error)
  6. setResultError(E_KeyInterrupt, KEY_INTERRUPT, line, file, true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  7. else if (pm->status != success)
  8. setResultError(E_TypeException, pm->status_message, line, file, true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  9. return CHECK_RESULT(result);
  10. }
  11. int isAbsolutePath(const char *path) {
  12. switch (*path) {
  13. case ':':
  14. return 1;
  15. case '@':
  16. return 2;
  17. case '$':
  18. return 3;
  19. default:
  20. return 0;
  21. }
  22. }
  23. static bool isExist(char **path, bool is_ab, char *file) {
  24. char *backup = is_ab ? memStrcpy((*path) + 1) : memStrcpy(*path);
  25. int status;
  26. if ((status = checkFileReadble(backup)) != 3 || (status = checkFileReadble(backup = memStrcat(backup, ".vm", true, false))) != 3) {
  27. memFree(*path);
  28. *path = backup;
  29. if (status == 2) {
  30. if (file == NULL)
  31. return false;
  32. #if __linux__
  33. if ((*path)[memStrlen(*path) - 1] != '/')
  34. *path = memStrcat(*path, "/", true, false);
  35. *path = memStrcat(*path, file, true, false);
  36. #else
  37. if ((*path)[memStrlen(*path) - 1] != '\\')
  38. *path = memStrcat(*path, "\\", true, false);
  39. *path = memStrcat(*path, file, true, false);
  40. #endif
  41. return isExist(path, false, NULL);
  42. } else
  43. return true;
  44. }
  45. memFree(backup);
  46. return false;
  47. }
  48. int checkFileDir(char **file_dir, INTER_FUNCTIONSIG) {
  49. switch (isAbsolutePath(*file_dir)) {
  50. case 1:
  51. if (isExist(file_dir, true, "__init__.vm"))
  52. return 1;
  53. goto error_;
  54. case 2:
  55. goto clib;
  56. case 3:
  57. goto path;
  58. default:
  59. break;
  60. }
  61. if (isExist(file_dir, false, "__init__.vm"))
  62. return 1;
  63. {
  64. char arr_cwd[200] = {};
  65. char *p_cwd = NULL;
  66. getcwd(arr_cwd, 200);
  67. #ifdef __linux__
  68. p_cwd = memStrcatIter(arr_cwd, false, "/", *file_dir, NULL);
  69. #else
  70. p_cwd = memStrcatIter(arr_cwd, false, "\\", *file_dir);
  71. #endif
  72. if (isExist(&p_cwd, false, "__init__.vm")) {
  73. memFree(*file_dir);
  74. *file_dir = p_cwd;
  75. return 1;
  76. }
  77. memFree(p_cwd);
  78. }
  79. path: {
  80. char *path = memStrcpy(getenv("VIRTUALMATHPATH"));
  81. for (char *tmp = strtok(path, ";"), *new_dir; tmp != NULL; tmp = strtok(NULL, ";")) {
  82. #ifdef __linux__
  83. if (*(tmp + (memStrlen(tmp) - 1)) != '/')
  84. new_dir = memStrcatIter(tmp, false, "/", *file_dir, NULL);
  85. #else
  86. if (*(tmp + (memStrlen(tmp) - 1)) != '\\')
  87. new_dir = memStrcatIter(tmp, false, "\\", *file_dir);
  88. #endif
  89. else
  90. new_dir = memStrcat(tmp, *file_dir, false, false);
  91. if (isExist(&new_dir, false, "__init__.vm")) {
  92. memFree(*file_dir);
  93. *file_dir = new_dir;
  94. return 1;
  95. }
  96. memFree(new_dir);
  97. }
  98. memFree(path);
  99. }
  100. clib:
  101. if (checkCLib(*file_dir))
  102. return 2;
  103. error_:
  104. setResultErrorSt(E_ImportException, "import/include file is not readable", true, st, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  105. return 0;
  106. }
  107. ResultType includeFile(INTER_FUNCTIONSIG) {
  108. Statement *new_st = NULL;
  109. ParserMessage *pm = NULL;
  110. char *file_dir = NULL;
  111. setResultCore(result);
  112. if (operationSafeInterStatement(CALL_INTER_FUNCTIONSIG(st->u.include_file.file, var_list, result, belong)))
  113. return result->type;
  114. if (!isType(result->value->value, string)){
  115. setResultErrorSt(E_TypeException, ONLY_ACC(include file dir, string), true, st, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  116. goto return_;
  117. }
  118. file_dir = result->value->value->data.str.str;
  119. freeResult(result);
  120. if (checkFileDir(&file_dir, CALL_INTER_FUNCTIONSIG(st, var_list, result, belong)) != 1)
  121. goto return_;
  122. new_st = makeStatement(0, file_dir);
  123. pm = makeParserMessage(file_dir);
  124. if (!importRunParser(pm, st->line, st->code_file, new_st, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong)))
  125. goto return_;
  126. functionSafeInterStatement(CALL_INTER_FUNCTIONSIG(new_st, var_list, result, belong));
  127. if (result->type == yield_return)
  128. setResult(result, inter, belong);
  129. else if (!CHECK_RESULT(result))
  130. setResultErrorSt(E_BaseException, NULL, false, st, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  131. return_:
  132. freeStatement(new_st);
  133. freeParserMessage(pm, true);
  134. return result->type;
  135. }
  136. ResultType importVMFileCore(Inter *import_inter, char *path, fline line, char *code_file, INTER_FUNCTIONSIG_NOT_ST) {
  137. ParserMessage *pm = NULL;
  138. Statement *run_st = NULL;
  139. setResultCore(result);
  140. pm = makeParserMessage(path);
  141. run_st = makeStatement(0, path);
  142. if (!importRunParser(pm, line, code_file, run_st, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong)))
  143. goto return_;
  144. globalIterStatement(result, import_inter, run_st);
  145. if (!CHECK_RESULT(result))
  146. setResultError(E_BaseException, NULL, line, code_file, false, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  147. else
  148. setResult(result, inter, belong);
  149. return_:
  150. freeStatement(run_st);
  151. freeParserMessage(pm, true);
  152. return result->type;
  153. }
  154. ResultType importFileCore(char **path, char **split, int *status, INTER_FUNCTIONSIG) {
  155. setResultCore(result);
  156. if (operationSafeInterStatement(CALL_INTER_FUNCTIONSIG(st, var_list, result, belong)))
  157. return result->type;
  158. if (!isType(result->value->value, string)) {
  159. setResultErrorSt(E_ImportException, ONLY_ACC(include file dir, string), true, st, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  160. return error_return;
  161. }
  162. *path = memStrcpy(result->value->value->data.str.str);
  163. *split = splitDir(*path); // 自动去除末尾路径分隔符
  164. freeResult(result);
  165. if ((*status = checkFileDir(path, CALL_INTER_FUNCTIONSIG(st, var_list, result, belong))) == 0)
  166. return result->type;
  167. return result->type;
  168. }
  169. ResultType runImportFile(Inter *import_inter, char **path, int status, INTER_FUNCTIONSIG) {
  170. setResultCore(result);
  171. if (status == 2)
  172. importClibCore(*path, belong, CALL_INTER_FUNCTIONSIG_CORE(inter->var_list));
  173. else
  174. importVMFileCore(import_inter, *path, st->line, st->code_file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  175. import_inter->var_list = NULL;
  176. mergeInter(import_inter, inter);
  177. return result->type;
  178. }
  179. static bool getPackage(LinkValue **imp_value, char *md5_str, char *split, int status, char **path, INTER_FUNCTIONSIG) {
  180. Value *pg;
  181. Inter *imp_inter;
  182. if ((pg = checkPackage(inter->package, md5_str, split)) == NULL) {
  183. setResultCore(result);
  184. imp_inter = deriveInter(belong, inter);
  185. pg = makeObject(inter, imp_inter->var_list, copyVarList(var_list, false, inter), NULL);
  186. inter->package = makePackage(pg, md5_str, split, inter->package);
  187. imp_inter->package = inter->package;
  188. freeResult(result);
  189. runImportFile(imp_inter, path, status, CALL_INTER_FUNCTIONSIG(st->u.import_file.file, var_list, result, belong));
  190. if (!CHECK_RESULT(result))
  191. return false;
  192. }
  193. *imp_value = makeLinkValue(pg, belong, inter);
  194. gc_addTmpLink(&(*imp_value)->gc_status);
  195. return true;
  196. }
  197. ResultType importFile(INTER_FUNCTIONSIG) {
  198. int status;
  199. char *split_path = NULL;
  200. char *path = NULL;
  201. LinkValue *imp_value = NULL;
  202. char md5_str[MD5_STRING] = {};
  203. setResultCore(result);
  204. gc_freeze(inter, var_list, NULL, true);
  205. importFileCore(&path, &split_path, &status, CALL_INTER_FUNCTIONSIG(st->u.import_file.file, var_list, result, belong));
  206. if (!CHECK_RESULT(result))
  207. goto return_;
  208. getFileMd5(path, md5_str);
  209. if (!getPackage(&imp_value, md5_str, split_path, status, &path, CALL_INTER_FUNCTIONSIG(st, var_list, result, belong)))
  210. goto return_;
  211. freeResult(result);
  212. if (st->u.import_file.as == NULL)
  213. addStrVar(split_path, false, false, imp_value, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  214. else
  215. assCore(st->u.import_file.as, imp_value, false, false, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  216. if (CHECK_RESULT(result))
  217. setResult(result, inter, belong);
  218. gc_freeTmpLink(&imp_value->gc_status);
  219. return_:
  220. memFree(split_path);
  221. memFree(path);
  222. gc_freeze(inter, var_list, NULL, false);
  223. return result->type;
  224. }
  225. static ResultType importParameter(fline line, char *file, Parameter *call_pt, Parameter *func_pt, VarList *func_var, LinkValue *func_belong, INTER_FUNCTIONSIG_NOT_ST) {
  226. Argument *call = NULL;
  227. setResultCore(result);
  228. call = getArgument(call_pt, false, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  229. if (!CHECK_RESULT(result)) {
  230. freeArgument(call, false);
  231. return result->type;
  232. }
  233. freeResult(result);
  234. setParameterCore(line, file, call, func_pt, func_var, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, func_belong));
  235. freeArgument(call, false);
  236. return result->type;
  237. }
  238. ResultType fromImportFile(INTER_FUNCTIONSIG) {
  239. int status;
  240. char *split_path = NULL;
  241. char *path = NULL;
  242. char md5_str[MD5_STRING] = {};
  243. VarList *imp_var = NULL;
  244. LinkValue *imp_value;
  245. Parameter *pt = st->u.from_import_file.pt;
  246. Parameter *as = st->u.from_import_file.as != NULL ? st->u.from_import_file.as : st->u.from_import_file.pt;
  247. setResultCore(result);
  248. importFileCore(&path, &split_path, &status, CALL_INTER_FUNCTIONSIG(st->u.from_import_file.file, var_list, result, belong));
  249. if (!CHECK_RESULT(result))
  250. goto return_;
  251. getFileMd5(path, md5_str);
  252. if (!getPackage(&imp_value, md5_str, split_path, status, &path, CALL_INTER_FUNCTIONSIG(st, var_list, result, belong)))
  253. goto return_;
  254. imp_var = imp_value->value->object.var;
  255. freeResult(result);
  256. if (pt != NULL) {
  257. importParameter(st->line, st->code_file, pt, as, var_list, belong, CALL_INTER_FUNCTIONSIG_NOT_ST(imp_var, result, imp_value));
  258. if (!CHECK_RESULT(result))
  259. goto return_;
  260. }
  261. else
  262. updateHashTable(var_list->hashtable, imp_var->hashtable, inter);
  263. gc_freeTmpLink(&imp_value->value->gc_status);
  264. setResult(result, inter, belong);
  265. return_:
  266. memFree(path);
  267. memFree(split_path);
  268. freeVarList(imp_var); // 当需要存储var_list的时候则不需要释放
  269. return result->type;
  270. }