2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* util.c
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2011-01-05 23:45:15 -05:00
|
|
|
* Copyright (c) 2006-2011 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
|
|
|
*/
|
|
|
|
|
2007-03-05 17:13:33 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
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>
|
2007-02-03 20:36:45 -05:00
|
|
|
#include <sys/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"
|
|
|
|
|
|
|
|
|
2011-06-28 00:42:24 -04:00
|
|
|
int trans_init(alpm_transflag_t flags)
|
2008-04-26 05:30:49 -04:00
|
|
|
{
|
2009-07-19 05:15:11 -04:00
|
|
|
int ret;
|
|
|
|
if(config->print) {
|
2011-06-07 14:29:05 -04:00
|
|
|
ret = alpm_trans_init(config->handle, flags, NULL, NULL, NULL);
|
2009-07-19 05:15:11 -04:00
|
|
|
} else {
|
2011-06-07 14:29:05 -04:00
|
|
|
ret = alpm_trans_init(config->handle, flags, cb_trans_evt, cb_trans_conv,
|
2009-07-19 05:15:11 -04:00
|
|
|
cb_trans_progress);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ret == -1) {
|
2011-06-28 00:50:48 -04:00
|
|
|
enum _alpm_errno_t err = alpm_errno(config->handle);
|
2011-07-01 12:01:38 -04:00
|
|
|
pm_fprintf(stderr, ALPM_LOG_ERROR, _("failed to init transaction (%s)\n"),
|
2011-06-07 17:06:16 -04:00
|
|
|
alpm_strerror(err));
|
2011-07-01 12:01:39 -04:00
|
|
|
if(err == ALPM_ERR_HANDLE_LOCK) {
|
2008-04-26 05:30:49 -04:00
|
|
|
fprintf(stderr, _(" if you're sure a package manager is not already\n"
|
2011-06-07 14:15:43 -04:00
|
|
|
" running, you can remove %s\n"),
|
|
|
|
alpm_option_get_lockfile(config->handle));
|
2008-04-26 05:30:49 -04:00
|
|
|
}
|
2011-02-28 11:46:00 -05:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-07-01 12:01:38 -04:00
|
|
|
pm_fprintf(stderr, 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-03-18 11:03:28 -04:00
|
|
|
/* discard unhandled input on the terminal's input buffer */
|
|
|
|
static int flush_term_input(void) {
|
|
|
|
#ifdef HAVE_TCFLUSH
|
|
|
|
if(isatty(fileno(stdin))) {
|
2011-06-13 18:43:09 -04:00
|
|
|
return tcflush(fileno(stdin), TCIFLUSH);
|
2011-03-18 11:03:28 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* fail silently */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-11-20 04:10:23 -05:00
|
|
|
/* gets the current screen column width */
|
2011-06-13 22:23:49 -04:00
|
|
|
int getcols()
|
2006-11-20 04:10:23 -05:00
|
|
|
{
|
2011-06-13 22:23:49 -04:00
|
|
|
int termwidth = -1;
|
|
|
|
const int default_tty = 80;
|
|
|
|
const int default_notty = 0;
|
|
|
|
|
|
|
|
if(!isatty(fileno(stdout))) {
|
|
|
|
return default_notty;
|
|
|
|
}
|
|
|
|
|
2006-11-21 23:53:10 -05:00
|
|
|
#ifdef TIOCGSIZE
|
2009-08-26 18:11:41 -04:00
|
|
|
struct ttysize win;
|
|
|
|
if(ioctl(1, TIOCGSIZE, &win) == 0) {
|
2011-06-13 22:23:49 -04:00
|
|
|
termwidth = 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;
|
|
|
|
if(ioctl(1, TIOCGWINSZ, &win) == 0) {
|
2011-06-13 22:23:49 -04:00
|
|
|
termwidth = win.ws_col;
|
2006-11-20 04:10:23 -05:00
|
|
|
}
|
2009-08-26 18:11:41 -04:00
|
|
|
#endif
|
2011-06-13 22:23:49 -04:00
|
|
|
return termwidth <= 0 ? default_tty : termwidth;
|
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 {
|
|
|
|
if(errno == ENOENT) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return 0;
|
2005-03-14 20:51:43 -05:00
|
|
|
} else if(errno == EPERM) {
|
|
|
|
/* fallthrough */
|
|
|
|
} else if(errno == EISDIR) {
|
|
|
|
/* fallthrough */
|
|
|
|
} else if(errno == ENOTDIR) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return 1;
|
2005-03-14 20:51:43 -05:00
|
|
|
} else {
|
|
|
|
/* 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)) {
|
|
|
|
if(dp->d_ino) {
|
2006-01-07 04:42:48 -05:00
|
|
|
char name[PATH_MAX];
|
2005-03-14 20:51:43 -05:00
|
|
|
sprintf(name, "%s/%s", path, dp->d_name);
|
2010-06-19 04:55:08 -04:00
|
|
|
if(strcmp(dp->d_name, "..") != 0 && strcmp(dp->d_name, ".") != 0) {
|
2005-03-14 20:51:43 -05:00
|
|
|
errflag += rmrf(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
if(rmdir(path)) {
|
|
|
|
errflag++;
|
|
|
|
}
|
2011-03-20 20:45:57 -04:00
|
|
|
return errflag;
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-25 17:13:30 -05:00
|
|
|
/** Parse the basename of a program from a path.
|
|
|
|
* @param path path to parse basename from
|
|
|
|
*
|
|
|
|
* @return everything following the final '/'
|
|
|
|
*/
|
2011-01-22 15:20:42 -05:00
|
|
|
const char *mbasename(const char *path)
|
2007-11-25 17:13:30 -05:00
|
|
|
{
|
2011-01-22 15:20:42 -05:00
|
|
|
const char *last = strrchr(path, '/');
|
|
|
|
if(last) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return last + 1;
|
2007-11-25 17:13:30 -05:00
|
|
|
}
|
2011-03-20 20:45:57 -04:00
|
|
|
return path;
|
2007-11-25 17:13:30 -05:00
|
|
|
}
|
|
|
|
|
2008-02-05 19:49:13 -05:00
|
|
|
/** Parse the dirname of a program from a path.
|
|
|
|
* The path returned should be freed.
|
|
|
|
* @param path path to parse dirname from
|
|
|
|
*
|
|
|
|
* @return everything preceding the final '/'
|
|
|
|
*/
|
|
|
|
char *mdirname(const char *path)
|
|
|
|
{
|
|
|
|
char *ret, *last;
|
|
|
|
|
|
|
|
/* null or empty path */
|
|
|
|
if(path == NULL || path == '\0') {
|
2011-03-20 20:45:57 -04:00
|
|
|
return strdup(".");
|
2008-02-05 19:49:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = strdup(path);
|
|
|
|
last = strrchr(ret, '/');
|
|
|
|
|
|
|
|
if(last != NULL) {
|
|
|
|
/* we found a '/', so terminate our string */
|
|
|
|
*last = '\0';
|
2011-03-20 20:45:57 -04:00
|
|
|
return ret;
|
2008-02-05 19:49:13 -05:00
|
|
|
}
|
|
|
|
/* no slash found */
|
|
|
|
free(ret);
|
2011-03-20 20:45:57 -04:00
|
|
|
return strdup(".");
|
2008-02-05 19:49:13 -05:00
|
|
|
}
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* output a string, but wrap words properly with a specified indentation
|
|
|
|
*/
|
2007-03-13 12:15:38 -04:00
|
|
|
void indentprint(const char *str, int indent)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2008-02-23 00:47:03 -05:00
|
|
|
wchar_t *wcstr;
|
|
|
|
const wchar_t *p;
|
2011-06-01 14:28:00 -04:00
|
|
|
int len, cidx;
|
2011-06-13 22:23:49 -04:00
|
|
|
const int cols = getcols();
|
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) {
|
2009-08-26 18:11:41 -04:00
|
|
|
printf("%s", str);
|
|
|
|
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
|
|
|
}
|
2008-09-20 11:09:17 -04:00
|
|
|
if(len > (cols - cidx - 1)) {
|
2008-02-23 00:47:03 -05:00
|
|
|
/* wrap to a newline and reindent */
|
2009-08-26 18:11:41 -04:00
|
|
|
printf("\n%-*s", 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
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert a string to uppercase
|
|
|
|
*/
|
|
|
|
char *strtoupper(char *str)
|
|
|
|
{
|
|
|
|
char *ptr = str;
|
|
|
|
|
|
|
|
while(*ptr) {
|
2011-01-07 22:06:06 -05:00
|
|
|
(*ptr) = (char)toupper((unsigned char)*ptr);
|
2005-03-14 20:51:43 -05:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Trim whitespace and newlines from a string
|
|
|
|
*/
|
|
|
|
char *strtrim(char *str)
|
|
|
|
{
|
|
|
|
char *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-03-20 20:45:57 -04:00
|
|
|
return str;
|
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) {
|
|
|
|
memmove(str, pch, (strlen(pch) + 1));
|
|
|
|
}
|
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-03-20 20:45:57 -04:00
|
|
|
return str;
|
2007-06-04 12:01:53 -04:00
|
|
|
}
|
|
|
|
|
2007-05-14 03:16:55 -04:00
|
|
|
pch = (str + (strlen(str) - 1));
|
2009-09-25 18:58:42 -04:00
|
|
|
while(isspace((unsigned char)*pch)) {
|
2005-03-14 20:51:43 -05:00
|
|
|
pch--;
|
|
|
|
}
|
|
|
|
*++pch = '\0';
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return str;
|
2007-06-04 12:01:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace all occurances of 'needle' with 'replace' in 'str', returning
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
/* no occurences of needle found */
|
|
|
|
if(!list) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return strdup(str);
|
2009-09-06 19:15:39 -04:00
|
|
|
}
|
|
|
|
/* size of new string = size of old string + "number of occurences of needle"
|
|
|
|
* x "size difference between replace and needle" */
|
|
|
|
newsz = strlen(str) + 1 +
|
|
|
|
alpm_list_count(list) * (replacesz - needlesz);
|
|
|
|
newstr = malloc(newsz);
|
|
|
|
if(!newstr) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return NULL;
|
2009-09-06 19:15:39 -04:00
|
|
|
}
|
|
|
|
*newstr = '\0';
|
|
|
|
|
|
|
|
p = str;
|
|
|
|
newp = newstr;
|
|
|
|
for(i = list; i; i = alpm_list_next(i)) {
|
|
|
|
q = alpm_list_getdata(i);
|
|
|
|
if(q > p){
|
|
|
|
/* add chars between this occurence and last occurence, if any */
|
2011-01-07 22:06:06 -05:00
|
|
|
strncpy(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
|
|
|
}
|
2009-09-06 19:15:39 -04:00
|
|
|
strncpy(newp, replace, replacesz);
|
|
|
|
newp += replacesz;
|
|
|
|
p = q + needlesz;
|
|
|
|
}
|
|
|
|
alpm_list_free(list);
|
|
|
|
|
|
|
|
if(*p) {
|
|
|
|
/* add the rest of 'p' */
|
|
|
|
strcpy(newp, p);
|
|
|
|
newp += strlen(p);
|
2007-06-04 12:01:53 -04:00
|
|
|
}
|
2009-09-06 19:15:39 -04:00
|
|
|
*newp = '\0';
|
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
|
|
|
}
|
|
|
|
|
2007-11-13 19:32:56 -05:00
|
|
|
/** Splits a string into a list of strings using the chosen character as
|
|
|
|
* a delimiter.
|
|
|
|
*
|
|
|
|
* @param str the string to split
|
|
|
|
* @param splitchar the character to split at
|
|
|
|
*
|
|
|
|
* @return a list containing the duplicated strings
|
|
|
|
*/
|
|
|
|
alpm_list_t *strsplit(const char *str, const char splitchar)
|
|
|
|
{
|
|
|
|
alpm_list_t *list = NULL;
|
|
|
|
const char *prev = str;
|
|
|
|
char *dup = NULL;
|
|
|
|
|
|
|
|
while((str = strchr(str, splitchar))) {
|
2011-01-07 22:06:06 -05:00
|
|
|
dup = strndup(prev, (size_t)(str - prev));
|
2007-11-13 19:32:56 -05:00
|
|
|
if(dup == NULL) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return NULL;
|
2007-11-13 19:32:56 -05:00
|
|
|
}
|
|
|
|
list = alpm_list_add(list, dup);
|
|
|
|
|
|
|
|
str++;
|
|
|
|
prev = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
dup = strdup(prev);
|
|
|
|
if(dup == NULL) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return NULL;
|
2007-11-13 19:32:56 -05:00
|
|
|
}
|
2008-03-23 16:16:31 -04:00
|
|
|
list = alpm_list_add(list, dup);
|
2007-11-13 19:32:56 -05:00
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return list;
|
2007-11-13 19:32:56 -05:00
|
|
|
}
|
|
|
|
|
2008-07-31 07:38:30 -04:00
|
|
|
static int string_length(const char *s)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
wchar_t *wcstr;
|
|
|
|
|
|
|
|
if(!s) {
|
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
|
|
|
}
|
|
|
|
|
2007-11-23 16:32:40 -05:00
|
|
|
void string_display(const char *title, const char *string)
|
|
|
|
{
|
2008-07-31 07:38:30 -04:00
|
|
|
if(title) {
|
|
|
|
printf("%s ", title);
|
|
|
|
}
|
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-06-01 14:28:00 -04:00
|
|
|
int len = string_length(title) + 1;
|
2008-07-31 07:38:30 -04:00
|
|
|
indentprint(string, len);
|
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-02-19 17:23:42 -05:00
|
|
|
static void table_print_line(const alpm_list_t *line,
|
|
|
|
const alpm_list_t *formats)
|
|
|
|
{
|
|
|
|
const alpm_list_t *curformat = formats;
|
|
|
|
const alpm_list_t *curcell = line;
|
|
|
|
|
|
|
|
while(curcell && curformat) {
|
|
|
|
printf(alpm_list_getdata(curformat), alpm_list_getdata(curcell));
|
|
|
|
curcell = alpm_list_next(curcell);
|
|
|
|
curformat = alpm_list_next(curformat);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* creates format strings by checking max cell lengths in cols */
|
|
|
|
static alpm_list_t *table_create_format(const alpm_list_t *header,
|
|
|
|
const alpm_list_t *rows)
|
|
|
|
{
|
|
|
|
alpm_list_t *longest_str, *longest_strs = NULL;
|
|
|
|
alpm_list_t *formats = NULL;
|
|
|
|
const alpm_list_t *i, *row, *cell;
|
|
|
|
char *str, *formatstr;
|
|
|
|
const int padding = 2;
|
|
|
|
int colwidth, totalwidth = 0;
|
|
|
|
int curcol = 0;
|
|
|
|
|
|
|
|
/* header determines column count and initial values of longest_strs */
|
|
|
|
for(i = header; i; i = alpm_list_next(i)) {
|
|
|
|
longest_strs = alpm_list_add(longest_strs, alpm_list_getdata(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now find the longest string in each column */
|
|
|
|
for(longest_str = longest_strs; longest_str;
|
|
|
|
longest_str = alpm_list_next(longest_str), curcol++) {
|
|
|
|
for(i = rows; i; i = alpm_list_next(i)) {
|
|
|
|
row = alpm_list_getdata(i);
|
|
|
|
cell = alpm_list_nth(row, curcol);
|
|
|
|
str = alpm_list_getdata(cell);
|
|
|
|
|
|
|
|
if(strlen(str) > strlen(alpm_list_getdata(longest_str))) {
|
|
|
|
longest_str->data = str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now use the column width info to generate format strings */
|
|
|
|
for(i = longest_strs; i; i = alpm_list_next(i)) {
|
2011-05-04 16:48:47 -04:00
|
|
|
const char *display;
|
2011-02-19 17:23:42 -05:00
|
|
|
colwidth = strlen(alpm_list_getdata(i)) + padding;
|
|
|
|
totalwidth += colwidth;
|
|
|
|
|
|
|
|
/* right align the last column for a cleaner table display */
|
2011-05-04 16:48:47 -04:00
|
|
|
display = (alpm_list_next(i) != NULL) ? "%%-%ds" : "%%%ds";
|
|
|
|
pm_asprintf(&formatstr, display, colwidth);
|
2011-02-19 17:23:42 -05:00
|
|
|
|
|
|
|
formats = alpm_list_add(formats, formatstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
alpm_list_free(longest_strs);
|
|
|
|
|
|
|
|
/* return NULL if terminal is not wide enough */
|
2011-06-13 22:23:49 -04:00
|
|
|
if(totalwidth > getcols()) {
|
|
|
|
fprintf(stderr, _("insufficient columns available for table display\n"));
|
2011-02-19 17:23:42 -05:00
|
|
|
FREELIST(formats);
|
2011-06-13 18:43:09 -04:00
|
|
|
return NULL;
|
2011-02-19 17:23:42 -05:00
|
|
|
}
|
|
|
|
|
2011-06-13 18:43:09 -04:00
|
|
|
return formats;
|
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)
|
|
|
|
*
|
|
|
|
* @return -1 if not enough terminal cols available, else 0
|
|
|
|
*/
|
|
|
|
int table_display(const char *title, const alpm_list_t *header,
|
|
|
|
const alpm_list_t *rows)
|
|
|
|
{
|
|
|
|
const alpm_list_t *i;
|
|
|
|
alpm_list_t *formats;
|
|
|
|
|
|
|
|
if(rows == NULL || header == NULL) {
|
2011-06-13 18:43:09 -04:00
|
|
|
return 0;
|
2011-02-19 17:23:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
formats = table_create_format(header, rows);
|
|
|
|
if(formats == NULL) {
|
2011-06-13 18:43:09 -04:00
|
|
|
return -1;
|
2011-02-19 17:23:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if(title != NULL) {
|
|
|
|
printf("%s\n\n", title);
|
|
|
|
}
|
|
|
|
|
|
|
|
table_print_line(header, formats);
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
for(i = rows; i; i = alpm_list_next(i)) {
|
|
|
|
table_print_line(alpm_list_getdata(i), formats);
|
|
|
|
}
|
|
|
|
|
|
|
|
FREELIST(formats);
|
2011-06-13 18:43:09 -04:00
|
|
|
return 0;
|
2011-02-19 17:23:42 -05:00
|
|
|
}
|
|
|
|
|
2007-06-05 17:34:33 -04:00
|
|
|
void list_display(const char *title, const alpm_list_t *list)
|
2007-01-19 04:28:44 -05:00
|
|
|
{
|
2007-06-05 17:34:33 -04:00
|
|
|
const alpm_list_t *i;
|
2011-06-01 14:28:00 -04:00
|
|
|
int 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;
|
2008-02-24 02:17:17 -05:00
|
|
|
printf("%s ", title);
|
|
|
|
}
|
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-06-13 22:23:49 -04:00
|
|
|
const int maxcols = getcols();
|
2011-06-13 18:04:35 -04:00
|
|
|
int cols = len;
|
|
|
|
const char *str = alpm_list_getdata(list);
|
|
|
|
printf("%s", str);
|
|
|
|
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-06-27 11:10:08 -04:00
|
|
|
str = alpm_list_getdata(i);
|
2008-07-31 09:58:25 -04:00
|
|
|
int 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) {
|
2008-07-31 09:58:25 -04:00
|
|
|
int j;
|
2007-01-19 04:28:44 -05:00
|
|
|
cols = len;
|
|
|
|
printf("\n");
|
2008-07-31 09:58:25 -04: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
|
|
|
}
|
2009-12-14 00:12:44 -05:00
|
|
|
printf("%s", str);
|
2007-01-19 04:28:44 -05:00
|
|
|
cols += s;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-31 09:58:25 -04:00
|
|
|
void list_display_linebreak(const char *title, const alpm_list_t *list)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
if(title) {
|
|
|
|
len = string_length(title) + 1;
|
|
|
|
printf("%s ", title);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
|
|
|
indentprint((const char *) alpm_list_getdata(list), len);
|
|
|
|
printf("\n");
|
|
|
|
/* Print the rest */
|
|
|
|
for(i = alpm_list_next(list); i; i = alpm_list_next(i)) {
|
|
|
|
int j;
|
|
|
|
for(j = 1; j <= len; j++) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
indentprint((const char *) alpm_list_getdata(i), len);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-20 08:42:05 -05:00
|
|
|
|
2011-07-01 17:50:32 -04:00
|
|
|
void signature_display(const char *title, alpm_sigresult_t *result)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
if(title) {
|
|
|
|
len = string_length(title) + 1;
|
|
|
|
printf("%s ", title);
|
|
|
|
}
|
|
|
|
if(result->count == 0) {
|
|
|
|
printf(_("None"));
|
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < result->count; i++) {
|
|
|
|
char sigline[PATH_MAX];
|
|
|
|
const char *validity, *name;
|
|
|
|
/* Don't re-indent the first result */
|
|
|
|
if(i != 0) {
|
|
|
|
int j;
|
|
|
|
for(j = 1; j <= len; j++) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch(result->status[i]) {
|
|
|
|
case ALPM_SIGSTATUS_VALID:
|
|
|
|
validity = _("Valid signature");
|
|
|
|
break;
|
|
|
|
case ALPM_SIGSTATUS_MARGINAL:
|
|
|
|
validity = _("Marginal signature");
|
|
|
|
break;
|
|
|
|
case ALPM_SIGSTATUS_UNKNOWN:
|
|
|
|
validity = _("Unknown signature");
|
|
|
|
break;
|
|
|
|
case ALPM_SIGSTATUS_BAD:
|
|
|
|
validity = _("Invalid signature");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
validity = _("Signature error");
|
|
|
|
}
|
|
|
|
name = result->uid[i] ? result->uid[i] : _("<Key Unknown>");
|
|
|
|
snprintf(sigline, PATH_MAX, _("%s from \"%s\""), validity, name);
|
|
|
|
indentprint(sigline, len);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-20 09:00:26 -05:00
|
|
|
/* creates a header row for use with table_display */
|
|
|
|
static alpm_list_t *create_verbose_header(int install)
|
|
|
|
{
|
|
|
|
alpm_list_t *res = NULL;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
pm_asprintf(&str, "%s", _("Name"));
|
|
|
|
res = alpm_list_add(res, str);
|
|
|
|
pm_asprintf(&str, "%s", _("Old Version"));
|
|
|
|
res = alpm_list_add(res, str);
|
|
|
|
if(install) {
|
|
|
|
pm_asprintf(&str, "%s", _("New Version"));
|
|
|
|
res = alpm_list_add(res, str);
|
|
|
|
}
|
|
|
|
pm_asprintf(&str, "%s", _("Size"));
|
|
|
|
res = alpm_list_add(res, str);
|
|
|
|
|
2011-06-13 18:43:09 -04:00
|
|
|
return res;
|
2011-02-20 09:00:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* returns package info as list of strings */
|
2011-06-28 09:26:39 -04:00
|
|
|
static alpm_list_t *create_verbose_row(alpm_pkg_t *pkg, int install)
|
2011-02-20 09:00:26 -05:00
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
double size;
|
|
|
|
const char *label;
|
|
|
|
alpm_list_t *ret = NULL;
|
2011-06-28 00:11:43 -04:00
|
|
|
alpm_db_t *ldb = alpm_option_get_localdb(config->handle);
|
2011-02-20 09:00:26 -05:00
|
|
|
|
|
|
|
/* a row consists of the package name, */
|
|
|
|
pm_asprintf(&str, "%s", alpm_pkg_get_name(pkg));
|
|
|
|
ret = alpm_list_add(ret, str);
|
|
|
|
|
|
|
|
/* old and new versions */
|
|
|
|
if(install) {
|
2011-06-28 09:26:39 -04:00
|
|
|
alpm_pkg_t *oldpkg = alpm_db_get_pkg(ldb, alpm_pkg_get_name(pkg));
|
2011-02-20 09:00:26 -05:00
|
|
|
pm_asprintf(&str, "%s",
|
|
|
|
oldpkg != NULL ? alpm_pkg_get_version(oldpkg) : "");
|
|
|
|
ret = alpm_list_add(ret, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
pm_asprintf(&str, "%s", alpm_pkg_get_version(pkg));
|
|
|
|
ret = alpm_list_add(ret, str);
|
|
|
|
|
|
|
|
/* and size */
|
|
|
|
size = humanize_size(alpm_pkg_get_size(pkg), 'M', 1, &label);
|
|
|
|
pm_asprintf(&str, "%.2f %s", size, label);
|
|
|
|
ret = alpm_list_add(ret, str);
|
|
|
|
|
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 */
|
|
|
|
void display_targets(const alpm_list_t *pkgs, int install)
|
2007-02-03 20:36:45 -05:00
|
|
|
{
|
|
|
|
char *str;
|
2011-02-20 08:42:05 -05:00
|
|
|
const char *title, *label;
|
2011-02-20 13:38:32 -05:00
|
|
|
double size;
|
2008-07-24 20:02:36 -04:00
|
|
|
const alpm_list_t *i;
|
2011-06-01 18:11:45 -04:00
|
|
|
off_t isize = 0, rsize = 0, dlsize = 0;
|
2011-02-20 09:00:26 -05:00
|
|
|
alpm_list_t *j, *lp, *header = NULL, *targets = NULL;
|
2011-06-28 00:11:43 -04:00
|
|
|
alpm_db_t *db_local = alpm_option_get_localdb(config->handle);
|
2008-01-27 06:24:50 -05:00
|
|
|
|
2008-07-24 20:02:36 -04:00
|
|
|
if(!pkgs) {
|
|
|
|
return;
|
|
|
|
}
|
2008-01-27 06:24:50 -05:00
|
|
|
|
2011-02-20 09:00:26 -05:00
|
|
|
/* gather pkg infos */
|
2008-07-24 20:02:36 -04:00
|
|
|
for(i = pkgs; i; i = alpm_list_next(i)) {
|
2011-06-28 09:26:39 -04:00
|
|
|
alpm_pkg_t *pkg = alpm_list_getdata(i);
|
2007-02-03 20:36:45 -05:00
|
|
|
|
2011-01-11 21:16:54 -05:00
|
|
|
if(install) {
|
2011-06-28 09:26:39 -04:00
|
|
|
alpm_pkg_t *lpkg = alpm_db_get_pkg(db_local, alpm_pkg_get_name(pkg));
|
2011-01-11 21:16:54 -05:00
|
|
|
dlsize += alpm_pkg_download_size(pkg);
|
2011-06-01 18:11:45 -04:00
|
|
|
if(lpkg) {
|
|
|
|
/* add up size of all removed packages */
|
|
|
|
rsize += alpm_pkg_get_isize(lpkg);
|
|
|
|
}
|
2011-01-11 21:16:54 -05:00
|
|
|
}
|
2007-02-08 15:44:47 -05:00
|
|
|
isize += alpm_pkg_get_isize(pkg);
|
2007-02-03 20:36:45 -05:00
|
|
|
|
2011-02-20 09:00:26 -05:00
|
|
|
if(config->verbosepkglists) {
|
|
|
|
targets = alpm_list_add(targets, create_verbose_row(pkg, install));
|
|
|
|
} else {
|
|
|
|
pm_asprintf(&str, "%s-%s", alpm_pkg_get_name(pkg),
|
|
|
|
alpm_pkg_get_version(pkg));
|
|
|
|
targets = alpm_list_add(targets, str);
|
|
|
|
}
|
2007-02-03 20:36:45 -05:00
|
|
|
}
|
|
|
|
|
2011-02-20 09:00:26 -05:00
|
|
|
/* print to screen */
|
2011-02-20 08:42:05 -05:00
|
|
|
title = install ? _("Targets (%d):") : _("Remove (%d):");
|
|
|
|
pm_asprintf(&str, title, alpm_list_count(pkgs));
|
|
|
|
|
|
|
|
printf("\n");
|
2011-02-20 09:00:26 -05:00
|
|
|
if(config->verbosepkglists) {
|
|
|
|
header = create_verbose_header(install);
|
|
|
|
if(table_display(str, header, targets) != 0) {
|
|
|
|
config->verbosepkglists = 0;
|
|
|
|
display_targets(pkgs, install);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
list_display(str, targets);
|
|
|
|
}
|
2011-02-20 08:42:05 -05:00
|
|
|
printf("\n");
|
2007-11-16 21:18:45 -05:00
|
|
|
|
2011-02-20 08:42:05 -05:00
|
|
|
if(install) {
|
2011-02-20 13:38:32 -05:00
|
|
|
size = humanize_size(dlsize, 'M', 1, &label);
|
|
|
|
printf(_("Total Download Size: %.2f %s\n"), size, label);
|
2011-07-01 12:01:39 -04:00
|
|
|
if(!(config->flags & ALPM_TRANS_FLAG_DOWNLOADONLY)) {
|
2011-02-20 13:38:32 -05:00
|
|
|
size = humanize_size(isize, 'M', 1, &label);
|
|
|
|
printf(_("Total Installed Size: %.2f %s\n"), size, label);
|
2011-06-01 18:11:45 -04:00
|
|
|
/* only show this net value if different from raw installed size */
|
|
|
|
if(rsize > 0) {
|
|
|
|
size = humanize_size(isize - rsize, 'M', 1, &label);
|
|
|
|
printf(_("Net Upgrade Size: %.2f %s\n"), size, label);
|
|
|
|
}
|
2009-12-23 17:09:38 -05:00
|
|
|
}
|
2008-07-24 20:02:36 -04:00
|
|
|
} else {
|
2011-02-20 13:38:32 -05:00
|
|
|
size = humanize_size(isize, 'M', 1, &label);
|
|
|
|
printf(_("Total Removed Size: %.2f %s\n"), size, label);
|
2007-02-03 20:36:45 -05:00
|
|
|
}
|
|
|
|
|
2011-02-20 09:00:26 -05:00
|
|
|
out:
|
|
|
|
/* cleanup */
|
|
|
|
if(config->verbosepkglists) {
|
|
|
|
/* targets is a list of lists of strings, free inner lists here */
|
|
|
|
for(j = alpm_list_first(targets); j; j = alpm_list_next(j)) {
|
|
|
|
lp = alpm_list_getdata(j);
|
|
|
|
FREELIST(lp);
|
|
|
|
}
|
|
|
|
alpm_list_free(targets);
|
|
|
|
FREELIST(header);
|
|
|
|
} else {
|
|
|
|
FREELIST(targets);
|
|
|
|
}
|
2011-02-20 08:42:05 -05:00
|
|
|
free(str);
|
2008-07-24 20:02:36 -04:00
|
|
|
}
|
2007-02-03 20:36:45 -05:00
|
|
|
|
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) {
|
|
|
|
pm_asprintf(&string, "%s/%s", alpm_list_getdata(servers),
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2011-03-24 18:34:17 -04:00
|
|
|
* @param long_labels whether to use short ("K") or long ("KiB") unit labels
|
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
|
|
|
|
*/
|
|
|
|
double humanize_size(off_t bytes, const char target_unit, int long_labels,
|
|
|
|
const char **label)
|
|
|
|
{
|
|
|
|
static const char *shortlabels[] = {"B", "K", "M", "G", "T", "P"};
|
2011-03-24 18:34:17 -04:00
|
|
|
static const char *longlabels[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"};
|
2011-02-20 13:38:32 -05:00
|
|
|
static const int unitcount = sizeof(shortlabels) / sizeof(shortlabels[0]);
|
|
|
|
|
|
|
|
const char **labels = long_labels ? longlabels : shortlabels;
|
|
|
|
double val = (double)bytes;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
for(index = 0; index < unitcount - 1; index++) {
|
|
|
|
if(target_unit != '\0' && shortlabels[index][0] == target_unit) {
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
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-06-28 09:26:39 -04:00
|
|
|
alpm_pkg_t *pkg = alpm_list_getdata(i);
|
2009-07-19 05:15:11 -04:00
|
|
|
char *string = strdup(config->print_format);
|
|
|
|
char *temp = string;
|
|
|
|
/* %n : pkgname */
|
|
|
|
if(strstr(temp,"%n")) {
|
|
|
|
string = strreplace(temp, "%n", alpm_pkg_get_name(pkg));
|
|
|
|
free(temp);
|
|
|
|
temp = string;
|
|
|
|
}
|
|
|
|
/* %v : pkgver */
|
|
|
|
if(strstr(temp,"%v")) {
|
|
|
|
string = strreplace(temp, "%v", alpm_pkg_get_version(pkg));
|
|
|
|
free(temp);
|
|
|
|
temp = string;
|
|
|
|
}
|
|
|
|
/* %l : location */
|
|
|
|
if(strstr(temp,"%l")) {
|
|
|
|
char *pkgloc = pkg_get_location(pkg);
|
|
|
|
string = strreplace(temp, "%l", pkgloc);
|
|
|
|
free(pkgloc);
|
|
|
|
free(temp);
|
|
|
|
temp = string;
|
|
|
|
}
|
|
|
|
/* %r : repo */
|
|
|
|
if(strstr(temp,"%r")) {
|
|
|
|
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 */
|
|
|
|
if(strstr(temp,"%s")) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
printf("%s\n",string);
|
|
|
|
free(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-23 20:29:10 -04:00
|
|
|
/* Helper function for comparing strings using the
|
|
|
|
* alpm "compare func" signature */
|
|
|
|
int str_cmp(const void *s1, const void *s2)
|
|
|
|
{
|
2011-03-20 20:45:57 -04:00
|
|
|
return strcmp(s1, s2);
|
2008-08-23 20:29:10 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
alpm_list_t *old = alpm_pkg_get_optdepends(oldpkg);
|
|
|
|
alpm_list_t *new = alpm_pkg_get_optdepends(newpkg);
|
|
|
|
alpm_list_t *optdeps = alpm_list_diff(new,old,str_cmp);
|
|
|
|
if(optdeps) {
|
|
|
|
printf(_("New optional dependencies for %s\n"), alpm_pkg_get_name(newpkg));
|
|
|
|
list_display_linebreak(" ", optdeps);
|
|
|
|
}
|
|
|
|
alpm_list_free(optdeps);
|
|
|
|
}
|
|
|
|
|
2011-06-28 09:26:39 -04:00
|
|
|
void display_optdepends(alpm_pkg_t *pkg)
|
2008-07-31 10:35:21 -04:00
|
|
|
{
|
|
|
|
alpm_list_t *optdeps = alpm_pkg_get_optdepends(pkg);
|
|
|
|
if(optdeps) {
|
|
|
|
printf(_("Optional dependencies for %s\n"), alpm_pkg_get_name(pkg));
|
|
|
|
list_display_linebreak(" ", optdeps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-17 13:14:41 -04:00
|
|
|
static void display_repo_list(const char *dbname, alpm_list_t *list)
|
|
|
|
{
|
|
|
|
const char *prefix= " ";
|
|
|
|
|
|
|
|
printf(":: ");
|
|
|
|
printf(_("Repository %s\n"), dbname);
|
|
|
|
list_display(prefix, list);
|
|
|
|
}
|
|
|
|
|
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;
|
2010-10-16 06:28:01 -04:00
|
|
|
|
|
|
|
for (i = pkglist; i; i = i->next) {
|
2011-06-28 09:26:39 -04:00
|
|
|
alpm_pkg_t *pkg = alpm_list_getdata(i);
|
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) {
|
|
|
|
display_repo_list(dbname, list);
|
|
|
|
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++;
|
|
|
|
}
|
2010-10-17 13:14:41 -04:00
|
|
|
display_repo_list(dbname, list);
|
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) {
|
|
|
|
fprintf(stderr, _("Invalid value: %d is not between %d and %d\n"),
|
|
|
|
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 {
|
|
|
|
fprintf(stderr, _("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;
|
|
|
|
|
|
|
|
for (str = response; ; str = NULL) {
|
|
|
|
int include = 1;
|
|
|
|
int start, end;
|
|
|
|
char *ends = NULL;
|
|
|
|
char *starts = strtok_r(str, " ", &saveptr);
|
|
|
|
|
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
|
|
|
}
|
2010-10-17 11:09:25 -04:00
|
|
|
strtrim(starts);
|
|
|
|
int len = strlen(starts);
|
|
|
|
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) {
|
|
|
|
array[start-1] = include;
|
|
|
|
} 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++) {
|
2010-10-17 11:09:25 -04:00
|
|
|
array[d-1] = include;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
char response[64];
|
|
|
|
FILE *stream;
|
|
|
|
|
|
|
|
if(config->noconfirm) {
|
|
|
|
stream = stdout;
|
|
|
|
} else {
|
|
|
|
/* Use stderr so questions are always displayed when redirecting output */
|
|
|
|
stream = stderr;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
memset(array, 1, count);
|
|
|
|
|
|
|
|
fprintf(stream, "\n");
|
|
|
|
fprintf(stream, _("Enter a selection (default=all)"));
|
|
|
|
fprintf(stream, ": ");
|
|
|
|
|
|
|
|
if(config->noconfirm) {
|
|
|
|
fprintf(stream, "\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-18 11:03:28 -04:00
|
|
|
flush_term_input();
|
|
|
|
|
2010-10-17 11:09:25 -04:00
|
|
|
if(fgets(response, sizeof(response), stdin)) {
|
|
|
|
strtrim(response);
|
|
|
|
if(strlen(response) > 0) {
|
|
|
|
if(multiselect_parse(array, count, response) == -1) {
|
|
|
|
/* only loop if user gave an invalid answer */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
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);
|
|
|
|
fprintf(stream, ": ");
|
|
|
|
|
|
|
|
if(config->noconfirm) {
|
|
|
|
fprintf(stream, "\n");
|
|
|
|
break;
|
|
|
|
}
|
2010-10-16 06:28:01 -04:00
|
|
|
|
2011-03-18 11:03:28 -04:00
|
|
|
flush_term_input();
|
|
|
|
|
2010-10-17 11:18:36 -04:00
|
|
|
if(fgets(response, sizeof(response), stdin)) {
|
|
|
|
strtrim(response);
|
|
|
|
if(strlen(response) > 0) {
|
|
|
|
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 */
|
2008-08-20 16:08:11 -04:00
|
|
|
static int question(short preset, char *fmt, va_list args)
|
2007-04-26 19:20:46 -04:00
|
|
|
{
|
|
|
|
char response[32];
|
2008-02-18 18:44:09 -05:00
|
|
|
FILE *stream;
|
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);
|
|
|
|
|
2008-02-18 18:44:09 -05:00
|
|
|
vfprintf(stream, fmt, 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]"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(config->noconfirm) {
|
|
|
|
fprintf(stream, "\n");
|
2011-03-20 20:45:57 -04:00
|
|
|
return preset;
|
2008-02-18 18:44:09 -05:00
|
|
|
}
|
|
|
|
|
2011-04-01 16:16:26 -04:00
|
|
|
fflush(stream);
|
2011-03-18 11:03:28 -04:00
|
|
|
flush_term_input();
|
|
|
|
|
2010-07-07 18:12:42 -04:00
|
|
|
if(fgets(response, sizeof(response), stdin)) {
|
2008-02-18 18:44:09 -05:00
|
|
|
strtrim(response);
|
|
|
|
if(strlen(response) == 0) {
|
2011-03-20 20:45:57 -04:00
|
|
|
return preset;
|
2007-04-26 19:20:46 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-08-20 16:08:11 -04:00
|
|
|
int yesno(char *fmt, ...)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
ret = question(1, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return ret;
|
2008-08-20 16:08:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int noyes(char *fmt, ...)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
ret = question(0, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return ret;
|
2008-08-20 16:08:11 -04:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
ret = pm_vfprintf(stdout, level, format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return ret;
|
2007-06-07 15:42:26 -04:00
|
|
|
}
|
|
|
|
|
2011-06-28 00:41:07 -04:00
|
|
|
int pm_fprintf(FILE *stream, 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);
|
|
|
|
ret = pm_vfprintf(stream, level, format, args);
|
|
|
|
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) {
|
2011-07-01 12:01:38 -04:00
|
|
|
pm_fprintf(stderr, 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
|
|
|
}
|
|
|
|
|
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:
|
2010-09-27 01:30:39 -04:00
|
|
|
pm_asprintf(string, _("error: %s"), msg);
|
2007-12-03 16:57:54 -05:00
|
|
|
break;
|
2011-07-01 12:01:38 -04:00
|
|
|
case ALPM_LOG_WARNING:
|
2010-09-27 01:30:39 -04:00
|
|
|
pm_asprintf(string, _("warning: %s"), 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';
|
|
|
|
|
|
|
|
printf("[%s] ", timestr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* print a prefix to the message */
|
|
|
|
switch(level) {
|
2011-07-01 12:01:38 -04:00
|
|
|
case ALPM_LOG_ERROR:
|
2007-06-07 15:42:26 -04:00
|
|
|
fprintf(stream, _("error: "));
|
|
|
|
break;
|
2011-07-01 12:01:38 -04:00
|
|
|
case ALPM_LOG_WARNING:
|
2007-06-07 15:42:26 -04:00
|
|
|
fprintf(stream, _("warning: "));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-10-24 01:37:50 -04:00
|
|
|
#ifndef HAVE_STRNDUP
|
|
|
|
/* A quick and dirty implementation derived from glibc */
|
|
|
|
static size_t strnlen(const char *s, size_t max)
|
|
|
|
{
|
|
|
|
register const char *p;
|
|
|
|
for(p = s; *p && max--; ++p);
|
2011-03-20 20:45:57 -04:00
|
|
|
return (p - s);
|
2007-10-24 01:37:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char *strndup(const char *s, size_t n)
|
|
|
|
{
|
|
|
|
size_t len = strnlen(s, n);
|
|
|
|
char *new = (char *) malloc(len + 1);
|
|
|
|
|
2011-04-20 20:45:16 -04:00
|
|
|
if(new == NULL)
|
2007-10-24 01:37:50 -04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
new[len] = '\0';
|
2011-03-20 20:45:57 -04:00
|
|
|
return (char *)memcpy(new, s, len);
|
2007-10-24 01:37:50 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* vim: set ts=2 sw=2 noet: */
|