aboutsummaryrefslogtreecommitdiff
path: root/src/format.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2011-05-03 21:06:47 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2011-05-03 21:23:25 +0300
commited838ca0467f7cc9745b042099b568cdf0f2b835 (patch)
tree1ffa754e73507d31ece0151e68db25a4ecb81592 /src/format.c
parent3d679b3df641f59fb81ca1651799f4e2965ed67e (diff)
downloadgrecs-ed838ca0467f7cc9745b042099b568cdf0f2b835.tar.gz
grecs-ed838ca0467f7cc9745b042099b568cdf0f2b835.tar.bz2
Various impovements.
* am/grecs.m4 (GRECS_SETUP): New flags: getopt and git2chg. * src/format.c (grecs_format_locus): Ignore NULL loci. (grecs_format_node_ident): Rename to grecs_format_node_path. Change semantics of the second argument. (grecs_format_value): Change signature (take flags). Correctly quote string values. * src/grecs.h: Protect the contents with #ifndef _GRECS_H. (GRECS_AGGR): New flag (for future use). (grecs_node) <prev>: New member. (grecs_format_value): Change signature. (grecs_format_node_ident): Rename to grecs_format_node_path. (GRECS_NODE_FLAG_PATH,GRECS_NODE_FLAG_VALUE) (GRECS_NODE_FLAG_QUOTE,GRECS_NODE_FLAG_QUOTE_HEX) (GRECS_NODE_FLAG_DEFAULT): New flags. (grecs_node_from_path): New proto. * src/lookup.c (grecs_node_from_path): New function. * src/tree.c (grecs_node_bind): Keep track of node->prev.
Diffstat (limited to 'src/format.c')
-rw-r--r--src/format.c53
1 files changed, 36 insertions, 17 deletions
diff --git a/src/format.c b/src/format.c
index 11b405a..96cdb0e 100644
--- a/src/format.c
+++ b/src/format.c
@@ -22,6 +22,7 @@
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
+#include "wordsplit.h"
const char *
grecs_data_type_string(enum grecs_data_type type)
@@ -186,38 +187,55 @@ grecs_format_statement_array(struct grecs_keyword *kwp,
void
grecs_format_locus(grecs_locus_t *locus, FILE *fp)
{
- fprintf(fp, "%s:%d:", locus->file, locus->line);
+ if (locus)
+ fprintf(fp, "%s:%d:", locus->file, locus->line);
}
void
-grecs_format_node_ident(struct grecs_node *node, int delim, FILE *fp)
+grecs_format_node_path(struct grecs_node *node, int flags, FILE *fp)
{
+ int delim = flags & 0xff;
if (node->up)
- grecs_format_node_ident(node->up, delim, fp);
- fputc(delim, fp);
+ grecs_format_node_path(node->up, flags, fp);
+ fputc(delim ? delim : '.', fp);
fprintf(fp, "%s", node->ident);
if (node->type == grecs_node_block &&
!GRECS_VALUE_EMPTY_P(&node->value)) {
fputc('=', fp);
- grecs_format_value(&node->value, fp);
+ grecs_format_value(&node->value, flags|GRECS_NODE_FLAG_QUOTE,
+ fp);
}
}
void
-grecs_format_value(struct grecs_value *val, FILE *fp)
+grecs_format_value(struct grecs_value *val, int flags, FILE *fp)
{
int i;
struct grecs_list_entry *ep;
+ size_t clen;
+ int need_quote;
switch (val->type) {
case GRECS_TYPE_STRING:
- fprintf(fp, "\"%s\"", val->v.string); /*FIXME: Quoting*/
+ clen = wordsplit_c_quoted_length(val->v.string,
+ flags & GRECS_NODE_FLAG_QUOTE_HEX,
+ &need_quote);
+ if (flags & GRECS_NODE_FLAG_QUOTE)
+ need_quote = 1;
+ if (need_quote) {
+ char *cbuf = grecs_malloc(clen + 1);
+ wordsplit_c_quote_copy(cbuf, val->v.string,
+ flags & GRECS_NODE_FLAG_QUOTE_HEX);
+ fprintf(fp, "\"%s\"", cbuf);
+ free(cbuf);
+ } else
+ fprintf(fp, "%s", val->v.string);
break;
case GRECS_TYPE_LIST:
fputc('(', fp);
for (ep = val->v.list->head; ep; ep = ep->next) {
- grecs_format_value(ep->data, fp);
+ grecs_format_value(ep->data, flags, fp);
if (ep->next) {
fputc(',', fp);
fputc(' ', fp);
@@ -230,7 +248,7 @@ grecs_format_value(struct grecs_value *val, FILE *fp)
for (i = 0; i < val->v.arg.c; i++) {
if (i)
fputc(' ', fp);
- grecs_format_value(&val->v.arg.v[i], fp);
+ grecs_format_value(&val->v.arg.v[i], flags, fp);
}
}
}
@@ -238,10 +256,8 @@ grecs_format_value(struct grecs_value *val, FILE *fp)
void
grecs_format_node(struct grecs_node *node, int flags, FILE *fp)
{
- int delim = flags & 0xff;
-
- if (!delim)
- delim = '.';
+ if (!flags)
+ flags = GRECS_NODE_FLAG_DEFAULT;
switch (node->type) {
case grecs_node_block:
for (node = node->down; node; node = node->next) {
@@ -256,10 +272,13 @@ grecs_format_node(struct grecs_node *node, int flags, FILE *fp)
grecs_format_locus(&node->locus, fp);
fputc(' ', fp);
}
- grecs_format_node_ident(node, delim, fp);
- fputc(':', fp);
- fputc(' ', fp);
- grecs_format_value(&node->value, fp);
+ if (flags & GRECS_NODE_FLAG_PATH) {
+ grecs_format_node_path(node, flags, fp);
+ fputc(':', fp);
+ fputc(' ', fp);
+ }
+ if (flags & GRECS_NODE_FLAG_VALUE)
+ grecs_format_value(&node->value, flags, fp);
}
}

Return to:

Send suggestions and report system problems to the System administrator.