Changeset: 73042a946b15 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=73042a946b15
Modified Files:
        clients/mapiclient/dump.c
        clients/mapiclient/mhelp.c
        clients/mapiclient/mhelp.h
        clients/mapiclient/msqldump.h
Branch: default
Log Message:

const char * and use standard strncasecmp.
(Posix standard, that is, not C standard).


diffs (197 lines):

diff --git a/clients/mapiclient/dump.c b/clients/mapiclient/dump.c
--- a/clients/mapiclient/dump.c
+++ b/clients/mapiclient/dump.c
@@ -17,7 +17,7 @@
 #include "msqldump.h"
 
 static void
-quoted_print(stream *f, const char *s, const char singleq)
+quoted_print(stream *f, const char *s, char singleq)
 {
        mnstr_write(f, singleq ? "'" : "\"", 1, 1);
        while (*s) {
@@ -1046,7 +1046,7 @@ describe_schema(Mapi mid, const char *sn
 
 static int
 dump_table_data(Mapi mid, const char *schema, const char *tname, stream 
*toConsole,
-               const char useInserts)
+               char useInserts)
 {
        int cnt, i;
        MapiHdl hdl = NULL;
@@ -1139,7 +1139,7 @@ dump_table_data(Mapi mid, const char *sc
                             strcmp(mapi_get_type(hdl, i), "timestamptz") == 0);
        }
        while (mapi_fetch_row(hdl)) {
-               char *s;
+               const char *s;
 
                if (useInserts)
                        mnstr_printf(toConsole, "INSERT INTO \"%s\".\"%s\" 
VALUES (",
@@ -1203,7 +1203,7 @@ dump_table_data(Mapi mid, const char *sc
 }
 
 int
-dump_table(Mapi mid, const char *schema, const char *tname, stream *toConsole, 
int describe, int foreign, const char useInserts)
+dump_table(Mapi mid, const char *schema, const char *tname, stream *toConsole, 
int describe, int foreign, char useInserts)
 {
        int rc;
 
@@ -1220,7 +1220,8 @@ dump_function(Mapi mid, stream *toConsol
        size_t qlen = 200 + strlen(fid);
        char *query = malloc(qlen);
        const char *sep;
-       char *ffunc, *sname, *fname;
+       char *ffunc;
+       const char *sname, *fname;
        int flang, ftype;
 
        snprintf(query, qlen, "SELECT f.id, f.func, f.language, f.type, s.name, 
f.name FROM sys.functions f, sys.schemas s WHERE f.schema_id = s.id AND f.id = 
%s", fid);
@@ -1437,7 +1438,7 @@ dump_functions(Mapi mid, stream *toConso
 }
 
 int
-dump_database(Mapi mid, stream *toConsole, int describe, const char useInserts)
+dump_database(Mapi mid, stream *toConsole, int describe, char useInserts)
 {
        const char *start = "START TRANSACTION";
        const char *end = "ROLLBACK";
@@ -2121,7 +2122,7 @@ dump_version(Mapi mid, stream *toConsole
 {
        MapiHdl hdl;
        char *dbname = NULL, *uri = NULL, *dbver = NULL, *dbrel = NULL;
-       char *name, *val;
+       const char *name, *val;
 
        if ((hdl = mapi_query(mid,
                              "SELECT name, value "
diff --git a/clients/mapiclient/mhelp.c b/clients/mapiclient/mhelp.c
--- a/clients/mapiclient/mhelp.c
+++ b/clients/mapiclient/mhelp.c
@@ -27,15 +27,18 @@
 #include "monetdb_config.h"
 #include <ctype.h>
 #include <string.h>
+#ifdef HAVE_STRINGS_H
+#include <strings.h>           /* for strncasecmp */
+#endif
 #include "stream.h"
 #include "mhelp.h"
 
 typedef struct {
-       char *command;
-       char *synopsis;
-       char *syntax;
-       char *rules;
-       char *comments;
+       const char *command;
+       const char *synopsis;
+       const char *syntax;
+       const char *rules;
+       const char *comments;
 } SQLhelp;
 
 SQLhelp sqlhelp[] = {
@@ -740,33 +743,40 @@ SQLhelp sqlhelp[] = {
        {NULL, NULL, NULL, NULL, NULL}  /* End of list marker */
 };
 
-// matching is against a substring of the command string
+#ifndef HAVE_STRNCASECMP
 static int
-strmatch(const char *heap, const char *needle)
+strncasecmp(const char *s1, const char *s2, size_t n)
 {
-       char heapbuf[2048], *s = heapbuf;
-       char needlebuf[2048], *t = needlebuf;
+       int c1, c2;
 
-       for (; *heap; heap++)
-               *s++ = (char) tolower((int) *heap);
-       *s = 0;
-       for (; *needle; needle++)
-               *t++ = (char) tolower((int) *needle);
-       *t = 0;
-       return strncmp(heapbuf, needlebuf, strlen(needlebuf)) == 0;
+       while (n > 0) {
+               c1 = (unsigned char) *s1++;
+               c2 = (unsigned char) *s2++;
+               if (c1 == 0)
+                       return -c2;
+               if (c2 == 0)
+                       return c1;
+               if (c1 != c2 && tolower(c1) != tolower(c2))
+                       return tolower(c1) - tolower(c2);
+               n--;
+       }
+       return 0;
 }
+#endif
 
 static const char *
 sql_grammar_rule(const char *word, stream *toConsole)
 {
        char buf[65], *s = buf;
+       size_t buflen;
        int i;
        while (s < buf + 64 && *word != ',' && *word && !isspace((unsigned 
char) *word))
                *s++ = *word++;
        *s = 0;
+       buflen = (size_t) (s - buf);
 
        for (i = 0; sqlhelp[i].command; i++) {
-               if (strmatch(sqlhelp[i].command, buf) && sqlhelp[i].synopsis == 
NULL) {
+               if (strncasecmp(sqlhelp[i].command, buf, buflen) == 0 && 
sqlhelp[i].synopsis == NULL) {
                        mnstr_printf(toConsole, "%s : %s\n", buf, 
sqlhelp[i].syntax);
                }
        }
@@ -821,7 +831,7 @@ sql_word(const char *word, size_t maxlen
 }
 
 void
-sql_help(char *pattern, stream *toConsole, int pagewidth)
+sql_help(const char *pattern, stream *toConsole, int pagewidth)
 {
        size_t maxlen = 1, len;
        int i, step, ncolumns, total = 0;
@@ -835,13 +845,14 @@ sql_help(char *pattern, stream *toConsol
                pattern++;
        }
 
-       if (*pattern && pattern[strlen(pattern) - 1] == '\n')
-               pattern[strlen(pattern) - 1] = 0;
-
        if (*pattern && *pattern != '*') {
                int first = 1;
+               size_t patlen = strlen(pattern);
+               /* ignore possible final newline in pattern */
+               if (pattern[patlen - 1] == '\n')
+                       patlen--;
                for (i = 0; *pattern && sqlhelp[i].command; i++)
-                       if (strmatch(sqlhelp[i].command, pattern)) {
+                       if (strncasecmp(sqlhelp[i].command, pattern, patlen) == 
0) {
                                if (!first)
                                        mnstr_printf(toConsole, "\n");
                                sql_grammar(i, toConsole);
diff --git a/clients/mapiclient/mhelp.h b/clients/mapiclient/mhelp.h
--- a/clients/mapiclient/mhelp.h
+++ b/clients/mapiclient/mhelp.h
@@ -6,4 +6,4 @@
  * Copyright 1997 - July 2008 CWI, August 2008 - 2017 MonetDB B.V.
  */
 
-extern void sql_help(char *pattern, stream *toConsole, int pagewidth);
+extern void sql_help(const char *pattern, stream *toConsole, int pagewidth);
diff --git a/clients/mapiclient/msqldump.h b/clients/mapiclient/msqldump.h
--- a/clients/mapiclient/msqldump.h
+++ b/clients/mapiclient/msqldump.h
@@ -9,7 +9,7 @@
 extern int describe_table(Mapi mid, const char *schema, const char *tname, 
stream *toConsole, int foreign);
 extern int describe_sequence(Mapi mid, const char *schema, const char *sname, 
stream *toConsole);
 extern int describe_schema(Mapi mid, const char *sname, stream *toConsole);
-extern int dump_table(Mapi mid, const char *schema, const char *tname, stream 
*toConsole, int describe, int foreign, const char useInserts);
+extern int dump_table(Mapi mid, const char *schema, const char *tname, stream 
*toConsole, int describe, int foreign, char useInserts);
 extern int dump_functions(Mapi mid, stream *toConsole, const char *sname, 
const char *fname);
-extern int dump_database(Mapi mid, stream *toConsole, int describe, const char 
useInserts);
+extern int dump_database(Mapi mid, stream *toConsole, int describe, char 
useInserts);
 extern void dump_version(Mapi mid, stream *toConsole, const char *prefix);
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to