Browse Source

use readline instead of fputs() & fgets()

T. Meissner 7 months ago
parent
commit
6344b1c057
2 changed files with 11 additions and 9 deletions
  1. 1
    1
      chapter_04/Makefile
  2. 10
    8
      chapter_04/prompt.c

+ 1
- 1
chapter_04/Makefile View File

@@ -2,7 +2,7 @@ all: prompt
2 2
 
3 3
 
4 4
 %: %.c
5
-	cc -std=c11 -Wall $@.c -o $@
5
+	cc -std=c11 -Wall $@.c -ledit -o $@
6 6
 
7 7
 
8 8
 .PHONY: clean

+ 10
- 8
chapter_04/prompt.c View File

@@ -1,8 +1,7 @@
1 1
 #include <stdio.h>
2
+#include <stdlib.h>
2 3
 
3
-
4
-/* Declare a buffer for user input of size 2048 */
5
-static char input[2048];
4
+#include <editline/readline.h>
6 5
 
7 6
 
8 7
 int main(int argc, char const *argv[])
@@ -13,14 +12,17 @@ int main(int argc, char const *argv[])
13 12
 
14 13
     /* In a never ending loop */
15 14
     while (1) {
16
-        /* Output our prompt */
17
-        fputs("lispy> ", stdout);
15
+        /* Output our prompt and get input */
16
+        char* input = readline("lispy> ");
18 17
 
19
-        /* Read a line of user input of max size 2014 */
20
-        fgets(input, 2048, stdin);
18
+        /* Add input to history */
19
+        add_history(input);
21 20
 
22 21
         /* Echo input back to user */
23
-        printf("No you're a %s", input);
22
+        printf("No you're a %s\n", input);
23
+
24
+        /* Free retrieved input */
25
+        free(input);
24 26
     }
25 27
 
26 28
     return 0;