rosenrot/plugins/shortcuts/shortcuts.c

66 lines
1.9 KiB
C
Raw Normal View History

2023-05-14 03:24:34 +00:00
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "../strings/strings.h"
2023-05-14 03:24:34 +00:00
2023-05-14 03:50:14 +00:00
#define SHORTCUT_N 41
2024-03-23 20:54:15 +00:00
#define DEBUG false
2023-05-14 03:24:34 +00:00
/* Inspired by https://duckduckgo.com/bangs */
int shortcut_expand(const char* uri, char* output)
{
2024-03-23 20:54:15 +00:00
int len_uri = strlen(uri);
int len_output = strlen(output);
2023-05-14 03:24:34 +00:00
2024-03-23 20:54:15 +00:00
if ((len_output - len_uri) < SHORTCUT_N) {
fprintf(stderr, "Not enough memory\n");
2023-05-14 03:24:34 +00:00
return 1; // not enough memory.
} else {
char* shortcuts[] = {
2024-03-23 14:14:29 +00:00
"!aa",
2024-03-16 10:57:32 +00:00
"!blog",
2023-05-14 03:24:34 +00:00
"!fnf",
"!fnc",
2023-06-17 06:29:27 +00:00
"!hn",
"!hnb"
2024-03-23 14:14:29 +00:00
"!x",
2023-05-14 03:24:34 +00:00
};
char* expansions[] = {
2024-03-23 14:14:29 +00:00
"https://annas-archive.org",
2024-03-16 10:57:32 +00:00
"https://nunosempere.com/blog",
2023-05-14 03:24:34 +00:00
"https://forum.nunosempere.com/frontpage",
"https://forum.nunosempere.com/comments",
"https://news.ycombinator.com",
"https://news.ycombinator.com/best",
2024-03-23 14:14:29 +00:00
"https://twitter.com",
2023-05-14 03:24:34 +00:00
};
// len = sizeof(shortcuts) / sizeof(shortcuts[0]);
2024-03-23 20:54:15 +00:00
int len = sizeof(shortcuts) / sizeof(char*);
2023-05-14 03:24:34 +00:00
for (int i = 0; i < len; i++) {
2024-03-23 20:54:15 +00:00
str_init(output, len_output);
int replace_check = str_replace_start(uri, shortcuts[i],
2023-05-14 03:24:34 +00:00
expansions[i], output);
2024-03-23 20:54:15 +00:00
switch (replace_check) {
case 0: // no match found
break;
case 1: // str_replace_start somehow failed
fprintf(stderr, "str_replace_start failed\n");
return 1;
break;
case 2: // match succeeded
return 2;
break;
default:
fprintf(stderr, "Unreachable state\n");
2023-05-14 03:24:34 +00:00
}
}
2024-03-23 20:54:15 +00:00
strcpy(output, uri);
2023-05-14 03:24:34 +00:00
}
2024-03-23 21:10:59 +00:00
if (DEBUG) printf("No match found\n\n");
2023-05-14 03:24:34 +00:00
return 0;
}