/ src / config.c
config.c
 1  /*-
 2   * Copyright (c) 2013 Baptiste Daroussin <bapt@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 <ctype.h>
28  #include <err.h>
29  #include <inttypes.h>
30  #include <stdio.h>
31  
32  #include <pkg.h>
33  
34  #include "pkgcli.h"
35  
36  void
37  usage_config(void)
38  {
39  	fprintf(stderr,
40              "Usage: pkg config <name>\n\n");
41  	//fprintf(stderr, "For more information see 'pkg help config'.\n");
42  }
43  
44  int
45  exec_config(int argc, char **argv)
46  {
47  	const pkg_object *conf, *o;
48  	pkg_iter it = NULL;
49  	const char *buf;
50  	char *key;
51  	int64_t integer;
52  	int i;
53  	bool b;
54  
55  	if (argc != 2) {
56  		usage_config();
57  		return (EXIT_FAILURE);
58  	}
59  
60  	key = argv[1];
61  	for (i = 0; key[i] != '\0'; i++)
62  		key[i] = toupper(key[i]);
63  
64  	conf = pkg_config_get(key);
65  	if (conf == NULL) {
66  		warnx("No such configuration options: %s", key);
67  		return (EXIT_FAILURE);
68  	}
69  
70  	switch (pkg_object_type(conf)) {
71  	case PKG_STRING:
72  		buf = pkg_object_string(conf);
73  		printf("%s\n", buf == NULL ? "" : buf);
74  		break;
75  	case PKG_BOOL:
76  		b = pkg_object_bool(conf);
77  		printf("%s\n", b ? "yes" : "no");
78  		break;
79  	case PKG_INT:
80  		integer = pkg_object_int(conf);
81  		printf("%"PRId64"\n", integer);
82  		break;
83  	case PKG_OBJECT:
84  		while ((o = pkg_object_iterate(conf, &it))) {
85  			printf("%s: %s\n", pkg_object_key(o), pkg_object_string(o));
86  		}
87  		break;
88  	case PKG_ARRAY:
89  		while ((o = pkg_object_iterate(conf, &it))) {
90  			printf("%s\n", pkg_object_string(o));
91  		}
92  		break;
93  	default:
94  		break;
95  	}
96  
97  	return (EXIT_SUCCESS);
98  }