Artifact e55efe4a5c7fcb55e1fcbbb3188db21b0bd61e4acfad369aff6f30e68ca16726:
- File search.c — part of check-in [c14cf350df] at 2014-05-15 21:36:21 on branch manualheaven — Move src and doc files to top directory (user: base@atomicules.co.uk size: 6005) [more...]
- File
src/search.c
— part of check-in
[f05023e8f3]
at
2011-02-15 22:24:16
on branch origin/tweaks
— Tweak search internals to always track the parent of items, and use this to allow locating of search entries
git-svn-id: https://pwman.svn.sourceforge.net/svnroot/pwman/trunk@121 db5f1c25-4851-472e-ac1e-5437d07aece1 (user: gagravarr@users.sourceforge.net size: 6005)
0000: 2f 2a 0a 20 2a 20 20 50 57 4d 61 6e 20 2d 20 70 /*. * PWMan - p
0010: 61 73 73 77 6f 72 64 20 6d 61 6e 61 67 65 6d 65 assword manageme
0020: 6e 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a 20 nt application.
0030: 2a 0a 20 2a 20 20 43 6f 70 79 72 69 67 68 74 20 *. * Copyright
0040: 28 43 29 20 32 30 30 32 20 20 49 76 61 6e 20 4b (C) 2002 Ivan K
0050: 65 6c 6c 79 20 3c 69 76 61 6e 40 69 76 61 6e 6b elly <ivan@ivank
0060: 65 6c 6c 79 2e 6e 65 74 3e 0a 20 2a 0a 20 2a 20 elly.net>. *. *
0070: 20 54 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 This program is
0080: 20 66 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 free software;
0090: 79 6f 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 you can redistri
00a0: 62 75 74 65 20 69 74 20 61 6e 64 2f 6f 72 20 6d bute it and/or m
00b0: 6f 64 69 66 79 0a 20 2a 20 20 69 74 20 75 6e 64 odify. * it und
00c0: 65 72 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20 er the terms of
00d0: 74 68 65 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 the GNU General
00e0: 50 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 61 Public License a
00f0: 73 20 70 75 62 6c 69 73 68 65 64 20 62 79 0a 20 s published by.
0100: 2a 20 20 74 68 65 20 46 72 65 65 20 53 6f 66 74 * the Free Soft
0110: 77 61 72 65 20 46 6f 75 6e 64 61 74 69 6f 6e 3b ware Foundation;
0120: 20 65 69 74 68 65 72 20 76 65 72 73 69 6f 6e 20 either version
0130: 32 20 6f 66 20 74 68 65 20 4c 69 63 65 6e 73 65 2 of the License
0140: 2c 20 6f 72 0a 20 2a 20 20 28 61 74 20 79 6f 75 , or. * (at you
0150: 72 20 6f 70 74 69 6f 6e 29 20 61 6e 79 20 6c 61 r option) any la
0160: 74 65 72 20 76 65 72 73 69 6f 6e 2e 0a 20 2a 0a ter version.. *.
0170: 20 2a 20 20 54 68 69 73 20 70 72 6f 67 72 61 6d * This program
0180: 20 69 73 20 64 69 73 74 72 69 62 75 74 65 64 20 is distributed
0190: 69 6e 20 74 68 65 20 68 6f 70 65 20 74 68 61 74 in the hope that
01a0: 20 69 74 20 77 69 6c 6c 20 62 65 20 75 73 65 66 it will be usef
01b0: 75 6c 2c 0a 20 2a 20 20 62 75 74 20 57 49 54 48 ul,. * but WITH
01c0: 4f 55 54 20 41 4e 59 20 57 41 52 52 41 4e 54 59 OUT ANY WARRANTY
01d0: 3b 20 77 69 74 68 6f 75 74 20 65 76 65 6e 20 74 ; without even t
01e0: 68 65 20 69 6d 70 6c 69 65 64 20 77 61 72 72 61 he implied warra
01f0: 6e 74 79 20 6f 66 0a 20 2a 20 20 4d 45 52 43 48 nty of. * MERCH
0200: 41 4e 54 41 42 49 4c 49 54 59 20 6f 72 20 46 49 ANTABILITY or FI
0210: 54 4e 45 53 53 20 46 4f 52 20 41 20 50 41 52 54 TNESS FOR A PART
0220: 49 43 55 4c 41 52 20 50 55 52 50 4f 53 45 2e 20 ICULAR PURPOSE.
0230: 20 53 65 65 20 74 68 65 0a 20 2a 20 20 47 4e 55 See the. * GNU
0240: 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 20 General Public
0250: 4c 69 63 65 6e 73 65 20 66 6f 72 20 6d 6f 72 65 License for more
0260: 20 64 65 74 61 69 6c 73 2e 0a 20 2a 0a 20 2a 20 details.. *. *
0270: 20 59 6f 75 20 73 68 6f 75 6c 64 20 68 61 76 65 You should have
0280: 20 72 65 63 65 69 76 65 64 20 61 20 63 6f 70 79 received a copy
0290: 20 6f 66 20 74 68 65 20 47 4e 55 20 47 65 6e 65 of the GNU Gene
02a0: 72 61 6c 20 50 75 62 6c 69 63 20 4c 69 63 65 6e ral Public Licen
02b0: 73 65 0a 20 2a 20 20 61 6c 6f 6e 67 20 77 69 74 se. * along wit
02c0: 68 20 74 68 69 73 20 70 72 6f 67 72 61 6d 3b 20 h this program;
02d0: 69 66 20 6e 6f 74 2c 20 77 72 69 74 65 20 74 6f if not, write to
02e0: 20 74 68 65 20 46 72 65 65 20 53 6f 66 74 77 61 the Free Softwa
02f0: 72 65 0a 20 2a 20 20 46 6f 75 6e 64 61 74 69 6f re. * Foundatio
0300: 6e 2c 20 49 6e 63 2e 2c 20 35 39 20 54 65 6d 70 n, Inc., 59 Temp
0310: 6c 65 20 50 6c 61 63 65 2c 20 53 75 69 74 65 20 le Place, Suite
0320: 33 33 30 2c 20 42 6f 73 74 6f 6e 2c 20 4d 41 20 330, Boston, MA
0330: 20 30 32 31 31 31 2d 31 33 30 37 20 20 55 53 41 02111-1307 USA
0340: 0a 20 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 20 3c . */..#include <
0350: 73 74 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c 75 stdlib.h>.#inclu
0360: 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 de <string.h>.#i
0370: 6e 63 6c 75 64 65 20 3c 70 77 6d 61 6e 2e 68 3e nclude <pwman.h>
0380: 0a 23 69 6e 63 6c 75 64 65 20 3c 75 69 2e 68 3e .#include <ui.h>
0390: 0a 0a 2f 2a 78 74 65 72 6e 20 69 6e 74 20 63 75 ../*xtern int cu
03a0: 72 69 74 65 6d 3b 2a 2f 0a 0a 50 77 53 65 61 72 ritem;*/..PwSear
03b0: 63 68 20 2a 0a 73 65 61 72 63 68 5f 6e 65 77 28 ch *.search_new(
03c0: 29 0a 7b 0a 09 50 77 53 65 61 72 63 68 20 2a 6e ).{..PwSearch *n
03d0: 65 77 3b 0a 0a 09 6e 65 77 20 3d 20 6d 61 6c 6c ew;...new = mall
03e0: 6f 63 28 73 69 7a 65 6f 66 28 50 77 53 65 61 72 oc(sizeof(PwSear
03f0: 63 68 29 29 3b 0a 09 69 66 28 6e 65 77 20 3d 3d ch));..if(new ==
0400: 20 4e 55 4c 4c 29 20 7b 0a 09 09 70 77 5f 61 62 NULL) {...pw_ab
0410: 6f 72 74 28 22 46 61 69 6c 65 64 20 74 6f 20 61 ort("Failed to a
0420: 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 llocate memory t
0430: 6f 20 68 6f 6c 64 20 73 65 61 72 63 68 20 64 65 o hold search de
0440: 74 61 69 6c 73 21 22 29 3b 0a 09 7d 0a 0a 09 6e tails!");..}...n
0450: 65 77 2d 3e 73 65 61 72 63 68 5f 74 65 72 6d 20 ew->search_term
0460: 3d 20 6d 61 6c 6c 6f 63 28 53 54 52 49 4e 47 5f = malloc(STRING_
0470: 4c 4f 4e 47 29 3b 0a 09 69 66 28 6e 65 77 2d 3e LONG);..if(new->
0480: 73 65 61 72 63 68 5f 74 65 72 6d 20 3d 3d 20 4e search_term == N
0490: 55 4c 4c 29 20 7b 0a 09 09 70 77 5f 61 62 6f 72 ULL) {...pw_abor
04a0: 74 28 22 46 61 69 6c 65 64 20 74 6f 20 61 6c 6c t("Failed to all
04b0: 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 6f 20 ocate memory to
04c0: 68 6f 6c 64 20 73 65 61 72 63 68 20 74 65 72 6d hold search term
04d0: 21 22 29 3b 0a 09 7d 0a 09 6e 65 77 2d 3e 73 65 !");..}..new->se
04e0: 61 72 63 68 5f 74 65 72 6d 5b 30 5d 20 3d 20 30 arch_term[0] = 0
04f0: 3b 0a 0a 09 72 65 74 75 72 6e 20 6e 65 77 3b 0a ;...return new;.
0500: 7d 0a 0a 2f 2a 0a 20 2a 20 53 74 72 69 6e 67 20 }../*. * String
0510: 63 68 65 63 6b 69 6e 67 20 6f 6e 6c 79 20 63 61 checking only ca
0520: 73 65 20 69 6e 73 65 6e 73 69 74 69 76 65 20 75 se insensitive u
0530: 73 69 6e 67 20 67 6e 75 20 67 6c 69 62 63 0a 20 sing gnu glibc.
0540: 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 2a 0a */.static char*.
0550: 73 65 61 72 63 68 5f 73 74 72 63 61 73 65 73 74 search_strcasest
0560: 72 28 63 68 61 72 20 2a 68 61 79 73 74 61 63 6b r(char *haystack
0570: 2c 20 63 68 61 72 20 2a 6e 65 65 64 6c 65 29 7b , char *needle){
0580: 0a 09 2f 2f 20 4e 65 76 65 72 20 6d 61 74 63 68 ..// Never match
0590: 65 73 20 69 66 20 6e 75 6c 6c 2f 65 6d 70 74 79 es if null/empty
05a0: 20 73 74 72 69 6e 67 20 67 69 76 65 6e 0a 09 69 string given..i
05b0: 66 28 68 61 79 73 74 61 63 6b 20 3d 3d 20 4e 55 f(haystack == NU
05c0: 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 20 30 LL) {...return 0
05d0: 3b 0a 09 7d 0a 09 69 66 28 73 74 72 6c 65 6e 28 ;..}..if(strlen(
05e0: 68 61 79 73 74 61 63 6b 29 20 3d 3d 20 30 29 20 haystack) == 0)
05f0: 7b 0a 09 09 72 65 74 75 72 6e 20 30 3b 0a 09 7d {...return 0;..}
0600: 0a 0a 23 69 66 64 65 66 20 48 41 56 45 5f 53 54 ..#ifdef HAVE_ST
0610: 52 43 41 53 45 53 54 52 0a 09 72 65 74 75 72 6e RCASESTR..return
0620: 20 28 63 68 61 72 2a 29 73 74 72 63 61 73 65 73 (char*)strcases
0630: 74 72 28 68 61 79 73 74 61 63 6b 2c 20 6e 65 65 tr(haystack, nee
0640: 64 6c 65 29 3b 0a 23 65 6c 73 65 0a 09 72 65 74 dle);.#else..ret
0650: 75 72 6e 20 28 63 68 61 72 2a 29 73 74 72 73 74 urn (char*)strst
0660: 72 28 68 61 79 73 74 61 63 6b 2c 20 6e 65 65 64 r(haystack, need
0670: 6c 65 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a le);.#endif.}...
0680: 50 57 53 65 61 72 63 68 52 65 73 75 6c 74 2a 20 PWSearchResult*
0690: 0a 5f 73 65 61 72 63 68 5f 61 64 64 5f 69 66 5f ._search_add_if_
06a0: 6d 61 74 63 68 65 73 28 50 57 53 65 61 72 63 68 matches(PWSearch
06b0: 52 65 73 75 6c 74 2a 20 63 75 72 72 65 6e 74 2c Result* current,
06c0: 20 50 77 2a 20 65 6e 74 72 79 2c 20 50 57 4c 69 Pw* entry, PWLi
06d0: 73 74 2a 20 6c 69 73 74 29 20 7b 0a 09 50 57 53 st* list) {..PWS
06e0: 65 61 72 63 68 52 65 73 75 6c 74 2a 20 6e 65 78 earchResult* nex
06f0: 74 20 3d 20 4e 55 4c 4c 3b 0a 0a 09 2f 2f 20 44 t = NULL;...// D
0700: 69 64 20 77 65 20 67 65 74 20 61 6e 20 65 6e 74 id we get an ent
0710: 72 79 20 6f 66 20 61 20 6c 69 73 74 3f 0a 09 69 ry of a list?..i
0720: 66 28 65 6e 74 72 79 20 21 3d 20 4e 55 4c 4c 29 f(entry != NULL)
0730: 20 7b 0a 09 09 69 66 28 73 65 61 72 63 68 5f 73 {...if(search_s
0740: 74 72 63 61 73 65 73 74 72 28 65 6e 74 72 79 2d trcasestr(entry-
0750: 3e 6e 61 6d 65 2c 20 6f 70 74 69 6f 6e 73 2d 3e >name, options->
0760: 73 65 61 72 63 68 2d 3e 73 65 61 72 63 68 5f 74 search->search_t
0770: 65 72 6d 29 0a 09 09 09 20 7c 7c 20 73 65 61 72 erm).... || sear
0780: 63 68 5f 73 74 72 63 61 73 65 73 74 72 28 65 6e ch_strcasestr(en
0790: 74 72 79 2d 3e 68 6f 73 74 2c 20 6f 70 74 69 6f try->host, optio
07a0: 6e 73 2d 3e 73 65 61 72 63 68 2d 3e 73 65 61 72 ns->search->sear
07b0: 63 68 5f 74 65 72 6d 29 0a 09 09 09 20 7c 7c 20 ch_term).... ||
07c0: 73 65 61 72 63 68 5f 73 74 72 63 61 73 65 73 74 search_strcasest
07d0: 72 28 65 6e 74 72 79 2d 3e 75 73 65 72 2c 20 6f r(entry->user, o
07e0: 70 74 69 6f 6e 73 2d 3e 73 65 61 72 63 68 2d 3e ptions->search->
07f0: 73 65 61 72 63 68 5f 74 65 72 6d 29 0a 09 09 09 search_term)....
0800: 20 7c 7c 20 73 65 61 72 63 68 5f 73 74 72 63 61 || search_strca
0810: 73 65 73 74 72 28 65 6e 74 72 79 2d 3e 70 61 73 sestr(entry->pas
0820: 73 77 64 2c 20 6f 70 74 69 6f 6e 73 2d 3e 73 65 swd, options->se
0830: 61 72 63 68 2d 3e 73 65 61 72 63 68 5f 74 65 72 arch->search_ter
0840: 6d 29 0a 09 09 09 20 7c 7c 20 73 65 61 72 63 68 m).... || search
0850: 5f 73 74 72 63 61 73 65 73 74 72 28 65 6e 74 72 _strcasestr(entr
0860: 79 2d 3e 6c 61 75 6e 63 68 2c 20 6f 70 74 69 6f y->launch, optio
0870: 6e 73 2d 3e 73 65 61 72 63 68 2d 3e 73 65 61 72 ns->search->sear
0880: 63 68 5f 74 65 72 6d 29 0a 09 09 29 20 7b 0a 09 ch_term)...) {..
0890: 09 09 6e 65 78 74 20 3d 20 6d 61 6c 6c 6f 63 28 ..next = malloc(
08a0: 20 73 69 7a 65 6f 66 28 50 57 53 65 61 72 63 68 sizeof(PWSearch
08b0: 52 65 73 75 6c 74 29 20 29 3b 0a 09 09 09 6e 65 Result) );....ne
08c0: 78 74 2d 3e 65 6e 74 72 79 20 3d 20 65 6e 74 72 xt->entry = entr
08d0: 79 3b 0a 09 09 09 6e 65 78 74 2d 3e 73 75 62 6c y;....next->subl
08e0: 69 73 74 20 3d 20 6c 69 73 74 3b 0a 09 09 09 64 ist = list;....d
08f0: 65 62 75 67 28 22 4d 61 74 63 68 65 64 20 65 6e ebug("Matched en
0900: 74 72 79 20 6f 6e 20 68 6f 73 74 20 27 25 73 27 try on host '%s'
0910: 22 2c 20 65 6e 74 72 79 2d 3e 68 6f 73 74 29 3b ", entry->host);
0920: 0a 09 09 7d 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 ...}..} else {..
0930: 09 69 66 28 73 65 61 72 63 68 5f 73 74 72 63 61 .if(search_strca
0940: 73 65 73 74 72 28 6c 69 73 74 2d 3e 6e 61 6d 65 sestr(list->name
0950: 2c 20 6f 70 74 69 6f 6e 73 2d 3e 73 65 61 72 63 , options->searc
0960: 68 2d 3e 73 65 61 72 63 68 5f 74 65 72 6d 29 29 h->search_term))
0970: 20 7b 0a 09 09 09 6e 65 78 74 20 3d 20 6d 61 6c {....next = mal
0980: 6c 6f 63 28 20 73 69 7a 65 6f 66 28 50 57 53 65 loc( sizeof(PWSe
0990: 61 72 63 68 52 65 73 75 6c 74 29 20 29 3b 0a 09 archResult) );..
09a0: 09 09 6e 65 78 74 2d 3e 73 75 62 6c 69 73 74 20 ..next->sublist
09b0: 3d 20 6c 69 73 74 3b 0a 09 09 09 6e 65 78 74 2d = list;....next-
09c0: 3e 65 6e 74 72 79 20 3d 20 4e 55 4c 4c 3b 0a 09 >entry = NULL;..
09d0: 09 09 64 65 62 75 67 28 22 4d 61 74 63 68 65 64 ..debug("Matched
09e0: 20 73 75 62 6c 69 73 74 20 27 25 73 27 22 2c 20 sublist '%s'",
09f0: 6c 69 73 74 2d 3e 6e 61 6d 65 29 3b 0a 09 09 7d list->name);...}
0a00: 0a 09 7d 0a 0a 09 2f 2f 20 49 66 20 77 65 20 6d ..}...// If we m
0a10: 61 74 63 68 65 64 2c 20 61 70 70 65 6e 64 0a 09 atched, append..
0a20: 69 66 28 6e 65 78 74 20 3d 3d 20 4e 55 4c 4c 29 if(next == NULL)
0a30: 20 7b 0a 09 09 72 65 74 75 72 6e 20 63 75 72 72 {...return curr
0a40: 65 6e 74 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 ent;..} else {..
0a50: 09 69 66 28 63 75 72 72 65 6e 74 20 3d 3d 20 4e .if(current == N
0a60: 55 4c 4c 29 20 7b 0a 09 09 09 2f 2f 20 46 69 72 ULL) {....// Fir
0a70: 73 74 20 68 69 74 0a 09 09 09 73 65 61 72 63 68 st hit....search
0a80: 5f 72 65 73 75 6c 74 73 20 3d 20 6e 65 78 74 3b _results = next;
0a90: 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 2f ...} else {..../
0aa0: 2f 20 41 64 64 69 74 69 6f 6e 61 6c 20 68 69 74 / Additional hit
0ab0: 2c 20 61 70 70 65 6e 64 0a 09 09 09 63 75 72 72 , append....curr
0ac0: 65 6e 74 2d 3e 6e 65 78 74 20 3d 20 6e 65 78 74 ent->next = next
0ad0: 3b 0a 09 09 7d 0a 09 09 2f 2f 20 46 6f 72 20 6e ;...}...// For n
0ae0: 6f 77 2c 20 6e 6f 74 68 69 6e 67 20 66 6f 6c 6c ow, nothing foll
0af0: 6f 77 73 20 75 73 0a 09 09 6e 65 78 74 2d 3e 6e ows us...next->n
0b00: 65 78 74 20 3d 20 4e 55 4c 4c 3b 0a 09 09 2f 2f ext = NULL;...//
0b10: 20 57 65 20 61 72 65 20 74 68 65 20 6e 65 77 20 We are the new
0b20: 63 75 72 72 65 6e 74 20 65 6e 74 72 79 0a 09 09 current entry...
0b30: 72 65 74 75 72 6e 20 6e 65 78 74 3b 0a 09 7d 0a return next;..}.
0b40: 7d 0a 0a 0a 69 6e 74 0a 73 65 61 72 63 68 5f 61 }...int.search_a
0b50: 70 70 6c 79 28 29 0a 7b 0a 09 50 57 4c 69 73 74 pply().{..PWList
0b60: 20 2a 73 74 61 63 6b 5b 4d 41 58 5f 53 45 41 52 *stack[MAX_SEAR
0b70: 43 48 5f 44 45 50 54 48 5d 3b 0a 09 50 57 4c 69 CH_DEPTH];..PWLi
0b80: 73 74 20 2a 74 6d 70 4c 69 73 74 20 3d 20 4e 55 st *tmpList = NU
0b90: 4c 4c 3b 20 0a 09 50 77 20 2a 74 6d 70 20 3d 20 LL; ..Pw *tmp =
0ba0: 4e 55 4c 4c 3b 20 0a 09 69 6e 74 20 64 65 70 74 NULL; ..int dept
0bb0: 68 3b 0a 09 69 6e 74 20 73 74 65 70 70 69 6e 67 h;..int stepping
0bc0: 5f 62 61 63 6b 3b 0a 0a 09 50 57 53 65 61 72 63 _back;...PWSearc
0bd0: 68 52 65 73 75 6c 74 20 2a 63 75 72 20 3d 20 4e hResult *cur = N
0be0: 55 4c 4c 3b 0a 0a 09 2f 2f 20 54 69 64 79 20 75 ULL;...// Tidy u
0bf0: 70 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 73 p any existing s
0c00: 65 61 72 63 68 20 72 65 73 75 6c 74 73 0a 09 69 earch results..i
0c10: 66 28 73 65 61 72 63 68 5f 72 65 73 75 6c 74 73 f(search_results
0c20: 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 5f 73 != NULL) {..._s
0c30: 65 61 72 63 68 5f 66 72 65 65 28 29 3b 0a 09 7d earch_free();..}
0c40: 0a 09 2f 2f 20 49 66 20 6e 6f 20 73 65 61 72 63 ..// If no searc
0c50: 68 20 74 65 72 6d 2c 20 74 68 65 6e 20 6e 6f 74 h term, then not
0c60: 68 69 6e 67 20 74 6f 20 64 6f 21 0a 09 69 66 28 hing to do!..if(
0c70: 20 73 65 61 72 63 68 5f 61 63 74 69 76 65 28 6f search_active(o
0c80: 70 74 69 6f 6e 73 2d 3e 73 65 61 72 63 68 29 20 ptions->search)
0c90: 3d 3d 20 30 20 29 20 7b 0a 09 09 72 65 74 75 72 == 0 ) {...retur
0ca0: 6e 20 31 3b 0a 09 7d 0a 0a 09 2f 2f 20 4d 61 6b n 1;..}...// Mak
0cb0: 65 20 73 75 72 65 20 77 65 20 68 61 76 65 20 61 e sure we have a
0cc0: 20 63 6c 65 61 6e 20 73 65 61 72 63 68 20 73 74 clean search st
0cd0: 61 63 6b 20 73 6f 20 77 65 20 77 6f 6e 27 74 20 ack so we won't
0ce0: 67 65 74 20 63 6f 6e 66 75 73 65 64 0a 09 66 6f get confused..fo
0cf0: 72 28 64 65 70 74 68 3d 30 3b 20 64 65 70 74 68 r(depth=0; depth
0d00: 3c 4d 41 58 5f 53 45 41 52 43 48 5f 44 45 50 54 <MAX_SEARCH_DEPT
0d10: 48 3b 20 64 65 70 74 68 2b 2b 29 20 7b 0a 09 09 H; depth++) {...
0d20: 73 74 61 63 6b 5b 64 65 70 74 68 5d 20 3d 20 4e stack[depth] = N
0d30: 55 4c 4c 3b 0a 09 7d 0a 0a 09 2f 2f 20 53 65 74 ULL;..}...// Set
0d40: 75 70 20 66 6f 72 20 73 74 61 72 74 0a 09 64 65 up for start..de
0d50: 70 74 68 20 3d 20 30 3b 0a 09 74 6d 70 4c 69 73 pth = 0;..tmpLis
0d60: 74 20 3d 20 70 77 6c 69 73 74 3b 0a 09 73 74 65 t = pwlist;..ste
0d70: 70 70 69 6e 67 5f 62 61 63 6b 20 3d 20 30 3b 0a pping_back = 0;.
0d80: 0a 09 2f 2f 20 46 69 6e 64 20 61 6e 79 74 68 69 ..// Find anythi
0d90: 6e 67 20 77 65 20 6c 69 6b 65 20 74 68 65 20 6c ng we like the l
0da0: 6f 6f 6b 20 6f 66 0a 09 77 68 69 6c 65 28 64 65 ook of..while(de
0db0: 70 74 68 20 3e 3d 20 30 29 20 7b 0a 09 09 2f 2f pth >= 0) {...//
0dc0: 20 41 6e 79 20 73 75 62 6c 69 73 74 73 3f 0a 09 Any sublists?..
0dd0: 09 69 66 28 21 73 74 65 70 70 69 6e 67 5f 62 61 .if(!stepping_ba
0de0: 63 6b 20 26 26 20 0a 09 09 09 09 74 6d 70 4c 69 ck && .....tmpLi
0df0: 73 74 2d 3e 73 75 62 6c 69 73 74 73 20 21 3d 20 st->sublists !=
0e00: 4e 55 4c 4c 20 26 26 20 64 65 70 74 68 20 3c 20 NULL && depth <
0e10: 4d 41 58 5f 53 45 41 52 43 48 5f 44 45 50 54 48 MAX_SEARCH_DEPTH
0e20: 29 20 7b 0a 09 09 09 2f 2f 20 50 72 65 70 61 72 ) {....// Prepar
0e30: 65 20 74 6f 20 64 65 73 63 65 6e 64 0a 09 09 09 e to descend....
0e40: 73 74 61 63 6b 5b 64 65 70 74 68 5d 20 3d 20 74 stack[depth] = t
0e50: 6d 70 4c 69 73 74 3b 0a 09 09 09 64 65 70 74 68 mpList;....depth
0e60: 2b 2b 3b 0a 09 09 09 74 6d 70 4c 69 73 74 20 3d ++;....tmpList =
0e70: 20 74 6d 70 4c 69 73 74 2d 3e 73 75 62 6c 69 73 tmpList->sublis
0e80: 74 73 3b 0a 0a 09 09 09 2f 2f 20 54 65 73 74 20 ts;.....// Test
0e90: 66 69 72 73 74 20 63 68 69 6c 64 0a 09 09 09 63 first child....c
0ea0: 75 72 20 3d 20 5f 73 65 61 72 63 68 5f 61 64 64 ur = _search_add
0eb0: 5f 69 66 5f 6d 61 74 63 68 65 73 28 63 75 72 2c _if_matches(cur,
0ec0: 20 4e 55 4c 4c 2c 20 74 6d 70 4c 69 73 74 29 3b NULL, tmpList);
0ed0: 0a 0a 09 09 09 2f 2f 20 44 65 73 63 65 6e 64 20 .....// Descend
0ee0: 69 6e 74 6f 20 66 69 72 73 74 20 63 68 69 6c 64 into first child
0ef0: 0a 09 09 09 63 6f 6e 74 69 6e 75 65 3b 0a 09 09 ....continue;...
0f00: 7d 0a 09 09 73 74 65 70 70 69 6e 67 5f 62 61 63 }...stepping_bac
0f10: 6b 20 3d 20 30 3b 0a 0a 09 09 2f 2f 20 41 6e 79 k = 0;....// Any
0f20: 20 65 6e 74 72 69 65 73 3f 0a 09 09 69 66 28 74 entries?...if(t
0f30: 6d 70 4c 69 73 74 2d 3e 6c 69 73 74 29 20 7b 0a mpList->list) {.
0f40: 09 09 09 74 6d 70 20 3d 20 74 6d 70 4c 69 73 74 ...tmp = tmpList
0f50: 2d 3e 6c 69 73 74 3b 0a 09 09 09 77 68 69 6c 65 ->list;....while
0f60: 28 74 6d 70 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a (tmp != NULL) {.
0f70: 09 09 09 09 2f 2f 20 54 65 73 74 20 74 68 69 73 ....// Test this
0f80: 20 65 6e 74 72 79 0a 09 09 09 09 63 75 72 20 3d entry.....cur =
0f90: 20 5f 73 65 61 72 63 68 5f 61 64 64 5f 69 66 5f _search_add_if_
0fa0: 6d 61 74 63 68 65 73 28 63 75 72 2c 20 74 6d 70 matches(cur, tmp
0fb0: 2c 20 74 6d 70 4c 69 73 74 29 3b 0a 09 09 09 09 , tmpList);.....
0fc0: 2f 2f 20 4e 65 78 74 20 65 6e 74 72 79 0a 09 09 // Next entry...
0fd0: 09 09 74 6d 70 20 3d 20 74 6d 70 2d 3e 6e 65 78 ..tmp = tmp->nex
0fe0: 74 3b 0a 09 09 09 7d 0a 09 09 7d 0a 0a 09 09 2f t;....}...}..../
0ff0: 2f 20 4e 65 78 74 20 73 69 62 6c 69 6e 67 20 69 / Next sibling i
1000: 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 0a 09 f there is one..
1010: 09 69 66 28 74 6d 70 4c 69 73 74 2d 3e 6e 65 78 .if(tmpList->nex
1020: 74 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 09 t != NULL) {....
1030: 74 6d 70 4c 69 73 74 20 3d 20 74 6d 70 4c 69 73 tmpList = tmpLis
1040: 74 2d 3e 6e 65 78 74 3b 0a 09 09 09 2f 2f 20 54 t->next;....// T
1050: 65 73 74 20 73 69 62 6c 69 6e 67 0a 09 09 09 63 est sibling....c
1060: 75 72 20 3d 20 5f 73 65 61 72 63 68 5f 61 64 64 ur = _search_add
1070: 5f 69 66 5f 6d 61 74 63 68 65 73 28 63 75 72 2c _if_matches(cur,
1080: 20 4e 55 4c 4c 2c 20 74 6d 70 4c 69 73 74 29 3b NULL, tmpList);
1090: 0a 09 09 09 2f 2f 20 50 72 6f 63 65 73 73 20 73 ....// Process s
10a0: 69 62 6c 69 6e 67 0a 09 09 09 63 6f 6e 74 69 6e ibling....contin
10b0: 75 65 3b 0a 09 09 7d 0a 0a 09 09 2f 2f 20 4f 74 ue;...}....// Ot
10c0: 68 65 72 77 69 73 65 20 73 74 65 70 20 75 70 0a herwise step up.
10d0: 09 09 64 65 70 74 68 2d 2d 3b 0a 09 09 73 74 65 ..depth--;...ste
10e0: 70 70 69 6e 67 5f 62 61 63 6b 20 3d 20 31 3b 0a pping_back = 1;.
10f0: 09 09 69 66 28 64 65 70 74 68 20 3e 3d 20 30 29 ..if(depth >= 0)
1100: 20 7b 0a 09 09 09 74 6d 70 4c 69 73 74 20 3d 20 {....tmpList =
1110: 73 74 61 63 6b 5b 64 65 70 74 68 5d 3b 0a 09 09 stack[depth];...
1120: 09 73 74 61 63 6b 5b 64 65 70 74 68 5d 20 3d 20 .stack[depth] =
1130: 4e 55 4c 4c 3b 0a 09 09 7d 0a 09 7d 0a 09 0a 09 NULL;...}..}....
1140: 2f 2f 20 41 6c 6c 20 64 6f 6e 65 0a 09 72 65 74 // All done..ret
1150: 75 72 6e 20 31 3b 0a 7d 0a 0a 69 6e 74 20 0a 73 urn 1;.}..int .s
1160: 65 61 72 63 68 5f 72 65 6d 6f 76 65 28 29 0a 7b earch_remove().{
1170: 0a 09 2f 2f 20 50 75 74 20 74 68 69 6e 67 73 20 ..// Put things
1180: 62 61 63 6b 20 68 6f 77 20 74 68 65 79 20 73 68 back how they sh
1190: 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 0a 09 ould have been..
11a0: 63 75 72 72 65 6e 74 5f 70 77 5f 73 75 62 6c 69 current_pw_subli
11b0: 73 74 20 3d 20 70 77 6c 69 73 74 3b 0a 09 63 75 st = pwlist;..cu
11c0: 72 72 65 6e 74 5f 70 77 5f 73 75 62 6c 69 73 74 rrent_pw_sublist
11d0: 2d 3e 63 75 72 72 65 6e 74 5f 69 74 65 6d 20 3d ->current_item =
11e0: 20 2d 31 3b 0a 0a 09 2f 2f 20 46 72 65 65 20 74 -1;...// Free t
11f0: 68 65 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 he memory held b
1200: 79 20 74 68 65 20 73 65 61 72 63 68 20 72 65 73 y the search res
1210: 75 6c 74 73 0a 09 5f 73 65 61 72 63 68 5f 66 72 ults.._search_fr
1220: 65 65 28 29 3b 0a 0a 09 2f 2f 20 43 6c 65 61 72 ee();...// Clear
1230: 20 74 68 65 20 73 65 61 72 63 68 20 74 65 72 6d the search term
1240: 20 74 6f 6f 0a 09 6f 70 74 69 6f 6e 73 2d 3e 73 too..options->s
1250: 65 61 72 63 68 2d 3e 73 65 61 72 63 68 5f 74 65 earch->search_te
1260: 72 6d 5b 30 5d 20 3d 20 30 3b 0a 0a 09 2f 2f 20 rm[0] = 0;...//
1270: 42 61 63 6b 20 74 6f 20 74 68 65 20 6f 6c 64 20 Back to the old
1280: 73 63 72 65 65 6e 0a 09 75 69 6c 69 73 74 5f 72 screen..uilist_r
1290: 65 66 72 65 73 68 28 29 3b 0a 7d 0a 0a 69 6e 74 efresh();.}..int
12a0: 20 0a 5f 73 65 61 72 63 68 5f 66 72 65 65 28 29 ._search_free()
12b0: 0a 7b 0a 09 50 57 53 65 61 72 63 68 52 65 73 75 .{..PWSearchResu
12c0: 6c 74 20 2a 63 75 72 3b 0a 09 50 57 53 65 61 72 lt *cur;..PWSear
12d0: 63 68 52 65 73 75 6c 74 20 2a 6e 65 78 74 3b 0a chResult *next;.
12e0: 0a 09 2f 2f 20 46 72 65 65 20 74 68 65 20 6d 65 ..// Free the me
12f0: 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 65 mory held by the
1300: 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 73 0a search results.
1310: 09 63 75 72 20 3d 20 73 65 61 72 63 68 5f 72 65 .cur = search_re
1320: 73 75 6c 74 73 3b 0a 09 77 68 69 6c 65 28 63 75 sults;..while(cu
1330: 72 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 6e r != NULL) {...n
1340: 65 78 74 20 3d 20 63 75 72 2d 3e 6e 65 78 74 3b ext = cur->next;
1350: 0a 09 09 66 72 65 65 28 63 75 72 29 3b 0a 09 09 ...free(cur);...
1360: 63 75 72 20 3d 20 6e 65 78 74 3b 0a 09 7d 0a 09 cur = next;..}..
1370: 73 65 61 72 63 68 5f 72 65 73 75 6c 74 73 20 3d search_results =
1380: 20 4e 55 4c 4c 3b 0a 7d 0a 0a 76 6f 69 64 0a 73 NULL;.}..void.s
1390: 65 61 72 63 68 5f 67 65 74 28 29 0a 7b 0a 09 63 earch_get().{..c
13a0: 68 61 72 20 63 3b 0a 09 69 66 28 6f 70 74 69 6f har c;..if(optio
13b0: 6e 73 2d 3e 73 65 61 72 63 68 20 3d 3d 20 4e 55 ns->search == NU
13c0: 4c 4c 29 20 7b 0a 09 09 64 65 62 75 67 28 22 4e LL) {...debug("N
13d0: 6f 20 6f 70 74 69 6f 6e 73 2d 3e 73 65 61 72 63 o options->searc
13e0: 68 22 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 h");..} else {..
13f0: 09 69 66 28 6f 70 74 69 6f 6e 73 2d 3e 73 65 61 .if(options->sea
1400: 72 63 68 2d 3e 73 65 61 72 63 68 5f 74 65 72 6d rch->search_term
1410: 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 09 64 == NULL) {....d
1420: 65 62 75 67 28 22 4e 6f 20 6f 70 74 69 6f 6e 73 ebug("No options
1430: 2d 3e 73 65 61 72 63 68 2d 3e 73 65 61 72 63 68 ->search->search
1440: 5f 74 65 72 6d 22 29 3b 0a 09 09 7d 20 65 6c 73 _term");...} els
1450: 65 20 7b 0a 09 09 09 64 65 62 75 67 28 22 4c 65 e {....debug("Le
1460: 6e 20 77 61 73 20 25 64 22 2c 20 73 74 72 6c 65 n was %d", strle
1470: 6e 28 6f 70 74 69 6f 6e 73 2d 3e 73 65 61 72 63 n(options->searc
1480: 68 2d 3e 73 65 61 72 63 68 5f 74 65 72 6d 29 29 h->search_term))
1490: 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 75 69 5f 73 74 ;...}..}...ui_st
14a0: 61 74 75 73 6c 69 6e 65 5f 61 73 6b 5f 73 74 72 atusline_ask_str
14b0: 28 22 53 74 72 69 6e 67 20 74 6f 20 73 65 61 72 ("String to sear
14c0: 63 68 20 66 6f 72 3a 20 22 2c 20 6f 70 74 69 6f ch for: ", optio
14d0: 6e 73 2d 3e 73 65 61 72 63 68 2d 3e 73 65 61 72 ns->search->sear
14e0: 63 68 5f 74 65 72 6d 2c 20 53 54 52 49 4e 47 5f ch_term, STRING_
14f0: 4d 45 44 49 55 4d 29 3b 0a 0a 09 73 65 61 72 63 MEDIUM);...searc
1500: 68 5f 61 70 70 6c 79 28 29 3b 0a 0a 09 63 75 72 h_apply();...cur
1510: 72 65 6e 74 5f 70 77 5f 73 75 62 6c 69 73 74 2d rent_pw_sublist-
1520: 3e 63 75 72 72 65 6e 74 5f 69 74 65 6d 20 3d 20 >current_item =
1530: 2d 31 3b 0a 09 75 69 6c 69 73 74 5f 72 65 66 72 -1;..uilist_refr
1540: 65 73 68 28 29 3b 0a 7d 0a 0a 0a 69 6e 74 0a 73 esh();.}...int.s
1550: 65 61 72 63 68 5f 61 6c 65 72 74 28 50 77 53 65 earch_alert(PwSe
1560: 61 72 63 68 2a 20 73 72 63 68 29 0a 7b 0a 09 63 arch* srch).{..c
1570: 68 61 72 20 61 6c 65 72 74 5b 38 30 5d 3b 09 0a har alert[80];..
1580: 0a 09 69 66 28 20 73 65 61 72 63 68 5f 61 63 74 ..if( search_act
1590: 69 76 65 28 73 72 63 68 29 20 3d 3d 20 30 20 29 ive(srch) == 0 )
15a0: 20 7b 0a 09 09 72 65 74 75 72 6e 20 31 3b 0a 09 {...return 1;..
15b0: 7d 0a 0a 09 69 66 28 73 65 61 72 63 68 5f 72 65 }...if(search_re
15c0: 73 75 6c 74 73 20 3d 3d 20 4e 55 4c 4c 29 20 7b sults == NULL) {
15d0: 0a 09 09 73 70 72 69 6e 74 66 28 61 6c 65 72 74 ...sprintf(alert
15e0: 2c 20 22 20 28 4e 6f 20 72 65 73 75 6c 74 73 20 , " (No results
15f0: 66 6f 75 6e 64 20 66 6f 72 20 27 25 73 27 29 22 found for '%s')"
1600: 2c 20 73 72 63 68 2d 3e 73 65 61 72 63 68 5f 74 , srch->search_t
1610: 65 72 6d 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a erm);..} else {.
1620: 09 09 73 70 72 69 6e 74 66 28 61 6c 65 72 74 2c ..sprintf(alert,
1630: 20 22 20 28 53 65 61 72 63 68 20 72 65 73 75 6c " (Search resul
1640: 74 73 20 66 6f 72 20 27 25 73 27 29 22 2c 20 73 ts for '%s')", s
1650: 72 63 68 2d 3e 73 65 61 72 63 68 5f 74 65 72 6d rch->search_term
1660: 29 3b 0a 09 7d 0a 0a 09 75 69 5f 73 74 61 74 75 );..}...ui_statu
1670: 73 6c 69 6e 65 5f 63 6c 65 61 72 28 29 3b 0a 09 sline_clear();..
1680: 75 69 5f 73 74 61 74 75 73 6c 69 6e 65 5f 6d 73 ui_statusline_ms
1690: 67 28 61 6c 65 72 74 29 3b 0a 7d 0a 0a 0a 69 6e g(alert);.}...in
16a0: 74 20 73 65 61 72 63 68 5f 61 63 74 69 76 65 28 t search_active(
16b0: 50 77 53 65 61 72 63 68 2a 20 73 72 63 68 29 0a PwSearch* srch).
16c0: 7b 0a 09 69 66 28 20 28 73 72 63 68 20 3d 3d 20 {..if( (srch ==
16d0: 4e 55 4c 4c 29 20 7c 7c 20 28 73 72 63 68 2d 3e NULL) || (srch->
16e0: 73 65 61 72 63 68 5f 74 65 72 6d 20 3d 3d 20 4e search_term == N
16f0: 55 4c 4c 29 20 29 7b 0a 09 09 2f 2a 20 6e 6f 20 ULL) ){.../* no
1700: 73 65 61 72 63 68 20 6f 62 6a 65 63 74 20 2a 2f search object */
1710: 0a 09 09 72 65 74 75 72 6e 20 30 3b 0a 09 7d 0a ...return 0;..}.
1720: 09 69 66 28 20 73 74 72 6c 65 6e 28 73 72 63 68 .if( strlen(srch
1730: 2d 3e 73 65 61 72 63 68 5f 74 65 72 6d 29 20 3d ->search_term) =
1740: 3d 20 30 20 29 7b 0a 09 09 2f 2a 20 6e 6f 20 73 = 0 ){.../* no s
1750: 65 61 72 63 68 20 2a 2f 0a 09 09 72 65 74 75 72 earch */...retur
1760: 6e 20 30 3b 0a 09 7d 0a 09 72 65 74 75 72 6e 20 n 0;..}..return
1770: 31 3b 0a 7d 0a 1;.}.