File: cmd_sites.c

package info (click to toggle)
cdcd 0.6.6-13.3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,768 kB
  • sloc: ansic: 4,891; sh: 2,142; makefile: 17
file content (343 lines) | stat: -rw-r--r-- 7,603 bytes parent folder | download | duplicates (7)
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
/*
cdcd - Command Driven CD player
Copyright (C) 1998-99 Tony Arcieri
Copyright (C) 2001, 2002, 2003 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.
*/

/*

  IMPORTANT THINGS TO FIX:

  - Look at cmd_sites_add;

  - Review cmd_sites_refresh;

*/

#include <stdio.h>

#include "cmd_sites.h"
#include "cmdline.h"
#include "str.h"
#include "rlhist.h"
#include "interface.h"

Execfunc cmd_sites_cdcd, cmd_sites_help, cmd_sites_refresh,
  cmd_sites_display, cmd_sites_add, cmd_sites_delete, cmd_sites_move,
  cmd_sites_quit;

struct Command sites_cmds[] = {
  ALIAS ("?", "help"),
  {
   "..", "", "Go back to the cdcd command-line.",
   cmd_sites_cdcd, NULL, 0},
  {
   "help", NULL, "Ha, ha, ha.  This is an truly excellent joke.",
   cmd_sites_help, NULL, 0},
  {
   "refresh", "[URL]", "Update the CDDB server list from the default "
   "CDDB server or the specified URL.", cmd_sites_refresh, NULL, 1},
  {
   "display", "", "Display the current CDDB server list.",
   cmd_sites_display, NULL, 1},
  {
   "add", "LOCATION url=URL TYPE",
   "Add an entry to the CDDB server list.  `LOCATION' is a position "
   "in the list.",
   cmd_sites_add, NULL, 1},
  {
   "del", "[LOCATION]", "Delete an entry from the CDDB server list.",
   cmd_sites_delete, NULL, 1},
  {
   "move", "SOURCE DESTINATION",
   "Move a CDDB server list entry from `SOURCE' to `DESTINATION'.",
   cmd_sites_move, NULL, 1},
  {
   "quit", "", "Quit cdcd.", cmd_sites_quit, NULL, 0},
  ALIAS ("exit", "quit"),
  {NULL, NULL, NULL, NULL, NULL, 0}
};

int
cmd_sites_quit (char **argv)
{
  return XRET_QUIT;
}

int
cmd_sites_cdcd (char **argv)
{
  return XRET_BACK;
}

int
cmd_sites_display (char **argv)
{
  struct cddb_serverlist list;
  struct cddb_conf conf;
  struct cddb_server proxy;
  char *p;

  cddb_read_serverlist (&conf, &list, &proxy);
  p = cddb_serverlist_prettystr (&list);
  fputs (p, stdout);
  free (p);
  return XRET_NULL;
}

/* FIXME: This has not been tested even once yet */
int
cmd_sites_refresh (char **argv)
{
  struct cddb_host host;
  struct cddb_serverlist list;
  struct cddb_conf conf;
  struct cddb_server proxy;

  cddb_read_serverlist (&conf, &list, &proxy);

  if (argv[1])
    cddb_process_url (&host, argv[1]);
  else
    {
      host.host_protocol = CDDB_MODE_CDDBP;
      strncpy (host.host_server.server_name, cdcd_cddb_host, 256);
      host.host_server.server_port = CDDBP_DEFAULT_PORT;
    }

  cddb_get_siteslist (&list, host);	/* FIXME: Isn't this a memory leak?  */

  if (list.list_len < 1)
    {
      puts ("Retrieved empty server list... not stored");
      return XRET_NULL;
    }

  conf.conf_access = CDDB_ACCESS_REMOTE;
  cddb_write_serverlist (conf, list, proxy);
  return XRET_NULL;
}

int
cmd_sites_add (char **argv)
{
  int location;
  int type;
  char *url;
  struct cddb_serverlist list;
  struct cddb_conf conf;
  struct cddb_server proxy;
  int loc_provided = 0, type_provided = 0, url_provided = 0;

  cddb_read_serverlist (&conf, &list, &proxy);

  for (++argv; *argv; argv++)
    {
      if (!loc_provided && !read_int (*argv, &location))
	{
	  loc_provided = 1;
	}
      else if (!type_provided && !read_type (*argv, &type))
	{
	  type_provided = 1;
	}
      else if (!url_provided && !read_url (*argv, &url))
	{
	  url_provided = 1;
	}
      else
	{
	  if (url_provided)
	    free (url);
	  puts ("Errors during arguments parsing");
	  return XRET_NULL;
	}
    }

  if (!loc_provided || !url_provided || !type_provided)
    {
      if (url_provided)
	free (url);
      puts ("You must supply all the arguments.");
      return XRET_NULL;
    }

  --location;

  if (location < 0 || location > list.list_len)
    {
      free (url);
      puts ("Invalid location");
      return XRET_NULL;
    }

  {
    int i;
    list.list_len++;
    for (i = list.list_len; i > location; i--)
      memcpy (&list.list_host[i],
	      &list.list_host[i - 1], sizeof (struct cddb_host));
    cddb_process_url (&list.list_host[location], url);
  }
  free (url);

  /* FIXME NOW: Since I don't understand how it worked, I don't know
     how to make it work here.  -- fb.  */
  if (type == TYPE_CDDB)
    list.list_host[location].host_protocol = CDDB_MODE_CDDBP;
  else
    list.list_host[location].host_protocol = CDINDEX_MODE_HTTP;

  cddb_write_serverlist (conf, list, proxy);
  return XRET_NULL;
}

int
cmd_sites_delete (char **argv)
{
  int loc_provided = 0;
  int location;
  struct cddb_serverlist list;
  struct cddb_conf conf;
  struct cddb_server proxy;

  cddb_read_serverlist (&conf, &list, &proxy);

  /* You may think this is useless, but it's much easier to maintain
     if we need, say, an additional option */
  for (++argv; *argv; argv++)
    {
      if (!loc_provided && !read_int (*argv, &location))
	loc_provided = 1;
      else
	{
	  puts ("Errors during arguments parsing");
	  return XRET_NULL;
	}
    }

  if (!loc_provided)
    location = 1;

  if (location < 1 || location > list.list_len)
    {
      puts ("Invalid location");
      return XRET_NULL;
    }

  {
    int i;
    for (i = location; i < list.list_len - 1; i++)
      memcpy (&list.list_host[i],
	      &list.list_host[i + 1], sizeof (struct cddb_host));
    list.list_len--;
  }

  cddb_write_serverlist (conf, list, proxy);
  return XRET_NULL;
}

int
cmd_sites_move (char **argv)
{
  struct cddb_serverlist list;
  struct cddb_conf conf;
  struct cddb_server proxy;
  struct cddb_host host;
  int l1, l2;
  int l1_prov = 0, l2_prov = 0;

  cddb_read_serverlist (&conf, &list, &proxy);

  {
    char **arg;
    for (arg = argv + 1; *arg; arg++)
      {
	if (!l1_prov && !read_int (*arg, &l1))
	  l1_prov = 1;
	else if (!l2_prov && !read_int (*arg, &l2))
	  l2_prov = 1;
	else
	  {
	    puts ("Errors during argument parsing");
	    return 0;
	  }
      }
  }

  if (!l1_prov || !l2_prov)
    {
      puts ("Not enough parameters");
      return 0;
    }

  l1--;
  l2--;
  if (l1 < 0 || l2 < 0 || l1 > list.list_len || l2 > list.list_len)
    {
      puts ("Arguments out of range");
      return 0;
    }

  /* This has been essentially copy-pasted */
  {
    int i;
    memcpy (&host, &list.list_host[l1], sizeof (struct cddb_host));
    if (l2 > l1)
      {
	for (i = l1; i < l2; i++)
	  memcpy (&list.list_host[i],
		  &list.list_host[i + 1], sizeof (struct cddb_host));
      }
    else if (l2 < l1)
      {
	for (i = l1; i > l2; i--)
	  memcpy (&list.list_host[i],
		  &list.list_host[i - 1], sizeof (struct cddb_host));
      }
    memcpy (&list.list_host[l2], &host, sizeof (struct cddb_host));
  }

  cddb_write_serverlist (conf, list, proxy);
  cddb_process_url (&host, argv[0]);
  return 0;
}

int
cmd_sites_help (char **argv)
{
  cmdhelp (sites_cmds, argv, 0);
  return 0;
}

char *
sites_command_matcher (const char *text, int state)
{
  return command_matcher (sites_cmds, text, state);
}

int
cmd_sites_mainloop ()
{
  return cmd_mainloop (sites_command_matcher, "cdcd/sites> ", sites_cmds);
}

void
init_cmd_sites ()
{
  sort_commands (sites_cmds);
}