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

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

Webmaster