front.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. #include <dirent.h>
  2. #include <rid.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #define STYLE_9
  6. #include <fn85.h>
  7. #include <rid_fn85.h>
  8. #include <unfy/unfy.h>
  9. #include <unfy/unfp.h>
  10. #include <rls/rls.h>
  11. #include <rls/rlsp.h>
  12. #define NIT_SHORT_NAMES
  13. #include <nit/list.h>
  14. #include <nit/set.h>
  15. #include <nit/map.h>
  16. #include <nit/err.h>
  17. #include <nit/file.h>
  18. #include <nit/buf.h>
  19. #include <nit/gap.h>
  20. #include "utilize.h"
  21. #include "examine.h"
  22. #include "front.h"
  23. /* More */
  24. static Ilk_more *
  25. ilk_more(Ilk_lib *lib, uint64_t srch_cnt, uint16_t act)
  26. {
  27. Ilk_more *more = lib->old_mores;
  28. if (more) {
  29. lib->old_mores = *(Ilk_more **) more;
  30. } else if (!(more = malloc(sizeof(*more)))) {
  31. err_std(&lib->err);
  32. return NULL;
  33. }
  34. more->srch_cnt = srch_cnt;
  35. more->act = act;
  36. return more;
  37. }
  38. static void
  39. more_free(void *dat, void *extra)
  40. {
  41. Ilk_more *more = dat;
  42. Ilk_sys *sys = extra;
  43. *(Ilk_more **) more = sys->lib->old_mores;
  44. sys->lib->old_mores = more;
  45. }
  46. static size_t
  47. more_len(const Rls_rule *rule, void *extra)
  48. {
  49. (void) rule;
  50. (void) extra;
  51. return sizeof(uint64_t) + sizeof(uint16_t);
  52. }
  53. static size_t
  54. more_write(const Rls_rule *rule, unsigned char *buf, void *extra)
  55. {
  56. Ilk_more *more = rule->dat;
  57. (void) buf;
  58. (void) extra;
  59. *(uint64_t *) buf = htole64(more->srch_cnt);
  60. buf += sizeof(uint64_t);
  61. *(uint16_t *) buf = htole16(more->act);
  62. return sizeof(uint64_t) + sizeof(uint16_t);
  63. }
  64. static int
  65. act_set(Ilk_sys *sys, Rls_rule *rule, uint16_t act_num, size_t pos)
  66. {
  67. if (act_num > sys->lib->act_dat.max_num) {
  68. err_cust(&sys->lib->err,
  69. "Invalid actable function number from deser");
  70. err_pos(&sys->lib->err, pos);
  71. return -1;
  72. }
  73. rule->u.act = sys->lib->act_dat.table[act_num];
  74. return 0;
  75. }
  76. static int
  77. more_deser(Rls_rule *rule, const unsigned char *buf, size_t *pos, void *extra)
  78. {
  79. Ilk_sys *sys = extra;
  80. Ilk_more *more = malloc(sizeof(*more));
  81. Ilk_rule_path path;
  82. if (!more) {
  83. err_std(&sys->lib->err);
  84. err_pos(&sys->lib->err, *pos);
  85. goto err_more;
  86. }
  87. more->srch_cnt = le64toh(*(uint64_t *) buf);
  88. *pos += sizeof(uint64_t);
  89. buf += sizeof(uint64_t);
  90. more->act = le16toh(*(uint16_t *) buf);
  91. if (more->act &&
  92. act_set(sys, rule, more->act, *pos) < 0) {
  93. free(more);
  94. goto err_act;
  95. }
  96. *pos += sizeof(uint16_t);
  97. rule->dat = more;
  98. return 0;
  99. err_act:
  100. free(more);
  101. ilk_rule_path(path, rule->id);
  102. err_where(&sys->lib->err, path);
  103. err_more:
  104. return -1;
  105. }
  106. static const char *
  107. more_deserable(const unsigned char *buf, size_t *pos, size_t max, void *extra)
  108. {
  109. (void) buf;
  110. (void) extra;
  111. if ((*pos += sizeof(uint64_t) + sizeof(uint16_t)) > max) {
  112. *pos = max;
  113. return "Not enough space for moredata";
  114. }
  115. return NULL;
  116. }
  117. /* Core code */
  118. static Rls_rule **
  119. ilk_bend(const Unfy_term *term, Unfy_stat *stat, void *extra)
  120. {
  121. Ilk_grp *grp = ilk_grp_get_term(extra, term, stat);
  122. if (!grp)
  123. return NULL;
  124. return &grp->rules;
  125. }
  126. static int
  127. load_ilksys_file(Ilk_sys *sys)
  128. {
  129. Nit_err tmp;
  130. Nit_file file;
  131. if (file_open(&file, &sys->dir, "Ilksys", &sys->lib->err) < 0)
  132. goto err_not_open;
  133. if ((size_t) file.len < sizeof(sys->srch_cnt)) {
  134. err_cust(&sys->lib->err,
  135. "Ilksys file invalid, too small for search count");
  136. goto err_file;
  137. }
  138. if (file_content(&file, &sys->lib->buf.bytes,
  139. &sys->lib->buf.size, &sys->lib->err) < 0)
  140. goto err_file;
  141. sys->srch_cnt = le64toh(*(uint64_t *) sys->lib->buf.bytes);
  142. if (file_close(&file, &sys->lib->err))
  143. goto err_not_open;
  144. return 0;
  145. err_file:
  146. file_close(&file, &tmp);
  147. err_not_open:
  148. return -1;
  149. }
  150. static void
  151. srlz_init(Rlsp_srlz *srlz)
  152. {
  153. srlz->dat_free = more_free;
  154. srlz->len = more_len;
  155. srlz->write = more_write;
  156. srlz->deser = more_deser;
  157. srlz->deserable = more_deserable;
  158. }
  159. static void
  160. act_dat_init(Ilk_act_dat *act_dat, uint16_t max_num, Rls_act *table)
  161. {
  162. act_dat->max_num = max_num;
  163. act_dat->table = table;
  164. }
  165. void
  166. ilk_lib_init(Ilk_lib *lib, uint16_t act_max, Rls_act *act_table)
  167. {
  168. act_dat_init(&lib->act_dat, act_max, act_table);
  169. lib->bend = ilk_bend;
  170. srlz_init(&lib->srlz);
  171. lib->old_grps = NULL;
  172. lib->old_mores = NULL;
  173. lib->old_syss = NULL;
  174. rls_recycle_init(&lib->rec);
  175. lib->stack = NULL;
  176. buf_init(&lib->buf, 0);
  177. err_init(&lib->err);
  178. lib->testing = 0;
  179. }
  180. void
  181. ilk_lib_dispose(Ilk_lib *lib)
  182. {
  183. while (lib->old_grps) {
  184. Ilk_grp *tmp = lib->old_grps;
  185. lib->old_grps = (Ilk_grp *) tmp->rules;
  186. free(tmp);
  187. }
  188. while (lib->old_mores) {
  189. Ilk_more *tmp = lib->old_mores;
  190. lib->old_mores = *(Ilk_more **) tmp;
  191. free(tmp);
  192. }
  193. while (lib->old_syss) {
  194. Ilk_sys *tmp = lib->old_syss;
  195. lib->old_syss = (Ilk_sys *) tmp->lib;
  196. free(tmp);
  197. }
  198. buf_dispose(&lib->buf);
  199. entries_free(lib->stack);
  200. rls_recycle_empty(&lib->rec);
  201. err_dispose(&lib->err);
  202. }
  203. int
  204. ilk_sys_init(Ilk_sys *sys, Ilk_lib *lib, const char *dir_name)
  205. {
  206. sys->lib = lib;
  207. sys->srch_cnt = 0;;
  208. if (dir_init(&sys->dir, dir_name, &sys->lib->err))
  209. return -1;
  210. switch (file_exists(&sys->dir, "Ilksys", &sys->lib->err)) {
  211. case 1:
  212. if (load_ilksys_file(sys) < 0)
  213. return -1;
  214. /* fallthru */
  215. case 0:
  216. break;
  217. default:
  218. goto err_dir;
  219. }
  220. if (map_init(&sys->grps, 0) < 0) {
  221. err_cust(&sys->lib->err, "Failed to init grps");
  222. goto err_dir;
  223. }
  224. if (map_init(&sys->srchs, 0) < 0) {
  225. err_cust(&sys->lib->err, "Failed to init srchs");
  226. goto err_srchs;
  227. }
  228. return 0;
  229. err_srchs:
  230. map_dispose(&sys->grps);
  231. err_dir:
  232. dir_dispose(&sys->dir);
  233. return -1;
  234. }
  235. static void
  236. grp_free(Ilk_grp *grp, Ilk_sys *sys)
  237. {
  238. ilk_grp_dispose(grp, sys);
  239. free(grp);
  240. }
  241. static void
  242. sys_grps_free(void *key, void *val, void *extra)
  243. {
  244. (void) key;
  245. grp_free(val, extra);
  246. }
  247. static void
  248. sys_srchs_free(void *key, void *val, void *extra)
  249. {
  250. Ilk_sys *sys = extra;
  251. (void) key;
  252. rls_recycle_srch(&sys->lib->rec, val);
  253. }
  254. void
  255. ilk_sys_dispose(Ilk_sys *sys)
  256. {
  257. map_empty(&sys->grps, sys_grps_free, sys);
  258. map_empty(&sys->srchs, sys_srchs_free, sys);
  259. map_dispose(&sys->grps);
  260. map_dispose(&sys->srchs);
  261. dir_dispose(&sys->dir);
  262. }
  263. Ilk_sys *
  264. ilk_sys(Ilk_lib *lib, const char *dir_name)
  265. {
  266. Ilk_sys *sys = lib->old_syss;
  267. if (sys) {
  268. lib->old_syss = (Ilk_sys *) sys->lib;
  269. } else if (!(sys = malloc(sizeof(*sys)))) {
  270. err_std(&lib->err);
  271. err_where(&lib->err, "sys");
  272. return NULL;
  273. }
  274. if (ilk_sys_init(sys, lib, dir_name) < 0) {
  275. free(sys);
  276. return NULL;
  277. }
  278. return sys;
  279. }
  280. void
  281. ilk_sys_rec(Ilk_sys *sys)
  282. {
  283. Ilk_lib *lib = sys->lib;
  284. ilk_sys_dispose(sys);
  285. sys->lib = (Ilk_lib *) lib->old_syss;
  286. lib->old_syss = sys;
  287. }
  288. static int
  289. save_ilksys_file(Ilk_sys *sys)
  290. {
  291. Nit_err tmp;
  292. Nit_file file;
  293. uint64_t save_cnt = htole64(sys->srch_cnt);
  294. if (file_open(&file, &sys->dir, "Ilksys", &sys->lib->err) < 0)
  295. goto err_no_close;
  296. if (file_set(&file, &save_cnt, sizeof(save_cnt), &sys->lib->err) < 0)
  297. goto err_close;
  298. if (file_close(&file, &sys->lib->err) < 0)
  299. goto err_no_close;
  300. return 0;
  301. err_close:
  302. file_close(&file, &tmp);
  303. err_no_close:
  304. return -1;
  305. }
  306. int
  307. ilk_sys_save(Ilk_sys *sys)
  308. {
  309. Nit_iter iter;
  310. if (save_ilksys_file(sys) < 0)
  311. return -1;
  312. if (!sys->grps.entry_num)
  313. goto srchs;
  314. iter_init(&iter, &sys->grps);
  315. do
  316. if (ilk_grp_save(iter_val(&iter), sys) < 0)
  317. return -1;
  318. while (iter_next(&iter));
  319. srchs:
  320. if (!sys->srchs.entry_num)
  321. goto end;
  322. iter_init(&iter, &sys->srchs);
  323. do {
  324. uint32_t size;
  325. uint64_t srch_cnt = *(uint64_t *) iter_key(&iter, &size);
  326. rlsp_srch_print(iter_val(&iter), 1);
  327. if (ilk_srch_save(sys, iter_val(&iter), srch_cnt) < 0)
  328. return -1;
  329. } while (iter_next(&iter));
  330. end:
  331. return 0;
  332. }
  333. const char *
  334. ilk_rule_valid(const Unfy_term *head, const Unfy_list *body)
  335. {
  336. static const char *empty_list = "Rule head cannot be an empty list";
  337. static const char *no_const = "Rule head list must start with const";
  338. static const char *invalid_type = "Rule head has invalid type";
  339. const char *error;
  340. if (!head)
  341. return 0;
  342. switch (head->type) {
  343. case UNFY_CONST:
  344. break;
  345. case UNFY_LIST:
  346. if (!head->u.list)
  347. return empty_list;
  348. if (head->u.list->term->type != UNFY_CONST)
  349. return no_const;
  350. break;
  351. default:
  352. return invalid_type;
  353. }
  354. for (; body; body = body->next)
  355. if ((error = ilk_rule_valid(body->term, NULL))) {
  356. if (error == empty_list)
  357. return "Rule body term cannot "
  358. "be an empty list";
  359. if (error == no_const)
  360. return "Rule body term list must "
  361. "start with const";
  362. return "Rule body term has invalid type";
  363. }
  364. return NULL;
  365. }
  366. Ilk_grp *
  367. ilk_rule_add(Ilk_sys *sys, const Rid id, Unfy_term *head, Unfy_list *body,
  368. uint64_t srch_cnt)
  369. {
  370. const char *error;
  371. Ilk_more *more;
  372. Ilk_grp *grp;
  373. if ((error = ilk_rule_valid(head, body))) {
  374. err_cust(&sys->lib->err, error);
  375. return NULL;
  376. }
  377. if (!(grp = ilk_grp_get_term(sys, head, NULL)) ||
  378. !(more = ilk_more(sys->lib, srch_cnt, 0)))
  379. return NULL;
  380. if (rls_rule_add(&grp->rules, id, head,
  381. body, more, &sys->lib->rec) < 0) {
  382. err_cust(&sys->lib->err, "Failed to add rule");
  383. free(more);
  384. return NULL;
  385. }
  386. grp->changed = 1;
  387. grp->accessed = 1;
  388. return grp;
  389. }
  390. Ilk_grp *
  391. ilk_act_add(Ilk_sys *sys, const Rid id, Unfy_term *head, uint16_t act_num)
  392. {
  393. const char *error;
  394. Ilk_more *more;
  395. Ilk_grp *grp;
  396. if (!act_num || act_num > sys->lib->act_dat.max_num) {
  397. err_cust(&sys->lib->err, "Act num invalid");
  398. return NULL;
  399. }
  400. if ((error = ilk_rule_valid(head, NULL))) {
  401. err_cust(&sys->lib->err, error);
  402. return NULL;
  403. }
  404. if (!(grp = ilk_grp_get_term(sys, head, NULL)) ||
  405. !(more = ilk_more(sys->lib, 0, act_num)))
  406. return NULL;
  407. if (rls_rule_add_actable(&grp->rules, id, head,
  408. sys->lib->act_dat.table[act_num],
  409. more, &sys->lib->rec) < 0) {
  410. err_cust(&sys->lib->err, "Failed to add actable rule");
  411. free(more);
  412. return NULL;
  413. }
  414. grp->changed = 1;
  415. grp->accessed = 1;
  416. return grp;
  417. }
  418. Unfy_stat
  419. ilk_srch_n_save(Ilk_sys *sys, Rls_srch **srch_ref, Unfy_term *term,
  420. int *limit, Rls_info *info)
  421. {
  422. Unfy_stat stat = ilk_srch(sys, srch_ref, term, limit, info);
  423. if (stat == UNFY_YES &&
  424. ilk_srch_save(sys, *srch_ref, sys->srch_cnt) < 0)
  425. return UNFY_ERR;
  426. return stat;
  427. }