source: mainline/uspace/app/loc/loc.c@ 4e7637a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4e7637a was 4e7637a, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Add show-cat subcommand to loc which lists services in a given category.

  • Property mode set to 100644
File size: 3.8 KB
Line 
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 <str.h>
40#include <sys/types.h>
41#include <sys/typefmt.h>
42
43#define NAME "loc"
44
45static 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 int rc;
51 size_t j;
52
53 printf("%s (%" PRIun "):\n", cat_name, cat_id);
54
55 rc = loc_category_get_svcs(cat_id, &svc_ids, &svc_cnt);
56 if (rc != EOK) {
57 printf(NAME ": Failed getting list of services in "
58 "category %s, skipping.\n", cat_name);
59 return rc;
60 }
61
62 for (j = 0; j < svc_cnt; j++) {
63 rc = loc_service_get_name(svc_ids[j], &svc_name);
64 if (rc != EOK) {
65 printf(NAME ": Unknown service name (SID %"
66 PRIun ").\n", svc_ids[j]);
67 continue;
68 }
69 printf("\t%s (%" PRIun ")\n", svc_name, svc_ids[j]);
70 free(svc_name);
71 }
72
73 free(svc_ids);
74 return EOK;
75}
76
77static int list_svcs_by_cat(void)
78{
79 category_id_t *cat_ids;
80 size_t cat_cnt;
81
82 size_t i;
83 char *cat_name;
84 int rc;
85
86 rc = loc_get_categories(&cat_ids, &cat_cnt);
87 if (rc != EOK) {
88 printf(NAME ": Error getting list of categories.\n");
89 return rc;
90 }
91
92 for (i = 0; i < cat_cnt; i++) {
93 rc = loc_category_get_name(cat_ids[i], &cat_name);
94 if (rc != EOK)
95 cat_name = str_dup("<unknown>");
96
97 if (cat_name == NULL) {
98 printf(NAME ": Error allocating memory.\n");
99 free(cat_ids);
100 return rc;
101 }
102
103 rc = show_cat(cat_name, cat_ids[i]);
104 (void) rc;
105
106 free(cat_name);
107 }
108
109 free(cat_ids);
110 return EOK;
111}
112
113static void print_syntax(void)
114{
115 printf("syntax:\n"
116 "\t" NAME " List categories and services "
117 "they contain\n"
118 "\t" NAME " show-cat <category> List services in category\n");
119}
120
121int main(int argc, char *argv[])
122{
123 int rc;
124 char *cmd;
125 char *cat_name;
126 category_id_t cat_id;
127
128 if (argc <= 1) {
129 rc = list_svcs_by_cat();
130 if (rc != EOK)
131 return 1;
132 return 0;
133 }
134
135 cmd = argv[1];
136 if (str_cmp(cmd, "show-cat") == 0) {
137 if (argc < 3) {
138 printf("Argument missing.\n");
139 print_syntax();
140 return 1;
141 }
142
143 cat_name = argv[2];
144 rc = loc_category_get_id(cat_name, &cat_id, 0);
145 if (rc != EOK) {
146 printf("Error looking up category '%s'.\n", cat_name);
147 return 1;
148 }
149
150 rc = show_cat(cat_name, cat_id);
151 if (rc != EOK)
152 return 1;
153 } else {
154 printf("Invalid command '%s'\n", cmd);
155 print_syntax();
156 return 1;
157 }
158
159 return 0;
160}
161
162/** @}
163 */
Note: See TracBrowser for help on using the repository browser.