2010-06-11 07:09:25 +00:00
|
|
|
/* print.c - Alpine Package Keeper (APK)
|
2010-03-05 08:13:25 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
|
2011-09-13 08:53:01 +00:00
|
|
|
* Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
|
2010-03-05 08:13:25 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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 http://www.gnu.org/ for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <errno.h>
|
2011-07-22 09:08:35 +00:00
|
|
|
#include <sys/ioctl.h>
|
2010-03-05 08:13:25 +00:00
|
|
|
|
|
|
|
#include "apk_defines.h"
|
|
|
|
#include "apk_print.h"
|
|
|
|
|
2013-06-17 13:47:49 +00:00
|
|
|
int apk_progress_fd;
|
2011-07-22 09:08:35 +00:00
|
|
|
static int apk_screen_width = 0;
|
2013-06-19 08:47:09 +00:00
|
|
|
static int apk_progress_force = 1;
|
2017-06-01 05:25:56 +00:00
|
|
|
static const char *apk_progress_char = "#";
|
2011-07-22 09:08:35 +00:00
|
|
|
|
|
|
|
void apk_reset_screen_width(void)
|
|
|
|
{
|
|
|
|
apk_screen_width = 0;
|
2013-06-19 08:47:09 +00:00
|
|
|
apk_progress_force = 1;
|
2011-07-22 09:08:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int apk_get_screen_width(void)
|
2010-03-05 08:13:25 +00:00
|
|
|
{
|
2011-07-22 09:08:35 +00:00
|
|
|
struct winsize w;
|
2017-06-01 05:25:56 +00:00
|
|
|
const char *lang;
|
|
|
|
const char *progress_char;
|
2010-03-05 08:13:25 +00:00
|
|
|
|
2011-07-22 09:08:35 +00:00
|
|
|
if (apk_screen_width == 0) {
|
2012-02-10 13:20:37 +00:00
|
|
|
apk_screen_width = 50;
|
2013-10-11 11:52:34 +00:00
|
|
|
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0 &&
|
2012-02-10 13:20:37 +00:00
|
|
|
w.ws_col > 50)
|
2011-07-22 09:08:35 +00:00
|
|
|
apk_screen_width = w.ws_col;
|
|
|
|
}
|
|
|
|
|
2017-06-01 05:25:56 +00:00
|
|
|
lang = getenv("LANG");
|
|
|
|
if (lang != NULL && strstr(lang, "UTF-8") != NULL)
|
|
|
|
apk_progress_char = "\u2588";
|
|
|
|
|
|
|
|
if ((progress_char = getenv("APK_PROGRESS_CHAR")) != NULL)
|
|
|
|
apk_progress_char = progress_char;
|
|
|
|
|
2011-07-22 09:08:35 +00:00
|
|
|
return apk_screen_width;
|
|
|
|
}
|
|
|
|
|
2013-06-19 08:47:09 +00:00
|
|
|
void apk_print_progress(size_t done, size_t total)
|
2013-06-17 13:47:49 +00:00
|
|
|
{
|
2013-06-19 08:47:09 +00:00
|
|
|
static size_t last_done = 0;
|
|
|
|
static int last_bar = 0, last_percent = 0;
|
|
|
|
int bar_width;
|
|
|
|
int bar = 0;
|
|
|
|
char buf[64]; /* enough for petabytes... */
|
|
|
|
int i, percent = 0;
|
|
|
|
|
|
|
|
if (last_done == done && !apk_progress_force)
|
|
|
|
return;
|
2013-06-17 13:47:49 +00:00
|
|
|
|
2013-06-19 08:47:09 +00:00
|
|
|
if (apk_progress_fd != 0) {
|
|
|
|
i = snprintf(buf, sizeof(buf), "%zu/%zu\n", done, total);
|
|
|
|
write(apk_progress_fd, buf, i);
|
|
|
|
}
|
|
|
|
last_done = done;
|
2013-06-17 13:47:49 +00:00
|
|
|
|
2013-06-19 08:47:09 +00:00
|
|
|
if (!(apk_flags & APK_PROGRESS))
|
2013-06-17 13:47:49 +00:00
|
|
|
return;
|
|
|
|
|
2017-06-01 05:25:56 +00:00
|
|
|
bar_width = apk_get_screen_width() - 6;
|
2013-06-19 08:47:09 +00:00
|
|
|
if (total > 0) {
|
|
|
|
bar = muldiv(bar_width, done, total);
|
|
|
|
percent = muldiv(100, done, total);
|
2013-06-17 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
2013-06-19 08:47:09 +00:00
|
|
|
if (bar == last_bar && percent == last_percent && !apk_progress_force)
|
|
|
|
return;
|
|
|
|
|
|
|
|
last_bar = bar;
|
|
|
|
last_percent = percent;
|
|
|
|
apk_progress_force = 0;
|
|
|
|
|
2017-06-01 05:25:56 +00:00
|
|
|
fprintf(stdout, "\e7%3i%% ", percent);
|
|
|
|
|
2013-06-19 08:47:09 +00:00
|
|
|
for (i = 0; i < bar; i++)
|
2017-06-01 05:25:56 +00:00
|
|
|
fputs(apk_progress_char, stdout);
|
2013-06-19 08:47:09 +00:00
|
|
|
for (; i < bar_width; i++)
|
2013-10-11 11:52:34 +00:00
|
|
|
fputc(' ', stdout);
|
2017-06-01 05:25:56 +00:00
|
|
|
|
2013-10-11 11:52:34 +00:00
|
|
|
fflush(stdout);
|
|
|
|
fputs("\e8\e[0K", stdout);
|
2013-06-17 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 09:08:35 +00:00
|
|
|
int apk_print_indented(struct apk_indent *i, apk_blob_t blob)
|
|
|
|
{
|
2011-09-09 17:06:10 +00:00
|
|
|
if (i->x + blob.len + 1 >= apk_get_screen_width())
|
|
|
|
i->x = printf("\n%*s" BLOB_FMT, i->indent, "", BLOB_PRINTF(blob)) - 1;
|
|
|
|
else if (i->x <= i->indent)
|
|
|
|
i->x += printf("%*s" BLOB_FMT, i->indent - i->x, "", BLOB_PRINTF(blob));
|
|
|
|
else
|
|
|
|
i->x += printf(" " BLOB_FMT, BLOB_PRINTF(blob));
|
2013-06-19 08:47:09 +00:00
|
|
|
apk_progress_force = 1;
|
2010-03-05 08:13:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void apk_print_indented_words(struct apk_indent *i, const char *text)
|
|
|
|
{
|
|
|
|
apk_blob_for_each_segment(APK_BLOB_STR(text), " ",
|
|
|
|
(apk_blob_cb) apk_print_indented, i);
|
|
|
|
}
|
|
|
|
|
2013-06-13 15:20:39 +00:00
|
|
|
void apk_print_indented_fmt(struct apk_indent *i, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char tmp[256];
|
|
|
|
size_t n;
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
n = vsnprintf(tmp, sizeof(tmp), fmt, va);
|
|
|
|
apk_print_indented(i, APK_BLOB_PTR_LEN(tmp, n));
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
2010-03-05 08:13:25 +00:00
|
|
|
const char *apk_error_str(int error)
|
|
|
|
{
|
|
|
|
if (error < 0)
|
|
|
|
error = -error;
|
|
|
|
switch (error) {
|
|
|
|
case ENOKEY:
|
|
|
|
return "UNTRUSTED signature";
|
|
|
|
case EKEYREJECTED:
|
|
|
|
return "BAD signature";
|
|
|
|
case EIO:
|
|
|
|
return "IO ERROR";
|
|
|
|
case EBADMSG:
|
|
|
|
return "BAD archive";
|
|
|
|
case ENOMSG:
|
|
|
|
return "archive does not contain expected data";
|
2013-06-17 11:24:34 +00:00
|
|
|
case ENOPKG:
|
2015-04-22 06:40:03 +00:00
|
|
|
return "could not find a repo which provides this package (check repositories file and run 'apk update')";
|
|
|
|
case ECONNABORTED:
|
|
|
|
return "network connection aborted";
|
|
|
|
case ECONNREFUSED:
|
|
|
|
return "could not connect to server (check repositories file)";
|
|
|
|
case ENETUNREACH:
|
|
|
|
return "network error (check Internet connection and firewall)";
|
|
|
|
case ENXIO:
|
|
|
|
return "DNS lookup error";
|
|
|
|
case EREMOTEIO:
|
2015-04-24 06:31:28 +00:00
|
|
|
return "remote server returned error (try 'apk update')";
|
2015-04-22 06:40:03 +00:00
|
|
|
case ETIMEDOUT:
|
|
|
|
return "operation timed out";
|
|
|
|
case EAGAIN:
|
|
|
|
return "temporary error (try again later)";
|
2015-04-24 06:31:28 +00:00
|
|
|
case EAPKBADURL:
|
2015-04-22 06:40:03 +00:00
|
|
|
return "invalid URL (check your repositories file)";
|
|
|
|
case EAPKSTALEINDEX:
|
2015-04-24 06:31:28 +00:00
|
|
|
return "package mentioned in index not found (try 'apk update')";
|
2010-03-05 08:13:25 +00:00
|
|
|
default:
|
|
|
|
return strerror(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-24 23:53:26 +00:00
|
|
|
static void log_internal(FILE *dest, const char *prefix, const char *format, va_list va)
|
2017-04-10 06:47:22 +00:00
|
|
|
{
|
2017-04-26 06:34:32 +00:00
|
|
|
if (dest != stdout)
|
|
|
|
fflush(stdout);
|
2017-04-10 06:47:22 +00:00
|
|
|
if (prefix != NULL)
|
|
|
|
fprintf(dest, "%s", prefix);
|
|
|
|
vfprintf(dest, format, va);
|
|
|
|
fprintf(dest, "\n");
|
|
|
|
fflush(dest);
|
|
|
|
apk_progress_force = 1;
|
|
|
|
}
|
|
|
|
|
2010-03-05 08:13:25 +00:00
|
|
|
void apk_log(const char *prefix, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
va_start(va, format);
|
2017-04-24 23:53:26 +00:00
|
|
|
log_internal(stdout, prefix, format, va);
|
2010-03-05 08:13:25 +00:00
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
2017-04-10 06:47:22 +00:00
|
|
|
void apk_log_err(const char *prefix, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
va_start(va, format);
|
2017-04-24 23:53:26 +00:00
|
|
|
log_internal(stderr, prefix, format, va);
|
2017-04-10 06:47:22 +00:00
|
|
|
va_end(va);
|
|
|
|
}
|