-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
127 lines (111 loc) · 2.48 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* This file is licensed under BSD 3-Clause.
* All license information is available in the included COPYING file.
*/
/*
* main.c
*
* Author : Finn Rayment <[email protected]>
* Date created : 21/07/2022
*/
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "y.tab.h"
extern int yyended;
extern int yyrunning;
extern void switch_to_buffer(char *);
extern void clean_buffer();
extern char *argv_parse_redirect(int, char **);
extern void argv_parse_cleanup(void);
void
help(void)
{
fprintf(stdout, "Usage: calc [OPTION]... [EQUATION]\n");
fprintf(stdout, "Lightweight command-line calculator.\n\n");
fprintf(stdout, " -h show this help message and exit\n");
fprintf(stdout, " -v print the proram version and exit\n");
fprintf(stdout, "\n");
fprintf(stdout, "If no equation is given, then the program will be opened\n"
"in interactive mode.\n");
fprintf(stdout, "\n");
fprintf(stdout, "Copyright (c) 2022, Finn Rayment\n");
fprintf(stdout, "This software is licensed under BSD 3-clause.\n");
}
void
version(void)
{
fprintf(stdout, "calc-%s\n", VERSION);
}
int
main(int argc,
char **argv)
{
int c, redirect;
char *argv_buf, *in, *in2;
size_t len;
in = NULL;
in2 = NULL;
len = 0;
while ((c = getopt(argc, argv, "hv")) != -1)
{
switch (c)
{
case 'h':
help();
return EXIT_SUCCESS;
case 'v':
version();
return EXIT_SUCCESS;
case '?':
if (isprint(optopt))
fprintf(stderr, "Unknown option '-%c'.\n", optopt);
else
fprintf(stderr, "Unknown option character '\\x%x'.\n", optopt);
return EXIT_FAILURE;
default:
abort();
}
}
redirect = optind < argc;
if (redirect)
{
argv_buf = argv_parse_redirect(argc, argv);
fprintf(stdout, "> %s\n", argv_buf);
}
while (yyrunning)
{
if (!redirect)
{
in = readline("> ");
if (!in)
break; /* eof */
len = strlen(in);
/* add a newline to cause input to ask again.
* if we don't do this, yyparse() will act like there is an eof. */
in2 = (char *) malloc((len+2) * sizeof(char));
memcpy(in2, in, sizeof(char) * len);
*(in2+len) = '\n';
*(in2+len+1) = '\0';
switch_to_buffer(in2);
}
yyparse();
if (!redirect)
{
add_history(in);
clean_buffer();
free(in2);
free(in);
in = NULL;
in2 = NULL;
}
}
if (redirect)
argv_parse_cleanup();
clear_history();
return EXIT_SUCCESS;
}