1 /* $NetBSD: chio.c,v 1.6 1998/01/04 23:53:58 thorpej Exp $ */
3 * Copyright (c) 1996 Jason R. Thorpe <thorpej@and.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgements:
16 * This product includes software developed by Jason R. Thorpe
17 * for And Communications, http://www.and.com/
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Additional Copyright (c) 1997, by Matthew Jacob, for NASA/Ames Research Ctr.
35 * Addidional Copyright (c) 2000, by C. Stephen Gunn, Waterspout Communications
40 static const char copyright[] =
41 "@(#) Copyright (c) 1996 Jason R. Thorpe. All rights reserved.";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
61 #include "pathnames.h"
63 static void usage(void);
64 static void cleanup(void);
65 static u_int16_t parse_element_type(char *);
66 static u_int16_t parse_element_unit(char *);
67 static const char * element_type_name(int et);
68 static int parse_special(char *);
69 static int is_special(char *);
70 static const char *bits_to_string(ces_status_flags, const char *);
72 static void find_element(char *, uint16_t *, uint16_t *);
73 static struct changer_element_status *get_element_status
74 (unsigned int, unsigned int, int);
76 static int do_move(const char *, int, char **);
77 static int do_exchange(const char *, int, char **);
78 static int do_position(const char *, int, char **);
79 static int do_params(const char *, int, char **);
80 static int do_getpicker(const char *, int, char **);
81 static int do_setpicker(const char *, int, char **);
82 static int do_status(const char *, int, char **);
83 static int do_ielem(const char *, int, char **);
84 static int do_return(const char *, int, char **);
85 static int do_voltag(const char *, int, char **);
86 static void print_designator(const char *, u_int8_t, u_int8_t);
89 #define CHET_VT 10 /* Completely Arbitrary */
92 /* Valid changer element types. */
93 static const struct element_type elements[] = {
95 { "picker", CHET_MT },
96 { "portal", CHET_IE },
98 { "voltag", CHET_VT }, /* Select tapes by barcode */
102 /* Valid commands. */
103 static const struct changer_command commands[] = {
104 { "exchange", do_exchange },
105 { "getpicker", do_getpicker },
106 { "ielem", do_ielem },
108 { "params", do_params },
109 { "position", do_position },
110 { "setpicker", do_setpicker },
111 { "status", do_status },
112 { "return", do_return },
113 { "voltag", do_voltag },
117 /* Valid special words. */
118 static const struct special_word specials[] = {
119 { "inv", SW_INVERT },
120 { "inv1", SW_INVERT1 },
121 { "inv2", SW_INVERT2 },
125 static int changer_fd;
126 static const char *changer_name;
129 main(int argc, char **argv)
133 while ((ch = getopt(argc, argv, "f:")) != -1) {
136 changer_name = optarg;
149 /* Get the default changer if not already specified. */
150 if (changer_name == NULL)
151 if ((changer_name = getenv(CHANGER_ENV_VAR)) == NULL)
152 changer_name = _PATH_CH;
154 /* Open the changer device. */
155 if ((changer_fd = open(changer_name, O_RDWR, 0600)) == -1)
156 err(1, "%s: open", changer_name);
158 /* Register cleanup function. */
160 err(1, "can't register cleanup function");
162 /* Find the specified command. */
163 for (i = 0; commands[i].cc_name != NULL; ++i)
164 if (strcmp(*argv, commands[i].cc_name) == 0)
166 if (commands[i].cc_name == NULL) {
167 /* look for abbreviation */
168 for (i = 0; commands[i].cc_name != NULL; ++i)
169 if (strncmp(*argv, commands[i].cc_name,
174 if (commands[i].cc_name == NULL)
175 errx(1, "unknown command: %s", *argv);
177 exit ((*commands[i].cc_handler)(commands[i].cc_name, argc, argv));
182 do_move(const char *cname, int argc, char **argv)
184 struct changer_move cmd;
188 * On a move command, we expect the following:
190 * <from ET> <from EU> <to ET> <to EU> [inv]
192 * where ET == element type and EU == element unit.
198 warnx("%s: too few arguments", cname);
200 } else if (argc > 5) {
201 warnx("%s: too many arguments", cname);
204 (void) memset(&cmd, 0, sizeof(cmd));
207 cmd.cm_fromtype = parse_element_type(*argv);
210 /* Check for voltag virtual type */
211 if (CHET_VT == cmd.cm_fromtype) {
212 find_element(*argv, &cmd.cm_fromtype, &cmd.cm_fromunit);
215 cmd.cm_fromunit = parse_element_unit(*argv);
220 cmd.cm_totype = parse_element_type(*argv);
223 /* Check for voltag virtual type, and report error */
224 if (CHET_VT == cmd.cm_totype)
225 errx(1,"%s: voltag only makes sense as an element source",
229 cmd.cm_tounit = parse_element_unit(*argv);
232 /* Deal with optional command modifier. */
234 val = parse_special(*argv);
237 cmd.cm_flags |= CM_INVERT;
241 errx(1, "%s: inappropriate modifier `%s'",
247 /* Send command to changer. */
248 if (ioctl(changer_fd, CHIOMOVE, &cmd))
249 err(1, "%s: CHIOMOVE", changer_name);
254 (void) fprintf(stderr, "usage: %s %s "
255 "<from ET> <from EU> <to ET> <to EU> [inv]\n", getprogname(), cname);
260 do_exchange(const char *cname, int argc, char **argv)
262 struct changer_exchange cmd;
266 * On an exchange command, we expect the following:
268 * <src ET> <src EU> <dst1 ET> <dst1 EU> [<dst2 ET> <dst2 EU>] [inv1] [inv2]
270 * where ET == element type and EU == element unit.
276 warnx("%s: too few arguments", cname);
278 } else if (argc > 8) {
279 warnx("%s: too many arguments", cname);
282 (void) memset(&cmd, 0, sizeof(cmd));
285 cmd.ce_srctype = parse_element_type(*argv);
288 /* Check for voltag virtual type */
289 if (CHET_VT == cmd.ce_srctype) {
290 find_element(*argv, &cmd.ce_srctype, &cmd.ce_srcunit);
293 cmd.ce_srcunit = parse_element_unit(*argv);
298 cmd.ce_fdsttype = parse_element_type(*argv);
301 /* Check for voltag virtual type */
302 if (CHET_VT == cmd.ce_fdsttype) {
303 find_element(*argv, &cmd.ce_fdsttype, &cmd.ce_fdstunit);
306 cmd.ce_fdstunit = parse_element_unit(*argv);
311 * If the next token is a special word or there are no more
312 * arguments, then this is a case of simple exchange.
315 if ((argc == 0) || is_special(*argv)) {
316 cmd.ce_sdsttype = cmd.ce_srctype;
317 cmd.ce_sdstunit = cmd.ce_srcunit;
322 cmd.ce_sdsttype = parse_element_type(*argv);
325 if (CHET_VT == cmd.ce_sdsttype)
326 errx(1,"%s %s: voltag only makes sense as an element source",
330 cmd.ce_sdstunit = parse_element_unit(*argv);
334 /* Deal with optional command modifiers. */
336 val = parse_special(*argv);
340 cmd.ce_flags |= CE_INVERT1;
344 cmd.ce_flags |= CE_INVERT2;
348 errx(1, "%s: inappropriate modifier `%s'",
354 /* Send command to changer. */
355 if (ioctl(changer_fd, CHIOEXCHANGE, &cmd))
356 err(1, "%s: CHIOEXCHANGE", changer_name);
361 (void) fprintf(stderr,
362 "usage: %s %s <src ET> <src EU> <dst1 ET> <dst1 EU>\n"
363 " [<dst2 ET> <dst2 EU>] [inv1] [inv2]\n",
364 getprogname(), cname);
369 do_position(const char *cname, int argc, char **argv)
371 struct changer_position cmd;
375 * On a position command, we expect the following:
377 * <to ET> <to EU> [inv]
379 * where ET == element type and EU == element unit.
385 warnx("%s: too few arguments", cname);
387 } else if (argc > 3) {
388 warnx("%s: too many arguments", cname);
391 (void) memset(&cmd, 0, sizeof(cmd));
394 cmd.cp_type = parse_element_type(*argv);
398 cmd.cp_unit = parse_element_unit(*argv);
401 /* Deal with optional command modifier. */
403 val = parse_special(*argv);
406 cmd.cp_flags |= CP_INVERT;
410 errx(1, "%s: inappropriate modifier `%s'",
416 /* Send command to changer. */
417 if (ioctl(changer_fd, CHIOPOSITION, &cmd))
418 err(1, "%s: CHIOPOSITION", changer_name);
423 (void) fprintf(stderr, "usage: %s %s <to ET> <to EU> [inv]\n",
424 getprogname(), cname);
430 do_params(const char *cname, int argc, char **argv)
432 struct changer_params data;
435 /* No arguments to this command. */
440 warnx("%s: no arguments expected", cname);
444 /* Get params from changer and display them. */
445 (void) memset(&data, 0, sizeof(data));
446 if (ioctl(changer_fd, CHIOGPARAMS, &data))
447 err(1, "%s: CHIOGPARAMS", changer_name);
449 (void) printf("%s: %d slot%s, %d drive%s, %d picker%s",
451 data.cp_nslots, (data.cp_nslots > 1) ? "s" : "",
452 data.cp_ndrives, (data.cp_ndrives > 1) ? "s" : "",
453 data.cp_npickers, (data.cp_npickers > 1) ? "s" : "");
454 if (data.cp_nportals)
455 (void) printf(", %d portal%s", data.cp_nportals,
456 (data.cp_nportals > 1) ? "s" : "");
458 /* Get current picker from changer and display it. */
459 if (ioctl(changer_fd, CHIOGPICKER, &picker))
460 err(1, "%s: CHIOGPICKER", changer_name);
462 (void) printf("\n%s: current picker: %d\n", changer_name, picker);
467 (void) fprintf(stderr, "usage: %s %s\n", getprogname(), cname);
473 do_getpicker(const char *cname, int argc, char **argv)
477 /* No arguments to this command. */
482 warnx("%s: no arguments expected", cname);
486 /* Get current picker from changer and display it. */
487 if (ioctl(changer_fd, CHIOGPICKER, &picker))
488 err(1, "%s: CHIOGPICKER", changer_name);
490 (void) printf("%s: current picker: %d\n", changer_name, picker);
495 (void) fprintf(stderr, "usage: %s %s\n", getprogname(), cname);
500 do_setpicker(const char *cname, int argc, char **argv)
507 warnx("%s: too few arguments", cname);
509 } else if (argc > 1) {
510 warnx("%s: too many arguments", cname);
514 picker = parse_element_unit(*argv);
516 /* Set the changer picker. */
517 if (ioctl(changer_fd, CHIOSPICKER, &picker))
518 err(1, "%s: CHIOSPICKER", changer_name);
523 (void) fprintf(stderr, "usage: %s %s <picker>\n", getprogname(), cname);
528 do_status(const char *cname, int argc, char **argv)
530 struct changer_params cp;
531 struct changer_element_status_request cesr;
533 u_int16_t base, count, chet, schet, echet;
534 const char *description;
547 optind = optreset = 1;
548 while ((c = getopt(argc, argv, "vVsSbaI")) != -1) {
569 pvoltag = avoltag = source = sense = scsi = intaddr = 1;
572 warnx("%s: bad option", cname);
581 * On a status command, we expect the following:
583 * [<ET> [<start> [<end>] ] ]
585 * where ET == element type, start == first element to report,
586 * end == number of elements to report
588 * If we get no arguments, we get the status of all
589 * known element types.
592 warnx("%s: too many arguments", cname);
597 * Get params from changer. Specifically, we need the element
600 if (ioctl(changer_fd, CHIOGPARAMS, (char *)&cp))
601 err(1, "%s: CHIOGPARAMS", changer_name);
604 schet = echet = parse_element_type(argv[0]);
610 base = (u_int16_t)atol(argv[1]);
614 count = (u_int16_t)atol(argv[2]) - base + 1;
616 for (chet = schet; chet <= echet; ++chet) {
620 count = cp.cp_npickers;
621 else if (count > cp.cp_npickers)
622 errx(1, "not that many pickers in device");
623 description = "picker";
628 count = cp.cp_nslots;
629 else if (count > cp.cp_nslots)
630 errx(1, "not that many slots in device");
631 description = "slot";
636 count = cp.cp_nportals;
637 else if (count > cp.cp_nportals)
638 errx(1, "not that many portals in device");
639 description = "portal";
644 count = cp.cp_ndrives;
645 else if (count > cp.cp_ndrives)
646 errx(1, "not that many drives in device");
647 description = "drive";
651 /* To appease gcc -Wuninitialized. */
660 printf("%s: no %s elements\n",
661 changer_name, description);
666 bzero(&cesr, sizeof(cesr));
667 cesr.cesr_element_type = chet;
668 cesr.cesr_element_base = base;
669 cesr.cesr_element_count = count;
670 /* Allocate storage for the status structures. */
671 cesr.cesr_element_status =
672 (struct changer_element_status *)
673 calloc((size_t)count, sizeof(struct changer_element_status));
675 if (!cesr.cesr_element_status)
676 errx(1, "can't allocate status storage");
678 if (avoltag || pvoltag)
679 cesr.cesr_flags |= CESR_VOLTAGS;
681 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr)) {
682 free(cesr.cesr_element_status);
683 err(1, "%s: CHIOGSTATUS", changer_name);
686 /* Dump the status for each reported element. */
687 for (i = 0; i < count; ++i) {
688 struct changer_element_status *ces =
689 &(cesr.cesr_element_status[i]);
690 printf("%s %d: %s", description, ces->ces_addr,
691 bits_to_string(ces->ces_flags,
694 printf(" sense: <0x%02x/0x%02x>",
698 printf(" voltag: <%s:%d>",
699 ces->ces_pvoltag.cv_volid,
700 ces->ces_pvoltag.cv_serial);
702 printf(" avoltag: <%s:%d>",
703 ces->ces_avoltag.cv_volid,
704 ces->ces_avoltag.cv_serial);
706 if (ces->ces_flags & CES_SOURCE_VALID)
707 printf(" source: <%s %d>",
709 ces->ces_source_type),
710 ces->ces_source_addr);
712 printf(" source: <>");
715 printf(" intaddr: <%d>", ces->ces_int_addr);
718 if (ces->ces_flags & CES_SCSIID_VALID)
719 printf("%d", ces->ces_scsi_id);
723 if (ces->ces_flags & CES_LUN_VALID)
724 printf("%d", ces->ces_scsi_lun);
729 if (ces->ces_designator_length > 0)
730 print_designator(ces->ces_designator,
732 ces->ces_designator_length);
736 free(cesr.cesr_element_status);
743 (void) fprintf(stderr, "usage: %s %s [-vVsSbaA] [<element type> [<start-addr> [<end-addr>] ] ]\n",
744 getprogname(), cname);
749 do_ielem(const char *cname, int argc, char **argv)
754 timeout = atol(argv[1]);
755 } else if (argc > 1) {
756 warnx("%s: too many arguments", cname);
760 if (ioctl(changer_fd, CHIOIELEM, &timeout))
761 err(1, "%s: CHIOIELEM", changer_name);
766 (void) fprintf(stderr, "usage: %s %s [<timeout>]\n",
767 getprogname(), cname);
772 do_voltag(const char *cname, int argc, char **argv)
778 struct changer_set_voltag_request csvr;
780 bzero(&csvr, sizeof(csvr));
782 optind = optreset = 1;
783 while ((c = getopt(argc, argv, "fca")) != -1) {
795 warnx("%s: bad option", cname);
804 warnx("%s: missing element specification", cname);
808 csvr.csvr_type = parse_element_type(argv[0]);
809 csvr.csvr_addr = (u_int16_t)atol(argv[1]);
812 if (argc < 3 || argc > 4) {
813 warnx("%s: missing argument", cname);
818 csvr.csvr_flags = CSVR_MODE_REPLACE;
820 csvr.csvr_flags = CSVR_MODE_SET;
822 if (strlen(argv[2]) > sizeof(csvr.csvr_voltag.cv_volid)) {
823 warnx("%s: volume label too long", cname);
827 strlcpy((char *)csvr.csvr_voltag.cv_volid, argv[2],
828 sizeof(csvr.csvr_voltag.cv_volid));
831 csvr.csvr_voltag.cv_serial = (u_int16_t)atol(argv[3]);
835 warnx("%s: unexpected argument", cname);
838 csvr.csvr_flags = CSVR_MODE_CLEAR;
842 csvr.csvr_flags |= CSVR_ALTERNATE;
845 if (ioctl(changer_fd, CHIOSETVOLTAG, &csvr))
846 err(1, "%s: CHIOSETVOLTAG", changer_name);
850 (void) fprintf(stderr,
851 "usage: %s %s [-fca] <element> [<voltag> [<vsn>] ]\n",
852 getprogname(), cname);
857 parse_element_type(char *cp)
861 for (i = 0; elements[i].et_name != NULL; ++i)
862 if (strcmp(elements[i].et_name, cp) == 0)
863 return ((u_int16_t)elements[i].et_type);
865 errx(1, "invalid element type `%s'", cp);
870 element_type_name(int et)
874 for (i = 0; elements[i].et_name != NULL; i++)
875 if (elements[i].et_type == et)
876 return elements[i].et_name;
882 parse_element_unit(char *cp)
887 i = (int)strtol(cp, &p, 10);
888 if ((i < 0) || (*p != '\0'))
889 errx(1, "invalid unit number `%s'", cp);
891 return ((u_int16_t)i);
895 parse_special(char *cp)
899 val = is_special(cp);
903 errx(1, "invalid modifier `%s'", cp);
912 for (i = 0; specials[i].sw_name != NULL; ++i)
913 if (strcmp(specials[i].sw_name, cp) == 0)
914 return (specials[i].sw_value);
920 bits_to_string(ces_status_flags v, const char *cp)
924 static char buf[128];
927 (void) memset(buf, 0, sizeof(buf));
929 for (sep = '<'; (f = *cp++) != 0; cp = np) {
930 for (np = cp; *np >= ' ';)
932 if (((int)v & (1 << (f - 1))) == 0)
934 (void) snprintf(bp, sizeof(buf) - (size_t)(bp - &buf[0]),
935 "%c%.*s", sep, (int)(long)(np - cp), cp);
947 * Given an element reference, ask the changer/picker to move that
948 * element back to its source slot.
951 do_return(const char *cname, int argc, char **argv)
953 struct changer_element_status *ces;
954 struct changer_move cmd;
955 uint16_t type, element;
960 warnx("%s: too few arguments", cname);
962 } else if (argc > 3) {
963 warnx("%s: too many arguments", cname);
967 type = parse_element_type(*argv);
970 /* Handle voltag virtual Changer Element Type */
971 if (CHET_VT == type) {
972 find_element(*argv, &type, &element);
974 element = parse_element_unit(*argv);
979 ces = get_element_status((unsigned int)type, (unsigned int)element,
983 errx(1, "%s: null element status pointer", cname);
985 if (!(ces->ces_flags & CES_SOURCE_VALID))
986 errx(1, "%s: no source information", cname);
988 (void) memset(&cmd, 0, sizeof(cmd));
990 cmd.cm_fromtype = type;
991 cmd.cm_fromunit = element;
992 cmd.cm_totype = ces->ces_source_type;
993 cmd.cm_tounit = ces->ces_source_addr;
995 if (ioctl(changer_fd, CHIOMOVE, &cmd) == -1)
996 err(1, "%s: CHIOMOVE", changer_name);
1002 (void) fprintf(stderr, "usage: %s %s "
1003 "<from ET> <from EU>\n", getprogname(), cname);
1008 * get_element_status()
1010 * return a *cesr for the specified changer element. This
1011 * routing will malloc()/calloc() the memory. The caller
1012 * should free() it when done.
1014 static struct changer_element_status *
1015 get_element_status(unsigned int type, unsigned int element, int use_voltags)
1017 struct changer_element_status_request cesr;
1018 struct changer_element_status *ces;
1020 ces = (struct changer_element_status *)
1021 calloc((size_t)1, sizeof(struct changer_element_status));
1024 errx(1, "can't allocate status storage");
1026 (void)memset(&cesr, 0, sizeof(cesr));
1028 cesr.cesr_element_type = (uint16_t)type;
1029 cesr.cesr_element_base = (uint16_t)element;
1030 cesr.cesr_element_count = 1; /* Only this one element */
1032 cesr.cesr_flags |= CESR_VOLTAGS; /* Grab voltags as well */
1033 cesr.cesr_element_status = ces;
1035 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) {
1037 err(1, "%s: CHIOGSTATUS", changer_name);
1048 * Given a <voltag> find the chager element and unit, or exit
1049 * with an error if it isn't found. We grab the changer status
1050 * and iterate until we find a match, or crap out.
1053 find_element(char *voltag, uint16_t *et, uint16_t *eu)
1055 struct changer_params cp;
1056 struct changer_element_status_request cesr;
1057 struct changer_element_status *ch_ces, *ces;
1059 size_t elem, total_elem;
1062 * Get the changer parameters, we're interested in the counts
1063 * for all types of elements to perform our search.
1065 if (ioctl(changer_fd, CHIOGPARAMS, (char *)&cp))
1066 err(1, "%s: CHIOGPARAMS", changer_name);
1068 /* Allocate some memory for the results */
1069 total_elem = (cp.cp_nslots + cp.cp_ndrives
1070 + cp.cp_npickers + cp.cp_nportals);
1072 ch_ces = (struct changer_element_status *)
1073 calloc(total_elem, sizeof(struct changer_element_status));
1076 errx(1, "can't allocate status storage");
1080 /* Read in the changer slots */
1081 if (cp.cp_nslots > 0) {
1082 (void) memset(&cesr, 0, sizeof(cesr));
1083 cesr.cesr_element_type = CHET_ST;
1084 cesr.cesr_element_base = 0;
1085 cesr.cesr_element_count = cp.cp_nslots;
1086 cesr.cesr_flags |= CESR_VOLTAGS;
1087 cesr.cesr_element_status = ces;
1089 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) {
1091 err(1, "%s: CHIOGSTATUS", changer_name);
1093 ces += cp.cp_nslots;
1096 /* Read in the drive information */
1097 if (cp.cp_ndrives > 0 ) {
1099 (void) memset(&cesr, 0, sizeof(cesr));
1100 cesr.cesr_element_type = CHET_DT;
1101 cesr.cesr_element_base = 0;
1102 cesr.cesr_element_count = cp.cp_ndrives;
1103 cesr.cesr_flags |= CESR_VOLTAGS;
1104 cesr.cesr_element_status = ces;
1106 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) {
1108 err(1, "%s: CHIOGSTATUS", changer_name);
1110 ces += cp.cp_ndrives;
1113 /* Read in the portal information */
1114 if (cp.cp_nportals > 0 ) {
1115 (void) memset(&cesr, 0, sizeof(cesr));
1116 cesr.cesr_element_type = CHET_IE;
1117 cesr.cesr_element_base = 0;
1118 cesr.cesr_element_count = cp.cp_nportals;
1119 cesr.cesr_flags |= CESR_VOLTAGS;
1120 cesr.cesr_element_status = ces;
1122 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) {
1124 err(1, "%s: CHIOGSTATUS", changer_name);
1126 ces += cp.cp_nportals;
1129 /* Read in the picker information */
1130 if (cp.cp_npickers > 0) {
1131 (void) memset(&cesr, 0, sizeof(cesr));
1132 cesr.cesr_element_type = CHET_MT;
1133 cesr.cesr_element_base = 0;
1134 cesr.cesr_element_count = cp.cp_npickers;
1135 cesr.cesr_flags |= CESR_VOLTAGS;
1136 cesr.cesr_element_status = ces;
1138 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) {
1140 err(1, "%s: CHIOGSTATUS", changer_name);
1145 * Now search the list the specified <voltag>
1147 for (elem = 0; elem <= total_elem; ++elem) {
1149 ces = &ch_ces[elem];
1151 /* Make sure we have a tape in this element */
1152 if ((ces->ces_flags & (CES_STATUS_ACCESS|CES_STATUS_FULL))
1153 != (CES_STATUS_ACCESS|CES_STATUS_FULL))
1156 /* Check to see if it is our target */
1157 if (strcasecmp(voltag,
1158 (const char *)ces->ces_pvoltag.cv_volid) == 0) {
1159 *et = ces->ces_type;
1160 *eu = ces->ces_addr;
1166 errx(1, "%s: unable to locate voltag: %s", changer_name,
1176 /* Simple enough... */
1177 (void)close(changer_fd);
1183 (void)fprintf(stderr, "usage: %s [-f changer] command [-<flags>] "
1184 "arg1 arg2 [arg3 [...]]\n", getprogname());
1188 #define UTF8CODESET "UTF-8"
1191 print_designator(const char *designator, u_int8_t code_set,
1192 u_int8_t designator_length)
1194 printf(" serial number: <");
1196 case CES_CODE_SET_ASCII: {
1198 * The driver insures that the string is always NUL terminated.
1200 printf("%s", designator);
1203 case CES_CODE_SET_UTF_8: {
1206 setlocale(LC_ALL, "");
1207 cs_native = nl_langinfo(CODESET);
1209 /* See if we can natively print UTF-8 */
1210 if (strcmp(cs_native, UTF8CODESET) == 0)
1213 if (cs_native == NULL) {
1214 /* We can natively print UTF-8, so use printf. */
1215 printf("%s", designator);
1220 * We can't natively print UTF-8. We should
1221 * convert it to the terminal's codeset, but that
1222 * requires iconv(3) and FreeBSD doesn't have
1223 * iconv(3) in the base system yet. So we use %XX
1224 * notation for non US-ASCII characters instead.
1226 for (i = 0; i < designator_length &&
1227 designator[i] != '\0'; i++) {
1228 if ((unsigned char)designator[i] < 0x80)
1229 printf("%c", designator[i]);
1232 (unsigned char)designator[i]);
1237 case CES_CODE_SET_BINARY: {
1240 for (i = 0; i < designator_length; i++)
1241 printf("%02X%s", designator[i],
1242 (i == designator_length - 1) ? "" : " ");