Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Merge pull request #766 from skinder/master
Vsevolod Stakhov committed 12 years ago
commit 3a95d7453e33a89cd9cc0f9c76da98fa855704ed
parent 42bed57
15 files changed +142 -22
modified libpkg/pkg.h.in
@@ -1339,6 +1339,8 @@ typedef enum {
	PKG_EVENT_NOTICE,
	PKG_EVENT_DEBUG,
	PKG_EVENT_INCREMENTAL_UPDATE,
+
	PKG_EVENT_QUERY_YESNO,
+
	PKG_EVENT_QUERY_SELECT,
	/* errors */
	PKG_EVENT_ERROR,
	PKG_EVENT_ERRNO,
@@ -1458,6 +1460,16 @@ struct pkg_event {
			int level;
			char *msg;
		} e_debug;
+
		struct {
+
			const char *msg;
+
			int deft;
+
		} e_query_yesno;
+
		struct {
+
			const char *msg;
+
			const char **items;
+
			int ncnt;
+
			int deft;
+
		} e_query_select;
	};
};

modified libpkg/pkg_event.c
@@ -55,6 +55,7 @@ sbuf_json_escape(struct sbuf *buf, const char *str)
static void
pipeevent(struct pkg_event *ev)
{
+
	int i;
	struct pkg_dep *dep = NULL;
	struct sbuf *msg, *buf;
	const char *message;
@@ -320,6 +321,32 @@ pipeevent(struct pkg_event *ev)
			ev->e_incremental_update.added,
			ev->e_incremental_update.processed);
		break;
+
	case PKG_EVENT_QUERY_YESNO:
+
		sbuf_printf(msg, "{\"type\": \"QUERY_YESNO\", "
+
		    "\"data\": {"
+
			"\"msg\": \"%s\","
+
			"\"default\": \"%d\""
+
			"}}", ev->e_query_yesno.msg,
+
			ev->e_query_yesno.deft);
+
		break;
+
	case PKG_EVENT_QUERY_SELECT:
+
		sbuf_printf(msg, "{\"type\": \"QUERY_SELECT\", "
+
		    "\"data\": {"
+
			"\"msg\": \"%s\","
+
			"\"ncnt\": \"%d\","
+
			"\"default\": \"%d\","
+
			"\"items\": ["
+
			, ev->e_query_select.msg,
+
			ev->e_query_select.ncnt,
+
			ev->e_query_select.deft);
+
		for (i = 0; i < ev->e_query_select.ncnt - 1; i++)
+
		{
+
			sbuf_printf(msg, "{ \"text\": \"%s\" },",
+
				ev->e_query_select.items[i]);
+
		}
+
		sbuf_printf(msg, "{ \"text\": \"%s\" } ] }}",
+
			ev->e_query_select.items[i]);
+
		break;
	default:
		break;
	}
@@ -336,13 +363,15 @@ pkg_event_register(pkg_event_cb cb, void *data)
	_data = data;
}

-
static void
+
static int
pkg_emit_event(struct pkg_event *ev)
{
+
	int ret = 0;
	pkg_plugins_hook_run(PKG_PLUGIN_HOOK_EVENT, ev, NULL);
	if (_cb != NULL)
-
		_cb(_data, ev);
+
		ret = _cb(_data, ev);
	pipeevent(ev);
+
	return (ret);
}

void
@@ -719,6 +748,36 @@ pkg_emit_incremental_update(int updated, int removed, int added, int processed)
	pkg_emit_event(&ev);
}

+
bool
+
pkg_emit_query_yesno(bool deft, const char *msg)
+
{
+
	struct pkg_event ev;
+
	int ret;
+

+
	ev.type = PKG_EVENT_QUERY_YESNO;
+
	ev.e_query_yesno.msg = msg;
+
	ev.e_query_yesno.deft = deft;
+

+
	ret = pkg_emit_event(&ev);
+
	return (ret ? true : false);
+
}
+

+
int
+
pkg_emit_query_select(const char *msg, const char **items, int ncnt, int deft)
+
{
+
	struct pkg_event ev;
+
	int ret;
+

+
	ev.type = PKG_EVENT_QUERY_SELECT;
+
	ev.e_query_select.msg = msg;
+
	ev.e_query_select.items = items;
+
	ev.e_query_select.ncnt = ncnt;
+
	ev.e_query_select.deft = deft;
+

+
	ret = pkg_emit_event(&ev);
+
	return ret;
+
}
+

void
pkg_debug(int level, const char *fmt, ...)
{
modified src/annotate.c
@@ -68,7 +68,7 @@ do_add(struct pkgdb *db, struct pkg *pkg, const char *tag, const char *value)
	int		 ret = EPKG_OK;


-
	if (yes || query_tty_yesno("%n-%v: Add annotation tagged: %S with "
+
	if (yes || query_tty_yesno(false, "%n-%v: Add annotation tagged: %S with "
	               "value: %S? [y/N]: ", pkg, pkg, tag, value)) {

		ret = pkgdb_add_annotation(db, pkg, tag, value);
@@ -103,7 +103,7 @@ do_modify(struct pkgdb *db, struct pkg *pkg, const char *tag, const char *value)
	int		 ret = EPKG_OK;


-
	if (yes || query_tty_yesno("%n-%v: Change annotation tagged: %S to "
+
	if (yes || query_tty_yesno(false, "%n-%v: Change annotation tagged: %S to "
		         "new value: %S? [y/N]: ", pkg, pkg, tag, value)) {
		ret = pkgdb_modify_annotation(db, pkg, tag, value);
		if (ret == EPKG_OK || ret == EPKG_WARN) {
@@ -127,7 +127,7 @@ do_delete(struct pkgdb *db, struct pkg *pkg, const char *tag)
	const char	*pkgname, *pkgversion;
	int		 ret = EPKG_OK;

-
	if (yes || query_tty_yesno("%n-%v: Delete annotation tagged: %S "
+
	if (yes || query_tty_yesno(false, "%n-%v: Delete annotation tagged: %S "
			 "[y/N]: ", pkg, pkg, tag)) {
		ret = pkgdb_delete_annotation(db, pkg, tag);
		if (ret == EPKG_OK) {
modified src/autoremove.c
@@ -128,7 +128,7 @@ exec_autoremove(__unused int argc, __unused char **argv)
		print_jobs_summary(jobs,
		    "Deinstallation has been requested for the following %d packages:\n\n", nbactions);
		if (!yes && !dry_run)
-
			yes = query_yesno(
+
			yes = query_yesno(false,
		            "\nProceed with deinstalling packages [y/N]: ");
		if (dry_run)
			yes = false;
modified src/check.c
@@ -177,7 +177,7 @@ fix_deps(struct pkgdb *db, struct deps_head *dh, int nbpkgs, bool yes)
	print_jobs_summary(jobs, "The following packages will be installed:\n\n");
	
	if (!yes)
-
		yes = query_yesno("\n>>> Try to fix the missing dependencies [y/N]: ");
+
		yes = query_yesno(false, "\n>>> Try to fix the missing dependencies [y/N]: ");

	if (yes) {
		if (pkgdb_access(PKGDB_MODE_WRITE, PKGDB_DB_LOCAL) ==
modified src/clean.c
@@ -287,7 +287,7 @@ exec_clean(int argc, char **argv)
	printf("The cleanup will free %s\n", size);
	if (!dry_run) {
		if (!yes)
-
			yes = query_yesno(
+
			yes = query_yesno(false,
				"\nProceed with cleaning the cache [y/N]: ");
		if (yes)
			retcode = delete_dellist(&dl);
modified src/delete.c
@@ -192,7 +192,7 @@ exec_delete(int argc, char **argv)
			goto cleanup;
		}
		if (!yes && !dry_run)
-
			yes = query_yesno(
+
			yes = query_yesno(false,
		            "\nProceed with deinstalling packages [y/N]: ");
	}
	if (!yes || (retcode = pkg_jobs_apply(jobs)) != EPKG_OK)
modified src/event.c
@@ -331,6 +331,15 @@ event_callback(void *data, struct pkg_event *ev)
	case PKG_EVENT_DEBUG:
		fprintf(stderr, "DBG(%d)> %s\n", ev->e_debug.level, ev->e_debug.msg);
		break;
+
	case PKG_EVENT_QUERY_YESNO:
+
		return ( ev->e_query_yesno.deft ?
+
			query_yesno(true, ev->e_query_yesno.msg, "[Y/n]") :
+
			query_yesno(false, ev->e_query_yesno.msg, "[y/N]") );
+
		break;
+
	case PKG_EVENT_QUERY_SELECT:
+
		return query_select(ev->e_query_select.msg, ev->e_query_select.items,
+
			ev->e_query_select.ncnt, ev->e_query_select.deft);
+
		break;
	default:
		break;
	}
modified src/fetch.c
@@ -180,7 +180,7 @@ exec_fetch(int argc, char **argv)
		print_jobs_summary(jobs, "The following packages will be fetched:\n\n");

		if (!yes)
-
			yes = query_yesno("\nProceed with fetching packages [y/N]: ");
+
			yes = query_yesno(false, "\nProceed with fetching packages [y/N]: ");
	}
	
	if (!yes || pkg_jobs_apply(jobs) != EPKG_OK)
modified src/install.c
@@ -209,7 +209,7 @@ exec_install(int argc, char **argv)
			    nbactions, pkg_jobs_total(jobs));

			if (!yes && !dry_run)
-
				yes = query_yesno(
+
				yes = query_yesno(false, 
				    "\nProceed with this action [y/N]: ");
			if (dry_run)
				yes = false;
modified src/lock.c
@@ -64,7 +64,7 @@ do_lock(struct pkgdb *db, struct pkg *pkg)
		return (EPKG_OK);
	}

-
	if (!yes && !query_yesno("%n-%v: lock this package? [y/N]: ",
+
	if (!yes && !query_yesno(false, "%n-%v: lock this package? [y/N]: ",
				 pkg, pkg))
		return (EPKG_OK);

@@ -84,7 +84,7 @@ do_unlock(struct pkgdb *db, struct pkg *pkg)
		return (EPKG_OK);
	}

-
	if (!yes && !query_yesno("%n-%v: unlock this package? [y/N]: ",
+
	if (!yes && !query_yesno(false, "%n-%v: unlock this package? [y/N]: ",
				 pkg, pkg))
		return (EPKG_OK);

modified src/pkgcli.h
@@ -240,7 +240,8 @@ void usage_config(void);
			 INFO_DESCR|INFO_MESSAGE|INFO_DEPS|INFO_RDEPS| \
			 INFO_FILES|INFO_DIRS)

-
bool query_yesno(const char *msg, ...);
+
bool query_yesno(bool deft, const char *msg, ...);
+
int query_select(const char *msg, const char **opts, int ncnt, int deft);
bool query_tty_yesno(const char *msg, ...);
int info_flags(uint64_t opt, bool remote);
void print_info(struct pkg * const pkg, uint64_t opt);
modified src/set.c
@@ -173,10 +173,10 @@ exec_set(int argc, char **argv)

		if (!yes) {
			if (pkg != NULL)
-
				yes = query_yesno("Change origin from %S to %S for %n-%v? [y/N]: ",
+
				yes = query_yesno(false, "Change origin from %S to %S for %n-%v? [y/N]: ",
				    oldorigin, neworigin, pkg, pkg);
			else
-
				yes = query_yesno("Change origin from %S to %S for all dependencies? "
+
				yes = query_yesno(false, "Change origin from %S to %S for all dependencies? "
				    "[y/N]: ", oldorigin, neworigin);
		}
		if (pkg != NULL && yes) {
@@ -203,9 +203,9 @@ exec_set(int argc, char **argv)
					continue;
				if (!yes) {
					if (newautomatic)
-
						yes = query_yesno("Mark %n-%v as automatically installed? [y/N]: ", pkg, pkg);
+
						yes = query_yesno(false, "Mark %n-%v as automatically installed? [y/N]: ", pkg, pkg);
					else
-
						yes = query_yesno("Mark %n-%v as not automatically installed? [y/N]: ", pkg, pkg);
+
						yes = query_yesno(false, "Mark %n-%v as not automatically installed? [y/N]: ", pkg, pkg);
				}
				if (yes)
					pkgdb_set(db, pkg, PKG_SET_AUTOMATIC, newautomatic);
modified src/upgrade.c
@@ -161,7 +161,7 @@ exec_upgrade(int argc, char **argv)
				nbactions, pkg_jobs_total(jobs));

			if (!yes && !dry_run)
-
				yes = query_yesno("\nProceed with this action [y/N]: ");
+
				yes = query_yesno(false, "\nProceed with this action [y/N]: ");
			if (dry_run)
				yes = false;
		}
modified src/utils.c
@@ -39,6 +39,8 @@
#include <unistd.h>
#include <stdarg.h>
#include <paths.h>
+
#define _WITH_GETLINE
+
#include <stdio.h>
#include <pkg.h>

#include "pkgcli.h"
@@ -85,10 +87,10 @@ cleanup:
}

bool
-
query_yesno(const char *msg, ...)
+
query_yesno(bool deft, const char *msg, ...)
{
	int	 c;
-
	bool	 r = false;
+
	bool	 r = deft;
	va_list	 ap;

	va_start(ap, msg);
@@ -98,8 +100,10 @@ query_yesno(const char *msg, ...)
	c = getchar();
	if (c == 'y' || c == 'Y')
		r = true;
+
	else if (c == 'n' || c == 'N')
+
		r = false;
	else if (c == '\n' || c == EOF)
-
		return (false);
+
		return r;

	while ((c = getchar()) != '\n' && c != EOF)
		continue;
@@ -107,6 +111,41 @@ query_yesno(const char *msg, ...)
	return (r);
}

+
int
+
query_select(const char *msg, const char **opts, int ncnt, int deft)
+
{
+
	int i;
+
	char *str = NULL;
+
	char *endpntr = NULL;
+
	size_t n = 0;
+

+
	printf("%s\n", msg);
+
	for (i = 0; i < ncnt; i++) {
+
		if (i + 1 == deft)
+
		{
+
			printf("*[%d] %s\n",
+
				i + 1, opts[i]);
+
		} else {
+
			printf(" [%d] %s\n",
+
				i + 1, opts[i]);
+
		}
+
	}
+

+
	getline(&str, &n, stdin);
+
	i = (int) strtoul(str, &endpntr, 10);
+

+
	if (endpntr == NULL || *endpntr == '\0') {
+
		i = deft;
+
	} else if (*endpntr == '\n' || *endpntr == '\r') {
+
		if (i > ncnt || i < 1)
+
			i = deft;
+
	} else
+
		i = -1;
+

+
	free(str);
+
	return i;
+
}
+

/* unlike realpath(3), this routine does not expand symbolic links */
char *
absolutepath(const char *src, char *dest, size_t dest_size) {