1 /* Copyright 2007-2008, Robert Nelson <robertn@the-nelsons.org>
\r
2 * Released under terms of the GNU General Public License as
\r
3 * required by the license on 'mtxl.c'.
\r
4 * $Date: 2007-01-28 19:23:33 -0800 (Sun, 28 Jan 2007) $
\r
8 /* This is a generic SCSI device control program. It operates by
\r
9 * directly sending commands to the device.
\r
14 * load -- Load medium
\r
15 * unload -- Unload medium
\r
16 * start -- Start device
\r
17 * stop -- Stop device
\r
18 * lock -- Lock medium
\r
19 * unlock -- Unlock medium
\r
32 #if HAVE_SYS_TYPES_H
\r
33 #include <sys/types.h>
\r
42 /* the device handle we're operating upon. */
\r
43 static char *device; /* the device name. */
\r
44 static DEVICE_TYPE DeviceFD = (DEVICE_TYPE) -1;
\r
46 static int S_load(void);
\r
47 static int S_unload(void);
\r
48 static int S_start(void);
\r
49 static int S_stop(void);
\r
50 static int S_lock(void);
\r
51 static int S_unlock(void);
\r
53 struct command_table_struct
\r
56 int (*command)(void);
\r
61 { "unload", S_unload },
\r
62 { "start", S_start },
\r
65 { "unlock", S_unlock },
\r
66 { NULL, NULL } /* terminate list */
\r
71 FatalError("Usage: scsieject -f <generic-device> <command> where <command> is:\n load | unload | start | stop | lock | unlock\n");
\r
74 /* open_device() -- set the 'DeviceFD' variable.... */
\r
75 void open_device(void)
\r
79 SCSI_CloseDevice("Unknown", DeviceFD);
\r
82 DeviceFD = SCSI_OpenDevice(device);
\r
85 /* we see if we've got a file open. If not, we open one :-(. Then
\r
86 * we execute the actual command. Or not :-(.
\r
88 int execute_command(struct command_table_struct *command)
\r
91 * If the device is not already open, then open it from the
\r
96 /* try to get it from STAPE or TAPE environment variable... */
\r
97 if ((device = getenv("STAPE")) == NULL &&
\r
98 (device = getenv("TAPE")) == NULL)
\r
100 Usage(); /* Doesn't return */
\r
106 /* okay, now to execute the command... */
\r
107 return command->command();
\r
112 * Basically, we are parsing argv/argc. We can have multiple commands
\r
113 * on a line, such as "load start" to load a tape and start the device.
\r
114 * We execute these commands one at a time as we come to them. If we don't
\r
115 * have a -f at the start and the default device isn't defined in a TAPE or
\r
116 * STAPE environment variable, we exit.
\r
119 int parse_args(int argc, char **argv)
\r
122 struct command_table_struct *command;
\r
126 for (index = 1; index < argc; index++)
\r
128 if (strcmp(argv[index], "-f") == 0)
\r
133 Usage(); /* Doesn't return */
\r
135 device = argv[index];
\r
140 for (command = &command_table[0]; command->name != NULL; command++)
\r
142 if (strcmp(command->name, argv[index]) == 0)
\r
148 if (command->name == NULL)
\r
150 Usage(); /* Doesn't return */
\r
153 retval = execute_command(command);
\r
157 /* Command failed, we probably shouldn't continue */
\r
168 int result = LoadUnload(DeviceFD, 1);
\r
172 fputs("scsieject: load failed\n", stderr);
\r
181 int result = LoadUnload(DeviceFD, 0);
\r
185 fputs("scsieject: unload failed\n", stderr);
\r
194 int result = StartStop(DeviceFD, 1);
\r
198 fputs("scsieject: start failed\n", stderr);
\r
207 int result = StartStop(DeviceFD, 0);
\r
211 fputs("scsieject: stop failed\n", stderr);
\r
220 int result = LockUnlock(DeviceFD, 1);
\r
224 fputs("scsieject: lock failed\n", stderr);
\r
233 int result = LockUnlock(DeviceFD, 0);
\r
237 fputs("scsieject: unlock failed\n", stderr);
\r
244 /* See parse_args for the scoop. parse_args does all. */
\r
245 int main(int argc, char **argv)
\r
247 parse_args(argc, argv);
\r
251 SCSI_CloseDevice(device, DeviceFD);
\r