-
Notifications
You must be signed in to change notification settings - Fork 0
/
ask.c
306 lines (238 loc) · 7.88 KB
/
ask.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
Copyright (C) 2000 - 2008 Pawel A. Gajda <mis@pld-linux.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation (see file COPYING for details).
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <trurl/trurl.h>
#include "compiler.h"
#include "i18n.h"
#include "log.h"
#include "poldek_term.h"
#include "poldek_ts.h"
#include "poldek_intern.h"
#include "pkg.h"
#include "capreq.h"
#define msg_ask(fmt, args...) poldek_log(LOGTTY|LOGINFO, fmt, ## args)
static int term_confirm(void *foo, const struct poldek_ts *ts, int hint,
const char *question)
{
const char *yn = "[Y/n]";
int a;
foo = foo;
ts = ts;
if (!isatty(STDIN_FILENO))
return hint;
if (hint == 0) /* no */
yn = "[N/y]";
msg_ask("%s %s", question, yn);
a = poldek_term_ask(STDIN_FILENO, "YyNn\n", NULL);
a = toupper(a);
switch(a) {
case 'Y': a = 1; break;
case 'N': a = 0; break;
case '\n': a = hint; break;
default:
n_assert(0);
}
msg_ask("_ %c\n", a ? 'y' : 'n');
return a;
}
static int term_ts_confirm(void *foo, const struct poldek_ts *ts)
{
int answer = 1;
foo = foo;
/* poldek__ts_display_summary(ts); */ /* already displayed */
if (ts->type == POLDEK_TS_UNINSTALL) {
if (ts->getop(ts, POLDEK_OP_CONFIRM_UNINST))
answer = term_confirm(foo, ts, 0, _("Proceed?"));
} else {
if (ts->getop(ts, POLDEK_OP_CONFIRM_INST))
answer = term_confirm(foo, ts, 1, _("Proceed?"));
}
return answer;
}
static int term_choose_equiv(void *foo, const struct poldek_ts *ts,
const struct pkg *pkg, const char *capname,
tn_array *candidates, int hint)
{
char *validchrs, *p;
int i, j, a, lines;
char choice[] = "abcdefghijklmnopqrtsuvwxyz1234567890ABCDEFGHIJKLMNOPRSTUVWXYZ";
foo = foo; ts = ts;
j = 0, lines = 0;
lines = poldek_term_get_height();
if (lines <= 0) lines = 30;
lines -= 6; // to show some info above
if (lines < 6) lines = 6;
if (lines > sizeof(choice)) lines = sizeof(choice);
if (hint >= lines) /* over ascii */
j = (hint/lines)*lines; // first show page with hint
if (!isatty(STDIN_FILENO))
return hint;
if (pkg) {
msg_ask(_("%s: required \"%s\" is provided by the following packages:"),
pkg_id(pkg), capname);
} else {
msg_ask(_("Required \"%s\" is provided by the following packages:"),
capname);
}
msg_ask("_\n");
validchrs = alloca(100);
onemoretime:
memset(&validchrs[0], 0, sizeof(validchrs));
p = validchrs;
*p++ = '\n';
if (j > 0) {
msgn(-1, _("-/backspace/pgup) page up"));
*p++ = '-';
*p++ = 0x7f; // backspace
}
for (i = 0; i+j < n_array_size(candidates); i++) {
msgn(-1, "%c) %s", choice[i], pkg_id(n_array_nth(candidates, i+j)));
*p++ = choice[i];
if (i > lines - 1)
break;
}
if (i+j < n_array_size(candidates)) {
msgn(-1, _("+/space/tab/pgdown) page down"));
*p++ = '+';
*p++ = ' '; // space
*p++ = '\t'; // tab
}
*p++ = 'Q';
msg_ask(_("Which one do you want to install ('Q' to abort)? [%s]"), pkg_id(n_array_nth(candidates, hint)));
a = poldek_term_ask(STDIN_FILENO, validchrs, NULL);
msg_ask("_\n");
if (a == '-' || a == 0x7f) {
if (j >= lines) j -= lines;
goto onemoretime;
}
if (a == '+' || a == ' ' || a == '\t' ) {
if (j + lines < n_array_size(candidates)) j += lines;
goto onemoretime;
}
if (a == '\n')
return hint;
if (a == 'Q')
return -1;
a = strchr(choice, a) - choice;
if (a >= 0 && a <= i)
return a + j;
return hint;
}
static int term_choose_suggests(void *foo, const struct poldek_ts *ts,
const struct pkg *pkg, tn_array *caps,
tn_array *choices, int hint)
{
char message[512], *question;
char *yns = "N/y/s", *yn = "N/y";
int i, a, ac;
foo = foo; ts = ts;
if (!isatty(STDIN_FILENO))
return hint;
if (hint) {
yns = "Y/n/s";
yn = "Y/n";
}
n_snprintf(message, sizeof(message),
_("Package %s suggests installation of:"), pkg_id(pkg));
question = ngettext("Try to install it?", "Try to install them?",
n_array_size(caps));
msg_ask("%s\n", message);
for (i=0; i < n_array_size(caps); i++) {
struct capreq *cap = n_array_nth(caps, i);
msgn(-1, "%d. %s", i + 1, capreq_stra(cap));
}
if (n_array_size(caps) > 1) {
msg_ask("%s ", question);
msg_ask(_("(y - all, n - nothing, s - select some of)? [%s]"),
yns);
a = poldek_term_ask(STDIN_FILENO, "YyNnSs\n", NULL);
} else {
msg_ask("%s [%s]", question, yn);
a = poldek_term_ask(STDIN_FILENO, "YyNn\n", NULL);
}
a = toupper(a);
switch(a) {
case 'Y': a = 1; ac = 'y'; break;
case 'N': a = 0; ac = 'n'; break;
case 'S': a = 2; ac = 's'; break;
case '\n': a = hint; ac = hint ? 'y':'n'; break;
default:
n_assert(0);
}
msg_ask("_ %c\n", ac);
if (a == 2) {
for (i=0; i < n_array_size(caps); i++) {
struct capreq *cap = n_array_nth(caps, i);
char q[512];
n_snprintf(q, sizeof(q), _("Try to install %s?"), capreq_stra(cap));
if (term_confirm(NULL, NULL, 1, q))
n_array_push(choices, cap);
}
}
return a;
}
int poldek__confirm(const struct poldek_ts *ts, int hint, const char *message)
{
if (ts->ctx->confirm_fn == NULL)
return hint;
return ts->ctx->confirm_fn(ts->ctx->data_confirm_fn, ts, hint, message);
}
int poldek__ts_confirm(const struct poldek_ts *ts)
{
if (ts->ctx->ts_confirm_fn == NULL)
return 1;
return ts->ctx->ts_confirm_fn(ts->ctx->data_ts_confirm_fn, ts);
}
int poldek__choose_equiv(const struct poldek_ts *ts,
const struct pkg *pkg, const char *capname,
tn_array *pkgs, struct pkg *hint)
{
int i, inthint = 0;
if (hint) {
for (i=0; i < n_array_size(pkgs); i++) {
if (hint && hint == n_array_nth(pkgs, i)) {
inthint = i;
break;
}
}
}
if (ts->ctx->choose_equiv_fn == NULL)
return inthint;
return ts->ctx->choose_equiv_fn(ts->ctx->data_choose_equiv_fn,
ts, pkg, capname, pkgs, inthint);
}
int poldek__choose_suggests(const struct poldek_ts *ts,
const struct pkg *pkg, tn_array *caps,
tn_array *choices, int hint)
{
if (ts->ctx->choose_suggests_fn == NULL)
return hint;
return ts->ctx->choose_suggests_fn(ts->ctx->data_choose_suggests_fn,
ts, pkg, caps, choices, hint);
}
void poldek__setup_default_ask_callbacks(struct poldek_ctx *ctx)
{
ctx->data_confirm_fn = NULL;
ctx->confirm_fn = term_confirm;
ctx->data_ts_confirm_fn = NULL;
ctx->ts_confirm_fn = term_ts_confirm;
ctx->data_choose_equiv_fn = NULL;
ctx->choose_equiv_fn = term_choose_equiv;
ctx->data_choose_suggests_fn = NULL;
ctx->choose_suggests_fn = term_choose_suggests;
}