AmendHub

Download:

jcs

/

subtext

/

amendments

/

162

settings: struct_editor: Add CONFIG_TYPE_IP


jcs made amendment 162 over 2 years ago
--- settings.c Tue Jun 21 16:23:52 2022 +++ settings.c Wed Jun 22 17:18:41 2022 @@ -21,6 +21,7 @@ #include "db.h" #include "focusable.h" #include "settings.h" +#include "tcp.h" /* for long2ip/ip2long */ #include "tetab.h" #include "util.h" @@ -65,7 +66,7 @@ print_options: sf = &fields[n]; if (n > 9) - co = 'A' + (n - 9); + co = 'A' + (n - 10); else co = '0' + n; @@ -73,24 +74,34 @@ print_options: switch (sf->type) { case CONFIG_TYPE_STRING: - session_printf(s, new_data + sf->off); + session_printf(s, "%s", new_data + sf->off); break; case CONFIG_TYPE_SHORT: - sval = (new_data[sf->off] << 8) | new_data[sf->off + 1]; + sval = CHARS_TO_SHORT(new_data[sf->off], new_data[sf->off + 1]); session_printf(s, "%d", sval); break; case CONFIG_TYPE_LONG: - lval = (new_data[sf->off] << 24) | - (new_data[sf->off + 1] << 16) | - (new_data[sf->off + 2] << 8) | - new_data[sf->off + 3]; + lval = CHARS_TO_LONG(new_data[sf->off], new_data[sf->off + 1], + new_data[sf->off + 2], new_data[sf->off + 3]); session_printf(s, "%ld", lval); break; case CONFIG_TYPE_BOOLEAN: session_printf(s, "%s", new_data[sf->off] == 0 ? "false" : "true"); break; + case CONFIG_TYPE_IP: { + char ip_str[16]; + lval = CHARS_TO_LONG(new_data[sf->off], new_data[sf->off + 1], + new_data[sf->off + 2], new_data[sf->off + 3]); + if (lval == 0) + session_printf(s, "0.0.0.0"); + else { + long2ip(lval, ip_str); + session_printf(s, "%s", ip_str); + } + break; } + } session_printf(s, "]\r\n"); } @@ -134,7 +145,7 @@ get_menu_option: break; } - if (n > 9 && (c == 'A' + (n - 9) || c == 'a' + (n - 9))) { + if (n > 9 && (c == 'A' + (n - 10) || c == 'a' + (n - 10))) { found = true; break; } @@ -173,13 +184,13 @@ get_input: case CONFIG_TYPE_SHORT: case CONFIG_TYPE_LONG: if (sf->type == CONFIG_TYPE_LONG) { - lval = (new_data[sf->off] << 24) | - (new_data[sf->off + 1] << 16) | - (new_data[sf->off + 2] << 8) | - new_data[sf->off + 3]; + lval = CHARS_TO_LONG(new_data[sf->off], + new_data[sf->off + 1], new_data[sf->off + 2], + new_data[sf->off + 3]); snprintf(initial, sizeof(initial), "%ld", lval); } else { - sval = (new_data[sf->off] << 8) | new_data[sf->off + 1]; + sval = CHARS_TO_SHORT(new_data[sf->off], + new_data[sf->off + 1]); snprintf(initial, sizeof(initial), "%d", sval); } input = session_field_input(s, 10, 10, initial, false, 0); @@ -244,6 +255,42 @@ get_input: } } break; + case CONFIG_TYPE_IP: { + char ip_str[16]; + lval = CHARS_TO_LONG(new_data[sf->off], new_data[sf->off + 1], + new_data[sf->off + 2], new_data[sf->off + 3]); + if (lval == 0) + snprintf(initial, sizeof(initial), "0.0.0.0"); + else { + long2ip(lval, ip_str); + snprintf(initial, sizeof(initial), ip_str); + } + + input = session_field_input(s, 16, 16, initial, false, 0); + session_output(s, "\r\n", 2); + session_flush(s); + if (input == NULL || s->ending) + break; + + if (input[0] == '\0') { + lval = 0; + free(input); + } else { + lval = ip2long(input); + free(input); + if (lval == 0) { + session_printf(s, + "Invalid IP address (^C to cancel)\r\n"); + goto get_input; + } + } + new_data[sf->off] = (lval >> 24) & 0xff; + new_data[sf->off + 1] = (lval >> 16) & 0xff; + new_data[sf->off + 2] = (lval >> 8) & 0xff; + new_data[sf->off + 3] = lval & 0xff; + any_changes = true; + break; + } } if (input != NULL) --- settings.h Sun May 15 15:58:58 2022 +++ settings.h Wed Jun 22 15:25:39 2022 @@ -23,7 +23,8 @@ enum { CONFIG_TYPE_STRING, CONFIG_TYPE_SHORT, CONFIG_TYPE_LONG, - CONFIG_TYPE_BOOLEAN + CONFIG_TYPE_BOOLEAN, + CONFIG_TYPE_IP }; struct struct_field {