parial completion feature
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Thu, 26 Jan 2023 17:46:43 +0000 (18:46 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Thu, 26 Jan 2023 17:46:43 +0000 (18:46 +0100)
calc.c
parser.c
parser.h

diff --git a/calc.c b/calc.c
index d4d224387294d1c41ab7ac3b3492ff494a937249..f84cf91af829d63256a9a57e7ffe4e72715f6b73 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -30,6 +30,7 @@
 char *progname = NULL;
 int mode = 1;
 int precision = 6;
+char **completion_list = NULL;
 
 /* help function */
 
@@ -106,6 +107,9 @@ int main (int argc, char *argv[])
     char format[9] = "=> %..g\n";
     format[5] = '0' + precision;
 
+    /* completion list*/
+    completion_list = generate_completion_list ();
+
     /* read from input stream */
 
     while (1) {
@@ -178,6 +182,8 @@ int main (int argc, char *argv[])
         }
     }
 
+    free_completion_list (completion_list);
+
     return ret;
 }
 
index c023a6e959a5fe6fb3f86f76fba752bb97a0745b..b4381ac338dab2381eab0516100b276ad4779939 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -2,6 +2,7 @@
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "debug.h"
 
@@ -627,7 +628,9 @@ void help (void)
     fprintf (stdout, "logical operators:");
     fprintf (stdout, " & | !\n");
     fprintf (stdout, "mathematic functions:");
-    fprintf (stdout, " pow sqrt cos sin tan acos asin atan log exp\n");
+    fprintf (stdout, " pow sqrt log exp\n");
+    fprintf (stdout, "trigonometric functions:");
+    fprintf (stdout, " cos sin tan acos asin atan\n");
     fprintf (stdout, "supported functions:");
     fprintf (stdout, " abs ceil floor\n");
     fprintf (stdout, "storage functions:");
@@ -797,4 +800,49 @@ double evaluate_element (element_t *root, char mask)
     return 0;
 }
 
+char **generate_completion_list ()
+{
+    int i, l = 0;
+    char **list = (char **) calloc (NB_FUNCTIONS + NB_CONSTANTS + 1, sizeof (char *));
+    if (list == NULL) {
+        VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
+        exit (1);
+    }
+
+    for (i = 0; i < NB_FUNCTIONS; i++) {
+        list[l] = strdup ((functions + i)->keyword);
+        if (list[l] == NULL) {
+            VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
+            exit (1);
+        }
+        l++;
+    }
+
+    for (i = 0; i < NB_CONSTANTS; i++) {
+        list[l] = strdup ((constants + i)->keyword);
+        if (list[l] == NULL) {
+            VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
+            exit (1);
+        }
+        l++;
+    }
+
+    return (list);
+}
+
+void free_completion_list (char **list)
+{
+    int i;
+
+    if (list) {
+        for (i = 0; i < NB_FUNCTIONS + NB_CONSTANTS; i++) {
+            if (list[i] != NULL) {
+                free (list[i]);
+            }
+        }
+        free (list);
+    }
+}
+
+
 /* vim: set ts=4 sw=4 et: */
index 156a52648efdfa2df0b82a8e9d2218ac28a059c0..b48f6d004491d09494ecd158e2d7ca2db94a4333 100644 (file)
--- a/parser.h
+++ b/parser.h
@@ -55,6 +55,12 @@ void print_element (element_t *root, int level);
 
 double evaluate_element (element_t *root, char mask);
 
+/* completion functions */
+
+char **generate_completion_list ();
+
+void free_completion_list (char **list);
+
 #endif /* __PARSER_H__ */
 
 /* vim: set ts=4 sw=4 et: */