[ef7052ec] | 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 |
|
---|
[32d96e1] | 29 | /** @addtogroup loc
|
---|
[ef7052ec] | 30 | * @{
|
---|
| 31 | */
|
---|
[32d96e1] | 32 | /** @file loc.c Print information from location service.
|
---|
[ef7052ec] | 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <errno.h>
|
---|
| 36 | #include <loc.h>
|
---|
| 37 | #include <stdio.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
[8d2dd7f2] | 39 | #include <stdint.h>
|
---|
[7354b5e] | 40 | #include <inttypes.h>
|
---|
[ef7052ec] | 41 | #include <str.h>
|
---|
| 42 |
|
---|
[32d96e1] | 43 | #define NAME "loc"
|
---|
[ef7052ec] | 44 |
|
---|
[b7fd2a0] | 45 | static errno_t show_cat(const char *cat_name, category_id_t cat_id)
|
---|
[ef7052ec] | 46 | {
|
---|
| 47 | service_id_t *svc_ids;
|
---|
| 48 | size_t svc_cnt;
|
---|
[4e7637a] | 49 | char *svc_name;
|
---|
[a3fcfba] | 50 | char *server_name;
|
---|
[b7fd2a0] | 51 | errno_t rc;
|
---|
[4e7637a] | 52 | size_t j;
|
---|
| 53 |
|
---|
[75b9c3d] | 54 | printf("%s:\n", cat_name);
|
---|
[4e7637a] | 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 | }
|
---|
[a3fcfba] | 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);
|
---|
[a35b458] | 83 |
|
---|
[4e7637a] | 84 | free(svc_name);
|
---|
[a3fcfba] | 85 | free(server_name);
|
---|
[4e7637a] | 86 | }
|
---|
| 87 |
|
---|
| 88 | free(svc_ids);
|
---|
| 89 | return EOK;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[b7fd2a0] | 92 | static errno_t list_svcs_by_cat(void)
|
---|
[4e7637a] | 93 | {
|
---|
| 94 | category_id_t *cat_ids;
|
---|
| 95 | size_t cat_cnt;
|
---|
[ef7052ec] | 96 |
|
---|
[4e7637a] | 97 | size_t i;
|
---|
[ef7052ec] | 98 | char *cat_name;
|
---|
[b7fd2a0] | 99 | errno_t rc;
|
---|
[ef7052ec] | 100 |
|
---|
| 101 | rc = loc_get_categories(&cat_ids, &cat_cnt);
|
---|
| 102 | if (rc != EOK) {
|
---|
| 103 | printf(NAME ": Error getting list of categories.\n");
|
---|
[4e7637a] | 104 | return rc;
|
---|
[ef7052ec] | 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");
|
---|
[4e7637a] | 114 | free(cat_ids);
|
---|
| 115 | return rc;
|
---|
[ef7052ec] | 116 | }
|
---|
| 117 |
|
---|
[4e7637a] | 118 | rc = show_cat(cat_name, cat_ids[i]);
|
---|
| 119 | (void) rc;
|
---|
[ef7052ec] | 120 |
|
---|
[4e7637a] | 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 "
|
---|
[1433ecda] | 132 | "they contain\n"
|
---|
[4e7637a] | 133 | "\t" NAME " show-cat <category> List services in category\n");
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | int main(int argc, char *argv[])
|
---|
| 137 | {
|
---|
[b7fd2a0] | 138 | errno_t rc;
|
---|
[4e7637a] | 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;
|
---|
[ef7052ec] | 156 | }
|
---|
| 157 |
|
---|
[4e7637a] | 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;
|
---|
[ef7052ec] | 163 | }
|
---|
| 164 |
|
---|
[4e7637a] | 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;
|
---|
[ef7052ec] | 172 | }
|
---|
| 173 |
|
---|
| 174 | return 0;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /** @}
|
---|
| 178 | */
|
---|