Home | History | Annotate | Line # | Download | only in cdplay
cdplay.c revision 1.22
      1  1.22        is /* 	$NetBSD: cdplay.c,v 1.22 2003/01/30 21:23:57 is Exp $	*/
      2   1.1        ad 
      3   1.1        ad /*
      4  1.15        ad  * Copyright (c) 1999, 2000, 2001 Andrew Doran.
      5   1.1        ad  * All rights reserved.
      6   1.1        ad  *
      7   1.1        ad  * Redistribution and use in source and binary forms, with or without
      8   1.1        ad  * modification, are permitted provided that the following conditions
      9   1.1        ad  * are met:
     10   1.1        ad  * 1. Redistributions of source code must retain the above copyright
     11   1.1        ad  *    notice, this list of conditions and the following disclaimer.
     12   1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     14   1.1        ad  *    documentation and/or other materials provided with the distribution.
     15   1.1        ad  *
     16   1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17   1.1        ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18   1.1        ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19   1.1        ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20   1.1        ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21   1.1        ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22   1.1        ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23   1.1        ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24   1.1        ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1        ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1        ad  * SUCH DAMAGE.
     27   1.1        ad  *
     28   1.1        ad  */
     29   1.1        ad 
     30   1.1        ad /*
     31  1.15        ad  * Compact Disc Control Utility, originally by Serge V. Vakulenko
     32  1.15        ad  * <vak (at) cronyx.ru>.  First appeared in FreeBSD under the guise of
     33  1.15        ad  * cdcontrol(1).  Based on the non-X based CD player by Jean-Marc
     34  1.15        ad  * Zucconi and Andrey A.  Chernov.  Fixed and further modified on
     35  1.15        ad  * by Jukka Ukkonen <jau (at) funet.fi>.  Lots of fixes and improvements
     36  1.15        ad  * made subsequently by The NetBSD Project.
     37   1.1        ad  *
     38   1.6        ad  * from FreeBSD: cdcontrol.c,v 1.17.2.1 1999/01/31 15:36:01 billf Exp
     39   1.1        ad  */
     40   1.1        ad 
     41   1.1        ad #include <sys/cdefs.h>
     42   1.1        ad #ifndef lint
     43  1.22        is __RCSID("$NetBSD: cdplay.c,v 1.22 2003/01/30 21:23:57 is Exp $");
     44   1.1        ad #endif /* not lint */
     45  1.19   thorpej 
     46  1.19   thorpej #include <sys/types.h>
     47   1.1        ad 
     48   1.5        ad #include <sys/endian.h>
     49   1.5        ad #include <sys/ioctl.h>
     50   1.5        ad #include <sys/file.h>
     51   1.5        ad #include <sys/cdio.h>
     52   1.5        ad 
     53  1.22        is #include <assert.h>
     54  1.22        is 
     55   1.1        ad #include <ctype.h>
     56   1.1        ad #include <err.h>
     57   1.1        ad #include <errno.h>
     58   1.4   msaitoh #include <histedit.h>
     59  1.21        is #include <limits.h>
     60  1.16        ad #include <signal.h>
     61   1.1        ad #include <stdio.h>
     62   1.1        ad #include <stdlib.h>
     63   1.1        ad #include <string.h>
     64   1.1        ad #include <unistd.h>
     65  1.11     lukem #include <util.h>
     66   1.1        ad 
     67  1.15        ad enum cmd {
     68  1.16        ad 	CMD_CLOSE,
     69  1.15        ad 	CMD_EJECT,
     70  1.15        ad 	CMD_HELP,
     71  1.15        ad 	CMD_INFO,
     72  1.16        ad 	CMD_NEXT,
     73  1.15        ad 	CMD_PAUSE,
     74  1.15        ad 	CMD_PLAY,
     75  1.16        ad 	CMD_PREV,
     76  1.15        ad 	CMD_QUIT,
     77  1.16        ad 	CMD_RESET,
     78  1.15        ad 	CMD_RESUME,
     79  1.16        ad 	CMD_SET,
     80  1.16        ad 	CMD_SHUFFLE,
     81  1.16        ad 	CMD_SKIP,
     82  1.16        ad 	CMD_STATUS,
     83  1.15        ad 	CMD_STOP,
     84  1.15        ad 	CMD_VOLUME,
     85  1.15        ad };
     86   1.1        ad 
     87   1.1        ad struct cmdtab {
     88  1.15        ad 	enum cmd	command;
     89  1.15        ad 	const char	*name;
     90  1.15        ad 	unsigned int	min;
     91  1.15        ad 	const char	*args;
     92  1.15        ad } const cmdtab[] = {
     93  1.16        ad 	{ CMD_HELP,	"?",	   1, 0 },
     94  1.16        ad 	{ CMD_CLOSE,	"close",   1, NULL },
     95  1.16        ad 	{ CMD_EJECT,	"eject",   1, NULL },
     96  1.16        ad 	{ CMD_HELP,	"help",    1, NULL },
     97  1.16        ad 	{ CMD_INFO,	"info",    1, NULL },
     98  1.16        ad 	{ CMD_NEXT,	"next",    1, NULL },
     99  1.16        ad 	{ CMD_PAUSE,	"pause",   2, NULL },
    100  1.16        ad 	{ CMD_PLAY,	"play",    1, "min1:sec1[.fram1] [min2:sec2[.fram2]]" },
    101  1.16        ad 	{ CMD_PLAY,	"play",    1, "track1[.index1] [track2[.index2]]" },
    102  1.16        ad 	{ CMD_PLAY,	"play",    1, "tr1 m1:s1[.f1] [[tr2] [m2:s2[.f2]]]" },
    103  1.16        ad 	{ CMD_PLAY,	"play",    1, "[#block [len]]" },
    104  1.16        ad 	{ CMD_PREV,	"prev",    2, NULL },
    105  1.16        ad 	{ CMD_QUIT,	"quit",    1, NULL },
    106  1.16        ad 	{ CMD_RESET,	"reset",   4, NULL },
    107  1.16        ad 	{ CMD_RESUME,	"resume",  4, NULL },
    108  1.16        ad 	{ CMD_SET,	"set",     2, "msf | lba" },
    109  1.16        ad 	{ CMD_SHUFFLE,	"shuffle", 2, NULL },
    110  1.16        ad 	{ CMD_SKIP,	"skip",    2, NULL },
    111  1.16        ad 	{ CMD_STATUS,	"status",  3, NULL },
    112  1.16        ad 	{ CMD_STOP,	"stop",    3, NULL },
    113  1.16        ad 	{ CMD_VOLUME,	"volume",  1, "<l> <r>|left|right|mute|mono|stereo" },
    114   1.1        ad };
    115   1.1        ad 
    116   1.1        ad struct cd_toc_entry toc_buffer[100];
    117   1.1        ad 
    118   1.1        ad const char *cdname;
    119   1.1        ad int     fd = -1;
    120   1.1        ad int     msf = 1;
    121  1.16        ad int	shuffle;
    122  1.16        ad int	interactive = 1;
    123  1.16        ad struct	itimerval itv_timer;
    124   1.1        ad 
    125   1.4   msaitoh History *hist;
    126   1.4   msaitoh HistEvent he;
    127   1.4   msaitoh EditLine *elptr;
    128   1.4   msaitoh 
    129  1.15        ad int	get_vol(int *, int *);
    130  1.16        ad int	get_status(int *, int *, int *, int *, int *);
    131  1.15        ad void	help(void);
    132  1.15        ad int	info(const char *);
    133  1.15        ad void	lba2msf(u_long, u_int *, u_int *, u_int *);
    134  1.15        ad int 	main(int, char **);
    135  1.15        ad u_int	msf2lba(u_int, u_int, u_int);
    136  1.15        ad int	opencd(void);
    137  1.15        ad const char   *parse(char *, int *);
    138  1.16        ad int	play(const char *, int);
    139  1.15        ad int	play_blocks(int, int);
    140  1.15        ad int	play_msf(int, int, int, int, int, int);
    141  1.15        ad int	play_track(int, int, int, int);
    142  1.15        ad int	print_status(const char *);
    143  1.15        ad void	print_track(struct cd_toc_entry *, int);
    144  1.15        ad const char	*prompt(void);
    145  1.15        ad int	read_toc_entrys(int);
    146  1.15        ad int	run(int, const char *);
    147  1.15        ad int	setvol(int, int);
    148  1.16        ad void	sig_timer(int, int, struct sigcontext *);
    149  1.16        ad int	skip(int, int);
    150  1.15        ad const char	*strstatus(int);
    151  1.15        ad void 	usage(void);
    152   1.1        ad 
    153  1.22        is void	toc2msf(u_int, u_int *, u_int *, u_int *);
    154  1.22        is int	toc2lba(u_int);
    155  1.22        is void	addmsf(u_int *, u_int *, u_int *, u_int, u_int, u_int);
    156  1.22        is 
    157  1.15        ad int
    158  1.15        ad main(int argc, char **argv)
    159   1.1        ad {
    160  1.15        ad 	const char *arg;
    161  1.15        ad 	char buf[80], *p;
    162   1.5        ad 	static char defdev[16];
    163  1.15        ad 	int cmd, len, c;
    164   1.4   msaitoh 	char *line;
    165   1.4   msaitoh 	const char *elline;
    166  1.16        ad 	int scratch, rv;
    167  1.16        ad 	struct sigaction sa_timer;
    168   1.1        ad 
    169   1.1        ad 	cdname = getenv("MUSIC_CD");
    170  1.15        ad 	if (cdname == NULL)
    171   1.1        ad 		cdname = getenv("CD_DRIVE");
    172  1.15        ad 	if (cdname == NULL)
    173   1.1        ad 		cdname = getenv("DISC");
    174  1.15        ad 	if (cdname == NULL)
    175   1.1        ad 		cdname = getenv("CDPLAY");
    176   1.1        ad 
    177  1.15        ad 	while ((c = getopt(argc, argv, "f:h")) != -1)
    178  1.15        ad 		switch (c) {
    179   1.1        ad 		case 'f':
    180   1.1        ad 			cdname = optarg;
    181   1.1        ad 			continue;
    182   1.1        ad 		case 'h':
    183   1.1        ad 		default:
    184   1.1        ad 			usage();
    185  1.15        ad 			/* NOTREACHED */
    186   1.1        ad 		}
    187   1.1        ad 	argc -= optind;
    188   1.1        ad 	argv += optind;
    189   1.1        ad 
    190  1.18        ad 	if (argc > 0 && strcasecmp(*argv, "help") == 0)
    191   1.1        ad 		usage();
    192   1.1        ad 
    193  1.15        ad 	if (cdname == NULL) {
    194  1.14        ad 		sprintf(defdev, "cd0%c", 'a' + getrawpartition());
    195   1.1        ad 		cdname = defdev;
    196   1.1        ad 	}
    197  1.15        ad 
    198   1.3        ad 	opencd();
    199  1.16        ad 	srandom(time(NULL));
    200  1.16        ad 
    201   1.1        ad 	if (argc > 0) {
    202  1.16        ad 		interactive = 0;
    203  1.15        ad 		for (p = buf; argc-- > 0; argv++) {
    204   1.1        ad 			len = strlen(*argv);
    205   1.1        ad 
    206   1.1        ad 			if (p + len >= buf + sizeof(buf) - 1)
    207   1.1        ad 				usage();
    208   1.1        ad 			if (p > buf)
    209   1.1        ad 				*p++ = ' ';
    210   1.1        ad 
    211   1.1        ad 			strcpy(p, *argv);
    212   1.1        ad 			p += len;
    213   1.1        ad 		}
    214  1.15        ad 		*p = '\0';
    215   1.1        ad 		arg = parse(buf, &cmd);
    216   1.1        ad 		return (run(cmd, arg));
    217   1.1        ad 	}
    218  1.15        ad 
    219   1.1        ad 	printf("Type `?' for command list\n\n");
    220   1.1        ad 
    221   1.4   msaitoh 	hist = history_init();
    222   1.4   msaitoh 	history(hist, &he, H_SETSIZE, 100);	/* 100 elt history buffer */
    223  1.12       cgd 	elptr = el_init(getprogname(), stdin, stdout, stderr);
    224   1.4   msaitoh 	el_set(elptr, EL_EDITOR, "emacs");
    225   1.4   msaitoh 	el_set(elptr, EL_PROMPT, prompt);
    226   1.4   msaitoh 	el_set(elptr, EL_HIST, history, hist);
    227   1.4   msaitoh 	el_source(elptr, NULL);
    228   1.4   msaitoh 
    229  1.16        ad 	sigemptyset(&sa_timer.sa_mask);
    230  1.16        ad 	sa_timer.sa_handler = (void (*)(int))sig_timer;
    231  1.16        ad 	sa_timer.sa_flags = SA_RESTART;
    232  1.16        ad 	if ((rv = sigaction(SIGALRM, &sa_timer, NULL)) < 0)
    233  1.16        ad 		err(EXIT_FAILURE, "sigaction()");
    234  1.16        ad 
    235   1.1        ad 	for (;;) {
    236   1.4   msaitoh 		line = NULL;
    237   1.4   msaitoh 		do {
    238   1.4   msaitoh 			if (((elline = el_gets(elptr, &scratch)) != NULL)
    239   1.4   msaitoh 			    && (scratch != 0)){
    240   1.4   msaitoh 				history(hist, &he, H_ENTER, elline);
    241   1.4   msaitoh 				line = strdup(elline);
    242   1.4   msaitoh 				arg = parse(line, &cmd);
    243   1.4   msaitoh 			} else {
    244   1.4   msaitoh 				cmd = CMD_QUIT;
    245   1.4   msaitoh 				fprintf(stderr, "\r\n");
    246   1.4   msaitoh 				arg = 0;
    247   1.4   msaitoh 				break;
    248   1.4   msaitoh 			}
    249  1.15        ad 		} while (arg == NULL);
    250   1.4   msaitoh 
    251   1.1        ad 		if (run(cmd, arg) < 0) {
    252  1.15        ad 			if (fd != -1)
    253  1.15        ad 				close(fd);
    254   1.1        ad 			fd = -1;
    255   1.1        ad 		}
    256   1.1        ad 		fflush(stdout);
    257   1.4   msaitoh 		if (line != NULL)
    258   1.4   msaitoh 			free(line);
    259   1.1        ad 	}
    260  1.15        ad 
    261   1.4   msaitoh 	el_end(elptr);
    262   1.4   msaitoh 	history_end(hist);
    263  1.15        ad 	exit(EXIT_SUCCESS);
    264  1.15        ad 	/* NOTREACHED */
    265   1.1        ad }
    266   1.1        ad 
    267  1.15        ad void
    268  1.15        ad usage(void)
    269   1.1        ad {
    270   1.1        ad 
    271  1.15        ad 	fprintf(stderr, "usage: cdplay [-f device] [command ...]\n");
    272  1.15        ad 	exit(EXIT_FAILURE);
    273  1.15        ad 	/* NOTREACHED */
    274  1.15        ad }
    275  1.15        ad 
    276  1.15        ad void
    277  1.15        ad help(void)
    278  1.15        ad {
    279  1.15        ad 	const struct cmdtab *c, *mc;
    280  1.15        ad 	const char *s;
    281  1.15        ad 	int i, n;
    282  1.15        ad 
    283  1.15        ad 	mc = cmdtab + sizeof(cmdtab) / sizeof(cmdtab[0]);
    284  1.15        ad 	for (c = cmdtab; c < mc; c++) {
    285  1.15        ad 		for (i = c->min, s = c->name; *s != '\0'; s++, i--) {
    286  1.15        ad 			n = (i > 0 ? toupper(*s) : *s);
    287  1.15        ad 			putchar(n);
    288   1.5        ad 		}
    289  1.15        ad 		if (c->args != NULL)
    290  1.15        ad 			printf(" %s", c->args);
    291  1.15        ad 		putchar('\n');
    292  1.15        ad 	}
    293  1.15        ad 	printf(
    294  1.15        ad 	    "\nThe word \"play\" is not required for the play commands.\n"
    295  1.15        ad 	    "The plain target address is taken as a synonym for play.\n");
    296  1.15        ad }
    297  1.15        ad 
    298  1.15        ad int
    299  1.15        ad run(int cmd, const char *arg)
    300  1.15        ad {
    301  1.15        ad 	int l, r, rv;
    302  1.15        ad 
    303  1.15        ad 	if (cmd == CMD_QUIT) {
    304  1.16        ad 		close(fd);
    305  1.15        ad 		exit(EXIT_SUCCESS);
    306  1.15        ad 		/* NOTREACHED */
    307   1.5        ad 	}
    308  1.15        ad 
    309   1.5        ad 	if (fd < 0 && !opencd())
    310   1.5        ad 		return (0);
    311  1.15        ad 
    312   1.5        ad 	switch (cmd) {
    313   1.1        ad 	case CMD_INFO:
    314  1.15        ad 		rv = info(arg);
    315  1.15        ad 		break;
    316   1.1        ad 
    317   1.1        ad 	case CMD_STATUS:
    318  1.15        ad 		rv = print_status(arg);
    319  1.15        ad 		break;
    320   1.1        ad 
    321   1.1        ad 	case CMD_PAUSE:
    322  1.15        ad 		if ((rv = ioctl(fd, CDIOCPAUSE)) < 0)
    323  1.15        ad 			warn("ioctl(CDIOCPAUSE)");
    324  1.15        ad 		break;
    325   1.1        ad 
    326   1.1        ad 	case CMD_RESUME:
    327  1.15        ad 		if ((rv = ioctl(fd, CDIOCRESUME)) < 0)
    328  1.15        ad 			warn("ioctl(CDIOCRESUME)");
    329  1.15        ad 		break;
    330   1.1        ad 
    331   1.1        ad 	case CMD_STOP:
    332  1.15        ad 		if ((rv = ioctl(fd, CDIOCSTOP)) < 0)
    333  1.15        ad 			warn("ioctl(CDIOCSTOP)");
    334  1.15        ad 		if (ioctl(fd, CDIOCALLOW) < 0)
    335  1.15        ad 			warn("ioctl(CDIOCALLOW)");
    336  1.15        ad 		break;
    337   1.1        ad 
    338   1.1        ad 	case CMD_RESET:
    339  1.15        ad 		if ((rv = ioctl(fd, CDIOCRESET)) >= 0) {
    340  1.15        ad 			close(fd);
    341  1.15        ad 			fd = -1;
    342  1.15        ad 		} else
    343  1.15        ad 			warn("ioctl(CDIOCRESET)");
    344   1.1        ad 		return (0);
    345   1.1        ad 
    346   1.1        ad 	case CMD_EJECT:
    347  1.16        ad 		if (shuffle)
    348  1.16        ad 			run(CMD_SHUFFLE, NULL);
    349  1.15        ad 		if (ioctl(fd, CDIOCALLOW) < 0)
    350  1.15        ad 			warn("ioctl(CDIOCALLOW)");
    351  1.15        ad 		if ((rv = ioctl(fd, CDIOCEJECT)) < 0)
    352  1.15        ad 			warn("ioctl(CDIOCEJECT)");
    353  1.15        ad 		break;
    354   1.1        ad 
    355   1.1        ad 	case CMD_CLOSE:
    356   1.3        ad 		ioctl(fd, CDIOCALLOW);
    357  1.15        ad 		if ((rv = ioctl(fd, CDIOCCLOSE)) >= 0) {
    358  1.15        ad 			close(fd);
    359  1.15        ad 			fd = -1;
    360  1.15        ad 		} else
    361  1.15        ad 			warn("ioctl(CDIOCCLOSE)");
    362  1.15        ad 		break;
    363   1.1        ad 
    364   1.1        ad 	case CMD_PLAY:
    365   1.1        ad 		while (isspace(*arg))
    366   1.1        ad 			arg++;
    367  1.16        ad 		rv = play(arg, 1);
    368  1.15        ad 		break;
    369   1.1        ad 
    370  1.13  gmcgarry 	case CMD_PREV:
    371  1.16        ad 		rv = skip(-1, 1);
    372  1.15        ad 		break;
    373  1.13  gmcgarry 
    374  1.13  gmcgarry 	case CMD_NEXT:
    375  1.16        ad 		rv = skip(1, 1);
    376  1.16        ad 		break;
    377  1.16        ad 
    378  1.16        ad 	case CMD_SHUFFLE:
    379  1.16        ad 		if (interactive == 0)
    380  1.16        ad 			errx(EXIT_FAILURE,
    381  1.16        ad 			    "`shuffle' valid only in interactive mode");
    382  1.16        ad 		if (shuffle == 0) {
    383  1.16        ad 			itv_timer.it_interval.tv_sec = 1;
    384  1.16        ad 			itv_timer.it_interval.tv_usec = 0;
    385  1.16        ad 			itv_timer.it_value.tv_sec = 1;
    386  1.16        ad 			itv_timer.it_value.tv_usec = 0;
    387  1.16        ad 			if (setitimer(ITIMER_REAL, &itv_timer, NULL) == 0) {
    388  1.16        ad 				shuffle = 1;
    389  1.16        ad 				skip(0, 1);
    390  1.16        ad 			}
    391  1.16        ad 		} else {
    392  1.16        ad 			itv_timer.it_interval.tv_sec = 0;
    393  1.16        ad 			itv_timer.it_interval.tv_usec = 0;
    394  1.16        ad 			itv_timer.it_value.tv_sec = 0;
    395  1.16        ad 			itv_timer.it_value.tv_usec = 0;
    396  1.16        ad 			if (setitimer(ITIMER_REAL, &itv_timer, NULL) == 0)
    397  1.16        ad 				shuffle = 0;
    398  1.16        ad 		}
    399  1.16        ad 		printf("shuffle play:\t%s\n", shuffle ? "on" : "off");
    400  1.16        ad 		rv = 0;
    401  1.16        ad 		break;
    402  1.16        ad 
    403  1.16        ad 	case CMD_SKIP:
    404  1.16        ad 		if (!interactive)
    405  1.16        ad 			errx(EXIT_FAILURE,
    406  1.16        ad 			    "`skip' valid only in interactive mode");
    407  1.16        ad 		if (!shuffle)
    408  1.16        ad 			warnx("`skip' valid only in shuffle mode");
    409  1.16        ad 		else
    410  1.16        ad 			skip(0, 1);
    411  1.15        ad 		break;
    412  1.15        ad 
    413   1.1        ad 	case CMD_SET:
    414  1.15        ad 		if (strcasecmp(arg, "msf") == 0)
    415   1.1        ad 			msf = 1;
    416  1.15        ad 		else if (strcasecmp(arg, "lba") == 0)
    417   1.5        ad 			msf = 0;
    418   1.1        ad 		else
    419   1.5        ad 			warnx("invalid command arguments");
    420  1.15        ad 		break;
    421   1.1        ad 
    422   1.1        ad 	case CMD_VOLUME:
    423  1.15        ad 		if (strncasecmp(arg, "left", strlen(arg)) == 0)
    424  1.15        ad 			rv = ioctl(fd, CDIOCSETLEFT);
    425  1.15        ad 		else if (strncasecmp(arg, "right", strlen(arg)) == 0)
    426  1.15        ad 			rv = ioctl(fd, CDIOCSETRIGHT);
    427  1.15        ad 		else if (strncasecmp(arg, "mono", strlen(arg)) == 0)
    428  1.15        ad 			rv = ioctl(fd, CDIOCSETMONO);
    429  1.15        ad 		else if (strncasecmp(arg, "stereo", strlen(arg)) == 0)
    430  1.15        ad 			rv = ioctl(fd, CDIOCSETSTEREO);
    431  1.15        ad 		else if (strncasecmp(arg, "mute", strlen(arg)) == 0)
    432  1.15        ad 			rv = ioctl(fd, CDIOCSETMUTE);
    433  1.15        ad 		else {
    434  1.15        ad 			rv = 0;
    435  1.15        ad 			if (sscanf(arg, "%d %d", &l, &r) != 2) {
    436  1.15        ad 				if (sscanf(arg, "%d", &l) == 1)
    437  1.15        ad 					r = l;
    438  1.15        ad 				else {
    439  1.15        ad 					warnx("invalid command arguments");
    440  1.15        ad 					break;
    441  1.15        ad 				}
    442   1.9       abs 			}
    443  1.15        ad 			rv = setvol(l, r);
    444   1.1        ad 		}
    445  1.15        ad 		break;
    446  1.15        ad 
    447   1.1        ad 	case CMD_HELP:
    448   1.1        ad 	default:
    449   1.1        ad 		help();
    450  1.15        ad 		rv = 0;
    451  1.15        ad 		break;
    452  1.15        ad 	}
    453   1.1        ad 
    454  1.15        ad 	return (rv);
    455   1.1        ad }
    456   1.1        ad 
    457  1.15        ad int
    458  1.16        ad play(const char *arg, int fromuser)
    459   1.1        ad {
    460  1.22        is 	int rv, n, start, end, istart, iend, blk, len, relend;
    461  1.15        ad 	u_int tr1, tr2, m1, m2, s1, s2, f1, f2, tm, ts, tf;
    462   1.1        ad 	struct ioc_toc_header h;
    463  1.15        ad 
    464  1.16        ad 	if (shuffle && fromuser) {
    465  1.17        ad 		warnx("`play' not valid in shuffle mode");
    466  1.16        ad 		return (0);
    467  1.16        ad 	}
    468  1.16        ad 
    469  1.15        ad 	if ((rv = ioctl(fd, CDIOREADTOCHEADER, &h)) <  0) {
    470  1.15        ad 		warn("ioctl(CDIOREADTOCHEADER)");
    471  1.15        ad 		return (rv);
    472  1.15        ad 	}
    473   1.1        ad 
    474   1.2        ad 	end = 0;
    475   1.2        ad 	istart = iend = 1;
    476   1.1        ad 	n = h.ending_track - h.starting_track + 1;
    477  1.15        ad 	rv = read_toc_entrys((n + 1) * sizeof(struct cd_toc_entry));
    478  1.15        ad 	if (rv < 0)
    479  1.15        ad 		return (rv);
    480   1.1        ad 
    481  1.15        ad 	if (arg == NULL || *arg == '\0') {
    482   1.1        ad 		/* Play the whole disc */
    483  1.16        ad 		return (play_track(h.starting_track, 1, h.ending_track, 99));
    484   1.1        ad 	}
    485  1.15        ad 
    486  1.15        ad 	if (strchr(arg, '#') != NULL) {
    487   1.1        ad 		/* Play block #blk [ len ] */
    488   1.3        ad 		len = 0;
    489   1.1        ad 
    490   1.1        ad 		if (2 != sscanf(arg, "#%d%d", &blk, &len) &&
    491   1.1        ad 		    1 != sscanf(arg, "#%d", &blk))
    492   1.1        ad 			goto Clean_up;
    493   1.1        ad 
    494   1.1        ad 		if (len == 0) {
    495  1.22        is 			len = toc2lba(n);
    496   1.1        ad 		}
    497   1.3        ad 		return (play_blocks(blk, len));
    498   1.1        ad 	}
    499  1.15        ad 
    500  1.15        ad 	if (strchr(arg, ':') != NULL) {
    501   1.1        ad 		/*
    502   1.1        ad 		 * Play MSF m1:s1 [ .f1 ] [ m2:s2 [ .f2 ] ]
    503   1.1        ad 		 *
    504   1.1        ad 		 * Will now also undestand timed addresses relative
    505   1.1        ad 		 * to the beginning of a track in the form...
    506   1.1        ad 		 *
    507   1.1        ad 		 *      tr1 m1:s1[.f1] [[tr2] [m2:s2[.f2]]]
    508   1.1        ad 		 */
    509  1.22        is 		relend = 1;
    510   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    511  1.15        ad 		if (8 == sscanf(arg, "%d %d:%d.%d %d %d:%d.%d", &tr1, &m1,
    512   1.3        ad 		    &s1, &f1, &tr2, &m2, &s2, &f2))
    513   1.1        ad 			goto Play_Relative_Addresses;
    514   1.1        ad 
    515   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    516  1.15        ad 		if (7 == sscanf(arg, "%d %d:%d %d %d:%d.%d", &tr1, &m1, &s1,
    517   1.3        ad 		    &tr2, &m2, &s2, &f2))
    518   1.1        ad 			goto Play_Relative_Addresses;
    519   1.1        ad 
    520   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    521  1.15        ad 		if (7 == sscanf(arg, "%d %d:%d.%d %d %d:%d", &tr1, &m1, &s1,
    522   1.3        ad 		    &f1, &tr2, &m2, &s2))
    523   1.1        ad 			goto Play_Relative_Addresses;
    524   1.1        ad 
    525   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    526  1.15        ad 		if (7 == sscanf(arg, "%d %d:%d.%d %d:%d.%d", &tr1, &m1, &s1,
    527   1.3        ad 		    &f1, &m2, &s2, &f2))
    528   1.1        ad 			goto Play_Relative_Addresses;
    529   1.1        ad 
    530   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    531   1.3        ad 		if (6 == sscanf(arg, "%d %d:%d.%d %d:%d", &tr1, &m1, &s1, &f1,
    532   1.3        ad 		    &m2, &s2))
    533   1.1        ad 			goto Play_Relative_Addresses;
    534   1.1        ad 
    535   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    536  1.15        ad 		if (6 == sscanf(arg, "%d %d:%d %d:%d.%d", &tr1, &m1, &s1, &m2,
    537   1.3        ad 		    &s2, &f2))
    538   1.1        ad 			goto Play_Relative_Addresses;
    539   1.1        ad 
    540   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    541  1.15        ad 		if (6 == sscanf(arg, "%d %d:%d.%d %d %d", &tr1, &m1, &s1, &f1,
    542   1.3        ad 		    &tr2, &m2))
    543   1.1        ad 			goto Play_Relative_Addresses;
    544   1.1        ad 
    545   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    546  1.22        is 		if (6 == sscanf(arg, "%d %d:%d %d %d:%d", &tr1, &m1, &s1, &tr2,
    547  1.22        is 		    &m2, &s2))
    548  1.22        is 			goto Play_Relative_Addresses;
    549  1.22        is 
    550  1.22        is 		tr2 = m2 = s2 = f2 = f1 = 0;
    551  1.15        ad 		if (5 == sscanf(arg, "%d %d:%d %d:%d", &tr1, &m1, &s1, &m2,
    552   1.3        ad 		    &s2))
    553   1.1        ad 			goto Play_Relative_Addresses;
    554   1.1        ad 
    555   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    556  1.15        ad 		if (5 == sscanf(arg, "%d %d:%d %d %d", &tr1, &m1, &s1, &tr2,
    557   1.3        ad 		    &m2))
    558   1.1        ad 			goto Play_Relative_Addresses;
    559   1.1        ad 
    560  1.22        is 		relend=0;
    561   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    562  1.15        ad 		if (5 == sscanf(arg, "%d %d:%d.%d %d", &tr1, &m1, &s1, &f1,
    563   1.3        ad 		    &tr2))
    564   1.1        ad 			goto Play_Relative_Addresses;
    565   1.1        ad 
    566   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    567   1.1        ad 		if (4 == sscanf(arg, "%d %d:%d %d", &tr1, &m1, &s1, &tr2))
    568   1.1        ad 			goto Play_Relative_Addresses;
    569   1.1        ad 
    570   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    571   1.1        ad 		if (4 == sscanf(arg, "%d %d:%d.%d", &tr1, &m1, &s1, &f1))
    572   1.1        ad 			goto Play_Relative_Addresses;
    573   1.1        ad 
    574   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    575   1.1        ad 		if (3 == sscanf(arg, "%d %d:%d", &tr1, &m1, &s1))
    576   1.1        ad 			goto Play_Relative_Addresses;
    577   1.1        ad 
    578   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    579   1.1        ad 		goto Try_Absolute_Timed_Addresses;
    580   1.1        ad 
    581   1.1        ad Play_Relative_Addresses:
    582   1.1        ad 		if (!tr1)
    583   1.1        ad 			tr1 = 1;
    584   1.2        ad 		else if (tr1 > n)
    585   1.2        ad 			tr1 = n;
    586   1.1        ad 
    587  1.22        is 		toc2msf(tr1-1, &tm, &ts, &tf);
    588  1.22        is 		addmsf(&m1, &s1, &f1, tm, ts, tf);
    589  1.22        is 
    590  1.22        is 		toc2msf(tr1, &tm, &ts, &tf);
    591  1.22        is 
    592  1.15        ad 		if ((m1 > tm) || ((m1 == tm) && ((s1 > ts) || ((s1 == ts) &&
    593   1.2        ad 		    (f1 > tf))))) {
    594  1.20    itojun 			warnx("Track %d is not that long.", tr1);
    595   1.1        ad 			return (0);
    596   1.1        ad 		}
    597  1.22        is 		tr1--;	/* XXXXX ???? */
    598   1.1        ad 
    599   1.1        ad 
    600   1.1        ad 		if (!tr2) {
    601  1.22        is 			if (relend) {
    602   1.1        ad 				tr2 = tr1;
    603  1.22        is 
    604  1.22        is 				addmsf(&m2, &s2, &f2, m1, s1, f1);
    605   1.1        ad 			} else {
    606   1.1        ad 				tr2 = n;
    607  1.22        is 
    608  1.22        is 				toc2msf(n, &m2, &s2, &f2);
    609   1.1        ad 			}
    610   1.1        ad 		} else {
    611   1.1        ad 			if (tr2 > n) {
    612   1.1        ad 				tr2 = n;
    613   1.1        ad 				m2 = s2 = f2 = 0;
    614   1.1        ad 			} else {
    615  1.22        is 				if (relend)
    616   1.1        ad 					tr2--;
    617  1.22        is 
    618  1.22        is 				toc2msf(tr2, &tm, &ts, &tf);
    619  1.22        is 				addmsf(&m2, &s2, &f2, tm, ts, tf);
    620   1.1        ad 			}
    621   1.1        ad 		}
    622   1.1        ad 
    623  1.22        is 		toc2msf(n, &tm, &ts, &tf);
    624   1.2        ad 
    625  1.15        ad 		if ((tr2 < n) && ((m2 > tm) || ((m2 == tm) && ((s2 > ts) ||
    626   1.2        ad 		    ((s2 == ts) && (f2 > tf)))))) {
    627  1.20    itojun 			warnx("The playing time of the disc is not that long.");
    628   1.1        ad 			return (0);
    629   1.1        ad 		}
    630   1.2        ad 
    631   1.1        ad 		return (play_msf(m1, s1, f1, m2, s2, f2));
    632   1.1        ad 
    633   1.1        ad Try_Absolute_Timed_Addresses:
    634  1.21        is 		m2 = UINT_MAX;
    635  1.21        is 
    636   1.1        ad 		if (6 != sscanf(arg, "%d:%d.%d%d:%d.%d",
    637   1.1        ad 			&m1, &s1, &f1, &m2, &s2, &f2) &&
    638   1.1        ad 		    5 != sscanf(arg, "%d:%d.%d%d:%d", &m1, &s1, &f1, &m2, &s2) &&
    639   1.1        ad 		    5 != sscanf(arg, "%d:%d%d:%d.%d", &m1, &s1, &m2, &s2, &f2) &&
    640   1.1        ad 		    3 != sscanf(arg, "%d:%d.%d", &m1, &s1, &f1) &&
    641   1.1        ad 		    4 != sscanf(arg, "%d:%d%d:%d", &m1, &s1, &m2, &s2) &&
    642   1.1        ad 		    2 != sscanf(arg, "%d:%d", &m1, &s1))
    643   1.1        ad 			goto Clean_up;
    644   1.1        ad 
    645  1.21        is 		if (m2 == UINT_MAX) {
    646   1.1        ad 			if (msf) {
    647   1.1        ad 				m2 = toc_buffer[n].addr.msf.minute;
    648   1.1        ad 				s2 = toc_buffer[n].addr.msf.second;
    649   1.1        ad 				f2 = toc_buffer[n].addr.msf.frame;
    650   1.1        ad 			} else {
    651   1.5        ad 				lba2msf(be32toh(toc_buffer[n].addr.lba),
    652   1.1        ad 				    &tm, &ts, &tf);
    653   1.1        ad 				m2 = tm;
    654   1.1        ad 				s2 = ts;
    655   1.1        ad 				f2 = tf;
    656   1.1        ad 			}
    657   1.1        ad 		}
    658   1.3        ad 		return (play_msf(m1, s1, f1, m2, s2, f2));
    659   1.1        ad 	}
    660  1.15        ad 
    661   1.1        ad 	/*
    662   1.1        ad 	 * Play track trk1 [ .idx1 ] [ trk2 [ .idx2 ] ]
    663   1.1        ad 	 */
    664   1.1        ad 	if (4 != sscanf(arg, "%d.%d%d.%d", &start, &istart, &end, &iend) &&
    665   1.1        ad 	    3 != sscanf(arg, "%d.%d%d", &start, &istart, &end) &&
    666   1.1        ad 	    3 != sscanf(arg, "%d%d.%d", &start, &end, &iend) &&
    667   1.1        ad 	    2 != sscanf(arg, "%d.%d", &start, &istart) &&
    668   1.1        ad 	    2 != sscanf(arg, "%d%d", &start, &end) &&
    669   1.1        ad 	    1 != sscanf(arg, "%d", &start))
    670   1.1        ad 		goto Clean_up;
    671   1.1        ad 
    672   1.1        ad 	if (end == 0)
    673   1.1        ad 		end = n;
    674   1.1        ad 	return (play_track(start, istart, end, iend));
    675   1.1        ad 
    676   1.1        ad Clean_up:
    677   1.1        ad 	warnx("invalid command arguments");
    678   1.1        ad 	return (0);
    679  1.13  gmcgarry }
    680  1.13  gmcgarry 
    681  1.16        ad void
    682  1.16        ad sig_timer(int sig, int code, struct sigcontext *scp)
    683  1.16        ad {
    684  1.16        ad 	sigset_t anymore;
    685  1.16        ad 
    686  1.16        ad 	sigpending(&anymore);
    687  1.16        ad 	if (sigismember(&anymore, SIGALRM))
    688  1.16        ad 		return;
    689  1.16        ad 	setitimer(ITIMER_REAL, &itv_timer, NULL);
    690  1.16        ad 	if (fd != -1)
    691  1.16        ad 		skip(0, 0);
    692  1.16        ad }
    693  1.16        ad 
    694  1.13  gmcgarry int
    695  1.16        ad skip(int dir, int fromuser)
    696  1.13  gmcgarry {
    697  1.15        ad 	char str[16];
    698  1.16        ad 	int rv, trk, idx, m, s, f;
    699  1.13  gmcgarry 	struct ioc_toc_header h;
    700  1.15        ad 
    701  1.15        ad 	if ((rv = ioctl(fd, CDIOREADTOCHEADER, &h)) <  0) {
    702  1.15        ad 		warn("ioctl(CDIOREADTOCHEADER)");
    703  1.15        ad 		return (rv);
    704  1.15        ad 	}
    705  1.16        ad 	if ((rv = get_status(&trk, &idx, &m, &s, &f)) < 0)
    706  1.15        ad 		return (rv);
    707  1.16        ad 
    708  1.16        ad 	if (dir == 0) {
    709  1.16        ad 		if (fromuser || (rv != CD_AS_PLAY_IN_PROGRESS &&
    710  1.16        ad 		    rv != CD_AS_PLAY_PAUSED))
    711  1.16        ad 			trk = h.starting_track +
    712  1.16        ad 			    random() % (h.ending_track - h.starting_track + 1);
    713  1.16        ad 		else
    714  1.16        ad 			return (0);
    715  1.16        ad 	} else {
    716  1.16        ad 		trk += dir;
    717  1.16        ad 		if (trk > h.ending_track)
    718  1.16        ad 			trk = h.starting_track;
    719  1.16        ad 		else if(trk < h.starting_track)
    720  1.16        ad 			trk = h.ending_track;
    721  1.16        ad 	}
    722  1.16        ad 
    723  1.16        ad 	if (shuffle)
    724  1.16        ad 		sprintf(str, "%d %d", trk, trk);
    725  1.16        ad 	else
    726  1.16        ad 		sprintf(str, "%d", trk);
    727  1.16        ad 
    728  1.16        ad 	return (play(str, 0));
    729   1.1        ad }
    730   1.1        ad 
    731  1.15        ad const char *
    732  1.15        ad strstatus(int sts)
    733   1.1        ad {
    734  1.15        ad 	const char *str;
    735   1.2        ad 
    736   1.1        ad 	switch (sts) {
    737  1.15        ad 	case CD_AS_AUDIO_INVALID:
    738  1.15        ad 		str = "invalid";
    739  1.15        ad 		break;
    740  1.15        ad 	case CD_AS_PLAY_IN_PROGRESS:
    741  1.15        ad 		str = "playing";
    742  1.15        ad 		break;
    743  1.15        ad 	case CD_AS_PLAY_PAUSED:
    744  1.15        ad 		str = "paused";
    745  1.15        ad 		break;
    746  1.15        ad 	case CD_AS_PLAY_COMPLETED:
    747  1.15        ad 		str = "completed";
    748  1.15        ad 		break;
    749  1.15        ad 	case CD_AS_PLAY_ERROR:
    750  1.15        ad 		str = "error";
    751  1.15        ad 		break;
    752  1.15        ad 	case CD_AS_NO_STATUS:
    753  1.15        ad 		str = "not playing";
    754  1.15        ad 		break;
    755   1.1        ad 	default:
    756  1.15        ad 		str = "<unknown>";
    757  1.15        ad 		break;
    758   1.1        ad 	}
    759  1.15        ad 
    760  1.15        ad 	return (str);
    761   1.1        ad }
    762   1.1        ad 
    763  1.15        ad int
    764  1.15        ad print_status(const char *arg)
    765   1.1        ad {
    766   1.2        ad 	struct cd_sub_channel_info data;
    767   1.1        ad 	struct ioc_read_subchannel ss;
    768  1.16        ad 	int rv, trk, idx, m, s, f;
    769   1.2        ad 	struct ioc_vol v;
    770   1.1        ad 
    771  1.16        ad 	if ((rv = get_status(&trk, &idx, &m, &s, &f)) >= 0) {
    772  1.16        ad 		printf("audio status:\t%s\n", strstatus(rv));
    773  1.15        ad 		printf("current track:\t%d\n", trk);
    774  1.16        ad 		printf("current index:\t%d\n", idx);
    775  1.15        ad 		printf("position:\t%d:%02d.%02d\n", m, s, f);
    776  1.15        ad 	} else
    777  1.15        ad 		printf("audio status:\tno info available\n");
    778  1.15        ad 
    779  1.16        ad 	printf("shuffle play:\t%s\n", shuffle ? "on" : "off");
    780  1.16        ad 
    781  1.15        ad 	bzero(&ss, sizeof(ss));
    782  1.15        ad 	ss.data = &data;
    783  1.15        ad 	ss.data_len = sizeof(data);
    784  1.15        ad 	ss.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
    785  1.15        ad 	ss.data_format = CD_MEDIA_CATALOG;
    786  1.15        ad 
    787  1.16        ad 	if (ioctl(fd, CDIOCREADSUBCHANNEL, (char *)&ss) >= 0) {
    788  1.15        ad 		printf("media catalog:\t%sactive",
    789  1.15        ad 		    ss.data->what.media_catalog.mc_valid ? "" : "in");
    790  1.15        ad 		if (ss.data->what.media_catalog.mc_valid &&
    791  1.15        ad 		    ss.data->what.media_catalog.mc_number[0])
    792  1.15        ad 			printf(" (%.15s)",
    793  1.15        ad 			    ss.data->what.media_catalog.mc_number);
    794  1.15        ad 		putchar('\n');
    795  1.15        ad 	} else
    796  1.15        ad 		printf("media catalog:\tnone\n");
    797  1.15        ad 
    798  1.16        ad 	if (ioctl(fd, CDIOCGETVOL, &v) >= 0) {
    799  1.15        ad 		printf("left volume:\t%d\n", v.vol[0]);
    800  1.15        ad 		printf("right volume:\t%d\n", v.vol[1]);
    801  1.15        ad 	} else {
    802  1.15        ad 		printf("left volume:\tnot available\n");
    803  1.15        ad 		printf("right volume:\tnot available\n");
    804   1.1        ad 	}
    805  1.15        ad 
    806  1.16        ad ;	return (0);
    807   1.1        ad }
    808   1.1        ad 
    809  1.15        ad int
    810  1.15        ad info(const char *arg)
    811   1.1        ad {
    812   1.1        ad 	struct ioc_toc_header h;
    813   1.1        ad 	int rc, i, n;
    814   1.1        ad 
    815   1.1        ad 	if ((rc = ioctl(fd, CDIOREADTOCHEADER, &h)) < 0) {
    816  1.15        ad 		warn("ioctl(CDIOREADTOCHEADER)");
    817   1.1        ad 		return (rc);
    818   1.1        ad 	}
    819   1.1        ad 
    820   1.1        ad 	n = h.ending_track - h.starting_track + 1;
    821   1.1        ad 	rc = read_toc_entrys((n + 1) * sizeof(struct cd_toc_entry));
    822   1.1        ad 	if (rc < 0)
    823   1.1        ad 		return (rc);
    824   1.1        ad 
    825   1.1        ad 	printf("track     start  duration   block  length   type\n");
    826   1.1        ad 	printf("-------------------------------------------------\n");
    827   1.1        ad 
    828   1.1        ad 	for (i = 0; i < n; i++) {
    829   1.1        ad 		printf("%5d  ", toc_buffer[i].track);
    830  1.15        ad 		print_track(toc_buffer + i, 0);
    831   1.1        ad 	}
    832   1.1        ad 	printf("%5d  ", toc_buffer[n].track);
    833  1.15        ad 	print_track(toc_buffer + n, 1);
    834   1.1        ad 	return (0);
    835   1.1        ad }
    836   1.1        ad 
    837  1.15        ad void
    838  1.15        ad lba2msf(u_long lba, u_int *m, u_int *s, u_int *f)
    839   1.1        ad {
    840  1.15        ad 
    841   1.1        ad 	lba += 150;		/* block start offset */
    842   1.1        ad 	lba &= 0xffffff;	/* negative lbas use only 24 bits */
    843   1.1        ad 	*m = lba / (60 * 75);
    844   1.1        ad 	lba %= (60 * 75);
    845   1.1        ad 	*s = lba / 75;
    846   1.1        ad 	*f = lba % 75;
    847   1.1        ad }
    848   1.1        ad 
    849  1.15        ad u_int
    850  1.15        ad msf2lba(u_int m, u_int s, u_int f)
    851   1.1        ad {
    852   1.2        ad 
    853   1.1        ad 	return (((m * 60) + s) * 75 + f) - 150;
    854   1.1        ad }
    855   1.1        ad 
    856  1.15        ad void
    857  1.15        ad print_track(struct cd_toc_entry *e, int lastflag)
    858   1.1        ad {
    859   1.1        ad 	int block, next, len;
    860  1.15        ad 	u_int m, s, f;
    861   1.1        ad 
    862   1.1        ad 	if (msf) {
    863   1.1        ad 		/* Print track start */
    864   1.1        ad 		printf("%2d:%02d.%02d  ", e->addr.msf.minute,
    865   1.1        ad 		    e->addr.msf.second, e->addr.msf.frame);
    866   1.1        ad 
    867   1.1        ad 		block = msf2lba(e->addr.msf.minute, e->addr.msf.second,
    868   1.1        ad 		    e->addr.msf.frame);
    869   1.1        ad 	} else {
    870   1.7        ad 		block = e->addr.lba;
    871   1.1        ad 		lba2msf(block, &m, &s, &f);
    872   1.1        ad 		/* Print track start */
    873   1.1        ad 		printf("%2d:%02d.%02d  ", m, s, f);
    874   1.1        ad 	}
    875   1.1        ad 	if (lastflag) {
    876   1.1        ad 		/* Last track -- print block */
    877   1.1        ad 		printf("       -  %6d       -      -\n", block);
    878   1.1        ad 		return;
    879   1.1        ad 	}
    880   1.1        ad 	if (msf)
    881   1.1        ad 		next = msf2lba(e[1].addr.msf.minute, e[1].addr.msf.second,
    882   1.1        ad 		    e[1].addr.msf.frame);
    883   1.1        ad 	else
    884   1.7        ad 		next = e[1].addr.lba;
    885   1.1        ad 	len = next - block;
    886   1.1        ad 	lba2msf(len, &m, &s, &f);
    887   1.1        ad 
    888   1.1        ad 	/* Print duration, block, length, type */
    889   1.1        ad 	printf("%2d:%02d.%02d  %6d  %6d  %5s\n", m, s, f, block, len,
    890   1.1        ad 	    (e->control & 4) ? "data" : "audio");
    891   1.1        ad }
    892   1.1        ad 
    893  1.15        ad int
    894  1.15        ad play_track(int tstart, int istart, int tend, int iend)
    895   1.1        ad {
    896   1.1        ad 	struct ioc_play_track t;
    897  1.15        ad 	int rv;
    898   1.1        ad 
    899   1.1        ad 	t.start_track = tstart;
    900   1.1        ad 	t.start_index = istart;
    901   1.1        ad 	t.end_track = tend;
    902   1.1        ad 	t.end_index = iend;
    903   1.1        ad 
    904  1.15        ad 	if ((rv = ioctl(fd, CDIOCPLAYTRACKS, &t)) < 0)
    905  1.15        ad 		warn("ioctl(CDIOCPLAYTRACKS)");
    906  1.15        ad 	return (rv);
    907   1.1        ad }
    908   1.1        ad 
    909  1.15        ad int
    910  1.15        ad play_blocks(int blk, int len)
    911   1.1        ad {
    912   1.1        ad 	struct ioc_play_blocks t;
    913  1.15        ad 	int rv;
    914   1.1        ad 
    915   1.1        ad 	t.blk = blk;
    916   1.1        ad 	t.len = len;
    917   1.1        ad 
    918  1.15        ad 	if ((rv = ioctl(fd, CDIOCPLAYBLOCKS, &t)) < 0)
    919  1.15        ad 		warn("ioctl(CDIOCPLAYBLOCKS");
    920  1.15        ad 	return (rv);
    921   1.1        ad }
    922   1.1        ad 
    923  1.15        ad int
    924  1.15        ad setvol(int left, int right)
    925   1.1        ad {
    926   1.1        ad 	struct ioc_vol v;
    927  1.15        ad 	int rv;
    928   1.1        ad 
    929   1.1        ad 	v.vol[0] = left;
    930   1.1        ad 	v.vol[1] = right;
    931   1.1        ad 	v.vol[2] = 0;
    932   1.1        ad 	v.vol[3] = 0;
    933   1.1        ad 
    934  1.15        ad 	if ((rv = ioctl(fd, CDIOCSETVOL, &v)) < 0)
    935  1.15        ad 		warn("ioctl(CDIOCSETVOL)");
    936  1.15        ad 	return (rv);
    937   1.1        ad }
    938   1.1        ad 
    939  1.15        ad int
    940  1.15        ad read_toc_entrys(int len)
    941   1.1        ad {
    942   1.1        ad 	struct ioc_read_toc_entry t;
    943  1.15        ad 	int rv;
    944   1.1        ad 
    945   1.1        ad 	t.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
    946   1.1        ad 	t.starting_track = 0;
    947   1.1        ad 	t.data_len = len;
    948   1.1        ad 	t.data = toc_buffer;
    949   1.1        ad 
    950  1.15        ad 	if ((rv = ioctl(fd, CDIOREADTOCENTRYS, &t)) < 0)
    951  1.15        ad 		warn("ioctl(CDIOREADTOCENTRYS)");
    952  1.15        ad 	return (rv);
    953   1.1        ad }
    954   1.1        ad 
    955  1.15        ad int
    956  1.15        ad play_msf(int start_m, int start_s, int start_f, int end_m, int end_s,
    957  1.15        ad 	 int end_f)
    958   1.1        ad {
    959   1.1        ad 	struct ioc_play_msf a;
    960  1.15        ad 	int rv;
    961   1.1        ad 
    962   1.1        ad 	a.start_m = start_m;
    963   1.1        ad 	a.start_s = start_s;
    964   1.1        ad 	a.start_f = start_f;
    965   1.1        ad 	a.end_m = end_m;
    966   1.1        ad 	a.end_s = end_s;
    967   1.1        ad 	a.end_f = end_f;
    968   1.1        ad 
    969  1.15        ad 	if ((rv = ioctl(fd, CDIOCPLAYMSF, &a)) < 0)
    970  1.15        ad 		warn("ioctl(CDIOREADTOCENTRYS)");
    971  1.15        ad 	return (rv);
    972   1.1        ad }
    973   1.1        ad 
    974  1.15        ad int
    975  1.16        ad get_status(int *trk, int *idx, int *min, int *sec, int *frame)
    976   1.1        ad {
    977   1.1        ad 	struct ioc_read_subchannel s;
    978   1.1        ad 	struct cd_sub_channel_info data;
    979  1.15        ad 	u_int mm, ss, ff;
    980  1.15        ad 	int rv;
    981   1.1        ad 
    982   1.1        ad 	bzero(&s, sizeof(s));
    983   1.1        ad 	s.data = &data;
    984   1.1        ad 	s.data_len = sizeof(data);
    985   1.1        ad 	s.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
    986   1.1        ad 	s.data_format = CD_CURRENT_POSITION;
    987   1.1        ad 
    988  1.15        ad 	if ((rv = ioctl(fd, CDIOCREADSUBCHANNEL, &s)) < 0) {
    989  1.15        ad 		warn("ioctl(CDIOCREADSUBCHANNEL)");
    990  1.15        ad 		return (rv);
    991  1.15        ad 	}
    992   1.1        ad 
    993   1.1        ad 	*trk = s.data->what.position.track_number;
    994  1.16        ad 	*idx = s.data->what.position.index_number;
    995   1.1        ad 	if (msf) {
    996   1.1        ad 		*min = s.data->what.position.reladdr.msf.minute;
    997   1.1        ad 		*sec = s.data->what.position.reladdr.msf.second;
    998   1.1        ad 		*frame = s.data->what.position.reladdr.msf.frame;
    999   1.1        ad 	} else {
   1000  1.15        ad 		lba2msf(be32toh(s.data->what.position.reladdr.lba), &mm,
   1001  1.15        ad 		    &ss, &ff);
   1002   1.1        ad 		*min = mm;
   1003   1.1        ad 		*sec = ss;
   1004   1.1        ad 		*frame = ff;
   1005   1.1        ad 	}
   1006   1.1        ad 
   1007   1.3        ad 	return (s.data->header.audio_status);
   1008   1.1        ad }
   1009   1.1        ad 
   1010  1.15        ad const char *
   1011  1.15        ad prompt(void)
   1012   1.1        ad {
   1013  1.15        ad 
   1014   1.4   msaitoh 	return ("cdplay> ");
   1015   1.1        ad }
   1016   1.1        ad 
   1017  1.15        ad const char *
   1018  1.15        ad parse(char *buf, int *cmd)
   1019   1.1        ad {
   1020  1.15        ad 	const struct cmdtab *c, *mc;
   1021   1.3        ad 	char *p, *q;
   1022   1.1        ad 	int len;
   1023   1.1        ad 
   1024   1.1        ad 	for (p = buf; isspace(*p); p++)
   1025   1.1        ad 		continue;
   1026   1.1        ad 
   1027   1.1        ad 	if (isdigit(*p) || (p[0] == '#' && isdigit(p[1]))) {
   1028   1.1        ad 		*cmd = CMD_PLAY;
   1029   1.1        ad 		return (p);
   1030   1.1        ad 	}
   1031   1.3        ad 
   1032  1.15        ad 	for (buf = p; *p != '\0' && !isspace(*p); p++)
   1033   1.1        ad 		continue;
   1034   1.1        ad 
   1035  1.15        ad 	if ((len = p - buf) == 0)
   1036   1.1        ad 		return (0);
   1037   1.1        ad 
   1038  1.15        ad 	if (*p != '\0') {		/* It must be a spacing character! */
   1039   1.1        ad 		*p++ = 0;
   1040  1.15        ad 		for (q = p; *q != '\0' && *q != '\n' && *q != '\r'; q++)
   1041   1.1        ad 			continue;
   1042   1.1        ad 		*q = 0;
   1043   1.1        ad 	}
   1044   1.3        ad 
   1045   1.1        ad 	*cmd = -1;
   1046   1.3        ad 
   1047  1.15        ad 	mc = cmdtab + sizeof(cmdtab) / sizeof(cmdtab[0]);
   1048  1.15        ad 	for (c = cmdtab; c < mc; c++) {
   1049   1.1        ad 		/* Is it an exact match? */
   1050  1.15        ad 		if (strcasecmp(buf, c->name) == 0) {
   1051   1.1        ad 			*cmd = c->command;
   1052   1.1        ad 			break;
   1053   1.1        ad 		}
   1054   1.1        ad 		/* Try short hand forms then... */
   1055  1.15        ad 		if (len >= c->min && strncasecmp(buf, c->name, len) == 0) {
   1056   1.1        ad 			if (*cmd != -1 && *cmd != c->command) {
   1057   1.1        ad 				warnx("ambiguous command");
   1058   1.1        ad 				return (0);
   1059   1.1        ad 			}
   1060   1.1        ad 			*cmd = c->command;
   1061   1.1        ad 		}
   1062   1.1        ad 	}
   1063   1.1        ad 
   1064   1.1        ad 	if (*cmd == -1) {
   1065   1.1        ad 		warnx("invalid command, enter ``help'' for commands");
   1066   1.1        ad 		return (0);
   1067   1.1        ad 	}
   1068   1.3        ad 
   1069   1.1        ad 	while (isspace(*p))
   1070   1.1        ad 		p++;
   1071   1.3        ad 	return (p);
   1072   1.1        ad }
   1073   1.1        ad 
   1074  1.15        ad int
   1075  1.15        ad opencd(void)
   1076   1.1        ad {
   1077   1.1        ad 	char devbuf[80];
   1078   1.1        ad 
   1079   1.1        ad 	if (fd > -1)
   1080   1.1        ad 		return (1);
   1081   1.1        ad 
   1082  1.11     lukem 	fd = opendisk(cdname, O_RDONLY, devbuf, sizeof(devbuf), 0);
   1083   1.1        ad 	if (fd < 0) {
   1084   1.1        ad 		if (errno == ENXIO) {
   1085  1.15        ad 			/*
   1086  1.15        ad 			 * ENXIO has an overloaded meaning here. The
   1087  1.15        ad 			 * original "Device not configured" should be
   1088  1.15        ad 			 * interpreted as "No disc in drive %s".
   1089  1.15        ad 			 */
   1090   1.1        ad 			warnx("no disc in drive %s", devbuf);
   1091   1.1        ad 			return (0);
   1092   1.1        ad 		}
   1093  1.15        ad 		err(EXIT_FAILURE, "%s", devbuf);
   1094   1.1        ad 	}
   1095   1.3        ad 
   1096   1.1        ad 	return (1);
   1097  1.22        is }
   1098  1.22        is 
   1099  1.22        is void
   1100  1.22        is toc2msf(u_int i, u_int *m, u_int *s, u_int *f)
   1101  1.22        is {
   1102  1.22        is 	struct cd_toc_entry *ctep;
   1103  1.22        is 
   1104  1.22        is 	assert(i >= 0);
   1105  1.22        is 	assert(i < 100);
   1106  1.22        is 
   1107  1.22        is 	ctep = &toc_buffer[i];
   1108  1.22        is 
   1109  1.22        is 	if (msf) {
   1110  1.22        is 		*m = ctep->addr.msf.minute;
   1111  1.22        is 		*s = ctep->addr.msf.second;
   1112  1.22        is 		*f = ctep->addr.msf.frame;
   1113  1.22        is 	} else {
   1114  1.22        is 		lba2msf(be32toh(ctep->addr.lba), m, s, f);
   1115  1.22        is 	}
   1116  1.22        is }
   1117  1.22        is 
   1118  1.22        is int
   1119  1.22        is toc2lba(u_int i)
   1120  1.22        is {
   1121  1.22        is 	struct cd_toc_entry *ctep;
   1122  1.22        is 
   1123  1.22        is 	assert(i > 0);
   1124  1.22        is 	assert(i < 100);
   1125  1.22        is 
   1126  1.22        is 	ctep = &toc_buffer[i-1];
   1127  1.22        is 
   1128  1.22        is 	if (msf) {
   1129  1.22        is 		return msf2lba(
   1130  1.22        is 		    ctep->addr.msf.minute,
   1131  1.22        is 		    ctep->addr.msf.second,
   1132  1.22        is 		    ctep->addr.msf.frame);
   1133  1.22        is 	} else {
   1134  1.22        is 		return be32toh(ctep->addr.lba);
   1135  1.22        is 	}
   1136  1.22        is }
   1137  1.22        is 
   1138  1.22        is void
   1139  1.22        is addmsf(u_int *m, u_int *s, u_int *f, u_int m2, u_int s2, u_int f2)
   1140  1.22        is {
   1141  1.22        is 	*f += f2;
   1142  1.22        is 	if (*f > 75) {
   1143  1.22        is 		*s += *f / 75;
   1144  1.22        is 		*f %= 75;
   1145  1.22        is 	}
   1146  1.22        is 
   1147  1.22        is 	*s += s2;
   1148  1.22        is 	if (*s > 60) {
   1149  1.22        is 		*m += *s / 60;
   1150  1.22        is 		*s %= 60;
   1151  1.22        is 	}
   1152  1.22        is 
   1153  1.22        is 	*m += m2;
   1154   1.1        ad }
   1155