aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c59
1 files changed, 35 insertions, 24 deletions
diff --git a/src/main.c b/src/main.c
index c92d24b..42b4173 100644
--- a/src/main.c
+++ b/src/main.c
@@ -32,6 +32,7 @@ static void parse_args(int argc,
static void __sqlite3_check(int rc, sqlite3 *db, const char *file, int line, const char *func)
{
+ /* Note: "rc" stands for "return code" */
if (rc == SQLITE_OK)
return;
@@ -127,6 +128,12 @@ static void sanitize_sql_str(char **str)
*str = new_str;
}
+/* TODO: add a routine for safely creating formatted string
+
+ Using stack-allocated space would be nice. Sketch:
+ static void strbuild(char *buf, int size, char *fmt, ...)
+ Advice: read the example at the end of the vsnprintf(3) manual page. */
+
/* Compute something that identifies the content of FILE. Algorithm by Dan
Bernstein from http://www.cse.yorku.ca/~oz/hash.html via
https://stackoverflow.com/a/7666577/20138083. */
@@ -155,7 +162,7 @@ static uint32_t sum(const char *file)
fclose(stream);
return sum;
}
-
+/* Return an integer timestamp from a date string of the format YYYY-MM-DD. */
static time_t time_from_str(const char *str)
{
int rc;
@@ -319,7 +326,8 @@ static void ftag_init(int, char **)
exit(EXIT_FAILURE);
}
-/* Sqlite callback that prints the first column without header. */
+/* Sqlite callback that prints the first column without header. Used for example
+ * by ftag_list_table. */
static int ftag_print(void *, int, char **cols, char **)
{
assert(cols[0]);
@@ -327,6 +335,8 @@ static int ftag_print(void *, int, char **cols, char **)
return 0;
}
+/* For every row of TABLE, print the value for column COL. Typically used by
+ * ftag_file_list for listing file names. */
static void ftag_list_table(const char *table, const char *col)
{
char sql[64];
@@ -358,7 +368,8 @@ static void canonicalize(char *out, const char *in)
}
/* Add a new file to the databse, prompting the user for needed information. */
-static void ftag_add_one_file(sqlite3 *db, int *next_id, const char *file, uint32_t file_sum)
+static void
+ftag_add_one_file(sqlite3 *db, int *next_id, const char *file, uint32_t file_sum)
{
char sql[2048];
int rc;
@@ -368,6 +379,10 @@ static void ftag_add_one_file(sqlite3 *db, int *next_id, const char *file, uint3
time_t date = time(NULL);
size_t line_len = 0;
ssize_t read_len;
+ /* TODO: possibly be non-interactive
+
+ Maybe take canonical_name, full_name, and description as
+ parameters. */
printf("ftag file add: adding file \"%s\" to database\n", file);
@@ -516,8 +531,7 @@ static int get_sums_callback(void *_known_sums, int, char **cols, char **)
return 0;
}
-/* Add new files to the database. If directories are given, every file in the
- * directory will be added, does _not_ recurse into subdirectories. */
+/* Add new files to the database. Non-file arguments are ignored. */
static void ftag_file_add(int argc, char **argv)
{
/* step 0: parse options */
@@ -594,7 +608,6 @@ static void ftag_file_add(int argc, char **argv)
for (int j = 0; j < file_count; j++) {
const uint32_t duplicate = known_sums_array[j];
if (duplicate == original) {
- /* sum already known in database */
printf("%s debug: file \"%s\" found in database\n",
__func__, argv[i]);
argv[i] = NULL;
@@ -660,10 +673,8 @@ static int get_id_by_col_callback(void *_id, int, char **cols, char **)
}
/* Return the id of the entry of TABLE that has the value VAL for column COL. */
-static int get_id_by_col(sqlite3 *db,
- const char *table,
- const char *col,
- const char *val)
+static int
+get_id_by_col(sqlite3 *db, const char *table, const char *col, const char *val)
{
int id = -1;
char sql[128];
@@ -679,8 +690,7 @@ static int get_id_by_col(sqlite3 *db,
static void ftag_file_tag(int argc, char **argv)
{
if (argc < 2) {
- fprintf(stderr,
- "ftag file tag: must supply a file name and tags\n");
+ fprintf(stderr, "Usage: ftag file tag FILE TAG...\n");
exit(EXIT_FAILURE);
}
const char *file = argv[0];
@@ -723,7 +733,7 @@ static void ftag_help(int, char **)
puts(" init initialize the database");
puts(" file manage files");
puts(" help print this message");
- puts(" query query ftag database");
+ puts(" query query the database");
puts(" tag manage tags");
puts("Some commands also have their own help, try \"ftag COMMAND help\"");
puts("Configuration:");
@@ -755,8 +765,9 @@ static int print_all_callback(void *_print_header, int count, char **cols, char
return 0;
}
-/* End of recursive calls to ftag_query_ */
-static void ftag_query_date(sqlite3 *db, char *sql, int max_len,
+/* End of recursive calls to ftag_query_join. */
+static void ftag_query_date(sqlite3 *db,
+ char *sql, int max_len,
time_t before_date, time_t after_date)
{
char buf[128];
@@ -785,7 +796,8 @@ static void ftag_query_date(sqlite3 *db, char *sql, int max_len,
}
/* Build the "join" part of the SQL query for the "ftag query" command. */
-static void ftag_query_join(sqlite3 *db, char *sql, int max_len,
+static void ftag_query_join(sqlite3 *db,
+ char *sql, int max_len,
char **tags, int tag_count,
time_t before_date, time_t after_date)
{
@@ -868,18 +880,16 @@ 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]);
+ "ftag tag add: error: tag \"%s\" already exists, "
+ "its description is:\n%s\n",
+ cols[0], cols[1]);
return 1;
}
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");
+ fprintf(stderr, "Usage: ftag tag add NAME DESCRIPTION\n");
exit(EXIT_FAILURE);
}
char *new_tag_name = argv[0];
@@ -887,7 +897,7 @@ static void ftag_tag_add(int argc, char **argv)
sanitize_sql_str(&new_tag_name);
char sql[1024];
- sqlite3 *db = NULL;
+ sqlite3 *db;
int rc;
rc = sqlite3_open(DATABASE_PATH, &db);
@@ -895,7 +905,8 @@ static void ftag_tag_add(int argc, char **argv)
memset(sql, 0, sizeof(sql));
snprintf(sql, sizeof(sql)-1,
- "SELECT name, description FROM tags WHERE name = '%s';", new_tag_name);
+ "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);