xref: /openbsd/usr.bin/cdio/cdio.c (revision 7b36286a)
1 /*	$OpenBSD: cdio.c,v 1.67 2008/08/30 10:41:38 fgsch Exp $	*/
2 
3 /*  Copyright (c) 1995 Serge V. Vakulenko
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Serge V. Vakulenko.
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.
20  *
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, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Compact Disc Control Utility by Serge V. Vakulenko <vak@cronyx.ru>.
35  * Based on the non-X based CD player by Jean-Marc Zucconi and
36  * Andrey A. Chernov.
37  *
38  * Fixed and further modified on 5-Sep-1995 by Jukka Ukkonen <jau@funet.fi>.
39  *
40  * 11-Sep-1995: Jukka A. Ukkonen <jau@funet.fi>
41  *              A couple of further fixes to my own earlier "fixes".
42  *
43  * 18-Sep-1995: Jukka A. Ukkonen <jau@funet.fi>
44  *              Added an ability to specify addresses relative to the
45  *              beginning of a track. This is in fact a variation of
46  *              doing the simple play_msf() call.
47  *
48  * 11-Oct-1995: Serge V.Vakulenko <vak@cronyx.ru>
49  *              New eject algorithm.
50  *              Some code style reformatting.
51  *
52  * $FreeBSD: cdcontrol.c,v 1.13 1996/06/25 21:01:27 ache Exp $
53  */
54 
55 #include <sys/param.h>
56 #include <sys/file.h>
57 #include <sys/cdio.h>
58 #include <sys/ioctl.h>
59 #include <sys/queue.h>
60 #include <sys/scsiio.h>
61 #include <sys/stat.h>
62 
63 #include <ctype.h>
64 #include <err.h>
65 #include <errno.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
70 #include <histedit.h>
71 #include <util.h>
72 #include <vis.h>
73 
74 #include "extern.h"
75 
76 #define ASTS_INVALID    0x00  /* Audio status byte not valid */
77 #define ASTS_PLAYING    0x11  /* Audio play operation in progress */
78 #define ASTS_PAUSED     0x12  /* Audio play operation paused */
79 #define ASTS_COMPLETED  0x13  /* Audio play operation successfully completed */
80 #define ASTS_ERROR      0x14  /* Audio play operation stopped due to error */
81 #define ASTS_VOID       0x15  /* No current audio status to return */
82 
83 #ifndef DEFAULT_CD_DRIVE
84 #  define DEFAULT_CD_DRIVE  "cd0"
85 #endif
86 
87 #define CMD_DEBUG       1
88 #define CMD_DEVICE      2
89 #define CMD_EJECT       3
90 #define CMD_HELP        4
91 #define CMD_INFO        5
92 #define CMD_PAUSE       6
93 #define CMD_PLAY        7
94 #define CMD_QUIT        8
95 #define CMD_RESUME      9
96 #define CMD_STOP        10
97 #define CMD_VOLUME      11
98 #define CMD_CLOSE       12
99 #define CMD_RESET       13
100 #define CMD_SET         14
101 #define CMD_STATUS      15
102 #define CMD_NEXT	16
103 #define CMD_PREV	17
104 #define CMD_REPLAY	18
105 #define CMD_CDDB	19
106 #define CMD_CDID	20
107 #define CMD_BLANK	21
108 #define CMD_CDRIP	22
109 #define CMD_CDPLAY	23
110 
111 struct cmdtab {
112 	int command;
113 	char *name;
114 	unsigned int min;
115 	char *args;
116 } cmdtab[] = {
117 { CMD_CLOSE,    "close",        1, "" },
118 { CMD_DEBUG,    "debug",        3, "on | off" },
119 { CMD_DEVICE,   "device",       1, "devname" },
120 { CMD_EJECT,    "eject",        1, "" },
121 { CMD_HELP,     "?",            1, 0 },
122 { CMD_HELP,     "help",         1, "" },
123 { CMD_INFO,     "info",         1, "" },
124 { CMD_NEXT,	"next",		1, "" },
125 { CMD_PAUSE,    "pause",        2, "" },
126 { CMD_PLAY,     "play",         1, "track1[.index1] [track2[.index2]]" },
127 { CMD_PLAY,     "play",         1, "[tr1] m1:s1[.f1] [tr2] [m2:s2[.f2]]" },
128 { CMD_PLAY,     "play",         1, "[#block [len]]" },
129 { CMD_PREV,	"previous",	2, "" },
130 { CMD_QUIT,     "quit",         1, "" },
131 { CMD_RESET,    "reset",        4, "" },
132 { CMD_RESUME,   "resume",       1, "" },
133 { CMD_REPLAY,	"replay",	3, "" },
134 { CMD_SET,      "set",          2, "msf | lba" },
135 { CMD_STATUS,   "status",       1, "" },
136 { CMD_STOP,     "stop",         3, "" },
137 { CMD_VOLUME,   "volume",       1, "<l> <r> | left | right | mute | mono | stereo" },
138 { CMD_CDDB,	"cddbinfo",     2, "[n]" },
139 { CMD_CDID,	"cdid",		3, "" },
140 { CMD_QUIT,	"exit",		2, "" },
141 { CMD_BLANK,	"blank",	1, "" },
142 { CMD_CDRIP,	"cdrip",	1, "[[track1-trackN] ...]" },
143 { CMD_CDPLAY,	"cdplay",	1, "[[track1-trackN] ...]" },
144 { 0, 0, 0, 0}
145 };
146 
147 struct cd_toc_entry *toc_buffer;
148 
149 char		*cdname;
150 int		fd = -1;
151 int		writeperm = 0;
152 int		mediacap[MMC_FEATURE_MAX / 8];
153 int		verbose = 1;
154 int		msf = 1;
155 const char	*cddb_host;
156 char		**track_names;
157 
158 EditLine	*el = NULL;	/* line-editing structure */
159 History		*hist = NULL;	/* line-editing history */
160 void		switch_el(void);
161 
162 extern char	*__progname;
163 
164 int		setvol(int, int);
165 int		read_toc_entrys(int);
166 int		play_msf(int, int, int, int, int, int);
167 int		play_track(int, int, int, int);
168 int		status(int *, int *, int *, int *);
169 int		is_wave(int);
170 __dead void	tao(int argc, char **argv);
171 int		play(char *arg);
172 int		info(char *arg);
173 int		cddbinfo(char *arg);
174 int		pstatus(char *arg);
175 int		play_next(char *arg);
176 int		play_prev(char *arg);
177 int		play_same(char *arg);
178 char		*input(int *);
179 char		*prompt(void);
180 void		prtrack(struct cd_toc_entry *e, int lastflag, char *name);
181 void		lba2msf(unsigned long lba, u_char *m, u_char *s, u_char *f);
182 unsigned int	msf2lba(u_char m, u_char s, u_char f);
183 int		play_blocks(int blk, int len);
184 int		run(int cmd, char *arg);
185 char		*parse(char *buf, int *cmd);
186 void		help(void);
187 void		usage(void);
188 char		*strstatus(int);
189 int		cdid(void);
190 void		addmsf(u_int *, u_int *, u_int *, u_char, u_char, u_char);
191 int		cmpmsf(u_char, u_char, u_char, u_char, u_char, u_char);
192 void		toc2msf(u_int, u_char *, u_char *, u_char *);
193 
194 void
195 help(void)
196 {
197 	struct cmdtab *c;
198 	char *s, n;
199 	u_int i;
200 
201 	for (c = cmdtab; c->name; ++c) {
202 		if (!c->args)
203 			continue;
204 		printf("\t");
205 		for (i = c->min, s = c->name; *s; s++, i--) {
206 			if (i > 0)
207 				n = toupper(*s);
208 			else
209 				n = *s;
210 			putchar(n);
211 		}
212 		if (*c->args)
213 			printf(" %s", c->args);
214 		printf("\n");
215 	}
216 	printf("\n\tThe word \"play\" is not required for the play commands.\n");
217 	printf("\tThe plain target address is taken as a synonym for play.\n");
218 }
219 
220 void
221 usage(void)
222 {
223 	fprintf(stderr, "usage: %s [-sv] [-d host:port] [-f device] [command args ...]\n",
224 	    __progname);
225 	exit(1);
226 }
227 
228 int
229 main(int argc, char **argv)
230 {
231 	int ch, cmd;
232 	char *arg;
233 
234 	cdname = getenv("DISC");
235 	if (!cdname)
236 		cdname = getenv("CDROM");
237 
238 	cddb_host = getenv("CDDB");
239 	if (!cddb_host)
240 		cddb_host = "freedb.freedb.org";
241 
242 	while ((ch = getopt(argc, argv, "svd:f:")) != -1)
243 		switch (ch) {
244 		case 's':
245 			verbose = 0;
246 			break;
247 		case 'v':
248 			verbose = 2;
249 			break;
250 		case 'f':
251 			cdname = optarg;
252 			break;
253 		case 'd':
254 			cddb_host = optarg;
255 			break;
256 		default:
257 			usage();
258 		}
259 
260 	argc -= optind;
261 	argv += optind;
262 
263 	if (argc > 0 && ! strcasecmp(*argv, "help"))
264 		usage();
265 
266 	if (!cdname) {
267 		cdname = DEFAULT_CD_DRIVE;
268 		fprintf(stderr,
269 		    "No CD device name specified. Defaulting to %s.\n", cdname);
270 	}
271 
272 	if (argc > 0 && !strcasecmp(*argv, "tao")) {
273 		tao(argc, argv);
274 		/* NOTREACHED */
275 	}
276 	if (argc > 0) {
277 		char buf[80], *p;
278 		int len;
279 
280 		for (p=buf; argc-->0; ++argv) {
281 			len = snprintf(p, buf + sizeof buf - p,
282 			   "%s%s", (p > buf) ? " " : "", *argv);
283 
284 			if (len == -1 || len >= buf + sizeof buf - p)
285 				errx(1, "argument list too long.");
286 
287 			p += len;
288 		}
289 		arg = parse(buf, &cmd);
290 		return (run(cmd, arg));
291 	}
292 
293 	if (verbose == 1)
294 		verbose = isatty(0);
295 
296 	if (verbose) {
297 		printf("Compact Disc Control utility, version %s\n", VERSION);
298 		printf("Type `?' for command list\n\n");
299 	}
300 
301 	switch_el();
302 
303 	for (;;) {
304 		arg = input(&cmd);
305 		if (run(cmd, arg) < 0) {
306 			if (verbose)
307 				warn(NULL);
308 			close(fd);
309 			fd = -1;
310 		}
311 		fflush(stdout);
312 	}
313 }
314 
315 int
316 run(int cmd, char *arg)
317 {
318 	int l, r, rc;
319 	static char newcdname[MAXPATHLEN];
320 
321 	switch (cmd) {
322 
323 	case CMD_QUIT:
324 		switch_el();
325 		exit(0);
326 
327 	case CMD_INFO:
328 		if (!open_cd(cdname, 0))
329 			return (0);
330 
331 		return info(arg);
332 
333 	case CMD_CDDB:
334 		if (!open_cd(cdname, 0))
335 			return (0);
336 
337 		return cddbinfo(arg);
338 
339 	case CMD_CDID:
340 		if (!open_cd(cdname, 0))
341 			return (0);
342 		return cdid();
343 
344 	case CMD_STATUS:
345 		if (!open_cd(cdname, 0))
346 			return (0);
347 
348 		return pstatus(arg);
349 
350 	case CMD_PAUSE:
351 		if (!open_cd(cdname, 0))
352 			return (0);
353 
354 		return ioctl(fd, CDIOCPAUSE);
355 
356 	case CMD_RESUME:
357 		if (!open_cd(cdname, 0))
358 			return (0);
359 
360 		return ioctl(fd, CDIOCRESUME);
361 
362 	case CMD_STOP:
363 		if (!open_cd(cdname, 0))
364 			return (0);
365 
366 		rc = ioctl(fd, CDIOCSTOP);
367 
368 		(void) ioctl(fd, CDIOCALLOW);
369 
370 		return (rc);
371 
372 	case CMD_RESET:
373 		if (!open_cd(cdname, 0))
374 			return (0);
375 
376 		rc = ioctl(fd, CDIOCRESET);
377 		if (rc < 0)
378 			return rc;
379 		close(fd);
380 		fd = -1;
381 		return (0);
382 
383 	case CMD_DEBUG:
384 		if (!open_cd(cdname, 0))
385 			return (0);
386 
387 		if (!strcasecmp(arg, "on"))
388 			return ioctl(fd, CDIOCSETDEBUG);
389 
390 		if (!strcasecmp(arg, "off"))
391 			return ioctl(fd, CDIOCCLRDEBUG);
392 
393 		printf("%s: Invalid command arguments\n", __progname);
394 
395 		return (0);
396 
397 	case CMD_DEVICE:
398 		/* close old device */
399 		if (fd > -1) {
400 			(void) ioctl(fd, CDIOCALLOW);
401 			close(fd);
402 			fd = -1;
403 		}
404 
405 		if (strlen(arg) == 0) {
406 			printf("%s: Invalid parameter\n", __progname);
407 			return (0);
408 		}
409 
410 		/* open new device */
411 		if (!open_cd(arg, 0))
412 			return (0);
413 		(void) strlcpy(newcdname, arg, sizeof(newcdname));
414 		cdname = newcdname;
415 		return (1);
416 
417 	case CMD_EJECT:
418 		if (!open_cd(cdname, 0))
419 			return (0);
420 
421 		(void) ioctl(fd, CDIOCALLOW);
422 		rc = ioctl(fd, CDIOCEJECT);
423 		if (rc < 0)
424 			return (rc);
425 #if defined(__OpenBSD__)
426 		close(fd);
427 		fd = -1;
428 #endif
429 		if (track_names)
430 			free_names(track_names);
431 		track_names = NULL;
432 		return (0);
433 
434 	case CMD_CLOSE:
435 #if defined(CDIOCCLOSE)
436 		if (!open_cd(cdname, 0))
437 			return (0);
438 
439 		(void) ioctl(fd, CDIOCALLOW);
440 		rc = ioctl(fd, CDIOCCLOSE);
441 		if (rc < 0)
442 			return (rc);
443 		close(fd);
444 		fd = -1;
445 		return (0);
446 #else
447 		printf("%s: Command not yet supported\n", __progname);
448 		return (0);
449 #endif
450 
451 	case CMD_PLAY:
452 		if (!open_cd(cdname, 0))
453 			return (0);
454 
455 		while (isspace(*arg))
456 			arg++;
457 
458 		return play(arg);
459 
460 	case CMD_SET:
461 		if (!strcasecmp(arg, "msf"))
462 			msf = 1;
463 		else if (!strcasecmp(arg, "lba"))
464 			msf = 0;
465 		else
466 			printf("%s: Invalid command arguments\n", __progname);
467 		return (0);
468 
469 	case CMD_VOLUME:
470 		if (!open_cd(cdname, 0))
471 			return (0);
472 
473 		if (!strncasecmp(arg, "left", strlen(arg)))
474 			return ioctl(fd, CDIOCSETLEFT);
475 
476 		if (!strncasecmp(arg, "right", strlen(arg)))
477 			return ioctl(fd, CDIOCSETRIGHT);
478 
479 		if (!strncasecmp(arg, "mono", strlen(arg)))
480 			return ioctl(fd, CDIOCSETMONO);
481 
482 		if (!strncasecmp(arg, "stereo", strlen(arg)))
483 			return ioctl(fd, CDIOCSETSTEREO);
484 
485 		if (!strncasecmp(arg, "mute", strlen(arg)))
486 			return ioctl(fd, CDIOCSETMUTE);
487 
488 		if (2 != sscanf(arg, "%d%d", &l, &r)) {
489 			printf("%s: Invalid command arguments\n", __progname);
490 			return (0);
491 		}
492 
493 		return setvol(l, r);
494 
495 	case CMD_NEXT:
496 		if (!open_cd(cdname, 0))
497 			return (0);
498 
499 		return play_next(arg);
500 
501 	case CMD_PREV:
502 		if (!open_cd(cdname, 0))
503 			return (0);
504 
505 		return play_prev(arg);
506 
507 	case CMD_REPLAY:
508 		if (!open_cd(cdname, 0))
509 			return 0;
510 
511 		return play_same(arg);
512 	case CMD_BLANK:
513 		if (!open_cd(cdname, 1))
514 			return 0;
515 
516 		if (get_media_capabilities(mediacap, 1) == -1) {
517 			warnx("Can't determine media type");
518 			return (0);
519 		}
520 		if (isset(mediacap, MMC_FEATURE_CDRW_WRITE) == 0 &&
521 		    get_media_type() != MEDIATYPE_CDRW) {
522 			warnx("The media doesn't support blanking");
523 			return (0);
524 		}
525 
526 		return blank();
527 	case CMD_CDRIP:
528 		if (!open_cd(cdname, 0))
529 			return (0);
530 
531 		while (isspace(*arg))
532 			arg++;
533 
534 		return cdrip(arg);
535 	case CMD_CDPLAY:
536 		if (!open_cd(cdname, 0))
537 			return (0);
538 
539 		while (isspace(*arg))
540 			arg++;
541 
542 		return cdplay(arg);
543 	default:
544 	case CMD_HELP:
545 		help();
546 		return (0);
547 
548 	}
549 }
550 
551 /*
552  * Check if audio file has RIFF WAVE format. If not, we assume it's just PCM.
553  */
554 int
555 is_wave(int fd)
556 {
557 	char buf[WAVHDRLEN];
558 	int rv;
559 
560 	rv = 0;
561 	if (read(fd, buf, sizeof(buf)) == sizeof(buf)) {
562 		if (memcmp(buf, "RIFF", 4) == 0 &&
563 		    memcmp(buf + 8, "WAVE", 4) == 0)
564 			rv = 1;
565 	}
566 
567 	return (rv);
568 }
569 
570 __dead void
571 tao(int argc, char **argv)
572 {
573 	struct stat sb;
574 	struct track_info *cur_track;
575 	struct track_info *tr;
576 	off_t availblk, needblk = 0;
577 	u_int blklen;
578 	u_int ntracks = 0;
579 	char type;
580 	int ch, speed;
581 	const char *errstr;
582 
583 	if (argc == 1)
584 		usage();
585 
586 	SLIST_INIT(&tracks);
587 	type = 'd';
588 	speed = DRIVE_SPEED_OPTIMAL;
589 	blklen = 2048;
590 	while (argc > 1) {
591 		tr = malloc(sizeof(struct track_info));
592 		if (tr == NULL)
593 			err(1, "tao");
594 
595 		optreset = 1;
596 		optind = 1;
597 		while ((ch = getopt(argc, argv, "ads:")) != -1) {
598 			switch (ch) {
599 			case 'a':
600 				type = 'a';
601 				blklen = 2352;
602 				break;
603 			case 'd':
604 				type = 'd';
605 				blklen = 2048;
606 				break;
607 			case 's':
608 				if (strcmp(optarg, "auto") == 0) {
609 					speed = DRIVE_SPEED_OPTIMAL;
610 				} else if (strcmp(optarg, "max") == 0) {
611 					speed = DRIVE_SPEED_MAX;
612 				} else {
613 					speed = (int)strtonum(optarg, 1,
614 					    CD_MAX_SPEED, &errstr);
615 					if (errstr != NULL) {
616 						errx(1,
617 						    "incorrect speed value");
618 					}
619 				}
620 				break;
621 			default:
622 				usage();
623 				/* NOTREACHED */
624 			}
625 		}
626 
627 		if (speed != DRIVE_SPEED_OPTIMAL && speed != DRIVE_SPEED_MAX)
628 			tr->speed = CD_SPEED_TO_KBPS(speed, blklen);
629 		else
630 			tr->speed = speed;
631 
632 		tr->type = type;
633 		tr->blklen = blklen;
634 		argc -= optind;
635 		argv += optind;
636 		if (argv[0] == NULL)
637 			usage();
638 		tr->file = argv[0];
639 		tr->fd = open(tr->file, O_RDONLY, 0640);
640 		if (tr->fd == -1)
641 			err(1, "cannot open file %s", tr->file);
642 		if (fstat(tr->fd, &sb) == -1)
643 			err(1, "cannot stat file %s", tr->file);
644 		tr->sz = sb.st_size;
645 		tr->off = 0;
646 		if (tr->type == 'a') {
647 			if (is_wave(tr->fd)) {
648 				tr->sz -= WAVHDRLEN;
649 				tr->off = WAVHDRLEN;
650 			}
651 		}
652 		if (SLIST_EMPTY(&tracks))
653 			SLIST_INSERT_HEAD(&tracks, tr, track_list);
654 		else
655 			SLIST_INSERT_AFTER(cur_track, tr, track_list);
656 		cur_track = tr;
657 	}
658 
659 	if (!open_cd(cdname, 1))
660 		exit(1);
661 	if (get_media_capabilities(mediacap, 1) == -1)
662 		errx(1, "Can't determine media type");
663 	if (isset(mediacap, MMC_FEATURE_CD_TAO) == 0)
664 		errx(1, "The media can't be written in TAO mode");
665 
666 	get_disc_size(&availblk);
667 	SLIST_FOREACH(tr, &tracks, track_list) {
668 		needblk += tr->sz/tr->blklen;
669 		ntracks++;
670 	}
671 	needblk += (ntracks - 1) * 150; /* transition area between tracks */
672 	if (needblk > availblk)
673 		errx(1, "Only %llu of the required %llu blocks available",
674 		    availblk, needblk);
675 	if (writetao(&tracks) != 0)
676 		exit(1);
677 	else
678 		exit(0);
679 }
680 
681 int
682 play(char *arg)
683 {
684 	struct ioc_toc_header h;
685 	unsigned char tm, ts, tf;
686 	unsigned int tr1, tr2, m1, m2, s1, s2, f1, f2, i1, i2;
687 	unsigned int blk, len, n;
688 	char c;
689 	int rc;
690 
691 	rc = ioctl(fd, CDIOREADTOCHEADER, &h);
692 
693 	if (rc < 0)
694 		return (rc);
695 
696 	if (h.starting_track > h.ending_track) {
697 		printf("TOC starting_track > TOC ending_track\n");
698 		return (0);
699 	}
700 
701 	n = h.ending_track - h.starting_track + 1;
702 	rc = read_toc_entrys((n + 1) * sizeof (struct cd_toc_entry));
703 
704 	if (rc < 0)
705 		return (rc);
706 
707 	/*
708 	 * Truncate trailing white space. Then by adding %c to the end of the
709 	 * sscanf() formats we catch any errant trailing characters.
710 	 */
711 	rc = strlen(arg) - 1;
712 	while (rc >= 0 && isspace(arg[rc])) {
713 		arg[rc] = '\0';
714 		rc--;
715 	}
716 
717 	if (!arg || ! *arg) {
718 		/* Play the whole disc */
719 		return (play_track(h.starting_track, 1, h.ending_track, 1));
720 	}
721 
722 	if (strchr(arg, '#')) {
723 		/* Play block #blk [ len ] */
724 		if (2 != sscanf(arg, "#%u%u%c", &blk, &len, &c) &&
725 		    1 != sscanf(arg, "#%u%c", &blk, &c)) {
726 			printf("%s: Invalid command arguments\n", __progname);
727 			return (0);
728 		}
729 
730 		if (len == 0) {
731 			if (msf)
732 				len = msf2lba(toc_buffer[n].addr.msf.minute,
733 				    toc_buffer[n].addr.msf.second,
734 				    toc_buffer[n].addr.msf.frame) - blk;
735 			else
736 				len = toc_buffer[n].addr.lba - blk;
737 		}
738 		return play_blocks(blk, len);
739 	}
740 
741 	if (strchr(arg, ':') == NULL) {
742 		/*
743 		 * Play track tr1[.i1] [tr2[.i2]]
744 		 */
745 		if (4 == sscanf(arg, "%u.%u%u.%u%c", &tr1, &i1, &tr2, &i2, &c))
746 			goto play_track;
747 
748 		i2 = 1;
749 		if (3 == sscanf(arg, "%u.%u%u%c", &tr1, &i1, &tr2, &c))
750 			goto play_track;
751 
752 		i1 = 1;
753 		if (3 == sscanf(arg, "%u%u.%u%c", &tr1, &tr2, &i2, &c))
754 			goto play_track;
755 
756 		tr2 = 0;
757 		i2 = 1;
758 		if (2 == sscanf(arg, "%u.%u%c", &tr1, &i1, &c))
759 			goto play_track;
760 
761 		i1 = i2 = 1;
762 		if (2 == sscanf(arg, "%u%u%c", &tr1, &tr2, &c))
763 			goto play_track;
764 
765 		i1 = i2 = 1;
766 		tr2 = 0;
767 		if (1 == sscanf(arg, "%u%c", &tr1, &c))
768 			goto play_track;
769 
770 		printf("%s: Invalid command arguments\n", __progname);
771 		return (0);
772 
773 play_track:
774 		if (tr1 > n || tr2 > n) {
775 			printf("Track number must be between 0 and %u\n", n);
776 			return (0);
777 		} else if (tr2 == 0)
778 			tr2 = h.ending_track;
779 
780 		if (tr1 > tr2) {
781 			printf("starting_track > ending_track\n");
782 			return (0);
783 		}
784 
785 		return (play_track(tr1, i1, tr2, i2));
786 	}
787 
788 	/*
789 	 * Play MSF [tr1] m1:s1[.f1] [tr2] [m2:s2[.f2]]
790 	 *
791 	 * Start Time		End Time
792 	 * ----------		--------
793 	 * tr1 m1:s1.f1		tr2 m2:s2.f2
794 	 * tr1 m1:s1   		tr2 m2:s2.f2
795 	 * tr1 m1:s1.f1		tr2 m2:s2
796 	 * tr1 m1:s1   		tr2 m2:s2
797 	 *     m1:s1.f1		tr2 m2:s2.f2
798 	 *     m1:s1   		tr2 m2:s2.f2
799 	 *     m1:s1.f1		tr2 m2:s2
800 	 *     m1:s1   		tr2 m2:s2
801 	 * tr1 m1:s1.f1		    m2:s2.f2
802 	 * tr1 m1:s1   		    m2:s2.f2
803 	 * tr1 m1:s1.f1		    m2:s2
804 	 * tr1 m1:s1  		    m2:s2
805 	 *     m1:s1.f1		    m2:s2.f2
806 	 *     m1:s1       	    m2:s2.f2
807 	 *     m1:s1.f1  	    m2:s2
808 	 *     m1:s1     	    m2:s2
809 	 * tr1 m1:s1.f1		tr2
810 	 * tr1 m1:s1    	tr2
811 	 *     m1:s1.f1  	tr2
812 	 *     m1:s1      	tr2
813 	 * tr1 m1:s1.f1		<end of disc>
814 	 * tr1 m1:s1    	<end of disc>
815 	 *     m1:s1.f1  	<end of disc>
816 	 *     m1:s1      	<end of disc>
817 	 */
818 
819 	/* tr1 m1:s1.f1		tr2 m2:s2.f2 */
820 	if (8 == sscanf(arg, "%u%u:%u.%u%u%u:%u.%u%c",
821 	    &tr1, &m1, &s1, &f1, &tr2, &m2, &s2, &f2, &c))
822 		goto play_msf;
823 
824 	/* tr1 m1:s1   		tr2 m2:s2.f2 */
825 	f1 = 0;
826 	if (7 == sscanf(arg, "%u%u:%u%u%u:%u.%u%c",
827 	    &tr1, &m1, &s1, &tr2, &m2, &s2, &f2, &c))
828 		goto play_msf;
829 
830 	/* tr1 m1:s1.f1		tr2 m2:s2 */
831 	f2 =0;
832 	if (7 == sscanf(arg, "%u%u:%u.%u%u%u:%u%c",
833 	    &tr1, &m1, &s1, &f1, &tr2, &m2, &s2, &c))
834 		goto play_msf;
835 
836 	/*     m1:s1.f1		tr2 m2:s2.f2 */
837 	tr1 = 0;
838 	if (7 == sscanf(arg, "%u:%u.%u%u%u:%u.%u%c",
839 	    &m1, &s1, &f1, &tr2, &m2, &s2, &f2, &c))
840 		goto play_msf;
841 
842 	/* tr1 m1:s1.f1		    m2:s2.f2 */
843 	tr2 = 0;
844 	if (7 == sscanf(arg, "%u%u:%u.%u%u:%u.%u%c",
845 	    &tr1, &m1, &s1, &f1, &m2, &s2, &f2, &c))
846 		goto play_msf;
847 
848 	/*     m1:s1   		tr2 m2:s2.f2 */
849 	tr1 = f1 = 0;
850 	if (6 == sscanf(arg, "%u:%u%u%u:%u.%u%c",
851 	    &m1, &s1, &tr2, &m2, &s2, &f2, &c))
852 		goto play_msf;
853 
854 	/*     m1:s1.f1		tr2 m2:s2 */
855 	tr1 = f2 = 0;
856 	if (6 == sscanf(arg, "%u:%u.%u%u%u:%u%c",
857 	    &m1, &s1, &f1, &tr2, &m2, &s2, &c))
858 		goto play_msf;
859 
860 	/*     m1:s1.f1		    m2:s2.f2 */
861 	tr1 = tr2 = 0;
862 	if (6 == sscanf(arg, "%u:%u.%u%u:%u.%u%c",
863 	    &m1, &s1, &f1, &m2, &s2, &f2, &c))
864 		goto play_msf;
865 
866 	/* tr1 m1:s1.f1		    m2:s2 */
867 	tr2 = f2 = 0;
868 	if (6 == sscanf(arg, "%u%u:%u.%u%u:%u%c",
869 	    &tr1, &m1, &s1, &f1, &m2, &s2, &c))
870 		goto play_msf;
871 
872 	/* tr1 m1:s1   		    m2:s2.f2 */
873 	tr2 = f1 = 0;
874 	if (6 == sscanf(arg, "%u%u:%u%u:%u.%u%c",
875 	    &tr1, &m1, &s1, &m2, &s2, &f2, &c))
876 		goto play_msf;
877 
878 	/* tr1 m1:s1   		tr2 m2:s2 */
879 	f1 = f2 = 0;
880 	if (6 == sscanf(arg, "%u%u:%u%u%u:%u%c",
881 	    &tr1, &m1, &s1, &tr2, &m2, &s2, &c))
882 		goto play_msf;
883 
884 	/*     m1:s1   		tr2 m2:s2 */
885 	tr1 = f1 = f2 = 0;
886 	if (5 == sscanf(arg, "%u:%u%u%u:%u%c", &m1, &s1, &tr2, &m2, &s2, &c))
887 		goto play_msf;
888 
889 	/* tr1 m1:s1  		    m2:s2 */
890 	f1 = tr2 = f2 = 0;
891 	if (5 == sscanf(arg, "%u%u:%u%u:%u%c", &tr1, &m1, &s1, &m2, &s2, &c))
892 		goto play_msf;
893 
894 	/*     m1:s1       	    m2:s2.f2 */
895 	tr1 = f1 = tr2 = 0;
896 	if (5 == sscanf(arg, "%u:%u%u:%u.%u%c", &m1, &s1, &m2, &s2, &f2, &c))
897 		goto play_msf;
898 
899 	/*     m1:s1.f1  	    m2:s2 */
900 	tr1 = tr2 = f2 = 0;
901 	if (5 == sscanf(arg, "%u:%u.%u%u:%u%c", &m1, &s1, &f1, &m2, &s2, &c))
902 		goto play_msf;
903 
904 	/* tr1 m1:s1.f1		tr2 */
905 	m2 = s2 = f2 = 0;
906 	if (5 == sscanf(arg, "%u%u:%u.%u%u%c", &tr1, &m1, &s1, &f1, &tr2, &c))
907 		goto play_msf;
908 
909 	/*     m1:s1     	    m2:s2 */
910 	tr1 = f1 = tr2 = f2 = 0;
911 	if (4 == sscanf(arg, "%u:%u%u:%u%c", &m1, &s1, &m2, &s2, &c))
912 		goto play_msf;
913 
914 	/* tr1 m1:s1.f1		<end of disc> */
915 	tr2 = m2 = s2 = f2 = 0;
916 	if (4 == sscanf(arg, "%u%u:%u.%u%c", &tr1, &m1, &s1, &f1, &c))
917 		goto play_msf;
918 
919 	/* tr1 m1:s1    	tr2 */
920 	f1 = m2 = s2 = f2 = 0;
921 	if (4 == sscanf(arg, "%u%u:%u%u%c", &tr1, &m1, &s1, &tr2, &c))
922 		goto play_msf;
923 
924 	/*     m1:s1.f1  	tr2 */
925 	tr1 = m2 = s2 = f2 = 0;
926 	if (4 == sscanf(arg, "%u%u:%u%u%c", &m1, &s1, &f1, &tr2, &c))
927 		goto play_msf;
928 
929 	/*     m1:s1.f1  	<end of disc> */
930 	tr1 = tr2 = m2 = s2 = f2 = 0;
931 	if (3 == sscanf(arg, "%u:%u.%u%c", &m1, &s1, &f1, &c))
932 		goto play_msf;
933 
934 	/* tr1 m1:s1    	<end of disc> */
935 	f1 = tr2 = m2 = s2 = f2 = 0;
936 	if (3 == sscanf(arg, "%u%u:%u%c", &tr1, &m1, &s1, &c))
937 		goto play_msf;
938 
939 	/*     m1:s1      	tr2 */
940 	tr1 = f1 = m2 = s2 = f2 = 0;
941 	if (3 == sscanf(arg, "%u:%u%u%c", &m1, &s1, &tr2, &c))
942 		goto play_msf;
943 
944 	/*     m1:s1      	<end of disc> */
945 	tr1 = f1 = tr2 = m2 = s2 = f2 = 0;
946 	if (2 == sscanf(arg, "%u:%u%c", &m1, &s1, &c))
947 		goto play_msf;
948 
949 	printf("%s: Invalid command arguments\n", __progname);
950 	return (0);
951 
952 play_msf:
953 	if (tr1 > n || tr2 > n) {
954 		printf("Track number must be between 0 and %u\n", n);
955 		return (0);
956 	} else if (m1 > 99 || m2 > 99) {
957 		printf("Minutes must be between 0 and 99\n");
958 		return (0);
959 	} else if (s1 > 59 || s2 > 59) {
960 		printf("Seconds must be between 0 and 59\n");
961 		return (0);
962 	} if (f1 > 74 || f2 > 74) {
963 		printf("Frames number must be between 0 and 74\n");
964 		return (0);
965 	}
966 
967 	if (tr1 > 0) {
968 		/*
969 		 * Start time is relative to tr1, Add start time of tr1
970 		 * to (m1,s1,f1) to yield absolute start time.
971 		 */
972 		toc2msf(tr1, &tm, &ts, &tf);
973 		addmsf(&m1, &s1, &f1, tm, ts, tf);
974 
975 		/* Compare (m1,s1,f1) to start time of next track. */
976 		toc2msf(tr1+1, &tm, &ts, &tf);
977 		if (cmpmsf(m1, s1, f1, tm, ts, tf) == 1) {
978 			printf("Track %u is not that long.\n", tr1);
979 			return (0);
980 		}
981 	}
982 
983 	toc2msf(n+1, &tm, &ts, &tf);
984 	if (cmpmsf(m1, s1, f1, tm, ts, tf) == 1) {
985 		printf("Start time is after end of disc.\n");
986 		return (0);
987 	}
988 
989 	if (tr2 > 0) {
990 		/*
991 		 * End time is relative to tr2, Add start time of tr2
992 		 * to (m2,s2,f2) to yield absolute end time.
993 		 */
994 		toc2msf(tr2, &tm, &ts, &tf);
995 		addmsf(&m2, &s2, &f2, tm, ts, tf);
996 
997 		/* Compare (m2,s2,f2) to start time of next track. */
998 		toc2msf(tr2+1, &tm, &ts, &tf);
999 		if (cmpmsf(m2, s2, f2, tm, ts, tf) == 1) {
1000 			printf("Track %u is not that long.\n", tr2);
1001 			return (0);
1002 		}
1003 	}
1004 
1005 	toc2msf(n+1, &tm, &ts, &tf);
1006 
1007 	if (!(tr2 || m2 || s2 || f2)) {
1008 		/* Play to end of disc. */
1009 		m2 = tm;
1010 		s2 = ts;
1011 		f2 = tf;
1012 	} else if (cmpmsf(m2, s2, f2, tm, ts, tf) == 1) {
1013 		printf("End time is after end of disc.\n");
1014 		return (0);
1015 	}
1016 
1017 	if (cmpmsf(m1, s1, f1, m2, s2, f2) == 1) {
1018 		printf("Start time is after end time.\n");
1019 		return (0);
1020 	}
1021 
1022 	return play_msf(m1, s1, f1, m2, s2, f2);
1023 }
1024 
1025 /* ARGSUSED */
1026 int
1027 play_prev(char *arg)
1028 {
1029 	int trk, min, sec, frm, rc;
1030 	struct ioc_toc_header h;
1031 
1032 	if (status(&trk, &min, &sec, &frm) >= 0) {
1033 		trk--;
1034 
1035 		rc = ioctl(fd, CDIOREADTOCHEADER, &h);
1036 		if (rc < 0) {
1037 			warn("getting toc header");
1038 			return (rc);
1039 		}
1040 
1041 		if (trk < h.starting_track)
1042 			return play_track(h.starting_track, 1,
1043 			    h.ending_track + 1, 1);
1044 		return play_track(trk, 1, h.ending_track, 1);
1045 	}
1046 
1047 	return (0);
1048 }
1049 
1050 /* ARGSUSED */
1051 int
1052 play_same(char *arg)
1053 {
1054 	int trk, min, sec, frm, rc;
1055 	struct ioc_toc_header h;
1056 
1057 	if (status (&trk, &min, &sec, &frm) >= 0) {
1058 		rc = ioctl(fd, CDIOREADTOCHEADER, &h);
1059 		if (rc < 0) {
1060 			warn("getting toc header");
1061 			return (rc);
1062 		}
1063 
1064 		return play_track(trk, 1, h.ending_track, 1);
1065 	}
1066 
1067 	return (0);
1068 }
1069 
1070 /* ARGSUSED */
1071 int
1072 play_next(char *arg)
1073 {
1074 	int trk, min, sec, frm, rc;
1075 	struct ioc_toc_header h;
1076 
1077 	if (status(&trk, &min, &sec, &frm) >= 0) {
1078 		trk++;
1079 		rc = ioctl(fd, CDIOREADTOCHEADER, &h);
1080 		if (rc < 0) {
1081 			warn("getting toc header");
1082 			return (rc);
1083 		}
1084 
1085 		if (trk > h.ending_track) {
1086 			printf("%s: end of CD\n", __progname);
1087 
1088 			rc = ioctl(fd, CDIOCSTOP);
1089 
1090 			(void) ioctl(fd, CDIOCALLOW);
1091 
1092 			return (rc);
1093 		}
1094 
1095 		return play_track(trk, 1, h.ending_track, 1);
1096 	}
1097 
1098 	return (0);
1099 }
1100 
1101 char *
1102 strstatus(int sts)
1103 {
1104 	switch (sts) {
1105 	case ASTS_INVALID:
1106 		return ("invalid");
1107 	case ASTS_PLAYING:
1108 		return ("playing");
1109 	case ASTS_PAUSED:
1110 		return ("paused");
1111 	case ASTS_COMPLETED:
1112 		return ("completed");
1113 	case ASTS_ERROR:
1114 		return ("error");
1115 	case ASTS_VOID:
1116 		return ("void");
1117 	default:
1118 		return ("??");
1119 	}
1120 }
1121 
1122 /* ARGSUSED */
1123 int
1124 pstatus(char *arg)
1125 {
1126 	struct ioc_vol v;
1127 	struct ioc_read_subchannel ss;
1128 	struct cd_sub_channel_info data;
1129 	int rc, trk, m, s, f;
1130 	char vis_catalog[1 + 4 * 15];
1131 
1132 	rc = status(&trk, &m, &s, &f);
1133 	if (rc >= 0) {
1134 		if (verbose) {
1135 			if (track_names)
1136 				printf("Audio status = %d<%s>, "
1137 				    "current track = %d (%s)\n"
1138 				    "\tcurrent position = %d:%02d.%02d\n",
1139 				    rc, strstatus(rc), trk,
1140 				    trk ? track_names[trk-1] : "", m, s, f);
1141 			else
1142 				printf("Audio status = %d<%s>, "
1143 				    "current track = %d, "
1144 				    "current position = %d:%02d.%02d\n",
1145 				    rc, strstatus(rc), trk, m, s, f);
1146 		} else
1147 			printf("%d %d %d:%02d.%02d\n", rc, trk, m, s, f);
1148 	} else
1149 		printf("No current status info available\n");
1150 
1151 	bzero(&ss, sizeof (ss));
1152 	ss.data = &data;
1153 	ss.data_len = sizeof (data);
1154 	ss.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
1155 	ss.data_format = CD_MEDIA_CATALOG;
1156 	rc = ioctl(fd, CDIOCREADSUBCHANNEL, (char *) &ss);
1157 	if (rc >= 0) {
1158 		printf("Media catalog is %sactive",
1159 		ss.data->what.media_catalog.mc_valid ? "": "in");
1160 		if (ss.data->what.media_catalog.mc_valid &&
1161 		    ss.data->what.media_catalog.mc_number[0]) {
1162 			strvisx(vis_catalog,
1163 			    (char *)ss.data->what.media_catalog.mc_number,
1164 			    15, VIS_SAFE);
1165 			printf(", number \"%.15s\"", vis_catalog);
1166 		}
1167 		putchar('\n');
1168 	} else
1169 		printf("No media catalog info available\n");
1170 
1171 	rc = ioctl(fd, CDIOCGETVOL, &v);
1172 	if (rc >= 0) {
1173 		if (verbose)
1174 			printf("Left volume = %d, right volume = %d\n",
1175 			    v.vol[0], v.vol[1]);
1176 		else
1177 			printf("%d %d\n", v.vol[0], v.vol[1]);
1178 	} else
1179 		printf("No volume level info available\n");
1180 	return(0);
1181 }
1182 
1183 int
1184 cdid(void)
1185 {
1186 	unsigned long id;
1187 	struct ioc_toc_header h;
1188 	int rc, n;
1189 
1190 	rc = ioctl(fd, CDIOREADTOCHEADER, &h);
1191 	if (rc == -1) {
1192 		warn("getting toc header");
1193 		return (rc);
1194 	}
1195 
1196 	n = h.ending_track - h.starting_track + 1;
1197 	rc = read_toc_entrys((n + 1) * sizeof (struct cd_toc_entry));
1198 	if (rc < 0)
1199 		return (rc);
1200 
1201 	id = cddb_discid(n, toc_buffer);
1202 	if (id) {
1203 		if (verbose)
1204 			printf("CDID=");
1205 		printf("%08lx\n", id);
1206 	}
1207 	return id ? 0 : 1;
1208 }
1209 
1210 /* ARGSUSED */
1211 int
1212 info(char *arg)
1213 {
1214 	struct ioc_toc_header h;
1215 	int rc, i, n;
1216 
1217 	rc = ioctl(fd, CDIOREADTOCHEADER, &h);
1218 	if (rc >= 0) {
1219 		if (verbose)
1220 			printf("Starting track = %d, ending track = %d, TOC size = %d bytes\n",
1221 			    h.starting_track, h.ending_track, h.len);
1222 		else
1223 			printf("%d %d %d\n", h.starting_track,
1224 			    h.ending_track, h.len);
1225 	} else {
1226 		warn("getting toc header");
1227 		return (rc);
1228 	}
1229 
1230 	n = h.ending_track - h.starting_track + 1;
1231 	rc = read_toc_entrys((n + 1) * sizeof (struct cd_toc_entry));
1232 	if (rc < 0)
1233 		return (rc);
1234 
1235 	if (verbose) {
1236 		printf("track     start  duration   block  length   type\n");
1237 		printf("-------------------------------------------------\n");
1238 	}
1239 
1240 	for (i = 0; i < n; i++) {
1241 		printf("%5d  ", toc_buffer[i].track);
1242 		prtrack(toc_buffer + i, 0, NULL);
1243 	}
1244 	printf("%5d  ", toc_buffer[n].track);
1245 	prtrack(toc_buffer + n, 1, NULL);
1246 	return (0);
1247 }
1248 
1249 int
1250 cddbinfo(char *arg)
1251 {
1252 	struct ioc_toc_header h;
1253 	int rc, i, n;
1254 
1255 	rc = ioctl(fd, CDIOREADTOCHEADER, &h);
1256 	if (rc == -1) {
1257 		warn("getting toc header");
1258 		return (rc);
1259 	}
1260 
1261 	n = h.ending_track - h.starting_track + 1;
1262 	rc = read_toc_entrys((n + 1) * sizeof (struct cd_toc_entry));
1263 	if (rc < 0)
1264 		return (rc);
1265 
1266 	if (track_names)
1267 		free_names(track_names);
1268 	track_names = NULL;
1269 
1270 	track_names = cddb(cddb_host, n, toc_buffer, arg);
1271 	if (!track_names)
1272 		return(0);
1273 
1274 	printf("-------------------------------------------------\n");
1275 
1276 	for (i = 0; i < n; i++) {
1277 		printf("%5d  ", toc_buffer[i].track);
1278 		prtrack(toc_buffer + i, 0, track_names[i]);
1279 	}
1280 	printf("%5d  ", toc_buffer[n].track);
1281 	prtrack(toc_buffer + n, 1, "");
1282 	return (0);
1283 }
1284 
1285 void
1286 lba2msf(unsigned long lba, u_char *m, u_char *s, u_char *f)
1287 {
1288 	lba += 150;		/* block start offset */
1289 	lba &= 0xffffff;	/* negative lbas use only 24 bits */
1290 	*m = lba / (60 * 75);
1291 	lba %= (60 * 75);
1292 	*s = lba / 75;
1293 	*f = lba % 75;
1294 }
1295 
1296 unsigned int
1297 msf2lba(u_char m, u_char s, u_char f)
1298 {
1299 	return (((m * 60) + s) * 75 + f) - 150;
1300 }
1301 
1302 unsigned long
1303 entry2time(struct cd_toc_entry *e)
1304 {
1305 	int block;
1306 	u_char m, s, f;
1307 
1308 	if (msf) {
1309 		return (e->addr.msf.minute * 60 + e->addr.msf.second);
1310 	} else {
1311 		block = e->addr.lba;
1312 		lba2msf(block, &m, &s, &f);
1313 		return (m*60+s);
1314 	}
1315 }
1316 
1317 unsigned long
1318 entry2frames(struct cd_toc_entry *e)
1319 {
1320 	int block;
1321 	unsigned char m, s, f;
1322 
1323 	if (msf) {
1324 		return e->addr.msf.frame + e->addr.msf.second * 75 +
1325 		    e->addr.msf.minute * 60 * 75;
1326 	} else {
1327 		block = e->addr.lba;
1328 		lba2msf(block, &m, &s, &f);
1329 		return f + s * 75 + m * 60 * 75;
1330 	}
1331 }
1332 
1333 void
1334 prtrack(struct cd_toc_entry *e, int lastflag, char *name)
1335 {
1336 	int block, next, len;
1337 	u_char m, s, f;
1338 
1339 	if (msf) {
1340 		if (!name || lastflag)
1341 			/* Print track start */
1342 			printf("%2d:%02d.%02d  ", e->addr.msf.minute,
1343 			    e->addr.msf.second, e->addr.msf.frame);
1344 
1345 		block = msf2lba(e->addr.msf.minute, e->addr.msf.second,
1346 			e->addr.msf.frame);
1347 	} else {
1348 		block = e->addr.lba;
1349 		if (!name || lastflag) {
1350 			lba2msf(block, &m, &s, &f);
1351 			/* Print track start */
1352 			printf("%2d:%02d.%02d  ", m, s, f);
1353 		}
1354 	}
1355 	if (lastflag) {
1356 		if (!name)
1357 			/* Last track -- print block */
1358 			printf("       -  %6d       -      -\n", block);
1359 		else
1360 			printf("\n");
1361 		return;
1362 	}
1363 
1364 	if (msf)
1365 		next = msf2lba(e[1].addr.msf.minute, e[1].addr.msf.second,
1366 			e[1].addr.msf.frame);
1367 	else
1368 		next = e[1].addr.lba;
1369 	len = next - block;
1370 	lba2msf(len - 150, &m, &s, &f);
1371 
1372 	if (name)
1373 		printf("%2d:%02d.%02d  %s\n", m, s, f, name);
1374 	/* Print duration, block, length, type */
1375 	else
1376 		printf("%2d:%02d.%02d  %6d  %6d  %5s\n", m, s, f, block, len,
1377 		    (e->control & 4) ? "data" : "audio");
1378 }
1379 
1380 int
1381 play_track(int tstart, int istart, int tend, int iend)
1382 {
1383 	struct ioc_play_track t;
1384 
1385 	t.start_track = tstart;
1386 	t.start_index = istart;
1387 	t.end_track = tend;
1388 	t.end_index = iend;
1389 
1390 	return ioctl(fd, CDIOCPLAYTRACKS, &t);
1391 }
1392 
1393 int
1394 play_blocks(int blk, int len)
1395 {
1396 	struct ioc_play_blocks  t;
1397 
1398 	t.blk = blk;
1399 	t.len = len;
1400 
1401 	return ioctl(fd, CDIOCPLAYBLOCKS, &t);
1402 }
1403 
1404 int
1405 setvol(int left, int right)
1406 {
1407 	struct ioc_vol  v;
1408 
1409 	v.vol[0] = left;
1410 	v.vol[1] = right;
1411 	v.vol[2] = 0;
1412 	v.vol[3] = 0;
1413 
1414 	return ioctl(fd, CDIOCSETVOL, &v);
1415 }
1416 
1417 int
1418 read_toc_entrys(int len)
1419 {
1420 	struct ioc_read_toc_entry t;
1421 
1422 	if (toc_buffer) {
1423 		free(toc_buffer);
1424 		toc_buffer = 0;
1425 	}
1426 
1427 	toc_buffer = malloc(len);
1428 
1429 	if (!toc_buffer) {
1430 		errno = ENOMEM;
1431 		return (-1);
1432 	}
1433 
1434 	t.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
1435 	t.starting_track = 0;
1436 	t.data_len = len;
1437 	t.data = toc_buffer;
1438 
1439 	return (ioctl(fd, CDIOREADTOCENTRYS, (char *) &t));
1440 }
1441 
1442 int
1443 play_msf(int start_m, int start_s, int start_f, int end_m, int end_s, int end_f)
1444 {
1445 	struct ioc_play_msf a;
1446 
1447 	a.start_m = start_m;
1448 	a.start_s = start_s;
1449 	a.start_f = start_f;
1450 	a.end_m = end_m;
1451 	a.end_s = end_s;
1452 	a.end_f = end_f;
1453 
1454 	return ioctl(fd, CDIOCPLAYMSF, (char *) &a);
1455 }
1456 
1457 int
1458 status(int *trk, int *min, int *sec, int *frame)
1459 {
1460 	struct ioc_read_subchannel s;
1461 	struct cd_sub_channel_info data;
1462 	u_char mm, ss, ff;
1463 
1464 	bzero(&s, sizeof (s));
1465 	s.data = &data;
1466 	s.data_len = sizeof (data);
1467 	s.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
1468 	s.data_format = CD_CURRENT_POSITION;
1469 
1470 	if (ioctl(fd, CDIOCREADSUBCHANNEL, (char *) &s) < 0)
1471 		return -1;
1472 
1473 	*trk = s.data->what.position.track_number;
1474 	if (msf) {
1475 		*min = s.data->what.position.reladdr.msf.minute;
1476 		*sec = s.data->what.position.reladdr.msf.second;
1477 		*frame = s.data->what.position.reladdr.msf.frame;
1478 	} else {
1479 		/*
1480 		 * NOTE: CDIOCREADSUBCHANNEL does not put the lba info into
1481 		 * host order like CDIOREADTOCENTRYS does.
1482 		 */
1483 		lba2msf(betoh32(s.data->what.position.reladdr.lba), &mm, &ss,
1484 		    &ff);
1485 		*min = mm;
1486 		*sec = ss;
1487 		*frame = ff;
1488 	}
1489 
1490 	return s.data->header.audio_status;
1491 }
1492 
1493 char *
1494 input(int *cmd)
1495 {
1496 	char *buf;
1497 	int siz = 0;
1498 	char *p;
1499 	HistEvent hev;
1500 
1501 	do {
1502 		if ((buf = (char *) el_gets(el, &siz)) == NULL || !siz) {
1503 			*cmd = CMD_QUIT;
1504 			fprintf(stderr, "\r\n");
1505 			return (0);
1506 		}
1507 		if (strlen(buf) > 1)
1508 			history(hist, &hev, H_ENTER, buf);
1509 		p = parse(buf, cmd);
1510 	} while (!p);
1511 	return (p);
1512 }
1513 
1514 char *
1515 parse(char *buf, int *cmd)
1516 {
1517 	struct cmdtab *c;
1518 	char *p;
1519 	size_t len;
1520 
1521 	for (p=buf; isspace(*p); p++)
1522 		continue;
1523 
1524 	if (isdigit(*p) || (p[0] == '#' && isdigit(p[1]))) {
1525 		*cmd = CMD_PLAY;
1526 		return (p);
1527 	}
1528 
1529 	for (buf = p; *p && ! isspace(*p); p++)
1530 		continue;
1531 
1532 	len = p - buf;
1533 	if (!len)
1534 		return (0);
1535 
1536 	if (*p) {		/* It must be a spacing character! */
1537 		char *q;
1538 
1539 		*p++ = 0;
1540 		for (q=p; *q && *q != '\n' && *q != '\r'; q++)
1541 			continue;
1542 		*q = 0;
1543 	}
1544 
1545 	*cmd = -1;
1546 	for (c=cmdtab; c->name; ++c) {
1547 		/* Is it an exact match? */
1548 		if (!strcasecmp(buf, c->name)) {
1549 			*cmd = c->command;
1550 			break;
1551 		}
1552 
1553 		/* Try short hand forms then... */
1554 		if (len >= c->min && ! strncasecmp(buf, c->name, len)) {
1555 			if (*cmd != -1 && *cmd != c->command) {
1556 				fprintf(stderr, "Ambiguous command\n");
1557 				return (0);
1558 			}
1559 			*cmd = c->command;
1560 		}
1561 	}
1562 
1563 	if (*cmd == -1) {
1564 		fprintf(stderr, "%s: Invalid command, enter ``help'' for commands.\n",
1565 		    __progname);
1566 		return (0);
1567 	}
1568 
1569 	while (isspace(*p))
1570 		p++;
1571 	return p;
1572 }
1573 
1574 int
1575 open_cd(char *dev, int needwrite)
1576 {
1577 	char *realdev;
1578 	int tries;
1579 
1580 	if (fd > -1) {
1581 		if (needwrite && !writeperm) {
1582 			close(fd);
1583 			fd = -1;
1584 		} else
1585 			return (1);
1586 	}
1587 
1588 	for (tries = 0; fd < 0 && tries < 10; tries++) {
1589 		if (needwrite)
1590 			fd = opendev(dev, O_RDWR, OPENDEV_PART, &realdev);
1591 		else
1592 			fd = opendev(dev, O_RDONLY, OPENDEV_PART, &realdev);
1593 		if (fd < 0) {
1594 			if (errno == ENXIO) {
1595 				/*  ENXIO has an overloaded meaning here.
1596 				 *  The original "Device not configured" should
1597 				 *  be interpreted as "No disc in drive %s". */
1598 				warnx("No disc in drive %s.", realdev);
1599 				return (0);
1600 			} else if (errno != EIO) {
1601 				/*  EIO may simply mean the device is not ready
1602 				 *  yet which is common with CD changers. */
1603 				warn("Can't open %s", realdev);
1604 				return (0);
1605 			}
1606 		}
1607 		sleep(1);
1608 	}
1609 	if (fd < 0) {
1610 		warn("Can't open %s", realdev);
1611 		return (0);
1612 	}
1613 	writeperm = needwrite;
1614 	return (1);
1615 }
1616 
1617 char *
1618 prompt(void)
1619 {
1620 	return (verbose ? "cdio> " : "");
1621 }
1622 
1623 void
1624 switch_el(void)
1625 {
1626 	HistEvent hev;
1627 
1628 	if (el == NULL && hist == NULL) {
1629 		el = el_init(__progname, stdin, stdout, stderr);
1630 		hist = history_init();
1631 		history(hist, &hev, H_SETSIZE, 100);
1632 		el_set(el, EL_HIST, history, hist);
1633 		el_set(el, EL_EDITOR, "emacs");
1634 		el_set(el, EL_PROMPT, prompt);
1635 		el_set(el, EL_SIGNAL, 1);
1636 		el_source(el, NULL);
1637 
1638 	} else {
1639 		if (hist != NULL) {
1640 			history_end(hist);
1641 			hist = NULL;
1642 		}
1643 		if (el != NULL) {
1644 			el_end(el);
1645 			el = NULL;
1646 		}
1647 	}
1648 }
1649 
1650 void
1651 addmsf(u_int *m, u_int *s, u_int *f, u_char m_inc, u_char s_inc, u_char f_inc)
1652 {
1653 	*f += f_inc;
1654 	if (*f > 75) {
1655 		*s += *f / 75;
1656 		*f %= 75;
1657 	}
1658 
1659 	*s += s_inc;
1660 	if (*s > 60) {
1661 		*m += *s / 60;
1662 		*s %= 60;
1663 	}
1664 
1665 	*m += m_inc;
1666 }
1667 
1668 int
1669 cmpmsf(u_char m1, u_char s1, u_char f1, u_char m2, u_char s2, u_char f2)
1670 {
1671 	if (m1 > m2)
1672 		return (1);
1673 	else if (m1 < m2)
1674 		return (-1);
1675 
1676 	if (s1 > s2)
1677 		return (1);
1678 	else if (s1 < s2)
1679 		return (-1);
1680 
1681 	if  (f1 > f2)
1682 		return (1);
1683 	else if (f1 < f2)
1684 		return (-1);
1685 
1686 	return (0);
1687 }
1688 
1689 void
1690 toc2msf(u_int track, u_char *m, u_char *s, u_char *f)
1691 {
1692 	struct cd_toc_entry *ctep;
1693 
1694 	ctep = &toc_buffer[track - 1];
1695 
1696 	if (msf) {
1697 		*m = ctep->addr.msf.minute;
1698 		*s = ctep->addr.msf.second;
1699 		*f = ctep->addr.msf.frame;
1700 	} else
1701 		lba2msf(ctep->addr.lba, m, s, f);
1702 }
1703