aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: ab10055dc4c92c9a5f978859b7ff514b1fc18a6a (plain)
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
#include <assert.h>
#include <errno.h>
#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#define DATABASE_PATH (FTAG_ROOT "/ftag.sqlite3")

struct ftag_command {
	const char *name;
	void (*func)(int argc, char **argv);
};

static void ftag_init(int, char **);
static void ftag_help(int, char **);
static void ftag_tag(int,  char **);

/* Parse arguments using an array of available commands. ARGV[0] has to match
 * the "name" field of an entry of COMMANDS. */
static void parse_args(int argc,
		       char ** argv,
		       const struct ftag_command *commands,
		       int command_count);

static const struct ftag_command toplevel_commands[] = {
	{.name = "init", .func = ftag_init},
	{.name = "help", .func = ftag_help},
	{.name = "tag",  .func = ftag_tag}
};
static const int toplevel_command_count = sizeof(toplevel_commands) / sizeof(struct ftag_command);

static void __sqlite3_check(int rc, sqlite3 *db, const char *file, int line, const char *func)
{
	if (rc == SQLITE_OK)
		return;

	fprintf(stderr, "%s:%d: %s: %s\n", file, line, func, sqlite3_errmsg(db));
	sqlite3_close(db);
	exit(EXIT_FAILURE);
}

#define sqlite3_check(RC, DB) __sqlite3_check(RC, DB, __FILE__, __LINE__, __func__)

static void assert_db_exists(void)
{
	struct stat statbuf __attribute__((unused));
	int rc = stat(DATABASE_PATH, &statbuf);

	if (rc == 0)
		return;

	if (errno == ENOENT) {
		fprintf(stderr,
			"ftag: database not found at \"%s\", "
			"have you run \"ftag init\"?\n",
			DATABASE_PATH);
	}
	else {
		perror(DATABASE_PATH);
	}
	exit(EXIT_FAILURE);
}

static void ftag_init(int, char **)
{
	char cmd[1024];
	memset(cmd, 0, sizeof(cmd));
	snprintf(cmd, sizeof(cmd)-1,
		 "sqlite3 %s < %s", DATABASE_PATH, FTAG_ROOT "/sql/init.sql");
	execl("/usr/bin/sh", "/usr/bin/sh", "-c", cmd, NULL);
	perror("exec");
	exit(EXIT_FAILURE);
}

static void ftag_help(int, char **)
{
	printf("Usage: ftag COMMAND [COMMAND-ARG]...\n");
	printf("Available values for COMMAND:\n");
	printf("  init    initialize the database\n");
	printf("  file    manage files");
	printf("  help    print this message\n");
	printf("  tag     manage tags\n");
	printf("Configuration:\n");
	printf("  database path    %s\n", DATABASE_PATH);
	printf("  ftag root        %s\n", FTAG_ROOT);
}

/* Check that the tag we are trying to create does not exist yet. If this
 * callback is ever called, then it already exist, this is a fatal error. */
static int ftag_tag_check(void *, int, char **cols, char **)
{
	assert(cols[0]);
	assert(cols[1]);
	fprintf(stderr,
		"ftag tag add: error: tag \"%s\" already exists, its description is:\n",
		cols[0]);
	fprintf(stderr, "%s\n", cols[1]);
	return 1;
}

static int ftag_tag_last_id(void *_id, int, char **cols, char **)
{
	int *id = _id;
	assert(cols);
	assert(cols[0]);
	*id = atoi(cols[0]);
	return 0;
}

static void ftag_tag_add(int argc, char **argv)
{
	if (argc != 2) {
		fprintf(stderr,
			"ftag tag add: error: must supply two arguments, "
			"the tag name and its description\n");
		exit(EXIT_FAILURE);
	}
	const char *new_tag_name = argv[0];
	assert(strlen(new_tag_name) <= 255);
	assert(strchr(new_tag_name, '\'') == NULL);

	char sql[1024];
	sqlite3 *db = NULL;
	int rc;

	rc = sqlite3_open(DATABASE_PATH, &db);
	sqlite3_check(rc, db);

	memset(sql, 0, sizeof(sql));
	snprintf(sql, sizeof(sql)-1,
		 "SELECT name, description FROM tags WHERE name = '%s';", new_tag_name);
	rc = sqlite3_exec(db, sql, ftag_tag_check, NULL, NULL);
	sqlite3_check(rc, db);

	int last_id = -1;
	memset(sql, 0, sizeof(sql));
	snprintf(sql, sizeof(sql)-1, "SELECT MAX(id) FROM tags;");
	rc = sqlite3_exec(db, sql, ftag_tag_last_id, &last_id, NULL);
	sqlite3_check(rc, db);

	const char *new_tag_desc = argv[1];
	assert(strlen(new_tag_desc) <= 600);
	assert(strchr(new_tag_desc, '\'') == NULL);
	last_id++;
	memset(sql, 0, sizeof(sql));
	snprintf(sql, sizeof(sql)-1,
		 "INSERT INTO tags VALUES(%d, '%s', '%s');",
		 last_id, new_tag_name, new_tag_desc);
	rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
	sqlite3_check(rc, db);
}

static int ftag_tag_print(void *, int, char **cols, char **)
{
	assert(cols[0]);
	printf("%s\n", cols[0]);
	return 0;
}

static void ftag_tag_list(int argc, char **argv)
{
	const char *sql = "SELECT name FROM tags;";
	sqlite3 *db = NULL;
	int rc;

	rc = sqlite3_open(DATABASE_PATH, &db);
	sqlite3_check(rc, db);

	rc = sqlite3_exec(db, sql, ftag_tag_print, NULL, NULL);
	sqlite3_check(rc, db);
}

static void ftag_tag(int argc, char **argv)
{
	assert_db_exists();
	const struct ftag_command tag_commands[] = {
		{.name = "add",  .func = ftag_tag_add},
		{.name = "list", .func = ftag_tag_list}
	};
	int tag_command_count = sizeof(tag_commands) / sizeof(struct ftag_command);
	parse_args(argc, argv, tag_commands, tag_command_count);
}

static void parse_args(int argc,
		       char **argv,
		       const struct ftag_command *commands,
		       int command_count)
{
	assert(argc > 0);
	assert(command_count > 0);
	for (int i = 0; i < command_count; i++) {
		if (strcmp(argv[0], commands[i].name) == 0) {
			commands[i].func(argc-1, argv+1);
			return;
		}
	}
}

int main(int argc, char *argv[])
{
	if (argc == 1) {
		ftag_help(0, NULL);
		exit(EXIT_FAILURE);
	}

	parse_args(argc-1, argv+1, toplevel_commands, toplevel_command_count);
	return EXIT_SUCCESS;
}