para_play, infrastructure.
[paraslash.git] / play.c
1 /*
2 * Copyright (C) 2012 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file play.c Paraslash's standalone player. */
8
9 #include <regex.h>
10
11 #include "para.h"
12 #include "list.h"
13 #include "play.cmdline.h"
14 #include "filter.cmdline.h"
15 #include "error.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "version.h"
19 #include "string.h"
20 #include "sched.h"
21 #include "filter.h"
22 #include "afh.h"
23 #include "recv.h"
24 #include "write.h"
25 #include "write_common.h"
26 #include "fd.h"
27
28 static struct play_args_info conf;
29
30 /** Initialize the array of errors for para_play. */
31 INIT_PLAY_ERRLISTS;
32
33 /* Activate the afh receiver. */
34 extern void afh_recv_init(struct receiver *r);
35 #undef AFH_RECEIVER
36 #define AFH_RECEIVER {.name = "afh", .init = afh_recv_init},
37 DEFINE_RECEIVER_ARRAY;
38
39 /* FIXME: This is needed by the amp filter. */
40 char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
41
42 static int loglevel = LL_WARNING;
43 INIT_STDERR_LOGGING(loglevel);
44
45 __noreturn static void print_help_and_die(void)
46 {
47 int d = conf.detailed_help_given;
48 const char **p = d? play_args_info_detailed_help
49 : play_args_info_help;
50
51 printf_or_die("%s\n\n", PLAY_CMDLINE_PARSER_PACKAGE "-"
52 PLAY_CMDLINE_PARSER_VERSION);
53 printf_or_die("%s\n\n", play_args_info_usage);
54 for (; *p; p++)
55 printf_or_die("%s\n", *p);
56 print_filter_helps(d);
57 print_writer_helps(d);
58 exit(0);
59 }
60
61 static void parse_config_or_die(int argc, char *argv[])
62 {
63 int ret;
64 char *config_file;
65 struct play_cmdline_parser_params params = {
66 .override = 0,
67 .initialize = 1,
68 .check_required = 0,
69 .check_ambiguity = 0,
70 .print_errors = 1
71 };
72
73 if (play_cmdline_parser_ext(argc, argv, &conf, &params))
74 exit(EXIT_FAILURE);
75 HANDLE_VERSION_FLAG("play", conf);
76 if (conf.help_given || conf.detailed_help_given)
77 print_help_and_die();
78 loglevel = get_loglevel_by_name(conf.loglevel_arg);
79 if (conf.config_file_given)
80 config_file = para_strdup(conf.config_file_arg);
81 else {
82 char *home = para_homedir();
83 config_file = make_message("%s/.paraslash/play.conf", home);
84 free(home);
85 }
86 ret = file_exists(config_file);
87 if (conf.config_file_given && !ret) {
88 PARA_EMERG_LOG("can not read config file %s\n", config_file);
89 goto err;
90 }
91 if (ret) {
92 params.initialize = 0;
93 params.check_required = 1;
94 play_cmdline_parser_config_file(config_file, &conf, &params);
95 }
96 free(config_file);
97 return;
98 err:
99 free(config_file);
100 exit(EXIT_FAILURE);
101 }
102
103 int main(int argc, char *argv[])
104 {
105 /* needed this early to make help work */
106 recv_init();
107 filter_init();
108 writer_init();
109
110 parse_config_or_die(argc, argv);
111 return 0;
112 }