aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client/cmd.cpp5
-rw-r--r--client/parse.cpp111
-rw-r--r--client/parse.h12
-rw-r--r--client/readme.md7
4 files changed, 116 insertions, 19 deletions
diff --git a/client/cmd.cpp b/client/cmd.cpp
index 4a2c8a3..1ec2cb8 100644
--- a/client/cmd.cpp
+++ b/client/cmd.cpp
@@ -48,9 +48,10 @@ void cmd_send(char* addr_str) {
char* data;
size_t data_size;
- if (strtodata(data_str, &data, &data_size)) {
+ int err = strtodata(data_str, &data, &data_size);
+ if (err <= 0) {
printf("data format error at index %d:\n%s\n%*s^\n",
- (int) data_size, data_str, (int) data_size, "");
+ -err, data_str, -err, "");
return;
}
diff --git a/client/parse.cpp b/client/parse.cpp
index d15207c..223dc5d 100644
--- a/client/parse.cpp
+++ b/client/parse.cpp
@@ -1,31 +1,116 @@
+#include <math.h>
#include <stdlib.h>
#include <string.h>
+#include <stdio.h>
#include "parse.h"
-static unsigned ifsrun(const char* input, const char* ift) {
- unsigned i;
- for (i = 0; input[i] != '\0' && strchr(ift, input[i]); i++);
- return i;
+static int parse_str(const char* str, char* data, size_t* size) {
+ char closing = str[0];
+ char escape = false;
+ bool scan = data == NULL;
+ int i = 0;
+ size_t len = strlen(str);
+
+ switch (str[i]) {
+ case '\'':
+ escape = false;
+ break;
+ case '\"':
+ escape = true;
+ break;
+ default:
+ return -i;
+ }
+
+ for (i = 1; i < len && str[i] != '\0'; i++) {
+ char c = str[i];
+
+ if (c == closing) {
+ if (scan) printf("string%s of length %d\n", escape ? " (w/ escape)" : "", i - 1);
+ return i + 1; // +1 for closing quote
+ }
+
+ if (scan) *size += 1;
+ }
+
+ return -i;
+}
+
+static int parse_num(const char* str, char* data, size_t* size) {
+ const char* ifs = IFS;
+ size_t len = strcspn(str, ifs);
+ bool scan = data == NULL;
+ int i = 0;
+ int base = 10;
+ bool bytestring = false;
+
+ const char* colon = strchr(str, ':');
+ if (colon != NULL && colon < str + len) { // byte string
+ base = 16;
+ bytestring = true;
+ } else if (len > 2 && strncmp(str, "0x", 2) == 0) { // hexadecimal prefix
+ base = 16;
+ i += 2;
+ }/* else if (len > 1 && strncmp(str, "0", 1) == 0) { // octal prefix
+ base = 8;
+ i += 1;
+ }*/
+
+ const char* set;
+ // if (base == 8) set = SET_OCT;
+ if (base == 10) set = SET_DEC;
+ if (base == 16) {
+ if (bytestring) set = SET_HEX_STR;
+ else set = SET_HEX;
+ }
+
+ size_t len_ok = strspn(str + i, set) + i;
+ if (len != len_ok) return -len_ok;
+
+ if (scan) {
+ if (base == 10) *size += 1;
+ else if (base == 16) {
+ if (!bytestring) {
+ *size += (len - i + 1) / 2;
+ } else {
+ for (; colon != NULL && colon < str + len; colon = strchr(str, ':')) {
+ *size += 1;
+ }
+ }
+ }
+ }
+
+ if (scan) printf("number (base %d%s) of length %lu\n", base, bytestring ? " as bytestring" : "", len - i);
+ return len;
}
int strtodata(const char* str, char** data, size_t* size) {
const char* ifs = IFS;
*size = 0;
size_t i;
- size_t str_len = strlen(str);
+ size_t len = strlen(str);
- // TODO: finish this parser
- // for (i = 0; i < str_len; i++) {
- // unsigned ifs_run = ifsrun(&str[i], ifs);
- //
- // }
+ for (i = 0; i < len;) {
+ // skip whitespace
+ int run;
+ run = strspn(&str[i], ifs);
+ if (run > 0) printf("skipping whitespace for %d bytes...\n", run);
+ i += run;
+ // end of string
+ if (str[i] == '\0') break;
- *size = str_len;
+ if ((run = parse_str(str + i, NULL, size)) > 0) { i += run; continue; }
+ if ((run = parse_num(str + i, NULL, size)) > 0) { i += run; continue; }
- *data = (char*) malloc(*size);
+ // no format detected
+ return -i + run;
+ }
+ printf("end of string w/o parse errors\n");
+ printf("buffer size is now %lu\n", *size);
+ exit(0);
- memcpy(*data, str, *size);
+ *data = (char*) malloc(*size);
return 0;
}
diff --git a/client/parse.h b/client/parse.h
index ac06446..10274e7 100644
--- a/client/parse.h
+++ b/client/parse.h
@@ -4,6 +4,11 @@
#define IFS " \t\n"
+#define SET_OCT "01234567"
+#define SET_DEC "0123456789"
+#define SET_HEX SET_DEC"abcdefABCDEF"
+#define SET_HEX_STR SET_HEX":"
+
/**
* \brief modify \p token to point to the first token when broken up on \p ifs
* and return the remaining data
@@ -26,13 +31,12 @@ char* consume_token(char* token, const char* ifs);
* \param data pointer to \c char* that will store the resulting data
* \param size size of \p data
*
- * \return 0 if the string was parsed succesfully, or 1 if the string could not
- * be parsed succesfully
+ * \return 0 or a negative integer representing the index where there is a
+ * syntax error if there was an error, or a positive integer representing the
+ * amount of bytes parsed from \p str
*
* \note The pointer that \p data refers to will not be initialized by this
* function if parsing fails
- * \note \p size will contain the index of \p str where the first invalid data
- * was found if parsing fails
*/
int strtodata(const char* str, char** data, size_t* size);
diff --git a/client/readme.md b/client/readme.md
index 9d755aa..04471d2 100644
--- a/client/readme.md
+++ b/client/readme.md
@@ -12,3 +12,10 @@ goal (in order of implementation):
ls list connected puzzle modules
```
+
+```
+send 0x39 "Hello world!" de:ad:be:ef 0xff 5 0a 0750
+ ^~~~~~~~~~~~~~ ^~~~~~~~~~~ ~^~~ ~^ ~^ ~~~~^
+ STR_INTP BYTE_ARR UNSIGNED UNSIGNED UNSIGNED UNSIGNED
+ (hex+0x) (dec) (hex) (oct)
+```