~ubuntu-branches/ubuntu/saucy/luakit/saucy-proposed

« back to all changes in this revision

Viewing changes to .pc/debian-changes-2011.07.22-2/luakit.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Adams
  • Date: 2011-07-26 14:36:08 UTC
  • Revision ID: james.westby@ubuntu.com-20110726143608-elctsivefctyqsqi
Tags: 2011.07.22-2
Change WebKitGTK+ build dependency to libwebkitgtk-dev.  closes:
#635418.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * luakit.c - luakit main functions
 
3
 *
 
4
 * Copyright © 2010-2011 Mason Larobina <mason.larobina@gmail.com>
 
5
 *
 
6
 * This program is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 *
 
19
 */
 
20
 
 
21
#include "globalconf.h"
 
22
#include "common/util.h"
 
23
#include "luah.h"
 
24
 
 
25
#include <gtk/gtk.h>
 
26
#include <signal.h>
 
27
#include <stdlib.h>
 
28
#include <sys/wait.h>
 
29
#include <unistd.h>
 
30
#include <errno.h>
 
31
#include <locale.h>
 
32
 
 
33
static void sigchld(int sigint);
 
34
 
 
35
void
 
36
sigchld(int signum) {
 
37
    (void) signum;
 
38
    while(0 < waitpid(-1, NULL, WNOHANG));
 
39
}
 
40
 
 
41
void
 
42
init_lua(gchar **uris)
 
43
{
 
44
    gchar *uri;
 
45
    lua_State *L;
 
46
 
 
47
    /* init globalconf structs */
 
48
    globalconf.windows = g_ptr_array_new();
 
49
 
 
50
    /* init lua */
 
51
    luaH_init();
 
52
    L = globalconf.L;
 
53
 
 
54
    /* push a table of the statup uris */
 
55
    lua_newtable(L);
 
56
    for (gint i = 0; uris && (uri = uris[i]); i++) {
 
57
        lua_pushstring(L, uri);
 
58
        lua_rawseti(L, -2, i + 1);
 
59
    }
 
60
    lua_setglobal(L, "uris");
 
61
}
 
62
 
 
63
void
 
64
init_directories(void)
 
65
{
 
66
    /* create luakit directory */
 
67
    globalconf.cache_dir  = g_build_filename(g_get_user_cache_dir(),  "luakit", NULL);
 
68
    globalconf.config_dir = g_build_filename(g_get_user_config_dir(), "luakit", NULL);
 
69
    globalconf.data_dir   = g_build_filename(g_get_user_data_dir(),   "luakit", NULL);
 
70
    g_mkdir_with_parents(globalconf.cache_dir,  0771);
 
71
    g_mkdir_with_parents(globalconf.config_dir, 0771);
 
72
    g_mkdir_with_parents(globalconf.data_dir,   0771);
 
73
}
 
74
 
 
75
/* load command line options into luakit and return uris to load */
 
76
gchar**
 
77
parseopts(int argc, gchar *argv[], gboolean **nonblock) {
 
78
    GOptionContext *context;
 
79
    gboolean *version_only = NULL;
 
80
    gboolean *check_only = NULL;
 
81
    gchar **uris = NULL;
 
82
 
 
83
    /* save luakit exec path */
 
84
    globalconf.execpath = g_strdup(argv[0]);
 
85
 
 
86
    /* define command line options */
 
87
    const GOptionEntry entries[] = {
 
88
      { "uri",     'u', 0, G_OPTION_ARG_STRING_ARRAY, &uris,                 "uri(s) to load at startup", "URI"  },
 
89
      { "config",  'c', 0, G_OPTION_ARG_STRING,       &globalconf.confpath,  "configuration file to use", "FILE" },
 
90
      { "verbose", 'v', 0, G_OPTION_ARG_NONE,         &globalconf.verbose,   "print debugging output",    NULL   },
 
91
      { "version", 'V', 0, G_OPTION_ARG_NONE,         &version_only,         "print version and exit",    NULL   },
 
92
      { "check",   'k', 0, G_OPTION_ARG_NONE,         &check_only,           "check config and exit",     NULL   },
 
93
      { "nonblock",'n', 0, G_OPTION_ARG_NONE,         nonblock,              "run in background",         NULL   },
 
94
      { NULL,      0,   0, 0,                         NULL,                  NULL,                        NULL   },
 
95
    };
 
96
 
 
97
    /* parse command line options */
 
98
    context = g_option_context_new("[URI...]");
 
99
    g_option_context_add_main_entries(context, entries, NULL);
 
100
    g_option_context_add_group(context, gtk_get_option_group(FALSE));
 
101
    // TODO Passing gtk options (like --sync) to luakit causes a segfault right
 
102
    // here. I'm clueless.
 
103
    g_option_context_parse(context, &argc, &argv, NULL);
 
104
    g_option_context_free(context);
 
105
 
 
106
    /* print version and exit */
 
107
    if (version_only) {
 
108
        g_printf("luakit %s\n", VERSION);
 
109
        exit(EXIT_SUCCESS);
 
110
    }
 
111
 
 
112
    /* check config syntax and exit */
 
113
    if (check_only) {
 
114
        init_directories();
 
115
        init_lua(NULL);
 
116
        if (!luaH_parserc(globalconf.confpath, FALSE)) {
 
117
            g_fprintf(stderr, "Confiuration file syntax error.\n");
 
118
            exit(EXIT_FAILURE);
 
119
        } else {
 
120
            g_fprintf(stderr, "Configuration file syntax OK.\n");
 
121
            exit(EXIT_SUCCESS);
 
122
        }
 
123
    }
 
124
 
 
125
    if (uris && argv[1])
 
126
        fatal("invalid mix of -u and default uri arguments");
 
127
 
 
128
    if (uris)
 
129
        return uris;
 
130
    else
 
131
        return argv+1;
 
132
}
 
133
 
 
134
gint
 
135
main(gint argc, gchar *argv[]) {
 
136
    gboolean *nonblock = NULL;
 
137
    gchar **uris = NULL;
 
138
    pid_t pid, sid;
 
139
 
 
140
    /* clean up any zombies */
 
141
    struct sigaction sigact;
 
142
    sigact.sa_handler=sigchld;
 
143
    sigemptyset (&sigact.sa_mask);
 
144
    sigact.sa_flags = SA_NOCLDSTOP;
 
145
    if (sigaction(SIGCHLD, &sigact, NULL))
 
146
        fatal("Can't install SIGCHLD handler");
 
147
 
 
148
    /* set numeric locale to C (required for compatibility with
 
149
       LuaJIT and luakit scripts) */
 
150
    gtk_set_locale();
 
151
    gtk_disable_setlocale();
 
152
    setlocale(LC_NUMERIC, "C");
 
153
 
 
154
    /* parse command line opts and get uris to load */
 
155
    uris = parseopts(argc, argv, &nonblock);
 
156
 
 
157
    /* if non block mode - respawn, detach and continue in child */
 
158
    if (nonblock) {
 
159
        pid = fork();
 
160
        if (pid < 0) {
 
161
            fatal("Cannot fork: %d", errno);
 
162
        } else if (pid > 0) {
 
163
            exit(EXIT_SUCCESS);
 
164
        }
 
165
        sid = setsid();
 
166
        if (sid < 0) {
 
167
            fatal("New SID creation failure: %d", errno);
 
168
        }
 
169
    }
 
170
 
 
171
    gtk_init(&argc, &argv);
 
172
    if (!g_thread_supported())
 
173
        g_thread_init(NULL);
 
174
 
 
175
    init_directories();
 
176
    init_lua(uris);
 
177
 
 
178
    /* parse and run configuration file */
 
179
    if(!luaH_parserc(globalconf.confpath, TRUE))
 
180
        fatal("couldn't find rc file");
 
181
 
 
182
    if (!globalconf.windows->len)
 
183
        fatal("no windows spawned by rc file, exiting");
 
184
 
 
185
    gtk_main();
 
186
    return EXIT_SUCCESS;
 
187
}
 
188
 
 
189
// vim: ft=c:et:sw=4:ts=8:sts=4:tw=80