rosenrot-browser

A hackable browser based on Webkitgtk
Log | Files | Refs | README

shortcuts.c (2021B)


      1 #include <stdbool.h>
      2 #include <stdio.h>
      3 #include <string.h>
      4 
      5 #include "../strings/strings.h"
      6 
      7 #define SHORTCUT_N 41
      8 #define DEBUG false
      9 
     10 /* Inspired by https://duckduckgo.com/bangs */
     11 int shortcut_expand(const char* uri, char* output)
     12 {
     13     int len_uri = strlen(uri);
     14     int len_output = strlen(output);
     15 
     16     if ((len_output - len_uri) < SHORTCUT_N) {
     17         fprintf(stderr, "Not enough memory\n");
     18         return 1; // not enough memory.
     19     } else {
     20         char* shortcuts[] = {
     21             "!aa",
     22             "!blog",
     23             "!fnf",
     24             "!fnc",
     25             "!fs",
     26             "!hn",
     27             "!hnb",
     28             "!ww",
     29             "!x"
     30         };
     31 
     32         char* expansions[] = {
     33             "https://annas-archive.org",
     34             "https://nunosempere.com/blog",
     35             "https://forum.nunosempere.com/frontpage",
     36             "https://forum.nunosempere.com/comments",
     37             "https://forecasting.substack.com",
     38             "https://news.ycombinator.com",
     39             "https://news.ycombinator.com/best",
     40             "https://web.whatsapp.com",
     41             "https://twitter.com"
     42         };
     43 
     44         // len = sizeof(shortcuts) / sizeof(shortcuts[0]);
     45         int len = sizeof(shortcuts) / sizeof(char*);
     46 
     47         for (int i = 0; i < len; i++) {
     48             str_init(output, len_output);
     49             int replace_check = str_replace_start(uri, shortcuts[i],
     50                 expansions[i], output);
     51             switch (replace_check) {
     52                 case 0: // no match found
     53                     break;
     54                 case 1: // str_replace_start somehow failed
     55                     fprintf(stderr, "str_replace_start failed\n");
     56                     return 1;
     57                     break;
     58                 case 2: // match succeeded
     59                     return 2;
     60                     break;
     61                 default:
     62                     fprintf(stderr, "Unreachable state\n");
     63             }
     64         }
     65         strcpy(output, uri);
     66     }
     67     if (DEBUG) printf("No match found\n\n");
     68     return 0;
     69 }