1 #ifndef I_WMGET_H
2 #define I_WMGET_H
3 /*
4     wmget - A background download manager as a Window Maker dock app
5     Copyright (c) 2001-2003 Aaron Trickey <aaron@amtrickey.net>
6 
7     Permission is hereby granted, free of charge, to any person
8     obtaining a copy of this software and associated documentation files
9     (the "Software"), to deal in the Software without restriction,
10     including without limitation the rights to use, copy, modify, merge,
11     publish, distribute, sublicense, and/or sell copies of the Software,
12     and to permit persons to whom the Software is furnished to do so,
13     subject to the following conditions:
14 
15     The above copyright notice and this permission notice shall be
16     included in all copies or substantial portions of the Software.
17 
18     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21     NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22     CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23     TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 
26     ********************************************************************
27     wmget.h - Common definitions for all wmget modules
28 
29     This file defines the public entry points in each *.c file in the
30     main wmget program, as well as some common constants, global
31     variables, and the structure of the shared memory segment.
32 */
33 
34 
35 #include <sys/param.h>
36 #include <curl/curl.h>
37 
38 /* Important: the Makefile greps the source to extract WMGET_VERSION.
39  * So don't change its format or anything without checking there first.
40  */
41 #define WMGET_VERSION           "0.6.0"
42 #define WMGET_VERSION_BANNER    "wmget " WMGET_VERSION \
43                                 ", compiled with libcurl " \
44                                         LIBCURL_VERSION
45 #define WMGET_COPYRIGHT         "Copyright (c) 2001-2003 " \
46                                  "Aaron Trickey <aaron@amtrickey.net>" \
47                                  "; ABSOLUTELY NO WARRANTY"
48 
49 
50 #define DEFAULT_USER_AGENT \
51     "wmget/" WMGET_VERSION " (libcurl/" LIBCURL_VERSION ")"
52 
53 #define MAXRCLINELEN    1024
54 #define MAXURL          1024
55 #define MAXUA           255
56 #define MAXAUTH         100
57 #define MAXIF           255
58 #define MAXCMDLEN       2048
59 #define MAXCMDARGS      20
60 #define MAX_DISPLAY     9
61 
62 #define MAX_ACTIVE_JOBS 4       /* a number constrained by the UI */
63 #define MAX_QUEUED_JOBS 20
64 
65 /* Command language */
66 
67 /* The GET command and its arguments: */
68 #define CMD_GET                 "GET"
69 #define ARG_GET_SOURCE_URL       "FROM"
70 #define ARG_GET_DISPLAY          "DISP"
71 #define ARG_GET_SAVE_TO          "TO"
72 #define ARG_GET_OVERWRITE        "OVER"
73 #define ARG_GET_CONTINUE_FROM    "CONT"
74 #define ARG_GET_PROXY            "PROXY"
75 #define ARG_GET_FOLLOW           "FOLLOW"
76 #define ARG_GET_UA               "UA"
77 #define ARG_GET_USE_ASCII        "ASCII"
78 #define ARG_GET_REFERER          "REF"
79 #define ARG_GET_INCLUDE          "INCL"
80 #define ARG_GET_INTERFACE        "IF"
81 #define ARG_GET_PROXY_AUTH       "PROXY-AUTH"
82 #define ARG_GET_AUTH             "AUTH"
83 
84 #define CMD_CANCEL              "CANCEL"
85 #define ARG_CANCEL_JOBID         "JOBID"
86 
87 #define CMD_LIST                "LIST"
88 
89 #define RESPONSE_JOB_ACCEPTED   "ACCEPTED"
90 #define RESPONSE_JOB_CANCELED   "CANCELED"
91 #define RESPONSE_LIST_COMING    "JOBLIST"
92 #define RESPONSE_ERROR          "ERROR"
93 
94 
95 /* Debug trace text output levels */
96 typedef enum {
97     OL_SILENT,
98     OL_NORMAL,
99     OL_DEBUG,
100 } OutputLevel;
101 
102 
103 /* The various states an individual job may be in.
104  */
105 typedef enum {
106     J_EMPTY,        /* this job slot is empty */
107     J_INIT,         /* slot has been taken, job not yet started */
108     J_RUNNING,      /* this job is running */
109     J_PAUSED,       /* this job is paused */
110     J_STOPPING,     /* a stop request has come from the user */
111     J_COMPLETE,     /* job complete, cleaning up */
112     J_FAILED,       /* job failed! */
113 } JobStatus;
114 
115 
116 /* The type of a job ID; these are allocated by the dockapp and are
117  * never reused within its lifetime.
118  */
119 typedef unsigned long job_id_t;
120 
121 
122 /* User-configurable job options.
123  */
124 typedef struct {
125     char  display[MAX_DISPLAY + 1]; /* Text to display */
126     char  save_to[MAXPATHLEN + 1];  /* Full pathname to save to */
127                                     /* (For srvr, this MUST be a dir) */
128     int   overwrite;                /* Allow overwrite of save_to? */
129     int   continue_from;            /* Byte# to resume from */
130     char  proxy[MAXURL + 1];        /* Proxy to use (or empty string) */
131     int   follow;                   /* How many redirects to follow */
132     char  user_agent[MAXUA + 1];    /* User-agent string to provide */
133 
134     int   use_ascii;                /* Force FTP to ASCII */
135     char  referer[MAXURL + 1];      /* Specify referer */
136     int   include;                  /* Include HTTP headers in output */
137     char  interface[MAXIF + 1];     /* Limit to given interface */
138     char  proxy_auth[MAXAUTH + 1];  /* Proxy authentication */
139     char  auth[MAXAUTH + 1];        /* Site authentication */
140 
141 } JobOptions;
142 
143 
144 /* A command-line download request.  Strings are NULL if defaulted;
145  * integers are -1.
146  */
147 typedef struct {
148     const char *source_url;         /* MANDATORY.  Duh. */
149     const char *display;
150     const char *save_to;
151     int         overwrite;
152     int         continue_from;
153     const char *proxy;
154     int         follow;
155     const char *user_agent;
156 
157     int         use_ascii;
158     const char *referer;
159     int         include;
160     const char *interface;
161     const char *proxy_auth;
162     const char *auth;
163 
164 } Request;
165 
166 
167 /* The totality of a running or queued job:
168  */
169 typedef struct {
170     job_id_t job_id;
171     JobStatus status;
172     char error[CURL_ERROR_SIZE + 1];
173     unsigned long progress;
174     unsigned long prog_max;
175     int stop_request;
176     char source_url[MAXURL + 1];    /* URL to fetch */
177 
178     JobOptions options;
179 } Job;
180 
181 
182 /* The shared-memory structure containing the active job list.  (Pending
183  * jobs are queued in the dockapp's private data segment.)
184  */
185 typedef struct {
186     Job jobs[MAX_ACTIVE_JOBS];
187 } Shmem;
188 
189 
190 /* Specifies the server configuration.  This is used only by server.c,
191  * and gets populated by config_server() in config.c.
192  */
193 typedef struct {
194     JobOptions job_defaults;
195 } ServerConfig;
196 
197 
198 /* Convenience macro
199  */
200 #define STRCPY_TO_ARRAY(to,from) \
201     do { \
202         strncpy (to, from, sizeof to); \
203         to[sizeof to - 1] = '\0'; \
204     } while (0)
205 
206 
207 /* configure.c */
208 extern void config_server (int argc, char **argv, ServerConfig *cfg);
209 extern void clear_request (Request *req);
210 extern void config_request (int argc, char **argv, Request *req);
211 
212 /* usage.c */
213 extern void usage (void);
214 
215 /* iq.c */
216 extern int iq_server_init (void);       /* called once, by server  */
217 extern FILE *iq_server_accept (void);   /* returns new cxn or NULL */
218 extern FILE *iq_client_connect (void);  /* called by each client   */
219 extern int iq_get_listen_fd (void);     /* so you can select/poll  */
220 
221 /* server.c */
222 extern Shmem *shmem;
223 extern int server (int argc, char **argv);
224 
225 /* request.c */
226 extern int request (int argc, char **argv);
227 
228 /* cancel.c */
229 extern int cancel (int argc, char **argv);
230 
231 /* list.c */
232 extern int list (int argc, char **argv);
233 
234 /* retrieve.c */
235 extern int retrieve (Job *job);
236 
237 /* wmget.c */
238 extern const char *home_directory (void);
239 extern void debug_dump_job (Job *job);
240 
241 /* messages.c */
242 extern void set_output_level (OutputLevel lev);
243 extern OutputLevel output_level (void);
244 extern void error (const char *fmt, ...);
245 extern void error_sys (const char *fmt, ...);
246 extern void info (const char *fmt, ...);
247 extern void debug (const char *fmt, ...);
248 extern void debug_sys (const char *fmt, ...);
249 
250 
251 #endif /* I_WMGET_H */
252