File: input.c

package info (click to toggle)
iptraf 3.0.0-8.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 2,928 kB
  • ctags: 1,229
  • sloc: ansic: 12,563; sh: 151; makefile: 137; awk: 1
file content (207 lines) | stat: -rw-r--r-- 4,194 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/***

input.c - a custom keyboard input module     
Written by Gerard Paul Java
Copyright (c) Gerard Paul Java 1997

This module is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

***/

#include <curses.h>
#include <panel.h>
#include <ctype.h>
#include "input.h"

void tx_initfields(struct FIELDLIST *list, int leny, int lenx, int begy,
		int begx, int dlgtextattr, int fieldattr)
{
    list->list = NULL;
    list->fieldwin = newwin(leny, lenx, begy, begx);
    list->fieldpanel = new_panel(list->fieldwin);
    tx_stdwinset(list->fieldwin);
    wtimeout(list->fieldwin, -1);
    wattrset(list->fieldwin, dlgtextattr);
    tx_colorwin(list->fieldwin);
    update_panels();
    doupdate();
    
    list->dlgtextattr = dlgtextattr;
    list->fieldattr = fieldattr;
}

void tx_addfield(struct FIELDLIST *list, unsigned int len,
	      unsigned int y, unsigned int x, const char *initstr)
{
    struct FIELD *newfield;
    int i;

    newfield = malloc(sizeof(struct FIELD));

    if (list->list == NULL) {
	list->list = newfield;
	newfield->prevfield = newfield;
	newfield->nextfield = newfield;
    } else {
	newfield->prevfield = list->list->prevfield;
	list->list->prevfield->nextfield = newfield;
	list->list->prevfield = newfield;
	newfield->nextfield = list->list;
    }

    newfield->xpos = x;
    newfield->ypos = y;
    newfield->len = len;
    newfield->tlen = strlen(initstr);
    newfield->buf = malloc(len + 1);
    bzero(newfield->buf, len + 1);
    strncpy(newfield->buf, initstr, len);

    if (newfield->tlen > (len))
	newfield->tlen = len;

    wattrset(list->fieldwin, list->fieldattr);
    wmove(list->fieldwin, y, x);
    for (i = 1; i <= len; i++)
	wprintw(list->fieldwin, " ");

    wmove(list->fieldwin, y, x);
    wprintw(list->fieldwin, "%s", newfield->buf);

    update_panels();
    doupdate();
}

void tx_getinput(struct FIELDLIST *list, struct FIELD *field, int *exitkey)
{
    int ch;
    int y, x;
    int endloop = 0;

    wmove(list->fieldwin, field->ypos, field->xpos);
    wattrset(list->fieldwin, list->fieldattr);
    wprintw(list->fieldwin, "%s", field->buf);
    update_panels();
    doupdate();

    do {
	ch = wgetch(list->fieldwin);
	switch (ch) {
#ifndef DISABLEBS
	case KEY_BACKSPACE:
#endif
	case 7:
	case 8:
	case KEY_DC:
	case KEY_LEFT:
    case 127:
	    if (field->tlen > 0) {
		getyx(list->fieldwin, y, x);
		x--;
		wmove(list->fieldwin, y, x);
		wprintw(list->fieldwin, " ");
		wmove(list->fieldwin, y, x);
		field->tlen--;
		field->buf[field->tlen] = '\0';
	    }
	    break;
	case 9:
	case 27:
	case 24:
	case 13:
	case 10:
	case KEY_UP:
	case KEY_DOWN:
	    endloop = 1;
	    *exitkey = ch;

	    break;
	case 12:
	    tx_refresh_screen();
	    break;
	default:
	    if ((field->tlen < field->len) && ((ch >= 32) && (ch <= 127))) {
		wprintw(list->fieldwin, "%c", ch);
		if (ch == ' ') {
		    getyx(list->fieldwin, y, x);
		    wmove(list->fieldwin, y, x);
		}
		field->buf[field->tlen + 1] = '\0';
		field->buf[field->tlen] = ch;
		field->tlen++;
	    }
	    break;
	}

	doupdate();
    } while (!endloop);
}

void tx_fillfields(struct FIELDLIST *list, int *aborted)
{
    struct FIELD *field;
    int exitkey;
    int exitloop = 0;

    field = list->list;

    curs_set(1);
    do {
	tx_getinput(list, field, &exitkey);

	switch (exitkey) {
	case 9:
	case KEY_DOWN:
	    field = field->nextfield;
	    break;
	case KEY_UP:
	    field = field->prevfield;
	    break;
	case 13:
	case 10:
	    *aborted = 0;
	    exitloop = 1;
	    break;
	case 27:
	case 24:
	    *aborted = 1;
	    exitloop = 1;
	    break;
	}
    } while (!exitloop);

    curs_set(0);
}

void tx_destroyfields(struct FIELDLIST *list)
{
    struct FIELD *ptmp;
    struct FIELD *pnext;

    list->list->prevfield->nextfield = NULL;
    ptmp = list->list;
    pnext = list->list->nextfield;

    do {
	free(ptmp);

	ptmp = pnext;
	if (pnext != NULL) {
	    pnext = pnext->nextfield;
	}
    } while (ptmp != NULL);

    del_panel(list->fieldpanel);
    delwin(list->fieldwin);
}

char *tx_ltrim(char *str)
{
    char *cptr = str;

    while (isspace(*cptr))
        cptr++;

    return cptr;
}