summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorart <art@openbsd.org>2002-01-18 08:37:08 +0000
committerart <art@openbsd.org>2002-01-18 08:37:08 +0000
commit86112ee176b5e2fea148838794a0bc3af6c0c04d (patch)
tree39d0a8397a572cb81665a52d8ee8332f2a576041
parentAdd disklabel-style editing for size/offset values in (diff)
downloadwireguard-openbsd-86112ee176b5e2fea148838794a0bc3af6c0c04d.tar.xz
wireguard-openbsd-86112ee176b5e2fea148838794a0bc3af6c0c04d.zip
Remove the NROW and NCOL limits. The static arrays are now replaced with
dynamically allocated memory. Code written by Vincent Labrecque <limitln@Psyfreaks.CA> with some minor tweaks by me.
-rw-r--r--usr.bin/mg/buffer.c4
-rw-r--r--usr.bin/mg/def.h3
-rw-r--r--usr.bin/mg/display.c167
-rw-r--r--usr.bin/mg/echo.c7
-rw-r--r--usr.bin/mg/tty.c39
-rw-r--r--usr.bin/mg/ttydef.h9
6 files changed, 168 insertions, 61 deletions
diff --git a/usr.bin/mg/buffer.c b/usr.bin/mg/buffer.c
index 22898104ee5..07c5daa6b9b 100644
--- a/usr.bin/mg/buffer.c
+++ b/usr.bin/mg/buffer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: buffer.c,v 1.17 2001/11/25 07:34:17 deraadt Exp $ */
+/* $OpenBSD: buffer.c,v 1.18 2002/01/18 08:37:08 art Exp $ */
/*
* Buffer handling.
@@ -301,7 +301,7 @@ anycb(f)
char prompt[NFILEN + 11];
for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
- if (*(bp->b_fname) != '\0'
+ if (bp->b_fname != NULL && *(bp->b_fname) != '\0'
&& (bp->b_flag & BFCHG) != 0) {
snprintf(prompt, sizeof prompt, "Save file %s",
bp->b_fname);
diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h
index 5508d4468b5..f33959e2ca6 100644
--- a/usr.bin/mg/def.h
+++ b/usr.bin/mg/def.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: def.h,v 1.23 2002/01/10 12:13:35 art Exp $ */
+/* $OpenBSD: def.h,v 1.24 2002/01/18 08:37:08 art Exp $ */
/*
* This file is the general header file for all parts
@@ -360,6 +360,7 @@ int notmodified __P((int, int));
int popbuftop __P((BUFFER *));
/* display.c */
+int vtresize __P((int, int, int));
void vtinit __P((void));
void vttidy __P((void));
void update __P((void));
diff --git a/usr.bin/mg/display.c b/usr.bin/mg/display.c
index c8212f1c2a8..aafc830fecf 100644
--- a/usr.bin/mg/display.c
+++ b/usr.bin/mg/display.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: display.c,v 1.6 2001/05/24 03:05:22 mickey Exp $ */
+/* $OpenBSD: display.c,v 1.7 2002/01/18 08:37:08 art Exp $ */
/*
* The functions in this file handle redisplay. The
@@ -36,15 +36,15 @@
/*
* A video structure always holds
* an array of characters whose length is equal to
- * the longest line possible. Only some of this is
- * used if "ncol" isn't the same as "NCOL".
+ * the longest line possible. v_text is allocated
+ * dynamically to fit the screen width.
*/
typedef struct {
short v_hash; /* Hash code, for compares. */
short v_flag; /* Flag word. */
short v_color; /* Color of the line. */
XSHORT v_cost; /* Cost of display. */
- char v_text[NCOL]; /* The actual characters. */
+ char *v_text; /* The actual characters. */
} VIDEO;
#define VFCHG 0x0001 /* Changed. */
@@ -65,7 +65,6 @@ typedef struct {
XSHORT s_cost; /* Display cost. */
} SCORE;
-
void vtmove __P((int, int));
void vtputc __P((int));
void vtpute __P((int));
@@ -81,19 +80,19 @@ void hash __P((VIDEO *));
int sgarbf = TRUE; /* TRUE if screen is garbage. */
-int vtrow = 0; /* Virtual cursor row. */
-int vtcol = 0; /* Virtual cursor column. */
+int vtrow = HUGE; /* Virtual cursor row. */
+int vtcol = HUGE; /* Virtual cursor column. */
int tthue = CNONE; /* Current color. */
int ttrow = HUGE; /* Physical cursor row. */
int ttcol = HUGE; /* Physical cursor column. */
int tttop = HUGE; /* Top of scroll region. */
int ttbot = HUGE; /* Bottom of scroll region. */
int lbound = 0; /* leftmost bound of the current line */
- /* being displayed */
+ /* being displayed */
-VIDEO *vscreen[NROW - 1]; /* Edge vector, virtual. */
-VIDEO *pscreen[NROW - 1]; /* Edge vector, physical. */
-VIDEO video[2 * (NROW - 1)]; /* Actual screen data. */
+VIDEO **vscreen; /* Edge vector, virtual. */
+VIDEO **pscreen; /* Edge vector, physical. */
+VIDEO *video; /* Actual screen data. */
VIDEO blanks; /* Blank line image. */
#ifdef GOSLING
@@ -104,8 +103,111 @@ VIDEO blanks; /* Blank line image. */
* It would be "SCORE score[NROW][NROW]" in old speak.
* Look at "setscores" to understand what is up.
*/
-SCORE score[NROW * NROW];
+SCORE *score; /* [NROW * NROW] */
+#endif
+
+/*
+ * Reinit the display data structures, this is called when the terminal
+ * size changes.
+ */
+int
+vtresize(int force, int newrow, int newcol)
+{
+ int i;
+ int rowchanged, colchanged;
+ static int first_run = 1;
+ VIDEO *vp;
+
+ if (newrow < 1 || newcol < 1) {
+ return -1;
+ }
+
+ rowchanged = (newrow != nrow);
+ colchanged = (newcol != ncol);
+
+#define TRYREALLOC(a, n) do { \
+ void *tmp; \
+ if ((tmp = realloc((a), (n))) == NULL) { \
+ panic("out of memory in display code"); \
+ } \
+ (a) = tmp; \
+ } while (0)
+
+ /* No update needed */
+ if (!first_run && !force && !rowchanged && !colchanged) {
+ return 0;
+ }
+
+ if (first_run) {
+ memset(&blanks, 0, sizeof(blanks));
+ }
+
+ if (rowchanged || first_run) {
+ int vidstart;
+
+ /*
+ * This is not pretty.
+ */
+ if (nrow == 0)
+ vidstart = 0;
+ else
+ vidstart = 2 * (nrow - 1);
+
+ /*
+ * We're shrinking, free some internal data
+ */
+ if (newrow < nrow) {
+ for (i = 2 * (newrow - 1); i < 2 * (nrow - 1); i++) {
+ free(video[i].v_text);
+ video[i].v_text = NULL;
+ }
+ }
+
+#ifdef GOSLING
+ TRYREALLOC(score, newrow * newrow * sizeof(SCORE));
#endif
+ TRYREALLOC(vscreen, (newrow - 1) * sizeof(VIDEO *));
+ TRYREALLOC(pscreen, (newrow - 1) * sizeof(VIDEO *));
+ TRYREALLOC(video, (2 * (newrow - 1)) * sizeof(VIDEO));
+
+ /*
+ * Zero-out the entries we just allocated
+ */
+ for (i = vidstart; i < 2 * (newrow - 1); i++) {
+ memset(&video[i], 0, sizeof(VIDEO));
+ }
+
+ /*
+ * Reinitialize vscreen and pscreen arrays completely.
+ */
+ vp = &video[0];
+ for (i = 0; i < newrow - 1; ++i) {
+ vscreen[i] = vp;
+ ++vp;
+ pscreen[i] = vp;
+ ++vp;
+ }
+ }
+ if (rowchanged || colchanged || first_run) {
+ for (i = 0; i < 2 * (newrow - 1); i++) {
+ TRYREALLOC(video[i].v_text, newcol * sizeof(char));
+ }
+ TRYREALLOC(blanks.v_text, newcol * sizeof(char));
+ }
+
+ nrow = newrow;
+ ncol = newcol;
+
+ if (ttrow > nrow)
+ ttrow = nrow;
+ if (ttcol > ncol)
+ ttcol = ncol;
+
+ first_run = 0;
+ return 0;
+}
+
+#undef TRYREALLOC
/*
* Initialize the data structures used
@@ -121,20 +223,18 @@ SCORE score[NROW * NROW];
void
vtinit()
{
- VIDEO *vp;
int i;
ttopen();
ttinit();
- vp = &video[0];
- for (i = 0; i < NROW - 1; ++i) {
- vscreen[i] = vp;
- ++vp;
- pscreen[i] = vp;
- ++vp;
- }
+
+ /*
+ * ttinit called ttresize(), which called vtresize(), so our data
+ * structures are setup correctly.
+ */
+
blanks.v_color = CTEXT;
- for (i = 0; i < NCOL; ++i)
+ for (i = 0; i < ncol; ++i)
blanks.v_text[i] = ' ';
}
@@ -169,7 +269,6 @@ void
vtmove(row, col)
int row, col;
{
-
vtrow = row;
vtcol = col;
}
@@ -800,16 +899,16 @@ setscores(offs, size)
++vp;
++sp;
}
- sp = &score[NROW]; /* Column 0, deletes. */
+ sp = &score[nrow]; /* Column 0, deletes. */
tempcost = 0;
for (i = 1; i <= size; ++i) {
sp->s_itrace = i - 1;
sp->s_jtrace = 0;
tempcost += tcdell;
sp->s_cost = tempcost;
- sp += NROW;
+ sp += nrow;
}
- sp1 = &score[NROW + 1]; /* [1, 1]. */
+ sp1 = &score[nrow + 1]; /* [1, 1]. */
pp = &pbase[1];
for (i = 1; i <= size; ++i) {
sp = sp1;
@@ -817,7 +916,7 @@ setscores(offs, size)
for (j = 1; j <= size; ++j) {
sp->s_itrace = i - 1;
sp->s_jtrace = j;
- bestcost = (sp - NROW)->s_cost;
+ bestcost = (sp - nrow)->s_cost;
if (j != size) /* Cd(A[i])=0 @ Dis. */
bestcost += tcdell;
tempcost = (sp - 1)->s_cost;
@@ -829,7 +928,7 @@ setscores(offs, size)
sp->s_jtrace = j - 1;
bestcost = tempcost;
}
- tempcost = (sp - NROW - 1)->s_cost;
+ tempcost = (sp - nrow - 1)->s_cost;
if ((*pp)->v_color != (*vp)->v_color
|| (*pp)->v_hash != (*vp)->v_hash)
tempcost += (*vp)->v_cost;
@@ -843,7 +942,7 @@ setscores(offs, size)
++vp;
}
++pp;
- sp1 += NROW; /* Next row. */
+ sp1 += nrow; /* Next row. */
}
}
@@ -875,17 +974,17 @@ traceback(offs, size, i, j)
if (i == 0 && j == 0) /* End of update. */
return;
- itrace = score[(NROW * i) + j].s_itrace;
- jtrace = score[(NROW * i) + j].s_jtrace;
+ itrace = score[(nrow * i) + j].s_itrace;
+ jtrace = score[(nrow * i) + j].s_jtrace;
if (itrace == i) { /* [i, j-1] */
ninsl = 0; /* Collect inserts. */
if (i != size)
ninsl = 1;
ndraw = 1;
while (itrace != 0 || jtrace != 0) {
- if (score[(NROW * itrace) + jtrace].s_itrace != itrace)
+ if (score[(nrow * itrace) + jtrace].s_itrace != itrace)
break;
- jtrace = score[(NROW * itrace) + jtrace].s_jtrace;
+ jtrace = score[(nrow * itrace) + jtrace].s_jtrace;
if (i != size)
++ninsl;
++ndraw;
@@ -906,9 +1005,9 @@ traceback(offs, size, i, j)
if (j != size)
ndell = 1;
while (itrace != 0 || jtrace != 0) {
- if (score[(NROW * itrace) + jtrace].s_jtrace != jtrace)
+ if (score[(nrow * itrace) + jtrace].s_jtrace != jtrace)
break;
- itrace = score[(NROW * itrace) + jtrace].s_itrace;
+ itrace = score[(nrow * itrace) + jtrace].s_itrace;
if (j != size)
++ndell;
}
diff --git a/usr.bin/mg/echo.c b/usr.bin/mg/echo.c
index 4c41ef13f22..387be8ffa98 100644
--- a/usr.bin/mg/echo.c
+++ b/usr.bin/mg/echo.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: echo.c,v 1.16 2001/05/24 09:47:33 art Exp $ */
+/* $OpenBSD: echo.c,v 1.17 2002/01/18 08:37:08 art Exp $ */
/*
* Echo line reading and writing.
@@ -417,7 +417,7 @@ complt_list(flags, c, buf, cpos)
int oldrow = ttrow;
int oldcol = ttcol;
int oldhue = tthue;
- char linebuf[NCOL + 1];
+ char *linebuf;
char *cp;
lh = NULL;
@@ -504,6 +504,8 @@ complt_list(flags, c, buf, cpos)
* Now do the display. objects are written into linebuf until
* it fills, and then put into the help buffer.
*/
+ if ((linebuf = malloc((nrow + 1) * sizeof(char))) == NULL)
+ return FALSE;
cp = linebuf;
width = 0;
lh2 = lh;
@@ -532,6 +534,7 @@ complt_list(flags, c, buf, cpos)
*cp = 0;
addline(bp, linebuf);
}
+ free(linebuf);
}
/*
* Note that we free lists only if they are put in wholelist lists
diff --git a/usr.bin/mg/tty.c b/usr.bin/mg/tty.c
index 0ae28308219..fe5a89ca852 100644
--- a/usr.bin/mg/tty.c
+++ b/usr.bin/mg/tty.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tty.c,v 1.10 2002/01/10 12:13:35 art Exp $ */
+/* $OpenBSD: tty.c,v 1.11 2002/01/18 08:37:08 art Exp $ */
/*
* Terminfo display driver
@@ -99,7 +99,7 @@ ttinit()
tcinsl = charcost(insert_line);
else
/* make this cost high enough */
- tcinsl = NROW * NCOL;
+ tcinsl = nrow * ncol;
/* Estimate cost of deleting a line */
if (change_scroll_region)
@@ -111,7 +111,7 @@ ttinit()
tcdell = charcost(delete_line);
else
/* make this cost high enough */
- tcdell = NROW * NCOL;
+ tcdell = nrow * ncol;
/* Flag to indicate that we can both insert and delete lines */
insdel = (insert_line || parm_insert_line) &&
@@ -400,33 +400,34 @@ ttcolor(color)
/*
* This routine is called by the "refresh the screen" command to try
- * to resize the display. The new size, which must not exceed the NROW
- * and NCOL limits, is stored back into "nrow" and * "ncol". Display can
- * always deal with a screen NROW by NCOL. Look in "window.c" to see how
+ * to resize the display. Look in "window.c" to see how
* the caller deals with a change.
+ *
+ * We use `newrow' and `newcol' so vtresize() know the difference between the
+ * new and old settings.
*/
void
ttresize()
{
+ int newrow = 0, newcol = 0;
+
#ifdef TIOCGWINSZ
struct winsize winsize;
if (ioctl(0, TIOCGWINSZ, (char *) &winsize) == 0) {
- nrow = winsize.ws_row;
- ncol = winsize.ws_col;
- } else nrow = 0;
+ newrow = winsize.ws_row;
+ newcol = winsize.ws_col;
+ }
#endif
- if ((nrow <= 0 || ncol <= 0) &&
- ((nrow = lines) <= 0 || (ncol = columns) <= 0)) {
- nrow = 24;
- ncol = 80;
+ if ((newrow <= 0 || newcol <= 0) &&
+ ((newrow = lines) <= 0 || (newcol = columns) <= 0)) {
+ newrow = 24;
+ newcol = 80;
}
-
- /* Enforce maximum screen size. */
- if (nrow > NROW)
- nrow = NROW;
- if (ncol > NCOL)
- ncol = NCOL;
+ if (vtresize(1, newrow, newcol))
+ panic("vtresize failed");
+ nrow = newrow;
+ ncol = newcol;
}
/*
diff --git a/usr.bin/mg/ttydef.h b/usr.bin/mg/ttydef.h
index 661032696f8..1b95c7cd1a5 100644
--- a/usr.bin/mg/ttydef.h
+++ b/usr.bin/mg/ttydef.h
@@ -1,4 +1,7 @@
-/* $OpenBSD: ttydef.h,v 1.4 2001/01/29 01:58:10 niklas Exp $ */
+/* $OpenBSD: ttydef.h,v 1.5 2002/01/18 08:37:08 art Exp $ */
+
+#ifndef TTYDEF_H
+#define TTYDEF_H
/*
* Terminfo terminal file, nothing special, just make it big
@@ -8,8 +11,6 @@
#define GOSLING /* Compile in fancy display. */
/* #define MEMMAP *//* Not memory mapped video. */
-#define NROW 66 /* (maximum) Rows. */
-#define NCOL 132 /* (maximum) Columns. */
/* #define MOVE_STANDOUT *//* don't move in standout mode */
#define STANDOUT_GLITCH /* possible standout glitch */
#define TERMCAP /* for possible use in ttyio.c */
@@ -24,3 +25,5 @@
#define KFIRST K00
#define KLAST K00
+
+#endif /* TTYDEF_H */