private static int skipBOM(LoadF lf) { CharPtr p = "\xEF\xBB\xBF"; /* Utf8 BOM mark */ int c; lf.n = 0; do { c = getc(lf.f); //if (c == EOF || c != *(unsigned char *)p++) return c; //FIXME:changed if (c == EOF) { return(c); } else if (c != (byte)p[0]) { p = p + 1; return(c); } p = p + 1; lf.buff[lf.n++] = (char)c; /* to be read by the parser */ //FIXME:added, (char) } while (p[0] != '\0'); lf.n = 0; /* prefix matched; discard it */ return(getc(lf.f)); /* return next character */ }
public static CharPtr getF(lua_State L, object ud, out uint size) { size = 0; //FIXME:added LoadF lf = (LoadF)ud; //(void)L; if (lf.n > 0) /* are there pre-read characters to be read? */ { size = (uint)lf.n; /* return them (chars already in buffer) */ //FIXME:(uint) lf.n = 0; /* no more pre-read characters */ } else /* read a block from file */ /* 'fread' can return > 0 *and* set the EOF flag. If next call to * 'getF' called 'fread', it might still wait for user input. * The next check avoids this problem. */ { if (feof(lf.f) != 0) { return(null); } size = (uint)fread(lf.buff, 1, lf.buff.chars.Length, lf.f); /* read block */ } return((size > 0) ? new CharPtr(lf.buff) : null); //FIXME:changed }
// KW: luaL_loadfile() modded for DAI public static int luaL_loadfile_DAI(lua_State L, CharPtr filename, string fileStr) { int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ LoadF lf = new LoadF(); lf.extraline = 0; if (filename == null) { luaL_argerror(L, 1, "file name must be supplied"); } else { lua_pushstring(L, new CharPtr(fileStr)); lf.f = fopen(filename, "r"); if (lf.f == null) { return(errfile(L, "open", fnameindex)); } } int c = getc(lf.f); if (c == '#') { /* Unix exec. file? */ lf.extraline = 1; while ((c = getc(lf.f)) != EOF && c != '\n') { ; /* skip first line */ } if (c == '\n') { c = getc(lf.f); } } if (c == LUA_SIGNATURE[0] && (filename != null)) { luaL_error(L, "binary files not supported"); } ungetc(c, lf.f); int status = lua_load(L, getF, lf, lua_tostring(L, -1)); int readstatus = ferror(lf.f); if (filename != null) { fclose(lf.f); /* close file (even in case of errors) */ } if (readstatus != 0) { lua_settop(L, fnameindex); /* ignore results from `lua_load' */ return(errfile(L, "read", fnameindex)); } lua_remove(L, fnameindex); return(status); }
public static int luaL_loadfile(lua_State L, CharPtr filename) { LoadF lf = new LoadF(); int status, readstatus; int c = 0; //FIXME: added, =0 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ if (filename == null) { lua_pushliteral(L, "=stdin"); lf.f = stdin; } else { lua_pushfstring(L, "@%s", filename); lf.f = fopen(filename, "r"); if (lf.f == null) { return(errfile(L, "open", fnameindex)); } } if (skipcomment(lf, ref c) != 0) /* read initial portion */ { lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ } if (c == (int)LUA_SIGNATURE[0] && filename != null) /* binary file? */ //FIXME:added, (int) { lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ if (lf.f == null) { return(errfile(L, "reopen", fnameindex)); } skipcomment(lf, ref c); /* re-read initial portion */ } if (c != EOF) { lf.buff[lf.n++] = (char)c; } /* 'c' is the first character of the stream */ //FIXME:added, (char) status = lua_load(L, getF, lf, lua_tostring(L, -1)); readstatus = ferror(lf.f); if (filename != null) { fclose(lf.f); /* close file (even in case of errors) */ } if (readstatus != 0) { lua_settop(L, fnameindex); /* ignore results from `lua_load' */ return(errfile(L, "read", fnameindex)); } lua_remove(L, fnameindex); return(status); }
/* ** reads the first character of file 'f' and skips an optional BOM mark ** in its beginning plus its first line if it starts with '#'. Returns ** true if it skipped the first line. In any case, '*cp' has the ** first "valid" character of the file (after the optional BOM and ** a first-line comment). */ private static int skipcomment(LoadF lf, ref int cp) { int c = (cp = skipBOM(lf)); if (c == '#') /* first line is a comment (Unix exec. file)? */ { while ((c = getc(lf.f)) != EOF && c != '\n') { ; /* skip first line */ } cp = getc(lf.f); /* skip end-of-line */ return(1); /* there was a comment */ } else { return(0); /* no comment */ } }
public static CharPtr getF(lua_State L, object ud, out uint size) { size = 0; LoadF lf = (LoadF)ud; //(void)L; if (lf.extraline != 0) { lf.extraline = 0; size = 1; return("\n"); } if (feof(lf.f) != 0) { return(null); } size = (uint)fread(lf.buff, 1, lf.buff.chars.Length, lf.f); return((size > 0) ? new CharPtr(lf.buff) : null); }
/* ** reads the first character of file 'f' and skips an optional BOM mark ** in its beginning plus its first line if it starts with '#'. Returns ** true if it skipped the first line. In any case, '*cp' has the ** first "valid" character of the file (after the optional BOM and ** a first-line comment). */ public static bool skipcomment(LoadF lf, ref int cp) { int c = skipBOM(lf); cp = c; if (c == '#') /* first line is a comment (Unix exec. file)? */ { do /* skip first line */ { c = laux.getc(lf.f); } while (c != EOF && c != '\n'); cp = laux.getc(lf.f); /* skip end-of-line, if present */ return(true); /* there was a comment */ } else { return(false); /* no comment */ } }
public static int skipBOM(LoadF lf) { byte c = 0; lf.n = 0; int i = 0; do { c = laux.getc(lf.f); if (c == EOF || c != laux.bom[i++]) { return(c); } lf.buff[lf.n++] = c; /* to be read by the parser */ } while (i < laux.bom.Length); lf.n = 0; /* prefix matched; discard it */ return(laux.getc(lf.f)); /* return next character */ }
public static CharPtr getF(lua_State L, object ud, out uint size) { size = 0; LoadF lf = (LoadF)ud; //(void)L; if (lf.extraline != 0) { lf.extraline = 0; size = 1; return("\n"); } /* 'fread' can return > 0 *and* set the EOF flag. If next call to * 'getF' calls 'fread', terminal may still wait for user input. * The next check avoids this problem. */ if (feof(lf.f) != 0) { return(null); } size = (uint)fread(lf.buff, 1, lf.buff.chars.Length, lf.f); return((size > 0) ? new CharPtr(lf.buff) : null); }
public static byte[] getF(lua_State L, object ud, ref int size) { LoadF lf = (LoadF)ud; if (lf.n > 0) /* are there pre-read characters to be read? */ { size = lf.n; /* return them (chars already in buffer) */ lf.n = 0; /* no more pre-read characters */ } else /* read a block from file */ /* 'fread' can return > 0 *and* set the EOF flag. If next call to * 'getF' called 'fread', it might still wait for user input. * The next check avoids this problem. */ { if (laux.feof(lf.f)) { return(null); } size = laux.fread(lf.buff, 1, lf.f); /* read block */ } return(lf.buff); }
public static int luaL_loadfile(lua_State L, CharPtr filename) { LoadF lf = new LoadF(); int status, readstatus; int c; int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ lf.extraline = 0; if (filename == null) { lua_pushliteral(L, "=stdin"); lf.f = stdin; } else { lua_pushfstring(L, "@%s", filename); lf.f = fopen(filename, "r"); if (lf.f == null) { return(errfile(L, "open", fnameindex)); } } c = getc(lf.f); if (c == '#') /* Unix exec. file? */ { lf.extraline = 1; while ((c = getc(lf.f)) != EOF && c != '\n') { ; /* skip first line */ } if (c == '\n') { c = getc(lf.f); } } if (c == LUA_SIGNATURE[0] && (filename != null)) /* binary file? */ { lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ if (lf.f == null) { return(errfile(L, "reopen", fnameindex)); } /* skip eventual `#!...' */ while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) { ; } lf.extraline = 0; } ungetc(c, lf.f); status = lua_load(L, getF, lf, lua_tostring(L, -1)); readstatus = ferror(lf.f); if (filename != null) { fclose(lf.f); /* close file (even in case of errors) */ } if (readstatus != 0) { lua_settop(L, fnameindex); /* ignore results from `lua_load' */ return(errfile(L, "read", fnameindex)); } lua_remove(L, fnameindex); return(status); }
public static int LinyeeLLoadFile(LinyeeState L, CharPtr filename) { LoadF lf = new LoadF(); int status, readstatus; int c; int fnameindex = LinyeeGetTop(L) + 1; /* index of filename on the stack */ lf.extraline = 0; if (filename == null) { LinyeePushLiteral(L, "=stdin"); lf.f = stdin; } else { LinyeePushFString(L, "@%s", filename); lf.f = fopen(filename, "r"); if (lf.f == null) { return(ErrFile(L, "open", fnameindex)); } } c = getc(lf.f); if (c == '#') /* Unix exec. file? */ { lf.extraline = 1; while ((c = getc(lf.f)) != EOF && c != '\n') { ; /* skip first line */ } if (c == '\n') { c = getc(lf.f); } } if (c == LINYEE_SIGNATURE[0] && (filename != null)) /* binary file? */ { lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ if (lf.f == null) { return(ErrFile(L, "reopen", fnameindex)); } /* skip eventual `#!...' */ while ((c = getc(lf.f)) != EOF && c != LINYEE_SIGNATURE[0]) { ; } lf.extraline = 0; } if (c == UTF8_SIGNATURE[0] && (filename != null)) { int c1 = getc(lf.f); if (c1 != UTF8_SIGNATURE[1]) { ungetc(c1, lf.f); } else { int c2 = getc(lf.f); if (c2 != UTF8_SIGNATURE[2]) { ungetc(c2, lf.f); ungetc(c1, lf.f); } else { c = getc(lf.f); } } } ungetc(c, lf.f); status = LinyeeLoad(L, GetF, lf, LinyeeToString(L, -1)); readstatus = ferror(lf.f); if (filename != null) { fclose(lf.f); /* close file (even in case of errors) */ } if (readstatus != 0) { LinyeeSetTop(L, fnameindex); /* ignore results from `ly_load' */ return(ErrFile(L, "read", fnameindex)); } LinyeeRemove(L, fnameindex); return(status); }
public static int luaL_loadfile(LuaState L, CharPtr filename) { LoadF lf = new LoadF(); int status, readstatus; int c; int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ lf.extraline = 0; if (filename == null) { lua_pushliteral(L, "=stdin"); lf.f = stdin; } else { lua_pushfstring(L, "@%s", filename); lf.f = fopen(filename, "r"); if (lf.f == null) return errfile(L, "open", fnameindex); } c = getc(lf.f); if (c == '#') { /* Unix exec. file? */ lf.extraline = 1; while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ if (c == '\n') c = getc(lf.f); } if (c == LUA_SIGNATURE[0] && (filename != null)) { /* binary file? */ lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ if (lf.f == null) return errfile(L, "reopen", fnameindex); /* skip eventual `#!...' */ while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; // do nothing here lf.extraline = 0; } ungetc(c, lf.f); status = lua_load(L, getF, lf, lua_tostring(L, -1)); readstatus = ferror(lf.f); if (filename != null) fclose(lf.f); /* close file (even in case of errors) */ if (readstatus != 0) { lua_settop(L, fnameindex); /* ignore results from `lua_load' */ return errfile(L, "read", fnameindex); } lua_remove(L, fnameindex); return status; }
public static int lua_load(LuaState L, lua_Reader reader, object data, CharPtr chunkname) { ZIO z = new ZIO(); int status; lua_lock(L); if (chunkname == null) { chunkname = "?"; } #if OVERRIDE_LOAD || true //#if false if (data is LoadS) { LoadS d = data as LoadS; if (d.size > 0 && d.s.chars[0] != LUA_SIGNATURE[0]) // if its not binary { Lexer l = new Lexer(); try { //Console.WriteLine(d.s); TokenReader tr = l.Lex(d.s); Parser p = new Parser(tr); Ast.Chunk c = p.Parse(); Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput(); string s = lco.Format(c); d.s = s; d.size = (lu_mem)s.Length; } catch (LuaSourceException ex) { throw ex; } } else { d.s.index = 0; // Why isn't the size equal to the chars.Length? Debug.WriteLine("Binary data: d.size=" + d.size + " d.s.chars.Length=" + d.s.chars.Length); Debug.WriteLine("Equal: " + (d.size == d.s.chars.Length)); //Debug.Assert(d.size == d.s.chars.Length); d.size = (uint)d.s.chars.Length; } } else if (data is LoadF) { LoadF lf = data as LoadF; if (lf.f.ReadByte() != LUA_SIGNATURE[0]) // if its not binary { lf.f.Position = 0; MemoryStream ms = new MemoryStream(); while (lf.f.Position < lf.f.Length) { ms.WriteByte((byte)lf.f.ReadByte()); } ms.Position = 0; // not binary file ms.Position = 0; StringBuilder sb = new StringBuilder(); while (ms.Position < ms.Length) { sb.Append((char)ms.ReadByte()); } try { Lexer l = new Lexer(); TokenReader tr = l.Lex(sb.ToString()); Parser p = new Parser(tr); Ast.Chunk c = p.Parse(); Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput(); string s = lco.Format(c); ms = new MemoryStream(); // TODO: there HAS to be a better way... foreach (char c2 in s) { ms.WriteByte((byte)c2); } ms.Position = 0; lf.f = ms; } catch (LuaSourceException ex) { lua_pushstring(L, ex.GenerateMessage(chunkname)); return(1); //throw ex; } } else { lf.f.Position = 0; // reset the read character } } #endif luaZ_init(L, z, reader, data); status = luaD_protectedparser(L, z, chunkname); lua_unlock(L); if (data is LoadF) { LoadF f = data as LoadF; if (f.f != null) { f.f.Close(); f.f.Dispose(); } } return(status); }
public static int lua_load(LuaState L, lua_Reader reader, object data, CharPtr chunkname) { ZIO z = new ZIO(); int status; lua_lock(L); if (chunkname == null) { chunkname = "?"; } if (data is LoadS) { LoadS d = data as LoadS; if (d.size > 0) { Lexer l = new Lexer(); try { //Console.WriteLine(d.s); TokenReader tr = l.Lex(d.s); Parser p = new Parser(tr); Ast.Chunk c = p.Parse(); Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput(); string s = lco.Format(c); d.s = s; d.size = (lu_mem)s.Length; } catch (LuaSourceException ex) { throw ex; } } } else if (data is LoadF) { LoadF lf = data as LoadF; MemoryStream ms = new MemoryStream(); while (lf.f.Position < lf.f.Length) { ms.WriteByte((byte)lf.f.ReadByte()); } ms.Position = 0; if (ms.ReadByte() != 27) { // not binary file ms.Position = 0; StringBuilder sb = new StringBuilder(); while (ms.Position < ms.Length) { sb.Append((char)ms.ReadByte()); } try { Lexer l = new Lexer(); TokenReader tr = l.Lex(sb.ToString()); Parser p = new Parser(tr); Ast.Chunk c = p.Parse(); Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput(); string s = lco.Format(c); ms = new MemoryStream(); // TODO: there HAS to be a better way... foreach (char c2 in s) { ms.WriteByte((byte)c2); } ms.Position = 0; lf.f = ms; } catch (LuaSourceException ex) { throw ex; } } } luaZ_init(L, z, reader, data); status = luaD_protectedparser(L, z, chunkname); lua_unlock(L); return(status); }