Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
/* * (find-angg "vtutil/pio_fontx.c") * (find-libcnode "String Length") * (find-libcnode "Copying and Concatenation") * (find-man "3 strlen") * (find-man "3 strcpy") * (find-lua51manualw3m "#lua_register") * * (find-books "__comp/__comp.el" "ierusalimschy") * (find-pil2page (+ 19 241) "26. Calling C from Lua") * (find-pil2text (+ 19 241) "Calling C from Lua") * (find-pil2page (+ 19 245) "luaL_register(L, \"mylib\", mylib)") * (find-pil2text (+ 19 245) "luaL_register(L, \"mylib\", mylib)") * (defun c () (interactive) (find-sh0 "gcc -Wall -shared -I$LUA51SRC/include -o sexpskeleton.so sexpskeleton.c")) * (find-lua51manualw3m "#pdf-package.loadlib") * (find-angg "LUA/lua50init.lua" "loadbitlib") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) so = "/home/edrx/blogme4/sexpskeleton.so" so = os.getenv("PWD").."/sexpskeleton.so" init = "luaopen_sexpskeleton" f = assert(package.loadlib(so, init))() PP(f('a\t"foo""foo" b(c () \\d\"ef\")')) */ #include <string.h> #include <stdio.h> #include "lua.h" #include "lauxlib.h" #include "lualib.h" static int l_sexpskeleton(lua_State *L) { const char *origstr = luaL_checkstring(L, 1); char s[258]; /* skeleton */ int len, i, depth; /* discard long lines */ if (strlen(origstr) > 256) return 0; /* skeleton is initially a copy of origstr */ strcpy(s, origstr); len = strlen(s); /* convert tabs to spaces */ for (i=0; i<len; ++i) if (s[i]==9) s[i] = 32; /* ignore trailing whitespace */ for (; len>0 && s[len-1]==32; --len); /* sexp hyperlinks must end with ')' */ if (len==0 || s[len-1]!=')') return 0; /* simplify backslashes */ for (i=0; i<len-1; ++i) if (s[i]=='\\') { s[i] = '_'; s[i+1] = '_'; } /* simplify strings */ for (i=len-1; i>=0; --i) if (s[i]=='\"') for (--i; i>=0 && s[i]!='\"'; --i) s[i] = '_'; // if (1) lua_pushstring(L, s); return 1; /* !!! */ /* find the beginning of the sexp */ for (i=len-2, depth=1; i>=0 && depth>0; --i) if (s[i]==')') { depth++; } else if (s[i]=='(') { depth--; } /* unbalanced parentheses? */ if (depth!=0) return 0; /* wipe out everything before the sexp */ for (; i>=0; --i) s[i] = 32; /* return the skeleton */ lua_pushstring(L, s); return 1; } int luaopen_sexpskeleton (lua_State *L) { lua_pushcfunction(L, l_sexpskeleton); return 1; }