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

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

Webmaster