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 /* taken from polkitagenttextlistener.c */
264 tcgetattr (fileno (tty), &ts);
265 ots = ts;
266 ts.c_lflag &= ~(ECHONL);
267 tcsetattr (fileno (tty), TCSAFLUSH, &ts);
268
269 str = g_string_new (NULL);
270 while (TRUE) {
271 gint c;
272 c = getc (tty);
273 if (c == '\n') {
274 /* ok, done */
275 break;
276 } else if (c == EOF) {
277 g_warning ("Got unexpected EOF.");
278 break;
279 } else {
280 gchar c_safe = (gchar) c;
281 g_string_append_len (str, (const gchar *) &c_safe, 1);
282 }
283 }
284 tcsetattr (fileno (tty), TCSAFLUSH, &ots);
285 putc ('\n', tty);
286
287 fclose (tty);
288 out:
289 return str;
290 }
291
292
293 /**
294 * pk_console_get_prompt:
295 * @question: question to ask user
296 * @defaultyes: %TRUE if the default answer is yes
297 *
298 * Prompt the user with a yes/no question.
299 *
300 * Return value: %TRUE if the user accepted the question.
301 **/
302 gboolean
303 pk_console_get_prompt (const gchar *question, gboolean defaultyes)
304 {
305 gboolean ret = FALSE;
306 gboolean valid = FALSE;
307 gchar *prompt;
308 GString *string;
309
310 prompt = g_strdup_printf ("%s %s ",
311 question,
312 defaultyes ? "[Y/n]" : "[N/y]");
313 while (!valid) {
314 string = pk_readline_unbuffered (prompt);
315 if (string == NULL)
316 break;
317 if (string->len == 0) {
318 if (defaultyes) {
319 valid = TRUE;
320 ret = TRUE;
321 } else {
322 valid = TRUE;
323 ret = FALSE;
324 }
325 }
326 if (strcasecmp (string->str, "y") == 0 ||
327 strcasecmp (string->str, "yes") == 0) {
328 valid = TRUE;
329 ret = TRUE;
330 }
331 if (strcasecmp (string->str, "n") == 0 ||
332 strcasecmp (string->str, "no") == 0) {
333 valid = TRUE;
334 ret = FALSE;
335 }
336 g_string_free (string, TRUE);
337 }
338 g_free (prompt);
339 return ret;
340 }
341
342 /**
343 * pk_status_enum_to_localised_text:
344 * @status: a #PkStatusEnum
345 *
346 * Convert a #PkStatusEnum to a localized string suitable for showing to a user.
347 *
348 * Return value: a localized string.
349 **/
350 const gchar *
351 pk_status_enum_to_localised_text (PkStatusEnum status)
352 {
353 const gchar *text = NULL;
354 switch (status) {
355 case PK_STATUS_ENUM_UNKNOWN:
356 /* TRANSLATORS: This is when the transaction status is not known */
357 text = _("Unknown state");
358 break;
359 case PK_STATUS_ENUM_SETUP:
360 /* TRANSLATORS: transaction state, the daemon is in the process of starting */
361 text = _("Starting");
362 break;
363 case PK_STATUS_ENUM_WAIT:
364 /* TRANSLATORS: transaction state, the transaction is waiting for another to complete */
365 text = _("Waiting in queue");
366 break;
367 case PK_STATUS_ENUM_RUNNING:
368 /* TRANSLATORS: transaction state, just started */
369 text = _("Running");
370 break;
371 case PK_STATUS_ENUM_QUERY:
372 /* TRANSLATORS: transaction state, is querying data */
373 text = _("Querying");
374 break;
375 case PK_STATUS_ENUM_INFO:
376 /* TRANSLATORS: transaction state, getting data from a server */
377 text = _("Getting information");
378 break;
379 case PK_STATUS_ENUM_REMOVE:
380 /* TRANSLATORS: transaction state, removing packages */
381 text = _("Removing packages");
382 break;
383 case PK_STATUS_ENUM_DOWNLOAD:
384 /* TRANSLATORS: transaction state, downloading package files */
385 text = _("Downloading packages");
386 break;
387 case PK_STATUS_ENUM_INSTALL:
388 /* TRANSLATORS: transaction state, installing packages */
389 text = _("Installing packages");
390 break;
391 case PK_STATUS_ENUM_REFRESH_CACHE:
392 /* TRANSLATORS: transaction state, refreshing internal lists */
393 text = _("Refreshing software list");
394 break;
395 case PK_STATUS_ENUM_UPDATE:
396 /* TRANSLATORS: transaction state, installing updates */
397 text = _("Installing updates");
398 break;
399 case PK_STATUS_ENUM_CLEANUP:
400 /* TRANSLATORS: transaction state, removing old packages, and cleaning config files */
401 text = _("Cleaning up packages");
402 break;
403 case PK_STATUS_ENUM_OBSOLETE:
404 /* TRANSLATORS: transaction state, obsoleting old packages */
405 text = _("Obsoleting packages");
406 break;
407 case PK_STATUS_ENUM_DEP_RESOLVE:
408 /* TRANSLATORS: transaction state, checking the transaction before we do it */
409 text = _("Resolving dependencies");
410 break;
411 case PK_STATUS_ENUM_SIG_CHECK:
412 /* TRANSLATORS: transaction state, checking if we have all the security keys for the operation */
413 text = _("Checking signatures");
414 break;
415 case PK_STATUS_ENUM_TEST_COMMIT:
416 /* TRANSLATORS: transaction state, when we're doing a test transaction */
417 text = _("Testing changes");
418 break;
419 case PK_STATUS_ENUM_COMMIT:
420 /* TRANSLATORS: transaction state, when we're writing to the system package database */
421 text = _("Committing changes");
422 break;
423 case PK_STATUS_ENUM_REQUEST:
424 /* TRANSLATORS: transaction state, requesting data from a server */
425 text = _("Requesting data");
426 break;
427 case PK_STATUS_ENUM_FINISHED:
428 /* TRANSLATORS: transaction state, all done! */
429 text = _("Finished");
430 break;
431 case PK_STATUS_ENUM_CANCEL:
432 /* TRANSLATORS: transaction state, in the process of cancelling */
433 text = _("Cancelling");
434 break;
435 case PK_STATUS_ENUM_DOWNLOAD_REPOSITORY:
436 /* TRANSLATORS: transaction state, downloading metadata */
437 text = _("Downloading repository information");
438 break;
439 case PK_STATUS_ENUM_DOWNLOAD_PACKAGELIST:
440 /* TRANSLATORS: transaction state, downloading metadata */
441 text = _("Downloading list of packages");
442 break;
443 case PK_STATUS_ENUM_DOWNLOAD_FILELIST:
444 /* TRANSLATORS: transaction state, downloading metadata */
445 text = _("Downloading file lists");
446 break;
447 case PK_STATUS_ENUM_DOWNLOAD_CHANGELOG:
448 /* TRANSLATORS: transaction state, downloading metadata */
449 text = _("Downloading lists of changes");
450 break;
451 case PK_STATUS_ENUM_DOWNLOAD_GROUP:
452 /* TRANSLATORS: transaction state, downloading metadata */
453 text = _("Downloading groups");
454 break;
455 case PK_STATUS_ENUM_DOWNLOAD_UPDATEINFO:
456 /* TRANSLATORS: transaction state, downloading metadata */
457 text = _("Downloading update information");
458 break;
459 case PK_STATUS_ENUM_REPACKAGING:
460 /* TRANSLATORS: transaction state, repackaging delta files */
461 text = _("Repackaging files");
462 break;
463 case PK_STATUS_ENUM_LOADING_CACHE:
464 /* TRANSLATORS: transaction state, loading databases */
465 text = _("Loading cache");
466 break;
467 case PK_STATUS_ENUM_SCAN_APPLICATIONS:
468 /* TRANSLATORS: transaction state, scanning for running processes */
469 text = _("Scanning applications");
470 break;
471 case PK_STATUS_ENUM_GENERATE_PACKAGE_LIST:
472 /* TRANSLATORS: transaction state, generating a list of packages installed on the system */
473 text = _("Generating package lists");
474 break;
475 case PK_STATUS_ENUM_WAITING_FOR_LOCK:
476 /* TRANSLATORS: transaction state, when we're waiting for the native tools to exit */
477 text = _("Waiting for package manager lock");
478 break;
479 case PK_STATUS_ENUM_WAITING_FOR_AUTH:
480 /* TRANSLATORS: transaction state, waiting for user to type in a password */
481 text = _("Waiting for authentication");
482 break;
483 case PK_STATUS_ENUM_SCAN_PROCESS_LIST:
484 /* TRANSLATORS: transaction state, we are updating the list of processes */
485 text = _("Updating running applications");
486 break;
487 case PK_STATUS_ENUM_CHECK_EXECUTABLE_FILES:
488 /* TRANSLATORS: transaction state, we are checking executable files currently in use */
489 text = _("Checking applications in use");
490 break;
491 case PK_STATUS_ENUM_CHECK_LIBRARIES:
492 /* TRANSLATORS: transaction state, we are checking for libraries currently in use */
493 text = _("Checking libraries in use");
494 break;
495 case PK_STATUS_ENUM_COPY_FILES:
496 /* TRANSLATORS: transaction state, we are copying package files before or after the transaction */
497 text = _("Copying files");
498 break;
499 case PK_STATUS_ENUM_RUN_HOOK:
500 /* TRANSLATORS: transaction state, we are running hooks before or after the transaction */
501 text = _("Running hooks");
502 break;
503 default:
504 g_warning ("status unrecognised: %s", pk_status_enum_to_string (status));
505 }
506 return text;
507 }