mirror of
				https://github.com/eledio-devices/thirdparty-tinyexpr.git
				synced 2025-10-31 00:32:38 +01:00 
			
		
		
		
	repl: fgets-based readline alternative
This commit is contained in:
		
							
								
								
									
										28
									
								
								repl.c
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								repl.c
									
									
									
									
									
								
							| @@ -1,8 +1,36 @@ | ||||
| #include "tinyexpr.h" | ||||
| #include <stdlib.h> | ||||
| #include <stdio.h> | ||||
| #include <string.h> | ||||
|  | ||||
| #ifndef NO_READLINE | ||||
| #include <readline/readline.h> | ||||
| #include <readline/history.h> | ||||
| #else | ||||
| static char *readline(const char *prompt) { | ||||
|     fprintf(stderr, "%s", prompt); | ||||
|     char buf[1024]; | ||||
|     char *line = fgets(buf, sizeof(buf), stdin); | ||||
|     if (line == NULL && feof(stdin)) { | ||||
|         return NULL; | ||||
|     } else if (line == NULL) { | ||||
|         perror("fgets"); | ||||
|         return NULL; | ||||
|     } | ||||
|  | ||||
|     size_t len = strlen(line); | ||||
|     if (line[len - 1] == '\n') { | ||||
|         line[len - 1] = '\0'; | ||||
|         len -= 1; | ||||
|     } | ||||
|  | ||||
|     line = malloc(len + 1); | ||||
|     strcpy(line, buf); | ||||
|     return line; | ||||
| } | ||||
|  | ||||
| static void add_history(const char *line) {} | ||||
| #endif | ||||
|  | ||||
| static int eval(const char *str) { | ||||
|     int err = 0; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user