2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include "sysevents.h"
45 struct srchlist *next, *prev;
62 wchar_t *begstr, *endstr, *onestr;
71 static void trycommit(void);
73 struct search *searches = NULL;
74 static struct srchlist *searchqueue = NULL;
75 static struct timer *committimer = NULL;
76 GCBCHAIN(newsrchcb, struct search *);
78 wchar_t *regexunquotesimple(wchar_t *re)
80 wchar_t *specials, *buf, *p;
82 specials = L"\\^$.*+?[{()|";
83 buf = smalloc((wcslen(re) + 1) * sizeof(wchar_t));
85 for(; *re != L'\0'; re++)
97 if(wcschr(specials, *re) != NULL)
109 static void freesln(struct wcslist *ln, struct wcslist **list)
112 ln->prev->next = ln->next;
114 ln->next->prev = ln->prev;
121 void freesl(struct wcslist **list)
124 freesln(*list, list);
127 static struct wcslist *newsl(struct wcslist **list, wchar_t *str)
131 ln = smalloc(sizeof(*ln));
132 memset(ln, 0, sizeof(*ln));
133 ln->str = swcsdup(str);
134 ln->len = wcslen(str);
143 static void slmerge1(struct wcslist **list, wchar_t *str)
146 struct wcslist *cur, *next;
149 for(cur = *list; cur != NULL; cur = next)
154 if(((len < cur->len) && wcsexists(cur->str, str)) || !wcscmp(str, cur->str))
156 } else if(len > cur->len) {
157 if(wcsexists(str, cur->str))
164 void slmergemax(struct wcslist **dest, struct wcslist *src)
166 for(; src != NULL; src = src->next)
167 slmerge1(dest, src->str);
170 static struct wcslist *makeminlist1(wchar_t *s1, wchar_t *s2)
174 struct wcslist *list;
177 for(p1 = s1; *p1 != L'\0'; p1++)
179 for(p2 = s2; *p2 != L'\0'; p2++)
181 for(i = 0; (p1[i] != L'\0') && (p2[i] != L'\0') && (towlower(p1[i]) == towlower(p2[i])); i++);
194 struct wcslist *slmergemin(struct wcslist *l1, struct wcslist *l2)
196 struct wcslist *cur1, *cur2, *list, *rlist;
199 for(cur1 = l1; cur1 != NULL; cur1 = cur1->next)
201 for(cur2 = l2; cur2 != NULL; cur2 = cur2->next)
203 rlist = makeminlist1(cur1->str, cur2->str);
204 slmergemax(&list, rlist);
211 static struct bound readbound(wchar_t *p, wchar_t **endret)
234 ret.min = wcstol(p, &p, 10);
241 ret.max = wcstol(p, &p, 10);
247 /* Cannot happen in a validated regex... */
248 flog(LOG_WARNING, "BUG? \"Impossible\" case encountered in search.c:readbound() (No `}' after `{'-type bound!");
263 #define fnc(p) do {if((p) != NULL) free(p); (p) = NULL; p ## size = 0; p ## data = 0;} while(0)
265 static struct reinfo analyzere(wchar_t *re, wchar_t **endret, wchar_t endc)
267 int i, commit, parsealt;
268 struct reinfo ret, sinf;
271 size_t cssize, csdata, nssize, nsdata, len1, len2, maxlen, minlen;
273 struct wcslist *list;
275 memset(&ret, 0, sizeof(ret));
276 commit = parsealt = 0;
279 cssize = csdata = nssize = nsdata = 0;
280 while((*re != endc) && !parsealt)
291 b = readbound(re, &re);
297 sinf = analyzere(re, &re, L')');
299 b = readbound(re, &re);
300 if(sinf.onestr != NULL)
302 for(i = 0; i < b.min; i++)
304 bufcat(cs, sinf.onestr, wcslen(sinf.onestr));
305 bufcat(ns, sinf.onestr, wcslen(sinf.onestr));
307 if((b.max == -1) || (b.max > b.min))
313 if(sinf.begstr != NULL)
314 bufcat(cs, sinf.begstr, wcslen(sinf.begstr));
315 if(sinf.endstr != NULL)
316 bufcat(ns, sinf.endstr, wcslen(sinf.endstr));
319 if(sinf.begstr != NULL)
321 if(sinf.endstr != NULL)
323 if(sinf.onestr != NULL)
326 slmergemax(&ret.strs, sinf.strs);
332 /* Must exist in a validated RE... */
333 while(*(re++) != L']');
343 /* A validated RE cannot end in a backslash, so fall
347 b = readbound(re, &re);
348 for(i = 0; i < b.min; i++)
350 if((b.max == -1) || (b.max > b.min))
361 ret.begstr = swcsdup(cs);
365 slmerge1(&ret.strs, cs);
381 ret.onestr = swcsdup(cs);
383 ret.endstr = swcsdup(cs);
384 slmerge1(&ret.strs, cs);
389 sinf = analyzere(re, &re, endc);
390 list = slmergemin(ret.strs, sinf.strs);
394 if(sinf.begstr != NULL)
396 if(ret.begstr != NULL)
398 for(i = 0; (sinf.begstr[i] != L'\0') && (ret.begstr != L'\0') && (ret.begstr[i] == sinf.begstr[i]); i++);
403 ret.begstr[i] = L'\0';
408 if(ret.begstr != NULL)
414 if(sinf.endstr != NULL)
416 if(ret.endstr != NULL)
418 len1 = wcslen(ret.endstr);
419 len2 = wcslen(sinf.endstr);
428 for(i = 1; (i <= minlen) && (ret.endstr[len1 - i] == sinf.endstr[len2 - i]); i++);
432 } else if(i <= maxlen) {
433 wmemmove(ret.endstr, ret.endstr + (len1 - i) + 1, i);
438 if(ret.endstr != NULL)
444 if(sinf.onestr != NULL)
446 if(ret.onestr != NULL)
448 /* XXX: Comparing beginning and end of the onestrs and
449 * create begstr and endstr if there isn't an exact
451 if(wcscmp(ret.onestr, sinf.onestr))
459 if(ret.onestr != NULL)
473 struct wcslist *regexfindstrings(wchar_t *re)
477 i = analyzere(re, NULL, L'\0');
487 static struct sexpr *newsexpr(void)
491 sexpr = smalloc(sizeof(*sexpr));
492 memset(sexpr, 0, sizeof(*sexpr));
497 void getsexpr(struct sexpr *sexpr)
502 void putsexpr(struct sexpr *sexpr)
504 if(--sexpr->refcount != 0)
510 if((sexpr->op == SOP_NAMERE) || (sexpr->op == SOP_LINKRE))
512 if(sexpr->d.re.sre != NULL)
513 free(sexpr->d.re.sre);
514 if(sexpr->d.re.inited)
515 regfree(&sexpr->d.re.cre);
517 if((sexpr->op == SOP_NAMESS) || (sexpr->op == SOP_LINKSS))
519 if(sexpr->d.s != NULL)
522 if(sexpr->op == SOP_HASHIS)
524 if(sexpr->d.hash != NULL)
525 freehash(sexpr->d.hash);
530 static struct tok *newtok(void)
534 tok = smalloc(sizeof(*tok));
535 memset(tok, 0, sizeof(*tok));
540 static void freetok(struct tok *tok)
542 if((tok->type == TOK_STR) && (tok->d.str != NULL))
544 if((tok->type == TOK_SE) && (tok->d.se != NULL))
549 static void pushtok(struct tok *tok, struct tok **st)
555 static struct tok *poptok(struct tok **st)
564 int calccost(struct sexpr *sexpr)
566 sexpr->tcost = sexpr->cost;
568 sexpr->tcost += calccost(sexpr->l);
570 sexpr->tcost += calccost(sexpr->r);
571 return(sexpr->tcost);
574 struct sexpr *parsesexpr(int argc, wchar_t **argv)
577 struct tok *st, *tok, *tok2;
584 for(i = 0; i < argc; i++)
586 pushtok(tok = newtok(), &st);
588 tok->d.str = swcsdup(argv[i]);
593 if((st->type == TOK_STR) && !wcscmp(st->d.str, L"("))
595 freetok(poptok(&st));
596 pushtok(tok = newtok(), &st);
599 } else if((st->type == TOK_STR) && !wcscmp(st->d.str, L")")) {
600 freetok(poptok(&st));
601 pushtok(tok = newtok(), &st);
604 } else if((st->type == TOK_STR) && (!wcsncmp(st->d.str, L"N~", 2) || !wcsncmp(st->d.str, L"L~", 2))) {
606 pushtok(tok = newtok(), &st);
609 if((wbuf = regexunquotesimple(tok2->d.str + 2)) != NULL)
611 if(tok2->d.str[0] == L'N')
612 sexpr->op = SOP_NAMESS;
614 sexpr->op = SOP_LINKSS;
618 if(tok2->d.str[0] == L'N')
619 sexpr->op = SOP_NAMERE;
621 sexpr->op = SOP_LINKRE;
623 if((buf = icwcstombs(tok2->d.str + 2, "UTF-8")) == NULL)
629 if(regcomp(&sexpr->d.re.cre, buf, REG_EXTENDED | REG_ICASE | REG_NOSUB))
637 sexpr->d.re.inited = 1;
638 sexpr->d.re.sre = swcsdup(tok2->d.str + 2);
640 getsexpr(tok->d.se = sexpr);
644 } else if((st->type == TOK_STR) && (!wcsncmp(st->d.str, L"S<", 2) || !wcsncmp(st->d.str, L"S=", 2) || !wcsncmp(st->d.str, L"S>", 2))) {
646 pushtok(tok = newtok(), &st);
649 if(tok2->d.str[1] == L'<')
650 sexpr->op = SOP_SIZELT;
651 else if(tok2->d.str[1] == L'=')
652 sexpr->op = SOP_SIZEEQ;
654 sexpr->op = SOP_SIZEGT;
655 sexpr->d.n = wcstol(tok2->d.str + 2, NULL, 0);
657 getsexpr(tok->d.se = sexpr);
661 } else if((st->type == TOK_STR) && !wcsncmp(st->d.str, L"H=", 2)) {
663 pushtok(tok = newtok(), &st);
666 sexpr->op = SOP_HASHIS;
667 if((sexpr->d.hash = parsehash(tok2->d.str + 2)) == NULL)
674 getsexpr(tok->d.se = sexpr);
678 } else if((std >= 3) && (st->type == TOK_CP) && (st->next->type == TOK_SE) && (st->next->next->type == TOK_OP)) {
679 freetok(poptok(&st));
681 freetok(poptok(&st));
685 } else if((std >= 2) && (st->type == TOK_SE) && (st->next->type == TOK_STR) && !wcscmp(st->next->d.str, L"!")) {
689 getsexpr(sexpr->l = st->d.se);
690 freetok(poptok(&st));
691 freetok(poptok(&st));
692 pushtok(tok = newtok(), &st);
694 getsexpr(tok->d.se = sexpr);
698 } else if((std >= 3) && (st->type == TOK_SE) && (st->next->type == TOK_STR) && (!wcscmp(st->next->d.str, L"&") || !wcscmp(st->next->d.str, L"|")) && (st->next->next->type == TOK_SE)) {
700 if(!wcscmp(st->next->d.str, L"&"))
705 getsexpr(sexpr->l = st->next->next->d.se);
706 getsexpr(sexpr->r = st->d.se);
707 freetok(poptok(&st));
708 freetok(poptok(&st));
709 freetok(poptok(&st));
710 pushtok(tok = newtok(), &st);
712 getsexpr(tok->d.se = sexpr);
719 if((st == NULL) || (st->next != NULL) || (st->type != TOK_SE))
721 getsexpr(sexpr = st->d.se);
728 freetok(poptok(&st));
732 void optsexpr(struct sexpr *sexpr)
736 if((sexpr->l != NULL) && (sexpr->r != NULL))
738 if(sexpr->l->tcost > sexpr->r->tcost)
751 struct wcslist *findsexprstrs(struct sexpr *sexpr)
753 struct wcslist *list, *l1, *l2;
759 list = findsexprstrs(sexpr->l);
760 l1 = findsexprstrs(sexpr->r);
761 slmergemax(&list, l1);
765 l1 = findsexprstrs(sexpr->l);
766 l2 = findsexprstrs(sexpr->r);
767 list = slmergemin(l1, l2);
773 list = regexfindstrings(sexpr->d.re.sre);
777 slmerge1(&list, sexpr->d.s);
785 static void unlinksqueue(struct srchlist *ln)
788 ln->prev->next = ln->next;
790 ln->next->prev = ln->prev;
791 if(ln == searchqueue)
792 searchqueue = ln->next;
796 static void ctexpire(int cancelled, void *data)
803 static void estimatequeue(void)
805 struct srchlist *cur;
806 struct srchfnnlist *ln;
809 if(searchqueue == NULL)
811 start = now = time(NULL);
812 for(ln = searchqueue->srch->fnl; ln != NULL; ln = ln->next)
814 if((ln->fn->lastsrch != 0) && (ln->fn->lastsrch + ln->fn->srchwait > start))
815 start = ln->fn->lastsrch + ln->fn->srchwait;
817 if(start != searchqueue->srch->eta)
819 searchqueue->srch->eta = start;
820 CBCHAINDOCB(searchqueue->srch, search_eta, searchqueue->srch);
822 for(cur = searchqueue->next; cur != NULL; cur = cur->next)
825 for(ln = cur->srch->fnl; ln != NULL; ln = ln->next)
827 if(now + ln->fn->srchwait > start)
828 start = now + ln->fn->srchwait;
830 if(start != cur->srch->eta)
832 cur->srch->eta = start;
833 CBCHAINDOCB(cur->srch, search_eta, cur->srch);
836 if((committimer == NULL) || ((time_t)committimer->at != searchqueue->srch->eta))
838 if(committimer != NULL)
839 canceltimer(committimer);
840 committimer = timercallback(searchqueue->srch->eta, ctexpire, NULL);
844 struct search *findsearch(int id)
848 for(srch = searches; srch != NULL; srch = srch->next)
856 struct search *newsearch(wchar_t *owner, struct sexpr *sexpr)
861 srch = smalloc(sizeof(*srch));
862 memset(srch, 0, sizeof(*srch));
864 srch->owner = swcsdup(owner);
865 if((srch->sexpr = sexpr) != NULL)
866 getsexpr(srch->sexpr);
867 CBCHAININIT(srch, search_eta);
868 CBCHAININIT(srch, search_commit);
869 CBCHAININIT(srch, search_result);
870 CBCHAININIT(srch, search_destroy);
871 srch->next = searches;
874 searches->prev = srch;
879 static void srchexpire(int cancelled, struct search *srch)
881 srch->freetimer = NULL;
886 static void trycommit(void)
888 struct srchfnnlist *ln;
892 if(searchqueue == NULL)
894 srch = searchqueue->srch;
896 for(ln = srch->fnl; ln != NULL; ln = ln->next)
898 if(now < ln->fn->lastsrch + ln->fn->srchwait)
903 unlinksqueue(searchqueue);
904 srch->state = SRCH_RUN;
905 srch->eta = time(NULL);
906 srch->committime = ntime();
907 srch->freetimer = timercallback(ntime() + 300, (void (*)(int, void *))srchexpire, srch);
908 CBCHAINDOCB(srch, search_commit, srch);
909 for(ln = srch->fnl; ln != NULL; ln = ln->next)
910 fnetsearch(ln->fn, srch, ln);
914 void freesearch(struct search *srch)
916 struct srchfnnlist *ln;
917 struct srchlist *sln;
919 if(srch->prev != NULL)
920 srch->prev->next = srch->next;
921 if(srch->next != NULL)
922 srch->next->prev = srch->prev;
924 searches = srch->next;
926 if(srch->freetimer != NULL)
927 canceltimer(srch->freetimer);
928 CBCHAINDOCB(srch, search_destroy, srch);
929 CBCHAINFREE(srch, search_eta);
930 CBCHAINFREE(srch, search_commit);
931 CBCHAINFREE(srch, search_result);
932 CBCHAINFREE(srch, search_destroy);
933 while(srch->results != NULL)
934 freesrchres(srch->results);
935 for(sln = searchqueue; sln != NULL; sln = sln->next)
937 if(sln->srch == srch)
943 while(srch->fnl != NULL)
946 srch->fnl = ln->next;
947 CBCHAINDOCB(ln, searchfnl_destroy, ln);
948 CBCHAINFREE(ln, searchfnl_destroy);
952 if(srch->sexpr != NULL)
953 putsexpr(srch->sexpr);
954 if(srch->owner != NULL)
959 void searchaddfn(struct search *srch, struct fnetnode *fn)
961 struct srchfnnlist *ln;
963 for(ln = srch->fnl; ln != NULL; ln = ln->next)
968 ln = smalloc(sizeof(*ln));
969 memset(ln, 0, sizeof(*ln));
970 getfnetnode(ln->fn = fn);
971 CBCHAININIT(ln, searchfnl_destroy);
972 ln->next = srch->fnl;
976 static void linksearch(struct search *srch, struct srchlist *prev)
978 struct srchlist *new;
980 new = smalloc(sizeof(*new));
985 new->next = searchqueue;
986 if(searchqueue != NULL)
987 searchqueue->prev = new;
991 if((new->next = prev->next) != NULL)
992 new->next->prev = new;
995 GCBCHAINDOCB(newsrchcb, srch);
1000 * queuesearch is also the "scheduler" function - it finds a suitable
1001 * place in the queue for the new search. I'll make a weak attempt at
1002 * describing the algorithm:
1003 * First, we find the first search that doesn't have a lower priority
1004 * than this one. If there is no such, we just link this one onto the
1006 * Then, if we have a search of this priority in the queue with the
1007 * same owner as the new search, we set lastmine to the search after
1008 * that one, otherwise, lastmine is the first search of this
1009 * priority. If lastmine is discovered either to not exist (that is,
1010 * our last search is at the end of the queue), or to be of lower
1011 * priority (higher number), we link it in at the appropriate end.
1012 * Then, we find the next search of the same priority and owner as
1013 * lastmine, and link this search in before it. That should yield a
1014 * 'round-robin-like' scheduling within priority boundaries. I think.
1016 void queuesearch(struct search *srch)
1018 struct srchlist *cur, *lastmine, *prev;
1021 for(prev = NULL, cur = searchqueue; cur != NULL; prev = cur, cur = cur->next)
1023 if(cur->srch->prio >= srch->prio)
1028 linksearch(srch, prev);
1032 for(; cur != NULL; prev = cur, cur = cur->next)
1034 if(!wcscmp(cur->srch->owner, srch->owner))
1035 lastmine = cur->next;
1036 if(cur->srch->prio > srch->prio)
1039 if((lastmine == NULL) || (lastmine->srch->prio > srch->prio))
1041 linksearch(srch, prev);
1044 nexto = lastmine->srch->owner;
1045 for(cur = lastmine->next; cur != NULL; prev = cur, cur = cur->next)
1047 if(!wcscmp(cur->srch->owner, nexto))
1049 if(cur->srch->prio > srch->prio)
1054 linksearch(srch, prev);
1057 linksearch(srch, prev);
1060 static int srisvalid(struct srchres *sr, struct sexpr *sexpr)
1073 if(!srisvalid(sr, sexpr->l))
1075 return(srisvalid(sr, sexpr->r));
1077 if(srisvalid(sr, sexpr->l))
1079 return(srisvalid(sr, sexpr->r));
1081 return(!srisvalid(sr, sexpr->l));
1083 if((buf = icwcstombs(sr->filename, "UTF-8")) == NULL)
1085 ret = regexec(&sexpr->d.re.cre, buf, 0, NULL, 0);
1090 if(sr->fnet->filebasename != NULL)
1091 p = sr->fnet->filebasename(p);
1092 if((buf = icwcstombs(p, "UTF-8")) == NULL)
1094 ret = regexec(&sexpr->d.re.cre, buf, 0, NULL, 0);
1098 return(wcsexists(sr->filename, sexpr->d.s));
1101 if(sr->fnet->filebasename != NULL)
1102 p = sr->fnet->filebasename(p);
1103 return(wcsexists(p, sexpr->d.s));
1105 return(sr->size < sexpr->d.n);
1107 return(sr->size == sexpr->d.n);
1109 return(sr->size > sexpr->d.n);
1111 if(sr->hash == NULL)
1113 return(hashcmp(sr->hash, sexpr->d.hash));
1118 struct srchres *newsrchres(struct fnet *fnet, wchar_t *filename, wchar_t *peerid)
1122 sr = smalloc(sizeof(*sr));
1123 memset(sr, 0, sizeof(*sr));
1127 sr->filename = swcsdup(filename);
1128 sr->peerid = swcsdup(peerid);
1132 void freesrchres(struct srchres *sr)
1134 if(sr->next != NULL)
1135 sr->next->prev = sr->prev;
1136 if(sr->prev != NULL)
1137 sr->prev->next = sr->next;
1138 if(sr->srch != NULL)
1140 if(sr->srch->results == sr)
1141 sr->srch->results = sr->next;
1144 if(sr->hash != NULL)
1146 if(sr->filename != NULL)
1148 if(sr->peerid != NULL)
1150 if(sr->peernick != NULL)
1153 putfnetnode(sr->fn);
1157 struct srchres *dupsrchres(struct srchres *sr)
1159 struct srchres *new;
1161 new = smalloc(sizeof(*new));
1162 memset(new, 0, sizeof(*new));
1163 new->size = sr->size;
1164 new->slots = sr->slots;
1165 new->fnet = sr->fnet;
1166 if(sr->peerid != NULL)
1167 new->peerid = swcsdup(sr->peerid);
1168 if(sr->peernick != NULL)
1169 new->peernick = swcsdup(sr->peernick);
1170 if(sr->filename != NULL)
1171 new->filename = swcsdup(sr->filename);
1173 getfnetnode(new->fn = sr->fn);
1174 if(sr->hash != NULL)
1175 new->hash = duphash(sr->hash);
1179 static void linksr(struct search *srch, struct srchres *sr)
1182 sr->next = srch->results;
1183 if(srch->results != NULL)
1184 srch->results->prev = sr;
1187 sr->time = ntime() - srch->committime;
1191 void submitsrchres(struct srchres *sr)
1193 struct search *srch;
1194 struct srchfnnlist *ln;
1195 struct srchres *dsr;
1197 for(srch = searches; srch != NULL; srch = srch->next)
1199 if(srch->state == SRCH_RUN)
1201 if(!srisvalid(sr, srch->sexpr))
1203 for(ln = srch->fnl; ln != NULL; ln = ln->next)
1205 if(((sr->fn != NULL) && (ln->fn == sr->fn)) || ((sr->fn == NULL) && (sr->fnet == ln->fn->fnet)))
1210 dsr = dupsrchres(sr);
1212 CBCHAINDOCB(srch, search_result, srch, dsr);