comment out user_agent, bad elias for adding them enabled\!
[xombrero.git] / xxxterm.h
blobae58a7928ae835ffc233ae88dd86f0b6b6b57caf
1 /*
2 * Copyright (c) 2011 Conformal Systems LLC <info@conformal.com>
3 * Copyright (c) 2011 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2010, 2011, 2012 Edd Barrett <vext01@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <ctype.h>
20 #include <dlfcn.h>
21 #include <err.h>
22 #include <errno.h>
23 #include <libgen.h>
24 #include <pwd.h>
25 #include <regex.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <dirent.h>
32 #include <fcntl.h>
33 #include <inttypes.h>
35 #include <sys/types.h>
36 #if defined(__linux__)
37 #include "linux/util.h"
38 #include "linux/tree.h"
39 #include <bsd/stdlib.h>
40 # if !defined(sane_libbsd_headers)
41 void arc4random_buf(void *, size_t);
42 u_int32_t arc4random_uniform(u_int32_t);
43 # endif
44 #elif defined(__FreeBSD__)
45 #include <libutil.h>
46 #include "freebsd/util.h"
47 #include <sys/tree.h>
48 #elif defined(__OpenBSD__)
49 #include <util.h>
50 #include <sys/tree.h>
51 #else
52 #include <sys/tree.h>
53 #endif
54 #include <sys/queue.h>
55 #include <sys/socket.h>
56 #include <sys/stat.h>
57 #include <sys/time.h>
58 #include <sys/un.h>
60 #include <gtk/gtk.h>
61 #include <gdk/gdkkeysyms.h>
63 #if GTK_CHECK_VERSION(3,0,0)
64 /* we still use GDK_* instead of GDK_KEY_* */
65 #include <gdk/gdkkeysyms-compat.h>
66 #endif
68 #include <webkit/webkit.h>
69 #include <libsoup/soup.h>
70 #include <JavaScriptCore/JavaScript.h>
71 #include <gnutls/gnutls.h>
72 #include <gnutls/x509.h>
74 /* comment if you don't want to use threads */
75 #define USE_THREADS
77 #ifdef USE_THREADS
78 #include <gcrypt.h>
79 #include <pthread.h>
80 #endif
82 /* set if you don't want to use signals */
83 #ifndef XT_SIGNALS_DISABLE
84 #include <sys/wait.h>
85 #endif
87 /* set if you don't want to use resource limits */
88 #ifndef XT_RESOURCE_LIMITS_DISABLE
89 #include <sys/resource.h>
90 #endif
92 #include "javascript.h"
94 javascript.h borrowed from vimprobable2 under the following license:
96 Copyright (c) 2009 Leon Winter
97 Copyright (c) 2009-2011 Hannes Schueller
98 Copyright (c) 2009-2010 Matto Fransen
99 Copyright (c) 2010-2011 Hans-Peter Deifel
100 Copyright (c) 2010-2011 Thomas Adam
101 Copyright (c) 2011 Albert Kim
102 Copyright (c) 2011 Daniel Carl
104 Permission is hereby granted, free of charge, to any person obtaining a copy
105 of this software and associated documentation files (the "Software"), to deal
106 in the Software without restriction, including without limitation the rights
107 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
108 copies of the Software, and to permit persons to whom the Software is
109 furnished to do so, subject to the following conditions:
111 The above copyright notice and this permission notice shall be included in
112 all copies or substantial portions of the Software.
114 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
115 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
116 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
117 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
118 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
119 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
120 THE SOFTWARE.
123 /*#define XT_DEBUG*/
124 #ifdef XT_DEBUG
125 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
126 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
127 #define XT_D_MOVE 0x0001
128 #define XT_D_KEY 0x0002
129 #define XT_D_TAB 0x0004
130 #define XT_D_URL 0x0008
131 #define XT_D_CMD 0x0010
132 #define XT_D_NAV 0x0020
133 #define XT_D_DOWNLOAD 0x0040
134 #define XT_D_CONFIG 0x0080
135 #define XT_D_JS 0x0100
136 #define XT_D_FAVORITE 0x0200
137 #define XT_D_PRINTING 0x0400
138 #define XT_D_COOKIE 0x0800
139 #define XT_D_KEYBINDING 0x1000
140 #define XT_D_CLIP 0x2000
141 #define XT_D_BUFFERCMD 0x4000
142 #define XT_D_INSPECTOR 0x8000
143 #define XT_D_VISITED 0x10000
144 #define XT_D_HISTORY 0x20000
145 extern uint32_t swm_debug;
146 #else
147 #define DPRINTF(x...)
148 #define DNPRINTF(n,x...)
149 #endif
151 #define LENGTH(x) (sizeof x / sizeof x[0])
152 #define CLEAN(mask) (mask & ~(GDK_MOD2_MASK) & \
153 ~(GDK_BUTTON1_MASK) & \
154 ~(GDK_BUTTON2_MASK) & \
155 ~(GDK_BUTTON3_MASK) & \
156 ~(GDK_BUTTON4_MASK) & \
157 ~(GDK_BUTTON5_MASK))
159 #define XT_NOMARKS (('z' - 'a' + 1) * 2 + 10)
161 /* Path Separator */
162 #ifndef PS
163 #define PS "/"
164 #endif
166 struct tab {
167 TAILQ_ENTRY(tab) entry;
168 GtkWidget *vbox;
169 GtkWidget *tab_content;
170 struct {
171 GtkWidget *label;
172 GtkWidget *favicon;
173 GtkWidget *eventbox;
174 GtkWidget *box;
175 GtkWidget *sep;
176 } tab_elems;
177 GtkWidget *label;
178 GtkWidget *spinner;
179 GtkWidget *uri_entry;
180 GtkWidget *search_entry;
181 GtkWidget *toolbar;
182 GtkWidget *browser_win;
183 GtkWidget *statusbar_box;
184 struct {
185 GtkWidget *statusbar;
186 GtkWidget *buffercmd;
187 GtkWidget *zoom;
188 GtkWidget *position;
189 } sbe;
190 GtkWidget *cmd;
191 GtkWidget *buffers;
192 GtkWidget *oops;
193 GtkWidget *backward;
194 GtkWidget *forward;
195 GtkWidget *stop;
196 GtkWidget *gohome;
197 GtkWidget *js_toggle;
198 GtkEntryCompletion *completion;
199 guint tab_id;
200 WebKitWebView *wv;
202 WebKitWebHistoryItem *item;
203 WebKitWebBackForwardList *bfl;
205 /* favicon */
206 WebKitNetworkRequest *icon_request;
207 WebKitDownload *icon_download;
208 gchar *icon_dest_uri;
210 /* adjustments for browser */
211 GtkScrollbar *sb_h;
212 GtkScrollbar *sb_v;
213 GtkAdjustment *adjust_h;
214 GtkAdjustment *adjust_v;
216 /* flags */
217 int mode;
218 #define XT_MODE_COMMAND (0)
219 #define XT_MODE_INSERT (1)
220 #define XT_MODE_HINT (2)
221 #define XT_MODE_PASSTHROUGH (3)
222 int (*mode_cb)(struct tab *, GdkEventKey *, gpointer);
223 gpointer mode_cb_data;
224 int focus_wv;
225 int ctrl_click;
226 gchar *status;
227 int xtp_meaning; /* identifies dls/favorites */
228 gchar *tmp_uri;
229 int popup; /* 1 if cmd_entry has popup visible */
230 #ifdef USE_THREADS
231 /* https thread stuff */
232 GThread *thread;
233 #endif
234 /* hints */
235 int script_init;
236 int new_tab;
238 /* custom stylesheet */
239 int styled;
240 char *stylesheet;
242 /* search */
243 char *search_text;
244 int search_forward;
245 guint search_id;
247 /* settings */
248 WebKitWebSettings *settings;
249 gchar *user_agent;
250 gboolean load_images;
252 /* marks */
253 double mark[XT_NOMARKS];
255 /* inspector */
256 WebKitWebInspector *inspector;
257 GtkWidget *inspector_window;
258 GtkWidget *inspector_view;
260 TAILQ_HEAD(tab_list, tab);
262 struct karg {
263 int i;
264 char *s;
265 int precount;
268 struct download {
269 RB_ENTRY(download) entry;
270 int id;
271 WebKitDownload *download;
272 struct tab *tab;
274 RB_HEAD(download_list, download);
275 RB_PROTOTYPE(download_list, download, entry, download_rb_cmp);
277 struct history {
278 RB_ENTRY(history) entry;
279 gchar *uri;
280 gchar *title;
281 time_t time; /* When the item was added. */
283 RB_HEAD(history_list, history);
284 RB_PROTOTYPE(history_list, history, entry, history_rb_cmp);
286 /* utility */
287 #define XT_NAME ("XXXTerm")
288 #define XT_CB_HANDLED (TRUE)
289 #define XT_CB_PASSTHROUGH (FALSE)
290 #define XT_FAVS_FILE ("favorites")
292 int run_script(struct tab *, char *);
293 void xt_icon_from_file(struct tab *, char *);
294 GtkWidget *create_window(const gchar *);
295 void show_oops(struct tab *, const char *, ...);
296 gchar *get_html_page(gchar *, gchar *, gchar *, bool);
297 const gchar *get_uri(struct tab *);
298 const gchar *get_title(struct tab *, bool);
299 void load_uri(struct tab *t, gchar *uri);
300 gboolean match_uri(const gchar *uri, const gchar *key);
301 int valid_url_type(char *);
303 void load_webkit_string(struct tab *, const char *, gchar *);
304 void button_set_stockid(GtkWidget *, char *);
306 /* cookies */
307 int remove_cookie(int);
308 int remove_cookie_domain(int);
309 int remove_cookie_all(void);
310 void print_cookie(char *msg, SoupCookie *);
311 void setup_cookies(void);
313 /* history */
314 int insert_history_item(const gchar *uri, const gchar *title, time_t time);
315 int save_global_history_to_disk(struct tab *t);
316 int restore_global_history(void);
317 char *color_visited_helper(void);
318 int color_visited(struct tab *t, char *visited);
320 /* completion */
321 void completion_add(struct tab *);
322 void completion_add_uri(const gchar *uri);
324 /* external editor */
325 #define XT_EE_BUFSZ (64 * 1024)
326 int edit_src(struct tab *t, struct karg *args);
327 int edit_element(struct tab *t, struct karg *a);
329 /* proxy */
330 #define XT_PRXY_SHOW (1<<0)
331 #define XT_PRXY_TOGGLE (1<<1)
333 /* inspector */
334 #define XT_INS_SHOW (1<<0)
335 #define XT_INS_HIDE (1<<1)
336 #define XT_INS_CLOSE (1<<2)
338 WebKitWebView* inspector_inspect_web_view_cb(WebKitWebInspector *,
339 WebKitWebView*, struct tab *);
340 void setup_inspector(struct tab *);
341 int inspector_cmd(struct tab *, struct karg *);
343 /* tld public suffix */
344 void tld_tree_init();
345 char *tld_get_suffix(const char *);
347 /* about */
348 #define XT_XTP_STR "xxxt://"
349 #define XT_URI_ABOUT ("about:")
350 #define XT_URI_ABOUT_LEN (strlen(XT_URI_ABOUT))
351 #define XT_URI_ABOUT_ABOUT ("about")
352 #define XT_URI_ABOUT_BLANK ("blank")
353 #define XT_URI_ABOUT_CERTS ("certs")
354 #define XT_URI_ABOUT_COOKIEWL ("cookiewl")
355 #define XT_URI_ABOUT_COOKIEJAR ("cookiejar")
356 #define XT_URI_ABOUT_DOWNLOADS ("downloads")
357 #define XT_URI_ABOUT_FAVORITES ("favorites")
358 #define XT_URI_ABOUT_HELP ("help")
359 #define XT_URI_ABOUT_HISTORY ("history")
360 #define XT_URI_ABOUT_JSWL ("jswl")
361 #define XT_URI_ABOUT_PLUGINWL ("plwl")
362 #define XT_URI_ABOUT_SET ("set")
363 #define XT_URI_ABOUT_STATS ("stats")
364 #define XT_URI_ABOUT_MARCO ("marco")
365 #define XT_URI_ABOUT_STARTPAGE ("startpage")
367 struct about_type {
368 char *name;
369 int (*func)(struct tab *, struct karg *);
372 struct sp {
373 char *line;
374 TAILQ_ENTRY(sp) entry;
376 TAILQ_HEAD(sp_list, sp);
378 int blank(struct tab *, struct karg *);
379 int help(struct tab *, struct karg *);
380 int about(struct tab *, struct karg *);
381 int stats(struct tab *, struct karg *);
382 void show_certs(struct tab *, gnutls_x509_crt_t *,
383 size_t, char *);
384 int ca_cmd(struct tab *, struct karg *);
385 int cookie_show_wl(struct tab *, struct karg *);
386 int xtp_page_cl(struct tab *, struct karg *);
387 int xtp_page_dl(struct tab *, struct karg *);
388 int xtp_page_fl(struct tab *, struct karg *);
389 int xtp_page_hl(struct tab *, struct karg *);
390 int parse_xtp_url(struct tab *, const char *);
391 int add_favorite(struct tab *, struct karg *);
392 void update_favorite_tabs(struct tab *);
393 void update_history_tabs(struct tab *);
394 void update_download_tabs(struct tab *);
395 void xtp_generate_keys(void);
396 size_t about_list_size(void);
397 int cookie_cmd(struct tab *, struct karg *);
398 int js_cmd(struct tab *, struct karg *);
399 int pl_cmd(struct tab *, struct karg *);
400 void startpage_add(const char *, ...);
403 * xtp tab meanings
404 * identifies which tabs have xtp pages in (corresponding to about_list indices)
406 #define XT_XTP_TAB_MEANING_NORMAL (-1) /* normal url */
407 #define XT_XTP_TAB_MEANING_BL (1) /* about:blank in this tab */
408 #define XT_XTP_TAB_MEANING_CL (4) /* cookie manager in this tab */
409 #define XT_XTP_TAB_MEANING_DL (5) /* download manager in this tab */
410 #define XT_XTP_TAB_MEANING_FL (6) /* favorite manager in this tab */
411 #define XT_XTP_TAB_MEANING_HL (8) /* history manager in this tab */
413 /* whitelists */
414 #define XT_WL_TOGGLE (1<<0)
415 #define XT_WL_ENABLE (1<<1)
416 #define XT_WL_DISABLE (1<<2)
417 #define XT_WL_FQDN (1<<3) /* default */
418 #define XT_WL_TOPLEVEL (1<<4)
419 #define XT_WL_PERSISTENT (1<<5)
420 #define XT_WL_SESSION (1<<6)
421 #define XT_WL_RELOAD (1<<7)
422 #define XT_SHOW (1<<8)
423 #define XT_DELETE (1<<9)
424 #define XT_SAVE (1<<10)
425 #define XT_OPEN (1<<11)
427 #define XT_WL_INVALID (0)
428 #define XT_WL_JAVASCRIPT (1)
429 #define XT_WL_COOKIE (2)
430 #define XT_WL_PLUGIN (3)
432 struct domain {
433 RB_ENTRY(domain) entry;
434 gchar *d;
435 int handy; /* app use */
437 RB_HEAD(domain_list, domain);
438 RB_PROTOTYPE(domain_list, domain, entry, domain_rb_cmp);
440 int wl_show(struct tab *, struct karg *, char *,
441 struct domain_list *);
443 /* uri aliases */
444 struct alias {
445 char *a_name;
446 char *a_uri;
447 TAILQ_ENTRY(alias) entry;
449 TAILQ_HEAD(alias_list, alias);
451 /* mime types */
452 struct mime_type {
453 char *mt_type;
454 char *mt_action;
455 int mt_default;
456 int mt_download;
457 TAILQ_ENTRY(mime_type) entry;
459 TAILQ_HEAD(mime_type_list, mime_type);
461 struct domain * wl_find(const gchar *, struct domain_list *);
462 int wl_save(struct tab *, struct karg *, int);
463 int toggle_cwl(struct tab *, struct karg *);
464 int toggle_js(struct tab *, struct karg *);
465 int toggle_pl(struct tab *, struct karg *);
467 /* input autofocus */
468 void input_autofocus(struct tab *);
469 void input_focus_blur(struct tab *, void *);
470 void *input_check_mode(struct tab *);
471 int command_mode(struct tab *, struct karg *);
473 /* settings */
474 #define XT_BM_NORMAL (0)
475 #define XT_BM_WHITELIST (1)
476 #define XT_BM_KIOSK (2)
478 #define XT_GM_CLASSIC (0)
479 #define XT_GM_MINIMAL (1)
481 #define XT_TABS_NORMAL (0)
482 #define XT_TABS_COMPACT (1)
484 #define XT_EM_HYBRID (0)
485 #define XT_EM_VI (1)
487 #define XT_DM_START (0)
488 #define XT_DM_ASK (1)
489 #define XT_DM_ADD (2)
491 #define XT_REFERER_ALWAYS (0)
492 #define XT_REFERER_NEVER (1)
493 #define XT_REFERER_SAME_DOMAIN (2)
494 #define XT_REFERER_CUSTOM (3)
496 #define CTRL GDK_CONTROL_MASK
497 #define MOD1 GDK_MOD1_MASK
498 #define SHFT GDK_SHIFT_MASK
500 struct key_binding {
501 char *cmd;
502 guint mask;
503 guint use_in_entry;
504 guint key;
505 TAILQ_ENTRY(key_binding) entry; /* in bss so no need to init */
507 TAILQ_HEAD(keybinding_list, key_binding);
509 struct user_agent {
510 char *value;
511 TAILQ_ENTRY(user_agent) entry;
513 TAILQ_HEAD(user_agent_list, user_agent);
515 struct settings {
516 char *name;
517 int type;
518 #define XT_S_INVALID (0)
519 #define XT_S_INT (1)
520 #define XT_S_STR (2)
521 #define XT_S_FLOAT (3)
522 uint32_t flags;
523 #define XT_SF_RESTART (1<<0)
524 #define XT_SF_RUNTIME (1<<1)
525 int *ival;
526 char **sval;
527 struct special *s;
528 gfloat *fval;
529 int (*activate)(char *);
532 int set(struct tab *, struct karg *);
533 size_t get_settings_size(void);
534 int settings_add(char *, char *);
535 void setup_proxy(char *);
536 int set_browser_mode(struct settings *, char *);
537 int set_gui_mode(struct settings *, char *);
538 int set_cookie_policy(struct settings *, char *);
539 char *get_browser_mode(struct settings *);
540 char *get_gui_mode(struct settings *);
541 char *get_cookie_policy(struct settings *);
542 void init_keybindings(void);
543 void config_parse(char *, int);
544 char *get_setting_name(int);
546 #define XT_DL_START (0)
547 #define XT_DL_RESTART (1)
548 int download_start(struct tab *, struct download *, int flag);
550 extern int tabless;
551 extern int enable_socket;
552 extern int single_instance;
553 extern int fancy_bar;
554 extern int browser_mode;
555 extern int enable_localstorage;
556 extern char *statusbar_elems;
558 extern int show_tabs;
559 extern int tab_style;
560 extern int show_url;
561 extern int show_statusbar;
562 extern int ctrl_click_focus;
563 extern int cookies_enabled;
564 extern int read_only_cookies;
565 extern int enable_scripts;
566 extern int enable_plugins;
567 extern gfloat default_zoom_level;
568 extern char default_script[PATH_MAX];
569 extern int window_height;
570 extern int window_width;
571 extern int window_maximize;
572 extern int icon_size;
573 extern int refresh_interval;
574 extern int enable_plugin_whitelist;
575 extern int enable_cookie_whitelist;
576 extern int enable_js_whitelist;
577 extern int session_timeout;
578 extern int cookie_policy;
579 extern char *ssl_ca_file;
580 extern char *resource_dir;
581 extern gboolean ssl_strict_certs;
582 extern int append_next;
583 extern char *home;
584 extern char *search_string;
585 extern char *http_proxy;
586 extern char *external_editor;
587 extern char download_dir[PATH_MAX];
588 extern int download_mode;
589 extern char runtime_settings[PATH_MAX];
590 extern int allow_volatile_cookies;
591 extern int color_visited_uris;
592 extern int save_global_history;
593 extern struct user_agent *user_agent;
594 extern int save_rejected_cookies;
595 extern int session_autosave;
596 extern int guess_search;
597 extern int dns_prefetch;
598 extern gint max_connections;
599 extern gint max_host_connections;
600 extern gint enable_spell_checking;
601 extern char *spell_check_languages;
602 extern int xterm_workaround;
603 extern char *url_regex;
604 extern int history_autosave;
605 extern char search_file[PATH_MAX];
606 extern char command_file[PATH_MAX];
607 extern char *encoding;
608 extern int autofocus_onload;
609 extern int js_autorun_enabled;
610 extern char *cmd_font_name;
611 extern char *oops_font_name;
612 extern char *statusbar_font_name;
613 extern char *tabbar_font_name;
614 extern int edit_mode;
615 extern int userstyle_global;
616 extern int auto_load_images;
617 extern int enable_autoscroll;
618 extern int enable_favicon_entry;
619 extern int enable_favicon_tabs;
620 extern int referer_mode;
621 extern char *referer_custom;
623 /* globals */
624 extern char *version;
625 extern char *icons[];
626 extern char rc_fname[PATH_MAX];
627 extern char work_dir[PATH_MAX];
628 extern char temp_dir[PATH_MAX];
629 extern struct passwd *pwd;
630 long long unsigned int blocked_cookies;
631 extern SoupCookieJar *s_cookiejar;
632 extern SoupCookieJar *p_cookiejar;
633 extern SoupSession *session;
634 extern GtkNotebook *notebook;
635 extern GtkListStore *completion_model;
637 extern void (*_soup_cookie_jar_add_cookie)(SoupCookieJar *, SoupCookie *);
639 extern struct history_list hl;
640 extern int hl_purge_count;
641 extern struct download_list downloads;
642 extern struct tab_list tabs;
643 extern struct about_type about_list[];
644 extern struct domain_list c_wl;
645 extern struct domain_list js_wl;
646 extern struct domain_list pl_wl;
647 extern struct alias_list aliases;
648 extern struct mime_type_list mtl;
649 extern struct keybinding_list kbl;
650 extern struct sp_list spl;
651 extern struct user_agent_list ua_list;
652 extern int user_agent_count;
654 extern PangoFontDescription *cmd_font;
655 extern PangoFontDescription *oops_font;
656 extern PangoFontDescription *statusbar_font;
657 extern PangoFontDescription *tabbar_font;