File: cdcd.c

package info (click to toggle)
cdcd 0.6.3-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 908 kB
  • ctags: 445
  • sloc: ansic: 4,797; sh: 2,607; makefile: 52
file content (271 lines) | stat: -rw-r--r-- 6,046 bytes parent folder | download
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
/*
cdcd - Command Driven CD player
Copyright (C) 1998, 1999 Tony Arcieri
Copyright (C) 2001 Fabrice BAUZAC

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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*

  TODO

  Contact a Bash/Readline programmer and report the history_tokenize()
  bug: it returns NULL (should return a pointer to NULL) when it's got
  an empty line.
  
  Set the `char history_comment_char' variable to `#' (?).

  Set history_quotes_inhibit_expansion to 1 (?).

  Take more time to classify functions in files.

*/

#include <stdio.h>
#include <signal.h>
#include <locale.h>
#include <time.h>

#include <mycdaudio.h>

#ifdef HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif

#include <errno.h>

/* Reminder: we have to make sure that the getopt files are
   up-to-date.  */
#include "getopt.h"

#include "rlhist.h"
#include "cdcd.h"
#include "conf.h"
#include "cmd_sites.h"
#include "cmd_cdcd.h"
#include "cmd_access.h"
#include "cmd_edit.h"
#include "str.h"
#include "cmdline.h"
#include "interface.h"
#include "global.h"

const char *cdcdrc_verbose_var = "verbose";
const char *cdcdrc_device_var = "device";
const char *cdcd_cddb_host = "freedb.freedb.org";
const char *cdcd_cdindex_host = "www.cdindex.org";
const char *author_email = "noon@users.sourceforge.net";


/* Generic functions.  */

static void
bug ()
{
  fprintf (stderr, "Please submit a bug report to <%s>.\n",
	   author_email);
  exit (1);
}

void
memory_exhausted ()
{
  fputs ("Virtual memory exhausted\n", stderr);
  exit(1);
}

void *
xmalloc (size_t size)
{
  void *value = malloc (size);
  if (!value)
    memory_exhausted ();
  return value;
}

void *
xrealloc (void *ptr, size_t size)
{
  void *value = realloc (ptr, size);
  if (!value)
    memory_exhausted ();
  return value;
}

void
xfree (void *ptr)
{
  free (ptr);
}


/* The more I look at this function, the more I feel like it's
   useless.  I wonder...  -- fb.  */
RETSIGTYPE
signal_handler (int signal)
{
  static int recursive_segv = 0;
  
  switch (signal) {
  case SIGSEGV:
    /* If there's a bug below which causes a segmentation violation,
       this will prevent the infinite recursion.  */
    if (recursive_segv++)
      exit (1);

    printf ("\n"
	    "SIGSEGV caught: Segmentation violation\n"
	    "Please report what you were doing when this occurred to:\n"
	    "%s", author_email);
    putchar ('\n');
    exit (1);
  case SIGTERM:
    putchar ('\n');
    exit (0);
  }
}

void
print_version ()
{
  char dispbuffer[4096];
  cd_version (dispbuffer, 4095);
  dispbuffer[4095] = 0;  /* I'm not sure whether this is done in
                            cd_version() or not, so it's done here.  */
  printf ("cdcd %s\n"
	  "Copyright (C) 1998, 1999 Tony Arcieri.\n"
	  "Copyright (C) 2001 Fabrice Bauzac.\n",
	  VERSION);
  pprintf (get_width() - 1,
	   "cdcd is free software, covered by the GNU General "
	   "Public Licence, and you are welcome to change it "
	   "and/or distribute copies of it under certain conditions.  "
	   "There is absolutely no warranty for cdcd.  See the "
	   "file COPYING for details.");
  putchar ('\n');
  printf ("Using %s.\n", dispbuffer);
}

void
print_cmd_line_help ()
{
  puts ("Usage: cdcd [options] [commands]");
  fputs ("Options:\n"
	 "  -h, --help           display this help and exit\n"
	 "  -v, --version        output version information and exit\n"
	 "  -d, --device=DEVICE  use DEVICE instead of /dev/cdrom\n",
	 stdout);
  print_all_commands (cmds, 1);
}

int
main (int argc, char **argv)
{
  signal (SIGSEGV, signal_handler);
  signal (SIGTERM, signal_handler);
  srand (time (0));
  setlocale (LC_ALL, "");

  init_cmd_cdcd ();
  init_cmd_sites ();
  init_cmd_access ();
  init_cmd_edit ();

  using_history ();
   
  rl_readline_name = PACKAGE;
  rl_completer_quote_characters = "\"'`";
  rl_basic_word_break_characters = " ";

  /* What is this for?  Seems to be unused, because it doesn't crash
     even if we set it to NULL!  -- fb.  */
  rl_basic_quote_characters = "\"'`";

  {
    struct cdcdrc conf;
    cdcdrc_read (&conf);
    verbosity = conf.verbosity;
    strncpy (device, conf.device, CDCDRC_DEVICE_LEN - 1);
    device[CDCDRC_DEVICE_LEN - 1] = 0;
  }

  {
    int opt;
    struct option o[] = {
      { "help",    no_argument,       NULL, 'h' },
      { "version", no_argument,       NULL, 'v' },
      { "device",  required_argument, NULL, 'd' },
      { NULL }
    };

    while ((opt = getopt_long (argc, argv, "hvd:", o, NULL)) != -1)
      {
	switch (opt)
	  {
	  case 'd':
	    strncpy (device, optarg, CDCDRC_DEVICE_LEN - 1);
	    device[CDCDRC_DEVICE_LEN - 1] = 0;
	    break;
	  case 'v':
	    print_version ();
	    return 0;
	  case 'h':
	    print_cmd_line_help ();
	    return 0;
	  case '?':
	    exit (1);
	  default:
	    fprintf (stderr, "%s:%d: bogus argument parsing\n",
		     __FILE__, __LINE__);
	    bug ();
	  }
      }
  }
   
  if((cd_desc = cd_init_device (device)) < 0)
    {
      if (errno == EBUSY)
	{
	  perror ("cdcd");
	  printf ("cdcd: %s appears to be already mounted.\n", device);
	}
      else
	{
	  fputs ("cdcd: ", stderr);
	  perror (device);
	}
      exit (1);
    }

  if (optind != argc)
    {
      char **p;
      p = cv2v0 (argc - optind, argv + optind);
      cmd_cdcd_execute (p);
      freev0 (p);
      exit (0);
    }
  else
    {
      print_version ();
      puts ("Enter ? for help.");
      cmd_cdcd_mainloop ();
      cd_finish (cd_desc);
    }
  return 0;
}