runfile.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 = wcsToStr(result->value->value->data.str.str, false);
  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. memFree(file_dir);
  133. freeStatement(new_st);
  134. freeParserMessage(pm, true);
  135. return result->type;
  136. }
  137. ResultType importVMFileCore(Inter *import_inter, char *path, fline line, char *code_file, INTER_FUNCTIONSIG_NOT_ST) {
  138. ParserMessage *pm = NULL;
  139. Statement *run_st = NULL;
  140. setResultCore(result);
  141. pm = makeParserMessage(path);
  142. run_st = makeStatement(0, path);
  143. if (!importRunParser(pm, line, code_file, run_st, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong)))
  144. goto return_;
  145. globalIterStatement(result, import_inter, run_st);
  146. if (!CHECK_RESULT(result))
  147. setResultError(E_BaseException, NULL, line, code_file, false, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  148. else
  149. setResult(result, inter, belong);
  150. return_:
  151. freeStatement(run_st);
  152. freeParserMessage(pm, true);
  153. return result->type;
  154. }
  155. ResultType importFileCore(char **path, char **split, int *status, INTER_FUNCTIONSIG) {
  156. setResultCore(result);
  157. if (operationSafeInterStatement(CALL_INTER_FUNCTIONSIG(st, var_list, result, belong)))
  158. return result->type;
  159. if (!isType(result->value->value, string)) {
  160. setResultErrorSt(E_ImportException, ONLY_ACC(include file dir, string), true, st, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  161. return error_return;
  162. }
  163. *path = wcsToStr(result->value->value->data.str.str, false);
  164. *split = splitDir(*path); // 自动去除末尾路径分隔符
  165. freeResult(result);
  166. if ((*status = checkFileDir(path, CALL_INTER_FUNCTIONSIG(st, var_list, result, belong))) == 0)
  167. return result->type;
  168. return result->type;
  169. }
  170. ResultType runImportFile(Inter *import_inter, char **path, int status, INTER_FUNCTIONSIG) {
  171. setResultCore(result);
  172. if (status == 2)
  173. importClibCore(*path, belong, CALL_INTER_FUNCTIONSIG_CORE(inter->var_list));
  174. else
  175. importVMFileCore(import_inter, *path, st->line, st->code_file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  176. import_inter->var_list = NULL;
  177. mergeInter(import_inter, inter);
  178. return result->type;
  179. }
  180. static bool getPackage(LinkValue **imp_value, char *md5_str, char *split, int status, char **path, int *is_new, bool is_lock, INTER_FUNCTIONSIG) {
  181. Value *pg;
  182. Inter *imp_inter;
  183. if (is_lock || (pg = checkPackage(inter->package, md5_str, split)) == NULL) {
  184. setResultCore(result);
  185. *is_new = true;
  186. imp_inter = deriveInter(belong, inter);
  187. pg = makeObject(inter, imp_inter->var_list, copyVarList(var_list, false, inter), NULL);
  188. if (!is_lock)
  189. inter->package = makePackage(pg, md5_str, split, inter->package); // 只有当不是保护读入或私密读入的时才可以记录
  190. imp_inter->package = inter->package;
  191. freeResult(result);
  192. runImportFile(imp_inter, path, status, CALL_INTER_FUNCTIONSIG(st, var_list, result, belong));
  193. if (!CHECK_RESULT(result))
  194. return false;
  195. } else
  196. *is_new = false;
  197. *imp_value = makeLinkValue(pg, belong, inter);
  198. gc_addTmpLink(&(*imp_value)->gc_status);
  199. return true;
  200. }
  201. ResultType importFile(INTER_FUNCTIONSIG) {
  202. bool is_new = false;
  203. bool is_lock = st->u.import_file.is_lock;
  204. Statement *file = st->u.import_file.file;
  205. int status;
  206. char *split_path = NULL;
  207. char *path = NULL;
  208. LinkValue *imp_value = NULL;
  209. char md5_str[MD5_STRING] = {};
  210. setResultCore(result);
  211. gc_freeze(inter, var_list, NULL, true);
  212. importFileCore(&path, &split_path, &status, CALL_INTER_FUNCTIONSIG(file, var_list, result, belong));
  213. if (!CHECK_RESULT(result))
  214. goto return_;
  215. getFileMd5(path, md5_str);
  216. if (!getPackage(&imp_value, md5_str, split_path, status, &path, &is_new, is_lock, CALL_INTER_FUNCTIONSIG(file, var_list, result, belong)))
  217. goto return_;
  218. freeResult(result);
  219. if (st->u.import_file.as == NULL) {
  220. wchar_t *name_ = strToWcs(split_path, false);
  221. addStrVar(name_, false, is_new, imp_value, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  222. memFree(name_);
  223. }
  224. else
  225. assCore(st->u.import_file.as, imp_value, false, is_new, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  226. if (CHECK_RESULT(result))
  227. setResult(result, inter, belong);
  228. gc_freeTmpLink(&imp_value->gc_status);
  229. return_:
  230. memFree(split_path);
  231. memFree(path);
  232. gc_freeze(inter, var_list, NULL, false);
  233. return result->type;
  234. }
  235. static ResultType importParameter(fline line, char *file, Parameter *call_pt, Parameter *func_pt, VarList *func_var, LinkValue *func_belong, INTER_FUNCTIONSIG_NOT_ST) {
  236. Argument *call = NULL;
  237. setResultCore(result);
  238. call = getArgument(call_pt, false, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  239. if (!CHECK_RESULT(result)) {
  240. freeArgument(call, false);
  241. return result->type;
  242. }
  243. freeResult(result);
  244. setParameterCore(line, file, call, func_pt, func_var, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, func_belong));
  245. freeArgument(call, false);
  246. return result->type;
  247. }
  248. ResultType fromImportFile(INTER_FUNCTIONSIG) {
  249. int status;
  250. bool is_new;
  251. bool is_lock = st->u.from_import_file.is_lock;
  252. Statement *file = st->u.from_import_file.file;
  253. char *split_path = NULL;
  254. char *path = NULL;
  255. char md5_str[MD5_STRING] = {};
  256. VarList *imp_var = NULL;
  257. LinkValue *imp_value;
  258. Parameter *pt = st->u.from_import_file.pt;
  259. Parameter *as = st->u.from_import_file.as != NULL ? st->u.from_import_file.as : st->u.from_import_file.pt;
  260. setResultCore(result);
  261. gc_freeze(inter, var_list, NULL, true);
  262. importFileCore(&path, &split_path, &status, CALL_INTER_FUNCTIONSIG(file, var_list, result, belong));
  263. if (!CHECK_RESULT(result))
  264. goto return_;
  265. getFileMd5(path, md5_str);
  266. if (!getPackage(&imp_value, md5_str, split_path, status, &path, &is_new, is_lock, CALL_INTER_FUNCTIONSIG(file, var_list, result, belong)))
  267. goto return_;
  268. imp_var = imp_value->value->object.var;
  269. if (is_new) {
  270. wchar_t *wcs;
  271. LinkValue *string;
  272. freeResult(result);
  273. wcs = strToWcs(split_path, false);
  274. makeStringValue(wcs, st->line, st->code_file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, imp_value));
  275. memFree(wcs);
  276. string = result->value;
  277. result->value = NULL;
  278. freeResult(result);
  279. newObjectSetting(string, st->line, st->code_file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, imp_value));
  280. gc_freeTmpLink(&string->gc_status);
  281. }
  282. freeResult(result);
  283. if (pt != NULL) {
  284. importParameter(st->line, st->code_file, pt, as, var_list, belong, CALL_INTER_FUNCTIONSIG_NOT_ST(imp_var, result, imp_value));
  285. if (!CHECK_RESULT(result)) {
  286. printf("TAG A\n");
  287. gc_freeTmpLink(&imp_value->gc_status);
  288. goto return_;
  289. }
  290. }
  291. else
  292. updateHashTable(var_list->hashtable, imp_var->hashtable, inter);
  293. setResult(result, inter, belong);
  294. gc_freeTmpLink(&imp_value->gc_status);
  295. return_:
  296. memFree(path);
  297. memFree(split_path);
  298. gc_freeze(inter, var_list, NULL, false);
  299. return result->type;
  300. }