Skip to content

Commit

Permalink
Clean up usage handling and output.
Browse files Browse the repository at this point in the history
Remove unnecessary and inconsistent logic, exit success for "-h", and
ensure all output goes to either stdout or stderr depending on status.

Improve the usage output, make it fit into 80 columns, and sort commands
into a logical order.

Simply hardcode "pkgin" rather than using setprogname().
  • Loading branch information
Jonathan Perkin committed Apr 25, 2018
1 parent 95469df commit b0a2b2d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 72 deletions.
2 changes: 1 addition & 1 deletion autoremove.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pkgin_autoremove()
* KEEP_LOCAL_PKGS returns full packages names
*/
if ((plisthead = rec_pkglist(KEEP_LOCAL_PKGS)) == NULL)
errx(EXIT_FAILURE, MSG_NO_PKGIN_PKGS, getprogname());
errx(EXIT_FAILURE, "no packages have been marked as keepable");

keephead = init_head();

Expand Down
80 changes: 40 additions & 40 deletions cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,63 +36,63 @@ static struct command {
const char *descr;
const int cmdtype;
} cmd[] = {
{ "list", "ls", "List installed packages.",
{ "list", "ls", "List installed local packages",
PKG_LLIST_CMD },
{ "avail", "av", "List available packages.",
{ "avail", "av", "List all available remote packages",
PKG_RLIST_CMD },
{ "install", "in", "Perform packages installation or upgrade.",
{ "search", "se", "Search for a remote package",
PKG_SRCH_CMD },
{ "install", "in", "Install or upgrade packages",
PKG_INST_CMD },
{ "update", "up" , "Create and populate the initial database.",
{ "update", "up" , "Refresh local and remote package lists",
PKG_UPDT_CMD },
{ "remove", "rm", "Remove packages and depending packages.",
PKG_REMV_CMD },
{ "upgrade", "ug", "Upgrade main packages to their newer versions.",
{ "upgrade", "ug", "Upgrade only packages marked with the keep flag",
PKG_UPGRD_CMD },
{ "full-upgrade", "fug", "Upgrade all packages to their newer versions.",
{ "full-upgrade", "fug", "Upgrade all packages",
PKG_FUPGRD_CMD },
{ "show-deps", "sd", "Display direct dependencies.",
PKG_SHDDP_CMD },
{ "show-full-deps", "sfd", "Display dependencies recursively.",
PKG_SHFDP_CMD },
{ "show-rev-deps", "srd", "Display reverse dependencies recursively.",
PKG_SHRDP_CMD },
{ "show-category", "sc", "Show packages belonging to category.",
PKG_SHCAT_CMD },
{ "show-pkg-category", "spc", "Show package's category.",
PKG_SHPCAT_CMD },
{ "show-all-categories", "sac", "Show all categories.",
PKG_SHALLCAT_CMD },
{ "keep", "ke", "Mark package as \"non auto-removable\".",
{ "remove", "rm", "Remove packages and any dependent packages",
PKG_REMV_CMD },
{ "keep", "ke", "Mark packages that should be kept",
PKG_KEEP_CMD },
{ "unkeep", "uk", "Mark package as \"auto-removable\".",
{ "unkeep", "uk", "Mark packages that can be autoremoved",
PKG_UNKEEP_CMD },
{ "show-keep", "sk", "Display \"non auto-removable\" packages.",
{ "export", "ex", "Display PKGPATH for all keep packages",
PKG_EXPORT_CMD },
{ "import", "im", "Import keep package list from file",
PKG_IMPORT_CMD },
{ "show-keep", "sk", "Display keep packages",
PKG_SHKP_CMD },
{ "show-no-keep", "snk", "Display \"auto-removable\" packages.",
{ "show-no-keep", "snk", "Display autoremovable packages",
PKG_SHNOKP_CMD },
{ "search", "se", "Search for a package.",
PKG_SRCH_CMD },
{ "clean", "cl", "Clean packages cache.",
PKG_CLEAN_CMD },
{ "autoremove", "ar", "Autoremove orphan dependencies.",
{ "autoremove", "ar", "Remove orphaned dependencies",
PKG_AUTORM_CMD },
{ "export", "ex", "Export \"non auto-removable\" packages to stdout.",
PKG_EXPORT_CMD },
{ "import", "im", "Import \"non auto-removable\" package list from file.",
PKG_IMPORT_CMD },
{ "provides", "prov", "Show what files a package provides.",
{ "clean", "cl", "Remove downloaded package files",
PKG_CLEAN_CMD },
{ "show-deps", "sd", "List remote package direct dependencies",
PKG_SHDDP_CMD },
{ "show-full-deps", "sfd", "List remote package full dependencies",
PKG_SHFDP_CMD },
{ "show-rev-deps", "srd", "List local package reverse dependencies",
PKG_SHRDP_CMD },
{ "provides", "prov", "Show which shared libraries a package provides",
PKG_SHPROV_CMD },
{ "requires", "req", "Show what files a package requires.",
{ "requires", "req", "Show which shared libraries a package requires",
PKG_SHREQ_CMD },
{ "pkg-content", "pc", "Show remote package's content.",
{ "show-category", "sc", "List all packages belonging to a category",
PKG_SHCAT_CMD },
{ "show-pkg-category", "spc", "Show categories a package belongs to",
PKG_SHPCAT_CMD },
{ "show-all-categories", "sac", "List all known categories",
PKG_SHALLCAT_CMD },
{ "pkg-content", "pc", "Show remote package content",
PKG_SHPKGCONT_CMD },
{ "pkg-descr", "pd", "Show remote package's long-description.",
{ "pkg-descr", "pd", "Show remote package long-description",
PKG_SHPKGDESC_CMD },
{ "pkg-build-defs", "pbd", "Show remote package's build definitions.",
{ "pkg-build-defs", "pbd", "Show remote package build definitions",
PKG_SHPKGBDEFS_CMD },
{ "tonic", "to", "Gin Tonic recipe.",
{ "tonic", "to", "Gin Tonic recipe",
PKG_GINTO_CMD },
{ "stats", "st", "Packages statistics.",
{ "stats", "st", "Show local and remote package statistics",
PKG_STATS_CMD },
{ NULL, NULL, NULL, 0 }
};
45 changes: 19 additions & 26 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "cmd.h"
#include "external/lib.h"

static void usage(void);
static void usage(int);
static int find_cmd(const char *);
static void missing_param(int, int, const char *);
static void ginto(void);
Expand All @@ -52,10 +52,7 @@ main(int argc, char *argv[])
int ch, i, rc = EXIT_SUCCESS;
const char *chrootpath = NULL;

setprogname(argv[0]);

if (argc < 2 || *argv[1] == 'h')
usage();
setprogname("pkgin");

/* Default to not doing \r printouts if we don't send to a tty */
parsable = !isatty(fileno(stdout));
Expand All @@ -77,12 +74,12 @@ main(int argc, char *argv[])
noflag = 1;
break;
case 'v':
printf("%s %s (using %s)\n",
getprogname(), PKGIN_VERSION, pdb_version());
printf("pkgin %s (using %s)\n",
PKGIN_VERSION, pdb_version());
exit(EXIT_SUCCESS);
/* NOTREACHED */
case 'h':
usage();
usage(EXIT_SUCCESS);
/* NOTREACHED */
case 'l':
lslimit = optarg[0];
Expand All @@ -108,22 +105,16 @@ main(int argc, char *argv[])
pflag = 1;
break;
default:
usage();
usage(EXIT_FAILURE);
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;

if (argc < 1) {
fprintf(stderr, "%s\n", MSG_MISSING_CMD);
usage();
/* NOTREACHED */
}

/* check we were given a valid command */
if ((ch = find_cmd(argv[0])) == -1) {
usage();
if (argc < 1 || (ch = find_cmd(argv[0])) == -1) {
usage(EXIT_FAILURE);
/* NOTREACHED */
}

Expand Down Expand Up @@ -284,7 +275,7 @@ main(int argc, char *argv[])
pkgindb_stats();
break;
default:
usage();
usage(EXIT_FAILURE);
/* NOTREACHED */
}

Expand Down Expand Up @@ -326,20 +317,22 @@ find_cmd(const char *arg)
}

static void
usage()
usage(int status)
{
int i;

(void)fprintf(stderr, MSG_USAGE, getprogname());
fprintf((status) ? stderr : stdout,
"Usage: pkgin [-cdfFhlnPtvVy] command [package ...]\n\n"
"Commands and shortcuts:\n");

printf(MSG_CMDS_SHORTCUTS);

for (i = 0; cmd[i].name != NULL; i++)
for (i = 0; cmd[i].name != NULL; i++) {
if (cmd[i].cmdtype != PKG_GINTO_CMD)
printf("%-19s (%-4s) - %s\n",
cmd[i].name, cmd[i].shortcut, cmd[i].descr);
fprintf((status) ? stderr : stdout,
"%-19s (%-4s) - %s\n",
cmd[i].name, cmd[i].shortcut, cmd[i].descr);
}

exit(EXIT_FAILURE);
exit(status);
}

static void
Expand Down
5 changes: 0 additions & 5 deletions messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
*/

/* main.c */
#define MSG_MISSING_CMD "missing command"
#define MSG_MISSING_PKGNAME "missing package name"
#define MSG_MISSING_FILENAME "missing file name"
#define MSG_FULLDEPTREE "full dependency tree for %s\n"
Expand All @@ -43,9 +42,6 @@
#define MSG_MISSING_SRCH "missing search string"
#define MSG_MISSING_CATEGORY "missing category"

#define MSG_USAGE "Usage: %s [-cdfFhlnPtvVy] command [package ...]\n"
#define MSG_CMDS_SHORTCUTS "\nCommands and shortcuts:\n"

#define MSG_CHROOT_FAILED "Unable to chroot"
#define MSG_CHDIR_FAILED "Unable to chroot"

Expand Down Expand Up @@ -111,7 +107,6 @@
/* autoremove.c */
#define MSG_AUTOREMOVE_WARNING "\
in order to remove packages from the autoremove list, flag those with the `keep' action.\n"
#define MSG_NO_PKGIN_PKGS "no packages have been installed with %s"
#define MSG_ALL_KEEP_PKGS "all packages are marked as \"keepable\"\n."
#define MSG_AUTOREMOVE_PKGS "%d packages to be autoremoved: %s\n"
#define MSG_MARK_PKG_KEEP "%s is marked as non-autoremovable\n"
Expand Down

0 comments on commit b0a2b2d

Please sign in to comment.