Update of /cvsroot/monetdb/clients/src/mapiclient
In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv17861

Modified Files:
        MapiClient.mx 
Log Message:
Instead of having 4 (or were there more?) different places where
tables were being printed in "raw" form, we now only have one.
Moreover, all places where tables were being printed (in whatever
form) now use the same (new) function, so all formatting options are
possible in all places.
This means, that things like
        mclient -l<lang> -s<statement>
        mclient -l<lang> file
        mclient -l<lang> < file
        mclient -l<lang>
now *all* can be used in combination with the -f<format> option
(before this was only the latter two cases).
Also, don't use 4 different (yes, 4) variables to determine whether we
need to do formatting.  Use just the one (formatter).
pagewidth is set to the required width if unknown so formatting can
proceed.
CSV and TAB formatting are removed from SQLrendering since they had
nothing in common with the main use (TABLEformatter).  They have also
been fixed to quote the separator and other special characters.
The `=' sign at the start of lines from the server are now always
removed.
In addition, some more cleanup was performed.


Index: MapiClient.mx
===================================================================
RCS file: /cvsroot/monetdb/clients/src/mapiclient/MapiClient.mx,v
retrieving revision 1.69
retrieving revision 1.70
diff -u -d -r1.69 -r1.70
--- MapiClient.mx       24 Aug 2007 21:01:34 -0000      1.69
+++ MapiClient.mx       27 Aug 2007 19:36:49 -0000      1.70
@@ -108,9 +108,6 @@
 #endif
 
 #ifdef NATIVE_WIN32
-/* Windows doesn't declare chdir, even though it does provide the function */
-extern int chdir(const char *);
-
 #define strdup _strdup
 #endif
 
@@ -118,7 +115,7 @@
 #define getlogin() "win32"
 #endif
 
-enum modes{
+enum modes {
        MAL,
        SQL,
        XQUERY,
@@ -137,7 +134,7 @@
 #define setPrompt() sprintf(promptbuf, "%.*s>", (int) sizeof(promptbuf) - 2, 
language);
 #define debugMode() (strncmp(promptbuf, "mdb", 3) == 0)
 
-/* some internal formatters */
+/* the internal formatters */
 enum formatters {
        NOformatter,
        RAWformatter,
@@ -167,13 +164,12 @@
 required is larger, then a heuristic routine is
 called to distribute the available space.
 Attribute values may then span multiple lines.
-Setting the pagewidth to 0 turns of row size
+Setting the pagewidth to 0 turns off row size
 control.
 @c
 #ifdef HAVE_POPEN
 static char *pager = 0;                /* use external pager */
 #endif
-static int cols = 0;
 static int rowsperpage = 0;    /* for SQL pagination */
 static int pagewidth = -1;     /* use raw mode for file input, tabular format 
in stdin */
 static int interactive_stdin = 0;
@@ -184,7 +180,7 @@
        DEBUGmodifier,
        TRACEmodifier
 };
-static enum modifiers specials = NOmodifier; /* set when we see EXPLAIN,DEBUG, 
or TRACE */
+static enum modifiers specials = NOmodifier; /* set when we see EXPLAIN,DEBUG, 
or TRACE (only if mode == SQL) */
 
 #define MINCOLSIZE 5
 
@@ -250,18 +246,16 @@
 {
        if (mode == SQL && command) {
                /* catch the specials for better rendering */
-               const char *s = command;
-
-               specials = NOmodifier;
-
-               while (*s == ' ' || *s == '\t')
-                       s++;
-               if (strncmp(s, "explain", 7) == 0)
+               while (*command == ' ' || *command == '\t')
+                       command++;
+               if (strncmp(command, "explain", 7) == 0)
                        specials = EXPLAINmodifier;
-               if (strncmp(s, "debug", 5) == 0)
+               else if (strncmp(command, "debug", 5) == 0)
                        specials = DEBUGmodifier;
-               if (strncmp(s, "trace", 5) == 0)
+               else if (strncmp(command, "trace", 5) == 0)
                        specials = TRACEmodifier;
+               else
+                       specials = NOmodifier;
        }
 }
 
@@ -269,78 +263,52 @@
 SQLrow(int *len, int *numeric, char **rest, int fields, int trim)
 {
        int i, more, first = 1;
-       char old;
        char *t;
-       char separator = ',';
 
        /* trim the text upon need */
-       switch (formatter) {
-       case TABLEformatter:
-               for (i = 0; i < fields; i++) {
-                       if (rest[i] && (int) strlen(rest[i]) > len[i]) {
-                               int cnt = strlen(rest[i]) - len[i];
+       for (i = 0; i < fields; i++) {
+               if (rest[i] && (int) strlen(rest[i]) > len[i]) {
+                       int cnt = strlen(rest[i]) - len[i];
 
-                               /* first remove a leading spaces unless 
requested */
-                               for (t = rest[i]; trim && t && *t && cnt; t++)
-                                       if (*t != ' ' && *t != '\t')
-                                               break;
-                                       else
-                                               cnt--;
-                               rest[i] = t;
-                       }
+                       /* first remove leading spaces unless requested */
+                       for (t = rest[i]; trim && t && *t && cnt; t++)
+                               if (*t != ' ' && *t != '\t')
+                                       break;
+                               else
+                                       cnt--;
+                       rest[i] = t;
                }
+       }
 
-               do {
-                       fprintf(toConsole, "| ");
-                       more = 0;
-                       for (i = 0; i < fields; i++) {
-                               if (rest[i] == NULL || *rest[i] == 0)
-                                       fprintf(toConsole, "%*s |", len[i], " 
");
-                               else {
-                                       /* break the string into pieces and 
left-adjust them in the column */
-                                       if ((int) strlen(rest[i]) > len[i]) {
+       do {
+               fprintf(toConsole, "| ");
+               more = 0;
+               for (i = 0; i < fields; i++) {
+                       if (rest[i] == NULL || *rest[i] == 0)
+                               fprintf(toConsole, "%*s |", len[i], " ");
+                       else {
+                               /* break the string into pieces and left-adjust 
them in the column */
+                               if ((int) strlen(rest[i]) > len[i]) {
+                                       t = rest[i] + len[i];
+                                       while (t > rest[i] && *t != ' ' && *t 
!= '\t')
+                                               t--;
+                                       if (t == rest[i] && *t != ' ' && *t != 
'\t')
                                                t = rest[i] + len[i];
-                                               while (t > rest[i] && *t != ' ' 
&& *t != '\t')
-                                                       t--;
-                                               if (t == rest[i] && *t != ' ' 
&& *t != '\t')
-                                                       t = rest[i] + len[i];
-                                               old = *t;
-                                               *t = 0;
-                                               fprintf(toConsole, "%*s |", 
((first && numeric[i]) ? len[i] : -len[i]), rest[i]);
-                                               *t = old;
-                                               if (old) {
-                                                       while (*t && (*t == ' ' 
|| *t == '\n'))
-                                                               t++;
-                                                       rest[i] = *t ? t : 0;
-                                               } else
-                                                       rest[i] = 0;
-                                               if (rest[i]) {
-                                                       more++;
-                                               }
-                                       } else {
-                                               fprintf(toConsole, "%*s |", 
((first && numeric[i]) ? len[i] : -len[i]), rest[i]);
-                                               rest[i] = 0;
-                                       }
+                                       fprintf(toConsole, "%*.*s |", first && 
numeric[i] ? len[i] : -len[i], (int) (t - rest[i]), rest[i]);
+                                       while (*t == ' ' || *t == '\n')
+                                               t++;
+                                       rest[i] = *t ? t : 0;
+                                       if (rest[i])
+                                               more = 1;
+                               } else {
+                                       fprintf(toConsole, "%*s |", first && 
numeric[i] ? len[i] : -len[i], rest[i]);
+                                       rest[i] = 0;
                                }
                        }
-                       first = 0;
-                       fprintf(toConsole, "\n");
-               } while (more);
-               break;
-       case TABformatter:
-               separator = '\t';
-       case CSVformatter:
-               for (i = 0; i < fields;) {
-                       if (rest[i])
-                               fprintf(toConsole, "%s", rest[i]);
-                       if (++i < fields)
-                               fputc(separator, toConsole);
                }
-               fputc('\n', toConsole);
-               break;
-       default:
-               break;
-       }
+               first = 0;
+               fprintf(toConsole, "\n");
+       } while (more);
 }
 
 static void
@@ -380,7 +348,7 @@
 we need to detect end of debugging. We overload
 the routine to our liking.
 @c
-int
+static int
 fetch_row(MapiHdl hdl)
 {
        char *reply;
@@ -399,12 +367,12 @@
 {
        int i, fields;
 
-       fprintf(toConsole, "<?xml version='1.0' encoding='utf-8'?>\n");
-       fprintf(toConsole, "<!DOCTYPE table ["
-               " <!ELEMENT table (row)*>"                      /* a table 
consists of zero or more rows */
-               " <!ELEMENT row (column)+>"                     /* a row 
consists of one or more columns */
-               " <!ELEMENT column (#PCDATA)>"
-               " <!ATTLIST table name CDATA #REQUIRED>"        /* a table has 
a name */
+       fprintf(toConsole, "<?xml version='1.0' encoding='UT-8'?>\n");
+       fprintf(toConsole, "<!DOCTYPE table [\n"
+               " <!ELEMENT table (row)*>\n"                    /* a table 
consists of zero or more rows */
+               " <!ELEMENT row (column)+>\n"                   /* a row 
consists of one or more columns */
+               " <!ELEMENT column (#PCDATA)>\n"
+               " <!ATTLIST table name CDATA #REQUIRED>\n"      /* a table has 
a name */
                " <!ATTLIST column name CDATA #REQUIRED>]>\n"); /* a column has 
a name */
        fprintf(toConsole, "<table");
        XMLprattr("name", mapi_get_table(hdl, 0));
@@ -425,6 +393,7 @@
        }
        fprintf(toConsole, "</table>\n");
 }
+
 static void
 RAWrenderer(MapiHdl hdl)
 {
@@ -433,27 +402,80 @@
        while ((line = mapi_fetch_line(hdl)) != 0) {
                if (strstr(line, "mdb>#EOD"))
                        setPrompt();
+               if (*line == '=')
+                       line++;
                fprintf(toConsole, "%s\n", line);
        }
 }
 
 static void
-SQLseparator(int *len, int fields, char sep)
+CSVrenderer(MapiHdl hdl)
 {
-       int i, j;
+       int fields;
+       char *s;
+       char *sep = formatter == CSVformatter ? "," : "\t";
+       int i;
 
-       if (formatter == TABLEformatter) {
-               fprintf(toConsole, "+%c", sep);
+       while ((fields = fetch_row(hdl)) != 0) {
                for (i = 0; i < fields; i++) {
-                       for (j = 0; j < (len[i] < 0 ? -len[i] : len[i]); j++)
-                               fprintf(toConsole, "%c", sep);
-                       fprintf(toConsole, "%c+", sep);
+                       s = mapi_fetch_field(hdl, i);
+                       if (s == NULL)
+                               s = "";
+                       if (strchr(s, *sep) != NULL || strchr(s, '\n') != NULL 
|| strchr(s, '"') != NULL) {
+                               fprintf(toConsole, "%s\"", i == 0 ? "" : sep);
+                               while (*s) {
+                                       switch (*s) {
+                                       case '\n':
+                                               putc('\\', toConsole);
+                                               putc('n', toConsole);
+                                               break;
+                                       case '\t':
+                                               putc('\\', toConsole);
+                                               putc('t', toConsole);
+                                               break;
+                                       case '\r':
+                                               putc('\\', toConsole);
+                                               putc('r', toConsole);
+                                               break;
+                                       case '\\':
+                                               putc('\\', toConsole);
+                                               putc('\\', toConsole);
+                                               break;
+                                       case '"':
+                                               putc('\\', toConsole);
+                                               putc('"', toConsole);
+                                               break;
+                                       default:
+                                               if (*s == *sep)
+                                                       putc('\\', toConsole);
+                                               putc(*s, toConsole);
+                                               break;
+                                       }
+                                       s++;
+                               }
+                               putc('"', toConsole);
+                       } else
+                               fprintf(toConsole, "%s%s", i == 0 ? "" : sep, s 
? s : "");
                }
                fprintf(toConsole, "\n");
        }
 }
 
 static void
+SQLseparator(int *len, int fields, char sep)
+{
+       int i, j;
+
+       fprintf(toConsole, "+%c", sep);
+       for (i = 0; i < fields; i++) {
+               for (j = 0; j < (len[i] < 0 ? -len[i] : len[i]); j++)
+                       fprintf(toConsole, "%c", sep);
+               fprintf(toConsole, "%c+", sep);
+       }
+       fprintf(toConsole, "\n");
+}
+
+static void
 SQLqueryEcho(MapiHdl hdl)
 {
        if (echoquery) {
@@ -471,20 +493,18 @@
 static void
 SQLheader(MapiHdl hdl, int *len, int fields)
 {
-       int i;
-       char **names = (char **) alloca(fields * sizeof(char *));
-       int *numeric = (int *) alloca(fields * sizeof(int));
-
        SQLqueryEcho(hdl);
-       if (formatter == TABLEformatter) {
-               SQLseparator(len, fields, '-');
-               if (mapi_get_name(hdl, 0)) {
-                       for (i = 0; i < fields; i++) {
-                               names[i] = mapi_get_name(hdl, i);
-                               numeric[i] = 0;
-                       }
-                       SQLrow(len, numeric, names, fields, 1);
+       SQLseparator(len, fields, '-');
+       if (mapi_get_name(hdl, 0)) {
+               int i;
+               char **names = (char **) alloca(fields * sizeof(char *));
+               int *numeric = (int *) alloca(fields * sizeof(int));
+
+               for (i = 0; i < fields; i++) {
+                       names[i] = mapi_get_name(hdl, i);
+                       numeric[i] = 0;
                }
+               SQLrow(len, numeric, names, fields, 1);
                SQLseparator(len, fields, '=');
        }
 }
@@ -495,9 +515,6 @@
        char *reply;
        int cnt = 0;
 
-       if (mapi_result_error(hdl) != NULL) {
-               mapi_explain_result(hdl, stderr);
-       }
        sprintf(promptbuf, "mdb>");
        while ((reply = mapi_fetch_line(hdl))) {
                cnt++;
@@ -535,11 +552,6 @@
        char *fields[1];
        int len[1], numeric[1];
 
-       if (mapi_result_error(hdl) != NULL) {
-               mapi_explain_result(hdl, stderr);
-               return;
-       }
-
        SQLqueryEcho(hdl);
 
        len[0] = pagewidth - 4; /* remove borders */
@@ -572,10 +584,6 @@
        int cols = 1;
        int numeric[2];
 
-       if (mapi_result_error(hdl) != NULL) {
-               mapi_explain_result(hdl, stderr);
-               return;
-       }
        SQLqueryEcho(hdl);
        len[0] = pagewidth - 4; /* remove borders */
        numeric[0] = 0;
@@ -584,7 +592,7 @@
        while ((fields[0] = mapi_fetch_line(hdl)) != NULL) {
                if (silent)
                        continue;
-               if (*fields[0] == '%' && pagewidth >= 0)
+               if (*fields[0] == '%')
                        continue;
                if (fields[0] && strstr(fields[0], "mdb>#EOD"))
                        setPrompt();
@@ -617,9 +625,8 @@
                        continue;       /* output should be rendered 
differently */
                }
                SQLrow(len, numeric, fields, cols, 0);
-               if (ps && rows % ps == ps - 1 && rows != 
mapi_get_row_count(hdl) && fromConsole) {
+               if (fromConsole && ps > 0 && rows % ps == ps - 1 && rows != 
mapi_get_row_count(hdl))
                        SQLpagemove(len, cols, &ps, &silent);
-               }
                rows++;
        }
        SQLseparator(len, cols, '-');
@@ -662,6 +669,14 @@
                                char *s;
 
                                len[i] = mapi_get_len(hdl, i);
+                               if (len[i] == 0) {
+                                       /* no table widths known, so
+                                          divide columns evenly over
+                                          available space */
+                                       len[i] = (pagewidth - 2 * fields - 3) / 
fields;
+                                       if (len[i] <= MINCOLSIZE)
+                                               len[i] = MINCOLSIZE;
+                               }
                                s = mapi_get_name(hdl, i);
                                if (s != NULL && (max = strlen(s)) > len[i])
                                        len[i] = max;
@@ -672,47 +687,45 @@
                                                           strcmp(s, "double") 
== 0 ||
                                                           strcmp(s, "float") 
== 0);
                        }
-                       if (pagewidth) {
+                       total = 0;
+                       for (i = 0; i < fields; i++)
+                               total += len[i];
+                       while (2 * fields + 2 + total >= pagewidth && max) {
+                               max = 0;
                                total = 0;
                                for (i = 0; i < fields; i++)
                                        total += len[i];
-                               while (2 * fields + 2 + total >= pagewidth && 
max) {
-                                       max = 0;
-                                       total = 0;
-                                       for (i = 0; i < fields; i++)
-                                               total += len[i];
 
-                                       /* punish the column headers first */
-                                       for (i = 0; i < fields; i++) {
-                                               char *name = mapi_get_name(hdl, 
i);
+                               /* punish the column headers first */
+                               for (i = 0; i < fields; i++) {
+                                       char *name = mapi_get_name(hdl, i);
 
-                                               if (name != NULL &&
-                                                   strlen(name) > (size_t) 
mapi_get_len(hdl, i) &&
-                                                   len[i] > MINCOLSIZE) {
-                                                       len[i]--;
-                                                       total--;
-                                                       if (2 * fields + 2 + 
total == pagewidth)
-                                                               break;
-                                                       max = 1;
-                                               }
+                                       if (name != NULL &&
+                                           strlen(name) > (size_t) 
mapi_get_len(hdl, i) &&
+                                           len[i] > MINCOLSIZE) {
+                                               len[i]--;
+                                               total--;
+                                               if (2 * fields + 2 + total == 
pagewidth)
+                                                       break;
+                                               max = 1;
                                        }
                                }
-                               /* punish the long value fields */
-                               while (2 * fields + 2 + total >= pagewidth) {
-                                       total = 0;
-                                       max = 0;
-                                       for (i = 0; i < fields; i++) {
-                                               total += len[i];
-                                               if (len[i] > len[max])
-                                                       max = i;
-                                       }
-
-                                       /* penalty for largest field */
-                                       len[max]--;
-                                       total--;
-                                       if (len[max] == 1)
-                                               break;
+                       }
+                       /* punish the long value fields */
+                       while (2 * fields + 2 + total >= pagewidth) {
+                               total = 0;
+                               max = 0;
+                               for (i = 0; i < fields; i++) {
+                                       total += len[i];
+                                       if (len[i] > len[max])
+                                               max = i;
                                }
+                               
+                               /* penalty for largest field */
+                               len[max]--;
+                               total--;
+                               if (len[max] == 1)
+                                       break;
                        }
 
                        SQLheader(hdl, len, fields);
@@ -721,13 +734,13 @@
 
                for (i = 0; i < fields; i++) {
                        rest[i] = mapi_fetch_field(hdl, i);
-                       if (rest[i] == NULL && formatter == TABLEformatter)
+                       if (rest[i] == NULL)
                                rest[i] = nullstring;
                }
 
                SQLrow(len, numeric, rest, fields, 1);
                rows++;
-               if (ps && (rows % ps) == ps - 1 && rows != 
mapi_get_row_count(hdl) && fromConsole) {
+               if (ps > 0 && (rows % ps) == ps - 1 && rows != 
mapi_get_row_count(hdl) && fromConsole) {
                        char buf[BUFSIZ];
 
                        SQLseparator(len, oldfields, '-');
@@ -776,13 +789,108 @@
 #ifdef TIOCGWINSZ
        struct winsize ws;
 
-       if (cols == 0 && ioctl(fileno(stdin), TIOCGWINSZ, &ws) == 0)
-               pagewidth = ws.ws_col > 0 ? ws.ws_col : DEFWIDTH;
+       if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) == 0 && ws.ws_col > 0)
+               pagewidth = ws.ws_col;
        else
-               pagewidth = cols;
-#else
+#endif
        pagewidth = DEFWIDTH;
+}
+
+static int
+format_result(MapiHdl hdl)
+{
+#ifdef HAVE_POPEN
+       FILE *saveFD = NULL;    /* for external paging */
 #endif
+       MapiMsg rc;
+
+#ifdef HAVE_POPEN
+       if (pager) {
+               saveFD = toConsole;
+               toConsole = popen(pager, "w");
+               if (toConsole == NULL) {
+                       toConsole = saveFD;
+                       fprintf(stderr, "Starting '%s' failed\n", pager);
+               }
+       }
+#endif
+
+       do {
+               char *reply;
+               int oldpagewidth;
+
+               oldpagewidth = pagewidth;
+               if (mapi_get_querytype(hdl) == Q_UPDATE) {
+                       if (formatter == RAWformatter)
+                               fprintf(toConsole, "[ %d\t]\n", 
mapi_rows_affected(hdl));
+                       else
+                               fprintf(toConsole, "Rows affected %d\n", 
mapi_rows_affected(hdl));
+                       continue;
+               }
+               if (pagewidth <= 0) {
+                       int i, n = mapi_get_field_count(hdl);
+
+                       pagewidth = 2 * n + 4;
+                       for (i = 0; i < n; i++) {
+                               int len = mapi_get_len(hdl, i);
+
+                               pagewidth += len < MINCOLSIZE ? MINCOLSIZE : 
len;
+                       }
+               }
+               /* note: specials != NOmodifier implies mode == SQL */
+               if (specials != NOmodifier && debugMode()) {
+                       SQLdebugRendering(hdl);
+                       continue;
+               }
+               if ((reply = mapi_result_error(hdl)) != NULL) {
+                       if (formatter == RAWformatter)
+                               mapi_explain_result(hdl, stderr);
+                       else
+                               fprintf(toConsole, "%s", reply);
+               }
+               if (debugMode())
+                       RAWrenderer(hdl);
+               else {
+                       switch (formatter) {
+                       case XMLformatter:
+                               XMLrenderer(hdl);
+                               break;
+                       case CSVformatter:
+                       case TABformatter:
+                               CSVrenderer(hdl);
+                               break;
+                       case TABLEformatter:
+                               switch (specials) {
+                               case DEBUGmodifier:
+                                       SQLdebugRendering(hdl);
+                                       break;
+                               case EXPLAINmodifier:
+                                       SQLexplainRendering(hdl);
+                                       break;
+                               case TRACEmodifier:
+                                       SQLtraceRenderer(hdl);
+                                       break;
+                               default:
+                                       SQLrenderer(hdl);
+                                       break;
+                               }
+                               break;
+                       default:
+                               RAWrenderer(hdl);
+                               break;
+                       }
+               }
+               pagewidth = oldpagewidth;
+       } while ((rc = mapi_needmore(hdl)) == MOK && (rc = 
mapi_next_result(hdl)) == 1);
+
+#ifdef HAVE_POPEN
+       if (saveFD) {
+               pclose(toConsole);
+               toConsole = saveFD;
+       }
+#endif
+
+       return rc;
 }
 
 static int
@@ -799,21 +907,9 @@
                return 1;
        }
 
-       if (mode == SQL &&
-           (pagewidth >= 0 ||
-            formatter == TABLEformatter ||
-            formatter == CSVformatter ||
-            formatter == TABformatter) &&
-           !debugMode()) {
-               if (pagewidth < 0)
-                       pagewidth = 0;
-               SQLrenderer(hdl);
-       } else if (mode == SQL && formatter == XMLformatter && !debugMode())
-               XMLrenderer(hdl);
-       else
-               RAWrenderer(hdl);
+       format_result(hdl);
 
-       if (!mapi_get_active(mid) || !interactive)
+       if (mapi_get_active(mid) == NULL || !interactive)
                mapi_close_handle(hdl);
        return 0;
 }
@@ -845,7 +941,6 @@
                        return 1;                                       \
                }
 
-
 static int
 doFile(Mapi mid, const char *file)
 {
@@ -898,7 +993,7 @@
                                   (must be at start of line) */
                                while (xquery_sep) {
                                        xquery_sep = strstr(xquery_sep + 1, 
"<>");
-                                       if (xquery_sep && (xquery_sep[-1] == 10 
|| xquery_sep[-1] == 13)) {
+                                       if (xquery_sep && (xquery_sep[-1] == 
'\n' || xquery_sep[-1] == '\r')) {
                                                char *next = xquery_sep + 2;
 
                                                while (*next && isspace(*next))
@@ -932,23 +1027,8 @@
 
                CHECK_RESULT(mid, hdl, buf, continue);
 
-               do {
-                       char *reply;
+               rc = format_result(hdl);
 
-                       if ((reply = mapi_result_error(hdl)) != NULL)
-                               mapi_explain_result(hdl, stderr);
-                       if (mapi_get_querytype(hdl) == Q_UPDATE) {
-                               fprintf(toConsole, "[ %d\t]\n", 
mapi_rows_affected(hdl));
-                       } else {
-                               while ((reply = mapi_fetch_line(hdl)) != NULL) {
-                                       if (strstr(reply, "mdb>#EOD"))
-                                               setPrompt();
-                                       if (mode == XQUERY && *reply == '=')
-                                               reply++;
-                                       fprintf(toConsole, "%s\n", reply);
-                               }
-                       }
-               } while ((rc = mapi_needmore(hdl)) == MOK && (rc = 
mapi_next_result(hdl)) == 1);
                if (rc == MMORE && (length > 0 || mapi_query_done(hdl) != MOK))
                        continue;       /* get more data */
 
@@ -1036,10 +1116,6 @@
        MapiMsg rc = MOK;
        int sent = 0;           /* whether we sent any data to the server */
 
-#ifdef HAVE_POPEN
-       FILE *pagerFD = 0;      /* for external paging */
-#endif
-
 #ifdef HAVE_LIBREADLINE
        if (prompt == NULL)
 #endif
@@ -1120,29 +1196,20 @@
                                        mapi_trace(mid, !mapi_get_trace(mid));
                                        continue;
                                case 'A':
-                                       if (mode == XQUERY)
+                                       if (mode != SQL)
                                                break;
                                        mapi_setAutocommit(mid, 1);
                                        continue;
                                case 'a':
-                                       if (mode == XQUERY)
+                                       if (mode != SQL)
                                                break;
                                        mapi_setAutocommit(mid, 0);
                                        continue;
                                case 'w':
-                                       if (mode == XQUERY)
-                                               break;
-                                       for (line += 2; *line && isspace((int) 
*line); line++)
-                                               ;
-                                       pagewidth = (int) atol(line);
+                                       pagewidth = atoi(line + 2);
                                        continue;
                                case 'r':
-                                       for (line += 2; *line && isspace((int) 
*line); line++)
-                                               ;
-                                       rowsperpage = (int) atol(line);
-                               case 'p':
-                                       if (mode == XQUERY)
-                                               break;
+                                       rowsperpage = atoi(line + 2);
                                        continue;
                                case 'd':
                                        if (mode != SQL)
@@ -1156,10 +1223,7 @@
                                        if (*line) {
                                                dump_table(mid, line, 
toConsole, 1);
                                        } else {
-                                               int old = pagewidth;
-
                                                /* get all table names in 
current schema */
-                                               pagewidth = 0;
                                                if ((hdl = mapi_query(mid,
                                                                      "SELECT 
\"t\".\"name\" "
                                                                      "FROM 
\"sys\".\"tables\" \"t\", "
@@ -1167,17 +1231,10 @@
                                                                      "WHERE 
\"t\".\"schema_id\" = \"s\".\"id\" "
                                                                      "AND 
\"s\".\"name\" = \"current_schema\" "
                                                                      "ORDER BY 
\"t\".\"name\"")) != NULL &&
-                                                   mapi_error(mid) == MOK) {
-                                                       if (formatter == 
XMLformatter)
-                                                               
XMLrenderer(hdl);
-                                                       else if (formatter == 
RAWformatter || debugMode())
-                                                               
RAWrenderer(hdl);
-                                                       else
-                                                               
SQLrenderer(hdl);
-                                               }
+                                                   mapi_error(mid) == MOK)
+                                                       format_result(hdl);
                                                mapi_close_handle(hdl);
                                                hdl = NULL;
-                                               pagewidth = old;
                                        }
                                        continue;
 
@@ -1267,11 +1324,10 @@
                                                s++;
                                        if (*s == 0)
                                                continue;
-                                       s[strlen(s) - 1] = 0;
-                                       pager = *s ? strdup(s) : NULL;
-                                       pagewidth = -1; /* disable pagination */
-                               }
+                                       s[strlen(s) - 1] = 0;  /* squash final 
\n */
+                                       pager = strdup(s);
                                        continue;
+                               }
 #endif
 #ifdef HAVE_LIBREADLINE
                                case 'h':
@@ -1288,13 +1344,16 @@
                                }
 /* for later
                                case '!':
-                               {       char *nl;
-                                       nl= strchr(line,'\n');
-                                       if( nl) *nl=0;
-                                       if( history_expand(line+2,&nl)){
-                                               fprintf(toConsole, "%s\n",nl);
+                               {
+                                       char *nl;
+
+                                       nl = strchr(line, '\n');
+                                       if (nl)
+                                               *nl = 0;
+                                       if (history_expand(line + 2, &nl)) {
+                                               fprintf(toConsole, "%s\n", nl);
                                        }
-                                       fprintf(toConsole,"Expansion needs 
work\n");
+                                       fprintf(toConsole, "Expansion needs 
work\n");
                                        continue;
                                }
 */
@@ -1361,107 +1420,13 @@
                }
                CHECK_RESULT(mid, hdl, buf, continue);
 
-#ifdef HAVE_POPEN
-               if (pager) {
-                       pagerFD = popen(pager, "w");
-                       if (pagerFD == NULL)
-                               fprintf(stderr, "Starting '%s' failed\n", 
pager);
-               }
-#endif
-
-               do {
-                       char *reply;
-
-                       if (mapi_get_querytype(hdl) == Q_UPDATE) {
-                               if (mode == SQL && pagewidth >= 0 && formatter 
!= RAWformatter)
-                                       fprintf(toConsole, "Rows affected 
%d\n", mapi_rows_affected(hdl));
-                               else
-                                       fprintf(toConsole, "[ %d\t]\n", 
mapi_rows_affected(hdl));
-                       } else if (mode == SQL) {
-                               if (specials != NOmodifier && debugMode())
-                                       SQLdebugRendering(hdl);
-                               else if ((reply = mapi_result_error(hdl)) != 
NULL) {
-                                       if (pagewidth < 0 || formatter == 
RAWformatter)
-                                               goto nononsense;
-                                       fprintf(toConsole, "%s", reply);
-                               } else
-                                       switch (specials) {
-                                       case EXPLAINmodifier:
-                                               if (pagewidth < 0)
-                                                       goto nononsense;
-                                               SQLexplainRendering(hdl);
-                                               break;
-                                       case DEBUGmodifier:
-                                               SQLdebugRendering(hdl);
-                                               break;
-                                       case TRACEmodifier:
-                                               if (pagewidth < 0)
-                                                       goto nononsense;
-                                               SQLtraceRenderer(hdl);
-                                               break;
-                                       default:
-                                               if (pagewidth < 0)
-                                                       goto nononsense;
-                                               if (formatter == XMLformatter)
-                                                       XMLrenderer(hdl);
-                                               else if (formatter == 
RAWformatter || debugMode())
-                                                       RAWrenderer(hdl);
-                                               else
-                                                       SQLrenderer(hdl);
-                                       }
-                       } else {
-                             nononsense:
-                               if ((reply = mapi_result_error(hdl)) != NULL)
-                                       mapi_explain_result(hdl, stderr);
+               rc = format_result(hdl);
 
-                               while ((reply = mapi_fetch_line(hdl)) != NULL) {
-                                       if (strstr(reply, "mdb>#EOD"))
-                                               setPrompt();
-                                       if (pagewidth >= 0 && *reply == '%') {
-                                               /* should only maybe happen in 
the first iteration */
-                                               switch (formatter) {
-                                               case XMLformatter:
-                                                       XMLrenderer(hdl);
-                                                       break;
-                                               case TABLEformatter:
-                                                       if (!debugMode()) {
-                                                               
SQLrenderer(hdl);
-                                                               break;
-                                                       }
-                                               default:
-                                                       RAWrenderer(hdl);
-                                               }
-                                               break;
-                                       }
-                                       if ((mode == XQUERY || mode == MAL) && 
*reply == '=')
-                                               reply++;
-                                       /* we know when the MAL debugger 
becomes active and finishes */
-                                       if (strstr(reply, "mdb>#EOD")) {
-                                               setPrompt();
-                                               continue;
-                                       }
-                                       if (strncmp(reply, "mdb>#", 5) == 0)
-                                               sprintf(promptbuf, "mdb>");
-#ifdef HAVE_POPEN
-                                       if (pagerFD)
-                                               fprintf(pagerFD, "%s\n", reply);
-                                       else
-#endif
-                                               fprintf(toConsole, "%s\n", 
reply);
-                               }
-                       }
-               } while ((rc = mapi_needmore(hdl)) == MOK && (rc = 
mapi_next_result(hdl)) == 1);
                if (rc == MMORE && (line != NULL || mapi_query_done(hdl) != 
MOK))
                        continue;       /* get more data */
 
                CHECK_RESULT(mid, hdl, buf, continue);
 
-#ifdef HAVE_POPEN
-               if (pagerFD) {
-                       pclose(pagerFD);
-                       pagerFD = NULL;
-               }
-#endif
                timerEnd();
                mapi_close_handle(hdl);
                hdl = NULL;
@@ -1606,7 +1571,7 @@
                        break;
                case 'L':
                {
-                       char buf[1024];
+                       char buf[32];
 
                        snprintf(buf, sizeof(buf), "monet_%d", (int) getpid());
                        logfile = optarg ? optarg : strdup(buf);
@@ -1667,29 +1632,18 @@
                                dbname = optarg;
                        break;
                case 's':
-                       if (cols == 0)
-                               cols = -1;
                        command = optarg;
                        break;
                case 'w':
-                       if (optarg)
-                               cols = atol(optarg);
+                       pagewidth = atoi(optarg);
                        break;
                case 'r':
-                       if (optarg)
-                               rowsperpage = atol(optarg);
+                       rowsperpage = atoi(optarg);
                        break;
 #ifdef HAVE_POPEN
                case '|':
-               {
-                       char *s;
-
-                       s = optarg;
-                       while (*s && isspace((int) *s))
-                               s++;
-                       pager = s;
+                       pager = optarg;
                        break;
-               }
 #endif
                case 't':
                        mark = "Timer";
@@ -1793,6 +1747,10 @@
           we're also interactive if no files */
        interactive |= (optind == argc && command == NULL);
 
+       /* default formatter depends on whether we're interactive */
+       if (formatter == NOformatter)
+               formatter = interactive && interactive_stdin && mode != XQUERY 
? TABLEformatter : RAWformatter;
+
        if (command) {
                /* execute from command-line */
                setWidth();
@@ -1821,11 +1779,7 @@
                        setPrompt();
                        prompt = promptbuf;
                        fromConsole = stdin;
-                       if( formatter== NOformatter)
-                               formatter= TABLEformatter;
-               } else
-               if( formatter== NOformatter)
-                       formatter= RAWformatter;
+               }
                /* use default rendering if not overruled at commandline */
                setWidth();
                /* stream xml document into the server */


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Monetdb-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/monetdb-checkins

Reply via email to