about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDaniel <steew0x8@protonmail.com>2021-11-06 19:17:00 +0100
committerDaniel <steew0x8@protonmail.com>2021-11-06 19:17:00 +0100
commit7807b47e960f77aa780e2084641066da2f33f791 (patch)
tree257a54ce3aa3f2b1915cfef54d1352f4e69a2d4d
parent2dba790a5b0d1e61acee6da4b85a486b036446ba (diff)
downloadrpncalc-7807b47e960f77aa780e2084641066da2f33f791.tar.gz
added argument count check for funcs
-rw-r--r--cmds.c16
-rw-r--r--main.c6
-rw-r--r--state.h1
-rw-r--r--util.c1
4 files changed, 18 insertions, 6 deletions
diff --git a/cmds.c b/cmds.c
index 7960834..3b44110 100644
--- a/cmds.c
+++ b/cmds.c
@@ -33,12 +33,12 @@ stack fpop(stack stack) {
 }
 
 command CMD_LIST[] = {
-  {"p", &fpop, "pop last element"},
-  {"ceil", &fceil, "truncate to the next integer"},
-  {"floor", &ffloor, "truncate to the previous integer"},
-  {"log", &flogB, "calculate logarithm in base LAST_NUM of LAST_NUM-1"},
-  {"neg", &fnegate, "change last element's sign"},
-  {0, 0, 0}
+  {"p", &fpop, "pop last element", 1},
+  {"ceil", &fceil, "truncate to the next integer", 1},
+  {"floor", &ffloor, "truncate to the previous integer", 1},
+  {"log", &flogB, "calculate logarithm in base LAST_NUM of LAST_NUM-1", 2},
+  {"neg", &fnegate, "change last element's sign", 1},
+  {0, 0, 0, 0}
 };
 
 void init_state(state *s) {
@@ -57,6 +57,10 @@ void exec(char *buf, state *s) {
   /* change this to a binary tree search */
   for (int i = 0; s->sorted[i].name != 0; i++) {
     if (strcmp(buf, s->sorted[i].name) == 0) {
+      if (s->sorted[i].required_args > s->stk.count) {
+	fprintf(s->defout,"this function requires %d arguments\n", s->sorted[i].required_args);
+	return;
+      }
       fprintf(s->defout, "executing function: %s\n", s->sorted[i].name);
       s->stk = s->sorted[i].exec(s->stk);
       return;
diff --git a/main.c b/main.c
index cb5dada..63e847f 100644
--- a/main.c
+++ b/main.c
@@ -6,6 +6,7 @@
 #include <string.h>
 #include "cmds.h"
 #include <err.h>
+#include <errno.h>
 
 #define BUF_SIZE 50
 
@@ -37,6 +38,10 @@ int main() {
     fgets(buf, BUF_SIZE, s.defbuf);
     buf[strcspn(buf, "\n")] = 0;
     double interpreted = strtod(buf, &endptr);
+    if (errno == ERANGE) {
+      fprintf(s.defout,"sorry, this number is too big\n");
+      continue;
+    }
     if (interpreted == (double)0 && endptr == buf) { /* strtod returned the char it could not read */
       TYPE t = discriminate(buf);
       if (t == OPERATOR ) {
@@ -66,6 +71,7 @@ int main() {
 	/* check for special commands, else pass to exec() */
 	if (strcmp(buf, "quit") == 0) {
 	  fprintf(s.defout, "quitting, bye!\n");
+	  free (s.sorted);
 	  exit(0);
 	} else if (strcmp(buf, "list") == 0) {
 	  for (int i = 0; s.sorted[i].name != 0; i++) {
diff --git a/state.h b/state.h
index 56e0f6d..a9b1a7b 100644
--- a/state.h
+++ b/state.h
@@ -14,6 +14,7 @@ typedef struct {
   char *name;
   stack (*exec)(stack);
   char *description;
+  short int required_args;
 } command;
 
 typedef struct {
diff --git a/util.c b/util.c
index 1693593..a6c831f 100644
--- a/util.c
+++ b/util.c
@@ -22,3 +22,4 @@ TYPE discriminate(char *s) {
     return OPERATOR;
   } else return FUNCTION;
 }
+