runfile.c 10 KB

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