diff --git a/src/configfile.c b/src/configfile.c index 697fed7..10717ac 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -215,7 +215,13 @@ static void config_parse_keys(GKeyFile *key_file, char *group) config_get_string(key_file, group, "log-file", &option.log_filename, NULL); config_get_bool(key_file, group, "log-append", &option.log_append); config_get_bool(key_file, group, "log-strip", &option.log_strip); - config_get_string(key_file, group, "map", &option.map, NULL); + config_get_string(key_file, group, "map", &string, NULL); + if (string != NULL) + { + option_parse_mappings(string); + g_free((void *)string); + string = NULL; + } config_get_string(key_file, group, "color", &string, NULL); if (string != NULL) { diff --git a/src/options.c b/src/options.c index 58ae78e..94a623f 100644 --- a/src/options.c +++ b/src/options.c @@ -85,7 +85,6 @@ struct option_t option = .local_echo = false, .timestamp = TIMESTAMP_NONE, .socket = NULL, - .map = "", .color = 256, // Bold .input_mode = INPUT_MODE_NORMAL, .output_mode = OUTPUT_MODE_NORMAL, @@ -109,6 +108,17 @@ struct option_t option = .hex_n_value = 0, .vt100 = false, .exec = NULL, + .map_i_nl_cr = false, + .map_i_cr_nl = false, + .map_ign_cr = false, + .map_i_ff_escc = false, + .map_i_nl_crnl = false, + .map_o_cr_nl = false, + .map_o_nl_crnl = false, + .map_o_del_bs = false, + .map_o_ltu = false, + .map_o_nulbrk = false, + .map_o_msblsb = false, }; void option_print_help(char *argv[]) @@ -692,6 +702,90 @@ void option_parse_script_run(const char *arg, script_run_t *script_run) } } +void option_parse_mappings(const char *map) +{ + bool token_found = true; + char *token = NULL; + char *buffer; + + if (map == NULL) + { + return; + } + + /* Parse any specified input or output mappings */ + buffer = strdup(map); + while (token_found == true) + { + if (token == NULL) + { + token = strtok(buffer,","); + } + else + { + token = strtok(NULL, ","); + } + + if (token != NULL) + { + if (strcmp(token,"INLCR") == 0) + { + option.map_i_nl_cr = true; + } + else if (strcmp(token,"IGNCR") == 0) + { + option.map_ign_cr = true; + } + else if (strcmp(token,"ICRNL") == 0) + { + option.map_i_cr_nl = true; + } + else if (strcmp(token,"OCRNL") == 0) + { + option.map_o_cr_nl = true; + } + else if (strcmp(token,"ODELBS") == 0) + { + option.map_o_del_bs = true; + } + else if (strcmp(token,"IFFESCC") == 0) + { + option.map_i_ff_escc = true; + } + else if (strcmp(token,"INLCRNL") == 0) + { + option.map_i_nl_crnl = true; + } + else if (strcmp(token, "ONLCRNL") == 0) + { + option.map_o_nl_crnl = true; + } + else if (strcmp(token, "OLTU") == 0) + { + option.map_o_ltu = true; + } + else if (strcmp(token, "ONULBRK") == 0) + { + option.map_o_nulbrk = true; + } + else if (strcmp(token, "MSB2LSB") == 0) + { + option.map_o_msblsb = true; + } + else + { + printf("Error: Unknown mapping flag %s\n", token); + exit(EXIT_FAILURE); + } + } + else + { + token_found = false; + } + } + free(buffer); +} + void options_print() { tio_printf(" Device: %s", device_name); @@ -928,7 +1022,7 @@ void options_parse(int argc, char *argv[]) break; case 'm': - option.map = optarg; + option_parse_mappings(optarg); break; case 'c': diff --git a/src/options.h b/src/options.h index 21a5f75..80e0f3a 100644 --- a/src/options.h +++ b/src/options.h @@ -69,7 +69,6 @@ struct option_t timestamp_t timestamp; char *log_filename; char *log_directory; - char *map; char *socket; int color; input_mode_t input_mode; @@ -94,6 +93,17 @@ struct option_t int hex_n_value; bool vt100; char *exec; + bool map_i_nl_cr; + bool map_i_cr_nl; + bool map_ign_cr; + bool map_i_ff_escc; + bool map_i_nl_crnl; + bool map_o_cr_nl; + bool map_o_nl_crnl; + bool map_o_del_bs; + bool map_o_ltu; + bool map_o_nulbrk; + bool map_o_msblsb; }; extern struct option_t option; @@ -119,3 +129,5 @@ const char *option_auto_connect_state_to_string(auto_connect_t strategy); void option_parse_timestamp(const char *arg, timestamp_t *timestamp); const char* option_timestamp_format_to_string(timestamp_t timestamp); + +void option_parse_mappings(const char *map); diff --git a/src/socket.c b/src/socket.c index 043b657..920596f 100644 --- a/src/socket.c +++ b/src/socket.c @@ -343,20 +343,20 @@ bool socket_handle_input(fd_set *rdfs, char *output_char) } /* If INLCR is set, a received NL character shall be translated into a CR character */ - if (*output_char == '\n' && map_i_nl_cr) + if (*output_char == '\n' && option.map_i_nl_cr) { *output_char = '\r'; } else if (*output_char == '\r') { /* If IGNCR is set, a received CR character shall be ignored (not read). */ - if (map_ign_cr) + if (option.map_ign_cr) { return false; } /* If IGNCR is not set and ICRNL is set, a received CR character shall be translated into an NL character. */ - if (map_i_cr_nl) + if (option.map_i_cr_nl) { *output_char = '\n'; } diff --git a/src/tty.c b/src/tty.c index 2adef9e..d6356bf 100644 --- a/src/tty.c +++ b/src/tty.c @@ -153,9 +153,6 @@ const char random_array[] = }; bool interactive_mode = true; -bool map_i_nl_cr = false; -bool map_i_cr_nl = false; -bool map_ign_cr = false; char key_hit = 0xff; @@ -167,14 +164,6 @@ static bool connected = false; static bool standard_baudrate = true; static void (*print)(char c); static int device_fd; -static bool map_i_ff_escc = false; -static bool map_i_nl_crnl = false; -static bool map_o_cr_nl = false; -static bool map_o_nl_crnl = false; -static bool map_o_del_bs = false; -static bool map_o_ltu = false; -static bool map_o_nulbrk = false; -static bool map_o_msblsb = false; static char hex_chars[2]; static unsigned char hex_char_index = 0; static char tty_buffer[BUFSIZ*2]; @@ -259,7 +248,7 @@ ssize_t tty_write(int fd, const void *buffer, size_t count) ssize_t retval = 0, bytes_written = 0; size_t i; - if (map_o_ltu) + if (option.map_o_ltu) { // Convert lower case to upper case for (i = 0; i