-
Notifications
You must be signed in to change notification settings - Fork 37
/
xombrero.c
8925 lines (7544 loc) · 205 KB
/
xombrero.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2010, 2011 Marco Peereboom <[email protected]>
* Copyright (c) 2011 Stevan Andjelkovic <[email protected]>
* Copyright (c) 2010, 2011, 2012 Edd Barrett <[email protected]>
* Copyright (c) 2011 Todd T. Fries <[email protected]>
* Copyright (c) 2011 Raphael Graf <[email protected]>
* Copyright (c) 2011 Michal Mazurek <[email protected]>
* Copyright (c) 2012, 2013 Josh Rickmar <[email protected]>
* Copyright (c) 2013 David Hill <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <xombrero.h>
#include "version.h"
char *version = XOMBRERO_VERSION;
#ifdef XT_DEBUG
uint32_t swm_debug = 0
| XT_D_MOVE
| XT_D_KEY
| XT_D_TAB
| XT_D_URL
| XT_D_CMD
| XT_D_NAV
| XT_D_DOWNLOAD
| XT_D_CONFIG
| XT_D_JS
| XT_D_FAVORITE
| XT_D_PRINTING
| XT_D_COOKIE
| XT_D_KEYBINDING
| XT_D_CLIP
| XT_D_BUFFERCMD
| XT_D_INSPECTOR
| XT_D_VISITED
| XT_D_HISTORY
;
#endif
char *icons[] = {
"xombreroicon16.png",
"xombreroicon32.png",
"xombreroicon48.png",
"xombreroicon64.png",
"xombreroicon128.png"
};
struct session {
TAILQ_ENTRY(session) entry;
const gchar *name;
};
TAILQ_HEAD(session_list, session);
struct undo {
TAILQ_ENTRY(undo) entry;
gchar *uri;
GList *history;
int back; /* Keeps track of how many back
* history items there are. */
};
TAILQ_HEAD(undo_tailq, undo);
struct command_entry {
char *line;
TAILQ_ENTRY(command_entry) entry;
};
TAILQ_HEAD(command_list, command_entry);
/* defines */
#define XT_CACHE_DIR ("cache")
#define XT_CERT_DIR ("certs")
#define XT_CERT_CACHE_DIR ("certs_cache")
#define XT_JS_DIR ("js")
#define XT_SESSIONS_DIR ("sessions")
#define XT_TEMP_DIR ("tmp")
#define XT_QMARKS_FILE ("quickmarks")
#define XT_SAVED_TABS_FILE ("main_session")
#define XT_RESTART_TABS_FILE ("restart_tabs")
#define XT_SOCKET_FILE ("socket")
#define XT_SAVE_SESSION_ID ("SESSION_NAME=")
#define XT_SEARCH_FILE ("search_history")
#define XT_COMMAND_FILE ("command_history")
#define XT_DLMAN_REFRESH "10"
#define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
#define XT_MAX_UNDO_CLOSE_TAB (32)
#define XT_PRINT_EXTRA_MARGIN 10
#define XT_URL_REGEX ("^[[:blank:]]*[^[:blank:]]*([[:alnum:]-]+\\.)+[[:alnum:]-][^[:blank:]]*[[:blank:]]*$")
#define XT_INVALID_MARK (-1) /* XXX this is a double, maybe use something else, like a nan */
#define XT_MAX_CERTS (32)
/* colors */
#define XT_COLOR_RED "#cc0000"
#define XT_COLOR_YELLOW "#ffff66"
#define XT_COLOR_BLUE "lightblue"
#define XT_COLOR_GREEN "#99ff66"
#define XT_COLOR_WHITE "white"
#define XT_COLOR_BLACK "black"
#define XT_COLOR_CT_BACKGROUND "#000000"
#define XT_COLOR_CT_INACTIVE "#dddddd"
#define XT_COLOR_CT_ACTIVE "#bbbb00"
#define XT_COLOR_CT_SEPARATOR "#555555"
#define XT_COLOR_SB_SEPARATOR "#555555"
/* CSS element names */
#define XT_CSS_NORMAL ""
#define XT_CSS_RED "red"
#define XT_CSS_YELLOW "yellow"
#define XT_CSS_GREEN "green"
#define XT_CSS_BLUE "blue"
#define XT_CSS_HIDDEN "hidden"
#define XT_CSS_ACTIVE "active"
#define XT_PROTO_DELIM "://"
/* actions */
#define XT_MOVE_INVALID (0)
#define XT_MOVE_DOWN (1)
#define XT_MOVE_UP (2)
#define XT_MOVE_BOTTOM (3)
#define XT_MOVE_TOP (4)
#define XT_MOVE_PAGEDOWN (5)
#define XT_MOVE_PAGEUP (6)
#define XT_MOVE_HALFDOWN (7)
#define XT_MOVE_HALFUP (8)
#define XT_MOVE_LEFT (9)
#define XT_MOVE_FARLEFT (10)
#define XT_MOVE_RIGHT (11)
#define XT_MOVE_FARRIGHT (12)
#define XT_MOVE_PERCENT (13)
#define XT_MOVE_CENTER (14)
#define XT_QMARK_SET (0)
#define XT_QMARK_OPEN (1)
#define XT_QMARK_TAB (2)
#define XT_MARK_SET (0)
#define XT_MARK_GOTO (1)
#define XT_GO_UP_ROOT (999)
#define XT_NAV_INVALID (0)
#define XT_NAV_BACK (1)
#define XT_NAV_FORWARD (2)
#define XT_NAV_RELOAD (3)
#define XT_NAV_STOP (4)
#define XT_FOCUS_INVALID (0)
#define XT_FOCUS_URI (1)
#define XT_FOCUS_SEARCH (2)
#define XT_SEARCH_INVALID (0)
#define XT_SEARCH_NEXT (1)
#define XT_SEARCH_PREV (2)
#define XT_PASTE_CURRENT_TAB (0)
#define XT_PASTE_NEW_TAB (1)
#define XT_ZOOM_IN (-1)
#define XT_ZOOM_OUT (-2)
#define XT_ZOOM_NORMAL (100)
#define XT_SES_DONOTHING (0)
#define XT_SES_CLOSETABS (1)
#define XT_PREFIX (1<<0)
#define XT_USERARG (1<<1)
#define XT_URLARG (1<<2)
#define XT_INTARG (1<<3)
#define XT_SESSARG (1<<4)
#define XT_SETARG (1<<5)
#define XT_HINT_NEWTAB (1<<0)
#define XT_BUFCMD_SZ (8)
#define XT_EJS_SHOW (1<<0)
GtkWidget * create_button(const char *, const char *, int);
void recalc_tabs(void);
void recolor_compact_tabs(void);
void set_current_tab(int page_num);
gboolean update_statusbar_position(GtkAdjustment*, gpointer);
void marks_clear(struct tab *t);
/* globals */
extern char *__progname;
char * const *start_argv;
struct passwd *pwd;
GtkWidget *main_window;
GtkNotebook *notebook;
GtkWidget *tab_bar;
GtkWidget *tab_bar_box;
GtkWidget *arrow, *abtn;
GdkEvent *fevent = NULL;
struct tab_list tabs;
struct history_list hl;
int hl_purge_count = 0;
struct session_list sessions;
struct wl_list c_wl;
struct wl_list js_wl;
struct wl_list pl_wl;
struct wl_list force_https;
struct wl_list svil;
struct strict_transport_tree st_tree;
struct undo_tailq undos;
struct keybinding_list kbl;
struct sp_list spl;
struct user_agent_list ua_list;
struct http_accept_list ha_list;
struct domain_id_list di_list;
struct cmd_alias_list cal;
struct custom_uri_list cul;
struct command_list chl;
struct command_list shl;
struct command_entry *history_at;
struct command_entry *search_at;
struct secviolation_list svl;
struct set_reject_list srl;
int undo_count;
int cmd_history_count = 0;
int search_history_count = 0;
char *global_search;
uint64_t blocked_cookies = 0;
char named_session[PATH_MAX];
GtkListStore *completion_model;
GtkListStore *buffers_store;
char *stylesheet;
char *qmarks[XT_NOQMARKS];
int btn_down; /* M1 down in any wv */
regex_t url_re; /* guess_search regex */
/* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
int next_download_id = 1;
void xxx_dir(char *);
int icon_size_map(int);
void activate_uri_entry_cb(GtkWidget*, struct tab *);
void activate_search_entry_cb(GtkWidget*, struct tab *);
void
history_delete(struct command_list *l, int *counter)
{
struct command_entry *c;
if (l == NULL || counter == NULL)
return;
c = TAILQ_LAST(l, command_list);
if (c == NULL)
return;
TAILQ_REMOVE(l, c, entry);
*counter -= 1;
g_free(c->line);
g_free(c);
}
void
history_add(struct command_list *list, char *file, char *l, int *counter)
{
struct command_entry *c;
FILE *f;
if (list == NULL || l == NULL || counter == NULL)
return;
/* don't add the same line */
c = TAILQ_FIRST(list);
if (c)
if (!strcmp(c->line + 1 /* skip space */, l))
return;
c = g_malloc0(sizeof *c);
c->line = g_strdup_printf(" %s", l);
*counter += 1;
TAILQ_INSERT_HEAD(list, c, entry);
if (*counter > 1000)
history_delete(list, counter);
if (history_autosave && file) {
f = fopen(file, "w");
if (f == NULL) {
show_oops(NULL, "couldn't write history %s", file);
return;
}
TAILQ_FOREACH_REVERSE(c, list, command_list, entry) {
c->line[0] = ' ';
fprintf(f, "%s\n", c->line);
}
fclose(f);
}
}
int
history_read(struct command_list *list, char *file, int *counter)
{
FILE *f;
char *s, line[65536];
if (list == NULL || file == NULL)
return (1);
f = fopen(file, "r");
if (f == NULL) {
startpage_add("couldn't open history file %s", file);
return (1);
}
for (;;) {
s = fgets(line, sizeof line, f);
if (s == NULL || feof(f) || ferror(f))
break;
if ((s = strchr(line, '\n')) == NULL) {
startpage_add("invalid history file %s", file);
fclose(f);
return (1);
}
*s = '\0';
history_add(list, NULL, line + 1, counter);
}
fclose(f);
return (0);
}
/* marks array storage. */
char
indextomark(int i)
{
if (i < 0 || i >= XT_NOMARKS)
return (0);
return XT_MARKS[i];
}
int
marktoindex(char m)
{
char *ret;
if ((ret = strchr(XT_MARKS, m)) != NULL)
return ret - XT_MARKS;
return (-1);
}
/* quickmarks array storage. */
char
indextoqmark(int i)
{
if (i < 0 || i >= XT_NOQMARKS)
return (0);
return XT_QMARKS[i];
}
int
qmarktoindex(char m)
{
char *ret;
if ((ret = strchr(XT_QMARKS, m)) != NULL)
return ret - XT_QMARKS;
return (-1);
}
int
is_g_object_setting(GObject *o, char *str)
{
guint n_props = 0, i;
GParamSpec **proplist;
int rv = 0;
if (!G_IS_OBJECT(o))
return (0);
proplist = g_object_class_list_properties(G_OBJECT_GET_CLASS(o),
&n_props);
for (i = 0; i < n_props; i++) {
if (! strcmp(proplist[i]->name, str)) {
rv = 1;
break;
}
}
g_free(proplist);
return (rv);
}
struct tab *
get_current_tab(void)
{
struct tab *t;
TAILQ_FOREACH(t, &tabs, entry) {
if (t->tab_id == gtk_notebook_get_current_page(notebook))
return (t);
}
warnx("%s: no current tab", __func__);
return (NULL);
}
int
set_ssl_ca_file(struct settings *s, char *file)
{
struct stat sb;
if (file == NULL || strlen(file) == 0)
return (-1);
if (stat(file, &sb)) {
warnx("no CA file: %s", file);
return (-1);
}
expand_tilde(ssl_ca_file, sizeof ssl_ca_file, file);
g_object_set(session,
SOUP_SESSION_SSL_CA_FILE, ssl_ca_file,
SOUP_SESSION_SSL_STRICT, ssl_strict_certs,
(void *)NULL);
return (0);
}
void
set_status(struct tab *t, gchar *fmt, ...)
{
va_list ap;
gchar *status;
va_start(ap, fmt);
status = g_strdup_vprintf(fmt, ap);
gtk_entry_set_text(GTK_ENTRY(t->sbe.uri), status);
if (!t->status)
t->status = status;
else if (strcmp(t->status, status)) {
/* set new status */
g_free(t->status);
t->status = status;
} else
g_free(status);
va_end(ap);
}
void
hide_cmd(struct tab *t)
{
DNPRINTF(XT_D_CMD, "%s: tab %d\n", __func__, t->tab_id);
history_at = NULL; /* just in case */
search_at = NULL; /* just in case */
gtk_widget_set_can_focus(t->cmd, FALSE);
gtk_widget_hide(t->cmd);
}
void
show_cmd(struct tab *t, const char *s)
{
DNPRINTF(XT_D_CMD, "%s: tab %d\n", __func__, t->tab_id);
/* without this you can't middle click in t->cmd to paste */
gtk_entry_set_text(GTK_ENTRY(t->cmd), "");
history_at = NULL;
search_at = NULL;
gtk_widget_hide(t->oops);
gtk_widget_set_can_focus(t->cmd, TRUE);
gtk_widget_show(t->cmd);
gtk_widget_grab_focus(GTK_WIDGET(t->cmd));
#if GTK_CHECK_VERSION(3, 0, 0)
gtk_widget_set_name(t->cmd, XT_CSS_NORMAL);
#else
gtk_widget_modify_base(t->cmd, GTK_STATE_NORMAL,
&t->default_style->base[GTK_STATE_NORMAL]);
#endif
gtk_entry_set_text(GTK_ENTRY(t->cmd), s);
gtk_editable_set_position(GTK_EDITABLE(t->cmd), -1);
}
void
hide_buffers(struct tab *t)
{
gtk_widget_hide(t->buffers);
gtk_widget_set_can_focus(t->buffers, FALSE);
gtk_list_store_clear(buffers_store);
}
enum {
COL_ID = 0,
COL_FAVICON,
COL_TITLE,
NUM_COLS
};
int
sort_tabs_by_page_num(struct tab ***stabs)
{
int num_tabs = 0;
struct tab *t;
num_tabs = gtk_notebook_get_n_pages(notebook);
*stabs = g_malloc0(num_tabs * sizeof(struct tab *));
TAILQ_FOREACH(t, &tabs, entry)
(*stabs)[gtk_notebook_page_num(notebook, t->vbox)] = t;
return (num_tabs);
}
void
buffers_make_list(void)
{
GtkTreeIter iter;
const gchar *title = NULL;
struct tab **stabs = NULL;
int i, num_tabs;
num_tabs = sort_tabs_by_page_num(&stabs);
for (i = 0; i < num_tabs; i++)
if (stabs[i]) {
gtk_list_store_append(buffers_store, &iter);
title = get_title(stabs[i], FALSE);
gtk_list_store_set(buffers_store, &iter,
COL_ID, i + 1, /* Enumerate the tabs starting from 1
* rather than 0. */
COL_FAVICON, gtk_image_get_pixbuf
(GTK_IMAGE(stabs[i]->tab_elems.favicon)),
COL_TITLE, title,
-1);
}
g_free(stabs);
}
void
show_buffers(struct tab *t)
{
GtkTreeIter iter;
GtkTreeSelection *sel;
GtkTreePath *path;
int index;
if (gtk_widget_get_visible(GTK_WIDGET(t->buffers)))
return;
buffers_make_list();
sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(t->buffers));
index = gtk_notebook_get_current_page(notebook);
path = gtk_tree_path_new_from_indices(index, -1);
if (gtk_tree_model_get_iter(GTK_TREE_MODEL(buffers_store), &iter, path))
gtk_tree_selection_select_iter(sel, &iter);
gtk_tree_path_free(path);
gtk_widget_show(t->buffers);
gtk_widget_set_can_focus(t->buffers, TRUE);
gtk_widget_grab_focus(GTK_WIDGET(t->buffers));
}
void
toggle_buffers(struct tab *t)
{
if (gtk_widget_get_visible(t->buffers))
hide_buffers(t);
else
show_buffers(t);
}
int
buffers(struct tab *t, struct karg *args)
{
show_buffers(t);
return (0);
}
int
set_scrollbar_visibility(struct tab *t, int visible)
{
#if GTK_CHECK_VERSION(3, 0, 0)
GtkWidget *h_scrollbar, *v_scrollbar;
h_scrollbar = gtk_scrolled_window_get_hscrollbar(
GTK_SCROLLED_WINDOW(t->browser_win));
v_scrollbar = gtk_scrolled_window_get_vscrollbar(
GTK_SCROLLED_WINDOW(t->browser_win));
if (visible == 0) {
gtk_widget_set_name(h_scrollbar, XT_CSS_HIDDEN);
gtk_widget_set_name(v_scrollbar, XT_CSS_HIDDEN);
} else {
gtk_widget_set_name(h_scrollbar, "");
gtk_widget_set_name(v_scrollbar, "");
}
return (0);
#else
return (visible == 0);
#endif
}
void
hide_oops(struct tab *t)
{
gtk_widget_hide(t->oops);
}
void
show_oops(struct tab *at, const char *fmt, ...)
{
va_list ap;
char *msg = NULL;
struct tab *t = NULL;
if (fmt == NULL)
return;
if (at == NULL) {
if ((t = get_current_tab()) == NULL)
return;
} else
t = at;
va_start(ap, fmt);
if ((msg = g_strdup_vprintf(fmt, ap)) == NULL)
errx(1, "show_oops failed");
va_end(ap);
gtk_entry_set_text(GTK_ENTRY(t->oops), msg);
gtk_widget_hide(t->cmd);
gtk_widget_show(t->oops);
if (msg)
g_free(msg);
}
char work_dir[PATH_MAX];
char certs_dir[PATH_MAX];
char certs_cache_dir[PATH_MAX];
char js_dir[PATH_MAX];
char cache_dir[PATH_MAX];
char sessions_dir[PATH_MAX];
char temp_dir[PATH_MAX];
char cookie_file[PATH_MAX];
char *strict_transport_file = NULL;
SoupSession *session;
SoupCookieJar *s_cookiejar;
SoupCookieJar *p_cookiejar;
char rc_fname[PATH_MAX];
struct mime_type_list mtl;
struct alias_list aliases;
/* protos */
struct tab *create_new_tab(const char *, struct undo *, int, int);
void delete_tab(struct tab *);
void setzoom_webkit(struct tab *, int);
int download_rb_cmp(struct download *, struct download *);
gboolean cmd_execute(struct tab *t, char *str);
int
history_rb_cmp(struct history *h1, struct history *h2)
{
return (strcmp(h1->uri, h2->uri));
}
RB_GENERATE(history_list, history, entry, history_rb_cmp);
int
download_rb_cmp(struct download *e1, struct download *e2)
{
return (e1->id < e2->id ? -1 : e1->id > e2->id);
}
RB_GENERATE(download_list, download, entry, download_rb_cmp);
int
secviolation_rb_cmp(struct secviolation *s1, struct secviolation *s2)
{
return (s1->xtp_arg < s2->xtp_arg ? -1 : s1->xtp_arg > s2->xtp_arg);
}
RB_GENERATE(secviolation_list, secviolation, entry, secviolation_rb_cmp);
int
user_agent_rb_cmp(struct user_agent *ua1, struct user_agent *ua2)
{
return (ua1->id < ua2->id ? -1 : ua1->id > ua2->id);
}
RB_GENERATE(user_agent_list, user_agent, entry, user_agent_rb_cmp);
int
http_accept_rb_cmp(struct http_accept *ha1, struct http_accept *ha2)
{
return (ha1->id < ha2->id ? -1 : ha1->id > ha2->id);
}
RB_GENERATE(http_accept_list, http_accept, entry, http_accept_rb_cmp);
int
domain_id_rb_cmp(struct domain_id *d1, struct domain_id *d2)
{
return (strcmp(d1->domain, d2->domain));
}
RB_GENERATE(domain_id_list, domain_id, entry, domain_id_rb_cmp);
struct valid_url_types {
char *type;
} vut[] = {
{ "http://" },
{ "https://" },
{ "ftp://" },
{ "file://" },
{ XT_URI_ABOUT },
{ XT_XTP_STR },
};
int
valid_url_type(const char *url)
{
int i;
for (i = 0; i < LENGTH(vut); i++)
if (!strncasecmp(vut[i].type, url, strlen(vut[i].type)))
return (0);
return (1);
}
char *
match_alias(const char *url_in)
{
struct alias *a;
char *arg;
char *url_out = NULL, *search, *enc_arg;
char **sv;
search = g_strdup(url_in);
arg = search;
if (strsep(&arg, " \t") == NULL) {
show_oops(NULL, "match_alias: NULL URL");
goto done;
}
TAILQ_FOREACH(a, &aliases, entry) {
if (!strcmp(search, a->a_name))
break;
}
if (a != NULL) {
DNPRINTF(XT_D_URL, "match_alias: matched alias %s\n",
a->a_name);
if (arg == NULL)
arg = "";
enc_arg = soup_uri_encode(arg, XT_RESERVED_CHARS);
sv = g_strsplit(a->a_uri, "%s", 2);
if (arg != NULL)
url_out = g_strjoinv(enc_arg, sv);
else
url_out = g_strjoinv("", sv);
g_free(enc_arg);
g_strfreev(sv);
}
done:
g_free(search);
return (url_out);
}
char *
guess_url_type(const char *url_in)
{
struct stat sb;
char cwd[PATH_MAX] = {0};
char *url_out = NULL, *enc_search = NULL;
char *path = NULL;
char **sv = NULL;
int i;
/* substitute aliases */
url_out = match_alias(url_in);
if (url_out != NULL)
return (url_out);
/* see if we are an about page */
if (!strncmp(url_in, XT_URI_ABOUT, XT_URI_ABOUT_LEN))
for (i = 0; i < about_list_size(); i++)
if (!strcmp(&url_in[XT_URI_ABOUT_LEN],
about_list[i].name)) {
url_out = g_strdup(url_in);
goto done;
}
if (guess_search && url_regex &&
!(g_str_has_prefix(url_in, "http://") ||
g_str_has_prefix(url_in, "https://"))) {
if (regexec(&url_re, url_in, 0, NULL, 0)) {
/* invalid URI so search instead */
enc_search = soup_uri_encode(url_in, XT_RESERVED_CHARS);
sv = g_strsplit(search_string, "%s", 2);
url_out = g_strjoinv(enc_search, sv);
g_free(enc_search);
g_strfreev(sv);
goto done;
}
}
/* XXX not sure about this heuristic */
if (stat(url_in, &sb) == 0) {
if (url_in[0] == '/')
url_out = g_filename_to_uri(url_in, NULL, NULL);
else {
if (getcwd(cwd, PATH_MAX) != NULL) {
path = g_strdup_printf("%s" PS "%s", cwd,
url_in);
url_out = g_filename_to_uri(path, NULL, NULL);
g_free(path);
}
}
} else
url_out = g_strdup_printf("http://%s", url_in); /* guess http */
done:
DNPRINTF(XT_D_URL, "guess_url_type: guessed %s\n", url_out);
return (url_out);
}
void
set_normal_tab_meaning(struct tab *t)
{
if (t == NULL)
return;
set_xtp_meaning(t, XT_XTP_TAB_MEANING_NORMAL);
}
void
load_uri(struct tab *t, const gchar *uri)
{
struct karg args;
gchar *newuri = NULL;
int i;
if (uri == NULL)
return;
/* Strip leading spaces. */
while (*uri && isspace((unsigned char)*uri))
uri++;
if (strlen(uri) == 0) {
blank(t, NULL);
return;
}
set_normal_tab_meaning(t);
if (valid_url_type(uri)) {
if ((newuri = guess_url_type(uri)) != NULL)
uri = newuri;
else
uri = "";
}
/* clear :cert show host */
if (t->about_cert_host) {
g_free(t->about_cert_host);
t->about_cert_host = NULL;
}
if (!strncmp(uri, XT_URI_ABOUT, XT_URI_ABOUT_LEN)) {
for (i = 0; i < about_list_size(); i++)
if (!strcmp(&uri[XT_URI_ABOUT_LEN], about_list[i].name) &&
about_list[i].func != NULL) {
bzero(&args, sizeof args);
about_list[i].func(t, &args);
gtk_widget_set_sensitive(GTK_WIDGET(t->stop),
FALSE);
goto done;
}
show_oops(t, "invalid about page");
goto done;
}
/* remove old HTTPS cert chain (if any) */
if (t->pem) {
g_free(t->pem);
t->pem = NULL;
}
set_status(t, "Loading: %s", (char *)uri);
marks_clear(t);
webkit_web_view_load_uri(t->wv, uri);
done:
if (newuri)
g_free(newuri);
}
const gchar *
get_uri(struct tab *t)
{
const gchar *uri;
if (webkit_web_view_get_load_status(t->wv) == WEBKIT_LOAD_FAILED &&
!t->download_requested)
return (NULL);
if (t->xtp_meaning == XT_XTP_TAB_MEANING_NORMAL)
uri = webkit_web_view_get_uri(t->wv);
else
uri = t->tmp_uri;
return (uri);
}
void
set_xtp_meaning(struct tab *t, int tab_meaning)
{
t->xtp_meaning = tab_meaning;
if (tab_meaning == XT_XTP_TAB_MEANING_NORMAL) {
if (t->session_key != NULL) {
g_free(t->session_key);
t->session_key = NULL;
}
} else {
/* use tmp_uri to make sure it is g_freed */
if (t->tmp_uri)
g_free(t->tmp_uri);
t->tmp_uri = g_strdup_printf("%s%s", XT_URI_ABOUT,
about_list[t->xtp_meaning].name);
}
}
const gchar *
get_title(struct tab *t, bool window)
{
const gchar *set = NULL, *title = NULL;
WebKitLoadStatus status;
status = webkit_web_view_get_load_status(t->wv);
if (status == WEBKIT_LOAD_PROVISIONAL ||
(status == WEBKIT_LOAD_FAILED && !t->download_requested) ||
t->xtp_meaning == XT_XTP_TAB_MEANING_BL)
goto notitle;
title = webkit_web_view_get_title(t->wv);
if ((set = title ? title : get_uri(t)))
return set;
notitle:
set = window ? XT_NAME : "(untitled)";
return set;
}
struct mime_type *
find_mime_type(char *mime_type)
{
struct mime_type *m, *def = NULL, *rv = NULL;
TAILQ_FOREACH(m, &mtl, entry) {
if (m->mt_default &&
!strncmp(mime_type, m->mt_type, strlen(m->mt_type)))
def = m;
if (m->mt_default == 0 && !strcmp(mime_type, m->mt_type)) {
rv = m;
break;
}
}
if (rv == NULL)
rv = def;
return (rv);
}