diff --git a/Makefile b/Makefile index fc88f41..71c5168 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ PACKAGE = libSystem VERSION = 0.1.7 -SUBDIRS = data doc include src tests +SUBDIRS = data doc include src tests tools PREFIX = /usr/local DESTDIR = MKDIR = mkdir -m 0755 -p @@ -98,6 +98,9 @@ dist: $(PACKAGE)-$(VERSION)/tests/Makefile \ $(PACKAGE)-$(VERSION)/tests/tests.sh \ $(PACKAGE)-$(VERSION)/tests/project.conf \ + $(PACKAGE)-$(VERSION)/tools/config.c \ + $(PACKAGE)-$(VERSION)/tools/Makefile \ + $(PACKAGE)-$(VERSION)/tools/project.conf \ $(PACKAGE)-$(VERSION)/Makefile \ $(PACKAGE)-$(VERSION)/COPYING \ $(PACKAGE)-$(VERSION)/config.sh \ diff --git a/project.conf b/project.conf index df8a4a8..831a938 100644 --- a/project.conf +++ b/project.conf @@ -1,7 +1,7 @@ package=libSystem version=0.1.7 -subdirs=data,doc,include,src,tests +subdirs=data,doc,include,src,tests,tools config=sh dist=Makefile,COPYING,config.sh,README.md diff --git a/tools/Makefile b/tools/Makefile new file mode 100644 index 0000000..f0b0022 --- /dev/null +++ b/tools/Makefile @@ -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 diff --git a/tools/config.c b/tools/config.c new file mode 100644 index 0000000..38fd62c --- /dev/null +++ b/tools/config.c @@ -0,0 +1,114 @@ +/* $Id$ */ +/* Copyright (c) 2014 Pierre Pronchery */ +/* 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 . */ + + + +#include +#include +#include +#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; +} diff --git a/tools/project.conf b/tools/project.conf new file mode 100644 index 0000000..817d699 --- /dev/null +++ b/tools/project.conf @@ -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