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"
44 struct srchlist *next, *prev;
61 wchar_t *begstr, *endstr, *onestr;
70 static void trycommit(void);
72 struct search *searches = NULL;
73 static struct srchlist *searchqueue = NULL;
74 static struct timer *committimer = NULL;
75 GCBCHAIN(newsrchcb, struct search *);
77 wchar_t *regexunquotesimple(wchar_t *re)
79 wchar_t *specials, *buf, *p;
81 specials = L"\\^$.*+?[{()|";
82 buf = smalloc((wcslen(re) + 1) * sizeof(wchar_t));
84 for(; *re != L'\0'; re++)
96 if(wcschr(specials, *re) != NULL)
108 static void freesln(struct wcslist *ln, struct wcslist **list)
111 ln->prev->next = ln->next;
113 ln->next->prev = ln->prev;
120 void freesl(struct wcslist **list)
123 freesln(*list, list);
126 static struct wcslist *newsl(struct wcslist **list, wchar_t *str)
130 ln = smalloc(sizeof(*ln));
131 memset(ln, 0, sizeof(*ln));
132 ln->str = swcsdup(str);
133 ln->len = wcslen(str);
142 static void slmerge1(struct wcslist **list, wchar_t *str)
145 struct wcslist *cur, *next;
148 for(cur = *list; cur != NULL; cur = next)
153 if(((len < cur->len) && wcsexists(cur->str, str)) || !wcscmp(str, cur->str))
155 } else if(len > cur->len) {
156 if(wcsexists(str, cur->str))
163 void slmergemax(struct wcslist **dest, struct wcslist *src)
165 for(; src != NULL; src = src->next)
166 slmerge1(dest, src->str);
169 static struct wcslist *makeminlist1(wchar_t *s1, wchar_t *s2)
173 struct wcslist *list;
176 for(p1 = s1; *p1 != L'\0'; p1++)
178 for(p2 = s2; *p2 != L'\0'; p2++)
180 for(i = 0; (p1[i] != L'\0') && (p2[i] != L'\0') && (towlower(p1[i]) == towlower(p2[i])); i++);
193 struct wcslist *slmergemin(struct wcslist *l1, struct wcslist *l2)
195 struct wcslist *cur1, *cur2, *list, *rlist;
198 for(cur1 = l1; cur1 != NULL; cur1 = cur1->next)
200 for(cur2 = l2; cur2 != NULL; cur2 = cur2->next)
202 rlist = makeminlist1(cur1->str, cur2->str);
203 slmergemax(&list, rlist);
210 static struct bound readbound(wchar_t *p, wchar_t **endret)
233 ret.min = wcstol(p, &p, 10);
240 ret.max = wcstol(p, &p, 10);
246 /* Cannot happen in a validated regex... */
247 flog(LOG_WARNING, "BUG? \"Impossible\" case encountered in search.c:readbound() (No `}' after `{'-type bound!");
262 #define fnc(p) do {if((p) != NULL) free(p); (p) = NULL; p ## size = 0; p ## data = 0;} while(0)
264 static struct reinfo analyzere(wchar_t *re, wchar_t **endret, wchar_t endc)
266 int i, commit, parsealt;
267 struct reinfo ret, sinf;
270 size_t cssize, csdata, nssize, nsdata, len1, len2, maxlen, minlen;
272 struct wcslist *list;
274 memset(&ret, 0, sizeof(ret));
275 commit = parsealt = 0;
278 cssize = csdata = nssize = nsdata = 0;
279 while((*re != endc) && !parsealt)
290 b = readbound(re, &re);
296 sinf = analyzere(re, &re, L')');
298 b = readbound(re, &re);
299 if(sinf.onestr != NULL)
301 for(i = 0; i < b.min; i++)
303 bufcat(cs, sinf.onestr, wcslen(sinf.onestr));
304 bufcat(ns, sinf.onestr, wcslen(sinf.onestr));
306 if((b.max == -1) || (b.max > b.min))
312 if(sinf.begstr != NULL)
313 bufcat(cs, sinf.begstr, wcslen(sinf.begstr));
314 if(sinf.endstr != NULL)
315 bufcat(ns, sinf.endstr, wcslen(sinf.endstr));
318 if(sinf.begstr != NULL)
320 if(sinf.endstr != NULL)
322 if(sinf.onestr != NULL)
325 slmergemax(&ret.strs, sinf.strs);
331 /* Must exist in a validated RE... */
332 while(*(re++) != L']');
342 /* A validated RE cannot end in a backslash, so fall
346 b = readbound(re, &re);
347 for(i = 0; i < b.min; i++)
349 if((b.max == -1) || (b.max > b.min))
360 ret.begstr = swcsdup(cs);
364 slmerge1(&ret.strs, cs);
380 ret.onestr = swcsdup(cs);
382 ret.endstr = swcsdup(cs);
383 slmerge1(&ret.strs, cs);
388 sinf = analyzere(re, &re, endc);
389 list = slmergemin(ret.strs, sinf.strs);
393 if(sinf.begstr != NULL)
395 if(ret.begstr != NULL)
397 for(i = 0; (sinf.begstr[i] != L'\0') && (ret.begstr != L'\0') && (ret.begstr[i] == sinf.begstr[i]); i++);
401 ret.begstr[i] = L'\0';
405 if(ret.begstr != NULL)
411 if(sinf.endstr != NULL)
413 if(ret.endstr != NULL)
415 len1 = wcslen(ret.endstr);
416 len2 = wcslen(sinf.endstr);
425 for(i = 1; (i <= minlen) && (ret.endstr[len1 - i] == sinf.endstr[len2 - i]); i++);
429 wmemmove(ret.endstr, ret.endstr + (len1 - i) + 1, i);
433 if(ret.endstr != NULL)
439 if(sinf.onestr != NULL)
441 if(ret.onestr != NULL)
443 /* XXX: Comparing beginning and end of the onestrs and
444 * create begstr and endstr if there isn't an exact
446 if(wcscmp(ret.onestr, sinf.onestr))
454 if(ret.onestr != NULL)
468 struct wcslist *regexfindstrings(wchar_t *re)
472 i = analyzere(re, NULL, L'\0');
482 static struct sexpr *newsexpr(void)
486 sexpr = smalloc(sizeof(*sexpr));
487 memset(sexpr, 0, sizeof(*sexpr));
492 void getsexpr(struct sexpr *sexpr)
497 void putsexpr(struct sexpr *sexpr)
499 if(--sexpr->refcount != 0)
505 if((sexpr->op == SOP_NAMERE) || (sexpr->op == SOP_LINKRE))
507 if(sexpr->d.re.sre != NULL)
508 free(sexpr->d.re.sre);
509 if(sexpr->d.re.inited)
510 regfree(&sexpr->d.re.cre);
512 if((sexpr->op == SOP_NAMESS) || (sexpr->op == SOP_LINKSS))
514 if(sexpr->d.s != NULL)
520 static struct tok *newtok(void)
524 tok = smalloc(sizeof(*tok));
525 memset(tok, 0, sizeof(*tok));
530 static void freetok(struct tok *tok)
532 if((tok->type == TOK_STR) && (tok->d.str != NULL))
534 if((tok->type == TOK_SE) && (tok->d.se != NULL))
539 static void pushtok(struct tok *tok, struct tok **st)
545 static struct tok *poptok(struct tok **st)
554 int calccost(struct sexpr *sexpr)
556 sexpr->tcost = sexpr->cost;
558 sexpr->tcost += calccost(sexpr->l);
560 sexpr->tcost += calccost(sexpr->r);
561 return(sexpr->tcost);
564 struct sexpr *parsesexpr(int argc, wchar_t **argv)
567 struct tok *st, *tok, *tok2;
574 for(i = 0; i < argc; i++)
576 pushtok(tok = newtok(), &st);
578 tok->d.str = swcsdup(argv[i]);
583 if((st->type == TOK_STR) && !wcscmp(st->d.str, L"("))
585 freetok(poptok(&st));
586 pushtok(tok = newtok(), &st);
589 } else if((st->type == TOK_STR) && !wcscmp(st->d.str, L")")) {
590 freetok(poptok(&st));
591 pushtok(tok = newtok(), &st);
594 } else if((st->type == TOK_STR) && (!wcsncmp(st->d.str, L"N~", 2) || !wcsncmp(st->d.str, L"L~", 2))) {
596 pushtok(tok = newtok(), &st);
599 if((wbuf = regexunquotesimple(tok2->d.str + 2)) != NULL)
601 if(tok2->d.str[0] == L'N')
602 sexpr->op = SOP_NAMESS;
604 sexpr->op = SOP_LINKSS;
608 if(tok2->d.str[0] == L'N')
609 sexpr->op = SOP_NAMERE;
611 sexpr->op = SOP_LINKRE;
613 if((buf = icwcstombs(tok2->d.str + 2, "UTF-8")) == NULL)
619 if(regcomp(&sexpr->d.re.cre, buf, REG_EXTENDED | REG_ICASE | REG_NOSUB))
627 sexpr->d.re.inited = 1;
628 sexpr->d.re.sre = swcsdup(tok2->d.str + 2);
630 getsexpr(tok->d.se = sexpr);
634 } 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))) {
636 pushtok(tok = newtok(), &st);
639 if(tok2->d.str[1] == L'<')
640 sexpr->op = SOP_SIZELT;
641 else if(tok2->d.str[1] == L'=')
642 sexpr->op = SOP_SIZEEQ;
644 sexpr->op = SOP_SIZEGT;
645 sexpr->d.n = wcstol(tok2->d.str + 2, NULL, 0);
647 getsexpr(tok->d.se = sexpr);
651 } else if((std >= 3) && (st->type == TOK_CP) && (st->next->type == TOK_SE) && (st->next->next->type == TOK_OP)) {
652 freetok(poptok(&st));
654 freetok(poptok(&st));
658 } else if((std >= 2) && (st->type == TOK_SE) && (st->next->type == TOK_STR) && !wcscmp(st->next->d.str, L"!")) {
662 getsexpr(sexpr->l = st->d.se);
663 freetok(poptok(&st));
664 freetok(poptok(&st));
665 pushtok(tok = newtok(), &st);
667 getsexpr(tok->d.se = sexpr);
671 } 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)) {
673 if(!wcscmp(st->next->d.str, L"&"))
678 getsexpr(sexpr->l = st->next->next->d.se);
679 getsexpr(sexpr->r = st->d.se);
680 freetok(poptok(&st));
681 freetok(poptok(&st));
682 freetok(poptok(&st));
683 pushtok(tok = newtok(), &st);
685 getsexpr(tok->d.se = sexpr);
692 if((st == NULL) || (st->next != NULL) || (st->type != TOK_SE))
694 getsexpr(sexpr = st->d.se);
701 freetok(poptok(&st));
705 void optsexpr(struct sexpr *sexpr)
709 if((sexpr->l != NULL) && (sexpr->r != NULL))
711 if(sexpr->l->tcost > sexpr->r->tcost)
724 struct wcslist *findsexprstrs(struct sexpr *sexpr)
726 struct wcslist *list, *l1, *l2;
732 list = findsexprstrs(sexpr->l);
733 l1 = findsexprstrs(sexpr->r);
734 slmergemax(&list, l1);
738 l1 = findsexprstrs(sexpr->l);
739 l2 = findsexprstrs(sexpr->r);
740 list = slmergemin(l1, l2);
748 list = regexfindstrings(sexpr->d.re.sre);
752 slmerge1(&list, sexpr->d.s);
760 static void unlinksqueue(struct srchlist *ln)
763 ln->prev->next = ln->next;
765 ln->next->prev = ln->prev;
766 if(ln == searchqueue)
767 searchqueue = ln->next;
771 static void ctexpire(int cancelled, void *data)
778 static void estimatequeue(void)
780 struct srchlist *cur;
781 struct srchfnnlist *ln;
784 if(searchqueue == NULL)
786 start = now = time(NULL);
787 for(ln = searchqueue->srch->fnl; ln != NULL; ln = ln->next)
789 if((ln->fn->lastsrch != 0) && (ln->fn->lastsrch + ln->fn->srchwait > start))
790 start = ln->fn->lastsrch + ln->fn->srchwait;
792 if(start != searchqueue->srch->eta)
794 searchqueue->srch->eta = start;
795 CBCHAINDOCB(searchqueue->srch, search_eta, searchqueue->srch);
797 for(cur = searchqueue->next; cur != NULL; cur = cur->next)
800 for(ln = cur->srch->fnl; ln != NULL; ln = ln->next)
802 if(now + ln->fn->srchwait > start)
803 start = now + ln->fn->srchwait;
805 if(start != cur->srch->eta)
807 cur->srch->eta = start;
808 CBCHAINDOCB(cur->srch, search_eta, cur->srch);
811 if((committimer == NULL) || ((time_t)committimer->at != searchqueue->srch->eta))
813 if(committimer != NULL)
814 canceltimer(committimer);
815 committimer = timercallback(searchqueue->srch->eta, ctexpire, NULL);
819 struct search *findsearch(int id)
823 for(srch = searches; srch != NULL; srch = srch->next)
831 struct search *newsearch(wchar_t *owner, struct sexpr *sexpr)
836 srch = smalloc(sizeof(*srch));
837 memset(srch, 0, sizeof(*srch));
839 srch->owner = swcsdup(owner);
840 if((srch->sexpr = sexpr) != NULL)
841 getsexpr(srch->sexpr);
842 CBCHAININIT(srch, search_eta);
843 CBCHAININIT(srch, search_commit);
844 CBCHAININIT(srch, search_result);
845 CBCHAININIT(srch, search_destroy);
846 srch->next = searches;
849 searches->prev = srch;
854 static void srchexpire(int cancelled, struct search *srch)
856 srch->freetimer = NULL;
861 static void trycommit(void)
863 struct srchfnnlist *ln;
867 if(searchqueue == NULL)
869 srch = searchqueue->srch;
871 for(ln = srch->fnl; ln != NULL; ln = ln->next)
873 if(now < ln->fn->lastsrch + ln->fn->srchwait)
878 unlinksqueue(searchqueue);
879 srch->state = SRCH_RUN;
880 srch->eta = time(NULL);
881 srch->committime = ntime();
882 srch->freetimer = timercallback(ntime() + 300, (void (*)(int, void *))srchexpire, srch);
883 CBCHAINDOCB(srch, search_commit, srch);
884 for(ln = srch->fnl; ln != NULL; ln = ln->next)
885 fnetsearch(ln->fn, srch, ln);
889 void freesearch(struct search *srch)
891 struct srchfnnlist *ln;
892 struct srchlist *sln;
894 if(srch->prev != NULL)
895 srch->prev->next = srch->next;
896 if(srch->next != NULL)
897 srch->next->prev = srch->prev;
899 searches = srch->next;
901 if(srch->freetimer != NULL)
902 canceltimer(srch->freetimer);
903 CBCHAINDOCB(srch, search_destroy, srch);
904 CBCHAINFREE(srch, search_eta);
905 CBCHAINFREE(srch, search_commit);
906 CBCHAINFREE(srch, search_result);
907 CBCHAINFREE(srch, search_destroy);
908 while(srch->results != NULL)
909 freesrchres(srch->results);
910 for(sln = searchqueue; sln != NULL; sln = sln->next)
912 if(sln->srch == srch)
918 while(srch->fnl != NULL)
921 srch->fnl = ln->next;
922 CBCHAINDOCB(ln, searchfnl_destroy, ln);
923 CBCHAINFREE(ln, searchfnl_destroy);
927 if(srch->sexpr != NULL)
928 putsexpr(srch->sexpr);
929 if(srch->owner != NULL)
934 void searchaddfn(struct search *srch, struct fnetnode *fn)
936 struct srchfnnlist *ln;
938 for(ln = srch->fnl; ln != NULL; ln = ln->next)
943 ln = smalloc(sizeof(*ln));
944 memset(ln, 0, sizeof(*ln));
945 getfnetnode(ln->fn = fn);
946 CBCHAININIT(ln, searchfnl_destroy);
947 ln->next = srch->fnl;
951 static void linksearch(struct search *srch, struct srchlist *prev)
953 struct srchlist *new;
955 new = smalloc(sizeof(*new));
960 new->next = searchqueue;
961 if(searchqueue != NULL)
962 searchqueue->prev = new;
966 if((new->next = prev->next) != NULL)
967 new->next->prev = new;
970 GCBCHAINDOCB(newsrchcb, srch);
975 * queuesearch is also the "scheduler" function - it finds a suitable
976 * place in the queue for the new search. I'll make a weak attempt at
977 * describing the algorithm:
978 * First, we find the first search that doesn't have a lower priority
979 * than this one. If there is no such, we just link this one onto the
981 * Then, if we have a search of this priority in the queue with the
982 * same owner as the new search, we set lastmine to the search after
983 * that one, otherwise, lastmine is the first search of this
984 * priority. If lastmine is discovered either to not exist (that is,
985 * our last search is at the end of the queue), or to be of lower
986 * priority (higher number), we link it in at the appropriate end.
987 * Then, we find the next search of the same priority and owner as
988 * lastmine, and link this search in before it. That should yield a
989 * 'round-robin-like' scheduling within priority boundaries. I think.
991 void queuesearch(struct search *srch)
993 struct srchlist *cur, *lastmine, *prev;
996 for(prev = NULL, cur = searchqueue; cur != NULL; prev = cur, cur = cur->next)
998 if(cur->srch->prio >= srch->prio)
1003 linksearch(srch, prev);
1007 for(; cur != NULL; prev = cur, cur = cur->next)
1009 if(!wcscmp(cur->srch->owner, srch->owner))
1010 lastmine = cur->next;
1011 if(cur->srch->prio > srch->prio)
1014 if((lastmine == NULL) || (lastmine->srch->prio > srch->prio))
1016 linksearch(srch, prev);
1019 nexto = lastmine->srch->owner;
1020 for(cur = lastmine->next; cur != NULL; prev = cur, cur = cur->next)
1022 if(!wcscmp(cur->srch->owner, nexto))
1024 if(cur->srch->prio > srch->prio)
1029 linksearch(srch, prev);
1032 linksearch(srch, prev);
1035 static int srisvalid(struct srchres *sr, struct sexpr *sexpr)
1048 if(!srisvalid(sr, sexpr->l))
1050 return(srisvalid(sr, sexpr->r));
1052 if(srisvalid(sr, sexpr->l))
1054 return(srisvalid(sr, sexpr->r));
1056 return(!srisvalid(sr, sexpr->l));
1058 if((buf = icwcstombs(sr->filename, "UTF-8")) == NULL)
1060 ret = regexec(&sexpr->d.re.cre, buf, 0, NULL, 0);
1065 if(sr->fnet->filebasename != NULL)
1066 p = sr->fnet->filebasename(p);
1067 if((buf = icwcstombs(p, "UTF-8")) == NULL)
1069 ret = regexec(&sexpr->d.re.cre, buf, 0, NULL, 0);
1073 return(wcsexists(sr->filename, sexpr->d.s));
1076 if(sr->fnet->filebasename != NULL)
1077 p = sr->fnet->filebasename(p);
1078 return(wcsexists(p, sexpr->d.s));
1080 return(sr->size < sexpr->d.n);
1082 return(sr->size == sexpr->d.n);
1084 return(sr->size > sexpr->d.n);
1089 struct srchres *newsrchres(struct fnet *fnet, wchar_t *filename, wchar_t *peerid)
1093 sr = smalloc(sizeof(*sr));
1094 memset(sr, 0, sizeof(*sr));
1098 sr->filename = swcsdup(filename);
1099 sr->peerid = swcsdup(peerid);
1103 void freesrchres(struct srchres *sr)
1105 if(sr->next != NULL)
1106 sr->next->prev = sr->prev;
1107 if(sr->prev != NULL)
1108 sr->prev->next = sr->next;
1109 if(sr->srch != NULL)
1111 if(sr->srch->results == sr)
1112 sr->srch->results = sr->next;
1115 if(sr->filename != NULL)
1117 if(sr->peerid != NULL)
1119 if(sr->peernick != NULL)
1122 putfnetnode(sr->fn);
1126 struct srchres *dupsrchres(struct srchres *sr)
1128 struct srchres *new;
1130 new = smalloc(sizeof(*new));
1131 memset(new, 0, sizeof(*new));
1132 new->size = sr->size;
1133 new->slots = sr->slots;
1134 new->fnet = sr->fnet;
1135 if(sr->peerid != NULL)
1136 new->peerid = swcsdup(sr->peerid);
1137 if(sr->peernick != NULL)
1138 new->peernick = swcsdup(sr->peernick);
1139 if(sr->filename != NULL)
1140 new->filename = swcsdup(sr->filename);
1142 getfnetnode(new->fn = sr->fn);
1146 static void linksr(struct search *srch, struct srchres *sr)
1149 sr->next = srch->results;
1150 if(srch->results != NULL)
1151 srch->results->prev = sr;
1154 sr->time = ntime() - srch->committime;
1158 void submitsrchres(struct srchres *sr)
1160 struct search *srch;
1161 struct srchfnnlist *ln;
1162 struct srchres *dsr;
1164 for(srch = searches; srch != NULL; srch = srch->next)
1166 if(srch->state == SRCH_RUN)
1168 if(!srisvalid(sr, srch->sexpr))
1170 for(ln = srch->fnl; ln != NULL; ln = ln->next)
1172 if(((sr->fn != NULL) && (ln->fn == sr->fn)) || ((sr->fn == NULL) && (sr->fnet == ln->fn->fnet)))
1177 dsr = dupsrchres(sr);
1179 CBCHAINDOCB(srch, search_result, srch, dsr);