git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Subject: [PATCH 3/4] config: support parsing config data from buffers
Date: Thu, 26 Jan 2012 02:40:31 -0500	[thread overview]
Message-ID: <20120126074031.GC30474@sigill.intra.peff.net> (raw)
In-Reply-To: <20120126073547.GA28689@sigill.intra.peff.net>

The only two ways to parse config data are from a file or
from the command-line. Because the command-line format is
totally different from the file format, they don't share any
code. Therefore, to add new sources of file-like config data,
we have to refactor git_parse_file to handle reading from
something besides stdio.

To fix this, our config_file structure now holds either a
"FILE *" pointer or a memory buffer. We intercept calls to
fgetc and ungetc and either pass them along to stdio, or
fake them with our buffer. This leaves the main parsing code
intact and easy to read.

Signed-off-by: Jeff King <peff@peff.net>
---
 cache.h  |    1 +
 config.c |   58 +++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 54 insertions(+), 5 deletions(-)

diff --git a/cache.h b/cache.h
index 21bbb0a..a298897 100644
--- a/cache.h
+++ b/cache.h
@@ -1110,6 +1110,7 @@ extern int update_server_info(int);
 typedef int (*config_fn_t)(const char *, const char *, void *);
 extern int git_default_config(const char *, const char *, void *);
 extern int git_config_from_file(config_fn_t fn, const char *, void *);
+extern int git_config_from_buffer(config_fn_t fn, void *, const char *, char *, unsigned long );
 extern void git_config_push_parameter(const char *text);
 extern int git_config_from_parameters(config_fn_t fn, void *data);
 extern int git_config(config_fn_t fn, void *);
diff --git a/config.c b/config.c
index b82f749..49a3d1a 100644
--- a/config.c
+++ b/config.c
@@ -18,6 +18,9 @@ typedef struct config_file {
 	const char *name;
 	int linenr;
 	int eof;
+	char *buf;
+	unsigned long size;
+	unsigned long cur;
 	struct strbuf value;
 	char var[MAXNAME];
 } config_file;
@@ -101,19 +104,45 @@ int git_config_from_parameters(config_fn_t fn, void *data)
 	return nr > 0;
 }
 
+static int get_one_char(void)
+{
+	if (cf->f)
+		return fgetc(cf->f);
+	else if (cf->buf) {
+		if (cf->cur < cf->size)
+			return cf->buf[cf->cur++];
+		return EOF;
+	}
+
+	die("BUG: attempt to read from NULL config_file");
+}
+
+static int unget_one_char(int c)
+{
+	if (cf->f)
+		ungetc(c, cf->f);
+	else if (cf->buf) {
+		if (cf->cur == 0)
+			return EOF;
+		cf->buf[--cf->cur] = c;
+		return c;
+	}
+
+	die("BUG: attempt to ungetc NULL config_file");
+}
+
 static int get_next_char(void)
 {
 	int c;
-	FILE *f;
 
 	c = '\n';
-	if (cf && ((f = cf->f) != NULL)) {
-		c = fgetc(f);
+	if (cf && (cf->f || cf->buf)) {
+		c = get_one_char();
 		if (c == '\r') {
 			/* DOS like systems */
-			c = fgetc(f);
+			c = get_one_char();
 			if (c != '\n') {
-				ungetc(c, f);
+				unget_one_char(c);
 				c = '\r';
 			}
 		}
@@ -833,6 +862,9 @@ static void config_file_push(config_file *top, const char *name)
 	top->name = name;
 	top->linenr = 1;
 	top->eof = 0;
+	top->buf = NULL;
+	top->size = 0;
+	top->cur = 0;
 	strbuf_init(&top->value, 1024);
 	cf = top;
 }
@@ -863,6 +895,22 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
 	return ret;
 }
 
+int git_config_from_buffer(config_fn_t fn, void *data, const char *name,
+			   char *buf, unsigned long size)
+{
+	int ret;
+	config_file top;
+
+	config_file_push(&top, name);
+	top.buf = buf;
+	top.size = size;
+
+	ret = git_parse_file(fn, data);
+
+	config_file_pop(&top);
+	return ret;
+}
+
 const char *git_etc_gitconfig(void)
 {
 	static const char *system_wide;
-- 
1.7.9.rc2.293.gaae2

  parent reply	other threads:[~2012-01-26  7:40 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-26  7:35 [RFC/PATCH 0/4] config include directives Jeff King
2012-01-26  7:37 ` [PATCH 1/4] config: add include directive Jeff King
2012-01-26  9:16   ` Johannes Sixt
2012-01-26 16:54     ` Jeff King
2012-01-26 20:42       ` Junio C Hamano
2012-01-26 22:25         ` Jeff King
2012-01-26 22:43           ` Jeff King
2012-01-26 20:58   ` Junio C Hamano
2012-01-26 22:51     ` Jeff King
2012-01-27  5:23       ` Junio C Hamano
2012-01-27  5:55         ` Jeff King
2012-01-27 17:03       ` Jens Lehmann
2012-01-27  0:02   ` Ævar Arnfjörð Bjarmason
2012-01-27  0:32     ` Jeff King
2012-01-27  9:33       ` Ævar Arnfjörð Bjarmason
2012-01-27  5:07   ` Michael Haggerty
2012-01-27  5:54     ` Jeff King
2012-01-26  7:38 ` [PATCH 2/4] config: factor out config file stack management Jeff King
2012-01-26  7:40 ` Jeff King [this message]
2012-01-26  7:42 ` [PATCH 4/4] config: allow including config from repository blobs Jeff King
2012-01-26  9:25   ` Johannes Sixt
2012-01-26 17:22     ` Jeff King
2012-01-27  3:47     ` Nguyen Thai Ngoc Duy
2012-01-27  5:57       ` Jeff King
2012-01-26 21:14   ` Junio C Hamano
2012-01-26 23:00     ` Jeff King
2012-01-27  0:35       ` Junio C Hamano
2012-01-27  0:49         ` Jeff King
2012-01-27  5:30           ` Junio C Hamano
2012-01-27  5:42             ` Jeff King
2012-01-27  7:27               ` Johannes Sixt
2012-01-27 23:10                 ` Junio C Hamano
2012-01-27  4:01   ` Nguyen Thai Ngoc Duy
2012-01-27  5:59     ` Jeff King
2012-01-27  9:51 ` [RFC/PATCH 0/4] config include directives Ævar Arnfjörð Bjarmason
2012-01-27 17:34   ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120126074031.GC30474@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).