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