From ec215ba5ca7f77701fe0cbec3b2b0c3dbf18aaa3 Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Fri, 26 Apr 2024 16:58:58 +0200 Subject: [PATCH] initial commit --- .gitignore | 5 + appli.c | 148 +++++++++ cmd.c | 111 +++++++ cmd.h | 14 + getcomments.pl | 85 +++++ makefile | 146 +++++++++ tui.c | 827 +++++++++++++++++++++++++++++++++++++++++++++++++ tui.h | 68 ++++ 8 files changed, 1404 insertions(+) create mode 100644 .gitignore create mode 100644 appli.c create mode 100644 cmd.c create mode 100644 cmd.h create mode 100644 getcomments.pl create mode 100644 makefile create mode 100644 tui.c create mode 100644 tui.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..214731f --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.d +*.ld +*.exe +*.log +*.o diff --git a/appli.c b/appli.c new file mode 100644 index 0000000..9020aad --- /dev/null +++ b/appli.c @@ -0,0 +1,148 @@ +/* depend: */ +/* cflags: */ +/* linker: cmd.o tui.o -lpdcurses */ + +#include +#include +#include +#include + +#include "cmd.h" +#include "tui.h" + +int display (char **lines, int xmax, int ymax) +{ + int stop = 0; + + statusmsg ("Press 'q' or escape to quit"); + + int skip = 0; + while (!stop) { + clsbody (); + int i = 0; + int eol = 0; + for (i = 0; i <= ymax; i++) { + if (lines [skip + i] == NULL) { + break; + } + if (eol) { + bodymsg ("\n"); + } + bodymsg (lines[skip + i]); + eol = (lines[skip + i][xmax - 1 ] == '\0'); + } + int key = waitforkey (); + switch (key) { + case 'q': + case KEY_ESC: + stop = 1; + break; + case 'i': + case KEY_UP: + skip = (skip > 0) ? skip - 1 : skip; + break; + case 'k': + case KEY_DOWN: + skip = (lines[skip + 1] != NULL) ? skip + 1 : skip; + break; + case ALT_F: + case ALT_H: + stop = 2; + break; + } + } + + rmstatus(); + + return (stop == 1); +} + +int view (char *cmd) +{ + char *buffer; + + int xmax, ymax; + WINDOW *wbody = bodywin (); + getmaxyx (wbody, ymax, xmax); + + buffer = exec_cmd (cmd); + if (buffer == NULL) { + char msg[80]; + sprintf (msg, "Error executing %s", cmd); + statusmsg (msg); + return 1; + } + + char **lines = split_lines (buffer, xmax); + free (buffer); + + int rc = display (lines, xmax, ymax); + free_lines (lines); + + return rc; +} + +void exec_ipconfig (void) +{ + static int index = 0; + + switch (index) { + case 0: view ("ipconfig"); break; + case 1: view ("ps aux"); break; + case 2: view ("ls"); break; + } + index = (index + 1) % 3; +} + +menu SubMenu0[] = +{ + { "Refresh", exec_ipconfig, "Various shell commands" }, + { "Exit", DoExit, "Terminate program" }, + { "", (FUNC)0, "" } +}; + +void sub0 (void) +{ + domenu (SubMenu0); +} + +menu SubMenu1[] = +{ + { "About", DoExit, "About..." }, + { "", (FUNC)0, "" } +}; + +void sub1 (void) +{ + domenu (SubMenu1); +} + +menu MainMenu[] = +{ + { "File", sub0, "File menu" }, + { "Help", sub1, "Help menu" }, + { "", (FUNC)0, "" } /* always add this as the last item! */ +}; + +void init (void) +{ + int rc = view ("ipconfig"); + if (!rc) { + mainmenu (MainMenu); + } +} + +int main (int argc, char *argv[]) +{ + char titre[256] = {0}; + + sprintf (titre, "Application: %s", (argc > 1) ? argv[1] : "demonstration program"); + + //setlocale (LC_ALL, ""); + + startmenu (MainMenu, titre, init); + + return 0; +} + +/* vim: set ts=4 sw=4 et: */ diff --git a/cmd.c b/cmd.c new file mode 100644 index 0000000..c8c9531 --- /dev/null +++ b/cmd.c @@ -0,0 +1,111 @@ +//#include +//#include +//#include +//#include + +#include "cmd.h" + +#define BUFFERSIZE 4096 + +static char *_load_file (FILE *fd) +{ + char *buffer = NULL; + size_t size = 0; + do { + size += BUFFERSIZE + (size == 0); + buffer = (char *) realloc (buffer, size); + memset (buffer + size - BUFFERSIZE - 1, 0, BUFFERSIZE + 1); + fread (buffer + size - BUFFERSIZE - 1, 1, BUFFERSIZE, fp); + } while (!feof (fp)); + + return buffer; +} + +char *exec_cmd (char *cmd) +{ + int status = -1; + char *buffer = NULL; + + FILE *fp = popen (cmd, "r"); + if (fp != NULL) { + buffer = _load_file (fp); + status = pclose(fp); + } + + if (status == -1) { + free (buffer); + buffer = NULL; + } + + return buffer; +} + +char *load_cmd (char *name) +{ + int status = -1; + char *buffer = NULL; + + FILE *fd = fopen (name, "r"); + if (fd != NULL) { + buffer = _load_file (fd); + status = fclose (fd); + } + + if (status == -1) { + free (buffer); + buffer = NULL; + } + + return buffer; +} + +char *read_stdin (void) +{ + int status = -1; + char *buffer = NULL; + + buffer = _load_file (stdin); + + return buffer; +} + +char **split_lines (char *buffer, int max) +{ + int n = 0; + + char **lines = NULL; + int i, j; + char *line = (char *) calloc (max + 1, sizeof (char)); + for (i = j = 0; buffer[i] != '\0'; i++) { + if (buffer[i] >= ' ') { + line[j++] = buffer[i]; + } + if ((j == max) || (buffer[i] == '\n')) { + lines = (char **) realloc (lines, (n + 2) * sizeof (char *)); + lines[n] = line; + lines[n + 1] = NULL; + line = calloc (max + 1, sizeof (char)); + n++; + j = 0; + } + } + if (line[0] != '\0') { + lines = (char **) realloc (lines, (n + 2) * sizeof (char *)); + lines[n] = line; + } else { + free (line); + } + + return lines; +} + +void free_lines (char **lines) +{ + int i = 0; + while (lines[i]) { + free (lines[i++]); + } + free (lines); + + return rc; +} diff --git a/cmd.h b/cmd.h new file mode 100644 index 0000000..407f126 --- /dev/null +++ b/cmd.h @@ -0,0 +1,14 @@ +#ifndef __CMD_H__ +#define __CMD_H__ + +char *exec_cmd (char *cmd); + +char *load_file (char *file); + +char *read_stdin (void); + +char **split_lines (char *buffer, int max); + +void free_lines (char **lines); + +#endif /* __CMD_H__ */ diff --git a/getcomments.pl b/getcomments.pl new file mode 100644 index 0000000..47edf67 --- /dev/null +++ b/getcomments.pl @@ -0,0 +1,85 @@ +#!/usr/bin/perl + +use strict; + +# default value +my $format = "%"; +my $pattern = ""; + +# help message +sub usage() { + + print <) { + my $cmt; + + # process c++ comments + ($cmt, $_) = m{//\s*(.*?)\s*$()} if (m{//} && !m{/\*.*//}); + + # process standard c comments + ($cmt, $_) = m{^.*?/\*\s*(.*?)\s*\*/(.*)}s if (m{/\*.*\*/}s); + + push(@comments, $cmt) if ($cmt); + + # empty buffer if no comment is present + undef($_) if (!m{/[/*]}); + } + + # close file + close (IN); + + # display comment blocks + foreach my $block (@comments) { + if (($block) = ($block =~ /$pattern(.*)/s)) { + ($_ = $format) =~ s/%/$block/gs; + print "$_\n"; + } + } +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..5199905 --- /dev/null +++ b/makefile @@ -0,0 +1,146 @@ +# Default flags + +CC = gcc + +INCLUDES = -I../debug -D__MEMORY_ALLOCATION__ +OFLAGS = -O4 -Os +#OFLAGS = -O4 -ffast-math -finline-functions +#OFLAGS = -O4 -finline-functions +#OFLAGS += -mtune=pentium3 -mmmx -msse -msse2 -m3dnow +#OFLAGS += -minline-all-stringops -fsingle-precision-constant +#OFLAGS += -malign-double +CFLAGS += -W -Wall -Wextra -g +CFLAGS += -std=c99 -D_XOPEN_SOURCE=500 +CFLAGS += $(OFLAGS) $(INCLUDES) $(GCOVER) +LDFLAGS += -g + +# Targets + +ALLEXE = +#ALLEXE += test +#ALLEXE += kbhit +#ALLEXE += tetris +ALLEXE += appli +#ALLEXE += line +#ALLEXE += skel + +SHELL = bash + +MAKE = mingw32-make +MAKEFLAGS += -s + +# Functions + +TITLE = echo -en "\033[0;1m$(strip $(1))\033[0;0m:\t" +PASS = echo -e "\033[1;32m$(strip $(1))\033[0;0m" +WARN = echo -e "\033[1;33m$(strip $(1))\033[0;0m" +FAIL = echo -e "\033[1;31m$(strip $(1))\033[0;0m" + +MKDIR = mkdir -p $(1) && chmod a+rx,go-w $(1) + +INSTALL = test -d `dirname $(2)` || $(call MKDIR, `dirname $(2)`) && cp -pa $(1) $(2) && chmod a+rX,go-w $(2) + +VALID = $(call TITLE, $(1)) && $(2) && $(call PASS, SUCCESS) || { $(call FAIL, FAILED); test; } + +## Generic rules + +all: depends + $(MAKE) $(ALLEXE:%=%.exe) + +count: + wc $(wildcard *.c *.h) $(MAKEFILE_LIST) + +clean: + $(call TITLE, "Cleaning") + touch clean + rm -f clean $(wildcard *.d *.ld *.log *.o *.test *~ .exec_*) + $(call PASS, SUCCESS) + +depends: $(patsubst %.c, %.d, $(wildcard *.c)) $(patsubst %, %.ld, $(ALLEXE)) + +gcovs: + $(MAKE) $(addprefix gcov_,$(ALLEXE)) + +purge: clean + $(call TITLE, "Purging") + touch purge + rm -f purge $(ALLEXE:%=%.exe) + $(call PASS, SUCCESS) + +valgrinds: + $(MAKE) $(addprefix valgrind_,$(ALLEXE)) + +wipe: purge + $(call TITLE, "Wiping") + touch wipe + rm -f wipe $(wildcard *.gcda *.gcno) + $(call PASS, SUCCESS) + +tests: all + $(MAKE) $(addprefix test_,$(ALLEXE)) + +## Main rules + +include $(wildcard *.d) +include $(wildcard *.ld) + +gcov_%: + $(MAKE) purge + CFLAGS="-fprofile-arcs -ftest-coverage" LDFLAGS="-fprofile-arcs -ftest-coverage" $(MAKE) + $(MAKE) test_$(@:gcov_%=%) + gcov `sed -e 's/\.exe:/.c/;s/\.o/.c/g' $(@:gcov_%=%.ld)` + touch gcov + rm -f gcov $(wildcard *.gcda *.gcno) + $(MAKE) purge + +%.test: %.c + $(call TITLE, "Building $@") +# awk '/\/\* *test:.*\*\// { sub(/^.*\/\* *test: */, ""); sub(/ *\*\/.*$$/, ""); print }' $< > $@ + ./getcomments.pl -p='test:\s' -f='%' $< > $@ + $(call PASS, SUCCESS) + +test_%: %.test %.exe + IFS=$$'\n'; RC=0; \ + for test in `cat $< | sed 's,${<:.test=.exe},./${<:.test=.exe},g'`; do \ + echo "=== $$test ==="; \ + eval $(VALGRIND) $$test; \ + [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \ + || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \ + done; \ + test "$$RC" -ne 1 + +valgrind_%: %.exe + VALGRIND="valgrind -v --leak-check=full --show-reachable=yes --log-fd=2"; \ + export VALGRIND; \ + $(MAKE) $(@:valgrind_%=test_%) + +%.d: %.c + $(call TITLE, "Building $@") + $(CC) $(INCLUDES) -MM $< -o $@~ + echo ${<:.c=.o}: $(shell ./getcomments.pl -p='depend:\s' -f='%' $<) >> $@~ + mv $@~ $@ + $(call PASS, SUCCESS) + +%.ld: %.c + $(call TITLE, "Building $@") + echo ${<:.c=.exe}: $(shell ./getcomments.pl -p='linker:\s' -f='%' $< | awk '{for (i=1;i<=NF;i++) if ($$(i) ~ /.o$$/) printf " %s", $$(i)}') > $@ + $(call PASS, SUCCESS) + +%.o: %.c + $(call TITLE, "Building $@") + $(CC) $(CFLAGS) $(INCLUDES) $(shell ./getcomments.pl -p='cflags:\s' -f='%' $<) -c $< -o $@ + $(call PASS, SUCCESS) + + +%.exe: %.o %.d + $(call TITLE, "Building $@") + $(CC) $(LDFLAGS) $(shell ./getcomments.pl -p='linker:\s' -f='%' ${<:.o=.c}) $< -o $@ + $(call PASS, SUCCESS) + +## Phony + +.PHONY: all clean count depends gcovs purge tests + +## Precious + +.PRECIOUS: %.d %.o diff --git a/tui.c b/tui.c new file mode 100644 index 0000000..47945cf --- /dev/null +++ b/tui.c @@ -0,0 +1,827 @@ +/********************************* tui.c ************************************/ +/* + * 'textual user interface' + * + * Author : P.J. Kunst + * Date : 1993-02-25 + */ + +#include +#include +#include +#include +#include +#include +#include "tui.h" + +void statusmsg(char *); +int waitforkey(void); +void rmerror(void); + +#if defined(__unix) && !defined(__DJGPP__) +#include +#endif + +#ifdef A_COLOR +# define TITLECOLOR 1 /* color pair indices */ +# define MAINMENUCOLOR (2 | A_BOLD) +# define MAINMENUREVCOLOR (3 | A_BOLD | A_REVERSE) +# define SUBMENUCOLOR (4 | A_BOLD) +# define SUBMENUREVCOLOR (5 | A_BOLD | A_REVERSE) +# define BODYCOLOR 6 +# define STATUSCOLOR (7 | A_BOLD) +# define INPUTBOXCOLOR 8 +# define EDITBOXCOLOR (9 | A_BOLD | A_REVERSE) +#else +# define TITLECOLOR 0 /* color pair indices */ +# define MAINMENUCOLOR (A_BOLD) +# define MAINMENUREVCOLOR (A_BOLD | A_REVERSE) +# define SUBMENUCOLOR (A_BOLD) +# define SUBMENUREVCOLOR (A_BOLD | A_REVERSE) +# define BODYCOLOR 0 +# define STATUSCOLOR (A_BOLD) +# define INPUTBOXCOLOR 0 +# define EDITBOXCOLOR (A_BOLD | A_REVERSE) +#endif + + +#define th 1 /* title window height */ +#define mh 1 /* main menu height */ +#define sh 2 /* status window height */ +#define bh (LINES - th - mh - sh) /* body window height */ +#define bw COLS /* body window width */ + + +/******************************* STATIC ************************************/ + +static WINDOW *wtitl, *wmain, *wbody, *wstat; /* title, menu, body, status win*/ +static int nexty, nextx; +static int key = ERR, ch = ERR; +static bool quit = FALSE; +static bool incurses = FALSE; + +#ifndef PDCURSES +static char wordchar(void) +{ + return 0x17; /* ^W */ +} +#endif + +static char *padstr(char *s, int length) +{ + static char buf[MAXSTRLEN]; + char fmt[10]; + + sprintf(fmt, (int)strlen(s) > length ? "%%.%ds" : "%%-%ds", length); + sprintf(buf, fmt, s); + + return buf; +} + +static char *prepad(char *s, int length) +{ + int i; + char *p = s; + + if (length > 0) + { + memmove((void *)(s + length), (const void *)s, strlen(s) + 1); + + for (i = 0; i < length; i++) + *p++ = ' '; + } + + return s; +} + +static void rmline(WINDOW *win, int nr) /* keeps box lines intact */ +{ + mvwaddstr(win, nr, 1, padstr(" ", bw - 2)); + wrefresh(win); +} + +static void initcolor(void) +{ +#ifdef A_COLOR + if (has_colors()) + start_color(); + + /* foreground, background */ + + init_pair(TITLECOLOR & ~A_ATTR, COLOR_BLACK, COLOR_BLUE); + init_pair(MAINMENUCOLOR & ~A_ATTR, COLOR_WHITE, COLOR_CYAN); + init_pair(MAINMENUREVCOLOR & ~A_ATTR, COLOR_WHITE, COLOR_BLACK); + init_pair(SUBMENUCOLOR & ~A_ATTR, COLOR_WHITE, COLOR_CYAN); + init_pair(SUBMENUREVCOLOR & ~A_ATTR, COLOR_WHITE, COLOR_BLACK); + init_pair(BODYCOLOR & ~A_ATTR, COLOR_WHITE, COLOR_BLUE); + init_pair(STATUSCOLOR & ~A_ATTR, COLOR_WHITE, COLOR_CYAN); + init_pair(INPUTBOXCOLOR & ~A_ATTR, COLOR_BLACK, COLOR_CYAN); + init_pair(EDITBOXCOLOR & ~A_ATTR, COLOR_WHITE, COLOR_BLACK); +#endif +} + +static void setcolor(WINDOW *win, chtype color) +{ + chtype attr = color & A_ATTR; /* extract Bold, Reverse, Blink bits */ + +#ifdef A_COLOR + attr &= ~A_REVERSE; /* ignore reverse, use colors instead! */ + wattrset(win, COLOR_PAIR(color & A_CHARTEXT) | attr); +#else + attr &= ~A_BOLD; /* ignore bold, gives messy display on HP-UX */ + wattrset(win, attr); +#endif +} + +static void colorbox(WINDOW *win, chtype color, int hasbox) +{ + int maxy; +#ifndef PDCURSES + int maxx; +#endif + chtype attr = color & A_ATTR; /* extract Bold, Reverse, Blink bits */ + + setcolor(win, color); + +#ifdef A_COLOR + if (has_colors()) + wbkgd(win, COLOR_PAIR(color & A_CHARTEXT) | (attr & ~A_REVERSE)); + else +#endif + wbkgd(win, attr); + + werase(win); + +#ifdef PDCURSES + maxy = getmaxy(win); +#else + getmaxyx(win, maxy, maxx); +#endif + if (hasbox && (maxy > 2)) + box(win, 0, 0); + + touchwin(win); + wrefresh(win); +} + +static void idle(void) +{ + char buf[MAXSTRLEN]; + time_t t; + struct tm *tp; + + if (time (&t) == -1) + return; /* time not available */ + + tp = localtime(&t); + sprintf(buf, " %.4d-%.2d-%.2d %.2d:%.2d:%.2d", + tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday, + tp->tm_hour, tp->tm_min, tp->tm_sec); + + mvwaddstr(wtitl, 0, bw - strlen(buf) - 2, buf); + wrefresh(wtitl); +} + +static void menudim(menu *mp, int *lines, int *columns) +{ + int n, l, mmax = 0; + + for (n = 0; mp->func; n++, mp++) + if ((l = strlen(mp->name)) > mmax) mmax = l; + + *lines = n; + *columns = mmax + 2; +} + +static void setmenupos(int y, int x) +{ + nexty = y; + nextx = x; +} + +static void getmenupos(int *y, int *x) +{ + *y = nexty; + *x = nextx; +} + +static int hotkey(const char *s) +{ + int c0 = *s; /* if no upper case found, return first char */ + + for (; *s; s++) + if (isupper((unsigned char)*s)) + break; + + return *s ? *s : c0; +} + +static void repaintmenu(WINDOW *wmenu, menu *mp) +{ + int i; + menu *p = mp; + + for (i = 0; p->func; i++, p++) + mvwaddstr(wmenu, i + 1, 2, p->name); + + touchwin(wmenu); + wrefresh(wmenu); +} + +static void repaintmainmenu(int width, menu *mp) +{ + int i; + menu *p = mp; + + for (i = 0; p->func; i++, p++) + mvwaddstr(wmain, 0, i * width, prepad(padstr(p->name, width - 1), 1)); + + touchwin(wmain); + wrefresh(wmain); +} + +void mainhelp(void) +{ +#ifdef ALT_X + statusmsg("Use arrow keys and Enter to select (Alt-X to quit)"); +#else + statusmsg("Use arrow keys and Enter to select"); +#endif +} + +void mainmenu(menu *mp) +{ + int nitems, barlen, old = -1, cur = 0, c, cur0; + + menudim(mp, &nitems, &barlen); + repaintmainmenu(barlen, mp); + + while (!quit) + { + if (cur != old) + { + if (old != -1) + { + mvwaddstr(wmain, 0, old * barlen, + prepad(padstr(mp[old].name, barlen - 1), 1)); + + statusmsg(mp[cur].desc); + } + else + mainhelp(); + + setcolor(wmain, MAINMENUREVCOLOR); + + mvwaddstr(wmain, 0, cur * barlen, + prepad(padstr(mp[cur].name, barlen - 1), 1)); + + setcolor(wmain, MAINMENUCOLOR); + old = cur; + wrefresh(wmain); + } + + switch (c = (key != ERR ? key : waitforkey())) + { + case KEY_DOWN: + case '\n': /* menu item selected */ + touchwin(wbody); + wrefresh(wbody); + rmerror(); + setmenupos(th + mh, cur * barlen); + curs_set(1); + (mp[cur].func)(); /* perform function */ + curs_set(0); + + switch (key) + { + case KEY_LEFT: + cur = (cur + nitems - 1) % nitems; + key = '\n'; + break; + + case KEY_RIGHT: + cur = (cur + 1) % nitems; + key = '\n'; + break; + + default: + key = ERR; + } + + repaintmainmenu(barlen, mp); + old = -1; + break; + + case KEY_LEFT: + cur = (cur + nitems - 1) % nitems; + break; + + case KEY_RIGHT: + cur = (cur + 1) % nitems; + break; + + case KEY_ESC: + mainhelp(); + break; + + default: + cur0 = cur; + + do + { + cur = (cur + 1) % nitems; + + } while ((cur != cur0) && (hotkey(mp[cur].name) != toupper(c))); + + if (hotkey(mp[cur].name) == toupper(c)) + key = '\n'; + } + + } + + rmerror(); + touchwin(wbody); + wrefresh(wbody); +} + +static void cleanup(void) /* cleanup curses settings */ +{ + if (incurses) + { + delwin(wtitl); + delwin(wmain); + delwin(wbody); + delwin(wstat); + curs_set(1); + endwin(); + incurses = FALSE; + } +} + + +/******************************* EXTERNAL **********************************/ + +void clsbody(void) +{ + werase(wbody); + wmove(wbody, 0, 0); +} + +int bodylen(void) +{ +#ifdef PDCURSES + return getmaxy(wbody); +#else + int maxy, maxx; + + getmaxyx(wbody, maxy, maxx); + return maxy; +#endif +} + +WINDOW *bodywin(void) +{ + return wbody; +} + +void rmerror(void) +{ + rmline(wstat, 0); +} + +void rmstatus(void) +{ + rmline(wstat, 1); +} + +void titlemsg(char *msg) +{ + mvwaddstr(wtitl, 0, 2, padstr(msg, bw - 3)); + wrefresh(wtitl); +} + +void bodymsg(char *msg) +{ + waddstr(wbody, msg); + wrefresh(wbody); +} + +void errormsg(char *msg) +{ + beep(); + mvwaddstr(wstat, 0, 2, padstr(msg, bw - 3)); + wrefresh(wstat); +} + +void statusmsg(char *msg) +{ + mvwaddstr(wstat, 1, 2, padstr(msg, bw - 3)); + wrefresh(wstat); +} + +bool keypressed(void) +{ + ch = wgetch(wbody); + + return ch != ERR; +} + +int getkey(void) +{ + int c = ch; + + ch = ERR; +#ifdef ALT_X + quit = (c == ALT_X); /* PC only ! */ +#endif + return c; +} + +int waitforkey(void) +{ + do idle(); while (!keypressed()); + return getkey(); +} + +void DoExit(void) /* terminate program */ +{ + quit = TRUE; +} + +void domenu(menu *mp) +{ + int y, x, nitems, barlen, mheight, mw, old = -1, cur = 0, cur0; + bool stop = FALSE; + WINDOW *wmenu; + + curs_set(0); + getmenupos(&y, &x); + menudim(mp, &nitems, &barlen); + mheight = nitems + 2; + mw = barlen + 2; + wmenu = newwin(mheight, mw, y, x); + colorbox(wmenu, SUBMENUCOLOR, 1); + repaintmenu(wmenu, mp); + + key = ERR; + + while (!stop && !quit) + { + if (cur != old) + { + if (old != -1) + mvwaddstr(wmenu, old + 1, 1, + prepad(padstr(mp[old].name, barlen - 1), 1)); + + setcolor(wmenu, SUBMENUREVCOLOR); + mvwaddstr(wmenu, cur + 1, 1, + prepad(padstr(mp[cur].name, barlen - 1), 1)); + + setcolor(wmenu, SUBMENUCOLOR); + statusmsg(mp[cur].desc); + + old = cur; + wrefresh(wmenu); + } + + switch (key = ((key != ERR) ? key : waitforkey())) + { + case '\n': /* menu item selected */ + touchwin(wbody); + wrefresh(wbody); + setmenupos(y + 1, x + 1); + rmerror(); + + key = ERR; + curs_set(1); + (mp[cur].func)(); /* perform function */ + curs_set(0); + + repaintmenu(wmenu, mp); + + old = -1; + break; + + case KEY_UP: + cur = (cur + nitems - 1) % nitems; + key = ERR; + break; + + case KEY_DOWN: + cur = (cur + 1) % nitems; + key = ERR; + break; + + case KEY_ESC: + case KEY_LEFT: + case KEY_RIGHT: + if (key == KEY_ESC) + key = ERR; /* return to prev submenu */ + + stop = TRUE; + break; + + default: + cur0 = cur; + + do + { + cur = (cur + 1) % nitems; + + } while ((cur != cur0) && + (hotkey(mp[cur].name) != toupper((int)key))); + + key = (hotkey(mp[cur].name) == toupper((int)key)) ? '\n' : ERR; + } + + } + + rmerror(); + delwin(wmenu); + touchwin(wbody); + wrefresh(wbody); +} + +void startmenu(menu *mp, char *mtitle, FUNC init) +{ + initscr(); + incurses = TRUE; + initcolor(); + + wtitl = subwin(stdscr, th, bw, 0, 0); + wmain = subwin(stdscr, mh, bw, th, 0); + wbody = subwin(stdscr, bh, bw, th + mh, 0); + wstat = subwin(stdscr, sh, bw, th + mh + bh, 0); + + colorbox(wtitl, TITLECOLOR, 0); + colorbox(wmain, MAINMENUCOLOR, 0); + colorbox(wbody, BODYCOLOR, 0); + colorbox(wstat, STATUSCOLOR, 0); + + if (mtitle) + titlemsg(mtitle); + + cbreak(); /* direct input (no newline required)... */ + noecho(); /* ... without echoing */ + curs_set(0); /* hide cursor (if possible) */ + nodelay(wbody, TRUE); /* don't wait for input... */ + halfdelay(10); /* ...well, no more than a second, anyway */ + keypad(wbody, TRUE); /* enable cursor keys */ + scrollok(wbody, TRUE); /* enable scrolling in main window */ + + leaveok(stdscr, TRUE); + leaveok(wtitl, TRUE); + leaveok(wmain, TRUE); + leaveok(wstat, TRUE); + + if (init) { + int nitems, barlen; + menudim(mp, &nitems, &barlen); + repaintmainmenu(barlen, mp); + (*init)(); + } else { + mainmenu(mp); + } + + cleanup(); +} + +static void repainteditbox(WINDOW *win, int x, char *buf) +{ +#ifndef PDCURSES + int maxy; +#endif + int maxx; + +#ifdef PDCURSES + maxx = getmaxx(win); +#else + getmaxyx(win, maxy, maxx); +#endif + werase(win); + mvwprintw(win, 0, 0, "%s", padstr(buf, maxx)); + wmove(win, 0, x); + wrefresh(win); +} + +/* + + weditstr() - edit string + + Description: + The initial value of 'str' with a maximum length of 'field' - 1, + which is supplied by the calling routine, is editted. The user's + erase (^H), kill (^U) and delete word (^W) chars are interpreted. + The PC insert or Tab keys toggle between insert and edit mode. + Escape aborts the edit session, leaving 'str' unchanged. + Enter, Up or Down Arrow are used to accept the changes to 'str'. + NOTE: editstr(), mveditstr(), and mvweditstr() are macros. + + Return Value: + Returns the input terminating character on success (Escape, + Enter, Up or Down Arrow) and ERR on error. + + Errors: + It is an error to call this function with a NULL window pointer. + The length of the initial 'str' must not exceed 'field' - 1. + +*/ + +int weditstr(WINDOW *win, char *buf, int field) +{ + char org[MAXSTRLEN], *tp, *bp = buf; + bool defdisp = TRUE, stop = FALSE, insert = FALSE; + int cury, curx, begy, begx; + chtype oldattr; + WINDOW *wedit; + int c = 0; + + if ((field >= MAXSTRLEN) || (buf == NULL) || + ((int)strlen(buf) > field - 1)) + return ERR; + + strcpy(org, buf); /* save original */ + + wrefresh(win); + getyx(win, cury, curx); + getbegyx(win, begy, begx); + + wedit = subwin(win, 1, field, begy + cury, begx + curx); + oldattr = getattrs(wedit); + colorbox(wedit, EDITBOXCOLOR, 0); + + keypad(wedit, TRUE); + curs_set(1); + + while (!stop) + { + idle(); + repainteditbox(wedit, bp - buf, buf); + + switch (c = wgetch(wedit)) + { + case ERR: + break; + + case KEY_ESC: + strcpy(buf, org); /* restore original */ + stop = TRUE; + break; + + case '\n': + case KEY_UP: + case KEY_DOWN: + stop = TRUE; + break; + + case KEY_LEFT: + if (bp > buf) + bp--; + break; + + case KEY_RIGHT: + defdisp = FALSE; + if (bp - buf < (int)strlen(buf)) + bp++; + break; + + case '\t': /* TAB -- because insert + is broken on HPUX */ + case KEY_IC: /* enter insert mode */ + case KEY_EIC: /* exit insert mode */ + defdisp = FALSE; + insert = !insert; + + curs_set(insert ? 2 : 1); + break; + + default: + if (c == erasechar()) /* backspace, ^H */ + { + if (bp > buf) + { + memmove((void *)(bp - 1), (const void *)bp, strlen(bp) + 1); + bp--; + } + } + else if (c == killchar()) /* ^U */ + { + bp = buf; + *bp = '\0'; + } + else if (c == wordchar()) /* ^W */ + { + tp = bp; + + while ((bp > buf) && (*(bp - 1) == ' ')) + bp--; + while ((bp > buf) && (*(bp - 1) != ' ')) + bp--; + + memmove((void *)bp, (const void *)tp, strlen(tp) + 1); + } + else if (isprint(c)) + { + if (defdisp) + { + bp = buf; + *bp = '\0'; + defdisp = FALSE; + } + + if (insert) + { + if ((int)strlen(buf) < field - 1) + { + memmove((void *)(bp + 1), (const void *)bp, + strlen(bp) + 1); + + *bp++ = c; + } + } + else if (bp - buf < field - 1) + { + /* append new string terminator */ + + if (!*bp) + bp[1] = '\0'; + + *bp++ = c; + } + } + } + } + + curs_set(0); + + wattrset(wedit, oldattr); + repainteditbox(wedit, bp - buf, buf); + delwin(wedit); + + return c; +} + +WINDOW *winputbox(WINDOW *win, int nlines, int ncols) +{ + WINDOW *winp; + int cury, curx, begy, begx; + + getyx(win, cury, curx); + getbegyx(win, begy, begx); + + winp = newwin(nlines, ncols, begy + cury, begx + curx); + colorbox(winp, INPUTBOXCOLOR, 1); + + return winp; +} + +int getstrings(char *desc[], char *buf[], int field) +{ + WINDOW *winput; + int oldy, oldx, maxy, maxx, nlines, ncols, i, n, l, mmax = 0; + int c = 0; + bool stop = FALSE; + + for (n = 0; desc[n]; n++) + if ((l = strlen(desc[n])) > mmax) + mmax = l; + + nlines = n + 2; ncols = mmax + field + 4; + getyx(wbody, oldy, oldx); + getmaxyx(wbody, maxy, maxx); + + winput = mvwinputbox(wbody, (maxy - nlines) / 2, (maxx - ncols) / 2, + nlines, ncols); + + for (i = 0; i < n; i++) + mvwprintw(winput, i + 1, 2, "%s", desc[i]); + + i = 0; + + while (!stop) + { + switch (c = mvweditstr(winput, i+1, mmax+3, buf[i], field)) + { + case KEY_ESC: + stop = TRUE; + break; + + case KEY_UP: + i = (i + n - 1) % n; + break; + + case '\n': + case '\t': + case KEY_DOWN: + if (++i == n) + stop = TRUE; /* all passed? */ + } + } + + delwin(winput); + touchwin(wbody); + wmove(wbody, oldy, oldx); + wrefresh(wbody); + + return c; +} diff --git a/tui.h b/tui.h new file mode 100644 index 0000000..413c168 --- /dev/null +++ b/tui.h @@ -0,0 +1,68 @@ +/* + * 'textual user interface' + * + * Author : P.J. Kunst + * Date : 1993-02-25 + */ + +#ifndef _TUI_H_ +#define _TUI_H_ + +#include + +#ifdef A_COLOR +#define A_ATTR (A_ATTRIBUTES ^ A_COLOR) /* A_BLINK, A_REVERSE, A_BOLD */ +#else +#define A_ATTR (A_ATTRIBUTES) /* standard UNIX attributes */ +#endif + +#define MAXSTRLEN 256 +#define KEY_ESC 0x1b /* Escape */ + +typedef void (*FUNC)(void); + +typedef struct +{ + char *name; /* item label */ + FUNC func; /* (pointer to) function */ + char *desc; /* function description */ +} menu; + +/* ANSI C function prototypes: */ + +void clsbody(void); +int bodylen(void); +WINDOW *bodywin(void); + +void rmerror(void); +void rmstatus(void); + +void titlemsg(char *msg); +void bodymsg(char *msg); +void errormsg(char *msg); +void statusmsg(char *msg); + +bool keypressed(void); +int getkey(void); +int waitforkey(void); + +void DoExit(void); +void startmenu(menu *mp, char *title, FUNC init); +void domenu(menu *mp); + +void mainmenu(menu *mp); +void helpmenu(void); + +int weditstr(WINDOW *win, char *buf, int field); +WINDOW *winputbox(WINDOW *win, int nlines, int ncols); +int getstrings(char *desc[], char *buf[], int field); + +#define editstr(s,f) (weditstr(stdscr,s,f)) +#define mveditstr(y,x,s,f) (move(y,x)==ERR?ERR:editstr(s,f)) +#define mvweditstr(w,y,x,s,f) (wmove(w,y,x)==ERR?ERR:weditstr(w,s,f)) + +#define inputbox(l,c) (winputbox(stdscr,l,c)) +#define mvinputbox(y,x,l,c) (move(y,x)==ERR?w:inputbox(l,c)) +#define mvwinputbox(w,y,x,l,c) (wmove(w,y,x)==ERR?w:winputbox(w,l,c)) + +#endif -- 2.30.2