Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Implemented `help' command.
jlaffaye committed 15 years ago
commit c74f99ce13264cc26b41c308793006444cb4c702
parent 078fe1a
7 files changed +97 -37
modified pkg/create.c
@@ -1,12 +1,22 @@
+
#include <sys/param.h>
+

#include <err.h>
#include <stdio.h>
#include <pkg.h>
#include <string.h>
-
#include <sys/param.h>
#include <unistd.h>
+
#include <sysexits.h>

#include "create.h"

+
void
+
usage_create(void)
+
{
+
	fprintf(stderr, "create [-gx] [-r rootdir] [-m manifest] [-f format] [-o outdir] "
+
			"<pkg-name>\n"
+
			"create -a [-r rootdir] [-m manifest] [-f format] [-o outdir]\n");
+
}
+

/*
 * options:
 * -x: regex
@@ -18,7 +28,7 @@
 */

int
-
cmd_create(int argc, char **argv)
+
exec_create(int argc, char **argv)
{
	struct pkgdb *db;
	struct pkg *pkg;
@@ -64,8 +74,8 @@ cmd_create(int argc, char **argv)
	argv += optind;

	if (match != MATCH_ALL && argc == 0) {
-
		warnx("No package provided");
-
		return (-1);
+
		usage_create();
+
		return (EX_USAGE);
	}

	if (rootdir == NULL)
modified pkg/create.h
@@ -1,5 +1,7 @@
#ifndef _CREATE_H
#define _CREATE_H

-
int cmd_create(int argc, char **argv);
+
int exec_create(int, char **);
+
void usage_create(void);
+

#endif
modified pkg/info.c
@@ -30,13 +30,20 @@ pkg_size(struct pkg *pkg)
	return (size);
}

+
void
+
usage_info(void)
+
{
+
	fprintf(stderr, "info [-egxXdrls] <pkg-name>\n"
+
			"info\n");
+
}
+

/*
 * list of options
 * -S <type> : show scripts, type can be pre-install etc: TODO
 */

int
-
cmd_info(int argc, char **argv)
+
exec_info(int argc, char **argv)
{
	struct pkgdb *db;
	struct pkg *pkg;
modified pkg/info.h
@@ -7,5 +7,7 @@
#define INFO_LIST_FILES (1<<3)
#define INFO_SIZE (1<<4)

-
int cmd_info(int argc, char **argv);
+
int exec_info(int, char **);
+
void usage_info(void);
+

#endif
modified pkg/main.c
@@ -1,37 +1,72 @@
+
#include <assert.h>
+
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
-
#include <err.h>

#include "create.h"
#include "info.h"
#include "which.h"

static void usage(void);
+
static void usage_help(void);
+
static int exec_help(int, char **);

static struct commands {
	const char *name;
-
	int (*exec_cmd)(int argc, char **argv);
+
	int (*exec)(int argc, char **argv);
+
	void (*usage)(void);
} cmd[] = { 
-
	{ "add", NULL },
-
	{ "create", cmd_create},
-
	{ "delete", NULL},
-
	{ "info", cmd_info},
-
	{ "install", NULL},
-
	{ "update", NULL},
-
	{ "which", cmd_which},
-
	{ "help", NULL},
-
	{ NULL, NULL },
+
	{ "add", NULL, NULL},
+
	{ "create", exec_create, usage_create},
+
	{ "delete", NULL, NULL},
+
	{ "info", exec_info, usage_info},
+
	{ "install", NULL, NULL},
+
	{ "update", NULL, NULL},
+
	{ "which", exec_which, usage_which},
+
	{ "help", exec_help, usage_help},
};
+
#define cmd_len (int)(sizeof(cmd)/sizeof(cmd[0]))

static void
usage()
{
-
	fprintf(stderr, "usage: ...");
+
	fprintf(stderr, "usage: pkg <command> [<args>]\n\n"
+
			"Where <command> can be:\n");
+
	for (int i = 0; i < cmd_len; i++) {
+
		fprintf(stderr, "  %s\n", cmd[i].name);
+
	}
	exit(EX_USAGE);
}

+
static void
+
usage_help()
+
{
+
	fprintf(stderr, "help <command>\n");
+
}
+

+
static int
+
exec_help(int argc, char **argv)
+
{
+
	if (argc != 2) {
+
		usage_help();
+
		return(EX_USAGE);
+
	}
+

+
	for (int i = 0; i < cmd_len; i++) {
+
		if (strcmp(cmd[i].name, argv[1]) == 0) {
+
			assert(cmd[i].usage != NULL);
+
			cmd[i].usage();
+
			return (0);
+
		}
+
	}
+

+
	// Command name not found
+
	warnx("%s is not a valid command", argv[1]);
+
	return (1);
+
}
+

int
main(int argc, char **argv)
{
@@ -44,7 +79,7 @@ main(int argc, char **argv)
		usage();

	len = strlen(argv[1]);
-
	for (i = 0; cmd[i].name != NULL; i++) {
+
	for (i = 0; i < cmd_len; i++) {
		if (strncmp(argv[1], cmd[i].name, len) == 0) {
			/* if we have the exact cmd */
			if (len == strlen(cmd[i].name)) {
@@ -72,15 +107,13 @@ main(int argc, char **argv)
	if (ambiguous == 0) {
		argc--;
		argv++;
-
		if (command->exec_cmd != NULL)
-
			return (command->exec_cmd(argc, argv));
-
		else
-
			printf("%s: No yet implemented\n", command->name);
+
		assert(command->exec != NULL);
+
		return (command->exec(argc, argv));
	}

	if (ambiguous == 1) {
		warnx("Ambiguous command: %s, could be:", argv[1]);
-
		for (i = 0; cmd[i].name != NULL; i++)
+
		for (i = 0; i < cmd_len; i++)
			if (strncmp(argv[1], cmd[i].name, len) == 0)
				warnx("\t%s",cmd[i].name);
	}
modified pkg/which.c
@@ -1,3 +1,5 @@
+
#include <sys/param.h>
+

#include <err.h>
#include <stdio.h>
#include <pkg.h>
@@ -5,12 +7,18 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-
#include <sys/param.h>
+
#include <sysexits.h>

#include "which.h"

+
void
+
usage_which(void)
+
{
+
	fprintf(stderr, "which <file>\n");
+
}
+

int
-
cmd_which(int argc, char **argv)
+
exec_which(int argc, char **argv)
{
	struct pkgdb *db;
	struct pkg *pkg;
@@ -18,21 +26,18 @@ cmd_which(int argc, char **argv)
	char pathabsdir[MAXPATHLEN];
	int retcode = 1;

-
	if (argc < 2 || argc > 4) {
-
		warnx("No file given");
-
		return (-1);
+
	if (argc != 2) {
+
		usage_which();
+
		return (EX_USAGE);
	}

-
	argc--;
-
	argv++;
-

	if (pkgdb_open(&db) == -1) {
		pkgdb_warn(db);
		return (-1);
	}

-
	realpath(dirname(argv[0]), pathabsdir);
-
	snprintf(pathabs, sizeof(pathabs), "%s/%s", pathabsdir, basename(argv[0]));
+
	realpath(dirname(argv[1]), pathabsdir);
+
	snprintf(pathabs, sizeof(pathabs), "%s/%s", pathabsdir, basename(argv[1]));

	pkg_new(&pkg);
	if (pkgdb_query_which(db, pathabs, pkg) == 0) {
modified pkg/which.h
@@ -1,6 +1,7 @@
-

#ifndef _WHICH_H
#define _WHICH_H

-
int cmd_which(int argc, char **argv);
+
int exec_which(int, char **);
+
void usage_which(void);
+

#endif