2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* util.c
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2014-01-01 05:24:48 -05:00
|
|
|
* Copyright (c) 2006-2014 Pacman Development Team <pacman-dev@archlinux.org>
|
2009-07-01 03:08:33 -04:00
|
|
|
* Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2005-03-14 20:51:43 -05:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-12-10 23:55:22 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-03-14 20:51:43 -05:00
|
|
|
*/
|
|
|
|
|
2006-11-21 23:53:10 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
2010-07-27 11:08:47 -04:00
|
|
|
#include <sys/stat.h>
|
2011-09-07 11:21:47 -04:00
|
|
|
#include <time.h>
|
2006-11-21 23:53:10 -05:00
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2007-04-26 19:20:46 -04:00
|
|
|
#include <stdarg.h>
|
2011-02-21 08:21:40 -05:00
|
|
|
#include <stdint.h> /* intmax_t */
|
2005-03-14 20:51:43 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
2005-03-16 16:21:06 -05:00
|
|
|
#include <unistd.h>
|
2007-09-06 20:03:38 -04:00
|
|
|
#include <limits.h>
|
2008-02-23 00:47:03 -05:00
|
|
|
#include <wchar.h>
|
2011-03-18 11:03:28 -04:00
|
|
|
#ifdef HAVE_TERMIOS_H
|
|
|
|
#include <termios.h> /* tcflush */
|
|
|
|
#endif
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-01-19 04:28:44 -05:00
|
|
|
#include <alpm.h>
|
|
|
|
#include <alpm_list.h>
|
2007-03-05 17:13:33 -05:00
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* pacman */
|
|
|
|
#include "util.h"
|
2005-12-20 18:58:27 -05:00
|
|
|
#include "conf.h"
|
2008-04-26 05:30:49 -04:00
|
|
|
#include "callback.h"
|
|
|
|
|
2014-08-11 09:43:12 -04:00
|
|
|
static int cached_columns = -1;
|
2008-04-26 05:30:49 -04:00
|
|
|
|
2013-07-03 22:34:14 -04:00
|
|
|
struct table_cell_t {
|
|
|
|
char *label;
|
|
|
|
size_t len;
|
|
|
|
int mode;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
CELL_NORMAL = 0,
|
|
|
|
CELL_TITLE = (1 << 0),
|
|
|
|
CELL_RIGHT_ALIGN = (1 << 1),
|
|
|
|
CELL_FREE = (1 << 3)
|
2013-02-28 18:52:34 -05:00
|
|
|
};
|
|
|
|
|
2011-06-08 00:08:06 -04:00
|
|
|
int trans_init(alpm_transflag_t flags, int check_valid)
|
2008-04-26 05:30:49 -04:00
|
|
|
{
|
2009-07-19 05:15:11 -04:00
|
|
|
int ret;
|
2011-06-08 00:08:06 -04:00
|
|
|
|
|
|
|
check_syncdbs(0, check_valid);
|
|
|
|
|
2011-09-01 18:16:56 -04:00
|
|
|
ret = alpm_trans_init(config->handle, flags);
|
2009-07-19 05:15:11 -04:00
|
|
|
if(ret == -1) {
|
2011-08-29 12:58:48 -04:00
|
|
|
trans_init_error();
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2008-04-26 05:30:49 -04:00
|
|
|
}
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2008-04-26 05:30:49 -04:00
|
|
|
}
|
|
|
|
|
2011-08-29 12:58:48 -04:00
|
|
|
void trans_init_error(void)
|
|
|
|
{
|
2011-10-28 22:03:24 -04:00
|
|
|
alpm_errno_t err = alpm_errno(config->handle);
|
2011-10-21 11:44:19 -04:00
|
|
|
pm_printf(ALPM_LOG_ERROR, _("failed to init transaction (%s)\n"),
|
2011-08-29 12:58:48 -04:00
|
|
|
alpm_strerror(err));
|
|
|
|
if(err == ALPM_ERR_HANDLE_LOCK) {
|
2012-02-20 00:24:26 -05:00
|
|
|
const char *lockfile = alpm_option_get_lockfile(config->handle);
|
|
|
|
pm_printf(ALPM_LOG_ERROR, _("could not lock database: %s\n"),
|
|
|
|
strerror(errno));
|
|
|
|
if(access(lockfile, F_OK) == 0) {
|
|
|
|
fprintf(stderr, _(" if you're sure a package manager is not already\n"
|
|
|
|
" running, you can remove %s\n"), lockfile);
|
|
|
|
}
|
2011-08-29 12:58:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-20 11:09:17 -04:00
|
|
|
int trans_release(void)
|
2008-04-26 05:30:49 -04:00
|
|
|
{
|
2011-06-07 14:29:05 -04:00
|
|
|
if(alpm_trans_release(config->handle) == -1) {
|
2011-10-21 11:44:19 -04:00
|
|
|
pm_printf(ALPM_LOG_ERROR, _("failed to release transaction (%s)\n"),
|
2011-06-07 17:06:16 -04:00
|
|
|
alpm_strerror(alpm_errno(config->handle)));
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2008-04-26 05:30:49 -04:00
|
|
|
}
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2008-04-26 05:30:49 -04:00
|
|
|
}
|
2007-04-26 19:20:46 -04:00
|
|
|
|
2009-05-16 11:59:45 -04:00
|
|
|
int needs_root(void)
|
2007-09-28 00:03:35 -04:00
|
|
|
{
|
2009-07-19 05:15:11 -04:00
|
|
|
switch(config->op) {
|
2009-05-20 15:46:23 -04:00
|
|
|
case PM_OP_DATABASE:
|
2011-03-20 20:45:57 -04:00
|
|
|
return 1;
|
2009-07-19 05:15:11 -04:00
|
|
|
case PM_OP_UPGRADE:
|
|
|
|
case PM_OP_REMOVE:
|
2011-03-20 20:45:57 -04:00
|
|
|
return !config->print;
|
2009-07-19 05:15:11 -04:00
|
|
|
case PM_OP_SYNC:
|
2011-03-20 20:45:57 -04:00
|
|
|
return (config->op_s_clean || config->op_s_sync ||
|
2009-07-19 05:15:11 -04:00
|
|
|
(!config->group && !config->op_s_info && !config->op_q_list &&
|
|
|
|
!config->op_s_search && !config->print));
|
|
|
|
default:
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2007-09-28 00:03:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-08 00:08:06 -04:00
|
|
|
int check_syncdbs(size_t need_repos, int check_valid)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
alpm_list_t *i;
|
2012-02-02 00:45:52 -05:00
|
|
|
alpm_list_t *sync_dbs = alpm_get_syncdbs(config->handle);
|
2011-06-08 00:08:06 -04:00
|
|
|
|
|
|
|
if(need_repos && sync_dbs == NULL) {
|
|
|
|
pm_printf(ALPM_LOG_ERROR, _("no usable package repositories configured.\n"));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(check_valid) {
|
|
|
|
/* ensure all known dbs are valid */
|
|
|
|
for(i = sync_dbs; i; i = alpm_list_next(i)) {
|
|
|
|
alpm_db_t *db = i->data;
|
|
|
|
if(alpm_db_get_valid(db)) {
|
|
|
|
pm_printf(ALPM_LOG_ERROR, _("database '%s' is not valid (%s)\n"),
|
|
|
|
alpm_db_get_name(db), alpm_strerror(alpm_errno(config->handle)));
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-03-18 11:03:28 -04:00
|
|
|
/* discard unhandled input on the terminal's input buffer */
|
2013-01-03 16:48:52 -05:00
|
|
|
static int flush_term_input(int fd)
|
|
|
|
{
|
2011-03-18 11:03:28 -04:00
|
|
|
#ifdef HAVE_TCFLUSH
|
2012-03-16 18:05:06 -04:00
|
|
|
if(isatty(fd)) {
|
|
|
|
return tcflush(fd, TCIFLUSH);
|
2011-03-18 11:03:28 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* fail silently */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-21 08:44:01 -04:00
|
|
|
void columns_cache_reset(void)
|
2006-11-20 04:10:23 -05:00
|
|
|
{
|
2014-08-11 09:43:12 -04:00
|
|
|
cached_columns = -1;
|
2014-06-21 08:44:01 -04:00
|
|
|
}
|
2011-06-13 22:23:49 -04:00
|
|
|
|
2014-06-21 08:44:01 -04:00
|
|
|
static int getcols_fd(int fd)
|
|
|
|
{
|
|
|
|
int width = -1;
|
2011-06-13 22:23:49 -04:00
|
|
|
|
2014-08-11 09:43:12 -04:00
|
|
|
if(!isatty(fd)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-16 18:05:06 -04:00
|
|
|
#if defined(TIOCGSIZE)
|
2009-08-26 18:11:41 -04:00
|
|
|
struct ttysize win;
|
2012-03-16 18:05:06 -04:00
|
|
|
if(ioctl(fd, TIOCGSIZE, &win) == 0) {
|
2014-06-21 08:44:01 -04:00
|
|
|
width = win.ts_cols;
|
2006-11-21 23:53:10 -05:00
|
|
|
}
|
2009-08-26 18:11:41 -04:00
|
|
|
#elif defined(TIOCGWINSZ)
|
|
|
|
struct winsize win;
|
2012-03-16 18:05:06 -04:00
|
|
|
if(ioctl(fd, TIOCGWINSZ, &win) == 0) {
|
2014-06-21 08:44:01 -04:00
|
|
|
width = win.ws_col;
|
2006-11-20 04:10:23 -05:00
|
|
|
}
|
2009-08-26 18:11:41 -04:00
|
|
|
#endif
|
2014-06-21 08:44:01 -04:00
|
|
|
|
|
|
|
if(width <= 0) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short getcols(void)
|
|
|
|
{
|
|
|
|
const char *e;
|
2014-08-11 09:43:12 -04:00
|
|
|
int c = -1;
|
2014-06-21 08:44:01 -04:00
|
|
|
|
2014-08-11 09:43:12 -04:00
|
|
|
if(cached_columns >= 0) {
|
2014-06-21 08:44:01 -04:00
|
|
|
return cached_columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
e = getenv("COLUMNS");
|
2014-08-11 09:43:12 -04:00
|
|
|
if(e && *e) {
|
|
|
|
char *p = NULL;
|
|
|
|
c = strtol(e, &p, 10);
|
|
|
|
if(*p != '\0') {
|
|
|
|
c= -1;
|
|
|
|
}
|
2014-06-21 08:44:01 -04:00
|
|
|
}
|
|
|
|
|
2014-08-11 09:43:12 -04:00
|
|
|
if(c < 0) {
|
2014-06-21 08:44:01 -04:00
|
|
|
c = getcols_fd(STDOUT_FILENO);
|
|
|
|
}
|
|
|
|
|
2014-08-11 09:43:12 -04:00
|
|
|
if(c < 0) {
|
2014-06-21 08:44:01 -04:00
|
|
|
c = 80;
|
|
|
|
}
|
|
|
|
|
|
|
|
cached_columns = c;
|
|
|
|
return c;
|
2006-11-20 04:10:23 -05:00
|
|
|
}
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* does the same thing as 'rm -rf' */
|
2007-05-31 02:51:28 -04:00
|
|
|
int rmrf(const char *path)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
|
|
|
int errflag = 0;
|
|
|
|
struct dirent *dp;
|
|
|
|
DIR *dirp;
|
|
|
|
|
|
|
|
if(!unlink(path)) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2005-03-14 20:51:43 -05:00
|
|
|
} else {
|
2013-09-02 16:30:47 -04:00
|
|
|
switch(errno) {
|
|
|
|
case ENOENT:
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2013-09-02 16:30:47 -04:00
|
|
|
case EPERM:
|
|
|
|
case EISDIR:
|
|
|
|
break;
|
|
|
|
default:
|
2005-03-14 20:51:43 -05:00
|
|
|
/* not a directory */
|
2011-03-20 20:45:57 -04:00
|
|
|
return 1;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2009-10-27 22:11:29 -04:00
|
|
|
dirp = opendir(path);
|
|
|
|
if(!dirp) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return 1;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
|
2014-01-28 11:50:43 -05:00
|
|
|
if(strcmp(dp->d_name, "..") != 0 && strcmp(dp->d_name, ".") != 0) {
|
|
|
|
char name[PATH_MAX];
|
|
|
|
snprintf(name, PATH_MAX, "%s/%s", path, dp->d_name);
|
|
|
|
errflag += rmrf(name);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
if(rmdir(path)) {
|
|
|
|
errflag++;
|
|
|
|
}
|
2011-03-20 20:45:57 -04:00
|
|
|
return errflag;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output a string, but wrap words properly with a specified indentation
|
|
|
|
*/
|
2012-03-16 18:57:04 -04:00
|
|
|
void indentprint(const char *str, unsigned short indent, unsigned short cols)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2008-02-23 00:47:03 -05:00
|
|
|
wchar_t *wcstr;
|
|
|
|
const wchar_t *p;
|
2012-04-07 14:01:13 -04:00
|
|
|
size_t len, cidx;
|
2008-02-23 00:47:03 -05:00
|
|
|
|
2008-02-24 02:17:17 -05:00
|
|
|
if(!str) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-13 18:04:35 -04:00
|
|
|
/* if we're not a tty, or our tty is not wide enough that wrapping even makes
|
|
|
|
* sense, print without indenting */
|
|
|
|
if(cols == 0 || indent > cols) {
|
2011-10-03 11:54:08 -04:00
|
|
|
fputs(str, stdout);
|
2009-08-26 18:11:41 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-23 00:47:03 -05:00
|
|
|
len = strlen(str) + 1;
|
|
|
|
wcstr = calloc(len, sizeof(wchar_t));
|
|
|
|
len = mbstowcs(wcstr, str, len);
|
|
|
|
p = wcstr;
|
|
|
|
cidx = indent;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2009-12-04 19:50:09 -05:00
|
|
|
if(!p || !len) {
|
2008-01-11 01:01:58 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
while(*p) {
|
2008-02-23 00:47:03 -05:00
|
|
|
if(*p == L' ') {
|
|
|
|
const wchar_t *q, *next;
|
2005-03-14 20:51:43 -05:00
|
|
|
p++;
|
2008-02-23 00:47:03 -05:00
|
|
|
if(p == NULL || *p == L' ') continue;
|
|
|
|
next = wcschr(p, L' ');
|
2005-03-14 20:51:43 -05:00
|
|
|
if(next == NULL) {
|
2008-02-23 00:47:03 -05:00
|
|
|
next = p + wcslen(p);
|
|
|
|
}
|
|
|
|
/* len captures # cols */
|
|
|
|
len = 0;
|
|
|
|
q = p;
|
|
|
|
while(q < next) {
|
|
|
|
len += wcwidth(*q++);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2012-08-11 09:43:43 -04:00
|
|
|
if((len + 1) > (cols - cidx)) {
|
2008-02-23 00:47:03 -05:00
|
|
|
/* wrap to a newline and reindent */
|
2011-09-28 10:44:15 -04:00
|
|
|
printf("\n%-*s", (int)indent, "");
|
2005-03-14 20:51:43 -05:00
|
|
|
cidx = indent;
|
|
|
|
} else {
|
|
|
|
printf(" ");
|
|
|
|
cidx++;
|
|
|
|
}
|
2008-02-23 00:47:03 -05:00
|
|
|
continue;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2009-08-26 18:11:41 -04:00
|
|
|
printf("%lc", (wint_t)*p);
|
2008-02-23 00:47:03 -05:00
|
|
|
cidx += wcwidth(*p);
|
2005-03-14 20:51:43 -05:00
|
|
|
p++;
|
|
|
|
}
|
2008-02-23 00:47:03 -05:00
|
|
|
free(wcstr);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Trim whitespace and newlines from a string
|
|
|
|
*/
|
2011-12-22 08:52:08 -05:00
|
|
|
size_t strtrim(char *str)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2011-12-22 08:52:08 -05:00
|
|
|
char *end, *pch = str;
|
2007-06-04 12:01:53 -04:00
|
|
|
|
2007-06-04 12:51:23 -04:00
|
|
|
if(str == NULL || *str == '\0') {
|
2007-06-04 12:01:53 -04:00
|
|
|
/* string is empty, so we're done. */
|
2011-12-22 08:52:08 -05:00
|
|
|
return 0;
|
2007-06-04 12:01:53 -04:00
|
|
|
}
|
|
|
|
|
2009-09-25 18:58:42 -04:00
|
|
|
while(isspace((unsigned char)*pch)) {
|
2005-03-14 20:51:43 -05:00
|
|
|
pch++;
|
|
|
|
}
|
|
|
|
if(pch != str) {
|
2011-08-25 18:14:19 -04:00
|
|
|
size_t len = strlen(pch);
|
|
|
|
if(len) {
|
|
|
|
memmove(str, pch, len + 1);
|
2014-04-20 11:02:18 -04:00
|
|
|
pch = str;
|
2011-08-25 18:14:19 -04:00
|
|
|
} else {
|
|
|
|
*str = '\0';
|
|
|
|
}
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2007-11-16 21:18:45 -05:00
|
|
|
|
2007-06-04 12:01:53 -04:00
|
|
|
/* check if there wasn't anything but whitespace in the string. */
|
|
|
|
if(*str == '\0') {
|
2011-12-22 08:52:08 -05:00
|
|
|
return 0;
|
2007-06-04 12:01:53 -04:00
|
|
|
}
|
|
|
|
|
2011-12-22 08:52:08 -05:00
|
|
|
end = (str + strlen(str) - 1);
|
|
|
|
while(isspace((unsigned char)*end)) {
|
|
|
|
end--;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
2011-12-22 08:52:08 -05:00
|
|
|
*++end = '\0';
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2011-12-22 08:52:08 -05:00
|
|
|
return end - pch;
|
2007-06-04 12:01:53 -04:00
|
|
|
}
|
|
|
|
|
2013-04-14 22:33:46 -04:00
|
|
|
/* Replace all occurrences of 'needle' with 'replace' in 'str', returning
|
2007-06-04 12:01:53 -04:00
|
|
|
* a new string (must be free'd) */
|
|
|
|
char *strreplace(const char *str, const char *needle, const char *replace)
|
|
|
|
{
|
2009-09-06 19:15:39 -04:00
|
|
|
const char *p = NULL, *q = NULL;
|
|
|
|
char *newstr = NULL, *newp = NULL;
|
|
|
|
alpm_list_t *i = NULL, *list = NULL;
|
|
|
|
size_t needlesz = strlen(needle), replacesz = strlen(replace);
|
|
|
|
size_t newsz;
|
2007-06-04 12:01:53 -04:00
|
|
|
|
2009-09-06 19:15:39 -04:00
|
|
|
if(!str) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return NULL;
|
2009-09-06 19:15:39 -04:00
|
|
|
}
|
2007-06-04 12:01:53 -04:00
|
|
|
|
2009-09-06 19:15:39 -04:00
|
|
|
p = str;
|
|
|
|
q = strstr(p, needle);
|
|
|
|
while(q) {
|
|
|
|
list = alpm_list_add(list, (char *)q);
|
|
|
|
p = q + needlesz;
|
2007-06-04 12:01:53 -04:00
|
|
|
q = strstr(p, needle);
|
2009-09-06 19:15:39 -04:00
|
|
|
}
|
|
|
|
|
2013-04-14 22:33:46 -04:00
|
|
|
/* no occurrences of needle found */
|
2009-09-06 19:15:39 -04:00
|
|
|
if(!list) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return strdup(str);
|
2009-09-06 19:15:39 -04:00
|
|
|
}
|
2013-04-14 22:33:46 -04:00
|
|
|
/* size of new string = size of old string + "number of occurrences of needle"
|
2009-09-06 19:15:39 -04:00
|
|
|
* x "size difference between replace and needle" */
|
|
|
|
newsz = strlen(str) + 1 +
|
|
|
|
alpm_list_count(list) * (replacesz - needlesz);
|
2011-07-05 15:16:17 -04:00
|
|
|
newstr = calloc(newsz, sizeof(char));
|
2009-09-06 19:15:39 -04:00
|
|
|
if(!newstr) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return NULL;
|
2009-09-06 19:15:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p = str;
|
|
|
|
newp = newstr;
|
|
|
|
for(i = list; i; i = alpm_list_next(i)) {
|
2011-10-06 01:55:47 -04:00
|
|
|
q = i->data;
|
2011-07-05 15:16:17 -04:00
|
|
|
if(q > p) {
|
2013-04-14 22:33:46 -04:00
|
|
|
/* add chars between this occurrence and last occurrence, if any */
|
2011-07-05 15:16:17 -04:00
|
|
|
memcpy(newp, p, (size_t)(q - p));
|
2009-09-06 19:15:39 -04:00
|
|
|
newp += q - p;
|
2007-06-04 12:01:53 -04:00
|
|
|
}
|
2011-07-05 15:16:17 -04:00
|
|
|
memcpy(newp, replace, replacesz);
|
2009-09-06 19:15:39 -04:00
|
|
|
newp += replacesz;
|
|
|
|
p = q + needlesz;
|
|
|
|
}
|
|
|
|
alpm_list_free(list);
|
|
|
|
|
|
|
|
if(*p) {
|
|
|
|
/* add the rest of 'p' */
|
|
|
|
strcpy(newp, p);
|
2007-06-04 12:01:53 -04:00
|
|
|
}
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return newstr;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2011-09-28 10:44:15 -04:00
|
|
|
static size_t string_length(const char *s)
|
2008-07-31 07:38:30 -04:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
wchar_t *wcstr;
|
|
|
|
|
2011-09-12 21:25:58 -04:00
|
|
|
if(!s || s[0] == '\0') {
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2008-07-31 07:38:30 -04:00
|
|
|
}
|
|
|
|
/* len goes from # bytes -> # chars -> # cols */
|
|
|
|
len = strlen(s) + 1;
|
|
|
|
wcstr = calloc(len, sizeof(wchar_t));
|
|
|
|
len = mbstowcs(wcstr, s, len);
|
|
|
|
len = wcswidth(wcstr, len);
|
|
|
|
free(wcstr);
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return len;
|
2008-07-31 07:38:30 -04:00
|
|
|
}
|
|
|
|
|
2013-07-03 22:34:14 -04:00
|
|
|
static void add_table_cell(alpm_list_t **row, char *label, int mode)
|
|
|
|
{
|
|
|
|
struct table_cell_t *cell = malloc(sizeof(struct table_cell_t));
|
|
|
|
|
|
|
|
cell->label = label;
|
|
|
|
cell->mode = mode;
|
|
|
|
cell->len = string_length(label);
|
|
|
|
|
|
|
|
*row = alpm_list_add(*row, cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void table_free_cell(void *ptr)
|
|
|
|
{
|
|
|
|
struct table_cell_t *cell = ptr;
|
|
|
|
|
|
|
|
if(cell) {
|
|
|
|
if(cell->mode & CELL_FREE) {
|
|
|
|
free(cell->label);
|
|
|
|
}
|
|
|
|
free(cell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void table_free(alpm_list_t *headers, alpm_list_t *rows)
|
|
|
|
{
|
|
|
|
alpm_list_t *i;
|
|
|
|
|
|
|
|
alpm_list_free_inner(headers, table_free_cell);
|
|
|
|
|
|
|
|
for(i = rows; i; i = alpm_list_next(i)) {
|
|
|
|
alpm_list_free_inner(i->data, table_free_cell);
|
|
|
|
alpm_list_free(i->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
alpm_list_free(headers);
|
|
|
|
alpm_list_free(rows);
|
|
|
|
}
|
|
|
|
|
2014-05-11 15:30:31 -04:00
|
|
|
static void add_transaction_sizes_row(alpm_list_t **rows, char *label, off_t size)
|
2013-07-03 22:34:14 -04:00
|
|
|
{
|
|
|
|
alpm_list_t *row = NULL;
|
|
|
|
char *str;
|
|
|
|
const char *units;
|
|
|
|
double s = humanize_size(size, 'M', 2, &units);
|
|
|
|
pm_asprintf(&str, "%.2f %s", s, units);
|
|
|
|
|
|
|
|
add_table_cell(&row, label, CELL_TITLE);
|
|
|
|
add_table_cell(&row, str, CELL_RIGHT_ALIGN | CELL_FREE);
|
|
|
|
|
|
|
|
*rows = alpm_list_add(*rows, row);
|
|
|
|
}
|
|
|
|
|
2012-03-16 18:57:04 -04:00
|
|
|
void string_display(const char *title, const char *string, unsigned short cols)
|
2007-11-23 16:32:40 -05:00
|
|
|
{
|
2008-07-31 07:38:30 -04:00
|
|
|
if(title) {
|
2013-03-01 15:18:28 -05:00
|
|
|
printf("%s%s%s ", config->colstr.title, title, config->colstr.nocolor);
|
2008-07-31 07:38:30 -04:00
|
|
|
}
|
2007-11-23 16:32:40 -05:00
|
|
|
if(string == NULL || string[0] == '\0') {
|
2008-07-31 07:38:30 -04:00
|
|
|
printf(_("None"));
|
2007-11-23 16:32:40 -05:00
|
|
|
} else {
|
2009-10-11 15:19:55 -04:00
|
|
|
/* compute the length of title + a space */
|
2011-09-28 10:44:15 -04:00
|
|
|
size_t len = string_length(title) + 1;
|
2012-03-16 18:57:04 -04:00
|
|
|
indentprint(string, (unsigned short)len, cols);
|
2007-11-23 16:32:40 -05:00
|
|
|
}
|
2008-07-31 07:38:30 -04:00
|
|
|
printf("\n");
|
2007-11-23 16:32:40 -05:00
|
|
|
}
|
|
|
|
|
2011-10-21 12:41:15 -04:00
|
|
|
static void table_print_line(const alpm_list_t *line, short col_padding,
|
|
|
|
size_t colcount, size_t *widths, int *has_data)
|
2011-02-19 17:23:42 -05:00
|
|
|
{
|
2013-07-03 22:34:14 -04:00
|
|
|
size_t i;
|
2011-10-21 12:41:15 -04:00
|
|
|
int need_padding = 0;
|
2011-10-10 22:26:17 -04:00
|
|
|
const alpm_list_t *curcell;
|
|
|
|
|
|
|
|
for(i = 0, curcell = line; curcell && i < colcount;
|
|
|
|
i++, curcell = alpm_list_next(curcell)) {
|
2013-07-03 22:34:14 -04:00
|
|
|
const struct table_cell_t *cell = curcell->data;
|
|
|
|
const char *str = (cell->label ? cell->label : "");
|
|
|
|
int cell_width;
|
2011-10-21 12:41:15 -04:00
|
|
|
|
|
|
|
if(!has_data[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-07-03 22:34:14 -04:00
|
|
|
cell_width = (cell->mode & CELL_RIGHT_ALIGN ? (int)widths[i] : -(int)widths[i]);
|
|
|
|
|
2011-10-21 12:41:15 -04:00
|
|
|
if(need_padding) {
|
|
|
|
printf("%*s", col_padding, "");
|
2011-10-10 22:26:17 -04:00
|
|
|
}
|
2013-07-03 22:34:14 -04:00
|
|
|
|
|
|
|
if(cell->mode & CELL_TITLE) {
|
|
|
|
printf("%s%*s%s", config->colstr.title, cell_width, str, config->colstr.nocolor);
|
2011-10-10 22:26:17 -04:00
|
|
|
} else {
|
2013-07-03 22:34:14 -04:00
|
|
|
printf("%*s", cell_width, str);
|
2011-10-10 22:26:17 -04:00
|
|
|
}
|
2011-10-21 12:41:15 -04:00
|
|
|
need_padding = 1;
|
2011-02-19 17:23:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2011-10-21 12:41:15 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the max string width of each column. Also determines whether values
|
|
|
|
* exist in the column and sets the value in has_data accordingly.
|
|
|
|
* @param header a list of header strings
|
|
|
|
* @param rows a list of lists of rows as strings
|
|
|
|
* @param padding the amount of padding between columns
|
|
|
|
* @param totalcols the total number of columns in the header and each row
|
|
|
|
* @param widths a pointer to store width data
|
|
|
|
* @param has_data a pointer to store whether column has data
|
|
|
|
*
|
|
|
|
* @return the total width of the table; 0 on failure
|
|
|
|
*/
|
2011-10-10 22:26:17 -04:00
|
|
|
static size_t table_calc_widths(const alpm_list_t *header,
|
2011-10-21 12:41:15 -04:00
|
|
|
const alpm_list_t *rows, short padding, size_t totalcols,
|
|
|
|
size_t **widths, int **has_data)
|
2011-02-19 17:23:42 -05:00
|
|
|
{
|
2011-09-12 21:25:58 -04:00
|
|
|
const alpm_list_t *i;
|
2011-10-21 12:41:15 -04:00
|
|
|
size_t curcol, totalwidth = 0, usefulcols = 0;
|
2011-09-12 21:25:58 -04:00
|
|
|
size_t *colwidths;
|
2011-10-21 12:41:15 -04:00
|
|
|
int *coldata;
|
2011-02-19 17:23:42 -05:00
|
|
|
|
2011-10-10 22:26:17 -04:00
|
|
|
if(totalcols <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-12 21:25:58 -04:00
|
|
|
colwidths = malloc(totalcols * sizeof(size_t));
|
2011-10-21 12:41:15 -04:00
|
|
|
coldata = calloc(totalcols, sizeof(int));
|
|
|
|
if(!colwidths || !coldata) {
|
2012-11-24 13:15:04 -05:00
|
|
|
free(colwidths);
|
|
|
|
free(coldata);
|
2011-10-10 22:26:17 -04:00
|
|
|
return 0;
|
2011-09-12 21:25:58 -04:00
|
|
|
}
|
2011-02-19 17:23:42 -05:00
|
|
|
/* header determines column count and initial values of longest_strs */
|
2011-09-12 21:25:58 -04:00
|
|
|
for(i = header, curcol = 0; i; i = alpm_list_next(i), curcol++) {
|
2013-07-03 22:34:14 -04:00
|
|
|
const struct table_cell_t *row = i->data;
|
|
|
|
colwidths[curcol] = row->len;
|
2011-10-21 12:41:15 -04:00
|
|
|
/* note: header does not determine whether column has data */
|
2011-02-19 17:23:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* now find the longest string in each column */
|
2011-09-12 21:25:58 -04:00
|
|
|
for(i = rows; i; i = alpm_list_next(i)) {
|
|
|
|
/* grab first column of each row and iterate through columns */
|
2011-10-06 01:55:47 -04:00
|
|
|
const alpm_list_t *j = i->data;
|
2011-09-12 21:25:58 -04:00
|
|
|
for(curcol = 0; j; j = alpm_list_next(j), curcol++) {
|
2013-07-03 22:34:14 -04:00
|
|
|
const struct table_cell_t *cell = j->data;
|
|
|
|
size_t str_len = cell ? cell->len : 0;
|
2011-09-12 21:25:58 -04:00
|
|
|
|
|
|
|
if(str_len > colwidths[curcol]) {
|
|
|
|
colwidths[curcol] = str_len;
|
2011-02-19 17:23:42 -05:00
|
|
|
}
|
2011-10-21 12:41:15 -04:00
|
|
|
if(str_len > 0) {
|
|
|
|
coldata[curcol] = 1;
|
|
|
|
}
|
2011-02-19 17:23:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-10 22:26:17 -04:00
|
|
|
for(i = header, curcol = 0; i; i = alpm_list_next(i), curcol++) {
|
2011-10-21 12:41:15 -04:00
|
|
|
/* only include columns that have data */
|
|
|
|
if(coldata[curcol]) {
|
|
|
|
usefulcols++;
|
|
|
|
totalwidth += colwidths[curcol];
|
2011-10-10 22:26:17 -04:00
|
|
|
}
|
2011-10-21 12:41:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add padding between columns */
|
|
|
|
if(usefulcols > 0) {
|
|
|
|
totalwidth += padding * (usefulcols - 1);
|
2011-02-19 17:23:42 -05:00
|
|
|
}
|
|
|
|
|
2011-10-10 22:26:17 -04:00
|
|
|
*widths = colwidths;
|
2011-10-21 12:41:15 -04:00
|
|
|
*has_data = coldata;
|
2011-10-10 22:26:17 -04:00
|
|
|
return totalwidth;
|
2011-02-19 17:23:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Displays the list in table format
|
|
|
|
*
|
|
|
|
* @param title the tables title
|
|
|
|
* @param header the column headers. column count is determined by the nr
|
|
|
|
* of headers
|
|
|
|
* @param rows the rows to display as a list of lists of strings. the outer
|
|
|
|
* list represents the rows, the inner list the cells (= columns)
|
2012-03-16 18:57:04 -04:00
|
|
|
* @param cols the number of columns available in the terminal
|
2011-02-19 17:23:42 -05:00
|
|
|
* @return -1 if not enough terminal cols available, else 0
|
|
|
|
*/
|
2013-07-03 22:34:14 -04:00
|
|
|
static int table_display(const alpm_list_t *header,
|
2012-03-16 18:57:04 -04:00
|
|
|
const alpm_list_t *rows, unsigned short cols)
|
2011-02-19 17:23:42 -05:00
|
|
|
{
|
2011-10-21 12:41:15 -04:00
|
|
|
const unsigned short padding = 2;
|
2013-07-03 22:34:14 -04:00
|
|
|
const alpm_list_t *i, *first;
|
2011-10-10 22:26:17 -04:00
|
|
|
size_t *widths = NULL, totalcols, totalwidth;
|
2011-10-21 12:41:15 -04:00
|
|
|
int *has_data = NULL;
|
2014-01-06 11:52:26 -05:00
|
|
|
int ret = 0;
|
2011-02-19 17:23:42 -05:00
|
|
|
|
2013-07-03 22:34:14 -04:00
|
|
|
if(rows == NULL) {
|
2014-01-06 11:52:26 -05:00
|
|
|
return ret;
|
2011-02-19 17:23:42 -05:00
|
|
|
}
|
|
|
|
|
2013-07-03 22:34:14 -04:00
|
|
|
/* we want the first row. if no headers are provided, use the first
|
|
|
|
* entry of the rows array. */
|
|
|
|
first = header ? header : rows->data;
|
|
|
|
|
|
|
|
totalcols = alpm_list_count(first);
|
|
|
|
totalwidth = table_calc_widths(first, rows, padding, totalcols,
|
2011-10-21 12:41:15 -04:00
|
|
|
&widths, &has_data);
|
2011-10-10 22:26:17 -04:00
|
|
|
/* return -1 if terminal is not wide enough */
|
2014-02-08 10:25:51 -05:00
|
|
|
if(cols && totalwidth > cols) {
|
2011-10-21 11:44:19 -04:00
|
|
|
pm_printf(ALPM_LOG_WARNING,
|
2011-10-17 09:28:57 -04:00
|
|
|
_("insufficient columns available for table display\n"));
|
2014-01-06 11:52:26 -05:00
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
2011-10-10 22:26:17 -04:00
|
|
|
}
|
2011-10-21 12:41:15 -04:00
|
|
|
if(!totalwidth || !widths || !has_data) {
|
2014-01-06 11:52:26 -05:00
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
2011-02-19 17:23:42 -05:00
|
|
|
}
|
|
|
|
|
2013-10-31 17:31:32 -04:00
|
|
|
if(header) {
|
2013-07-03 22:34:14 -04:00
|
|
|
table_print_line(header, padding, totalcols, widths, has_data);
|
|
|
|
printf("\n");
|
2011-02-19 17:23:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for(i = rows; i; i = alpm_list_next(i)) {
|
2011-10-21 12:41:15 -04:00
|
|
|
table_print_line(i->data, padding, totalcols, widths, has_data);
|
2011-02-19 17:23:42 -05:00
|
|
|
}
|
|
|
|
|
2014-01-06 11:52:26 -05:00
|
|
|
cleanup:
|
2011-10-10 22:26:17 -04:00
|
|
|
free(widths);
|
2011-10-21 12:41:15 -04:00
|
|
|
free(has_data);
|
2014-01-06 11:52:26 -05:00
|
|
|
return ret;
|
2011-02-19 17:23:42 -05:00
|
|
|
}
|
|
|
|
|
2012-03-16 18:57:04 -04:00
|
|
|
void list_display(const char *title, const alpm_list_t *list,
|
|
|
|
unsigned short maxcols)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
2007-06-05 17:34:33 -04:00
|
|
|
const alpm_list_t *i;
|
2011-09-28 10:44:15 -04:00
|
|
|
size_t len = 0;
|
2008-02-23 00:47:03 -05:00
|
|
|
|
2008-02-24 02:17:17 -05:00
|
|
|
if(title) {
|
2008-07-31 09:58:25 -04:00
|
|
|
len = string_length(title) + 1;
|
2013-03-01 15:18:28 -05:00
|
|
|
printf("%s%s%s ", config->colstr.title, title, config->colstr.nocolor);
|
2008-02-24 02:17:17 -05:00
|
|
|
}
|
2007-01-19 04:28:44 -05:00
|
|
|
|
2008-07-31 09:58:25 -04:00
|
|
|
if(!list) {
|
2008-08-23 12:20:07 -04:00
|
|
|
printf("%s\n", _("None"));
|
2008-07-31 09:58:25 -04:00
|
|
|
} else {
|
2011-09-28 10:44:15 -04:00
|
|
|
size_t cols = len;
|
2011-10-06 01:55:47 -04:00
|
|
|
const char *str = list->data;
|
2011-10-03 11:54:08 -04:00
|
|
|
fputs(str, stdout);
|
2011-06-13 18:04:35 -04:00
|
|
|
cols += string_length(str);
|
2011-06-15 10:10:33 -04:00
|
|
|
for(i = alpm_list_next(list); i; i = alpm_list_next(i)) {
|
2011-10-06 01:55:47 -04:00
|
|
|
str = i->data;
|
2011-09-28 10:44:15 -04:00
|
|
|
size_t s = string_length(str);
|
2011-06-13 18:04:35 -04:00
|
|
|
/* wrap only if we have enough usable column space */
|
|
|
|
if(maxcols > len && cols + s + 2 >= maxcols) {
|
2011-09-28 10:44:15 -04:00
|
|
|
size_t j;
|
2007-01-19 04:28:44 -05:00
|
|
|
cols = len;
|
|
|
|
printf("\n");
|
2011-12-06 23:37:32 -05:00
|
|
|
for(j = 1; j <= len; j++) {
|
2007-01-19 04:28:44 -05:00
|
|
|
printf(" ");
|
|
|
|
}
|
2011-04-20 20:45:16 -04:00
|
|
|
} else if(cols != len) {
|
2009-12-14 00:12:44 -05:00
|
|
|
/* 2 spaces are added if this is not the first element on a line. */
|
|
|
|
printf(" ");
|
|
|
|
cols += 2;
|
2007-01-19 04:28:44 -05:00
|
|
|
}
|
2011-10-03 11:54:08 -04:00
|
|
|
fputs(str, stdout);
|
2007-01-19 04:28:44 -05:00
|
|
|
cols += s;
|
|
|
|
}
|
2011-10-03 11:54:08 -04:00
|
|
|
putchar('\n');
|
2007-01-19 04:28:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-16 18:57:04 -04:00
|
|
|
void list_display_linebreak(const char *title, const alpm_list_t *list,
|
|
|
|
unsigned short maxcols)
|
2008-07-31 09:58:25 -04:00
|
|
|
{
|
2012-03-16 18:57:04 -04:00
|
|
|
unsigned short len = 0;
|
2008-07-31 09:58:25 -04:00
|
|
|
|
|
|
|
if(title) {
|
2012-03-16 18:57:04 -04:00
|
|
|
len = (unsigned short)string_length(title) + 1;
|
2013-03-01 15:18:28 -05:00
|
|
|
printf("%s%s%s ", config->colstr.title, title, config->colstr.nocolor);
|
2008-07-31 09:58:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!list) {
|
2008-08-23 12:20:07 -04:00
|
|
|
printf("%s\n", _("None"));
|
2008-07-31 09:58:25 -04:00
|
|
|
} else {
|
2011-07-01 17:50:32 -04:00
|
|
|
const alpm_list_t *i;
|
2008-07-31 09:58:25 -04:00
|
|
|
/* Print the first element */
|
2012-03-16 18:57:04 -04:00
|
|
|
indentprint((const char *)list->data, len, maxcols);
|
2008-07-31 09:58:25 -04:00
|
|
|
printf("\n");
|
|
|
|
/* Print the rest */
|
|
|
|
for(i = alpm_list_next(list); i; i = alpm_list_next(i)) {
|
2011-09-28 10:44:15 -04:00
|
|
|
size_t j;
|
2008-07-31 09:58:25 -04:00
|
|
|
for(j = 1; j <= len; j++) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
2012-03-16 18:57:04 -04:00
|
|
|
indentprint((const char *)i->data, len, maxcols);
|
2008-07-31 09:58:25 -04:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-20 08:42:05 -05:00
|
|
|
|
2012-03-16 18:57:04 -04:00
|
|
|
void signature_display(const char *title, alpm_siglist_t *siglist,
|
|
|
|
unsigned short maxcols)
|
2011-07-01 17:50:32 -04:00
|
|
|
{
|
2012-03-16 18:57:04 -04:00
|
|
|
unsigned short len = 0;
|
2011-07-01 17:50:32 -04:00
|
|
|
|
|
|
|
if(title) {
|
2012-03-16 18:57:04 -04:00
|
|
|
len = (unsigned short)string_length(title) + 1;
|
2013-03-25 14:43:39 -04:00
|
|
|
printf("%s%s%s ", config->colstr.title, title, config->colstr.nocolor);
|
2011-07-01 17:50:32 -04:00
|
|
|
}
|
2011-08-24 14:24:42 -04:00
|
|
|
if(siglist->count == 0) {
|
2011-07-01 17:50:32 -04:00
|
|
|
printf(_("None"));
|
|
|
|
} else {
|
2011-08-24 14:24:42 -04:00
|
|
|
size_t i;
|
|
|
|
for(i = 0; i < siglist->count; i++) {
|
2011-09-19 20:57:29 -04:00
|
|
|
char *sigline;
|
2011-07-22 11:48:13 -04:00
|
|
|
const char *status, *validity, *name;
|
2011-09-19 20:57:29 -04:00
|
|
|
int ret;
|
2011-08-24 14:24:42 -04:00
|
|
|
alpm_sigresult_t *result = siglist->results + i;
|
2011-07-01 17:50:32 -04:00
|
|
|
/* Don't re-indent the first result */
|
|
|
|
if(i != 0) {
|
2011-09-28 10:44:15 -04:00
|
|
|
size_t j;
|
2011-07-01 17:50:32 -04:00
|
|
|
for(j = 1; j <= len; j++) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
}
|
2011-08-24 14:24:42 -04:00
|
|
|
switch(result->status) {
|
2011-07-01 17:50:32 -04:00
|
|
|
case ALPM_SIGSTATUS_VALID:
|
2011-07-22 11:48:13 -04:00
|
|
|
status = _("Valid");
|
2011-07-01 17:50:32 -04:00
|
|
|
break;
|
2011-07-22 11:48:13 -04:00
|
|
|
case ALPM_SIGSTATUS_KEY_EXPIRED:
|
|
|
|
status = _("Key expired");
|
2011-07-01 17:50:32 -04:00
|
|
|
break;
|
2011-07-22 11:48:13 -04:00
|
|
|
case ALPM_SIGSTATUS_SIG_EXPIRED:
|
|
|
|
status = _("Expired");
|
2011-07-01 17:50:32 -04:00
|
|
|
break;
|
2011-07-22 11:48:13 -04:00
|
|
|
case ALPM_SIGSTATUS_INVALID:
|
|
|
|
status = _("Invalid");
|
2011-07-01 17:50:32 -04:00
|
|
|
break;
|
2011-07-22 11:48:13 -04:00
|
|
|
case ALPM_SIGSTATUS_KEY_UNKNOWN:
|
|
|
|
status = _("Key unknown");
|
|
|
|
break;
|
2011-09-21 16:30:12 -04:00
|
|
|
case ALPM_SIGSTATUS_KEY_DISABLED:
|
|
|
|
status = _("Key disabled");
|
|
|
|
break;
|
2011-07-22 11:48:13 -04:00
|
|
|
default:
|
|
|
|
status = _("Signature error");
|
|
|
|
break;
|
|
|
|
}
|
2011-08-24 14:24:42 -04:00
|
|
|
switch(result->validity) {
|
2011-07-22 11:48:13 -04:00
|
|
|
case ALPM_SIGVALIDITY_FULL:
|
2011-08-11 11:28:59 -04:00
|
|
|
validity = _("full trust");
|
2011-07-22 11:48:13 -04:00
|
|
|
break;
|
|
|
|
case ALPM_SIGVALIDITY_MARGINAL:
|
2011-08-11 11:28:59 -04:00
|
|
|
validity = _("marginal trust");
|
2011-07-22 11:48:13 -04:00
|
|
|
break;
|
|
|
|
case ALPM_SIGVALIDITY_NEVER:
|
2011-08-11 11:28:59 -04:00
|
|
|
validity = _("never trust");
|
2011-07-22 11:48:13 -04:00
|
|
|
break;
|
|
|
|
case ALPM_SIGVALIDITY_UNKNOWN:
|
2011-07-01 17:50:32 -04:00
|
|
|
default:
|
2011-07-22 11:48:13 -04:00
|
|
|
validity = _("unknown trust");
|
|
|
|
break;
|
2011-07-01 17:50:32 -04:00
|
|
|
}
|
2011-08-24 14:24:42 -04:00
|
|
|
name = result->key.uid ? result->key.uid : result->key.fingerprint;
|
2011-09-19 20:57:29 -04:00
|
|
|
ret = pm_asprintf(&sigline, _("%s, %s from \"%s\""),
|
2011-07-22 11:48:13 -04:00
|
|
|
status, validity, name);
|
2011-09-21 15:47:17 -04:00
|
|
|
if(ret == -1) {
|
2011-09-19 20:57:29 -04:00
|
|
|
continue;
|
|
|
|
}
|
2012-03-16 18:57:04 -04:00
|
|
|
indentprint(sigline, len, maxcols);
|
2011-07-01 17:50:32 -04:00
|
|
|
printf("\n");
|
2011-09-19 20:57:29 -04:00
|
|
|
free(sigline);
|
2011-07-01 17:50:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-20 09:00:26 -05:00
|
|
|
/* creates a header row for use with table_display */
|
2013-07-03 22:34:14 -04:00
|
|
|
static alpm_list_t *create_verbose_header(size_t count)
|
2011-02-20 09:00:26 -05:00
|
|
|
{
|
2013-07-03 22:34:14 -04:00
|
|
|
alpm_list_t *ret = NULL;
|
|
|
|
|
|
|
|
char *header;
|
|
|
|
pm_asprintf(&header, "%s (%zd)", _("Package"), count);
|
|
|
|
|
|
|
|
add_table_cell(&ret, header, CELL_TITLE | CELL_FREE);
|
|
|
|
add_table_cell(&ret, _("Old Version"), CELL_TITLE);
|
|
|
|
add_table_cell(&ret, _("New Version"), CELL_TITLE);
|
|
|
|
add_table_cell(&ret, _("Net Change"), CELL_TITLE);
|
|
|
|
add_table_cell(&ret, _("Download Size"), CELL_TITLE);
|
2011-02-20 09:00:26 -05:00
|
|
|
|
2013-07-03 22:34:14 -04:00
|
|
|
return ret;
|
2011-02-20 09:00:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* returns package info as list of strings */
|
2012-03-14 22:19:13 -04:00
|
|
|
static alpm_list_t *create_verbose_row(pm_target_t *target)
|
2011-02-20 09:00:26 -05:00
|
|
|
{
|
|
|
|
char *str;
|
2011-07-22 13:01:58 -04:00
|
|
|
off_t size = 0;
|
|
|
|
double human_size;
|
2011-02-20 09:00:26 -05:00
|
|
|
const char *label;
|
|
|
|
alpm_list_t *ret = NULL;
|
|
|
|
|
|
|
|
/* a row consists of the package name, */
|
2011-07-22 13:01:58 -04:00
|
|
|
if(target->install) {
|
2011-10-16 16:42:25 -04:00
|
|
|
const alpm_db_t *db = alpm_pkg_get_db(target->install);
|
|
|
|
if(db) {
|
|
|
|
pm_asprintf(&str, "%s/%s", alpm_db_get_name(db), alpm_pkg_get_name(target->install));
|
|
|
|
} else {
|
|
|
|
pm_asprintf(&str, "%s", alpm_pkg_get_name(target->install));
|
|
|
|
}
|
2011-07-22 13:01:58 -04:00
|
|
|
} else {
|
|
|
|
pm_asprintf(&str, "%s", alpm_pkg_get_name(target->remove));
|
|
|
|
}
|
2014-01-06 11:58:25 -05:00
|
|
|
add_table_cell(&ret, str, CELL_NORMAL | CELL_FREE);
|
2011-02-20 09:00:26 -05:00
|
|
|
|
|
|
|
/* old and new versions */
|
2011-07-22 13:01:58 -04:00
|
|
|
pm_asprintf(&str, "%s",
|
|
|
|
target->remove != NULL ? alpm_pkg_get_version(target->remove) : "");
|
2014-01-06 11:58:25 -05:00
|
|
|
add_table_cell(&ret, str, CELL_NORMAL | CELL_FREE);
|
2011-02-20 09:00:26 -05:00
|
|
|
|
2011-07-22 13:01:58 -04:00
|
|
|
pm_asprintf(&str, "%s",
|
|
|
|
target->install != NULL ? alpm_pkg_get_version(target->install) : "");
|
2014-01-06 11:58:25 -05:00
|
|
|
add_table_cell(&ret, str, CELL_NORMAL | CELL_FREE);
|
2011-02-20 09:00:26 -05:00
|
|
|
|
|
|
|
/* and size */
|
2011-07-22 13:01:58 -04:00
|
|
|
size -= target->remove ? alpm_pkg_get_isize(target->remove) : 0;
|
|
|
|
size += target->install ? alpm_pkg_get_isize(target->install) : 0;
|
2012-02-02 17:43:40 -05:00
|
|
|
human_size = humanize_size(size, 'M', 2, &label);
|
2011-07-22 13:01:58 -04:00
|
|
|
pm_asprintf(&str, "%.2f %s", human_size, label);
|
2014-01-06 11:58:25 -05:00
|
|
|
add_table_cell(&ret, str, CELL_RIGHT_ALIGN | CELL_FREE);
|
2011-02-20 09:00:26 -05:00
|
|
|
|
2012-03-14 22:19:13 -04:00
|
|
|
size = target->install ? alpm_pkg_download_size(target->install) : 0;
|
|
|
|
if(size != 0) {
|
2012-02-02 17:43:40 -05:00
|
|
|
human_size = humanize_size(size, 'M', 2, &label);
|
2012-03-14 22:19:13 -04:00
|
|
|
pm_asprintf(&str, "%.2f %s", human_size, label);
|
|
|
|
} else {
|
|
|
|
str = NULL;
|
2011-09-12 20:44:15 -04:00
|
|
|
}
|
2014-01-06 11:58:25 -05:00
|
|
|
add_table_cell(&ret, str, CELL_RIGHT_ALIGN | CELL_FREE);
|
2011-09-12 20:44:15 -04:00
|
|
|
|
2011-06-13 18:43:09 -04:00
|
|
|
return ret;
|
2011-02-20 09:00:26 -05:00
|
|
|
}
|
|
|
|
|
2008-07-24 20:02:36 -04:00
|
|
|
/* prepare a list of pkgs to display */
|
2011-09-28 13:55:27 -04:00
|
|
|
static void _display_targets(alpm_list_t *targets, int verbose)
|
2007-02-03 20:36:45 -05:00
|
|
|
{
|
|
|
|
char *str;
|
2011-06-01 18:11:45 -04:00
|
|
|
off_t isize = 0, rsize = 0, dlsize = 0;
|
2012-03-16 18:57:04 -04:00
|
|
|
unsigned short cols;
|
2013-07-03 22:34:14 -04:00
|
|
|
alpm_list_t *i, *names = NULL, *header = NULL, *rows = NULL;
|
2008-01-27 06:24:50 -05:00
|
|
|
|
2011-07-22 13:01:58 -04:00
|
|
|
if(!targets) {
|
2008-07-24 20:02:36 -04:00
|
|
|
return;
|
|
|
|
}
|
2008-01-27 06:24:50 -05:00
|
|
|
|
2011-07-22 13:01:58 -04:00
|
|
|
/* gather package info */
|
|
|
|
for(i = targets; i; i = alpm_list_next(i)) {
|
2011-10-06 01:55:47 -04:00
|
|
|
pm_target_t *target = i->data;
|
2007-02-03 20:36:45 -05:00
|
|
|
|
2011-07-22 13:01:58 -04:00
|
|
|
if(target->install) {
|
|
|
|
dlsize += alpm_pkg_download_size(target->install);
|
|
|
|
isize += alpm_pkg_get_isize(target->install);
|
|
|
|
}
|
|
|
|
if(target->remove) {
|
|
|
|
/* add up size of all removed packages */
|
|
|
|
rsize += alpm_pkg_get_isize(target->remove);
|
2011-01-11 21:16:54 -05:00
|
|
|
}
|
2012-01-18 20:29:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* form data for both verbose and non-verbose display */
|
|
|
|
for(i = targets; i; i = alpm_list_next(i)) {
|
|
|
|
pm_target_t *target = i->data;
|
2007-02-03 20:36:45 -05:00
|
|
|
|
2013-07-03 22:34:14 -04:00
|
|
|
if(verbose) {
|
|
|
|
rows = alpm_list_add(rows, create_verbose_row(target));
|
|
|
|
}
|
|
|
|
|
2011-09-28 13:55:27 -04:00
|
|
|
if(target->install) {
|
|
|
|
pm_asprintf(&str, "%s-%s", alpm_pkg_get_name(target->install),
|
|
|
|
alpm_pkg_get_version(target->install));
|
2011-09-29 07:07:48 -04:00
|
|
|
} else if(isize == 0) {
|
|
|
|
pm_asprintf(&str, "%s-%s", alpm_pkg_get_name(target->remove),
|
|
|
|
alpm_pkg_get_version(target->remove));
|
2011-02-20 09:00:26 -05:00
|
|
|
} else {
|
2013-03-12 00:48:02 -04:00
|
|
|
pm_asprintf(&str, "%s-%s [%s]", alpm_pkg_get_name(target->remove),
|
|
|
|
alpm_pkg_get_version(target->remove), _("removal"));
|
2011-02-20 09:00:26 -05:00
|
|
|
}
|
2011-09-28 13:55:27 -04:00
|
|
|
names = alpm_list_add(names, str);
|
2007-02-03 20:36:45 -05:00
|
|
|
}
|
|
|
|
|
2011-02-20 09:00:26 -05:00
|
|
|
/* print to screen */
|
2013-07-03 22:34:14 -04:00
|
|
|
pm_asprintf(&str, "%s (%zd)", _("Packages"), alpm_list_count(targets));
|
2011-02-20 08:42:05 -05:00
|
|
|
printf("\n");
|
2012-03-16 18:57:04 -04:00
|
|
|
|
2014-06-21 08:44:01 -04:00
|
|
|
cols = getcols();
|
2011-09-28 13:55:27 -04:00
|
|
|
if(verbose) {
|
2013-07-03 22:34:14 -04:00
|
|
|
header = create_verbose_header(alpm_list_count(targets));
|
|
|
|
if(table_display(header, rows, cols) != 0) {
|
2011-09-28 13:55:27 -04:00
|
|
|
/* fallback to list display if table wouldn't fit */
|
2012-03-16 18:57:04 -04:00
|
|
|
list_display(str, names, cols);
|
2011-02-20 09:00:26 -05:00
|
|
|
}
|
|
|
|
} else {
|
2012-03-16 18:57:04 -04:00
|
|
|
list_display(str, names, cols);
|
2011-02-20 09:00:26 -05:00
|
|
|
}
|
2011-02-20 08:42:05 -05:00
|
|
|
printf("\n");
|
2007-11-16 21:18:45 -05:00
|
|
|
|
2013-07-03 22:34:14 -04:00
|
|
|
table_free(header, rows);
|
2011-09-28 13:55:27 -04:00
|
|
|
FREELIST(names);
|
|
|
|
free(str);
|
2013-07-03 22:34:14 -04:00
|
|
|
rows = NULL;
|
2011-09-28 13:55:27 -04:00
|
|
|
|
2011-09-28 05:46:22 -04:00
|
|
|
if(dlsize > 0 || config->op_s_downloadonly) {
|
2013-07-03 22:34:14 -04:00
|
|
|
add_transaction_sizes_row(&rows, _("Total Download Size:"), dlsize);
|
2011-07-22 13:01:58 -04:00
|
|
|
}
|
2011-09-28 05:46:22 -04:00
|
|
|
if(!config->op_s_downloadonly) {
|
2011-07-22 13:01:58 -04:00
|
|
|
if(isize > 0) {
|
2013-07-03 22:34:14 -04:00
|
|
|
add_transaction_sizes_row(&rows, _("Total Installed Size:"), isize);
|
2009-12-23 17:09:38 -05:00
|
|
|
}
|
2011-09-29 07:13:22 -04:00
|
|
|
if(rsize > 0 && isize == 0) {
|
2013-07-03 22:34:14 -04:00
|
|
|
add_transaction_sizes_row(&rows, _("Total Removed Size:"), rsize);
|
2011-07-22 13:01:58 -04:00
|
|
|
}
|
|
|
|
/* only show this net value if different from raw installed size */
|
|
|
|
if(isize > 0 && rsize > 0) {
|
2013-07-03 22:34:14 -04:00
|
|
|
add_transaction_sizes_row(&rows, _("Net Upgrade Size:"), isize - rsize);
|
2011-07-22 13:01:58 -04:00
|
|
|
}
|
2007-02-03 20:36:45 -05:00
|
|
|
}
|
2013-07-03 22:34:14 -04:00
|
|
|
table_display(NULL, rows, cols);
|
|
|
|
table_free(NULL, rows);
|
2008-07-24 20:02:36 -04:00
|
|
|
}
|
2007-02-03 20:36:45 -05:00
|
|
|
|
2011-07-22 13:01:58 -04:00
|
|
|
static int target_cmp(const void *p1, const void *p2)
|
|
|
|
{
|
|
|
|
const pm_target_t *targ1 = p1;
|
|
|
|
const pm_target_t *targ2 = p2;
|
2011-07-29 16:35:59 -04:00
|
|
|
/* explicit are always sorted after implicit (e.g. deps, pulled targets) */
|
|
|
|
if(targ1->is_explicit != targ2->is_explicit) {
|
|
|
|
return targ1->is_explicit > targ2->is_explicit;
|
|
|
|
}
|
2011-07-22 13:01:58 -04:00
|
|
|
const char *name1 = targ1->install ?
|
|
|
|
alpm_pkg_get_name(targ1->install) : alpm_pkg_get_name(targ1->remove);
|
|
|
|
const char *name2 = targ2->install ?
|
|
|
|
alpm_pkg_get_name(targ2->install) : alpm_pkg_get_name(targ2->remove);
|
|
|
|
return strcmp(name1, name2);
|
|
|
|
}
|
|
|
|
|
2011-07-29 16:35:59 -04:00
|
|
|
static int pkg_cmp(const void *p1, const void *p2)
|
|
|
|
{
|
|
|
|
/* explicit cast due to (un)necessary removal of const */
|
|
|
|
alpm_pkg_t *pkg1 = (alpm_pkg_t *)p1;
|
|
|
|
alpm_pkg_t *pkg2 = (alpm_pkg_t *)p2;
|
|
|
|
return strcmp(alpm_pkg_get_name(pkg1), alpm_pkg_get_name(pkg2));
|
|
|
|
}
|
|
|
|
|
2011-07-22 13:01:58 -04:00
|
|
|
void display_targets(void)
|
|
|
|
{
|
|
|
|
alpm_list_t *i, *targets = NULL;
|
2012-02-02 00:45:52 -05:00
|
|
|
alpm_db_t *db_local = alpm_get_localdb(config->handle);
|
2011-07-22 13:01:58 -04:00
|
|
|
|
|
|
|
for(i = alpm_trans_get_add(config->handle); i; i = alpm_list_next(i)) {
|
2011-10-06 01:55:47 -04:00
|
|
|
alpm_pkg_t *pkg = i->data;
|
2011-07-22 13:01:58 -04:00
|
|
|
pm_target_t *targ = calloc(1, sizeof(pm_target_t));
|
|
|
|
if(!targ) return;
|
|
|
|
targ->install = pkg;
|
|
|
|
targ->remove = alpm_db_get_pkg(db_local, alpm_pkg_get_name(pkg));
|
2011-07-29 16:35:59 -04:00
|
|
|
if(alpm_list_find(config->explicit_adds, pkg, pkg_cmp)) {
|
|
|
|
targ->is_explicit = 1;
|
|
|
|
}
|
2011-07-22 13:01:58 -04:00
|
|
|
targets = alpm_list_add(targets, targ);
|
|
|
|
}
|
|
|
|
for(i = alpm_trans_get_remove(config->handle); i; i = alpm_list_next(i)) {
|
2011-10-06 01:55:47 -04:00
|
|
|
alpm_pkg_t *pkg = i->data;
|
2011-07-22 13:01:58 -04:00
|
|
|
pm_target_t *targ = calloc(1, sizeof(pm_target_t));
|
|
|
|
if(!targ) return;
|
|
|
|
targ->remove = pkg;
|
2011-07-29 16:35:59 -04:00
|
|
|
if(alpm_list_find(config->explicit_removes, pkg, pkg_cmp)) {
|
|
|
|
targ->is_explicit = 1;
|
|
|
|
}
|
2011-07-22 13:01:58 -04:00
|
|
|
targets = alpm_list_add(targets, targ);
|
|
|
|
}
|
|
|
|
|
|
|
|
targets = alpm_list_msort(targets, alpm_list_count(targets), target_cmp);
|
2011-09-28 13:55:27 -04:00
|
|
|
_display_targets(targets, config->verbosepkglists);
|
2011-07-22 13:01:58 -04:00
|
|
|
FREELIST(targets);
|
|
|
|
}
|
|
|
|
|
2011-06-28 09:26:39 -04:00
|
|
|
static off_t pkg_get_size(alpm_pkg_t *pkg)
|
2009-07-19 05:15:11 -04:00
|
|
|
{
|
|
|
|
switch(config->op) {
|
|
|
|
case PM_OP_SYNC:
|
2011-03-20 20:45:57 -04:00
|
|
|
return alpm_pkg_download_size(pkg);
|
2009-07-19 05:15:11 -04:00
|
|
|
case PM_OP_UPGRADE:
|
2011-03-20 20:45:57 -04:00
|
|
|
return alpm_pkg_get_size(pkg);
|
2009-07-19 05:15:11 -04:00
|
|
|
default:
|
2011-03-20 20:45:57 -04:00
|
|
|
return alpm_pkg_get_isize(pkg);
|
2009-07-19 05:15:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-28 09:26:39 -04:00
|
|
|
static char *pkg_get_location(alpm_pkg_t *pkg)
|
2009-07-19 05:15:11 -04:00
|
|
|
{
|
2011-06-08 00:22:58 -04:00
|
|
|
alpm_list_t *servers;
|
|
|
|
char *string = NULL;
|
2009-07-19 05:15:11 -04:00
|
|
|
switch(config->op) {
|
|
|
|
case PM_OP_SYNC:
|
2011-06-08 00:22:58 -04:00
|
|
|
servers = alpm_db_get_servers(alpm_pkg_get_db(pkg));
|
|
|
|
if(servers) {
|
2013-02-10 01:47:39 -05:00
|
|
|
pm_asprintf(&string, "%s/%s", (char *)(servers->data),
|
2011-06-08 00:22:58 -04:00
|
|
|
alpm_pkg_get_filename(pkg));
|
|
|
|
return string;
|
2009-07-19 05:15:11 -04:00
|
|
|
}
|
|
|
|
case PM_OP_UPGRADE:
|
2011-03-20 20:45:57 -04:00
|
|
|
return strdup(alpm_pkg_get_filename(pkg));
|
2009-07-19 05:15:11 -04:00
|
|
|
default:
|
2010-09-27 01:30:39 -04:00
|
|
|
pm_asprintf(&string, "%s-%s", alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg));
|
2011-03-20 20:45:57 -04:00
|
|
|
return string;
|
2009-07-19 05:15:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-02 23:36:46 -05:00
|
|
|
/* a pow() implementation that is specialized for an integer base and small,
|
|
|
|
* positive-only integer exponents. */
|
|
|
|
static double simple_pow(int base, int exp)
|
|
|
|
{
|
|
|
|
double result = 1.0;
|
|
|
|
for(; exp > 0; exp--) {
|
|
|
|
result *= base;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-02-20 13:38:32 -05:00
|
|
|
/** Converts sizes in bytes into human readable units.
|
|
|
|
*
|
|
|
|
* @param bytes the size in bytes
|
|
|
|
* @param target_unit '\0' or a short label. If equal to one of the short unit
|
|
|
|
* labels ('B', 'K', ...) bytes is converted to target_unit; if '\0', the first
|
|
|
|
* unit which will bring the value to below a threshold of 2048 will be chosen.
|
2012-02-06 23:12:11 -05:00
|
|
|
* @param precision number of decimal places, ensures -0.00 gets rounded to
|
2012-02-02 17:43:40 -05:00
|
|
|
* 0.00; -1 if no rounding desired
|
2011-02-20 13:38:32 -05:00
|
|
|
* @param label will be set to the appropriate unit label
|
|
|
|
*
|
|
|
|
* @return the size in the appropriate unit
|
|
|
|
*/
|
2012-02-02 17:43:40 -05:00
|
|
|
double humanize_size(off_t bytes, const char target_unit, int precision,
|
|
|
|
const char **label)
|
2011-02-20 13:38:32 -05:00
|
|
|
{
|
2011-08-24 23:13:26 -04:00
|
|
|
static const char *labels[] = {"B", "KiB", "MiB", "GiB",
|
2011-08-24 22:52:32 -04:00
|
|
|
"TiB", "PiB", "EiB", "ZiB", "YiB"};
|
2011-08-24 23:13:26 -04:00
|
|
|
static const int unitcount = sizeof(labels) / sizeof(labels[0]);
|
2011-02-20 13:38:32 -05:00
|
|
|
|
|
|
|
double val = (double)bytes;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
for(index = 0; index < unitcount - 1; index++) {
|
2011-08-24 23:13:26 -04:00
|
|
|
if(target_unit != '\0' && labels[index][0] == target_unit) {
|
2011-02-20 13:38:32 -05:00
|
|
|
break;
|
2011-06-15 05:04:09 -04:00
|
|
|
} else if(target_unit == '\0' && val <= 2048.0 && val >= -2048.0) {
|
2011-02-20 13:38:32 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
val /= 1024.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(label) {
|
|
|
|
*label = labels[index];
|
|
|
|
}
|
|
|
|
|
2013-07-15 00:14:46 -04:00
|
|
|
/* do not display negative zeroes */
|
2012-02-02 23:36:46 -05:00
|
|
|
if(precision >= 0 && val < 0.0 &&
|
|
|
|
val > (-0.5 / simple_pow(10, precision))) {
|
2012-02-02 17:43:40 -05:00
|
|
|
val = 0.0;
|
|
|
|
}
|
|
|
|
|
2011-06-13 18:43:09 -04:00
|
|
|
return val;
|
2011-02-20 13:38:32 -05:00
|
|
|
}
|
|
|
|
|
2009-07-19 05:15:11 -04:00
|
|
|
void print_packages(const alpm_list_t *packages)
|
|
|
|
{
|
|
|
|
const alpm_list_t *i;
|
|
|
|
if(!config->print_format) {
|
|
|
|
config->print_format = strdup("%l");
|
|
|
|
}
|
|
|
|
for(i = packages; i; i = alpm_list_next(i)) {
|
2011-10-06 01:55:47 -04:00
|
|
|
alpm_pkg_t *pkg = i->data;
|
2009-07-19 05:15:11 -04:00
|
|
|
char *string = strdup(config->print_format);
|
|
|
|
char *temp = string;
|
|
|
|
/* %n : pkgname */
|
2011-10-13 13:30:31 -04:00
|
|
|
if(strstr(temp, "%n")) {
|
2009-07-19 05:15:11 -04:00
|
|
|
string = strreplace(temp, "%n", alpm_pkg_get_name(pkg));
|
|
|
|
free(temp);
|
|
|
|
temp = string;
|
|
|
|
}
|
|
|
|
/* %v : pkgver */
|
2011-10-13 13:30:31 -04:00
|
|
|
if(strstr(temp, "%v")) {
|
2009-07-19 05:15:11 -04:00
|
|
|
string = strreplace(temp, "%v", alpm_pkg_get_version(pkg));
|
|
|
|
free(temp);
|
|
|
|
temp = string;
|
|
|
|
}
|
|
|
|
/* %l : location */
|
2011-10-13 13:30:31 -04:00
|
|
|
if(strstr(temp, "%l")) {
|
2009-07-19 05:15:11 -04:00
|
|
|
char *pkgloc = pkg_get_location(pkg);
|
|
|
|
string = strreplace(temp, "%l", pkgloc);
|
|
|
|
free(pkgloc);
|
|
|
|
free(temp);
|
|
|
|
temp = string;
|
|
|
|
}
|
|
|
|
/* %r : repo */
|
2011-10-13 13:30:31 -04:00
|
|
|
if(strstr(temp, "%r")) {
|
2009-07-19 05:15:11 -04:00
|
|
|
const char *repo = "local";
|
2011-06-28 00:11:43 -04:00
|
|
|
alpm_db_t *db = alpm_pkg_get_db(pkg);
|
2009-07-19 05:15:11 -04:00
|
|
|
if(db) {
|
|
|
|
repo = alpm_db_get_name(db);
|
|
|
|
}
|
|
|
|
string = strreplace(temp, "%r", repo);
|
|
|
|
free(temp);
|
|
|
|
temp = string;
|
|
|
|
}
|
|
|
|
/* %s : size */
|
2011-10-13 13:30:31 -04:00
|
|
|
if(strstr(temp, "%s")) {
|
2009-07-19 05:15:11 -04:00
|
|
|
char *size;
|
2011-02-21 08:21:40 -05:00
|
|
|
pm_asprintf(&size, "%jd", (intmax_t)pkg_get_size(pkg));
|
2009-07-19 05:15:11 -04:00
|
|
|
string = strreplace(temp, "%s", size);
|
|
|
|
free(size);
|
|
|
|
free(temp);
|
|
|
|
}
|
2012-03-12 22:47:29 -04:00
|
|
|
printf("%s\n", string);
|
2009-07-19 05:15:11 -04:00
|
|
|
free(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:54:06 -04:00
|
|
|
/**
|
|
|
|
* Helper function for comparing depends using the alpm "compare func"
|
|
|
|
* signature. The function descends through the structure in the following
|
|
|
|
* comparison order: name, modifier (e.g., '>', '='), version, description.
|
|
|
|
* @param d1 the first depend structure
|
|
|
|
* @param d2 the second depend structure
|
|
|
|
* @return -1, 0, or 1 if first is <, ==, or > second
|
|
|
|
*/
|
|
|
|
static int depend_cmp(const void *d1, const void *d2)
|
2008-08-23 20:29:10 -04:00
|
|
|
{
|
2011-07-17 19:54:06 -04:00
|
|
|
const alpm_depend_t *dep1 = d1;
|
|
|
|
const alpm_depend_t *dep2 = d2;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = strcmp(dep1->name, dep2->name);
|
|
|
|
if(ret == 0) {
|
|
|
|
ret = dep1->mod - dep2->mod;
|
|
|
|
}
|
|
|
|
if(ret == 0) {
|
|
|
|
if(dep1->version && dep2->version) {
|
|
|
|
ret = strcmp(dep1->version, dep2->version);
|
|
|
|
} else if(!dep1->version && dep2->version) {
|
|
|
|
ret = -1;
|
|
|
|
} else if(dep1->version && !dep2->version) {
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(ret == 0) {
|
|
|
|
if(dep1->desc && dep2->desc) {
|
|
|
|
ret = strcmp(dep1->desc, dep2->desc);
|
|
|
|
} else if(!dep1->desc && dep2->desc) {
|
|
|
|
ret = -1;
|
|
|
|
} else if(dep1->desc && !dep2->desc) {
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2008-08-23 20:29:10 -04:00
|
|
|
}
|
|
|
|
|
2012-08-12 07:03:04 -04:00
|
|
|
static char *make_optstring(alpm_depend_t *optdep)
|
|
|
|
{
|
|
|
|
char *optstring = alpm_dep_compute_string(optdep);
|
|
|
|
char *status = NULL;
|
|
|
|
if(alpm_db_get_pkg(alpm_get_localdb(config->handle), optdep->name)) {
|
|
|
|
status = _(" [installed]");
|
|
|
|
} else if(alpm_pkg_find(alpm_trans_get_add(config->handle), optdep->name)) {
|
|
|
|
status = _(" [pending]");
|
|
|
|
}
|
|
|
|
if(status) {
|
|
|
|
optstring = realloc(optstring, strlen(optstring) + strlen(status) + 1);
|
|
|
|
strcpy(optstring + strlen(optstring), status);
|
|
|
|
}
|
|
|
|
return optstring;
|
|
|
|
}
|
|
|
|
|
2011-06-28 09:26:39 -04:00
|
|
|
void display_new_optdepends(alpm_pkg_t *oldpkg, alpm_pkg_t *newpkg)
|
2008-08-23 20:29:10 -04:00
|
|
|
{
|
2011-07-17 19:54:06 -04:00
|
|
|
alpm_list_t *i, *old, *new, *optdeps, *optstrings = NULL;
|
|
|
|
|
|
|
|
old = alpm_pkg_get_optdepends(oldpkg);
|
|
|
|
new = alpm_pkg_get_optdepends(newpkg);
|
|
|
|
optdeps = alpm_list_diff(new, old, depend_cmp);
|
|
|
|
|
|
|
|
/* turn optdepends list into a text list */
|
|
|
|
for(i = optdeps; i; i = alpm_list_next(i)) {
|
|
|
|
alpm_depend_t *optdep = i->data;
|
2012-08-12 07:03:04 -04:00
|
|
|
optstrings = alpm_list_add(optstrings, make_optstring(optdep));
|
2011-07-17 19:54:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(optstrings) {
|
2008-08-23 20:29:10 -04:00
|
|
|
printf(_("New optional dependencies for %s\n"), alpm_pkg_get_name(newpkg));
|
2014-06-21 08:44:01 -04:00
|
|
|
unsigned short cols = getcols();
|
2012-03-16 18:57:04 -04:00
|
|
|
list_display_linebreak(" ", optstrings, cols);
|
2008-08-23 20:29:10 -04:00
|
|
|
}
|
2011-07-17 19:54:06 -04:00
|
|
|
|
2008-08-23 20:29:10 -04:00
|
|
|
alpm_list_free(optdeps);
|
2011-07-17 19:54:06 -04:00
|
|
|
FREELIST(optstrings);
|
2008-08-23 20:29:10 -04:00
|
|
|
}
|
|
|
|
|
2011-06-28 09:26:39 -04:00
|
|
|
void display_optdepends(alpm_pkg_t *pkg)
|
2008-07-31 10:35:21 -04:00
|
|
|
{
|
2011-07-17 19:54:06 -04:00
|
|
|
alpm_list_t *i, *optdeps, *optstrings = NULL;
|
|
|
|
|
|
|
|
optdeps = alpm_pkg_get_optdepends(pkg);
|
|
|
|
|
|
|
|
/* turn optdepends list into a text list */
|
|
|
|
for(i = optdeps; i; i = alpm_list_next(i)) {
|
|
|
|
alpm_depend_t *optdep = i->data;
|
2012-08-12 07:03:04 -04:00
|
|
|
optstrings = alpm_list_add(optstrings, make_optstring(optdep));
|
2011-07-17 19:54:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(optstrings) {
|
2008-07-31 10:35:21 -04:00
|
|
|
printf(_("Optional dependencies for %s\n"), alpm_pkg_get_name(pkg));
|
2014-06-21 08:44:01 -04:00
|
|
|
unsigned short cols = getcols();
|
2012-03-16 18:57:04 -04:00
|
|
|
list_display_linebreak(" ", optstrings, cols);
|
2008-07-31 10:35:21 -04:00
|
|
|
}
|
2011-07-17 19:54:06 -04:00
|
|
|
|
|
|
|
FREELIST(optstrings);
|
2008-07-31 10:35:21 -04:00
|
|
|
}
|
|
|
|
|
2012-03-16 18:57:04 -04:00
|
|
|
static void display_repo_list(const char *dbname, alpm_list_t *list,
|
|
|
|
unsigned short cols)
|
2010-10-17 13:14:41 -04:00
|
|
|
{
|
2013-01-03 16:48:51 -05:00
|
|
|
const char *prefix = " ";
|
2010-10-17 13:14:41 -04:00
|
|
|
|
2013-03-01 13:40:02 -05:00
|
|
|
colon_printf(_("Repository %s\n"), dbname);
|
2012-03-16 18:57:04 -04:00
|
|
|
list_display(prefix, list, cols);
|
2010-10-17 13:14:41 -04:00
|
|
|
}
|
|
|
|
|
2010-10-16 06:28:01 -04:00
|
|
|
void select_display(const alpm_list_t *pkglist)
|
|
|
|
{
|
|
|
|
const alpm_list_t *i;
|
|
|
|
int nth = 1;
|
|
|
|
alpm_list_t *list = NULL;
|
|
|
|
char *string = NULL;
|
2010-10-17 13:14:41 -04:00
|
|
|
const char *dbname = NULL;
|
2014-06-21 08:44:01 -04:00
|
|
|
unsigned short cols = getcols();
|
2010-10-16 06:28:01 -04:00
|
|
|
|
2011-12-06 23:37:32 -05:00
|
|
|
for(i = pkglist; i; i = i->next) {
|
2011-10-06 01:55:47 -04:00
|
|
|
alpm_pkg_t *pkg = i->data;
|
2011-06-28 00:11:43 -04:00
|
|
|
alpm_db_t *db = alpm_pkg_get_db(pkg);
|
2010-10-17 13:14:41 -04:00
|
|
|
|
|
|
|
if(!dbname)
|
|
|
|
dbname = alpm_db_get_name(db);
|
|
|
|
if(strcmp(alpm_db_get_name(db), dbname) != 0) {
|
2012-03-16 18:57:04 -04:00
|
|
|
display_repo_list(dbname, list, cols);
|
2010-10-17 13:14:41 -04:00
|
|
|
FREELIST(list);
|
|
|
|
dbname = alpm_db_get_name(db);
|
|
|
|
}
|
2010-10-16 06:28:01 -04:00
|
|
|
string = NULL;
|
2010-10-17 13:14:41 -04:00
|
|
|
pm_asprintf(&string, "%d) %s", nth, alpm_pkg_get_name(pkg));
|
2010-10-16 06:28:01 -04:00
|
|
|
list = alpm_list_add(list, string);
|
|
|
|
nth++;
|
|
|
|
}
|
2012-03-16 18:57:04 -04:00
|
|
|
display_repo_list(dbname, list, cols);
|
2010-10-16 06:28:01 -04:00
|
|
|
FREELIST(list);
|
|
|
|
}
|
|
|
|
|
2010-10-17 11:09:25 -04:00
|
|
|
static int parseindex(char *s, int *val, int min, int max)
|
|
|
|
{
|
|
|
|
char *endptr = NULL;
|
|
|
|
int n = strtol(s, &endptr, 10);
|
|
|
|
if(*endptr == '\0') {
|
|
|
|
if(n < min || n > max) {
|
2011-10-21 11:51:49 -04:00
|
|
|
pm_printf(ALPM_LOG_ERROR,
|
|
|
|
_("invalid value: %d is not between %d and %d\n"),
|
2010-10-17 11:09:25 -04:00
|
|
|
n, min, max);
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2010-10-17 11:09:25 -04:00
|
|
|
}
|
|
|
|
*val = n;
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2010-10-17 11:09:25 -04:00
|
|
|
} else {
|
2011-10-21 11:51:49 -04:00
|
|
|
pm_printf(ALPM_LOG_ERROR, _("invalid number: %s\n"), s);
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2010-10-17 11:09:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int multiselect_parse(char *array, int count, char *response)
|
|
|
|
{
|
|
|
|
char *str, *saveptr;
|
|
|
|
|
2011-12-06 23:37:32 -05:00
|
|
|
for(str = response; ; str = NULL) {
|
2010-10-17 11:09:25 -04:00
|
|
|
int include = 1;
|
|
|
|
int start, end;
|
2011-12-23 15:47:30 -05:00
|
|
|
size_t len;
|
2010-10-17 11:09:25 -04:00
|
|
|
char *ends = NULL;
|
2012-05-05 13:34:07 -04:00
|
|
|
char *starts = strtok_r(str, " ,", &saveptr);
|
2010-10-17 11:09:25 -04:00
|
|
|
|
2011-04-20 20:45:16 -04:00
|
|
|
if(starts == NULL) {
|
2010-10-17 11:09:25 -04:00
|
|
|
break;
|
2011-04-02 14:51:25 -04:00
|
|
|
}
|
2011-12-23 15:47:30 -05:00
|
|
|
len = strtrim(starts);
|
2010-10-17 11:09:25 -04:00
|
|
|
if(len == 0)
|
|
|
|
continue;
|
|
|
|
|
2011-04-20 20:45:16 -04:00
|
|
|
if(*starts == '^') {
|
2010-10-17 11:09:25 -04:00
|
|
|
starts++;
|
|
|
|
len--;
|
|
|
|
include = 0;
|
|
|
|
} else if(str) {
|
|
|
|
/* if first token is including, we unselect all targets */
|
|
|
|
memset(array, 0, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(len > 1) {
|
|
|
|
/* check for range */
|
|
|
|
char *p;
|
2011-04-02 14:51:25 -04:00
|
|
|
if((p = strchr(starts + 1, '-'))) {
|
2010-10-17 11:09:25 -04:00
|
|
|
*p = 0;
|
2011-04-02 14:51:25 -04:00
|
|
|
ends = p + 1;
|
2010-10-17 11:09:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(parseindex(starts, &start, 1, count) != 0)
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2010-10-17 11:09:25 -04:00
|
|
|
|
|
|
|
if(!ends) {
|
2013-01-03 16:48:51 -05:00
|
|
|
array[start - 1] = include;
|
2010-10-17 11:09:25 -04:00
|
|
|
} else {
|
2011-04-02 14:51:25 -04:00
|
|
|
int d;
|
|
|
|
if(parseindex(ends, &end, start, count) != 0) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return -1;
|
2011-04-02 14:51:25 -04:00
|
|
|
}
|
|
|
|
for(d = start; d <= end; d++) {
|
2013-01-03 16:48:51 -05:00
|
|
|
array[d - 1] = include;
|
2010-10-17 11:09:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2010-10-17 11:09:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int multiselect_question(char *array, int count)
|
|
|
|
{
|
2011-07-25 11:05:36 -04:00
|
|
|
char *response, *lastchar;
|
2010-10-17 11:09:25 -04:00
|
|
|
FILE *stream;
|
2011-07-25 11:05:36 -04:00
|
|
|
size_t response_len = 64;
|
2010-10-17 11:09:25 -04:00
|
|
|
|
|
|
|
if(config->noconfirm) {
|
|
|
|
stream = stdout;
|
|
|
|
} else {
|
|
|
|
/* Use stderr so questions are always displayed when redirecting output */
|
|
|
|
stream = stderr;
|
|
|
|
}
|
|
|
|
|
2011-07-25 11:05:36 -04:00
|
|
|
response = malloc(response_len);
|
|
|
|
if(!response) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
lastchar = response + response_len - 1;
|
|
|
|
/* sentinel byte to later see if we filled up the entire string */
|
|
|
|
*lastchar = 1;
|
|
|
|
|
2010-10-17 11:09:25 -04:00
|
|
|
while(1) {
|
|
|
|
memset(array, 1, count);
|
|
|
|
|
|
|
|
fprintf(stream, "\n");
|
|
|
|
fprintf(stream, _("Enter a selection (default=all)"));
|
2013-05-04 09:39:28 -04:00
|
|
|
fprintf(stream, ": ");
|
2011-07-25 11:05:36 -04:00
|
|
|
fflush(stream);
|
2010-10-17 11:09:25 -04:00
|
|
|
|
|
|
|
if(config->noconfirm) {
|
|
|
|
fprintf(stream, "\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-03-16 18:05:06 -04:00
|
|
|
flush_term_input(fileno(stdin));
|
2011-03-18 11:03:28 -04:00
|
|
|
|
2014-08-06 16:36:00 -04:00
|
|
|
if(safe_fgets(response, response_len, stdin)) {
|
2011-07-25 11:05:36 -04:00
|
|
|
const size_t response_incr = 64;
|
2011-12-23 15:47:30 -05:00
|
|
|
size_t len;
|
2011-07-25 11:05:36 -04:00
|
|
|
/* handle buffer not being large enough to read full line case */
|
|
|
|
while(*lastchar == '\0' && lastchar[-1] != '\n') {
|
|
|
|
response_len += response_incr;
|
|
|
|
response = realloc(response, response_len);
|
|
|
|
if(!response) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
lastchar = response + response_len - 1;
|
|
|
|
/* sentinel byte */
|
|
|
|
*lastchar = 1;
|
2014-08-06 16:36:00 -04:00
|
|
|
if(safe_fgets(response + response_len - response_incr - 1,
|
2011-07-25 11:05:36 -04:00
|
|
|
response_incr + 1, stdin) == 0) {
|
|
|
|
free(response);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2011-12-23 15:47:30 -05:00
|
|
|
|
|
|
|
len = strtrim(response);
|
|
|
|
if(len > 0) {
|
2010-10-17 11:09:25 -04:00
|
|
|
if(multiselect_parse(array, count, response) == -1) {
|
|
|
|
/* only loop if user gave an invalid answer */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2011-07-25 11:05:36 -04:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
free(response);
|
|
|
|
return -1;
|
2010-10-17 11:09:25 -04:00
|
|
|
}
|
|
|
|
}
|
2011-07-25 11:05:36 -04:00
|
|
|
|
|
|
|
free(response);
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2010-10-17 11:09:25 -04:00
|
|
|
}
|
|
|
|
|
2010-10-16 06:28:01 -04:00
|
|
|
int select_question(int count)
|
|
|
|
{
|
|
|
|
char response[32];
|
|
|
|
FILE *stream;
|
|
|
|
int preset = 1;
|
|
|
|
|
|
|
|
if(config->noconfirm) {
|
|
|
|
stream = stdout;
|
|
|
|
} else {
|
|
|
|
/* Use stderr so questions are always displayed when redirecting output */
|
|
|
|
stream = stderr;
|
|
|
|
}
|
|
|
|
|
2010-10-17 11:18:36 -04:00
|
|
|
while(1) {
|
2010-10-16 06:28:01 -04:00
|
|
|
fprintf(stream, "\n");
|
2010-10-17 11:18:36 -04:00
|
|
|
fprintf(stream, _("Enter a number (default=%d)"), preset);
|
2013-05-04 09:39:28 -04:00
|
|
|
fprintf(stream, ": ");
|
|
|
|
fflush(stream);
|
2010-10-17 11:18:36 -04:00
|
|
|
|
|
|
|
if(config->noconfirm) {
|
|
|
|
fprintf(stream, "\n");
|
|
|
|
break;
|
|
|
|
}
|
2010-10-16 06:28:01 -04:00
|
|
|
|
2012-03-16 18:05:06 -04:00
|
|
|
flush_term_input(fileno(stdin));
|
2011-03-18 11:03:28 -04:00
|
|
|
|
2014-08-06 16:36:00 -04:00
|
|
|
if(safe_fgets(response, sizeof(response), stdin)) {
|
2011-12-23 15:47:30 -05:00
|
|
|
size_t len = strtrim(response);
|
|
|
|
if(len > 0) {
|
2010-10-17 11:18:36 -04:00
|
|
|
int n;
|
|
|
|
if(parseindex(response, &n, 1, count) != 0)
|
|
|
|
continue;
|
2011-03-20 20:45:57 -04:00
|
|
|
return (n - 1);
|
2010-10-16 06:28:01 -04:00
|
|
|
}
|
|
|
|
}
|
2010-10-17 11:18:36 -04:00
|
|
|
break;
|
2010-10-16 06:28:01 -04:00
|
|
|
}
|
2010-10-17 11:18:36 -04:00
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return (preset - 1);
|
2010-10-16 06:28:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 19:20:46 -04:00
|
|
|
/* presents a prompt and gets a Y/N answer */
|
2013-02-10 01:47:39 -05:00
|
|
|
__attribute__((format(printf, 2, 0)))
|
2013-03-01 13:52:14 -05:00
|
|
|
static int question(short preset, const char *format, va_list args)
|
2007-04-26 19:20:46 -04:00
|
|
|
{
|
|
|
|
char response[32];
|
2008-02-18 18:44:09 -05:00
|
|
|
FILE *stream;
|
2012-03-16 18:05:06 -04:00
|
|
|
int fd_in = fileno(stdin);
|
2007-04-26 19:20:46 -04:00
|
|
|
|
|
|
|
if(config->noconfirm) {
|
2008-02-18 18:44:09 -05:00
|
|
|
stream = stdout;
|
|
|
|
} else {
|
|
|
|
/* Use stderr so questions are always displayed when redirecting output */
|
|
|
|
stream = stderr;
|
2007-04-26 19:20:46 -04:00
|
|
|
}
|
|
|
|
|
2011-04-01 16:16:26 -04:00
|
|
|
/* ensure all text makes it to the screen before we prompt the user */
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
|
2013-03-06 11:53:04 -05:00
|
|
|
fputs(config->colstr.colon, stream);
|
2013-03-01 13:52:14 -05:00
|
|
|
vfprintf(stream, format, args);
|
2007-04-26 19:20:46 -04:00
|
|
|
|
2008-02-18 18:44:09 -05:00
|
|
|
if(preset) {
|
|
|
|
fprintf(stream, " %s ", _("[Y/n]"));
|
|
|
|
} else {
|
|
|
|
fprintf(stream, " %s ", _("[y/N]"));
|
|
|
|
}
|
|
|
|
|
2013-03-06 11:53:04 -05:00
|
|
|
fputs(config->colstr.nocolor, stream);
|
|
|
|
fflush(stream);
|
|
|
|
|
2008-02-18 18:44:09 -05:00
|
|
|
if(config->noconfirm) {
|
|
|
|
fprintf(stream, "\n");
|
2011-03-20 20:45:57 -04:00
|
|
|
return preset;
|
2008-02-18 18:44:09 -05:00
|
|
|
}
|
|
|
|
|
2012-03-16 18:05:06 -04:00
|
|
|
flush_term_input(fd_in);
|
2011-03-18 11:03:28 -04:00
|
|
|
|
2014-08-06 16:36:00 -04:00
|
|
|
if(safe_fgets(response, sizeof(response), stdin)) {
|
2011-12-23 15:47:30 -05:00
|
|
|
size_t len = strtrim(response);
|
|
|
|
if(len == 0) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return preset;
|
2007-04-26 19:20:46 -04:00
|
|
|
}
|
|
|
|
|
2012-01-12 10:08:48 -05:00
|
|
|
/* if stdin is piped, response does not get printed out, and as a result
|
2013-07-15 00:14:46 -04:00
|
|
|
* a \n is missing, resulting in broken output */
|
2012-03-16 18:05:06 -04:00
|
|
|
if(!isatty(fd_in)) {
|
2012-01-12 10:08:48 -05:00
|
|
|
fprintf(stream, "%s\n", response);
|
|
|
|
}
|
|
|
|
|
2010-06-19 04:55:08 -04:00
|
|
|
if(strcasecmp(response, _("Y")) == 0 || strcasecmp(response, _("YES")) == 0) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return 1;
|
2011-04-20 20:45:16 -04:00
|
|
|
} else if(strcasecmp(response, _("N")) == 0 || strcasecmp(response, _("NO")) == 0) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2007-04-26 19:20:46 -04:00
|
|
|
}
|
|
|
|
}
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2007-04-26 19:20:46 -04:00
|
|
|
}
|
|
|
|
|
2013-03-01 13:52:14 -05:00
|
|
|
int yesno(const char *format, ...)
|
2008-08-20 16:08:11 -04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list args;
|
|
|
|
|
2013-03-01 13:52:14 -05:00
|
|
|
va_start(args, format);
|
|
|
|
ret = question(1, format, args);
|
2008-08-20 16:08:11 -04:00
|
|
|
va_end(args);
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return ret;
|
2008-08-20 16:08:11 -04:00
|
|
|
}
|
|
|
|
|
2013-03-01 13:52:14 -05:00
|
|
|
int noyes(const char *format, ...)
|
2008-08-20 16:08:11 -04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list args;
|
|
|
|
|
2013-03-01 13:52:14 -05:00
|
|
|
va_start(args, format);
|
|
|
|
ret = question(0, format, args);
|
2008-08-20 16:08:11 -04:00
|
|
|
va_end(args);
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return ret;
|
2008-08-20 16:08:11 -04:00
|
|
|
}
|
|
|
|
|
2013-03-01 13:40:02 -05:00
|
|
|
int colon_printf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
2013-03-06 11:53:04 -05:00
|
|
|
fputs(config->colstr.colon, stdout);
|
2013-03-01 13:40:02 -05:00
|
|
|
ret = vprintf(fmt, args);
|
2013-03-06 11:53:04 -05:00
|
|
|
fputs(config->colstr.nocolor, stdout);
|
2013-03-01 13:40:02 -05:00
|
|
|
va_end(args);
|
|
|
|
|
2013-03-06 11:53:04 -05:00
|
|
|
fflush(stdout);
|
2013-03-01 13:40:02 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-06-28 00:41:07 -04:00
|
|
|
int pm_printf(alpm_loglevel_t level, const char *format, ...)
|
2007-06-07 15:42:26 -04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
/* print the message using va_arg list */
|
|
|
|
va_start(args, format);
|
2011-10-21 11:38:02 -04:00
|
|
|
ret = pm_vfprintf(stderr, level, format, args);
|
2007-06-07 15:42:26 -04:00
|
|
|
va_end(args);
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return ret;
|
2007-06-07 15:42:26 -04:00
|
|
|
}
|
|
|
|
|
2010-09-27 01:30:39 -04:00
|
|
|
int pm_asprintf(char **string, const char *format, ...)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
/* print the message using va_arg list */
|
|
|
|
va_start(args, format);
|
|
|
|
if(vasprintf(string, format, args) == -1) {
|
2013-11-08 00:44:40 -05:00
|
|
|
pm_printf(ALPM_LOG_ERROR, _("failed to allocate string\n"));
|
2010-09-27 01:30:39 -04:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return ret;
|
2010-09-27 01:30:39 -04:00
|
|
|
}
|
|
|
|
|
2014-02-12 10:32:30 -05:00
|
|
|
int pm_sprintf(char **string, alpm_loglevel_t level, const char *format, ...)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
/* print the message using va_arg list */
|
|
|
|
va_start(args, format);
|
|
|
|
ret = pm_vasprintf(string, level, format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-06-28 00:41:07 -04:00
|
|
|
int pm_vasprintf(char **string, alpm_loglevel_t level, const char *format, va_list args)
|
2007-12-03 16:57:54 -05:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
char *msg = NULL;
|
|
|
|
|
|
|
|
/* if current logmask does not overlap with level, do not print msg */
|
|
|
|
if(!(config->logmask & level)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print the message using va_arg list */
|
|
|
|
ret = vasprintf(&msg, format, args);
|
|
|
|
|
|
|
|
/* print a prefix to the message */
|
|
|
|
switch(level) {
|
2011-07-01 12:01:38 -04:00
|
|
|
case ALPM_LOG_ERROR:
|
2013-11-20 21:55:39 -05:00
|
|
|
pm_asprintf(string, "%s%s%s%s", config->colstr.err, _("error: "),
|
|
|
|
config->colstr.nocolor, msg);
|
2007-12-03 16:57:54 -05:00
|
|
|
break;
|
2011-07-01 12:01:38 -04:00
|
|
|
case ALPM_LOG_WARNING:
|
2013-11-20 21:55:39 -05:00
|
|
|
pm_asprintf(string, "%s%s%s%s", config->colstr.warn, _("warning: "),
|
|
|
|
config->colstr.nocolor, msg);
|
2007-12-03 16:57:54 -05:00
|
|
|
break;
|
2011-07-01 12:01:38 -04:00
|
|
|
case ALPM_LOG_DEBUG:
|
2011-02-28 18:50:23 -05:00
|
|
|
pm_asprintf(string, "debug: %s", msg);
|
|
|
|
break;
|
2011-07-01 12:01:38 -04:00
|
|
|
case ALPM_LOG_FUNCTION:
|
2011-02-28 18:50:23 -05:00
|
|
|
pm_asprintf(string, "function: %s", msg);
|
2007-12-03 16:57:54 -05:00
|
|
|
break;
|
|
|
|
default:
|
2011-02-28 18:50:23 -05:00
|
|
|
pm_asprintf(string, "%s", msg);
|
2007-12-03 16:57:54 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(msg);
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return ret;
|
2007-12-03 16:57:54 -05:00
|
|
|
}
|
|
|
|
|
2011-06-28 00:41:07 -04:00
|
|
|
int pm_vfprintf(FILE *stream, alpm_loglevel_t level, const char *format, va_list args)
|
2007-06-07 15:42:26 -04:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* if current logmask does not overlap with level, do not print msg */
|
|
|
|
if(!(config->logmask & level)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(PACMAN_DEBUG)
|
|
|
|
/* If debug is on, we'll timestamp the output */
|
2011-07-01 12:01:38 -04:00
|
|
|
if(config->logmask & ALPM_LOG_DEBUG) {
|
2007-06-07 15:42:26 -04:00
|
|
|
time_t t;
|
|
|
|
struct tm *tmp;
|
|
|
|
char timestr[10] = {0};
|
|
|
|
|
|
|
|
t = time(NULL);
|
|
|
|
tmp = localtime(&t);
|
|
|
|
strftime(timestr, 9, "%H:%M:%S", tmp);
|
|
|
|
timestr[8] = '\0';
|
|
|
|
|
2011-08-20 13:07:15 -04:00
|
|
|
fprintf(stream, "[%s] ", timestr);
|
2007-06-07 15:42:26 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* print a prefix to the message */
|
|
|
|
switch(level) {
|
2011-07-01 12:01:38 -04:00
|
|
|
case ALPM_LOG_ERROR:
|
2013-03-01 14:56:18 -05:00
|
|
|
fprintf(stream, "%s%s%s", config->colstr.err, _("error: "),
|
|
|
|
config->colstr.nocolor);
|
2007-06-07 15:42:26 -04:00
|
|
|
break;
|
2011-07-01 12:01:38 -04:00
|
|
|
case ALPM_LOG_WARNING:
|
2013-03-01 14:56:18 -05:00
|
|
|
fprintf(stream, "%s%s%s", config->colstr.warn, _("warning: "),
|
|
|
|
config->colstr.nocolor);
|
2007-06-07 15:42:26 -04:00
|
|
|
break;
|
2011-07-01 12:01:38 -04:00
|
|
|
case ALPM_LOG_DEBUG:
|
2011-02-28 18:50:23 -05:00
|
|
|
fprintf(stream, "debug: ");
|
|
|
|
break;
|
2011-07-01 12:01:38 -04:00
|
|
|
case ALPM_LOG_FUNCTION:
|
2011-02-28 18:50:23 -05:00
|
|
|
fprintf(stream, "function: ");
|
2007-06-07 15:42:26 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print the message using va_arg list */
|
|
|
|
ret = vfprintf(stream, format, args);
|
2011-03-20 20:45:57 -04:00
|
|
|
return ret;
|
2007-06-07 15:42:26 -04:00
|
|
|
}
|
|
|
|
|
2014-01-22 18:06:11 -05:00
|
|
|
/* vim: set noet: */
|