gopher.c - frontends - front-ends for some sites (experiment)
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       gopher.c (1753B)
       ---
            1 #include <sys/types.h>
            2 
            3 #include <ctype.h>
            4 #include <errno.h>
            5 #include <locale.h>
            6 #include <stdio.h>
            7 #include <stdlib.h>
            8 #include <string.h>
            9 #include <unistd.h>
           10 #include <wchar.h>
           11 
           12 #include "duckduckgo.h"
           13 #include "https.h"
           14 #include "util.h"
           15 
           16 #define OUT(s) fputs(s, stdout)
           17 #define OUTTEXT(s) gophertext(stdout, s, strlen(s))
           18 
           19 static const char *host = "127.0.0.1", *port = "70";
           20 
           21 int
           22 main(void)
           23 {
           24         struct duckduckgo_results *results;
           25         struct duckduckgo_result *result;
           26         char *p, *search = NULL;
           27         size_t i;
           28 
           29         setlocale(LC_CTYPE, "");
           30 
           31         if ((p = getenv("SERVER_NAME")))
           32                 host = p;
           33         if ((p = getenv("SERVER_PORT")))
           34                 port = p;
           35         if (!(p = getenv("X_GOPHER_SEARCH"))) /* geomyidae */
           36                 p = getenv("SEARCHREQUEST"); /* gophernicus */
           37         if (p)
           38                 search = p;
           39 
           40         if (pledge("stdio dns inet rpath unveil", NULL) == -1) {
           41                 fprintf(stderr, "pledge: %s\n", strerror(errno));
           42                 exit(1);
           43         }
           44         if (unveil(TLS_CA_CERT_FILE, "r") == -1) {
           45                 fprintf(stderr, "unveil: %s\n", strerror(errno));
           46                 exit(1);
           47         }
           48         if (unveil(NULL, NULL) == -1) {
           49                 fprintf(stderr, "unveil: %s\n", strerror(errno));
           50                 exit(1);
           51         }
           52 
           53         if (search == NULL) {
           54                 printf("3\tSpecify a search term\t%s\t%s\r\n", host, port);
           55                 printf(".\r\n");
           56                 exit(1);
           57         }
           58 
           59         if ((results = duckduckgo_search(search))) {
           60                 for (i = 0; i < results->nitems; i++) {
           61                         result = &(results->items[i]);
           62 
           63                         OUT("h");
           64                         OUTTEXT(result->title);
           65                         OUT("\tURL:");
           66                         OUTTEXT(result->urldecoded);
           67                         printf("\t%s\t%s\r\n", host, port);
           68 
           69                         /* TODO: multi-line wrap ? */
           70                         OUT("h");
           71                         OUTTEXT(result->description);
           72                         OUT("\tURL:");
           73                         OUTTEXT(result->urldecoded);
           74                         printf("\t%s\t%s\r\n", host, port);
           75 
           76                         printf("i\t\t%s\t%s\r\n", host, port);
           77                         printf("i\t\t%s\t%s\r\n", host, port);
           78                 }
           79         }
           80         printf(".\r\n");
           81 
           82         return 0;
           83 }