Browse code

Skeleton server (shamelessly ripped from newslettersan)

Dario Rodriguez authored on 25/04/2015 09:55:24
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,158 @@
1
+/*
2
+ * rec_config.c
3
+ *
4
+ * Configuration load/save for recremote
5
+ *
6
+ * Author: Dario Rodriguez dario@softhome.net
7
+ * This program is licensed under the terms of the Affero GPL v1+
8
+ */
9
+
10
+#include <stdio.h>
11
+#include <stdlib.h>
12
+#include <unistd.h>
13
+#include <string.h>
14
+#include <sys/types.h>
15
+#include <sys/stat.h>
16
+
17
+#include "loglib.h"
18
+#include "rec_config.h"
19
+
20
+#define MAXLINESIZE 2048
21
+#define LINESBLOCK 32
22
+
23
+typedef enum esection {
24
+        section_none=0,
25
+        section_general
26
+} esection;
27
+
28
+
29
+recconfig *
30
+recconfig_init(char *configfile)
31
+{
32
+	recconfig *config;
33
+	FILE *f;
34
+	char line[MAXLINESIZE];
35
+        int len;
36
+        char *ptr,*sep,*value;
37
+        esection section;
38
+        int lineno;
39
+        char **configvalue;
40
+        if((f=fopen(configfile,"r"))==NULL)
41
+                return(NULL);
42
+        if((config=malloc(sizeof(recconfig)))==NULL) {
43
+                fclose(f),f=NULL;
44
+                return(NULL);
45
+        }
46
+        memset(config,0,sizeof(recconfig));
47
+        section=section_none;
48
+        lineno=0;
49
+        while(fgets(line,sizeof(line),f)!=NULL) {
50
+                lineno++;
51
+                line[sizeof(line)-1]='\0';
52
+                /* trim line */
53
+                for(ptr=line;*ptr!='\0' && strchr("\t ",*ptr)!=NULL;ptr++)
54
+                        ;
55
+                len=strlen(line);
56
+                while(len>0 && strchr("\n\t ",line[len-1])!=NULL)
57
+	                line[len-1]='\0',len--;
58
+		for(ptr=line;*ptr!='\0' && strchr("\t ",*ptr)!=NULL;ptr++)
59
+                        ;
60
+                len=strlen(ptr);
61
+                /* ignore empty lines and comments */
62
+                if(*ptr=='\0' || strchr("#;",*ptr)!=NULL)
63
+                        continue;
64
+                /* parse line */
65
+                if(*ptr=='[' && ptr[len-1]==']') {
66
+                        /* section */
67
+                        if(strcmp(ptr,"[general]")==0) {
68
+                                section=section_general;
69
+                        } else {
70
+                                log_write("CONF","%s:%i: unknown section name \"%s\"\n",configfile,lineno,ptr);
71
+                                section=section_none;
72
+                        }
73
+                        continue;
74
+                } else if((sep=strchr(ptr,'='))!=NULL) {
75
+                        *sep='\0';
76
+                        value=sep+1;
77
+                        /* trim key */
78
+                        while(sep>ptr && strchr(" \t",sep[-1])!=NULL) {
79
+                                sep--;
80
+                                *sep='\0';
81
+                        }
82
+                        /* trim value */
83
+                        while(*value!='\0' && strchr(" \t",*value)!=NULL)
84
+                                value++;
85
+			/* sanity check */
86
+                        if(*value=='\0') {
87
+				log_write("CONF","%s:%i: ignoring key without value; key:\"%s\"\n",configfile,lineno,ptr);
88
+                                continue;
89
+                        }
90
+			 /* assign value */
91
+                        if(section==section_none) {
92
+                                log_write("CONF","%s:%i: ignoring key-value pair in unknown section; key:\"%s\"\n",configfile,lineno,ptr);
93
+                                continue;
94
+                        }
95
+                        /* identify key-value pair */
96
+                        if(section==section_general) {
97
+                                if(strcmp(ptr,"logfile")==0) {
98
+                                        configvalue=&(config->logfile);
99
+				} else {
100
+                                        log_write("CONF","%s:%i: unknown key, ignoring key-value pair; key:\"%s\"\n",configfile,lineno,ptr);
101
+                                        continue;
102
+                                }
103
+			} else
104
+                                continue;
105
+			/* store value */
106
+                        if(*configvalue!=NULL) {
107
+                                log_write("CONF","%s:%i: duplicate key, ignoring key-value pair; key:\"%s\"\n",configfile,lineno,ptr);
108
+                                continue;
109
+                        }
110
+                        if((*configvalue=strdup(value))==NULL) {
111
+                                fclose(f),f=NULL;
112
+                                recconfig_free(config),config=NULL;
113
+                                return(NULL); /* insufficient memory */
114
+                        }
115
+                        continue;
116
+                } else {
117
+                        log_write("CONF","%s:%i: malformed line, ignoring; contents:\"%s\"\n",configfile,lineno,ptr);
118
+                        continue;
119
+                }
120
+        }
121
+        fclose(f),f=NULL;
122
+        return(config);
123
+}
124
+
125
+void
126
+recconfig_free(recconfig *config)
127
+{
128
+        if(config==NULL)
129
+                return;
130
+        if(config->logfile!=NULL)
131
+                free(config->logfile),config->logfile=NULL;
132
+        free(config),config=NULL;
133
+        return;
134
+}
135
+
136
+int
137
+recconfig_exists(char *configfile)
138
+{
139
+        struct stat st;
140
+        if(stat(configfile,&st)!=0)
141
+                return(-1);
142
+        return(0);
143
+}
144
+
145
+int
146
+recconfig_write(char *configfile,char *logfile)
147
+{
148
+        FILE *f;
149
+        if((f=fopen(configfile,"w"))==NULL)
150
+                return(-1);
151
+        fprintf(f,"; recremote config file\n");
152
+        fprintf(f,"[general]\n");
153
+        fprintf(f,"logfile=%s\n",(logfile!=NULL)?logfile:"recremote.log");
154
+        return(0);
155
+}
156
+
157
+
158
+