runfile.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #include "__run.h"
  2. bool importRunParser(ParserMessage *pm, fline line, char *file, Statement *run_st, FUNC_NT) {
  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, CNEXT_NT);
  7. else if (pm->status != success) {
  8. wchar_t *wcs_message = memStrToWcs(pm->status_message, false);
  9. setResultError(E_TypeException, wcs_message, line, file, true, CNEXT_NT);
  10. memFree(wcs_message);
  11. }
  12. return CHECK_RESULT(result);
  13. }
  14. int isAbsolutePath(const char *path) {
  15. switch (*path) {
  16. case ':':
  17. return 1;
  18. case '@':
  19. return 2;
  20. case '$':
  21. return 3;
  22. default:
  23. return 0;
  24. }
  25. }
  26. static bool isExist(char **path, bool is_ab, char *file) {
  27. char *backup = is_ab ? memStrcpy((*path) + 1) : memStrcpy(*path);
  28. int status;
  29. if ((status = checkFileReadble(backup)) != 3 || (status = checkFileReadble(backup = memStrcat(backup, ".vm", true, false))) != 3) {
  30. memFree(*path);
  31. *path = backup;
  32. if (status == 2) {
  33. if (file == NULL)
  34. return false;
  35. #if __linux__
  36. if ((*path)[memStrlen(*path) - 1] != '/')
  37. *path = memStrcat(*path, "/", true, false);
  38. *path = memStrcat(*path, file, true, false);
  39. #else
  40. if ((*path)[memStrlen(*path) - 1] != '\\')
  41. *path = memStrcat(*path, "\\", true, false);
  42. *path = memStrcat(*path, file, true, false);
  43. #endif
  44. return isExist(path, false, NULL);
  45. } else
  46. return true;
  47. }
  48. memFree(backup);
  49. return false;
  50. }
  51. int checkFileDir(char **file_dir, FUNC) {
  52. switch (isAbsolutePath(*file_dir)) {
  53. case 1:
  54. if (isExist(file_dir, true, "__init__.vm"))
  55. return 1;
  56. goto error_;
  57. case 2:
  58. goto clib;
  59. case 3:
  60. goto path;
  61. default:
  62. break;
  63. }
  64. if (isExist(file_dir, false, "__init__.vm"))
  65. return 1;
  66. {
  67. char *getcwd(char *buf,size_t size);
  68. char arr_cwd[200];
  69. char *p_cwd = NULL;
  70. getcwd(arr_cwd, 200);
  71. #ifdef __linux__
  72. p_cwd = memStrcatIter(arr_cwd, false, "/", *file_dir, NULL);
  73. #else
  74. p_cwd = memStrcatIter(arr_cwd, false, "\\", *file_dir);
  75. #endif
  76. if (isExist(&p_cwd, false, "__init__.vm")) {
  77. memFree(*file_dir);
  78. *file_dir = p_cwd;
  79. return 1;
  80. }
  81. memFree(p_cwd);
  82. }
  83. path: {
  84. char *path = memStrcpy(getenv("VIRTUALMATHPATH"));
  85. for (char *tmp = strtok(path, ";"), *new_dir; tmp != NULL; tmp = strtok(NULL, ";")) {
  86. #ifdef __linux__
  87. if (*(tmp + (memStrlen(tmp) - 1)) != '/')
  88. new_dir = memStrcatIter(tmp, false, "/", *file_dir, NULL);
  89. #else
  90. if (*(tmp + (memStrlen(tmp) - 1)) != '\\')
  91. new_dir = memStrcatIter(tmp, false, "\\", *file_dir);
  92. #endif
  93. else
  94. new_dir = memStrcat(tmp, *file_dir, false, false);
  95. if (isExist(&new_dir, false, "__init__.vm")) {
  96. memFree(*file_dir);
  97. *file_dir = new_dir;
  98. return 1;
  99. }
  100. memFree(new_dir);
  101. }
  102. memFree(path);
  103. }
  104. clib:
  105. if (checkCLib(*file_dir))
  106. return 2;
  107. error_:
  108. setResultErrorSt(E_ImportException, L"import/include file is not readable", true, st, CNEXT_NT);
  109. return 0;
  110. }
  111. ResultType includeFile(FUNC) {
  112. Statement *new_st = NULL;
  113. ParserMessage *pm = NULL;
  114. char *file_dir = NULL;
  115. setResultCore(result);
  116. if (operationSafeInterStatement(CFUNC(st->u.include_file.file, var_list, result, belong)))
  117. return result->type;
  118. if (!isType(result->value->value, V_str)){
  119. setResultErrorSt(E_TypeException, ONLY_ACC(include file dir, V_str), true, st, CNEXT_NT);
  120. goto return_;
  121. }
  122. file_dir = memWcsToStr(result->value->value->data.str.str, false);
  123. freeResult(result);
  124. if (checkFileDir(&file_dir, CNEXT) != 1)
  125. goto return_;
  126. new_st = makeStatement(0, file_dir);
  127. pm = makeParserMessageFile(file_dir);
  128. if (!importRunParser(pm, st->line, st->code_file, new_st, CNEXT_NT))
  129. goto return_;
  130. includeSafeInterStatement(CFUNC(new_st, var_list, result, belong));
  131. if (result->type == R_yield)
  132. setResult(result, inter);
  133. else if (!CHECK_RESULT(result))
  134. setResultErrorSt(E_BaseException, NULL, false, st, CNEXT_NT);
  135. return_:
  136. memFree(file_dir);
  137. freeStatement(new_st);
  138. freeParserMessage(pm, true);
  139. return result->type;
  140. }
  141. ResultType importVMFileCore(Inter *import_inter, char *path, fline line, char *code_file, FUNC_NT) {
  142. ParserMessage *pm = NULL;
  143. Statement *run_st = NULL;
  144. setResultCore(result);
  145. pm = makeParserMessageFile(path);
  146. run_st = makeStatement(0, path);
  147. if (!importRunParser(pm, line, code_file, run_st, CNEXT_NT))
  148. goto return_;
  149. globalIterStatement(result, import_inter, run_st);
  150. if (!CHECK_RESULT(result))
  151. setResultError(E_BaseException, NULL, line, code_file, false, CNEXT_NT);
  152. else
  153. setResult(result, inter);
  154. return_:
  155. freeStatement(run_st);
  156. freeParserMessage(pm, true);
  157. return result->type;
  158. }
  159. ResultType importFileCore(char **path, char **split, int *status, FUNC) {
  160. setResultCore(result);
  161. if (operationSafeInterStatement(CNEXT))
  162. return result->type;
  163. if (!isType(result->value->value, V_str)) {
  164. setResultErrorSt(E_ImportException, ONLY_ACC(include file dir, V_str), true, st, CNEXT_NT);
  165. return R_error;
  166. }
  167. *path = memWcsToStr(result->value->value->data.str.str, false);
  168. *split = splitDir(*path); // 自动去除末尾路径分隔符
  169. freeResult(result);
  170. if ((*status = checkFileDir(path, CNEXT)) == 0)
  171. return result->type;
  172. return result->type;
  173. }
  174. ResultType runImportFile(Inter *import_inter, char **path, int status, FUNC) {
  175. setResultCore(result);
  176. if (status == 2)
  177. importClibCore(*path, belong, CFUNC_CORE(inter->var_list));
  178. else
  179. importVMFileCore(import_inter, *path, st->line, st->code_file, CNEXT_NT);
  180. import_inter->var_list = NULL;
  181. mergeInter(import_inter, inter);
  182. return result->type;
  183. }
  184. static bool getPackage(LinkValue **imp_value, char *md5_str, char *split, int status, char **path, int *is_new, bool is_lock, FUNC) {
  185. Value *pg;
  186. Inter *imp_inter;
  187. if (is_lock || (pg = checkPackage(inter->package, md5_str, split)) == NULL) {
  188. setResultCore(result);
  189. *is_new = true;
  190. imp_inter = deriveInter(belong, inter);
  191. pg = makeObject(inter, imp_inter->var_list, copyVarList(var_list, false, inter), NULL);
  192. if (!is_lock)
  193. inter->package = makePackage(pg, md5_str, split, inter->package); // 只有当不是保护读入或私密读入的时才可以记录
  194. imp_inter->package = inter->package;
  195. freeResult(result);
  196. runImportFile(imp_inter, path, status, CNEXT);
  197. if (!CHECK_RESULT(result))
  198. return false;
  199. } else
  200. *is_new = false;
  201. *imp_value = makeLinkValue(pg, belong, inter);
  202. gc_addTmpLink(&(*imp_value)->gc_status);
  203. return true;
  204. }
  205. ResultType importFile(FUNC) {
  206. bool is_new = false;
  207. bool is_lock = st->u.import_file.is_lock;
  208. Statement *file = st->u.import_file.file;
  209. int status;
  210. char *split_path = NULL;
  211. char *path = NULL;
  212. LinkValue *imp_value = NULL;
  213. char md5_str[MD5_STRING];
  214. setResultCore(result);
  215. gc_freeze(inter, var_list, NULL, true);
  216. importFileCore(&path, &split_path, &status, CFUNC(file, var_list, result, belong));
  217. if (!CHECK_RESULT(result))
  218. goto return_;
  219. getFileMd5(path, md5_str);
  220. if (!getPackage(&imp_value, md5_str, split_path, status, &path, &is_new, is_lock, CFUNC(file, var_list, result, belong)))
  221. goto return_;
  222. freeResult(result);
  223. if (st->u.import_file.as == NULL) {
  224. wchar_t *name_ = memStrToWcs(split_path, false);
  225. addStrVar(name_, false, is_new, imp_value, LINEFILE, false, CNEXT_NT);
  226. memFree(name_);
  227. }
  228. else
  229. assCore(st->u.import_file.as, imp_value, false, is_new, CNEXT_NT);
  230. if (CHECK_RESULT(result)) // 若没有出现错误则设定none
  231. setResult(result, inter);
  232. gc_freeTmpLink(&imp_value->gc_status);
  233. return_:
  234. memFree(split_path);
  235. memFree(path);
  236. gc_freeze(inter, var_list, NULL, false);
  237. return result->type;
  238. }
  239. static ResultType importParameter(fline line, char *file, Parameter *call_pt, Parameter *func_pt, VarList *func_var, LinkValue *imp_belong, FUNC_NT) {
  240. Argument *call = NULL;
  241. setResultCore(result);
  242. call = getArgument(call_pt, false, CFUNC_NT(var_list, result, imp_belong));
  243. if (!CHECK_RESULT(result)) {
  244. freeArgument(call, false);
  245. return result->type;
  246. }
  247. freeResult(result);
  248. setParameterCore(line, file, call, func_pt, func_var, CNEXT_NT);
  249. freeArgument(call, false);
  250. return result->type;
  251. }
  252. ResultType fromImportFile(FUNC) {
  253. int status;
  254. bool is_new;
  255. bool is_lock = st->u.from_import_file.is_lock;
  256. Statement *file = st->u.from_import_file.file;
  257. char *split_path = NULL;
  258. char *path = NULL;
  259. char md5_str[MD5_STRING]; // TODO-szh 设置为空字符串
  260. VarList *imp_var = NULL;
  261. LinkValue *imp_value;
  262. Parameter *pt = st->u.from_import_file.pt;
  263. Parameter *as = st->u.from_import_file.as != NULL ? st->u.from_import_file.as : st->u.from_import_file.pt;
  264. setResultCore(result);
  265. gc_freeze(inter, var_list, NULL, true);
  266. importFileCore(&path, &split_path, &status, CFUNC(file, var_list, result, belong));
  267. if (!CHECK_RESULT(result))
  268. goto return_;
  269. getFileMd5(path, md5_str);
  270. if (!getPackage(&imp_value, md5_str, split_path, status, &path, &is_new, is_lock, CFUNC(file, var_list, result, belong)))
  271. goto return_;
  272. imp_var = imp_value->value->object.var;
  273. if (is_new) {
  274. wchar_t *wcs;
  275. LinkValue *string;
  276. freeResult(result);
  277. wcs = memStrToWcs(split_path, false);
  278. makeStringValue(wcs, st->line, st->code_file, CFUNC_NT(var_list, result, imp_value));
  279. memFree(wcs);
  280. string = result->value;
  281. result->value = NULL;
  282. freeResult(result);
  283. newObjectSetting(string, st->line, st->code_file, CFUNC_NT(var_list, result, imp_value));
  284. gc_freeTmpLink(&string->gc_status);
  285. }
  286. freeResult(result);
  287. if (pt != NULL) {
  288. importParameter(st->line, st->code_file, pt, as, var_list, imp_value, CFUNC_NT(imp_var, result, belong));
  289. if (!CHECK_RESULT(result)) {
  290. gc_freeTmpLink(&imp_value->gc_status);
  291. goto return_;
  292. }
  293. }
  294. else
  295. updateHashTable(var_list->hashtable, imp_var->hashtable, inter);
  296. setResult(result, inter);
  297. gc_freeTmpLink(&imp_value->gc_status);
  298. return_:
  299. memFree(path);
  300. memFree(split_path);
  301. gc_freeze(inter, var_list, NULL, false);
  302. return result->type;
  303. }