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

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

Webmaster