1 /*
2    Copyright (C) 1999 T. Scott Dattalo
3 
4 This file is part of gpsim.
5 
6 gpsim is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 gpsim is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with gpsim; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20 
21 
22 #include <iostream>
23 #include <iomanip>
24 #include <string>
25 
26 #include "command.h"
27 #include "cmd_module.h"
28 
29 #include "../src/pic-processor.h"
30 #include "../src/modules.h"
31 #include "../src/symbol.h"
32 #include "../src/cmd_manager.h"
33 
34 
35 
36 cmd_module c_module;
37 
38 #define CMD_MOD_LIST    1
39 #define CMD_MOD_LOAD    2
40 #define CMD_MOD_DUMP    3
41 #define CMD_MOD_LIB     4
42 #define CMD_MOD_PINS    5
43 
44 
45 static cmd_options cmd_module_options[] =
46 {
47   {"list",      CMD_MOD_LIST,    OPT_TT_BITFLAG},
48   {"load",      CMD_MOD_LOAD,    OPT_TT_STRING},
49   {"dump",      CMD_MOD_DUMP,    OPT_TT_BITFLAG},
50   {"pins",      CMD_MOD_PINS,    OPT_TT_STRING},
51   {"library",   CMD_MOD_LIB ,    OPT_TT_STRING},
52   {"lib",       CMD_MOD_LIB ,    OPT_TT_STRING},
53   {0,0,0}
54 };
55 
56 
cmd_module()57 cmd_module::cmd_module()
58   : command("module","mod")
59 {
60 
61   brief_doc = string("Select & Display modules");
62 
63   long_doc = string (
64     "module [ [load module_type [module_name]] | [lib lib_name] | [list] | \n"
65     "[[dump | pins] module_name] ] \n"
66     "\tIf no options are specified, then the currently defined module(s)\n"
67     "\twill be displayed. This is the same as the `module list' command.\n"
68     "\tThe `module load lib_name' tells gpsim to search for the module\n"
69     "\tlibrary called `lib_name' and to load it. (Note that the format of\n"
70     "\tmodule libraries is exactly the same as a Linux shared library. This\n"
71     "\tmeans that the module library should reside in a path available to\n"
72     "\tdlopen(). Please see the README.MODULES in the gpsim distribution.\n"
73     "\tTo instantiate a new module, then type\n"
74     "\t  module module_type module_name\n"
75     "\twhere module_type refers to a specific module in a module library\n"
76     "\tand module_name is the user name assigned to it.\n"
77     "\tInformation about a module can be displayed by the command\n"
78     "\t  module module_name [dump | pins]\n"
79     "\twhere module_name is the name that you assigned when the module\n"
80     "\twas instantiated. The optional dump and pins identifiers specify\n"
81     "\tthe information you wish to display (dump is the default).\n"
82     "\n"
83     "\tDevelopers of gpsim and developers building libraries for use with\n"
84     "\tgpsim may find it useful to set the GPSIM_MODULE_PATH environment variable\n"
85     "\tto the target folder of the library module that is under development.\n"
86     "\tMultiple folders may be delimited with a ':' for Linux and ';' for\n"
87     "\tWindows.\n"
88     "\n"
89     "\texamples:\n"
90     "\n"
91     "\tmodule                      // Display the modules you've already defined.\n"
92     "\tmodule lib my_mods.so       // Load the module library called my_mods.\n"
93     "\tmodule list                 // Display the list of modules supported.\n"
94     "\tmodule load lcd my_lcd      // Create an instance of an 'lcd'\n"
95     "\tmodule pins my_lcd          // Display the pin states of an instantiated module\n"
96     "\tmodule load lcd lcd2x20     // Create a new module.\n"
97     "\tmodule load pullup R1       // and another.\n"
98 );
99 
100   op = cmd_module_options;
101 }
102 
dumpModules(const SymbolTableEntry_t & st)103 static void dumpModules(const SymbolTableEntry_t &st)
104 {
105   cout << " Module: " << st.first << endl;
106 }
107 
module(void)108 void cmd_module::module(void)
109 {
110 
111   if(verbose)
112     cout << "cmd_module: display modules\n";
113 #ifdef OLD_MODULE_LIBRARY
114   cout << ModuleLibrary::DisplayModuleList();
115 #else
116   globalSymbolTable().ForEachModule(dumpModules);
117 #endif
118 
119 }
120 
module(cmd_options * opt)121 void cmd_module::module(cmd_options *opt)
122 {
123   if(!opt)
124     return;
125 
126   switch(opt->value)
127     {
128 
129     case CMD_MOD_LIST:
130 #ifdef OLD_MODULE_LIBRARY
131       cout << ModuleLibrary::DisplayFileList();
132 #else
133       ModuleLibrary::ListLoadableModules();
134 #endif
135       break;
136 
137     default:
138       cout << "cmd_module error:";
139       if(opt->name)
140         cout << " no parameters with option: " << opt->name;
141       cout << endl;
142     }
143 
144 }
145 
module(cmd_options_str * cos,list<string> * strs)146 void cmd_module::module(cmd_options_str *cos,
147 			list <string> *strs)
148 {
149   //  const int cMAX_PARAMETERS=2;
150   //  int nParameters=cMAX_PARAMETERS;
151   //  guint64 parameters[cMAX_PARAMETERS] = {0,0};
152 
153   //  evaluate(eList, parameters, &nParameters);
154 
155   list <string> :: iterator si;
156   std::string s1;
157   int nStrings = 0;
158 
159   if (strs) {
160     nStrings = strs->size();
161 
162     si = strs->begin();
163     if (nStrings >= 1) {
164       s1 = *si;
165 
166       if (nStrings >= 2) {
167         ++si;
168       }
169     }
170   }
171 
172   // Now choose the specific command based on the input parameters
173 
174   if(nStrings==0)
175     module(cos);
176   else if(nStrings==1)
177     module(cos, s1.c_str());
178   //else if(nParameters==0 && nStrings==2)
179   //  module(cos, (char*)s1.c_str(), (char*)s2.c_str());
180   //else if(nParameters==1 && nStrings==1)
181   //  module(cos, (char*)s1.c_str(), parameters[0]);
182   //else if(nParameters==2 && nStrings==0)
183   //  module(cos, parameters[0], parameters[1]);
184   //else if(nParameters==1 && nStrings==2)
185   //  module(cos, (char*)s1.c_str(), (char*)s2.c_str(), parameters[0]);
186   else
187     cout << "module command error\n";
188 }
189 
190 
module(cmd_options_str * cos)191 void cmd_module::module(cmd_options_str *cos)
192 {
193 
194   if(!cos)
195     return;
196 
197   switch(cos->co->value)
198     {
199     case CMD_MOD_LIB:
200       if(verbose)
201         cout << "module command got the library " << cos->str << endl;
202 
203       try {
204         string fname(cos->str);
205         ModuleLibrary::LoadFile(fname);
206       }
207       catch(Error &pError) {
208         std::cout << pError.what();
209       }
210 
211       break;
212     case CMD_MOD_LOAD:
213       // Load a module from (an already loaded) library and let
214       // gpsim assign the name.
215       if(verbose)
216         cout << "module command got the module " << cos->str << '\n';
217 #ifdef OLD_MODULE_LIBRARY
218       if(ModuleLibrary::NewObject(cos->str) == NULL) {
219         GetUserInterface().DisplayMessage("module type %s not created\n", cos->str);
220       }
221 #else
222       {
223         cout << "Fixme -- module NewObject\n";
224       }
225 #endif
226 
227       break;
228 
229     case CMD_MOD_DUMP:
230       cout <<  " is not supported yet\n";
231       break;
232 
233     case CMD_MOD_PINS:
234       cout << "Fixme: display module pins is not supported...\n";
235       //ModuleLibrary::DisplayModulePins(cos->str);
236       break;
237 
238     default:
239       cout << "cmd_module error\n";
240     }
241 
242 }
243 
244 
module(cmd_options_str * cos,const char * op1)245 void  cmd_module::module(cmd_options_str *cos, const char *op1)
246 {
247 
248   switch(cos->co->value)
249     {
250 
251     case CMD_MOD_LOAD:
252       // Load a module from (an already loaded) library
253 #ifdef OLD_MODULE_LIBRARY
254       if(ModuleLibrary::NewObject(cos->str,  op1) == NULL) {
255         GetUserInterface().DisplayMessage("module type %s not created\n", cos->str);
256       }
257 #else
258       {
259         string mName(cos->str);
260         string refDes(op1);
261         if(!ModuleLibrary::InstantiateObject(mName,refDes))
262           GetUserInterface().DisplayMessage("module type %s not created\n", cos->str);
263       }
264 #endif
265 
266       break;
267 
268     default:
269       cout << "Warning, ignoring module command\n";
270     }
271 }
272 
273