diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-26 15:44:26 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-26 15:44:26 +0200 |
commit | ee5d161f6153e1f9da73e477f63f38dcf52ca10c (patch) | |
tree | 926413950f64ed7b0f45f6b3551c16446ecd08d4 /client | |
parent | c77d271d9cb86bf6893098ec6c431fc87945e6c9 (diff) |
`make format`
Diffstat (limited to 'client')
-rw-r--r-- | client/cmd.cpp | 60 | ||||
-rw-r--r-- | client/cmd.h | 8 | ||||
-rw-r--r-- | client/i2c.cpp | 10 | ||||
-rw-r--r-- | client/i2c.h | 3 | ||||
-rw-r--r-- | client/main.cpp | 11 | ||||
-rw-r--r-- | client/mod.c | 3 | ||||
-rw-r--r-- | client/parse.cpp | 47 | ||||
-rw-r--r-- | client/parse.h | 7 | ||||
-rw-r--r-- | client/rl.cpp | 60 | ||||
-rw-r--r-- | client/rl.h | 1 | ||||
-rw-r--r-- | client/sock.cpp | 23 | ||||
-rw-r--r-- | client/sock.h | 5 | ||||
-rw-r--r-- | client/xxd.c | 9 | ||||
-rw-r--r-- | client/xxd.h | 1 |
14 files changed, 110 insertions, 138 deletions
diff --git a/client/cmd.cpp b/client/cmd.cpp index e365a59..539e42d 100644 --- a/client/cmd.cpp +++ b/client/cmd.cpp @@ -4,42 +4,37 @@ #include <string.h> #include "cmd.h" -#include "pb-types.h" +#include "i2c.h" +#include "parse.h" #include "pb-buf.h" #include "pb-send.h" +#include "pb-types.h" #include "rl.h" -#include "i2c.h" -#include "parse.h" -char* consume_token(char* input, const char* ifs) { +char * consume_token(char * input, const char * ifs) { strtok(input, ifs); return strtok(NULL, "\0"); } -void cmd_exit(char*) { - exit(EXIT_SUCCESS); -} +void cmd_exit(char *) { exit(EXIT_SUCCESS); } -void cmd_test(char*) { - const char* data = "Hello world!"; +void cmd_test(char *) { + const char * data = "Hello world!"; i2c_send(0x39, data, strlen(data)); } -void cmd_help(char*) { +void cmd_help(char *) { printf("List of available commands:\n"); for (size_t i = 0; i < cmds_length; i++) { cmd_t cmd = cmds[i]; printf(" %-*s", 10, cmd.name); - if (cmd.info != NULL) - printf(" %s", cmd.info); + if (cmd.info != NULL) printf(" %s", cmd.info); printf("\n"); } - printf( - "\n" - "See man pbc(1) for more info about specific commands\n" - "Hint: you can use the TAB key to autocomplete commands\n" - ); + printf("\n" + "See man pbc(1) for more info about specific commands\n" + "Hint: you can use the TAB key to autocomplete commands\n"); } void cmd_send(char * addr_str) { @@ -48,30 +43,31 @@ void cmd_send(char * addr_str) { return; } - char* data_str = consume_token(addr_str, IFS); + char * data_str = consume_token(addr_str, IFS); if (data_str == NULL) { printf("error: no data\n"); return; } - char* end; + char * end; uint16_t addr = strtol(addr_str, &end, 0); if (addr_str + strlen(addr_str) != end) { printf("address format error\n"); return; } - char* data; + char * data; size_t 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", - -err, data_str, -err, ""); + printf("data format error at index %d:\n%s\n%*s^\n", -err, data_str, + -err, ""); return; } - printf("sending char data[%lu = 0x%02lx] to 0x%02x\n", data_size, data_size, addr); + printf("sending char data[%lu = 0x%02lx] to 0x%02x\n", data_size, data_size, + addr); i2c_send(addr, data, data_size); free(data); @@ -82,7 +78,7 @@ static void cmd_set_state(char * line, pb_global_state_t state) { printf("error: no address\n"); return; } - + i2c_addr_t addr = strtol(line, NULL, 0); pb_buf_t buf = pb_send_state_set(state); @@ -90,22 +86,14 @@ static void cmd_set_state(char * line, pb_global_state_t state) { pb_buf_free(&buf); } -void cmd_reset(char * line) { - cmd_set_state(line, PB_GS_IDLE); -} +void cmd_reset(char * line) { cmd_set_state(line, PB_GS_IDLE); } -void cmd_skip(char * line) { - cmd_set_state(line, PB_GS_SOLVED); -} +void cmd_skip(char * line) { cmd_set_state(line, PB_GS_SOLVED); } extern bool i2c_dump_send; extern bool i2c_dump_recv; const char * dump_modes[] = { - "none", - "send", - "recv", - "both", - NULL, + "none", "send", "recv", "both", NULL, }; void cmd_dump(char * mode) { consume_token(mode, IFS); @@ -121,7 +109,7 @@ void cmd_dump(char * mode) { printf("mode \"%s\" unknown\n", mode); } -char** cmd_dump_complete(const char * text, int begin, int end) { +char ** cmd_dump_complete(const char * text, int begin, int end) { int word = rl_word(rl_line_buffer, begin); if (word == 1) return rl_complete_list(text, dump_modes); return NULL; diff --git a/client/cmd.h b/client/cmd.h index 7fee700..babc8f2 100644 --- a/client/cmd.h +++ b/client/cmd.h @@ -28,7 +28,7 @@ typedef void cmd_handle_t(char * line); * \return Array of \c char* with suggestions. The array is terminated by a * NULL pointer. */ -typedef char** cmd_complete_t(const char* text, int start, int end); +typedef char ** cmd_complete_t(const char * text, int start, int end); /** * \internal @@ -36,8 +36,9 @@ typedef char** cmd_complete_t(const char* text, int start, int end); */ typedef struct { cmd_handle_t * handle; //!< Handler function (required) - const char* name; //!< Command name (required) - const char* info; //!< Command info (shown in help command) (optional = NULL) + const char * name; //!< Command name (required) + const char * + info; //!< Command info (shown in help command) (optional = NULL) cmd_complete_t * complete; //!< Completion function (optional = NULL) } cmd_t; @@ -247,4 +248,3 @@ static const cmd_t cmds[] = { static const size_t cmds_length = sizeof(cmds) / sizeof(cmds[0]); /// \} - diff --git a/client/i2c.cpp b/client/i2c.cpp index 18be4bc..3613baf 100644 --- a/client/i2c.cpp +++ b/client/i2c.cpp @@ -6,11 +6,11 @@ #include "sock.h" #include "xxd.h" -#include "pb.h" #include "pb-buf.h" +#include "pb-mod.h" #include "pb-msg.h" #include "pb-types.h" -#include "pb-mod.h" +#include "pb.h" #ifdef DEBUG bool i2c_dump_send = true; @@ -27,7 +27,7 @@ void i2c_send(uint16_t addr, const char * data, size_t data_size) { .length = data_size, }; - char* packed; + char * packed; size_t size; if (!i2ctcp_write(&msg, &packed, &size)) return; @@ -66,9 +66,9 @@ void i2c_dump(const i2ctcp_msg_t * msg) { _rl_printf_start(); const char * direction = send ? "send" : "recv"; - printf("[I2C %s] 0x%02x -> 0x%02x data(0x%02lx):\n", direction, sender, receiver, msg->length); + printf("[I2C %s] 0x%02x -> 0x%02x data(0x%02lx):\n", direction, sender, + receiver, msg->length); xxd(msg->data, msg->length); _rl_printf_stop(); } - diff --git a/client/i2c.h b/client/i2c.h index 538624a..30d9d8a 100644 --- a/client/i2c.h +++ b/client/i2c.h @@ -1,7 +1,7 @@ #pragma once -#include <stdint.h> #include <stddef.h> +#include <stdint.h> #include "i2ctcpv1.h" @@ -36,4 +36,3 @@ void i2c_send(uint16_t addr, const char * data, size_t data_size); void i2c_dump(const i2ctcp_msg_t * msg); /// \} - diff --git a/client/main.cpp b/client/main.cpp index 5c26107..c69fbcc 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -1,21 +1,21 @@ +#include <cstdint> #include <cstdio> #include <cstdlib> -#include <cstdint> #include <exception> #include "rl.h" #include "sock.h" -PBSocket* sock; +PBSocket * sock; -int main(int argc, char** argv) { +int main(int argc, char ** argv) { if (argc < 2) { printf("usage: %s addr [port]\n", argv[0]); return EXIT_FAILURE; } // parse arguments - char* addr = argv[1]; + char * addr = argv[1]; uint16_t port = 9191; if (argc >= 3) port = atoi(argv[2]); @@ -23,7 +23,7 @@ int main(int argc, char** argv) { try { // connect to TCP socket (automatically spawns thread) sock->sock_connect(); - } catch (const std::exception& e) { + } catch (const std::exception & e) { printf("error: %s\n", e.what()); return EXIT_FAILURE; } @@ -35,4 +35,3 @@ int main(int argc, char** argv) { return ret; } - diff --git a/client/mod.c b/client/mod.c index b67a61d..4ce4e8e 100644 --- a/client/mod.c +++ b/client/mod.c @@ -1,9 +1,8 @@ -#include "pb.h" #include "pb-mod.h" +#include "pb.h" const char * PB_MOD_NAME = "client"; const i2c_addr_t PB_MOD_ADDR = PB_ADDR_MOD_MAIN; void pb_i2c_recv(const uint8_t * buf, size_t sz) {} void pb_i2c_send(i2c_addr_t i2c_addr, const uint8_t * buf, size_t sz) {} - diff --git a/client/parse.cpp b/client/parse.cpp index 16f0781..516445b 100644 --- a/client/parse.cpp +++ b/client/parse.cpp @@ -1,7 +1,7 @@ +#include <netinet/in.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> -#include <stdio.h> -#include <netinet/in.h> #include "parse.h" @@ -25,15 +25,14 @@ static int parse_string(const char * str, char * data, size_t * offset) { for (i = 1; i < len && str[i] != '\0'; i++, *offset += 1) { char c = str[i]; - if (c == closing) - return i + 1; // +1 for closing quote + if (c == closing) return i + 1; // +1 for closing quote if (escape && c == '\\') { char x = str[i + 1]; - if (x == '0') c = '\0'; - else if (x == 't') c = '\t'; - else if (x == 'n') c = '\n'; - else if (x == 'r') c = '\r'; + if (x == '0') c = '\0'; + else if (x == 't') c = '\t'; + else if (x == 'n') c = '\n'; + else if (x == 'r') c = '\r'; else if (x == '\\') c = '\\'; else if (x == '\"') c = '\"'; else if (x == '\'') c = '\''; @@ -41,8 +40,7 @@ static int parse_string(const char * str, char * data, size_t * offset) { i++; } - if (data != NULL) - data[*offset] = c; + if (data != NULL) data[*offset] = c; } return -i; @@ -53,7 +51,7 @@ static int parse_hexstr(const char * str, char * data, size_t * offset) { int i = 0; // check if token contains at least one colon - const char* colon = strchr(str, ':'); + const char * colon = strchr(str, ':'); if (colon == NULL) return -i; if (colon >= str + len) return -i; @@ -63,11 +61,9 @@ static int parse_hexstr(const char * str, char * data, size_t * offset) { size_t c = 0; while (c < len) { // count bytes in bytestring - if (strspn(str + c, SET_HEX) != 2) - return -i -c; + if (strspn(str + c, SET_HEX) != 2) return -i - c; - if (data != NULL) - data[*offset] = strtol(str + c, NULL, 16) & 0xff; + if (data != NULL) data[*offset] = strtol(str + c, NULL, 16) & 0xff; c += 2; *offset += 1; @@ -92,16 +88,16 @@ static int parse_number(const char * str, char * data, size_t * offset) { 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 + } /* else if (len > 1 && strncmp(str, "0", 1) == 0) { // octal prefix base = 8; i += 1; }*/ - const char* set; + const char * set; // if (base == 8) set = SET_OCT; if (base == 10) set = SET_DEC; if (base == 16) set = SET_HEX; - + size_t len_ok = strspn(str + i, set) + i; if (len != len_ok) return -len_ok; @@ -109,9 +105,9 @@ static int parse_number(const char * str, char * data, size_t * offset) { if (base == 16) { size_t prefixless = len - i; switch (prefixless) { - case 2: // 8-bit (2 hex characters) - case 4: // 16-bit - case 8: // 32-bit + case 2: // 8-bit (2 hex characters) + case 4: // 16-bit + case 8: // 32-bit case 16: // 64-bit break; default: @@ -133,12 +129,12 @@ static int parse_number(const char * str, char * data, size_t * offset) { case 2: number = htons(number); // TODO: check if the endianness is OK, or reverse these *offset indices* - data[*offset + 1] = (number) & 0xff; + data[*offset + 1] = (number) & 0xff; data[*offset + 0] = (number >>= 8) & 0xff; break; case 4: number = htonl(number); - data[*offset + 3] = (number) & 0xff; + data[*offset + 3] = (number) & 0xff; data[*offset + 2] = (number >>= 8) & 0xff; data[*offset + 1] = (number >>= 8) & 0xff; data[*offset + 0] = (number >>= 8) & 0xff; @@ -150,7 +146,7 @@ static int parse_number(const char * str, char * data, size_t * offset) { return len; } -static int _strtodata_main(const char * str, char* data, size_t * offset) { +static int _strtodata_main(const char * str, char * data, size_t * offset) { size_t len = strlen(str); int i, run; @@ -177,8 +173,7 @@ int strtodata(const char * str, char ** data, size_t * size) { if (ret <= 0) return ret; // on error // 2nd pass: convert string literals into binary data - *data = (char*) malloc(*size); + *data = (char *) malloc(*size); size_t written = 0; return _strtodata_main(str, *data, &written); } - diff --git a/client/parse.h b/client/parse.h index 1beb714..8427bb1 100644 --- a/client/parse.h +++ b/client/parse.h @@ -17,9 +17,9 @@ //! Decimal digit character set #define SET_DEC "0123456789" //! Hexadecimal digit character set -#define SET_HEX SET_DEC"abcdefABCDEF" +#define SET_HEX SET_DEC "abcdefABCDEF" //! (Hexadecimal) byte string character set -#define SET_HEX_STR SET_HEX":" +#define SET_HEX_STR SET_HEX ":" /** * \brief modify \p token to point to the first token when broken up on \p ifs @@ -34,7 +34,7 @@ * * \return the remaining data after \p token and the first \p ifs */ -char* consume_token(char * token, const char * ifs); +char * consume_token(char * token, const char * ifs); /** * \brief convert string with literals into raw data @@ -53,4 +53,3 @@ char* consume_token(char * token, const char * ifs); int strtodata(const char * str, char ** data, size_t * size); /// \} - diff --git a/client/rl.cpp b/client/rl.cpp index 82d1412..cc3de69 100644 --- a/client/rl.cpp +++ b/client/rl.cpp @@ -1,16 +1,16 @@ -#include <stdlib.h> -#include <stdio.h> -#include <stdbool.h> #include <stdarg.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> -#include <readline/readline.h> #include <readline/history.h> +#include <readline/readline.h> -#include "rl.h" #include "cmd.h" #include "parse.h" +#include "rl.h" -static char* saved_line; +static char * saved_line; static int saved_point, saved_end; void _rl_printf_start() { @@ -35,7 +35,7 @@ void _rl_printf_stop() { free(saved_line); } -void rl_printf(const char *fmt, ...) { +void rl_printf(const char * fmt, ...) { _rl_printf_start(); va_list args; @@ -46,13 +46,12 @@ void rl_printf(const char *fmt, ...) { _rl_printf_stop(); } -static void cli_cmd(char* cmd) { +static void cli_cmd(char * cmd) { cmd += strspn(cmd, IFS); // skip leading whitespace - char* line = consume_token(cmd, IFS); + char * line = consume_token(cmd, IFS); for (size_t i = 0; i < cmds_length; i++) { - if (strncmp(cmds[i].name, cmd, strlen(cmd)) != 0) - continue; + if (strncmp(cmds[i].name, cmd, strlen(cmd)) != 0) continue; cmds[i].handle(line); return; @@ -61,7 +60,7 @@ static void cli_cmd(char* cmd) { printf("unknown command!\n"); } -static char* rl_completion_entries(const char *text, int state) { +static char * rl_completion_entries(const char * text, int state) { static size_t i = 0; if (state == 0) i = 0; @@ -76,7 +75,7 @@ static char* rl_completion_entries(const char *text, int state) { return NULL; } -static char** rl_attempted_completion(const char * text, int start, int end) { +static char ** rl_attempted_completion(const char * text, int start, int end) { // do not suggest filenames rl_attempted_completion_over = 1; @@ -90,7 +89,8 @@ static char** rl_attempted_completion(const char * text, int start, int end) { for (size_t i = 0; i < cmds_length; i++) { cmd_t cmd = cmds[i]; if (cmd.complete == NULL) continue; - if (strncmp(cmd.name, rl_line_buffer + cmd_start, cmd_len) != 0) continue; + if (strncmp(cmd.name, rl_line_buffer + cmd_start, cmd_len) != 0) + continue; return cmd.complete(text, start, end); } @@ -99,7 +99,7 @@ static char** rl_attempted_completion(const char * text, int start, int end) { } int cli_main() { - char* input = NULL; + char * input = NULL; rl_attempted_completion_function = rl_attempted_completion; while (1) { @@ -111,7 +111,7 @@ int cli_main() { add_history(input); cli_cmd(input); - } + } return EXIT_SUCCESS; } @@ -132,22 +132,22 @@ typedef struct { const char * word; const char ** options; } _rl_complete_list_data_t; -char** rl_complete_list(const char * word, const char ** options) { +char ** rl_complete_list(const char * word, const char ** options) { _rl_complete_list_data_t data = { .word = word, .options = options, }; - return rl_completion_matches((char *) &data, [](const char * text, int state) -> char * { - _rl_complete_list_data_t data = *(_rl_complete_list_data_t *) text; - static size_t i = 0; - if (state == 0) i = 0; - - while (data.options[i] != NULL) { - const char * option = data.options[i++]; - if (strncmp(data.word, option, strlen(data.word)) == 0) - return strdup(option); - } - return NULL; - }); + return rl_completion_matches( + (char *) &data, [](const char * text, int state) -> char * { + _rl_complete_list_data_t data = *(_rl_complete_list_data_t *) text; + static size_t i = 0; + if (state == 0) i = 0; + + while (data.options[i] != NULL) { + const char * option = data.options[i++]; + if (strncmp(data.word, option, strlen(data.word)) == 0) + return strdup(option); + } + return NULL; + }); } - diff --git a/client/rl.h b/client/rl.h index 4a5b2c1..ff30602 100644 --- a/client/rl.h +++ b/client/rl.h @@ -55,4 +55,3 @@ int rl_word(const char * line, int cursor); char ** rl_complete_list(const char * word, const char * options[]); /// \} - diff --git a/client/sock.cpp b/client/sock.cpp index c123411..7c959c6 100644 --- a/client/sock.cpp +++ b/client/sock.cpp @@ -1,23 +1,23 @@ #include <arpa/inet.h> +#include <cstdio> #include <cstring> +#include <errno.h> +#include <netinet/in.h> #include <stdexcept> -#include <unistd.h> -#include <cstdio> -#include <sys/types.h> #include <sys/socket.h> -#include <netinet/in.h> -#include <errno.h> +#include <sys/types.h> #include <thread> +#include <unistd.h> +#include "i2c.h" #include "i2ctcpv1.h" -#include "sock.h" #include "rl.h" -#include "i2c.h" +#include "sock.h" using std::logic_error; using std::thread; -PBSocket::PBSocket() { } +PBSocket::PBSocket() {} PBSocket::PBSocket(const char * addr, uint16_t port) : PBSocket() { set_server(addr, port); } @@ -47,7 +47,7 @@ void PBSocket::sock_connect() { _fd = socket(AF_INET, SOCK_STREAM, 0); if (_fd < 0) throw logic_error("socket create failed"); - + struct sockaddr_in server = { .sin_family = AF_INET, .sin_port = htons(_port), @@ -55,7 +55,7 @@ void PBSocket::sock_connect() { .s_addr = inet_addr(_addr), }, }; - int ret = connect(_fd, (struct sockaddr*) &server, sizeof(server)); + int ret = connect(_fd, (struct sockaddr *) &server, sizeof(server)); if (ret != 0) throw logic_error(strerror(errno)); this->_thread = new thread(&PBSocket::sock_task, this); @@ -75,7 +75,7 @@ void PBSocket::sock_task() { i2ctcp_msg_t input; i2ctcp_read_reset(&input); - while(1) { + while (1) { char buf[80]; ssize_t bytes = read(_fd, buf, sizeof(buf)); @@ -105,4 +105,3 @@ void PBSocket::sock_task() { sock_close(); } - diff --git a/client/sock.h b/client/sock.h index 5b62861..dbe6d6a 100644 --- a/client/sock.h +++ b/client/sock.h @@ -52,7 +52,7 @@ private: void sock_close(); //! Pointer to thread running \c sock_task() - std::thread* _thread = nullptr; + std::thread * _thread = nullptr; /** * \brief IP address of server to connect to @@ -69,7 +69,6 @@ private: }; //! Singleton \c PBSocket instance -extern PBSocket* sock; +extern PBSocket * sock; /// \} - diff --git a/client/xxd.c b/client/xxd.c index 5d83635..2b30dfb 100644 --- a/client/xxd.c +++ b/client/xxd.c @@ -1,5 +1,5 @@ -#include <stdio.h> #include <ctype.h> +#include <stdio.h> #include "xxd.h" @@ -33,12 +33,9 @@ void xxd(const char * data, size_t size) { continue; } - if (isprint(data[i])) - printf("%c", data[i]); - else - printf("."); + if (isprint(data[i])) printf("%c", data[i]); + else printf("."); } printf("|\n"); } } - diff --git a/client/xxd.h b/client/xxd.h index ede9fff..b9ade3c 100644 --- a/client/xxd.h +++ b/client/xxd.h @@ -23,4 +23,3 @@ void xxd(const char * data, size_t size); #ifdef __cplusplus } #endif - |