Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

editbuf(3) - edit a string with curses Part01/01

0 views
Skip to first unread message

Jan-Piet Mens

unread,
Oct 17, 1991, 11:39:11 AM10/17/91
to
Submitted-by: j...@Logix.DE
Archive-name: editbuf/part01

editbuf() is a routine for Curses(3X). It allows a user to edit an
alphanumeric string. Rather comfortably.


---- Cut Here and feed the following to sh ----
#!/bin/sh
# This is editbuf, a shell archive (produced by shar 3.49)
# To extract the files from this archive, save it to a file, remove
# everything above the "!/bin/sh" line above, and type "sh file_name".
#
# made 10/17/1991 15:37 UTC by j...@Logix.DE
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length mode name
# ------ ---------- ------------------------------------------
# 1826 -rw-r--r-- README
# 370 -rw-r--r-- Copyright
# 528 -rw-r--r-- Makefile
# 4468 -rw-r--r-- editbuf.c
# 1027 -rw-r--r-- demo.c
# 3509 -rw-r--r-- editbuf.3
#
# ============= README ==============
if test -f 'README' -a X"$1" != X"-c"; then
echo 'x - skipping README (File already exists)'
else
echo 'x - extracting README (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'README' &&
This is editbuf 1.0
===================
X
editbuf() is a routine for Curses(3X). It allows a user to edit an
alphanumeric string.
X
XFrom the manual:
X
X The following keys will call special functions while
X editing string. The control sequences (CTL X) can also be
X replaced by function keys if so wished.
X
X KEY_RIGHT The cursor is moved one position to the right on
X the screen. The buffer is not modified.
X
X KEY_LEFT The cursor is moved one position to the left.
X
X KEY_DEL or ^X
X Delete the character under the cursor. The rest of
X the characters in the field are moved left.
X
X KEY_INS or ^I
X Make room for one character.(Insert). The last
X character of the field is lost.
X
X KEY_UNDO or ^U
X Undo. The value of def is inserted into the input
X buffer. If def is not defined, a copy of string is
X used the input buffer.
X
X BS or ^H Delete previous character (not the same as
X KEY_DEL). If the cursor is in the middle of
X string, the string will be cut off.
X
X CTL-B move one word back in the string.
X
X CTL-D Delete to the end of the field.
X
X CTL-E move to the end of the string.
X
X CTL-K Change case of the character under the cursor.
X UPPER/LOWER conversion of each character. The
X cursor moves one character further.
X
X KEY_REFRESH or ^L
X Repaint whole screen.
X
X CTL-O Delete next word.
X
X CTL-P move to the beginning of the string.
X
X CTL-W move one word further in the string. A word is
X separated from the next by a blank.
X
X
Installation
============
X
X Check the Makefile. Adjust the name of your compiler if needed.
X Do a `make'. this should produce an executable called `demo'.
X
X Run that and follow the instructions.
X
X If you like editbuf(), install editbuf.o in some acessible
X place.
X
X Jan-Piet Mens
X j...@Logix.DE
SHAR_EOF
chmod 0644 README ||
echo 'restore of README failed'
Wc_c="`wc -c < 'README'`"
test 1826 -eq "$Wc_c" ||
echo 'README: original size 1826, current size' "$Wc_c"
fi
# ============= Copyright ==============
if test -f 'Copyright' -a X"$1" != X"-c"; then
echo 'x - skipping Copyright (File already exists)'
else
echo 'x - extracting Copyright (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'Copyright' &&
/*
X * edit(3), Copyright 1991, Jan-Piet Mens
X * License to freely use and distribute this software is hereby granted
X * by the author, subject to the condition that this copyright notice
X * remains intact. The author retains the exclusive right to publish
X * derivative works based on this work, including, but not limited
X * to, revised versions of this work
X */
SHAR_EOF
chmod 0644 Copyright ||
echo 'restore of Copyright failed'
Wc_c="`wc -c < 'Copyright'`"
test 370 -eq "$Wc_c" ||
echo 'Copyright: original size 370, current size' "$Wc_c"
fi
# ============= Makefile ==============
if test -f 'Makefile' -a X"$1" != X"-c"; then
echo 'x - skipping Makefile (File already exists)'
else
echo 'x - extracting Makefile (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'Makefile' &&
X
# Define NO_KEYPAD if your keypad() routine breaks. (SCO)
NO_KEYPAD=-DNO_KEYPAD
X
CC=cc
CFLAGS=-O -s $(NO_KEYPAD)
LIBS=-lcurses
PROG= demo
X
OBJS= editbuf.o demo.o
SRCS= editbuf.c demo.c
MANPAGE=editbuf.3
SHARFILES= README Copyright Makefile
X
$(PROG): $(OBJS)
X $(CC) $(CFLAGS) -o $(PROG) $(OBJS) $(LIBS)
X
$(OBJS): Makefile
X
clean:
X rm -f $(OBJS) core a.out
clobber: clean
X rm -f $(PROG) Part.??
X
dist: $(SRCS) $(MANPAGE) $(SHARFILES)
X shar49 -n editbuf -a -s 'j...@Logix.DE' -o Part -l 50 -c \
X $(SHARFILES) $(SRCS) $(MANPAGE)
SHAR_EOF
chmod 0644 Makefile ||
echo 'restore of Makefile failed'
Wc_c="`wc -c < 'Makefile'`"
test 528 -eq "$Wc_c" ||
echo 'Makefile: original size 528, current size' "$Wc_c"
fi
# ============= editbuf.c ==============
if test -f 'editbuf.c' -a X"$1" != X"-c"; then
echo 'x - skipping editbuf.c (File already exists)'
else
echo 'x - extracting editbuf.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'editbuf.c' &&
/*
X * editbuf(3), Copyright 1991, Jan-Piet Mens/Logix GmbH
X * License to freely use and distribute this software is hereby granted
X * by the author, subject to the condition that this copyright notice
X * remains intact. The author retains the exclusive right to publish
X * derivative works based on this work, including, but not limited
X * to, revised versions of this work
X */
X
#include <curses.h>
#include <ctype.h>
X
#define EOS '\0'
#define CTL(c) (c & 037)
#define PROMPT '_'
X
#define MAKE for (n=strlen(buf);n<max;n++) \
X buf[n] = PROMPT; buf[n] = EOS
X
int editbuf(win,lin,col,min,max,atr,string,keys,def)
WINDOW *win;
int lin,col,min,max;
int atr;
char *string;
int keys[];
char *def;
{
X int modif = FALSE; /* Buffer has been modified */
X int c,j,i,n,k;
X char buf[BUFSIZ]; /* Working string */
X char *malloc();
X char nodef;
X
X if (def == (char *)0)
X {
X nodef = TRUE;
X def = malloc((unsigned)100);
X strcpy(def,string);
X }
X
X memset(buf,EOS,BUFSIZ);
X strncpy(buf,string,max);
X for (i=max;i>0;i--)
X if (buf[i-1] != ' ')
X {
X buf[i] = EOS;
X break;
X }
X if (i == 0)
X *buf = EOS;
X MAKE;
X
X i = 0;
X wattrset(win,atr);
X mvwaddstr(win,lin,col,buf);
X while (1)
X {
X wmove(win,lin,col + i);
X wrefresh(win);
X
X c = wgetch(win);
X switch (c)
X {
X case ERR: break; /* JPM 250190 for NODELAY */
X case 0: break; /* Return from trap */
X case KEY_UNDO: /* UNDO */
X case CTL('U'):
X strcpy(buf,def);
X MAKE;
X wmove(win,lin,col);
X mvwaddstr(win,lin,col,buf);
X i = 0;
X modif = TRUE;
X break;
X case KEY_ENTER:
X case '\n':
X case '\r':
X FINISH:
X for (i=max-1;i>=0;i--)
X {
X if (buf[i] == PROMPT)
X buf[i] = ' ';
X else break;
X }
X mvwaddstr(win,lin,col,buf);
X for (i=max;i>0;i--)
X if (buf[i-1] != ' ')
X {
X buf[i] = EOS;
X break;
X }
X if (i == 0)
X *buf = EOS;
X if (c != '\r' && c != '\n') /* TermKey */
X goto RET; /* No checking*/
X if (strlen(buf) < min)
X {
X beep();
X MAKE;
X modif = TRUE;
X break;
X }
X
X RET:
X wattroff(win,atr);
X buf[max] = EOS;
X strcpy(string,buf);
X if (nodef)
X free(def);
X return (c);
X case KEY_RIGHT: /* RIGHT 1 CHR */
X if (i > max - 2)
X ;
X else if (buf[i] == PROMPT)
X ;
X else ++i;
X break;
X case KEY_LEFT: /* LEFT 1 CHR */
X --i;
X break;
X case KEY_BACKSPACE:
X case CTL('H'): /* BACKSPACE */
X if (i > 0)
X buf[--i] = EOS;
X modif = TRUE;
X break;
X case CTL('W'): /* RIGHT 1 WORD */
X for (n=i+1;n<max;n++)
X if (buf[n] == ' ')
X break;
X i = ++n;
X break;
X case CTL('B'): /* BACK 1 WORD */
X for (n=i-2;n>=0;n--)
X if (buf[n] == ' ')
X break;
X i = ++n;
X break;
X case CTL('K'): /* UPPER/LOWER CONV. */
X if (isupper(buf[i]))
X buf[i] = tolower(buf[i]);
X else buf[i] = toupper(buf[i]);
X ++i;
X modif = TRUE;
X break;
X case KEY_REFRESH: /* REFRESH */
X case CTL('L'):
X clearok(curscr,TRUE);
X touchwin(curscr);
X wrefresh(curscr);
X break;
X case CTL('P'): /* GOTO BEG.OF FIELD */
X i = 0;
X break;
X case CTL('E'): /* GOTO END OF FIELD */
X for (n=0;n<max;n++)
X if (buf[n] == PROMPT) break;
X i = n;
X break;
X case CTL('O'): /* REMOVE NEXT WORD */
X for (n=i;n<max;n++)
X {
X for (j=i;j<max;j++)
X buf[j] = buf[j+1];
X if (buf[i] == ' ')
X break;
X }
X for (n=i;n<max;n++)
X buf[n] = buf[n+1];
X buf[n] = EOS;
X modif = TRUE;
X break;
X case CTL('D'): /* DEL TO END OF FIELD */
X for (n=i;n<max;n++)
X buf[n] = EOS;
X modif = TRUE;
X break;
#ifdef KEY_INS
X case KEY_INS: /* INSERT 1 BLANK */
#endif
X case CTL('I'):
X for (n=max-1;n>i;n--)
X buf[n] = buf[n-1];
X buf[n] = ' ';
X modif = TRUE;
X break;
#ifdef KEY_DEL
X case KEY_DEL: /* DELETE 1 CHR */
#endif
X case CTL('X'):
X for (n=i;n<max;n++)
X buf[n] = buf[n+1];
X buf[n] = EOS;
X modif = TRUE;
X break;
X default:
X if (c >= ' ' && c < KEY_DOWN)
X {
X buf[i++] = (char)c;
X modif = TRUE;
X break;
X }
X else
X {
X /*
X * Search for valid terminating
X * key, and if found, finish field
X */
X
X for (k=0;k<20;k++)
X {
X if (keys[k] == -1)
X break;
X if (c == keys[k])
X {
X goto FINISH;
X }
X }
X }
X beep();
X break;
X }
X if (modif == TRUE)
X {
X MAKE;
X mvwaddstr(win,lin,col,buf);
X }
X
X i = (i < 0) ? 0 : i;
X i = (i > max-1) ? max-1 : i;
X }
}
SHAR_EOF
chmod 0644 editbuf.c ||
echo 'restore of editbuf.c failed'
Wc_c="`wc -c < 'editbuf.c'`"
test 4468 -eq "$Wc_c" ||
echo 'editbuf.c: original size 4468, current size' "$Wc_c"
fi
# ============= demo.c ==============
if test -f 'demo.c' -a X"$1" != X"-c"; then
echo 'x - skipping demo.c (File already exists)'
else
echo 'x - extracting demo.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'demo.c' &&
#include <curses.h>
X
main()
{
X static int keys[] = {KEY_UP, KEY_DOWN, -1};
X static char buf[100] = "Hello World!";
X static char dflt[100] = "This is the default string";
X WINDOW *w;
X
X initscr(); noecho(); nonl(); cbreak();
X box(stdscr, 0, 0);
X addstr("editbuf() demonstration");
X mvaddstr(10, 3, "Try hitting any one of these keys (^ means Control)");
X mvaddstr(11, 3, "^K reverse upper/lower case");
X mvaddstr(12, 3, "^E Go to end of field");
X mvaddstr(13, 3, "^P Go to begin of field");
X mvaddstr(14, 3, "^D Delete to end of field ");
X mvaddstr(15, 3, "^L Refresh");
X mvaddstr(16, 3, "^I Insert one character");
X mvaddstr(17, 3, "^X Delete one character");
X mvaddstr(18, 3, "^U Undo. Insert default value");
X mvaddstr(19, 3, "^O Delete word. ^W Word forward. ^B word back");
X refresh();
X
X if ((w = newwin(5,50, 2, 10)) == (WINDOW *)0)
X exit(endwin());
X box(w,0,0);
#ifndef NO_KEYPAD
X keypad(w, TRUE);
#endif
X wrefresh(w);
X
X editbuf(w, 2, 2, 2, 40, A_NORMAL, buf, keys, dflt);
X endwin();
X puts(buf);
X return (0);
}
SHAR_EOF
chmod 0644 demo.c ||
echo 'restore of demo.c failed'
Wc_c="`wc -c < 'demo.c'`"
test 1027 -eq "$Wc_c" ||
echo 'demo.c: original size 1027, current size' "$Wc_c"
fi
# ============= editbuf.3 ==============
if test -f 'editbuf.3' -a X"$1" != X"-c"; then
echo 'x - skipping editbuf.3 (File already exists)'
else
echo 'x - extracting editbuf.3 (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'editbuf.3' &&
.\" eroff -man %s
.\"
.\" editbuf(3), Copyright 1991, Jan-Piet Mens
.\" License to freely use and distribute this software is hereby granted
.\" by the author, subject to the condition that this copyright notice
.\" remains intact. The author retains the exclusive right to publish
.\" derivative works based on this work, including, but not limited
.\" to, revised versions of this work
.\"/
.de EC
\fB\s-2ECURSES\s0\fR
..
.TH EDITBUF 3ec "" "Ecurses Programmers Reference" "Logix GmbH"
.SH NAME
.nf
editbuf - edit an alphanumeric buffer
.fi
.SH SYNOPSIS
.ft B
.nf
#include <curses.h>
X
int editbuf(win,lin,col,min,max,atr,string,keys,def)
WINDOW *win;
int lin,col,min,max,atr;
char *string;
int keys[];
char *def;
.fi
.ft
.SH DESCRIPTION
.I editbuf()
will print the content of
.B string
on the window
.B win,
using the appropriate
CURSES(3) routines on line
.B lin
and column
.B col.
The characters will be shown
using the attribute
.B atr
which can be one of any of the allowed video attributes. The
cursor will be placed at the beginning of
.B string.
Characters can be replaced in
.B string.
The content of
.B string
will be returned to the calling function as soon as
a legal terminating key has been pressed
(NEWLINE, function key ...), any of which
must be contained in the list
.B keys.
.PP
.B Def
points to a default string
that will be inserted whenever KEY_UNDO is
hit.
.B Def
may be NULL, in which case, the given
.B string
is used as default value.
.PP
The length of
.B string
will never exceed
.B max
characters, and if the length of the string
is less than
.B min
characters, the string
will not be returned, but instead a warning
beep will be heard.
.PP
The following keys will call special
functions while editing string. The control
sequences (\fBCTL X\fR) can also be replaced by
function keys if so wished.
.IP KEY_RIGHT 1i
The cursor is moved one
position to the right on the
screen. The buffer is not
modified.
.IP KEY_LEFT 1i
The cursor is moved one
position to the left.
.IP "KEY_DEL or ^X" 1i
Delete the character under
the cursor. The rest of the
characters in the field are
moved left.
.IP "KEY_INS or ^I" 1i
Make room for one
character.(Insert). The last
character of the field is
lost.
.IP "KEY_UNDO or ^U" 1i
Undo. The value of
.B def
is inserted into the input buffer. If
.B def
is not defined, a copy of
.B string
is used
the input buffer.
.IP "BS or ^H" 1i
Delete previous character
(not the same as KEY_DEL). If
the cursor is in the middle
of string, the string will be
cut off.
.IP CTL-B 1i
move one word back in the string.
.IP CTL-D 1i
Delete to the end of the field.
.IP CTL-E 1i
move to the end of the string.
.IP CTL-K 1i
Change case of the character
under the cursor. UPPER/LOWER
conversion of each character.
The cursor moves one character further.
.IP "KEY_REFRESH or ^L" 1i
Repaint whole screen.
.IP CTL-O 1i
Delete next word.
.IP CTL-P 1i
move to the beginning of the string.
.IP CTL-W 1i
move one word further in the
string. A word is separated
from the next by a blank.
.PP
Any other non-printable key will be checked
for in the array
.B keys,
which must be
terminated by a (-1). If the pressed key
matches one of those in the list,
.I editbuf()
will return the token function key number
to the caller whether the requested mininum
of characters in the field has been
satisfied or not.
.SH NOTES
.I keypad()
should be enabled on window
.B win.
.SH AUTHOR
Jan-Piet Mens - j...@Logix.DE
SHAR_EOF
chmod 0644 editbuf.3 ||
echo 'restore of editbuf.3 failed'
Wc_c="`wc -c < 'editbuf.3'`"
test 3509 -eq "$Wc_c" ||
echo 'editbuf.3: original size 3509, current size' "$Wc_c"
fi
exit 0
--
__ _____ __ __
| || _ \ | \/ | Logix GmbH j...@Logix.DE
__| || ___/ | | Moritzstrasse 50 j...@logixwi.UUCP
|_____||__| |__||__| D-6200 Wiesbaden ...!uunet!mcsun!unido!logixwi!jpm

0 new messages