File: file.c

package info (click to toggle)
mpdscribble 0.2.10-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 716 kB
  • ctags: 500
  • sloc: ansic: 3,904; sh: 3,603; makefile: 70
file content (540 lines) | stat: -rw-r--r-- 13,025 bytes parent folder | download | duplicates (2)
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
    This file is part of mpdscribble,
    another audioscrobbler plugin for music player daemon.
    Copyright © 2005,2006 Kuno Woudt <kuno@frob.nl>

    mpdscribble 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.

    mpdscribble 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 mpdscribble; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <regex.h>

#include "as.h"
#include "file.h"
#include "misc.h"

struct pair
{
  char *key;
  char *val;
  struct pair *next;
};

enum file_type { conf_type, cache_type, log_type, };

struct config file_config =
  { NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, file_unknown, };

FILE *file_loghandle = NULL;
int file_saved_count = 0;

static char *file_getname (enum file_type type);

static const char *blurb = 
  "mpdscribble (" AS_CLIENT_ID " " AS_CLIENT_VERSION ").\n"
  "another audioscrobbler plugin for music player daemon.\n"
  "Copyright 2005,2006 Kuno Woudt <kuno@frob.nl>.\n"
  "\n";


void
version (void)
{
  printf (blurb);

  printf ("mpdscribble comes with NO WARRANTY, to the extent permitted by law.\n"
          "You may redistribute copies of mpdscribble under the terms of the\n"
          "GNU General Public License.\n"
          "For more information about these matters, see the file named COPYING.\n"
          "\n");

  exit (1);
}

void
help (void)
{
  printf (blurb);

  printf ("Usage: mpdscribble [OPTIONS]\n"
          "\n"
          "  --help                      \tthis message\n"
          "  --version                   \tthat message\n"
          "  --log            <filename> \tlog file\n"
          "  --cache          <filename> \tcache file\n"
          "  --conf           <filename> \tconfiguration file\n"
          "  --host           <host>     \tmpd host\n"
          "  --port           <port>     \tmpd port\n"
          "  --sleep          <interval> \tupdate interval (default 1 second)\n"
          "  --cache-interval <interval> \twrite cache file every i seconds\n"
          "                              \t(default 600 seconds)\n"
          "  --verbose <0-2>             \tverbosity (default 1)\n"
          "\n"
          "Report bugs to <kuno@frob.nl>.\n");

  exit (1);
}

static int
file_atoi (const char *s)
{
  if (!s)
    return 0;

  return atoi (s);
}

static void
free_pairs (struct pair *p)
{
  struct pair *n;

  if (!p)
    return;

  do
    {
      n = p->next;
      free (p->key);
      free (p->val);
      free (p);
      p = n;
    }
  while (p);
}

static void
add_pair (struct pair **stack, char *ptr,
          int s0, int e0, int s1, int e1)
{
  char *key = (char *) calloc (e0 - s0 + 1, 1);
  char *val = (char *) calloc (e1 - s1 + 1, 1);
  struct pair *p = (struct pair *) malloc (sizeof (struct pair));
  struct pair *last;

  if (!p)
    exit (ENOMEM);

  memcpy (key, ptr + s0, e0 - s0);
  memcpy (val, ptr + s1, e1 - s1);

  p->key = key;
  p->val = val;
  p->next = NULL;

  if (!*stack)
    {
      *stack = p;
      return;
    }

  last = *stack;
  while (last->next)
    last = last->next;
  last->next = p;
}

static struct pair *
get_pair (char *str)
{
  struct pair *p = NULL;
  char *ptr;
  regex_t compiled;
  regmatch_t m[4];
  int error = 0;

  if ((error = regcomp (&compiled,
                        "^(#.*|[ \t]*|([A-Za-z_][A-Za-z0-9_]*) = (.*))$",
                        REG_NEWLINE | REG_EXTENDED)))
    fatal ("error %i when compiling regexp, this is a bug.\n", error);

  m[0].rm_eo = 0;
  ptr = str - 1;
  do
    {
      ptr += m[0].rm_eo + 1;
      error = regexec (&compiled, ptr, 4, m, 0);
      if (!error && m[3].rm_eo != -1)
        add_pair (&p, ptr,
                  m[2].rm_so, m[2].rm_eo,
                  m[3].rm_so, m[3].rm_eo);
    }
  while (!error);

  regfree (&compiled);

  return p;
}

static char *
read_file (char *filename)
{
  long size;
  char *ret;
  size_t count;

  FILE *handle = fopen (filename, "rb");
  if (!handle)
    {
      warning_errno ("error opening %s", filename);
      return NULL;
    }

  fseek (handle, 0, SEEK_END);
  size = ftell (handle);
  fseek (handle, 0, SEEK_SET);

  ret = (char *) calloc (size+1, 1);
  count = fread (ret, 1, size, handle);
  fclose (handle);

  if (count == size)
    return ret;

  warning_errno ("error reading %s", filename);
  free (ret);
  return NULL;
}

static int
file_exists (char *filename)
{
  FILE *handle = fopen (filename, "rb");
  if (!handle)
    return 0;

  fclose (handle);
  return 1;
}

static char *
file_expand_tilde (char *path)
{
  char *home;

  if (path[0] != '~')
    return strdup2 (path);
    
  home = getenv ("HOME");
  if (!home)
    home = "./";

  return concat (home, path+1, NULL);
}


static char *
file_getname (enum file_type type)
{
  char *file = NULL;

  switch (type)
    {
    case conf_type:
      file = file_expand_tilde (FILE_HOME_CONF);
      if (file_exists (file))
        {
          file_config.loc = file_home;
        }
      else
        {
          free (file);
          file = file_expand_tilde (FILE_CONF);
          if (!file_exists (file))
            {
              free (file);
              return NULL;
            }
          file_config.loc = file_etc;
        }
      break;

    case cache_type:
      if (file_config.loc == file_home)
        file = file_expand_tilde (FILE_HOME_CACHE);
      else if (file_config.loc == file_etc)
        file = file_expand_tilde (FILE_CACHE);
      break;
    case log_type:
      if (file_config.loc == file_home)
        file = file_expand_tilde (FILE_HOME_LOG);
      else if (file_config.loc == file_etc)
        file = file_expand_tilde (FILE_LOG);
      break;
    }

  if (!file)
    {
      switch (type)
        {
        case conf_type:
          fatal ("internal error. this is a bug.");
        case cache_type:
          fatal ("please specify where to put the cache file.");
        case log_type:
          fatal ("please specify where to put the log file.");
        }
    }

  return file;
}

FILE *
file_open_logfile (void)
{
  char *log = file_config.log;

  file_loghandle = fopen (log, "ab");
  if (!file_loghandle)
    fatal_errno ("cannot open %s", log);

  return file_loghandle;
}

static void
replace (char **dst, char *src)
{
  if (*dst)
    free (*dst);
  *dst = src;
}

int
file_read_config (int argc, char **argv)
{
  char *mpd_host = getenv ("MPD_HOST");
  char *mpd_port = getenv ("MPD_PORT");
  char *data;
  int i;

  file_config.verbose = -1;

  /* look for config path in command-line options. */
  for (i = 0; i < argc; i++)
    {
      if (!strcmp ("--conf", argv[i]))
        replace (&file_config.conf, strdup2 (argv[++i]));
    }

  if (!file_config.conf
      || !file_exists (file_config.conf))
    {
      file_config.conf = file_getname (conf_type);
    }

  /* parse config file options. */
  if (file_config.conf && (data = read_file (file_config.conf)))
    {
      struct pair *p = get_pair (data);

      while (p)
        {
          if (!strcmp ("username", p->key))
            file_config.username = strdup2 (p->val);
          else if (!strcmp ("password", p->key))
            file_config.password = strdup2 (p->val);
          else if (!strcmp ("log", p->key))
            file_config.log = strdup2 (p->val);
          else if (!strcmp ("cache", p->key))
            file_config.cache = strdup2 (p->val);
          else if (!strcmp ("musicdir", p->key))
            file_config.musicdir = strdup2 (p->val);
          else if (!strcmp ("host", p->key))
            file_config.host = strdup2 (p->val);
          else if (!strcmp ("port", p->key))
            file_config.port = file_atoi (p->val);
          else if (!strcmp ("sleep", p->key))
            file_config.sleep = file_atoi (p->val);
          else if (!strcmp ("cache_interval", p->key))
            file_config.cache_interval = file_atoi (p->val);
          else if (!strcmp ("verbose", p->key))
            file_config.verbose = file_atoi (p->val);
          p = p->next;
        }

      free_pairs (p);
    }

  /* parse command-line options. */
  for (i = 0; i < argc; i++)
    {
      if (!strcmp ("--help", argv[i]))
        help ();
      else if (!strcmp ("--version", argv[i]))
        version ();
      else if (!strcmp ("--host", argv[i]))
        replace (&file_config.host, strdup2 (argv[++i]));
      else if (!strcmp ("--log", argv[i]))
        replace (&file_config.log, strdup2 (argv[++i]));
      else if (!strcmp ("--cache", argv[i]))
        replace (&file_config.cache, strdup2 (argv[++i]));
      else if (!strcmp ("--port", argv[i]))
        file_config.port = file_atoi (argv[++i]);
      else if (!strcmp ("--sleep", argv[i]))
        file_config.sleep = file_atoi (argv[++i]);
      else if (!strcmp ("--cache-interval", argv[i]))
        file_config.cache_interval = file_atoi (argv[++i]);
      else if (!strcmp ("--verbose", argv[i]))
        file_config.verbose = file_atoi (argv[++i]);
    }

  if (!file_config.conf)
    fatal ("cannot find configuration file");

  if (!file_config.username)
    fatal ("no audioscrobbler username specified in %s", file_config.conf);
  if (!file_config.password)
    fatal ("no audioscrobbler password specified in %s", file_config.conf);
  if (!file_config.host)
    file_config.host = mpd_host;
  if (!file_config.host)
    file_config.host = strdup2 (FILE_DEFAULT_HOST);
  if (!file_config.log)
    file_config.log = file_getname (log_type);
  if (!file_config.cache)
    file_config.cache = file_getname (cache_type);
  if (!file_config.port && mpd_port)
    file_config.port = file_atoi (mpd_port);
  if (!file_config.port)
    file_config.port = FILE_DEFAULT_PORT;
  if (!file_config.sleep)
    file_config.sleep = 1;
  if (!file_config.cache_interval)
    file_config.cache_interval = 600;
  if (file_config.verbose == -1)
    file_config.verbose = 2;

  free (data);
  return 1;
}

void
file_cleanup (void)
{
  if (file_config.username)
    free (file_config.username);
  if (file_config.password)
    free (file_config.password);
  if (file_config.host)
    free (file_config.host);
  if (file_config.log)
    free (file_config.log);
  if (file_config.conf)
    free (file_config.conf);
  if (file_config.cache)
    free (file_config.cache);

  if (file_loghandle)
    fclose (file_loghandle);
}

int
file_write_cache (struct song *sng)
{
  struct song *tmp = sng;
  int count = 0;
  FILE *handle;

  if (!tmp && file_saved_count == 0)
    return -1;

  handle = fopen (file_config.cache, "wb");
  if (!handle)
    {
      warning_errno ("error opening %s", file_config.cache);
      return 0;
    }

  while (tmp)
    {
      fprintf (handle, "# song %i in queue\na = %s\nt = %s\nb = %s\nm = %s\n"
               "i = %s\nl = %i\n\n", ++count, tmp->artist, tmp->track,
               tmp->album, tmp->mbid, tmp->time, tmp->length);

      tmp = tmp->next;
    }

  fclose (handle);

  file_saved_count = count;
  return count;
}

static void
clear_song (struct song *s)
{
  s->artist = NULL;
  s->track = NULL;
  s->album = NULL;
  s->mbid = NULL;
  s->time = NULL;
  s->length = 0;
}

int
file_read_cache (void)
{
  char *data;
  int count = 0;

  if ((data = read_file (file_config.cache)))
    {
      struct pair *root = get_pair (data);
      struct pair *p = root;
      struct song sng;

      clear_song (&sng);

      while (p)
        {
          if (!strcmp ("a", p->key)) sng.artist = strdup2 (p->val);
          if (!strcmp ("t", p->key)) sng.track = strdup2 (p->val);
          if (!strcmp ("b", p->key)) sng.album = strdup2 (p->val);
          if (!strcmp ("m", p->key)) sng.mbid = strdup2 (p->val);
          if (!strcmp ("i", p->key)) sng.time = strdup2 (p->val);
          if (!strcmp ("l", p->key))
            {
              sng.length = file_atoi (p->val);

              as_songchange ("", sng.artist, sng.track, sng.album,
                             sng.mbid, sng.length, sng.time);

              count++;

              if (sng.artist) { free (sng.artist); sng.artist = NULL; }
              if (sng.track)  { free (sng.track);  sng.track  = NULL; }
              if (sng.album)  { free (sng.album);  sng.album  = NULL; }
              if (sng.mbid)   { free (sng.mbid);   sng.mbid   = NULL; }
              if (sng.time)   { free (sng.time);   sng.time   = NULL; }

              clear_song (&sng);
            }

          p = p->next;
        }

      free_pairs (p);
    }

  file_saved_count = count;

  free (data);
  return count;
}