The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Sam Ravnborg <sam@ravnborg.org>
To: Roman Zippel <zippel@linux-m68k.org>, linux-kernel@vger.kernel.org
Subject: kconfig: avoid temporary file
Date: Fri, 31 Dec 2004 00:52:16 +0100	[thread overview]
Message-ID: <20041230235216.GB9450@mars.ravnborg.org> (raw)
In-Reply-To: <20041230235146.GA9450@mars.ravnborg.org>

# This is a BitKeeper generated diff -Nru style patch.
#
# ChangeSet
#   2004/12/30 22:30:10+01:00 sam@mars.ravnborg.org 
#   kconfig: avoid temporary file
#   
#   Use a growable string when building up result of searching - avoiding an additional
#   temporary file. A temporary file is still used but on a higher level.
#   
#   Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
# 
# scripts/kconfig/mconf.c
#   2004/12/30 22:29:51+01:00 sam@mars.ravnborg.org +78 -28
#   introduce a general growable string.
#   Allow us to skip one additional temporary file
# 
diff -Nru a/scripts/kconfig/mconf.c b/scripts/kconfig/mconf.c
--- a/scripts/kconfig/mconf.c	2004-12-31 00:45:13 +01:00
+++ b/scripts/kconfig/mconf.c	2004-12-31 00:45:13 +01:00
@@ -104,7 +104,6 @@
 static void show_help(struct menu *menu);
 static void show_readme(void);
 static void show_file(const char *filename, const char *title, int r, int c);
-static void show_expr(struct menu *menu, FILE *fp);
 static void search_conf(char *pattern);
 static int regex_match(const char *string, regex_t *re);
 
@@ -113,6 +112,53 @@
 static void cprint_done(void);
 static int cprint(const char *fmt, ...);
 
+/* Growable string. Allocates memory as needed when string expands */
+struct gstr {
+	char *s;
+	size_t len;
+};
+
+static struct gstr str_init(void)
+{
+	struct gstr gs;
+	gs.s = malloc(sizeof(char) * 16);
+	strcpy(gs.s, "\0");
+	gs.len = 16;
+	return gs;
+}
+
+static void str_del(struct gstr *gs)
+{
+	if (gs->len && gs->s)
+		free(gs->s);
+	gs->s = NULL;
+	gs->len = 0;
+}
+
+static void str_add(struct gstr *gs, const char *s)
+{
+	size_t l = strlen(gs->s) + strlen(s) + 1;
+	if (l >= gs->len)
+		gs->s = realloc(gs->s, l);
+	gs->len = l;
+	strcat(gs->s, s);
+}
+
+static void str_printf(struct gstr *gs, const char *fmt, ...)
+{
+	va_list ap;
+	char s[1024];
+	va_start(ap, fmt);
+	vsnprintf(s, sizeof(s), fmt, ap);
+	str_add(gs, s);
+	va_end(ap);
+}
+
+static const char *str_get(struct gstr *gs)
+{
+	return gs->s;
+}
+
 static void init_wsize(void)
 {
 	struct winsize ws;
@@ -289,36 +335,46 @@
 	return 1;
 }
 
-static void show_expr(struct menu *menu, FILE *fp)
+static void expr_print_str_helper(void *data, const char *str)
+{
+	str_add((struct gstr*)data, str);
+}
+
+static void expr_str(struct expr *e, struct gstr *gs)
+{
+	expr_print(e, expr_print_str_helper, gs, E_NONE);
+}
+
+static void show_expr(struct menu *menu, struct gstr *gs)
 {
 	bool hit = false;
-	fprintf(fp, "Depends:\n ");
+	str_add(gs, "Depends:\n ");
 	if (menu->prompt->visible.expr) {
 		if (!hit)
 			hit = true;
-		expr_fprint(menu->prompt->visible.expr, fp);
+		expr_str(menu->prompt->visible.expr, gs);
 	}
 	if (!hit)
-		fprintf(fp, "None");
+		str_add(gs, "None");
 	if (menu->sym) {
 		struct property *prop;
 		hit = false;
-		fprintf(fp, "\nSelects:\n ");
+		str_add(gs, "\nSelects:\n ");
 		for_all_properties(menu->sym, prop, P_SELECT) {
 			if (!hit)
 				hit = true;
-			expr_fprint(prop->expr, fp);
+			expr_str(prop->expr, gs);
 		}
 		if (!hit)
-			fprintf(fp, "None");
+			str_add(gs, "None");
 		hit = false;
-		fprintf(fp, "\nSelected by:\n ");
+		str_add(gs, "\nSelected by:\n ");
 		if (menu->sym->rev_dep.expr) {
 			hit = true;
-			expr_fprint(menu->sym->rev_dep.expr, fp);
+			expr_str(menu->sym->rev_dep.expr, gs);
 		}
 		if (!hit)
-			fprintf(fp, "None");
+			str_add(gs, "None");
 	}
 }
 
@@ -327,19 +383,13 @@
 	struct symbol *sym = NULL;
 	struct menu *menu[32] = { 0 };
 	struct property *prop = NULL;
-	FILE *fp = NULL;
+	struct gstr str = str_init();
 	bool hit = false;
 	int i, j, k, l;
 	regex_t re;
 
 	if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB))
 		return;
-
-	fp = fopen(".search.tmp", "w");
-	if (fp == NULL) {
-		perror("fopen");
-		return;
-	}
 	for_all_symbols(i, sym) {
 		if (!sym->name)
 			continue;
@@ -358,33 +408,33 @@
 			if (j > 0) {
 				if (!hit)
 					hit = true;
-				fprintf(fp, "%s (%s)\n", prop->text, sym->name);
-				fprintf(fp, "Location:\n");
+				str_printf(&str, "%s (%s)\n",
+				           prop->text, sym->name);
+				str_add(&str, "Location:\n");
 			}
 			for (k = j-2, l=1; k > 0; k--, l++) {
 				const char *prompt = menu_get_prompt(menu[k]);
 				if (menu[k]->sym)
-					fprintf(fp, "%*c-> %s (%s)\n",
+					str_printf(&str, "%*c-> %s (%s)\n",
 								l, ' ',
 								prompt,
 								menu[k]->sym->name);
 				else
-					fprintf(fp, "%*c-> %s\n",
+					str_printf(&str, "%*c-> %s\n",
 								l, ' ',
 								prompt);
 			}
 			if (hit) {
-				show_expr(menu[0], fp);
-				fprintf(fp, "\n\n\n");
+				show_expr(menu[0], &str);
+				str_add(&str, "\n\n\n");
 			}
 		}
 	}
 	if (!hit)
-		fprintf(fp, "No matches found.");
+		str_add(&str, "No matches found.");
 	regfree(&re);
-	fclose(fp);
-	show_file(".search.tmp", "Search Results", rows, cols);
-	unlink(".search.tmp");
+	show_textbox("Search Results", str_get(&str), rows, cols);
+	str_del(&str);
 }
 
 static void build_conf(struct menu *menu)

  reply	other threads:[~2004-12-30 23:52 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-30 23:51 kconfig: Sam Ravnborg
2004-12-30 23:52 ` Sam Ravnborg [this message]
2005-01-03  0:55   ` kconfig: avoid temporary file Roman Zippel
2005-01-03  5:10     ` Sam Ravnborg
2005-01-05 12:40       ` Roman Zippel
2005-01-05 18:14         ` Sam Ravnborg
2005-01-06 14:53           ` Roman Zippel
2004-12-30 23:52 ` kconfig: remove noise from show_expr Sam Ravnborg
2004-12-30 23:53 ` kconfig: help includes dependency information Sam Ravnborg
2005-01-01  4:02   ` Ingo Oeser
2005-01-02 20:16     ` Sam Ravnborg
2005-01-03 19:24       ` Ingo Oeser
2005-01-03  0:34 ` kconfig: Roman Zippel
2005-01-03  5:11   ` kconfig: Sam Ravnborg

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=20041230235216.GB9450@mars.ravnborg.org \
    --to=sam@ravnborg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=zippel@linux-m68k.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