added usage info

Tue, 13 Aug 2013 11:19:22 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 13 Aug 2013 11:19:22 +0200
changeset 11
5db6178d8b58
parent 10
e6d0fbe0ebd9
child 12
493128ef1b12

added usage info

dav/Makefile file | annotate | diff | comparison | revisions
dav/crypto.c file | annotate | diff | comparison | revisions
dav/main.c file | annotate | diff | comparison | revisions
dav/main.h file | annotate | diff | comparison | revisions
dav/optparser.c file | annotate | diff | comparison | revisions
dav/optparser.h file | annotate | diff | comparison | revisions
dav/utils.c file | annotate | diff | comparison | revisions
dav/webdav.c file | annotate | diff | comparison | revisions
--- a/dav/Makefile	Mon Aug 12 19:50:14 2013 +0200
+++ b/dav/Makefile	Tue Aug 13 11:19:22 2013 +0200
@@ -37,6 +37,7 @@
 SRC += methods.c
 SRC += config.c
 SRC += crypto.c
+SRC += optparser.c
 
 
 OBJ = $(SRC:%.c=../build/tool/%.$(OBJ_EXT))
--- a/dav/crypto.c	Mon Aug 12 19:50:14 2013 +0200
+++ b/dav/crypto.c	Tue Aug 13 11:19:22 2013 +0200
@@ -167,6 +167,7 @@
             memcpy(out, enc->iv, ivl);
         }
         EVP_EncryptUpdate(&enc->ctx, out + ivl, &outlen, in, in_len);
+        free(in);
     } else {
         out = malloc(16);
         EVP_EncryptFinal_ex(&enc->ctx, out, &outlen);
--- a/dav/main.c	Mon Aug 12 19:50:14 2013 +0200
+++ b/dav/main.c	Tue Aug 13 11:19:22 2013 +0200
@@ -52,24 +52,48 @@
     load_config();
     
     if(argc < 2) {
-        print_usage();
+        fprintf(stderr, "Missing command\n");
+        print_usage(argv[0]);
+        return -1;
+    }
+    
+    char *cmd = argv[1];
+    CmdArgs *args = cmd_parse_args(argc - 2, argv + 2);
+    if(!args) {
+        print_usage(argv[0]);
         return -1;
     }
     
-    if(!strcmp(argv[1], "list") || !strcmp(argv[1], "ls")) {
-        return cmd_list(argc - 2, argv + 2);
-    } else if(!strcmp(argv[1], "get")) {
-        return cmd_get(argc - 2, argv + 2);
-    } else if(!strcmp(argv[1], "put")) {
-        return cmd_put(argc - 2, argv + 2);
+    if(!strcmp(cmd, "list") || !strcmp(cmd, "ls")) {
+        return cmd_list(args);
+    } else if(!strcmp(cmd, "get")) {
+        return cmd_get(args);
+    } else if(!strcmp(cmd, "put")) {
+        return cmd_put(args);
     }
     
-    print_usage();
+    print_usage(argv[0]);
     return -1;
 }
 
-void print_usage() {
-    
+void print_usage(char *cmd) {
+    fprintf(stderr, "Usage: %s command [options] arguments...\n\n", cmd);
+    fprintf(stderr, "Commands:\n");
+    fprintf(stderr, "        list [-al] <url>\n");
+    fprintf(stderr, "        get [-p] [-k <key>] [-o <file>] <url>\n");
+    fprintf(stderr, "        put [-p] [-k <key>] <url> <file>\n");
+    fprintf(stderr, "\n");
+    fprintf(stderr, "Options:\n");
+    fprintf(stderr,
+            "        -k <key>   Key to use for encryption or decryption\n");
+    fprintf(stderr, "        -p         Don't encrypt or decrypt files\n");
+    fprintf(stderr, "        -o <file>  Write output to file\n");
+    fprintf(stderr, "\n");
+    fprintf(stderr,
+            "Instead of an url you can pass a repository name "
+            "with an optional path:\n");
+    fprintf(stderr, "        <repository>/path/\n");
+    fprintf(stderr, "\n");
 }
 
 void url_get_parts(char *url, char **root, char **path) {
@@ -78,9 +102,9 @@
     *path = NULL;
     
     int s;
-    if(ulen > 7 && !strncmp(url, "http://", 7)) {
+    if(ulen > 7 && !strncasecmp(url, "http://", 7)) {
         s = 7;
-    } else if(ulen > 8 && !strncmp(url, "https://", 8)) {
+    } else if(ulen > 8 && !strncasecmp(url, "https://", 8)) {
         s = 8;
     } else {
         s = 1;
@@ -105,15 +129,15 @@
 }
 
 
-int cmd_list(int argc, char **argv) {
-    if(argc == 0) {
+int cmd_list(CmdArgs *a) {
+    if(a->argc == 0) {
         return -1;
     }
     
     
     DavContext *ctx = dav_context_new();
     DavSession *sn = NULL;
-    char *url = argv[0];
+    char *url = a->argv[0];
     char *root = NULL;
     char *path = NULL;
     url_get_parts(url, &root, &path);
@@ -141,15 +165,15 @@
     return 0;
 }
 
-int cmd_get(int argc, char **argv) {
-    if(argc == 0) {
+int cmd_get(CmdArgs *a) {
+    if(a->argc == 0) {
         return -1;
     }
     
     DavContext *ctx = dav_context_new();
     dav_add_namespace(ctx, "U", "http://www.uap-core.de/");
     DavSession *sn = NULL;
-    char *url = argv[0];
+    char *url = a->argv[0];
     char *root = NULL;
     char *path = NULL;
     url_get_parts(url, &root, &path);
@@ -198,15 +222,15 @@
     return 0;
 }
 
-int cmd_put(int argc, char **argv) {
-    if(argc < 2) {
+int cmd_put(CmdArgs *a) {
+    if(a->argc < 2) {
         return -1;
     }
     
     DavContext *ctx = dav_context_new();
     DavSession *sn = NULL;
-    char *url = argv[0];
-    char *file = argv[1];
+    char *url = a->argv[0];
+    char *file = a->argv[1];
     char *root = NULL;
     char *path = NULL;
     url_get_parts(url, &root, &path);
--- a/dav/main.h	Mon Aug 12 19:50:14 2013 +0200
+++ b/dav/main.h	Tue Aug 13 11:19:22 2013 +0200
@@ -30,18 +30,17 @@
 #define	MAIN_H
 
 #include <curl/curl.h>
+#include "optparser.h"
 
 #ifdef	__cplusplus
 extern "C" {
 #endif
 
-void print_usage();
-void get_file(CURL *curl, char *url, char *path);
-void put_file(CURL *curl, char *url, char *path);
+void print_usage(char *cmd);
 
-int cmd_list(int argc, char **argv);
-int cmd_get(int argc, char **argv);
-int cmd_put(int argc, char **argv);
+int cmd_list(CmdArgs *args);
+int cmd_get(CmdArgs *args);
+int cmd_put(CmdArgs *args);
 
 #ifdef	__cplusplus
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dav/optparser.c	Tue Aug 13 11:19:22 2013 +0200
@@ -0,0 +1,106 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2012 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "optparser.h"
+
+void cmd_args_free(CmdArgs *args) {
+    ucx_map_free(args->options);
+    free(args->argv);
+}
+
+CmdArgs* cmd_parse_args(int argc, char **argv) {
+    CmdArgs *a = malloc(sizeof(CmdArgs));
+    a->options = ucx_map_new(16);
+    a->argv = calloc(argc, sizeof(char*));
+    a->argc = 0;
+    
+    char *option = NULL;
+    char optchar = 0;
+    for(int i=0;i<argc;i++) {
+        char *arg = argv[i];
+        size_t len = strlen(arg);
+        if(len > 1 && arg[0] == '-') {
+            for(int c=1;c<len;c++) {
+                switch(arg[c]) {
+                    default: {
+                        fprintf(stderr, "Unknown option -%c", arg[c]);
+                        cmd_args_free(a);
+                        return NULL;
+                    }
+                    case 'k': {
+                        if(!option) {
+                            option = "key";
+                            optchar = 'k';
+                        } else {
+                            fprintf(
+                                    stderr,
+                                    "Missing argument for option -%c\n",
+                                    optchar);
+                            cmd_args_free(a);
+                            return NULL;
+                        }
+                        break;
+                    }
+                    case 'p': {
+                        ucx_map_cstr_put(a->options, "plain", "");
+                        break;
+                    }
+                    case 'o': {
+                        if(!option) {
+                            option = "output";
+                            optchar = 'o';
+                        } else {
+                            fprintf(
+                                    stderr,
+                                    "Missing argument for option -%c\n",
+                                    optchar);
+                            cmd_args_free(a);
+                            return NULL;
+                        }
+                        break;
+                    }
+                }
+            }
+        } else if(option) {
+            ucx_map_cstr_put(a->options, option, arg);
+            option = NULL;
+        } else {
+            a->argv[a->argc++] = arg;
+        }
+    }
+    
+    return a;
+}
+
+char* cmd_getoption(CmdArgs *arg, char *name) {
+    return ucx_map_cstr_get(arg->options, name);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dav/optparser.h	Tue Aug 13 11:19:22 2013 +0200
@@ -0,0 +1,52 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2012 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef OPTPARSER_H
+#define	OPTPARSER_H
+
+#include <ucx/map.h>
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+typedef struct {
+    UcxMap *options;
+    char   **argv;
+    int    argc;
+} CmdArgs;
+
+CmdArgs* cmd_parse_args(int argc, char **argv);
+char* cmd_getoption(CmdArgs *arg, char *name);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* OPTPARSER_H */
+
--- a/dav/utils.c	Mon Aug 12 19:50:14 2013 +0200
+++ b/dav/utils.c	Tue Aug 13 11:19:22 2013 +0200
@@ -70,18 +70,26 @@
 
 char* util_url_path(char *url) { 
     char *path = NULL;
+    size_t len = strlen(url);
     int slashcount = 0;
+    int slmax;
+    if(len > 7 && !strncasecmp(url, "http://", 7)) {
+        slmax = 3;
+    } else if(len > 8 && !strncasecmp(url, "https://", 8)) {
+        slmax = 3;
+    } else {
+        slmax = 1;
+    }
     char c;
-    int i = 0;
-    while((c = url[i]) != 0) {
+    for(int i=0;i<len;i++) {
+        c = url[i];
         if(c == '/') {
             slashcount++;
-            if(slashcount == 3) {
+            if(slashcount == slmax) {
                 path = url + i;
                 break;
             }
         }
-        i++;
     } 
     return path;
 }
--- a/dav/webdav.c	Mon Aug 12 19:50:14 2013 +0200
+++ b/dav/webdav.c	Tue Aug 13 11:19:22 2013 +0200
@@ -317,7 +317,7 @@
     CURLcode ret = do_propfind_request(handle, rqbuf, rpbuf);
     if(ret == CURLE_OK) {
         //printf("response\n%s\n", rpbuf->space);
-        
+        // TODO: use parse_propfind_response()
         xmlDoc *doc = xmlReadMemory(rpbuf->space, rpbuf->size, url, NULL, 0);
         if(!doc) {
             return 1;
@@ -333,6 +333,8 @@
             }
             node = node->next;
         }
+        
+        set_davprops(res);
     }
     return 0;
 }

mercurial