about summary refs log tree commit diff
path: root/Src
diff options
context:
space:
mode:
Diffstat (limited to 'Src')
-rw-r--r--Src/Modules/curses.c176
1 files changed, 110 insertions, 66 deletions
diff --git a/Src/Modules/curses.c b/Src/Modules/curses.c
index d2b85157e..aba8028e5 100644
--- a/Src/Modules/curses.c
+++ b/Src/Modules/curses.c
@@ -60,6 +60,12 @@ struct colorpairnode {
 };
 typedef struct colorpairnode *Colorpairnode;
 
+struct zcurses_subcommand {
+    struct zcurses_namenumberpair nn;
+    int minargs;
+    int maxargs;
+};
+
 static WINDOW *win_zero;
 static struct ttyinfo saved_tty_state;
 static struct ttyinfo curses_tty_state;
@@ -76,6 +82,18 @@ static HashTable zcurses_colorpairs = NULL;
 #define ZCURSES_ATTRON 1
 #define ZCURSES_ATTROFF 2
 
+#define ZCURSES_SC_INIT 1
+#define ZCURSES_SC_ADDWIN 2
+#define ZCURSES_SC_DELWIN 3
+#define ZCURSES_SC_REFRESH 4
+#define ZCURSES_SC_MOVE 5
+#define ZCURSES_SC_CHAR 6
+#define ZCURSES_SC_STRING 7
+#define ZCURSES_SC_BORDER 8
+#define ZCURSES_SC_ENDWIN 9
+#define ZCURSES_SC_ATTR 10
+#define ZCURSES_SC_COLOR 11
+
 static int zc_errno, zc_color_phase=0;
 static short next_cp=0;
 
@@ -261,8 +279,42 @@ freecolorpairnode(HashNode hn)
 static int
 bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
 {
+    char **saargs;
+    struct zcurses_subcommand *zcsc;
+    int sc;
+
+    struct zcurses_subcommand scs[] = {
+	{{"init", ZCURSES_SC_INIT}, 0, 0},
+	{{"addwin", ZCURSES_SC_ADDWIN}, 5, 5},
+	{{"delwin", ZCURSES_SC_DELWIN}, 1, 1},
+	{{"refresh", ZCURSES_SC_REFRESH}, 0, 1},
+	{{"move", ZCURSES_SC_MOVE}, 3, 3},
+	{{"c", ZCURSES_SC_CHAR}, 2, 2},
+	{{"s", ZCURSES_SC_STRING}, 2, 2},
+	{{"border", ZCURSES_SC_BORDER}, 1, 5},
+	{{"endwin", ZCURSES_SC_ENDWIN}, 1, 1},
+	{{"attr", ZCURSES_SC_ATTR}, 2, 2},
+	{{"color", ZCURSES_SC_COLOR}, 2, 2},
+	{{NULL, -1}, 0, 0}
+    };
+
+    for(zcsc = scs; zcsc->nn.name; zcsc++) {
+	if(!strcmp(args[0], zcsc->nn.name))
+	    break;
+    }
+
+    sc = zcsc->nn.number;
+
+    if (sc == -1) {
+	zwarnnam(nam, "unknown subcommand: %s", args[0], 0);
+	return 1;
+    }
+
+    /* here would be a good place to validate number of args */
+    saargs = args + 1;
+
     /* Initialise curses */
-    if (OPT_ISSET(ops,'i')) {
+    if (sc == ZCURSES_SC_INIT) {
 	if (!win_zero) {
 	    gettyinfo(&saved_tty_state);
 	    win_zero = initscr();
@@ -272,8 +324,8 @@ bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
 		zcurses_colorpairs = newhashtable(8, "zc_colorpairs", NULL);
 
 		zcurses_colorpairs->hash        = hasher;
-	        zcurses_colorpairs->emptytable  = emptyhashtable;
-	        zcurses_colorpairs->filltable   = NULL;
+		zcurses_colorpairs->emptytable  = emptyhashtable;
+		zcurses_colorpairs->filltable   = NULL;
 		zcurses_colorpairs->cmpnodes    = strcmp;
 		zcurses_colorpairs->addnode     = addhashnode;
 		zcurses_colorpairs->getnode     = gethashnode2;
@@ -290,27 +342,26 @@ bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
 	    settyinfo(&curses_tty_state);
 	}
 	return 0;
-    }
-
-    if (OPT_ISSET(ops,'a')) {
+    } else
+    if (sc == ZCURSES_SC_ADDWIN) {
 	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);
+	if (zcurses_validate_window(saargs[0], ZCURSES_UNUSED) == NULL && zc_errno) {
+	    zerrnam(nam, "%s: %s", zcurses_strerror(zc_errno), saargs[0], 0);
 	    return 1;
 	}
 
-	nlines = atoi(args[1]);
-	ncols = atoi(args[2]);
-	begin_y = atoi(args[3]);
-	begin_x = atoi(args[4]);
+	nlines = atoi(saargs[1]);
+	ncols = atoi(saargs[2]);
+	begin_y = atoi(saargs[3]);
+	begin_x = atoi(saargs[4]);
 
 	w = (ZCWin)zshcalloc(sizeof(struct zc_win));
 	if (!w)
 	    return 1;
 
-	w->name = ztrdup(args[0]);
+	w->name = ztrdup(saargs[0]);
 	w->win = newwin(nlines, ncols, begin_y, begin_x);
 
 	if (w->win == NULL) {
@@ -322,22 +373,21 @@ bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
 	zinsertlinknode(zcurses_windows, lastnode(zcurses_windows), (void *)w);
 
 	return 0;
-    }
-
-    if (OPT_ISSET(ops,'d')) {
+    } else
+    if (sc == ZCURSES_SC_DELWIN) {
 	LinkNode node;
 	ZCWin w;
 
-	node = zcurses_validate_window(OPT_ARG(ops,'d'), ZCURSES_USED);
+	node = zcurses_validate_window(saargs[0], ZCURSES_USED);
 	if (node == NULL) {
-	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), OPT_ARG(ops,'d'), 0);
+	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), saargs[0], 0);
 	    return 1;
 	}
 
 	w = (ZCWin)getdata(node);
 
 	if (w == NULL) {
-	    zwarnnam(nam, "record for window `%s' is corrupt", OPT_ARG(ops, 'd'), 0);
+	    zwarnnam(nam, "record for window `%s' is corrupt", saargs[0], 0);
 	    return 1;
 	}
 	if (delwin(w->win)!=OK)
@@ -349,16 +399,15 @@ bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
 	zfree((ZCWin)remnode(zcurses_windows, node), sizeof(struct zc_win));
 
 	return 0;
-    }
-
-    if (OPT_ISSET(ops,'r')) {
-	if (args[0]) {
+    } else
+    if (sc == ZCURSES_SC_REFRESH) {
+	if (saargs[0]) {
 	    LinkNode node;
 	    ZCWin w;
 
-	    node = zcurses_validate_window(args[0], ZCURSES_USED);
+	    node = zcurses_validate_window(saargs[0], ZCURSES_USED);
 	    if (node == NULL) {
-		zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0],
+		zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), saargs[0],
 			0);
 		return 1;
 	    }
@@ -371,21 +420,20 @@ bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
 	{
 	    return (refresh() != OK) ? 1 : 0;
 	}
-    }
-
-    if (OPT_ISSET(ops,'m')) {
+    } else
+    if (sc == ZCURSES_SC_MOVE) {
 	int y, x;
 	LinkNode node;
 	ZCWin w;
 
-	node = zcurses_validate_window(args[0], ZCURSES_USED);
+	node = zcurses_validate_window(saargs[0], ZCURSES_USED);
 	if (node == NULL) {
-	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
+	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), saargs[0], 0);
 	    return 1;
 	}
 
-	y = atoi(args[1]);
-	x = atoi(args[2]);
+	y = atoi(saargs[1]);
+	x = atoi(saargs[2]);
 
 	w = (ZCWin)getdata(node);
 
@@ -393,9 +441,8 @@ bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
 	    return 1;
 
 	return 0;
-    }
-
-    if (OPT_ISSET(ops,'c')) {
+    } else
+    if (sc == ZCURSES_SC_CHAR) {
 	LinkNode node;
 	ZCWin w;
 #ifdef HAVE_SETCCHAR
@@ -403,16 +450,16 @@ bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
 	cchar_t cc;
 #endif
 
-	node = zcurses_validate_window(args[0], ZCURSES_USED);
+	node = zcurses_validate_window(saargs[0], ZCURSES_USED);
 	if (node == NULL) {
-	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
+	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), saargs[0], 0);
 	    return 1;
 	}
 
 	w = (ZCWin)getdata(node);
 
 #ifdef HAVE_SETCCHAR
-	if (mbrtowc(&c, args[1], MB_CUR_MAX, NULL) < 1)
+	if (mbrtowc(&c, saargs[1], MB_CUR_MAX, NULL) < 1)
 	    return 1;
 
 	if (setcchar(&cc, &c, A_NORMAL, 0, NULL)==ERR)
@@ -421,14 +468,13 @@ bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
 	if (wadd_wch(w->win, &cc)!=OK)
 	    return 1;
 #else
-	if (waddch(w->win, (chtype)args[1][0])!=OK)
+	if (waddch(w->win, (chtype)saargs[1][0])!=OK)
 	    return 1;
 #endif
 
 	return 0;
-    }
-
-    if (OPT_ISSET(ops,'s')) {
+    } else
+    if (sc == ZCURSES_SC_STRING) {
 	LinkNode node;
 	ZCWin w;
 
@@ -436,10 +482,10 @@ bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
 	int clen;
 	wint_t wc;
 	wchar_t *wstr, *wptr;
-	char *str = args[1];
+	char *str = saargs[1];
 #endif
 
-	node = zcurses_validate_window(args[0], ZCURSES_USED);
+	node = zcurses_validate_window(saargs[0], ZCURSES_USED);
 	if (node == NULL) {
 	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
 	    return 1;
@@ -462,19 +508,18 @@ bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
 	    return 1;
 	}
 #else
-	if (waddstr(w->win, args[1])!=OK)
+	if (waddstr(w->win, saargs[1])!=OK)
 	    return 1;
 #endif
 	return 0;
-    }
-
-    if (OPT_ISSET(ops,'b')) {
+    } else
+    if (sc == ZCURSES_SC_BORDER) {
 	LinkNode node;
 	ZCWin w;
 
-	node = zcurses_validate_window(OPT_ARG(ops,'b'), ZCURSES_USED);
+	node = zcurses_validate_window(saargs[0], ZCURSES_USED);
 	if (node == NULL) {
-	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), OPT_ARG(ops,'b'), 0);
+	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), saargs[0], 0);
 	    return 1;
 	}
 
@@ -484,10 +529,9 @@ bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
 	    return 1;
 
 	return 0;
-    }
-
+    } else
     /* Finish using curses */
-    if (OPT_ISSET(ops,'e')) {
+    if (sc == ZCURSES_SC_ENDWIN) {
 	if (win_zero) {
 	    endwin();
 	    /* Restore TTY as it was before zcurses -i */
@@ -500,24 +544,24 @@ bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
 	    gettyinfo(&shttyinfo);
 	}
 	return 0;
-    }
-    if (OPT_ISSET(ops,'A')) {
+    } else
+    if (sc == ZCURSES_SC_ATTR) {
 	LinkNode node;
 	ZCWin w;
 	char **attrs;
 
-	if (!args[0])
+	if (!saargs[0])
 	    return 1;
 
-	node = zcurses_validate_window(args[0], ZCURSES_USED);
+	node = zcurses_validate_window(saargs[0], ZCURSES_USED);
 	if (node == NULL) {
-	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
+	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), saargs[0], 0);
 	    return 1;
 	}
 
 	w = (ZCWin)getdata(node);
 
-	for(attrs = args+1; *attrs; attrs++) {
+	for(attrs = saargs+1; *attrs; attrs++) {
 	    switch(*attrs[0]) {
 		case '-':
 		    zcurses_attribute(w->win, (*attrs)+1, ZCURSES_ATTROFF);
@@ -531,23 +575,23 @@ bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
 	    }
 	}
 	return 0;
-    }
-    if (OPT_ISSET(ops,'C')) {
+    } else
+    if (sc == ZCURSES_SC_COLOR) {
 	LinkNode node;
 	ZCWin w;
 
-	if (!args[0] || !args[1] || !zc_color_phase)
+	if (!saargs[0] || !saargs[1] || !zc_color_phase)
 	    return 1;
 
-	node = zcurses_validate_window(args[0], ZCURSES_USED);
+	node = zcurses_validate_window(saargs[0], ZCURSES_USED);
 	if (node == NULL) {
-	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), args[0], 0);
+	    zwarnnam(nam, "%s: %s", zcurses_strerror(zc_errno), saargs[0], 0);
 	    return 1;
 	}
 
 	w = (ZCWin)getdata(node);
 
-	return zcurses_colorset(w->win, args[1]);
+	return zcurses_colorset(w->win, saargs[1]);
     }
 
     return 0;
@@ -558,7 +602,7 @@ bin_zcurses(char *nam, char **args, Options ops, UNUSED(int func))
  */
 
 static struct builtin bintab[] = {
-    BUILTIN("zcurses", 0, bin_zcurses, 0, 5, 0, "Aab:Ccd:eimrs", NULL),
+    BUILTIN("zcurses", 0, bin_zcurses, 1, 6, 0, "", NULL),
 };
 
 static struct features module_features = {