Debian Codegraph @

dbus/client/find.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2023 Bartosz Golaszewski <[email protected]>
3
4 #include <stdlib.h>
5
6 #include "common.h"
7
8 static void find_line_in_chip(gpointer elem, gpointer user_data)
9 {
10 g_autoptr(GpiodbusObject) line_obj = NULL;
11 GpiodbusObject *chip_obj = elem;
12 const gchar *name = user_data;
13 GpiodbusChip *chip;
14 GpiodbusLine *line;
15
16 line_obj = get_line_obj_by_name_for_chip(chip_obj, name);
17 if (!line_obj)
18 return;
19
20 chip = gpiodbus_object_peek_chip(chip_obj);
21 line = gpiodbus_object_peek_line(line_obj);
22
23 g_print("%s %u\n",
24 gpiodbus_chip_get_name(chip),
25 gpiodbus_line_get_offset(line));
26
27 exit(EXIT_SUCCESS);
28 }
29
30 int gpiocli_find_main(int argc, char **argv)
31 {
32 static const gchar *const summary =
33 "Gicen a line name, find the name of the parent chip and offset of the line within that chip.";
34
35 static const gchar *const description =
36 "As line names are not guaranteed to be unique, this command finds the first line with given name.";
37
38 g_autolist(GpiodbusObject) objs = NULL;
39 g_auto(GStrv) line_name = NULL;
40
41 const GOptionEntry opts[] = {
42 {
43 .long_name = G_OPTION_REMAINING,
44 .flags = G_OPTION_FLAG_NONE,
45 .arg = G_OPTION_ARG_STRING_ARRAY,
46 .arg_data = &line_name,
47 .arg_description = "<line name>",
48 },
49 { }
50 };
51
52 parse_options(opts, summary, description, &argc, &argv);
53 check_manager();
54
55 if (!line_name)
56 die_parsing_opts("line name must be specified");
57 if (g_strv_length(line_name) != 1)
58 die_parsing_opts("only one line can be mapped");
59
60 objs = get_chip_objs(NULL);
61 g_list_foreach(objs, find_line_in_chip, line_name[0]);
62
63 /* If we got here, the line was not found. */
64 die("line '%s' not found", line_name[0]);
65 return EXIT_FAILURE;
66 }