char* cm_strcat(const char* a, const char* b);
char* cm_strdup(const char* str);
+char* cm_trimstart(const char* str);
+char* cm_trimend(const char* str);
+char* cm_trim(const char* str);
#endif
}
char* cm_strdup(const char* str) { return cm_strcat(str, ""); }
+
+char* cm_trimstart(const char* str){
+ int i;
+ for(i = 0; str[i] != 0; i++){
+ if(str[i] != ' ' && str[i] != '\t'){
+ return cm_strdup(str + i);
+ }
+ }
+ return cm_strdup("");
+}
+
+char* cm_trimend(const char* str){
+ char* s = cm_strdup(str);
+ int i;
+ for(i = strlen(s) - 1; i >= 0; i--){
+ if(s[i] != '\t' && s[i] != ' '){
+ s[i + 1] = 0;
+ break;
+ }
+ }
+ return s;
+}
+
+char* cm_trim(const char* str){
+ char* tmp = cm_trimstart(str);
+ char* s = cm_trimend(tmp);
+ free(tmp);
+ return s;
+}
PWD = `pwd`
PLATFORM = generic
-PREFIX = /usr
+PREFIX = /usr/local
FLAGS = PWD=$(PWD) PLATFORM=$(PLATFORM) PREFIX=$(PREFIX)
.PHONY: all clean
.SUFFIXES: .c .o
-OBJS = version.o main.o
+OBJS = version.o main.o config.o
all: tewi$(EXEC)
--- /dev/null
+/* $Id$ */
+
+#include "tw_config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <cm_string.h>
+#include <cm_log.h>
+
+int tw_config_read(const char* path){
+ cm_log("Config", "Reading %s", path);
+ char cbuf[2];
+ cbuf[1] = 0;
+ FILE* f = fopen(path, "r");
+ if(f != NULL){
+ char* line = malloc(1);
+ line[0] = 0;
+ while(1){
+ int c = fread(cbuf, 1, 1, f);
+ if(cbuf[0] == '\n' || c <= 0){
+ char* l = cm_trim(line);
+ if(strlen(l) > 0 && l[0] != '#'){
+ printf("[%s]\n", l);
+ }
+ free(l);
+ free(line);
+ line = malloc(1);
+ line[0] = 0;
+ if(c <= 0) break;
+ }else if(cbuf[0] != '\r'){
+ char* tmp = line;
+ line = cm_strcat(tmp, cbuf);
+ free(tmp);
+ }
+ }
+ free(line);
+ fclose(f);
+ return 0;
+ }else{
+ return 1;
+ }
+}
#include <cm_log.h>
+#include "tw_config.h"
#include "tw_version.h"
extern bool cm_do_log;
int main(int argc, char** argv) {
int i;
+ const char* config = PREFIX "/etc/tewi.conf";
for(i = 1; i < argc; i++) {
if(argv[i][0] == '-') {
if(strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
} else {
cm_do_log = true;
}
+ } else if(strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-C") == 0){
+ i++;
+ if(argv[i] == NULL){
+ fprintf(stderr, "Missing argument\n");
+ return 1;
+ }
+ config = argv[i];
} else {
fprintf(stderr, "Unknown option: %s\n", argv[i]);
return 1;
}
}
}
+ if(tw_config_read(config) != 0){
+ fprintf(stderr, "Could not read the config\n");
+ return 1;
+ }
cm_log("Daemon", "Ready");
}
--- /dev/null
+/* $Id$ */
+
+#ifndef __TW_CONFIG_H__
+#define __TW_CONFIG_H__
+
+int tw_config_read(const char* path);
+
+#endif