1 /*-
2 * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * Project homepage:
26 * https://amath.innolan.net
27 *
28 */
29
30 #include "amath.h"
31 #include "amathc.h"
32 #include "console.h"
33 #include "program.h"
34 #include "loc/text.h"
35
36 static const char *version = TXTVERSMSG;
37 static const char *compiler = TXTCOMPMSG;
38 static const char *about = NEWLINE NEWLINE
39 "amath features a case sensitive command line interface, internal IEEE 754" NEWLINE
40 "calculations with 15 significant digits, calculations with real and complex" NEWLINE
41 "numbers, variables and user defined functions, logarithmic and exponential" NEWLINE
42 "functions, trigonometric and hyperbolic function and selected mathematical" NEWLINE
43 "constants and rounding functions." NEWLINE;
44 static const char *help =
45 "usage: amath [ --noansi ] [ --shell | expression ]" NEWLINE;
46 static const char *copyright =
47 "Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>";
48 static const char *license =
49 "Copyright (c) 2007 The NetBSD Foundation, Inc." NEWLINE
50 "Copyright (c) 1990, 1993 The Regents of the University of California." NEWLINE
51 "All rights reserved." NEWLINE NEWLINE
52 "This code is derived from software written by Stephen L. Moshier." NEWLINE
53 "It is redistributed by the NetBSD Foundation by permission of the author." NEWLINE NEWLINE
54 "This code is derived from software contributed to Berkeley by" NEWLINE
55 "Mike Hibler and Chris Torek." NEWLINE NEWLINE
56 "Redistribution and use in source and binary forms, with or without" NEWLINE
57 "modification, are permitted provided that the following conditions are met:" NEWLINE NEWLINE
58 "* Redistributions of source code must retain the above copyright notice, this" NEWLINE
59 " list of conditions and the following disclaimer." NEWLINE NEWLINE
60 "* Redistributions in binary form must reproduce the above copyright notice," NEWLINE
61 " this list of conditions and the following disclaimer in the documentation" NEWLINE
62 " and/or other materials provided with the distribution." NEWLINE NEWLINE
63 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"" NEWLINE
64 "AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE" NEWLINE
65 "IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE" NEWLINE
66 "DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE" NEWLINE
67 "FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL" NEWLINE
68 "DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR" NEWLINE
69 "SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER" NEWLINE
70 "CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY," NEWLINE
71 "OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE" NEWLINE
72 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." NEWLINE NEWLINE;
73 static const char *footer = "Specific details should be found in the source files.";
74
ConsoleBase(const char * prompt)75 ConsoleBase::ConsoleBase(const char* prompt)
76 {
77 AllocAndCopy(&this->prompt, prompt);
78 }
79
~ConsoleBase()80 ConsoleBase::~ConsoleBase()
81 {
82 delete prompt;
83 }
84
GetName()85 const char* ConsoleBase::GetName()
86 {
87 return CPROCNAME;
88 }
89
GetVersionText()90 const char * ConsoleBase::GetVersionText()
91 {
92 return version;
93 }
94
GetCompilerText()95 const char *ConsoleBase::GetCompilerText()
96 {
97 return compiler;
98 }
99
Open()100 bool ConsoleBase::Open()
101 {
102 return true;
103 }
104
Close()105 void ConsoleBase::Close()
106 {
107 }
108
SetAnsiMode(bool value)109 bool ConsoleBase::SetAnsiMode(bool value)
110 {
111 ansiMode = value;
112 return true;
113 }
114
ResetConsole()115 void ConsoleBase::ResetConsole()
116 {
117 if (ansiMode)
118 {
119 static const char *normal = "\x1B[0m";
120 WriteString(normal);
121 }
122 }
123
AnsiBold()124 void ConsoleBase::AnsiBold()
125 {
126 if (ansiMode)
127 {
128 static const char *bold = "\x1B[1m";
129 WriteString(bold);
130 }
131 }
132
AnsiItalics()133 void ConsoleBase::AnsiItalics()
134 {
135 if (ansiMode)
136 {
137 static const char *italics = "\x1B[3m";
138 WriteString(italics);
139 }
140 }
141
Clear()142 void ConsoleBase::Clear()
143 {
144 if (ansiMode)
145 {
146 static const char *clear = "\x1B[1;1H\x1B[J";
147 WriteString(clear);
148 ResetConsole();
149 }
150 else
151 {
152 static const char *msg = "Screen can only be cleared in ANSI mode.";
153 WriteString(msg);
154 WriteString(NEWLINE);
155 }
156 }
157
ShowHelp()158 void ConsoleBase::ShowHelp()
159 {
160 WriteString(help);
161 }
162
ShowVersion()163 void ConsoleBase::ShowVersion()
164 {
165 AnsiBold();
166 WriteString(version);
167 ResetConsole();
168 WriteString(NEWLINE);
169 WriteString(compiler);
170 WriteString(NEWLINE);
171 ResetConsole();
172 }
173
ShowAbout()174 void ConsoleBase::ShowAbout()
175 {
176 AnsiBold();
177 WriteString(version);
178 ResetConsole();
179 WriteString(NEWLINE);
180 WriteString(compiler);
181 ResetConsole();
182 WriteString(about);
183 ResetConsole();
184 }
185
ShowLicense()186 void ConsoleBase::ShowLicense()
187 {
188 WriteString(NEWLINE);
189 AnsiBold();
190 WriteString(copyright);
191 ResetConsole();
192 WriteString(NEWLINE);
193 WriteString(license);
194 AnsiItalics();
195 WriteString(footer);
196 ResetConsole();
197 WriteString(NEWLINE);
198 WriteString(NEWLINE);
199 }
200
StartMessage()201 void ConsoleBase::StartMessage()
202 {
203 WriteString(INTROMSG);
204 ResetConsole();
205 }
206
Prompt()207 void ConsoleBase::Prompt()
208 {
209 ResetConsole();
210 WriteString(prompt);
211 ResetConsole();
212 }
213
SetPrompt(const char * string)214 void ConsoleBase::SetPrompt(const char* string)
215 {
216 delete prompt;
217 AllocAndCopy(&prompt, string);
218 }
219