diff --git a/src/command/commands.c b/src/command/commands.c index 400a7c8d..4919648a 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -72,7 +72,6 @@ static void _update_presence(const resource_presence_t presence, const char * const show, gchar **args); static gboolean _cmd_set_boolean_preference(gchar *arg, struct cmd_help_t help, const char * const display, preference_t pref); -static gboolean _strtoi(char *str, int *saveptr, int min, int max, char **err_msg); static void _cmd_show_filtered_help(char *heading, gchar *cmd_filter[], int filter_size); static gint _compare_commands(Command *a, Command *b); static void _who_room(gchar **args, struct cmd_help_t help); @@ -213,7 +212,7 @@ cmd_connect(gchar **args, struct cmd_help_t help) if (g_hash_table_contains(options, "port")) { char *port_str = g_hash_table_lookup(options, "port"); char *err_msg = NULL; - gboolean res = _strtoi(port_str, &port, 1, 65535, &err_msg); + gboolean res = strtoi_range(port_str, &port, 1, 65535, &err_msg); if (!res) { cons_show(err_msg); cons_show(""); @@ -469,7 +468,7 @@ cmd_account(gchar **args, struct cmd_help_t help) } else if (strcmp(property, "port") == 0) { int port; char *err_msg = NULL; - gboolean res = _strtoi(value, &port, 1, 65535, &err_msg); + gboolean res = strtoi_range(value, &port, 1, 65535, &err_msg); if (!res) { cons_show(err_msg); cons_show(""); @@ -529,7 +528,7 @@ cmd_account(gchar **args, struct cmd_help_t help) } else if (valid_resource_presence_string(property)) { int intval; char *err_msg = NULL; - gboolean res = _strtoi(value, &intval, -128, 127, &err_msg); + gboolean res = strtoi_range(value, &intval, -128, 127, &err_msg); if (res) { resource_presence_t presence_type = resource_presence_from_string(property); switch (presence_type) @@ -1575,7 +1574,7 @@ cmd_roster(gchar **args, struct cmd_help_t help) } int intval = 0; char *err_msg = NULL; - gboolean res = _strtoi(args[1], &intval, 1, 99, &err_msg); + gboolean res = strtoi_range(args[1], &intval, 1, 99, &err_msg); if (res) { prefs_set_roster_size(intval); cons_show("Roster screen size set to: %d%%", intval); @@ -2859,7 +2858,7 @@ cmd_occupants(gchar **args, struct cmd_help_t help) } else { int intval = 0; char *err_msg = NULL; - gboolean res = _strtoi(args[1], &intval, 1, 99, &err_msg); + gboolean res = strtoi_range(args[1], &intval, 1, 99, &err_msg); if (res) { prefs_set_occupants_size(intval); cons_show("Occupants screen size set to: %d%%", intval); @@ -3630,7 +3629,7 @@ cmd_inpblock(gchar **args, struct cmd_help_t help) int intval = 0; char *err_msg = NULL; - gboolean res = _strtoi(value, &intval, 1, 1000, &err_msg); + gboolean res = strtoi_range(value, &intval, 1, 1000, &err_msg); if (res) { cons_show("Input blocking set to %d milliseconds.", intval); prefs_set_inpblock(intval); @@ -3676,7 +3675,7 @@ cmd_log(gchar **args, struct cmd_help_t help) int intval = 0; char *err_msg = NULL; - gboolean res = _strtoi(value, &intval, PREFS_MIN_LOG_SIZE, INT_MAX, &err_msg); + gboolean res = strtoi_range(value, &intval, PREFS_MIN_LOG_SIZE, INT_MAX, &err_msg); if (res) { prefs_set_max_log_size(intval); cons_show("Log maxinum size set to %d bytes", intval); @@ -3725,7 +3724,7 @@ cmd_reconnect(gchar **args, struct cmd_help_t help) int intval = 0; char *err_msg = NULL; - gboolean res = _strtoi(value, &intval, 0, INT_MAX, &err_msg); + gboolean res = strtoi_range(value, &intval, 0, INT_MAX, &err_msg); if (res) { prefs_set_reconnect(intval); if (intval == 0) { @@ -3749,7 +3748,7 @@ cmd_autoping(gchar **args, struct cmd_help_t help) int intval = 0; char *err_msg = NULL; - gboolean res = _strtoi(value, &intval, 0, INT_MAX, &err_msg); + gboolean res = strtoi_range(value, &intval, 0, INT_MAX, &err_msg); if (res) { prefs_set_autoping(intval); iq_set_autoping(intval); @@ -3814,7 +3813,7 @@ cmd_autoaway(gchar **args, struct cmd_help_t help) if (strcmp(setting, "time") == 0) { int minutesval = 0; char *err_msg = NULL; - gboolean res = _strtoi(value, &minutesval, 1, INT_MAX, &err_msg); + gboolean res = strtoi_range(value, &minutesval, 1, INT_MAX, &err_msg); if (res) { prefs_set_autoaway_time(minutesval); cons_show("Auto away time set to: %d minutes.", minutesval); @@ -3860,7 +3859,7 @@ cmd_priority(gchar **args, struct cmd_help_t help) int intval = 0; char *err_msg = NULL; - gboolean res = _strtoi(value, &intval, -128, 127, &err_msg); + gboolean res = strtoi_range(value, &intval, -128, 127, &err_msg); if (res) { accounts_set_priority_all(jabber_get_account_name(), intval); resource_presence_t last_presence = accounts_get_last_presence(jabber_get_account_name()); @@ -4412,33 +4411,6 @@ _cmd_set_boolean_preference(gchar *arg, struct cmd_help_t help, return TRUE; } -static gboolean -_strtoi(char *str, int *saveptr, int min, int max, char **err_msg) -{ - char *ptr; - int val; - - errno = 0; - val = (int)strtol(str, &ptr, 0); - if (errno != 0 || *str == '\0' || *ptr != '\0') { - GString *err_str = g_string_new(""); - g_string_printf(err_str, "Could not convert \"%s\" to a number.", str); - *err_msg = err_str->str; - g_string_free(err_str, FALSE); - return FALSE; - } else if (val < min || val > max) { - GString *err_str = g_string_new(""); - g_string_printf(err_str, "Value %s out of range. Must be in %d..%d.", str, min, max); - *err_msg = err_str->str; - g_string_free(err_str, FALSE); - return FALSE; - } - - *saveptr = val; - - return TRUE; -} - static void _cmd_show_filtered_help(char *heading, gchar *cmd_filter[], int filter_size) { diff --git a/src/common.c b/src/common.c index 47598ddd..653be720 100644 --- a/src/common.c +++ b/src/common.c @@ -202,6 +202,33 @@ str_contains(const char str[], int size, char ch) return 0; } +gboolean +strtoi_range(char *str, int *saveptr, int min, int max, char **err_msg) +{ + char *ptr; + int val; + + errno = 0; + val = (int)strtol(str, &ptr, 0); + if (errno != 0 || *str == '\0' || *ptr != '\0') { + GString *err_str = g_string_new(""); + g_string_printf(err_str, "Could not convert \"%s\" to a number.", str); + *err_msg = err_str->str; + g_string_free(err_str, FALSE); + return FALSE; + } else if (val < min || val > max) { + GString *err_str = g_string_new(""); + g_string_printf(err_str, "Value %s out of range. Must be in %d..%d.", str, min, max); + *err_msg = err_str->str; + g_string_free(err_str, FALSE); + return FALSE; + } + + *saveptr = val; + + return TRUE; +} + int utf8_display_len(const char * const str) { diff --git a/src/common.h b/src/common.h index d64ea33a..9521a701 100644 --- a/src/common.h +++ b/src/common.h @@ -111,6 +111,7 @@ gboolean mkdir_recursive(const char *dir); char * str_replace(const char *string, const char *substr, const char *replacement); int str_contains(const char str[], int size, char ch); +gboolean strtoi_range(char *str, int *saveptr, int min, int max, char **err_msg); int utf8_display_len(const char * const str); gboolean utf8_is_printable(const wint_t ch); char * prof_getline(FILE *stream);