amath  1.8.5
Simple command line calculator
console.cpp
Go to the documentation of this file.
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 
75 ConsoleBase::ConsoleBase(const char* prompt)
76 {
77  AllocAndCopy(&this->prompt, prompt);
78 }
79 
81 {
82  delete prompt;
83 }
84 
85 const char* ConsoleBase::GetName()
86 {
87  return CPROCNAME;
88 }
89 
90 const char * ConsoleBase::GetVersionText()
91 {
92  return version;
93 }
94 
96 {
97  return compiler;
98 }
99 
101 {
102  return true;
103 }
104 
106 {
107 }
108 
109 bool ConsoleBase::SetAnsiMode(bool value)
110 {
111  ansiMode = value;
112  return true;
113 }
114 
116 {
117  if (ansiMode)
118  {
119  static const char *normal = "\x1B[0m";
120  WriteString(normal);
121  }
122 }
123 
125 {
126  if (ansiMode)
127  {
128  static const char *bold = "\x1B[1m";
129  WriteString(bold);
130  }
131 }
132 
134 {
135  if (ansiMode)
136  {
137  static const char *italics = "\x1B[3m";
138  WriteString(italics);
139  }
140 }
141 
143 {
144  if (ansiMode)
145  {
146  static const char *clear = "\x1B[1;1H\x1B[J";
147  WriteString(clear);
149  }
150  else
151  {
152  static const char *msg = "Screen can only be cleared in ANSI mode.";
153  WriteString(msg);
155  }
156 }
157 
159 {
161 }
162 
164 {
165  AnsiBold();
172 }
173 
175 {
176  AnsiBold();
184 }
185 
187 {
189  AnsiBold();
199 }
200 
202 {
205 }
206 
208 {
212 }
213 
214 void ConsoleBase::SetPrompt(const char* string)
215 {
216  delete prompt;
217  AllocAndCopy(&prompt, string);
218 }
#define TXTCOMPMSG
Definition: amath.h:291
#define NEWLINE
Definition: amath.h:222
#define INTROMSG
Definition: text.h:47
static const char * license
Definition: console.cpp:48
char * prompt
Definition: console.h:68
virtual void Clear()
Definition: console.cpp:142
const char * GetName()
Definition: console.cpp:85
virtual void ShowAbout()
Definition: console.cpp:174
ConsoleBase(const char *prompt)
Definition: console.cpp:75
Abstract base class encapsulating console logic.
Definition: console.h:43
void AnsiBold()
Definition: console.cpp:124
const char * GetVersionText()
Definition: console.cpp:90
virtual bool SetAnsiMode(bool value)
Definition: console.cpp:109
#define TXTVERSMSG
Definition: amath.h:290
virtual void Close()
Definition: console.cpp:105
static const char * about
Definition: console.cpp:38
virtual void ResetConsole()
Definition: console.cpp:115
void Prompt()
Definition: console.cpp:207
static const char * copyright
Definition: console.cpp:46
virtual void WriteString(const char *string)=0
virtual ~ConsoleBase()
Definition: console.cpp:80
static const char * compiler
Definition: console.cpp:37
static const char * version
Definition: console.cpp:36
static const char * help
Definition: console.cpp:44
virtual void ShowHelp()
Definition: console.cpp:158
const char * GetCompilerText()
Definition: console.cpp:95
static const char * footer
Definition: console.cpp:73
virtual void SetPrompt(const char *string)
Definition: console.cpp:214
virtual void ShowLicense()
Definition: console.cpp:186
virtual bool Open()
Definition: console.cpp:100
virtual void StartMessage()
Definition: console.cpp:201
unsigned int AllocAndCopy(char **destination, const char *source)
Allocate memory and copy a string into the array.
Definition: alloccpy.c:40
#define CPROCNAME
Definition: amath.h:293
bool ansiMode
Definition: console.h:69
void AnsiItalics()
Definition: console.cpp:133
virtual void ShowVersion()
Definition: console.cpp:163