print.c 4.06 KB
Newer Older
1
/* print.c - Alpine Package Keeper (APK)
Natanael Copa's avatar
Natanael Copa committed
2 3
 *
 * Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
4
 * Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
Natanael Copa's avatar
Natanael Copa committed
5 6 7 8 9 10 11 12 13 14 15 16
 * 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>
Timo Teräs's avatar
Timo Teräs committed
17
#include <sys/ioctl.h>
Natanael Copa's avatar
Natanael Copa committed
18 19 20 21

#include "apk_defines.h"
#include "apk_print.h"

22
int apk_progress_fd;
Timo Teräs's avatar
Timo Teräs committed
23
static int apk_screen_width = 0;
24
static int apk_progress_force = 1;
Timo Teräs's avatar
Timo Teräs committed
25 26 27 28

void apk_reset_screen_width(void)
{
	apk_screen_width = 0;
29
	apk_progress_force = 1;
Timo Teräs's avatar
Timo Teräs committed
30 31 32
}

int apk_get_screen_width(void)
Natanael Copa's avatar
Natanael Copa committed
33
{
Timo Teräs's avatar
Timo Teräs committed
34
	struct winsize w;
Natanael Copa's avatar
Natanael Copa committed
35

Timo Teräs's avatar
Timo Teräs committed
36
	if (apk_screen_width == 0) {
37
		apk_screen_width = 50;
38
		if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0 &&
39
		    w.ws_col > 50)
Timo Teräs's avatar
Timo Teräs committed
40 41 42 43 44 45
			apk_screen_width = w.ws_col;
	}

	return apk_screen_width;
}

46
void apk_print_progress(size_t done, size_t total)
47
{
48 49 50 51 52 53 54 55 56
	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;
57

58 59 60 61 62
	if (apk_progress_fd != 0) {
		i = snprintf(buf, sizeof(buf), "%zu/%zu\n", done, total);
		write(apk_progress_fd, buf, i);
	}
	last_done = done;
63

64
	if (!(apk_flags & APK_PROGRESS))
65 66
		return;

67 68 69 70
	bar_width = apk_get_screen_width() - 7;
	if (total > 0) {
		bar = muldiv(bar_width, done, total);
		percent = muldiv(100, done, total);
71 72
	}

73 74 75 76 77 78 79
	if (bar  == last_bar && percent == last_percent && !apk_progress_force)
		return;

	last_bar = bar;
	last_percent = percent;
	apk_progress_force = 0;

80
	fprintf(stdout, "\e7%3i%% [", percent);
81
	for (i = 0; i < bar; i++)
82
		fputc('#', stdout);
83
	for (; i < bar_width; i++)
84 85 86 87
		fputc(' ', stdout);
	fputc(']', stdout);
	fflush(stdout);
	fputs("\e8\e[0K", stdout);
88 89
}

Timo Teräs's avatar
Timo Teräs committed
90 91
int apk_print_indented(struct apk_indent *i, apk_blob_t blob)
{
92 93 94 95 96 97
	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));
98
	apk_progress_force = 1;
Natanael Copa's avatar
Natanael Copa committed
99 100 101 102 103 104 105 106 107
	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);
}

108 109 110 111 112 113 114 115 116 117 118 119
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);
}

Natanael Copa's avatar
Natanael Copa committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
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";
135
	case ENOPKG:
136 137 138 139 140 141 142 143 144 145
		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:
146
		return "remote server returned error (try 'apk update')";
147 148 149 150
	case ETIMEDOUT:
		return "operation timed out";
	case EAGAIN:
		return "temporary error (try again later)";
151
	case EAPKBADURL:
152 153
		return "invalid URL (check your repositories file)";
	case EAPKSTALEINDEX:
154
		return "package mentioned in index not found (try 'apk update')";
Natanael Copa's avatar
Natanael Copa committed
155 156 157 158 159 160 161 162 163 164
	default:
		return strerror(error);
	}
}

void apk_log(const char *prefix, const char *format, ...)
{
	va_list va;

	if (prefix != NULL)
165
		fprintf(stdout, "%s", prefix);
Natanael Copa's avatar
Natanael Copa committed
166
	va_start(va, format);
167
	vfprintf(stdout, format, va);
Natanael Copa's avatar
Natanael Copa committed
168
	va_end(va);
169
	fprintf(stdout, "\n");
Natanael Copa's avatar
Natanael Copa committed
170
	fflush(stdout);
171
	apk_progress_force = 1;
Natanael Copa's avatar
Natanael Copa committed
172 173
}