]> Nishi Git Mirror - libw3.git/commitdiff
adding
authornishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Sun, 28 Jan 2024 23:55:02 +0000 (23:55 +0000)
committernishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Sun, 28 Jan 2024 23:55:02 +0000 (23:55 +0000)
git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@124 d27a3e52-49c5-7645-884c-6793ebffc270

Example/Makefile
Example/w3b/Makefile [new file with mode: 0644]
Example/w3b/w3b.c [new file with mode: 0644]
Makefile

index 4b219b47a1dbd015a4a9938657738619a8f0ae2f..49d7a5c86734b562cd1c235900763159f3ec2fd1 100644 (file)
@@ -9,14 +9,17 @@ examples:
        $(MAKE) -C ./fetch CC=$(CC)
        $(MAKE) -C ./interactive CC=$(CC)
        $(MAKE) -C ./pop3-list CC=$(CC)
+       $(MAKE) -C ./w3b CC=$(CC)
 
 install:
        $(MAKE) -C ./fetch CC=$(CC) PREFIX=$(PREFIX) install
        $(MAKE) -C ./interactive CC=$(CC) PREFIX=$(PREFIX) install
        $(MAKE) -C ./pop3-list CC=$(CC) PREFIX=$(PREFIX) install
+       $(MAKE) -C ./w3b CC=$(CC) PREFIX=$(PREFIX) install
 
 clean:
        rm -f *.o *.so *.core *~ *.exe *.res
        $(MAKE) -C ./fetch clean
        $(MAKE) -C ./interactive clean
        $(MAKE) -C ./pop3-list clean
+       $(MAKE) -C ./w3b clean
diff --git a/Example/w3b/Makefile b/Example/w3b/Makefile
new file mode 100644 (file)
index 0000000..e811bca
--- /dev/null
@@ -0,0 +1,18 @@
+# $Id$
+.PHONY: clean install
+
+./w3b: ./w3b.o $(RESFILE)
+       $(CC) -g -o $@ -L ../../Library $^ -lw3
+
+./%.o: ./%.c
+       $(CC) -g -c -o $@ -I ../../Library $<
+
+../libw3.res:
+       $(MAKE) -C .. ./libw3.res WINDRES=$(WINDRES)
+
+clean:
+       rm -f w3b *.o *.so *.core *~ *.exe *.res
+
+install: ./w3b
+       mkdir -p $(PREFIX)/bin
+       cp ./w3b $(PREFIX)/bin/w3b
diff --git a/Example/w3b/w3b.c b/Example/w3b/w3b.c
new file mode 100644 (file)
index 0000000..79b16ac
--- /dev/null
@@ -0,0 +1,136 @@
+/*
+ * $Id$
+ *
+ * Simple browser
+ */
+
+#include <W3Core.h>
+#include <W3HTTP.h>
+
+#include <W3URL.h>
+#include <W3Util.h> /* It has some useful functions, you know */
+
+#include <ctype.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+char* databuf;
+int datalen;
+
+void status_handler(struct W3* w3, int status) { printf("Response code: %d\n", status); }
+void header_handler(struct W3* w3, char* key, char* value) { printf("Header: %s: %s\n", key, value); }
+
+void data_handler(struct W3* w3, char* data, size_t size) {
+       if(databuf == NULL) {
+               databuf = malloc(size);
+               datalen = size;
+               memcpy(databuf, data, size);
+       } else {
+               char* oldbuf = databuf;
+               databuf = malloc(datalen + size);
+               memcpy(databuf, oldbuf, datalen);
+               memcpy(databuf + datalen, data, size);
+               datalen += size;
+               free(oldbuf);
+       }
+}
+
+void access_site(const char* url) {
+       struct W3URL* u = W3_Parse_URL(url);
+       if(u != NULL) {
+               struct W3* w3 = W3_Create(u->protocol, u->host, u->port);
+               if(w3 != NULL) {
+                       if(databuf != NULL) {
+                               free(databuf);
+                       }
+                       databuf = NULL;
+                       datalen = 0;
+                       W3_Set_Method(w3, "GET");
+                       W3_Set_Path(w3, u->path);
+                       W3_HTTP_Enable_Redirect(w3);
+                       W3_On(w3, "status", (void*)status_handler);
+                       W3_On(w3, "header", (void*)header_handler);
+                       W3_On(w3, "data", (void*)data_handler);
+                       W3_Send_Request(w3);
+                       W3_Free(w3);
+                       printf("%d bytes\n", datalen);
+               } else {
+                       fprintf(stderr, "Failed to connect\n");
+               }
+               W3_Free_URL(u);
+       } else {
+               fprintf(stderr, "Failed to parse\n");
+       }
+}
+
+int main(int argc, char** argv) {
+       int i;
+       databuf = NULL;
+       datalen = 0;
+       char* url = NULL;
+       for(i = 1; i < argc; i++) {
+               if(strcmp(argv[i], "--version") == 0) {
+                       printf("LibW3 %s\n", LIBW3_VERSION);
+                       return 0;
+               } else if(argv[i][0] == '-') {
+                       fprintf(stderr, "%s: unknown option: %s\n", argv[0], argv[i]);
+                       return 1;
+               } else {
+                       if(url != NULL) {
+                               free(url);
+                               fprintf(stderr, "%s: garbage argument found\n", argv[0]);
+                               return 1;
+                       }
+                       url = __W3_Strdup(argv[i]);
+               }
+       }
+       W3_Library_Init();
+       int phase = 0;
+       char c = 0;
+       bool acc = false;
+       if(url != NULL) {
+               access_site(url);
+               acc = true;
+       }
+       while(true) { /* Loop */
+               if(c != '\n' && c != '\r') {
+                       printf("(O)pen, (Q)uit");
+                       if(acc) printf(", (P)rint all");
+                       printf("? ");
+                       fflush(stdout);
+               }
+               if(scanf("%c", &c) < 0) break;
+               switch(tolower(c)) {
+               case 'q':
+                       goto exitnow;
+               case 'o':
+                       printf("URL: ");
+                       fflush(stdout);
+                       if(url != NULL) free(url);
+                       url = malloc(2049);
+                       scanf("%s", url);
+                       acc = false;
+                       break;
+               case 'p':
+                       if(acc) {
+                               write(1, databuf, datalen);
+                       }
+                       break;
+               case '\n':
+               case '\r':
+                       break;
+               default:
+                       printf("What do you mean?\n");
+                       break;
+               }
+               if(!acc && url != NULL) {
+                       access_site(url);
+                       acc = true;
+               }
+       }
+       printf("\n");
+exitnow:;
+}
index a6086580f98baa42230d262c207cff0aa22a128e..0e5fa6458f6555336b00ae3a72d29c21bf30e59e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -112,6 +112,7 @@ install: ./w3.pc
 
 archive: all
        mkdir -p w3-$(VERSION)/Library
+       mkdir -p w3-$(VERSION)/Example/w3b
        mkdir -p w3-$(VERSION)/Example/pop3-list
        mkdir -p w3-$(VERSION)/Example/interactive
        mkdir -p w3-$(VERSION)/Example/fetch
@@ -122,11 +123,13 @@ ifdef WINDOWS
        cp ./Example/fetch/fetch.exe w3-$(VERSION)/Example/fetch/
        cp ./Example/interactive/interactive.exe w3-$(VERSION)/Example/interactive/
        cp ./Example/pop3-list/pop3-list.exe w3-$(VERSION)/Example/pop3-list/
+       cp ./Example/w3b/w3b.exe w3-$(VERSION)/Example/w3b/
 else
        cp ./Library/*.so w3-$(VERSION)/Library/
        cp ./Example/fetch/fetch w3-$(VERSION)/Example/fetch/
        cp ./Example/interactive/interactive w3-$(VERSION)/Example/interactive/
        cp ./Example/pop3-list/pop3-list w3-$(VERSION)/Example/pop3-list/
+       cp ./Example/w3b/w3b w3-$(VERSION)/Example/w3b/
 endif
        -mv w3-$(VERSION)/*.h w3-$(VERSION)/Library/
        -mv w3-$(VERSION)/*.so w3-$(VERSION)/Library/