diff --git a/Makefile b/Makefile index a261d79..443a56c 100644 --- a/Makefile +++ b/Makefile @@ -2,23 +2,23 @@ OUT=libscomp.a VERSION=0.1.0 TESTS=test/input test/exec -all: $(OUT) libscomm_amalg.c libscomm_amalg.h +all: $(OUT) libscomp_amalg.c libscomp_amalg.h $(OUT): input.o exec.o $(AR) rcs $(OUT) input.o exec.o tests: $(TESTS) for i in $(TESTS); do ./$$i -v; done -libscomm_amalg.h: exec.h input.h - echo '#pragma once' > libscomm_amalg.h - echo '#define LIBSCOMM_VERSION "${VERSION}"' >> libscomm_amalg.h +libscomp_amalg.h: exec.h input.h + echo '#pragma once' > libscomp_amalg.h + echo '#define LIBSCOMP_VERSION "${VERSION}"' >> libscomp_amalg.h cat input.h exec.h | \ sed 's/#pragma .*//g; s/#include ".*//g' \ - >> libscomm_amalg.h + >> libscomp_amalg.h -libscomm_amalg.c: exec.c input.c - echo '#include "libscomm_amalg.h"' > libscomm_amalg.c +libscomp_amalg.c: exec.c input.c + echo '#include "libscomp_amalg.h"' > libscomp_amalg.c cat exec.c input.c | sed 's/#include ".*//g' \ - >> libscomm_amalg.c + >> libscomp_amalg.c TEST_EXEC=test/exec.o exec.o test/exec: $(TEST_EXEC) diff --git a/exec.c b/exec.c index 133063f..91fcfc6 100644 --- a/exec.c +++ b/exec.c @@ -1,6 +1,6 @@ #include "exec.h" -#ifdef LIBSCOMM_FREESTANDING +#ifdef LIBSCOMP_FREESTANDING int strcmp(const char *s1, const char *s2) { while (*s1 == *s2) { if (!*s1) @@ -15,8 +15,8 @@ int strcmp(const char *s1, const char *s2) { # include #endif -struct libscomm_cmd *hg_bsearch(const char *name, - const struct libscomm_cmd_store *cmds) { +struct libscomp_cmd *hg_bsearch(const char *name, + const struct libscomp_cmd_store *cmds) { size_t lower = 0, upper = cmds->len-1; size_t i; int rel; @@ -36,12 +36,12 @@ struct libscomm_cmd *hg_bsearch(const char *name, return NULL; } -int libscomm_exec(const struct libscomm_cmd_store *cmds, - struct libscomm_line *ln, void *ptr) { - struct libscomm_cmd *cmd; +int libscomp_exec(const struct libscomp_cmd_store *cmds, + struct libscomp_line *ln, void *ptr) { + struct libscomp_cmd *cmd; cmd = hg_bsearch(ln->buf[ln->name], cmds); if (!cmd) - return LIBSCOMM_NOT_FOUND; + return LIBSCOMP_NOT_FOUND; return cmd->exec(ln,ptr); } diff --git a/exec.h b/exec.h index 30a0c66..1b071b9 100644 --- a/exec.h +++ b/exec.h @@ -1,27 +1,27 @@ #pragma once #include "input.h" -enum libscomm_cmd_r { - LIBSCOMM_NOT_FOUND = -1, - LIBSCOMM_CMD_OK = 0 +enum libscomp_cmd_r { + LIBSCOMP_NOT_FOUND = -1, + LIBSCOMP_CMD_OK = 0 }; -struct libscomm_cmd { +struct libscomp_cmd { const char *name; - int (*exec)(struct libscomm_line *, void *); + int (*exec)(struct libscomp_line *, void *); }; -struct libscomm_cmd_store { - struct libscomm_cmd *arr; +struct libscomp_cmd_store { + struct libscomp_cmd *arr; size_t len; }; #if defined(__STDC_VERSION__) -#define libscomm_mkcmds(...) {(struct libscomm_cmd []){__VA_ARGS__}, \ - sizeof((struct libscomm_cmd []){__VA_ARGS__}) / \ - sizeof((struct libscomm_cmd []){__VA_ARGS__}[0])} +#define libscomp_mkcmds(...) {(struct libscomp_cmd []){__VA_ARGS__}, \ + sizeof((struct libscomp_cmd []){__VA_ARGS__}) / \ + sizeof((struct libscomp_cmd []){__VA_ARGS__}[0])} #endif -int libscomm_exec(const struct libscomm_cmd_store *cmds, - struct libscomm_line *ln, +int libscomp_exec(const struct libscomp_cmd_store *cmds, + struct libscomp_line *ln, void *ptr ); diff --git a/input.c b/input.c index 6b9dd0b..ee6cc70 100644 --- a/input.c +++ b/input.c @@ -1,26 +1,26 @@ #include "input.h" -enum { LIBSCOMM_READ, LIBSCOMM_DISCARD }; +enum { LIBSCOMP_READ, LIBSCOMP_DISCARD }; -void libscomm_reset(struct libscomm_input *in) { - in->state = LIBSCOMM_READ; +void libscomp_reset(struct libscomp_input *in) { + in->state = LIBSCOMP_READ; in->len = 0; } -static enum libscomm_input_r libscomm_parse( - struct libscomm_input *in, struct libscomm_line *line) { +static enum libscomp_input_r libscomp_parse( + struct libscomp_input *in, struct libscomp_line *line) { char *s = in->intbuf; - enum libscomm_input_r r = LIBSCOMM_ARG_OVERFLOW; + enum libscomp_input_r r = LIBSCOMP_ARG_OVERFLOW; line->name = (*s == ':'); line->len = 0; - while (line->len < LIBSCOMM_MAXARG) { + while (line->len < LIBSCOMP_MAXARG) { line->buf[line->len++] = s; for (; *s && *s != '\t'; s++); if (!*s) { - r = LIBSCOMM_COMPLETE; + r = LIBSCOMP_COMPLETE; break; } else { *s = 0; @@ -28,35 +28,35 @@ static enum libscomm_input_r libscomm_parse( } } - libscomm_reset(in); + libscomp_reset(in); return r; } -enum libscomm_input_r libscomm_read(struct libscomm_input *in, +enum libscomp_input_r libscomp_read(struct libscomp_input *in, char **s, - struct libscomm_line *line) { + struct libscomp_line *line) { char c; while ((c = **s)) { (*s)++; - if (in->state == LIBSCOMM_DISCARD) { + if (in->state == LIBSCOMP_DISCARD) { if (c == '\n') { - libscomm_reset(in); - return LIBSCOMM_OVERFLOW; + libscomp_reset(in); + return LIBSCOMP_OVERFLOW; } } else { switch (c) { case '\n': in->intbuf[in->len] = 0; - return libscomm_parse(in, line); + return libscomp_parse(in, line); default: in->intbuf[in->len++] = c; - if (in->len == LIBSCOMM_MAXBUF) - in->state = LIBSCOMM_DISCARD; + if (in->len == LIBSCOMP_MAXBUF) + in->state = LIBSCOMP_DISCARD; } } } - return LIBSCOMM_MORE; + return LIBSCOMP_MORE; } diff --git a/input.h b/input.h index ff51e14..28c33aa 100644 --- a/input.h +++ b/input.h @@ -1,30 +1,30 @@ #pragma once #include -#define LIBSCOMM_MAXARG 32 -#define LIBSCOMM_MAXBUF 1024 +#define LIBSCOMP_MAXARG 32 +#define LIBSCOMP_MAXBUF 1024 -struct libscomm_line { +struct libscomp_line { size_t name; - char *buf[LIBSCOMM_MAXARG]; + char *buf[LIBSCOMP_MAXARG]; size_t len; }; -struct libscomm_input { +struct libscomp_input { int state; - char intbuf[LIBSCOMM_MAXBUF]; + char intbuf[LIBSCOMP_MAXBUF]; size_t len; }; -enum libscomm_input_r { - LIBSCOMM_MORE, - LIBSCOMM_OVERFLOW, - LIBSCOMM_ARG_OVERFLOW, - LIBSCOMM_COMPLETE +enum libscomp_input_r { + LIBSCOMP_MORE, + LIBSCOMP_OVERFLOW, + LIBSCOMP_ARG_OVERFLOW, + LIBSCOMP_COMPLETE }; -void libscomm_reset(struct libscomm_input *); -enum libscomm_input_r libscomm_read(struct libscomm_input *, +void libscomp_reset(struct libscomp_input *); +enum libscomp_input_r libscomp_read(struct libscomp_input *, char **, - struct libscomm_line *); + struct libscomp_line *); diff --git a/test/exec.c b/test/exec.c index ab5526f..0f7a9a7 100644 --- a/test/exec.c +++ b/test/exec.c @@ -2,12 +2,12 @@ #include "../exec.h" #define mkfun(f, n) \ - int f(struct libscomm_line *x __attribute__((unused)), \ + int f(struct libscomp_line *x __attribute__((unused)), \ void *ptr __attribute__((unused))) { \ return n; \ } -mkfun(foo, LIBSCOMM_CMD_OK) +mkfun(foo, LIBSCOMP_CMD_OK) mkfun(bar, 1) mkfun(ocmd, 2) mkfun(baz, 3) @@ -20,32 +20,32 @@ mkfun(yan, 9) mkfun(val, 10) mkfun(cal, 11) -struct libscomm_cmd cmdarr[] = { +struct libscomp_cmd cmdarr[] = { {"apple", apple}, {"bar", bar}, {"baz", baz}, {"cal", cal}, {"foo", foo}, {"ocmd", ocmd}, {"quox", quox}, {"rex", rex}, {"test1", test1}, {"under", under}, {"val", val}, {"yan", yan} }; #define arrlen(x) (sizeof(x) / sizeof((x)[0])) -const struct libscomm_cmd_store cmds = {cmdarr, arrlen(cmdarr)}; +const struct libscomp_cmd_store cmds = {cmdarr, arrlen(cmdarr)}; #define doexec(f, n) do{\ x.buf[0] = f; \ - ASSERT_EQ(libscomm_exec(&cmds, &x, NULL), n); \ + ASSERT_EQ(libscomp_exec(&cmds, &x, NULL), n); \ } while(0) TEST execute_one(void) { - struct libscomm_line x = {0}; + struct libscomp_line x = {0}; x.len = 1; - doexec("foo", LIBSCOMM_CMD_OK); + doexec("foo", LIBSCOMP_CMD_OK); PASS(); } TEST execute_multiple(void) { - struct libscomm_line x = {0}; + struct libscomp_line x = {0}; x.len = 1; - doexec("foo", LIBSCOMM_CMD_OK); + doexec("foo", LIBSCOMP_CMD_OK); doexec("bar", 1); doexec("ocmd", 2); doexec("baz", 3); @@ -62,12 +62,12 @@ TEST execute_multiple(void) { } TEST not_in(void) { - struct libscomm_line x = {0}; + struct libscomp_line x = {0}; x.len = 1; - doexec("test2", LIBSCOMM_NOT_FOUND); - doexec("flub", LIBSCOMM_NOT_FOUND); - doexec("doexec", LIBSCOMM_NOT_FOUND); + doexec("test2", LIBSCOMP_NOT_FOUND); + doexec("flub", LIBSCOMP_NOT_FOUND); + doexec("doexec", LIBSCOMP_NOT_FOUND); PASS(); } diff --git a/test/input.c b/test/input.c index 7852ddf..2583421 100644 --- a/test/input.c +++ b/test/input.c @@ -2,13 +2,13 @@ #include "../input.h" TEST one_pass(void) { - struct libscomm_input in; + struct libscomp_input in; char s[] = "Arg 1\tArg \r\v\t!\n"; char *p = s; - struct libscomm_line l; + struct libscomp_line l; - libscomm_reset(&in); - ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE); + libscomp_reset(&in); + ASSERT_EQ(libscomp_read(&in, &p, &l), LIBSCOMP_COMPLETE); ASSERT_EQ(l.len, 3); ASSERT_EQ(l.name, 0); ASSERT_EQ(*p, 0); @@ -22,33 +22,33 @@ TEST one_pass(void) { } TEST line_limit(void) { - struct libscomm_input in; - struct libscomm_line l; - char s[LIBSCOMM_MAXBUF + 1]; + struct libscomp_input in; + struct libscomp_line l; + char s[LIBSCOMP_MAXBUF + 1]; char *p = s; int i; - for (i = 0; i < LIBSCOMM_MAXBUF - 1; i++) { + for (i = 0; i < LIBSCOMP_MAXBUF - 1; i++) { s[i] = (i + 1) % 64 ? 'a' : '\t'; } - s[LIBSCOMM_MAXBUF - 1] = '\n'; - s[LIBSCOMM_MAXBUF] = 0; + s[LIBSCOMP_MAXBUF - 1] = '\n'; + s[LIBSCOMP_MAXBUF] = 0; #if 0 if (GREATEST_IS_VERBOSE()) fprintf(stderr, "%s", s); #endif - libscomm_reset(&in); - ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE); - ASSERT_EQ(p, &s[LIBSCOMM_MAXBUF]); + libscomp_reset(&in); + ASSERT_EQ(libscomp_read(&in, &p, &l), LIBSCOMP_COMPLETE); + ASSERT_EQ(p, &s[LIBSCOMP_MAXBUF]); PASS(); } TEST single_arg_too_large(void) { - struct libscomm_input in; - struct libscomm_line l; - char s[LIBSCOMM_MAXBUF + 12]; + struct libscomp_input in; + struct libscomp_line l; + char s[LIBSCOMP_MAXBUF + 12]; char *p; size_t i; int ostate; @@ -59,31 +59,31 @@ TEST single_arg_too_large(void) { s[sizeof s - 1] = 0; p = s; - libscomm_reset(&in); - ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_OVERFLOW); + libscomp_reset(&in); + ASSERT_EQ(libscomp_read(&in, &p, &l), LIBSCOMP_OVERFLOW); ASSERT_EQ(p, &s[sizeof s - 1]); ASSERT_EQ(in.len, 0); /* state values are opaque. */ ostate = in.state; - libscomm_reset(&in); + libscomp_reset(&in); ASSERT_EQ(ostate, in.state); PASS(); } TEST arg_limit(void) { - struct libscomm_input in; - struct libscomm_line l; + struct libscomp_input in; + struct libscomp_line l; char s[] = " 1\t 2\t 3\t 4\t 5\t 6\t 7\t 8\t 9\t 10\t 11\t 12\t 13\t 14\t 15\t 16\t 17\t 18\t 19\t 20\t 21\t 22\t 23\t 24\t 25\t 26\t 27\t 28\t 29\t 30\t 31\t 32\n"; char *p = s; char tbuf[10]; int i; - libscomm_reset(&in); - ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE); + libscomp_reset(&in); + ASSERT_EQ(libscomp_read(&in, &p, &l), LIBSCOMP_COMPLETE); ASSERT_EQ(l.name, 0); - ASSERT_EQ(l.len, LIBSCOMM_MAXARG); + ASSERT_EQ(l.len, LIBSCOMP_MAXARG); ASSERT_EQ(p, &s[sizeof s - 1]); for (i = 1; i <= l.len; i++) { @@ -95,32 +95,32 @@ TEST arg_limit(void) { } TEST too_many_arg(void) { - struct libscomm_input in; - struct libscomm_line l; + struct libscomp_input in; + struct libscomp_line l; char s[] = " 1\t 2\t 3\t 4\t 5\t 6\t 7\t 8\t 9\t 10\t 11\t 12\t 13\t 14\t 15\t 16\t 17\t 18\t 19\t 20\t 21\t 22\t 23\t 24\t 25\t 26\t 27\t 28\t 29\t 30\t 31\t 32\t 33\n"; char *p = s; - libscomm_reset(&in); - ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_ARG_OVERFLOW); + libscomp_reset(&in); + ASSERT_EQ(libscomp_read(&in, &p, &l), LIBSCOMP_ARG_OVERFLOW); ASSERT_EQ(p, &s[sizeof s - 1]); PASS(); } TEST two_in_one(void) { - struct libscomm_input in; - struct libscomm_line l; + struct libscomp_input in; + struct libscomp_line l; char s[] = "Arg 1\tArg 2\tArg 3\nArg 4\tArg 5\tArg 6\tArg 7\n"; char *p = s; - libscomm_reset(&in); - ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE); + libscomp_reset(&in); + ASSERT_EQ(libscomp_read(&in, &p, &l), LIBSCOMP_COMPLETE); ASSERT_EQ(l.name, 0); ASSERT_EQ(l.len, 3); ASSERT_STR_EQ(l.buf[0], "Arg 1"); ASSERT_STR_EQ(l.buf[1], "Arg 2"); ASSERT_STR_EQ(l.buf[2], "Arg 3"); - ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE); + ASSERT_EQ(libscomp_read(&in, &p, &l), LIBSCOMP_COMPLETE); ASSERT_EQ(l.name, 0); ASSERT_EQ(l.len, 4); ASSERT_STR_EQ(l.buf[0], "Arg 4"); @@ -132,13 +132,13 @@ TEST two_in_one(void) { } TEST label(void) { - struct libscomm_input in; - struct libscomm_line l; + struct libscomp_input in; + struct libscomp_line l; char s[] = ":N a m e\tArg 1\tArg 2\tArg 3\n"; char *p = s; - libscomm_reset(&in); - ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE); + libscomp_reset(&in); + ASSERT_EQ(libscomp_read(&in, &p, &l), LIBSCOMP_COMPLETE); ASSERT_EQ(l.name, 1); ASSERT_EQ(l.len, 4); ASSERT_STR_EQ(l.buf[0], ":N a m e"); @@ -150,23 +150,23 @@ TEST label(void) { } TEST split(void) { - struct libscomm_input in; - struct libscomm_line l; + struct libscomp_input in; + struct libscomp_line l; char s1[] = ":123456\tArg 1\tArg 2\tAr"; char s2[] = "g 3\tArg 4\tArg 5\tArg 6"; char s3[] = "\n"; char *p = s1; - libscomm_reset(&in); - ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_MORE); + libscomp_reset(&in); + ASSERT_EQ(libscomp_read(&in, &p, &l), LIBSCOMP_MORE); ASSERT_EQ(p, &s1[sizeof s1 - 1]); p = s2; - ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_MORE); + ASSERT_EQ(libscomp_read(&in, &p, &l), LIBSCOMP_MORE); ASSERT_EQ(p, &s2[sizeof s2 - 1]); p = s3; - ASSERT_EQ(libscomm_read(&in, &p, &l), LIBSCOMM_COMPLETE); + ASSERT_EQ(libscomp_read(&in, &p, &l), LIBSCOMP_COMPLETE); ASSERT_EQ(p, &s3[sizeof s3 - 1]); ASSERT_EQ(l.name, 1);