Annotation of libwww/Robot/src/HTRobot.c, revision 1.30

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

Webmaster