zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: make curses module not need wideness
@ 2007-10-14  4:21 Clint Adams
  2007-10-14 16:26 ` Bart Schaefer
  0 siblings, 1 reply; 12+ messages in thread
From: Clint Adams @ 2007-10-14  4:21 UTC (permalink / raw)
  To: zsh-workers

This will build against "narrow" ncurses.  To my amusement and chagrin,
zcurses -s works this way.

Index: Src/Modules/curses.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/curses.c,v
retrieving revision 1.3
diff -u -r1.3 curses.c
--- Src/Modules/curses.c	8 Oct 2007 23:23:19 -0000	1.3
+++ Src/Modules/curses.c	14 Oct 2007 04:19:28 -0000
@@ -30,7 +30,9 @@
 #define _XOPEN_SOURCE_EXTENDED 1
 
 #include <ncurses.h>
-#include <wchar.h>
+#ifdef HAVE_SETCCHAR
+# include <wchar.h>
+#endif
 
 #include <stdio.h>
 
@@ -165,8 +167,10 @@
     }
 
     if (OPT_ISSET(ops,'c')) {
+#ifdef HAVE_SETCCHAR
 	wchar_t c;
 	cchar_t cc;
+#endif
 
 	targetwin = zcurses_validate_window(args[0], ZCURSES_USED);
 	if (targetwin == -1) {
@@ -174,6 +178,7 @@
 	    return 1;
 	}
 
+#ifdef HAVE_SETCCHAR
 	if (mbrtowc(&c, args[1], MB_CUR_MAX, NULL) < 1)
 	    return 1;
 
@@ -182,14 +187,20 @@
 
 	if (wadd_wch(zcurses_WIN[targetwin], &cc)!=OK)
 	    return 1;
+#else
+	if (waddch(zcurses_WIN[targetwin], (chtype)args[1][0])!=OK)
+	    return 1;
+#endif
 
 	return 0;
     }
 
     if (OPT_ISSET(ops,'s')) {
+#ifdef HAVE_SETCCHAR
 	wchar_t *ws;
 	cchar_t *wcc;
 	size_t sl;
+#endif
 
 	targetwin = zcurses_validate_window(args[0], ZCURSES_USED);
 	if (targetwin == -1) {
@@ -197,6 +208,7 @@
 	    return 1;
 	}
 
+#ifdef HAVE_SETCCHAR
 	sl = strlen(args[1]);
 
 	if (sl == 0) {
@@ -224,6 +236,10 @@
 	}
 
 	free(wcc);
+#else
+	if (waddstr(zcurses_WIN[targetwin], args[1])!=OK)
+	    return 1;
+#endif
 	return 0;
     }
 
Index: Src/Modules/curses.mdd
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/curses.mdd,v
retrieving revision 1.2
diff -u -r1.2 curses.mdd
--- Src/Modules/curses.mdd	8 Oct 2007 14:11:29 -0000	1.2
+++ Src/Modules/curses.mdd	14 Oct 2007 04:19:28 -0000
@@ -1,5 +1,5 @@
 name=zsh/curses
-link='if test "x$ac_cv_func_initscr" = xyes -a "x$ac_cv_header_curses_h" = xyes -a "x$ac_cv_func_setcchar" = xyes; then echo dynamic; else echo no; fi'
+link='if test "x$ac_cv_func_initscr" = xyes -a "x$ac_cv_header_curses_h" = xyes; then echo dynamic; else echo no; fi'
 load=no
 
 autobins="zcurses"


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PATCH: make curses module not need wideness
  2007-10-14  4:21 PATCH: make curses module not need wideness Clint Adams
@ 2007-10-14 16:26 ` Bart Schaefer
  2007-10-14 17:08   ` Clint Adams
  0 siblings, 1 reply; 12+ messages in thread
From: Bart Schaefer @ 2007-10-14 16:26 UTC (permalink / raw)
  To: zsh-workers

On Oct 14, 12:21am, Clint Adams wrote:
}
} This will build against "narrow" ncurses.  To my amusement and chagrin,
} zcurses -s works this way.

Is there any documentation for zcurses yet?  What is "zcurses -s"?

As an aside, when I "zmodload zsh/curses" my terminal gets erased as
if I'd invoked "zle clear-screen".  That seems a bit unfriendly.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PATCH: make curses module not need wideness
  2007-10-14 16:26 ` Bart Schaefer
@ 2007-10-14 17:08   ` Clint Adams
  2007-10-14 22:16     ` Bart Schaefer
  0 siblings, 1 reply; 12+ messages in thread
From: Clint Adams @ 2007-10-14 17:08 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

On Sun, Oct 14, 2007 at 09:26:46AM -0700, Bart Schaefer wrote:
> Is there any documentation for zcurses yet?  What is "zcurses -s"?

Oops. -s outputs strings to a window. Perhaps -c and -s should be
combined.

> As an aside, when I "zmodload zsh/curses" my terminal gets erased as
> if I'd invoked "zle clear-screen".  That seems a bit unfriendly.

We could delay the initscr() to the first invocation of the zcurses
builtin, but I'm not sure how much more friendly that is.

Index: Doc/Makefile.in
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Makefile.in,v
retrieving revision 1.36
diff -u -r1.36 Makefile.in
--- Doc/Makefile.in	1 May 2007 22:05:04 -0000	1.36
+++ Doc/Makefile.in	14 Oct 2007 17:03:41 -0000
@@ -57,7 +57,7 @@
 MODDOCSRC = \
 Zsh/mod_cap.yo Zsh/mod_clone.yo \
 Zsh/mod_compctl.yo Zsh/mod_complete.yo Zsh/mod_complist.yo \
-Zsh/mod_computil.yo \
+Zsh/mod_computil.yo Zsh/mod_curses.yo \
 Zsh/mod_datetime.yo Zsh/mod_deltochar.yo \
 Zsh/mod_example.yo Zsh/mod_files.yo \
 Zsh/mod_mapfile.yo Zsh/mod_mathfunc.yo Zsh/mod_newuser.yo \
Index: Doc/Zsh/mod_curses.yo
===================================================================
RCS file: Doc/Zsh/mod_curses.yo
diff -N Doc/Zsh/mod_curses.yo
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Doc/Zsh/mod_curses.yo	14 Oct 2007 17:03:41 -0000
@@ -0,0 +1,38 @@
+COMMENT(!MOD!zsh/curses
+curses windowing commands
+!MOD!)
+The tt(zsh/curses) module makes available one builtin command:
+
+startitem()
+findex(zcurses)
+cindex(windows, curses)
+xitem(tt(zcurses) tt(-a) var(nlines) var(ncols) var(begin_y) var(begin_x) var(targetwin) )
+xitem(tt(zcurses) tt(-d) var(targetwin) )
+xitem(tt(zcurses) tt(-r) var(targetwin) )
+xitem(tt(zcurses) tt(-m) var(targetwin) var(new_y) var(new_x) )
+xitem(tt(zcurses) tt(-c) var(targetwin) var(character) )
+xitem(tt(zcurses) tt(-s) var(targetwin) var(string) )
+item(tt(zcurses) tt(-b) var(targetwin) var(border) )(
+Manipulate curses windows.
+
+With tt(-a), create a window with var(nlines) lines and var(ncols) columns.
+Its upper left corner will be placed at row var(begin_y) and column
+var(begin_x) of the screen.  var(targetwin) must be an integer between 1
+and 9 inclusive, and refer to a window that is not currently assigned.
+
+Use tt(-d) to delete a window created with tt(-a).
+
+The tt(-r) command will refresh window var(targetwin); this is necessary to
+make any pending changes (such as characters you have prepared for output
+with tt(-c)) visible on the screen.
+
+tt(-m) moves var(targetwin) to new coordinates var(new_y) and var(new_x).
+
+Outputting characters and strings are achieved by tt(-c) and tt(-s)
+respectively.
+
+To draw a border around window var(targetwin), use tt(-b).
+)
+enditem()
+
+enditem()


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PATCH: make curses module not need wideness
  2007-10-14 17:08   ` Clint Adams
@ 2007-10-14 22:16     ` Bart Schaefer
  2007-10-15  9:11       ` Peter Stephenson
  2007-10-15 13:25       ` Clint Adams
  0 siblings, 2 replies; 12+ messages in thread
From: Bart Schaefer @ 2007-10-14 22:16 UTC (permalink / raw)
  To: zsh-workers

On Oct 14,  1:08pm, Clint Adams wrote:
}
} Oops. -s outputs strings to a window. Perhaps -c and -s should be
} combined.

Please pardon that everything that follows is griping.

I'm not having much luck with this.  The doc should mention that window
zero is automatically created as the whole terminal.  Also there's no
check that the targetwin argument is really an integer; I typo'd a "q"
for a "1", got some errors about the window already existing (apparently
because non-integers are read as "0"), and ended up with a core dump:

(gdb) where
#0  doupdate () at ../../ncurses/tty/tty_update.c:873
#1  0x035d7fe9 in wrefresh (win=0x87f29c0)
    at ../../ncurses/base/lib_refresh.c:58
#2  0x00817e67 in bin_zcurses (nam=0xb7d2d460 "zcurses", args=0xbfe3eab8, 
    ops=0xbfe3eb00, func=0) at ../../../zsh-4.0/Src/Modules/curses.c:148
#3  0x080537f4 in execbuiltin (args=0xb7d2d430, bn=0x8195e0)
    at ../../zsh-4.0/Src/builtin.c:438
(etc.)

The doc for "zcurses -m" says "moves targetwin to new coordinates" but
really it positions the cursor within the target window, doesn't it?
Maybe better phrasing is all that's needed.

Also I none of the strings I send with "zcurses -s" show up in the
window.  A border added with "zcurses -b" will display, but that's all.
"zcurses -c" has a similar problem.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PATCH: make curses module not need wideness
  2007-10-14 22:16     ` Bart Schaefer
@ 2007-10-15  9:11       ` Peter Stephenson
  2007-10-15 13:35         ` Clint Adams
  2007-10-15 14:34         ` Clint Adams
  2007-10-15 13:25       ` Clint Adams
  1 sibling, 2 replies; 12+ messages in thread
From: Peter Stephenson @ 2007-10-15  9:11 UTC (permalink / raw)
  To: zsh-workers

Bart Schaefer wrote:
> I'm not having much luck with this.  The doc should mention that window
> zero is automatically created as the whole terminal.  Also there's no
> check that the targetwin argument is really an integer; I typo'd a "q"
> for a "1", got some errors about the window already existing (apparently
> because non-integers are read as "0"), and ended up with a core dump:

I think it would probably be better to let the windows have arbitrary names
and store them in a linked list, as we do for ztcp and zftp.  Presumably
there aren't going to be that many of them.  (The names can still be
numbers, obviously.)

I'm also wondering if it wouldn't be better for the window number/name
to be the first regular argument in all cases.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PATCH: make curses module not need wideness
  2007-10-14 22:16     ` Bart Schaefer
  2007-10-15  9:11       ` Peter Stephenson
@ 2007-10-15 13:25       ` Clint Adams
  2007-10-15 15:04         ` Bart Schaefer
  1 sibling, 1 reply; 12+ messages in thread
From: Clint Adams @ 2007-10-15 13:25 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

On Sun, Oct 14, 2007 at 03:16:08PM -0700, Bart Schaefer wrote:
> I'm not having much luck with this.  The doc should mention that window
> zero is automatically created as the whole terminal.  Also there's no
> check that the targetwin argument is really an integer; I typo'd a "q"
> for a "1", got some errors about the window already existing (apparently
> because non-integers are read as "0"), and ended up with a core dump:

I'll just disallow specification of window 0 for now.

> The doc for "zcurses -m" says "moves targetwin to new coordinates" but
> really it positions the cursor within the target window, doesn't it?
> Maybe better phrasing is all that's needed.

Agreed.

> Also I none of the strings I send with "zcurses -s" show up in the
> window.  A border added with "zcurses -b" will display, but that's all.
> "zcurses -c" has a similar problem.

As I perhaps failed to express before, -c and -s work fine for me with
non-wide ncurses, and only -c functions properly with wide ncurses.


Index: Doc/Zsh/mod_curses.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/mod_curses.yo,v
retrieving revision 1.1
diff -u -r1.1 mod_curses.yo
--- Doc/Zsh/mod_curses.yo	14 Oct 2007 17:14:22 -0000	1.1
+++ Doc/Zsh/mod_curses.yo	15 Oct 2007 13:20:15 -0000
@@ -26,7 +26,8 @@
 make any pending changes (such as characters you have prepared for output
 with tt(-c)) visible on the screen.
 
-tt(-m) moves var(targetwin) to new coordinates var(new_y) and var(new_x).
+tt(-m) moves the cursor position in var(targetwin) to new coordinates
+var(new_y) and var(new_x).
 
 Outputting characters and strings are achieved by tt(-c) and tt(-s)
 respectively.
Index: Src/Modules/curses.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/curses.c,v
retrieving revision 1.4
diff -u -r1.4 curses.c
--- Src/Modules/curses.c	14 Oct 2007 04:24:47 -0000	1.4
+++ Src/Modules/curses.c	15 Oct 2007 13:20:15 -0000
@@ -78,7 +78,7 @@
 
     target = (unsigned)atoi(win);
 
-    if (target > ZCURSES_MAX_WINDOWS) {
+    if (target > ZCURSES_MAX_WINDOWS || target < 1) {
 	zc_errno = ZCURSES_ERANGE;
 	return -1;
     }


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PATCH: make curses module not need wideness
  2007-10-15  9:11       ` Peter Stephenson
@ 2007-10-15 13:35         ` Clint Adams
  2007-10-15 14:34         ` Clint Adams
  1 sibling, 0 replies; 12+ messages in thread
From: Clint Adams @ 2007-10-15 13:35 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-workers

On Mon, Oct 15, 2007 at 10:11:52AM +0100, Peter Stephenson wrote:
> I'm also wondering if it wouldn't be better for the window number/name
> to be the first regular argument in all cases.

First:

Index: Doc/Zsh/mod_curses.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/mod_curses.yo,v
retrieving revision 1.2
diff -u -r1.2 mod_curses.yo
--- Doc/Zsh/mod_curses.yo	15 Oct 2007 13:30:08 -0000	1.2
+++ Doc/Zsh/mod_curses.yo	15 Oct 2007 13:33:53 -0000
@@ -6,7 +6,7 @@
 startitem()
 findex(zcurses)
 cindex(windows, curses)
-xitem(tt(zcurses) tt(-a) var(nlines) var(ncols) var(begin_y) var(begin_x) var(targetwin) )
+xitem(tt(zcurses) tt(-a) var(targetwin) var(nlines) var(ncols) var(begin_y) var(begin_x) )
 xitem(tt(zcurses) tt(-d) var(targetwin) )
 xitem(tt(zcurses) tt(-r) var(targetwin) )
 xitem(tt(zcurses) tt(-m) var(targetwin) var(new_y) var(new_x) )
Index: Src/Modules/curses.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/curses.c,v
retrieving revision 1.5
diff -u -r1.5 curses.c
--- Src/Modules/curses.c	15 Oct 2007 13:30:10 -0000	1.5
+++ Src/Modules/curses.c	15 Oct 2007 13:33:53 -0000
@@ -105,14 +105,14 @@
     if (OPT_ISSET(ops,'a')) {
 	int nlines, ncols, begin_y, begin_x;
 
-	nlines = atoi(args[0]);
-	ncols = atoi(args[1]);
-	begin_y = atoi(args[2]);
-	begin_x = atoi(args[3]);
-	targetwin = zcurses_validate_window(args[4], ZCURSES_UNUSED);
+	targetwin = zcurses_validate_window(args[0], ZCURSES_UNUSED);
+	nlines = atoi(args[1]);
+	ncols = atoi(args[2]);
+	begin_y = atoi(args[3]);
+	begin_x = atoi(args[4]);
 
 	if (targetwin == -1) {
-	    zerrnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[4], 0);
+	    zerrnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
 	    return 1;
 	}
 


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PATCH: make curses module not need wideness
  2007-10-15  9:11       ` Peter Stephenson
  2007-10-15 13:35         ` Clint Adams
@ 2007-10-15 14:34         ` Clint Adams
  2007-10-15 14:58           ` Peter Stephenson
  1 sibling, 1 reply; 12+ messages in thread
From: Clint Adams @ 2007-10-15 14:34 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-workers

On Mon, Oct 15, 2007 at 10:11:52AM +0100, Peter Stephenson wrote:
> I think it would probably be better to let the windows have arbitrary names
> and store them in a linked list, as we do for ztcp and zftp.  Presumably
> there aren't going to be that many of them.  (The names can still be
> numbers, obviously.)

So have a struct containing just the WINDOW pointer and some kind of
identifier?  Should there be a special assoc parameter containing
pointers?


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PATCH: make curses module not need wideness
  2007-10-15 14:34         ` Clint Adams
@ 2007-10-15 14:58           ` Peter Stephenson
  2007-10-15 16:45             ` Clint Adams
  0 siblings, 1 reply; 12+ messages in thread
From: Peter Stephenson @ 2007-10-15 14:58 UTC (permalink / raw)
  To: zsh-workers

Clint Adams wrote:
> On Mon, Oct 15, 2007 at 10:11:52AM +0100, Peter Stephenson wrote:
> > I think it would probably be better to let the windows have arbitrary names
> > and store them in a linked list, as we do for ztcp and zftp.  Presumably
> > there aren't going to be that many of them.  (The names can still be
> > numbers, obviously.)
> 
> So have a struct containing just the WINDOW pointer and some kind of
> identifier?

Something like that.

> Should there be a special assoc parameter containing pointers?

I don't think there needs to be an assoc *parameter*: there's nothing to
map at the shell code level, just a list of names.  This could be
visible in a special array as a free bonus.

Internally, from the point of view of efficiency it's probably not worth
having anything more than a linked list, but on the other hand the hash
stuff does more of the work for you, once you've got it set up (some of
the module stuff got simpler when I turned those into a hash).

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PATCH: make curses module not need wideness
  2007-10-15 13:25       ` Clint Adams
@ 2007-10-15 15:04         ` Bart Schaefer
  2007-10-15 16:48           ` Clint Adams
  0 siblings, 1 reply; 12+ messages in thread
From: Bart Schaefer @ 2007-10-15 15:04 UTC (permalink / raw)
  To: zsh-workers

On Oct 15,  9:25am, Clint Adams wrote:
}
} As I perhaps failed to express before, -c and -s work fine for me with
} non-wide ncurses, and only -c functions properly with wide ncurses.

No, you successfully expressed that.  What I failed to express is that
*neither* -s nor -c work for me with wide curses.  I haven't had time
to try with "regular" curses yet.

(What ncurses version do you have?  My RPM version is ncurses-5.4-13)


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PATCH: make curses module not need wideness
  2007-10-15 14:58           ` Peter Stephenson
@ 2007-10-15 16:45             ` Clint Adams
  0 siblings, 0 replies; 12+ messages in thread
From: Clint Adams @ 2007-10-15 16:45 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-workers

On Mon, Oct 15, 2007 at 03:58:30PM +0100, Peter Stephenson wrote:
> Something like that.

Here goes nothing.

Index: Doc/Zsh/mod_curses.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/mod_curses.yo,v
retrieving revision 1.3
diff -u -r1.3 mod_curses.yo
--- Doc/Zsh/mod_curses.yo	15 Oct 2007 13:37:41 -0000	1.3
+++ Doc/Zsh/mod_curses.yo	15 Oct 2007 16:44:55 -0000
@@ -17,8 +17,8 @@
 
 With tt(-a), create a window with var(nlines) lines and var(ncols) columns.
 Its upper left corner will be placed at row var(begin_y) and column
-var(begin_x) of the screen.  var(targetwin) must be an integer between 1
-and 9 inclusive, and refer to a window that is not currently assigned.
+var(begin_x) of the screen.  var(targetwin) is a string and refers
+to the name of a window that is not currently assigned.
 
 Use tt(-d) to delete a window created with tt(-a).
 
Index: Src/Modules/curses.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/curses.c,v
retrieving revision 1.6
diff -u -r1.6 curses.c
--- Src/Modules/curses.c	15 Oct 2007 13:37:41 -0000	1.6
+++ Src/Modules/curses.c	15 Oct 2007 16:44:55 -0000
@@ -39,9 +39,13 @@
 #include "curses.mdh"
 #include "curses.pro"
 
-#define ZCURSES_MAX_WINDOWS 9
+typedef struct zc_win {
+    WINDOW *win;
+    char *name;
+} *ZCWin;
 
-static WINDOW *zcurses_WIN[ZCURSES_MAX_WINDOWS + 1];
+WINDOW *win_zero;
+LinkList zcurses_windows;
 
 #define ZCURSES_ERANGE 1
 #define ZCURSES_EDEFINED 2
@@ -52,7 +56,6 @@
 
 static int zc_errno;
 
-/**/
 static const char *
 zcurses_strerror(int err)
 {
@@ -65,94 +68,144 @@
     return errs[(err < 1 || err > 2) ? 0 : err];
 }
 
-/**/
-static unsigned
-zcurses_validate_window(char *win, int criteria)
+static LinkNode
+zcurses_getwindowbyname(char *name)
 {
-    unsigned target;
+    LinkNode node;
+    ZCWin w;
 
-    if (win==NULL) {
-	zc_errno = ZCURSES_ERANGE;
-	return -1;
-    }
+    for (node = firstnode(zcurses_windows); node; incnode(node))
+	if (w = (ZCWin)getdata(node), !strcmp(w->name, name))
+	    return node;
 
-    target = (unsigned)atoi(win);
+    return NULL;
+}
 
-    if (target > ZCURSES_MAX_WINDOWS || target < 1) {
+static LinkNode
+zcurses_validate_window(char *win, int criteria)
+{
+    LinkNode target;
+
+    if (win==NULL || strlen(win) < 1) {
 	zc_errno = ZCURSES_ERANGE;
-	return -1;
+	return NULL;
     }
 
-    if (zcurses_WIN[target]!=NULL && (criteria & ZCURSES_UNUSED)) {
+    target = zcurses_getwindowbyname(win);
+
+    if (target && (criteria & ZCURSES_UNUSED)) {
 	zc_errno = ZCURSES_EDEFINED;
-	return -1;
+	return NULL;
     }
 
-    if (zcurses_WIN[target]==NULL && (criteria & ZCURSES_USED)) {
+    if (!target && (criteria & ZCURSES_USED)) {
 	zc_errno = ZCURSES_EUNDEFINED;
-	return -1;
+	return NULL;
     }
 
+    zc_errno = 0;
     return target;
 }
 
+static int
+zcurses_free_window(ZCWin w)
+{
+    if (delwin(w->win)!=OK)
+	return 1;
+
+    if (w->name)
+	zsfree(w->name);
+
+    zfree(w, sizeof(struct zc_win));
+
+    return 0;
+}
+
 /**/
 static int
 bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
 {
-    unsigned targetwin;
-
     if (OPT_ISSET(ops,'a')) {
 	int nlines, ncols, begin_y, begin_x;
+        ZCWin w;
+
+	if (zcurses_validate_window(args[0], ZCURSES_UNUSED) == NULL && zc_errno) {
+	    zerrnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
+	    return 1;
+	}
 
-	targetwin = zcurses_validate_window(args[0], ZCURSES_UNUSED);
 	nlines = atoi(args[1]);
 	ncols = atoi(args[2]);
 	begin_y = atoi(args[3]);
 	begin_x = atoi(args[4]);
 
-	if (targetwin == -1) {
-	    zerrnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
+        w = (ZCWin)zshcalloc(sizeof(struct zc_win));
+        if (!w)
 	    return 1;
-	}
 
-	zcurses_WIN[targetwin]=newwin(nlines, ncols, begin_y, begin_x);
+        w->name = ztrdup(args[0]);
+	w->win = newwin(nlines, ncols, begin_y, begin_x);
 
-	if (zcurses_WIN[targetwin]==NULL)
+	if (w->win == NULL) {
+	    zsfree(w->name);
+	    free(w);
 	    return 1;
+	}
+
+        zinsertlinknode(zcurses_windows, lastnode(zcurses_windows), (void *)w);
 
 	return 0;
     }
 
     if (OPT_ISSET(ops,'d')) {
-	targetwin = zcurses_validate_window(OPT_ARG(ops,'d'), ZCURSES_USED);
-	if (targetwin == -1) {
+	LinkNode node;
+	ZCWin w;
+
+	node = zcurses_validate_window(OPT_ARG(ops,'d'), ZCURSES_USED);
+	if (node == NULL) {
 	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), OPT_ARG(ops,'d'), 0);
 	    return 1;
 	}
 
-	if (delwin(zcurses_WIN[targetwin])!=OK)
-		return 1;
+	w = (ZCWin)getdata(node);
+
+	if (w == NULL) {
+	    zwarnnam(nam, "record for window `%s' is corrupt", OPT_ARG(ops, 'd'), 0);
+	    return 1;
+	}
+	if (delwin(w->win)!=OK)
+	    return 1;
+
+	if (w->name)
+	    zsfree(w->name);
+
+        remnode(zcurses_windows, node);
 
-	zcurses_WIN[targetwin]=NULL;
 	return 0;
     }
 
     if (OPT_ISSET(ops,'r')) {
-	targetwin = zcurses_validate_window(OPT_ARG(ops,'r'), ZCURSES_USED);
-	if (targetwin == -1) {
+	LinkNode node;
+	ZCWin w;
+
+	node = zcurses_validate_window(OPT_ARG(ops,'r'), ZCURSES_USED);
+	if (node == NULL) {
 	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), OPT_ARG(ops,'r'), 0);
 	    return 1;
 	}
 
-	return (wrefresh(zcurses_WIN[targetwin])!=OK) ? 1 : 0;
+	w = (ZCWin)getdata(node);
+
+	return (wrefresh(w->win)!=OK) ? 1 : 0;
     }
 
     if (OPT_ISSET(ops,'m')) {
 	int y, x;
+	LinkNode node;
+	ZCWin w;
 
-	targetwin = zcurses_validate_window(args[0], ZCURSES_USED);
-	if (targetwin == -1) {
+	node = zcurses_validate_window(args[0], ZCURSES_USED);
+	if (node == NULL) {
 	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
 	    return 1;
 	}
@@ -160,24 +213,30 @@
 	y = atoi(args[1]);
 	x = atoi(args[2]);
 
-	if (wmove(zcurses_WIN[targetwin], y, x)!=OK)
+	w = (ZCWin)getdata(node);
+
+	if (wmove(w->win, y, x)!=OK)
 	    return 1;
 
 	return 0;
     }
 
     if (OPT_ISSET(ops,'c')) {
+	LinkNode node;
+	ZCWin w;
 #ifdef HAVE_SETCCHAR
 	wchar_t c;
 	cchar_t cc;
 #endif
 
-	targetwin = zcurses_validate_window(args[0], ZCURSES_USED);
-	if (targetwin == -1) {
+	node = zcurses_validate_window(args[0], ZCURSES_USED);
+	if (node == NULL) {
 	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
 	    return 1;
 	}
 
+	w = (ZCWin)getdata(node);
+
 #ifdef HAVE_SETCCHAR
 	if (mbrtowc(&c, args[1], MB_CUR_MAX, NULL) < 1)
 	    return 1;
@@ -185,10 +244,10 @@
 	if (setcchar(&cc, &c, A_NORMAL, 0, NULL)==ERR)
 	    return 1;
 
-	if (wadd_wch(zcurses_WIN[targetwin], &cc)!=OK)
+	if (wadd_wch(w->win, &cc)!=OK)
 	    return 1;
 #else
-	if (waddch(zcurses_WIN[targetwin], (chtype)args[1][0])!=OK)
+	if (waddch(w->win, (chtype)args[1][0])!=OK)
 	    return 1;
 #endif
 
@@ -196,18 +255,23 @@
     }
 
     if (OPT_ISSET(ops,'s')) {
+	LinkNode node;
+	ZCWin w;
+
 #ifdef HAVE_SETCCHAR
 	wchar_t *ws;
 	cchar_t *wcc;
 	size_t sl;
 #endif
 
-	targetwin = zcurses_validate_window(args[0], ZCURSES_USED);
-	if (targetwin == -1) {
+	node = zcurses_validate_window(args[0], ZCURSES_USED);
+	if (node == NULL) {
 	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
 	    return 1;
 	}
 
+	w = (ZCWin)getdata(node);
+
 #ifdef HAVE_SETCCHAR
 	sl = strlen(args[1]);
 
@@ -230,28 +294,32 @@
 
 	free(ws);
 
-	if (wadd_wchstr(zcurses_WIN[targetwin], wcc)!=OK) {
+	if (wadd_wchstr(w->win, wcc)!=OK) {
 	    free(wcc);
 	    return 1;
 	}
 
 	free(wcc);
 #else
-	if (waddstr(zcurses_WIN[targetwin], args[1])!=OK)
+	if (waddstr(w->win, args[1])!=OK)
 	    return 1;
 #endif
 	return 0;
     }
 
     if (OPT_ISSET(ops,'b')) {
+	LinkNode node;
+	ZCWin w;
 
-	targetwin = zcurses_validate_window(OPT_ARG(ops,'b'), ZCURSES_USED);
-	if (targetwin == -1) {
+	node = zcurses_validate_window(OPT_ARG(ops,'b'), ZCURSES_USED);
+	if (node == NULL) {
 	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), OPT_ARG(ops,'b'), 0);
 	    return 1;
 	}
 
-	if (wborder(zcurses_WIN[targetwin], 0, 0, 0, 0, 0, 0, 0, 0)!=OK)
+	w = (ZCWin)getdata(node);
+
+	if (wborder(w->win, 0, 0, 0, 0, 0, 0, 0, 0)!=OK)
 	    return 1;
 
 	return 0;
@@ -302,11 +370,8 @@
 int
 boot_(Module m)
 {
-    int i;
-    for(i=1;i<=ZCURSES_MAX_WINDOWS;i++)
-	zcurses_WIN[i]=NULL;
-
-    zcurses_WIN[0]=initscr();
+    zcurses_windows = znewlinklist();
+    win_zero=initscr();
 
     return 0;
 }
@@ -315,6 +380,7 @@
 int
 cleanup_(Module m)
 {
+    freelinklist(zcurses_windows, (FreeFunc) zcurses_free_window);
     return setfeatureenables(m, &module_features, NULL);
 }
 


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: PATCH: make curses module not need wideness
  2007-10-15 15:04         ` Bart Schaefer
@ 2007-10-15 16:48           ` Clint Adams
  0 siblings, 0 replies; 12+ messages in thread
From: Clint Adams @ 2007-10-15 16:48 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

On Mon, Oct 15, 2007 at 08:04:17AM -0700, Bart Schaefer wrote:
> (What ncurses version do you have?  My RPM version is ncurses-5.4-13)

Tested with these on Debian:

||/ Name                                 Version
+++-====================================-==============
ii  libncurses5                          5.6+20070908-1
ii  libncurses5-dev                      5.6+20070908-1
ii  libncursesw5                         5.6+20070908-1
ii  libncursesw5-dev                     5.6+20070908-1


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2007-10-15 16:48 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-14  4:21 PATCH: make curses module not need wideness Clint Adams
2007-10-14 16:26 ` Bart Schaefer
2007-10-14 17:08   ` Clint Adams
2007-10-14 22:16     ` Bart Schaefer
2007-10-15  9:11       ` Peter Stephenson
2007-10-15 13:35         ` Clint Adams
2007-10-15 14:34         ` Clint Adams
2007-10-15 14:58           ` Peter Stephenson
2007-10-15 16:45             ` Clint Adams
2007-10-15 13:25       ` Clint Adams
2007-10-15 15:04         ` Bart Schaefer
2007-10-15 16:48           ` Clint Adams

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).