Imported the generic "config" tool (to parse configuration files)

This commit is contained in:
Pierre Pronchery 2014-05-09 02:50:01 +02:00
parent b3121a94d1
commit 124c9769db
5 changed files with 168 additions and 2 deletions

View File

@ -1,6 +1,6 @@
PACKAGE = libSystem PACKAGE = libSystem
VERSION = 0.1.7 VERSION = 0.1.7
SUBDIRS = data doc include src tests SUBDIRS = data doc include src tests tools
PREFIX = /usr/local PREFIX = /usr/local
DESTDIR = DESTDIR =
MKDIR = mkdir -m 0755 -p MKDIR = mkdir -m 0755 -p
@ -98,6 +98,9 @@ dist:
$(PACKAGE)-$(VERSION)/tests/Makefile \ $(PACKAGE)-$(VERSION)/tests/Makefile \
$(PACKAGE)-$(VERSION)/tests/tests.sh \ $(PACKAGE)-$(VERSION)/tests/tests.sh \
$(PACKAGE)-$(VERSION)/tests/project.conf \ $(PACKAGE)-$(VERSION)/tests/project.conf \
$(PACKAGE)-$(VERSION)/tools/config.c \
$(PACKAGE)-$(VERSION)/tools/Makefile \
$(PACKAGE)-$(VERSION)/tools/project.conf \
$(PACKAGE)-$(VERSION)/Makefile \ $(PACKAGE)-$(VERSION)/Makefile \
$(PACKAGE)-$(VERSION)/COPYING \ $(PACKAGE)-$(VERSION)/COPYING \
$(PACKAGE)-$(VERSION)/config.sh \ $(PACKAGE)-$(VERSION)/config.sh \

View File

@ -1,7 +1,7 @@
package=libSystem package=libSystem
version=0.1.7 version=0.1.7
subdirs=data,doc,include,src,tests subdirs=data,doc,include,src,tests,tools
config=sh config=sh
dist=Makefile,COPYING,config.sh,README.md dist=Makefile,COPYING,config.sh,README.md

40
tools/Makefile Normal file
View File

@ -0,0 +1,40 @@
TARGETS = config
PREFIX = /usr/local
DESTDIR =
BINDIR = $(PREFIX)/bin
SBINDIR = $(PREFIX)/sbin
CC = cc
CPPFLAGSF=
CPPFLAGS=
CFLAGSF = -W
CFLAGS = -Wall -g -O2 -pedantic
LDFLAGSF= -Wl,-rpath,$(PREFIX)/lib -L../src -lSystem
RM = rm -f
LN = ln -f
MKDIR = mkdir -m 0755 -p
INSTALL = install
all: $(TARGETS)
config_OBJS = config.o
config_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS)
config_LDFLAGS = $(LDFLAGSF) $(LDFLAGS)
config: $(config_OBJS)
$(CC) -o config $(config_OBJS) $(config_LDFLAGS)
config.o: config.c
$(CC) $(config_CFLAGS) -c config.c
clean:
$(RM) -- $(config_OBJS)
distclean: clean
$(RM) -- $(TARGETS)
install: $(TARGETS)
uninstall:
.PHONY: all clean distclean install uninstall

114
tools/config.c Normal file
View File

@ -0,0 +1,114 @@
/* $Id$ */
/* Copyright (c) 2014 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS System libSystem */
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include "System/config.h"
#include "System/error.h"
#ifndef PROGNAME
# define PROGNAME "config"
#endif
/* config */
/* private */
/* prototypes */
static int _config(int verbose, char const * filename, char const * section,
char const * key);
static int _error(char const * progname, int ret);
static int _usage(void);
/* functions */
/* config */
static int _config(int verbose, char const * filename, char const * section,
char const * key)
{
int ret = 0;
Config * config;
char const * p;
if((config = config_new()) == NULL)
ret = _error(PROGNAME, 1);
if(config_load(config, filename) != 0)
ret = _error(PROGNAME, 1);
else if((p = config_get(config, section, key)) != NULL)
printf("%s%s%s%s%s\n",
(verbose && section != NULL) ? section : "",
(verbose && section != NULL) ? "." : "",
(verbose) ? key : "", (verbose) ? "=" : "", p);
else
ret = _error(PROGNAME, 1);
config_delete(config);
return ret;
}
/* error */
static int _error(char const * progname, int ret)
{
error_print(progname);
return ret;
}
/* usage */
static int _usage(void)
{
fputs("Usage: config [-v] -f filename [section.]key\n", stderr);
return 1;
}
/* public */
/* functions */
/* main */
int main(int argc, char * argv[])
{
int o;
char const * filename = NULL;
int verbose = 0;
char * section;
char * key;
while((o = getopt(argc, argv, "f:v")) != -1)
switch(o)
{
case 'f':
filename = optarg;
break;
case 'v':
verbose = 1;
break;
default:
return _usage();
}
if(filename == NULL || optind + 1 != argc)
return _usage();
section = argv[optind];
if((key = strchr(section, '.')) == NULL)
{
key = section;
section = NULL;
}
else
*(key++) = '\0';
return (_config(verbose, filename, section, key) == 0) ? 0 : 2;
}

9
tools/project.conf Normal file
View File

@ -0,0 +1,9 @@
targets=config
cflags_force=-W
cflags=-Wall -g -O2 -pedantic
ldflags_force=-Wl,-rpath,$(PREFIX)/lib -L../src -lSystem
dist=Makefile
[config]
type=binary
sources=config.c