]> git.armaanb.net Git - chorizo.git/blob - browser.c
11e34da963ff17c7876f3f3df08a918cf9661183
[chorizo.git] / browser.c
1 #include <limits.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <string.h>
8
9 #include <gtk/gtk.h>
10 #include <gtk/gtkx.h>
11 #include <gdk/gdkkeysyms.h>
12 #include <gio/gio.h>
13 #include <webkit2/webkit2.h>
14 #include <JavaScriptCore/JavaScript.h>
15
16
17 static void client_destroy(GtkWidget *, gpointer);
18 static gboolean client_destroy_request(WebKitWebView *, gpointer);
19 static WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean);
20 static WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
21                                          gpointer);
22 static void cooperation_setup(void);
23 static void changed_download_progress(GObject *, GParamSpec *, gpointer);
24 static void changed_load_progress(GObject *, GParamSpec *, gpointer);
25 static void changed_title(GObject *, GParamSpec *, gpointer);
26 static void changed_uri(GObject *, GParamSpec *, gpointer);
27 static gboolean crashed_web_view(WebKitWebView *, gpointer);
28 static gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
29                               WebKitPolicyDecisionType, gpointer);
30 static gboolean download_handle(WebKitDownload *, gchar *, gpointer);
31 static void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
32 static void downloadmanager_cancel(GtkToolButton *, gpointer);
33 static gboolean downloadmanager_delete(GtkWidget *, gpointer);
34 static void downloadmanager_setup(void);
35 static gchar *ensure_uri_scheme(const gchar *);
36 static void external_handler_run(GSimpleAction *, GVariant *, gpointer);
37 static void grab_environment_configuration(void);
38 static void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
39 static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
40 static void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
41 static gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
42 static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
43 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
44 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
45 static void keywords_load(void);
46 static gboolean keywords_try_search(WebKitWebView *, const gchar *);
47 static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
48                               WebKitHitTestResult *, gpointer);
49 static gboolean quit_if_nothing_active(void);
50 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
51 static void run_user_scripts(WebKitWebView *);
52 static void search(gpointer, gint);
53 static void show_web_view(WebKitWebView *, gpointer);
54 static Window tabbed_launch(void);
55 static void trust_user_certs(WebKitWebContext *);
56
57
58 struct Client
59 {
60     gchar *external_handler_uri;
61     gchar *hover_uri;
62     gchar *feed_html;
63     GtkWidget *location;
64     GtkWidget *vbox;
65     GtkWidget *web_view;
66     GtkWidget *win;
67 };
68
69 struct DownloadManager
70 {
71     GtkWidget *scroll;
72     GtkWidget *toolbar;
73     GtkWidget *win;
74 } dm;
75
76
77 static const gchar *accepted_language[2] = { NULL, NULL };
78 static gint clients = 0, downloads = 0;
79 static gboolean cooperative_alone = TRUE;
80 static gboolean cooperative_instances = TRUE;
81 static int cooperative_pipe_fp = 0;
82 static gchar *download_dir = "/var/tmp";
83 static gboolean enable_console_to_stdout = FALSE;
84 static Window embed = 0;
85 static gchar *fifo_suffix = "main";
86 static gdouble global_zoom = 1.0;
87 static gchar *history_file = NULL;
88 static gchar *home_uri = "about:blank";
89 static gboolean initial_wc_setup_done = FALSE;
90 static GHashTable *keywords = NULL;
91 static gchar *search_text = NULL;
92 static gboolean tabbed_automagic = TRUE;
93 static gchar *user_agent = NULL;
94
95
96 void
97 client_destroy(GtkWidget *widget, gpointer data)
98 {
99     struct Client *c = (struct Client *)data;
100
101     g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
102                                          changed_load_progress, c);
103
104     free(c);
105     clients--;
106
107     quit_if_nothing_active();
108 }
109
110 gboolean
111 client_destroy_request(WebKitWebView *web_view, gpointer data)
112 {
113     struct Client *c = (struct Client *)data;
114
115     gtk_widget_destroy(c->win);
116
117     return TRUE;
118 }
119
120 WebKitWebView *
121 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
122 {
123     struct Client *c;
124     WebKitWebContext *wc;
125     gchar *f;
126
127     if (uri != NULL && cooperative_instances && !cooperative_alone)
128     {
129         f = ensure_uri_scheme(uri);
130         write(cooperative_pipe_fp, f, strlen(f));
131         write(cooperative_pipe_fp, "\n", 1);
132         g_free(f);
133         return NULL;
134     }
135
136     c = calloc(1, sizeof(struct Client));
137     if (!c)
138     {
139         fprintf(stderr, __NAME__": fatal: calloc failed\n");
140         exit(EXIT_FAILURE);
141     }
142
143     if (embed != 0)
144     {
145         c->win = gtk_plug_new(embed);
146         if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
147         {
148             fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
149             gtk_widget_destroy(c->win);
150             c->win = NULL;
151             embed = 0;
152         }
153     }
154
155     if (c->win == NULL)
156         c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
157
158     gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
159
160     g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
161     gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
162
163     if (related_wv == NULL)
164         c->web_view = webkit_web_view_new();
165     else
166         c->web_view = webkit_web_view_new_with_related_view(related_wv);
167     wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
168
169     webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
170     g_signal_connect(G_OBJECT(c->web_view), "notify::title",
171                      G_CALLBACK(changed_title), c);
172     g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
173                      G_CALLBACK(changed_uri), c);
174     g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
175                      G_CALLBACK(changed_load_progress), c);
176     g_signal_connect(G_OBJECT(c->web_view), "create",
177                      G_CALLBACK(client_new_request), NULL);
178     g_signal_connect(G_OBJECT(c->web_view), "context-menu",
179                      G_CALLBACK(menu_web_view), c);
180     g_signal_connect(G_OBJECT(c->web_view), "close",
181                      G_CALLBACK(client_destroy_request), c);
182     g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
183                      G_CALLBACK(decide_policy), NULL);
184     g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
185                      G_CALLBACK(key_web_view), c);
186     g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
187                      G_CALLBACK(key_web_view), c);
188     g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
189                      G_CALLBACK(key_web_view), c);
190     g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
191                      G_CALLBACK(hover_web_view), c);
192     g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
193                      G_CALLBACK(crashed_web_view), c);
194
195     if (!initial_wc_setup_done)
196     {
197         if (accepted_language[0] != NULL)
198             webkit_web_context_set_preferred_languages(wc, accepted_language);
199
200         g_signal_connect(G_OBJECT(wc), "download-started",
201                          G_CALLBACK(download_handle_start), NULL);
202
203         trust_user_certs(wc);
204
205         initial_wc_setup_done = TRUE;
206     }
207
208     if (user_agent != NULL)
209         g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
210                      "user-agent", user_agent, NULL);
211
212     if (enable_console_to_stdout)
213         webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
214
215     webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
216
217     c->location = gtk_entry_new();
218     g_signal_connect(G_OBJECT(c->location), "key-press-event",
219                      G_CALLBACK(key_location), c);
220     g_signal_connect(G_OBJECT(c->location), "icon-release",
221                      G_CALLBACK(icon_location), c);
222     /* XXX This is a workaround. Setting this to NULL (which is done in
223      * grab_feeds_finished() if no feed has been detected) adds a little
224      * padding left of the text. Not sure why. The point of this call
225      * right here is to have that padding right from the start. This
226      * avoids a graphical artifact. */
227     gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
228                                       GTK_ENTRY_ICON_PRIMARY,
229                                       NULL);
230
231     c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
232     gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
233     gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
234
235     gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
236
237     if (show)
238         show_web_view(NULL, c);
239     else
240         g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
241                          G_CALLBACK(show_web_view), c);
242
243     if (uri != NULL)
244     {
245         f = ensure_uri_scheme(uri);
246         webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
247         g_free(f);
248     }
249
250     clients++;
251
252     return WEBKIT_WEB_VIEW(c->web_view);
253 }
254
255 WebKitWebView *
256 client_new_request(WebKitWebView *web_view,
257                    WebKitNavigationAction *navigation_action, gpointer data)
258 {
259     return client_new(NULL, web_view, FALSE);
260 }
261
262 void
263 cooperation_setup(void)
264 {
265     GIOChannel *towatch;
266     gchar *fifofilename, *fifopath;
267
268     fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
269     fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
270     g_free(fifofilename);
271
272     if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
273         mkfifo(fifopath, 0600);
274
275     cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
276     if (!cooperative_pipe_fp)
277     {
278         fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
279     }
280     else
281     {
282         if (write(cooperative_pipe_fp, "", 0) == -1)
283         {
284             /* Could not do an empty write to the FIFO which means there's
285              * no one listening. */
286             close(cooperative_pipe_fp);
287             towatch = g_io_channel_new_file(fifopath, "r+", NULL);
288             g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
289         }
290         else
291             cooperative_alone = FALSE;
292     }
293
294     g_free(fifopath);
295 }
296
297 void
298 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
299 {
300     WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
301     WebKitURIResponse *resp;
302     GtkToolItem *tb = GTK_TOOL_ITEM(data);
303     gdouble p, size_mb;
304     const gchar *uri;
305     gchar *t, *filename, *base;
306
307     p = webkit_download_get_estimated_progress(download);
308     p = p > 1 ? 1 : p;
309     p = p < 0 ? 0 : p;
310     p *= 100;
311     resp = webkit_download_get_response(download);
312     size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
313
314     uri = webkit_download_get_destination(download);
315     filename = g_filename_from_uri(uri, NULL, NULL);
316     if (filename == NULL)
317     {
318         /* This really should not happen because WebKit uses that URI to
319          * write to a file... */
320         fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
321         t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
322                             webkit_uri_response_get_uri(resp), p, size_mb);
323     }
324     else
325     {
326         base = g_path_get_basename(filename);
327         t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
328         g_free(filename);
329         g_free(base);
330     }
331     gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
332     g_free(t);
333 }
334
335 void
336 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
337 {
338     struct Client *c = (struct Client *)data;
339     gdouble p;
340     gchar *grab_feeds =
341         "a = document.querySelectorAll('"
342         "    html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
343         "    html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
344         "');"
345         "if (a.length == 0)"
346         "    null;"
347         "else"
348         "{"
349         "    out = '';"
350         "    for (i = 0; i < a.length; i++)"
351         "    {"
352         "        url = encodeURIComponent(a[i].href);"
353         "        if ('title' in a[i] && a[i].title != '')"
354         "            title = encodeURIComponent(a[i].title);"
355         "        else"
356         "            title = url;"
357         "        out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
358         "    }"
359         "    out;"
360         "}";
361
362     p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
363     if (p == 1)
364     {
365         p = 0;
366
367         /* The page has loaded fully. We now run the short JavaScript
368          * snippet above that operates on the DOM. It tries to grab all
369          * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
370          * references. */
371         webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
372                                        grab_feeds, NULL,
373                                        grab_feeds_finished, c);
374
375         run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
376     }
377     gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
378 }
379
380 void
381 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
382 {
383     const gchar *t, *u;
384     struct Client *c = (struct Client *)data;
385
386     u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
387     t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
388
389     u = u == NULL ? __NAME__ : u;
390     u = u[0] == 0 ? __NAME__ : u;
391
392     t = t == NULL ? u : t;
393     t = t[0] == 0 ? u : t;
394
395     gtk_window_set_title(GTK_WINDOW(c->win), t);
396 }
397
398 void
399 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
400 {
401     const gchar *t;
402     struct Client *c = (struct Client *)data;
403     FILE *fp;
404
405     t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
406
407     /* When a web process crashes, we get a "notify::uri" signal, but we
408      * can no longer read a meaningful URI. It's just an empty string
409      * now. Not updating the location bar in this scenario is important,
410      * because we would override the "WEB PROCESS CRASHED" message. */
411     if (t != NULL && strlen(t) > 0)
412     {
413         gtk_entry_set_text(GTK_ENTRY(c->location), t);
414
415         if (history_file != NULL)
416         {
417             fp = fopen(history_file, "a");
418             if (fp != NULL)
419             {
420                 fprintf(fp, "%s\n", t);
421                 fclose(fp);
422             }
423             else
424                 perror(__NAME__": Error opening history file");
425         }
426     }
427 }
428
429 gboolean
430 crashed_web_view(WebKitWebView *web_view, gpointer data)
431 {
432     gchar *t;
433     struct Client *c = (struct Client *)data;
434
435     t = g_strdup_printf("WEB PROCESS CRASHED: %s",
436                         webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
437     gtk_entry_set_text(GTK_ENTRY(c->location), t);
438     g_free(t);
439
440     return TRUE;
441 }
442
443 gboolean
444 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
445               WebKitPolicyDecisionType type, gpointer data)
446 {
447     WebKitResponsePolicyDecision *r;
448
449     switch (type)
450     {
451         case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
452             r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
453             if (!webkit_response_policy_decision_is_mime_type_supported(r))
454                 webkit_policy_decision_download(decision);
455             else
456                 webkit_policy_decision_use(decision);
457             break;
458         default:
459             /* Use whatever default there is. */
460             return FALSE;
461     }
462     return TRUE;
463 }
464
465 void
466 download_handle_finished(WebKitDownload *download, gpointer data)
467 {
468     downloads--;
469 }
470
471 void
472 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
473                       gpointer data)
474 {
475     g_signal_connect(G_OBJECT(download), "decide-destination",
476                      G_CALLBACK(download_handle), data);
477 }
478
479 gboolean
480 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
481 {
482     gchar *sug_clean, *path, *path2 = NULL, *uri;
483     GtkToolItem *tb;
484     int suffix = 1;
485     size_t i;
486
487     sug_clean = g_strdup(suggested_filename);
488     for (i = 0; i < strlen(sug_clean); i++)
489         if (sug_clean[i] == G_DIR_SEPARATOR)
490             sug_clean[i] = '_';
491
492     path = g_build_filename(download_dir, sug_clean, NULL);
493     path2 = g_strdup(path);
494     while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
495     {
496         g_free(path2);
497
498         path2 = g_strdup_printf("%s.%d", path, suffix);
499         suffix++;
500     }
501
502     if (suffix == 1000)
503     {
504         fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
505         webkit_download_cancel(download);
506     }
507     else
508     {
509         uri = g_filename_to_uri(path2, NULL, NULL);
510         webkit_download_set_destination(download, uri);
511         g_free(uri);
512
513         tb = gtk_tool_button_new(NULL, NULL);
514         gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
515         gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
516         gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
517         gtk_widget_show_all(dm.win);
518
519         g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
520                          G_CALLBACK(changed_download_progress), tb);
521
522         downloads++;
523         g_signal_connect(G_OBJECT(download), "finished",
524                          G_CALLBACK(download_handle_finished), NULL);
525
526         g_object_ref(download);
527         g_signal_connect(G_OBJECT(tb), "clicked",
528                          G_CALLBACK(downloadmanager_cancel), download);
529     }
530
531     g_free(sug_clean);
532     g_free(path);
533     g_free(path2);
534
535     /* Propagate -- to whom it may concern. */
536     return FALSE;
537 }
538
539 void
540 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
541 {
542     WebKitDownload *download = WEBKIT_DOWNLOAD(data);
543
544     webkit_download_cancel(download);
545     g_object_unref(download);
546
547     gtk_widget_destroy(GTK_WIDGET(tb));
548 }
549
550 gboolean
551 downloadmanager_delete(GtkWidget *obj, gpointer data)
552 {
553     if (!quit_if_nothing_active())
554         gtk_widget_hide(dm.win);
555
556     return TRUE;
557 }
558
559 void
560 downloadmanager_setup(void)
561 {
562     dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
563     gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
564     gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
565     gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
566     g_signal_connect(G_OBJECT(dm.win), "delete-event",
567                      G_CALLBACK(downloadmanager_delete), NULL);
568     g_signal_connect(G_OBJECT(dm.win), "key-press-event",
569                      G_CALLBACK(key_downloadmanager), NULL);
570
571     dm.toolbar = gtk_toolbar_new();
572     gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
573                                    GTK_ORIENTATION_VERTICAL);
574     gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
575     gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
576
577     dm.scroll = gtk_scrolled_window_new(NULL, NULL);
578     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
579                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
580     gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
581
582     gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
583 }
584
585 gchar *
586 ensure_uri_scheme(const gchar *t)
587 {
588     gchar *f, *fabs;
589
590     f = g_ascii_strdown(t, -1);
591     if (!g_str_has_prefix(f, "http:") &&
592         !g_str_has_prefix(f, "https:") &&
593         !g_str_has_prefix(f, "file:") &&
594         !g_str_has_prefix(f, "about:") &&
595         !g_str_has_prefix(f, "data:"))
596     {
597         g_free(f);
598         fabs = realpath(t, NULL);
599         if (fabs != NULL)
600         {
601             f = g_strdup_printf("file://%s", fabs);
602             free(fabs);
603         }
604         else
605             f = g_strdup_printf("http://%s", t);
606         return f;
607     }
608     else
609         return g_strdup(t);
610 }
611
612 void
613 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
614 {
615     struct Client *c = (struct Client *)data;
616     gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
617     GPid pid;
618     GError *err = NULL;
619
620     (void)simple;
621     (void)param;
622
623     argv[2] = c->external_handler_uri;
624     if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
625                        &pid, &err))
626     {
627         fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
628                 err->message);
629         g_error_free(err);
630     }
631     else
632         g_spawn_close_pid(pid);
633 }
634
635 void
636 grab_environment_configuration(void)
637 {
638     const gchar *e;
639
640     e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
641     if (e != NULL)
642         accepted_language[0] = g_strdup(e);
643
644     e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
645     if (e != NULL)
646         download_dir = g_strdup(e);
647
648     e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
649     if (e != NULL)
650         enable_console_to_stdout = TRUE;
651
652     e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
653     if (e != NULL)
654         fifo_suffix = g_strdup(e);
655
656     e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
657     if (e != NULL)
658         history_file = g_strdup(e);
659
660     e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
661     if (e != NULL)
662         home_uri = g_strdup(e);
663
664     e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
665     if (e != NULL)
666         user_agent = g_strdup(e);
667
668     e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
669     if (e != NULL)
670         global_zoom = atof(e);
671 }
672
673 void
674 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
675 {
676     struct Client *c = (struct Client *)data;
677     WebKitJavascriptResult *js_result;
678     JSCValue *value;
679     JSCException *exception;
680     GError *err = NULL;
681     gchar *str_value;
682
683     g_free(c->feed_html);
684     c->feed_html = NULL;
685
686     /* This was taken almost verbatim from the example in WebKit's
687      * documentation:
688      *
689      * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
690
691     js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
692                                                       result, &err);
693     if (!js_result)
694     {
695         fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
696         g_error_free(err);
697         return;
698     }
699
700     value = webkit_javascript_result_get_js_value(js_result);
701     if (jsc_value_is_string(value))
702     {
703         str_value = jsc_value_to_string(value);
704         exception = jsc_context_get_exception(jsc_value_get_context(value));
705         if (exception != NULL)
706         {
707             fprintf(stderr, __NAME__": Error running javascript: %s\n",
708                     jsc_exception_get_message(exception));
709         }
710         else
711             c->feed_html = str_value;
712
713         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
714                                           GTK_ENTRY_ICON_PRIMARY,
715                                           "application-rss+xml-symbolic");
716         gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
717                                        GTK_ENTRY_ICON_PRIMARY,
718                                        TRUE);
719     }
720     else
721     {
722         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
723                                           GTK_ENTRY_ICON_PRIMARY,
724                                           NULL);
725     }
726
727     webkit_javascript_result_unref(js_result);
728 }
729
730 void
731 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
732                gpointer data)
733 {
734     struct Client *c = (struct Client *)data;
735
736     if (!gtk_widget_is_focus(c->location))
737     {
738         if (webkit_hit_test_result_context_is_link(ht))
739         {
740             gtk_entry_set_text(GTK_ENTRY(c->location),
741                                webkit_hit_test_result_get_link_uri(ht));
742
743             if (c->hover_uri != NULL)
744                 g_free(c->hover_uri);
745             c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
746         }
747         else
748         {
749             gtk_entry_set_text(GTK_ENTRY(c->location),
750                                webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
751
752             if (c->hover_uri != NULL)
753                 g_free(c->hover_uri);
754             c->hover_uri = NULL;
755         }
756     }
757 }
758
759 void
760 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
761               gpointer data)
762 {
763     struct Client *c = (struct Client *)data;
764     gchar *d;
765     gchar *data_template =
766         "data:text/html,"
767         "<!DOCTYPE html>"
768         "<html>"
769         "    <head>"
770         "        <meta charset=\"UTF-8\">"
771         "        <title>Feeds</title>"
772         "    </head>"
773         "    <body>"
774         "        <p>Feeds found on this page:</p>"
775         "        <ul>"
776         "        %s"
777         "        </ul>"
778         "    </body>"
779         "</html>";
780
781     if (c->feed_html != NULL)
782     {
783         /* What we're actually trying to do is show a simple HTML page
784          * that lists all the feeds on the current page. The function
785          * webkit_web_view_load_html() looks like the proper way to do
786          * that. Sad thing is, it doesn't create a history entry, but
787          * instead simply replaces the content of the current page. This
788          * is not what we want.
789          *
790          * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
791          * mechanism to show my custom HTML snippet *and* create a
792          * history entry.
793          *
794          * [0]: https://tools.ietf.org/html/rfc2397
795          * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
796         d = g_strdup_printf(data_template, c->feed_html);
797         webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
798         g_free(d);
799     }
800 }
801
802 gboolean
803 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
804 {
805     struct Client *c = (struct Client *)data;
806     WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
807     gchar *f;
808
809     if (event->type == GDK_KEY_PRESS)
810     {
811         if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
812         {
813             switch (((GdkEventKey *)event)->keyval)
814             {
815                 case GDK_KEY_q:  /* close window (left hand) */
816                     gtk_widget_destroy(c->win);
817                     return TRUE;
818                 case GDK_KEY_w:  /* home (left hand) */
819                     f = ensure_uri_scheme(home_uri);
820                     webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
821                     g_free(f);
822                     return TRUE;
823                 case GDK_KEY_e:  /* new tab (left hand) */
824                     f = ensure_uri_scheme(home_uri);
825                     client_new(f, NULL, TRUE);
826                     g_free(f);
827                     return TRUE;
828                 case GDK_KEY_r:  /* reload (left hand) */
829                     webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
830                                                         c->web_view));
831                     return TRUE;
832                 case GDK_KEY_d:  /* download manager (left hand) */
833                     gtk_widget_show_all(dm.win);
834                     return TRUE;
835                 case GDK_KEY_2:  /* search forward (left hand) */
836                 case GDK_KEY_n:  /* search forward (maybe both hands) */
837                     search(c, 1);
838                     return TRUE;
839                 case GDK_KEY_3:  /* search backward (left hand) */
840                     search(c, -1);
841                     return TRUE;
842                 case GDK_KEY_l:  /* location (BOTH hands) */
843                     gtk_widget_grab_focus(c->location);
844                     return TRUE;
845                 case GDK_KEY_k:  /* initiate search (BOTH hands) */
846                     gtk_widget_grab_focus(c->location);
847                     gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
848                     gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
849                     return TRUE;
850                 case GDK_KEY_c:  /* reload trusted certs (left hand) */
851                     trust_user_certs(wc);
852                     return TRUE;
853                 case GDK_KEY_x:  /* launch external handler (left hand) */
854                     if (c->external_handler_uri != NULL)
855                         g_free(c->external_handler_uri);
856                     c->external_handler_uri = g_strdup(
857                         webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
858                     external_handler_run(NULL, NULL, c);
859                     return TRUE;
860             }
861         }
862         /* navigate backward (left hand) */
863         else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
864         {
865             webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
866             return TRUE;
867         }
868         /* navigate forward (left hand) */
869         else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
870         {
871             webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
872             return TRUE;
873         }
874     }
875
876     return FALSE;
877 }
878
879 gboolean
880 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
881 {
882     if (event->type == GDK_KEY_PRESS)
883     {
884         if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
885         {
886             switch (((GdkEventKey *)event)->keyval)
887             {
888                 case GDK_KEY_d:  /* close window (left hand) */
889                 case GDK_KEY_q:
890                     downloadmanager_delete(dm.win, NULL);
891                     return TRUE;
892             }
893         }
894     }
895
896     return FALSE;
897 }
898
899 gboolean
900 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
901 {
902     struct Client *c = (struct Client *)data;
903     const gchar *t;
904     gchar *f;
905
906     if (key_common(widget, event, data))
907         return TRUE;
908
909     if (event->type == GDK_KEY_PRESS)
910     {
911         switch (((GdkEventKey *)event)->keyval)
912         {
913             case GDK_KEY_KP_Enter:
914             case GDK_KEY_Return:
915                 gtk_widget_grab_focus(c->web_view);
916                 t = gtk_entry_get_text(GTK_ENTRY(c->location));
917                 if (t != NULL && t[0] == ':' && t[1] == '/')
918                 {
919                     if (search_text != NULL)
920                         g_free(search_text);
921                     search_text = g_strdup(t + 2);
922                     search(c, 0);
923                 }
924                 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
925                 {
926                     f = ensure_uri_scheme(t);
927                     webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
928                     g_free(f);
929                 }
930                 return TRUE;
931             case GDK_KEY_Escape:
932                 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
933                 gtk_entry_set_text(GTK_ENTRY(c->location),
934                                    (t == NULL ? __NAME__ : t));
935                 return TRUE;
936         }
937     }
938
939     return FALSE;
940 }
941
942 gboolean
943 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
944 {
945     struct Client *c = (struct Client *)data;
946     gdouble dx, dy;
947     gfloat z;
948
949     if (key_common(widget, event, data))
950         return TRUE;
951
952     if (event->type == GDK_KEY_PRESS)
953     {
954         if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
955         {
956             webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
957             gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
958         }
959     }
960     else if (event->type == GDK_BUTTON_RELEASE)
961     {
962         switch (((GdkEventButton *)event)->button)
963         {
964             case 2:
965                 if (c->hover_uri != NULL)
966                 {
967                     client_new(c->hover_uri, NULL, TRUE);
968                     return TRUE;
969                 }
970                 break;
971             case 8:
972                 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
973                 return TRUE;
974             case 9:
975                 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
976                 return TRUE;
977         }
978     }
979     else if (event->type == GDK_SCROLL)
980     {
981         if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
982             ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
983         {
984             gdk_event_get_scroll_deltas(event, &dx, &dy);
985             z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
986             z += -dy * 0.1;
987             z = dx != 0 ? global_zoom : z;
988             webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
989             return TRUE;
990         }
991     }
992
993     return FALSE;
994 }
995
996 void
997 keywords_load(void)
998 {
999     GError *err = NULL;
1000     GIOChannel *channel = NULL;
1001     gchar *path = NULL, *buf = NULL;
1002     gchar **tokens = NULL;
1003
1004     keywords = g_hash_table_new(g_str_hash, g_str_equal);
1005
1006     path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
1007                             NULL);
1008     channel = g_io_channel_new_file(path, "r", &err);
1009     if (channel != NULL)
1010     {
1011         while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
1012                == G_IO_STATUS_NORMAL)
1013         {
1014             g_strstrip(buf);
1015             if (buf[0] != '#')
1016             {
1017                 tokens = g_strsplit(buf, " ", 2);
1018                 if (tokens[0] != NULL && tokens[1] != NULL)
1019                     g_hash_table_insert(keywords, g_strdup(tokens[0]),
1020                                         g_strdup(tokens[1]));
1021                 g_strfreev(tokens);
1022             }
1023             g_free(buf);
1024         }
1025         g_io_channel_shutdown(channel, FALSE, NULL);
1026     }
1027     g_free(path);
1028 }
1029
1030 gboolean
1031 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1032 {
1033     gboolean ret = FALSE;
1034     gchar **tokens = NULL;
1035     gchar *val = NULL, *escaped = NULL, *uri = NULL;
1036
1037     tokens = g_strsplit(t, " ", 2);
1038     if (tokens[0] != NULL && tokens[1] != NULL)
1039     {
1040         val = g_hash_table_lookup(keywords, tokens[0]);
1041         if (val != NULL)
1042         {
1043             escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1044             uri = g_strdup_printf((gchar *)val, escaped);
1045             webkit_web_view_load_uri(web_view, uri);
1046             g_free(uri);
1047             g_free(escaped);
1048             ret = TRUE;
1049         }
1050     }
1051     g_strfreev(tokens);
1052
1053     return ret;
1054 }
1055
1056 gboolean
1057 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1058               WebKitHitTestResult *ht, gpointer data)
1059 {
1060     struct Client *c = (struct Client *)data;
1061     GSimpleAction *action = NULL;
1062     WebKitContextMenuItem *mi = NULL;
1063     const gchar *uri = NULL;
1064
1065     (void)ev;
1066
1067     if (webkit_hit_test_result_context_is_link(ht))
1068         uri = webkit_hit_test_result_get_link_uri(ht);
1069     else if (webkit_hit_test_result_context_is_image(ht))
1070         uri = webkit_hit_test_result_get_image_uri(ht);
1071     else if (webkit_hit_test_result_context_is_media(ht))
1072         uri = webkit_hit_test_result_get_media_uri(ht);
1073
1074     if (uri != NULL)
1075     {
1076         webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1077
1078         if (c->external_handler_uri != NULL)
1079             g_free(c->external_handler_uri);
1080         c->external_handler_uri = g_strdup(uri);
1081         action = g_simple_action_new("external_handler", NULL);
1082         g_signal_connect(G_OBJECT(action), "activate",
1083                          G_CALLBACK(external_handler_run), data);
1084         mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1085                                                        "Open with external handler",
1086                                                        NULL);
1087         webkit_context_menu_append(menu, mi);
1088         g_object_unref(action);
1089     }
1090
1091     /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1092     return FALSE;
1093 }
1094
1095 gboolean
1096 quit_if_nothing_active(void)
1097 {
1098     if (clients == 0)
1099     {
1100         if (downloads == 0)
1101         {
1102             gtk_main_quit();
1103             return TRUE;
1104         }
1105         else
1106             gtk_widget_show_all(dm.win);
1107     }
1108
1109     return FALSE;
1110 }
1111
1112 gboolean
1113 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1114 {
1115     gchar *uri = NULL;
1116
1117     g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1118     if (uri)
1119     {
1120         g_strstrip(uri);
1121         client_new(uri, NULL, TRUE);
1122         g_free(uri);
1123     }
1124     return TRUE;
1125 }
1126
1127 void
1128 run_user_scripts(WebKitWebView *web_view)
1129 {
1130     gchar *base = NULL, *path = NULL, *contents = NULL;
1131     const gchar *entry = NULL;
1132     GDir *scriptdir = NULL;
1133
1134     base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts", NULL);
1135     scriptdir = g_dir_open(base, 0, NULL);
1136     if (scriptdir != NULL)
1137     {
1138         while ((entry = g_dir_read_name(scriptdir)) != NULL)
1139         {
1140             path = g_build_filename(base, entry, NULL);
1141             if (g_str_has_suffix(path, ".js"))
1142             {
1143                 if (g_file_get_contents(path, &contents, NULL, NULL))
1144                 {
1145                     webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1146                     g_free(contents);
1147                 }
1148             }
1149             g_free(path);
1150         }
1151         g_dir_close(scriptdir);
1152     }
1153
1154     g_free(base);
1155 }
1156
1157 void
1158 search(gpointer data, gint direction)
1159 {
1160     struct Client *c = (struct Client *)data;
1161     WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1162     WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1163
1164     if (search_text == NULL)
1165         return;
1166
1167     switch (direction)
1168     {
1169         case 0:
1170             webkit_find_controller_search(fc, search_text,
1171                                           WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1172                                           WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1173                                           G_MAXUINT);
1174             break;
1175         case 1:
1176             webkit_find_controller_search_next(fc);
1177             break;
1178         case -1:
1179             webkit_find_controller_search_previous(fc);
1180             break;
1181     }
1182 }
1183
1184 void
1185 show_web_view(WebKitWebView *web_view, gpointer data)
1186 {
1187     struct Client *c = (struct Client *)data;
1188
1189     (void)web_view;
1190
1191     gtk_widget_grab_focus(c->web_view);
1192     gtk_widget_show_all(c->win);
1193 }
1194
1195 Window
1196 tabbed_launch(void)
1197 {
1198     gint tabbed_stdout;
1199     GIOChannel *tabbed_stdout_channel;
1200     GError *err = NULL;
1201     gchar *output = NULL;
1202     char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
1203     Window plug_into;
1204
1205     if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
1206                                   NULL, NULL, NULL, &tabbed_stdout, NULL,
1207                                   &err))
1208     {
1209         fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
1210         g_error_free(err);
1211         return 0;
1212     }
1213
1214     tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
1215     if (tabbed_stdout_channel == NULL)
1216     {
1217         fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
1218         return 0;
1219     }
1220     g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
1221     g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
1222     if (output == NULL)
1223     {
1224         fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
1225         return 0;
1226     }
1227     g_strstrip(output);
1228     plug_into = strtol(output, NULL, 16);
1229     g_free(output);
1230     if (plug_into == 0)
1231         fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
1232     return plug_into;
1233 }
1234
1235 void
1236 trust_user_certs(WebKitWebContext *wc)
1237 {
1238     GTlsCertificate *cert;
1239     const gchar *basedir, *file, *absfile;
1240     GDir *dir;
1241
1242     basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1243     dir = g_dir_open(basedir, 0, NULL);
1244     if (dir != NULL)
1245     {
1246         file = g_dir_read_name(dir);
1247         while (file != NULL)
1248         {
1249             absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1250                                        file, NULL);
1251             cert = g_tls_certificate_new_from_file(absfile, NULL);
1252             if (cert == NULL)
1253                 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1254             else
1255                 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1256             file = g_dir_read_name(dir);
1257         }
1258         g_dir_close(dir);
1259     }
1260 }
1261
1262
1263 int
1264 main(int argc, char **argv)
1265 {
1266     gchar *c;
1267     int opt, i;
1268
1269     gtk_init(&argc, &argv);
1270     webkit_web_context_set_process_model(webkit_web_context_get_default(),
1271         WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1272
1273     grab_environment_configuration();
1274
1275     while ((opt = getopt(argc, argv, "e:CT")) != -1)
1276     {
1277         switch (opt)
1278         {
1279             case 'e':
1280                 embed = atol(optarg);
1281                 tabbed_automagic = FALSE;
1282                 break;
1283             case 'C':
1284                 cooperative_instances = FALSE;
1285                 break;
1286             case 'T':
1287                 tabbed_automagic = FALSE;
1288                 break;
1289             default:
1290                 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1291                 exit(EXIT_FAILURE);
1292         }
1293     }
1294
1295     keywords_load();
1296     if (cooperative_instances)
1297         cooperation_setup();
1298     downloadmanager_setup();
1299
1300     if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
1301         embed = tabbed_launch();
1302
1303     if (!cooperative_instances || cooperative_alone)
1304     {
1305         c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1306                              NULL);
1307         webkit_web_context_set_web_extensions_directory(
1308             webkit_web_context_get_default(), c
1309         );
1310     }
1311
1312     if (optind >= argc)
1313         client_new(home_uri, NULL, TRUE);
1314     else
1315     {
1316         for (i = optind; i < argc; i++)
1317             client_new(argv[i], NULL, TRUE);
1318     }
1319
1320     if (!cooperative_instances || cooperative_alone)
1321         gtk_main();
1322     exit(EXIT_SUCCESS);
1323 }