diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-05-05 14:09:48 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-05-05 14:39:29 +0300 |
commit | 6c3f6c1b02dd5f9ac343c713bdae4aa83bafc328 (patch) | |
tree | 49bc8df1cff7157c711c63447c3e272ebc67cd4c /src | |
parent | a60eb4b18345626a84e23784d77ca231812e1dff (diff) | |
download | grecs-6c3f6c1b02dd5f9ac343c713bdae4aa83bafc328.tar.gz grecs-6c3f6c1b02dd5f9ac343c713bdae4aa83bafc328.tar.bz2 |
Improve node formatting. Add version comparasion functions.
* am/grecs.m4 (GRECS_SETUP): New option: shared.
* doc/GRECS_SETUP.3: Document new options.
* doc/grecs_format_locus.3: Update.
* doc/grecs_format_node.3: Document new flags.
* src/.gitignore: Update.
* src/version.c: New file.
* src/Make.am (GRECS_SRC): Add version.c
(EXTRA_DIST): Update.
* src/diag.c (default_print_diag): Flush stdout as per the docs.
* src/format.c (grecs_format_locus): Don't print trailing semicolon.
(grecs_format_value): Handle GRECS_NODE_FLAG_NOQUOTE flag.
(grecs_format_node): Print delimiters when needed.
* src/grecs.h (grecs_version_info): New struct.
(grecs_version, grecs_version_cmp): New protos.
(GRECS_NODE_FLAG_NOQUOTE): New flag.
* tests/gcfver.c: New file.
* tests/vercmp.at: New file.
* tests/.gitignore: Update.
* tests/Makefile.am: Define GRECS_VERCMP_AT in package.m4
(TESTSUITE_AT): Add vercmp.at (conditionally).
(noinst_PROGRAMS): Add gcfver.
* tests/testsuite.at: Conditionally include vercmp.at.
Diffstat (limited to 'src')
-rw-r--r-- | src/.gitignore | 3 | ||||
-rw-r--r-- | src/Make-shared.am | 18 | ||||
-rw-r--r-- | src/Make-static.am (renamed from src/Make-noinst.am) | 0 | ||||
-rw-r--r-- | src/Make.am | 3 | ||||
-rw-r--r-- | src/diag.c | 1 | ||||
-rw-r--r-- | src/format.c | 18 | ||||
-rw-r--r-- | src/grecs.h | 15 | ||||
-rw-r--r-- | src/version.c | 86 |
8 files changed, 136 insertions, 8 deletions
diff --git a/src/.gitignore b/src/.gitignore index bf5d140..9ef1b57 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -2,5 +2,6 @@ grecs-gram.c grecs-gram.h grecs-gram.output grecs-lex.c -Make-noinst.in Make-inst.in +Make-shared.in +Make-static.in diff --git a/src/Make-shared.am b/src/Make-shared.am new file mode 100644 index 0000000..bbf25d4 --- /dev/null +++ b/src/Make-shared.am @@ -0,0 +1,18 @@ +# This file is part of grecs - Gray's Extensible Configuration System +# Copyright (C) 2007, 2009-2011 Sergey Poznyakoff +# +# Grecs 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 3, or (at your option) +# any later version. +# +# Grecs 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 Grecs. If not, see <http://www.gnu.org/licenses/>. +include Make.am +noinst_LT_LIBRARIES=libgrecs.la +libgrecs_la_SOURCES = $(GRECS_SRC) diff --git a/src/Make-noinst.am b/src/Make-static.am index bf4f7ce..bf4f7ce 100644 --- a/src/Make-noinst.am +++ b/src/Make-static.am diff --git a/src/Make.am b/src/Make.am index e655767..fb3b80b 100644 --- a/src/Make.am +++ b/src/Make.am @@ -27,10 +27,11 @@ GRECS_SRC = \ text.c\ tree.c\ grecs.h\ + version.c\ wordsplit.c\ wordsplit.h -EXTRA_DIST=grecs-gram.h $(PP_SETUP_FILE) Make.am Make-inst.am Make-noinst.am +EXTRA_DIST=grecs-gram.h $(PP_SETUP_FILE) Make.am Make-inst.am Make-shared.am Make-static.am INCLUDES = -I$(srcdir) @GRECS_INCLUDES@ AM_YFLAGS = -dtv @@ -26,6 +26,7 @@ static void default_print_diag(grecs_locus_t *locus, int err, int errcode, const char *msg) { + fflush(stdout); if (locus) fprintf(stderr, "%s:%d: ", locus->file, locus->line); if (!err) diff --git a/src/format.c b/src/format.c index 2c97b72..6e33521 100644 --- a/src/format.c +++ b/src/format.c @@ -188,7 +188,7 @@ void grecs_format_locus(grecs_locus_t *locus, FILE *fp) { if (locus) - fprintf(fp, "%s:%d:", locus->file, locus->line); + fprintf(fp, "%s:%d", locus->file, locus->line); } void @@ -222,6 +222,8 @@ grecs_format_value(struct grecs_value *val, int flags, FILE *fp) &need_quote); if (flags & GRECS_NODE_FLAG_QUOTE) need_quote = 1; + else if (flags & GRECS_NODE_FLAG_NOQUOTE) + need_quote = 0; if (need_quote) { char *cbuf = grecs_malloc(clen + 1); wordsplit_c_quote_copy(cbuf, val->v.string, @@ -257,6 +259,8 @@ grecs_format_value(struct grecs_value *val, int flags, FILE *fp) void grecs_format_node(struct grecs_node *node, int flags, FILE *fp) { + const char *delim_str = NULL; + if (!flags) flags = GRECS_NODE_FLAG_DEFAULT; switch (node->type) { @@ -271,15 +275,19 @@ grecs_format_node(struct grecs_node *node, int flags, FILE *fp) case grecs_node_stmt: if (flags & GRECS_NODE_FLAG_LOCUS) { grecs_format_locus(&node->locus, fp); - fputc(' ', fp); + delim_str = ": "; } if (flags & GRECS_NODE_FLAG_PATH) { + if (delim_str) + fprintf(fp, "%s", delim_str); grecs_format_node_path(node, flags, fp); - fputc(':', fp); - fputc(' ', fp); + delim_str = ": "; } - if (flags & GRECS_NODE_FLAG_VALUE) + if (flags & GRECS_NODE_FLAG_VALUE) { + if (delim_str) + fprintf(fp, "%s", delim_str); grecs_format_value(&node->value, flags, fp); + } } } diff --git a/src/grecs.h b/src/grecs.h index c408aa0..b6ac85c 100644 --- a/src/grecs.h +++ b/src/grecs.h @@ -37,6 +37,15 @@ # define N_(s) s #endif +struct grecs_version_info { + const char *package; + const char *version; + int major; + int minor; + int patch; + char *suffix; +}; + typedef struct { char *file; int line; @@ -149,6 +158,9 @@ struct grecs_sockaddr { struct sockaddr *sa; }; +void grecs_version(struct grecs_version_info *pv); +int grecs_version_cmp(const char *vstr); + extern void *(*grecs_malloc_fun)(size_t size); extern void *(*grecs_realloc_fun)(void *ptr, size_t size); extern void (*grecs_alloc_die_fun)(void); @@ -236,7 +248,8 @@ void grecs_format_value(struct grecs_value *val, int flags, FILE *fp); #define GRECS_NODE_FLAG_PATH 0x0200 #define GRECS_NODE_FLAG_VALUE 0x0400 #define GRECS_NODE_FLAG_QUOTE 0x0800 -#define GRECS_NODE_FLAG_QUOTE_HEX 0x1000 +#define GRECS_NODE_FLAG_NOQUOTE 0x1000 +#define GRECS_NODE_FLAG_QUOTE_HEX 0x2000 #define GRECS_NODE_FLAG_DEFAULT \ (GRECS_NODE_FLAG_PATH|GRECS_NODE_FLAG_VALUE|GRECS_NODE_FLAG_QUOTE) void grecs_format_node(struct grecs_node *node, int flags, FILE *fp); diff --git a/src/version.c b/src/version.c new file mode 100644 index 0000000..d34b8b5 --- /dev/null +++ b/src/version.c @@ -0,0 +1,86 @@ +/* grecs - Gray's Extensible Configuration System + Copyright (C) 2007-2011 Sergey Poznyakoff + + Grecs 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 3 of the License, or (at your + option) any later version. + + Grecs 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 Grecs. If not, see <http://www.gnu.org/licenses/>. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif +#include <grecs.h> +#include <string.h> +#include <ctype.h> + +void +grecs_version(struct grecs_version_info *pv) +{ + char *p; + memset(pv, 0, sizeof(*pv)); + pv->package = PACKAGE_NAME; + pv->version = PACKAGE_VERSION; + pv->major = strtoul(pv->version, &p, 10); + if (*p && *p == '.') { + pv->minor = strtoul(p + 1, &p, 10); + if (*p && *p == '.') { + char *q; + + pv->patch = strtoul(p + 1, &q, 10); + if (q == p + 1) + pv->patch = 0; + else + p = q; + } + } + pv->suffix = p; +} + +int +grecs_version_cmp(const char *vstr) +{ + struct grecs_version_info v; + char *p; + unsigned long n; + size_t len; + int check; + + if (!vstr) + return -1; + grecs_version(&v); + len = strcspn(vstr, " \t"); + if (vstr[len]) { + if (len != strlen(v.package) || memcmp(v.package, vstr, len)) + return 1; + for (vstr += len; *vstr && isspace(*vstr); vstr++) + ; + } + if (!*vstr) + return 1; + n = strtoul(vstr, &p, 10); + if (n > v.major) + return 1; + check = n == v.major; + if (*p && *p == '.') { + n = strtoul(p + 1, &p, 10); + if (check && n > v.minor) + return 1; + check = n == v.minor; + if (*p && *p == '.') { + n = strtoul(p + 1, &p, 10); + if (check && n > v.patch) + return 1; + } + } + if (*p && (!v.suffix || strcmp(p, v.suffix))) + return 1; + return 0; +} |