Merge branch 'vendor/GDTOA'
[dragonfly.git] / bin / chio / chio.c
blob574ebf6fdb6340eb8401dca431c8d347e0bee69a
1 /*
2 * Copyright (c) 1996 Jason R. Thorpe <thorpej@and.com>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgements:
15 * This product includes software developed by Jason R. Thorpe
16 * for And Communications, http://www.and.com/
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * @(#) Copyright (c) 1996 Jason R. Thorpe. All rights reserved.
33 * $NetBSD: chio.c,v 1.6 1998/01/04 23:53:58 thorpej Exp $
34 * $FreeBSD: src/bin/chio/chio.c,v 1.15.2.3 2001/07/28 19:22:01 mikeh Exp $
35 * $DragonFly: src/bin/chio/chio.c,v 1.5 2004/08/25 01:13:05 dillon Exp $
38 * Additional Copyright (c) 1997, by Matthew Jacob, for NASA/Ames Research Ctr.
39 * Addidional Copyright (c) 2000, by C. Stephen Gunn, Waterspout Communications
42 #include <sys/chio.h>
43 #include <sys/param.h>
45 #include <err.h>
46 #include <fcntl.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
52 #include "defs.h"
53 #include "pathnames.h"
55 extern char *__progname; /* from crt0.o */
57 static void usage (void);
58 static void cleanup (void);
59 static int parse_element_type (char *);
60 static int parse_element_unit (char *);
61 static const char * element_type_name (int et);
62 static int parse_special (char *);
63 static int is_special (char *);
64 static const char *bits_to_string (ces_status_flags, const char *);
66 static void find_element (char *, u_int16_t *, u_int16_t *);
67 static struct changer_element_status *get_element_status
68 (unsigned int, unsigned int);
70 static int do_move (const char *, int, char **);
71 static int do_exchange (const char *, int, char **);
72 static int do_position (const char *, int, char **);
73 static int do_params (const char *, int, char **);
74 static int do_getpicker (const char *, int, char **);
75 static int do_setpicker (const char *, int, char **);
76 static int do_status (const char *, int, char **);
77 static int do_ielem (const char *, int, char **);
78 static int do_return (const char *, int, char **);
79 static int do_voltag (const char *, int, char **);
81 #ifndef CHET_VT
82 #define CHET_VT 10 /* Completely Arbitrary */
83 #endif
85 /* Valid changer element types. */
86 const struct element_type elements[] = {
87 { "drive", CHET_DT },
88 { "picker", CHET_MT },
89 { "portal", CHET_IE },
90 { "slot", CHET_ST },
91 { "voltag", CHET_VT }, /* Select tapes by barcode */
92 { NULL, 0 },
95 /* Valid commands. */
96 const struct changer_command commands[] = {
97 { "exchange", do_exchange },
98 { "getpicker", do_getpicker },
99 { "ielem", do_ielem },
100 { "move", do_move },
101 { "params", do_params },
102 { "position", do_position },
103 { "setpicker", do_setpicker },
104 { "status", do_status },
105 { "return", do_return },
106 { "voltag", do_voltag },
107 { NULL, 0 },
110 /* Valid special words. */
111 const struct special_word specials[] = {
112 { "inv", SW_INVERT },
113 { "inv1", SW_INVERT1 },
114 { "inv2", SW_INVERT2 },
115 { NULL, 0 },
118 static int changer_fd;
119 static const char *changer_name;
122 main(int argc, char **argv)
124 int ch, i;
126 while ((ch = getopt(argc, argv, "f:")) != -1) {
127 switch (ch) {
128 case 'f':
129 changer_name = optarg;
130 break;
131 default:
132 usage();
135 argc -= optind;
136 argv += optind;
138 if (argc == 0)
139 usage();
141 /* Get the default changer if not already specified. */
142 if (changer_name == NULL)
143 if ((changer_name = getenv(CHANGER_ENV_VAR)) == NULL)
144 changer_name = _PATH_CH;
146 /* Open the changer device. */
147 if ((changer_fd = open(changer_name, O_RDWR, 0600)) == -1)
148 err(1, "%s: open", changer_name);
150 /* Register cleanup function. */
151 if (atexit(cleanup))
152 err(1, "can't register cleanup function");
154 /* Find the specified command. */
155 for (i = 0; commands[i].cc_name != NULL; ++i)
156 if (strcmp(*argv, commands[i].cc_name) == 0)
157 break;
158 if (commands[i].cc_name == NULL) {
159 /* look for abbreviation */
160 for (i = 0; commands[i].cc_name != NULL; ++i)
161 if (strncmp(*argv, commands[i].cc_name,
162 strlen(*argv)) == 0)
163 break;
166 if (commands[i].cc_name == NULL)
167 errx(1, "unknown command: %s", *argv);
169 exit ((*commands[i].cc_handler)(commands[i].cc_name, argc, argv));
170 /* NOTREACHED */
173 static int
174 do_move(const char *cname, int argc, char **argv)
176 struct changer_move cmd;
177 int val;
180 * On a move command, we expect the following:
182 * <from ET> <from EU> <to ET> <to EU> [inv]
184 * where ET == element type and EU == element unit.
187 ++argv; --argc;
189 if (argc < 4) {
190 warnx("%s: too few arguments", cname);
191 goto usage;
192 } else if (argc > 5) {
193 warnx("%s: too many arguments", cname);
194 goto usage;
196 memset(&cmd, 0, sizeof(cmd));
198 /* <from ET> */
199 cmd.cm_fromtype = parse_element_type(*argv);
200 ++argv; --argc;
202 /* Check for voltag virtual type */
203 if (CHET_VT == cmd.cm_fromtype) {
204 find_element(*argv, &cmd.cm_fromtype, &cmd.cm_fromunit);
205 } else {
206 /* <from EU> */
207 cmd.cm_fromunit = parse_element_unit(*argv);
209 ++argv; --argc;
211 /* <to ET> */
212 cmd.cm_totype = parse_element_type(*argv);
213 ++argv; --argc;
215 /* Check for voltag virtual type, and report error */
216 if (CHET_VT == cmd.cm_totype)
217 errx(1,"%s: voltag only makes sense as an element source",
218 cname);
220 /* <to EU> */
221 cmd.cm_tounit = parse_element_unit(*argv);
222 ++argv; --argc;
224 /* Deal with optional command modifier. */
225 if (argc) {
226 val = parse_special(*argv);
227 switch (val) {
228 case SW_INVERT:
229 cmd.cm_flags |= CM_INVERT;
230 break;
232 default:
233 errx(1, "%s: inappropriate modifier `%s'",
234 cname, *argv);
235 /* NOTREACHED */
239 /* Send command to changer. */
240 if (ioctl(changer_fd, CHIOMOVE, &cmd))
241 err(1, "%s: CHIOMOVE", changer_name);
243 return (0);
245 usage:
246 fprintf(stderr, "usage: %s %s "
247 "<from ET> <from EU> <to ET> <to EU> [inv]\n", __progname, cname);
248 return (1);
251 static int
252 do_exchange(const char *cname, int argc, char **argv)
254 struct changer_exchange cmd;
255 int val;
258 * On an exchange command, we expect the following:
260 * <src ET> <src EU> <dst1 ET> <dst1 EU> [<dst2 ET> <dst2 EU>] [inv1] [inv2]
262 * where ET == element type and EU == element unit.
265 ++argv; --argc;
267 if (argc < 4) {
268 warnx("%s: too few arguments", cname);
269 goto usage;
270 } else if (argc > 8) {
271 warnx("%s: too many arguments", cname);
272 goto usage;
274 memset(&cmd, 0, sizeof(cmd));
276 /* <src ET> */
277 cmd.ce_srctype = parse_element_type(*argv);
278 ++argv; --argc;
280 /* Check for voltag virtual type */
281 if (CHET_VT == cmd.ce_srctype) {
282 find_element(*argv, &cmd.ce_srctype, &cmd.ce_srcunit);
283 } else {
284 /* <from EU> */
285 cmd.ce_srcunit = parse_element_unit(*argv);
287 ++argv; --argc;
289 /* <dst1 ET> */
290 cmd.ce_fdsttype = parse_element_type(*argv);
291 ++argv; --argc;
293 /* Check for voltag virtual type */
294 if (CHET_VT == cmd.ce_fdsttype) {
295 find_element(*argv, &cmd.ce_fdsttype, &cmd.ce_fdstunit);
296 } else {
297 /* <from EU> */
298 cmd.ce_fdstunit = parse_element_unit(*argv);
300 ++argv; --argc;
303 * If the next token is a special word or there are no more
304 * arguments, then this is a case of simple exchange.
305 * dst2 == src.
307 if ((argc == 0) || is_special(*argv)) {
308 cmd.ce_sdsttype = cmd.ce_srctype;
309 cmd.ce_sdstunit = cmd.ce_srcunit;
310 goto do_special;
313 /* <dst2 ET> */
314 cmd.ce_sdsttype = parse_element_type(*argv);
315 ++argv; --argc;
317 if (CHET_VT == cmd.ce_sdsttype)
318 errx(1,"%s %s: voltag only makes sense as an element source",
319 cname, *argv);
321 /* <dst2 EU> */
322 cmd.ce_sdstunit = parse_element_unit(*argv);
323 ++argv; --argc;
325 do_special:
326 /* Deal with optional command modifiers. */
327 while (argc) {
328 val = parse_special(*argv);
329 ++argv; --argc;
330 switch (val) {
331 case SW_INVERT1:
332 cmd.ce_flags |= CE_INVERT1;
333 break;
335 case SW_INVERT2:
336 cmd.ce_flags |= CE_INVERT2;
337 break;
339 default:
340 errx(1, "%s: inappropriate modifier `%s'",
341 cname, *argv);
342 /* NOTREACHED */
346 /* Send command to changer. */
347 if (ioctl(changer_fd, CHIOEXCHANGE, &cmd))
348 err(1, "%s: CHIOEXCHANGE", changer_name);
350 return (0);
352 usage:
353 fprintf(stderr,
354 "usage: %s %s <src ET> <src EU> <dst1 ET> <dst1 EU>\n"
355 " [<dst2 ET> <dst2 EU>] [inv1] [inv2]\n",
356 __progname, cname);
357 return (1);
360 static int
361 do_position(const char *cname, int argc, char **argv)
363 struct changer_position cmd;
364 int val;
367 * On a position command, we expect the following:
369 * <to ET> <to EU> [inv]
371 * where ET == element type and EU == element unit.
374 ++argv; --argc;
376 if (argc < 2) {
377 warnx("%s: too few arguments", cname);
378 goto usage;
379 } else if (argc > 3) {
380 warnx("%s: too many arguments", cname);
381 goto usage;
383 memset(&cmd, 0, sizeof(cmd));
385 /* <to ET> */
386 cmd.cp_type = parse_element_type(*argv);
387 ++argv; --argc;
389 /* <to EU> */
390 cmd.cp_unit = parse_element_unit(*argv);
391 ++argv; --argc;
393 /* Deal with optional command modifier. */
394 if (argc) {
395 val = parse_special(*argv);
396 switch (val) {
397 case SW_INVERT:
398 cmd.cp_flags |= CP_INVERT;
399 break;
401 default:
402 errx(1, "%s: inappropriate modifier `%s'",
403 cname, *argv);
404 /* NOTREACHED */
408 /* Send command to changer. */
409 if (ioctl(changer_fd, CHIOPOSITION, &cmd))
410 err(1, "%s: CHIOPOSITION", changer_name);
412 return (0);
414 usage:
415 fprintf(stderr, "usage: %s %s <to ET> <to EU> [inv]\n",
416 __progname, cname);
417 return (1);
420 /* ARGSUSED */
421 static int
422 do_params(const char *cname, int argc, char **argv)
424 struct changer_params data;
425 int picker;
427 /* No arguments to this command. */
429 ++argv; --argc;
431 if (argc) {
432 warnx("%s: no arguments expected", cname);
433 goto usage;
436 /* Get params from changer and display them. */
437 memset(&data, 0, sizeof(data));
438 if (ioctl(changer_fd, CHIOGPARAMS, &data))
439 err(1, "%s: CHIOGPARAMS", changer_name);
441 printf("%s: %d slot%s, %d drive%s, %d picker%s",
442 changer_name,
443 data.cp_nslots, (data.cp_nslots > 1) ? "s" : "",
444 data.cp_ndrives, (data.cp_ndrives > 1) ? "s" : "",
445 data.cp_npickers, (data.cp_npickers > 1) ? "s" : "");
446 if (data.cp_nportals)
447 printf(", %d portal%s", data.cp_nportals,
448 (data.cp_nportals > 1) ? "s" : "");
450 /* Get current picker from changer and display it. */
451 if (ioctl(changer_fd, CHIOGPICKER, &picker))
452 err(1, "%s: CHIOGPICKER", changer_name);
454 printf("\n%s: current picker: %d\n", changer_name, picker);
456 return (0);
458 usage:
459 fprintf(stderr, "usage: %s %s\n", __progname, cname);
460 return (1);
463 /* ARGSUSED */
464 static int
465 do_getpicker(const char *cname, int argc, char **argv)
467 int picker;
469 /* No arguments to this command. */
471 ++argv; --argc;
473 if (argc) {
474 warnx("%s: no arguments expected", cname);
475 goto usage;
478 /* Get current picker from changer and display it. */
479 if (ioctl(changer_fd, CHIOGPICKER, &picker))
480 err(1, "%s: CHIOGPICKER", changer_name);
482 printf("%s: current picker: %d\n", changer_name, picker);
484 return (0);
486 usage:
487 fprintf(stderr, "usage: %s %s\n", __progname, cname);
488 return (1);
491 static int
492 do_setpicker(const char *cname, int argc, char **argv)
494 int picker;
496 ++argv; --argc;
498 if (argc < 1) {
499 warnx("%s: too few arguments", cname);
500 goto usage;
501 } else if (argc > 1) {
502 warnx("%s: too many arguments", cname);
503 goto usage;
506 picker = parse_element_unit(*argv);
508 /* Set the changer picker. */
509 if (ioctl(changer_fd, CHIOSPICKER, &picker))
510 err(1, "%s: CHIOSPICKER", changer_name);
512 return (0);
514 usage:
515 fprintf(stderr, "usage: %s %s <picker>\n", __progname, cname);
516 return (1);
519 static int
520 do_status(const char *cname, int argc, char **argv)
522 struct changer_params cp;
523 struct changer_element_status_request cesr;
524 int i, count, base, chet, schet, echet;
525 const char *description;
526 int pvoltag = 0;
527 int avoltag = 0;
528 int sense = 0;
529 int scsi = 0;
530 int source = 0;
531 int intaddr = 0;
532 int c;
534 count = 0;
535 base = 0;
536 description = NULL;
538 optind = optreset = 1;
539 while ((c = getopt(argc, argv, "vVsSbaI")) != -1) {
540 switch (c) {
541 case 'v':
542 pvoltag = 1;
543 break;
544 case 'V':
545 avoltag = 1;
546 break;
547 case 's':
548 sense = 1;
549 break;
550 case 'S':
551 source = 1;
552 break;
553 case 'b':
554 scsi = 1;
555 break;
556 case 'I':
557 intaddr = 1;
558 break;
559 case 'a':
560 pvoltag = avoltag = source = sense = scsi = intaddr = 1;
561 break;
562 default:
563 warnx("%s: bad option", cname);
564 goto usage;
568 argc -= optind;
569 argv += optind;
572 * On a status command, we expect the following:
574 * [<ET> [<start> [<end>] ] ]
576 * where ET == element type, start == first element to report,
577 * end == number of elements to report
579 * If we get no arguments, we get the status of all
580 * known element types.
582 if (argc > 3) {
583 warnx("%s: too many arguments", cname);
584 goto usage;
588 * Get params from changer. Specifically, we need the element
589 * counts.
591 if (ioctl(changer_fd, CHIOGPARAMS, (char *)&cp))
592 err(1, "%s: CHIOGPARAMS", changer_name);
594 if (argc > 0)
595 schet = echet = parse_element_type(argv[0]);
596 else {
597 schet = CHET_MT;
598 echet = CHET_DT;
600 if (argc > 1) {
601 base = atol(argv[1]);
602 count = 1;
604 if (argc > 2)
605 count = atol(argv[2]) - base + 1;
607 if (base < 0 || count < 0)
608 errx(1, "bad arguments");
610 for (chet = schet; chet <= echet; ++chet) {
611 switch (chet) {
612 case CHET_MT:
613 if (count == 0)
614 count = cp.cp_npickers;
615 else if (count > cp.cp_npickers)
616 errx(1, "not that many pickers in device");
617 description = "picker";
618 break;
620 case CHET_ST:
621 if (count == 0)
622 count = cp.cp_nslots;
623 else if (count > cp.cp_nslots)
624 errx(1, "not that many slots in device");
625 description = "slot";
626 break;
628 case CHET_IE:
629 if (count == 0)
630 count = cp.cp_nportals;
631 else if (count > cp.cp_nportals)
632 errx(1, "not that many portals in device");
633 description = "portal";
634 break;
636 case CHET_DT:
637 if (count == 0)
638 count = cp.cp_ndrives;
639 else if (count > cp.cp_ndrives)
640 errx(1, "not that many drives in device");
641 description = "drive";
642 break;
644 default:
645 /* To appease gcc -Wuninitialized. */
646 count = 0;
647 description = NULL;
650 if (count == 0) {
651 if (argc == 0)
652 continue;
653 else {
654 printf("%s: no %s elements\n",
655 changer_name, description);
656 return (0);
660 bzero(&cesr, sizeof(cesr));
661 cesr.cesr_element_type = chet;
662 cesr.cesr_element_base = base;
663 cesr.cesr_element_count = count;
664 /* Allocate storage for the status structures. */
665 cesr.cesr_element_status =
666 (struct changer_element_status *)
667 calloc((size_t)count, sizeof(struct changer_element_status));
669 if (!cesr.cesr_element_status)
670 errx(1, "can't allocate status storage");
672 if (avoltag || pvoltag)
673 cesr.cesr_flags |= CESR_VOLTAGS;
675 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr)) {
676 free(cesr.cesr_element_status);
677 err(1, "%s: CHIOGSTATUS", changer_name);
680 /* Dump the status for each reported element. */
681 for (i = 0; i < count; ++i) {
682 struct changer_element_status *ces =
683 &(cesr.cesr_element_status[i]);
684 printf("%s %d: %s", description, ces->ces_addr,
685 bits_to_string(ces->ces_flags,
686 CESTATUS_BITS));
687 if (sense)
688 printf(" sense: <0x%02x/0x%02x>",
689 ces->ces_sensecode,
690 ces->ces_sensequal);
691 if (pvoltag)
692 printf(" voltag: <%s:%d>",
693 ces->ces_pvoltag.cv_volid,
694 ces->ces_pvoltag.cv_serial);
695 if (avoltag)
696 printf(" avoltag: <%s:%d>",
697 ces->ces_avoltag.cv_volid,
698 ces->ces_avoltag.cv_serial);
699 if (source) {
700 if (ces->ces_flags & CES_SOURCE_VALID)
701 printf(" source: <%s %d>",
702 element_type_name(
703 ces->ces_source_type),
704 ces->ces_source_addr);
705 else
706 printf(" source: <>");
708 if (intaddr)
709 printf(" intaddr: <%d>", ces->ces_int_addr);
710 if (scsi) {
711 printf(" scsi: <");
712 if (ces->ces_flags & CES_SCSIID_VALID)
713 printf("%d", ces->ces_scsi_id);
714 else
715 putchar('?');
716 putchar(':');
717 if (ces->ces_flags & CES_LUN_VALID)
718 printf("%d", ces->ces_scsi_lun);
719 else
720 putchar('?');
721 putchar('>');
723 putchar('\n');
726 free(cesr.cesr_element_status);
727 count = 0;
730 return (0);
732 usage:
733 fprintf(stderr, "usage: %s %s [-vVsSbaA] [<element type> [<start-addr> [<end-addr>] ] ]\n",
734 __progname, cname);
735 return (1);
738 static int
739 do_ielem(const char *cname, int argc, char **argv)
741 int timeout = 0;
743 if (argc == 2) {
744 timeout = atol(argv[1]);
745 } else if (argc > 1) {
746 warnx("%s: too many arguments", cname);
747 goto usage;
750 if (ioctl(changer_fd, CHIOIELEM, &timeout))
751 err(1, "%s: CHIOIELEM", changer_name);
753 return (0);
755 usage:
756 fprintf(stderr, "usage: %s %s [<timeout>]\n",
757 __progname, cname);
758 return (1);
761 static int
762 do_voltag(const char *cname, int argc, char **argv)
764 int force = 0;
765 int clear = 0;
766 int alternate = 0;
767 int c;
768 struct changer_set_voltag_request csvr;
770 bzero(&csvr, sizeof(csvr));
772 optind = optreset = 1;
773 while ((c = getopt(argc, argv, "fca")) != -1) {
774 switch (c) {
775 case 'f':
776 force = 1;
777 break;
778 case 'c':
779 clear = 1;
780 break;
781 case 'a':
782 alternate = 1;
783 break;
784 default:
785 warnx("%s: bad option", cname);
786 goto usage;
790 argc -= optind;
791 argv += optind;
793 if (argc < 2) {
794 warnx("%s: missing element specification", cname);
795 goto usage;
798 csvr.csvr_type = parse_element_type(argv[0]);
799 csvr.csvr_addr = atol(argv[1]);
801 if (!clear) {
802 if (argc < 3 || argc > 4) {
803 warnx("%s: missing argument", cname);
804 goto usage;
807 if (force)
808 csvr.csvr_flags = CSVR_MODE_REPLACE;
809 else
810 csvr.csvr_flags = CSVR_MODE_SET;
812 if (strlen(argv[2]) > sizeof(csvr.csvr_voltag.cv_volid)) {
813 warnx("%s: volume label too long", cname);
814 goto usage;
817 strlcpy((char *)csvr.csvr_voltag.cv_volid, argv[2],
818 sizeof(csvr.csvr_voltag.cv_volid));
820 if (argc == 4) {
821 csvr.csvr_voltag.cv_serial = atol(argv[3]);
823 } else {
824 if (argc != 2) {
825 warnx("%s: unexpected argument", cname);
826 goto usage;
828 csvr.csvr_flags = CSVR_MODE_CLEAR;
831 if (alternate) {
832 csvr.csvr_flags |= CSVR_ALTERNATE;
835 if (ioctl(changer_fd, CHIOSETVOLTAG, &csvr))
836 err(1, "%s: CHIOSETVOLTAG", changer_name);
838 return 0;
839 usage:
840 fprintf(stderr, "usage: %s %s [-fca] <element> [<voltag> [<vsn>] ]\n",
841 __progname, cname);
842 return 1;
845 static int
846 parse_element_type(char *cp)
848 int i;
850 for (i = 0; elements[i].et_name != NULL; ++i)
851 if (strcmp(elements[i].et_name, cp) == 0)
852 return (elements[i].et_type);
854 errx(1, "invalid element type `%s'", cp);
855 /* NOTREACHED */
858 static const char *
859 element_type_name(int et)
861 int i;
863 for (i = 0; elements[i].et_name != NULL; i++)
864 if (elements[i].et_type == et)
865 return elements[i].et_name;
867 return "unknown";
870 static int
871 parse_element_unit(char *cp)
873 int i;
874 char *p;
876 i = (int)strtol(cp, &p, 10);
877 if ((i < 0) || (*p != '\0'))
878 errx(1, "invalid unit number `%s'", cp);
880 return (i);
883 static int
884 parse_special(char *cp)
886 int val;
888 val = is_special(cp);
889 if (val)
890 return (val);
892 errx(1, "invalid modifier `%s'", cp);
893 /* NOTREACHED */
896 static int
897 is_special(char *cp)
899 int i;
901 for (i = 0; specials[i].sw_name != NULL; ++i)
902 if (strcmp(specials[i].sw_name, cp) == 0)
903 return (specials[i].sw_value);
905 return (0);
908 static const char *
909 bits_to_string(ces_status_flags v, const char *cp)
911 const char *np;
912 char f, sep, *bp;
913 static char buf[128];
915 bp = buf;
916 memset(buf, 0, sizeof(buf));
918 for (sep = '<'; (f = *cp++) != 0; cp = np) {
919 for (np = cp; *np >= ' ';)
920 np++;
921 if ((v & (1 << (f - 1))) == 0)
922 continue;
923 snprintf(bp, sizeof(buf) - (bp - &buf[0]),
924 "%c%.*s", sep, (int)(long)(np - cp), cp);
925 bp += strlen(bp);
926 sep = ',';
928 if (sep != '<')
929 *bp = '>';
931 return (buf);
934 * do_return()
936 * Given an element reference, ask the changer/picker to move that
937 * element back to its source slot.
939 static int
940 do_return(const char *cname, int argc, char **argv)
942 struct changer_element_status *ces;
943 struct changer_move cmd;
944 u_int16_t type, element;
946 ++argv; --argc;
948 if (argc < 2) {
949 warnx("%s: too few arguments", cname);
950 goto usage;
951 } else if (argc > 3) {
952 warnx("%s: too many arguments", cname);
953 goto usage;
956 type = parse_element_type(*argv);
957 ++argv; --argc;
959 /* Handle voltag virtual Changer Element Type */
960 if (CHET_VT == type) {
961 find_element(*argv, &type, &element);
962 } else {
963 element = parse_element_unit(*argv);
965 ++argv; --argc;
967 /* Get the status */
968 ces = get_element_status((unsigned int)type, (unsigned int)element);
970 if (NULL == ces)
971 errx(1, "%s: null element status pointer", cname);
973 if (!(ces->ces_flags & CES_SOURCE_VALID))
974 errx(1, "%s: no source information", cname);
976 memset(&cmd, 0, sizeof(cmd));
978 cmd.cm_fromtype = type;
979 cmd.cm_fromunit = element;
980 cmd.cm_totype = ces->ces_source_type;
981 cmd.cm_tounit = ces->ces_source_addr;
983 if (ioctl(changer_fd, CHIOMOVE, &cmd) == -1)
984 err(1, "%s: CHIOMOVE", changer_name);
985 free(ces);
987 return(0);
989 usage:
990 fprintf(stderr, "usage: %s %s "
991 "<from ET> <from EU>\n", __progname, cname);
992 return(1);
996 * get_element_status()
998 * return a *cesr for the specified changer element. This
999 * routing will malloc()/calloc() the memory. The caller
1000 * should free() it when done.
1002 static struct changer_element_status *
1003 get_element_status(unsigned int type, unsigned int element)
1005 struct changer_element_status_request cesr;
1006 struct changer_element_status *ces;
1008 ces = (struct changer_element_status *)
1009 calloc((size_t)1, sizeof(struct changer_element_status));
1011 if (NULL == ces)
1012 errx(1, "can't allocate status storage");
1014 memset(&cesr, 0, sizeof(cesr));
1016 cesr.cesr_element_type = (u_int16_t)type;
1017 cesr.cesr_element_base = (u_int16_t)element;
1018 cesr.cesr_element_count = 1; /* Only this one element */
1019 cesr.cesr_flags |= CESR_VOLTAGS; /* Grab voltags as well */
1020 cesr.cesr_element_status = ces;
1022 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) {
1023 free(ces);
1024 err(1, "%s: CHIOGSTATUS", changer_name);
1025 /* NOTREACHED */
1028 return ces;
1033 * find_element()
1035 * Given a <voltag> find the chager element and unit, or exit
1036 * with an error if it isn't found. We grab the changer status
1037 * and iterate until we find a match, or crap out.
1039 static void
1040 find_element(char *voltag, u_int16_t *et, u_int16_t *eu)
1042 struct changer_params cp;
1043 struct changer_element_status_request cesr;
1044 struct changer_element_status *ch_ces, *ces;
1045 int found = 0;
1046 size_t elem, total_elem;
1049 * Get the changer parameters, we're interested in the counts
1050 * for all types of elements to perform our search.
1052 if (ioctl(changer_fd, CHIOGPARAMS, (char *)&cp))
1053 err(1, "%s: CHIOGPARAMS", changer_name);
1055 /* Allocate some memory for the results */
1056 total_elem = (cp.cp_nslots + cp.cp_ndrives
1057 + cp.cp_npickers + cp.cp_nportals);
1059 ch_ces = (struct changer_element_status *)
1060 calloc(total_elem, sizeof(struct changer_element_status));
1062 if (NULL == ch_ces)
1063 errx(1, "can't allocate status storage");
1065 ces = ch_ces;
1067 /* Read in the changer slots */
1068 if (cp.cp_nslots > 0) {
1069 cesr.cesr_element_type = CHET_ST;
1070 cesr.cesr_element_base = 0;
1071 cesr.cesr_element_count = cp.cp_nslots;
1072 cesr.cesr_flags |= CESR_VOLTAGS;
1073 cesr.cesr_element_status = ces;
1075 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) {
1076 free(ch_ces);
1077 err(1, "%s: CHIOGSTATUS", changer_name);
1079 ces += cp.cp_nslots;
1082 /* Read in the drive information */
1083 if (cp.cp_ndrives > 0 ) {
1085 memset(&cesr, 0, sizeof(cesr));
1086 cesr.cesr_element_type = CHET_DT;
1087 cesr.cesr_element_base = 0;
1088 cesr.cesr_element_count = cp.cp_ndrives;
1089 cesr.cesr_flags |= CESR_VOLTAGS;
1090 cesr.cesr_element_status = ces;
1092 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) {
1093 free(ch_ces);
1094 err(1, "%s: CHIOGSTATUS", changer_name);
1096 ces += cp.cp_ndrives;
1099 /* Read in the portal information */
1100 if (cp.cp_nportals > 0 ) {
1101 memset(&cesr, 0, sizeof(cesr));
1102 cesr.cesr_element_type = CHET_IE;
1103 cesr.cesr_element_base = 0;
1104 cesr.cesr_element_count = cp.cp_nportals;
1105 cesr.cesr_flags |= CESR_VOLTAGS;
1106 cesr.cesr_element_status = ces;
1108 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) {
1109 free(ch_ces);
1110 err(1, "%s: CHIOGSTATUS", changer_name);
1112 ces += cp.cp_nportals;
1115 /* Read in the picker information */
1116 if (cp.cp_npickers > 0) {
1117 memset(&cesr, 0, sizeof(cesr));
1118 cesr.cesr_element_type = CHET_MT;
1119 cesr.cesr_element_base = 0;
1120 cesr.cesr_element_count = cp.cp_npickers;
1121 cesr.cesr_flags |= CESR_VOLTAGS;
1122 cesr.cesr_element_status = ces;
1124 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) {
1125 free(ch_ces);
1126 err(1, "%s: CHIOGSTATUS", changer_name);
1131 * Now search the list the specified <voltag>
1133 for (elem = 0; elem <= total_elem; ++elem) {
1135 ces = &ch_ces[elem];
1137 /* Make sure we have a tape in this element */
1138 if ((ces->ces_flags & (CES_STATUS_ACCESS|CES_STATUS_FULL))
1139 != (CES_STATUS_ACCESS|CES_STATUS_FULL))
1140 continue;
1142 /* Check to see if it is our target */
1143 if (strcasecmp(voltag,
1144 (const char *)ces->ces_pvoltag.cv_volid) == 0) {
1145 *et = ces->ces_type;
1146 *eu = ces->ces_addr;
1147 ++found;
1148 break;
1151 if (!found) {
1152 errx(1, "%s: unable to locate voltag: %s", changer_name,
1153 voltag);
1155 free(ch_ces);
1156 return;
1159 static void
1160 cleanup(void)
1162 /* Simple enough... */
1163 close(changer_fd);
1166 static void
1167 usage(void)
1169 fprintf(stderr, "usage: %s [-f changer] command [-<flags>] "
1170 "arg1 arg2 [arg3 [...]]\n", __progname);
1171 exit(1);