1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
/*
* Module: cdctrl.c
* Purpose: to control Linux CD-ROMs
* Author: Wade Hampton
*
* Copyright (C) 1997, 1998, Wade Hampton
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 dated June, 1991.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <getopt.h>
#include "config.h"
#include "cdtool.h"
#include "commands.h"
#include "hardware.h"
#include "info.h"
#include "shuffle.h"
#include "util.h"
#define CD_DEVICE "/dev/cdrom"
/* CONSTANTS- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static char svnid[] = "$Id: cdctrl.c 1533 2005-10-17 19:11:59Z max $";
/* LOCAL DATA - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
char caLineIn[BUFSIZ];
/* PROTOTYPES - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
void print_userhelp(int iVersion);
void print_help(void);
void print_info(cdtool_t *cdtool, char *pszCmd);
int cmd_parse (cdtool_t *cdtool, char *pszBuf, int iRead);
/************************************************************************/
/* Procedure: print_userhelp
* Purpose: to print command and user help on commands
*
* Inputs: none
* Outputs: to stdout, to stderr
* Returns: none
* Notes:
* 1.
*/
/************************************************************************/
void print_userhelp(int iVersion)
{
printf ("cdctrl:\nCommand line CDROM control\nOptions:\n");
printf (" cdctrl -c device -- output using CRLF\n");
printf (" cdctrl device -- normal output\n");
printf (" cdctrl [-h|-?|-V] -- help/version\n");
if (iVersion == TRUE) {
printf (" Version %s\n", svnid);
} else {
printf ("Command-line control options are:\n");
print_help ();
}
}
/************************************************************************/
/* Procedure: print_help
* Purpose: to print help on commands
*
* Inputs: none
* Outputs: to stdout, to stderr
* Returns: none
* Notes:
* 1.
*/
/************************************************************************/
void print_help ()
{
infomsg("1 play (start play at track 1)");
infomsg("s stop");
infomsg("p pause");
infomsg("r resume");
infomsg("e eject");
infomsg("c close");
infomsg("i info");
infomsg("d dir");
infomsg("l shuffle (1 track only)");
infomsg("- previous track");
infomsg("+ next track");
infomsg("N play track N");
infomsg("q quit");
infomsg("? help");
infomsg("info format: \"CMD cmd-name cd-status track abs-time rel-time\"");
infomsg(" cmd-name := {play, stop, ... quit} from above list");
infomsg(" cd-status := {invalid, play, paused, completed, error, no status}");
infomsg(" track := {1..99} CD track");
infomsg(" abs-time := HH:MM:SS elapsed since CD start");
infomsg(" rel-time := HH:MM:SS elapsed since track start");
}
/************************************************************************/
/* Procedure: print_info
* Purpose: to print info on status
*
* Inputs: command, device of CDROM
* Outputs: to stdout, to stderr
* Returns: none
* Notes:
* 1.
*/
/************************************************************************/
void print_info(cdtool_t *cdtool, char *pszCmd)
{
printf ("CMD %s ", pszCmd);
do_info (cdtool, I_DISP_ALL);
}
/************************************************************************/
/* Procedure: cmd_parse
* Purpose: to parse user's commands
*
* Inputs: program name, file description for CDROM, buffer,
* count, device of CDROM
* Outputs: to CDROM, to stdout, to stderr
* Returns: T/F, T when "done"
* Notes:
* 1.
*/
/************************************************************************/
int cmd_parse (cdtool_t *cdtool, char *pszBuf, int iRead)
{
int iDone = FALSE;
int iTrk;
char *pszCmd;
pszCmd = "NULL";
if ((pszBuf[0] >= '0') && (pszBuf[0] <= '9')) {
iTrk = atoi (pszBuf);
do_play(cdtool, iTrk, 0);
pszCmd = "play";
} else {
/* handle commands.... */
switch (pszBuf[0]) {
case 's':
do_stop(cdtool, 99);
pszCmd = "stop";
break;
case 'p':
do_pause(cdtool);
pszCmd = "pause";
break;
case 'r':
do_play(cdtool, 0, 0);
pszCmd = "resume";
break;
case 'e':
do_eject(cdtool);
sleep (3);
pszCmd = "eject";
break;
case 'c':
do_close(cdtool);
sleep (3);
pszCmd = "close";
break;
case 'i':
case 0x0d:
case 0x0a:
pszCmd = "info";
break;
case 'd':
do_dir (cdtool, P_RAW, 0);
pszCmd = "dir";
break;
case '-':
do_skip(cdtool, SKIPBACK, 1);
pszCmd = "previous";
break;
case '+':
do_skip(cdtool, SKIPFORWARD, 1);
pszCmd = "next";
break;
case 'q':
iDone = TRUE;
pszCmd = "QUIT";
break;
case 'l':
do_shuffle (cdtool, 99, 1, 1);
pszCmd = "shuffle";
break;
case '?':
print_help();
pszCmd = "help";
break;
default:
errormsg("ERROR invalid command %s", pszBuf);
pszCmd = "default";
break;
}
}
print_info(cdtool, pszCmd);
infomsg ("END");
return (iDone);
}
/************************************************************************/
/* Procedure: main
* Purpose: cdctrl's main
*
* Inputs: command line args
* Outputs: to CDROM, to stdout, to stderr
* Returns: T/F, T when "done"
* Notes:
* 1.
*/
/************************************************************************/
int main (int argc, char *argv[])
{
cdtool_t cdtool;
char *device = NULL;
int opt=1;
int iRead;
int iDone = FALSE;
setprogname("cdctrl");
cdtool.device = NULL;
cdtool_show_crlf = DOLF;
cdtool_show_debug = FALSE;
cdtool_show_verbose = FALSE;
/* parse environment and command line args */
while (argv[opt] != NULL && argv[opt][0] == '-') {
switch (argv[opt][1]) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
device = strdup(CD_DEVICE "?");
*(device + sizeof(CD_DEVICE)-1) = argv[opt][1] - '0';
argv++; argc--;
break;
case 'd':
if (argv[opt+1] == NULL) {
errormsg("%P: -d needs an argument");
exit(1);
}
device = argv[opt+1];
argv += 2; argc -= 2;
break;
case 'D':
cdtool_show_debug = TRUE;
argv++; argc--;
break;
case 'v':
cdtool_show_verbose = TRUE;
argv++; argc--;
break;
case 'c':
cdtool_show_crlf = DOCRLF;
argv++; argc--;
break;
case 'h':
case 'V':
case '?':
print_userhelp(FALSE);
exit(0);
default:
errormsg("%P: unknown option: %s", argv[opt]);
exit(1);
}
}
if (argc > 1)
device = argv[opt];
/* close stderr and redirect to stdout */
close(2);
dup2(1, 2);
/* try to open the CD device(s) */
if (cdtool_open(&cdtool, device) < 0)
exit(1);
infomsg("Opened %s as device %d", cdtool.device, cdtool.fd);
/* process commands */
while (iDone == FALSE) {
if (update_hw(cdtool.hw, cdtool.fd) < 0) {
errormsg("Error reading from hardware\n");
break;
}
if (fgets(caLineIn, sizeof(caLineIn)-1, stdin) != NULL) {
iRead = strlen(caLineIn);
if (iRead > 0) {
iDone = cmd_parse(&cdtool, caLineIn, iRead);
}
}
fflush (stdout);
}
/* cleanup */
close (cdtool.fd);
infomsg("EXITING");
return (TRUE);
}
/*
* Revision History: (no longer updated)
*
* Language/Compiler/O/S: GCC 2.7.2, Linux 2.0.X
* Date: 12/22/97
*
* [from RCS]
*
* $Log: cdctrl.c,v $
* Revision 1.6 1998/07/15 13:49:58 wadeh
* prior to Lin's changes
*
* Revision 1.5 1998/01/06 21:00:33 wadeh
* Added DoCr, changes to fix cdeject crash.
*
* Revision 1.4 1997/12/30 22:59:31 wadeh
* Added fflush to properly work with pipes...
*
* Revision 1.3 1997/12/24 15:39:10 wadeh
* Minor bug fixes:
* 1) Fixed cdctrl eject command causing a signal 11.
* 2) Fixed Makefile debug option to not strip symbols, can now debug.
* 3) Added command.h and more prototypes, more documentation.
* 4) Fixed read_hw to always return buffer, even on error.
* ,
*
* Revision 1.2 1997/12/23 22:52:51 wadeh
* 2.1 beta 1 release
*
* Revision 1.1 1997/12/23 21:38:52 wadeh
* Initial revision
*
*
* Notes:
* 1)
*/
|