File: settings.c

package info (click to toggle)
audtty 0.1.12-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 592 kB
  • sloc: ansic: 1,560; sh: 193; makefile: 63
file content (212 lines) | stat: -rw-r--r-- 5,456 bytes parent folder | download | duplicates (6)
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
/*
    This file is part of xmms-curses, copyright 2003-2005 Knut Auvor Grythe.

    xmms-curses 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.

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

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "main.h"
#include "settings.h"

extern prefs settings;

static void usage(void) 
{
	fprintf(stderr, "Usage: %s [ options ]\n\n", PACKAGE);
	fprintf(stderr, "OPTIONS:\n"
			"  To negate a short option, use '+' instead of '-' (e.g. +a)\n"
			"  Long-options are negated with 'no-', (e.g. --no-read-all)\n\n"
			"  -n, --numbers  Show numbers in playlist (default on)\n"
			"  -t, --title    Read the title from ID3/ogg-tags (SLOW) (default on)\n"
			"  -l, --length   Show length of track in playlist (SLOW) (default on)\n"
			"  -a, --read-all Read all titles and lengths on startup (EXTREMELY SLOW)\n"
			"                 (default off)\n"
			"  -V, --version  Display version information\n"
			"  -h, --help     Print this message\n"
		   	"\n");
	fprintf(stderr, "Tip: Things going slow? Try %s +tl\n\n", PACKAGE);
}

static void unknown_option(gchar *option)
{
	fprintf(stderr, "ERROR: Unrecognized option: '%s'\n\n", option);
	usage();
	exit(EXIT_FAILURE);
}

static void version(void)
{
	printf("%s version %s,\n%s\n\n",
			PACKAGE, VERSION, COPYRIGHT);
	printf("This program is released under the GNU Public License.\n"
			"See the COPYING file for details.\n");
	exit(EXIT_SUCCESS);
}

void set_defaults(void) {
	settings.show_numbers = true;
	settings.read_all = false;
	settings.read_title = true;
	settings.read_length = true;
}

static void shortoptions(gchar cmd, gint state)
{
	gchar *tmp;

	switch (cmd) {
		case 'n': // Show numbers in playlist
			settings.show_numbers = state;
			break;
		case 't': // Read title from ID3/ogg-tags
			settings.read_title = state;
			break;
		case 'l': // Read song length
			settings.read_length = state;
			break;
		case 'a': // Read all tags on startup
			settings.read_all = state;
			break;
		case 'V': // Show version information
			if (state) version();
			break;
		case 'h': // Help
			if (state) {
				usage();
				exit(EXIT_SUCCESS);
			}
			break;
		default:
			tmp = (gchar*)malloc(sizeof(gchar)*3);
			sprintf(tmp, "-%c", cmd);
			unknown_option(tmp);
	}
        g_free(tmp);
}

static void longoptions(gchar *input)
{
	gint state = true;
	if (strncmp(input, "no-", 3) == 0) {
		state = false;
		input += 3;
	}

	if (g_strcmp0(input, "numbers") == 0) {
		shortoptions('n', state);
	} else if (g_strcmp0(input, "title") == 0) {
		shortoptions('t', state);
	} else if (g_strcmp0(input, "length") == 0) {
		shortoptions('l', state);
	} else if (g_strcmp0(input, "read-all") == 0) {
		shortoptions('a', state);
	} else if (g_strcmp0(input, "version") == 0) {
		shortoptions('V', state);
	} else if (g_strcmp0(input, "help") == 0) {
		shortoptions('h', state);
	} else {
		unknown_option(input);
	}
}

static void parse_line(gchar *file, gchar *line)
{
	gchar *end = strchr(line, '#');
	if (end == NULL) 
		end = line+strlen(line)-1;
	else
		*end-- = '\0';
	while (*end == ' ' || *end == '\t' || *end == '\n') *end-- = '\0';
	while (*line == ' ' || *line == '\t') ++line;
	if (*line == '\0') return;
	longoptions(line);
        g_free(end);
}

static void read_file(gchar *file)
{
	struct stat buf;
	if (stat(file, &buf) == -1) {
		if(errno != ENOENT) {
			printf("Error %d while reading %s: %s\n", 
					errno, file, strerror(errno));
			exit(1);
		}
		return;
	}
	
	FILE *fd = fopen(file, "r");
	if (fd != NULL) {
		gchar *buffer = (gchar*) malloc(sizeof(gchar) * CONFIG_MAX_LINE_LENGTH);
		while (fgets(buffer, CONFIG_MAX_LINE_LENGTH, fd) != NULL) {
			parse_line(file, buffer);
		}
                g_free(buffer);
	} else {
		fprintf(stderr, "Warning: Error %d while opening %s: %s\n", 
				errno, file, strerror(errno));
	}
        fclose(fd);
}

void read_conffiles(void)
{
	gchar *homedir = getenv("HOME");
	
	read_file("/etc/audtty.conf");
	
	if (homedir != NULL) {
		gint length = strlen(homedir) + strlen("/.audtty") + 1;
		gchar *homefile = malloc(sizeof(gchar) * length);
		snprintf(homefile, length, "%s/.audtty", homedir);
		read_file(homefile);
		g_free(homefile);
	}
}

void arguments(gint argc, gchar *argv[])
{
	gint i;
	for (i=1; i<argc; i++) {
		gint state = true;
		gchar *input = argv[i];
		
		// Short options
		if ((*input == '-' || *input == '+') && *(input+1) != '-') {
			gchar *pos = input;
			state = *pos++ == '-';
			while (*pos != '\0')
				shortoptions(*pos++, state);
                        g_free(pos);
		}
		
		// Long options
		else if (strncmp(input, "--", 2) == 0) {
			input += 2;
			longoptions(input);
		} else {
			unknown_option(input);
		}
                g_free(input);
	}

}