| 1 | /* | 
|---|
| 2 | * Copyright (c) 2011 Jiri Svoboda | 
|---|
| 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 | * | 
|---|
| 9 | * - Redistributions of source code must retain the above copyright | 
|---|
| 10 | *   notice, this list of conditions and the following disclaimer. | 
|---|
| 11 | * - 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 | * - The name of the author may not be used to endorse or promote products | 
|---|
| 15 | *   derived from this software without specific prior written permission. | 
|---|
| 16 | * | 
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** @addtogroup loc | 
|---|
| 30 | * @{ | 
|---|
| 31 | */ | 
|---|
| 32 | /** @file loc.c Print information from location service. | 
|---|
| 33 | */ | 
|---|
| 34 |  | 
|---|
| 35 | #include <errno.h> | 
|---|
| 36 | #include <loc.h> | 
|---|
| 37 | #include <stdio.h> | 
|---|
| 38 | #include <stdlib.h> | 
|---|
| 39 | #include <stdint.h> | 
|---|
| 40 | #include <inttypes.h> | 
|---|
| 41 | #include <str.h> | 
|---|
| 42 |  | 
|---|
| 43 | #define NAME "loc" | 
|---|
| 44 |  | 
|---|
| 45 | static int show_cat(const char *cat_name, category_id_t cat_id) | 
|---|
| 46 | { | 
|---|
| 47 | service_id_t *svc_ids; | 
|---|
| 48 | size_t svc_cnt; | 
|---|
| 49 | char *svc_name; | 
|---|
| 50 | char *server_name; | 
|---|
| 51 | int rc; | 
|---|
| 52 | size_t j; | 
|---|
| 53 |  | 
|---|
| 54 | printf("%s:\n", cat_name); | 
|---|
| 55 |  | 
|---|
| 56 | rc = loc_category_get_svcs(cat_id, &svc_ids, &svc_cnt); | 
|---|
| 57 | if (rc != EOK) { | 
|---|
| 58 | printf(NAME ": Failed getting list of services in " | 
|---|
| 59 | "category %s, skipping.\n", cat_name); | 
|---|
| 60 | return rc; | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | for (j = 0; j < svc_cnt; j++) { | 
|---|
| 64 | rc = loc_service_get_name(svc_ids[j], &svc_name); | 
|---|
| 65 | if (rc != EOK) { | 
|---|
| 66 | printf(NAME ": Unknown service name (SID %" | 
|---|
| 67 | PRIun ").\n", svc_ids[j]); | 
|---|
| 68 | continue; | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | rc = loc_service_get_server_name(svc_ids[j], &server_name); | 
|---|
| 72 | if (rc != EOK && rc != EINVAL) { | 
|---|
| 73 | free(svc_name); | 
|---|
| 74 | printf(NAME ": Unknown service name (SID %" | 
|---|
| 75 | PRIun ").\n", svc_ids[j]); | 
|---|
| 76 | continue; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | if (rc == EOK) | 
|---|
| 80 | printf("\t%s : %s\n", svc_name, server_name); | 
|---|
| 81 | else | 
|---|
| 82 | printf("\t%s\n", svc_name); | 
|---|
| 83 |  | 
|---|
| 84 | free(svc_name); | 
|---|
| 85 | free(server_name); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | free(svc_ids); | 
|---|
| 89 | return EOK; | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | static int list_svcs_by_cat(void) | 
|---|
| 93 | { | 
|---|
| 94 | category_id_t *cat_ids; | 
|---|
| 95 | size_t cat_cnt; | 
|---|
| 96 |  | 
|---|
| 97 | size_t i; | 
|---|
| 98 | char *cat_name; | 
|---|
| 99 | int rc; | 
|---|
| 100 |  | 
|---|
| 101 | rc = loc_get_categories(&cat_ids, &cat_cnt); | 
|---|
| 102 | if (rc != EOK) { | 
|---|
| 103 | printf(NAME ": Error getting list of categories.\n"); | 
|---|
| 104 | return rc; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | for (i = 0; i < cat_cnt; i++) { | 
|---|
| 108 | rc = loc_category_get_name(cat_ids[i], &cat_name); | 
|---|
| 109 | if (rc != EOK) | 
|---|
| 110 | cat_name = str_dup("<unknown>"); | 
|---|
| 111 |  | 
|---|
| 112 | if (cat_name == NULL) { | 
|---|
| 113 | printf(NAME ": Error allocating memory.\n"); | 
|---|
| 114 | free(cat_ids); | 
|---|
| 115 | return rc; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | rc = show_cat(cat_name, cat_ids[i]); | 
|---|
| 119 | (void) rc; | 
|---|
| 120 |  | 
|---|
| 121 | free(cat_name); | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | free(cat_ids); | 
|---|
| 125 | return EOK; | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | static void print_syntax(void) | 
|---|
| 129 | { | 
|---|
| 130 | printf("syntax:\n" | 
|---|
| 131 | "\t" NAME "                      List categories and services " | 
|---|
| 132 | "they contain\n" | 
|---|
| 133 | "\t" NAME " show-cat <category>  List services in category\n"); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | int main(int argc, char *argv[]) | 
|---|
| 137 | { | 
|---|
| 138 | int rc; | 
|---|
| 139 | char *cmd; | 
|---|
| 140 | char *cat_name; | 
|---|
| 141 | category_id_t cat_id; | 
|---|
| 142 |  | 
|---|
| 143 | if (argc <= 1) { | 
|---|
| 144 | rc = list_svcs_by_cat(); | 
|---|
| 145 | if (rc != EOK) | 
|---|
| 146 | return 1; | 
|---|
| 147 | return 0; | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | cmd = argv[1]; | 
|---|
| 151 | if (str_cmp(cmd, "show-cat") == 0) { | 
|---|
| 152 | if (argc < 3) { | 
|---|
| 153 | printf("Argument missing.\n"); | 
|---|
| 154 | print_syntax(); | 
|---|
| 155 | return 1; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | cat_name = argv[2]; | 
|---|
| 159 | rc = loc_category_get_id(cat_name, &cat_id, 0); | 
|---|
| 160 | if (rc != EOK) { | 
|---|
| 161 | printf("Error looking up category '%s'.\n", cat_name); | 
|---|
| 162 | return 1; | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | rc = show_cat(cat_name, cat_id); | 
|---|
| 166 | if (rc != EOK) | 
|---|
| 167 | return 1; | 
|---|
| 168 | } else { | 
|---|
| 169 | printf("Invalid command '%s'\n", cmd); | 
|---|
| 170 | print_syntax(); | 
|---|
| 171 | return 1; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | return 0; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | /** @} | 
|---|
| 178 | */ | 
|---|