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)
521 static struct tok *newtok(void)
525 tok = smalloc(sizeof(*tok));
526 memset(tok, 0, sizeof(*tok));
531 static void freetok(struct tok *tok)
533 if((tok->type == TOK_STR) && (tok->d.str != NULL))
535 if((tok->type == TOK_SE) && (tok->d.se != NULL))
540 static void pushtok(struct tok *tok, struct tok **st)
546 static struct tok *poptok(struct tok **st)
555 int calccost(struct sexpr *sexpr)
557 sexpr->tcost = sexpr->cost;
559 sexpr->tcost += calccost(sexpr->l);
561 sexpr->tcost += calccost(sexpr->r);
562 return(sexpr->tcost);
565 struct sexpr *parsesexpr(int argc, wchar_t **argv)
568 struct tok *st, *tok, *tok2;
575 for(i = 0; i < argc; i++)
577 pushtok(tok = newtok(), &st);
579 tok->d.str = swcsdup(argv[i]);
584 if((st->type == TOK_STR) && !wcscmp(st->d.str, L"("))
586 freetok(poptok(&st));
587 pushtok(tok = newtok(), &st);
590 } else 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) && (!wcsncmp(st->d.str, L"N~", 2) || !wcsncmp(st->d.str, L"L~", 2))) {
597 pushtok(tok = newtok(), &st);
600 if((wbuf = regexunquotesimple(tok2->d.str + 2)) != NULL)
602 if(tok2->d.str[0] == L'N')
603 sexpr->op = SOP_NAMESS;
605 sexpr->op = SOP_LINKSS;
609 if(tok2->d.str[0] == L'N')
610 sexpr->op = SOP_NAMERE;
612 sexpr->op = SOP_LINKRE;
614 if((buf = icwcstombs(tok2->d.str + 2, "UTF-8")) == NULL)
620 if(regcomp(&sexpr->d.re.cre, buf, REG_EXTENDED | REG_ICASE | REG_NOSUB))
628 sexpr->d.re.inited = 1;
629 sexpr->d.re.sre = swcsdup(tok2->d.str + 2);
631 getsexpr(tok->d.se = sexpr);
635 } 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))) {
637 pushtok(tok = newtok(), &st);
640 if(tok2->d.str[1] == L'<')
641 sexpr->op = SOP_SIZELT;
642 else if(tok2->d.str[1] == L'=')
643 sexpr->op = SOP_SIZEEQ;
645 sexpr->op = SOP_SIZEGT;
646 sexpr->d.n = wcstol(tok2->d.str + 2, NULL, 0);
648 getsexpr(tok->d.se = sexpr);
652 } else if((std >= 3) && (st->type == TOK_CP) && (st->next->type == TOK_SE) && (st->next->next->type == TOK_OP)) {
653 freetok(poptok(&st));
655 freetok(poptok(&st));
659 } else if((std >= 2) && (st->type == TOK_SE) && (st->next->type == TOK_STR) && !wcscmp(st->next->d.str, L"!")) {
663 getsexpr(sexpr->l = st->d.se);
664 freetok(poptok(&st));
665 freetok(poptok(&st));
666 pushtok(tok = newtok(), &st);
668 getsexpr(tok->d.se = sexpr);
672 } 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)) {
674 if(!wcscmp(st->next->d.str, L"&"))
679 getsexpr(sexpr->l = st->next->next->d.se);
680 getsexpr(sexpr->r = st->d.se);
681 freetok(poptok(&st));
682 freetok(poptok(&st));
683 freetok(poptok(&st));
684 pushtok(tok = newtok(), &st);
686 getsexpr(tok->d.se = sexpr);
693 if((st == NULL) || (st->next != NULL) || (st->type != TOK_SE))
695 getsexpr(sexpr = st->d.se);
702 freetok(poptok(&st));
706 void optsexpr(struct sexpr *sexpr)
710 if((sexpr->l != NULL) && (sexpr->r != NULL))
712 if(sexpr->l->tcost > sexpr->r->tcost)
725 struct wcslist *findsexprstrs(struct sexpr *sexpr)
727 struct wcslist *list, *l1, *l2;
733 list = findsexprstrs(sexpr->l);
734 l1 = findsexprstrs(sexpr->r);
735 slmergemax(&list, l1);
739 l1 = findsexprstrs(sexpr->l);
740 l2 = findsexprstrs(sexpr->r);
741 list = slmergemin(l1, l2);
749 list = regexfindstrings(sexpr->d.re.sre);
753 slmerge1(&list, sexpr->d.s);
761 static void unlinksqueue(struct srchlist *ln)
764 ln->prev->next = ln->next;
766 ln->next->prev = ln->prev;
767 if(ln == searchqueue)
768 searchqueue = ln->next;
772 static void ctexpire(int cancelled, void *data)
779 static void estimatequeue(void)
781 struct srchlist *cur;
782 struct srchfnnlist *ln;
785 if(searchqueue == NULL)
787 start = now = time(NULL);
788 for(ln = searchqueue->srch->fnl; ln != NULL; ln = ln->next)
790 if((ln->fn->lastsrch != 0) && (ln->fn->lastsrch + ln->fn->srchwait > start))
791 start = ln->fn->lastsrch + ln->fn->srchwait;
793 if(start != searchqueue->srch->eta)
795 searchqueue->srch->eta = start;
796 CBCHAINDOCB(searchqueue->srch, search_eta, searchqueue->srch);
798 for(cur = searchqueue->next; cur != NULL; cur = cur->next)
801 for(ln = cur->srch->fnl; ln != NULL; ln = ln->next)
803 if(now + ln->fn->srchwait > start)
804 start = now + ln->fn->srchwait;
806 if(start != cur->srch->eta)
808 cur->srch->eta = start;
809 CBCHAINDOCB(cur->srch, search_eta, cur->srch);
812 if((committimer == NULL) || ((time_t)committimer->at != searchqueue->srch->eta))
814 if(committimer != NULL)
815 canceltimer(committimer);
816 committimer = timercallback(searchqueue->srch->eta, ctexpire, NULL);
820 struct search *findsearch(int id)
824 for(srch = searches; srch != NULL; srch = srch->next)
832 struct search *newsearch(wchar_t *owner, struct sexpr *sexpr)
837 srch = smalloc(sizeof(*srch));
838 memset(srch, 0, sizeof(*srch));
840 srch->owner = swcsdup(owner);
841 if((srch->sexpr = sexpr) != NULL)
842 getsexpr(srch->sexpr);
843 CBCHAININIT(srch, search_eta);
844 CBCHAININIT(srch, search_commit);
845 CBCHAININIT(srch, search_result);
846 CBCHAININIT(srch, search_destroy);
847 srch->next = searches;
850 searches->prev = srch;
855 static void srchexpire(int cancelled, struct search *srch)
857 srch->freetimer = NULL;
862 static void trycommit(void)
864 struct srchfnnlist *ln;
868 if(searchqueue == NULL)
870 srch = searchqueue->srch;
872 for(ln = srch->fnl; ln != NULL; ln = ln->next)
874 if(now < ln->fn->lastsrch + ln->fn->srchwait)
879 unlinksqueue(searchqueue);
880 srch->state = SRCH_RUN;
881 srch->eta = time(NULL);
882 srch->committime = ntime();
883 srch->freetimer = timercallback(ntime() + 300, (void (*)(int, void *))srchexpire, srch);
884 CBCHAINDOCB(srch, search_commit, srch);
885 for(ln = srch->fnl; ln != NULL; ln = ln->next)
886 fnetsearch(ln->fn, srch, ln);
890 void freesearch(struct search *srch)
892 struct srchfnnlist *ln;
893 struct srchlist *sln;
895 if(srch->prev != NULL)
896 srch->prev->next = srch->next;
897 if(srch->next != NULL)
898 srch->next->prev = srch->prev;
900 searches = srch->next;
902 if(srch->freetimer != NULL)
903 canceltimer(srch->freetimer);
904 CBCHAINDOCB(srch, search_destroy, srch);
905 CBCHAINFREE(srch, search_eta);
906 CBCHAINFREE(srch, search_commit);
907 CBCHAINFREE(srch, search_result);
908 CBCHAINFREE(srch, search_destroy);
909 while(srch->results != NULL)
910 freesrchres(srch->results);
911 for(sln = searchqueue; sln != NULL; sln = sln->next)
913 if(sln->srch == srch)
919 while(srch->fnl != NULL)
922 srch->fnl = ln->next;
923 CBCHAINDOCB(ln, searchfnl_destroy, ln);
924 CBCHAINFREE(ln, searchfnl_destroy);
928 if(srch->sexpr != NULL)
929 putsexpr(srch->sexpr);
930 if(srch->owner != NULL)
935 void searchaddfn(struct search *srch, struct fnetnode *fn)
937 struct srchfnnlist *ln;
939 for(ln = srch->fnl; ln != NULL; ln = ln->next)
944 ln = smalloc(sizeof(*ln));
945 memset(ln, 0, sizeof(*ln));
946 getfnetnode(ln->fn = fn);
947 CBCHAININIT(ln, searchfnl_destroy);
948 ln->next = srch->fnl;
952 static void linksearch(struct search *srch, struct srchlist *prev)
954 struct srchlist *new;
956 new = smalloc(sizeof(*new));
961 new->next = searchqueue;
962 if(searchqueue != NULL)
963 searchqueue->prev = new;
967 if((new->next = prev->next) != NULL)
968 new->next->prev = new;
971 GCBCHAINDOCB(newsrchcb, srch);
976 * queuesearch is also the "scheduler" function - it finds a suitable
977 * place in the queue for the new search. I'll make a weak attempt at
978 * describing the algorithm:
979 * First, we find the first search that doesn't have a lower priority
980 * than this one. If there is no such, we just link this one onto the
982 * Then, if we have a search of this priority in the queue with the
983 * same owner as the new search, we set lastmine to the search after
984 * that one, otherwise, lastmine is the first search of this
985 * priority. If lastmine is discovered either to not exist (that is,
986 * our last search is at the end of the queue), or to be of lower
987 * priority (higher number), we link it in at the appropriate end.
988 * Then, we find the next search of the same priority and owner as
989 * lastmine, and link this search in before it. That should yield a
990 * 'round-robin-like' scheduling within priority boundaries. I think.
992 void queuesearch(struct search *srch)
994 struct srchlist *cur, *lastmine, *prev;
997 for(prev = NULL, cur = searchqueue; cur != NULL; prev = cur, cur = cur->next)
999 if(cur->srch->prio >= srch->prio)
1004 linksearch(srch, prev);
1008 for(; cur != NULL; prev = cur, cur = cur->next)
1010 if(!wcscmp(cur->srch->owner, srch->owner))
1011 lastmine = cur->next;
1012 if(cur->srch->prio > srch->prio)
1015 if((lastmine == NULL) || (lastmine->srch->prio > srch->prio))
1017 linksearch(srch, prev);
1020 nexto = lastmine->srch->owner;
1021 for(cur = lastmine->next; cur != NULL; prev = cur, cur = cur->next)
1023 if(!wcscmp(cur->srch->owner, nexto))
1025 if(cur->srch->prio > srch->prio)
1030 linksearch(srch, prev);
1033 linksearch(srch, prev);
1036 static int srisvalid(struct srchres *sr, struct sexpr *sexpr)
1049 if(!srisvalid(sr, sexpr->l))
1051 return(srisvalid(sr, sexpr->r));
1053 if(srisvalid(sr, sexpr->l))
1055 return(srisvalid(sr, sexpr->r));
1057 return(!srisvalid(sr, sexpr->l));
1059 if((buf = icwcstombs(sr->filename, "UTF-8")) == NULL)
1061 ret = regexec(&sexpr->d.re.cre, buf, 0, NULL, 0);
1066 if(sr->fnet->filebasename != NULL)
1067 p = sr->fnet->filebasename(p);
1068 if((buf = icwcstombs(p, "UTF-8")) == NULL)
1070 ret = regexec(&sexpr->d.re.cre, buf, 0, NULL, 0);
1074 return(wcsexists(sr->filename, sexpr->d.s));
1077 if(sr->fnet->filebasename != NULL)
1078 p = sr->fnet->filebasename(p);
1079 return(wcsexists(p, sexpr->d.s));
1081 return(sr->size < sexpr->d.n);
1083 return(sr->size == sexpr->d.n);
1085 return(sr->size > sexpr->d.n);
1090 struct srchres *newsrchres(struct fnet *fnet, wchar_t *filename, wchar_t *peerid)
1094 sr = smalloc(sizeof(*sr));
1095 memset(sr, 0, sizeof(*sr));
1099 sr->filename = swcsdup(filename);
1100 sr->peerid = swcsdup(peerid);
1104 void freesrchres(struct srchres *sr)
1106 if(sr->next != NULL)
1107 sr->next->prev = sr->prev;
1108 if(sr->prev != NULL)
1109 sr->prev->next = sr->next;
1110 if(sr->srch != NULL)
1112 if(sr->srch->results == sr)
1113 sr->srch->results = sr->next;
1116 if(sr->hash != NULL)
1118 if(sr->filename != NULL)
1120 if(sr->peerid != NULL)
1122 if(sr->peernick != NULL)
1125 putfnetnode(sr->fn);
1129 struct srchres *dupsrchres(struct srchres *sr)
1131 struct srchres *new;
1133 new = smalloc(sizeof(*new));
1134 memset(new, 0, sizeof(*new));
1135 new->size = sr->size;
1136 new->slots = sr->slots;
1137 new->fnet = sr->fnet;
1138 if(sr->peerid != NULL)
1139 new->peerid = swcsdup(sr->peerid);
1140 if(sr->peernick != NULL)
1141 new->peernick = swcsdup(sr->peernick);
1142 if(sr->filename != NULL)
1143 new->filename = swcsdup(sr->filename);
1145 getfnetnode(new->fn = sr->fn);
1146 if(sr->hash != NULL)
1147 new->hash = duphash(sr->hash);
1151 static void linksr(struct search *srch, struct srchres *sr)
1154 sr->next = srch->results;
1155 if(srch->results != NULL)
1156 srch->results->prev = sr;
1159 sr->time = ntime() - srch->committime;
1163 void submitsrchres(struct srchres *sr)
1165 struct search *srch;
1166 struct srchfnnlist *ln;
1167 struct srchres *dsr;
1169 for(srch = searches; srch != NULL; srch = srch->next)
1171 if(srch->state == SRCH_RUN)
1173 if(!srisvalid(sr, srch->sexpr))
1175 for(ln = srch->fnl; ln != NULL; ln = ln->next)
1177 if(((sr->fn != NULL) && (ln->fn == sr->fn)) || ((sr->fn == NULL) && (sr->fnet == ln->fn->fnet)))
1182 dsr = dupsrchres(sr);
1184 CBCHAINDOCB(srch, search_result, srch, dsr);