Annotation of libwww/Robot/src/HTRobot.c, revision 1.32
1.1 frystyk 1: /* HTRobot.c
2: ** W3C MINI ROBOT
3: **
4: ** (c) COPRIGHT MIT 1995.
5: ** Please first read the full copyright statement in the file COPYRIGH.
6: **
7: ** This program illustrates how to travers links using the Anchor object
8: **
9: ** Authors:
10: ** HFN Henrik Frystyk Nielsen, (frystyk@w3.org)
11: **
12: ** History:
13: ** Dec 04 95 First version
14: */
15:
16: #include "WWWLib.h" /* Global Library Include file */
17: #include "WWWApp.h" /* Application stuff */
1.17 frystyk 18: #include "WWWTrans.h"
1.10 frystyk 19: #include "WWWInit.h"
1.9 frystyk 20:
1.4 frystyk 21: #include "HText.h"
1.1 frystyk 22:
23: #include "HTRobot.h" /* Implemented here */
24:
1.14 frystyk 25: #ifndef W3C_VERSION
26: #define W3C_VERSION "unspecified"
1.1 frystyk 27: #endif
28:
29: #define APP_NAME "W3CRobot"
1.14 frystyk 30: #define APP_VERSION W3C_VERSION
1.1 frystyk 31:
32: #define DEFAULT_OUTPUT_FILE "robot.out"
33: #define DEFAULT_RULE_FILE "robot.conf"
34: #define DEFAULT_LOG_FILE "robot.log"
1.7 frystyk 35: #define DEFAULT_DEPTH 0
1.1 frystyk 36:
37: #define SHOW_MSG (WWWTRACE || HTAlert_interactive())
38:
1.7 frystyk 39: #define DEFAULT_TIMEOUT 10 /* timeout in seconds */
1.1 frystyk 40:
41: #if defined(__svr4__)
42: #define CATCH_SIG
43: #endif
44:
45: typedef enum _MRFlags {
1.2 frystyk 46: MR_IMG = 0x1,
47: MR_LINK = 0x2,
1.12 frystyk 48: MR_PREEMPTIVE= 0x4,
49: MR_TIME = 0x8
1.1 frystyk 50: } MRFlags;
51:
52: typedef struct _Robot {
53: HTRequest * request;
1.7 frystyk 54: HTRequest * timeout; /* Until we get a server eventloop */
1.1 frystyk 55: HTParentAnchor * anchor;
1.2 frystyk 56: int depth; /* How deep is our tree */
1.30 frystyk 57: int cnt; /* Count of requests */
1.2 frystyk 58: HTList * hyperdoc; /* List of our HyperDoc Objects */
1.4 frystyk 59: HTList * htext; /* List of our HText Objects */
1.1 frystyk 60: struct timeval * tv; /* Timeout on socket */
61: char * cwd; /* Current dir URL */
62: char * rules;
63: char * logfile;
64: char * outputfile;
65: FILE * output;
66: MRFlags flags;
67: } Robot;
68:
69: typedef enum _LoadState {
70: L_INVALID = -2,
71: L_LOADING = -1,
72: L_SUCCESS = 0,
73: L_ERROR
74: } LoadState;
75:
76: /*
77: ** The HyperDoc object is bound to the anchor and contains information about
78: ** where we are in the search for recursive searches
79: */
80: typedef struct _HyperDoc {
81: HTParentAnchor * anchor;
82: LoadState state;
83: int depth;
84: } HyperDoc;
85:
86: /*
87: ** This is the HText object that is created every time we start parsing a
88: ** HTML object
89: */
1.4 frystyk 90: struct _HText {
1.1 frystyk 91: HTRequest * request;
1.4 frystyk 92: };
1.1 frystyk 93:
94: PUBLIC HText * HTMainText = NULL;
95: PUBLIC HTParentAnchor * HTMainAnchor = NULL;
96: PUBLIC HTStyleSheet * styleSheet = NULL;
97:
98: /* ------------------------------------------------------------------------- */
99:
1.13 eric 100: /* Standard (non-error) Output
101: ** ---------------------------
102: */
103: PUBLIC int OutputData(const char * fmt, ...)
104: {
105: int ret;
106: va_list pArgs;
107: va_start(pArgs, fmt);
108: ret = vfprintf(stdout, fmt, pArgs);
109: va_end(pArgs);
110: return ret;
111: }
112:
113: /* ------------------------------------------------------------------------- */
114:
1.2 frystyk 115: /* Create a "HyperDoc" object
116: ** --------------------------
117: ** A HyperDoc object contains information about whether we have already
118: ** started checking the anchor and the depth in our search
119: */
120: PRIVATE HyperDoc * HyperDoc_new (Robot * mr,HTParentAnchor * anchor, int depth)
121: {
122: HyperDoc * hd;
1.14 frystyk 123: if ((hd = (HyperDoc *) HT_CALLOC(1, sizeof(HyperDoc))) == NULL)
124: HT_OUTOFMEM("HyperDoc_new");
1.2 frystyk 125: hd->state = L_INVALID;
126: hd->depth = depth;
127:
128: /* Bind the HyperDoc object together with the Anchor Object */
129: hd->anchor = anchor;
130: HTAnchor_setDocument(anchor, (void *) hd);
131:
132: /* Add this HyperDoc object to our list */
133: if (!mr->hyperdoc) mr->hyperdoc = HTList_new();
134: HTList_addObject(mr->hyperdoc, (void *) hd);
135: return hd;
136: }
137:
138: /* Delete a "HyperDoc" object
139: ** --------------------------
140: */
141: PRIVATE BOOL HyperDoc_delete (HyperDoc * hd)
142: {
143: if (hd) {
1.11 frystyk 144: HT_FREE (hd);
1.2 frystyk 145: return YES;
146: }
147: return NO;
148: }
149:
1.1 frystyk 150: /* Create a Command Line Object
151: ** ----------------------------
152: */
153: PRIVATE Robot * Robot_new (void)
154: {
155: Robot * me;
1.14 frystyk 156: if ((me = (Robot *) HT_CALLOC(1, sizeof(Robot))) == NULL ||
157: (me->tv = (struct timeval*) HT_CALLOC(1, sizeof(struct timeval))) == NULL)
158: HT_OUTOFMEM("Robot_new");
1.2 frystyk 159: me->hyperdoc = HTList_new();
1.4 frystyk 160: me->htext = HTList_new();
1.1 frystyk 161: me->tv->tv_sec = DEFAULT_TIMEOUT;
1.25 frystyk 162: me->cwd = HTGetCurrentDirectoryURL();
1.1 frystyk 163: me->output = OUTPUT;
1.30 frystyk 164: me->cnt = 1;
1.1 frystyk 165:
1.7 frystyk 166: /* We keep an extra timeout request object for the timeout_handler */
167: me->timeout = HTRequest_new();
168: HTRequest_setContext (me->timeout, me);
169:
1.1 frystyk 170: /* Bind the Robot object together with the Request Object */
171: me->request = HTRequest_new();
172: HTRequest_setContext (me->request, me);
173: return me;
174: }
175:
176: /* Delete a Command Line Object
177: ** ----------------------------
178: */
179: PRIVATE BOOL Robot_delete (Robot * me)
180: {
181: if (me) {
1.2 frystyk 182: if (me->hyperdoc) {
183: HTList * cur = me->hyperdoc;
184: HyperDoc * pres;
185: while ((pres = (HyperDoc *) HTList_nextObject(cur)))
186: HyperDoc_delete(pres);
187: HTList_delete(me->hyperdoc);
188: }
1.4 frystyk 189: if (me->htext) {
190: HTList * cur = me->htext;
191: HText * pres;
192: while ((pres = (HText *) HTList_nextObject(cur)))
193: HText_free(pres);
194: HTList_delete(me->htext);
195: }
1.1 frystyk 196: if (me->logfile) HTLog_close();
197: if (me->output && me->output != STDOUT) fclose(me->output);
1.12 frystyk 198: if (me->flags & MR_TIME) {
199: time_t local = time(NULL);
1.13 eric 200: HTTrace("Robot terminated %s\n",HTDateTimeStr(&local,YES));
1.12 frystyk 201: }
1.11 frystyk 202: HT_FREE(me->cwd);
203: HT_FREE(me->tv);
204: HT_FREE(me);
1.1 frystyk 205: return YES;
206: }
207: return NO;
208: }
209:
1.2 frystyk 210: /*
211: ** This function creates a new request object and initializes it
212: */
213: PRIVATE HTRequest * Thread_new (Robot * mr, HTMethod method)
214: {
215: HTRequest * newreq = HTRequest_new();
216: HTRequest_setContext (newreq, mr);
1.7 frystyk 217: if (mr->flags & MR_PREEMPTIVE) HTRequest_setPreemptive(newreq, YES);
1.5 frystyk 218: HTRequest_addRqHd(newreq, HT_C_HOST);
1.2 frystyk 219: HTRequest_setMethod(newreq, method);
1.30 frystyk 220: mr->cnt++;
1.2 frystyk 221: return newreq;
222: }
223:
224: PRIVATE BOOL Thread_delete (Robot * mr, HTRequest * request)
225: {
226: if (mr && request) {
227: HTRequest_delete(request);
1.30 frystyk 228: mr->cnt--;
1.2 frystyk 229: return YES;
230: }
231: return NO;
232: }
233:
234: /*
235: ** Cleanup and make sure we close all connections including the persistent
236: ** ones
237: */
1.1 frystyk 238: PRIVATE void Cleanup (Robot * me, int status)
239: {
240: Robot_delete(me);
1.29 eric 241: HTProfile_delete();
1.1 frystyk 242: #ifdef VMS
243: exit(status ? status : 1);
244: #else
245: exit(status ? status : 0);
246: #endif
247: }
248:
249: #ifdef CATCH_SIG
250: #include <signal.h>
251: /* SetSignal
252: ** This function sets up signal handlers. This might not be necessary to
253: ** call if the application has its own handlers (lossage on SVR4)
254: */
255: PRIVATE void SetSignal (void)
256: {
257: /* On some systems (SYSV) it is necessary to catch the SIGPIPE signal
258: ** when attemting to connect to a remote host where you normally should
259: ** get `connection refused' back
260: */
261: if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
1.13 eric 262: if (PROT_TRACE) HTTrace("HTSignal.... Can't catch SIGPIPE\n");
1.1 frystyk 263: } else {
1.13 eric 264: if (PROT_TRACE) HTTrace("HTSignal.... Ignoring SIGPIPE\n");
1.1 frystyk 265: }
266: }
267: #endif /* CATCH_SIG */
268:
269: PRIVATE void VersionInfo (void)
270: {
1.13 eric 271: OutputData("\n\nW3C Reference Software\n\n");
272: OutputData("\tW3C Mini Robot (%s) version %s.\n",
1.1 frystyk 273: APP_NAME, APP_VERSION);
1.13 eric 274: OutputData("\tW3C Reference Library version %s.\n\n",HTLib_version());
275: OutputData("Please send feedback to <libwww@w3.org>\n");
1.1 frystyk 276: }
277:
278: /* terminate_handler
279: ** -----------------
1.2 frystyk 280: ** This function is registered to handle the result of the request.
281: ** If no more requests are pending then terminate program
1.1 frystyk 282: */
1.32 ! frystyk 283: PRIVATE int terminate_handler (HTRequest * request, HTResponse * response,
! 284: void * param, int status)
1.1 frystyk 285: {
286: Robot * mr = (Robot *) HTRequest_context(request);
1.2 frystyk 287: Thread_delete(mr, request);
1.30 frystyk 288: if (HTNet_isEmpty()) {
289: if (SHOW_MSG) HTTrace("Robot....... Everything is finished...\n");
290: Cleanup(mr, 0);
291: }
292: if (SHOW_MSG) HTTrace("Robot....... %d outstanding requests\n", mr->cnt);
1.1 frystyk 293: return HT_OK;
294: }
295:
296: /* timeout_handler
297: ** ---------------
298: ** This function is registered to handle timeout in select eventloop
1.7 frystyk 299: **
300: ** BUG: This doesn't work as we don't get the right request object
301: ** back from the event loop
1.1 frystyk 302: */
303: PRIVATE int timeout_handler (HTRequest * request)
304: {
1.27 frystyk 305: #if 0
1.2 frystyk 306: Robot * mr = (Robot *) HTRequest_context(request);
1.27 frystyk 307: #endif
1.25 frystyk 308: if (SHOW_MSG) HTTrace("Robot....... We don't know how to handle timeout...\n");
1.7 frystyk 309: #if 0
1.1 frystyk 310: HTRequest_kill(request);
1.2 frystyk 311: Thread_delete(mr, request);
1.7 frystyk 312: #endif
1.4 frystyk 313: return HT_OK;
1.1 frystyk 314: }
315:
316: /* ------------------------------------------------------------------------- */
317: /* HTEXT INTERFACE */
318: /* ------------------------------------------------------------------------- */
319:
320: PUBLIC HText * HText_new2 (HTRequest * request, HTParentAnchor * anchor,
321: HTStream * stream)
322: {
323: HText * me;
1.4 frystyk 324: Robot * mr = (Robot *) HTRequest_context(request);
1.14 frystyk 325: if ((me = (HText *) HT_CALLOC(1, sizeof(HText))) == NULL)
326: HT_OUTOFMEM("HText_new2");
1.4 frystyk 327:
328: /* Bind the HText object together with the Request Object */
1.1 frystyk 329: me->request = request;
1.4 frystyk 330:
331: /* Add this HyperDoc object to our list */
332: if (!mr->htext) mr->htext = HTList_new();
333: HTList_addObject(mr->htext, (void *) me);
1.1 frystyk 334: return me;
335: }
336:
1.4 frystyk 337: PUBLIC void HText_free (HText * me) {
1.11 frystyk 338: if (me) HT_FREE (me);
1.4 frystyk 339: }
340:
1.1 frystyk 341: PUBLIC void HText_beginAnchor (HText * text, HTChildAnchor * anchor)
342: {
343: if (text && anchor) {
1.2 frystyk 344: Robot * mr = (Robot *) HTRequest_context(text->request);
1.1 frystyk 345: HTAnchor * dest = HTAnchor_followMainLink((HTAnchor *) anchor);
346: HTParentAnchor * dest_parent = HTAnchor_parent(dest);
1.7 frystyk 347: char * uri = HTAnchor_address((HTAnchor *) dest_parent);
1.1 frystyk 348: HyperDoc * hd = HTAnchor_document(dest_parent);
349:
1.13 eric 350: if (SHOW_MSG) HTTrace("Robot....... Found `%s\' - ", uri ? uri : "NULL");
1.7 frystyk 351:
1.2 frystyk 352: /* Test whether we already have a hyperdoc for this document */
353: if (mr->flags & MR_LINK && dest_parent && !hd) {
1.1 frystyk 354: HTParentAnchor * parent = HTRequest_parent(text->request);
355: HyperDoc * last = HTAnchor_document(parent);
356: int depth = last ? last->depth+1 : 0;
1.2 frystyk 357: HTRequest * newreq = Thread_new(mr, METHOD_GET);
358: HyperDoc_new(mr, dest_parent, depth);
1.7 frystyk 359: HTRequest_setParent(newreq, HTRequest_anchor(text->request));
360: if (depth >= mr->depth) {
361: if (SHOW_MSG)
1.13 eric 362: HTTrace("loading at depth %d using HEAD\n", depth);
1.7 frystyk 363: HTRequest_setMethod(newreq, METHOD_HEAD);
1.30 frystyk 364: HTRequest_setOutputFormat(newreq, WWW_DEBUG);
1.7 frystyk 365: } else {
1.13 eric 366: if (SHOW_MSG) HTTrace("loading at depth %d\n", depth);
1.2 frystyk 367: }
368: if (HTLoadAnchor((HTAnchor *) dest_parent, newreq) != YES) {
1.13 eric 369: if (SHOW_MSG) HTTrace("not tested!\n");
1.2 frystyk 370: Thread_delete(mr, newreq);
371: }
1.7 frystyk 372: } else {
1.18 frystyk 373: if (SHOW_MSG) HTTrace("duplicate or max depth reached\n");
1.2 frystyk 374: }
1.11 frystyk 375: HT_FREE(uri);
1.2 frystyk 376: }
377: }
378:
379: PUBLIC void HText_appendImage (HText * text, HTChildAnchor * anchor,
1.14 frystyk 380: const char *alt, const char * align, BOOL isMap)
1.2 frystyk 381: {
382: if (text && anchor) {
383: Robot * mr = (Robot *) HTRequest_context(text->request);
384: HTParentAnchor * dest = (HTParentAnchor *)
385: HTAnchor_followMainLink((HTAnchor *) anchor);
386: HyperDoc * hd = HTAnchor_document(dest);
1.1 frystyk 387:
1.2 frystyk 388: /* Test whether we already have a hyperdoc for this document */
389: if (mr->flags & MR_IMG && dest && !hd) {
390: HTParentAnchor * parent = HTRequest_parent(text->request);
391: HyperDoc * last = HTAnchor_document(parent);
392: int depth = last ? last->depth+1 : 0;
393: HTRequest * newreq = Thread_new(mr, METHOD_HEAD);
394: HyperDoc_new(mr, dest, depth);
395: if (SHOW_MSG) {
396: char * uri = HTAnchor_address((HTAnchor *) dest);
1.13 eric 397: HTTrace("Robot....... Checking Image `%s\'\n", uri);
1.11 frystyk 398: HT_FREE(uri);
1.2 frystyk 399: }
400: if (HTLoadAnchor((HTAnchor *) dest, newreq) != YES) {
401: if (SHOW_MSG)
1.13 eric 402: HTTrace("Robot....... Image not tested!\n");
1.2 frystyk 403: Thread_delete(mr, newreq);
1.1 frystyk 404: }
405: }
406: }
407: }
408:
409: PUBLIC void HText_endAnchor (HText * text) {}
1.14 frystyk 410: PUBLIC void HText_appendText (HText * text, const char * str) {}
1.1 frystyk 411: PUBLIC void HText_appendCharacter (HText * text, char ch) {}
412: PUBLIC void HText_endAppend (HText * text) {}
413: PUBLIC void HText_setStyle (HText * text, HTStyle * style) {}
414: PUBLIC void HText_beginAppend (HText * text) {}
415: PUBLIC void HText_appendParagraph (HText * text) {}
416:
417: /* ------------------------------------------------------------------------- */
418: /* MAIN PROGRAM */
419: /* ------------------------------------------------------------------------- */
420:
421: int main (int argc, char ** argv)
422: {
423: int status = 0;
424: int arg;
425: HTChunk * keywords = NULL; /* From command line */
426: int keycnt = 0;
1.12 frystyk 427: Robot * mr = NULL;
1.1 frystyk 428:
429: /* Starts Mac GUSI socket library */
430: #ifdef GUSI
431: GUSISetup(GUSIwithSIOUXSockets);
432: GUSISetup(GUSIwithInternetSockets);
433: #endif
434:
435: #ifdef __MWERKS__ /* STR */
436: InitGraf((Ptr) &qd.thePort);
437: InitFonts();
438: InitWindows();
439: InitMenus(); TEInit();
440: InitDialogs(nil);
441: InitCursor();
442: SIOUXSettings.asktosaveonclose = false;
443: argc=ccommand(&argv);
444: #endif
445:
1.27 frystyk 446: /* Initiate W3C Reference Library with a robot profile */
447: HTProfile_newRobot(APP_NAME, APP_VERSION);
448:
449: /* Add the default HTML parser to the set of converters */
450: {
451: HTList * converters = HTFormat_conversion();
452: HTMLInit(converters);
453: }
1.1 frystyk 454:
1.12 frystyk 455: /* Build a new robot object */
456: mr = Robot_new();
457:
1.1 frystyk 458: /* Scan command Line for parameters */
459: for (arg=1; arg<argc; arg++) {
460: if (*argv[arg] == '-') {
461:
462: /* non-interactive */
1.17 frystyk 463: if (!strcmp(argv[arg], "-n")) {
1.1 frystyk 464: HTAlert_setInteractive(NO);
465:
466: /* log file */
467: } else if (!strcmp(argv[arg], "-l")) {
468: mr->logfile = (arg+1 < argc && *argv[arg+1] != '-') ?
469: argv[++arg] : DEFAULT_LOG_FILE;
470:
471: /* rule file */
472: } else if (!strcmp(argv[arg], "-r")) {
473: mr->rules = (arg+1 < argc && *argv[arg+1] != '-') ?
474: argv[++arg] : DEFAULT_RULE_FILE;
475:
476: /* output filename */
477: } else if (!strcmp(argv[arg], "-o")) {
478: mr->outputfile = (arg+1 < argc && *argv[arg+1] != '-') ?
479: argv[++arg] : DEFAULT_OUTPUT_FILE;
480:
481: /* timeout -- Change the default request timeout */
482: } else if (!strcmp(argv[arg], "-timeout")) {
483: int timeout = (arg+1 < argc && *argv[arg+1] != '-') ?
484: atoi(argv[++arg]) : DEFAULT_TIMEOUT;
485: if (timeout > 0) mr->tv->tv_sec = timeout;
486:
1.7 frystyk 487: /* preemptive or non-preemptive access */
1.1 frystyk 488: } else if (!strcmp(argv[arg], "-single")) {
1.7 frystyk 489: HTRequest_setPreemptive(mr->request, YES);
490: mr->flags |= MR_PREEMPTIVE;
1.2 frystyk 491:
492: /* test inlined images */
493: } else if (!strcmp(argv[arg], "-img")) {
494: mr->flags |= MR_IMG;
495:
496: /* load anchors */
497: } else if (!strcmp(argv[arg], "-link")) {
498: mr->flags |= MR_LINK;
1.7 frystyk 499: mr->depth = (arg+1 < argc && *argv[arg+1] != '-') ?
500: atoi(argv[++arg]) : DEFAULT_DEPTH;
1.2 frystyk 501:
1.7 frystyk 502: /* preemptive or non-preemptive access */
1.2 frystyk 503: } else if (!strcmp(argv[arg], "-single")) {
1.7 frystyk 504: HTRequest_setPreemptive(mr->request, YES);
505: mr->flags |= MR_PREEMPTIVE;
1.1 frystyk 506:
1.12 frystyk 507: /* Output start and end time */
508: } else if (!strcmp(argv[arg], "-ss")) {
509: time_t local = time(NULL);
1.13 eric 510: HTTrace("Robot started on %s\n",
1.12 frystyk 511: HTDateTimeStr(&local, YES));
512: mr->flags |= MR_TIME;
513:
1.1 frystyk 514: /* print version and exit */
515: } else if (!strcmp(argv[arg], "-version")) {
516: VersionInfo();
517: Cleanup(mr, 0);
518:
519: #ifdef WWWTRACE
520: /* trace flags */
521: } else if (!strncmp(argv[arg], "-v", 2)) {
1.24 frystyk 522: HTSetTraceMessageMask(argv[arg]+2);
1.1 frystyk 523: #endif
524:
525: } else {
1.13 eric 526: if (SHOW_MSG) HTTrace("Bad Argument (%s)\n", argv[arg]);
1.1 frystyk 527: }
1.17 frystyk 528: } else { /* If no leading `-' then check for URL or keywords */
1.1 frystyk 529: if (!keycnt) {
530: char * ref = HTParse(argv[arg], mr->cwd, PARSE_ALL);
531: mr->anchor = (HTParentAnchor *) HTAnchor_findAddress(ref);
1.7 frystyk 532: HyperDoc_new(mr, mr->anchor, 0);
1.1 frystyk 533: keycnt = 1;
1.11 frystyk 534: HT_FREE(ref);
1.1 frystyk 535: } else { /* Check for successive keyword arguments */
536: char *escaped = HTEscape(argv[arg], URL_XALPHAS);
537: if (keycnt++ <= 1)
1.5 frystyk 538: keywords = HTChunk_new(128);
1.1 frystyk 539: else
1.5 frystyk 540: HTChunk_putc(keywords, ' ');
541: HTChunk_puts(keywords, HTStrip(escaped));
1.11 frystyk 542: HT_FREE(escaped);
1.1 frystyk 543: }
544: }
545: }
546:
547: #ifdef CATCH_SIG
548: SetSignal();
549: #endif
550:
551: if (!keycnt) {
1.13 eric 552: if (SHOW_MSG) HTTrace("Please specify URL to check.\n");
1.1 frystyk 553: Cleanup(mr, -1);
554: }
555:
1.23 manoli 556: /* Testing that HTTrace is working */
557: HTTrace ("Welcome to the W3C mini Robot\n");
558:
1.1 frystyk 559: /* Rule file specified? */
560: if (mr->rules) {
561: char * rules = HTParse(mr->rules, mr->cwd, PARSE_ALL);
1.27 frystyk 562: if (!HTLoadRules(rules))
1.13 eric 563: if (SHOW_MSG) HTTrace("Can't access rules\n");
1.11 frystyk 564: HT_FREE(rules);
1.1 frystyk 565: }
566:
567: /* Output file specified? */
568: if (mr->outputfile) {
569: if ((mr->output = fopen(mr->outputfile, "wb")) == NULL) {
1.13 eric 570: if (SHOW_MSG) HTTrace("Can't open `%s'\n", mr->outputfile);
1.1 frystyk 571: mr->output = OUTPUT;
572: }
573: }
574:
575: /* Log file specifed? */
576: if (mr->logfile) HTLog_open(mr->logfile, YES, YES);
577:
1.27 frystyk 578: /* Register our own someterminater filter */
1.32 ! frystyk 579: HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
1.1 frystyk 580:
581: /* Set timeout on sockets */
1.16 frystyk 582: HTEventrg_registerTimeout(mr->tv, mr->timeout, timeout_handler, NO);
1.1 frystyk 583:
584: /* Start the request */
585: if (keywords) /* Search */
1.28 frystyk 586: status = HTSearchAnchor(keywords, (HTAnchor *)mr->anchor, mr->request);
1.1 frystyk 587: else
588: status = HTLoadAnchor((HTAnchor *) mr->anchor, mr->request);
589:
1.5 frystyk 590: if (keywords) HTChunk_delete(keywords);
1.1 frystyk 591: if (status != YES) {
1.13 eric 592: if (SHOW_MSG) HTTrace("Can't access resource\n");
1.1 frystyk 593: Cleanup(mr, -1);
594: }
595:
596: /* Go into the event loop... */
1.16 frystyk 597: HTEventrg_loop(mr->request);
1.1 frystyk 598:
599: /* Only gets here if event loop fails */
600: Cleanup(mr, 0);
601: return 0;
602: }
Webmaster