1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
/* $Id: backend.c,v 1.15 2010/08/08 09:53:42 htrb Exp $ */
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "fm.h"
#include <gc.h>
#include "terms.h"
/* Prototype declaration of internal functions */
#ifdef HAVE_READLINE
#include <readline/readline.h>
#else /* ! HAVE_READLINE */
static char *readline(char *);
#endif /* ! HAVE_READLINE */
static TextList *split(char *);
/* Prototype declaration of command functions */
static void get(TextList *);
static void post(TextList *);
static void set(TextList *);
static void show(TextList *);
static void quit(TextList *);
static void help(TextList *);
/* *INDENT-OFF* */
/* Table of command functions */
struct {
const char *name;
const char *option_string;
const char *help;
void (*func)(TextList*);
} command_table[] = {
{"get", "[-download_only] URL", "Retrieve URL.", get},
{"post", "[-download_only] [-target TARGET] [-charset CHARSET]"
" [-enctype ENCTYPE] [-body BODY] [-boundary BOUNDARY] [-length LEN] URL",
"Retrieve URL.", post},
{"set", "VARIABLE VALUE", "Set VALUE to VARIABLE.", set},
{"show", "VARIABLE", "Show value of VARIABLE.", show},
{"quit", "", "Quit program.", quit},
{"help", "", "Display help messages.", help},
{NULL, NULL, NULL, NULL},
};
/* *INDENT-ON* */
/* Prototype declaration of functions to manipulate configuration variables */
static void set_column(TextList *);
static void show_column(TextList *);
/* *INDENT-OFF* */
/* Table of configuration variables */
struct {
const char *name;
void (*set_func)(TextList*);
void (*show_func)(TextList*);
} variable_table[] = {
{"column", set_column, show_column},
{NULL, NULL, NULL},
};
/* *INDENT-ON* */
static void
print_headers(Buffer *buf, int len)
{
TextListItem *tp;
if (buf->document_header) {
for (tp = buf->document_header->first; tp; tp = tp->next)
printf("%s\n", tp->ptr);
}
printf("w3m-current-url: %s\n", parsedURL2Str(&buf->currentURL)->ptr);
if (buf->baseURL)
printf("w3m-base-url: %s\n", parsedURL2Str(buf->baseURL)->ptr);
printf("w3m-content-type: %s\n", buf->type);
#ifdef USE_M17N
if (buf->document_charset)
printf("w3m-content-charset: %s\n",
wc_ces_to_charset(buf->document_charset));
#endif
if (len > 0)
printf("w3m-content-length: %d\n", len);
}
static void
internal_get(char *url, int flag, FormList *request)
{
Buffer *buf;
backend_halfdump_buf = NULL;
do_download = flag;
buf = loadGeneralFile(url, NULL, NO_REFERER, 0, request);
do_download = FALSE;
if (buf != NULL && buf != NO_BUFFER) {
if (is_html_type(buf->type) && backend_halfdump_buf) {
TextLineListItem *p;
Str first, last;
int len = 0;
for (p = backend_halfdump_buf->first; p; p = p->next) {
p->ptr->line = Str_conv_to_halfdump(p->ptr->line);
len += p->ptr->line->length + 1;
}
first = Strnew_charp("<pre>\n");
last = Strnew_m_charp("</pre><title>", html_quote(buf->buffername),
"</title>\n", NULL);
print_headers(buf, len + first->length + last->length);
printf("\n");
printf("%s", first->ptr);
for (p = backend_halfdump_buf->first; p; p = p->next)
printf("%s\n", p->ptr->line->ptr);
printf("%s", last->ptr);
}
else {
if (!strcasecmp(buf->type, "text/plain")) {
Line *lp;
int len = 0;
for (lp = buf->firstLine; lp; lp = lp->next) {
len += lp->len;
if (lp->lineBuf[lp->len - 1] != '\n')
len++;
}
print_headers(buf, len);
printf("\n");
saveBuffer(buf, stdout, TRUE);
}
else {
print_headers(buf, 0);
}
}
}
}
/* Command: get */
static void
get(TextList *argv)
{
char *p, *url = NULL;
int flag = FALSE;
while ((p = popText(argv))) {
if (!strcasecmp(p, "-download_only"))
flag = TRUE;
else
url = p;
}
if (url) {
internal_get(url, flag, NULL);
}
}
/* Command: post */
static void
post(TextList *argv)
{
FormList *request;
char *p, *target = NULL, *charset = NULL,
*enctype = NULL, *body = NULL, *boundary = NULL, *url = NULL;
int flag = FALSE, length = 0;
while ((p = popText(argv))) {
if (!strcasecmp(p, "-download_only"))
flag = TRUE;
else if (!strcasecmp(p, "-target"))
target = popText(argv);
else if (!strcasecmp(p, "-charset"))
charset = popText(argv);
else if (!strcasecmp(p, "-enctype"))
enctype = popText(argv);
else if (!strcasecmp(p, "-body"))
body = popText(argv);
else if (!strcasecmp(p, "-boundary"))
boundary = popText(argv);
else if (!strcasecmp(p, "-length"))
length = atol(popText(argv));
else
url = p;
}
if (url) {
request =
newFormList(NULL, "post", charset, enctype, target, NULL, NULL);
request->body = body;
request->boundary = boundary;
request->length = (length > 0) ? length : (body ? strlen(body) : 0);
internal_get(url, flag, request);
}
}
/* Command: set */
static void
set(TextList *argv)
{
if (argv->nitem > 1) {
int i;
for (i = 0; variable_table[i].name; i++) {
if (!strcasecmp(variable_table[i].name, argv->first->ptr)) {
popText(argv);
if (variable_table[i].set_func)
variable_table[i].set_func(argv);
break;
}
}
}
}
/* Command: show */
static void
show(TextList *argv)
{
if (argv->nitem >= 1) {
int i;
for (i = 0; variable_table[i].name; i++) {
if (!strcasecmp(variable_table[i].name, argv->first->ptr)) {
popText(argv);
if (variable_table[i].show_func)
variable_table[i].show_func(argv);
break;
}
}
}
}
/* Command: quit */
static void
quit(TextList *argv)
{
#ifdef USE_COOKIE
save_cookies();
#endif /* USE_COOKIE */
w3m_exit(0);
}
/* Command: help */
static void
help(TextList *argv)
{
int i;
for (i = 0; command_table[i].name; i++)
printf("%s %s\n %s\n",
command_table[i].name,
command_table[i].option_string, command_table[i].help);
}
/* Sub command: set COLS */
static void
set_column(TextList *argv)
{
if (argv->nitem == 1) {
COLS = atol(argv->first->ptr);
}
}
/* Sub command: show COLS */
static void
show_column(TextList *argv)
{
fprintf(stdout, "column=%d\n", COLS);
}
/* Call appropriate command function based on given string */
static void
call_command_function(char *str)
{
int i;
TextList *argv = split(str);
if (argv->nitem > 0) {
for (i = 0; command_table[i].name; i++) {
if (!strcasecmp(command_table[i].name, argv->first->ptr)) {
popText(argv);
if (command_table[i].func)
command_table[i].func(argv);
break;
}
}
}
}
/* Main function */
int
backend(void)
{
char *str;
w3m_dump = 0;
if (COLS == 0)
COLS = DEFAULT_COLS;
#ifdef USE_MOUSE
use_mouse = FALSE;
#endif /* USE_MOUSE */
if (backend_batch_commands) {
while ((str = popText(backend_batch_commands)))
call_command_function(str);
}
else {
while ((str = readline("w3m> ")))
call_command_function(str);
}
quit(NULL);
return 0;
}
/* Dummy function of readline(). */
#ifndef HAVE_READLINE
static char *
readline(char *prompt)
{
Str s;
fputs(prompt, stdout);
fflush(stdout);
s = Strfgets(stdin);
if (feof(stdin) && (strlen(s->ptr) == 0))
return NULL;
else
return s->ptr;
}
#endif /* ! HAVE_READLINE */
/* Splits a string into a list of tokens and returns that list. */
static TextList *
split(char *p)
{
int in_double_quote = FALSE, in_single_quote = FALSE;
Str s = Strnew();
TextList *tp = newTextList();
for (; *p; p++) {
switch (*p) {
case '"':
if (in_single_quote)
Strcat_char(s, '"');
else
in_double_quote = !in_double_quote;
break;
case '\'':
if (in_double_quote)
Strcat_char(s, '\'');
else
in_single_quote = !in_single_quote;
break;
case '\\':
if (!in_single_quote) {
/* Process escape characters. */
p++;
switch (*p) {
case 't':
Strcat_char(s, '\t');
break;
case 'r':
Strcat_char(s, '\r');
break;
case 'f':
Strcat_char(s, '\f');
break;
case 'n':
Strcat_char(s, '\n');
break;
case '\0':
goto LAST;
default:
Strcat_char(s, *p);
}
}
else {
Strcat_char(s, *p);
}
break;
case ' ':
case '\t':
case '\r':
case '\f':
case '\n':
/* Separators are detected. */
if (in_double_quote || in_single_quote) {
Strcat_char(s, *p);
}
else if (s->length > 0) {
pushText(tp, s->ptr);
s = Strnew();
}
break;
default:
Strcat_char(s, *p);
}
}
LAST:
if (s->length > 0)
pushText(tp, s->ptr);
return tp;
}
|