about summary refs log tree commit diff stats
path: root/src/command.c
diff options
context:
space:
mode:
authorDmitry Podgorny <pasis.ua@gmail.com>2012-11-14 00:23:06 +0200
committerDmitry Podgorny <pasis.ua@gmail.com>2012-11-14 00:23:06 +0200
commit52c4c3d953008cc89af230651568f2081e132c9b (patch)
tree144fa17e6d50c9c761df59e6c12b4c5e389ef1bb /src/command.c
parentaba1ea5133f468346b45b5e6e68676b037ebe49b (diff)
downloadprofani-tty-52c4c3d953008cc89af230651568f2081e132c9b.tar.gz
introduce _strtoi() and validation of typed numbers
Diffstat (limited to 'src/command.c')
-rw-r--r--src/command.c44
1 files changed, 37 insertions, 7 deletions
diff --git a/src/command.c b/src/command.c
index bf7ae5be..1ca20c47 100644
--- a/src/command.c
+++ b/src/command.c
@@ -22,6 +22,8 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <limits.h>
+#include <errno.h>
 
 #include <glib.h>
 
@@ -79,6 +81,8 @@ static void _notify_autocomplete(char *input, int *size);
 static void _parameter_autocomplete(char *input, int *size, char *command,
     autocomplete_func func);
 
+static int _strtoi(char *str, int *saveptr, int min, int max);
+
 // command prototypes
 static gboolean _cmd_quit(const char * const inp, struct cmd_help_t help);
 static gboolean _cmd_help(const char * const inp, struct cmd_help_t help);
@@ -1404,14 +1408,17 @@ _cmd_set_log(const char * const inp, struct cmd_help_t help)
     value = strtok(NULL, " ");
     if (subcmd == NULL || value == NULL) {
         cons_show("Usage: %s", help.usage);
-    } else {
-        if (strcmp(subcmd, "maxsize") == 0) {
-            intval = atoi(value);
+        return TRUE;
+    }
+
+    if (strcmp(subcmd, "maxsize") == 0) {
+        if (_strtoi(value, &intval, PREFS_MIN_LOG_SIZE, INT_MAX) == 0) {
             prefs_set_max_log_size(intval);
             cons_show("Log maxinum size set to %d bytes", intval);
         }
-        /* TODO: make 'level' subcommand for debug level */
     }
+    /* TODO: make 'level' subcommand for debug level */
+
     return TRUE;
 }
 
@@ -1427,10 +1434,12 @@ _cmd_set_priority(const char * const inp, struct cmd_help_t help)
     value = strtok(NULL, " ");
     if (value == NULL) {
         cons_show("Usage: %s", help.usage);
-    } else {
+        return TRUE;
+    }
+
+    if (_strtoi(value, &intval, -128, 127) == 0) {
         char *status = jabber_get_status();
-        intval = atoi(value);
-        prefs_set_priority(intval);
+        prefs_set_priority((int)intval);
         // update presence with new priority
         jabber_update_presence(jabber_get_presence(), status);
         if (status != NULL)
@@ -1702,3 +1711,24 @@ _notify_autocomplete(char *input, int *size)
         }
     }
 }
+
+static int
+_strtoi(char *str, int *saveptr, int min, int max)
+{
+    char *ptr;
+    int val;
+
+    errno = 0;
+    val = (int)strtol(str, &ptr, 0);
+    if (*str == '\0' || *ptr != '\0') {
+        cons_show("Illegal character. Must be a number.");
+        return -1;
+    } else if (errno == ERANGE || val < min || val > max) {
+        cons_show("Value out of range. Must be in %d..%d.", min, max);
+        return -1;
+    }
+
+    *saveptr = val;
+
+    return 0;
+}