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

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

Webmaster