-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
51 lines (43 loc) · 1012 Bytes
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// UMATH terminal based natural input calculator utilising unicode mathsyms
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <errno.h>
#include "umath.h"
#define _POSIX_C_SOURCE 200809L
int main(int argc, char *argv[]) {
char *input = NULL;
// Input modes
if(argc >= 2) {
// Cmdline argument
input = argv[1];
} else if(isatty(fileno(stdin))) {
// Interactive terminal
input = readline("> ");
} else {
// Pipe
size_t n = 0;
ssize_t result = getline(&input, &n, stdin);
if(result == -1) {
if(errno == EINVAL) {
fprintf(stderr, "Bad argument to getline()\n");
} else if (errno == ENOMEM) {
fprintf(stderr, "Line allocation failed\n");
}
}
// Strip newline
if(input[result - 1] == '\n') {
input[result - 1] = '\0';
}
}
if(!input) {
return EXIT_SUCCESS;
}
char *output = umath(input);
printf("%s", output);
free(output);
if(argc < 2) free(input);
return EXIT_SUCCESS;
}