File: index_main.c

package info (click to toggle)
sgrep 1.94a-7
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 1,144 kB
  • sloc: ansic: 11,324; sh: 6,182; makefile: 165
file content (266 lines) | stat: -rw-r--r-- 6,801 bytes parent folder | download
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "sgrep.h"

int index_query(IndexOptions *options, int argc, char *argv[]);

/*
 * List & description of options accepted by sgindex 
 */
struct index_opt_data {
	char opt;
	char *have_param;
	char *what_does;
} index_options[]= {
#if 0
    { 'C',NULL,"display copyright notice" },
#endif
    { 'h',NULL,"help (means this text)" },
    { 'i',NULL,"fold all words to lower case when indexing" },
    /* { 'R',NULL,"recurse into subdirectories" }, */ 
    { 'T',NULL,"show statistics about created index files" },
    { 'V',NULL,"display version information" },
    { 'v',NULL,"verbose mode. Shows what is going on"},
    { 'c',"<index file>", "create new index file" },
    { 'F',"<file>","read list of input files from <file> instead of command line" },
    { 'g',"<option>","set scanner option:" },
    { 'l',"<limit>", "make a list of possible stopwords" },
    { 'L',"<stop file>","write possible stopwords to file" },
    { 'S',"<stop file>","read stop word list from file" },
    { 'm',"<megabytes>", "main memory available for indexing in megabytes" },
    { 'w',"<char list>","set the list of characters used to recognize words" },
    { 'x',"<index file>","query existing index file"},
    { 'q',"<query>","Only can query terms" },
    { 0,NULL,NULL }
};

void index_usage(SgrepData *sgrep) {
    int i;
    sgrep_error(sgrep,"Usage: (sgindex | sgrep -I) [ -");
    for(i=0;index_options[i].opt!=0;i  ) {
	if (index_options[i].have_param!=NULL)
	{
	    sgrep_error(sgrep," -%c %s",
		    index_options[i].opt,
		    index_options[i].have_param);
	} else sgrep_error(sgrep,"%c",index_options[i].opt);
    }
    sgrep_error(sgrep," ] [<files...>]\n");
    sgrep_error(sgrep,"sgindex -h for help\n");
}

/*
 * Prints help 
 */
void print_index_help()
{
	int i;
	
	printf("Usage: (sgindex | sgrep -I) <options> <files...>\n");
	printf("Use 'sgrep -h' for help on query mode options.\n");
	printf("\nIndexing mode options are:\n");
	for (i=0;index_options[i].opt!=0;i  )
	{
		printf("  -%c %-13s%s\n",
			index_options[i].opt,
			(index_options[i].have_param==NULL) ?
				(char *)"":
				index_options[i].have_param,
			index_options[i].what_does);
		if (index_options[i].opt=='g') {
		    print_scanner_help();
		}		
	}
	printf("\t--\t\tno more options\n");
}

int parse_index_options(IndexOptions *o, char **argv) {
	int i,j;
	SGREPDATA(o);
	
	i=0;
	j=1;

	
	while ( *argv!=NULL && *argv[0]=='-' )
	{
		/* option -- means no more options */
		if (strcmp(*argv,"--")==0) return i 1;

		switch((*argv)[j])
		{
		case 'g': {
		        char *arg;
			arg=get_arg(sgrep,&argv,&i,&j);
			if ((!arg) || 
			    set_scanner_option(o->sgrep,arg)==SGREP_ERROR) {
			    return SGREP_ERROR;
			}
			break;
		}
		case 'h':
			print_index_help();
			o->index_mode=IM_DONE;
			break;
		case 'i':
			o->sgrep->ignore_case=1;
			break;
		case 'l': {
			char *endptr;
		        char *arg=get_arg(sgrep,&argv,&i,&j);
			if (!arg) return SGREP_ERROR;
			o->stop_word_limit=strtol(arg,&endptr,10);
			if (o->stop_word_limit<0 || *endptr!=0) {
			    sgrep_error(sgrep,"Invalid stop word limit '%s'\n",
				    arg);
			    return SGREP_ERROR;
			}
			break;
		}
		case 'm': {
			char *endptr;
		        char *arg=get_arg(sgrep,&argv,&i,&j);
			if (!arg) return SGREP_ERROR;
			o->available_memory=strtol(arg,&endptr,10)*1024*1024;
			if (o->available_memory<0 || *endptr!=0) {
			    sgrep_error(sgrep,"Invalid memory size '%s'\n",
				    arg);
			    return SGREP_ERROR;
			}
			break;
		}		    
		case 'L':
		        o->output_stop_word_file=get_arg(sgrep,&argv,&i,&j);
			if (!o->output_stop_word_file) return SGREP_ERROR;
			break;
		case 'S':
			o->input_stop_word_file=get_arg(sgrep,&argv,&i,&j);
			if (!o->input_stop_word_file) return SGREP_ERROR;
			break;
		case 'V':
			printf("sgindex version %s compiled at %s\n",
				VERSION,__DATE__);
			o->index_mode=IM_DONE;
			break;
		case 'v':
		        o->sgrep->progress_output=1;
			break;
		case 'T':
			o->index_stats=1;
			break;
#if 0
		case 'C':
			copyright_notice();
			o->index_mode=IM_DONE;
			break;
#endif
		case 'R':
			o->sgrep->recurse_dirs=1;
			sgrep_error(sgrep,"WARNING -R not working (yet)\n");
			break;
		case 'c':
		    o->file_name=get_arg(sgrep,&argv,&i,&j);
		    if (o->file_name==NULL) return SGREP_ERROR;
		    o->index_mode=IM_CREATE;
		    break;
		case 'x':
		    o->sgrep->index_file=get_arg(sgrep,&argv,&i,&j);
		    if (o->sgrep->index_file==NULL) return SGREP_ERROR;
		    break;
		case 'q': {
		    const char *arg=get_arg(sgrep,&argv,&i,&j);
		    if (strcmp(arg,"terms")==0) {
			o->index_mode=IM_TERMS;
		    } else {
			sgrep_error(sgrep,"Don't know how to query '%s'\n",
				    arg);
			return SGREP_ERROR;
		    }	
		    break;
		}		    
		case 'F': {
		    char *arg;
		    arg=get_arg(sgrep,&argv,&i,&j);
		    if (arg==NULL) return SGREP_ERROR;
		    if (o->file_list_files==NULL) {
			o->file_list_files=new_flist(sgrep);
		    }
		    flist_add(o->file_list_files,arg);
		    break;
		}
		case 'w':
			o->sgrep->word_chars=get_arg(sgrep,&argv,&i,&j);
			if (!o->sgrep->word_chars) return SGREP_ERROR;
			break;
		default:
			sgrep_error(sgrep,"Illegal option -%c\n",(*argv)[j]);
			return SGREP_ERROR;
			break;
		}
		if ((*argv)[  j]==0)
		{
			argv  ;
			i  ;
			j=1;
		}
	}
	return i;
}

int index_main(SgrepData *sgrep,int argc, char *argv[]) {
    int end_options;
    IndexOptions options;
    FileList *file_list=NULL;

    set_default_index_options(sgrep,&options);
    /* 
     * Get the command line options 
     */
    end_options=parse_index_options(&options,argv);

    if (end_options==SGREP_ERROR) {
	/* There was error. Display usage information */
	index_usage(sgrep);
	goto error;
    }

    switch(options.index_mode) {
    case IM_CREATE: {
	if (argc==end_options && options.file_list_files==NULL) {
	    sgrep_error(sgrep,"Can't read input from stdin when indexing.\n");
	    sgrep_error(sgrep," Use filename '-' to force indexing from stdin.\n");
	    goto error;
	}
	if (argc>end_options) {
	    file_list=check_files(sgrep,argc-end_options,argv end_options,
				  0,NULL);
	}
	options.file_list=file_list;
	if (create_index(&options)==SGREP_ERROR) {
	    goto error;
	}
	break;
    }
    case IM_TERMS: {
	if (index_query(&options,argc-end_options,argv end_options)
	    ==SGREP_ERROR) {
	    return 2;
	} else {
	    return 0;
	}
    }
    case IM_DONE:
	return 0;	
    case IM_NONE:	
    default:
	sgrep_error(sgrep,"sgindex: You have to give one of -c, -C -h\n");
	index_usage(sgrep);
	goto error;
    }
    if (file_list) delete_flist(file_list);
    if (options.file_list_files) delete_flist(options.file_list_files);
    return 0;

 error:
    if (file_list) delete_flist(file_list);
    if (options.file_list_files) delete_flist(options.file_list_files);
    return 2;
}