[Libguestfs] [PATCH 1/2] builder: add an optional suffix string for INI parsing errors

Pino Toscano ptoscano at redhat.com
Tue Apr 22 15:41:43 UTC 2014


---
 builder/index-parse.y    | 6 ++++--
 builder/index-parser-c.c | 3 ++-
 builder/index-struct.h   | 1 +
 builder/ini_reader.ml    | 6 +++---
 builder/ini_reader.mli   | 2 +-
 5 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/builder/index-parse.y b/builder/index-parse.y
index 310870d..7ddef53 100644
--- a/builder/index-parse.y
+++ b/builder/index-parse.y
@@ -150,12 +150,14 @@ emptylines:
 void
 yyerror (YYLTYPE * yylloc, yyscan_t scanner, struct parse_context *context, const char *msg)
 {
-  fprintf (stderr, "%s%s%s%ssyntax error at line %d: %s\n",
+  fprintf (stderr, "%s%s%s%ssyntax error at line %d: %s%s%s\n",
            context->program_name ? context->program_name : "",
            context->program_name ? ": " : "",
            context->input_file ? context->input_file : "",
            context->input_file ? ": " : "",
-           yylloc->first_line, msg);
+           yylloc->first_line, msg,
+           context->error_suffix ? " " : "",
+           context->error_suffix ? context->error_suffix : "");
 }
 
 int
diff --git a/builder/index-parser-c.c b/builder/index-parser-c.c
index 5dcc82f..099bdf8 100644
--- a/builder/index-parser-c.c
+++ b/builder/index-parser-c.c
@@ -46,7 +46,7 @@ extern void unix_error (int errcode, char * cmdname, value arg) Noreturn;
 extern int do_parse (struct parse_context *context, FILE *in);
 
 value
-virt_builder_parse_index (value progv, value filenamev)
+virt_builder_parse_index (value progv, value error_suffixv, value filenamev)
 {
   CAMLparam2 (progv, filenamev);
   CAMLlocal5 (rv, v, sv, sv2, fv);
@@ -58,6 +58,7 @@ virt_builder_parse_index (value progv, value filenamev)
   parse_context_init (&context);
   context.program_name = String_val (progv);
   context.input_file = String_val (filenamev);
+  context.error_suffix = String_val (error_suffixv);
 
   in = fopen (String_val (filenamev), "r");
   if (in == NULL)
diff --git a/builder/index-struct.h b/builder/index-struct.h
index 3edd06d..150535d 100644
--- a/builder/index-struct.h
+++ b/builder/index-struct.h
@@ -45,6 +45,7 @@ struct parse_context {
   int seen_comments;
   const char *input_file;
   const char *program_name;
+  const char *error_suffix;
 };
 
 /* Initialize the content of a parse_context. */
diff --git a/builder/ini_reader.ml b/builder/ini_reader.ml
index 68e3863..c64125c 100644
--- a/builder/ini_reader.ml
+++ b/builder/ini_reader.ml
@@ -27,10 +27,10 @@ and c_section = string * c_fields             (* [name] + fields *)
 and c_fields = field array
 
 (* Calls yyparse in the C code. *)
-external parse_index : prog:string -> string -> c_sections = "virt_builder_parse_index"
+external parse_index : prog:string -> error_suffix:string -> string -> c_sections = "virt_builder_parse_index"
 
-let read_ini ~prog file =
-  let sections = parse_index ~prog file in
+let read_ini ~prog ?(error_suffix = "") file =
+  let sections = parse_index ~prog ~error_suffix file in
   let sections = Array.to_list sections in
   List.map (
     fun (n, fields) ->
diff --git a/builder/ini_reader.mli b/builder/ini_reader.mli
index ac3bebe..1b8e894 100644
--- a/builder/ini_reader.mli
+++ b/builder/ini_reader.mli
@@ -21,4 +21,4 @@ and section = string * fields                (* [name] + fields *)
 and fields = field list
 and field = string * string option * string  (* key + subkey + value *)
 
-val read_ini : prog:string -> string -> sections
+val read_ini : prog:string -> ?error_suffix:string -> string -> sections
-- 
1.9.0




More information about the Libguestfs mailing list