summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2022-08-01 11:22:59 -0300
committermrb0nk500 <b0nk@b0nk.xyz>2022-08-01 11:25:02 -0300
commit6de2da7f73135fcff28d33845e3b73f293125f60 (patch)
tree9fda547f42e9f2fb68ad7a083cb671c2cb07d542
parent05c0cd0db19063cd3a4043a5401c00b874ededa7 (diff)
git, keyword: Move `parse_key_value_file()` to `keyword.{c,h}`
-rw-r--r--git.c49
-rw-r--r--git.h2
-rw-r--r--keyword.c51
-rw-r--r--keyword.h2
4 files changed, 53 insertions, 51 deletions
diff --git a/git.c b/git.c
index 2a09841..931ba09 100644
--- a/git.c
+++ b/git.c
@@ -280,55 +280,6 @@ int parse_comment_reply(void *ctx, void *ret, const keyword *key, keyword_val va
return 1;
}
-int parse_key_value_file(void *ret, void *ctx, const keyword **keywords, char *buf, const char *delm, parse_callback *parse_cb) {
- if (buf != NULL) {
- int ret_val = 0;
- for (;;) {
- char *lhs, *rhs;
- /* Find the keyword before the delimiter(s). */
- lhs = strtok_r(skip_whitespace(buf), delm, &buf);
- /* Remove any whitespace ahead of us. */
- lhs = strtok_r(lhs, " \t\v\r\n", &rhs);
-
- /* Did we hit EOF? */
- if (is_empty(lhs) || is_empty(buf)) {
- break;
- } else {
- int error;
- char *tmp = skip_whitespace(buf);
-
- /* Does the right hand side start with a double, or single quote? */
- if (*tmp == '\"' || *tmp == '\'') {
- const char *delm = (*delm == '\"') ? "\"" : "\'";
- /* Get the string in between the start, and end qoute. */
- rhs = get_str_delm_range(tmp, delm, delm, &buf);
- } else {
- /* Get the rest of the line. */
- rhs = strtok_r(tmp, "\n", &buf);
- }
-
- /* Did we fail to parse the keyword? */
- if (error = parse_keywords(keywords, lhs, rhs, ret, ctx)) {
- log(LOG_WARNING, "Failed to parse keyword \"%s\". Error code: %i", lhs, error);
- ret_val = error;
- }
- }
-
- /* Do we have a parse callback? */
- if (parse_cb != NULL) {
- /* Did the callback return an error. */
- if (parse_cb(&ret, ctx, buf) < 0) {
- ret_val = 7;
- break;
- }
- }
- }
- return ret_val;
- } else {
- return 8;
- }
-}
-
static int is_end_of_comment(void **ret, void *ctx, char *buf) {
/* Do we have at least one blank line? */
if (strspn(buf, "\n") >= 1) {
diff --git a/git.h b/git.h
index b430aa6..b8bba77 100644
--- a/git.h
+++ b/git.h
@@ -12,7 +12,6 @@ typedef struct file file;
typedef struct comment comment;
typedef struct git_branch git_branch;
typedef struct git_repo git_repo;
-typedef int (parse_callback)(void **ret, void *ctx, char *buf);
struct file {
char *name; /* Name of file. */
@@ -54,7 +53,6 @@ struct pull_request {
extern void cleanup_git(git_repo **repos);
extern void cleanup_pull_request(pull_request *pr);
extern git_repo **init_git(config *cfg);
-extern int parse_key_value_file(void *ret, void *ctx, const keyword **keywords, char *buf, const char *delm, parse_callback *parse_cb);
extern pull_request *get_pull_request(index_t *idx, const char *root, const char *repo);
extern int add_comment(comment *comment, const char *pr_root);
extern int create_pull_request_dir(pull_request *pr, index_t *idx, const char *root, const char *repo);
diff --git a/keyword.c b/keyword.c
index d7bd550..8127440 100644
--- a/keyword.c
+++ b/keyword.c
@@ -2,8 +2,10 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
+#include <syslog.h>
#include <time.h>
#include "keyword.h"
+#include "macros.h"
#include "misc.h"
void *get_keyword_offset_ptr(const keyword *key, void *ptr) {
@@ -87,3 +89,52 @@ int parse_keywords(const keyword **keys, char *key, char *value, void *ret, void
}
return error;
}
+
+int parse_key_value_file(void *ret, void *ctx, const keyword **keywords, char *buf, const char *delm, parse_callback *parse_cb) {
+ if (buf != NULL) {
+ int ret_val = 0;
+ for (;;) {
+ char *lhs, *rhs;
+ /* Find the keyword before the delimiter(s). */
+ lhs = strtok_r(skip_whitespace(buf), delm, &buf);
+ /* Remove any whitespace ahead of us. */
+ lhs = strtok_r(lhs, " \t\v\r\n", &rhs);
+
+ /* Did we hit EOF? */
+ if (is_empty(lhs) || is_empty(buf)) {
+ break;
+ } else {
+ int error;
+ char *tmp = skip_whitespace(buf);
+
+ /* Does the right hand side start with a double, or single quote? */
+ if (*tmp == '\"' || *tmp == '\'') {
+ const char *delm = (*delm == '\"') ? "\"" : "\'";
+ /* Get the string in between the start, and end qoute. */
+ rhs = get_str_delm_range(tmp, delm, delm, &buf);
+ } else {
+ /* Get the rest of the line. */
+ rhs = strtok_r(tmp, "\n", &buf);
+ }
+
+ /* Did we fail to parse the keyword? */
+ if (error = parse_keywords(keywords, lhs, rhs, ret, ctx)) {
+ log(LOG_WARNING, "Failed to parse keyword \"%s\". Error code: %i", lhs, error);
+ ret_val = error;
+ }
+ }
+
+ /* Do we have a parse callback? */
+ if (parse_cb != NULL) {
+ /* Did the callback return an error. */
+ if (parse_cb(&ret, ctx, buf) < 0) {
+ ret_val = 7;
+ break;
+ }
+ }
+ }
+ return ret_val;
+ } else {
+ return 8;
+ }
+}
diff --git a/keyword.h b/keyword.h
index d4343b0..5fccbc4 100644
--- a/keyword.h
+++ b/keyword.h
@@ -7,6 +7,7 @@ typedef enum keyword_type keyword_type;
typedef struct keyword keyword;
typedef union keyword_val keyword_val;
typedef int (keyword_cb)(void *ctx, void *ret, const keyword *key, keyword_val val);
+typedef int (parse_callback)(void **ret, void *ctx, char *buf);
enum keyword_type {
TYPE_NONE,
@@ -39,4 +40,5 @@ extern keyword_val get_keyword_value(const keyword *key, char *value, int *error
extern int set_keyword(const keyword *key, keyword_val val, void *ret, void *ctx);
extern keyword_val parse_keyword(const keyword *key, char *key_str, char *value, int *error);
extern int parse_keywords(const keyword **keys, char *key, char *value, void *ret, void *ctx);
+extern int parse_key_value_file(void *ret, void *ctx, const keyword **keywords, char *buf, const char *delm, parse_callback *parse_cb);
#endif