File: dbquery.c

package info (click to toggle)
mcelog 104-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 996 kB
  • ctags: 1,508
  • sloc: ansic: 7,739; sh: 481; makefile: 87
file content (130 lines) | stat: -rw-r--r-- 2,538 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
/* Access db files. This is for testing and debugging only. */
#define _GNU_SOURCE 1
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>
#include "db.h"

#define C(x) if (x) printf(#x " failed: %s\n", strerror(errno))
#define NEEDGROUP  if (!group) { printf("need group first\n"); break; }

void Eprintf(char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
}

void usage(void)
{
	printf(
	"s sync\n"
	"q close/quit\n"
	"ggroup find group\n"
	"G delete group\n"
	"agroup add group\n" 
	"ventry dump entry\n"
	"centry,val change entry to val\n"
	"fentry,val find entry with value and dump its group\n"
	"Ccomment add comment\n"
	"Lnewname clone group to newname\n"
	"d dump group\n"
	"D dump database\n");
}	

int main(int ac, char **av)
{
	struct database *db;
	struct group *group = NULL;
	char *line = NULL;
	size_t linesz = 0;
	if (!av[1]) {
		printf("%s database\n", av[0]);
		exit(1);
	}
	printf("dbtest\n");
	db = open_db(av[1], 1);  
	while (printf("> "), 
		fflush(stdout), 
		getline(&line, &linesz, stdin) > 0) { 
		char *p = line + strlen(line) - 1;
		while (p >= line && isspace(*p))
			*p-- = 0; 
		switch (line[0]) { 
		case 's':
			C(sync_db(db));
			break;
		case 'q':
			C(close_db(db)); 
			exit(0);
		case 'g':
			group = find_group(db, line + 1); 
			if (group)
				printf("found\n"); 
			break;
		case 'G':
			NEEDGROUP; 
			C(delete_group(db, group));
			group = NULL;
			break;			
		case 'a': { 
			int existed = 0;
			group = add_group(db, line + 1, &existed);
			if (existed) 
				printf("existed\n");
			break;
		}
		case 'v':
			NEEDGROUP; 
			printf("%s\n", entry_val(group, line + 1)); 
			break;
		case 'c': {
			p = line + 1;
			char *entry = strsep(&p, ","); 
			NEEDGROUP; 
			change_entry(db, group, entry, strsep(&p, ""));
			break;
		} 
		case 'L':
			NEEDGROUP; 
			clone_group(db, group, line + 1); 
			break;
		case 'f': { 
			struct group *g;
			p = line + 1; 
			char *entry = strsep(&p, ",");
			char *val = strsep(&p, "");	
			g = NULL;
			int nr = 0;
			while ((g = find_entry(db, g, entry, val)) != NULL) {
				if (nr == 0)
					group = g;
				nr++;
				dump_group(group, stdout);
			}
			if (nr == 0) 
				printf("not found\n");
			break;
		}
		case 'C':
			NEEDGROUP;
			add_comment(db, group, line + 1); 
			break;
		case 'd':
			NEEDGROUP;
			dump_group(group, stdout); 
			break;
		case 'D':
			dump_database(db, stdout);
			break;
		default:
			usage();
			break;
		}
	}
	return 0;
}