print.c 2.28 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"

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

void apk_reset_screen_width(void)
{
	apk_screen_width = 0;
}

int apk_get_screen_width(void)
Natanael Copa's avatar
Natanael Copa committed
30
{
Timo Teräs's avatar
Timo Teräs committed
31
	struct winsize w;
Natanael Copa's avatar
Natanael Copa committed
32

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

	return apk_screen_width;
}

int apk_print_indented(struct apk_indent *i, apk_blob_t blob)
{
45 46 47 48 49 50
	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));
Natanael Copa's avatar
Natanael Copa committed
51 52 53 54 55 56 57 58 59
	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);
}

60 61 62 63 64 65 66 67 68 69 70 71
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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
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";
87 88
	case ENOPKG:
		return "package not available";
Natanael Copa's avatar
Natanael Copa committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	default:
		return strerror(error);
	}
}

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

	if (prefix != NULL)
		fprintf(stderr, "%s", prefix);
	va_start(va, format);
	vfprintf(stderr, format, va);
	va_end(va);
	fprintf(stderr, "\n");
}