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