2 * Compact Disc Control Utility by Serge V. Vakulenko <vak@cronyx.ru>.
3 * Based on the non-X based CD player by Jean-Marc Zucconi and
6 * Fixed and further modified on 5-Sep-1995 by Jukka Ukkonen <jau@funet.fi>.
8 * 11-Sep-1995: Jukka A. Ukkonen <jau@funet.fi>
9 * A couple of further fixes to my own earlier "fixes".
11 * 18-Sep-1995: Jukka A. Ukkonen <jau@funet.fi>
12 * Added an ability to specify addresses relative to the
13 * beginning of a track. This is in fact a variation of
14 * doing the simple play_msf() call.
16 * 11-Oct-1995: Serge V.Vakulenko <vak@cronyx.ru>
17 * New eject algorithm.
18 * Some code style reformatting.
20 * $FreeBSD: src/usr.sbin/cdcontrol/cdcontrol.c,v 1.24.2.11 2002/11/20 00:26:19 njl Exp $
24 #include <sys/cdrio.h>
26 #include <sys/ioctl.h>
27 #include <sys/param.h>
42 #define ASTS_INVALID 0x00 /* Audio status byte not valid */
43 #define ASTS_PLAYING 0x11 /* Audio play operation in progress */
44 #define ASTS_PAUSED 0x12 /* Audio play operation paused */
45 #define ASTS_COMPLETED 0x13 /* Audio play operation successfully completed */
46 #define ASTS_ERROR 0x14 /* Audio play operation stopped due to error */
47 #define ASTS_VOID 0x15 /* No current audio status to return */
49 #ifndef DEFAULT_CD_DRIVE
50 # define DEFAULT_CD_DRIVE "/dev/cd0c"
53 #ifndef DEFAULT_CD_PARTITION
54 # define DEFAULT_CD_PARTITION "c"
73 #define CMD_PREVIOUS 17
75 #define STATUS_AUDIO 0x1
76 #define STATUS_MEDIA 0x2
77 #define STATUS_VOLUME 0x4
85 { CMD_CLOSE
, "close", 1, "" },
86 { CMD_DEBUG
, "debug", 1, "on | off" },
87 { CMD_EJECT
, "eject", 1, "" },
88 { CMD_HELP
, "?", 1, 0 },
89 { CMD_HELP
, "help", 1, "" },
90 { CMD_INFO
, "info", 1, "" },
91 { CMD_NEXT
, "next", 1, "" },
92 { CMD_PAUSE
, "pause", 2, "" },
93 { CMD_PLAY
, "play", 1, "min1:sec1[.fram1] [min2:sec2[.fram2]]" },
94 { CMD_PLAY
, "play", 1, "track1[.index1] [track2[.index2]]" },
95 { CMD_PLAY
, "play", 1, "tr1 m1:s1[.f1] [[tr2] [m2:s2[.f2]]]" },
96 { CMD_PLAY
, "play", 1, "[#block [len]]" },
97 { CMD_PREVIOUS
, "previous", 2, "" },
98 { CMD_QUIT
, "quit", 1, "" },
99 { CMD_QUIT
, "exit", 1, "" },
100 { CMD_RESET
, "reset", 4, "" },
101 { CMD_RESUME
, "resume", 1, "" },
102 { CMD_SET
, "set", 2, "msf | lba" },
103 { CMD_STATUS
, "status", 1, "[audio | media | volume]" },
104 { CMD_STOP
, "stop", 3, "" },
105 { CMD_VOLUME
, "volume", 1,
106 "<l> <r> | left | right | mute | mono | stereo" },
107 { CMD_CDID
, "cdid", 2, "" },
108 { CMD_SPEED
, "speed", 2, "speed" },
112 struct cd_toc_entry toc_buffer
[100];
119 int setvol(int, int);
120 int read_toc_entrys(int);
121 int play_msf(int, int, int, int, int, int);
122 int play_track(int, int, int, int);
123 int get_vol(int *, int *);
124 int status(int *, int *, int *, int *);
126 int next_prev(char *arg
, int);
130 int pstatus(char *arg
);
132 void prtrack(struct cd_toc_entry
*e
, int lastflag
);
133 void lba2msf(unsigned long lba
,
134 u_char
*m
, u_char
*s
, u_char
*f
);
135 unsigned int msf2lba(u_char m
, u_char s
, u_char f
);
136 int play_blocks(int blk
, int len
);
137 int run(int cmd
, char *arg
);
138 char *parse(char *buf
, int *cmd
);
141 char *use_cdrom_instead(const char *);
142 const char *strstatus(int);
143 static u_int
dbprog_discid(void);
144 const char *cdcontrol_prompt(void);
154 for (c
=cmdtab
; c
->name
; ++c
) {
158 for (i
= c
->min
, s
= c
->name
; *s
; s
++, i
--) {
166 printf (" %s", c
->args
);
169 printf ("\n\tThe word \"play\" is not required for the play commands.\n");
170 printf ("\tThe plain target address is taken as a synonym for play.\n");
176 fprintf (stderr
, "usage: cdcontrol [-sv] [-f device] [command ...]\n");
181 use_cdrom_instead(const char *old_envvar
)
185 device
= getenv(old_envvar
);
187 warnx("%s environment variable deprecated, "
188 "please use CDROM in the future.", old_envvar
);
194 main(int argc
, char **argv
)
200 switch (getopt (argc
, argv
, "svhf:")) {
221 if (argc
> 0 && ! strcasecmp (*argv
, "help"))
225 cdname
= getenv("CDROM");
229 cdname
= use_cdrom_instead("MUSIC_CD");
231 cdname
= use_cdrom_instead("CD_DRIVE");
233 cdname
= use_cdrom_instead("DISC");
235 cdname
= use_cdrom_instead("CDPLAY");
238 cdname
= DEFAULT_CD_DRIVE
;
239 warnx("no CD device name specified, defaulting to %s", cdname
);
246 for (p
=buf
; argc
-->0; ++argv
) {
247 len
= strlen (*argv
);
249 if (p
+ len
>= buf
+ sizeof (buf
) - 1)
259 arg
= parse (buf
, &cmd
);
260 return (run (cmd
, arg
));
264 verbose
= isatty (0);
267 printf ("Compact Disc Control utility, version %s\n", VERSION
);
268 printf ("Type `?' for command list\n\n");
273 if (run (cmd
, arg
) < 0) {
284 run(int cmd
, char *arg
)
295 if (fd
< 0 && ! open_cd ())
301 if (fd
< 0 && ! open_cd ())
307 if (fd
< 0 && ! open_cd ())
310 return pstatus (arg
);
314 if (fd
< 0 && ! open_cd ())
317 while (isspace (*arg
))
320 return next_prev (arg
, cmd
);
323 if (fd
< 0 && ! open_cd ())
326 return ioctl (fd
, CDIOCPAUSE
);
329 if (fd
< 0 && ! open_cd ())
332 return ioctl (fd
, CDIOCRESUME
);
335 if (fd
< 0 && ! open_cd ())
338 rc
= ioctl (fd
, CDIOCSTOP
);
340 ioctl (fd
, CDIOCALLOW
);
345 if (fd
< 0 && ! open_cd ())
348 rc
= ioctl (fd
, CDIOCRESET
);
356 if (fd
< 0 && ! open_cd ())
359 if (! strcasecmp (arg
, "on"))
360 return ioctl (fd
, CDIOCSETDEBUG
);
362 if (! strcasecmp (arg
, "off"))
363 return ioctl (fd
, CDIOCCLRDEBUG
);
365 warnx("invalid command arguments");
370 if (fd
< 0 && ! open_cd ())
373 ioctl (fd
, CDIOCALLOW
);
374 rc
= ioctl (fd
, CDIOCEJECT
);
380 if (fd
< 0 && ! open_cd ())
383 ioctl (fd
, CDIOCALLOW
);
384 rc
= ioctl (fd
, CDIOCCLOSE
);
392 if (fd
< 0 && ! open_cd ())
395 while (isspace (*arg
))
401 if (! strcasecmp (arg
, "msf"))
403 else if (! strcasecmp (arg
, "lba"))
406 warnx("invalid command arguments");
410 if (fd
< 0 && !open_cd ())
413 if (! strncasecmp (arg
, "left", strlen(arg
)))
414 return ioctl (fd
, CDIOCSETLEFT
);
416 if (! strncasecmp (arg
, "right", strlen(arg
)))
417 return ioctl (fd
, CDIOCSETRIGHT
);
419 if (! strncasecmp (arg
, "mono", strlen(arg
)))
420 return ioctl (fd
, CDIOCSETMONO
);
422 if (! strncasecmp (arg
, "stereo", strlen(arg
)))
423 return ioctl (fd
, CDIOCSETSTERIO
);
425 if (! strncasecmp (arg
, "mute", strlen(arg
)))
426 return ioctl (fd
, CDIOCSETMUTE
);
428 if (2 != sscanf (arg
, "%d %d", &l
, &r
)) {
429 warnx("invalid command arguments");
433 return setvol (l
, r
);
436 if (fd
< 0 && ! open_cd ())
440 if (strcasecmp("max", arg
) == 0)
441 speed
= CDR_MAX_SPEED
;
443 speed
= strtol(arg
, NULL
, 10) * 177;
444 if (speed
<= 0 || speed
> INT_MAX
) {
445 warnx("invalid command arguments %s", arg
);
448 return ioctl(fd
, CDRIOCREADSPEED
, &speed
);
461 struct ioc_toc_header h
;
463 int rc
, start
, end
= 0, istart
= 1, iend
= 1;
465 rc
= ioctl (fd
, CDIOREADTOCHEADER
, &h
);
470 n
= h
.ending_track
- h
.starting_track
+ 1;
471 rc
= read_toc_entrys ((n
+ 1) * sizeof (struct cd_toc_entry
));
476 if (! arg
|| ! *arg
) {
477 /* Play the whole disc */
479 return play_blocks (0, msf2lba (toc_buffer
[n
].addr
.msf
.minute
,
480 toc_buffer
[n
].addr
.msf
.second
,
481 toc_buffer
[n
].addr
.msf
.frame
));
483 return play_blocks (0, ntohl(toc_buffer
[n
].addr
.lba
));
486 if (strchr (arg
, '#')) {
487 /* Play block #blk [ len ] */
490 if (2 != sscanf (arg
, "#%d%d", &blk
, &len
) &&
491 1 != sscanf (arg
, "#%d", &blk
))
496 len
= msf2lba (toc_buffer
[n
].addr
.msf
.minute
,
497 toc_buffer
[n
].addr
.msf
.second
,
498 toc_buffer
[n
].addr
.msf
.frame
) - blk
;
500 len
= ntohl(toc_buffer
[n
].addr
.lba
) - blk
;
502 return play_blocks (blk
, len
);
505 if (strchr (arg
, ':')) {
507 * Play MSF m1:s1 [ .f1 ] [ m2:s2 [ .f2 ] ]
509 * Will now also undestand timed addresses relative
510 * to the beginning of a track in the form...
512 * tr1 m1:s1[.f1] [[tr2] [m2:s2[.f2]]]
515 unsigned m1
, m2
, s1
, s2
, f1
, f2
;
516 unsigned char tm
, ts
, tf
;
518 tr2
= m2
= s2
= f2
= f1
= 0;
519 if (8 == sscanf (arg
, "%d %d:%d.%d %d %d:%d.%d",
520 &tr1
, &m1
, &s1
, &f1
, &tr2
, &m2
, &s2
, &f2
))
521 goto Play_Relative_Addresses
;
523 tr2
= m2
= s2
= f2
= f1
= 0;
524 if (7 == sscanf (arg
, "%d %d:%d %d %d:%d.%d",
525 &tr1
, &m1
, &s1
, &tr2
, &m2
, &s2
, &f2
))
526 goto Play_Relative_Addresses
;
528 tr2
= m2
= s2
= f2
= f1
= 0;
529 if (7 == sscanf (arg
, "%d %d:%d.%d %d %d:%d",
530 &tr1
, &m1
, &s1
, &f1
, &tr2
, &m2
, &s2
))
531 goto Play_Relative_Addresses
;
533 tr2
= m2
= s2
= f2
= f1
= 0;
534 if (7 == sscanf (arg
, "%d %d:%d.%d %d:%d.%d",
535 &tr1
, &m1
, &s1
, &f1
, &m2
, &s2
, &f2
))
536 goto Play_Relative_Addresses
;
538 tr2
= m2
= s2
= f2
= f1
= 0;
539 if (6 == sscanf (arg
, "%d %d:%d.%d %d:%d",
540 &tr1
, &m1
, &s1
, &f1
, &m2
, &s2
))
541 goto Play_Relative_Addresses
;
543 tr2
= m2
= s2
= f2
= f1
= 0;
544 if (6 == sscanf (arg
, "%d %d:%d %d:%d.%d",
545 &tr1
, &m1
, &s1
, &m2
, &s2
, &f2
))
546 goto Play_Relative_Addresses
;
548 tr2
= m2
= s2
= f2
= f1
= 0;
549 if (6 == sscanf (arg
, "%d %d:%d.%d %d %d",
550 &tr1
, &m1
, &s1
, &f1
, &tr2
, &m2
))
551 goto Play_Relative_Addresses
;
553 tr2
= m2
= s2
= f2
= f1
= 0;
554 if (5 == sscanf (arg
, "%d %d:%d %d:%d", &tr1
, &m1
, &s1
, &m2
, &s2
))
555 goto Play_Relative_Addresses
;
557 tr2
= m2
= s2
= f2
= f1
= 0;
558 if (5 == sscanf (arg
, "%d %d:%d %d %d",
559 &tr1
, &m1
, &s1
, &tr2
, &m2
))
560 goto Play_Relative_Addresses
;
562 tr2
= m2
= s2
= f2
= f1
= 0;
563 if (5 == sscanf (arg
, "%d %d:%d.%d %d",
564 &tr1
, &m1
, &s1
, &f1
, &tr2
))
565 goto Play_Relative_Addresses
;
567 tr2
= m2
= s2
= f2
= f1
= 0;
568 if (4 == sscanf (arg
, "%d %d:%d %d", &tr1
, &m1
, &s1
, &tr2
))
569 goto Play_Relative_Addresses
;
571 tr2
= m2
= s2
= f2
= f1
= 0;
572 if (4 == sscanf (arg
, "%d %d:%d.%d", &tr1
, &m1
, &s1
, &f1
))
573 goto Play_Relative_Addresses
;
575 tr2
= m2
= s2
= f2
= f1
= 0;
576 if (3 == sscanf (arg
, "%d %d:%d", &tr1
, &m1
, &s1
))
577 goto Play_Relative_Addresses
;
579 tr2
= m2
= s2
= f2
= f1
= 0;
580 goto Try_Absolute_Timed_Addresses
;
582 Play_Relative_Addresses
:
591 tm
= toc_buffer
[tr1
].addr
.msf
.minute
;
592 ts
= toc_buffer
[tr1
].addr
.msf
.second
;
593 tf
= toc_buffer
[tr1
].addr
.msf
.frame
;
595 lba2msf(ntohl(toc_buffer
[tr1
].addr
.lba
),
602 printf ("Track %d is not that long.\n", tr1
+ 1);
621 if (m2
|| s2
|| f2
) {
639 m2
= toc_buffer
[n
].addr
.msf
.minute
;
640 s2
= toc_buffer
[n
].addr
.msf
.second
;
641 f2
= toc_buffer
[n
].addr
.msf
.frame
;
643 lba2msf(ntohl(toc_buffer
[n
].addr
.lba
),
650 } else if (tr2
> n
) {
657 tm
= toc_buffer
[tr2
].addr
.msf
.minute
;
658 ts
= toc_buffer
[tr2
].addr
.msf
.second
;
659 tf
= toc_buffer
[tr2
].addr
.msf
.frame
;
661 lba2msf(ntohl(toc_buffer
[tr2
].addr
.lba
),
679 tm
= toc_buffer
[n
].addr
.msf
.minute
;
680 ts
= toc_buffer
[n
].addr
.msf
.second
;
681 tf
= toc_buffer
[n
].addr
.msf
.frame
;
683 lba2msf(ntohl(toc_buffer
[n
].addr
.lba
),
691 printf ("The playing time of the disc is not that long.\n");
694 return (play_msf (m1
, s1
, f1
, m2
, s2
, f2
));
696 Try_Absolute_Timed_Addresses
:
697 if (6 != sscanf (arg
, "%d:%d.%d%d:%d.%d",
698 &m1
, &s1
, &f1
, &m2
, &s2
, &f2
) &&
699 5 != sscanf (arg
, "%d:%d.%d%d:%d", &m1
, &s1
, &f1
, &m2
, &s2
) &&
700 5 != sscanf (arg
, "%d:%d%d:%d.%d", &m1
, &s1
, &m2
, &s2
, &f2
) &&
701 3 != sscanf (arg
, "%d:%d.%d", &m1
, &s1
, &f1
) &&
702 4 != sscanf (arg
, "%d:%d%d:%d", &m1
, &s1
, &m2
, &s2
) &&
703 2 != sscanf (arg
, "%d:%d", &m1
, &s1
))
708 m2
= toc_buffer
[n
].addr
.msf
.minute
;
709 s2
= toc_buffer
[n
].addr
.msf
.second
;
710 f2
= toc_buffer
[n
].addr
.msf
.frame
;
712 lba2msf(ntohl(toc_buffer
[n
].addr
.lba
),
719 return play_msf (m1
, s1
, f1
, m2
, s2
, f2
);
723 * Play track trk1 [ .idx1 ] [ trk2 [ .idx2 ] ]
725 if (4 != sscanf (arg
, "%d.%d%d.%d", &start
, &istart
, &end
, &iend
) &&
726 3 != sscanf (arg
, "%d.%d%d", &start
, &istart
, &end
) &&
727 3 != sscanf (arg
, "%d%d.%d", &start
, &end
, &iend
) &&
728 2 != sscanf (arg
, "%d.%d", &start
, &istart
) &&
729 2 != sscanf (arg
, "%d%d", &start
, &end
) &&
730 1 != sscanf (arg
, "%d", &start
))
735 return (play_track (start
, istart
, end
, iend
));
738 warnx("invalid command arguments");
743 next_prev(char *arg
, int cmd
)
745 struct ioc_toc_header h
;
746 int dir
, junk
, n
, off
, rc
, trk
;
748 dir
= (cmd
== CMD_NEXT
) ? 1 : -1;
749 rc
= ioctl (fd
, CDIOREADTOCHEADER
, &h
);
753 n
= h
.ending_track
- h
.starting_track
+ 1;
754 rc
= status (&trk
, &junk
, &junk
, &junk
);
759 if (sscanf (arg
, "%u", &off
) != 1) {
760 warnx("invalid command argument");
767 if (trk
> h
.ending_track
)
770 return (play_track (trk
, 1, n
, 1));
777 case ASTS_INVALID
: return ("invalid");
778 case ASTS_PLAYING
: return ("playing");
779 case ASTS_PAUSED
: return ("paused");
780 case ASTS_COMPLETED
: return ("completed");
781 case ASTS_ERROR
: return ("error");
782 case ASTS_VOID
: return ("void");
783 default: return ("??");
791 struct ioc_read_subchannel ss
;
792 struct cd_sub_channel_info data
;
793 int rc
, trk
, m
, s
, f
;
795 char *p
, vmcn
[(4 * 15) + 1];
797 while ((p
= strtok(arg
, " \t"))) {
799 if (!strncasecmp(p
, "audio", strlen(p
)))
800 what
|= STATUS_AUDIO
;
801 else if (!strncasecmp(p
, "media", strlen(p
)))
802 what
|= STATUS_MEDIA
;
803 else if (!strncasecmp(p
, "volume", strlen(p
)))
804 what
|= STATUS_VOLUME
;
806 warnx("invalid command arguments");
811 what
= STATUS_AUDIO
|STATUS_MEDIA
|STATUS_VOLUME
;
812 if (what
& STATUS_AUDIO
) {
813 rc
= status (&trk
, &m
, &s
, &f
);
816 printf ("Audio status = %d<%s>, current track = %d, current position = %d:%02d.%02d\n",
817 rc
, strstatus (rc
), trk
, m
, s
, f
);
819 printf ("%d %d %d:%02d.%02d\n", rc
, trk
, m
, s
, f
);
821 printf ("No current status info available\n");
823 if (what
& STATUS_MEDIA
) {
824 bzero (&ss
, sizeof (ss
));
826 ss
.data_len
= sizeof (data
);
827 ss
.address_format
= msf
? CD_MSF_FORMAT
: CD_LBA_FORMAT
;
828 ss
.data_format
= CD_MEDIA_CATALOG
;
829 rc
= ioctl (fd
, CDIOCREADSUBCHANNEL
, (char *) &ss
);
831 printf("Media catalog is %sactive",
832 ss
.data
->what
.media_catalog
.mc_valid
? "": "in");
833 if (ss
.data
->what
.media_catalog
.mc_valid
&&
834 ss
.data
->what
.media_catalog
.mc_number
[0])
836 strvisx (vmcn
, ss
.data
->what
.media_catalog
.mc_number
,
837 (sizeof (vmcn
) - 1) / 4, VIS_OCTAL
| VIS_NL
);
838 printf(", number \"%.*s\"", (int)sizeof (vmcn
), vmcn
);
842 printf("No media catalog info available\n");
844 if (what
& STATUS_VOLUME
) {
845 rc
= ioctl (fd
, CDIOCGETVOL
, &v
);
848 printf ("Left volume = %d, right volume = %d\n",
851 printf ("%d %d\n", v
.vol
[0], v
.vol
[1]);
853 printf ("No volume level info available\n");
860 * Convert an integer to its text string representation, and
861 * compute its checksum. Used by dbprog_discid to derive the
865 * n - The integer value.
868 * The integer checksum.
877 /* For backward compatibility this algorithm must not change */
878 sprintf(buf
, "%u", n
);
879 for (p
= buf
; *p
!= '\0'; p
++)
888 * Compute a magic disc ID based on the number of tracks,
889 * the length of each track, and a checksum of the string
890 * that represents the offset of each track.
893 * s - Pointer to the curstat_t structure.
896 * The integer disc ID.
901 struct ioc_toc_header h
;
907 rc
= ioctl (fd
, CDIOREADTOCHEADER
, &h
);
910 ntr
= h
.ending_track
- h
.starting_track
+ 1;
913 rc
= read_toc_entrys ((ntr
+ 1) * sizeof (struct cd_toc_entry
));
917 /* For backward compatibility this algorithm must not change */
918 for (i
= 0; i
< ntr
; i
++) {
919 #define TC_MM(a) toc_buffer[a].addr.msf.minute
920 #define TC_SS(a) toc_buffer[a].addr.msf.second
921 n
+= dbprog_sum((TC_MM(i
) * 60) + TC_SS(i
));
923 t
+= ((TC_MM(i
+1) * 60) + TC_SS(i
+1)) -
924 ((TC_MM(i
) * 60) + TC_SS(i
));
927 return((n
% 0xff) << 24 | t
<< 8 | ntr
);
935 id
= dbprog_discid();
940 printf ("%08x\n",id
);
946 info(char *arg __unused
)
948 struct ioc_toc_header h
;
951 rc
= ioctl (fd
, CDIOREADTOCHEADER
, &h
);
954 printf ("Starting track = %d, ending track = %d, TOC size = %d bytes\n",
955 h
.starting_track
, h
.ending_track
, h
.len
);
957 printf ("%d %d %d\n", h
.starting_track
,
958 h
.ending_track
, h
.len
);
960 warn("getting toc header");
964 n
= h
.ending_track
- h
.starting_track
+ 1;
965 rc
= read_toc_entrys ((n
+ 1) * sizeof (struct cd_toc_entry
));
970 printf ("track start duration block length type\n");
971 printf ("-------------------------------------------------\n");
974 for (i
= 0; i
< n
; i
++) {
975 printf ("%5d ", toc_buffer
[i
].track
);
976 prtrack (toc_buffer
+ i
, 0);
978 printf ("%5d ", toc_buffer
[n
].track
);
979 prtrack (toc_buffer
+ n
, 1);
984 lba2msf(unsigned long lba
, u_char
*m
, u_char
*s
, u_char
*f
)
986 lba
+= 150; /* block start offset */
987 lba
&= 0xffffff; /* negative lbas use only 24 bits */
988 *m
= lba
/ (60 * 75);
995 msf2lba(u_char m
, u_char s
, u_char f
)
997 return (((m
* 60) + s
) * 75 + f
) - 150;
1001 prtrack(struct cd_toc_entry
*e
, int lastflag
)
1003 int block
, next
, len
;
1007 /* Print track start */
1008 printf ("%2d:%02d.%02d ", e
->addr
.msf
.minute
,
1009 e
->addr
.msf
.second
, e
->addr
.msf
.frame
);
1011 block
= msf2lba (e
->addr
.msf
.minute
, e
->addr
.msf
.second
,
1014 block
= ntohl(e
->addr
.lba
);
1015 lba2msf(block
, &m
, &s
, &f
);
1016 /* Print track start */
1017 printf ("%2d:%02d.%02d ", m
, s
, f
);
1020 /* Last track -- print block */
1021 printf (" - %6d - -\n", block
);
1026 next
= msf2lba (e
[1].addr
.msf
.minute
, e
[1].addr
.msf
.second
,
1027 e
[1].addr
.msf
.frame
);
1029 next
= ntohl(e
[1].addr
.lba
);
1031 /* Take into account a start offset time. */
1032 lba2msf (len
- 150, &m
, &s
, &f
);
1034 /* Print duration, block, length, type */
1035 printf ("%2d:%02d.%02d %6d %6d %5s\n", m
, s
, f
, block
, len
,
1036 (e
->control
& 4) ? "data" : "audio");
1040 play_track(int tstart
, int istart
, int tend
, int iend
)
1042 struct ioc_play_track t
;
1044 t
.start_track
= tstart
;
1045 t
.start_index
= istart
;
1049 return ioctl (fd
, CDIOCPLAYTRACKS
, &t
);
1053 play_blocks(int blk
, int len
)
1055 struct ioc_play_blocks t
;
1060 return ioctl (fd
, CDIOCPLAYBLOCKS
, &t
);
1064 setvol(int left
, int right
)
1073 return ioctl (fd
, CDIOCSETVOL
, &v
);
1077 read_toc_entrys(int len
)
1079 struct ioc_read_toc_entry t
;
1081 t
.address_format
= msf
? CD_MSF_FORMAT
: CD_LBA_FORMAT
;
1082 t
.starting_track
= 0;
1084 t
.data
= toc_buffer
;
1086 return (ioctl (fd
, CDIOREADTOCENTRYS
, (char *) &t
));
1090 play_msf(int start_m
, int start_s
, int start_f
,
1091 int end_m
, int end_s
, int end_f
)
1093 struct ioc_play_msf a
;
1095 a
.start_m
= start_m
;
1096 a
.start_s
= start_s
;
1097 a
.start_f
= start_f
;
1102 return ioctl (fd
, CDIOCPLAYMSF
, (char *) &a
);
1106 status(int *trk
, int *min
, int *sec
, int *frame
)
1108 struct ioc_read_subchannel s
;
1109 struct cd_sub_channel_info data
;
1112 bzero (&s
, sizeof (s
));
1114 s
.data_len
= sizeof (data
);
1115 s
.address_format
= msf
? CD_MSF_FORMAT
: CD_LBA_FORMAT
;
1116 s
.data_format
= CD_CURRENT_POSITION
;
1118 if (ioctl (fd
, CDIOCREADSUBCHANNEL
, (char *) &s
) < 0)
1121 *trk
= s
.data
->what
.position
.track_number
;
1123 *min
= s
.data
->what
.position
.reladdr
.msf
.minute
;
1124 *sec
= s
.data
->what
.position
.reladdr
.msf
.second
;
1125 *frame
= s
.data
->what
.position
.reladdr
.msf
.frame
;
1127 lba2msf(ntohl(s
.data
->what
.position
.reladdr
.lba
),
1134 return s
.data
->header
.audio_status
;
1138 cdcontrol_prompt(void)
1140 return ("cdcontrol> ");
1147 static EditLine
*el
= NULL
;
1148 static History
*hist
= NULL
;
1149 static HistEvent he
;
1150 static char buf
[MAXLINE
];
1153 const char *bp
= NULL
;
1159 el
= el_init("cdcontrol", stdin
, stdout
, stderr
);
1160 hist
= history_init();
1161 history(hist
, &he
, H_SETSIZE
, 100);
1162 el_set(el
, EL_HIST
, history
, hist
);
1163 el_set(el
, EL_EDITOR
, "emacs");
1164 el_set(el
, EL_PROMPT
, cdcontrol_prompt
);
1165 el_set(el
, EL_SIGNAL
, 1);
1166 el_source(el
, NULL
);
1168 if ((bp
= el_gets(el
, &num
)) == NULL
|| num
== 0) {
1170 fprintf (stderr
, "\r\n");
1174 len
= (num
> MAXLINE
) ? MAXLINE
: num
;
1175 memcpy(buf
, bp
, len
);
1177 history(hist
, &he
, H_ENTER
, bp
);
1181 if (! fgets (buf
, sizeof (buf
), stdin
)) {
1183 fprintf (stderr
, "\r\n");
1187 p
= parse (buf
, cmd
);
1193 parse(char *buf
, int *cmd
)
1199 for (p
=buf
; isspace (*p
); p
++)
1202 if (isdigit (*p
) || (p
[0] == '#' && isdigit (p
[1]))) {
1205 } else if (*p
== '+') {
1208 } else if (*p
== '-') {
1209 *cmd
= CMD_PREVIOUS
;
1213 for (buf
= p
; *p
&& ! isspace (*p
); p
++)
1220 if (*p
) { /* It must be a spacing character! */
1224 for (q
=p
; *q
&& *q
!= '\n' && *q
!= '\r'; q
++)
1230 for (c
=cmdtab
; c
->name
; ++c
) {
1231 /* Is it an exact match? */
1232 if (! strcasecmp (buf
, c
->name
)) {
1237 /* Try short hand forms then... */
1238 if (len
>= c
->min
&& ! strncasecmp (buf
, c
->name
, len
)) {
1239 if (*cmd
!= -1 && *cmd
!= c
->command
) {
1240 warnx("ambiguous command");
1248 warnx("invalid command, enter ``help'' for commands");
1252 while (isspace (*p
))
1260 char devbuf
[MAXPATHLEN
];
1265 if (*cdname
== '/') {
1266 snprintf (devbuf
, MAXPATHLEN
, "%s", cdname
);
1268 snprintf (devbuf
, MAXPATHLEN
, "%s%s", _PATH_DEV
, cdname
);
1271 fd
= open (devbuf
, O_RDONLY
);
1273 if (fd
< 0 && errno
== ENOENT
) {
1274 strcat (devbuf
, DEFAULT_CD_PARTITION
);
1275 fd
= open (devbuf
, O_RDONLY
);
1279 if (errno
== ENXIO
) {
1280 /* ENXIO has an overloaded meaning here.
1281 * The original "Device not configured" should
1282 * be interpreted as "No disc in drive %s". */
1283 warnx("no disc in drive %s", devbuf
);
1286 err(1, "%s", devbuf
);