File: lua.cc

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (627 lines) | stat: -rw-r--r-- 12,029 bytes parent folder | download | duplicates (2)
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
// Copyright (C) 2003 Graydon Hoare <graydon@pobox.com>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.

#include "base.hh"
#include "lua.hh"

#include "globish.hh"
#include "sanity.hh"
#include "platform.hh"
#include "pcrewrap.hh"

#include <set>
#include "vector.hh"
#include <utility>
#include <cstring> // strlen
#include <algorithm> // std::sort

using std::pair;
using std::set;
using std::sort;
using std::string;
using std::vector;
using std::malloc;
using std::free;

// adapted from "programming in lua", section 24.2.3
// http://www.lua.org/pil/24.2.3.html
// output is from bottom (least accessible) to top (most accessible, where
// push and pop happen).
static string
dump_stack(lua_State * st)
{
  string out;
  int i;
  int top = lua_gettop(st);
  for (i = 1; i <= top; i++) {  /* repeat for each level */
    int t = lua_type(st, i);
    switch (t) {
    case LUA_TSTRING:  /* strings */
      out += '`';
      out += string(lua_tostring(st, i), lua_strlen(st, i));
      out += '\'';
      break;

    case LUA_TBOOLEAN:  /* booleans */
      out += (lua_toboolean(st, i) ? "true" : "false");
      break;

    case LUA_TNUMBER:  /* numbers */
      out += (FL("%g") % lua_tonumber(st, i)).str();
      break;

    default:  /* other values */
      out += std::string(lua_typename(st, t));
      break;

    }
    out += "  ";  /* put a separator */
  }
  return out;
}


Lua::Lua(lua_State * s) :
  st(s), failed(false)
{}

Lua::~Lua()
{
  lua_settop(st, 0);
}

void
Lua::fail(string const & reason)
{
  L(FL("lua failure: %s; stack = %s") % reason % dump_stack(st));
  failed = true;
}

bool
Lua::ok()
{
  if (failed)
    L(FL("Lua::ok(): failed"));
  return !failed;
}

void
Lua::report_error()
{
//  I(lua_isstring(st, -1));
  string err = string(lua_tostring(st, -1), lua_strlen(st, -1));
  W(i18n_format("%s") % err);
  L(FL("lua stack: %s") % dump_stack(st));
  lua_pop(st, 1);
  failed = true;
}

bool
Lua::check_stack(int count)
{
  if (!lua_checkstack(st, count))
    {
      fail((FL("lua stack limit '%d' reached") % LUAI_MAXCSTACK).str());
      return false;
    }
  return true;
}

// getters

Lua &
Lua::get(int idx)
{
  if (failed) return *this;
  if (!lua_istable (st, idx))
    {
      fail("istable() in get");
      return *this;
    }
  if (lua_gettop (st) < 1)
    {
      fail("stack top > 0 in get");
      return *this;
    }
  lua_gettable(st, idx);
  return *this;
}

Lua &
Lua::get_fn(int idx)
{
  if (failed) return *this;
  get(idx);
  if (!lua_isfunction (st, -1))
    fail("isfunction() in get_fn");
  return *this;
}

Lua &
Lua::get_tab(int idx)
{
  if (failed) return *this;
  get(idx);
  if (!lua_istable (st, -1))
    fail("istable() in get_tab");
  return *this;
}

Lua &
Lua::get_str(int idx)
{
  if (failed) return *this;
  get(idx);
  if (!lua_isstring (st, -1))
    fail("isstring() in get_str");
  return *this;
}

Lua &
Lua::get_num(int idx)
{
  if (failed) return *this;
  get(idx);
  if (!lua_isnumber (st, -1))
    fail("isnumber() in get_num");
  return *this;
}

Lua &
Lua::get_bool(int idx)
{
  if (failed) return *this;
  get(idx);
  if (!lua_isboolean (st, -1))
    fail("isboolean() in get_bool");
  return *this;
}

// extractors

Lua &
Lua::extract_str_nolog(string & str)
{
  if (failed) return *this;
  if (!lua_isstring (st, -1))
    {
      fail("isstring() in extract_str");
      return *this;
    }
  str = string(lua_tostring(st, -1), lua_strlen(st, -1));
  return *this;
}

Lua &
Lua::extract_str(string & str)
{
  extract_str_nolog(str);
  L(FL("lua: extracted string = %s") % str);
  return *this;
}

Lua &
Lua::extract_classified_str(string & str)
{
  extract_str_nolog(str);
  L(FL("lua: extracted string [CLASSIFIED]"));
  return *this;
}

Lua &
Lua::extract_int(int & i)
{
  if (failed) return *this;
  if (!lua_isnumber (st, -1))
    {
      fail("isnumber() in extract_int");
      return *this;
    }
  i = lua_tointeger(st, -1);
  L(FL("lua: extracted int = %i") % i);
  return *this;
}

Lua &
Lua::extract_double(double & i)
{
  if (failed) return *this;
  if (!lua_isnumber (st, -1))
    {
      fail("isnumber() in extract_double");
      return *this;
    }
  i = lua_tonumber(st, -1);
  L(FL("lua: extracted double = %i") % i);
  return *this;
}


Lua &
Lua::extract_bool(bool & i)
{
  if (failed) return *this;
  if (!lua_isboolean (st, -1))
    {
      fail("isboolean() in extract_bool");
      return *this;
    }
  i = (lua_toboolean(st, -1) == 1);
  L(FL("lua: extracted bool = %i") % i);
  return *this;
}


// table iteration

Lua &
Lua::begin()
{
  if (failed) return *this;
  if (!lua_istable(st, -1))
    {
      fail("istable() in begin");
      return *this;
    }
  check_stack(1);
  lua_pushnil(st);
  return *this;
}

bool
Lua::next()
{
  if (failed) return false;
  if (!lua_istable(st, -2))
    {
      fail("istable() in next");
      return false;
    }
  if (!check_stack(1)) return false;
  if (lua_next(st, -2) != 0)
    {
      return true;
    }
  pop();
  return false;
}

// pushers

Lua &
Lua::push_str(string const & str)
{
  if (failed) return *this;
  if (!check_stack(1)) return *this;
  lua_pushlstring(st, str.c_str(), str.size());
  return *this;
}

Lua &
Lua::push_int(int num)
{
  if (failed) return *this;
  if (!check_stack(1)) return *this;
  lua_pushnumber(st, num);
  return *this;
}

Lua &
Lua::push_double(double num)
{
  if (failed) return *this;
  if (check_stack(1)) return *this;
  lua_pushnumber(st, num);
  return *this;
}

Lua &
Lua::push_bool(bool b)
{
  if (failed) return *this;
  if (!check_stack(1)) return *this;
  lua_pushboolean(st, b);
  return *this;
}

Lua &
Lua::push_nil()
{
  if (failed) return *this;
  if (!check_stack(1)) return *this;
  lua_pushnil(st);
  return *this;
}

Lua &
Lua::push_table()
{
  if (failed) return *this;
  if (!check_stack(1)) return *this;
  lua_newtable(st);
  return *this;
}

Lua &
Lua::set_table(int idx)
{
  if (failed) return *this;
  if (!check_stack(1)) return *this;
  lua_settable(st, idx);
  return *this;
}

Lua &
Lua::set_field(const string& key, int idx)
{
  if (failed) return *this;
  lua_setfield(st, idx, key.c_str());
  return *this;
}

Lua &
Lua::call(int in, int out)
{
  if (failed) return *this;
  if (!check_stack(out)) return *this;
  if (lua_pcall(st, in, out, 0) != 0)
    {
      report_error();
    }
  return *this;
}

Lua &
Lua::pop(int count)
{
  if (failed) return *this;
  if (lua_gettop (st) < count)
    {
      fail("stack top is not >= count in pop");
      return *this;
    }
  lua_pop(st, count);
  return *this;
}

Lua &
Lua::func(string const & fname)
{
  L(FL("loading lua hook %s") % fname);
  if (!failed)
    {
      if (missing_functions.find(fname) != missing_functions.end())
        failed = true;
      else
        {
          push_str(fname);
          get_fn();
          if (failed)
            missing_functions.insert(fname);
        }
    }
  return *this;
}

Lua &
Lua::loadstring(char const * str, char const * identity)
{
  if (!failed)
    {
      if (luaL_loadbuffer(st, str, strlen(str), identity))
        {
          report_error();
        }
    }
  return *this;
}

Lua &
Lua::loadfile(char const * filename)
{
  if (!failed)
    {
      if (luaL_loadfile(st, filename))
        {
          report_error();
        }
    }
  return *this;
}

set<string> Lua::missing_functions;

luaext::ftmap * luaext::fns;

luaext::extfn::extfn(std::string const & name, std::string const & table, int (*func) (lua_State *))
{
  static bool first(true);
  if (first)
    {
      first = false;
      fns = new ftmap;
    }
  (*fns)[table].insert(make_pair(name, func));
}

void add_functions(lua_State * st)
{
  for (luaext::ftmap::const_iterator i = luaext::fns->begin();
       i != luaext::fns->end(); ++i)
    {
      std::string const & table(i->first);
      if (table != "")
        {
          lua_newtable(st);
          lua_pushvalue(st, -1);
          lua_setfield(st, LUA_GLOBALSINDEX, table.c_str());
        }
      for (luaext::fmap::const_iterator j = i->second.begin();
           j != i->second.end(); ++j)
        {
          if (table == "")
            lua_register(st, j->first.c_str(), j->second);
          else
            {
              lua_pushcfunction(st, j->second);
              lua_setfield(st, -2, j->first.c_str());
            }
        }
      if (table != "")
        lua_pop(st, 1);
    }
}

LUAEXT(include, )
{
  const char *path = luaL_checkstring(LS, -1);
  E(path, origin::user,
    F("%s called with an invalid parameter") % "Include");

  bool res = run_file(LS, path);

  lua_pushboolean(LS, res);
  return 1;
}

LUAEXT(includedir, )
{
  const char *pathstr = luaL_checkstring(LS, -1);
  E(pathstr, origin::user,
    F("%s called with an invalid parameter") % "IncludeDir");

  run_directory(LS, pathstr, "*");
  lua_pushboolean(LS, true);
  return 1;
}

LUAEXT(includedirpattern, )
{
  const char *pathstr = luaL_checkstring(LS, -2);
  const char *pattern = luaL_checkstring(LS, -1);
  E(pathstr && pattern, origin::user,
    F("%s called with an invalid parameter") % "IncludeDirPattern");

  run_directory(LS, pathstr, pattern);
  lua_pushboolean(LS, true);
  return 1;
}

LUAEXT(search, regex)
{
  const char *re = luaL_checkstring(LS, -2);
  const char *str = luaL_checkstring(LS, -1);

  bool result = false;
  try {
    result = pcre::regex(re, origin::user).match(str, origin::user);
  } catch (recoverable_failure & e) {
    lua_pushstring(LS, e.what());
    return lua_error(LS);
  }
  lua_pushboolean(LS, result);
  return 1;
}

LUAEXT(gettext, )
{
  const char *msgid = luaL_checkstring(LS, -1);
  lua_pushstring(LS, gettext(msgid));
  return 1;
}

bool
run_string(lua_State * st, char const * str, char const * identity)
{
  I(st);
  return
    Lua(st)
    .loadstring(str, identity)
    .call(0,1)
    .ok();
}

bool
run_file(lua_State * st, char const * filename)
{
  I(st);
  return
    Lua(st)
    .loadfile(filename)
    .call(0,1)
    .ok();
}

namespace
{
  struct ignore_directories : public dirent_consumer
  { virtual void consume(const char *) {} };
  struct record_if_matches : public dirent_consumer
  {
    record_if_matches(string const & b, char const * p,
                      vector<string> & t)
      : base(b + "/"), glob(p, origin::user), target(t)
    { target.clear(); }

    virtual void consume(const char * component)
    {
      if (glob.matches(component))
        target.push_back(base + component);
    }
  private:
    string base;
    globish glob;
    vector<string> & target;
  };
}

// ??? should maybe deal in system_paths and use read_directory.
void
run_directory(lua_State * st, char const * pathstr, char const * pattern)
{
  string path(pathstr);
  switch (get_path_status(path))
    {
    case path::nonexistent:
      E(false, origin::user, F("Directory '%s' does not exist") % pathstr);
    case path::file:
      E(false, origin::user, F("'%s' is not a directory") % pathstr);
    case path::directory:
      break;
    }

  // directory, iterate over it, skipping subdirs, taking every filename
  // matching the pattern, sorting them and loading in sorted order
  vector<string> arr;
  {
    ignore_directories id;
    record_if_matches rim(path, pattern, arr);
    read_directory(path, rim, id, id);
  }

  sort(arr.begin(), arr.end());
  for (vector<string>::iterator i= arr.begin(); i != arr.end(); ++i)
    {
      L(FL("opening rcfile '%s'") % *i);
      bool res = Lua(st)
        .loadfile(i->c_str())
        .call(0,1)
        .ok();
      E(res, origin::user, F("lua error while loading rcfile '%s'") % *i);
      L(FL("'%s' is ok") % *i);
    }
}

// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: