aboutsummaryrefslogtreecommitdiff
path: root/contrib/global/btreeop/btreeop.c
blob: 18160fb8c714e8c70206e3806712866ba52420bb (plain) (blame)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * Copyright (c) 1996, 1997 Shigio Yamaguchi. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Shigio Yamaguchi.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	btreeop.c				6-Nov-97
 *
 */
#include <sys/stat.h>

#include <ctype.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>

#include "dbio.h"
#include "global.h"

char    *dbdefault = "btree";   	/* default database name */
char	*progname  = "btreeop";		/* command name */

static void	usage __P((void));
void	signal_setup __P((void));
void	onintr __P((int));
void	main __P((int, char **));
void	dbwrite __P((DBIO *));
void	dbkey __P((DBIO *, char *, int));
void	dbscan __P((DBIO *, char *, int));
void	dbdel __P((DBIO *, char *, int));
void	dbbysecondkey __P((DBIO *, int, char *, int));

#define F_KEY	0
#define F_DEL	1

static void
usage()
{
	fprintf(stderr, "%s\n",
		"usage: btreeop [-A][-C][-D[n] key][-K[n] key][-L][-k prefix][dbname]");
	exit(1);
}

/*
 * Btreeop catch signal even if the parent ignore it.
 */
int	exitflag = 0;

void
onintr(signo)
int	signo;
{
	exitflag = 1;
}

void
signal_setup()
{
	signal(SIGHUP, onintr);
	signal(SIGINT, onintr);
	signal(SIGQUIT, onintr);
	signal(SIGTERM, onintr);
}

void
main(argc, argv)
int	argc;
char	*argv[];
{
	char	command = 'R';
	char	*key = NULL;
	int     mode = 0;
	char	*dbname;
	DBIO	*dbio;
	int	i, c;
	int	secondkey = 0;
	char	*prefix = (char *)0;

	for (i = 1; i < argc && argv[i][0] == '-'; ++i) {
		switch (c = argv[i][1]) {
		case 'D':
		case 'K':
			if (argv[i][2] && isdigit(argv[i][2]))
				secondkey = atoi(&argv[i][2]);
			key = argv[++i];
		case 'A':
		case 'C':
		case 'L':
			if (command != 'R')
				usage();
			command = c;
			break;
		case 'k':
			prefix = argv[++i];
			break;
		default:
			usage();
		}
	}
	dbname = (i < argc) ? argv[i] : dbdefault;
	switch (command) {
	case 'A':
	case 'D':
		mode = 2;
		break;
	case 'C':
		mode = 1;
		break;
	case 'K':
	case 'L':
	case 'R':
		mode = 0;
		break;
	}
	dbio = db_open(dbname, mode, 0644, DBIO_DUP);
	if (dbio == NULL)
		die1("db_open failed (dbname = %s).", dbname);

	switch (command) {
	case 'A':			/* Append records */
	case 'C':			/* Create database */
		dbwrite(dbio);
		break;
	case 'D':			/* Delete records */
		dbdel(dbio, key, secondkey);
		break;
	case 'K':			/* Keyed (indexed) read */
		dbkey(dbio, key, secondkey);
		break;
	case 'R':			/* sequencial Read */
	case 'L':			/* primary key List */
		dbscan(dbio, prefix, (command == 'L') ? 1 : 0);
		break;
	}
	db_close(dbio);
	if (exitflag)
		exit(1);
	exit(0);
}
/*
 * dbwrite: write to database
 *
 *	i)	dbio		database
 */
void
dbwrite(dbio)
DBIO	*dbio;
{
	char	*p;
	char	keybuf[MAXKEYLEN+1];
	char	*c;

	signal_setup();
	/*
	 * Input file format:
	 * +--------------------------------------------------
	 * |Primary-key	secondary-key-1 secondary-key-2 Data\n
	 * |Primary-key	secondary-key-1 secondary-key-2 Data\n
	 * 	.
	 * 	.
	 * - Keys and Data are separated by blank('\t' or ' '). 
	 * - Keys cannot include blank.
	 * - Data can include blank.
	 * - Null record not allowed.
	 * - Secondary-key is assumed as a part of data by db(3).
	 *
	 * META record:
	 * You can write meta record by making key start with a ' '.
	 * You can read this record only by indexed read ('-K' option).
	 * +------------------
	 * | __.VERSION 2
	 */
	while ((p = mgets(stdin, 0, NULL)) != NULL) {
		if (exitflag)
			break;
		c = p;
		if (*c == ' ') {			/* META record */
			if (*++c == ' ')
				die("key cannot include blanks.");
		}
		for (; *c && !isspace(*c); c++)		/* skip key part */
			;
		if (*c == 0)
			die("data part not found.");
		if (c - p > MAXKEYLEN)
			die("primary key too long.");
		strncpy(keybuf, p, c - p);		/* make key string */
		keybuf[c - p] = 0;
		for (; *c && isspace(*c); c++)		/* skip blanks */
			;
		if (*c == 0)
			die("data part is null.");
		entab(p);
		db_put(dbio, keybuf, p);
	}
}

/*
 * dbkey: Keyed search
 *
 *	i)	dbio		database
 *	i)	skey		key for search
 *	i)	secondkey	0: primary key, >0: secondary key
 */
void
dbkey(dbio, skey, secondkey)
DBIO	*dbio;
char	*skey;
int	secondkey;
{
	char	*p;

	if (!secondkey) {
		for (p = db_first(dbio, skey, 0); p; p = db_next(dbio))
			detab(stdout, p);
		return;
	}
	dbbysecondkey(dbio, F_KEY, skey, secondkey);
}

/*
 * dbscan: Scan records
 *
 *	i)	dbio		database
 *	i)	prefix		prefix of primary key
 *	i)	keylist		0: key and data, 1: primary key only
 */
void
dbscan(dbio, prefix, keylist)
DBIO	*dbio;
char	*prefix;
int	keylist;
{
	char	*p;
	int	flags = DBIO_SKIPMETA; 

	if (prefix)	
		flags |= DBIO_PREFIX;
	if (keylist)
		flags |= DBIO_KEY;

	for (p = db_first(dbio, prefix, flags); p; p = db_next(dbio))
		detab(stdout, p);
}

/*
 * dbdel: Delete records
 *
 *	i)	dbio		database
 *	i)	skey		key for search
 *	i)	secondkey	0: primary key, >0: secondary key
 */
void
dbdel(dbio, skey, secondkey)
DBIO	*dbio;
char	*skey;
int	secondkey;
{
	signal_setup();
	if (!secondkey) {
		db_del(dbio, skey);
		return;
	}
	dbbysecondkey(dbio, F_DEL, skey, secondkey);
}
/*
 * dbbysecondkey: proc by second key
 *
 *	i)	dbio	database
 *	i)	func	F_KEY, F_DEL
 *	i)	skey
 *	i)	secondkey
 */
void
dbbysecondkey(dbio, func, skey, secondkey)
DBIO	*dbio;
int	func;
char	*skey;
int	secondkey;
{
	char	*c, *p;
	int	i;

	/* trim skey */
	for (c = skey; *c && isspace(*c); c++)
		;
	skey = c;
	for (c = skey+strlen(skey)-1; *c && isspace(*c); c--)
		*c = 0;

	for (p = db_first(dbio, NULL, DBIO_SKIPMETA); p; p = db_next(dbio)) {
		if (exitflag)
			break;
		c = p;
		/* reach to specified key */
		for (i = secondkey; i; i--) {
			for (; *c && !isspace(*c); c++)
				;
			if (*c == 0)
				die("specified key not found.");
			for (; *c && isspace(*c); c++)
				;
			if (*c == 0)
				die("specified key not found.");
		}
		i = strlen(skey);
		if (!strncmp(c, skey, i) && (*(c+i) == 0 || isspace(*(c+i)))) {
			switch (func) {
			case F_KEY:
				detab(stdout, p);
				break;
			case F_DEL:
				db_del(dbio, NULL);
				break;
			}
		}
		if (exitflag)
			break;
	}
}