Skip to content

Instantly share code, notes, and snippets.

@lf94
Created May 14, 2015 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lf94/dfe07854a4c165f12923 to your computer and use it in GitHub Desktop.
Save lf94/dfe07854a4c165f12923 to your computer and use it in GitHub Desktop.
Bill Joy's Ex/Vi's buffer data management
/*
* Append after line a lines returned by function f.
* Be careful about intermediate states to avoid scramble
* if an interrupt comes in.
*/
int
append(int (*f)(void), line *a)
{
register line *a1, *a2, *rdot;
int nline;
nline = 0;
dot = a;
if(FIXUNDO && !inopen && f!=getsub) {
undap1 = undap2 = dot + 1;
undkind = UNDCHANGE;
}
while ((*f)() == 0) {
if (truedol >= endcore) { // If we've reached past the end of the buffer...
if (morelines() < 0) { // ...Give us more lines.
if (FIXUNDO && f == getsub) {
undap1 = addr1;
undap2 = addr2 + 1;
}
error(catgets(catd, 1, 39,
"Out of memory@- too many lines in file"));
}
}
nline++;
a1 = truedol + 1;
a2 = a1 + 1;
dot++;
undap2++;
dol++;
unddol++;
truedol++;
for (rdot = dot; a1 > rdot;)
*--a2 = *--a1;
*rdot = 0;
putmark(rdot);
if (f == gettty) {
dirtcnt++;
TSYNC();
}
}
return (nline);
}
int
morelines(void)
{
#ifdef _SC_PAGESIZE
static long pg;
if (pg == 0) {
pg = sysconf(_SC_PAGESIZE);
if (pg <= 0 || pg >= 65536)
pg = 4096;
pg /= sizeof (line);
}
if ((char *)sbrk(pg * sizeof (line)) == (char *)-1)
return (-1);
endcore += pg;
return (0);
#else /* !_SC_PAGESIZE */
if (sbrk(1024 * sizeof (line)) == (char *)-1)
return (-1);
endcore += 1024;
return (0);
#endif /* !_SC_PAGESIZE */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment