openMSX
NowindCommand.cc
Go to the documentation of this file.
1 #include "NowindCommand.hh"
2 #include "NowindRomDisk.hh"
3 #include "NowindInterface.hh"
4 #include "DiskChanger.hh"
5 #include "DSKDiskImage.hh"
6 #include "DiskPartition.hh"
7 #include "FileContext.hh"
8 #include "StringOp.hh"
9 #include "FileOperations.hh"
10 #include "CommandException.hh"
11 #include "TclObject.hh"
12 #include "span.hh"
13 #include "unreachable.hh"
14 #include <cassert>
15 #include <memory>
16 
17 using std::string;
18 using std::string_view;
19 using std::unique_ptr;
20 using std::vector;
21 
22 namespace openmsx {
23 
24 NowindCommand::NowindCommand(const string& basename,
25  CommandController& commandController_,
26  NowindInterface& interface_)
27  : Command(commandController_, basename)
28  , interface(interface_)
29 {
30 }
31 
32 unique_ptr<DiskChanger> NowindCommand::createDiskChanger(
33  const string& basename, unsigned n, MSXMotherBoard& motherBoard) const
34 {
35  return std::make_unique<DiskChanger>(
36  motherBoard,
37  strCat(basename, n + 1),
38  false, true);
39 }
40 
41 unsigned NowindCommand::searchRomdisk(const NowindHost::Drives& drives) const
42 {
43  for (size_t i = 0; i < drives.size(); ++i) {
44  if (drives[i]->isRomdisk()) {
45  return i;
46  }
47  }
48  return 255;
49 }
50 
51 void NowindCommand::processHdimage(
52  string_view hdimage, NowindHost::Drives& drives) const
53 {
54  MSXMotherBoard& motherboard = interface.getMotherBoard();
55 
56  // Possible formats are:
57  // <filename> or <filename>:<range>
58  // Though <filename> itself can contain ':' characters. To solve this
59  // disambiguity we will always interpret the string as <filename> if
60  // it is an existing filename.
61  vector<unsigned> partitions;
62  auto pos = hdimage.find_last_of(':');
63  if ((pos != string::npos) && !FileOperations::exists(hdimage)) {
64  partitions = StringOp::parseRange(
65  hdimage.substr(pos + 1), 1, 31);
66  }
67 
68  auto wholeDisk = std::make_shared<DSKDiskImage>(Filename(string(hdimage)));
69  bool failOnError = true;
70  if (partitions.empty()) {
71  // insert all partitions
72  failOnError = false;
73  for (unsigned i = 1; i <= 31; ++i) {
74  partitions.push_back(i);
75  }
76  }
77 
78  for (auto& p : partitions) {
79  try {
80  // Explicit conversion to shared_ptr<SectorAccessibleDisk> is
81  // for some reason needed in 32-bit vs2013 build (not in 64-bit
82  // and not in vs2012, nor gcc/clang). Compiler bug???
83  auto partition = std::make_unique<DiskPartition>(
84  *wholeDisk, p,
85  std::shared_ptr<SectorAccessibleDisk>(wholeDisk));
86  auto drive = createDiskChanger(
87  interface.basename, unsigned(drives.size()),
88  motherboard);
89  drive->changeDisk(unique_ptr<Disk>(std::move(partition)));
90  drives.push_back(std::move(drive));
91  } catch (MSXException&) {
92  if (failOnError) throw;
93  }
94  }
95 }
96 
98 {
99  auto& host = interface.host;
100  auto& drives = interface.drives;
101  unsigned oldRomdisk = searchRomdisk(drives);
102 
103  if (tokens.size() == 1) {
104  // no arguments, show general status
105  assert(!drives.empty());
106  string r;
107  for (size_t i = 0; i < drives.size(); ++i) {
108  strAppend(r, "nowind", i + 1, ": ");
109  if (dynamic_cast<NowindRomDisk*>(drives[i].get())) {
110  strAppend(r, "romdisk\n");
111  } else if (auto changer = dynamic_cast<DiskChanger*>(
112  drives[i].get())) {
113  string filename = changer->getDiskName().getOriginal();
114  strAppend(r, (filename.empty() ? "--empty--" : filename),
115  '\n');
116  } else {
117  UNREACHABLE;
118  }
119  }
120  strAppend(r, "phantom drives: ",
121  (host.getEnablePhantomDrives() ? "enabled" : "disabled"),
122  "\n"
123  "allow other diskroms: ",
124  (host.getAllowOtherDiskroms() ? "yes" : "no"),
125  '\n');
126  result = r;
127  return;
128  }
129 
130  // first parse complete commandline and store state in these local vars
131  bool enablePhantom = false;
132  bool disablePhantom = false;
133  bool allowOther = false;
134  bool disallowOther = false;
135  bool changeDrives = false;
136  unsigned romdisk = 255;
137  NowindHost::Drives tmpDrives;
138  string error;
139 
140  // actually parse the commandline
141  span<const TclObject> args(&tokens[1], tokens.size() - 1);
142  while (error.empty() && !args.empty()) {
143  bool createDrive = false;
144  string_view image;
145 
146  string_view arg = args.front().getString();
147  args = args.subspan(1);
148  if ((arg == "--ctrl") || (arg == "-c")) {
149  enablePhantom = false;
150  disablePhantom = true;
151  } else if ((arg == "--no-ctrl") || (arg == "-C")) {
152  enablePhantom = true;
153  disablePhantom = false;
154  } else if ((arg == "--allow") || (arg == "-a")) {
155  allowOther = true;
156  disallowOther = false;
157  } else if ((arg == "--no-allow") || (arg == "-A")) {
158  allowOther = false;
159  disallowOther = true;
160 
161  } else if ((arg == "--romdisk") || (arg == "-j")) {
162  if (romdisk != 255) {
163  error = "Can only have one romdisk";
164  } else {
165  romdisk = unsigned(tmpDrives.size());
166  tmpDrives.push_back(std::make_unique<NowindRomDisk>());
167  changeDrives = true;
168  }
169 
170  } else if ((arg == "--image") || (arg == "-i")) {
171  if (args.empty()) {
172  error = strCat("Missing argument for option: ", arg);
173  } else {
174  image = args.front().getString();
175  args = args.subspan(1);
176  createDrive = true;
177  }
178 
179  } else if ((arg == "--hdimage") || (arg == "-m")) {
180  if (args.empty()) {
181  error = strCat("Missing argument for option: ", arg);
182  } else {
183  try {
184  string_view hdimage = args.front().getString();
185  args = args.subspan(1);
186  processHdimage(hdimage, tmpDrives);
187  changeDrives = true;
188  } catch (MSXException& e) {
189  error = std::move(e).getMessage();
190  }
191  }
192 
193  } else {
194  // everything else is interpreted as an image name
195  image = arg;
196  createDrive = true;
197  }
198 
199  if (createDrive) {
200  auto drive = createDiskChanger(
201  interface.basename, unsigned(tmpDrives.size()),
202  interface.getMotherBoard());
203  changeDrives = true;
204  if (!image.empty()) {
205  if (drive->insertDisk(image)) {
206  error = strCat("Invalid disk image: ", image);
207  }
208  }
209  tmpDrives.push_back(std::move(drive));
210  }
211  }
212  if (tmpDrives.size() > 8) {
213  error = "Can't have more than 8 drives";
214  }
215 
216  // if there was no error, apply the changes
217  bool optionsChanged = false;
218  if (error.empty()) {
219  if (enablePhantom && !host.getEnablePhantomDrives()) {
220  host.setEnablePhantomDrives(true);
221  optionsChanged = true;
222  }
223  if (disablePhantom && host.getEnablePhantomDrives()) {
224  host.setEnablePhantomDrives(false);
225  optionsChanged = true;
226  }
227  if (allowOther && !host.getAllowOtherDiskroms()) {
228  host.setAllowOtherDiskroms(true);
229  optionsChanged = true;
230  }
231  if (disallowOther && host.getAllowOtherDiskroms()) {
232  host.setAllowOtherDiskroms(false);
233  optionsChanged = true;
234  }
235  if (changeDrives) {
236  std::swap(tmpDrives, drives);
237  }
238  }
239 
240  // cleanup tmpDrives, this contains either
241  // - the old drives (when command was successful)
242  // - the new drives (when there was an error)
243  auto prevSize = tmpDrives.size();
244  tmpDrives.clear();
245  for (auto& d : drives) {
246  if (auto disk = dynamic_cast<DiskChanger*>(d.get())) {
247  disk->createCommand();
248  }
249  }
250 
251  if (!error.empty()) {
252  throw CommandException(error);
253  }
254 
255  // calculate result string
256  string r;
257  if (changeDrives && (prevSize != drives.size())) {
258  r += "Number of drives changed. ";
259  }
260  if (changeDrives && (romdisk != oldRomdisk)) {
261  if (oldRomdisk == 255) {
262  r += "Romdisk added. ";
263  } else if (romdisk == 255) {
264  r += "Romdisk removed. ";
265  } else {
266  r += "Romdisk changed position. ";
267  }
268  }
269  if (optionsChanged) {
270  r += "Boot options changed. ";
271  }
272  if (!r.empty()) {
273  r += "You may need to reset the MSX for the changes to take effect.";
274  }
275  result = r;
276 }
277 
278 string NowindCommand::help(const vector<string>& /*tokens*/) const
279 {
280  return "Similar to the disk<x> commands there is a nowind<x> command "
281  "for each nowind interface. This command is modeled after the "
282  "'usbhost' command of the real nowind interface. Though only a "
283  "subset of the options is supported. Here's a short overview.\n"
284  "\n"
285  "Command line options\n"
286  " long short explanation\n"
287  "--image -i specify disk image\n"
288  "--hdimage -m specify harddisk image\n"
289  "--romdisk -j enable romdisk\n"
290  // "--flash -f update firmware\n"
291  "--ctrl -c no phantom disks\n"
292  "--no-ctrl -C enable phantom disks\n"
293  "--allow -a allow other diskroms to initialize\n"
294  "--no-allow -A don't allow other diskroms to initialize\n"
295  //"--dsk2rom -z converts a 360kB disk to romdisk.bin\n"
296  //"--debug -d enable libnowind debug info\n"
297  //"--test -t testmode\n"
298  //"--help -h help message\n"
299  "\n"
300  "If you don't pass any arguments to this command, you'll get "
301  "an overview of the current nowind status.\n"
302  "\n"
303  "This command will create a certain amount of drives on the "
304  "nowind interface and (optionally) insert diskimages in those "
305  "drives. For each of these drives there will also be a "
306  "'nowind<1..8>' command created. Those commands are similar to "
307  "e.g. the diska command. They can be used to access the more "
308  "advanced diskimage insertion options. See 'help nowind<1..8>' "
309  "for details.\n"
310  "\n"
311  "In some cases it is needed to reboot the MSX before the "
312  "changes take effect. In those cases you'll get a message "
313  "that warns about this.\n"
314  "\n"
315  "Examples:\n"
316  "nowinda -a image.dsk -j Image.dsk is inserted into drive A: and the romdisk\n"
317  " will be drive B:. Other diskroms will be able to\n"
318  " install drives as well. For example when the MSX has\n"
319  " an internal diskdrive, drive C: en D: will be\n"
320  " available as well.\n"
321  "nowinda disk1.dsk disk2.dsk The two images will be inserted in A: and B:\n"
322  " respectively.\n"
323  "nowinda -m hdimage.dsk Inserts a harddisk image. All available partitions\n"
324  " will be mounted as drives.\n"
325  "nowinda -m hdimage.dsk:1 Inserts the first partition only.\n"
326  "nowinda -m hdimage.dsk:2-4 Inserts the 2nd, 3th and 4th partition as drive A:\n"
327  " B: and C:.\n";
328 }
329 
330 void NowindCommand::tabCompletion(vector<string>& tokens) const
331 {
332  static constexpr const char* const extra[] = {
333  "-c", "--ctrl",
334  "-C", "--no-ctrl",
335  "-a", "--allow",
336  "-A", "--no-allow",
337  "-j", "--romdisk",
338  "-i", "--image",
339  "-m", "--hdimage",
340  };
341  completeFileName(tokens, userFileContext(), extra);
342 }
343 
344 } // namespace openmsx
NowindCommand(const std::string &basename, CommandController &commandController, NowindInterface &interface)
Definition: span.hh:34
vector< unsigned > parseRange(string_view str, unsigned min, unsigned max)
Definition: StringOp.cc:250
void strAppend(std::string &result, Ts &&...ts)
Definition: strCat.hh:644
static void completeFileName(std::vector< std::string > &tokens, const FileContext &context, const RANGE &extra)
Definition: Completer.hh:139
FileContext userFileContext(string_view savePath)
Definition: FileContext.cc:161
constexpr const char *const filename
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
MSXMotherBoard & getMotherBoard() const
Get the mother board this device belongs to.
Definition: MSXDevice.cc:74
void execute(span< const TclObject > tokens, TclObject &result) override
Execute this command.
void tabCompletion(std::vector< std::string > &tokens) const override
Attempt tab completion for this command.
bool exists(string_view filename)
void partition(SectorAccessibleDisk &disk, const std::vector< unsigned > &sizes)
Write a partition table to the given disk and format each partition.
std::vector< std::unique_ptr< DiskContainer > > Drives
Definition: NowindHost.hh:20
std::string help(const std::vector< std::string > &tokens) const override
Print help for this command.
std::string strCat(Ts &&...ts)
Definition: strCat.hh:573
std::unique_ptr< DiskChanger > createDiskChanger(const std::string &basename, unsigned n, MSXMotherBoard &motherBoard) const
#define UNREACHABLE
Definition: unreachable.hh:38