1 /* $Id: backend.c,v 1.13 2003/09/22 21:02:16 ukai Exp $ */
10 /* Prototype declaration of internal functions */
12 #include <readline/readline.h>
13 #else /* ! HAVE_READLINE */
14 static char *readline(char *);
15 #endif /* ! HAVE_READLINE */
16 static TextList *split(char *);
19 /* Prototype declaration of command functions */
20 static void get(TextList *);
21 static void post(TextList *);
22 static void set(TextList *);
23 static void show(TextList *);
24 static void quit(TextList *);
25 static void help(TextList *);
29 /* Table of command functions */
32 const char *option_string;
34 void (*func)(TextList*);
36 {"get", "[-download_only] URL", "Retrieve URL.", get},
37 {"post", "[-download_only] [-target TARGET] [-charset CHARSET]"
38 " [-enctype ENCTYPE] [-body BODY] [-boundary BOUNDARY] [-length LEN] URL",
39 "Retrieve URL.", post},
40 {"set", "VARIABLE VALUE", "Set VALUE to VARIABLE.", set},
41 {"show", "VARIABLE", "Show value of VARIABLE.", show},
42 {"quit", "", "Quit program.", quit},
43 {"help", "", "Display help messages.", help},
44 {NULL, NULL, NULL, NULL},
48 /* Prototype declaration of functions to manipulate configuration variables */
49 static void set_column(TextList *);
50 static void show_column(TextList *);
54 /* Table of configuration variables */
57 void (*set_func)(TextList*);
58 void (*show_func)(TextList*);
59 } variable_table[] = {
60 {"column", set_column, show_column},
66 print_headers(Buffer *buf, int len)
70 if (buf->document_header) {
71 for (tp = buf->document_header->first; tp; tp = tp->next)
72 printf("%s\n", tp->ptr);
74 printf("w3m-current-url: %s\n", parsedURL2Str(&buf->currentURL)->ptr);
76 printf("w3m-base-url: %s\n", parsedURL2Str(buf->baseURL)->ptr);
77 printf("w3m-content-type: %s\n", buf->type);
79 if (buf->document_charset)
80 printf("w3m-content-charset: %s\n",
81 wc_ces_to_charset(buf->document_charset));
84 printf("w3m-content-length: %d\n", len);
89 internal_get(char *url, int flag, FormList *request)
93 backend_halfdump_buf = NULL;
95 buf = loadGeneralFile(url, NULL, NO_REFERER, 0, request);
97 if (buf != NULL && buf != NO_BUFFER) {
98 if (!strcasecmp(buf->type, "text/html") && backend_halfdump_buf) {
102 for (p = backend_halfdump_buf->first; p; p = p->next) {
103 p->ptr->line = Str_conv_to_halfdump(p->ptr->line);
104 len += p->ptr->line->length + 1;
106 first = Strnew_charp("<pre>\n");
107 last = Strnew_m_charp("</pre><title>", html_quote(buf->buffername),
109 print_headers(buf, len + first->length + last->length);
111 printf("%s", first->ptr);
112 for (p = backend_halfdump_buf->first; p; p = p->next)
113 printf("%s\n", p->ptr->line->ptr);
114 printf("%s", last->ptr);
117 if (!strcasecmp(buf->type, "text/plain")) {
120 for (lp = buf->firstLine; lp; lp = lp->next) {
122 if (lp->lineBuf[lp->len - 1] != '\n')
125 print_headers(buf, len);
127 saveBuffer(buf, stdout, TRUE);
130 print_headers(buf, 0);
141 char *p, *url = NULL;
144 while ((p = popText(argv))) {
145 if (!strcasecmp(p, "-download_only"))
151 internal_get(url, flag, NULL);
161 char *p, *target = NULL, *charset = NULL,
162 *enctype = NULL, *body = NULL, *boundary = NULL, *url = NULL;
163 int flag = FALSE, length = 0;
165 while ((p = popText(argv))) {
166 if (!strcasecmp(p, "-download_only"))
168 else if (!strcasecmp(p, "-target"))
169 target = popText(argv);
170 else if (!strcasecmp(p, "-charset"))
171 charset = popText(argv);
172 else if (!strcasecmp(p, "-enctype"))
173 enctype = popText(argv);
174 else if (!strcasecmp(p, "-body"))
175 body = popText(argv);
176 else if (!strcasecmp(p, "-boundary"))
177 boundary = popText(argv);
178 else if (!strcasecmp(p, "-length"))
179 length = atol(popText(argv));
185 newFormList(NULL, "post", charset, enctype, target, NULL, NULL);
186 request->body = body;
187 request->boundary = boundary;
188 request->length = (length > 0) ? length : (body ? strlen(body) : 0);
189 internal_get(url, flag, request);
198 if (argv->nitem > 1) {
200 for (i = 0; variable_table[i].name; i++) {
201 if (!strcasecmp(variable_table[i].name, argv->first->ptr)) {
203 if (variable_table[i].set_func)
204 variable_table[i].set_func(argv);
216 if (argv->nitem >= 1) {
218 for (i = 0; variable_table[i].name; i++) {
219 if (!strcasecmp(variable_table[i].name, argv->first->ptr)) {
221 if (variable_table[i].show_func)
222 variable_table[i].show_func(argv);
236 #endif /* USE_COOKIE */
246 for (i = 0; command_table[i].name; i++)
247 printf("%s %s\n %s\n",
248 command_table[i].name,
249 command_table[i].option_string, command_table[i].help);
253 /* Sub command: set COLS */
255 set_column(TextList *argv)
257 if (argv->nitem == 1) {
258 COLS = atol(argv->first->ptr);
262 /* Sub command: show COLS */
264 show_column(TextList *argv)
266 fprintf(stdout, "column=%d\n", COLS);
270 /* Call appropriate command function based on given string */
272 call_command_function(char *str)
275 TextList *argv = split(str);
276 if (argv->nitem > 0) {
277 for (i = 0; command_table[i].name; i++) {
278 if (!strcasecmp(command_table[i].name, argv->first->ptr)) {
280 if (command_table[i].func)
281 command_table[i].func(argv);
300 #endif /* USE_MOUSE */
302 if (backend_batch_commands) {
303 while ((str = popText(backend_batch_commands)))
304 call_command_function(str);
307 while ((str = readline("w3m> ")))
308 call_command_function(str);
315 /* Dummy function of readline(). */
316 #ifndef HAVE_READLINE
318 readline(char *prompt)
321 fputs(prompt, stdout);
324 if (feof(stdin) && (strlen(s->ptr) == 0))
329 #endif /* ! HAVE_READLINE */
332 /* Splits a string into a list of tokens and returns that list. */
336 int in_double_quote = FALSE, in_single_quote = FALSE;
338 TextList *tp = newTextList();
346 in_double_quote = !in_double_quote;
350 Strcat_char(s, '\'');
352 in_single_quote = !in_single_quote;
355 if (!in_single_quote) {
356 /* Process escape characters. */
360 Strcat_char(s, '\t');
363 Strcat_char(s, '\r');
366 Strcat_char(s, '\f');
369 Strcat_char(s, '\n');
386 /* Separators are detected. */
387 if (in_double_quote || in_single_quote) {
390 else if (s->length > 0) {
391 pushText(tp, s->ptr);
401 pushText(tp, s->ptr);