forked from luakit/luakit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluah.c
852 lines (758 loc) · 22.9 KB
/
luah.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
/*
* luah.c - Lua helper functions
*
* Copyright (C) 2010 Mason Larobina <mason.larobina@gmail.com>
* Copyright (C) 2008-2009 Julien Danjou <julien@danjou.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <gtk/gtk.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <webkit/webkit.h>
#include "common/util.h"
#include "common/lualib.h"
#include "luakit.h"
#include "widget.h"
#include "luah.h"
void
luaH_modifier_table_push(lua_State *L, guint state) {
gint i = 1;
lua_newtable(L);
if (state & GDK_MODIFIER_MASK) {
#define MODKEY(key, name) \
if (state & GDK_##key##_MASK) { \
lua_pushstring(L, name); \
lua_rawseti(L, -2, i++); \
}
MODKEY(SHIFT, "Shift");
MODKEY(LOCK, "Lock");
MODKEY(CONTROL, "Control");
MODKEY(MOD1, "Mod1");
MODKEY(MOD2, "Mod2");
MODKEY(MOD3, "Mod3");
MODKEY(MOD4, "Mod4");
MODKEY(MOD5, "Mod5");
#undef MODKEY
}
}
void
luaH_keystr_push(lua_State *L, guint keyval)
{
gchar ucs[7];
guint ulen;
guint32 ukval = gdk_keyval_to_unicode(keyval);
/* check for printable unicode character */
if (g_unichar_isgraph(ukval)) {
ulen = g_unichar_to_utf8(ukval, ucs);
ucs[ulen] = 0;
lua_pushstring(L, ucs);
}
/* sent keysym for non-printable characters */
else
lua_pushstring(L, gdk_keyval_name(keyval));
}
/* UTF-8 aware string length computing.
* Returns the number of elements pushed on the stack. */
static gint
luaH_utf8_strlen(lua_State *L)
{
const gchar *cmd = luaL_checkstring(L, 1);
lua_pushnumber(L, (ssize_t) g_utf8_strlen(NONULL(cmd), -1));
return 1;
}
/* Overload standard Lua next function to use __next key on metatable.
* Returns the number of elements pushed on stack. */
static gint
luaHe_next(lua_State *L)
{
if(luaL_getmetafield(L, 1, "__next")) {
lua_insert(L, 1);
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
return lua_gettop(L);
}
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 2);
if(lua_next(L, 1))
return 2;
lua_pushnil(L);
return 1;
}
/* Overload lua_next() function by using __next metatable field to get
* next elements. `idx` is the index number of elements in stack.
* Returns 1 if more elements to come, 0 otherwise. */
gint
luaH_next(lua_State *L, gint idx)
{
if(luaL_getmetafield(L, idx, "__next")) {
/* if idx is relative, reduce it since we got __next */
if(idx < 0) idx--;
/* copy table and then move key */
lua_pushvalue(L, idx);
lua_pushvalue(L, -3);
lua_remove(L, -4);
lua_pcall(L, 2, 2, 0);
/* next returned nil, it's the end */
if(lua_isnil(L, -1)) {
/* remove nil */
lua_pop(L, 2);
return 0;
}
return 1;
}
else if(lua_istable(L, idx))
return lua_next(L, idx);
/* remove the key */
lua_pop(L, 1);
return 0;
}
/* Generic pairs function.
* Returns the number of elements pushed on stack. */
static gint
luaH_generic_pairs(lua_State *L)
{
lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
lua_pushvalue(L, 1); /* state, */
lua_pushnil(L); /* and initial value */
return 3;
}
/* Overload standard pairs function to use __pairs field of metatables.
* Returns the number of elements pushed on stack. */
static gint
luaHe_pairs(lua_State *L)
{
if(luaL_getmetafield(L, 1, "__pairs")) {
lua_insert(L, 1);
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
return lua_gettop(L);
}
luaL_checktype(L, 1, LUA_TTABLE);
return luaH_generic_pairs(L);
}
static gint
luaH_ipairs_aux(lua_State *L)
{
gint i = luaL_checkint(L, 2) + 1;
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushinteger(L, i);
lua_rawgeti(L, 1, i);
return (lua_isnil(L, -1)) ? 0 : 2;
}
/* Overload standard ipairs function to use __ipairs field of metatables.
* Returns the number of elements pushed on stack. */
static gint
luaHe_ipairs(lua_State *L)
{
if(luaL_getmetafield(L, 1, "__ipairs")) {
lua_insert(L, 1);
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
return lua_gettop(L);
}
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushvalue(L, lua_upvalueindex(1));
lua_pushvalue(L, 1);
lua_pushinteger(L, 0); /* and initial value */
return 3;
}
/* Enhanced type() function which recognize luakit objects.
* \param L The Lua VM state.
* \return The number of arguments pushed on the stack.
*/
static gint
luaHe_type(lua_State *L)
{
luaL_checkany(L, 1);
lua_pushstring(L, luaH_typename(L, 1));
return 1;
}
/* Fix up and add handy standard lib functions */
static void
luaH_fixups(lua_State *L)
{
/* export string.wlen */
lua_getglobal(L, "string");
lua_pushcfunction(L, &luaH_utf8_strlen);
lua_setfield(L, -2, "wlen");
lua_pop(L, 1);
/* replace next */
lua_pushliteral(L, "next");
lua_pushcfunction(L, luaHe_next);
lua_settable(L, LUA_GLOBALSINDEX);
/* replace pairs */
lua_pushliteral(L, "pairs");
lua_pushcfunction(L, luaHe_next);
lua_pushcclosure(L, luaHe_pairs, 1); /* pairs get next as upvalue */
lua_settable(L, LUA_GLOBALSINDEX);
/* replace ipairs */
lua_pushliteral(L, "ipairs");
lua_pushcfunction(L, luaH_ipairs_aux);
lua_pushcclosure(L, luaHe_ipairs, 1);
lua_settable(L, LUA_GLOBALSINDEX);
/* replace type */
lua_pushliteral(L, "type");
lua_pushcfunction(L, luaHe_type);
lua_settable(L, LUA_GLOBALSINDEX);
}
/* Look for an item: table, function, etc.
* \param L The Lua VM state.
* \param item The pointer item.
*/
gboolean
luaH_hasitem(lua_State *L, gconstpointer item)
{
lua_pushnil(L);
while(luaH_next(L, -2)) {
if(lua_topointer(L, -1) == item) {
/* remove value and key */
lua_pop(L, 2);
return TRUE;
}
if(lua_istable(L, -1))
if(luaH_hasitem(L, item)) {
/* remove key and value */
lua_pop(L, 2);
return TRUE;
}
/* remove value */
lua_pop(L, 1);
}
return FALSE;
}
/* Browse a table pushed on top of the index, and put all its table and
* sub-table ginto an array.
* \param L The Lua VM state.
* \param elems The elements array.
* \return False if we encounter an elements already in list.
*/
static gboolean
luaH_isloop_check(lua_State *L, GPtrArray *elems)
{
if(lua_istable(L, -1)) {
gconstpointer object = lua_topointer(L, -1);
/* Check that the object table is not already in the list */
for(guint i = 0; i < elems->len; i++)
if(elems->pdata[i] == object)
return FALSE;
/* push the table in the elements list */
g_ptr_array_add(elems, (gpointer) object);
/* look every object in the "table" */
lua_pushnil(L);
while(luaH_next(L, -2)) {
if(!luaH_isloop_check(L, elems)) {
/* remove key and value */
lua_pop(L, 2);
return FALSE;
}
/* remove value, keep key for next iteration */
lua_pop(L, 1);
}
}
return TRUE;
}
/* Check if a table is a loop. When using tables as direct acyclic digram,
* this is useful.
* \param L The Lua VM state.
* \param idx The index of the table in the stack
* \return True if the table loops.
*/
gboolean
luaH_isloop(lua_State *L, gint idx)
{
/* elems is an elements array that we will fill with all array we
* encounter while browsing the tables */
GPtrArray *elems = g_ptr_array_new();
/* push table on top */
lua_pushvalue(L, idx);
gboolean ret = luaH_isloop_check(L, elems);
/* remove pushed table */
lua_pop(L, 1);
g_ptr_array_free(elems, TRUE);
return !ret;
}
/* Returns a string from X selection.
* \param L The Lua VM state.
* \return The number of elements pushed on stack (1).
*/
static gint
luaH_luakit_get_selection(lua_State *L)
{
int n = lua_gettop(L);
GdkAtom atom = GDK_SELECTION_PRIMARY;
if (n) {
const gchar *arg = luaL_checkstring(L, 1);
/* Follow xclip(1) behavior: check only the first character of argument */
switch (arg[0]) {
case 'p':
break;
case 's':
atom = GDK_SELECTION_SECONDARY;
break;
case 'c':
atom = GDK_SELECTION_CLIPBOARD;
break;
default:
luaL_argerror(L, 1, "should be 'primary', 'secondary' or 'clipboard'");
break;
}
}
GtkClipboard *selection = gtk_clipboard_get(atom);
gchar *text = gtk_clipboard_wait_for_text(selection);
lua_pushstring(L, text);
g_free(text);
return 1;
}
/* Sets an X selection.
* \param L The Lua VM state.
* \return The number of elements pushed on stack (0).
*/
static gint
luaH_luakit_set_selection(lua_State *L)
{
int n = lua_gettop(L);
GdkAtom atom = GDK_SELECTION_PRIMARY;
if (0 == n)
luaL_error(L, "missing argument, string expected");
const gchar *text = luaL_checkstring(L, 1);
if (1 < n)
{
const gchar *arg = luaL_checkstring(L, 2);
/* Follow xclip(1) behavior: check only the first character of argument */
switch (arg[0]) {
case 'p':
break;
case 's':
atom = GDK_SELECTION_SECONDARY;
break;
case 'c':
atom = GDK_SELECTION_CLIPBOARD;
break;
default:
luaL_argerror(L, 1, "should be 'primary', 'secondary' or 'clipboard'");
break;
}
}
GtkClipboard *selection = gtk_clipboard_get(atom);
glong len = g_utf8_strlen (text, -1);
gtk_clipboard_set_text(selection, text, len);
return 0;
}
/* Escapes a string for use in a URI.
* \param L The Lua VM state.
* \return The number of elements pushed on stack (1).
*/
static gint
luaH_luakit_uri_encode(lua_State *L)
{
const gchar *string = luaL_checkstring(L, 1);
gchar *res = g_uri_escape_string(string, NULL, false);
lua_pushstring(L, res);
g_free(res);
return 1;
}
/* Unescapes a whole escaped string.
* \param L The Lua VM state.
* \return The number of elements pushed on stack (1).
*/
static gint
luaH_luakit_uri_decode(lua_State *L)
{
const gchar *string = luaL_checkstring(L, 1);
gchar *res = g_uri_unescape_string(string, NULL);
lua_pushstring(L, res);
g_free(res);
return 1;
}
static gint
luaH_luakit_get_special_dir(lua_State *L)
{
const gchar *name = luaL_checkstring(L, 1);
luakit_token_t token = l_tokenize(name);
GUserDirectory atom;
/* match token with G_USER_DIR_* atom */
switch(token) {
case L_TK_DESKTOP: atom = G_USER_DIRECTORY_DESKTOP; break;
case L_TK_DOCUMENTS: atom = G_USER_DIRECTORY_DOCUMENTS; break;
case L_TK_DOWNLOAD: atom = G_USER_DIRECTORY_DOWNLOAD; break;
case L_TK_MUSIC: atom = G_USER_DIRECTORY_MUSIC; break;
case L_TK_PICTURES: atom = G_USER_DIRECTORY_PICTURES; break;
case L_TK_PUBLIC_SHARE: atom = G_USER_DIRECTORY_PUBLIC_SHARE; break;
case L_TK_TEMPLATES: atom = G_USER_DIRECTORY_TEMPLATES; break;
case L_TK_VIDEOS: atom = G_USER_DIRECTORY_VIDEOS; break;
default:
warn("unknown atom G_USER_DIRECTORY_%s", name);
luaL_argerror(L, 1, "invalid G_USER_DIRECTORY_* atom");
return 0;
}
lua_pushstring(L, g_get_user_special_dir(atom));
return 1;
}
/* Spawns a command synchonously.
* \param L The Lua VM state.
* \return The number of elements pushed on stack (3).
*/
static gint
luaH_luakit_spawn_sync(lua_State *L)
{
GError *e = NULL;
gchar *_stdout = NULL;
gchar *_stderr = NULL;
gint rv;
struct sigaction sigact;
struct sigaction oldact;
const gchar *command = luaL_checkstring(L, 1);
/* Note: we have to temporarily clear the SIGCHLD handler. Otherwise
* g_spawn_sync wouldn't be able to read subprocess' return value. */
sigact.sa_handler=SIG_DFL;
sigemptyset (&sigact.sa_mask);
sigact.sa_flags=0;
if (sigaction(SIGCHLD, &sigact, &oldact))
fatal("Can't clear SIGCHLD handler");
g_spawn_command_line_sync(command, &_stdout, &_stderr, &rv, &e);
if (sigaction(SIGCHLD, &oldact, NULL))
fatal("Can't restore SIGCHLD handler");
if(e)
{
lua_pushstring(L, e->message);
g_clear_error(&e);
lua_error(L);
}
lua_pushinteger(L, WEXITSTATUS(rv));
lua_pushstring(L, _stdout);
lua_pushstring(L, _stderr);
g_free(_stdout);
g_free(_stderr);
return 3;
}
/* Spawns a command.
* \param L The Lua VM state.
* \return The number of elements pushed on stack (0).
*/
static gint
luaH_luakit_spawn(lua_State *L)
{
GError *e = NULL;
const gchar *command = luaL_checkstring(L, 1);
g_spawn_command_line_async(command, &e);
if(e)
{
lua_pushstring(L, e->message);
g_clear_error(&e);
lua_error(L);
}
return 0;
}
static gint
luaH_exec(lua_State *L)
{
const gchar *cmd = luaL_checkstring(L, 1);
l_exec(cmd);
return 0;
}
/* luakit global table.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lfield font The default font.
* \lfield font_height The default font height.
* \lfield conffile The configuration file which has been loaded.
*/
static gint
luaH_luakit_index(lua_State *L)
{
if(luaH_usemetatable(L, 1, 2))
return 1;
widget_t *w;
const gchar *prop = luaL_checkstring(L, 2);
luakit_token_t token = l_tokenize(prop);
switch(token) {
/* push class methods */
PF_CASE(GET_SPECIAL_DIR, luaH_luakit_get_special_dir)
PF_CASE(SPAWN, luaH_luakit_spawn)
PF_CASE(SPAWN_SYNC, luaH_luakit_spawn_sync)
PF_CASE(GET_SELECTION, luaH_luakit_get_selection)
PF_CASE(SET_SELECTION, luaH_luakit_set_selection)
PF_CASE(EXEC, luaH_exec)
PF_CASE(URI_ENCODE, luaH_luakit_uri_encode)
PF_CASE(URI_DECODE, luaH_luakit_uri_decode)
/* push string properties */
PS_CASE(CACHE_DIR, globalconf.cache_dir)
PS_CASE(CONFIG_DIR, globalconf.config_dir)
PS_CASE(DATA_DIR, globalconf.data_dir)
PS_CASE(EXECPATH, globalconf.execpath)
PS_CASE(CONFPATH, globalconf.confpath)
/* push boolean properties */
PB_CASE(VERBOSE, globalconf.verbose)
/* push integer properties */
PI_CASE(WEBKIT_MAJOR_VERSION, webkit_major_version())
PI_CASE(WEBKIT_MINOR_VERSION, webkit_minor_version())
PI_CASE(WEBKIT_MICRO_VERSION, webkit_micro_version())
PI_CASE(WEBKIT_USER_AGENT_MAJOR_VERSION, WEBKIT_USER_AGENT_MAJOR_VERSION)
PI_CASE(WEBKIT_USER_AGENT_MINOR_VERSION, WEBKIT_USER_AGENT_MINOR_VERSION)
case L_TK_WINDOWS:
lua_newtable(L);
for (guint i = 0; i < globalconf.windows->len; i++) {
w = globalconf.windows->pdata[i];
luaH_object_push(L, w->ref);
lua_rawseti(L, -2, i+1);
}
return 1;
case L_TK_INSTALL_PATH:
lua_pushliteral(L, LUAKIT_INSTALL_PATH);
return 1;
case L_TK_VERSION:
lua_pushliteral(L, VERSION);
return 1;
default:
break;
}
return 0;
}
/* Newindex function for the luakit global table.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static gint
luaH_luakit_newindex(lua_State *L)
{
if(luaH_usemetatable(L, 1, 2))
return 1;
size_t len;
const gchar *buf = luaL_checklstring(L, 2, &len);
debug("Luakit newindex %s", buf);
return 0;
}
/* Add a global signal.
* Returns the number of elements pushed on stack.
* \luastack
* \lparam A string with the event name.
* \lparam The function to call.
*/
static gint
luaH_luakit_add_signal(lua_State *L)
{
const gchar *name = luaL_checkstring(L, 1);
luaH_checkfunction(L, 2);
signal_add(globalconf.signals, name, luaH_object_ref(L, 2));
return 0;
}
/* Remove a global signal.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lparam A string with the event name.
* \lparam The function to call.
*/
static gint
luaH_luakit_remove_signal(lua_State *L)
{
const gchar *name = luaL_checkstring(L, 1);
luaH_checkfunction(L, 2);
gpointer func = (gpointer) lua_topointer(L, 2);
signal_remove(globalconf.signals, name, func);
luaH_object_unref(L, (void *) func);
return 0;
}
/* Emit a global signal.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
* \luastack
* \lparam A string with the event name.
* \lparam The function to call.
*/
static gint
luaH_luakit_emit_signal(lua_State *L)
{
signal_object_emit(L, globalconf.signals, luaL_checkstring(L, 1), lua_gettop(L) - 1);
return 0;
}
static gint
luaH_panic(lua_State *L)
{
warn("unprotected error in call to Lua API (%s)", lua_tostring(L, -1));
return 0;
}
static gint
luaH_quit(lua_State *L)
{
(void) L;
gtk_main_quit();
return 0;
}
static gint
luaH_dofunction_on_error(lua_State *L)
{
/* duplicate string error */
lua_pushvalue(L, -1);
/* emit error signal */
signal_object_emit(L, globalconf.signals, "debug::error", 1);
if(!luaL_dostring(L, "return debug.traceback(\"error while running function\", 3)"))
{
/* Move traceback before error */
lua_insert(L, -2);
/* Insert sentence */
lua_pushliteral(L, "\nerror: ");
/* Move it before error */
lua_insert(L, -2);
lua_concat(L, 3);
}
return 1;
}
void
luaH_init(void)
{
lua_State *L;
static const struct luaL_reg luakit_lib[] = {
{ "quit", luaH_quit },
{ "add_signal", luaH_luakit_add_signal },
{ "remove_signal", luaH_luakit_remove_signal },
{ "emit_signal", luaH_luakit_emit_signal },
{ "__index", luaH_luakit_index },
{ "__newindex", luaH_luakit_newindex },
{ NULL, NULL }
};
/* Lua VM init */
L = globalconf.L = luaL_newstate();
/* Set panic fuction */
lua_atpanic(L, luaH_panic);
/* Set error handling function */
lualib_dofunction_on_error = luaH_dofunction_on_error;
luaL_openlibs(L);
luaH_fixups(L);
luaH_object_setup(L);
/* Export luakit lib */
luaH_openlib(L, "luakit", luakit_lib, luakit_lib);
/* Export widget */
widget_class_setup(L);
/* add Lua search paths */
lua_getglobal(L, "package");
if(LUA_TTABLE != lua_type(L, 1)) {
warn("package is not a table");
return;
}
lua_getfield(L, 1, "path");
if(LUA_TSTRING != lua_type(L, 2)) {
warn("package.path is not a string");
lua_pop(L, 1);
return;
}
/* compile list of package search paths */
GPtrArray *paths = g_ptr_array_new_with_free_func(g_free);
#if DEVELOPMENT_PATHS
/* allows for testing luakit in the project directory */
g_ptr_array_add(paths, g_strdup("./lib"));
g_ptr_array_add(paths, g_strdup("./config"));
#endif
/* add users config dir (see: XDG_CONFIG_DIR) */
g_ptr_array_add(paths, g_strdup(globalconf.config_dir));
/* add system config dirs (see: XDG_CONFIG_DIRS) */
const gchar* const *config_dirs = g_get_system_config_dirs();
for (; *config_dirs; config_dirs++)
g_ptr_array_add(paths, g_build_filename(*config_dirs, "luakit", NULL));
/* add luakit install path */
g_ptr_array_add(paths, g_build_filename(LUAKIT_INSTALL_PATH, "lib", NULL));
const gchar *path;
for (guint i = 0; i < paths->len; i++) {
path = paths->pdata[i];
/* Search for file */
lua_pushliteral(L, ";");
lua_pushstring(L, path);
lua_pushliteral(L, "/?.lua");
lua_concat(L, 3);
/* Search for lib */
lua_pushliteral(L, ";");
lua_pushstring(L, path);
lua_pushliteral(L, "/?/init.lua");
lua_concat(L, 3);
/* concat with package.path */
lua_concat(L, 3);
}
g_ptr_array_free(paths, TRUE);
/* package.path = "concatenated string" */
lua_setfield(L, 1, "path");
}
gboolean
luaH_loadrc(const gchar *confpath, gboolean run)
{
debug("Loading rc: %s", confpath);
lua_State *L = globalconf.L;
if(!luaL_loadfile(L, confpath)) {
if(run) {
if(lua_pcall(L, 0, LUA_MULTRET, 0)) {
g_fprintf(stderr, "%s\n", lua_tostring(L, -1));
} else
return TRUE;
} else
lua_pop(L, 1);
return TRUE;
} else
g_fprintf(stderr, "%s\n", lua_tostring(L, -1));
return FALSE;
}
/* Load a configuration file. */
gboolean
luaH_parserc(const gchar *confpath, gboolean run)
{
const gchar* const *config_dirs = NULL;
gboolean ret = FALSE;
GPtrArray *paths = NULL;
/* try to load, return if it's ok */
if(confpath) {
if(luaH_loadrc(confpath, run))
ret = TRUE;
goto bailout;
}
/* compile list of config search paths */
paths = g_ptr_array_new_with_free_func(g_free);
#if DEVELOPMENT_PATHS
/* allows for testing luakit in the project directory */
g_ptr_array_add(paths, g_strdup("./config/rc.lua"));
#endif
/* search users config dir (see: XDG_CONFIG_HOME) */
g_ptr_array_add(paths, g_build_filename(globalconf.config_dir, "rc.lua", NULL));
/* search system config dirs (see: XDG_CONFIG_DIRS) */
config_dirs = g_get_system_config_dirs();
for(; *config_dirs; config_dirs++)
g_ptr_array_add(paths, g_build_filename(*config_dirs, "luakit", "rc.lua", NULL));
const gchar *path;
for (guint i = 0; i < paths->len; i++) {
path = paths->pdata[i];
if (file_exists(path)) {
if(luaH_loadrc(path, run)) {
globalconf.confpath = g_strdup(path);
ret = TRUE;
goto bailout;
} else if(!run)
goto bailout;
}
}
bailout:
if (paths) g_ptr_array_free(paths, TRUE);
return ret;
}
gint
luaH_class_index_miss_property(lua_State *L, lua_object_t *obj)
{
(void) obj;
signal_object_emit(L, globalconf.signals, "debug::index::miss", 2);
return 0;
}
gint
luaH_class_newindex_miss_property(lua_State *L, lua_object_t *obj)
{
(void) obj;
signal_object_emit(L, globalconf.signals, "debug::newindex::miss", 3);
return 0;
}
// vim: ft=c:et:sw=4:ts=8:sts=4:tw=80