/* ** This is a simple program used to retrieve an HTML document using ** HTTP. The program also fetches all images that the document ** references. */ #include #include #include "getpage.h" #define stricmp strcasecmp /* ** Each image to be loaded is an instance of the following structure. */ typedef struct Image Image; struct Image { char *zUrl; /* The URL for this image */ char *zLocal; /* The local filename */ Image *pNext; /* Next in a list of them all */ }; static FILE *html; /* Html output to this file. */ static int nImage = 0; /* Number of images loaded so far */ static Image *pImage; /* List of all images */ static global_nErr = 0; /* System wide errors */ static char baseUrl[1000];/* The base URL */ static int quiet = 0; /* The quiet flag */ /* ** Make sure the given URL is loaded as a local file. Return the ** name of the local file. */ static char *GetImage(char *zUrl){ Image *p; for(p=pImage; p; p=p->pNext){ if( strcmp(p->zUrl,zUrl)==0 ){ return p->zLocal; } } p = malloc( sizeof(*p) + strlen(zUrl) + 100 ); p->zUrl = (char*)&p[1]; strcpy(p->zUrl, zUrl); p->zLocal = &p->zUrl[strlen(zUrl)+1]; sprintf(p->zLocal,"image%d", ++nImage); p->pNext = pImage; pImage = p; HttpFetch(zUrl, p->zLocal, quiet, 0, 0); return p->zLocal; } /* ** Print a usage comment and exit */ void usage(char *argv0){ fprintf(stderr,"Usage: %s URL\n",argv0); exit(1); } /* ** Handle anything that isn't markup */ static void WordHandler(const char *zText, void *notUsed){ fprintf(html, zText); } /* ** Handle all markup that we don't care about. */ static void DefaultMarkup(int argc, const char **argv, void *notUsed){ int i; fprintf(html,"<%s",argv[0]); for(i=1; i"); } /* ** Handler for markup */ static void ImageMarkup(int argc, const char **argv, void *notUsed){ int i; for(i=1; i (%s)\n",baseUrl, azUrl[0], zResolved); } argv[i+1] = GetImage(zResolved); /* printf("%s -> %s -> argv[i+1]\n",argv[i+1], zResolved); */ free(zResolved); } } DefaultMarkup(argc, argv, 0); } /* ** Handler for markup */ static void BaseMarkup(int argc, const char **argv, void *notUsed){ int i; for(i=1; i