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 "program_stdc.h"
33 #include "console_stdc.h"
34 #include "console_termios.h"
35 #include "console_windows.h"
36 #include "preferences_stdc.h"
37 #include "lib/charbuf.h"
38 #include "main/evaluator.h"
39 
40 #if !defined(AMIGA) && !defined(HAIKU)
41 
StandardProgram()42 StandardProgram::StandardProgram()
43     : Program()
44 {
45     Console = nullptr;
46     line = nullptr;
47     shellMode = false;
48 }
49 
~StandardProgram()50 StandardProgram::~StandardProgram()
51 {
52     if (Console != nullptr)
53     {
54         Console->Close();
55         delete Console;
56     }
57 
58     if (line != nullptr)
59     {
60         delete line;
61     }
62 }
63 
Initialize(int argc,char ** argv)64 void StandardProgram::Initialize(int argc, char** argv)
65 {
66 #if defined(WINDOWS)
67         Console = new WindowsConsole(Preferences->GetPrompt(), Language);
68 #elif defined(TERMIOS)
69         Console = new TermiosConsole(Preferences->GetPrompt(), Language);
70 #else
71         Console = new StandardConsole(Preferences->GetPrompt(), Language);
72 #endif
73     SetAnsiMode(true);
74 
75     line = new CharBuffer();
76     line->Empty();
77 
78     bool options = true;
79     unsigned int len = 1;
80     for (int i = 1; i < argc; i++)
81     {
82         if (options)
83         {
84             if (StrIsEqual(argv[i], "noansi") || StrIsEqual(argv[i], "--noansi"))
85             {
86                 SetAnsiMode(false);
87                 continue;
88             }
89             else if (StrIsEqual(argv[i], "shell") || StrIsEqual(argv[i], "--shell"))
90             {
91                 shellMode = true;
92             }
93             else
94             {
95                 options = false;
96             }
97         }
98 
99         if (!options)
100         {
101             len += StrLen(argv[i]) + 1;
102             line->EnsureSize(len);
103             line->Append(argv[i]);
104             line->Append(' ');
105         }
106     }
107 
108     if (len > 1)
109     {
110         line->DeleteLastChar();
111     }
112 }
113 
Start()114 void StandardProgram::Start()
115 {
116     if(Console == nullptr || !Console->Open())
117     {
118         return;
119     }
120 
121     Preferences->Load();
122 
123     if (!line->IsEmpty())
124     {
125         Evaluator* evaluator = new Evaluator(line->GetString());
126         evaluator->Evaluate();
127         const char* res = evaluator->GetResult();
128         Console->WriteString(res);
129         Console->ResetConsole();
130         delete evaluator;
131         return;
132     }
133 
134 #if defined(WINDOWS)
135     Console->Start();
136 #else
137     if (shellMode)
138     {
139         Console->Start();
140         return;
141     }
142 
143     SetAnsiMode(false);
144     Console->ShowHelp();
145 #endif
146 }
147 
148 #endif
149