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