diff options
Diffstat (limited to 'usr.bin/mg')
-rw-r--r-- | usr.bin/mg/basic.c | 4 | ||||
-rw-r--r-- | usr.bin/mg/def.h | 10 | ||||
-rw-r--r-- | usr.bin/mg/file.c | 4 | ||||
-rw-r--r-- | usr.bin/mg/kbd.c | 28 | ||||
-rw-r--r-- | usr.bin/mg/line.c | 127 |
5 files changed, 64 insertions, 109 deletions
diff --git a/usr.bin/mg/basic.c b/usr.bin/mg/basic.c index dc2926126e4..bb2e6b81245 100644 --- a/usr.bin/mg/basic.c +++ b/usr.bin/mg/basic.c @@ -1,4 +1,4 @@ -/* $OpenBSD: basic.c,v 1.7 2002/02/08 21:21:11 deraadt Exp $ */ +/* $OpenBSD: basic.c,v 1.8 2002/02/13 22:36:58 vincent Exp $ */ /* * Basic cursor motion commands. @@ -166,7 +166,7 @@ forwline(f, n) } curwp->w_doto = 0; while (n-- >= 0) { - if ((dlp = lallocx(0)) == NULL) + if ((dlp = lalloc(0)) == NULL) return FALSE; dlp->l_fp = curbp->b_linep; dlp->l_bp = lback(dlp->l_fp); diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h index bd2af3aae6b..cc880b52637 100644 --- a/usr.bin/mg/def.h +++ b/usr.bin/mg/def.h @@ -1,4 +1,4 @@ -/* $OpenBSD: def.h,v 1.25 2002/02/13 03:03:49 vincent Exp $ */ +/* $OpenBSD: def.h,v 1.26 2002/02/13 22:36:58 vincent Exp $ */ /* * This file is the general header file for all parts @@ -121,11 +121,7 @@ typedef struct LINE { struct LINE *l_bp; /* Link to the previous line */ int l_size; /* Allocated size */ int l_used; /* Used size */ -#ifndef ZEROARRAY - char l_text[1]; /* A bunch of chars. */ -#else - char l_text[]; /* A bunch of chars. */ -#endif + char *l_text; /* Content of the line */ } LINE; /* @@ -318,7 +314,7 @@ void upmodes __P((BUFFER *)); /* line.c X */ LINE *lalloc __P((int)); -LINE *lallocx __P((int)); +int lrealloc __P((LINE *, int)); void lfree __P((LINE *)); void lchange __P((int)); int linsert __P((int, int)); diff --git a/usr.bin/mg/file.c b/usr.bin/mg/file.c index 34497e502d8..593d68eeb66 100644 --- a/usr.bin/mg/file.c +++ b/usr.bin/mg/file.c @@ -1,4 +1,4 @@ -/* $OpenBSD: file.c,v 1.10 2002/02/13 03:03:49 vincent Exp $ */ +/* $OpenBSD: file.c,v 1.11 2002/02/13 22:36:58 vincent Exp $ */ /* * File commands. @@ -96,7 +96,7 @@ findbuffer(fname) { BUFFER *bp; char bname[NBUFN]; - unsigned int count = 1; + unsigned int count; for (bp = bheadp; bp != NULL; bp = bp->b_bufp) { if (strcmp(bp->b_fname, fname) == 0) diff --git a/usr.bin/mg/kbd.c b/usr.bin/mg/kbd.c index 0cd3f1dd4fe..21a49f86c85 100644 --- a/usr.bin/mg/kbd.c +++ b/usr.bin/mg/kbd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kbd.c,v 1.10 2001/05/24 03:05:23 mickey Exp $ */ +/* $OpenBSD: kbd.c,v 1.11 2002/02/13 22:36:58 vincent Exp $ */ /* * Terminal independent keyboard handling. @@ -375,28 +375,20 @@ selfinsert(f, n) /* last command was insert -- tack on the end */ if (lastflag & CFINS) { macrocount--; + /* Ensure the line can handle the new characters */ if (maclcur->l_size < maclcur->l_used + n) { - if ((lp = lallocx(maclcur->l_used + n)) == NULL) + if (lrealloc(maclcur, maclcur->l_used + n) == + FALSE) return FALSE; - lp->l_fp = maclcur->l_fp; - lp->l_bp = maclcur->l_bp; - lp->l_fp->l_bp = lp->l_bp->l_fp = lp; - bcopy(maclcur->l_text, lp->l_text, - maclcur->l_used); - for (count = maclcur->l_used; - count < lp->l_used; count++) - lp->l_text[count] = c; - free((char *)maclcur); - maclcur = lp; - } else { - maclcur->l_used += n; - for (count = maclcur->l_used - n; - count < maclcur->l_used; count++) - maclcur->l_text[count] = c; } + maclcur->l_used += n; + /* Copy in the new data */ + for (count = maclcur->l_used - n; + count < maclcur->l_used; count++) + maclcur->l_text[count] = c; } else { macro[macrocount - 1].m_funct = insert; - if ((lp = lallocx(n)) == NULL) + if ((lp = lalloc(n)) == NULL) return FALSE; lp->l_bp = maclcur; lp->l_fp = maclcur->l_fp; diff --git a/usr.bin/mg/line.c b/usr.bin/mg/line.c index 81267d93c5f..83dba9c88a4 100644 --- a/usr.bin/mg/line.c +++ b/usr.bin/mg/line.c @@ -1,4 +1,4 @@ -/* $OpenBSD: line.c,v 1.8 2001/05/24 03:05:23 mickey Exp $ */ +/* $OpenBSD: line.c,v 1.9 2002/02/13 22:36:58 vincent Exp $ */ /* * Text line handling. @@ -43,50 +43,37 @@ static RSIZE kstart = 0; /* # of first used byte in KB. */ static int kgrow __P((int)); /* - * This routine allocates a block of memory large enough to hold a LINE - * containing "used" characters. The block is rounded up to whatever - * needs to be allocated. (use lallocx for lines likely to grow.) - * Return a pointer to the new block, or NULL if there isn't any memory - * left. Print a message in the message line if no space. + * Allocate a new line of size `used'. lrealloc() can be called if the line + * ever needs to grow beyond that. */ LINE * -lalloc(used) - int used; +lalloc(int used) { - LINE *lp; - int size; - - /* any padding at the end of the structure is used */ - if ((size = used + OFFSET(LINE, l_text[0])) < sizeof(LINE)) - size = sizeof(LINE); -#ifdef MALLOCROUND - MALLOCROUND(size); /* round up to a size optimal to malloc */ -#endif - if ((lp = malloc((unsigned)size)) == NULL) { - ewprintf("Can't get %d bytes", size); + LINE *lp; + + if ((lp = malloc(sizeof *lp)) == NULL) + return FALSE; + lp->l_text = NULL; + lp->l_size = 0; + lp->l_used = used; /* XXX */ + if (lrealloc(lp, used) == FALSE) { + free(lp); return NULL; } - lp->l_size = size - OFFSET(LINE, l_text[0]); - lp->l_used = used; return lp; } -/* - * Like lalloc, only round amount desired up because this line will - * probably grow. We always make room for at least one more char. - * (thus making 0 not a special case anymore.) - */ -LINE * -lallocx(used) - int used; +int +lrealloc(LINE *lp, int newsize) { - int size; - LINE *lp; + char *tmp; - size = (NBLOCK + used) & ~(NBLOCK - 1); - if ((lp = lalloc(size)) != NULL) - lp->l_used = used; - return lp; + if ((tmp = realloc(lp->l_text, newsize)) == NULL) + return FALSE; + lp->l_text = tmp; + lp->l_size = newsize; + + return TRUE; } /* @@ -128,7 +115,9 @@ lfree(lp) } lp->l_bp->l_fp = lp->l_fp; lp->l_fp->l_bp = lp->l_bp; - free((char *)lp); + if (lp->l_text != NULL) + free(lp->l_text); + free(lp); } /* @@ -171,27 +160,29 @@ int linsert(n, c) int n, c; { - LINE *lp1, *lp2, *lp3; + LINE *lp1; MGWIN *wp; RSIZE i; int doto; - char *cp1, *cp2; lchange(WFEDIT); /* current line */ lp1 = curwp->w_dotp; - + /* special case for the end */ if (lp1 == curbp->b_linep) { + LINE *lp2, *lp3; + /* now should only happen in empty buffer */ if (curwp->w_doto != 0) { ewprintf("bug: linsert"); return FALSE; } /* allocate a new line */ - if ((lp2 = lallocx(n)) == NULL) + if ((lp2 = lalloc(n)) == NULL) return FALSE; + /* previous line */ lp3 = lp1->l_bp; /* link in */ @@ -209,62 +200,37 @@ linsert(n, c) if (wp->w_markp == lp1) wp->w_markp = lp2; } - /* NOSTRICT */ + curwp->w_doto = n; return TRUE; } /* save for later */ doto = curwp->w_doto; - /* NOSTRICT (2) */ - /* Hard case: reallocate */ - if (lp1->l_used + n > lp1->l_size) { - if ((lp2 = lallocx(lp1->l_used + n)) == NULL) + + + if ((lp1->l_used + n) > lp1->l_size) { + if (lrealloc(lp1, lp1->l_used + n) == FALSE) return FALSE; - cp1 = &lp1->l_text[0]; - cp2 = &lp2->l_text[0]; - while (cp1 != &lp1->l_text[doto]) - *cp2++ = *cp1++; - /* NOSTRICT */ - cp2 += n; - while (cp1 != &lp1->l_text[lp1->l_used]) - *cp2++ = *cp1++; - lp1->l_bp->l_fp = lp2; - lp2->l_fp = lp1->l_fp; - lp1->l_fp->l_bp = lp2; - lp2->l_bp = lp1->l_bp; - free((char *)lp1); - /* Easy case: in place */ - } else { - /* pretend there's a new line */ - lp2 = lp1; - /* NOSTRICT */ - lp2->l_used += n; - cp2 = &lp1->l_text[lp1->l_used]; - - cp1 = cp2 - n; - while (cp1 != &lp1->l_text[doto]) - *--cp2 = *--cp1; - } + } + lp1->l_used += n; + if (lp1->l_used != n) + memmove(&lp1->l_text[doto + n], &lp1->l_text[doto], + lp1->l_used - n - doto); + /* Add the characters */ for (i = 0; i < n; ++i) - lp2->l_text[doto + i] = c; - + lp1->l_text[doto + i] = c; for (wp = wheadp; wp != NULL; wp = wp->w_wndp) { - if (wp->w_linep == lp1) - wp->w_linep = lp2; if (wp->w_dotp == lp1) { - wp->w_dotp = lp2; if (wp == curwp || wp->w_doto > doto) - /* NOSTRICT */ wp->w_doto += n; } if (wp->w_markp == lp1) { - wp->w_markp = lp2; if (wp->w_marko > doto) - /* NOSTRICT */ wp->w_marko += n; } } + return TRUE; } @@ -288,7 +254,7 @@ lnewline() /* avoid unnecessary copying */ if (doto == 0) { /* new first part */ - if ((lp2 = lallocx(0)) == NULL) + if ((lp2 = lalloc(0)) == NULL) return FALSE; lp2->l_bp = lp1->l_bp; lp1->l_bp->l_fp = lp2; @@ -304,7 +270,7 @@ lnewline() nlen = llength(lp1) - doto; /* new second half line */ - if ((lp2 = lallocx(nlen)) == NULL) + if ((lp2 = lalloc(nlen)) == NULL) return FALSE; if (nlen != 0) bcopy(&lp1->l_text[doto], &lp2->l_text[0], nlen); @@ -483,6 +449,7 @@ ldelnewline() return TRUE; } + /* * Replace plen characters before dot with argument string. Control-J * characters in st are interpreted as newlines. There is a casehack |