HelenOS sources
This source file includes following definitions.
- show_cat
- list_svcs_by_cat
- print_syntax
- main
#include <errno.h>
#include <loc.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <str.h>
#define NAME "loc"
static errno_t show_cat(const char *cat_name, category_id_t cat_id)
{
service_id_t *svc_ids;
size_t svc_cnt;
char *svc_name;
char *server_name;
errno_t rc;
size_t j;
printf("%s:\n", cat_name);
rc = loc_category_get_svcs(cat_id, &svc_ids, &svc_cnt);
if (rc != EOK) {
printf(NAME ": Failed getting list of services in "
"category %s, skipping.\n", cat_name);
return rc;
}
for (j = 0; j < svc_cnt; j++) {
rc = loc_service_get_name(svc_ids[j], &svc_name);
if (rc != EOK) {
printf(NAME ": Unknown service name (SID %"
PRIun ").\n", svc_ids[j]);
continue;
}
rc = loc_service_get_server_name(svc_ids[j], &server_name);
if (rc != EOK && rc != EINVAL) {
free(svc_name);
printf(NAME ": Unknown service name (SID %"
PRIun ").\n", svc_ids[j]);
continue;
}
if (rc == EOK)
printf("\t%s : %s\n", svc_name, server_name);
else
printf("\t%s\n", svc_name);
free(svc_name);
free(server_name);
}
free(svc_ids);
return EOK;
}
static errno_t list_svcs_by_cat(void)
{
category_id_t *cat_ids;
size_t cat_cnt;
size_t i;
char *cat_name;
errno_t rc;
rc = loc_get_categories(&cat_ids, &cat_cnt);
if (rc != EOK) {
printf(NAME ": Error getting list of categories.\n");
return rc;
}
for (i = 0; i < cat_cnt; i++) {
rc = loc_category_get_name(cat_ids[i], &cat_name);
if (rc != EOK)
cat_name = str_dup("<unknown>");
if (cat_name == NULL) {
printf(NAME ": Error allocating memory.\n");
free(cat_ids);
return rc;
}
rc = show_cat(cat_name, cat_ids[i]);
(void) rc;
free(cat_name);
}
free(cat_ids);
return EOK;
}
static void print_syntax(void)
{
printf("syntax:\n"
"\t" NAME " List categories and services "
"they contain\n"
"\t" NAME " show-cat <category> List services in category\n");
}
int main(int argc, char *argv[])
{
errno_t rc;
char *cmd;
char *cat_name;
category_id_t cat_id;
if (argc <= 1) {
rc = list_svcs_by_cat();
if (rc != EOK)
return 1;
return 0;
}
cmd = argv[1];
if (str_cmp(cmd, "show-cat") == 0) {
if (argc < 3) {
printf("Argument missing.\n");
print_syntax();
return 1;
}
cat_name = argv[2];
rc = loc_category_get_id(cat_name, &cat_id, 0);
if (rc != EOK) {
printf("Error looking up category '%s'.\n", cat_name);
return 1;
}
rc = show_cat(cat_name, cat_id);
if (rc != EOK)
return 1;
} else {
printf("Invalid command '%s'\n", cmd);
print_syntax();
return 1;
}
return 0;
}
HelenOS homepage, sources at GitHub