summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/lobject.c')
-rw-r--r--apps/plugins/lua/lobject.c66
1 files changed, 39 insertions, 27 deletions
diff --git a/apps/plugins/lua/lobject.c b/apps/plugins/lua/lobject.c
index 62ad8e9359..a351ff41da 100644
--- a/apps/plugins/lua/lobject.c
+++ b/apps/plugins/lua/lobject.c
@@ -178,37 +178,49 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
178 return msg; 178 return msg;
179} 179}
180 180
181/* lua 5.2 lobject.c,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $ */
182/* number of chars of a literal string without the ending \0 */
183#define LL(x) (sizeof(x)/sizeof(char) - 1)
184
185#define RETS "..."
186#define PRE "[string \""
187#define POS "\"]"
188
189#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
181 190
182void luaO_chunkid (char *out, const char *source, size_t bufflen) { 191void luaO_chunkid (char *out, const char *source, size_t bufflen) {
183 if (*source == '=') { 192 size_t l = strlen(source);
184 strncpy(out, source+1, bufflen); /* remove first char */ 193 if (*source == '=') { /* 'literal' source */
185 out[bufflen-1] = '\0'; /* ensures null termination */ 194 if (l <= bufflen) /* small enough? */
195 memcpy(out, source + 1, l * sizeof(char));
196 else { /* truncate it */
197 addstr(out, source + 1, bufflen - 1);
198 *out = '\0';
199 }
186 } 200 }
187 else { /* out = "source", or "...source" */ 201 else if (*source == '@') { /* file name */
188 if (*source == '@') { 202 if (l <= bufflen) /* small enough? */
189 size_t l; 203 memcpy(out, source + 1, l * sizeof(char));
190 source++; /* skip the `@' */ 204 else { /* add '...' before rest of name */
191 bufflen -= sizeof(" '...' "); 205 addstr(out, RETS, LL(RETS));
192 l = strlen(source); 206 bufflen -= LL(RETS);
193 strcpy(out, ""); 207 memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
194 if (l > bufflen) {
195 source += (l-bufflen); /* get last part of file name */
196 strcat(out, "...");
197 }
198 strcat(out, source);
199 } 208 }
200 else { /* out = [string "string"] */ 209 }
201 size_t len = strcspn(source, "\n\r"); /* stop at first newline */ 210 else { /* string; format as [string "source"] */
202 bufflen -= sizeof(" [string \"...\"] "); 211 const char *nl = strchr(source, '\n'); /* find first new line (if any) */
203 if (len > bufflen) len = bufflen; 212 addstr(out, PRE, LL(PRE)); /* add prefix */
204 strcpy(out, "[string \""); 213 bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
205 if (source[len] != '\0') { /* must truncate? */ 214 if (l < bufflen && nl == NULL) { /* small one-line source? */
206 strncat(out, source, len); 215 addstr(out, source, l); /* keep it */
207 strcat(out, "...");
208 }
209 else
210 strcat(out, source);
211 strcat(out, "\"]");
212 } 216 }
217 else {
218 if (nl != NULL) l = nl - source; /* stop at first newline */
219 if (l > bufflen) l = bufflen;
220 addstr(out, source, l);
221 addstr(out, RETS, LL(RETS));
222 }
223 memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
213 } 224 }
214} 225}
226