cleanup uzblctrl
[uzbl-mobile] / uzblctrl.c
1 /* -*- c-basic-offset: 4; -*- */
2 /* Socket code more or less completely copied from here: http://www.ecst.csuchico.edu/~beej/guide/ipc/usock.html */
3
4 #include <gtk/gtk.h>
5 #include <gdk/gdkx.h>
6 #include <gdk/gdkkeysyms.h>
7 #include <webkit/webkit.h>
8 #include <pthread.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/un.h>
20
21 static gchar* sockpath;
22 static gchar* command;
23
24 static GOptionEntry entries[] =
25 {
26     { "socket",  's', 0, G_OPTION_ARG_STRING, &sockpath, "Path to the uzbl socket", NULL },
27     { "command", 'c', 0, G_OPTION_ARG_STRING, &command,  "The uzbl command to execute",    NULL },
28     { NULL,       0,  0, 0,                    NULL,      NULL,                            NULL }
29 };
30
31 int
32 main(int argc, char* argv[]) {
33     GError *error = NULL;
34     GOptionContext* context = g_option_context_new ("- utility for controlling uzbl");
35     g_option_context_add_main_entries (context, entries, NULL);
36     g_option_context_add_group        (context, gtk_get_option_group (TRUE));
37     g_option_context_parse            (context, &argc, &argv, &error);
38
39
40     if (sockpath && command) {
41         int s, len;
42         struct sockaddr_un remote;
43         
44         if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) == -1) {
45             perror ("socket");
46             exit (1);
47         }
48         
49         remote.sun_family = AF_UNIX;
50         strcpy (remote.sun_path, (char *) sockpath);
51         len = strlen (remote.sun_path) + sizeof (remote.sun_family);
52         
53         if (connect (s, (struct sockaddr *) &remote, len) == -1) {
54             perror ("connect");
55             exit (1);
56         }
57         
58         if (send (s, command, strlen (command), 0) == -1) {
59             perror ("send");
60             exit (1);
61         }
62         
63         close(s);
64         
65         return 0;
66     } else {
67         puts ("Usage: uzblctrl -s /path/to/socket -c \"command\"");
68         return 1;
69     }
70 }
71 /* vi: set et ts=4: */