From: Alan Cox Date: Fri, 21 Aug 2015 20:21:50 +0000 (+0100) Subject: curses: small cleanups and fixme notes X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=71fd23c4e753a04e21799eca0d6c54c082f92693;p=FUZIX.git curses: small cleanups and fixme notes --- diff --git a/Library/libs/curses/cursesio.c b/Library/libs/curses/cursesio.c index 2b63e27d..bb31dc6d 100644 --- a/Library/libs/curses/cursesio.c +++ b/Library/libs/curses/cursesio.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "curspriv.h" @@ -39,14 +40,16 @@ char *vb; /* visual bell */ /* fatal - report error and die. Never returns */ void fatal(char *s) { - (void) fprintf(stderr, "curses: %s\n", s); + write(2, "curses: ", 8); + write(2, s, strlen(s)); + write(2, "\n", 1); exit(1); } /* Outc - call putchar, necessary because putchar is a macro. */ -void outc(int c) +int outc(int c) { - putchar(c); + return putchar(c); } /* Move cursor to r,c */ @@ -126,7 +129,7 @@ unsigned int _cursgraftable[27] = '>', '<', 'v', '^', '#', ':', ' ', '#', '+', '\'', '#', '+', '+', '+', '+', '+', '-', ' ', '-', ' ', '_', '+', '+', '+', '+', '|' }; -char _cursident[28] = "+,.-0ahI`fgjklmnopqrstuvwx~"; +unsigned char _cursident[28] = "+,.-0ahI`fgjklmnopqrstuvwx~"; int setterm(char *type) { diff --git a/Library/libs/curses/doupdt.c b/Library/libs/curses/doupdt.c index 08db7fc6..8f8ee7be 100644 --- a/Library/libs/curses/doupdt.c +++ b/Library/libs/curses/doupdt.c @@ -10,11 +10,11 @@ static WINDOW *twin; /* used by many routines */ /* Time to optimize than to do things directly. */ /****************************************************************/ -_PROTOTYPE(static void gotoxy, (int row, int col )); -_PROTOTYPE(static void newattr, (int ch )); -_PROTOTYPE(static void Putchar, (int ch )); -_PROTOTYPE(static void clrupdate, (WINDOW *scr )); -_PROTOTYPE(static void transformline, (int lineno )); +static void gotoxy(int row, int col ); +static void newattr(int ch ); +static void Putchar(int ch ); +static void clrupdate(WINDOW *scr ); +static void transformline(int lineno); static void gotoxy(int row, int col) { @@ -48,6 +48,9 @@ static void newattr(int ch) /* Putchar() writes a character, with attributes, to the physical screen, but avoids writing to the lower right screen position. Should it care about am? + + FIXME: try and move away from sucking in stdio in curses and in + termcap */ /* Output char with attribute */ @@ -145,7 +148,7 @@ static void transformline(register int lineno) /* Updates the screen to look like curscr. */ /****************************************************************/ -void doupdate() +void doupdate(void) { int i; diff --git a/Library/libs/curses/endwin.c b/Library/libs/curses/endwin.c index af167c29..e3a68865 100644 --- a/Library/libs/curses/endwin.c +++ b/Library/libs/curses/endwin.c @@ -2,7 +2,7 @@ #include "curspriv.h" #include -int endwin() +int endwin(void) { extern char *me; diff --git a/Library/libs/curses/initscr.c b/Library/libs/curses/initscr.c index 84df5cef..d4a70a18 100644 --- a/Library/libs/curses/initscr.c +++ b/Library/libs/curses/initscr.c @@ -4,7 +4,7 @@ #include #include "curspriv.h" -WINDOW *initscr() +WINDOW *initscr(void) { char *term; diff --git a/Library/libs/curses/newwin.c b/Library/libs/curses/newwin.c index 918ee871..39fb8324 100644 --- a/Library/libs/curses/newwin.c +++ b/Library/libs/curses/newwin.c @@ -7,8 +7,6 @@ /* Actual lines themselves. */ /****************************************************************/ -_PROTOTYPE(static WINDOW *makenew, (int nlines, int ncols, int begy,int begx)); - static WINDOW *makenew(int num_lines, int num_columns, int begy, int begx) { int i; diff --git a/Library/libs/curses/prnt.c b/Library/libs/curses/prnt.c new file mode 100644 index 00000000..15f06a8c --- /dev/null +++ b/Library/libs/curses/prnt.c @@ -0,0 +1,61 @@ +#include +#include +#include "curspriv.h" + +extern char __printscanbuf[513]; /* buffer used during I/O */ + +/****************************************************************/ +/* Wprintw(win,fmt,args) does a printf() in window 'win'. */ +/****************************************************************/ +int wprintw(WINDOW *win, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + vsnprintf(__printscanbuf, sizeof(__printscanbuf), fmt, args); + if (waddstr(win, __printscanbuf) == ERR) return(ERR); + return(strlen(__printscanbuf)); +} + +/****************************************************************/ +/* Printw(fmt,args) does a printf() in stdscr. */ +/****************************************************************/ +int printw(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + vsnprintf(__printscanbuf, sizeof(__printscanbuf), fmt, args); + if (waddstr(stdscr, __printscanbuf) == ERR) return(ERR); + return(strlen(__printscanbuf)); +} /* printw */ + +/****************************************************************/ +/* Mvprintw(fmt,args) moves the stdscr cursor to a new posi- */ +/* tion, then does a printf() in stdscr. */ +/****************************************************************/ +int mvprintw(int y, int x, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + if (wmove(stdscr, y, x) == ERR) return(ERR); + vsnprintf(__printscanbuf, sizeof(__printscanbuf), fmt, args); + if (waddstr(stdscr, __printscanbuf) == ERR) return(ERR); + return(strlen(__printscanbuf)); +} + +/****************************************************************/ +/* Mvwprintw(win,fmt,args) moves the window 'win's cursor to */ +/* A new position, then does a printf() in window 'win'. */ +/****************************************************************/ +int mvwprintw(WINDOW *win, int y, int x, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + if (wmove(win, y, x) == ERR) return(ERR); + vsnprintf(__printscanbuf, sizeof(__printscanbuf), fmt, args); + if (waddstr(win, __printscanbuf) == ERR) return(ERR); + return(strlen(__printscanbuf)); +} /* mvwprintw */ diff --git a/Library/libs/curses/prntscan.c b/Library/libs/curses/prntscan.c index 80257f1b..45695c1a 100644 --- a/Library/libs/curses/prntscan.c +++ b/Library/libs/curses/prntscan.c @@ -2,128 +2,10 @@ #include #include "curspriv.h" -static char printscanbuf[513]; /* buffer used during I/O */ +/* These functions are expensive so keep them in their own files and + don't suck them in for anything else. + + Really we should probably use the internal snprintf helpers and + remove the length limit and buffer but that's a FIXME */ -/****************************************************************/ -/* Wprintw(win,fmt,args) does a printf() in window 'win'. */ -/****************************************************************/ -int wprintw(WINDOW *win, const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - vsprintf(printscanbuf, fmt, args); - if (waddstr(win, printscanbuf) == ERR) return(ERR); - return(strlen(printscanbuf)); -} - -/****************************************************************/ -/* Printw(fmt,args) does a printf() in stdscr. */ -/****************************************************************/ -int printw(const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - vsprintf(printscanbuf, fmt, args); - if (waddstr(stdscr, printscanbuf) == ERR) return(ERR); - return(strlen(printscanbuf)); -} /* printw */ - -/****************************************************************/ -/* Mvprintw(fmt,args) moves the stdscr cursor to a new posi- */ -/* tion, then does a printf() in stdscr. */ -/****************************************************************/ -int mvprintw(int y, int x, const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - if (wmove(stdscr, y, x) == ERR) return(ERR); - vsprintf(printscanbuf, fmt, args); - if (waddstr(stdscr, printscanbuf) == ERR) return(ERR); - return(strlen(printscanbuf)); -} - -/****************************************************************/ -/* Mvwprintw(win,fmt,args) moves the window 'win's cursor to */ -/* A new position, then does a printf() in window 'win'. */ -/****************************************************************/ -int mvwprintw(WINDOW *win, int y, int x, const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - if (wmove(win, y, x) == ERR) return(ERR); - vsprintf(printscanbuf, fmt, args); - if (waddstr(win, printscanbuf) == ERR) return(ERR); - return(strlen(printscanbuf)); -} /* mvwprintw */ - -/****************************************************************/ -/* Wscanw(win,fmt,args) gets a string via window 'win', then */ -/* Scans the string using format 'fmt' to extract the values */ -/* And put them in the variables pointed to the arguments. */ -/****************************************************************/ -int wscanw(WINDOW *win, const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - wrefresh(win); /* set cursor */ - if (wgetstr(win, printscanbuf) == ERR) /* get string */ - return(ERR); - return(vsscanf(printscanbuf, fmt, args)); -} /* wscanw */ - -/****************************************************************/ -/* Scanw(fmt,args) gets a string via stdscr, then scans the */ -/* String using format 'fmt' to extract the values and put them */ -/* In the variables pointed to the arguments. */ -/****************************************************************/ -int scanw(const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - wrefresh(stdscr); /* set cursor */ - if (wgetstr(stdscr, printscanbuf) == ERR) /* get string */ - return(ERR); - return(vsscanf(printscanbuf, fmt, args)); -} /* scanw */ - -/****************************************************************/ -/* Mvscanw(y,x,fmt,args) moves stdscr's cursor to a new posi- */ -/* Tion, then gets a string via stdscr and scans the string */ -/* Using format 'fmt' to extract the values and put them in the */ -/* Variables pointed to the arguments. */ -/****************************************************************/ -int mvscanw(int y, int x, const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - if (wmove(stdscr, y, x) == ERR) return(ERR); - wrefresh(stdscr); /* set cursor */ - if (wgetstr(stdscr, printscanbuf) == ERR) /* get string */ - return(ERR); - return(vsscanf(printscanbuf, fmt, args)); -} /* mvscanw */ - -/****************************************************************/ -/* Mvwscanw(win,y,x,fmt,args) moves window 'win's cursor to a */ -/* New position, then gets a string via 'win' and scans the */ -/* String using format 'fmt' to extract the values and put them */ -/* In the variables pointed to the arguments. */ -/****************************************************************/ -int mvwscanw(WINDOW *win, int y, int x, const char *fmt, ...) -{ - va_list args; - - va_start(args, fmt); - if (wmove(win, y, x) == ERR) return(ERR); - wrefresh(win); /* set cursor */ - if (wgetstr(win, printscanbuf) == ERR) /* get string */ - return(ERR); - return(vsscanf(printscanbuf, fmt, args)); -} /* mvwscanw */ +char __printscanbuf[513]; /* buffer used during I/O */ diff --git a/Library/libs/curses/scan.c b/Library/libs/curses/scan.c new file mode 100644 index 00000000..886bc954 --- /dev/null +++ b/Library/libs/curses/scan.c @@ -0,0 +1,73 @@ +#include +#include +#include "curspriv.h" + +extern char __printscanbuf[513]; /* buffer used during I/O */ + +/****************************************************************/ +/* Wscanw(win,fmt,args) gets a string via window 'win', then */ +/* Scans the string using format 'fmt' to extract the values */ +/* And put them in the variables pointed to the arguments. */ +/****************************************************************/ +int wscanw(WINDOW *win, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + wrefresh(win); /* set cursor */ + if (wgetstr(win, __printscanbuf) == ERR) /* get string */ + return(ERR); + return(vsscanf(__printscanbuf, fmt, args)); +} /* wscanw */ + +/****************************************************************/ +/* Scanw(fmt,args) gets a string via stdscr, then scans the */ +/* String using format 'fmt' to extract the values and put them */ +/* In the variables pointed to the arguments. */ +/****************************************************************/ +int scanw(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + wrefresh(stdscr); /* set cursor */ + if (wgetstr(stdscr, __printscanbuf) == ERR) /* get string */ + return(ERR); + return(vsscanf(__printscanbuf, fmt, args)); +} /* scanw */ + +/****************************************************************/ +/* Mvscanw(y,x,fmt,args) moves stdscr's cursor to a new posi- */ +/* Tion, then gets a string via stdscr and scans the string */ +/* Using format 'fmt' to extract the values and put them in the */ +/* Variables pointed to the arguments. */ +/****************************************************************/ +int mvscanw(int y, int x, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + if (wmove(stdscr, y, x) == ERR) return(ERR); + wrefresh(stdscr); /* set cursor */ + if (wgetstr(stdscr, __printscanbuf) == ERR) /* get string */ + return(ERR); + return(vsscanf(__printscanbuf, fmt, args)); +} /* mvscanw */ + +/****************************************************************/ +/* Mvwscanw(win,y,x,fmt,args) moves window 'win's cursor to a */ +/* New position, then gets a string via 'win' and scans the */ +/* String using format 'fmt' to extract the values and put them */ +/* In the variables pointed to the arguments. */ +/****************************************************************/ +int mvwscanw(WINDOW *win, int y, int x, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + if (wmove(win, y, x) == ERR) return(ERR); + wrefresh(win); /* set cursor */ + if (wgetstr(win, __printscanbuf) == ERR) /* get string */ + return(ERR); + return(vsscanf(__printscanbuf, fmt, args)); +} /* mvwscanw */ diff --git a/Library/libs/curses/setterm.c b/Library/libs/curses/setterm.c index ac3c3092..75225b69 100644 --- a/Library/libs/curses/setterm.c +++ b/Library/libs/curses/setterm.c @@ -1,9 +1,7 @@ #include #include "curspriv.h" -_PROTOTYPE( static void ttysetflags, (void) ); - -static void ttysetflags() +static void ttysetflags(void) { _tty.c_iflag |= ICRNL | IXON; _tty.c_oflag |= OPOST | ONLCR; @@ -27,49 +25,49 @@ static void ttysetflags() tcsetattr(0, TCSANOW, &_tty); } /* ttysetflags */ -void raw() +void raw(void) { _cursvar.rawmode = TRUE; ttysetflags(); } /* raw */ -void noraw() +void noraw(void) { _cursvar.rawmode = FALSE; ttysetflags(); } /* noraw */ -void echo() +void echo(void) { _cursvar.echoit = TRUE; ttysetflags(); } -void noecho() +void noecho(void) { _cursvar.echoit = FALSE; ttysetflags(); } -void nl() +void nl(void) { NONL = FALSE; ttysetflags(); } /* nl */ -void nonl() +void nonl(void) { NONL = TRUE; ttysetflags(); } /* nonl */ -void cbreak() +void cbreak(void) { _cursvar.cbrkmode = TRUE; ttysetflags(); } /* cbreak */ -void nocbreak() +void nocbreak(void) { _cursvar.cbrkmode = FALSE; ttysetflags(); diff --git a/Library/libs/curses/tparam.c b/Library/libs/curses/tparam.c deleted file mode 100644 index 66f91b5b..00000000 --- a/Library/libs/curses/tparam.c +++ /dev/null @@ -1,270 +0,0 @@ -/* tparam.c - * Merge parameters into a termcap entry string. - * Copyright (C) 1985, 1987, 1993 Free Software Foundation, Inc. - */ -#include -#include -#include -#include -#include - -static void memory_out __P((void)); -static char *xmalloc __P((unsigned size)); -static char *xrealloc __P((char *ptr, unsigned size)); -static char *tparam1 __P((char *string, char *outstring, int len, char *up, char *left, int *argp)); - -static void memory_out() { - write(2, "virtual memory exhausted\n", 25); - exit(1); -} - -static char *xmalloc(unsigned size) -{ - register char *tem = malloc(size); - - if (!tem) - memory_out(); - return tem; -} - -static char *xrealloc(char *ptr, unsigned size) -{ - register char *tem = realloc(ptr, size); - - if (!tem) - memory_out(); - return tem; -} - -/* Assuming STRING is the value of a termcap string entry - containing `%' constructs to expand parameters, - merge in parameter values and store result in block OUTSTRING points to. - LEN is the length of OUTSTRING. If more space is needed, - a block is allocated with `malloc'. - - The value returned is the address of the resulting string. - This may be OUTSTRING or may be the address of a block got with `malloc'. - In the latter case, the caller must free the block. - - The fourth and following args to tparam serve as the parameter values. - */ -/* VARARGS 2 */ -char * tparam(char *string, char *outstring, int len, int arg0, int arg1, int arg2,int arg3) -{ -#ifdef NO_ARG_ARRAY - int arg[4]; - arg[0] = arg0; - arg[1] = arg1; - arg[2] = arg2; - arg[3] = arg3; - return tparam1(string, outstring, len, NULL, NULL, arg); -#else - (void)arg1; (void)arg2; (void)arg3; - return tparam1(string, outstring, len, NULL, NULL, &arg0); -#endif -} - -char *BC; -char *UP; - -static char tgoto_buf[50]; - -char *tgoto(char *cm, int hpos, int vpos) -{ - int args[2]; - - if (!cm) - return NULL; - args[0] = vpos; - args[1] = hpos; - return tparam1(cm, tgoto_buf, 50, UP, BC, args); -} - -static char *tparam1(char *string, char *outstring, int len, char *up, char *left, register int *argp) -{ - register int c, tem; - register char *p = string; - register char *op = outstring; - int *old_argp = argp; - int outlen = 0; - int doleft = 0; - int doup = 0; - char *outend = outstring + len; - - while (1) { - /* If the buffer might be too short, make it bigger. */ - if (op + 5 >= outend) { - register char *new; - if (outlen == 0) { - outlen = len + 40; - new = (char *) xmalloc(outlen); - outend += 40; - memcpy(new, outstring, op - outstring); - } - else { - outend += outlen; - outlen *= 2; - new = (char *) xrealloc(outstring, outlen); - } - op += new -outstring; - outend += new -outstring; - outstring = new; - } - c = *p++; - if (!c) - break; - if (c == '%') { - c = *p++; - tem = *argp; - switch (c) { - case 'd': /* %d means output in decimal. */ - if (tem < 10) - goto onedigit; - if (tem < 100) - goto twodigit; - case '3': /* %3 means output in decimal, 3 digits. */ - if (tem > 999) { - *op++ = tem / 1000 + '0'; - tem %= 1000; - } - *op++ = tem / 100 + '0'; - case '2': /* %2 means output in decimal, 2 digits. */ -twodigit: - tem %= 100; - *op++ = tem / 10 + '0'; -onedigit: - *op++ = tem % 10 + '0'; - argp++; - break; - - case 'C': - /* For c-100: print quotient of value by 96, if - * nonzero, then do like %+. */ - if (tem >= 96) { - *op++ = tem / 96; - tem %= 96; - } - case '+': /* %+x means add character code of char x. */ - tem += *p++; - case '.': /* %. means output as character. */ - if (left) { - /* If want to forbid output of 0 and \n and - * \t, and this is one of them, increment - * it. */ - while (tem == 0 || tem == '\n' || tem == '\t') { - tem++; - if (argp == old_argp) - doup++, outend -= strlen(up); - else - doleft++, outend -= strlen(left); - } - } - *op++ = tem ? tem : 0200; - case 'f': /* %f means discard next arg. */ - argp++; - break; - - case 'b': /* %b means back up one arg (and re-use - * it). */ - argp--; - break; - - case 'r': /* %r means interchange following two args. */ - argp[0] = argp[1]; - argp[1] = tem; - old_argp++; - break; - - case '>': /* %>xy means if arg is > char code of x, */ - if (argp[0] > *p++) /* then add char code of y - * to the arg, */ - argp[0] += *p; /* and in any case don't - * output. */ - p++; /* Leave the arg to be output later. */ - break; - - case 'a': /* %a means arithmetic. */ - /* Next character says what operation. Add or - * subtract either a constant or some other arg. */ - /* First following character is + to add or - to - * subtract or = to assign. */ - /* Next following char is 'p' and an arg spec (0100 - * plus position of that arg relative to this one) - * or 'c' and a constant stored in a character. */ - tem = p[2] & 0177; - if (p[1] == 'p') - tem = argp[tem - 0100]; - if (p[0] == '-') - argp[0] -= tem; - else if (p[0] == '+') - argp[0] += tem; - else if (p[0] == '*') - argp[0] *= tem; - else if (p[0] == '/') - argp[0] /= tem; - else - argp[0] = tem; - - p += 3; - break; - - case 'i': /* %i means add one to arg, */ - argp[0]++; /* and leave it to be output later. */ - argp[1]++; /* Increment the following arg, - * too! */ - break; - - case '%': /* %% means output %; no arg. */ - goto ordinary; - - case 'n': /* %n means xor each of next two args with - * 140. */ - argp[0] ^= 0140; - argp[1] ^= 0140; - break; - - case 'm': /* %m means xor each of next two args with - * 177. */ - argp[0] ^= 0177; - argp[1] ^= 0177; - break; - - case 'B': /* %B means express arg as BCD char code. */ - argp[0] += 6 * (tem / 10); - break; - - case 'D': /* %D means weird Delta Data - * transformation. */ - argp[0] -= 2 * (tem % 16); - break; - } - } - else - /* Ordinary character in the argument string. */ -ordinary: - *op++ = c; - } - *op = 0; - while (doup-- > 0) - strcat(op, up); - while (doleft-- > 0) - strcat(op, left); - return outstring; -} - -#ifdef TEST - -int main(int argc, char **argv) -{ - char buf[50]; - int args[3]; - - args[0] = atoi(argv[2]); - args[1] = atoi(argv[3]); - args[2] = atoi(argv[4]); - tparam1(argv[1], buf, 50, "LEFT", "UP", args); - printf("%s\n", buf); - return 0; -} -#endif /* DEBUG */ - diff --git a/Library/libs/curses/waddch.c b/Library/libs/curses/waddch.c index 97a95dc3..ebb35350 100644 --- a/Library/libs/curses/waddch.c +++ b/Library/libs/curses/waddch.c @@ -6,8 +6,6 @@ /* If error, return -1. */ /****************************************************************/ -_PROTOTYPE( static short newline, (WINDOW *win, int lin)); - static short newline(WINDOW *win, int lin) { if (++lin > win->_regbottom) {