File: ngsrch.c

package info (click to toggle)
trn4 4.0-test77-13
  • links: PTS, VCS
  • area: non-free
  • in suites: bullseye
  • size: 3,656 kB
  • sloc: ansic: 48,331; sh: 6,817; tcl: 1,696; yacc: 660; perl: 108; makefile: 24
file content (225 lines) | stat: -rw-r--r-- 4,680 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* ngsrch.c
 */
/* This software is copyrighted as detailed in the LICENSE file. */


#include "EXTERN.h"
#include "common.h"
#include "list.h"
#include "ngdata.h"
#include "hash.h"
#include "nntpclient.h"
#include "datasrc.h"
#include "addng.h"
#include "nntp.h"
#include "ngstuff.h"
#include "rcstuff.h"
#include "final.h"
#include "search.h"
#include "trn.h"
#include "util.h"
#include "util2.h"
#include "term.h"
#include "rcln.h"
#include "cache.h"
#include "rt-select.h"
#include "rt-util.h"
#include "INTERN.h"
#include "ngsrch.h"

#ifdef NGSEARCH
COMPEX ngcompex;
#endif

void
ngsrch_init()
{
#ifdef NGSEARCH
    init_compex(&ngcompex);
#endif
}

#ifdef NGSEARCH
int
ng_search(patbuf,get_cmd)
char* patbuf;				/* if patbuf != buf, get_cmd must */
int get_cmd;				/*   be set to FALSE!!! */
{
    register char cmdchr = *patbuf;	/* what kind of search? */
    register char* s;
    char* pattern;			/* unparsed pattern */
    char* cmdlst = NULL;		/* list of commands to do */
    int ret = NGS_NOTFOUND;		/* assume no commands */
    bool backward = cmdchr == '?';	/* direction of search */
    bool output_level = (!use_threads && gmode != 's');
    NGDATA* ng_start = ngptr;

    int_count = 0;
    if (get_cmd && buf == patbuf)
	if (!finish_command(FALSE))	/* get rest of command */
	    return NGS_ABORT;

    perform_status_init(newsgroup_toread);
    s = cpytill(buf,patbuf+1,cmdchr);	/* ok to cpy buf+1 to buf */
    for (pattern = buf; *pattern == ' '; pattern++) ;
    if (*pattern)
	ng_doempty = FALSE;

    if (*s) {				/* modifiers or commands? */
	while (*++s) {
	    switch (*s) {
	    case 'r':
		ng_doempty = TRUE;
		break;
	    default:
		goto loop_break;
	    }
	}
      loop_break:;
    }
    while (isspace(*s) || *s == ':')
	s++;
    if (*s)
	cmdlst = savestr(s);
    else if (gmode == 's')
	cmdlst = savestr("+");
    if (cmdlst)
	ret = NGS_DONE;
    if ((s = ng_comp(&ngcompex,pattern,TRUE,TRUE)) != NULL) {
					/* compile regular expression */
	errormsg(s);
	ret = NGS_ERROR;
	goto exit;
    }
    if (!cmdlst) {
	fputs("\nSearching...",stdout);	/* give them something to read */
	fflush(stdout);
    }

    if (first_addgroup) {
	ADDGROUP *gp = first_addgroup;
	do {
	    if (execute(&ngcompex,gp->name) != NULL) {
		if (!cmdlst)
		    return NGS_FOUND;
		if (addgrp_perform(gp,cmdlst,output_level && page_line==1)<0) {
		    free(cmdlst);
		    return NGS_INTR;
		}
	    }
	    if (!output_level && page_line == 1)
		perform_status(newsgroup_toread, 50);
	} while ((gp = gp->next) != NULL);
	goto exit;
    }

    if (backward) {
	if (!ngptr)
	    ng_start = ngptr = last_ng;
	else if (!cmdlst) {
	    if (ngptr == first_ng)	/* skip current newsgroup */
		ngptr = last_ng;
	    else
		ngptr = ngptr->prev;
	}
    }
    else {
	if (!ngptr)
	    ng_start = ngptr = first_ng;
	else if (!cmdlst) {
	    if (ngptr == last_ng)	/* skip current newsgroup */
		ngptr = first_ng;
	    else
		ngptr = ngptr->next;
	}
    }

    if (!ngptr)
	return NGS_NOTFOUND;

    do {
	if (int_count) {
	    int_count = 0;
	    ret = NGS_INTR;
	    break;
	}

	if (ngptr->toread >= TR_NONE && ng_wanted(ngptr)) {
	    if (ngptr->toread == TR_NONE)
		set_toread(ngptr, ST_LAX);
	    if (ng_doempty || ((ngptr->toread > TR_NONE) ^ sel_rereading)) {
		if (!cmdlst)
		    return NGS_FOUND;
		set_ng(ngptr);
		if (ng_perform(cmdlst,output_level && page_line == 1) < 0) {
		    free(cmdlst);
		    return NGS_INTR;
		}
	    }
	    if (output_level && !cmdlst) {
		printf("\n[0 unread in %s -- skipping]",ngptr->rcline);
		fflush(stdout);
	    }
	}
	if (!output_level && page_line == 1)
	    perform_status(newsgroup_toread, 50);
    } while ((ngptr = (backward? (ngptr->prev? ngptr->prev : last_ng)
			       : (ngptr->next? ngptr->next : first_ng)))
		!= ng_start);
exit:
    if (cmdlst)
	free(cmdlst);
    return ret;
}
#endif /* NGSEARCH */

#ifdef NGSEARCH
bool
ng_wanted(np)
NGDATA* np;
{
    return execute(&ngcompex,np->rcline) != NULL;
}
#endif /* NGSEARCH */

char*
ng_comp(compex,pattern,RE,fold)
COMPEX* compex;
char* pattern;
bool_int RE;
bool_int fold;
{
    char ng_pattern[128];
    register char* s = pattern;
    register char* d = ng_pattern;

    if (!*s) {
	if (compile(compex, "", RE, fold))
	    return "No previous search pattern";
	return NULL;			/* reuse old pattern */
    }
    for (; *s; s++) {
	if (*s == '.') {
	    *d++ = '\\';
	    *d++ = *s;
	}
	else if (*s == '?') {
	    *d++ = '.';
	}
	else if (*s == '*') {
	    *d++ = '.';
	    *d++ = *s;
	}
#if OLD_RN_WAY
	else if (strnEQ(s,"all",3)) {
	    *d++ = '.';
	    *d++ = '*';
	    s += 2;
	}
#endif
	else
	    *d++ = *s;
    }
    *d = '\0';
    return compile(compex,ng_pattern,RE,fold);
}