better treatment for source names

This commit is contained in:
Roberto Ierusalimschy
2000-10-09 11:47:32 -02:00
parent ae63a0e692
commit d6232a0b2e
4 changed files with 34 additions and 30 deletions

View File

@@ -1,5 +1,5 @@
/*
** $Id: ldebug.c,v 1.45 2000/10/05 13:00:17 roberto Exp roberto $
** $Id: ldebug.c,v 1.46 2000/10/06 12:45:25 roberto Exp roberto $
** Debug Interface
** See Copyright Notice in lua.h
*/
@@ -193,7 +193,7 @@ static void lua_funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
lua_error(L, "value for `lua_getinfo' is not a function");
}
if (cl->isC) {
ar->source = "(C)";
ar->source = "=C";
ar->linedefined = -1;
ar->what = "C";
}

12
ldo.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: ldo.c,v 1.103 2000/10/05 13:00:17 roberto Exp roberto $
** $Id: ldo.c,v 1.104 2000/10/06 12:45:25 roberto Exp roberto $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
@@ -260,14 +260,16 @@ static int protectedparser (lua_State *L, ZIO *z, int bin) {
static int parse_file (lua_State *L, const char *filename) {
ZIO z;
char source[MAXFILENAME];
int status;
int bin; /* flag for file mode */
int c; /* look ahead char */
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
if (f == NULL) return LUA_ERRFILE; /* unable to open file */
if (filename == NULL) filename = "(stdin)";
sprintf(source, "@%.*s", (int)sizeof(source)-2, filename);
lua_pushstring(L, "@");
lua_pushstring(L, (filename == NULL) ? "(stdin)" : filename);
lua_concat(L, 2);
filename = lua_tostring(L, -1); /* filename = '@'..filename */
lua_pop(L, 1); /* OK: there is no GC during parser */
c = fgetc(f);
ungetc(c, f);
bin = (c == ID_CHUNK);
@@ -275,7 +277,7 @@ static int parse_file (lua_State *L, const char *filename) {
f = freopen(filename, "rb", f); /* set binary mode */
if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
}
luaZ_Fopen(&z, f, source);
luaZ_Fopen(&z, f, filename);
status = protectedparser(L, &z, bin);
if (f != stdin)
fclose(f);

View File

@@ -1,5 +1,5 @@
/*
** $Id: llimits.h,v 1.16 2000/10/03 14:03:21 roberto Exp roberto $
** $Id: llimits.h,v 1.17 2000/10/06 19:28:38 roberto Exp roberto $
** Limits, basic types, and some other "installation-dependent" definitions
** See Copyright Notice in lua.h
*/
@@ -104,7 +104,7 @@ typedef unsigned long Instruction;
/*
** limits for opcode arguments.
** we use (signed) int to manipulate most arguments,
** so they must fit in BITS_INT-1 bits (-1 for signal)
** so they must fit in BITS_INT-1 bits (-1 for sign)
*/
#if SIZE_U < BITS_INT-1
#define MAXARG_U ((1<<SIZE_U)-1)
@@ -196,10 +196,4 @@ typedef unsigned long Instruction;
#endif
/* maximum part of a file name kept for error messages */
#ifndef MAXFILENAME
#define MAXFILENAME 260
#endif
#endif

View File

@@ -1,5 +1,5 @@
/*
** $Id: lobject.c,v 1.51 2000/10/03 14:03:21 roberto Exp roberto $
** $Id: lobject.c,v 1.52 2000/10/05 12:14:08 roberto Exp roberto $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/
@@ -87,23 +87,31 @@ void luaO_verror (lua_State *L, const char *fmt, ...) {
}
#define EXTRALEN sizeof("string \"...\"0")
#define EXTRALEN sizeof(" string \"s...\" ")
void luaO_chunkid (char *out, const char *source, int len) {
if (*source == '(') {
strncpy(out, source+1, len-1); /* remove first char */
out[len-1] = '\0'; /* make sure `out' has an end */
out[strlen(out)-1] = '\0'; /* remove last char */
}
void luaO_chunkid (char *out, const char *source, int bufflen) {
if (*source == '=')
sprintf(out, "%.*s", bufflen, source+1); /* remove first char */
else {
len -= EXTRALEN;
if (*source == '@')
sprintf(out, "file `%.*s'", len, source+1);
bufflen -= EXTRALEN;
if (*source == '@') {
int l;
source++; /* skip the `@' */
l = strlen(source);
if (l>bufflen) {
source += (l-bufflen); /* get last part of file name */
sprintf(out, "file `...%s'", source);
}
else
sprintf(out, "file `%s'", source);
}
else {
const char *b = strchr(source , '\n'); /* stop at first new line */
int lim = (b && (b-source)<len) ? b-source : len;
sprintf(out, "string \"%.*s\"", lim, source);
strcpy(out+lim+(EXTRALEN-sizeof("...\"0")), "...\"");
int len = strcspn(source, "\n"); /* stop at first newline */
if (len > bufflen) len = bufflen;
if (source[len] != '\0') /* must truncate? */
sprintf(out, "string \"%.*s...\"", len, source);
else
sprintf(out, "string \"%s\"", source);
}
}
}