-
Notifications
You must be signed in to change notification settings - Fork 1
/
getarg.c
250 lines (222 loc) · 4.72 KB
/
getarg.c
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* Copyright (C) 1988-2018 Sean MacLennan */
#include "z.h"
/** @addtogroup zedit
* @{
*/
#define PNUMCOLS 3 /* default columns for pout */
#define PCOLSIZE 26 /* default column size */
/* globals for getarg */
int InPaw;
bool First;
int Pawcol, Pawlen, Pshift;
struct zbuff *Paw;
struct zbuff *Buff_save;
/* globals for getplete */
static char **Carray;
static int Csize, Cnum = 0, Cret;
void (*Nextpart)(void);
/* General purpose string argument input routine which recursively calls the
* editor through the PAW buffer.
* Returns 0 if ok, ABORT if user aborted, 1 if empty string.
* Prompt the user for an argument, with an optional default in arg.
* Only allow max chars.
* Arg is NOT overwritten if the user aborts, or returns a null string.
*/
bool _getarg(const char *prompt, char *arg, int max, bool tostart)
{
char *ptr;
int argp_save, arg_save, rc;
int tcol, trow;
tcol = Pcol; trow = Prow;
t_goto(Rowmax - 1, 0); /* display the prompt */
tprntstr(prompt);
Pawcol = Pcol = strlen(prompt); /* prompts are always simple ascii */
argp_save = Argp;
arg_save = Arg;
Buff_save = Curbuff;
Paw->bmode = Curbuff->bmode;
InPaw = true;
Curcmds = 1;
Pshift = 0;
Pawlen = max;
makepaw(arg, false);
if (tostart) {
btostart(Bbuff);
First = true;
}
while (InPaw == true)
execute();
if (InPaw != ABORT) {
/* get the argument */
btostart(Bbuff);
for (ptr = arg; !bisend(Bbuff) && Buff() != '\0'; bmove1(Bbuff))
*ptr++ = Buff();
*ptr = '\0';
rc = ptr == arg; /* set to 1 if string empty */
} else
rc = ABORT;
InPaw = false;
Insearch = false; /* used by Zcase when in search command */
Argp = argp_save;
Arg = arg_save;
zswitchto(Buff_save); /* go back */
Curbuff->bmode = Paw->bmode; /* mainly for EXACT mode */
t_goto(trow, tcol);
Curwdo->modeflags = INVALID;
Curcmds = 0;
clrpaw();
return rc;
}
bool getarg(const char *prompt, char *arg, int max)
{
return _getarg(prompt, arg, max, true);
}
/* General purpose ask for an argument with completion given a struct
* or char * array.
* Returns offset of entry in array if found, else -1.
*/
int getplete(const char *prompt, const char *def, char **array,
int size, int num)
{
char cmdstr[STRMAX + 1];
Carray = array;
Csize = size / sizeof(char *);
Cnum = num;
if (def)
strcpy(cmdstr, def);
else
*cmdstr = '\0';
if (getarg(prompt, cmdstr, STRMAX))
Cret = -1;
Cnum = 0;
return Cret;
}
static int p_row, p_col;
static void pclear(void)
{
int i;
struct wdo *wdo;
/* We can't use redisplay here */
foreachwdo(wdo)
wdo->modeflags = INVALID;
for (i = 0; i < Rowmax - 2; ++i) {
t_goto(i, 0);
tcleol();
}
invalidate_scrnmarks(0, Rowmax - 2);
p_row = p_col = 0;
}
static void pout(char *str)
{
t_goto(p_row, p_col * PCOLSIZE);
invalidate_scrnmarks(p_row, p_row + 1);
if (p_row < Rowmax - 2) {
tprntstr(str);
tcleol();
}
if (++p_col >= PNUMCOLS) {
++p_row;
p_col = 0;
}
}
static void pcmdplete(bool show)
{
char cmd[STRMAX + 1], **cca, *mstr = NULL;
int i = 0, len, len1 = 0, rc;
Cret = -1;
getbtxt(cmd, STRMAX);
len = strlen(cmd);
cca = Carray;
if (show && !len)
for (; i < Cnum; ++i, cca += Csize)
pout(*cca);
else
for (; i < Cnum && (rc = strncasecmp(*cca, cmd, len)) <= 0;
++i, cca += Csize)
if (rc == 0) {
if (show)
pout(*cca);
else if (mstr) {
Cret = -1;
len1 = nmatch(mstr, *cca);
} else {
Cret = i;
len1 = strlen(mstr = *cca);
}
}
if (show)
return;
if (mstr) {
if (len == len1) {
/* Help!!!!*/
pclear();
pcmdplete(true);
return;
}
strncpy(cmd, mstr, len1);
cmd[len1] = '\0';
makepaw(cmd, false);
} else
tbell();
}
/* Use instead of Zinsert when in PAW */
void pinsert(void)
{
struct mark tmark;
int width;
if (Cmd == '\t' && Cnum) {
pcmdplete(false);
return;
}
if (First) {
bempty(Bbuff);
First = false;
invalidate_scrnmarks(Rowmax - 1, Rowmax);
}
width = chwidth(Cmd, Pcol, false);
if (bgetcol(false, 0) + width <= Pawlen) {
Zinsert();
bmrktopnt(Bbuff, &tmark);
btoend(Bbuff);
if (bgetcol(false, 0) > Pawlen) {
/* Insert in middle pushed text past end */
bmove(Bbuff, -width);
bdelete(Bbuff, width);
}
bpnttomrk(Bbuff, &tmark);
} else
tbell();
}
/* Use instead of Znewline when in PAW */
void pnewline(void)
{
if (Cnum) {
char cmdstr[STRMAX + 1], **ptr;
int i, found = 0, len;
getbtxt(cmdstr, STRMAX);
len = strlen(cmdstr);
for (i = 0, ptr = Carray; i < Cnum; ++i, ptr += Csize)
if (strncasecmp(cmdstr, *ptr, len) == 0) {
Cret = i;
if (strcasecmp(cmdstr, *ptr) == 0) {
/* Exact match */
InPaw = false;
return;
}
++found;
}
if (found != 1) {
tbell();
return;
}
}
InPaw = false;
}
void Zpart(void)
{
if (Nextpart)
Nextpart();
else
tbell();
}
/* @} */