-
Notifications
You must be signed in to change notification settings - Fork 2
/
core.c
executable file
·146 lines (122 loc) · 3.46 KB
/
core.c
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
/* Copyright (c) 2016 z411, see LICENSE for details */
#include "core.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAXLINE 100
struct Config config = { 1, NULL, NULL, 0 };
void
run_command(const char * cmd, struct TorrentItem * item)
{
setenv("TITLE", item->title, 1);
setenv("URL", item->link, 1);
system(config.cmd);
}
void
apply_config_opt(char * key, char * val)
{
if (!strcmp(key, "download")) {
config.download = (!strcmp(val, "yes")) ? 1 : 0;
} else if (!strcmp(key, "category")) {
config.category = strtol(val, NULL, 10);
} else if (!strcmp(key, "cmd")) {
if (config.cmd == NULL) {
config.cmd = malloc((strlen(val)+1)*sizeof(*config.cmd));
if (config.cmd != NULL) strcpy(config.cmd, val);
} else
printf("config: Duplicate option, ignoring: %s\n", key);
} else if (!strcmp(key, "path")) {
if (config.cmd == NULL) {
config.path = malloc((strlen(val)+1)*sizeof(*config.path));
if (config.path != NULL) strcpy(config.path, val);
} else
printf("config: Duplicate option, ignoring: %s\n", key);
} else {
printf("config: Invalid option, ignoring: %s\n", key);
}
}
int
load_config(const char *name)
{
/* Try current directory */
if(access(name, R_OK) == 0)
return parse_config(name);
char *env;
char *path;
/* Try XDG */
env = getenv("XDG_CONFIG_HOME");
if(env) {
size_t sz = strlen(env) + 1 + strlen("ncnyaa") + 1 + strlen(name) + 1;
path = malloc(sz);
if(path && snprintf(path, sz, "%s/ncnyaa/%s", env, name) && access(path, R_OK) == 0)
return parse_config(path);
}
/* Try HOME */
env = getenv("HOME");
if(env) {
size_t sz = strlen(env) + 1 + strlen(".config/ncnyaa") + 1 + strlen(name) + 1;
path = malloc(sz);
if(path && snprintf(path, sz, "%s/.config/ncnyaa/%s", env, name) && access(path, R_OK) == 0)
return parse_config(path);
}
printf("No configuration file found.\n");
return 0;
}
int
parse_config(const char * filename)
{
printf("reading %s\n", filename);
FILE *fp;
char line[MAXLINE];
if ((fp = fopen(filename, "r")) != NULL) {
char * key;
char * val;
while (fgets(line, MAXLINE, fp)) {
/* Ignore comment */
if(line[0] == '#') continue;
/* Remove endline */
int ln = strlen(line) - 1;
if(line[ln] == '\n')
line[ln--] = 0;
/* Remove CR */
if(line[ln] == '\r')
line[ln--] = 0;
/* Ignore empty lines */
if(ln<0) continue;
/* Parse config line */
key = strtok(line, "=");
val = strtok(NULL, "=");
if (val != NULL) {
strip(key);
strip(val);
apply_config_opt(key, val);
} else {
printf("config: Invalid line, ignoring: %s\n", line);
}
}
fclose(fp);
return 1;
} else {
perror("Error reading configuration file");
return 0;
}
}
void
cleanup()
{
free(config.cmd);
free(config.path);
}
void
strip(char * str)
{
if (*str) {
char *p, *q;
for (p = q = str; *p == ' '; p++);
while (*p) *q++ = *p++;
for (; *(q-1) == ' '; q--);
*q = '\0';
}
}