]>
git.datanom.net - caldav.git/blob - test-app/cmdline-parser.c
4 * Copyright 2017 Michael Rasmussen <mir@datanom.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
31 #include <sys/types.h>
35 #include "cmdline-parser.h"
38 static const gchar
* help
[] = {
39 "Usage: test-app [Options] -o operation -p password -u username URL",
41 "a|data Use this option to insert calendar data from file.",
42 " As an alternative use redirect IO with <.",
43 "d|debug Print a lot of debug information to STDOUT",
44 "e|etag ETag to use for update and delete",
45 "f|finish End time interval in ISO 8601 local time. 2017-01-13T13:13:13",
46 "h|help Show this help info.",
47 "r|href HREFs to use for getobjects. Each href on a separate line.",
48 " As an alternative use redirect IO with <.",
49 "s|start Start time interval in ISO 8601 local time. 2017-01-13T13:13:13",
53 static void show_help() {
54 for (int i
= 0; help
[i
]; i
++) {
55 printf("%s\n", help
[i
]);
59 static void get_operation(const gchar
* op
, Operation
* o
) {
61 if (g_strcmp0("CALENDARINFO", op
) == 0) {
63 } else if (g_strcmp0("GETOBJECTS", op
) == 0) {
65 } else if (g_strcmp0("GETALLOBJECTS", op
) == 0) {
67 } else if (g_strcmp0("SIMPLEGET", op
) == 0) {
69 } else if (g_strcmp0("CHANGEINFO", op
) == 0) {
71 } else if (g_strcmp0("UPDATEOBJECTS", op
) == 0) {
73 } else if (g_strcmp0("ADDOBJECTS", op
) == 0) {
75 } else if (g_strcmp0("DELETEOBJECTS", op
) == 0) {
77 } else if (g_strcmp0("DISCOVER", op
) == 0) {
79 } else if (g_strcmp0("OPTIONSINFO", op
) == 0) {
81 } else if (g_strcmp0("FREEBUSY", op
) == 0) {
89 static gboolean
invalid_test_case(Runtime
* test
) {
90 gboolean invalid
= TRUE
;
91 gboolean error
= FALSE
;
94 if (!test
->username
) {
95 printf("Missing username\n");
98 if (!test
->password
) {
99 printf("Missing password\n");
103 printf("Missing URL\n");
106 if (!test
->operation
) {
107 printf("Missing operation\n");
110 invalid
= (error
) ? TRUE
: FALSE
;
116 #define BUF_SIZE 1024
117 static gchar
* read_stream(FILE* stream
) {
118 gchar buffer
[BUF_SIZE
];
120 gchar
* content
= g_malloc0(sizeof(gchar
) * BUF_SIZE
);
122 while (fgets(buffer
, BUF_SIZE
, stream
) != NULL
) {
123 size
+= strlen(buffer
);
124 content
= g_realloc(content
, size
);
125 g_strlcat(content
, buffer
, size
);
131 static GSList
* parse_input(const gchar
* input
) {
134 gchar
** strings
= g_strsplit(input
, "\n", 0);
135 for (gchar
** h
= strings
; *h
; ++h
) {
136 if (g_strcmp0(*h
, "") != 0) {
137 HrefData
* hd
= href_data_new(*h
, NULL
);
138 list
= g_slist_append(list
, hd
);
146 Runtime
* parse_cmdline(int argc
, char **argv
) {
149 Runtime
* test
= runtime_new();
150 Operation
* op
= g_new(Operation
, 1);
153 struct option long_options
[] = {
154 {"data", required_argument
, 0, 'a'},
155 {"etag", required_argument
, 0, 'e'},
156 {"finish", required_argument
, 0, 'f'},
157 {"debug", no_argument
, &debug_flag
, 1 },
158 {"help", no_argument
, 0, 'h'},
159 {"operation", required_argument
, 0, 'o'},
160 {"password", required_argument
, 0, 'p'},
161 {"href", required_argument
, 0, 'r'},
162 {"start", required_argument
, 0, 's'},
163 {"username", required_argument
, 0, 'u'},
167 int option_index
= 0;
168 while ((opt
= getopt_long(argc
, argv
, "a:de:f:h:o:p:rs:u:",
169 long_options
, &option_index
)) != -1) {
172 stream
= fopen(optarg
, "r");
186 test
->etag
= g_strdup(optarg
);
190 test
->finish
= get_date_time_from_string(optarg
);
199 get_operation(optarg
, op
);
200 if (op
&& *op
!= UNKNOWN
) {
201 test
->operation
= op
;
204 fprintf(stderr
, "%s: Invalid operation\n", optarg
);
211 test
->password
= g_strdup(optarg
);
215 stream
= fopen(optarg
, "r");
225 test
->start
= get_date_time_from_string(optarg
);
229 test
->username
= g_strdup(optarg
);
233 /* missing option argument */
235 printf("%s: option '-%c' requires an argument\n", argv
[0], optopt
);
243 fprintf(stderr
, "%s: option '-%c' is invalid: ignored\n", argv
[0], optopt
);
249 if (optind
+ 1 < argc
) {
251 printf("Only one URL allowed\n");
256 test
->url
= g_strdup(argv
[optind
]);
258 if (invalid_test_case(test
)) {
264 if (*test
->operation
== ADDOBJECTS
|| *test
->operation
== UPDATEOBJECTS
) {
266 if (fstat(fileno(stdin
), &sb
) == -1) {
273 if (stream
&& sb
.st_size
> 0) {
274 fprintf(stderr
, "Error: Option --data or --href active. Cannot redirect STDIN\n");
278 stream
= (stream
) ? stream
: stdin
;
282 gchar
* text
= read_stream(stream
);
284 switch (*test
->operation
) {
286 test
->hrefs
= parse_input(text
);
291 test
->component
= text
;
294 fprintf(stderr
, "%i: Input not allowed\n", *test
->operation
);
301 test
->debug
= (debug_flag
) ? TRUE
: FALSE
;
This page took 0.080634 seconds and 6 git commands to generate.