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