mirror of
https://git.FreeBSD.org/src.git
synced 2024-12-04 09:09:56 +00:00
Add another hateful global to libdialog (what the heck, there are already
so many). For now, the only extended attribute implemented is NO ECHO, useful for things like passwords. See TESTS/input2.c for an example. This should go into 2.2.
This commit is contained in:
parent
89c1d5dcd9
commit
ae5b2dcf86
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=20442
@ -1,9 +1,9 @@
|
||||
# Really quick and evil Makefile for building all the tests. I wish that bmake was friendlier to
|
||||
# the concept of multiple progs/libs in the same directory.
|
||||
# $Id: Makefile,v 1.2 1996/01/01 03:45:21 jkh Exp $
|
||||
# $Id: Makefile,v 1.3 1996/04/07 03:34:35 jkh Exp $
|
||||
|
||||
PROGS= msg yesno prgbox gauge dselect fselect text menu1 menu2 menu3 \
|
||||
input1 check1 check2 check3 radio1 radio2 radio3
|
||||
input1 input2 check1 check2 check3 radio1 radio2 radio3
|
||||
|
||||
CFLAGS+= -Wall -Wstrict-prototypes
|
||||
LDFLAGS += -ldialog -lncurses -lmytinfo
|
||||
|
46
gnu/lib/libdialog/TESTS/input2.c
Normal file
46
gnu/lib/libdialog/TESTS/input2.c
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* small test-driver for new dialog functionality
|
||||
*
|
||||
* Copyright (c) 1995, Jordan Hubbard
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code may be used, modified, copied, distributed, and
|
||||
* sold, in both source and binary form provided that the above
|
||||
* copyright and these terms are retained, verbatim, as the first
|
||||
* lines of this file. Under no circumstances is the author
|
||||
* responsible for the proper functioning of the software nor does
|
||||
* the author assume any responsibility for damages incurred with
|
||||
* its use.
|
||||
*
|
||||
* $Id: input1.c,v 1.1 1996/01/01 03:45:25 jkh Exp $
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <dialog.h>
|
||||
|
||||
/* Kick it off, James! */
|
||||
int
|
||||
main(int argc, unsigned char *argv[])
|
||||
{
|
||||
int retval;
|
||||
unsigned char result[128];
|
||||
|
||||
init_dialog();
|
||||
|
||||
result[0]='\0';
|
||||
DialogInputAttrs |= DITEM_NO_ECHO;
|
||||
retval = dialog_inputbox("this is dialog_inputbox() in action, test #2 (no echo)",
|
||||
"Enter something really secret below, please.",
|
||||
-1, -1, result);
|
||||
DialogInputAttrs &= DITEM_NO_ECHO;
|
||||
dialog_clear();
|
||||
fprintf(stderr, "returned value for dialog_inputbox was %d (%s)\n", retval, result);
|
||||
|
||||
end_dialog();
|
||||
return 0;
|
||||
}
|
@ -50,6 +50,9 @@
|
||||
#define DITEM_RESTORE (1 << 19)
|
||||
#define DITEM_CONTINUE (1 << 20)
|
||||
|
||||
/* Attributes as used by entry fields right now */
|
||||
#define DITEM_NO_ECHO 0x0001
|
||||
|
||||
|
||||
/* negative offsets for buttons in item lists, if specified */
|
||||
#define OK_BUTTON -2
|
||||
@ -77,7 +80,7 @@ typedef struct _dmenu_item {
|
||||
#define FALSE (0)
|
||||
#endif
|
||||
|
||||
extern int DialogX, DialogY;
|
||||
extern int DialogX, DialogY, DialogInputAttrs;
|
||||
|
||||
/*
|
||||
* Attribute names
|
||||
@ -122,7 +125,7 @@ extern bool use_shadow;
|
||||
void draw_shadow(WINDOW *win, int y, int x, int height, int width);
|
||||
#endif
|
||||
void draw_box(WINDOW *win, int y, int x, int height, int width, chtype box, chtype border);
|
||||
int line_edit(WINDOW *dialog, int box_y, int box_x, int flen, int box_width, chtype attrs, int first, unsigned char *result);
|
||||
int line_edit(WINDOW *dialog, int box_y, int box_x, int flen, int box_width, chtype attrs, int first, unsigned char *result, int attr_mask);
|
||||
int strheight(const char *p);
|
||||
int strwidth(const char *p);
|
||||
|
||||
|
@ -106,7 +106,7 @@ int dialog_inputbox(unsigned char *title, unsigned char *prompt, int height, int
|
||||
while (key != ESC) {
|
||||
|
||||
if (button == -1) { /* Input box selected */
|
||||
key = line_edit(dialog, box_y, box_x, -1, box_width, inputbox_attr, first, instr);
|
||||
key = line_edit(dialog, box_y, box_x, -1, box_width, inputbox_attr, first, instr, DialogInputAttrs);
|
||||
first = 0;
|
||||
}
|
||||
else
|
||||
|
@ -90,6 +90,9 @@
|
||||
*/
|
||||
int DialogX, DialogY;
|
||||
|
||||
/* This "secret" global allows you to change the behavior of an input field */
|
||||
int DialogInputAttrs;
|
||||
|
||||
/*
|
||||
* Do some initialization for dialog
|
||||
*/
|
||||
|
@ -24,12 +24,12 @@
|
||||
#include <dialog.h>
|
||||
#include "dialog.priv.h"
|
||||
|
||||
static void redraw_field(WINDOW *dialog, int box_y, int box_x, int flen, int box_width, unsigned char instr[], int input_x, int scroll, chtype attr, chtype old_attr, int fexit);
|
||||
static void redraw_field(WINDOW *dialog, int box_y, int box_x, int flen, int box_width, unsigned char instr[], int input_x, int scroll, chtype attr, chtype old_attr, int fexit, int attr_mask);
|
||||
|
||||
/*
|
||||
* Line editor
|
||||
*/
|
||||
int line_edit(WINDOW* dialog, int box_y, int box_x, int flen, int box_width, chtype attr, int first, unsigned char *result)
|
||||
int line_edit(WINDOW* dialog, int box_y, int box_x, int flen, int box_width, chtype attr, int first, unsigned char *result, int attr_mask)
|
||||
{
|
||||
int i, key;
|
||||
chtype old_attr;
|
||||
@ -53,7 +53,7 @@ int line_edit(WINDOW* dialog, int box_y, int box_x, int flen, int box_width, cht
|
||||
/* scroll = i - input_x;*/
|
||||
scroll = (i > box_width) ? i - box_width + 1: 0;
|
||||
}
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE);
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
||||
|
||||
for (;;) {
|
||||
wattrset(dialog, attr);
|
||||
@ -86,12 +86,12 @@ int line_edit(WINDOW* dialog, int box_y, int box_x, int flen, int box_width, cht
|
||||
case '\013':
|
||||
case KEY_EOL:
|
||||
memset(instr + scroll + input_x, '\0', sizeof(instr) - scroll - input_x);
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE);
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
||||
continue;
|
||||
case '\001':
|
||||
case KEY_HOME:
|
||||
input_x = scroll = 0;
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE);
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
||||
continue;
|
||||
case '\005':
|
||||
case KEY_END:
|
||||
@ -100,7 +100,7 @@ int line_edit(WINDOW* dialog, int box_y, int box_x, int flen, int box_width, cht
|
||||
i++;
|
||||
input_x = i % box_width;
|
||||
scroll = i - input_x;
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE);
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
||||
continue;
|
||||
case '\002':
|
||||
case KEY_LEFT:
|
||||
@ -109,7 +109,7 @@ int line_edit(WINDOW* dialog, int box_y, int box_x, int flen, int box_width, cht
|
||||
int oldscroll = scroll;
|
||||
scroll = scroll < box_width-1 ? 0 : scroll-(box_width-1);
|
||||
input_x = oldscroll - 1 - scroll;
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE);
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
||||
} else {
|
||||
input_x--;
|
||||
wmove(dialog, box_y, input_x + box_x);
|
||||
@ -126,7 +126,7 @@ int line_edit(WINDOW* dialog, int box_y, int box_x, int flen, int box_width, cht
|
||||
instr[scroll+input_x] = ' ';
|
||||
if (input_x == box_width-1) {
|
||||
scroll++;
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE);
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
||||
}
|
||||
else {
|
||||
wmove(dialog, box_y, input_x + box_x);
|
||||
@ -149,7 +149,7 @@ int line_edit(WINDOW* dialog, int box_y, int box_x, int flen, int box_width, cht
|
||||
input_x = oldscroll - 1 - scroll;
|
||||
} else
|
||||
input_x--;
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE);
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
||||
} else
|
||||
beep();
|
||||
continue;
|
||||
@ -163,7 +163,7 @@ int line_edit(WINDOW* dialog, int box_y, int box_x, int flen, int box_width, cht
|
||||
continue;
|
||||
}
|
||||
memmove(instr+scroll+input_x, instr+scroll+input_x+1, i-(scroll+input_x));
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE);
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
||||
continue;
|
||||
default:
|
||||
if (CCEQ(key, erase_char))
|
||||
@ -182,7 +182,7 @@ int line_edit(WINDOW* dialog, int box_y, int box_x, int flen, int box_width, cht
|
||||
scroll++;
|
||||
else
|
||||
input_x++;
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE);
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
|
||||
} else
|
||||
beep(); /* Alarm user about overflow */
|
||||
continue;
|
||||
@ -190,14 +190,14 @@ int line_edit(WINDOW* dialog, int box_y, int box_x, int flen, int box_width, cht
|
||||
}
|
||||
}
|
||||
ret:
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, TRUE);
|
||||
redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, TRUE, attr_mask);
|
||||
wrefresh(dialog);
|
||||
strcpy(result, instr);
|
||||
return key;
|
||||
}
|
||||
|
||||
static void
|
||||
redraw_field(WINDOW *dialog, int box_y, int box_x, int flen, int box_width, unsigned char instr[], int input_x, int scroll, chtype attr, chtype old_attr, int fexit)
|
||||
redraw_field(WINDOW *dialog, int box_y, int box_x, int flen, int box_width, unsigned char instr[], int input_x, int scroll, chtype attr, chtype old_attr, int fexit, int attr_mask)
|
||||
{
|
||||
int i, fix_len;
|
||||
|
||||
@ -205,9 +205,9 @@ redraw_field(WINDOW *dialog, int box_y, int box_x, int flen, int box_width, unsi
|
||||
wmove(dialog, box_y, box_x);
|
||||
fix_len = flen >= 0 ? MIN(flen-scroll,box_width) : box_width;
|
||||
for (i = 0; i < fix_len; i++)
|
||||
waddch(dialog, instr[scroll+i] ? instr[scroll+i] : ' ');
|
||||
waddch(dialog, instr[scroll+i] ? ((attr_mask & DITEM_NO_ECHO) ? '*' : instr[scroll+i]) : ' ');
|
||||
wattrset(dialog, old_attr);
|
||||
for ( ; i < box_width; i++)
|
||||
waddch(dialog, instr[scroll+i] ? instr[scroll+i] : ' ');
|
||||
waddch(dialog, instr[scroll+i] ? ((attr_mask & DITEM_NO_ECHO) ? '*' : instr[scroll+i]) : ' ');
|
||||
wmove(dialog, box_y, input_x + box_x);
|
||||
}
|
||||
|
@ -657,7 +657,7 @@ static int get_search_term(WINDOW *win, unsigned char *search_term, int height,
|
||||
|
||||
first = 1;
|
||||
while (key != ESC) {
|
||||
key = line_edit(win, y+1, x+1, -1, box_width-2, searchbox_attr, first, search_term);
|
||||
key = line_edit(win, y+1, x+1, -1, box_width-2, searchbox_attr, first, search_term, 0);
|
||||
first = 0;
|
||||
switch (key) {
|
||||
case '\n':
|
||||
|
@ -29,7 +29,6 @@
|
||||
|
||||
#define ESC 27
|
||||
|
||||
|
||||
/***********************************************************************
|
||||
*
|
||||
* Obj routines
|
||||
@ -239,6 +238,22 @@ DelObj(ComposeObj *Obj)
|
||||
*
|
||||
***********************************************************************/
|
||||
|
||||
static void
|
||||
outstr(WINDOW *win, char *str, int attrs)
|
||||
{
|
||||
if (attrs & DITEM_NO_ECHO) {
|
||||
char *cpy;
|
||||
int n = strlen(str);
|
||||
|
||||
cpy = alloca(n + 1);
|
||||
memset(cpy, '*', n);
|
||||
cpy[n] = '\0';
|
||||
waddstr(win, cpy);
|
||||
}
|
||||
else
|
||||
waddstr(win, str);
|
||||
}
|
||||
|
||||
void
|
||||
RefreshStringObj(StringObj *so)
|
||||
/*
|
||||
@ -256,9 +271,9 @@ RefreshStringObj(StringObj *so)
|
||||
wmove(so->win, so->y+2, so->x+1);
|
||||
if (strlen(so->s) > so->w-2) {
|
||||
strncpy(tmp, (char *) so->s + strlen(so->s) - so->w + 2, so->w - 1);
|
||||
waddstr(so->win, tmp);
|
||||
outstr(so->win, tmp, so->attr_mask);
|
||||
} else {
|
||||
waddstr(so->win, so->s);
|
||||
outstr(so->win, so->s, so->attr_mask);
|
||||
}
|
||||
|
||||
return;
|
||||
@ -292,6 +307,7 @@ NewStringObj(WINDOW *win, char *title, char *s, int y, int x, int w, int len)
|
||||
so->w = w;
|
||||
so->len = len;
|
||||
so->win = win;
|
||||
so->attr_mask = DialogInputAttrs; /* Grossly use a global to avoid changing API */
|
||||
|
||||
/* Draw it on the screen */
|
||||
RefreshStringObj(so);
|
||||
@ -310,7 +326,7 @@ SelectStringObj(StringObj *so)
|
||||
|
||||
strcpy(tmp, so->s);
|
||||
key = line_edit(so->win, so->y+2, so->x+1,
|
||||
so->len, so->w-2, inputbox_attr, TRUE, tmp);
|
||||
so->len, so->w-2, inputbox_attr, TRUE, tmp, so->attr_mask);
|
||||
if ((key == '\n') || (key == '\r') || (key == '\t') || key == (KEY_BTAB) ) {
|
||||
strcpy(so->s, tmp);
|
||||
}
|
||||
|
@ -53,6 +53,7 @@ typedef struct {
|
||||
int x, y, w, len; /* the (y, x) position of the upperleft */
|
||||
/* corner and the width <w> of the display */
|
||||
/* and length <len> of the field */
|
||||
int attr_mask; /* special attributes */
|
||||
} StringObj;
|
||||
|
||||
typedef struct {
|
||||
|
Loading…
Reference in New Issue
Block a user