Line data Source code
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 : #include <sys/sbuf.h>
29 :
30 : #include <err.h>
31 : #include <errno.h>
32 : #include <libgen.h>
33 : #include <stdio.h>
34 : #include <string.h>
35 : #include <sysexits.h>
36 : #include <unistd.h>
37 : #include <getopt.h>
38 :
39 : #include <pkg.h>
40 :
41 : #include "pkgcli.h"
42 :
43 : void
44 0 : usage_alias(void)
45 : {
46 0 : fprintf(stderr, "Usage: pkg alias [-ql] [alias]\n\n");
47 0 : fprintf(stderr, "For more information see 'pkg help alias'.\n");
48 0 : }
49 :
50 : int
51 11 : exec_alias(int argc, char **argv)
52 : {
53 : const pkg_object *all_aliases;
54 : const pkg_object *alias;
55 11 : pkg_iter it = NULL;
56 : int ch;
57 11 : int ret = EX_OK;
58 11 : bool list = false;
59 :
60 11 : struct option longopts[] = {
61 : { "quiet", no_argument, NULL, 'q' },
62 : { "list", no_argument, NULL, 'l' },
63 : { NULL, 0, NULL, 0 },
64 : };
65 :
66 27 : while ((ch = getopt_long(argc, argv, "+ql", longopts, NULL)) != -1) {
67 5 : switch (ch) {
68 : case 'q':
69 5 : quiet = true;
70 5 : break;
71 : case 'l':
72 0 : list = true;
73 0 : break;
74 : default:
75 0 : usage_alias();
76 0 : return (EX_USAGE);
77 : }
78 : }
79 :
80 11 : argc -= optind;
81 11 : argv += optind;
82 :
83 11 : all_aliases = pkg_config_get("ALIAS");
84 :
85 11 : if (argc == 0) {
86 8 : if (!quiet && list)
87 0 : printf("%s\n", "ALIAS");
88 8 : else if (!quiet)
89 3 : printf("%-20s %s\n", "ALIAS", "ARGUMENTS");
90 28 : while ((alias = pkg_object_iterate(all_aliases, &it))) {
91 12 : if (list)
92 0 : printf("%s\n", pkg_object_key(alias));
93 : else
94 12 : printf("%-20s '%s'\n", pkg_object_key(alias), pkg_object_string(alias));
95 : }
96 8 : return (ret);
97 : }
98 :
99 6 : for (int i = 0; i < argc; i++) {
100 3 : it = NULL;
101 10 : while ((alias = pkg_object_iterate(all_aliases, &it))) {
102 4 : if (strcmp(argv[i], pkg_object_key(alias)) == 0)
103 0 : break;
104 : }
105 3 : if (alias) {
106 0 : if (list)
107 0 : printf("%s\n", argv[i]);
108 : else
109 0 : printf("%-20s '%s'\n", argv[i], pkg_object_string(alias));
110 : } else {
111 3 : warnx("No such alias: '%s'", argv[i]);
112 3 : ret = EX_UNAVAILABLE;
113 : }
114 : }
115 :
116 3 : return (ret);
117 : }
118 :
|