Debian Codegraph @

lib/packagekit-glib2/pk-console-private.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2008-2009 Richard Hughes <[email protected]>
4 * Copyright (C) 2014-2026 Matthias Klumpp <[email protected]>
5 *
6 * Licensed under the GNU Lesser General Public License Version 2.1
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "config.h"
24
25 #include <termios.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <wchar.h>
30 #include <strings.h>
31 #include <gio/gio.h>
32 #include <glib/gi18n.h>
33 #include <packagekit-glib2/pk-enum.h>
34
35 #include "pk-console-private.h"
36
37 /**
38 * pk_console_str_width:
39 * @text: the UTF-8 string to measure
40 *
41 * Calculate the display width of a UTF-8 string in terminal columns.
42 *
43 * Returns: the display width in terminal columns
44 */
45 guint
46 pk_console_str_width (const gchar *text)
47 {
48 guint width = 0;
49 const gchar *p;
50 wchar_t wc;
51 gint char_width;
52 mbstate_t state;
53
54 if (text == NULL)
55 return 0;
56
57 memset (&state, 0, sizeof (mbstate_t));
58 p = text;
59
60 while (*p != '\0') {
61 size_t bytes = mbrtowc (&wc, p, MB_CUR_MAX, &state);
62
63 if (bytes == (size_t)-1 || bytes == (size_t)-2) {
64 /* Invalid multibyte sequence, treat as single-width */
65 width++;
66 p++;
67 memset (&state, 0, sizeof (mbstate_t));
68 continue;
69 }
70
71 if (bytes == 0)
72 break;
73
74 char_width = wcwidth (wc);
75 if (char_width < 0) {
76 /* Non-printable character, treat as zero-width */
77 char_width = 0;
78 }
79
80 width += char_width;
81 p += bytes;
82 }
83
84 return width;
85 }
86
87 /**
88 * pk_console_text_truncate:
89 * @text: the UTF-8 string to truncate
90 * @max_width: maximum display width in terminal columns
91 *
92 * Truncate a UTF-8 string to fit within a given display width
93 */
94 gchar*
95 pk_console_text_truncate (const gchar *text, guint max_width)
96 {
97 const gchar *p;
98 guint width = 0;
99 wchar_t wc;
100 gint char_width;
101 mbstate_t state;
102 guint target_width;
103 gboolean use_ellipsis;
104 gsize byte_len;
105 gchar *result;
106
107 if (text == NULL)
108 return g_strdup ("");
109
110 if (max_width == 0)
111 return g_strdup ("");
112
113 /* Check if the string already fits */
114 if (pk_console_str_width (text) <= max_width)
115 return g_strdup (text);
116
117 /* String needs truncation */
118 memset (&state, 0, sizeof (mbstate_t));
119 p = text;
120
121 /* Reserve space for ellipsis if possible */
122 target_width = max_width;
123 use_ellipsis = max_width > 3;
124 if (use_ellipsis)
125 target_width = max_width - 3;
126
127 while (*p != '\0') {
128 size_t bytes = mbrtowc (&wc, p, MB_CUR_MAX, &state);
129
130 if (bytes == (size_t)-1 || bytes == (size_t)-2) {
131 /* Invalid multibyte sequence */
132 if (width + 1 > target_width)
133 break;
134 width++;
135 p++;
136 memset (&state, 0, sizeof (mbstate_t));
137 continue;
138 }
139
140 if (bytes == 0)
141 break;
142
143 char_width = wcwidth (wc);
144 if (char_width < 0)
145 char_width = 0;
146
147 if (width + char_width > target_width)
148 break;
149
150 width += char_width;
151 p += bytes;
152 }
153
154 byte_len = p - text;
155 if (use_ellipsis) {
156 result = g_malloc0 (byte_len + 4);
157 memcpy (result, text, byte_len);
158 memcpy (result + byte_len, "...", 3);
159 return result;
160 }
161
162 return g_strndup (text, byte_len);
163 }
164
165 /**
166 * pk_console_strpad:
167 * @text: the UTF-8 string to pad
168 * @width: desired display width in terminal columns
169 *
170 * Pad a UTF-8 string to exactly the specified display width
171 */
172 gchar *
173 pk_console_strpad (const gchar *text, guint width)
174 {
175 guint display_width;
176 guint padding_needed;
177 g_autofree gchar *padding = NULL;
178
179 if (text == NULL)
180 return g_strnfill (width, ' ');
181
182 /* Calculate actual display width */
183 display_width = pk_console_str_width (text);
184
185 if (display_width >= width)
186 return g_strdup (text);
187
188 /* Add padding to reach desired width */
189 padding_needed = width - display_width;
190
191 padding = g_strnfill (padding_needed, ' ');
192 return g_strdup_printf ("%s%s", text, padding);
193 }
194
195 /**
196 * pk_console_get_number:
197 * @question: question to ask user
198 * @maxnum: maximum number allowed
199 *
200 * Get a number from the console.
201 *
202 * Return value: a number entered by the user.
203 **/
204 guint
205 pk_console_get_number (const gchar *question, guint maxnum)
206 {
207 gint answer = 0;
208 gint retval;
209
210 /* pretty print */
211 g_print ("%s", question);
212
213 do {
214 char buffer[64];
215
216 /* swallow the \n at end of line too */
217 if (!fgets (buffer, sizeof (buffer), stdin))
218 break;
219 if (strlen (buffer) == sizeof (buffer) - 1)
220 continue;
221
222 /* get a number */
223 retval = sscanf (buffer, "%u", &answer);
224
225 /* positive */
226 if (retval == 1 && answer > 0 && answer <= (gint) maxnum)
227 break;
228 g_print (_("Please enter a number from 1 to %i: "), maxnum);
229 } while (TRUE);
230 return answer;
231 }
232
233
234 /*
235 * pk_readline_unbuffered:
236 **/
237 static GString *
238 pk_readline_unbuffered (const gchar *prompt)
239 {
240 const gchar *tty_name;
241 FILE *tty;
242 GString *str = NULL;
243 struct termios ts, ots;
244
245 tty_name = ctermid (NULL);
246 if (tty_name == NULL) {
247 g_warning ("Cannot get terminal: %s",
248 strerror (errno));
249 goto out;
250 }
251
252 tty = fopen (tty_name, "r+");
253 if (tty == NULL) {
254 g_warning ("Error opening terminal for the process (`%s'): %s",
255 tty_name, strerror (errno));
256 goto out;
257 }
258
259 fprintf (tty, "%s", prompt);
260 fflush (tty);
261 setbuf (tty, NULL);
262
263 /* based on polkitagenttextlistener.c, but use TCSADRAIN rather than
264 * TCSAFLUSH: we only toggle ECHONL here, so there is no reason to discard
265 * input that has already been queued. TCSAFLUSH would race with anything
266 * that answers the prompt the moment it appears (e.g. the test harness or
267 * piped input) and silently drop the reply, leaving us blocked in getc(). */
268 tcgetattr (fileno (tty), &ts);
269 ots = ts;
270 ts.c_lflag &= ~(ECHONL);
271 tcsetattr (fileno (tty), TCSADRAIN, &ts);
272
273 str = g_string_new (NULL);
274 while (TRUE) {
275 gint c;
276 c = getc (tty);
277 if (c == '\n') {
278 /* ok, done */
279 break;
280 } else if (c == EOF) {
281 g_warning ("Got unexpected EOF.");
282 break;
283 } else {
284 gchar c_safe = (gchar) c;
285 g_string_append_len (str, (const gchar *) &c_safe, 1);
286 }
287 }
288 tcsetattr (fileno (tty), TCSAFLUSH, &ots);
289 putc ('\n', tty);
290
291 fclose (tty);
292 out:
293 return str;
294 }
295
296
297 /**
298 * pk_console_get_prompt:
299 * @question: question to ask user
300 * @defaultyes: %TRUE if the default answer is yes
301 *
302 * Prompt the user with a yes/no question.
303 *
304 * Return value: %TRUE if the user accepted the question.
305 **/
306 gboolean
307 pk_console_get_prompt (const gchar *question, gboolean defaultyes)
308 {
309 gboolean ret = FALSE;
310 gboolean valid = FALSE;
311 gchar *prompt;
312 GString *string;
313
314 prompt = g_strdup_printf ("%s %s ",
315 question,
316 defaultyes ? "[Y/n]" : "[N/y]");
317 while (!valid) {
318 string = pk_readline_unbuffered (prompt);
319 if (string == NULL)
320 break;
321 if (string->len == 0) {
322 if (defaultyes) {
323 valid = TRUE;
324 ret = TRUE;
325 } else {
326 valid = TRUE;
327 ret = FALSE;
328 }
329 }
330 if (strcasecmp (string->str, "y") == 0 ||
331 strcasecmp (string->str, "yes") == 0) {
332 valid = TRUE;
333 ret = TRUE;
334 }
335 if (strcasecmp (string->str, "n") == 0 ||
336 strcasecmp (string->str, "no") == 0) {
337 valid = TRUE;
338 ret = FALSE;
339 }
340 g_string_free (string, TRUE);
341 }
342 g_free (prompt);
343 return ret;
344 }
345
346 /**
347 * pk_status_enum_to_localised_text:
348 * @status: a #PkStatusEnum
349 *
350 * Convert a #PkStatusEnum to a localized string suitable for showing to a user.
351 *
352 * Return value: a localized string.
353 **/
354 const gchar *
355 pk_status_enum_to_localised_text (PkStatusEnum status)
356 {
357 const gchar *text = NULL;
358 switch (status) {
359 case PK_STATUS_ENUM_UNKNOWN:
360 /* TRANSLATORS: This is when the transaction status is not known */
361 text = _("Unknown state");
362 break;
363 case PK_STATUS_ENUM_SETUP:
364 /* TRANSLATORS: transaction state, the daemon is in the process of starting */
365 text = _("Starting");
366 break;
367 case PK_STATUS_ENUM_WAIT:
368 /* TRANSLATORS: transaction state, the transaction is waiting for another to complete */
369 text = _("Waiting in queue");
370 break;
371 case PK_STATUS_ENUM_RUNNING:
372 /* TRANSLATORS: transaction state, just started */
373 text = _("Running");
374 break;
375 case PK_STATUS_ENUM_QUERY:
376 /* TRANSLATORS: transaction state, is querying data */
377 text = _("Querying");
378 break;
379 case PK_STATUS_ENUM_INFO:
380 /* TRANSLATORS: transaction state, getting data from a server */
381 text = _("Getting information");
382 break;
383 case PK_STATUS_ENUM_REMOVE:
384 /* TRANSLATORS: transaction state, removing packages */
385 text = _("Removing packages");
386 break;
387 case PK_STATUS_ENUM_DOWNLOAD:
388 /* TRANSLATORS: transaction state, downloading package files */
389 text = _("Downloading packages");
390 break;
391 case PK_STATUS_ENUM_INSTALL:
392 /* TRANSLATORS: transaction state, installing packages */
393 text = _("Installing packages");
394 break;
395 case PK_STATUS_ENUM_REFRESH_CACHE:
396 /* TRANSLATORS: transaction state, refreshing internal lists */
397 text = _("Refreshing software list");
398 break;
399 case PK_STATUS_ENUM_UPDATE:
400 /* TRANSLATORS: transaction state, installing updates */
401 text = _("Installing updates");
402 break;
403 case PK_STATUS_ENUM_CLEANUP:
404 /* TRANSLATORS: transaction state, removing old packages, and cleaning config files */
405 text = _("Cleaning up packages");
406 break;
407 case PK_STATUS_ENUM_OBSOLETE:
408 /* TRANSLATORS: transaction state, obsoleting old packages */
409 text = _("Obsoleting packages");
410 break;
411 case PK_STATUS_ENUM_DEP_RESOLVE:
412 /* TRANSLATORS: transaction state, checking the transaction before we do it */
413 text = _("Resolving dependencies");
414 break;
415 case PK_STATUS_ENUM_SIG_CHECK:
416 /* TRANSLATORS: transaction state, checking if we have all the security keys for the operation */
417 text = _("Checking signatures");
418 break;
419 case PK_STATUS_ENUM_TEST_COMMIT:
420 /* TRANSLATORS: transaction state, when we're doing a test transaction */
421 text = _("Testing changes");
422 break;
423 case PK_STATUS_ENUM_COMMIT:
424 /* TRANSLATORS: transaction state, when we're writing to the system package database */
425 text = _("Committing changes");
426 break;
427 case PK_STATUS_ENUM_REQUEST:
428 /* TRANSLATORS: transaction state, requesting data from a server */
429 text = _("Requesting data");
430 break;
431 case PK_STATUS_ENUM_FINISHED:
432 /* TRANSLATORS: transaction state, all done! */
433 text = _("Finished");
434 break;
435 case PK_STATUS_ENUM_CANCEL:
436 /* TRANSLATORS: transaction state, in the process of cancelling */
437 text = _("Cancelling");
438 break;
439 case PK_STATUS_ENUM_DOWNLOAD_REPOSITORY:
440 /* TRANSLATORS: transaction state, downloading metadata */
441 text = _("Downloading repository information");
442 break;
443 case PK_STATUS_ENUM_DOWNLOAD_PACKAGELIST:
444 /* TRANSLATORS: transaction state, downloading metadata */
445 text = _("Downloading list of packages");
446 break;
447 case PK_STATUS_ENUM_DOWNLOAD_FILELIST:
448 /* TRANSLATORS: transaction state, downloading metadata */
449 text = _("Downloading file lists");
450 break;
451 case PK_STATUS_ENUM_DOWNLOAD_CHANGELOG:
452 /* TRANSLATORS: transaction state, downloading metadata */
453 text = _("Downloading lists of changes");
454 break;
455 case PK_STATUS_ENUM_DOWNLOAD_GROUP:
456 /* TRANSLATORS: transaction state, downloading metadata */
457 text = _("Downloading groups");
458 break;
459 case PK_STATUS_ENUM_DOWNLOAD_UPDATEINFO:
460 /* TRANSLATORS: transaction state, downloading metadata */
461 text = _("Downloading update information");
462 break;
463 case PK_STATUS_ENUM_REPACKAGING:
464 /* TRANSLATORS: transaction state, repackaging delta files */
465 text = _("Repackaging files");
466 break;
467 case PK_STATUS_ENUM_LOADING_CACHE:
468 /* TRANSLATORS: transaction state, loading databases */
469 text = _("Loading cache");
470 break;
471 case PK_STATUS_ENUM_SCAN_APPLICATIONS:
472 /* TRANSLATORS: transaction state, scanning for running processes */
473 text = _("Scanning applications");
474 break;
475 case PK_STATUS_ENUM_GENERATE_PACKAGE_LIST:
476 /* TRANSLATORS: transaction state, generating a list of packages installed on the system */
477 text = _("Generating package lists");
478 break;
479 case PK_STATUS_ENUM_WAITING_FOR_LOCK:
480 /* TRANSLATORS: transaction state, when we're waiting for the native tools to exit */
481 text = _("Waiting for package manager lock");
482 break;
483 case PK_STATUS_ENUM_WAITING_FOR_AUTH:
484 /* TRANSLATORS: transaction state, waiting for user to type in a password */
485 text = _("Waiting for authentication");
486 break;
487 case PK_STATUS_ENUM_SCAN_PROCESS_LIST:
488 /* TRANSLATORS: transaction state, we are updating the list of processes */
489 text = _("Updating running applications");
490 break;
491 case PK_STATUS_ENUM_CHECK_EXECUTABLE_FILES:
492 /* TRANSLATORS: transaction state, we are checking executable files currently in use */
493 text = _("Checking applications in use");
494 break;
495 case PK_STATUS_ENUM_CHECK_LIBRARIES:
496 /* TRANSLATORS: transaction state, we are checking for libraries currently in use */
497 text = _("Checking libraries in use");
498 break;
499 case PK_STATUS_ENUM_COPY_FILES:
500 /* TRANSLATORS: transaction state, we are copying package files before or after the transaction */
501 text = _("Copying files");
502 break;
503 case PK_STATUS_ENUM_RUN_HOOK:
504 /* TRANSLATORS: transaction state, we are running hooks before or after the transaction */
505 text = _("Running hooks");
506 break;
507 default:
508 g_warning ("status unrecognised: %s", pk_status_enum_to_string (status));
509 }
510 return text;
511 }