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

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

Webmaster