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