[libgda/LIBGDA_4.2] Gda-Sql: handle "shebang" calling



commit 8cbcaabd5ea61bf49dc27bdb4c766c797093d70e
Author: Vivien Malerba <malerba gnome-db org>
Date:   Thu May 12 19:01:48 2011 +0200

    Gda-Sql: handle "shebang" calling

 tools/gda-sql.1.in |    8 ++++++++
 tools/gda-sql.c    |   28 ++++++++++++++++++++++++++--
 2 files changed, 34 insertions(+), 2 deletions(-)
---
diff --git a/tools/gda-sql.1.in b/tools/gda-sql.1.in
index fa31965..e18619b 100644
--- a/tools/gda-sql.1.in
+++ b/tools/gda-sql.1.in
@@ -36,6 +36,14 @@ Alternatively, input can be from a file.
 In addition, it provides a number of meta-commands and various shell-like features to facilitate writing
 scripts and automating a wide variety of tasks.
 
+.PP
+It is also possible to run the tool as a script using the classic '#!' string at the start of a script file,
+with the limitation that behaviour of arguments passed on the line after the '#!' command is undefined.
+Example:
+
+\fB#!/bin/path/to/gda-sql\fP
+
+\fB#!/usr/bin/env gda-sql\fP
 
 
 .SH OPTIONS
diff --git a/tools/gda-sql.c b/tools/gda-sql.c
index f7d094c..2bce6c1 100644
--- a/tools/gda-sql.c
+++ b/tools/gda-sql.c
@@ -303,6 +303,32 @@ main (int argc, char *argv[])
 
 		if (*argv[i] == '-')
 			continue;
+#define SHEBANG "#!"
+#define SHEBANGLEN 2
+
+		/* try to see if argv[i] corresponds to a file starting with SHEBANG */
+		FILE *file;
+		file = fopen (argv[i], "r");
+		if (file) {
+			char buffer [SHEBANGLEN + 1];
+			size_t nread;
+			nread = fread (buffer, 1, SHEBANGLEN, file);
+			if (nread == SHEBANGLEN) {
+				buffer [SHEBANGLEN] = 0;
+				if (!strcmp (buffer, SHEBANG)) {
+					if (! set_input_file (argv[i], &error)) {
+						g_print ("Can't read file '%s': %s\n", commandsfile,
+							 error->message);
+						exit_status = EXIT_FAILURE;
+						fclose (file);
+						goto cleanup;
+					}
+					fclose (file);
+					continue;
+				}
+			}
+			fclose (file);
+		}
 
                 info = gda_config_get_dsn_info (argv[i]);
 		if (info)
@@ -1812,9 +1838,7 @@ data_model_to_string (SqlConsole *console, GdaDataModel *model)
 		meta = xmlNewChild (header, NULL, BAD_CAST "meta", NULL);
 		xmlSetProp (meta, BAD_CAST "http-equiv", BAD_CAST "content-type");
 		xmlSetProp (meta, BAD_CAST "content", BAD_CAST "text/html; charset=UTF-8");
-
 		div = xmlNewChild (top, NULL, BAD_CAST "body", NULL);
-
 		table = xmlNewChild (div, NULL, BAD_CAST "table", NULL);
 		xmlSetProp (table, BAD_CAST "border", BAD_CAST "1");
 		



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]