Branch data 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 : :
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 : 0 : usage_alias(void)
41 : : {
42 : 0 : fprintf(stderr, "Usage: pkg alias [-ql] [alias]\n\n");
43 : 0 : fprintf(stderr, "For more information see 'pkg help alias'.\n");
44 : 0 : }
45 : :
46 : : int
47 : 11 : exec_alias(int argc, char **argv)
48 : : {
49 : : const pkg_object *all_aliases;
50 : : const pkg_object *alias;
51 : 11 : pkg_iter it = NULL;
52 : : int ch;
53 : 11 : int ret = EXIT_SUCCESS;
54 : 11 : bool list = false;
55 : :
56 : 11 : struct option longopts[] = {
57 : : { "quiet", no_argument, NULL, 'q' },
58 : : { "list", no_argument, NULL, 'l' },
59 : : { NULL, 0, NULL, 0 },
60 : : };
61 : :
62 [ + + ]: 16 : while ((ch = getopt_long(argc, argv, "+ql", longopts, NULL)) != -1) {
63 [ - - + ]: 5 : switch (ch) {
64 : : case 'q':
65 : 5 : quiet = true;
66 : 5 : break;
67 : : case 'l':
68 : 0 : list = true;
69 : 0 : break;
70 : : default:
71 : 0 : usage_alias();
72 : 0 : return (EXIT_FAILURE);
73 : : }
74 : : }
75 : :
76 : 11 : argc -= optind;
77 : 11 : argv += optind;
78 : :
79 : 11 : all_aliases = pkg_config_get("ALIAS");
80 : :
81 [ + + ]: 11 : if (argc == 0) {
82 [ + + + - ]: 8 : if (!quiet && list)
83 : 0 : printf("%s\n", "ALIAS");
84 [ + + ]: 8 : else if (!quiet)
85 : 3 : printf("%-20s %s\n", "ALIAS", "ARGUMENTS");
86 [ + + ]: 20 : while ((alias = pkg_object_iterate(all_aliases, &it))) {
87 [ - + ]: 12 : if (list)
88 : 0 : printf("%s\n", pkg_object_key(alias));
89 : : else
90 : 12 : printf("%-20s '%s'\n", pkg_object_key(alias), pkg_object_string(alias));
91 : : }
92 : 8 : return (ret);
93 : : }
94 : :
95 [ + + ]: 6 : for (int i = 0; i < argc; i++) {
96 : 3 : it = NULL;
97 [ + + ]: 7 : while ((alias = pkg_object_iterate(all_aliases, &it))) {
98 [ - + ]: 4 : if (STREQ(argv[i], pkg_object_key(alias)))
99 : 0 : break;
100 : : }
101 [ - + ]: 3 : if (alias) {
102 [ # # ]: 0 : if (list)
103 : 0 : printf("%s\n", argv[i]);
104 : : else
105 : 0 : printf("%-20s '%s'\n", argv[i], pkg_object_string(alias));
106 : 0 : } else {
107 : 3 : warnx("No such alias: '%s'", argv[i]);
108 : 3 : ret = EXIT_FAILURE;
109 : : }
110 : 3 : }
111 : :
112 : 3 : return (ret);
113 : 11 : }
114 : :
|