lobject.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. ** $Id: lobject.c,v 2.113 2016/12/22 13:08:50 roberto Exp $
  3. ** Some generic functions over Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lobject_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <locale.h>
  10. #include <math.h>
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "lua.h"
  16. #include "lctype.h"
  17. #include "ldebug.h"
  18. #include "ldo.h"
  19. #include "lmem.h"
  20. #include "lobject.h"
  21. #include "lstate.h"
  22. #include "lstring.h"
  23. #include "lvm.h"
  24. LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
  25. /*
  26. ** converts an integer to a "floating point byte", represented as
  27. ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
  28. ** eeeee != 0 and (xxx) otherwise.
  29. */
  30. int luaO_int2fb(unsigned int x)
  31. {
  32. int e = 0; /* exponent */
  33. if (x < 8) return x;
  34. while (x >= (8 << 4)) /* coarse steps */
  35. {
  36. x = (x + 0xf) >> 4; /* x = ceil(x / 16) */
  37. e += 4;
  38. }
  39. while (x >= (8 << 1)) /* fine steps */
  40. {
  41. x = (x + 1) >> 1; /* x = ceil(x / 2) */
  42. e++;
  43. }
  44. return ((e + 1) << 3) | (cast_int(x) - 8);
  45. }
  46. /* converts back */
  47. int luaO_fb2int(int x)
  48. {
  49. return (x < 8) ? x : ((x & 7) + 8) << ((x >> 3) - 1);
  50. }
  51. /*
  52. ** Computes ceil(log2(x))
  53. */
  54. int luaO_ceillog2(unsigned int x)
  55. {
  56. static const lu_byte log_2[256] = /* log_2[i] = ceil(log2(i - 1)) */
  57. {
  58. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  59. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  60. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  61. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  62. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  63. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  64. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  65. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
  66. };
  67. int l = 0;
  68. x--;
  69. while (x >= 256)
  70. {
  71. l += 8;
  72. x >>= 8;
  73. }
  74. return l + log_2[x];
  75. }
  76. static lua_Integer intarith(lua_State *L, int op, lua_Integer v1,
  77. lua_Integer v2)
  78. {
  79. switch (op)
  80. {
  81. case LUA_OPADD:
  82. return intop(+, v1, v2);
  83. case LUA_OPSUB:
  84. return intop(-, v1, v2);
  85. case LUA_OPMUL:
  86. return intop( *, v1, v2);
  87. case LUA_OPMOD:
  88. return luaV_mod(L, v1, v2);
  89. case LUA_OPIDIV:
  90. return luaV_div(L, v1, v2);
  91. case LUA_OPBAND:
  92. return intop( &, v1, v2);
  93. case LUA_OPBOR:
  94. return intop( | , v1, v2);
  95. case LUA_OPBXOR:
  96. return intop( ^ , v1, v2);
  97. case LUA_OPSHL:
  98. return luaV_shiftl(v1, v2);
  99. case LUA_OPSHR:
  100. return luaV_shiftl(v1, -v2);
  101. case LUA_OPUNM:
  102. return intop(-, 0, v1);
  103. case LUA_OPBNOT:
  104. return intop( ^ , ~l_castS2U(0), v1);
  105. default:
  106. lua_assert(0);
  107. return 0;
  108. }
  109. }
  110. static lua_Number numarith(lua_State *L, int op, lua_Number v1,
  111. lua_Number v2)
  112. {
  113. switch (op)
  114. {
  115. case LUA_OPADD:
  116. return luai_numadd(L, v1, v2);
  117. case LUA_OPSUB:
  118. return luai_numsub(L, v1, v2);
  119. case LUA_OPMUL:
  120. return luai_nummul(L, v1, v2);
  121. case LUA_OPDIV:
  122. return luai_numdiv(L, v1, v2);
  123. case LUA_OPPOW:
  124. return luai_numpow(L, v1, v2);
  125. case LUA_OPIDIV:
  126. return luai_numidiv(L, v1, v2);
  127. case LUA_OPUNM:
  128. return luai_numunm(L, v1);
  129. case LUA_OPMOD:
  130. {
  131. lua_Number m;
  132. luai_nummod(L, v1, v2, m);
  133. return m;
  134. }
  135. default:
  136. lua_assert(0);
  137. return 0;
  138. }
  139. }
  140. void luaO_arith(lua_State *L, int op, const TValue *p1, const TValue *p2,
  141. TValue *res)
  142. {
  143. switch (op)
  144. {
  145. case LUA_OPBAND:
  146. case LUA_OPBOR:
  147. case LUA_OPBXOR:
  148. case LUA_OPSHL:
  149. case LUA_OPSHR:
  150. case LUA_OPBNOT: /* operate only on integers */
  151. {
  152. lua_Integer i1;
  153. lua_Integer i2;
  154. if (tointeger(p1, &i1) && tointeger(p2, &i2))
  155. {
  156. setivalue(res, intarith(L, op, i1, i2));
  157. return;
  158. }
  159. else break; /* go to the end */
  160. }
  161. case LUA_OPDIV:
  162. case LUA_OPPOW: /* operate only on floats */
  163. {
  164. lua_Number n1;
  165. lua_Number n2;
  166. if (tonumber(p1, &n1) && tonumber(p2, &n2))
  167. {
  168. setfltvalue(res, numarith(L, op, n1, n2));
  169. return;
  170. }
  171. else break; /* go to the end */
  172. }
  173. default: /* other operations */
  174. {
  175. lua_Number n1;
  176. lua_Number n2;
  177. if (ttisinteger(p1) && ttisinteger(p2))
  178. {
  179. setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
  180. return;
  181. }
  182. else if (tonumber(p1, &n1) && tonumber(p2, &n2))
  183. {
  184. setfltvalue(res, numarith(L, op, n1, n2));
  185. return;
  186. }
  187. else break; /* go to the end */
  188. }
  189. }
  190. /* could not perform raw operation; try metamethod */
  191. lua_assert(L != NULL); /* should not fail when folding (compile time) */
  192. luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD));
  193. }
  194. int luaO_hexavalue(int c)
  195. {
  196. if (lisdigit(c)) return c - '0';
  197. else return (ltolower(c) - 'a') + 10;
  198. }
  199. static int isneg(const char **s)
  200. {
  201. if (**s == '-')
  202. {
  203. (*s)++;
  204. return 1;
  205. }
  206. else if (**s == '+')(*s)++;
  207. return 0;
  208. }
  209. /*
  210. ** {==================================================================
  211. ** Lua's implementation for 'lua_strx2number'
  212. ** ===================================================================
  213. */
  214. #if !defined(lua_strx2number)
  215. /* maximum number of significant digits to read (to avoid overflows
  216. even with single floats) */
  217. #define MAXSIGDIG 30
  218. /*
  219. ** convert an hexadecimal numeric string to a number, following
  220. ** C99 specification for 'strtod'
  221. */
  222. static lua_Number lua_strx2number(const char *s, char **endptr)
  223. {
  224. int dot = lua_getlocaledecpoint();
  225. lua_Number r = 0.0; /* result (accumulator) */
  226. int sigdig = 0; /* number of significant digits */
  227. int nosigdig = 0; /* number of non-significant digits */
  228. int e = 0; /* exponent correction */
  229. int neg; /* 1 if number is negative */
  230. int hasdot = 0; /* true after seen a dot */
  231. *endptr = cast(char *, s); /* nothing is valid yet */
  232. while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
  233. neg = isneg(&s); /* check signal */
  234. if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
  235. return 0.0; /* invalid format (no '0x') */
  236. for (s += 2; ; s++) /* skip '0x' and read numeral */
  237. {
  238. if (*s == dot)
  239. {
  240. if (hasdot) break; /* second dot? stop loop */
  241. else hasdot = 1;
  242. }
  243. else if (lisxdigit(cast_uchar(*s)))
  244. {
  245. if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
  246. nosigdig++;
  247. else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
  248. r = (r * cast_num(16.0)) + luaO_hexavalue(*s);
  249. else e++; /* too many digits; ignore, but still count for exponent */
  250. if (hasdot) e--; /* decimal digit? correct exponent */
  251. }
  252. else break; /* neither a dot nor a digit */
  253. }
  254. if (nosigdig + sigdig == 0) /* no digits? */
  255. return 0.0; /* invalid format */
  256. *endptr = cast(char *, s); /* valid up to here */
  257. e *= 4; /* each digit multiplies/divides value by 2^4 */
  258. if (*s == 'p' || *s == 'P') /* exponent part? */
  259. {
  260. int exp1 = 0; /* exponent value */
  261. int neg1; /* exponent signal */
  262. s++; /* skip 'p' */
  263. neg1 = isneg(&s); /* signal */
  264. if (!lisdigit(cast_uchar(*s)))
  265. return 0.0; /* invalid; must have at least one digit */
  266. while (lisdigit(cast_uchar(*s))) /* read exponent */
  267. exp1 = exp1 * 10 + *(s++) - '0';
  268. if (neg1) exp1 = -exp1;
  269. e += exp1;
  270. *endptr = cast(char *, s); /* valid up to here */
  271. }
  272. if (neg) r = -r;
  273. return l_mathop(ldexp)(r, e);
  274. }
  275. #endif
  276. /* }====================================================== */
  277. /* maximum length of a numeral */
  278. #if !defined (L_MAXLENNUM)
  279. #define L_MAXLENNUM 200
  280. #endif
  281. static const char *l_str2dloc(const char *s, lua_Number *result, int mode)
  282. {
  283. char *endptr;
  284. *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */
  285. : lua_str2number(s, &endptr);
  286. if (endptr == s) return NULL; /* nothing recognized? */
  287. while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */
  288. return (*endptr == '\0') ? endptr : NULL; /* OK if no trailing characters */
  289. }
  290. /*
  291. ** Convert string 's' to a Lua number (put in 'result'). Return NULL
  292. ** on fail or the address of the ending '\0' on success.
  293. ** 'pmode' points to (and 'mode' contains) special things in the string:
  294. ** - 'x'/'X' means an hexadecimal numeral
  295. ** - 'n'/'N' means 'inf' or 'nan' (which should be rejected)
  296. ** - '.' just optimizes the search for the common case (nothing special)
  297. ** This function accepts both the current locale or a dot as the radix
  298. ** mark. If the convertion fails, it may mean number has a dot but
  299. ** locale accepts something else. In that case, the code copies 's'
  300. ** to a buffer (because 's' is read-only), changes the dot to the
  301. ** current locale radix mark, and tries to convert again.
  302. */
  303. static const char *l_str2d(const char *s, lua_Number *result)
  304. {
  305. const char *endptr;
  306. const char *pmode = strpbrk(s, ".xXnN");
  307. int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
  308. if (mode == 'n') /* reject 'inf' and 'nan' */
  309. return NULL;
  310. endptr = l_str2dloc(s, result, mode); /* try to convert */
  311. if (endptr == NULL) /* failed? may be a different locale */
  312. {
  313. char buff[L_MAXLENNUM + 1];
  314. const char *pdot = strchr(s, '.');
  315. if (strlen(s) > L_MAXLENNUM || pdot == NULL)
  316. return NULL; /* string too long or no dot; fail */
  317. strcpy(buff, s); /* copy string to buffer */
  318. buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */
  319. endptr = l_str2dloc(buff, result, mode); /* try again */
  320. if (endptr != NULL)
  321. endptr = s + (endptr - buff); /* make relative to 's' */
  322. }
  323. return endptr;
  324. }
  325. #define MAXBY10 cast(lua_Unsigned, LUA_MAXINTEGER / 10)
  326. #define MAXLASTD cast_int(LUA_MAXINTEGER % 10)
  327. static const char *l_str2int(const char *s, lua_Integer *result)
  328. {
  329. lua_Unsigned a = 0;
  330. int empty = 1;
  331. int neg;
  332. while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
  333. neg = isneg(&s);
  334. if (s[0] == '0' &&
  335. (s[1] == 'x' || s[1] == 'X')) /* hex? */
  336. {
  337. s += 2; /* skip '0x' */
  338. for (; lisxdigit(cast_uchar(*s)); s++)
  339. {
  340. a = a * 16 + luaO_hexavalue(*s);
  341. empty = 0;
  342. }
  343. }
  344. else /* decimal */
  345. {
  346. for (; lisdigit(cast_uchar(*s)); s++)
  347. {
  348. int d = *s - '0';
  349. if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
  350. return NULL; /* do not accept it (as integer) */
  351. a = a * 10 + d;
  352. empty = 0;
  353. }
  354. }
  355. while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */
  356. if (empty || *s != '\0') return NULL; /* something wrong in the numeral */
  357. else
  358. {
  359. *result = l_castU2S((neg) ? 0u - a : a);
  360. return s;
  361. }
  362. }
  363. size_t luaO_str2num(const char *s, TValue *o)
  364. {
  365. lua_Integer i;
  366. lua_Number n;
  367. const char *e;
  368. if ((e = l_str2int(s, &i)) != NULL) /* try as an integer */
  369. {
  370. setivalue(o, i);
  371. }
  372. else if ((e = l_str2d(s, &n)) != NULL) /* else try as a float */
  373. {
  374. setfltvalue(o, n);
  375. }
  376. else
  377. return 0; /* conversion failed */
  378. return (e - s) + 1; /* success; return string size */
  379. }
  380. int luaO_utf8esc(char *buff, unsigned long x)
  381. {
  382. int n = 1; /* number of bytes put in buffer (backwards) */
  383. lua_assert(x <= 0x10FFFF);
  384. if (x < 0x80) /* ascii? */
  385. buff[UTF8BUFFSZ - 1] = cast(char, x);
  386. else /* need continuation bytes */
  387. {
  388. unsigned int mfb = 0x3f; /* maximum that fits in first byte */
  389. do /* add continuation bytes */
  390. {
  391. buff[UTF8BUFFSZ - (n++)] = cast(char, 0x80 | (x & 0x3f));
  392. x >>= 6; /* remove added bits */
  393. mfb >>= 1; /* now there is one less bit available in first byte */
  394. }
  395. while (x > mfb); /* still needs continuation byte? */
  396. buff[UTF8BUFFSZ - n] = cast(char, (~mfb << 1) | x); /* add first byte */
  397. }
  398. return n;
  399. }
  400. /* maximum length of the conversion of a number to a string */
  401. #define MAXNUMBER2STR 50
  402. /*
  403. ** Convert a number object to a string
  404. */
  405. void luaO_tostring(lua_State *L, StkId obj)
  406. {
  407. char buff[MAXNUMBER2STR];
  408. size_t len;
  409. lua_assert(ttisnumber(obj));
  410. if (ttisinteger(obj))
  411. len = lua_integer2str(buff, sizeof(buff), ivalue(obj));
  412. else
  413. {
  414. len = lua_number2str(buff, sizeof(buff), fltvalue(obj));
  415. #if !defined(LUA_COMPAT_FLOATSTRING)
  416. if (buff[strspn(buff, "-0123456789")] == '\0') /* looks like an int? */
  417. {
  418. buff[len++] = lua_getlocaledecpoint();
  419. buff[len++] = '0'; /* adds '.0' to result */
  420. }
  421. #endif
  422. }
  423. setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
  424. }
  425. static void pushstr(lua_State *L, const char *str, size_t l)
  426. {
  427. setsvalue2s(L, L->top, luaS_newlstr(L, str, l));
  428. luaD_inctop(L);
  429. }
  430. /*
  431. ** this function handles only '%d', '%c', '%f', '%p', and '%s'
  432. conventional formats, plus Lua-specific '%I' and '%U'
  433. */
  434. const char *luaO_pushvfstring(lua_State *L, const char *fmt, va_list argp)
  435. {
  436. int n = 0;
  437. for (;;)
  438. {
  439. const char *e = strchr(fmt, '%');
  440. if (e == NULL) break;
  441. pushstr(L, fmt, e - fmt);
  442. switch (*(e + 1))
  443. {
  444. case 's': /* zero-terminated string */
  445. {
  446. const char *s = va_arg(argp, char *);
  447. if (s == NULL) s = "(null)";
  448. pushstr(L, s, strlen(s));
  449. break;
  450. }
  451. case 'c': /* an 'int' as a character */
  452. {
  453. char buff = cast(char, va_arg(argp, int));
  454. if (lisprint(cast_uchar(buff)))
  455. pushstr(L, &buff, 1);
  456. else /* non-printable character; print its code */
  457. luaO_pushfstring(L, "<\\%d>", cast_uchar(buff));
  458. break;
  459. }
  460. case 'd': /* an 'int' */
  461. {
  462. setivalue(L->top, va_arg(argp, int));
  463. goto top2str;
  464. }
  465. case 'I': /* a 'lua_Integer' */
  466. {
  467. setivalue(L->top, cast(lua_Integer, va_arg(argp, l_uacInt)));
  468. goto top2str;
  469. }
  470. case 'f': /* a 'lua_Number' */
  471. {
  472. setfltvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
  473. top2str: /* convert the top element to a string */
  474. luaD_inctop(L);
  475. luaO_tostring(L, L->top - 1);
  476. break;
  477. }
  478. case 'p': /* a pointer */
  479. {
  480. char buff[4 * sizeof(void *) + 8]; /* should be enough space for a '%p' */
  481. int l = l_sprintf(buff, sizeof(buff), "%p", va_arg(argp, void *));
  482. pushstr(L, buff, l);
  483. break;
  484. }
  485. case 'U': /* an 'int' as a UTF-8 sequence */
  486. {
  487. char buff[UTF8BUFFSZ];
  488. int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long)));
  489. pushstr(L, buff + UTF8BUFFSZ - l, l);
  490. break;
  491. }
  492. case '%':
  493. {
  494. pushstr(L, "%", 1);
  495. break;
  496. }
  497. default:
  498. {
  499. luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'",
  500. *(e + 1));
  501. }
  502. }
  503. n += 2;
  504. fmt = e + 2;
  505. }
  506. luaD_checkstack(L, 1);
  507. pushstr(L, fmt, strlen(fmt));
  508. if (n > 0) luaV_concat(L, n + 1);
  509. return svalue(L->top - 1);
  510. }
  511. const char *luaO_pushfstring(lua_State *L, const char *fmt, ...)
  512. {
  513. const char *msg;
  514. va_list argp;
  515. va_start(argp, fmt);
  516. msg = luaO_pushvfstring(L, fmt, argp);
  517. va_end(argp);
  518. return msg;
  519. }
  520. /* number of chars of a literal string without the ending \0 */
  521. #define LL(x) (sizeof(x)/sizeof(char) - 1)
  522. #define RETS "..."
  523. #define PRE "[string \""
  524. #define POS "\"]"
  525. #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
  526. void luaO_chunkid(char *out, const char *source, size_t bufflen)
  527. {
  528. size_t l = strlen(source);
  529. if (*source == '=') /* 'literal' source */
  530. {
  531. if (l <= bufflen) /* small enough? */
  532. memcpy(out, source + 1, l * sizeof(char));
  533. else /* truncate it */
  534. {
  535. addstr(out, source + 1, bufflen - 1);
  536. *out = '\0';
  537. }
  538. }
  539. else if (*source == '@') /* file name */
  540. {
  541. if (l <= bufflen) /* small enough? */
  542. memcpy(out, source + 1, l * sizeof(char));
  543. else /* add '...' before rest of name */
  544. {
  545. addstr(out, RETS, LL(RETS));
  546. bufflen -= LL(RETS);
  547. memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
  548. }
  549. }
  550. else /* string; format as [string "source"] */
  551. {
  552. const char *nl = strchr(source, '\n'); /* find first new line (if any) */
  553. addstr(out, PRE, LL(PRE)); /* add prefix */
  554. bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
  555. if (l < bufflen && nl == NULL) /* small one-line source? */
  556. {
  557. addstr(out, source, l); /* keep it */
  558. }
  559. else
  560. {
  561. if (nl != NULL) l = nl - source; /* stop at first newline */
  562. if (l > bufflen) l = bufflen;
  563. addstr(out, source, l);
  564. addstr(out, RETS, LL(RETS));
  565. }
  566. memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
  567. }
  568. }