amath  1.8.5
Simple command line calculator
console_termios.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 "console_termios.h"
34 #include "lib/charval.h"
35 #include "lib/aengine.h"
36 #include "main/evaluator.h"
37 
38 #if defined(TERMIOS)
39 #include <stdio.h>
40 #include <termios.h>
41 
42 TermiosConsole::TermiosConsole(const char* prompt, CharValidator* validator) :
43  ConsoleBase(prompt), line(nullptr), exit(false)
44 {
45  proc = new AnsiConoleEngine(prompt, validator);
46 }
47 
48 TermiosConsole::~TermiosConsole()
49 {
50  delete proc;
51 }
52 
53 bool TermiosConsole::Open()
54 {
55  if (tcgetattr(STDIN_FILENO, &oldAttr) != 0)
56  {
57  termError = true;
58  return false;
59  }
60 
61  newAttr = oldAttr;
62  newAttr.c_lflag &=(~ICANON & ~ECHO);
63  newAttr.c_cc[VMIN] = 1;
64  newAttr.c_cc[VTIME] = 0;
65 
66  return (tcsetattr(STDIN_FILENO, TCSANOW, &newAttr) != -1);
67 }
68 
69 void TermiosConsole::Close()
70 {
71  if (!termError)
72  {
73  tcsetattr(STDIN_FILENO, TCSANOW, &oldAttr);
74  }
75 }
76 
77 void TermiosConsole::Start()
78 {
79  exit = false;
81 
82  while (!exit)
83  {
84  Prompt();
85  ReadLine();
86  Evaluator* evaluator = new Evaluator(line);
87  evaluator->Evaluate();
88  const char* res = evaluator->GetResult();
89  Write(res, StrLen(res));
90  delete evaluator;
91  }
92 }
93 
94 void TermiosConsole::Exit()
95 {
96  exit = true;
97 }
98 
99 void TermiosConsole::ReadLine()
100 {
101  char c;
102  proc->StartInput();
103  while (!proc->InputDone())
104  {
105  ssize_t res = read(STDIN_FILENO, &c, sizeof(char));
106  if (res != 1)
107  {
108  break;
109  }
110 
111  const char* out = proc->ProcessChar(c);
112  WriteString(out);
114  }
115 
116  line = proc->GetLine();
117 }
118 
119 void TermiosConsole::WriteString(const char* string)
120 {
121  Write(string, StrLen(string));
122 }
123 
124 void TermiosConsole::Write(const char* string, unsigned int length)
125 {
126  ssize_t res = write(STDOUT_FILENO, string, length);
127  if (res != (ssize_t)length)
128  {
129  exit = true;
130  }
131 }
132 
133 void TermiosConsole::SetPrompt(const char* string)
134 {
136  proc->SetPrompt(string);
137 }
138 
139 #endif
#define TERMIOS
Definition: amath.h:89
bool InputDone() const
Definition: aengine.cpp:380
char * GetResult() const
Definition: evaluator.cpp:91
ConsoleBase(const char *prompt)
Definition: console.cpp:75
Abstract base class encapsulating console logic.
Definition: console.h:43
const char * GetLine() const
Definition: aengine.cpp:385
ANSI console controller.
Definition: aengine.h:47
AnsiConoleEngine(const char *prompt, CharValidator *validator)
Definition: aengine.cpp:44
virtual void ResetConsole()
Definition: console.cpp:115
void Prompt()
Definition: console.cpp:207
Evaluator(const char *input)
Definition: evaluator.cpp:36
void StartInput()
Definition: aengine.cpp:89
virtual void SetPrompt(const char *string)
Definition: console.cpp:214
void SetPrompt(const char *string)
Definition: aengine.cpp:390
int StrLen(const char *string)
Get the length of a null terminated string.
Definition: strlen.c:34
const char * ProcessChar(const unsigned char character)
Definition: aengine.cpp:104
void Evaluate() const
Definition: evaluator.cpp:50
virtual void StartMessage()
Definition: console.cpp:201