summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosias <me@josias.dev>2021-07-29 08:35:50 -0700
committerJosias <me@josias.dev>2021-07-29 08:35:50 -0700
commit8d347a8d9fb5e9083d582682b484ff3426863649 (patch)
tree5583e4408a8218e7e7b3fa500d35997139a435fa
parent1046dce6fe71bcad9ee515f861aa6cfc227fa59a (diff)
Add list command
Now you can list all valid notebooks (with a notebook.yaml file) with the `jw list` command. Also involved some refactoring of the argument checker at the beginning of `main`.
-rw-r--r--config.c2
-rw-r--r--include/notebook.h1
-rw-r--r--main.c78
-rw-r--r--notebook.c31
4 files changed, 75 insertions, 37 deletions
diff --git a/config.c b/config.c
index db5eba7..15608a2 100644
--- a/config.c
+++ b/config.c
@@ -19,7 +19,7 @@ char *config_root_get(const char *subdir)
strcat(path, SEPARATOR);
struct config config = config_get();
strncat(path, config.root, 511);
- if (strncmp(subdir, SEPARATOR, 1) != 0 && subdir != NULL) {
+ if (subdir != NULL && strncmp(subdir, SEPARATOR, 1) != 0) {
strcat(path, SEPARATOR);
strncat(path, subdir, 511);
}
diff --git a/include/notebook.h b/include/notebook.h
index a2683db..a877ab3 100644
--- a/include/notebook.h
+++ b/include/notebook.h
@@ -22,5 +22,6 @@ struct notebook {
int notebook_new(const char *);
struct notebook notebook_load(const char*, int*);
+void notebook_list();
#endif
diff --git a/main.c b/main.c
index a7db6cf..8c35e23 100644
--- a/main.c
+++ b/main.c
@@ -73,6 +73,26 @@ static char *get_text()
return text;
}
+struct notebook open_notebook(const char *notebook_id, int *err) {
+ *err = 0;
+ struct notebook notebook = notebook_load(notebook_id, err);
+
+ bool exists = true;
+ struct stat st = {0};
+ if (stat(notebook.path, &st) == -1)
+ exists = false;
+
+ if (exists) {
+ fprintf(stderr, "Error opening notebook %s. Make sure notebook.yaml is available\n", notebook_id);
+ *err = -1;
+ } else {
+ fprintf(stderr, "Notebook %s does not exist. Create it with `jw new %s`\n", notebook_id, notebook_id);
+ *err = -1;
+ }
+
+ return notebook;
+}
+
int main(int argc, char **argv)
{
if (argc == 1) {
@@ -80,57 +100,35 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}
- const char *notebook_commands[] = { "new", "search", "post" };
- bool is_notebook_command = false;
- for (size_t i = 0; i < 3; ++i) {
- if (strcmp(notebook_commands[i], argv[1]) == 0) {
- is_notebook_command = true;
- }
- }
-
if (strcmp(argv[1], "help") == 0) {
print_help();
return EXIT_SUCCESS;
} else if (strcmp(argv[1], "version") == 0) {
print_version_info();
return EXIT_SUCCESS;
- } else if (argc < 3 && is_notebook_command == true) {
- fprintf(stderr, "Usage: jw %s NOTEBOOK\n", argv[1]);
- return EXIT_FAILURE;
- } else if (is_notebook_command == false) {
- fprintf(stderr, "jw: command \"%s\" not found\n", argv[1]);
- return EXIT_FAILURE;
}
- char *notebook_id = argv[2];
int err = 0;
- struct notebook notebook = notebook_load(notebook_id, &err);
-
- bool exists = true;
- struct stat st = {0};
- if (stat(notebook.path, &st) == -1)
- exists = false;
-
- if (exists && err != 0 && strcmp(argv[1], "new") != 0) {
- fprintf(stderr, "Error opening notebook %s. Make sure notebook.yaml is available\n", notebook_id);
- return EXIT_FAILURE;
- }
-
- if (!exists && strcmp(argv[1], "new") != 0) {
- fprintf(stderr, "Notebook %s does not exist. Create it with `jw new %s`\n", notebook_id, notebook_id);
- return EXIT_FAILURE;
- }
-
if (strcmp(argv[1], "new") == 0) {
- err = notebook_new(notebook.id);
+ err = notebook_new(argv[2]);
if (err != 0) {
if (err == -1)
fprintf(stderr, "Notebook already exists\n");
- fprintf(stderr, "Failed to make notebook: %s\n", notebook_id);
+ fprintf(stderr, "Failed to make notebook: %s\n", argv[2]);
return EXIT_FAILURE;
}
- fprintf(stdout, "Notebook %s created\n", notebook.id);
+ fprintf(stdout, "Notebook %s created\n", argv[2]);
} else if (strcmp(argv[1], "post") == 0) {
+ if (argc < 3) {
+ fprintf(stderr, "Usage: jw post NOTEBOOK\n");
+ return EXIT_FAILURE;
+ }
+
+ struct notebook notebook = open_notebook(argv[2], &err);
+ if (err != 0) {
+ return EXIT_FAILURE;
+ }
+
char *text = get_text();
if (text == NULL) {
fprintf(stderr, "Out of memory! Check /tmp/jw.* to recover post.\n");
@@ -152,6 +150,11 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}
+ struct notebook notebook = open_notebook(argv[2], &err);
+ if (err != 0) {
+ return EXIT_FAILURE;
+ }
+
size_t count = 0;
struct result *results = search(notebook, argv[3], &count);
if (results && count > 0) {
@@ -160,8 +163,11 @@ int main(int argc, char **argv)
}
}
printf("Found %zu results\n", count);
+ } else if (strcmp(argv[1], "list") == 0) {
+ notebook_list();
} else {
- fprintf(stderr, "jw: unknown option: %s\n", argv[1]);
+ fprintf(stderr, "jw: command \"%s\" not found\n", argv[1]);
+ return EXIT_FAILURE;
}
return EXIT_SUCCESS;
diff --git a/notebook.c b/notebook.c
index e099a97..ff17233 100644
--- a/notebook.c
+++ b/notebook.c
@@ -2,6 +2,7 @@
#include <stdio.h>
#include <string.h>
#include <dirent.h>
+#include <glob.h>
#include "main.h"
#include "config.h"
@@ -78,3 +79,33 @@ struct notebook notebook_load(const char *notebook_id, int *err) {
return notebook;
}
+
+// lists all notebooks with proper configuration
+void notebook_list() {
+ char *root = config_root_get(NULL);
+ char p[512];
+ strncpy(p, root, 511);
+ strncat(p, "*", 511);
+ strncat(p, SEPARATOR, 511);
+ strncat(p, "notebook.yaml", 511);
+
+ glob_t glob_result;
+ int err = glob(p, GLOB_TILDE, NULL, &glob_result);
+ if (err != 0) {
+ return;
+ }
+
+ for (size_t i = 0; i < glob_result.gl_pathc; ++i) {
+ size_t ri = 0;
+ size_t pi = 0;
+ while (root[ri] != '\0' && root[ri] == glob_result.gl_pathv[i][pi]) {
+ ri++;
+ pi++;
+ }
+ while (glob_result.gl_pathv[i][pi] != SEPARATOR[0]) {
+ fputc((int)glob_result.gl_pathv[i][pi], stdout);
+ pi++;
+ }
+ fputc('\n', stdout);
+ }
+}