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

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

Webmaster