4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
24 #include <sys/ioctl.h>
31 #include <stdint.h> /* intmax_t */
40 #include <termios.h> /* tcflush */
44 #include <alpm_list.h>
52 int trans_init(alpm_transflag_t flags
)
56 ret
= alpm_trans_init(config
->handle
, flags
, NULL
, NULL
, NULL
);
58 ret
= alpm_trans_init(config
->handle
, flags
, cb_trans_evt
, cb_trans_conv
,
63 enum _alpm_errno_t err
= alpm_errno(config
->handle
);
64 pm_fprintf(stderr
, PM_LOG_ERROR
, _("failed to init transaction (%s)\n"),
66 if(err
== PM_ERR_HANDLE_LOCK
) {
67 fprintf(stderr
, _(" if you're sure a package manager is not already\n"
68 " running, you can remove %s\n"),
69 alpm_option_get_lockfile(config
->handle
));
77 int trans_release(void)
79 if(alpm_trans_release(config
->handle
) == -1) {
80 pm_fprintf(stderr
, PM_LOG_ERROR
, _("failed to release transaction (%s)\n"),
81 alpm_strerror(alpm_errno(config
->handle
)));
94 return !config
->print
;
96 return (config
->op_s_clean
|| config
->op_s_sync
||
97 (!config
->group
&& !config
->op_s_info
&& !config
->op_q_list
&&
98 !config
->op_s_search
&& !config
->print
));
104 /* discard unhandled input on the terminal's input buffer */
105 static int flush_term_input(void) {
107 if(isatty(fileno(stdin
))) {
108 return tcflush(fileno(stdin
), TCIFLUSH
);
116 /* gets the current screen column width */
120 const int default_tty
= 80;
121 const int default_notty
= 0;
123 if(!isatty(fileno(stdout
))) {
124 return default_notty
;
129 if(ioctl(1, TIOCGSIZE
, &win
) == 0) {
130 termwidth
= win
.ts_cols
;
132 #elif defined(TIOCGWINSZ)
134 if(ioctl(1, TIOCGWINSZ
, &win
) == 0) {
135 termwidth
= win
.ws_col
;
138 return termwidth
<= 0 ? default_tty
: termwidth
;
141 /* does the same thing as 'rm -rf' */
142 int rmrf(const char *path
)
151 if(errno
== ENOENT
) {
153 } else if(errno
== EPERM
) {
155 } else if(errno
== EISDIR
) {
157 } else if(errno
== ENOTDIR
) {
160 /* not a directory */
164 dirp
= opendir(path
);
168 for(dp
= readdir(dirp
); dp
!= NULL
; dp
= readdir(dirp
)) {
171 sprintf(name
, "%s/%s", path
, dp
->d_name
);
172 if(strcmp(dp
->d_name
, "..") != 0 && strcmp(dp
->d_name
, ".") != 0) {
173 errflag
+= rmrf(name
);
185 /** Parse the basename of a program from a path.
186 * @param path path to parse basename from
188 * @return everything following the final '/'
190 const char *mbasename(const char *path
)
192 const char *last
= strrchr(path
, '/');
199 /** Parse the dirname of a program from a path.
200 * The path returned should be freed.
201 * @param path path to parse dirname from
203 * @return everything preceding the final '/'
205 char *mdirname(const char *path
)
209 /* null or empty path */
210 if(path
== NULL
|| path
== '\0') {
215 last
= strrchr(ret
, '/');
218 /* we found a '/', so terminate our string */
227 /* output a string, but wrap words properly with a specified indentation
229 void indentprint(const char *str
, int indent
)
234 const int cols
= getcols();
240 /* if we're not a tty, or our tty is not wide enough that wrapping even makes
241 * sense, print without indenting */
242 if(cols
== 0 || indent
> cols
) {
247 len
= strlen(str
) + 1;
248 wcstr
= calloc(len
, sizeof(wchar_t));
249 len
= mbstowcs(wcstr
, str
, len
);
259 const wchar_t *q
, *next
;
261 if(p
== NULL
|| *p
== L
' ') continue;
262 next
= wcschr(p
, L
' ');
264 next
= p
+ wcslen(p
);
266 /* len captures # cols */
270 len
+= wcwidth(*q
++);
272 if(len
> (cols
- cidx
- 1)) {
273 /* wrap to a newline and reindent */
274 printf("\n%-*s", indent
, "");
282 printf("%lc", (wint_t)*p
);
289 /* Convert a string to uppercase
291 char *strtoupper(char *str
)
296 (*ptr
) = (char)toupper((unsigned char)*ptr
);
302 /* Trim whitespace and newlines from a string
304 char *strtrim(char *str
)
308 if(str
== NULL
|| *str
== '\0') {
309 /* string is empty, so we're done. */
313 while(isspace((unsigned char)*pch
)) {
317 memmove(str
, pch
, (strlen(pch
) + 1));
320 /* check if there wasn't anything but whitespace in the string. */
325 pch
= (str
+ (strlen(str
) - 1));
326 while(isspace((unsigned char)*pch
)) {
334 /* Replace all occurances of 'needle' with 'replace' in 'str', returning
335 * a new string (must be free'd) */
336 char *strreplace(const char *str
, const char *needle
, const char *replace
)
338 const char *p
= NULL
, *q
= NULL
;
339 char *newstr
= NULL
, *newp
= NULL
;
340 alpm_list_t
*i
= NULL
, *list
= NULL
;
341 size_t needlesz
= strlen(needle
), replacesz
= strlen(replace
);
349 q
= strstr(p
, needle
);
351 list
= alpm_list_add(list
, (char *)q
);
353 q
= strstr(p
, needle
);
356 /* no occurences of needle found */
360 /* size of new string = size of old string + "number of occurences of needle"
361 * x "size difference between replace and needle" */
362 newsz
= strlen(str
) + 1 +
363 alpm_list_count(list
) * (replacesz
- needlesz
);
364 newstr
= malloc(newsz
);
372 for(i
= list
; i
; i
= alpm_list_next(i
)) {
373 q
= alpm_list_getdata(i
);
375 /* add chars between this occurence and last occurence, if any */
376 strncpy(newp
, p
, (size_t)(q
- p
));
379 strncpy(newp
, replace
, replacesz
);
383 alpm_list_free(list
);
386 /* add the rest of 'p' */
395 /** Splits a string into a list of strings using the chosen character as
398 * @param str the string to split
399 * @param splitchar the character to split at
401 * @return a list containing the duplicated strings
403 alpm_list_t
*strsplit(const char *str
, const char splitchar
)
405 alpm_list_t
*list
= NULL
;
406 const char *prev
= str
;
409 while((str
= strchr(str
, splitchar
))) {
410 dup
= strndup(prev
, (size_t)(str
- prev
));
414 list
= alpm_list_add(list
, dup
);
424 list
= alpm_list_add(list
, dup
);
429 static int string_length(const char *s
)
437 /* len goes from # bytes -> # chars -> # cols */
439 wcstr
= calloc(len
, sizeof(wchar_t));
440 len
= mbstowcs(wcstr
, s
, len
);
441 len
= wcswidth(wcstr
, len
);
447 void string_display(const char *title
, const char *string
)
450 printf("%s ", title
);
452 if(string
== NULL
|| string
[0] == '\0') {
455 /* compute the length of title + a space */
456 int len
= string_length(title
) + 1;
457 indentprint(string
, len
);
462 static void table_print_line(const alpm_list_t
*line
,
463 const alpm_list_t
*formats
)
465 const alpm_list_t
*curformat
= formats
;
466 const alpm_list_t
*curcell
= line
;
468 while(curcell
&& curformat
) {
469 printf(alpm_list_getdata(curformat
), alpm_list_getdata(curcell
));
470 curcell
= alpm_list_next(curcell
);
471 curformat
= alpm_list_next(curformat
);
477 /* creates format strings by checking max cell lengths in cols */
478 static alpm_list_t
*table_create_format(const alpm_list_t
*header
,
479 const alpm_list_t
*rows
)
481 alpm_list_t
*longest_str
, *longest_strs
= NULL
;
482 alpm_list_t
*formats
= NULL
;
483 const alpm_list_t
*i
, *row
, *cell
;
484 char *str
, *formatstr
;
485 const int padding
= 2;
486 int colwidth
, totalwidth
= 0;
489 /* header determines column count and initial values of longest_strs */
490 for(i
= header
; i
; i
= alpm_list_next(i
)) {
491 longest_strs
= alpm_list_add(longest_strs
, alpm_list_getdata(i
));
494 /* now find the longest string in each column */
495 for(longest_str
= longest_strs
; longest_str
;
496 longest_str
= alpm_list_next(longest_str
), curcol
++) {
497 for(i
= rows
; i
; i
= alpm_list_next(i
)) {
498 row
= alpm_list_getdata(i
);
499 cell
= alpm_list_nth(row
, curcol
);
500 str
= alpm_list_getdata(cell
);
502 if(strlen(str
) > strlen(alpm_list_getdata(longest_str
))) {
503 longest_str
->data
= str
;
508 /* now use the column width info to generate format strings */
509 for(i
= longest_strs
; i
; i
= alpm_list_next(i
)) {
511 colwidth
= strlen(alpm_list_getdata(i
)) + padding
;
512 totalwidth
+= colwidth
;
514 /* right align the last column for a cleaner table display */
515 display
= (alpm_list_next(i
) != NULL
) ? "%%-%ds" : "%%%ds";
516 pm_asprintf(&formatstr
, display
, colwidth
);
518 formats
= alpm_list_add(formats
, formatstr
);
521 alpm_list_free(longest_strs
);
523 /* return NULL if terminal is not wide enough */
524 if(totalwidth
> getcols()) {
525 fprintf(stderr
, _("insufficient columns available for table display\n"));
533 /** Displays the list in table format
535 * @param title the tables title
536 * @param header the column headers. column count is determined by the nr
538 * @param rows the rows to display as a list of lists of strings. the outer
539 * list represents the rows, the inner list the cells (= columns)
541 * @return -1 if not enough terminal cols available, else 0
543 int table_display(const char *title
, const alpm_list_t
*header
,
544 const alpm_list_t
*rows
)
546 const alpm_list_t
*i
;
547 alpm_list_t
*formats
;
549 if(rows
== NULL
|| header
== NULL
) {
553 formats
= table_create_format(header
, rows
);
554 if(formats
== NULL
) {
559 printf("%s\n\n", title
);
562 table_print_line(header
, formats
);
565 for(i
= rows
; i
; i
= alpm_list_next(i
)) {
566 table_print_line(alpm_list_getdata(i
), formats
);
573 void list_display(const char *title
, const alpm_list_t
*list
)
575 const alpm_list_t
*i
;
579 len
= string_length(title
) + 1;
580 printf("%s ", title
);
584 printf("%s\n", _("None"));
586 const int maxcols
= getcols();
588 const char *str
= alpm_list_getdata(list
);
590 cols
+= string_length(str
);
591 for(i
= alpm_list_next(list
); i
; i
= alpm_list_next(i
)) {
592 str
= alpm_list_getdata(i
);
593 int s
= string_length(str
);
594 /* wrap only if we have enough usable column space */
595 if(maxcols
> len
&& cols
+ s
+ 2 >= maxcols
) {
599 for (j
= 1; j
<= len
; j
++) {
602 } else if(cols
!= len
) {
603 /* 2 spaces are added if this is not the first element on a line. */
614 void list_display_linebreak(const char *title
, const alpm_list_t
*list
)
616 const alpm_list_t
*i
;
620 len
= string_length(title
) + 1;
621 printf("%s ", title
);
625 printf("%s\n", _("None"));
627 /* Print the first element */
628 indentprint((const char *) alpm_list_getdata(list
), len
);
631 for(i
= alpm_list_next(list
); i
; i
= alpm_list_next(i
)) {
633 for(j
= 1; j
<= len
; j
++) {
636 indentprint((const char *) alpm_list_getdata(i
), len
);
642 /* creates a header row for use with table_display */
643 static alpm_list_t
*create_verbose_header(int install
)
645 alpm_list_t
*res
= NULL
;
648 pm_asprintf(&str
, "%s", _("Name"));
649 res
= alpm_list_add(res
, str
);
650 pm_asprintf(&str
, "%s", _("Old Version"));
651 res
= alpm_list_add(res
, str
);
653 pm_asprintf(&str
, "%s", _("New Version"));
654 res
= alpm_list_add(res
, str
);
656 pm_asprintf(&str
, "%s", _("Size"));
657 res
= alpm_list_add(res
, str
);
662 /* returns package info as list of strings */
663 static alpm_list_t
*create_verbose_row(alpm_pkg_t
*pkg
, int install
)
668 alpm_list_t
*ret
= NULL
;
669 alpm_db_t
*ldb
= alpm_option_get_localdb(config
->handle
);
671 /* a row consists of the package name, */
672 pm_asprintf(&str
, "%s", alpm_pkg_get_name(pkg
));
673 ret
= alpm_list_add(ret
, str
);
675 /* old and new versions */
677 alpm_pkg_t
*oldpkg
= alpm_db_get_pkg(ldb
, alpm_pkg_get_name(pkg
));
678 pm_asprintf(&str
, "%s",
679 oldpkg
!= NULL
? alpm_pkg_get_version(oldpkg
) : "");
680 ret
= alpm_list_add(ret
, str
);
683 pm_asprintf(&str
, "%s", alpm_pkg_get_version(pkg
));
684 ret
= alpm_list_add(ret
, str
);
687 size
= humanize_size(alpm_pkg_get_size(pkg
), 'M', 1, &label
);
688 pm_asprintf(&str
, "%.2f %s", size
, label
);
689 ret
= alpm_list_add(ret
, str
);
694 /* prepare a list of pkgs to display */
695 void display_targets(const alpm_list_t
*pkgs
, int install
)
698 const char *title
, *label
;
700 const alpm_list_t
*i
;
701 off_t isize
= 0, rsize
= 0, dlsize
= 0;
702 alpm_list_t
*j
, *lp
, *header
= NULL
, *targets
= NULL
;
703 alpm_db_t
*db_local
= alpm_option_get_localdb(config
->handle
);
709 /* gather pkg infos */
710 for(i
= pkgs
; i
; i
= alpm_list_next(i
)) {
711 alpm_pkg_t
*pkg
= alpm_list_getdata(i
);
714 alpm_pkg_t
*lpkg
= alpm_db_get_pkg(db_local
, alpm_pkg_get_name(pkg
));
715 dlsize
+= alpm_pkg_download_size(pkg
);
717 /* add up size of all removed packages */
718 rsize
+= alpm_pkg_get_isize(lpkg
);
721 isize
+= alpm_pkg_get_isize(pkg
);
723 if(config
->verbosepkglists
) {
724 targets
= alpm_list_add(targets
, create_verbose_row(pkg
, install
));
726 pm_asprintf(&str
, "%s-%s", alpm_pkg_get_name(pkg
),
727 alpm_pkg_get_version(pkg
));
728 targets
= alpm_list_add(targets
, str
);
732 /* print to screen */
733 title
= install
? _("Targets (%d):") : _("Remove (%d):");
734 pm_asprintf(&str
, title
, alpm_list_count(pkgs
));
737 if(config
->verbosepkglists
) {
738 header
= create_verbose_header(install
);
739 if(table_display(str
, header
, targets
) != 0) {
740 config
->verbosepkglists
= 0;
741 display_targets(pkgs
, install
);
745 list_display(str
, targets
);
750 size
= humanize_size(dlsize
, 'M', 1, &label
);
751 printf(_("Total Download Size: %.2f %s\n"), size
, label
);
752 if(!(config
->flags
& PM_TRANS_FLAG_DOWNLOADONLY
)) {
753 size
= humanize_size(isize
, 'M', 1, &label
);
754 printf(_("Total Installed Size: %.2f %s\n"), size
, label
);
755 /* only show this net value if different from raw installed size */
757 size
= humanize_size(isize
- rsize
, 'M', 1, &label
);
758 printf(_("Net Upgrade Size: %.2f %s\n"), size
, label
);
762 size
= humanize_size(isize
, 'M', 1, &label
);
763 printf(_("Total Removed Size: %.2f %s\n"), size
, label
);
768 if(config
->verbosepkglists
) {
769 /* targets is a list of lists of strings, free inner lists here */
770 for(j
= alpm_list_first(targets
); j
; j
= alpm_list_next(j
)) {
771 lp
= alpm_list_getdata(j
);
774 alpm_list_free(targets
);
782 static off_t
pkg_get_size(alpm_pkg_t
*pkg
)
786 return alpm_pkg_download_size(pkg
);
788 return alpm_pkg_get_size(pkg
);
790 return alpm_pkg_get_isize(pkg
);
794 static char *pkg_get_location(alpm_pkg_t
*pkg
)
796 alpm_list_t
*servers
;
800 servers
= alpm_db_get_servers(alpm_pkg_get_db(pkg
));
802 pm_asprintf(&string
, "%s/%s", alpm_list_getdata(servers
),
803 alpm_pkg_get_filename(pkg
));
807 return strdup(alpm_pkg_get_filename(pkg
));
809 pm_asprintf(&string
, "%s-%s", alpm_pkg_get_name(pkg
), alpm_pkg_get_version(pkg
));
814 /** Converts sizes in bytes into human readable units.
816 * @param bytes the size in bytes
817 * @param target_unit '\0' or a short label. If equal to one of the short unit
818 * labels ('B', 'K', ...) bytes is converted to target_unit; if '\0', the first
819 * unit which will bring the value to below a threshold of 2048 will be chosen.
820 * @param long_labels whether to use short ("K") or long ("KiB") unit labels
821 * @param label will be set to the appropriate unit label
823 * @return the size in the appropriate unit
825 double humanize_size(off_t bytes
, const char target_unit
, int long_labels
,
828 static const char *shortlabels
[] = {"B", "K", "M", "G", "T", "P"};
829 static const char *longlabels
[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"};
830 static const int unitcount
= sizeof(shortlabels
) / sizeof(shortlabels
[0]);
832 const char **labels
= long_labels
? longlabels
: shortlabels
;
833 double val
= (double)bytes
;
836 for(index
= 0; index
< unitcount
- 1; index
++) {
837 if(target_unit
!= '\0' && shortlabels
[index
][0] == target_unit
) {
839 } else if(target_unit
== '\0' && val
<= 2048.0 && val
>= -2048.0) {
846 *label
= labels
[index
];
852 void print_packages(const alpm_list_t
*packages
)
854 const alpm_list_t
*i
;
855 if(!config
->print_format
) {
856 config
->print_format
= strdup("%l");
858 for(i
= packages
; i
; i
= alpm_list_next(i
)) {
859 alpm_pkg_t
*pkg
= alpm_list_getdata(i
);
860 char *string
= strdup(config
->print_format
);
863 if(strstr(temp
,"%n")) {
864 string
= strreplace(temp
, "%n", alpm_pkg_get_name(pkg
));
869 if(strstr(temp
,"%v")) {
870 string
= strreplace(temp
, "%v", alpm_pkg_get_version(pkg
));
875 if(strstr(temp
,"%l")) {
876 char *pkgloc
= pkg_get_location(pkg
);
877 string
= strreplace(temp
, "%l", pkgloc
);
883 if(strstr(temp
,"%r")) {
884 const char *repo
= "local";
885 alpm_db_t
*db
= alpm_pkg_get_db(pkg
);
887 repo
= alpm_db_get_name(db
);
889 string
= strreplace(temp
, "%r", repo
);
894 if(strstr(temp
,"%s")) {
896 pm_asprintf(&size
, "%jd", (intmax_t)pkg_get_size(pkg
));
897 string
= strreplace(temp
, "%s", size
);
901 printf("%s\n",string
);
906 /* Helper function for comparing strings using the
907 * alpm "compare func" signature */
908 int str_cmp(const void *s1
, const void *s2
)
910 return strcmp(s1
, s2
);
913 void display_new_optdepends(alpm_pkg_t
*oldpkg
, alpm_pkg_t
*newpkg
)
915 alpm_list_t
*old
= alpm_pkg_get_optdepends(oldpkg
);
916 alpm_list_t
*new = alpm_pkg_get_optdepends(newpkg
);
917 alpm_list_t
*optdeps
= alpm_list_diff(new,old
,str_cmp
);
919 printf(_("New optional dependencies for %s\n"), alpm_pkg_get_name(newpkg
));
920 list_display_linebreak(" ", optdeps
);
922 alpm_list_free(optdeps
);
925 void display_optdepends(alpm_pkg_t
*pkg
)
927 alpm_list_t
*optdeps
= alpm_pkg_get_optdepends(pkg
);
929 printf(_("Optional dependencies for %s\n"), alpm_pkg_get_name(pkg
));
930 list_display_linebreak(" ", optdeps
);
934 static void display_repo_list(const char *dbname
, alpm_list_t
*list
)
936 const char *prefix
= " ";
939 printf(_("Repository %s\n"), dbname
);
940 list_display(prefix
, list
);
943 void select_display(const alpm_list_t
*pkglist
)
945 const alpm_list_t
*i
;
947 alpm_list_t
*list
= NULL
;
949 const char *dbname
= NULL
;
951 for (i
= pkglist
; i
; i
= i
->next
) {
952 alpm_pkg_t
*pkg
= alpm_list_getdata(i
);
953 alpm_db_t
*db
= alpm_pkg_get_db(pkg
);
956 dbname
= alpm_db_get_name(db
);
957 if(strcmp(alpm_db_get_name(db
), dbname
) != 0) {
958 display_repo_list(dbname
, list
);
960 dbname
= alpm_db_get_name(db
);
963 pm_asprintf(&string
, "%d) %s", nth
, alpm_pkg_get_name(pkg
));
964 list
= alpm_list_add(list
, string
);
967 display_repo_list(dbname
, list
);
971 static int parseindex(char *s
, int *val
, int min
, int max
)
974 int n
= strtol(s
, &endptr
, 10);
975 if(*endptr
== '\0') {
976 if(n
< min
|| n
> max
) {
977 fprintf(stderr
, _("Invalid value: %d is not between %d and %d\n"),
984 fprintf(stderr
, _("Invalid number: %s\n"), s
);
989 static int multiselect_parse(char *array
, int count
, char *response
)
993 for (str
= response
; ; str
= NULL
) {
997 char *starts
= strtok_r(str
, " ", &saveptr
);
1003 int len
= strlen(starts
);
1007 if(*starts
== '^') {
1012 /* if first token is including, we unselect all targets */
1013 memset(array
, 0, count
);
1017 /* check for range */
1019 if((p
= strchr(starts
+ 1, '-'))) {
1025 if(parseindex(starts
, &start
, 1, count
) != 0)
1029 array
[start
-1] = include
;
1032 if(parseindex(ends
, &end
, start
, count
) != 0) {
1035 for(d
= start
; d
<= end
; d
++) {
1036 array
[d
-1] = include
;
1044 int multiselect_question(char *array
, int count
)
1049 if(config
->noconfirm
) {
1052 /* Use stderr so questions are always displayed when redirecting output */
1057 memset(array
, 1, count
);
1059 fprintf(stream
, "\n");
1060 fprintf(stream
, _("Enter a selection (default=all)"));
1061 fprintf(stream
, ": ");
1063 if(config
->noconfirm
) {
1064 fprintf(stream
, "\n");
1070 if(fgets(response
, sizeof(response
), stdin
)) {
1072 if(strlen(response
) > 0) {
1073 if(multiselect_parse(array
, count
, response
) == -1) {
1074 /* only loop if user gave an invalid answer */
1084 int select_question(int count
)
1090 if(config
->noconfirm
) {
1093 /* Use stderr so questions are always displayed when redirecting output */
1098 fprintf(stream
, "\n");
1099 fprintf(stream
, _("Enter a number (default=%d)"), preset
);
1100 fprintf(stream
, ": ");
1102 if(config
->noconfirm
) {
1103 fprintf(stream
, "\n");
1109 if(fgets(response
, sizeof(response
), stdin
)) {
1111 if(strlen(response
) > 0) {
1113 if(parseindex(response
, &n
, 1, count
) != 0)
1121 return (preset
- 1);
1125 /* presents a prompt and gets a Y/N answer */
1126 static int question(short preset
, char *fmt
, va_list args
)
1131 if(config
->noconfirm
) {
1134 /* Use stderr so questions are always displayed when redirecting output */
1138 /* ensure all text makes it to the screen before we prompt the user */
1142 vfprintf(stream
, fmt
, args
);
1145 fprintf(stream
, " %s ", _("[Y/n]"));
1147 fprintf(stream
, " %s ", _("[y/N]"));
1150 if(config
->noconfirm
) {
1151 fprintf(stream
, "\n");
1158 if(fgets(response
, sizeof(response
), stdin
)) {
1160 if(strlen(response
) == 0) {
1164 if(strcasecmp(response
, _("Y")) == 0 || strcasecmp(response
, _("YES")) == 0) {
1166 } else if(strcasecmp(response
, _("N")) == 0 || strcasecmp(response
, _("NO")) == 0) {
1173 int yesno(char *fmt
, ...)
1178 va_start(args
, fmt
);
1179 ret
= question(1, fmt
, args
);
1185 int noyes(char *fmt
, ...)
1190 va_start(args
, fmt
);
1191 ret
= question(0, fmt
, args
);
1197 int pm_printf(alpm_loglevel_t level
, const char *format
, ...)
1202 /* print the message using va_arg list */
1203 va_start(args
, format
);
1204 ret
= pm_vfprintf(stdout
, level
, format
, args
);
1210 int pm_fprintf(FILE *stream
, alpm_loglevel_t level
, const char *format
, ...)
1215 /* print the message using va_arg list */
1216 va_start(args
, format
);
1217 ret
= pm_vfprintf(stream
, level
, format
, args
);
1223 int pm_asprintf(char **string
, const char *format
, ...)
1228 /* print the message using va_arg list */
1229 va_start(args
, format
);
1230 if(vasprintf(string
, format
, args
) == -1) {
1231 pm_fprintf(stderr
, PM_LOG_ERROR
, _("failed to allocate string\n"));
1239 int pm_vasprintf(char **string
, alpm_loglevel_t level
, const char *format
, va_list args
)
1244 /* if current logmask does not overlap with level, do not print msg */
1245 if(!(config
->logmask
& level
)) {
1249 /* print the message using va_arg list */
1250 ret
= vasprintf(&msg
, format
, args
);
1252 /* print a prefix to the message */
1255 pm_asprintf(string
, _("error: %s"), msg
);
1257 case PM_LOG_WARNING
:
1258 pm_asprintf(string
, _("warning: %s"), msg
);
1261 pm_asprintf(string
, "debug: %s", msg
);
1263 case PM_LOG_FUNCTION
:
1264 pm_asprintf(string
, "function: %s", msg
);
1267 pm_asprintf(string
, "%s", msg
);
1275 int pm_vfprintf(FILE *stream
, alpm_loglevel_t level
, const char *format
, va_list args
)
1279 /* if current logmask does not overlap with level, do not print msg */
1280 if(!(config
->logmask
& level
)) {
1284 #if defined(PACMAN_DEBUG)
1285 /* If debug is on, we'll timestamp the output */
1286 if(config
->logmask
& PM_LOG_DEBUG
) {
1289 char timestr
[10] = {0};
1292 tmp
= localtime(&t
);
1293 strftime(timestr
, 9, "%H:%M:%S", tmp
);
1296 printf("[%s] ", timestr
);
1300 /* print a prefix to the message */
1303 fprintf(stream
, _("error: "));
1305 case PM_LOG_WARNING
:
1306 fprintf(stream
, _("warning: "));
1309 fprintf(stream
, "debug: ");
1311 case PM_LOG_FUNCTION
:
1312 fprintf(stream
, "function: ");
1318 /* print the message using va_arg list */
1319 ret
= vfprintf(stream
, format
, args
);
1323 #ifndef HAVE_STRNDUP
1324 /* A quick and dirty implementation derived from glibc */
1325 static size_t strnlen(const char *s
, size_t max
)
1327 register const char *p
;
1328 for(p
= s
; *p
&& max
--; ++p
);
1332 char *strndup(const char *s
, size_t n
)
1334 size_t len
= strnlen(s
, n
);
1335 char *new = (char *) malloc(len
+ 1);
1341 return (char *)memcpy(new, s
, len
);
1345 /* vim: set ts=2 sw=2 noet: */