• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lejp test app
3  *
4  * Written in 2010-2019 by Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  *
9  * This demonstrates a minimal http server that performs a form GET with a couple
10  * of parameters.  It dumps the parameters to the console log and redirects
11  * to another page.
12  */
13 
14 #include <libwebsockets.h>
15 #include <string.h>
16 
17 
18 static const char * const reason_names[] = {
19 	"LEJPCB_CONSTRUCTED",
20 	"LEJPCB_DESTRUCTED",
21 	"LEJPCB_START",
22 	"LEJPCB_COMPLETE",
23 	"LEJPCB_FAILED",
24 	"LEJPCB_PAIR_NAME",
25 	"LEJPCB_VAL_TRUE",
26 	"LEJPCB_VAL_FALSE",
27 	"LEJPCB_VAL_NULL",
28 	"LEJPCB_VAL_NUM_INT",
29 	"LEJPCB_VAL_NUM_FLOAT",
30 	"LEJPCB_VAL_STR_START",
31 	"LEJPCB_VAL_STR_CHUNK",
32 	"LEJPCB_VAL_STR_END",
33 	"LEJPCB_ARRAY_START",
34 	"LEJPCB_ARRAY_END",
35 	"LEJPCB_OBJECT_START",
36 	"LEJPCB_OBJECT_END",
37 	"LEJPCB_OBJECT_END_PRE",
38 };
39 
40 static const char * const tok[] = {
41 	"dummy___"
42 };
43 
44 static signed char
cb(struct lejp_ctx * ctx,char reason)45 cb(struct lejp_ctx *ctx, char reason)
46 {
47 	char buf[1024], *p = buf, *end = &buf[sizeof(buf)];
48 	int n;
49 
50 	for (n = 0; n < ctx->sp; n++)
51 		*p++ = ' ';
52 	*p = '\0';
53 
54 	if (reason & LEJP_FLAG_CB_IS_VALUE) {
55 		p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "   value '%s' ", ctx->buf);
56 		if (ctx->ipos) {
57 			int n;
58 
59 			p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "(array indexes: ");
60 			for (n = 0; n < ctx->ipos; n++)
61 				p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "%d ", ctx->i[n]);
62 			p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), ") ");
63 		}
64 		lwsl_notice("%s (%s)\r\n", buf,
65 		       reason_names[(unsigned int)
66 			(reason) & (LEJP_FLAG_CB_IS_VALUE - 1)]);
67 
68 		(void)reason_names; /* NO_LOGS... */
69 		return 0;
70 	}
71 
72 	switch (reason) {
73 	case LEJPCB_COMPLETE:
74 		lwsl_notice("%sParsing Completed (LEJPCB_COMPLETE)\n", buf);
75 		break;
76 	case LEJPCB_PAIR_NAME:
77 		lwsl_notice("%spath: '%s' (LEJPCB_PAIR_NAME)\n", buf, ctx->path);
78 		break;
79 	}
80 
81 	lwsl_notice("%s%s: path %s match %d statckp %d\r\n", buf, reason_names[(unsigned int)
82 		(reason) & (LEJP_FLAG_CB_IS_VALUE - 1)], ctx->path,
83 		ctx->path_match, ctx->pst[ctx->pst_sp].ppos);
84 
85 	return 0;
86 }
87 
88 int
main(int argc,char * argv[])89 main(int argc, char *argv[])
90 {
91 	int fd, n = 1, ret = 1, m = 0;
92 	struct lejp_ctx ctx;
93 	char buf[128];
94 
95 	lws_set_log_level(7, NULL);
96 
97 	lwsl_notice("libwebsockets-test-lejp  (C) 2017 - 2018 andy@warmcat.com\n");
98 	lwsl_notice("  usage: cat my.json | libwebsockets-test-lejp\n\n");
99 
100 	lejp_construct(&ctx, cb, NULL, tok, LWS_ARRAY_SIZE(tok));
101 
102 	fd = 0;
103 
104 	while (n > 0) {
105 		n = (int)read(fd, buf, sizeof(buf));
106 		if (n <= 0)
107 			continue;
108 
109 		m = lejp_parse(&ctx, (uint8_t *)buf, n);
110 		if (m < 0 && m != LEJP_CONTINUE) {
111 			lwsl_err("parse failed %d\n", m);
112 			goto bail;
113 		}
114 	}
115 	lwsl_notice("okay (%d)\n", m);
116 	ret = 0;
117 bail:
118 	lejp_destruct(&ctx);
119 
120 	return ret;
121 }
122