remove configfile

This commit is contained in:
Olaf Rempel 2007-05-02 20:32:36 +02:00
parent e4aedb2332
commit 3cea5a1374
9 changed files with 16 additions and 285 deletions

View File

@ -2,7 +2,7 @@ CFLAGS := -O2 -pipe -Wall
all: zyxel-revert compress decompress cfgpatch
zyxel-revert: configfile.o event.o filedata.o logging.o context.o serial.o statemachine.o xmodem.o zyxel-revert.o
zyxel-revert: event.o filedata.o logging.o context.o serial.o statemachine.o xmodem.o zyxel-revert.o
$(CC) $(CFLAGS) $^ -o $@
compress: filedata.o lzsc.o romfile.o compress.o

View File

@ -1,217 +0,0 @@
/***************************************************************************
* Copyright (C) 06/2006 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* This program 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. *
* *
* This program 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 this program; 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 <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include "configfile.h"
#include "list.h"
#include "logging.h"
#define BUFSIZE 1024
struct conf_section {
struct list_head list;
struct list_head tupel_list;
const char *name;
};
struct conf_tupel {
struct list_head list;
const char *option;
const char *parameter;
};
static LIST_HEAD(config_list);
static struct conf_section * config_add_section(const char *name)
{
struct conf_section *section;
section = malloc(sizeof(struct conf_section) + strlen(name));
if (section == NULL)
return NULL;
INIT_LIST_HEAD(&section->list);
INIT_LIST_HEAD(&section->tupel_list);
section->name = strdup(name);
if (section->name == NULL) {
free(section);
return NULL;
}
list_add_tail(&section->list, &config_list);
return section;
}
static int config_add_tupel(struct conf_section *section, const char *option, const char *parameter)
{
struct conf_tupel *tupel = malloc(sizeof(struct conf_tupel));
if (tupel == NULL)
return -1;
INIT_LIST_HEAD(&tupel->list);
tupel->option = strdup(option);
tupel->parameter = strdup(parameter);
if (tupel->option == NULL || tupel->parameter == NULL) {
free(tupel);
return -1;
}
list_add_tail(&tupel->list, &section->tupel_list);
return 0;
}
int config_parse(const char *config)
{
FILE *fz = fopen(config, "r");
if (fz == NULL) {
log_print(LOG_ERROR, "config_parse(): %s", config);
return -1;
}
char *line = malloc(BUFSIZE);
if (line == NULL) {
log_print(LOG_ERROR, "config_parse(): out of memory");
fclose(fz);
return -1;
}
int linenum = 0;
struct conf_section *section = NULL;
while (fgets(line, BUFSIZE, fz) != NULL) {
linenum++;
if (line[0] == '#' || line[0] <= ' ') {
continue;
} else if (line[0] == '[') {
char *tok = strtok(line +1, " ]\n");
if (tok == NULL || (section = config_add_section(tok)) == NULL) {
log_print(LOG_WARN, "config_parse(): invalid section in row %d", linenum);
free(line);
fclose(fz);
return -1;
}
continue;
} else if (section == NULL) {
log_print(LOG_WARN, "config_parse(): missing section in row %d", linenum);
free(line);
fclose(fz);
return -1;
}
char *tok = strtok(line, " \n");
if (tok != NULL) {
char *tok2;
while ((tok2 = strtok(NULL, " \n"))) {
if (config_add_tupel(section, tok, tok2) != 0)
log_print(LOG_WARN, "config_parse(): invalid row %d", linenum);
}
}
}
fclose(fz);
free(line);
return 0;
}
void config_free(void)
{
struct conf_section *section, *section_tmp;
struct conf_tupel *tupel, *tupel_tmp;
list_for_each_entry_safe(section, section_tmp, &config_list, list) {
list_for_each_entry_safe(tupel, tupel_tmp, &section->tupel_list, list) {
list_del(&tupel->list);
free((char *)tupel->option);
free((char *)tupel->parameter);
free(tupel);
}
list_del(&section->list);
free(section);
}
}
static struct conf_section * config_get_section(const char *name)
{
struct conf_section *section;
list_for_each_entry(section, &config_list, list) {
if (!strcmp(section->name, name))
return section;
}
return NULL;
}
const char * config_get_string(const char *section_str, const char *option, const char *def)
{
struct conf_section *section = config_get_section(section_str);
if (section != NULL) {
struct conf_tupel *tupel;
list_for_each_entry(tupel, &section->tupel_list, list) {
if (!strcmp(tupel->option, option))
return tupel->parameter;
}
}
if (def != NULL)
log_print(LOG_WARN, "config [%s:%s] not found, using default: '%s'",
section_str, option, def);
return def;
}
int config_get_int(const char *section, const char *option, int def)
{
const char *ret = config_get_string(section, option, NULL);
if (ret == NULL) {
log_print(LOG_WARN, "config [%s:%s] not found, using default: '%d'",
section, option, def);
return def;
}
return atoi(ret);
}
int config_get_strings(const char *section_str, const char *option,
int (*callback)(const char *value, void *privdata),
void *privdata)
{
struct conf_section *section = config_get_section(section_str);
if (section == NULL)
return -1;
int cnt = 0;
struct conf_tupel *tupel;
list_for_each_entry(tupel, &section->tupel_list, list) {
if (!strcmp(tupel->option, option))
if (callback(tupel->parameter, privdata) == 0)
cnt++;
}
return cnt;
}

View File

@ -1,15 +0,0 @@
#ifndef _CONFIG_H_
#define _CONFIG_H_
int config_parse(const char *config);
void config_free(void);
const char * config_get_string(const char *section_str, const char *option, const char *def);
int config_get_int(const char *section, const char *option, int def);
int config_get_strings(const char *section_str, const char *option,
int (*callback)(const char *value, void *privdata),
void *privdata);
#endif /* _CONFIG_H_ */

View File

@ -6,7 +6,6 @@
#include <fcntl.h>
#include <termios.h>
#include "configfile.h"
#include "context.h"
#include "event.h"
#include "logging.h"
@ -80,7 +79,7 @@ static int close_serial(struct context *ctx)
return 0;
}
static int serial_init_cb(const char *parameter, void *privdata)
int serial_init(const char *device)
{
struct context *ctx = create_context();
if (ctx == NULL)
@ -93,7 +92,7 @@ static int serial_init_cb(const char *parameter, void *privdata)
return -1;
}
if (open_serial(ctx, parameter) < 0) {
if (open_serial(ctx, device) < 0) {
free(ctx->dev_privdata);
destroy_context(ctx);
return -1;
@ -107,9 +106,3 @@ static int serial_init_cb(const char *parameter, void *privdata)
ctx->event = event_add_readfd(NULL, ctx->fd, statemachine_read, ctx);
return 0;
}
int serial_init(void)
{
config_get_strings("ports", "serial", serial_init_cb, NULL);
return 0;
}

View File

@ -1,6 +1,6 @@
#ifndef _SERIAL_H_
#define _SERIAL_H_
int serial_init(void);
int serial_init(const char *device);
#endif /* _SERIAL_H_ */

View File

@ -8,7 +8,6 @@
#include <sys/stat.h>
#include <fcntl.h>
#include "configfile.h"
#include "context.h"
#include "filedata.h"
#include "logging.h"
@ -101,12 +100,8 @@ int xmodem_read(int fd, void *privdata)
return 0;
}
int xmodem_init(void)
int xmodem_init(const char *filename)
{
const char *filename = config_get_string("global", "configdata", NULL);
if (filename == NULL)
return -1;
filedata = get_filedata(filename);
if (filedata == NULL)
return -1;

View File

@ -1,7 +1,7 @@
#ifndef _XMODEM_H_
#define _XMODEM_H_
int xmodem_init(void);
int xmodem_init(const char *filename);
void xmodem_close(void);
int xmodem_read(int fd, void *privdata);

View File

@ -5,45 +5,34 @@
#include <getopt.h>
#include "configfile.h"
#include "context.h"
#include "event.h"
#include "serial.h"
#include "xmodem.h"
#define DEFAULT_CONFIG "zyxel-revert.conf"
static struct option opts[] = {
{"config", 1, 0, 'c'},
{"debug", 0, 0, 'd'},
{"help", 0, 0, 'h'},
{"device", 1, 0, 'd'},
{"device", 1, 0, 'f'},
{0, 0, 0, 0}
};
int main(int argc, char *argv[])
{
char *config = DEFAULT_CONFIG;
int code, arg = 0, debug = 0;
char *devicename = NULL, *filename = NULL;
int code, arg = 0;
do {
code = getopt_long(argc, argv, "c:dh", opts, &arg);
code = getopt_long(argc, argv, "d:f:", opts, &arg);
switch (code) {
case 'c': /* config */
config = optarg;
case 'd': devicename = optarg;
break;
case 'd': /* debug */
debug = 1;
case 'f': filename = optarg;
break;
case 'h': /* help */
printf("Usage: zyxel-revert [options]\n"
"Options: \n"
" --config -c configfile use this configfile\n"
" --debug -d do not fork and log to stderr\n"
" --help -h this help\n"
"\n");
printf("Usage: zyxel-revert -d <device> -f <file>\n");
exit(0);
break;
@ -56,18 +45,10 @@ int main(int argc, char *argv[])
}
} while (code != -1);
if (config_parse(config))
if (devicename == NULL || serial_init(devicename))
exit(1);
if (serial_init())
exit(1);
// if (network_init()) {
// context_close();
// exit(1);
// }
if (xmodem_init()) {
if (filename == NULL || xmodem_init(filename)) {
context_close();
exit(1);
}
@ -76,6 +57,5 @@ int main(int argc, char *argv[])
xmodem_close();
context_close();
return 0;
}

View File

@ -1,5 +0,0 @@
[global]
configdata 350LI2C1.rom
[ports]
serial /dev/ttyS0