Break out common standalone routines from cd-info and cd-read.

This commit is contained in:
rocky
2003-09-21 04:21:39 +00:00
parent 11d4eb000c
commit d3909991ab
5 changed files with 221 additions and 254 deletions

View File

@@ -1,4 +1,4 @@
# $Id: Makefile.am,v 1.15 2003/09/17 12:13:07 rocky Exp $
# $Id: Makefile.am,v 1.16 2003/09/21 04:21:39 rocky Exp $
#
# Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
#
@@ -22,10 +22,10 @@
CDDB_LIBS=@CDDB_LIBS@
if BUILD_CDINFO
cd_info_SOURCES = cd-info.c
cd_info_SOURCES = cd-info.c util.c util.h
cd_info_LDADD = $(LIBISO9660_LIBS) $(LIBCDIO_LIBS) $(LIBPOPT_LIBS) $(CDDB_LIBS) $(VCDINFO_LIBS)
cd_read_SOURCES = cd-read.c
cd_read_SOURCES = cd-read.c util.c util.h
cd_read_LDADD = $(LIBCDIO_LIBS) $(LIBPOPT_LIBS)
if BUILD_CDINFO_LINUX

View File

@@ -1,5 +1,5 @@
/*
$Id: cd-info.c,v 1.35 2003/09/21 03:35:40 rocky Exp $
$Id: cd-info.c,v 1.36 2003/09/21 04:21:39 rocky Exp $
Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
Copyright (C) 1996,1997,1998 Gerd Knorr <kraxel@bytesex.org>
@@ -24,21 +24,11 @@
the CD.
*/
#include "util.h"
#define PROGRAM_NAME "CD Info"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <popt.h>
/* Accomodate to older popt that doesn't support the "optional" flag */
#ifndef POPT_ARGFLAG_OPTIONAL
#define POPT_ARGFLAG_OPTIONAL 0
#endif
#include "config.h"
#ifdef HAVE_CDDB
#include <cddb/cddb.h>
#endif
@@ -49,8 +39,6 @@
#include <libvcd/info.h>
#endif
#include <cdio/cdio.h>
#include <cdio/logging.h>
#include <cdio/util.h>
#include <cdio/cd_types.h>
#include <cdio/iso9660.h>
@@ -71,41 +59,6 @@
#include <errno.h>
#ifdef ENABLE_NLS
#include <locale.h>
# include <libintl.h>
# define _(String) dgettext ("cdinfo", String)
#else
/* Stubs that do something close enough. */
# define _(String) (String)
#endif
/* The following test is to work around the gross typo in
systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
is defined to 0, not 1. */
#if !EXIT_FAILURE
# undef EXIT_FAILURE
# define EXIT_FAILURE 1
#endif
#ifndef EXIT_SUCCESS
# define EXIT_SUCCESS 0
#endif
#define DEBUG 1
#if DEBUG
#define dbg_print(level, s, args...) \
if (opts.debug_level >= level) \
fprintf(stderr, "%s: "s, __func__ , ##args)
#else
#define dbg_print(level, s, args...)
#endif
#define err_exit(fmt, args...) \
fprintf(stderr, "%s: "fmt, program_name, ##args); \
myexit(cdio, EXIT_FAILURE)
#if 0
#define STRONG "\033[1m"
#define NORMAL "\033[0m"
@@ -120,22 +73,9 @@ struct cdrom_multisession ms;
struct cdrom_subchnl sub;
#endif
char *source_name = NULL;
char *program_name;
const char *argp_program_version = PROGRAM_NAME " " VERSION;
const char *argp_program_bug_address = "rocky@panix.com";
typedef enum
{
IMAGE_AUTO,
IMAGE_DEVICE,
IMAGE_BIN,
IMAGE_CUE,
IMAGE_NRG,
IMAGE_UNKNOWN
} source_image_t;
/* Used by `main' to communicate with `parse_opt'. And global options
*/
struct arguments
@@ -188,25 +128,6 @@ enum {
char *temp_str;
#define DEV_PREFIX "/dev/"
static char *
fillout_device_name(const char *device_name)
{
#if defined(HAVE_WIN32_CDROM)
return strdup(device_name);
#else
unsigned int prefix_len=strlen(DEV_PREFIX);
if (0 == strncmp(device_name, DEV_PREFIX, prefix_len))
return strdup(device_name);
else {
char *full_device_name=malloc(strlen(device_name)+prefix_len);
sprintf(full_device_name, DEV_PREFIX "%s", device_name);
return full_device_name;
}
#endif
}
/* Parse a all options. */
static bool
parse_options (int argc, const char *argv[])
@@ -360,46 +281,6 @@ parse_options (int argc, const char *argv[])
return true;
}
static void
print_version (bool version_only)
{
driver_id_t driver_id;
if (!opts.no_header)
printf( _("CD Info %s (c) 2003 Gerd Knorr, Heiko Ei<45>feldt & R. Bernstein\n"),
VERSION);
printf( _("This is free software; see the source for copying conditions.\n\
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
PARTICULAR PURPOSE.\n\
"));
if (version_only) {
char *default_device;
for (driver_id=DRIVER_UNKNOWN+1; driver_id<=CDIO_MAX_DRIVER; driver_id++) {
if (cdio_have_driver(driver_id)) {
printf("Have driver: %s\n", cdio_driver_describe(driver_id));
}
}
default_device=cdio_get_default_device(NULL);
if (default_device)
printf("Default CD-ROM device: %s\n", default_device);
else
printf("No CD-ROM device found.\n");
exit(100);
}
}
static void
myexit(CdIo *cdio, int rc)
{
if (NULL != cdio)
cdio_destroy(cdio);
exit(rc);
}
/* ------------------------------------------------------------------------ */
/* CDDB */
@@ -871,7 +752,7 @@ main(int argc, const char *argv[])
be reflected in `arguments'. */
parse_options(argc, argv);
print_version(opts.version_only);
print_version(program_name, VERSION, opts.no_header, opts.version_only);
switch (opts.source_image) {
case IMAGE_UNKNOWN:

View File

@@ -1,5 +1,5 @@
/*
$Id: cd-read.c,v 1.4 2003/09/21 03:35:40 rocky Exp $
$Id: cd-read.c,v 1.5 2003/09/21 04:21:39 rocky Exp $
Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
@@ -20,65 +20,7 @@
/* Program to debug read routines audio, mode1, mode2 forms 1 & 2. */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <sys/types.h>
#include <cdio/cdio.h>
#include <cdio/logging.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <ctype.h>
#include <popt.h>
/* Accomodate to older popt that doesn't support the "optional" flag */
#ifndef POPT_ARGFLAG_OPTIONAL
#define POPT_ARGFLAG_OPTIONAL 0
#endif
#ifdef ENABLE_NLS
#include <locale.h>
# include <libintl.h>
# define _(String) dgettext ("cdinfo", String)
#else
/* Stubs that do something close enough. */
# define _(String) (String)
#endif
/* The following test is to work around the gross typo in
systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
is defined to 0, not 1. */
#if !EXIT_FAILURE
# undef EXIT_FAILURE
# define EXIT_FAILURE 1
#endif
#ifndef EXIT_SUCCESS
# define EXIT_SUCCESS 0
#endif
#define err_exit(fmt, args...) \
fprintf(stderr, "%s: "fmt, program_name, ##args); \
myexit(cdio, EXIT_FAILURE)
char *source_name = NULL;
char *program_name;
typedef enum
{
IMAGE_AUTO,
IMAGE_DEVICE,
IMAGE_BIN,
IMAGE_CUE,
IMAGE_NRG,
IMAGE_UNKNOWN
} source_image_t;
#include "util.h"
/* Configuration option codes */
enum {
@@ -143,34 +85,6 @@ struct arguments
lsn_t start_lsn;
} opts;
/* Comparison function called by bearch() to find sub-option record. */
static int
compare_subopts(const void *key1, const void *key2)
{
subopt_entry_t *a = (subopt_entry_t *) key1;
subopt_entry_t *b = (subopt_entry_t *) key2;
return (strncmp(a->name, b->name, 30));
}
/* CDIO logging routines */
static cdio_log_handler_t gl_default_cdio_log_handler = NULL;
static void
_log_handler (cdio_log_level_t level, const char message[])
{
if (level == CDIO_LOG_DEBUG && opts.debug_level < 3)
return;
if (level == CDIO_LOG_INFO && opts.debug_level < 2)
return;
if (level == CDIO_LOG_WARN && opts.debug_level < 1)
return;
gl_default_cdio_log_handler (level, message);
}
static void
hexdump (uint8_t * buffer, unsigned int len)
{
@@ -194,6 +108,15 @@ hexdump (uint8_t * buffer, unsigned int len)
printf ("\n");
}
/* Comparison function called by bearch() to find sub-option record. */
static int
compare_subopts(const void *key1, const void *key2)
{
subopt_entry_t *a = (subopt_entry_t *) key1;
subopt_entry_t *b = (subopt_entry_t *) key2;
return (strncmp(a->name, b->name, 30));
}
/* Do processing of a --mode sub option.
Basically we find the option in the array, set it's corresponding
flag variable to true as well as the "show.all" false.
@@ -227,39 +150,6 @@ process_suboption(const char *subopt, subopt_entry_t *sublist, const int num,
}
}
#define DEV_PREFIX "/dev/"
static char *
fillout_device_name(const char *device_name)
{
#if defined(HAVE_WIN32_CDROM)
return strdup(device_name);
#else
unsigned int prefix_len=strlen(DEV_PREFIX);
if (0 == strncmp(device_name, DEV_PREFIX, prefix_len))
return strdup(device_name);
else {
char *full_device_name=malloc(strlen(device_name)+prefix_len);
sprintf(full_device_name, DEV_PREFIX "%s", device_name);
return full_device_name;
}
#endif
}
static void
print_version (void)
{
if (!opts.no_header)
printf( _("cd-read %s (c) 2003 R. Bernstein\n"),
VERSION);
printf( _("This is free software; see the source for copying conditions.\n\
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
PARTICULAR PURPOSE.\n\
"));
exit(100);
}
/* Parse a options. */
static bool
parse_options (int argc, const char *argv[])
@@ -350,7 +240,7 @@ parse_options (int argc, const char *argv[])
"--mode");
break;
case OP_VERSION:
print_version();
print_version(program_name, VERSION, 0, true);
exit (EXIT_SUCCESS);
break;
@@ -410,13 +300,21 @@ parse_options (int argc, const char *argv[])
}
static void
myexit(CdIo *cdio, int rc)
log_handler (cdio_log_level_t level, const char message[])
{
if (NULL != cdio)
cdio_destroy(cdio);
exit(rc);
if (level == CDIO_LOG_DEBUG && opts.debug_level < 2)
return;
if (level == CDIO_LOG_INFO && opts.debug_level < 1)
return;
if (level == CDIO_LOG_WARN && opts.debug_level < 0)
return;
gl_default_cdio_log_handler (level, message);
}
int
main(int argc, const char *argv[])
{
@@ -432,7 +330,7 @@ main(int argc, const char *argv[])
opts.read_mode = READ_MODE_UNINIT;
opts.source_image = IMAGE_UNKNOWN;
gl_default_cdio_log_handler = cdio_log_set_handler (_log_handler);
gl_default_cdio_log_handler = cdio_log_set_handler (log_handler);
/* Parse our arguments; every option seen by `parse_opt' will
be reflected in `arguments'. */

86
src/util.c Normal file
View File

@@ -0,0 +1,86 @@
/*
$Id: util.c,v 1.1 2003/09/21 04:21:39 rocky Exp $
Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
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; either version 2 of the License, or
(at your option) any later version.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* Miscellaneous things common to standalone programs. */
#include "util.h"
cdio_log_handler_t gl_default_cdio_log_handler = NULL;
char *source_name = NULL;
char *program_name;
void
myexit(CdIo *cdio, int rc)
{
if (NULL != cdio)
cdio_destroy(cdio);
exit(rc);
}
void
print_version (const char *program_name, const char *version,
int no_header, bool version_only)
{
driver_id_t driver_id;
if (no_header == 0)
printf( _("%s %s (c) 2003 R. Bernstein\n"),
program_name, version);
printf( _("This is free software; see the source for copying conditions.\n\
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
PARTICULAR PURPOSE.\n\
"));
if (version_only) {
char *default_device;
for (driver_id=DRIVER_UNKNOWN+1; driver_id<=CDIO_MAX_DRIVER; driver_id++) {
if (cdio_have_driver(driver_id)) {
printf("Have driver: %s\n", cdio_driver_describe(driver_id));
}
}
default_device=cdio_get_default_device(NULL);
if (default_device)
printf("Default CD-ROM device: %s\n", default_device);
else
printf("No CD-ROM device found.\n");
exit(100);
}
}
#define DEV_PREFIX "/dev/"
char *
fillout_device_name(const char *device_name)
{
#if defined(HAVE_WIN32_CDROM)
return strdup(device_name);
#else
unsigned int prefix_len=strlen(DEV_PREFIX);
if (0 == strncmp(device_name, DEV_PREFIX, prefix_len))
return strdup(device_name);
else {
char *full_device_name=malloc(strlen(device_name)+prefix_len);
sprintf(full_device_name, DEV_PREFIX "%s", device_name);
return full_device_name;
}
#endif
}

102
src/util.h Normal file
View File

@@ -0,0 +1,102 @@
/*
$Id: util.h,v 1.1 2003/09/21 04:21:39 rocky Exp $
Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
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; either version 2 of the License, or
(at your option) any later version.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* Miscellaneous things common to standalone programs. */
#ifndef UTIL_H
#define UTIL_H
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <sys/types.h>
#include <cdio/cdio.h>
#include <cdio/logging.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <ctype.h>
#include <popt.h>
/* Accomodate to older popt that doesn't support the "optional" flag */
#ifndef POPT_ARGFLAG_OPTIONAL
#define POPT_ARGFLAG_OPTIONAL 0
#endif
#ifdef ENABLE_NLS
#include <locale.h>
# include <libintl.h>
# define _(String) dgettext ("cdinfo", String)
#else
/* Stubs that do something close enough. */
# define _(String) (String)
#endif
/* The following test is to work around the gross typo in
systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
is defined to 0, not 1. */
#if !EXIT_FAILURE
# undef EXIT_FAILURE
# define EXIT_FAILURE 1
#endif
#ifndef EXIT_SUCCESS
# define EXIT_SUCCESS 0
#endif
#define DEBUG 1
#if DEBUG
#define dbg_print(level, s, args...) \
if (opts.debug_level >= level) \
fprintf(stderr, "%s: "s, __func__ , ##args)
#else
#define dbg_print(level, s, args...)
#endif
#define err_exit(fmt, args...) \
fprintf(stderr, "%s: "fmt, program_name, ##args); \
myexit(cdio, EXIT_FAILURE)
typedef enum
{
IMAGE_AUTO,
IMAGE_DEVICE,
IMAGE_BIN,
IMAGE_CUE,
IMAGE_NRG,
IMAGE_UNKNOWN
} source_image_t;
extern char *source_name;
extern char *program_name;
extern cdio_log_handler_t gl_default_cdio_log_handler;
void myexit(CdIo *cdio, int rc);
void print_version (const char *program_name, const char *version,
int no_header, bool version_only);
char *fillout_device_name(const char *device_name);
#endif /* UTIL_H */