Don't require write support on help file.
authorJelmer Vernooij <jelmer@samba.org>
Mon, 19 Mar 2007 22:43:54 +0000 (23:43 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Mon, 19 Mar 2007 22:43:54 +0000 (23:43 +0100)
src/admin.c
src/help.c
testsuite/test-help.c

index c58487745a6e4b63a9aa6e07df64b54b252e0e85..71ae6690beef0764249cf45a2386fc83d3df992d 100644 (file)
@@ -60,8 +60,6 @@ static void network_admin_out(admin_handle h, const char *data)
 static void cmd_help(admin_handle h, char **args, void *userdata)
 {
        const char *s;
-       char **lines;
-       int i;
 
        s = help_get(help, args[1] != NULL?args[1]:"index");
 
@@ -73,13 +71,13 @@ static void cmd_help(admin_handle h, char **args, void *userdata)
                return;
        }
 
-       lines = g_strsplit(s, "\n", 0);
-
-       for (i = 0; lines[i]; i++) {
-               admin_out(h, "%s", lines[i]);
+       while (strncmp(s, "%\n", 2) != 0) {
+               char *tmp;
+               admin_out(h, "%s", tmp = g_strndup(s, strchr(s, '\n')-s));
+               g_free(tmp);
+                       
+               s = strchr(s, '\n')+1;
        }
-
-       g_strfreev(lines);
 }
 
 struct client *admin_get_client(admin_handle h)
index 2e418f06ed64fa9fbc180d27657edc39979d73a6..1f930ae38cc1ad2661bae4b5c2d94abecad68360 100644 (file)
@@ -42,31 +42,32 @@ void help_free(help_t *h)
 GHashTable *help_build_hash(char *data, gsize len)
 {
        GHashTable *h = g_hash_table_new(g_str_hash, g_str_equal);
-       gsize i, k;
+       gsize i;
        char *p;
 
        i = 0;
        while (i < len) {
-               if (data[i] == '?') {
-                       /* Key starts here */
-                       k = i+1;
-                       p = g_strstr_len(data+k, len-k, "\n%\n");
-                       if (p == NULL) {
-                               log_global(LOG_WARNING, "Error parsing help file");
-                               g_hash_table_destroy(h);
-                               return NULL;
-                       }
-                       p[1] = 0;
-                       i+=strlen(data+i)+2;
-                       p = g_strstr_len(data+k, len-k, "\n");
-                       p[0] = 0;
-                       g_hash_table_insert(h, data+k, p+1);
-               } else {
+               if (data[i] != '?') {
                        log_global(LOG_WARNING, "Unknown character '0x%02x' in help file", 
                                           data[i]);
                        g_hash_table_destroy(h);
                        return NULL;
                }
+               /* Key starts here */
+               p = g_strstr_len(data+i, len-i, "\n");
+               if (p == NULL) {
+                       log_global(LOG_WARNING, "Error parsing help file");
+                       g_hash_table_destroy(h);
+                       return NULL;
+               }
+               g_hash_table_insert(h, g_strndup(data+i+1, p-(data+i)-1), p+1);
+               p = g_strstr_len(data+i, len-i, "\n%\n");
+               if (p == NULL) {
+                       log_global(LOG_WARNING, "Error parsing help file");
+                       g_hash_table_destroy(h);
+                       return NULL;
+               }
+               i = p-data+3;
        }
        
        return h;
@@ -82,7 +83,7 @@ help_t *help_load_file( const char *helpfile )
        h = g_new0 (help_t, 1);
        
 #if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 8
-       h->file = g_mapped_file_new(helpfile, TRUE, &error);
+       h->file = g_mapped_file_new(helpfile, FALSE, &error);
        if (h->file != NULL) {
                len = g_mapped_file_get_length(h->file);
                data = g_mapped_file_get_contents(h->file);
index 19615e875e154a1af47b90eed8a99b6ddd6ce59b..3bb8b1b4fd1c186cc110bb2dbff223b8c55d4f00 100644 (file)
@@ -16,7 +16,7 @@ static GHashTable *build_hash(const char *data)
 START_TEST(test_help_hash)
        GHashTable *h = build_hash("?foo\nbar\n%\n");
        fail_unless(g_hash_table_lookup(h, "foo") != NULL);
-       fail_unless(strcmp(g_hash_table_lookup(h, "foo"), "bar\n") == 0);
+       fail_unless(strncmp(g_hash_table_lookup(h, "foo"), "bar\n", 4) == 0);
 END_TEST
 
 START_TEST(test_help_hash_nopercent)
@@ -27,15 +27,15 @@ END_TEST
 START_TEST(test_help_hash_multiple)
        GHashTable *h = build_hash("?foo\nbar\n%\n?bla\nbloe\nblie\n%\n");
        fail_unless(g_hash_table_lookup(h, "foo") != NULL);
-       fail_unless(strcmp(g_hash_table_lookup(h, "foo"), "bar\n") == 0);
-       fail_unless(strcmp(g_hash_table_lookup(h, "bla"), "bloe\nblie\n") == 0);
+       fail_unless(strncmp(g_hash_table_lookup(h, "foo"), "bar\n", 4) == 0);
+       fail_unless(strncmp(g_hash_table_lookup(h, "bla"), "bloe\nblie\n", 10) == 0);
 END_TEST
 
 START_TEST(test_help_hash_empty)
        GHashTable *h = build_hash("?foo\nbar\n%\n?\nbloe\nblie\n%\n");
        fail_unless(g_hash_table_lookup(h, "foo") != NULL);
-       fail_unless(strcmp(g_hash_table_lookup(h, "foo"), "bar\n") == 0);
-       fail_unless(strcmp(g_hash_table_lookup(h, ""), "bloe\nblie\n") == 0);
+       fail_unless(strncmp(g_hash_table_lookup(h, "foo"), "bar\n", 4) == 0);
+       fail_unless(strncmp(g_hash_table_lookup(h, ""), "bloe\nblie\n", 10) == 0);
 END_TEST
 
 START_TEST(test_help_hash_strange)