chiark / gitweb /
more thorough kvp.c testing
[disorder] / server / trackname.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004, 2005, 2006 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20
21 #include <config.h>
22 #include "types.h"
23
24 #include <getopt.h>
25 #include <locale.h>
26 #include <errno.h>
27 #include <string.h>
28
29 #include "configuration.h"
30 #include "syscalls.h"
31 #include "log.h"
32 #include "trackname.h"
33 #include "mem.h"
34 #include "charset.h"
35 #include "defs.h"
36
37 static const struct option options[] = {
38   { "help", no_argument, 0, 'h' },
39   { "version", no_argument, 0, 'V' },
40   { "config", required_argument, 0, 'c' },
41   { "debug", no_argument, 0, 'd' },
42   { 0, 0, 0, 0 }
43 };
44
45 /* display usage message and terminate */
46 static void help(void) {
47   xprintf("Usage:\n"
48           "  trackname [OPTIONS] TRACK CONTEXT PART\n"
49           "Options:\n"
50           "  --help, -h              Display usage message\n"
51           "  --version, -V           Display version number\n"
52           "  --config PATH, -c PATH  Set configuration file\n"
53           "  --debug, -d             Turn on debugging\n");
54   xfclose(stdout);
55   exit(0);
56 }
57
58 /* display version number and terminate */
59 static void version(void) {
60   xprintf("disorder version %s\n", disorder_version_string);
61   xfclose(stdout);
62   exit(0);
63 }
64
65 int main(int argc, char **argv) {
66   int n;
67   const char *s;
68
69   if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
70   while((n = getopt_long(argc, argv, "hVc:d", options, 0)) >= 0) {
71     switch(n) {
72     case 'h': help();
73     case 'V': version();
74     case 'c': configfile = optarg; break;
75     case 'd': debugging = 1; break;
76     default: fatal(0, "invalid option");
77     }
78   }
79   if(argc - optind < 3) fatal(0, "not enough arguments");
80   if(argc - optind > 3) fatal(0, "too many arguments");
81   if(config_read(0)) fatal(0, "cannot read configuration");
82   s = trackname_part(argv[optind], argv[optind+1], argv[optind+2]);
83   if(!s) fatal(0, "trackname_part returned NULL");
84   xprintf("%s\n", nullcheck(utf82mb(s)));
85   if(fclose(stdout) < 0) fatal(errno, "error closing stdout");
86   return 0;
87 }
88
89 /*
90 Local Variables:
91 c-basic-offset:2
92 comment-column:40
93 End:
94 */