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