/*-1* Copyright (c) 2015 Lars Engels <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer9* in this position and unchanged.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT19* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/2526#include <sys/param.h>2728#include <err.h>29#include <stdio.h>30#include <string.h>31#include <unistd.h>32#include <getopt.h>3334#include <pkg.h>3536#include "pkgcli.h"3738void39usage_alias(void)40{41fprintf(stderr, "Usage: pkg alias [-ql] [alias]\n\n");42fprintf(stderr, "For more information see 'pkg help alias'.\n");43}4445int46exec_alias(int argc, char **argv)47{48const pkg_object *all_aliases;49const pkg_object *alias;50pkg_iter it = NULL;51int ch;52int ret = EXIT_SUCCESS;53bool list = false;5455struct option longopts[] = {56{ "quiet", no_argument, NULL, 'q' },57{ "list", no_argument, NULL, 'l' },58{ NULL, 0, NULL, 0 },59};6061while ((ch = getopt_long(argc, argv, "+ql", longopts, NULL)) != -1) {62switch (ch) {63case 'q':64quiet = true;65break;66case 'l':67list = true;68break;69default:70usage_alias();71return (EXIT_FAILURE);72}73}7475argc -= optind;76argv += optind;7778all_aliases = pkg_config_get("ALIAS");7980if (argc == 0) {81if (!quiet && list)82printf("%s\n", "ALIAS");83else if (!quiet)84printf("%-20s %s\n", "ALIAS", "ARGUMENTS");85while ((alias = pkg_object_iterate(all_aliases, &it))) {86if (list)87printf("%s\n", pkg_object_key(alias));88else89printf("%-20s '%s'\n", pkg_object_key(alias), pkg_object_string(alias));90}91return (ret);92}9394for (int i = 0; i < argc; i++) {95it = NULL;96while ((alias = pkg_object_iterate(all_aliases, &it))) {97if (STREQ(argv[i], pkg_object_key(alias)))98break;99}100if (alias) {101if (list)102printf("%s\n", argv[i]);103else104printf("%-20s '%s'\n", argv[i], pkg_object_string(alias));105} else {106warnx("No such alias: '%s'", argv[i]);107ret = EXIT_FAILURE;108}109}110111return (ret);112}113114115116