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++);
402 ret.begstr[i] = L'\0';
406 if(ret.begstr != NULL)
412 if(sinf.endstr != NULL)
414 if(ret.endstr != NULL)
416 len1 = wcslen(ret.endstr);
417 len2 = wcslen(sinf.endstr);
426 for(i = 1; (i <= minlen) && (ret.endstr[len1 - i] == sinf.endstr[len2 - i]); i++);
430 wmemmove(ret.endstr, ret.endstr + (len1 - i) + 1, i);
434 if(ret.endstr != NULL)
440 if(sinf.onestr != NULL)
442 if(ret.onestr != NULL)
444 /* XXX: Comparing beginning and end of the onestrs and
445 * create begstr and endstr if there isn't an exact
447 if(wcscmp(ret.onestr, sinf.onestr))
455 if(ret.onestr != NULL)
469 struct wcslist *regexfindstrings(wchar_t *re)
473 i = analyzere(re, NULL, L'\0');
483 static struct sexpr *newsexpr(void)
487 sexpr = smalloc(sizeof(*sexpr));
488 memset(sexpr, 0, sizeof(*sexpr));
493 void getsexpr(struct sexpr *sexpr)
498 void putsexpr(struct sexpr *sexpr)
500 if(--sexpr->refcount != 0)
506 if((sexpr->op == SOP_NAMERE) || (sexpr->op == SOP_LINKRE))
508 if(sexpr->d.re.sre != NULL)
509 free(sexpr->d.re.sre);
510 if(sexpr->d.re.inited)
511 regfree(&sexpr->d.re.cre);
513 if((sexpr->op == SOP_NAMESS) || (sexpr->op == SOP_LINKSS))
515 if(sexpr->d.s != NULL)
518 if(sexpr->op == SOP_HASHIS)
520 if(sexpr->d.hash != NULL)
521 freehash(sexpr->d.hash);
526 static struct tok *newtok(void)
530 tok = smalloc(sizeof(*tok));
531 memset(tok, 0, sizeof(*tok));
536 static void freetok(struct tok *tok)
538 if((tok->type == TOK_STR) && (tok->d.str != NULL))
540 if((tok->type == TOK_SE) && (tok->d.se != NULL))
545 static void pushtok(struct tok *tok, struct tok **st)
551 static struct tok *poptok(struct tok **st)
560 int calccost(struct sexpr *sexpr)
562 sexpr->tcost = sexpr->cost;
564 sexpr->tcost += calccost(sexpr->l);
566 sexpr->tcost += calccost(sexpr->r);
567 return(sexpr->tcost);
570 struct sexpr *parsesexpr(int argc, wchar_t **argv)
573 struct tok *st, *tok, *tok2;
580 for(i = 0; i < argc; i++)
582 pushtok(tok = newtok(), &st);
584 tok->d.str = swcsdup(argv[i]);
589 if((st->type == TOK_STR) && !wcscmp(st->d.str, L"("))
591 freetok(poptok(&st));
592 pushtok(tok = newtok(), &st);
595 } else if((st->type == TOK_STR) && !wcscmp(st->d.str, L")")) {
596 freetok(poptok(&st));
597 pushtok(tok = newtok(), &st);
600 } else if((st->type == TOK_STR) && (!wcsncmp(st->d.str, L"N~", 2) || !wcsncmp(st->d.str, L"L~", 2))) {
602 pushtok(tok = newtok(), &st);
605 if((wbuf = regexunquotesimple(tok2->d.str + 2)) != NULL)
607 if(tok2->d.str[0] == L'N')
608 sexpr->op = SOP_NAMESS;
610 sexpr->op = SOP_LINKSS;
614 if(tok2->d.str[0] == L'N')
615 sexpr->op = SOP_NAMERE;
617 sexpr->op = SOP_LINKRE;
619 if((buf = icwcstombs(tok2->d.str + 2, "UTF-8")) == NULL)
625 if(regcomp(&sexpr->d.re.cre, buf, REG_EXTENDED | REG_ICASE | REG_NOSUB))
633 sexpr->d.re.inited = 1;
634 sexpr->d.re.sre = swcsdup(tok2->d.str + 2);
636 getsexpr(tok->d.se = sexpr);
640 } 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))) {
642 pushtok(tok = newtok(), &st);
645 if(tok2->d.str[1] == L'<')
646 sexpr->op = SOP_SIZELT;
647 else if(tok2->d.str[1] == L'=')
648 sexpr->op = SOP_SIZEEQ;
650 sexpr->op = SOP_SIZEGT;
651 sexpr->d.n = wcstol(tok2->d.str + 2, NULL, 0);
653 getsexpr(tok->d.se = sexpr);
657 } else if((st->type == TOK_STR) && !wcsncmp(st->d.str, L"H=", 2)) {
659 pushtok(tok = newtok(), &st);
662 sexpr->op = SOP_HASHIS;
663 if((sexpr->d.hash = parsehash(tok2->d.str + 2)) == NULL)
670 getsexpr(tok->d.se = sexpr);
674 } else if((std >= 3) && (st->type == TOK_CP) && (st->next->type == TOK_SE) && (st->next->next->type == TOK_OP)) {
675 freetok(poptok(&st));
677 freetok(poptok(&st));
681 } else if((std >= 2) && (st->type == TOK_SE) && (st->next->type == TOK_STR) && !wcscmp(st->next->d.str, L"!")) {
685 getsexpr(sexpr->l = st->d.se);
686 freetok(poptok(&st));
687 freetok(poptok(&st));
688 pushtok(tok = newtok(), &st);
690 getsexpr(tok->d.se = sexpr);
694 } 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)) {
696 if(!wcscmp(st->next->d.str, L"&"))
701 getsexpr(sexpr->l = st->next->next->d.se);
702 getsexpr(sexpr->r = st->d.se);
703 freetok(poptok(&st));
704 freetok(poptok(&st));
705 freetok(poptok(&st));
706 pushtok(tok = newtok(), &st);
708 getsexpr(tok->d.se = sexpr);
715 if((st == NULL) || (st->next != NULL) || (st->type != TOK_SE))
717 getsexpr(sexpr = st->d.se);
724 freetok(poptok(&st));
728 void optsexpr(struct sexpr *sexpr)
732 if((sexpr->l != NULL) && (sexpr->r != NULL))
734 if(sexpr->l->tcost > sexpr->r->tcost)
747 struct wcslist *findsexprstrs(struct sexpr *sexpr)
749 struct wcslist *list, *l1, *l2;
755 list = findsexprstrs(sexpr->l);
756 l1 = findsexprstrs(sexpr->r);
757 slmergemax(&list, l1);
761 l1 = findsexprstrs(sexpr->l);
762 l2 = findsexprstrs(sexpr->r);
763 list = slmergemin(l1, l2);
769 list = regexfindstrings(sexpr->d.re.sre);
773 slmerge1(&list, sexpr->d.s);
781 static void unlinksqueue(struct srchlist *ln)
784 ln->prev->next = ln->next;
786 ln->next->prev = ln->prev;
787 if(ln == searchqueue)
788 searchqueue = ln->next;
792 static void ctexpire(int cancelled, void *data)
799 static void estimatequeue(void)
801 struct srchlist *cur;
802 struct srchfnnlist *ln;
805 if(searchqueue == NULL)
807 start = now = time(NULL);
808 for(ln = searchqueue->srch->fnl; ln != NULL; ln = ln->next)
810 if((ln->fn->lastsrch != 0) && (ln->fn->lastsrch + ln->fn->srchwait > start))
811 start = ln->fn->lastsrch + ln->fn->srchwait;
813 if(start != searchqueue->srch->eta)
815 searchqueue->srch->eta = start;
816 CBCHAINDOCB(searchqueue->srch, search_eta, searchqueue->srch);
818 for(cur = searchqueue->next; cur != NULL; cur = cur->next)
821 for(ln = cur->srch->fnl; ln != NULL; ln = ln->next)
823 if(now + ln->fn->srchwait > start)
824 start = now + ln->fn->srchwait;
826 if(start != cur->srch->eta)
828 cur->srch->eta = start;
829 CBCHAINDOCB(cur->srch, search_eta, cur->srch);
832 if((committimer == NULL) || ((time_t)committimer->at != searchqueue->srch->eta))
834 if(committimer != NULL)
835 canceltimer(committimer);
836 committimer = timercallback(searchqueue->srch->eta, ctexpire, NULL);
840 struct search *findsearch(int id)
844 for(srch = searches; srch != NULL; srch = srch->next)
852 struct search *newsearch(wchar_t *owner, struct sexpr *sexpr)
857 srch = smalloc(sizeof(*srch));
858 memset(srch, 0, sizeof(*srch));
860 srch->owner = swcsdup(owner);
861 if((srch->sexpr = sexpr) != NULL)
862 getsexpr(srch->sexpr);
863 CBCHAININIT(srch, search_eta);
864 CBCHAININIT(srch, search_commit);
865 CBCHAININIT(srch, search_result);
866 CBCHAININIT(srch, search_destroy);
867 srch->next = searches;
870 searches->prev = srch;
875 static void srchexpire(int cancelled, struct search *srch)
877 srch->freetimer = NULL;
882 static void trycommit(void)
884 struct srchfnnlist *ln;
888 if(searchqueue == NULL)
890 srch = searchqueue->srch;
892 for(ln = srch->fnl; ln != NULL; ln = ln->next)
894 if(now < ln->fn->lastsrch + ln->fn->srchwait)
899 unlinksqueue(searchqueue);
900 srch->state = SRCH_RUN;
901 srch->eta = time(NULL);
902 srch->committime = ntime();
903 srch->freetimer = timercallback(ntime() + 300, (void (*)(int, void *))srchexpire, srch);
904 CBCHAINDOCB(srch, search_commit, srch);
905 for(ln = srch->fnl; ln != NULL; ln = ln->next)
906 fnetsearch(ln->fn, srch, ln);
910 void freesearch(struct search *srch)
912 struct srchfnnlist *ln;
913 struct srchlist *sln;
915 if(srch->prev != NULL)
916 srch->prev->next = srch->next;
917 if(srch->next != NULL)
918 srch->next->prev = srch->prev;
920 searches = srch->next;
922 if(srch->freetimer != NULL)
923 canceltimer(srch->freetimer);
924 CBCHAINDOCB(srch, search_destroy, srch);
925 CBCHAINFREE(srch, search_eta);
926 CBCHAINFREE(srch, search_commit);
927 CBCHAINFREE(srch, search_result);
928 CBCHAINFREE(srch, search_destroy);
929 while(srch->results != NULL)
930 freesrchres(srch->results);
931 for(sln = searchqueue; sln != NULL; sln = sln->next)
933 if(sln->srch == srch)
939 while(srch->fnl != NULL)
942 srch->fnl = ln->next;
943 CBCHAINDOCB(ln, searchfnl_destroy, ln);
944 CBCHAINFREE(ln, searchfnl_destroy);
948 if(srch->sexpr != NULL)
949 putsexpr(srch->sexpr);
950 if(srch->owner != NULL)
955 void searchaddfn(struct search *srch, struct fnetnode *fn)
957 struct srchfnnlist *ln;
959 for(ln = srch->fnl; ln != NULL; ln = ln->next)
964 ln = smalloc(sizeof(*ln));
965 memset(ln, 0, sizeof(*ln));
966 getfnetnode(ln->fn = fn);
967 CBCHAININIT(ln, searchfnl_destroy);
968 ln->next = srch->fnl;
972 static void linksearch(struct search *srch, struct srchlist *prev)
974 struct srchlist *new;
976 new = smalloc(sizeof(*new));
981 new->next = searchqueue;
982 if(searchqueue != NULL)
983 searchqueue->prev = new;
987 if((new->next = prev->next) != NULL)
988 new->next->prev = new;
991 GCBCHAINDOCB(newsrchcb, srch);
996 * queuesearch is also the "scheduler" function - it finds a suitable
997 * place in the queue for the new search. I'll make a weak attempt at
998 * describing the algorithm:
999 * First, we find the first search that doesn't have a lower priority
1000 * than this one. If there is no such, we just link this one onto the
1002 * Then, if we have a search of this priority in the queue with the
1003 * same owner as the new search, we set lastmine to the search after
1004 * that one, otherwise, lastmine is the first search of this
1005 * priority. If lastmine is discovered either to not exist (that is,
1006 * our last search is at the end of the queue), or to be of lower
1007 * priority (higher number), we link it in at the appropriate end.
1008 * Then, we find the next search of the same priority and owner as
1009 * lastmine, and link this search in before it. That should yield a
1010 * 'round-robin-like' scheduling within priority boundaries. I think.
1012 void queuesearch(struct search *srch)
1014 struct srchlist *cur, *lastmine, *prev;
1017 for(prev = NULL, cur = searchqueue; cur != NULL; prev = cur, cur = cur->next)
1019 if(cur->srch->prio >= srch->prio)
1024 linksearch(srch, prev);
1028 for(; cur != NULL; prev = cur, cur = cur->next)
1030 if(!wcscmp(cur->srch->owner, srch->owner))
1031 lastmine = cur->next;
1032 if(cur->srch->prio > srch->prio)
1035 if((lastmine == NULL) || (lastmine->srch->prio > srch->prio))
1037 linksearch(srch, prev);
1040 nexto = lastmine->srch->owner;
1041 for(cur = lastmine->next; cur != NULL; prev = cur, cur = cur->next)
1043 if(!wcscmp(cur->srch->owner, nexto))
1045 if(cur->srch->prio > srch->prio)
1050 linksearch(srch, prev);
1053 linksearch(srch, prev);
1056 static int srisvalid(struct srchres *sr, struct sexpr *sexpr)
1069 if(!srisvalid(sr, sexpr->l))
1071 return(srisvalid(sr, sexpr->r));
1073 if(srisvalid(sr, sexpr->l))
1075 return(srisvalid(sr, sexpr->r));
1077 return(!srisvalid(sr, sexpr->l));
1079 if((buf = icwcstombs(sr->filename, "UTF-8")) == NULL)
1081 ret = regexec(&sexpr->d.re.cre, buf, 0, NULL, 0);
1086 if(sr->fnet->filebasename != NULL)
1087 p = sr->fnet->filebasename(p);
1088 if((buf = icwcstombs(p, "UTF-8")) == NULL)
1090 ret = regexec(&sexpr->d.re.cre, buf, 0, NULL, 0);
1094 return(wcsexists(sr->filename, sexpr->d.s));
1097 if(sr->fnet->filebasename != NULL)
1098 p = sr->fnet->filebasename(p);
1099 return(wcsexists(p, sexpr->d.s));
1101 return(sr->size < sexpr->d.n);
1103 return(sr->size == sexpr->d.n);
1105 return(sr->size > sexpr->d.n);
1107 if(sr->hash == NULL)
1109 return(hashcmp(sr->hash, sexpr->d.hash));
1114 struct srchres *newsrchres(struct fnet *fnet, wchar_t *filename, wchar_t *peerid)
1118 sr = smalloc(sizeof(*sr));
1119 memset(sr, 0, sizeof(*sr));
1123 sr->filename = swcsdup(filename);
1124 sr->peerid = swcsdup(peerid);
1128 void freesrchres(struct srchres *sr)
1130 if(sr->next != NULL)
1131 sr->next->prev = sr->prev;
1132 if(sr->prev != NULL)
1133 sr->prev->next = sr->next;
1134 if(sr->srch != NULL)
1136 if(sr->srch->results == sr)
1137 sr->srch->results = sr->next;
1140 if(sr->hash != NULL)
1142 if(sr->filename != NULL)
1144 if(sr->peerid != NULL)
1146 if(sr->peernick != NULL)
1149 putfnetnode(sr->fn);
1153 struct srchres *dupsrchres(struct srchres *sr)
1155 struct srchres *new;
1157 new = smalloc(sizeof(*new));
1158 memset(new, 0, sizeof(*new));
1159 new->size = sr->size;
1160 new->slots = sr->slots;
1161 new->fnet = sr->fnet;
1162 if(sr->peerid != NULL)
1163 new->peerid = swcsdup(sr->peerid);
1164 if(sr->peernick != NULL)
1165 new->peernick = swcsdup(sr->peernick);
1166 if(sr->filename != NULL)
1167 new->filename = swcsdup(sr->filename);
1169 getfnetnode(new->fn = sr->fn);
1170 if(sr->hash != NULL)
1171 new->hash = duphash(sr->hash);
1175 static void linksr(struct search *srch, struct srchres *sr)
1178 sr->next = srch->results;
1179 if(srch->results != NULL)
1180 srch->results->prev = sr;
1183 sr->time = ntime() - srch->committime;
1187 void submitsrchres(struct srchres *sr)
1189 struct search *srch;
1190 struct srchfnnlist *ln;
1191 struct srchres *dsr;
1193 for(srch = searches; srch != NULL; srch = srch->next)
1195 if(srch->state == SRCH_RUN)
1197 if(!srisvalid(sr, srch->sexpr))
1199 for(ln = srch->fnl; ln != NULL; ln = ln->next)
1201 if(((sr->fn != NULL) && (ln->fn == sr->fn)) || ((sr->fn == NULL) && (sr->fnet == ln->fn->fnet)))
1206 dsr = dupsrchres(sr);
1208 CBCHAINDOCB(srch, search_result, srch, dsr);