File: main.c

package info (click to toggle)
libwebsockets 4.3.5-1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 30,588 kB
  • sloc: ansic: 194,407; javascript: 1,550; sh: 1,387; cpp: 505; java: 461; perl: 405; xml: 118; makefile: 76; awk: 5
file content (230 lines) | stat: -rw-r--r-- 4,670 bytes parent folder | download | duplicates (3)
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
/*
 * lws-api-test-fts - lws full-text search api test
 *
 * Written in 2010-2019 by Andy Green <andy@warmcat.com>
 *
 * This file is made available under the Creative Commons CC0 1.0
 * Universal Public Domain Dedication.
 */

#include <libwebsockets.h>
#if defined(LWS_HAS_GETOPT_LONG) || defined(WIN32)
#include <getopt.h>
#endif
#include <fcntl.h>

#if defined(LWS_HAS_GETOPT_LONG) || defined(WIN32)
static struct option options[] = {
	{ "help",	no_argument,		NULL, 'h' },
	{ "createindex", no_argument,		NULL, 'c' },
	{ "index",	required_argument,	NULL, 'i' },
	{ "debug",	required_argument,	NULL, 'd' },
	{ "file",	required_argument,	NULL, 'f' },
	{ "lines",	required_argument,	NULL, 'l' },
	{ NULL, 0, 0, 0 }
};
#endif

static const char *index_filepath = "/tmp/lws-fts-test-index";
static char filepath[256];

int main(int argc, char **argv)
{
	int n, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE;
	int fd, fi, ft, createindex = 0, flags = LWSFTS_F_QUERY_AUTOCOMPLETE;
	struct lws_fts_search_params params;
	struct lws_fts_result *result;
	struct lws_fts_file *jtf;
	struct lws_fts *t;
	char buf[16384];

	do {
#if defined(LWS_HAS_GETOPT_LONG) || defined(WIN32)
		n = getopt_long(argc, argv, "hd:i:cfl", options, NULL);
#else
       n = getopt(argc, argv, "hd:i:cfl");
#endif
		if (n < 0)
			continue;
		switch (n) {
		case 'i':
			strncpy(filepath, optarg, sizeof(filepath) - 1);
			filepath[sizeof(filepath) - 1] = '\0';
			index_filepath = filepath;
			break;
		case 'd':
			logs = atoi(optarg);
			break;
		case 'c':
			createindex = 1;
			break;
		case 'f':
			flags &= ~LWSFTS_F_QUERY_AUTOCOMPLETE;
			flags |= LWSFTS_F_QUERY_FILES;
			break;
		case 'l':
			flags |= LWSFTS_F_QUERY_FILES |
				 LWSFTS_F_QUERY_FILE_LINES;
			break;
		case 'h':
			fprintf(stderr,
				"Usage: %s [--createindex]"
					"[--index=<index filepath>] "
					"[-d <log bitfield>] file1 file2 \n",
					argv[0]);
			exit(1);
		}
	} while (n >= 0);

	lws_set_log_level(logs, NULL);
	lwsl_user("LWS API selftest: full-text search\n");

	if (createindex) {

		lwsl_notice("Creating index\n");

		/*
		 * create an index by shifting through argv and indexing each
		 * file given there into a single combined index
		 */

		ft = open(index_filepath, O_CREAT | O_WRONLY | O_TRUNC, 0600);
		if (ft < 0) {
			lwsl_err("%s: can't open index %s\n", __func__,
				 index_filepath);

			goto bail;
		}

		t = lws_fts_create(ft);
		if (!t) {
			lwsl_err("%s: Unable to allocate trie\n", __func__);

			goto bail1;
		}

		while (optind < argc) {

			fi = lws_fts_file_index(t, argv[optind],
						(int)strlen(argv[optind]), 1);
			if (fi < 0) {
				lwsl_err("%s: Failed to get file idx for %s\n",
					 __func__, argv[optind]);

				goto bail1;
			}

			fd = open(argv[optind], O_RDONLY);
			if (fd < 0) {
				lwsl_err("unable to open %s for read\n",
						argv[optind]);
				goto bail;
			}

			do {
				int n = (int)read(fd, buf, sizeof(buf));

				if (n <= 0)
					break;

				if (lws_fts_fill(t, (uint32_t)fi, buf, (size_t)n)) {
					lwsl_err("%s: lws_fts_fill failed\n",
						 __func__);
					close(fd);

					goto bail;
				}

			} while (1);

			close(fd);
			optind++;
		}

		if (lws_fts_serialize(t)) {
			lwsl_err("%s: serialize failed\n", __func__);

			goto bail;
		}

		lws_fts_destroy(&t);
		close(ft);

		return 0;
	}

	/*
	 * shift through argv searching for each token
	 */

	jtf = lws_fts_open(index_filepath);
	if (!jtf)
		goto bail;

	while (optind < argc) {

		struct lws_fts_result_autocomplete *ac;
		struct lws_fts_result_filepath *fp;
		uint32_t *l, n;

		memset(&params, 0, sizeof(params));

		params.needle = argv[optind];
		params.flags = flags;
		params.max_autocomplete = 20;
		params.max_files = 20;

		result = lws_fts_search(jtf, &params);

		if (!result) {
			lwsl_err("%s: search failed\n", __func__);
			lws_fts_close(jtf);
			goto bail;
		}

		ac = result->autocomplete_head;
		fp = result->filepath_head;

		if (!ac)
			lwsl_notice("%s: no autocomplete results\n", __func__);

		while (ac) {
			lwsl_notice("%s: AC %s: %d agg hits\n", __func__,
				((char *)(ac + 1)), ac->instances);

			ac = ac->next;
		}

		if (!fp)
			lwsl_notice("%s: no filepath results\n", __func__);

		while (fp) {
			lwsl_notice("%s: %s: (%d lines) %d hits \n", __func__,
				(((char *)(fp + 1)) + fp->matches_length),
				fp->lines_in_file, fp->matches);

			if (fp->matches_length) {
				l = (uint32_t *)(fp + 1);
				n = 0;
				while ((int)n++ < fp->matches)
					lwsl_notice(" %d\n", *l++);
			}
			fp = fp->next;
		}

		lwsac_free(&params.results_head);

		optind++;
	}

	lws_fts_close(jtf);

	return 0;

bail1:
	close(ft);
bail:
	lwsl_user("FAILED\n");

	return 1;
}