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

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

Webmaster