1 /*
2  * Copyright (c) 2011 Tim van der Molen <tim@kariliq.nl>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 
21 #include "siren.h"
22 
23 #if defined(HAVE_PLEDGE) && defined(HAVE_ERR)
24 #include <err.h>
25 #endif
26 
27 #if defined(DEBUG) && defined(__OpenBSD__)
28 extern const char	*malloc_options;
29 #endif
30 
31 #ifdef HAVE___PROGNAME
32 extern char		*__progname;
33 #else
34 const char		*__progname;
35 #endif
36 
37 NORETURN static void
38 usage(void)
39 {
40 	fprintf(stderr, "usage: %s [-lv] [-c directory]\n", __progname);
41 	exit(1);
42 }
43 
44 NORETURN static void
45 version(void)
46 {
47 	printf("siren %s\n", VERSION);
48 	exit(0);
49 }
50 
51 int
52 main(int argc, char **argv)
53 {
54 	int	 c, lflag;
55 	char	*confdir;
56 #ifdef HAVE_PLEDGE
57 	char	*promises;
58 #endif
59 
60 #if defined(DEBUG) && defined(__OpenBSD__)
61 	malloc_options = "CFGJRS";
62 #endif
63 
64 #ifndef HAVE___PROGNAME
65 	if (argv[0] != NULL && argv[0][0] != '\0')
66 		__progname = argv[0];
67 	else
68 		__progname = "siren";
69 #endif
70 
71 	confdir = NULL;
72 	lflag = 0;
73 	while ((c = getopt(argc, argv, "c:lv")) != -1)
74 		switch (c) {
75 		case 'c':
76 			confdir = optarg;
77 			break;
78 		case 'l':
79 			lflag = 1;
80 			break;
81 		case 'v':
82 			version();
83 			break;
84 		default:
85 			usage();
86 			break;
87 		}
88 
89 	if (argc != optind)
90 		usage();
91 
92 	opterr = 0;
93 
94 	log_init(lflag);
95 	input_init();
96 	option_init();
97 	bind_init();
98 	conf_init(confdir);
99 	screen_init();
100 	plugin_init();
101 	track_init();
102 	library_init();
103 	playlist_init();
104 	queue_init();
105 	browser_init();
106 	player_init();
107 	prompt_init();
108 
109 #ifdef HAVE_PLEDGE
110 	promises = xstrdup("stdio rpath wpath cpath getpw tty");
111 	plugin_append_promises(&promises);
112 	LOG_INFO("pledging %s", promises);
113 
114 	if (pledge(promises, NULL) == -1)
115 		err(1, "pledge");
116 
117 	free(promises);
118 #endif
119 
120 	screen_print();
121 	conf_read_file();
122 	library_read_file();
123 	cache_update();
124 	input_handle_key();
125 
126 	prompt_end();
127 	player_end();
128 	browser_end();
129 	queue_end();
130 	playlist_end();
131 	library_end();
132 	track_end();
133 	plugin_end();
134 	screen_end();
135 	conf_end();
136 	bind_end();
137 	option_end();
138 	log_end();
139 
140 	return 0;
141 }
142