/ src / alias.c
alias.c
  1  /*-
  2   * Copyright (c) 2015 Lars Engels <lme@FreeBSD.org>
  3   * All rights reserved.
  4   * 
  5   * Redistribution and use in source and binary forms, with or without
  6   * modification, are permitted provided that the following conditions
  7   * are met:
  8   * 1. Redistributions of source code must retain the above copyright
  9   *    notice, this list of conditions and the following disclaimer
 10   *    in this position and unchanged.
 11   * 2. Redistributions in binary form must reproduce the above copyright
 12   *    notice, this list of conditions and the following disclaimer in the
 13   *    documentation and/or other materials provided with the distribution.
 14   * 
 15   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 16   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 17   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 18   * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 19   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 20   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 21   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 22   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 23   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 24   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 25   */
 26  
 27  #include <sys/param.h>
 28  
 29  #include <err.h>
 30  #include <stdio.h>
 31  #include <string.h>
 32  #include <unistd.h>
 33  #include <getopt.h>
 34  
 35  #include <pkg.h>
 36  
 37  #include "pkgcli.h"
 38  
 39  void
 40  usage_alias(void)
 41  {
 42  	fprintf(stderr, "Usage: pkg alias [-ql] [alias]\n\n");
 43  	fprintf(stderr, "For more information see 'pkg help alias'.\n");
 44  }
 45  
 46  int
 47  exec_alias(int argc, char **argv)
 48  {
 49  	const pkg_object *all_aliases;
 50  	const pkg_object *alias;
 51  	pkg_iter it = NULL;
 52  	int ch;
 53  	int ret = EXIT_SUCCESS;
 54  	bool list = false;
 55  
 56  	struct option longopts[] = {
 57  		{ "quiet",	no_argument,	NULL, 'q' },
 58  		{ "list",	no_argument,	NULL, 'l' },
 59  		{ NULL,		0,		NULL, 0 },
 60  	};
 61  
 62  	while ((ch = getopt_long(argc, argv, "+ql", longopts, NULL)) != -1) {
 63  		switch (ch) {
 64  		case 'q':
 65  			quiet = true;
 66  			break;
 67  		case 'l':
 68  			list = true;
 69  			break;
 70  		default:
 71  			usage_alias();
 72  			return (EXIT_FAILURE);
 73  		}
 74  	}
 75  
 76  	argc -= optind;
 77  	argv += optind;
 78  
 79  	all_aliases = pkg_config_get("ALIAS");
 80  
 81  	if (argc == 0) {
 82  		if (!quiet && list)
 83  			printf("%s\n", "ALIAS");
 84  		else if (!quiet)
 85  			printf("%-20s %s\n", "ALIAS", "ARGUMENTS");
 86  		while ((alias = pkg_object_iterate(all_aliases, &it))) {
 87  			if (list)
 88  				printf("%s\n", pkg_object_key(alias));
 89  			else
 90  				printf("%-20s '%s'\n", pkg_object_key(alias), pkg_object_string(alias));
 91  		}
 92  		return (ret);
 93  	}
 94  
 95  	for (int i = 0; i < argc; i++) {
 96  		it = NULL;
 97  		while ((alias = pkg_object_iterate(all_aliases, &it))) {
 98  			if (STREQ(argv[i], pkg_object_key(alias)))
 99  				break;
100  		}
101  		if (alias) {
102  			if (list)
103  				printf("%s\n", argv[i]);
104  			else
105  				printf("%-20s '%s'\n", argv[i], pkg_object_string(alias));
106  		} else {
107  			warnx("No such alias: '%s'", argv[i]);
108  			ret = EXIT_FAILURE;
109  		}
110  	}
111  
112  	return (ret);
113  }
114