Home | History | Annotate | Line # | Download | only in cdplay
cdplay.c revision 1.37
      1  1.37    dogcow /* 	$NetBSD: cdplay.c,v 1.37 2006/12/27 18:47:20 dogcow 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.37    dogcow __RCSID("$NetBSD: cdplay.c,v 1.37 2006/12/27 18:47:20 dogcow 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.33   xtraeme #include <sys/time.h>
     53  1.33   xtraeme #include <sys/audioio.h>
     54  1.33   xtraeme #include <sys/scsiio.h>
     55   1.5        ad 
     56  1.22        is #include <assert.h>
     57  1.22        is 
     58   1.1        ad #include <ctype.h>
     59   1.1        ad #include <err.h>
     60   1.1        ad #include <errno.h>
     61   1.4   msaitoh #include <histedit.h>
     62  1.21        is #include <limits.h>
     63  1.33   xtraeme #include <sched.h>
     64  1.16        ad #include <signal.h>
     65   1.1        ad #include <stdio.h>
     66   1.1        ad #include <stdlib.h>
     67   1.1        ad #include <string.h>
     68   1.1        ad #include <unistd.h>
     69  1.11     lukem #include <util.h>
     70   1.1        ad 
     71  1.15        ad enum cmd {
     72  1.16        ad 	CMD_CLOSE,
     73  1.33   xtraeme 	CMD_DIGITAL,
     74  1.15        ad 	CMD_EJECT,
     75  1.15        ad 	CMD_HELP,
     76  1.15        ad 	CMD_INFO,
     77  1.16        ad 	CMD_NEXT,
     78  1.15        ad 	CMD_PAUSE,
     79  1.15        ad 	CMD_PLAY,
     80  1.16        ad 	CMD_PREV,
     81  1.15        ad 	CMD_QUIT,
     82  1.16        ad 	CMD_RESET,
     83  1.15        ad 	CMD_RESUME,
     84  1.16        ad 	CMD_SET,
     85  1.16        ad 	CMD_SHUFFLE,
     86  1.31   garbled 	CMD_SINGLE,
     87  1.16        ad 	CMD_SKIP,
     88  1.16        ad 	CMD_STATUS,
     89  1.15        ad 	CMD_STOP,
     90  1.15        ad 	CMD_VOLUME,
     91  1.15        ad };
     92   1.1        ad 
     93   1.1        ad struct cmdtab {
     94  1.15        ad 	enum cmd	command;
     95  1.15        ad 	const char	*name;
     96  1.15        ad 	unsigned int	min;
     97  1.15        ad 	const char	*args;
     98  1.15        ad } const cmdtab[] = {
     99  1.16        ad 	{ CMD_HELP,	"?",	   1, 0 },
    100  1.16        ad 	{ CMD_CLOSE,	"close",   1, NULL },
    101  1.33   xtraeme 	{ CMD_DIGITAL,	"digital", 1, "fpw" },
    102  1.16        ad 	{ CMD_EJECT,	"eject",   1, NULL },
    103  1.16        ad 	{ CMD_HELP,	"help",    1, NULL },
    104  1.16        ad 	{ CMD_INFO,	"info",    1, NULL },
    105  1.16        ad 	{ CMD_NEXT,	"next",    1, NULL },
    106  1.16        ad 	{ CMD_PAUSE,	"pause",   2, NULL },
    107  1.16        ad 	{ CMD_PLAY,	"play",    1, "min1:sec1[.fram1] [min2:sec2[.fram2]]" },
    108  1.16        ad 	{ CMD_PLAY,	"play",    1, "track1[.index1] [track2[.index2]]" },
    109  1.16        ad 	{ CMD_PLAY,	"play",    1, "tr1 m1:s1[.f1] [[tr2] [m2:s2[.f2]]]" },
    110  1.16        ad 	{ CMD_PLAY,	"play",    1, "[#block [len]]" },
    111  1.16        ad 	{ CMD_PREV,	"prev",    2, NULL },
    112  1.16        ad 	{ CMD_QUIT,	"quit",    1, NULL },
    113  1.16        ad 	{ CMD_RESET,	"reset",   4, NULL },
    114  1.16        ad 	{ CMD_RESUME,	"resume",  4, NULL },
    115  1.16        ad 	{ CMD_SET,	"set",     2, "msf | lba" },
    116  1.16        ad 	{ CMD_SHUFFLE,	"shuffle", 2, NULL },
    117  1.31   garbled 	{ CMD_SINGLE,	"single",  2, "[<track>]" },
    118  1.16        ad 	{ CMD_SKIP,	"skip",    2, NULL },
    119  1.16        ad 	{ CMD_STATUS,	"status",  3, NULL },
    120  1.16        ad 	{ CMD_STOP,	"stop",    3, NULL },
    121  1.16        ad 	{ CMD_VOLUME,	"volume",  1, "<l> <r>|left|right|mute|mono|stereo" },
    122   1.1        ad };
    123  1.33   xtraeme 
    124  1.33   xtraeme #define IOCTL_SIMPLE(fd, ctl)			\
    125  1.33   xtraeme 	do {					\
    126  1.33   xtraeme 		if (ioctl((fd), (ctl)) >= 0) {	\
    127  1.33   xtraeme 			close(fd);		\
    128  1.33   xtraeme 			fd = -1;		\
    129  1.33   xtraeme 		} else				\
    130  1.33   xtraeme 			warn("ioctl(" #ctl ")");\
    131  1.33   xtraeme 	} while (/* CONSTCOND */ 0)
    132  1.33   xtraeme 
    133  1.33   xtraeme #define CDDA_SIZE	2352
    134   1.1        ad 
    135  1.23    simonb #define	CD_MAX_TRACK	99	/* largest 2 digit BCD number */
    136  1.23    simonb 
    137  1.23    simonb struct cd_toc_entry toc_buffer[CD_MAX_TRACK + 1];
    138   1.1        ad 
    139   1.1        ad const char *cdname;
    140   1.1        ad int     fd = -1;
    141   1.1        ad int     msf = 1;
    142  1.16        ad int	shuffle;
    143  1.16        ad int	interactive = 1;
    144  1.33   xtraeme int	digital = 0;
    145  1.33   xtraeme int	tbvalid = 0;
    146  1.16        ad struct	itimerval itv_timer;
    147  1.33   xtraeme struct {
    148  1.33   xtraeme 	const char *auname;
    149  1.33   xtraeme 	u_char *audata, *aubuf;
    150  1.33   xtraeme 	int afd;
    151  1.33   xtraeme 	int fpw;
    152  1.33   xtraeme 	int lba_start, lba_end, lba_current;
    153  1.33   xtraeme 	int lowat, hiwat, readseek, playseek;
    154  1.33   xtraeme 	int playing, changed;
    155  1.33   xtraeme 	int read_errors;
    156  1.33   xtraeme }      da;
    157   1.1        ad 
    158   1.4   msaitoh History *hist;
    159   1.4   msaitoh HistEvent he;
    160   1.4   msaitoh EditLine *elptr;
    161   1.4   msaitoh 
    162  1.15        ad int	get_vol(int *, int *);
    163  1.16        ad int	get_status(int *, int *, int *, int *, int *);
    164  1.15        ad void	help(void);
    165  1.15        ad int	info(const char *);
    166  1.15        ad void	lba2msf(u_long, u_int *, u_int *, u_int *);
    167  1.15        ad int 	main(int, char **);
    168  1.15        ad u_int	msf2lba(u_int, u_int, u_int);
    169  1.15        ad int	opencd(void);
    170  1.33   xtraeme int	openaudio(void);
    171  1.15        ad const char   *parse(char *, int *);
    172  1.16        ad int	play(const char *, int);
    173  1.15        ad int	play_blocks(int, int);
    174  1.33   xtraeme int	play_digital(int, int);
    175  1.15        ad int	play_msf(int, int, int, int, int, int);
    176  1.15        ad int	play_track(int, int, int, int);
    177  1.15        ad int	print_status(const char *);
    178  1.23    simonb void	print_track(struct cd_toc_entry *);
    179  1.15        ad const char	*prompt(void);
    180  1.33   xtraeme int	readaudio(int, int, int, u_char *);
    181  1.15        ad int	read_toc_entrys(int);
    182  1.15        ad int	run(int, const char *);
    183  1.15        ad int	setvol(int, int);
    184  1.27  christos void	sig_timer(int);
    185  1.16        ad int	skip(int, int);
    186  1.15        ad const char	*strstatus(int);
    187  1.15        ad void 	usage(void);
    188   1.1        ad 
    189  1.22        is void	toc2msf(u_int, u_int *, u_int *, u_int *);
    190  1.22        is int	toc2lba(u_int);
    191  1.22        is void	addmsf(u_int *, u_int *, u_int *, u_int, u_int, u_int);
    192  1.22        is 
    193  1.15        ad int
    194  1.15        ad main(int argc, char **argv)
    195   1.1        ad {
    196  1.15        ad 	const char *arg;
    197  1.15        ad 	char buf[80], *p;
    198   1.5        ad 	static char defdev[16];
    199  1.15        ad 	int cmd, len, c;
    200   1.4   msaitoh 	char *line;
    201   1.4   msaitoh 	const char *elline;
    202  1.16        ad 	int scratch, rv;
    203  1.16        ad 	struct sigaction sa_timer;
    204   1.1        ad 
    205   1.1        ad 	cdname = getenv("MUSIC_CD");
    206  1.15        ad 	if (cdname == NULL)
    207   1.1        ad 		cdname = getenv("CD_DRIVE");
    208  1.15        ad 	if (cdname == NULL)
    209   1.1        ad 		cdname = getenv("DISC");
    210  1.15        ad 	if (cdname == NULL)
    211   1.1        ad 		cdname = getenv("CDPLAY");
    212   1.1        ad 
    213  1.33   xtraeme 	da.auname = getenv("AUDIODEV");
    214  1.33   xtraeme 	if (!da.auname)
    215  1.33   xtraeme 		da.auname = getenv("SPEAKER");
    216  1.33   xtraeme 	if (!da.auname)
    217  1.33   xtraeme 		da.auname = "/dev/sound";
    218  1.33   xtraeme 
    219  1.33   xtraeme 	while ((c = getopt(argc, argv, "a:f:h")) != -1)
    220  1.15        ad 		switch (c) {
    221  1.33   xtraeme 		case 'a':
    222  1.33   xtraeme 			da.auname = optarg;
    223  1.33   xtraeme 			continue;
    224   1.1        ad 		case 'f':
    225   1.1        ad 			cdname = optarg;
    226   1.1        ad 			continue;
    227   1.1        ad 		case 'h':
    228   1.1        ad 		default:
    229   1.1        ad 			usage();
    230  1.15        ad 			/* NOTREACHED */
    231   1.1        ad 		}
    232   1.1        ad 	argc -= optind;
    233   1.1        ad 	argv += optind;
    234   1.1        ad 
    235  1.18        ad 	if (argc > 0 && strcasecmp(*argv, "help") == 0)
    236   1.1        ad 		usage();
    237   1.1        ad 
    238  1.15        ad 	if (cdname == NULL) {
    239  1.25    itojun 		snprintf(defdev, sizeof(defdev), "cd0%c",
    240  1.25    itojun 		    'a' + getrawpartition());
    241   1.1        ad 		cdname = defdev;
    242   1.1        ad 	}
    243  1.15        ad 
    244   1.3        ad 	opencd();
    245  1.33   xtraeme 	srandom((u_long)time(NULL));
    246  1.33   xtraeme 	da.afd = -1;
    247  1.33   xtraeme 
    248   1.1        ad 	if (argc > 0) {
    249  1.16        ad 		interactive = 0;
    250  1.15        ad 		for (p = buf; argc-- > 0; argv++) {
    251   1.1        ad 			len = strlen(*argv);
    252   1.1        ad 
    253   1.1        ad 			if (p + len >= buf + sizeof(buf) - 1)
    254   1.1        ad 				usage();
    255   1.1        ad 			if (p > buf)
    256   1.1        ad 				*p++ = ' ';
    257   1.1        ad 
    258  1.26    itojun 			strlcpy(p, *argv, sizeof(buf) - (p - buf));
    259   1.1        ad 			p += len;
    260   1.1        ad 		}
    261  1.15        ad 		*p = '\0';
    262   1.1        ad 		arg = parse(buf, &cmd);
    263   1.1        ad 		return (run(cmd, arg));
    264   1.1        ad 	}
    265  1.15        ad 
    266  1.33   xtraeme 	setbuf(stdout, NULL);
    267   1.1        ad 	printf("Type `?' for command list\n\n");
    268   1.1        ad 
    269   1.4   msaitoh 	hist = history_init();
    270   1.4   msaitoh 	history(hist, &he, H_SETSIZE, 100);	/* 100 elt history buffer */
    271  1.12       cgd 	elptr = el_init(getprogname(), stdin, stdout, stderr);
    272   1.4   msaitoh 	el_set(elptr, EL_EDITOR, "emacs");
    273   1.4   msaitoh 	el_set(elptr, EL_PROMPT, prompt);
    274   1.4   msaitoh 	el_set(elptr, EL_HIST, history, hist);
    275  1.33   xtraeme 	el_set(elptr, EL_SIGNAL, 1);
    276   1.4   msaitoh 	el_source(elptr, NULL);
    277   1.4   msaitoh 
    278  1.16        ad 	sigemptyset(&sa_timer.sa_mask);
    279  1.27  christos 	sa_timer.sa_handler = sig_timer;
    280  1.16        ad 	sa_timer.sa_flags = SA_RESTART;
    281  1.16        ad 	if ((rv = sigaction(SIGALRM, &sa_timer, NULL)) < 0)
    282  1.16        ad 		err(EXIT_FAILURE, "sigaction()");
    283  1.16        ad 
    284   1.1        ad 	for (;;) {
    285   1.4   msaitoh 		line = NULL;
    286  1.37    dogcow 		arg = NULL;
    287   1.4   msaitoh 		do {
    288   1.4   msaitoh 			if (((elline = el_gets(elptr, &scratch)) != NULL)
    289   1.4   msaitoh 			    && (scratch != 0)){
    290   1.4   msaitoh 				history(hist, &he, H_ENTER, elline);
    291   1.4   msaitoh 				line = strdup(elline);
    292  1.36       alc 				if (line != NULL) {
    293  1.36       alc 					arg = parse(line, &cmd);
    294  1.33   xtraeme 					free(line);
    295  1.36       alc 				}
    296   1.4   msaitoh 			} else {
    297   1.4   msaitoh 				cmd = CMD_QUIT;
    298  1.33   xtraeme 				warnx("\r\n");
    299  1.37    dogcow 				arg = NULL;
    300   1.4   msaitoh 				break;
    301   1.4   msaitoh 			}
    302  1.15        ad 		} while (arg == NULL);
    303   1.4   msaitoh 
    304   1.1        ad 		if (run(cmd, arg) < 0) {
    305  1.15        ad 			if (fd != -1)
    306  1.15        ad 				close(fd);
    307   1.1        ad 			fd = -1;
    308   1.1        ad 		}
    309   1.1        ad 	}
    310  1.15        ad 
    311   1.4   msaitoh 	el_end(elptr);
    312   1.4   msaitoh 	history_end(hist);
    313  1.15        ad 	exit(EXIT_SUCCESS);
    314  1.15        ad 	/* NOTREACHED */
    315   1.1        ad }
    316   1.1        ad 
    317  1.15        ad void
    318  1.15        ad usage(void)
    319   1.1        ad {
    320   1.1        ad 
    321  1.33   xtraeme 	fprintf(stderr, "usage: cdplay [-a audio_device] [-f cd_device] [command ...]\n");
    322  1.15        ad 	exit(EXIT_FAILURE);
    323  1.15        ad 	/* NOTREACHED */
    324  1.15        ad }
    325  1.15        ad 
    326  1.15        ad void
    327  1.15        ad help(void)
    328  1.15        ad {
    329  1.15        ad 	const struct cmdtab *c, *mc;
    330  1.15        ad 	const char *s;
    331  1.15        ad 	int i, n;
    332  1.15        ad 
    333  1.15        ad 	mc = cmdtab + sizeof(cmdtab) / sizeof(cmdtab[0]);
    334  1.15        ad 	for (c = cmdtab; c < mc; c++) {
    335  1.15        ad 		for (i = c->min, s = c->name; *s != '\0'; s++, i--) {
    336  1.29       dsl 			n = (i > 0 ? toupper((unsigned char)*s) : *s);
    337  1.15        ad 			putchar(n);
    338   1.5        ad 		}
    339  1.15        ad 		if (c->args != NULL)
    340  1.15        ad 			printf(" %s", c->args);
    341  1.15        ad 		putchar('\n');
    342  1.15        ad 	}
    343  1.15        ad 	printf(
    344  1.15        ad 	    "\nThe word \"play\" is not required for the play commands.\n"
    345  1.15        ad 	    "The plain target address is taken as a synonym for play.\n");
    346  1.15        ad }
    347  1.15        ad 
    348  1.15        ad int
    349  1.15        ad run(int cmd, const char *arg)
    350  1.15        ad {
    351  1.15        ad 	int l, r, rv;
    352  1.15        ad 
    353  1.30     lukem 	rv = 0;
    354  1.15        ad 	if (cmd == CMD_QUIT) {
    355  1.16        ad 		close(fd);
    356  1.15        ad 		exit(EXIT_SUCCESS);
    357  1.15        ad 		/* NOTREACHED */
    358   1.5        ad 	}
    359  1.15        ad 
    360   1.5        ad 	if (fd < 0 && !opencd())
    361   1.5        ad 		return (0);
    362  1.15        ad 
    363   1.5        ad 	switch (cmd) {
    364   1.1        ad 	case CMD_INFO:
    365  1.15        ad 		rv = info(arg);
    366  1.15        ad 		break;
    367   1.1        ad 
    368   1.1        ad 	case CMD_STATUS:
    369  1.15        ad 		rv = print_status(arg);
    370  1.15        ad 		break;
    371   1.1        ad 
    372   1.1        ad 	case CMD_PAUSE:
    373  1.33   xtraeme 		if (digital) {
    374  1.33   xtraeme 			da.playing = 0;
    375  1.33   xtraeme 			return (0);
    376  1.33   xtraeme 		} else if ((rv = ioctl(fd, CDIOCPAUSE)) < 0)
    377  1.15        ad 			warn("ioctl(CDIOCPAUSE)");
    378  1.15        ad 		break;
    379   1.1        ad 
    380   1.1        ad 	case CMD_RESUME:
    381  1.33   xtraeme 		if (digital) {
    382  1.33   xtraeme 			da.playing = 1;
    383  1.33   xtraeme 			return (0);
    384  1.33   xtraeme 		} else if ((rv = ioctl(fd, CDIOCRESUME)) < 0)
    385  1.15        ad 			warn("ioctl(CDIOCRESUME)");
    386  1.15        ad 		break;
    387   1.1        ad 
    388   1.1        ad 	case CMD_STOP:
    389  1.33   xtraeme 		if (digital) {
    390  1.33   xtraeme 			da.playing = 0;
    391  1.33   xtraeme 			return (0);
    392  1.33   xtraeme 		} else {
    393  1.33   xtraeme 			if ((rv = ioctl(fd, CDIOCSTOP)) < 0)
    394  1.33   xtraeme 				warn("ioctl(CDIOCSTOP)");
    395  1.33   xtraeme 			if (ioctl(fd, CDIOCALLOW) < 0)
    396  1.33   xtraeme 				warn("ioctl(CDIOCALLOW)");
    397  1.33   xtraeme 		}
    398  1.15        ad 		break;
    399   1.1        ad 
    400   1.1        ad 	case CMD_RESET:
    401  1.33   xtraeme 		tbvalid = 0;
    402  1.32   garbled 		IOCTL_SIMPLE(fd, CDIOCRESET);
    403   1.1        ad 		return (0);
    404   1.1        ad 
    405   1.1        ad 	case CMD_EJECT:
    406  1.33   xtraeme 		tbvalid = 0;
    407  1.33   xtraeme 		if (digital)
    408  1.33   xtraeme 			da.playing = 0;
    409  1.16        ad 		if (shuffle)
    410  1.16        ad 			run(CMD_SHUFFLE, NULL);
    411  1.15        ad 		if (ioctl(fd, CDIOCALLOW) < 0)
    412  1.15        ad 			warn("ioctl(CDIOCALLOW)");
    413  1.32   garbled 		IOCTL_SIMPLE(fd, CDIOCEJECT);
    414  1.15        ad 		break;
    415   1.1        ad 
    416   1.1        ad 	case CMD_CLOSE:
    417   1.3        ad 		ioctl(fd, CDIOCALLOW);
    418  1.32   garbled 		IOCTL_SIMPLE(fd, CDIOCCLOSE);
    419  1.32   garbled 		if (interactive && fd == -1)
    420  1.32   garbled 			opencd();
    421  1.15        ad 		break;
    422   1.1        ad 
    423   1.1        ad 	case CMD_PLAY:
    424  1.29       dsl 		while (isspace((unsigned char)*arg))
    425   1.1        ad 			arg++;
    426  1.16        ad 		rv = play(arg, 1);
    427  1.15        ad 		break;
    428   1.1        ad 
    429  1.13  gmcgarry 	case CMD_PREV:
    430  1.16        ad 		rv = skip(-1, 1);
    431  1.15        ad 		break;
    432  1.13  gmcgarry 
    433  1.13  gmcgarry 	case CMD_NEXT:
    434  1.16        ad 		rv = skip(1, 1);
    435  1.16        ad 		break;
    436  1.16        ad 
    437  1.31   garbled 	case CMD_SINGLE:
    438  1.31   garbled 		if (interactive == 0)
    439  1.31   garbled 			errx(EXIT_FAILURE,
    440  1.31   garbled 			    "'single' valid only in interactive mode");
    441  1.31   garbled 	/*FALLTHROUGH*/
    442  1.16        ad 	case CMD_SHUFFLE:
    443  1.16        ad 		if (interactive == 0)
    444  1.16        ad 			errx(EXIT_FAILURE,
    445  1.16        ad 			    "`shuffle' valid only in interactive mode");
    446  1.16        ad 		if (shuffle == 0) {
    447  1.33   xtraeme 			if (digital == 0) {
    448  1.33   xtraeme 				itv_timer.it_interval.tv_sec = 1;
    449  1.33   xtraeme 				itv_timer.it_interval.tv_usec = 0;
    450  1.33   xtraeme 				itv_timer.it_value.tv_sec = 1;
    451  1.33   xtraeme 				itv_timer.it_value.tv_usec = 0;
    452  1.33   xtraeme 				if (setitimer(ITIMER_REAL, &itv_timer, NULL) == 0) {
    453  1.33   xtraeme 					if (cmd == CMD_SHUFFLE) {
    454  1.31   garbled 						shuffle = 1;
    455  1.33   xtraeme 					} else {
    456  1.33   xtraeme 						while (isspace((unsigned char)*arg))
    457  1.33   xtraeme 							arg++;
    458  1.33   xtraeme 						shuffle = -atoi(arg);
    459  1.33   xtraeme 					}
    460  1.31   garbled 				}
    461  1.33   xtraeme 			} else
    462  1.33   xtraeme 				shuffle = cmd == CMD_SINGLE ? -atoi(arg) : 1;
    463  1.33   xtraeme 			/*
    464  1.33   xtraeme 			if (shuffle)
    465  1.33   xtraeme 				rv = skip(0, 1);
    466  1.33   xtraeme 			*/
    467  1.33   xtraeme 		} else {
    468  1.33   xtraeme 			if (digital == 0) {
    469  1.33   xtraeme 				itv_timer.it_interval.tv_sec = 0;
    470  1.33   xtraeme 				itv_timer.it_interval.tv_usec = 0;
    471  1.33   xtraeme 				itv_timer.it_value.tv_sec = 0;
    472  1.33   xtraeme 				itv_timer.it_value.tv_usec = 0;
    473  1.33   xtraeme 				setitimer(ITIMER_REAL, &itv_timer, NULL);
    474  1.16        ad 			}
    475  1.33   xtraeme 			shuffle = 0;
    476  1.16        ad 		}
    477  1.31   garbled 		if (shuffle < 0)
    478  1.31   garbled 			printf("single track:\t%d\n", -shuffle);
    479  1.31   garbled 		else
    480  1.31   garbled 			printf("shuffle play:\t%s\n", (shuffle != 0) ? "on" : "off");
    481  1.16        ad 		rv = 0;
    482  1.16        ad 		break;
    483  1.16        ad 
    484  1.33   xtraeme 	case CMD_DIGITAL:
    485  1.33   xtraeme 		if (digital == 0) {
    486  1.33   xtraeme 			int fpw;
    487  1.33   xtraeme 
    488  1.33   xtraeme 			fpw = atoi(arg);
    489  1.33   xtraeme 			if (fpw > 0)
    490  1.33   xtraeme 				da.fpw = fpw;
    491  1.33   xtraeme 			else
    492  1.33   xtraeme 				da.fpw = 5;
    493  1.33   xtraeme 			da.read_errors = 0;
    494  1.33   xtraeme 			da.aubuf = malloc(da.fpw * CDDA_SIZE);
    495  1.33   xtraeme 			if (da.aubuf == NULL) {
    496  1.33   xtraeme 				warn("Not enough memory for audio buffers");
    497  1.33   xtraeme 				return (1);
    498  1.33   xtraeme 			}
    499  1.33   xtraeme 			if (da.afd == -1 && !openaudio()) {
    500  1.33   xtraeme 				warn("Cannot open audio device");
    501  1.33   xtraeme 				return (1);
    502  1.33   xtraeme 			}
    503  1.33   xtraeme 			itv_timer.it_interval.tv_sec = itv_timer.it_value.tv_sec = da.fpw / 75;
    504  1.33   xtraeme 			itv_timer.it_interval.tv_usec = itv_timer.it_value.tv_usec =
    505  1.33   xtraeme 			    (da.fpw * 6666) % 1000000;
    506  1.33   xtraeme 			rv = setitimer(ITIMER_REAL, &itv_timer, NULL);
    507  1.33   xtraeme 			if (rv == 0) {
    508  1.33   xtraeme 				digital = 1;
    509  1.33   xtraeme 			} else
    510  1.33   xtraeme 				warnx("setitimer in CMD_DIGITAL");
    511  1.33   xtraeme 			msf = 0;
    512  1.33   xtraeme 			tbvalid = 0;
    513  1.33   xtraeme 		} else {
    514  1.33   xtraeme 			if (shuffle == 1)
    515  1.33   xtraeme 				itv_timer.it_interval.tv_sec = itv_timer.it_value.tv_sec = 1;
    516  1.33   xtraeme 			else
    517  1.33   xtraeme 				itv_timer.it_interval.tv_sec = itv_timer.it_value.tv_sec = 0;
    518  1.33   xtraeme 			itv_timer.it_interval.tv_usec = itv_timer.it_value.tv_usec = 0;
    519  1.33   xtraeme 			digital = 0;
    520  1.33   xtraeme 			rv = setitimer(ITIMER_REAL, &itv_timer, NULL);
    521  1.33   xtraeme 			free(da.audata);
    522  1.33   xtraeme 			close(da.afd);
    523  1.33   xtraeme 			da.afd = -1;
    524  1.33   xtraeme 		}
    525  1.33   xtraeme 		return (0);
    526  1.33   xtraeme 
    527  1.16        ad 	case CMD_SKIP:
    528  1.16        ad 		if (!interactive)
    529  1.16        ad 			errx(EXIT_FAILURE,
    530  1.16        ad 			    "`skip' valid only in interactive mode");
    531  1.16        ad 		if (!shuffle)
    532  1.16        ad 			warnx("`skip' valid only in shuffle mode");
    533  1.16        ad 		else
    534  1.16        ad 			skip(0, 1);
    535  1.15        ad 		break;
    536  1.15        ad 
    537   1.1        ad 	case CMD_SET:
    538  1.33   xtraeme 		tbvalid = 0;
    539  1.15        ad 		if (strcasecmp(arg, "msf") == 0)
    540   1.1        ad 			msf = 1;
    541  1.15        ad 		else if (strcasecmp(arg, "lba") == 0)
    542   1.5        ad 			msf = 0;
    543   1.1        ad 		else
    544   1.5        ad 			warnx("invalid command arguments");
    545  1.15        ad 		break;
    546   1.1        ad 
    547   1.1        ad 	case CMD_VOLUME:
    548  1.33   xtraeme 		if (digital) {
    549  1.33   xtraeme 			rv = 0;
    550  1.33   xtraeme 			warnx("`volume' is ignored while in digital xfer mode");
    551  1.33   xtraeme 		} else if (strncasecmp(arg, "left", strlen(arg)) == 0)
    552  1.15        ad 			rv = ioctl(fd, CDIOCSETLEFT);
    553  1.15        ad 		else if (strncasecmp(arg, "right", strlen(arg)) == 0)
    554  1.15        ad 			rv = ioctl(fd, CDIOCSETRIGHT);
    555  1.15        ad 		else if (strncasecmp(arg, "mono", strlen(arg)) == 0)
    556  1.15        ad 			rv = ioctl(fd, CDIOCSETMONO);
    557  1.15        ad 		else if (strncasecmp(arg, "stereo", strlen(arg)) == 0)
    558  1.15        ad 			rv = ioctl(fd, CDIOCSETSTEREO);
    559  1.15        ad 		else if (strncasecmp(arg, "mute", strlen(arg)) == 0)
    560  1.15        ad 			rv = ioctl(fd, CDIOCSETMUTE);
    561  1.15        ad 		else {
    562  1.15        ad 			rv = 0;
    563  1.15        ad 			if (sscanf(arg, "%d %d", &l, &r) != 2) {
    564  1.15        ad 				if (sscanf(arg, "%d", &l) == 1)
    565  1.15        ad 					r = l;
    566  1.15        ad 				else {
    567  1.15        ad 					warnx("invalid command arguments");
    568  1.15        ad 					break;
    569  1.15        ad 				}
    570   1.9       abs 			}
    571  1.15        ad 			rv = setvol(l, r);
    572   1.1        ad 		}
    573  1.15        ad 		break;
    574  1.15        ad 
    575   1.1        ad 	case CMD_HELP:
    576   1.1        ad 	default:
    577   1.1        ad 		help();
    578  1.15        ad 		rv = 0;
    579  1.15        ad 		break;
    580  1.15        ad 	}
    581   1.1        ad 
    582  1.15        ad 	return (rv);
    583   1.1        ad }
    584   1.1        ad 
    585  1.15        ad int
    586  1.16        ad play(const char *arg, int fromuser)
    587   1.1        ad {
    588  1.22        is 	int rv, n, start, end, istart, iend, blk, len, relend;
    589  1.15        ad 	u_int tr1, tr2, m1, m2, s1, s2, f1, f2, tm, ts, tf;
    590   1.1        ad 	struct ioc_toc_header h;
    591  1.15        ad 
    592  1.16        ad 	if (shuffle && fromuser) {
    593  1.17        ad 		warnx("`play' not valid in shuffle mode");
    594  1.16        ad 		return (0);
    595  1.16        ad 	}
    596  1.16        ad 
    597  1.15        ad 	if ((rv = ioctl(fd, CDIOREADTOCHEADER, &h)) <  0) {
    598  1.15        ad 		warn("ioctl(CDIOREADTOCHEADER)");
    599  1.15        ad 		return (rv);
    600  1.15        ad 	}
    601   1.1        ad 
    602   1.2        ad 	end = 0;
    603   1.2        ad 	istart = iend = 1;
    604   1.1        ad 	n = h.ending_track - h.starting_track + 1;
    605  1.15        ad 	rv = read_toc_entrys((n + 1) * sizeof(struct cd_toc_entry));
    606  1.15        ad 	if (rv < 0)
    607  1.15        ad 		return (rv);
    608   1.1        ad 
    609  1.15        ad 	if (arg == NULL || *arg == '\0') {
    610   1.1        ad 		/* Play the whole disc */
    611  1.16        ad 		return (play_track(h.starting_track, 1, h.ending_track, 99));
    612   1.1        ad 	}
    613  1.15        ad 
    614  1.15        ad 	if (strchr(arg, '#') != NULL) {
    615   1.1        ad 		/* Play block #blk [ len ] */
    616   1.3        ad 		len = 0;
    617   1.1        ad 
    618   1.1        ad 		if (2 != sscanf(arg, "#%d%d", &blk, &len) &&
    619   1.1        ad 		    1 != sscanf(arg, "#%d", &blk))
    620   1.1        ad 			goto Clean_up;
    621   1.1        ad 
    622   1.1        ad 		if (len == 0) {
    623  1.22        is 			len = toc2lba(n);
    624   1.1        ad 		}
    625   1.3        ad 		return (play_blocks(blk, len));
    626   1.1        ad 	}
    627  1.15        ad 
    628  1.15        ad 	if (strchr(arg, ':') != NULL) {
    629   1.1        ad 		/*
    630   1.1        ad 		 * Play MSF m1:s1 [ .f1 ] [ m2:s2 [ .f2 ] ]
    631   1.1        ad 		 *
    632   1.1        ad 		 * Will now also undestand timed addresses relative
    633   1.1        ad 		 * to the beginning of a track in the form...
    634   1.1        ad 		 *
    635   1.1        ad 		 *      tr1 m1:s1[.f1] [[tr2] [m2:s2[.f2]]]
    636   1.1        ad 		 */
    637  1.22        is 		relend = 1;
    638   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    639  1.15        ad 		if (8 == sscanf(arg, "%d %d:%d.%d %d %d:%d.%d", &tr1, &m1,
    640   1.3        ad 		    &s1, &f1, &tr2, &m2, &s2, &f2))
    641   1.1        ad 			goto Play_Relative_Addresses;
    642   1.1        ad 
    643   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    644  1.15        ad 		if (7 == sscanf(arg, "%d %d:%d %d %d:%d.%d", &tr1, &m1, &s1,
    645   1.3        ad 		    &tr2, &m2, &s2, &f2))
    646   1.1        ad 			goto Play_Relative_Addresses;
    647   1.1        ad 
    648   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    649  1.15        ad 		if (7 == sscanf(arg, "%d %d:%d.%d %d %d:%d", &tr1, &m1, &s1,
    650   1.3        ad 		    &f1, &tr2, &m2, &s2))
    651   1.1        ad 			goto Play_Relative_Addresses;
    652   1.1        ad 
    653   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    654  1.15        ad 		if (7 == sscanf(arg, "%d %d:%d.%d %d:%d.%d", &tr1, &m1, &s1,
    655   1.3        ad 		    &f1, &m2, &s2, &f2))
    656   1.1        ad 			goto Play_Relative_Addresses;
    657   1.1        ad 
    658   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    659   1.3        ad 		if (6 == sscanf(arg, "%d %d:%d.%d %d:%d", &tr1, &m1, &s1, &f1,
    660   1.3        ad 		    &m2, &s2))
    661   1.1        ad 			goto Play_Relative_Addresses;
    662   1.1        ad 
    663   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    664  1.15        ad 		if (6 == sscanf(arg, "%d %d:%d %d:%d.%d", &tr1, &m1, &s1, &m2,
    665   1.3        ad 		    &s2, &f2))
    666   1.1        ad 			goto Play_Relative_Addresses;
    667   1.1        ad 
    668   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    669  1.15        ad 		if (6 == sscanf(arg, "%d %d:%d.%d %d %d", &tr1, &m1, &s1, &f1,
    670   1.3        ad 		    &tr2, &m2))
    671   1.1        ad 			goto Play_Relative_Addresses;
    672   1.1        ad 
    673   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    674  1.22        is 		if (6 == sscanf(arg, "%d %d:%d %d %d:%d", &tr1, &m1, &s1, &tr2,
    675  1.22        is 		    &m2, &s2))
    676  1.22        is 			goto Play_Relative_Addresses;
    677  1.22        is 
    678  1.22        is 		tr2 = m2 = s2 = f2 = f1 = 0;
    679  1.15        ad 		if (5 == sscanf(arg, "%d %d:%d %d:%d", &tr1, &m1, &s1, &m2,
    680   1.3        ad 		    &s2))
    681   1.1        ad 			goto Play_Relative_Addresses;
    682   1.1        ad 
    683   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    684  1.15        ad 		if (5 == sscanf(arg, "%d %d:%d %d %d", &tr1, &m1, &s1, &tr2,
    685   1.3        ad 		    &m2))
    686   1.1        ad 			goto Play_Relative_Addresses;
    687   1.1        ad 
    688  1.22        is 		relend=0;
    689   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    690  1.15        ad 		if (5 == sscanf(arg, "%d %d:%d.%d %d", &tr1, &m1, &s1, &f1,
    691   1.3        ad 		    &tr2))
    692   1.1        ad 			goto Play_Relative_Addresses;
    693   1.1        ad 
    694   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    695   1.1        ad 		if (4 == sscanf(arg, "%d %d:%d %d", &tr1, &m1, &s1, &tr2))
    696   1.1        ad 			goto Play_Relative_Addresses;
    697   1.1        ad 
    698   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    699   1.1        ad 		if (4 == sscanf(arg, "%d %d:%d.%d", &tr1, &m1, &s1, &f1))
    700   1.1        ad 			goto Play_Relative_Addresses;
    701   1.1        ad 
    702   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    703   1.1        ad 		if (3 == sscanf(arg, "%d %d:%d", &tr1, &m1, &s1))
    704   1.1        ad 			goto Play_Relative_Addresses;
    705   1.1        ad 
    706   1.1        ad 		tr2 = m2 = s2 = f2 = f1 = 0;
    707   1.1        ad 		goto Try_Absolute_Timed_Addresses;
    708   1.1        ad 
    709   1.1        ad Play_Relative_Addresses:
    710   1.1        ad 		if (!tr1)
    711   1.1        ad 			tr1 = 1;
    712   1.2        ad 		else if (tr1 > n)
    713   1.2        ad 			tr1 = n;
    714   1.1        ad 
    715  1.22        is 		toc2msf(tr1-1, &tm, &ts, &tf);
    716  1.22        is 		addmsf(&m1, &s1, &f1, tm, ts, tf);
    717  1.22        is 
    718  1.22        is 		toc2msf(tr1, &tm, &ts, &tf);
    719  1.22        is 
    720  1.15        ad 		if ((m1 > tm) || ((m1 == tm) && ((s1 > ts) || ((s1 == ts) &&
    721   1.2        ad 		    (f1 > tf))))) {
    722  1.20    itojun 			warnx("Track %d is not that long.", tr1);
    723   1.1        ad 			return (0);
    724   1.1        ad 		}
    725  1.22        is 		tr1--;	/* XXXXX ???? */
    726   1.1        ad 
    727   1.1        ad 
    728   1.1        ad 		if (!tr2) {
    729  1.22        is 			if (relend) {
    730   1.1        ad 				tr2 = tr1;
    731  1.22        is 
    732  1.22        is 				addmsf(&m2, &s2, &f2, m1, s1, f1);
    733   1.1        ad 			} else {
    734   1.1        ad 				tr2 = n;
    735  1.22        is 
    736  1.22        is 				toc2msf(n, &m2, &s2, &f2);
    737   1.1        ad 			}
    738   1.1        ad 		} else {
    739   1.1        ad 			if (tr2 > n) {
    740   1.1        ad 				tr2 = n;
    741   1.1        ad 				m2 = s2 = f2 = 0;
    742   1.1        ad 			} else {
    743  1.22        is 				if (relend)
    744   1.1        ad 					tr2--;
    745  1.22        is 
    746  1.22        is 				toc2msf(tr2, &tm, &ts, &tf);
    747  1.22        is 				addmsf(&m2, &s2, &f2, tm, ts, tf);
    748   1.1        ad 			}
    749   1.1        ad 		}
    750   1.1        ad 
    751  1.22        is 		toc2msf(n, &tm, &ts, &tf);
    752   1.2        ad 
    753  1.15        ad 		if ((tr2 < n) && ((m2 > tm) || ((m2 == tm) && ((s2 > ts) ||
    754   1.2        ad 		    ((s2 == ts) && (f2 > tf)))))) {
    755  1.20    itojun 			warnx("The playing time of the disc is not that long.");
    756   1.1        ad 			return (0);
    757   1.1        ad 		}
    758   1.2        ad 
    759   1.1        ad 		return (play_msf(m1, s1, f1, m2, s2, f2));
    760   1.1        ad 
    761   1.1        ad Try_Absolute_Timed_Addresses:
    762  1.21        is 		m2 = UINT_MAX;
    763  1.21        is 
    764   1.1        ad 		if (6 != sscanf(arg, "%d:%d.%d%d:%d.%d",
    765   1.1        ad 			&m1, &s1, &f1, &m2, &s2, &f2) &&
    766   1.1        ad 		    5 != sscanf(arg, "%d:%d.%d%d:%d", &m1, &s1, &f1, &m2, &s2) &&
    767   1.1        ad 		    5 != sscanf(arg, "%d:%d%d:%d.%d", &m1, &s1, &m2, &s2, &f2) &&
    768   1.1        ad 		    3 != sscanf(arg, "%d:%d.%d", &m1, &s1, &f1) &&
    769   1.1        ad 		    4 != sscanf(arg, "%d:%d%d:%d", &m1, &s1, &m2, &s2) &&
    770   1.1        ad 		    2 != sscanf(arg, "%d:%d", &m1, &s1))
    771   1.1        ad 			goto Clean_up;
    772   1.1        ad 
    773  1.21        is 		if (m2 == UINT_MAX) {
    774   1.1        ad 			if (msf) {
    775   1.1        ad 				m2 = toc_buffer[n].addr.msf.minute;
    776   1.1        ad 				s2 = toc_buffer[n].addr.msf.second;
    777   1.1        ad 				f2 = toc_buffer[n].addr.msf.frame;
    778   1.1        ad 			} else {
    779  1.33   xtraeme 				lba2msf(toc_buffer[n].addr.lba, &tm, &ts, &tf);
    780   1.1        ad 				m2 = tm;
    781   1.1        ad 				s2 = ts;
    782   1.1        ad 				f2 = tf;
    783   1.1        ad 			}
    784   1.1        ad 		}
    785   1.3        ad 		return (play_msf(m1, s1, f1, m2, s2, f2));
    786   1.1        ad 	}
    787  1.15        ad 
    788   1.1        ad 	/*
    789   1.1        ad 	 * Play track trk1 [ .idx1 ] [ trk2 [ .idx2 ] ]
    790   1.1        ad 	 */
    791   1.1        ad 	if (4 != sscanf(arg, "%d.%d%d.%d", &start, &istart, &end, &iend) &&
    792   1.1        ad 	    3 != sscanf(arg, "%d.%d%d", &start, &istart, &end) &&
    793   1.1        ad 	    3 != sscanf(arg, "%d%d.%d", &start, &end, &iend) &&
    794   1.1        ad 	    2 != sscanf(arg, "%d.%d", &start, &istart) &&
    795   1.1        ad 	    2 != sscanf(arg, "%d%d", &start, &end) &&
    796   1.1        ad 	    1 != sscanf(arg, "%d", &start))
    797   1.1        ad 		goto Clean_up;
    798   1.1        ad 
    799   1.1        ad 	if (end == 0)
    800   1.1        ad 		end = n;
    801   1.1        ad 	return (play_track(start, istart, end, iend));
    802   1.1        ad 
    803   1.1        ad Clean_up:
    804   1.1        ad 	warnx("invalid command arguments");
    805   1.1        ad 	return (0);
    806  1.13  gmcgarry }
    807  1.13  gmcgarry 
    808  1.16        ad void
    809  1.27  christos sig_timer(int sig)
    810  1.16        ad {
    811  1.33   xtraeme 	int aulen, auwr, fpw;
    812  1.16        ad 	sigset_t anymore;
    813  1.16        ad 
    814  1.16        ad 	sigpending(&anymore);
    815  1.16        ad 	if (sigismember(&anymore, SIGALRM))
    816  1.16        ad 		return;
    817  1.33   xtraeme 	if (digital) {
    818  1.33   xtraeme 		if (!da.playing)
    819  1.33   xtraeme 			return;
    820  1.33   xtraeme 		if (da.changed) {
    821  1.33   xtraeme 			da.lba_current = da.lba_start;
    822  1.33   xtraeme 			da.changed = 0;
    823  1.33   xtraeme 		}
    824  1.33   xtraeme 		/* read frames into circular buffer */
    825  1.33   xtraeme 		fpw = da.lba_end - da.lba_current + 1;
    826  1.33   xtraeme 		if (fpw > da.fpw)
    827  1.33   xtraeme 			fpw = da.fpw;
    828  1.33   xtraeme 		if (fpw > 0) {
    829  1.33   xtraeme 			aulen = readaudio(fd, da.lba_current, fpw, da.aubuf);
    830  1.33   xtraeme 			if (aulen > 0) {
    831  1.33   xtraeme 				auwr = write(da.afd, da.aubuf, aulen);
    832  1.33   xtraeme 				da.lba_current += fpw;
    833  1.33   xtraeme 			}
    834  1.33   xtraeme 		}
    835  1.33   xtraeme 		if (da.lba_current > da.lba_end)
    836  1.33   xtraeme 			da.playing = 0;
    837  1.33   xtraeme 	}
    838  1.33   xtraeme 	if (shuffle)
    839  1.33   xtraeme 		skip(0, 0);
    840  1.33   xtraeme 	sched_yield();
    841  1.16        ad 	setitimer(ITIMER_REAL, &itv_timer, NULL);
    842  1.16        ad }
    843  1.16        ad 
    844  1.13  gmcgarry int
    845  1.16        ad skip(int dir, int fromuser)
    846  1.13  gmcgarry {
    847  1.15        ad 	char str[16];
    848  1.16        ad 	int rv, trk, idx, m, s, f;
    849  1.13  gmcgarry 	struct ioc_toc_header h;
    850  1.15        ad 
    851  1.15        ad 	if ((rv = ioctl(fd, CDIOREADTOCHEADER, &h)) <  0) {
    852  1.15        ad 		warn("ioctl(CDIOREADTOCHEADER)");
    853  1.15        ad 		return (rv);
    854  1.15        ad 	}
    855  1.16        ad 	if ((rv = get_status(&trk, &idx, &m, &s, &f)) < 0)
    856  1.15        ad 		return (rv);
    857  1.16        ad 
    858  1.33   xtraeme 	if (dir == 0 || shuffle != 0) {
    859  1.16        ad 		if (fromuser || (rv != CD_AS_PLAY_IN_PROGRESS &&
    860  1.16        ad 		    rv != CD_AS_PLAY_PAUSED))
    861  1.31   garbled 			trk = shuffle < 0 ? (-shuffle) : (h.starting_track +
    862  1.31   garbled 			    arc4random() % (h.ending_track - h.starting_track + 1));
    863  1.16        ad 		else
    864  1.16        ad 			return (0);
    865  1.16        ad 	} else {
    866  1.16        ad 		trk += dir;
    867  1.16        ad 		if (trk > h.ending_track)
    868  1.16        ad 			trk = h.starting_track;
    869  1.16        ad 		else if(trk < h.starting_track)
    870  1.16        ad 			trk = h.ending_track;
    871  1.16        ad 	}
    872  1.16        ad 
    873  1.16        ad 	if (shuffle)
    874  1.25    itojun 		snprintf(str, sizeof(str), "%d %d", trk, trk);
    875  1.16        ad 	else
    876  1.25    itojun 		snprintf(str, sizeof(str), "%d", trk);
    877  1.16        ad 
    878  1.16        ad 	return (play(str, 0));
    879   1.1        ad }
    880   1.1        ad 
    881  1.15        ad const char *
    882  1.15        ad strstatus(int sts)
    883   1.1        ad {
    884  1.15        ad 	const char *str;
    885   1.2        ad 
    886   1.1        ad 	switch (sts) {
    887  1.15        ad 	case CD_AS_AUDIO_INVALID:
    888  1.15        ad 		str = "invalid";
    889  1.15        ad 		break;
    890  1.15        ad 	case CD_AS_PLAY_IN_PROGRESS:
    891  1.15        ad 		str = "playing";
    892  1.15        ad 		break;
    893  1.15        ad 	case CD_AS_PLAY_PAUSED:
    894  1.15        ad 		str = "paused";
    895  1.15        ad 		break;
    896  1.15        ad 	case CD_AS_PLAY_COMPLETED:
    897  1.15        ad 		str = "completed";
    898  1.15        ad 		break;
    899  1.15        ad 	case CD_AS_PLAY_ERROR:
    900  1.15        ad 		str = "error";
    901  1.15        ad 		break;
    902  1.15        ad 	case CD_AS_NO_STATUS:
    903  1.15        ad 		str = "not playing";
    904  1.15        ad 		break;
    905   1.1        ad 	default:
    906  1.15        ad 		str = "<unknown>";
    907  1.15        ad 		break;
    908   1.1        ad 	}
    909  1.15        ad 
    910  1.15        ad 	return (str);
    911   1.1        ad }
    912   1.1        ad 
    913  1.15        ad int
    914  1.15        ad print_status(const char *arg)
    915   1.1        ad {
    916   1.2        ad 	struct cd_sub_channel_info data;
    917   1.1        ad 	struct ioc_read_subchannel ss;
    918  1.16        ad 	int rv, trk, idx, m, s, f;
    919   1.2        ad 	struct ioc_vol v;
    920   1.1        ad 
    921  1.16        ad 	if ((rv = get_status(&trk, &idx, &m, &s, &f)) >= 0) {
    922  1.16        ad 		printf("audio status:\t%s\n", strstatus(rv));
    923  1.15        ad 		printf("current track:\t%d\n", trk);
    924  1.33   xtraeme 		if (!digital)
    925  1.33   xtraeme 			printf("current index:\t%d\n", idx);
    926  1.15        ad 		printf("position:\t%d:%02d.%02d\n", m, s, f);
    927  1.15        ad 	} else
    928  1.15        ad 		printf("audio status:\tno info available\n");
    929  1.15        ad 
    930  1.31   garbled 	if (shuffle < 0)
    931  1.31   garbled 		printf("single track:\t%d\n", -shuffle);
    932  1.31   garbled 	else
    933  1.31   garbled 		printf("shuffle play:\t%s\n", (shuffle != 0) ? "on" : "off");
    934  1.33   xtraeme 	if (digital)
    935  1.33   xtraeme 		printf("digital xfer:\tto %s (%d frames per wakeup, %ld.%03lds period)\n",
    936  1.33   xtraeme 		    da.auname, da.fpw, itv_timer.it_interval.tv_sec,
    937  1.33   xtraeme 		    itv_timer.it_interval.tv_usec);
    938  1.33   xtraeme 	else
    939  1.33   xtraeme 		printf("digital xfer:\toff\n");
    940  1.16        ad 
    941  1.15        ad 	bzero(&ss, sizeof(ss));
    942  1.15        ad 	ss.data = &data;
    943  1.15        ad 	ss.data_len = sizeof(data);
    944  1.15        ad 	ss.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
    945  1.15        ad 	ss.data_format = CD_MEDIA_CATALOG;
    946  1.15        ad 
    947  1.33   xtraeme 	if (!digital && ioctl(fd, CDIOCREADSUBCHANNEL, (char *) &ss) >= 0) {
    948  1.15        ad 		printf("media catalog:\t%sactive",
    949  1.15        ad 		    ss.data->what.media_catalog.mc_valid ? "" : "in");
    950  1.15        ad 		if (ss.data->what.media_catalog.mc_valid &&
    951  1.15        ad 		    ss.data->what.media_catalog.mc_number[0])
    952  1.15        ad 			printf(" (%.15s)",
    953  1.15        ad 			    ss.data->what.media_catalog.mc_number);
    954  1.15        ad 		putchar('\n');
    955  1.15        ad 	} else
    956  1.15        ad 		printf("media catalog:\tnone\n");
    957  1.15        ad 
    958  1.33   xtraeme 	if (digital)
    959  1.33   xtraeme 		return (0);
    960  1.16        ad 	if (ioctl(fd, CDIOCGETVOL, &v) >= 0) {
    961  1.15        ad 		printf("left volume:\t%d\n", v.vol[0]);
    962  1.15        ad 		printf("right volume:\t%d\n", v.vol[1]);
    963  1.15        ad 	} else {
    964  1.15        ad 		printf("left volume:\tnot available\n");
    965  1.15        ad 		printf("right volume:\tnot available\n");
    966   1.1        ad 	}
    967  1.15        ad 
    968  1.16        ad ;	return (0);
    969   1.1        ad }
    970   1.1        ad 
    971  1.15        ad int
    972  1.15        ad info(const char *arg)
    973   1.1        ad {
    974   1.1        ad 	struct ioc_toc_header h;
    975   1.1        ad 	int rc, i, n;
    976   1.1        ad 
    977   1.1        ad 	if ((rc = ioctl(fd, CDIOREADTOCHEADER, &h)) < 0) {
    978  1.15        ad 		warn("ioctl(CDIOREADTOCHEADER)");
    979   1.1        ad 		return (rc);
    980   1.1        ad 	}
    981   1.1        ad 
    982   1.1        ad 	n = h.ending_track - h.starting_track + 1;
    983   1.1        ad 	rc = read_toc_entrys((n + 1) * sizeof(struct cd_toc_entry));
    984   1.1        ad 	if (rc < 0)
    985   1.1        ad 		return (rc);
    986   1.1        ad 
    987  1.23    simonb 	printf("track     start  duration   block  length     type\n");
    988  1.23    simonb 	printf("--------------------------------------------------\n");
    989   1.1        ad 
    990   1.1        ad 	for (i = 0; i < n; i++) {
    991   1.1        ad 		printf("%5d  ", toc_buffer[i].track);
    992  1.23    simonb 		print_track(toc_buffer + i);
    993   1.1        ad 	}
    994  1.24      matt 	printf("    -  ");	/* Lead-out area */
    995  1.23    simonb 	print_track(toc_buffer + n);
    996   1.1        ad 	return (0);
    997   1.1        ad }
    998   1.1        ad 
    999  1.15        ad void
   1000  1.15        ad lba2msf(u_long lba, u_int *m, u_int *s, u_int *f)
   1001   1.1        ad {
   1002  1.15        ad 
   1003   1.1        ad 	lba += 150;		/* block start offset */
   1004   1.1        ad 	lba &= 0xffffff;	/* negative lbas use only 24 bits */
   1005   1.1        ad 	*m = lba / (60 * 75);
   1006   1.1        ad 	lba %= (60 * 75);
   1007   1.1        ad 	*s = lba / 75;
   1008   1.1        ad 	*f = lba % 75;
   1009   1.1        ad }
   1010   1.1        ad 
   1011  1.15        ad u_int
   1012  1.15        ad msf2lba(u_int m, u_int s, u_int f)
   1013   1.1        ad {
   1014   1.2        ad 
   1015   1.1        ad 	return (((m * 60) + s) * 75 + f) - 150;
   1016   1.1        ad }
   1017   1.1        ad 
   1018  1.15        ad void
   1019  1.23    simonb print_track(struct cd_toc_entry *e)
   1020   1.1        ad {
   1021   1.1        ad 	int block, next, len;
   1022  1.15        ad 	u_int m, s, f;
   1023   1.1        ad 
   1024   1.1        ad 	if (msf) {
   1025   1.1        ad 		/* Print track start */
   1026   1.1        ad 		printf("%2d:%02d.%02d  ", e->addr.msf.minute,
   1027   1.1        ad 		    e->addr.msf.second, e->addr.msf.frame);
   1028   1.1        ad 
   1029   1.1        ad 		block = msf2lba(e->addr.msf.minute, e->addr.msf.second,
   1030   1.1        ad 		    e->addr.msf.frame);
   1031   1.1        ad 	} else {
   1032   1.7        ad 		block = e->addr.lba;
   1033   1.1        ad 		lba2msf(block, &m, &s, &f);
   1034   1.1        ad 		/* Print track start */
   1035   1.1        ad 		printf("%2d:%02d.%02d  ", m, s, f);
   1036   1.1        ad 	}
   1037  1.23    simonb 	if (e->track > CD_MAX_TRACK) {
   1038  1.23    simonb 		/* lead-out area -- print block */
   1039  1.23    simonb 		printf("       -  %6d       - lead-out\n", block);
   1040   1.1        ad 		return;
   1041   1.1        ad 	}
   1042   1.1        ad 	if (msf)
   1043   1.1        ad 		next = msf2lba(e[1].addr.msf.minute, e[1].addr.msf.second,
   1044   1.1        ad 		    e[1].addr.msf.frame);
   1045   1.1        ad 	else
   1046   1.7        ad 		next = e[1].addr.lba;
   1047   1.1        ad 	len = next - block;
   1048  1.35     chuck 	/* XXX: take into account the 150 frame start offset time */
   1049  1.35     chuck 	/* XXX: this is a mis-use of lba2msf() because 'len' is a */
   1050  1.35     chuck 	/* XXX: length in frames and not a LBA! */
   1051  1.35     chuck 	lba2msf(len - 150, &m, &s, &f);
   1052   1.1        ad 
   1053   1.1        ad 	/* Print duration, block, length, type */
   1054  1.23    simonb 	printf("%2d:%02d.%02d  %6d  %6d %8s\n", m, s, f, block, len,
   1055   1.1        ad 	    (e->control & 4) ? "data" : "audio");
   1056   1.1        ad }
   1057   1.1        ad 
   1058  1.15        ad int
   1059  1.15        ad play_track(int tstart, int istart, int tend, int iend)
   1060   1.1        ad {
   1061   1.1        ad 	struct ioc_play_track t;
   1062  1.15        ad 	int rv;
   1063   1.1        ad 
   1064  1.33   xtraeme 	if (digital) {
   1065  1.33   xtraeme 		tstart--;
   1066  1.33   xtraeme 		if (msf) {
   1067  1.33   xtraeme 			return (play_msf(toc_buffer[tstart].addr.msf.minute,
   1068  1.33   xtraeme 				toc_buffer[tstart].addr.msf.second, toc_buffer[tstart].addr.msf.frame,
   1069  1.33   xtraeme 				toc_buffer[tend].addr.msf.minute, toc_buffer[tend].addr.msf.second,
   1070  1.33   xtraeme 				toc_buffer[tend].addr.msf.frame));
   1071  1.33   xtraeme 		} else
   1072  1.33   xtraeme 			return (play_digital(toc_buffer[tstart].addr.lba,
   1073  1.33   xtraeme 				toc_buffer[tend].addr.lba));
   1074  1.33   xtraeme 	}
   1075   1.1        ad 	t.start_track = tstart;
   1076   1.1        ad 	t.start_index = istart;
   1077   1.1        ad 	t.end_track = tend;
   1078   1.1        ad 	t.end_index = iend;
   1079   1.1        ad 
   1080  1.15        ad 	if ((rv = ioctl(fd, CDIOCPLAYTRACKS, &t)) < 0)
   1081  1.15        ad 		warn("ioctl(CDIOCPLAYTRACKS)");
   1082  1.15        ad 	return (rv);
   1083   1.1        ad }
   1084   1.1        ad 
   1085  1.15        ad int
   1086  1.15        ad play_blocks(int blk, int len)
   1087   1.1        ad {
   1088   1.1        ad 	struct ioc_play_blocks t;
   1089  1.15        ad 	int rv;
   1090   1.1        ad 
   1091   1.1        ad 	t.blk = blk;
   1092   1.1        ad 	t.len = len;
   1093   1.1        ad 
   1094  1.15        ad 	if ((rv = ioctl(fd, CDIOCPLAYBLOCKS, &t)) < 0)
   1095  1.15        ad 		warn("ioctl(CDIOCPLAYBLOCKS");
   1096  1.15        ad 	return (rv);
   1097   1.1        ad }
   1098   1.1        ad 
   1099  1.15        ad int
   1100  1.33   xtraeme play_digital(start, end)
   1101  1.33   xtraeme 	int start, end;
   1102  1.33   xtraeme {
   1103  1.33   xtraeme 	da.lba_start = start;
   1104  1.33   xtraeme 	da.lba_end = --end;
   1105  1.33   xtraeme 	da.changed = da.playing = 1;
   1106  1.33   xtraeme 	return (0);
   1107  1.33   xtraeme }
   1108  1.33   xtraeme 
   1109  1.33   xtraeme int
   1110  1.15        ad setvol(int left, int right)
   1111   1.1        ad {
   1112   1.1        ad 	struct ioc_vol v;
   1113  1.15        ad 	int rv;
   1114   1.1        ad 
   1115   1.1        ad 	v.vol[0] = left;
   1116   1.1        ad 	v.vol[1] = right;
   1117   1.1        ad 	v.vol[2] = 0;
   1118   1.1        ad 	v.vol[3] = 0;
   1119   1.1        ad 
   1120  1.15        ad 	if ((rv = ioctl(fd, CDIOCSETVOL, &v)) < 0)
   1121  1.15        ad 		warn("ioctl(CDIOCSETVOL)");
   1122  1.15        ad 	return (rv);
   1123   1.1        ad }
   1124   1.1        ad 
   1125  1.15        ad int
   1126  1.15        ad read_toc_entrys(int len)
   1127   1.1        ad {
   1128   1.1        ad 	struct ioc_read_toc_entry t;
   1129  1.15        ad 	int rv;
   1130   1.1        ad 
   1131   1.1        ad 	t.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
   1132   1.1        ad 	t.starting_track = 0;
   1133   1.1        ad 	t.data_len = len;
   1134   1.1        ad 	t.data = toc_buffer;
   1135   1.1        ad 
   1136  1.15        ad 	if ((rv = ioctl(fd, CDIOREADTOCENTRYS, &t)) < 0)
   1137  1.15        ad 		warn("ioctl(CDIOREADTOCENTRYS)");
   1138  1.33   xtraeme 	tbvalid = 1;
   1139  1.15        ad 	return (rv);
   1140   1.1        ad }
   1141   1.1        ad 
   1142  1.15        ad int
   1143  1.15        ad play_msf(int start_m, int start_s, int start_f, int end_m, int end_s,
   1144  1.15        ad 	 int end_f)
   1145   1.1        ad {
   1146   1.1        ad 	struct ioc_play_msf a;
   1147  1.15        ad 	int rv;
   1148   1.1        ad 
   1149  1.33   xtraeme 	if (digital)
   1150  1.33   xtraeme 		return (play_digital(msf2lba(start_m, start_s, start_f),
   1151  1.33   xtraeme 			msf2lba(end_m, end_s, end_f)));
   1152   1.1        ad 	a.start_m = start_m;
   1153   1.1        ad 	a.start_s = start_s;
   1154   1.1        ad 	a.start_f = start_f;
   1155   1.1        ad 	a.end_m = end_m;
   1156   1.1        ad 	a.end_s = end_s;
   1157   1.1        ad 	a.end_f = end_f;
   1158   1.1        ad 
   1159  1.15        ad 	if ((rv = ioctl(fd, CDIOCPLAYMSF, &a)) < 0)
   1160  1.15        ad 		warn("ioctl(CDIOREADTOCENTRYS)");
   1161  1.15        ad 	return (rv);
   1162   1.1        ad }
   1163   1.1        ad 
   1164  1.15        ad int
   1165  1.16        ad get_status(int *trk, int *idx, int *min, int *sec, int *frame)
   1166   1.1        ad {
   1167   1.1        ad 	struct ioc_read_subchannel s;
   1168   1.1        ad 	struct cd_sub_channel_info data;
   1169  1.33   xtraeme 	struct ioc_toc_header h;
   1170  1.15        ad 	u_int mm, ss, ff;
   1171  1.15        ad 	int rv;
   1172  1.33   xtraeme 	int lba, i, n, rc;
   1173  1.33   xtraeme 
   1174  1.33   xtraeme 	if (!tbvalid) {
   1175  1.33   xtraeme 		if ((rc = ioctl(fd, CDIOREADTOCHEADER, &h)) < 0) {
   1176  1.33   xtraeme 			warn("ioctl(CDIOREADTOCHEADER)");
   1177  1.33   xtraeme 			return (rc);
   1178  1.33   xtraeme 		}
   1179  1.33   xtraeme 
   1180  1.33   xtraeme 		n = h.ending_track - h.starting_track + 1;
   1181  1.33   xtraeme 		rc = read_toc_entrys((n + 1) * sizeof(struct cd_toc_entry));
   1182  1.33   xtraeme 		if (rc < 0)
   1183  1.33   xtraeme 			return (rc);
   1184  1.33   xtraeme 	}
   1185   1.1        ad 
   1186  1.33   xtraeme #define SWAPLBA(x) (msf?be32toh(x):(x))
   1187  1.33   xtraeme 	if (digital && da.playing) {
   1188  1.33   xtraeme 		lba = da.lba_current + 150;
   1189  1.33   xtraeme 		for (i = 1; i < 99; i++) {
   1190  1.33   xtraeme 			if (lba < SWAPLBA(toc_buffer[i].addr.lba)) {
   1191  1.33   xtraeme 				lba -= SWAPLBA(toc_buffer[i - 1].addr.lba);
   1192  1.33   xtraeme 				*trk = i;
   1193  1.33   xtraeme 				break;
   1194  1.33   xtraeme 			}
   1195  1.33   xtraeme 		}
   1196  1.33   xtraeme 		lba2msf(lba - 150, &mm, &ss, &ff);
   1197  1.33   xtraeme 		*min = mm;
   1198  1.33   xtraeme 		*sec = ss;
   1199  1.33   xtraeme 		*frame = ff;
   1200  1.33   xtraeme 		*idx = 0;
   1201  1.33   xtraeme 		return CD_AS_PLAY_IN_PROGRESS;
   1202  1.33   xtraeme 	}
   1203   1.1        ad 	bzero(&s, sizeof(s));
   1204   1.1        ad 	s.data = &data;
   1205   1.1        ad 	s.data_len = sizeof(data);
   1206   1.1        ad 	s.address_format = msf ? CD_MSF_FORMAT : CD_LBA_FORMAT;
   1207   1.1        ad 	s.data_format = CD_CURRENT_POSITION;
   1208   1.1        ad 
   1209  1.15        ad 	if ((rv = ioctl(fd, CDIOCREADSUBCHANNEL, &s)) < 0) {
   1210  1.15        ad 		warn("ioctl(CDIOCREADSUBCHANNEL)");
   1211  1.15        ad 		return (rv);
   1212  1.15        ad 	}
   1213   1.1        ad 
   1214   1.1        ad 	*trk = s.data->what.position.track_number;
   1215  1.16        ad 	*idx = s.data->what.position.index_number;
   1216   1.1        ad 	if (msf) {
   1217   1.1        ad 		*min = s.data->what.position.reladdr.msf.minute;
   1218   1.1        ad 		*sec = s.data->what.position.reladdr.msf.second;
   1219   1.1        ad 		*frame = s.data->what.position.reladdr.msf.frame;
   1220   1.1        ad 	} else {
   1221  1.33   xtraeme 		lba2msf(s.data->what.position.reladdr.lba, &mm,
   1222  1.15        ad 		    &ss, &ff);
   1223   1.1        ad 		*min = mm;
   1224   1.1        ad 		*sec = ss;
   1225   1.1        ad 		*frame = ff;
   1226   1.1        ad 	}
   1227   1.1        ad 
   1228   1.3        ad 	return (s.data->header.audio_status);
   1229   1.1        ad }
   1230   1.1        ad 
   1231  1.15        ad const char *
   1232  1.15        ad prompt(void)
   1233   1.1        ad {
   1234  1.15        ad 
   1235   1.4   msaitoh 	return ("cdplay> ");
   1236   1.1        ad }
   1237   1.1        ad 
   1238  1.15        ad const char *
   1239  1.15        ad parse(char *buf, int *cmd)
   1240   1.1        ad {
   1241  1.15        ad 	const struct cmdtab *c, *mc;
   1242   1.3        ad 	char *p, *q;
   1243   1.1        ad 	int len;
   1244   1.1        ad 
   1245  1.29       dsl 	for (p = buf; isspace((unsigned char)*p); p++)
   1246   1.1        ad 		continue;
   1247   1.1        ad 
   1248  1.29       dsl 	if (isdigit((unsigned char)*p) || (p[0] == '#' && isdigit((unsigned char)p[1]))) {
   1249   1.1        ad 		*cmd = CMD_PLAY;
   1250   1.1        ad 		return (p);
   1251   1.1        ad 	}
   1252   1.3        ad 
   1253  1.29       dsl 	for (buf = p; *p != '\0' && !isspace((unsigned char)*p); p++)
   1254   1.1        ad 		continue;
   1255   1.1        ad 
   1256  1.15        ad 	if ((len = p - buf) == 0)
   1257   1.1        ad 		return (0);
   1258   1.1        ad 
   1259  1.15        ad 	if (*p != '\0') {		/* It must be a spacing character! */
   1260   1.1        ad 		*p++ = 0;
   1261  1.15        ad 		for (q = p; *q != '\0' && *q != '\n' && *q != '\r'; q++)
   1262   1.1        ad 			continue;
   1263   1.1        ad 		*q = 0;
   1264   1.1        ad 	}
   1265   1.3        ad 
   1266   1.1        ad 	*cmd = -1;
   1267   1.3        ad 
   1268  1.15        ad 	mc = cmdtab + sizeof(cmdtab) / sizeof(cmdtab[0]);
   1269  1.15        ad 	for (c = cmdtab; c < mc; c++) {
   1270   1.1        ad 		/* Is it an exact match? */
   1271  1.15        ad 		if (strcasecmp(buf, c->name) == 0) {
   1272   1.1        ad 			*cmd = c->command;
   1273   1.1        ad 			break;
   1274   1.1        ad 		}
   1275   1.1        ad 		/* Try short hand forms then... */
   1276  1.15        ad 		if (len >= c->min && strncasecmp(buf, c->name, len) == 0) {
   1277   1.1        ad 			if (*cmd != -1 && *cmd != c->command) {
   1278   1.1        ad 				warnx("ambiguous command");
   1279   1.1        ad 				return (0);
   1280   1.1        ad 			}
   1281   1.1        ad 			*cmd = c->command;
   1282   1.1        ad 		}
   1283   1.1        ad 	}
   1284   1.1        ad 
   1285   1.1        ad 	if (*cmd == -1) {
   1286   1.1        ad 		warnx("invalid command, enter ``help'' for commands");
   1287   1.1        ad 		return (0);
   1288   1.1        ad 	}
   1289   1.3        ad 
   1290  1.29       dsl 	while (isspace((unsigned char)*p))
   1291   1.1        ad 		p++;
   1292   1.3        ad 	return (p);
   1293   1.1        ad }
   1294   1.1        ad 
   1295  1.15        ad int
   1296  1.15        ad opencd(void)
   1297   1.1        ad {
   1298   1.1        ad 	char devbuf[80];
   1299   1.1        ad 
   1300   1.1        ad 	if (fd > -1)
   1301   1.1        ad 		return (1);
   1302   1.1        ad 
   1303  1.11     lukem 	fd = opendisk(cdname, O_RDONLY, devbuf, sizeof(devbuf), 0);
   1304   1.1        ad 	if (fd < 0) {
   1305   1.1        ad 		if (errno == ENXIO) {
   1306  1.15        ad 			/*
   1307  1.15        ad 			 * ENXIO has an overloaded meaning here. The
   1308  1.15        ad 			 * original "Device not configured" should be
   1309  1.15        ad 			 * interpreted as "No disc in drive %s".
   1310  1.15        ad 			 */
   1311   1.1        ad 			warnx("no disc in drive %s", devbuf);
   1312   1.1        ad 			return (0);
   1313   1.1        ad 		}
   1314  1.15        ad 		err(EXIT_FAILURE, "%s", devbuf);
   1315   1.1        ad 	}
   1316  1.33   xtraeme 	return (1);
   1317  1.33   xtraeme }
   1318  1.33   xtraeme 
   1319  1.33   xtraeme int
   1320  1.33   xtraeme openaudio()
   1321  1.33   xtraeme {
   1322  1.33   xtraeme 	audio_info_t ai;
   1323  1.33   xtraeme 	audio_encoding_t ae;
   1324  1.33   xtraeme 	int rc, aei;
   1325   1.3        ad 
   1326  1.33   xtraeme 	if (da.afd > -1)
   1327  1.33   xtraeme 	return (1);
   1328  1.33   xtraeme 	da.afd = open(da.auname, O_WRONLY);
   1329  1.33   xtraeme 	if (da.afd < 0) {
   1330  1.33   xtraeme 		warn("openaudio");
   1331  1.33   xtraeme 		return (0);
   1332  1.33   xtraeme 	}
   1333  1.33   xtraeme 	AUDIO_INITINFO(&ai);
   1334  1.33   xtraeme 	ae.index = 0;
   1335  1.33   xtraeme 	aei = -1;
   1336  1.33   xtraeme 	rc = ioctl(da.afd, AUDIO_GETENC, &ae);
   1337  1.33   xtraeme 	do {
   1338  1.33   xtraeme 		if (ae.encoding == AUDIO_ENCODING_SLINEAR_LE && ae.precision == 16)
   1339  1.33   xtraeme 			aei = ae.index;
   1340  1.33   xtraeme 		ae.index++;
   1341  1.33   xtraeme 		rc = ioctl(da.afd, AUDIO_GETENC, &ae);
   1342  1.33   xtraeme 	} while (rc == 0);
   1343  1.33   xtraeme 	if (aei == -1) {
   1344  1.33   xtraeme 		warn("No suitable audio encoding found!");
   1345  1.33   xtraeme 		close(da.afd);
   1346  1.33   xtraeme 		da.afd = -1;
   1347  1.33   xtraeme 		return (0);
   1348  1.33   xtraeme 	}
   1349  1.33   xtraeme 	ai.mode = AUMODE_PLAY;
   1350  1.33   xtraeme 	ai.play.sample_rate = 44100;
   1351  1.33   xtraeme 	ai.play.channels = 2;
   1352  1.33   xtraeme 	ai.play.precision = 16;
   1353  1.33   xtraeme 	ai.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
   1354  1.33   xtraeme 	ai.blocksize = 0;
   1355  1.33   xtraeme 	rc = ioctl(da.afd, AUDIO_SETINFO, &ai);
   1356  1.33   xtraeme 	if (rc < 0) {
   1357  1.33   xtraeme 		warn("AUDIO_SETINFO");
   1358  1.33   xtraeme 		close(da.afd);
   1359  1.33   xtraeme 		da.afd = -1;
   1360  1.33   xtraeme 		return (0);
   1361  1.33   xtraeme 	}
   1362   1.1        ad 	return (1);
   1363  1.22        is }
   1364  1.22        is 
   1365  1.33   xtraeme int
   1366  1.33   xtraeme readaudio(afd, lba, blocks, data)
   1367  1.33   xtraeme 	int afd, lba, blocks;
   1368  1.33   xtraeme 	u_char *data;
   1369  1.33   xtraeme {
   1370  1.33   xtraeme 	struct scsireq sc;
   1371  1.33   xtraeme 	int rc;
   1372  1.33   xtraeme 
   1373  1.33   xtraeme 	memset(&sc, 0, sizeof(sc));
   1374  1.33   xtraeme 	sc.cmd[0] = 0xBE;
   1375  1.33   xtraeme 	sc.cmd[1] = 1 << 2;
   1376  1.33   xtraeme 	sc.cmd[2] = (lba >> 24) & 0xff;
   1377  1.33   xtraeme 	sc.cmd[3] = (lba >> 16) & 0xff;
   1378  1.33   xtraeme 	sc.cmd[4] = (lba >> 8) & 0xff;
   1379  1.33   xtraeme 	sc.cmd[5] = lba & 0xff;
   1380  1.33   xtraeme 	sc.cmd[6] = (blocks >> 16) & 0xff;
   1381  1.33   xtraeme 	sc.cmd[7] = (blocks >> 8) & 0xff;
   1382  1.33   xtraeme 	sc.cmd[8] = blocks & 0xff;
   1383  1.33   xtraeme 	sc.cmd[9] = 1 << 4;
   1384  1.33   xtraeme 	sc.cmd[10] = 0;
   1385  1.33   xtraeme 	sc.cmdlen = 12;
   1386  1.33   xtraeme 	sc.databuf = (caddr_t) data;
   1387  1.33   xtraeme 	sc.datalen = CDDA_SIZE * blocks;
   1388  1.33   xtraeme 	sc.senselen = sizeof(sc.sense);
   1389  1.33   xtraeme 	sc.flags = SCCMD_READ;
   1390  1.33   xtraeme 	sc.timeout = da.fpw * 15;
   1391  1.33   xtraeme 	rc = ioctl(afd, SCIOCCOMMAND, &sc);
   1392  1.33   xtraeme 	if (rc < 0 || sc.retsts != SCCMD_OK) {
   1393  1.33   xtraeme 		if (da.read_errors < 10) {
   1394  1.33   xtraeme 			warnx("scsi cmd failed: retsts %d status %d\n",
   1395  1.33   xtraeme 			    sc.retsts, sc.status);
   1396  1.33   xtraeme 		}
   1397  1.33   xtraeme 		da.read_errors++;
   1398  1.33   xtraeme 		return -1;
   1399  1.33   xtraeme 	}
   1400  1.33   xtraeme 	return CDDA_SIZE * blocks;
   1401  1.33   xtraeme }
   1402  1.33   xtraeme 
   1403  1.22        is void
   1404  1.22        is toc2msf(u_int i, u_int *m, u_int *s, u_int *f)
   1405  1.22        is {
   1406  1.22        is 	struct cd_toc_entry *ctep;
   1407  1.22        is 
   1408  1.23    simonb 	assert(i <= CD_MAX_TRACK);
   1409  1.22        is 
   1410  1.22        is 	ctep = &toc_buffer[i];
   1411  1.22        is 
   1412  1.22        is 	if (msf) {
   1413  1.22        is 		*m = ctep->addr.msf.minute;
   1414  1.22        is 		*s = ctep->addr.msf.second;
   1415  1.22        is 		*f = ctep->addr.msf.frame;
   1416  1.22        is 	} else {
   1417  1.33   xtraeme 		lba2msf(ctep->addr.lba, m, s, f);
   1418  1.22        is 	}
   1419  1.22        is }
   1420  1.22        is 
   1421  1.22        is int
   1422  1.22        is toc2lba(u_int i)
   1423  1.22        is {
   1424  1.22        is 	struct cd_toc_entry *ctep;
   1425  1.22        is 
   1426  1.22        is 	assert(i > 0);
   1427  1.23    simonb 	assert(i <= CD_MAX_TRACK);
   1428  1.22        is 
   1429  1.22        is 	ctep = &toc_buffer[i-1];
   1430  1.22        is 
   1431  1.22        is 	if (msf) {
   1432  1.22        is 		return msf2lba(
   1433  1.22        is 		    ctep->addr.msf.minute,
   1434  1.22        is 		    ctep->addr.msf.second,
   1435  1.22        is 		    ctep->addr.msf.frame);
   1436  1.22        is 	} else {
   1437  1.33   xtraeme 		return (ctep->addr.lba);
   1438  1.22        is 	}
   1439  1.22        is }
   1440  1.22        is 
   1441  1.22        is void
   1442  1.22        is addmsf(u_int *m, u_int *s, u_int *f, u_int m2, u_int s2, u_int f2)
   1443  1.22        is {
   1444  1.22        is 	*f += f2;
   1445  1.22        is 	if (*f > 75) {
   1446  1.22        is 		*s += *f / 75;
   1447  1.22        is 		*f %= 75;
   1448  1.22        is 	}
   1449  1.22        is 
   1450  1.22        is 	*s += s2;
   1451  1.22        is 	if (*s > 60) {
   1452  1.22        is 		*m += *s / 60;
   1453  1.22        is 		*s %= 60;
   1454  1.22        is 	}
   1455  1.22        is 
   1456  1.22        is 	*m += m2;
   1457   1.1        ad }
   1458