changeset 1117:d13f51a2d239

update to libjeffpc's new nvlist API Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Sun, 01 Apr 2018 20:42:57 -0400
parents 3f9303b0c87f
children 1dc65e2b39e7
files comment.c pipeline.c post_nv.c req.c sidebar.c story.c template.y vars.c vars.h
diffstat 9 files changed, 63 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/comment.c	Thu Apr 12 10:28:37 2018 -0400
+++ b/comment.c	Sun Apr 01 20:42:57 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2009-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -81,9 +81,9 @@
 #define COMMENT_EMPTY		"x"
 
 static const struct nvl_convert_info comment_convert[] = {
-	{ .name = COMMENT_DATE,		.tgt_type = NVT_INT, },
-	{ .name = COMMENT_ID,		.tgt_type = NVT_INT, },
-	{ .name = COMMENT_CAPTCHA,	.tgt_type = NVT_INT, },
+	{ .name = COMMENT_DATE,		.tgt_type = VT_INT, },
+	{ .name = COMMENT_ID,		.tgt_type = VT_INT, },
+	{ .name = COMMENT_CAPTCHA,	.tgt_type = VT_INT, },
 	{ .name = NULL, },
 };
 
@@ -416,13 +416,13 @@
 	int id;
 
 	if (nvl_exists_type(req->scgi->request.headers, SCGI_HTTP_USER_AGENT,
-			    NVT_STR)) {
+			    VT_STR)) {
 		DBG("Missing user agent...");
 		return USERAGENT_MISSING;
 	}
 
 	if (nvl_exists_type(req->scgi->request.headers, SCGI_REMOTE_ADDR,
-			    NVT_STR)) {
+			    VT_STR)) {
 		DBG("Missing remote addr...");
 		return INTERNAL_ERR;
 	}
--- a/pipeline.c	Thu Apr 12 10:28:37 2018 -0400
+++ b/pipeline.c	Sun Apr 01 20:42:57 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2013-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -143,6 +143,8 @@
 		case VT_CONS:
 		case VT_BOOL:
 		case VT_CHAR:
+		case VT_ARRAY:
+		case VT_NVL:
 			panic("%s called with value type %d", __func__,
 			      val->type);
 	}
--- a/post_nv.c	Thu Apr 12 10:28:37 2018 -0400
+++ b/post_nv.c	Sun Apr 01 20:42:57 2018 -0400
@@ -31,38 +31,27 @@
 static int __tag_val(struct nvlist *post, avl_tree_t *list)
 {
 	struct post_tag *cur;
-	struct nvval *tags;
+	struct val **tags;
 	size_t ntags;
 	size_t i;
-	int ret;
 
 	ntags = avl_numnodes(list);
 
-	tags = mem_reallocarray(NULL, ntags, sizeof(struct nvval));
+	tags = mem_reallocarray(NULL, ntags, sizeof(struct val *));
 	if (!tags)
 		return -ENOMEM;
 
 	i = 0;
-	avl_for_each(list, cur) {
-		struct nvval *tag = &tags[i++];
-
-		tag->type = NVT_STR;
-		tag->str = str_getref(cur->tag);
-	}
+	avl_for_each(list, cur)
+		tags[i++] = str_getref_val(cur->tag);
 
-	ret = nvl_set_array(post, "tags", tags, ntags);
-	if (ret) {
-		nvval_release_array(tags, ntags);
-		free(tags);
-	}
-
-	return ret;
+	return nvl_set_array(post, "tags", tags, ntags);
 }
 
 static int __com_val(struct nvlist *post, struct list *list)
 {
 	struct comment *cur;
-	struct nvval *comments;
+	struct val **comments;
 	size_t ncomments;
 	int ret;
 	int i;
@@ -76,13 +65,12 @@
 	if (!ncomments)
 		return 0;
 
-	comments = mem_reallocarray(NULL, ncomments, sizeof(struct nvval));
+	comments = mem_reallocarray(NULL, ncomments, sizeof(struct val *));
 	if (!comments)
 		return -ENOMEM;
 
 	i = 0;
 	list_for_each(cur, list) {
-		struct nvval *comment = &comments[i++];
 		struct nvlist *c;
 
 		c = nvl_alloc();
@@ -91,8 +79,7 @@
 			goto err;
 		}
 
-		comment->type = NVT_NVL;
-		comment->nvl = c;
+		comments[i++] = nvl_cast_to_val(c);
 
 		if ((ret = nvl_set_int(c, "commid", cur->id)))
 			goto err;
@@ -110,14 +97,12 @@
 			goto err;
 	}
 
-	ret = nvl_set_array(post, "comments", comments, ncomments);
-	if (ret)
-		goto err;
-
-	return 0;
+	return nvl_set_array(post, "comments", comments, ncomments);
 
 err:
-	nvval_release_array(comments, i);
+	while (i-- > 0)
+		val_putref(comments[i]);
+
 	free(comments);
 
 	return ret;
@@ -208,14 +193,14 @@
 void load_posts(struct req *req, struct post **posts, int nposts,
 		bool moreposts)
 {
-	struct nvval *nvposts;
+	struct val **nvposts;
 	size_t nnvposts;
 	time_t maxtime;
 	size_t i;
 
 	maxtime = 0;
 
-	nvposts = mem_reallocarray(NULL, nposts, sizeof(struct nvval));
+	nvposts = mem_reallocarray(NULL, nposts, sizeof(struct val *));
 	ASSERT(nvposts);
 
 	nnvposts = 0;
@@ -225,9 +210,8 @@
 
 		post_lock(post, true);
 
-		nvposts[nnvposts].type = NVT_NVL;
-		nvposts[nnvposts].nvl = __store_vars(req, post, NULL);
-		if (IS_ERR(nvposts[nnvposts].nvl)) {
+		nvposts[nnvposts] = nvl_cast_to_val(__store_vars(req, post, NULL));
+		if (IS_ERR(nvposts[nnvposts])) {
 			post_unlock(post);
 			post_putref(post);
 			continue;
--- a/req.c	Thu Apr 12 10:28:37 2018 -0400
+++ b/req.c	Sun Apr 01 20:42:57 2018 -0400
@@ -197,7 +197,7 @@
 	nvl_set_nvl(logentry, "options", tmp);
 
 	/* serialize */
-	buf = nvl_pack(logentry, NVF_JSON);
+	buf = nvl_pack(logentry, VF_JSON);
 	if (IS_ERR(buf))
 		goto err_free;
 
@@ -241,10 +241,10 @@
 }
 
 static const struct nvl_convert_info info[] = {
-	{ .name = "p",       .tgt_type = NVT_INT, },
-	{ .name = "paged",   .tgt_type = NVT_INT, },
-	{ .name = "m",       .tgt_type = NVT_INT, },
-	{ .name = "preview", .tgt_type = NVT_INT, },
+	{ .name = "p",       .tgt_type = VT_INT, },
+	{ .name = "paged",   .tgt_type = VT_INT, },
+	{ .name = "m",       .tgt_type = VT_INT, },
+	{ .name = "preview", .tgt_type = VT_INT, },
 	{ .name = NULL, },
 };
 
--- a/sidebar.c	Thu Apr 12 10:28:37 2018 -0400
+++ b/sidebar.c	Sun Apr 01 20:42:57 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2013-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -36,7 +36,7 @@
 
 struct tagcloud_state {
 	unsigned long ntags;
-	struct nvval *cloud;
+	struct val **cloud;
 };
 
 static int __tag_size(int count, int cmin, int cmax)
@@ -57,7 +57,7 @@
 {
 	struct tagcloud_state *state = arg;
 
-	state->cloud = mem_reallocarray(NULL, ntags, sizeof(struct nvval));
+	state->cloud = mem_reallocarray(NULL, ntags, sizeof(struct val *));
 	state->ntags = 0;
 
 	return state->cloud ? 0 : -ENOMEM;
@@ -87,8 +87,7 @@
 	if ((ret = nvl_set_int(tmp, "size", __tag_size(count, cmin, cmax))))
 		goto err;
 
-	state->cloud[state->ntags].type = NVT_NVL;
-	state->cloud[state->ntags].nvl = tmp;
+	state->cloud[state->ntags] = nvl_cast_to_val(tmp);
 	state->ntags++;
 
 	return;
--- a/story.c	Thu Apr 12 10:28:37 2018 -0400
+++ b/story.c	Sun Apr 01 20:42:57 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2009-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -37,7 +37,7 @@
 static int __load_post(struct req *req, int p, bool preview)
 {
 	struct nvlist *post;
-	struct nvval *val;
+	struct val **val;
 
 	post = get_post(req, p, "title", preview);
 	if (!post) {
@@ -49,12 +49,16 @@
 		return -ENOENT;
 	}
 
-	val = malloc(sizeof(struct nvval));
-	if (!val)
+	val = malloc(sizeof(struct val *));
+	if (!val) {
+		DBG("failed to allocate val array");
+
+		nvl_putref(post);
+
 		return -ENOMEM;
+	}
 
-	val->type = NVT_NVL;
-	val->nvl = post;
+	*val = nvl_cast_to_val(post);
 
 	vars_set_array(&req->vars, "posts", val, 1);
 
--- a/template.y	Thu Apr 12 10:28:37 2018 -0400
+++ b/template.y	Sun Apr 01 20:42:57 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2012-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -77,7 +77,7 @@
 
 static char *__foreach(struct req *req, const struct nvpair *var, char *tmpl)
 {
-	const struct nvval *items;
+	struct val **items;
 	size_t nitems;
 	size_t i;
 	char *out;
@@ -91,13 +91,14 @@
 	for (i = 0; i < nitems; i++) {
 		vars_scope_push(&req->vars);
 
-		switch (items[i].type) {
-			case NVT_NVL:
-				vars_merge(&req->vars, items[i].nvl);
+		switch (items[i]->type) {
+			case VT_NVL:
+				vars_merge(&req->vars,
+					   val_cast_to_nvl(items[i]));
 				break;
-			case NVT_STR:
+			case VT_STR:
 				vars_set_str(&req->vars, nvpair_name(var),
-					     str_getref(items[i].str));
+					     val_getref_str(items[i]));
 				break;
 			default:
 				//vars_dump(&req->vars);
@@ -150,6 +151,8 @@
 		case VT_CONS:
 		case VT_BOOL:
 		case VT_CHAR:
+		case VT_ARRAY:
+		case VT_NVL:
 			panic("%s called with value of type %d", __func__,
 			      val->type);
 	}
@@ -164,12 +167,12 @@
 	char *ret;
 
 	switch (nvpair_type(var)) {
-		case NVT_STR:
+		case VT_STR:
 			str = nvpair_value_str(var);
 			ret = xstrdup(str_cstr(str));
 			str_putref(str);
 			break;
-		case NVT_INT:
+		case VT_INT:
 			snprintf(buf, sizeof(buf), "%"PRIu64, pair2int(var));
 			ret = xstrdup(buf);
 			break;
@@ -202,10 +205,10 @@
 	}
 
 	switch (nvpair_type(var)) {
-		case NVT_STR:
+		case VT_STR:
 			val = str_cast_to_val(nvpair_value_str(var));
 			break;
-		case NVT_INT:
+		case VT_INT:
 			val = VAL_ALLOC_INT(pair2int(var));
 			break;
 		default:
@@ -261,7 +264,7 @@
 		return 0;
 
 	switch (nvpair_type(var)) {
-		case NVT_INT:
+		case VT_INT:
 			return pair2int(var);
 		default:
 			panic("unexpected nvpair type: %d",
--- a/vars.c	Thu Apr 12 10:28:37 2018 -0400
+++ b/vars.c	Sun Apr 01 20:42:57 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2013-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -95,7 +95,7 @@
 
 WRAP_SET1(vars_set_str, nvl_set_str, struct str *);
 WRAP_SET1(vars_set_int, nvl_set_int, uint64_t);
-WRAP_SET2(vars_set_array, nvl_set_array, struct nvval *);
+WRAP_SET2(vars_set_array, nvl_set_array, struct val **);
 
 const struct nvpair *vars_lookup(struct vars *vars, const char *name)
 {
--- a/vars.h	Thu Apr 12 10:28:37 2018 -0400
+++ b/vars.h	Sun Apr 01 20:42:57 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2013-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -38,7 +38,7 @@
 extern void vars_set_str(struct vars *vars, const char *name, struct str *val);
 extern void vars_set_int(struct vars *vars, const char *name, uint64_t val);
 extern void vars_set_array(struct vars *vars, const char *name,
-		struct nvval *val, size_t nval);
+		struct val **vals, size_t nval);
 extern const struct nvpair *vars_lookup(struct vars *vars, const char *name);
 extern struct str *vars_lookup_str(struct vars *vars, const char *name);
 extern uint64_t vars_lookup_int(struct vars *vars, const char *name);