youtube/cli: use ctype macros, add shortdescription in TSV, fix comment - front… | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 6242ec658d5610990c6b19944473fa7277493ac1 | |
parent 86b28b384e968fd3eecbe0c33924d2489a067421 | |
Author: Hiltjo Posthuma <[email protected]> | |
Date: Tue, 15 Oct 2024 00:29:19 +0200 | |
youtube/cli: use ctype macros, add shortdescription in TSV, fix comment | |
Diffstat: | |
M youtube/cli.c | 30 ++++++++++++++++++++++++------ | |
1 file changed, 24 insertions(+), 6 deletions(-) | |
--- | |
diff --git a/youtube/cli.c b/youtube/cli.c | |
@@ -1,7 +1,6 @@ | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
-#include <ctype.h> | |
#include <errno.h> | |
#include <netdb.h> | |
#include <stdarg.h> | |
@@ -16,16 +15,35 @@ | |
#define OUT(s) fputs((s), stdout) | |
#define OUTESCAPE(s) printescape((s)) | |
+#define OUTENCODED(s) printencoded((s)) | |
/* print: ignore control-characters */ | |
void | |
printescape(const char *s) | |
{ | |
for (; *s; ++s) | |
- if (!iscntrl((unsigned char)*s)) | |
+ if (!ISCNTRL((unsigned char)*s)) | |
fputc(*s, stdout); | |
} | |
+/* print: encode TAB, newline and '\', remove other whitespace. */ | |
+void | |
+printencoded(const char *s) | |
+{ | |
+ for (; *s; ++s) { | |
+ switch (*s) { | |
+ case '\n': putchar('\\'); putchar('n'); break; | |
+ case '\\': putchar('\\'); putchar('\\'); break; | |
+ case '\t': putchar('\\'); putchar('t'); break; | |
+ default: | |
+ /* ignore control chars */ | |
+ if (!ISCNTRL((unsigned char)*s)) | |
+ putchar(*s); | |
+ break; | |
+ } | |
+ } | |
+} | |
+ | |
void | |
printescape_multiline(const char *s, const char *indent) | |
{ | |
@@ -38,7 +56,7 @@ printescape_multiline(const char *s, const char *indent) | |
if (*s == '\n') { | |
i = 0; | |
fputc(*s, stdout); | |
- } else if (!iscntrl((unsigned char)*s)) { | |
+ } else if (!ISCNTRL((unsigned char)*s)) { | |
fputc(*s, stdout); | |
i = 1; | |
} | |
@@ -65,7 +83,7 @@ render_search_tsv(struct search_response *r) | |
OUT("\t"); | |
OUTESCAPE(v->publishedat); | |
OUT("\t"); | |
- OUTESCAPE(v->viewcount); /* from Youtube can be text: "No view… | |
+ OUTESCAPE(v->viewcount); /* from Youtube, the text can be: "No… | |
OUT("\t"); | |
OUTESCAPE(v->duration); | |
OUT("\t"); | |
@@ -81,8 +99,8 @@ render_search_tsv(struct search_response *r) | |
OUTESCAPE(v->channeltitle); | |
OUT("\t"); | |
OUTESCAPE(v->userid); | |
-/* OUT("\t"); | |
- OUTESCAPE(v->shortdescription); */ /* TODO: escape newlines et… | |
+ OUT("\t"); | |
+ OUTENCODED(v->shortdescription); | |
OUT("\n"); | |
} | |