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