Imported from ../lua-5.0.2.tar.gz.
[lua.git] / etc / saconfig.c
blobbf3c64b701c0640c083231103dea823a28694e03
1 /* sa-config.c -- configuration for stand-alone Lua interpreter
3 * #define LUA_USERCONFIG to this file
5 * Here are the features that can be customized using #define:
7 *** Line edit and history:
8 * #define USE_READLINE to use the GNU readline library.
10 * To use another library for this, use the code below as a start.
11 * Make sure you #define lua_readline and lua_saveline accordingly.
12 * If you do not #define lua_readline, you'll get a version based on fgets
13 * that uses a static buffer of size MAXINPUT.
16 *** Static Lua libraries to be loaded at startup:
17 * #define lua_userinit(L) to a Lua function that loads libraries; typically
18 * #define lua_userinit(L) openstdlibs(L);myinit(L)
19 * or
20 * #define lua_userinit(L) myinit(L)
22 * Another way is to add the prototypes of the init functions here and
23 * #define LUA_EXTRALIBS accordingly. For example,
24 * #define LUA_EXTRALIBS {"mylib","luaopen_mylib"},
25 * Note the ending comma!
28 *** Prompts:
29 * The stand-alone Lua interpreter uses two prompts: PROMPT and PROMPT2.
30 * PROMPT is the primary prompt, shown when the intepreter is ready to receive
31 * a new statement. PROMPT2 is the secondary prompt, shown while a statement
32 * is being entered but is still incomplete.
35 *** Program name:
36 * Error messages usually show argv[0] as a program name. In systems that do
37 * not give a valid string as argv[0], error messages show PROGNAME instead.
42 #ifdef USE_READLINE
44 * This section implements of lua_readline and lua_saveline for lua.c using
45 * the GNU readline and history libraries. It should also work with drop-in
46 * replacements such as editline and libedit (you may have to include
47 * different headers, though).
51 #define lua_readline myreadline
52 #define lua_saveline mysaveline
54 #include <ctype.h>
55 #include <readline/readline.h>
56 #include <readline/history.h>
58 static int myreadline (lua_State *L, const char *prompt) {
59 char *s=readline(prompt);
60 if (s==NULL)
61 return 0;
62 else {
63 lua_pushstring(L,s);
64 lua_pushliteral(L,"\n");
65 lua_concat(L,2);
66 free(s);
67 return 1;
71 static void mysaveline (lua_State *L, const char *s) {
72 const char *p;
73 for (p=s; isspace(*p); p++)
75 if (*p!=0) {
76 size_t n=strlen(s)-1;
77 if (s[n]!='\n')
78 add_history(s);
79 else {
80 lua_pushlstring(L,s,n);
81 s=lua_tostring(L,-1);
82 add_history(s);
83 lua_remove(L,-1);
87 #endif