| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2011 Jiri Svoboda
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | /** @addtogroup loc
|
|---|
| 8 | * @{
|
|---|
| 9 | */
|
|---|
| 10 | /** @file loc.c Print information from location service.
|
|---|
| 11 | */
|
|---|
| 12 |
|
|---|
| 13 | #include <errno.h>
|
|---|
| 14 | #include <loc.h>
|
|---|
| 15 | #include <stdio.h>
|
|---|
| 16 | #include <stdlib.h>
|
|---|
| 17 | #include <stdint.h>
|
|---|
| 18 | #include <inttypes.h>
|
|---|
| 19 | #include <str.h>
|
|---|
| 20 |
|
|---|
| 21 | #define NAME "loc"
|
|---|
| 22 |
|
|---|
| 23 | static errno_t show_cat(const char *cat_name, category_id_t cat_id)
|
|---|
| 24 | {
|
|---|
| 25 | service_id_t *svc_ids;
|
|---|
| 26 | size_t svc_cnt;
|
|---|
| 27 | char *svc_name;
|
|---|
| 28 | char *server_name;
|
|---|
| 29 | errno_t rc;
|
|---|
| 30 | size_t j;
|
|---|
| 31 |
|
|---|
| 32 | printf("%s:\n", cat_name);
|
|---|
| 33 |
|
|---|
| 34 | rc = loc_category_get_svcs(cat_id, &svc_ids, &svc_cnt);
|
|---|
| 35 | if (rc != EOK) {
|
|---|
| 36 | printf(NAME ": Failed getting list of services in "
|
|---|
| 37 | "category %s, skipping.\n", cat_name);
|
|---|
| 38 | return rc;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | for (j = 0; j < svc_cnt; j++) {
|
|---|
| 42 | rc = loc_service_get_name(svc_ids[j], &svc_name);
|
|---|
| 43 | if (rc != EOK) {
|
|---|
| 44 | printf(NAME ": Unknown service name (SID %"
|
|---|
| 45 | PRIun ").\n", svc_ids[j]);
|
|---|
| 46 | continue;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | rc = loc_service_get_server_name(svc_ids[j], &server_name);
|
|---|
| 50 | if (rc != EOK && rc != EINVAL) {
|
|---|
| 51 | free(svc_name);
|
|---|
| 52 | printf(NAME ": Unknown service name (SID %"
|
|---|
| 53 | PRIun ").\n", svc_ids[j]);
|
|---|
| 54 | continue;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | if (rc == EOK)
|
|---|
| 58 | printf("\t%s : %s\n", svc_name, server_name);
|
|---|
| 59 | else
|
|---|
| 60 | printf("\t%s\n", svc_name);
|
|---|
| 61 |
|
|---|
| 62 | free(svc_name);
|
|---|
| 63 | free(server_name);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | free(svc_ids);
|
|---|
| 67 | return EOK;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | static errno_t list_svcs_by_cat(void)
|
|---|
| 71 | {
|
|---|
| 72 | category_id_t *cat_ids;
|
|---|
| 73 | size_t cat_cnt;
|
|---|
| 74 |
|
|---|
| 75 | size_t i;
|
|---|
| 76 | char *cat_name;
|
|---|
| 77 | errno_t rc;
|
|---|
| 78 |
|
|---|
| 79 | rc = loc_get_categories(&cat_ids, &cat_cnt);
|
|---|
| 80 | if (rc != EOK) {
|
|---|
| 81 | printf(NAME ": Error getting list of categories.\n");
|
|---|
| 82 | return rc;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | for (i = 0; i < cat_cnt; i++) {
|
|---|
| 86 | rc = loc_category_get_name(cat_ids[i], &cat_name);
|
|---|
| 87 | if (rc != EOK)
|
|---|
| 88 | cat_name = str_dup("<unknown>");
|
|---|
| 89 |
|
|---|
| 90 | if (cat_name == NULL) {
|
|---|
| 91 | printf(NAME ": Error allocating memory.\n");
|
|---|
| 92 | free(cat_ids);
|
|---|
| 93 | return rc;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | rc = show_cat(cat_name, cat_ids[i]);
|
|---|
| 97 | (void) rc;
|
|---|
| 98 |
|
|---|
| 99 | free(cat_name);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | free(cat_ids);
|
|---|
| 103 | return EOK;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | static void print_syntax(void)
|
|---|
| 107 | {
|
|---|
| 108 | printf("syntax:\n"
|
|---|
| 109 | "\t" NAME " List categories and services "
|
|---|
| 110 | "they contain\n"
|
|---|
| 111 | "\t" NAME " show-cat <category> List services in category\n");
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | int main(int argc, char *argv[])
|
|---|
| 115 | {
|
|---|
| 116 | errno_t rc;
|
|---|
| 117 | char *cmd;
|
|---|
| 118 | char *cat_name;
|
|---|
| 119 | category_id_t cat_id;
|
|---|
| 120 |
|
|---|
| 121 | if (argc <= 1) {
|
|---|
| 122 | rc = list_svcs_by_cat();
|
|---|
| 123 | if (rc != EOK)
|
|---|
| 124 | return 1;
|
|---|
| 125 | return 0;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | cmd = argv[1];
|
|---|
| 129 | if (str_cmp(cmd, "show-cat") == 0) {
|
|---|
| 130 | if (argc < 3) {
|
|---|
| 131 | printf("Argument missing.\n");
|
|---|
| 132 | print_syntax();
|
|---|
| 133 | return 1;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | cat_name = argv[2];
|
|---|
| 137 | rc = loc_category_get_id(cat_name, &cat_id, 0);
|
|---|
| 138 | if (rc != EOK) {
|
|---|
| 139 | printf("Error looking up category '%s'.\n", cat_name);
|
|---|
| 140 | return 1;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | rc = show_cat(cat_name, cat_id);
|
|---|
| 144 | if (rc != EOK)
|
|---|
| 145 | return 1;
|
|---|
| 146 | } else {
|
|---|
| 147 | printf("Invalid command '%s'\n", cmd);
|
|---|
| 148 | print_syntax();
|
|---|
| 149 | return 1;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | return 0;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /** @}
|
|---|
| 156 | */
|
|---|