1 /*
2    cadaver, command-line DAV client
3    Copyright (C) 1999-2005, 2008, Joe Orton <joe@orton.demon.co.uk>
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 
19 */
20 
21 #ifndef CADAVER_H
22 #define CADAVER_H
23 
24 #include <ne_session.h> /* for ne_session */
25 #include <ne_locks.h> /* for ne_lock_store */
26 
27 #ifdef HAVE_LIBREADLINE
28 
29 /* readline requires FILE *, silly thing */
30 #include <stdio.h>
31 
32 #ifdef HAVE_READLINE_H
33 #include <readline.h>
34 #else /* !HAVE_READLINE_H */
35 #ifdef HAVE_READLINE_READLINE_H
36 #include <readline/readline.h>
37 #endif
38 #endif /* HAVE_READLINE_H */
39 
40 #ifdef HAVE_HISTORY_H
41 #include <history.h>
42 #else
43 #ifdef HAVE_READLINE_HISTORY_H
44 #include <readline/history.h>
45 #endif
46 #endif /* HAVE_HISTORY_H */
47 
48 #endif
49 
50 #include "common.h"
51 
52 /* DON'T FORGET TO ADD A NEW COMMAND ALIAS WHEN YOU ADD A NEW COMMAND */
53 enum command_id {
54     cmd_ls, cmd_cd, cmd_quit, cmd_open, cmd_close, cmd_about, cmd_pwd,
55     cmd_help, cmd_put, cmd_get, cmd_mkcol, cmd_delete, cmd_move, cmd_copy,
56     cmd_less, cmd_cat, cmd_lpwd, cmd_lcd, cmd_lls, cmd_mput, cmd_mget,
57     cmd_echo, cmd_set, cmd_unset, cmd_rmcol, cmd_lock, cmd_unlock,
58     cmd_steal, cmd_discover, cmd_showlocks, cmd_propedit, cmd_propnames,
59     cmd_propget, cmd_propset, cmd_propdel, cmd_chexec, cmd_edit, cmd_logout,
60     cmd_describe, cmd_search, cmd_version, cmd_checkin, cmd_checkout,
61     cmd_uncheckout, cmd_history, cmd_label,
62     cmd_unknown
63 /* DON'T FORGET TO ADD A NEW COMMAND ALIAS WHEN YOU ADD A NEW COMMAND */
64 };
65 
66 struct command {
67     enum command_id id; /* unique ID */
68     const char *name; /* real command name. */
69     unsigned int needs_connection; /* true if only works when connected */
70     int min_args, max_args; /* min and max # of arguments */
71     enum { /* parameter scope */
72 	parmscope_none, /* not at all */
73 	parmscope_option, /* option parameter */
74 	parmscope_local, /* local filenames */
75 	parmscope_remote /* remote filename */
76     } scope;
77     union {
78 	void (*take0)(void);
79 	void (*take1)(const char *);
80 	void (*take2)(const char *, const char *);
81 	void (*take3)(const char *, const char *, const char *);
82 	void (*takeV)(int, const char **);
83     } handler;
84     const char *call; /* command usage */
85     const char *short_help; /* single-line help message */
86 };
87 
88 extern char *proxy_hostname;
89 extern int proxy_port;
90 
91 struct session {
92     ne_uri uri;
93     ne_session *sess;
94     int connected; /* non-zero when connected. */
95     int isdav; /* non-zero if real DAV collection */
96     ne_lock_store *locks; /* stored locks */
97     char *lastwp; /* last working path. */
98 };
99 
100 /* Current session state. */
101 extern struct session session;
102 
103 /* Sets the current collection to the given path.  Returns zero on
104  * success, non-zero if newpath is an untolerated non-WebDAV
105  * collection. */
106 int set_path(const char *newpath);
107 
108 extern int tolerant;
109 
110 #ifdef HAVE_LIBREADLINE
111 char *command_generator(const char *text, int state);
112 #else
113 /* roll our own */
114 char *readline(const char *prompt);
115 #endif
116 
117 enum resource_type {
118     resr_normal = 0,
119     resr_collection,
120     resr_reference,
121     resr_error
122 };
123 
124 #ifdef HAVE_UNSIGNED_LONG_LONG
125 typedef unsigned long long dav_size_t;
126 #define FMT_DAV_SIZE_T "ll"
127 #ifdef HAVE_STRTOULL
128 #define DAV_STRTOL strtoull
129 #endif
130 #else
131 typedef unsigned long dav_size_t;
132 #define FMT_DAV_SIZE_T "l"
133 #endif
134 
135 #ifndef DAV_STRTOL
136 #define DAV_STRTOL strtol
137 #endif
138 
139 struct resource {
140     char *uri;
141     char *displayname;
142     enum resource_type type;
143     dav_size_t size;
144     time_t modtime;
145     int is_executable;
146     int is_vcr;    /* Is version resource. 0: no vcr, 1 checkin 2 checkout */
147     char *error_reason; /* error string returned for this resource */
148     int error_status; /* error status returned for this resource */
149     struct resource *next;
150 };
151 
152 void close_connection(void);
153 void open_connection(const char *url);
154 
155 void execute_ls(const char *remote);
156 void execute_edit(const char *remote);
157 
158 /* Determine whether the resource is a version controlled resource
159  * 1: Checkin, 2: Checkout, 0: Otherwise */
160 int is_vcr(const char *remote);
161 
162 void execute_version(const char *remote);
163 void execute_checkin(const char *remote);
164 void execute_checkout(const char *remote);
165 void execute_uncheckout(const char *remote);
166 void execute_history(const char *remote);
167 
168 void execute_label(const char *res, const char *act, const char *value);
169 
170 void execute_search(int count, const char **args);
171 
172 void free_resource(struct resource *res);
173 void free_resource_list(struct resource *res);
174 
175 int fetch_resource_list(ne_session *sess, const char *uri,
176 			int depth, int include_uri,
177 			struct resource **reslist);
178 
179 /* Command feedback handling */
180 enum output_type {
181     o_start,
182     o_upload,
183     o_download,
184     o_finish
185 };
186 
187 void output(enum output_type, const char *fmt, ...)
188 #ifdef __GNUC__
189                 __attribute__ ((format (printf, 2, 3)))
190 #endif /* __GNUC__ */
191 ;
192 
193 #endif /* CADAVER_H */
194 
195