amath  1.8.5
Simple command line calculator
program_amiga.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_amiga.h"
35 #include "console_amiga.h"
36 #include "window_amiga.h"
37 #include "language_amiga.h"
38 #include "filesystem_amiga.h"
39 #include "preferences_amiga.h"
40 #include "lib/charbuf.h"
41 #include "main/evaluator.h"
42 
43 #if defined(AMIGA)
44 #define ARGS_FORMAT "SHELL/S,NOANSI/S,INPUT/F"
45 #include <clib/dos_protos.h>
46 
47 #if defined(AOS3)
48 # define RDPTR LONG*
49 #else
50 # define RDPTR IPTR*
51 #endif
52 
53 AmigaProgram::AmigaProgram()
54  : Program()
55 {
56  rdargs = nullptr;
57  args.shell = FALSE;
58  args.noansi = FALSE;
59  args.input = nullptr;
60  Console = nullptr;
61 }
62 
63 AmigaProgram::~AmigaProgram()
64 {
65  if (Console != nullptr) {
66  Console->Close();
67  delete Console;
68  }
69 
70  if (rdargs != nullptr) {
71  FreeArgs(rdargs);
72  }
73 }
74 
75 void AmigaProgram::Initialize(int argc, char **argv)
76 {
77  rdargs = (RDArgs*)ReadArgs((const char*)ARGS_FORMAT, (RDPTR)&args, 0);
78  if (!rdargs)
79  {
80  PrintFault(IoErr(), (STRPTR)argv[0]);
81  return;
82  }
83 
84  shellMode = args.shell ? true : false;
85  ansiMode = args.noansi ? false : true;
86 
87  if (shellMode || args.input != nullptr)
88  {
89  Console = new AmigaShellConsole(Preferences->GetPrompt());
90  }
91  else
92  {
93  Console = new AmigaWindow(Preferences->GetPrompt(), Language);
94  }
95 
96  InitAnsiMode();
97 }
98 
99 void AmigaProgram::Start()
100 {
101  if(Console == nullptr || !Console->Open())
102  {
103  return;
104  }
105 
106  Preferences->Load();
107 
108  if (args.input != nullptr)
109  {
110  Evaluator *evaluator = new Evaluator(args.input);
111  evaluator->Evaluate();
112  const char *res = evaluator->GetResult();
113  Console->WriteString(res);
114  Console->ResetConsole();
115  delete evaluator;
116  return;
117  }
118 
119  Console->Start();
120  return;
121 }
122 
123 #endif