/
main.c
247 lines (218 loc) · 7.2 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* (c) 2009-2011 Jan Dlabal <dlabaljan@gmail.com> */
/* */
/* This file is part of HC (HoubySoft Calculator). */
/* */
/* HC (HoubySoft Calculator) is free software: you can redistribute it */
/* and/or modify it under the terms of the GNU General Public License as */
/* published by the Free Software Foundation, either version 3 of the */
/* License, or any later version. */
/* */
/* HC (HoubySoft Calculator) is distributed in the hope that it will be */
/* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with HC (HoubySoft Calculator). If not, see */
/* <http://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <stdlib.h>
#include <unistd.h>
#ifndef I_HATE_TIPS
#include <time.h>
#endif
#include <string.h>
#include "hc.h"
#include "hc_functions.h"
#ifndef I_HATE_TIPS
char *tips[] = {
"We may be in the matrix right now.",
"To report a bug or suggest a feature, you can go to bugs.houbysoft.com.",
"If you don't like these tips, you can recompile HC with the -DI_HATE_TIPS flag.",
"To get the factorial of n, type factorial(n) or use the '!' notation.",
"To recall the last result, you can use ans().",
"You can use \\rpn to switch between RPN-mode and infix-mode.",
"You can use \\deg, \\rad, and \\grad to switch the angle mode.",
"HC is also a good converter. For example, to convert 1 US Fl Oz to mL, type floztoml(1). Type conversions to get a list of other built-in conversions!",
"You can type a constant's name with english characters to use it in a calculation -- for example, try pi.",
"This calculator can provide you The Answer.",
"From version 0.4.1, you can use the '!' notation for factorial and the '%' notation for modulus.",
"You can use implied multiplication in hc.",
"You can graph multiple functions on one graph with the gmul() function, or with the graph() function, passing a list of functions as the function (for example, graph([x,x^2,x^3])).",
"You can enter numbers not only in decimal, but also hexadecimal and binary form, using the 0x and 0b prefixes respectively.",
"Need help about something? Call help(\"nameoftopic\"), for example help(\"totient\") to get help about the totient function.",
"There is an easy way to reuse the last result. Call help(\"ans\") for more details.",
"HC even supports lambda functions and functional programming. Call help(\"lambda\") for more details."
};
#define TIP_COUNT 17
#endif
char **hc_completion(const char *text, int start, int end);
char *hc_cmd_generator(const char *text, int state);
int main(int argc, char *argv[])
{
char c;
char err=0;
char *eval = NULL;
char *import = NULL;
while ((c = getopt(argc, argv, "e:i:"))!=-1)
switch (c)
{
case 'e':
eval = optarg;
break;
case 'i':
import = optarg;
break;
default:
err++;
}
if (err || ((eval || import) && argv[optind]!=NULL) || (!eval && !import && (argc!=2 && argc!=1)))
{
printf("%s\nUsage:\n%s [[-i] FILE] [-e EXPRESSION]\n - use either FILE to load FILE into HC and execute it (add -i to leave the calculator open after execution)\n - OR use -e EXPRESSION to directly compute EXPRESSION and exit\n - OR pass no arguments to launch the default CLI interface and have access to more help and options.\n",eval ? "Invalid arguments (did you enclose the expression to calculate in quotes?)." : "Invalid arguments.",argv[0]);
exit(1);
}
if (eval)
{
hc.announce = FALSE;
char *fme = hc_result(eval);
if (fme && strlen(fme))
printf("%s",fme);
free(fme);
return 0;
} else if (argv[optind]!=NULL) {
hc.announce = FALSE;
hc_load(argv[optind]);
return 0;
} else {
hc.announce = TRUE;
if (import)
hc_load(import);
}
char *fme;
using_history();
rl_readline_name = "HC";
rl_attempted_completion_function = hc_completion;
rl_basic_word_break_characters = " \t\n\"'`@$><=;|&{(*/+-)^,!%";
printf("Welcome to hc %s -- http://houbysoft.com/hc/\n(type help for help, and exit to exit)\n",VERSION);
#ifndef I_HATE_TIPS
srand(time(NULL));
printf("Did you know? %s\n",tips[rand()%TIP_COUNT]);
#endif
hc_load_cfg();
char *expr;
while (42)
{
expr = readline("> ");
if (!expr)
exit(1);
while (!check_completeness(expr))
{
char *expr2 = readline(" . ");
if (!expr2)
exit(1);
expr = realloc(expr,strlen(expr)+strlen(expr2)+2); // +2 = space and \0
strcat(expr," ");
strcat(expr,expr2);
free(expr2);
}
if (strcmp(expr,"exit")==0 || strcmp(expr,"quit")==0) {
free(expr);
exit(0);
}
fme = hc_result(expr);
if (fme && strlen(fme))
{
printf(" %s\n",fme);
}
if (fme)
free(fme);
add_history(expr);
free(expr);
}
return 0;
}
char **hc_completion(const char *text, int start, int end)
{
rl_completion_suppress_append = 1;
char **matches = (char **)NULL;
matches = rl_completion_matches(text, hc_cmd_generator);
return matches;
}
char *hc_cmd_generator(const char *text, int state)
{
static int list_index, len;
static struct hc_ventry *var_tmp;
char *name;
if (!state)
{
list_index = 0;
len = strlen (text);
var_tmp = hc_var_first;
}
char isfunc;
/* Return the next name which partially matches from the command list. */
while (list_index < HC_NAMES)
{
if (strcmp(hc_names[list_index][1],"#") == 0)
{
list_index++;
continue;
}
name = (char *)hc_names[list_index][0];
if (strstr((char *)hc_names[list_index][1],"func")==(char *)hc_names[list_index][1])
isfunc = 1;
else
isfunc = 0;
list_index++;
if (strncmp (name, text, len) == 0)
{
char *ret;
if (!isfunc)
ret = malloc(strlen(name)+1);
else
ret = malloc(strlen(name)+2);
if (!ret)
mem_error();
strcpy(ret,name);
if (isfunc)
strcat(ret,"(");
return ret;
}
}
while (var_tmp)
{
if (var_tmp->name)
{
name = var_tmp->name;
if (var_tmp->type == HC_USR_FUNC)
isfunc = 1;
else
isfunc = 0;
if (var_tmp->next)
var_tmp = var_tmp->next;
else
var_tmp = NULL;
if (strncmp (name, text, len) == 0)
{
char *ret;
if (!isfunc)
ret = malloc(strlen(name)+1);
else
ret = malloc(strlen(name)+2);
if (!ret)
mem_error();
strcpy(ret,name);
if (isfunc)
strcat(ret,"(");
return ret;
}
} else {
var_tmp = NULL;
break;
}
}
/* If no names matched, then return NULL. */
return ((char *)NULL);
}