From: Armaan Bhojwani Date: Tue, 12 Oct 2021 21:02:43 +0000 (-0400) Subject: draft 2 X-Git-Url: https://git.armaanb.net/?p=chorizo.git;a=commitdiff_plain;h=d216586560dfb856066b86de4ffe06ac92ba3f7e draft 2 --- diff --git a/.clang-format b/.clang-format index 29b06b0..0e1993d 100644 --- a/.clang-format +++ b/.clang-format @@ -7,11 +7,11 @@ AlignEscapedNewlines: Left # Unknown to clang-format-4.0 AlignOperands: true AlignTrailingComments: false AllowAllParametersOfDeclarationOnNextLine: false -AllowShortBlocksOnASingleLine: false +AllowShortBlocksOnASingleLine: true AllowShortCaseLabelsOnASingleLine: false AllowShortFunctionsOnASingleLine: None -AllowShortIfStatementsOnASingleLine: false -AllowShortLoopsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: true +AllowShortLoopsOnASingleLine: true AlwaysBreakAfterDefinitionReturnType: true AlwaysBreakAfterReturnType: None AlwaysBreakBeforeMultilineStrings: false diff --git a/Makefile b/Makefile index 40a4089..f123f02 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAGS += -std=c11 -Wall -Wextra -Wno-unused-parameter -D_XOPEN_SOURCE="700" +CFLAGS += -std=c11 -Wno-unused-parameter -D_XOPEN_SOURCE="700" LIBS = `pkg-config --cflags --libs gtk+-3.0 glib-2.0 webkit2gtk-4.0` PREFIX = /usr/local bindir = $(DESTDIR)$(PREFIX)/bin @@ -8,7 +8,7 @@ mandir = $(DESTDIR)/$(PREFIX)/man .PHONY: chorizo clean uninstall install extensions update-darkreader -all: extensions chorizo +all: chorizo chorizo: $(CC) $(CFLAGS) $(LDFLAGS) $(LIBS) -o chorizo *.c diff --git a/browser.c b/browser.c index 020ce9f..b35d383 100644 --- a/browser.c +++ b/browser.c @@ -63,6 +63,14 @@ int cooperative_pipe_fp = 0; gchar *search_text; gchar *fifopath; char **closed_tabs; +size_t num_closed = 0; + +void +allocfail(void) +{ + fprintf(stderr, "chorizo: fatal: alloc failed\n"); + exit(EXIT_FAILURE); +} void togglejs(GtkButton *jsbutton, gpointer data) @@ -84,17 +92,27 @@ client_destroy(GtkWidget *widget, gpointer data) idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox); if (idx == -1) - fprintf(stderr, "chorizo: Tab index was -1, bamboozled\n"); + fprintf(stderr, "chorizo: warning: tab index was -1\n"); else gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx); if (!cfg.private && WEBKIT_IS_WEB_VIEW(c->web_view)) { - int len = sizeof(closed_tabs) / sizeof(char *); const char *uri = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)); - closed_tabs = (char **)realloc(closed_tabs, - len * sizeof(closed_tabs[0])); - closed_tabs[len] = strdup(uri); + + // TODO: Shift everything left if over certain amount + num_closed++; + if (num_closed > cfg_max_tabs_closed) { + memmove(closed_tabs, closed_tabs, + cfg_max_tabs_closed - 1); + num_closed = cfg_max_tabs_closed; + } else { + closed_tabs = + realloc(closed_tabs, + num_closed * sizeof(closed_tabs[0])); + if (!closed_tabs) allocfail(); + } + closed_tabs[num_closed - 1] = strdup(uri); } free(c); @@ -127,10 +145,7 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show, return NULL; } c = calloc(1, sizeof(struct Client)); - if (!c) { - fprintf(stderr, "chorizo: fatal: calloc failed\n"); - exit(EXIT_FAILURE); - } + if (!c) allocfail(); c->focus_new_tab = focus_tab; if (related_wv == NULL) { @@ -160,8 +175,7 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show, webkit_user_script_unref(wkscript); } g_free(path); - if (source) - g_free(source); + if (source) g_free(source); } g_dir_close(dir); } @@ -199,10 +213,9 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show, c->settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)); webkit_settings_set_enable_javascript(c->settings, cfg_js_default); - if (cfg.verbose) { + if (cfg.verbose) webkit_settings_set_enable_write_console_messages_to_stdout( c->settings, true); - } webkit_settings_set_enable_developer_extras(c->settings, TRUE); g_signal_connect(G_OBJECT(c->web_view), "notify::favicon", @@ -330,10 +343,11 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show, clients++; client_arr = realloc(client_arr, (clients + 1) * sizeof(client_arr[0])); + if (!client_arr) allocfail(); + client_arr[clients] = c; - if (clients == 1 || uri == NULL) - gtk_widget_grab_focus(c->location); + if (clients == 1 || uri == NULL) gtk_widget_grab_focus(c->location); return WEBKIT_WEB_VIEW(c->web_view); } @@ -353,8 +367,7 @@ mkdirp(const char *dir, mode_t mode) size_t len; snprintf(tmp, sizeof(tmp), "%s", dir); len = strlen(tmp); - if (tmp[len - 1] == '/') - tmp[len - 1] = 0; + if (tmp[len - 1] == '/') tmp[len - 1] = 0; for (p = tmp + 1; *p; p++) if (*p == '/') { *p = 0; @@ -380,12 +393,11 @@ cooperation_setup(void) mkdirp(dirname(fifopath), 0600); g_free(fifofilename); - if (!g_file_test(fifopath, G_FILE_TEST_EXISTS)) - mkfifo(fifopath, 0600); + if (!g_file_test(fifopath, G_FILE_TEST_EXISTS)) mkfifo(fifopath, 0600); cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK); if (!cooperative_pipe_fp) { - fprintf(stderr, "chorizo: Can't open FIFO at all.\n"); + fprintf(stderr, "chorizo: error: can't open FIFO\n"); } else { if (write(cooperative_pipe_fp, "", 0) == -1) { /* @@ -491,6 +503,8 @@ changed_title(GObject *obj, GParamSpec *pspec, gpointer data) t = t[0] == 0 ? u : t; gchar *name = malloc(strlen(t) + 4); + if (!name) allocfail(); + gboolean mute = webkit_web_view_get_is_muted(WEBKIT_WEB_VIEW(c->web_view)); gchar *muted = (mute) ? "[m] " : ""; @@ -537,7 +551,7 @@ changed_uri(GObject *obj, GParamSpec *pspec, gpointer data) fprintf(fp, "%s\n", t); fclose(fp); } else { - perror("chorizo: Error opening history file"); + perror("chorizo: error: could not open history file"); } } g_free(history_file); @@ -619,7 +633,8 @@ grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data) js_result = webkit_web_view_run_javascript_finish( WEBKIT_WEB_VIEW(object), result, &err); if (!js_result) { - fprintf(stderr, "chorizo: Error running javascript: %s\n", + fprintf(stderr, + "chorizo: error: error running javascript: %s\n", err->message); g_error_free(err); return; @@ -631,7 +646,7 @@ grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data) jsc_context_get_exception(jsc_value_get_context(value)); if (exception != NULL) { fprintf(stderr, - "chorizo: Error running javascript: %s\n", + "chorizo: warning: error running javascript: %s\n", jsc_exception_get_message(exception)); } else { c->feed_html = str_value; @@ -666,8 +681,7 @@ hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, c->hover_uri = NULL; } - if (!gtk_widget_is_focus(c->location)) - set_uri(to_show, c); + if (!gtk_widget_is_focus(c->location)) set_uri(to_show, c); } void @@ -768,8 +782,7 @@ search(gpointer data, gint direction) WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view); WebKitFindController *fc = webkit_web_view_get_find_controller(web_view); - if (search_text == NULL) - return; + if (search_text == NULL) return; switch (direction) { case 0: @@ -938,16 +951,14 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data) client_new(cfg_home_uri, NULL, TRUE, TRUE); return TRUE; } else if (GDK_KEY_bracketleft == key) { - if (!closed_tabs) - return TRUE; - int len = sizeof(closed_tabs) / sizeof(char *); - if (len < 1) - return TRUE; - client_new(closed_tabs[len], NULL, TRUE, TRUE); - free(closed_tabs[len]); - closed_tabs = (char **)realloc( + if (num_closed == 0) return TRUE; + client_new(closed_tabs[num_closed - 1], NULL, + TRUE, TRUE); + num_closed--; + closed_tabs = realloc( closed_tabs, - (len - 1) * sizeof(closed_tabs[0])); + num_closed * sizeof(closed_tabs[0])); + if (!closed_tabs) allocfail(); return TRUE; } else if (GDK_KEY_i == key) { gtk_notebook_next_page( @@ -999,8 +1010,7 @@ key_location(GtkWidget *widget, GdkEvent *event, gpointer data) { struct Client *c = (struct Client *)data; const gchar *t; - if (key_common(widget, event, data)) - return TRUE; + if (key_common(widget, event, data)) return TRUE; if (event->type == GDK_KEY_PRESS) { int key = ((GdkEventKey *)event)->keyval; @@ -1008,14 +1018,14 @@ key_location(GtkWidget *widget, GdkEvent *event, gpointer data) gtk_widget_grab_focus(c->web_view); t = gtk_entry_get_text(GTK_ENTRY(c->location)); if (t != NULL && t[0] == 's' && t[1] == '/') { - if (search_text != NULL) - g_free(search_text); + if (search_text != NULL) g_free(search_text); search_text = g_strdup(t + 2); search(c, 0); } else if (t != NULL && t[0] == 'w' && t[1] == '/') { int len = strlen(cfg_search_engine) + strlen(t) - 2; gchar *f = malloc(len + 1); + if (!f) allocfail(); snprintf(f, len + 1, "%s%s", cfg_search_engine, t + 2); webkit_web_view_load_uri( @@ -1069,8 +1079,7 @@ key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data) struct Client *c = (struct Client *)data; gdouble dx, dy; gfloat z; - if (key_common(widget, event, data)) - return TRUE; + if (key_common(widget, event, data)) return TRUE; if (event->type == GDK_KEY_PRESS) { if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape) { @@ -1124,6 +1133,7 @@ mainwindow_setup(void) gchar *priv = (cfg.private) ? "-private" : ""; gchar *title = malloc(strlen(priv) + 7); + if (!title) allocfail(); sprintf(title, "%s%s", "chorizo", priv); gtk_window_set_title(GTK_WINDOW(mw.win), title); g_free(title); @@ -1141,8 +1151,7 @@ mainwindow_title(gint idx) GtkWidget *child, *widg, *tablabel; const gchar *text; child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx); - if (child == NULL) - return; + if (child == NULL) return; widg = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child); tablabel = (GtkWidget *)g_object_get_data(G_OBJECT(widg), @@ -1222,7 +1231,7 @@ trust_user_certs(WebKitWebContext *wc) g_free(absfile); if (cert == NULL) fprintf(stderr, - "chorizo: Could not load trusted cert '%s'\n", + "chorizo: warning: could not load trusted cert: %s\n", file); else webkit_web_context_allow_tls_certificate_for_host( @@ -1233,6 +1242,12 @@ trust_user_certs(WebKitWebContext *wc) } } +void +version(void) +{ + printf("%s %s\n", "chorizo", VERSION); +} + int main(int argc, char **argv) { @@ -1241,6 +1256,8 @@ main(int argc, char **argv) //TODO:pretty this cfg.noncooperative_instances = FALSE; cfg.cooperative_alone = TRUE; + closed_tabs = malloc(0); + if (!closed_tabs) allocfail(); while ((opt = getopt(argc, argv, "cpvV")) != -1) { switch (opt) { @@ -1254,23 +1271,24 @@ main(int argc, char **argv) cfg.verbose = TRUE; break; case 'V': - printf("%s %s\n", "chorizo", VERSION); + version(); exit(0); default: fprintf(stderr, - "Usage: chorizo [OPTION]... [URI]...\n"); + "usage: chorizo [OPTION]... [URI]...\n"); exit(EXIT_FAILURE); } } + if (cfg.verbose) version(); + gtk_init(&argc, &argv); //Keep clipboard contents after program closes gtk_clipboard_store(gtk_clipboard_get_for_display( gdk_display_get_default(), GDK_SELECTION_CLIPBOARD)); - if (!cfg.noncooperative_instances) - cooperation_setup(); + if (!cfg.noncooperative_instances) cooperation_setup(); if (cfg.noncooperative_instances || cfg.cooperative_alone) init_default_web_context(); @@ -1279,6 +1297,8 @@ main(int argc, char **argv) mainwindow_setup(); client_arr = malloc(sizeof(struct Client *)); + if (!client_arr) allocfail(); + if (optind >= argc) { client_new(cfg_home_uri, NULL, TRUE, TRUE); } else { @@ -1290,9 +1310,7 @@ main(int argc, char **argv) gtk_main(); remove(fifopath); } - for (int i = 0; i < clients; i++) { - free(&(client_arr[i])); - } + for (int i = 0; i < clients; i++) { free(&(client_arr[i])); } exit(EXIT_SUCCESS); } diff --git a/config.h b/config.h index af1da40..d782887 100644 --- a/config.h +++ b/config.h @@ -12,5 +12,6 @@ int cfg_scroll_lines = 3; //Number of lines to scroll at a time gchar *cfg_search_engine = "https://searx.be/search?q="; //Search engine +int cfg_max_tabs_closed = 16; #endif diff --git a/downloads.c b/downloads.c index c738863..439f2a0 100644 --- a/downloads.c +++ b/downloads.c @@ -19,6 +19,7 @@ struct DownloadItem { GtkToolButton *tb; WebKitDownload *download; }; + gboolean key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data) { @@ -148,14 +149,29 @@ download_cancel(GtkMenuItem *tb, gpointer data) webkit_download_cancel(payload->download); } +const char * +download_get_path(struct DownloadItem *payload) +{ + const char *path = webkit_download_get_destination(payload->download); + // Offset by 7 to remove "file://" + return path += 7; +} + void -download_remove(GtkMenuItem *tb, gpointer data) +download_hide(GtkMenuItem *tb, gpointer data) { struct DownloadItem *payload = data; g_object_unref(payload->download); gtk_widget_destroy(GTK_WIDGET(payload->tb)); } +void +download_delete(GtkMenuItem *tb, gpointer data) +{ + remove(download_get_path((struct DownloadItem *)data)); + download_hide(tb, data); +} + void download_copy_url(GtkMenuItem *tb, gpointer data) { @@ -169,11 +185,9 @@ download_copy_url(GtkMenuItem *tb, gpointer data) void download_copy_path(GtkMenuItem *tb, gpointer data) { - struct DownloadItem *payload = data; - const gchar *path = webkit_download_get_destination(payload->download); + const char *path = download_get_path((struct DownloadItem *)data); gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), path + 7, strlen(path) - 7); - //Offset by 7 to remove "file://" } void @@ -199,9 +213,16 @@ download_click(GtkToolButton *tb, gpointer data) gtk_widget_show(option); gtk_menu_shell_append(GTK_MENU_SHELL(pmenu), option); } else { - option = gtk_menu_item_new_with_label("Remove download"); + option = + gtk_menu_item_new_with_label("Hide download from list"); + g_signal_connect(G_OBJECT(option), "activate", + G_CALLBACK(download_hide), data); + gtk_widget_show(option); + gtk_menu_shell_append(GTK_MENU_SHELL(pmenu), option); + + option = gtk_menu_item_new_with_label("Delete downloaded file"); g_signal_connect(G_OBJECT(option), "activate", - G_CALLBACK(download_remove), data); + G_CALLBACK(download_delete), data); gtk_widget_show(option); gtk_menu_shell_append(GTK_MENU_SHELL(pmenu), option); @@ -230,8 +251,7 @@ download_click(GtkToolButton *tb, gpointer data) gboolean downloadmanager_delete(GtkWidget *obj, gpointer data) { - if (!quit_if_nothing_active()) - gtk_widget_hide(dm.win); + if (!quit_if_nothing_active()) gtk_widget_hide(dm.win); return TRUE; } diff --git a/extensions/we_adblock.c b/extensions/we_adblock.c index 37fa3b7..f09c119 100644 --- a/extensions/we_adblock.c +++ b/extensions/we_adblock.c @@ -22,7 +22,7 @@ adblock_load(void) { re = g_regex_new(buf, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, G_REGEX_MATCH_PARTIAL, &err); if (err != NULL) { - fprintf(stderr, "chorizo: Could not compile regex: %s\n", + fprintf(stderr, "we_adblock: error: could not compile regex: %s\n", buf); g_error_free(err); err = NULL;