cli.c - frontends - front-ends for some sites (experiment) (DIR) Log (DIR) Files (DIR) Refs (DIR) README (DIR) LICENSE --- cli.c (2498B) --- 1 #include <sys/socket.h> 2 #include <sys/types.h> 3 4 #include <ctype.h> 5 #include <errno.h> 6 #include <netdb.h> 7 #include <stdarg.h> 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <string.h> 11 #include <time.h> 12 #include <unistd.h> 13 14 #include "https.h" 15 #include "reddit.h" 16 #include "util.h" 17 18 #define OUT(s) (fputs((s), stdout)) 19 #define OUTESCAPE(s) (printescape(s)) 20 21 /* print: ignore control-characters */ 22 void 23 printescape(const char *s) 24 { 25 for (; *s; ++s) 26 if (!iscntrl((unsigned char)*s)) 27 fputc(*s, stdout); 28 } 29 30 void 31 printitem(struct item *item) 32 { 33 if (!item || !item->title[0]) 34 return; 35 36 printf("title: %s\n", item->title); 37 printf("url: %s\n", item->url); 38 printf("subreddit: %s\n", item->subreddit); 39 printf("author: %s\n", item->author); 40 printf("thumbnail: %s\n", item->thumbnail); 41 printf("ups: %ld\n", item->ups); 42 printf("downs: %ld\n", item->downs); 43 printf("num_comments: %ld\n", item->num_comments); 44 45 struct tm *tm = gmtime(&(item->created_utc)); 46 printf("created_utc: %lld\n", (long long)item->created_utc); 47 printf("created_utc: %s", asctime(tm)); 48 49 if (item->is_video) { 50 printf("is_video: %d\n", item->is_video); 51 printf("duration: %ld\n", item->duration); 52 printf("dash_url: %s\n", item->dash_url); 53 } 54 55 printf("===\n"); 56 } 57 58 int 59 render(struct list_response *r) 60 { 61 size_t i; 62 63 if (pledge("stdio", NULL) == -1) { 64 fprintf(stderr, "pledge: %s\n", strerror(errno)); 65 exit(1); 66 } 67 68 for (i = 0; i < r->nitems; i++) 69 printitem(&(r->items[i])); 70 71 if (r->before[0]) 72 printf("before pagination token: %s\n", r->before); 73 if (r->after[0]) 74 printf("after pagination token: %s\n", r->after); 75 76 return 0; 77 } 78 79 static void 80 usage(const char *argv0) 81 { 82 fprintf(stderr, "usage: %s [subreddit]\n", argv0); 83 exit(1); 84 } 85 86 int 87 main(int argc, char *argv[]) 88 { 89 struct list_response *r; 90 char subreddit[1024] = ""; 91 92 if (pledge("stdio dns inet rpath unveil", NULL) == -1) { 93 fprintf(stderr, "pledge: %s\n", strerror(errno)); 94 exit(1); 95 } 96 if (unveil(TLS_CA_CERT_FILE, "r") == -1) { 97 fprintf(stderr, "unveil: %s\n", strerror(errno)); 98 exit(1); 99 } 100 if (unveil(NULL, NULL) == -1) { 101 fprintf(stderr, "unveil: %s\n", strerror(errno)); 102 exit(1); 103 } 104 105 if (argc > 1) { 106 if (!uriencode(argv[1], subreddit, sizeof(subreddit))) 107 usage(argv[0]); 108 } 109 110 r = reddit_list(subreddit, 100, "", ""); /* TODO: pagination */ 111 if (!r || r->nitems == 0) { 112 OUT("No items found\n"); 113 exit(1); 114 } 115 116 render(r); 117 118 return 0; 119 }