| // SPDX-License-Identifier: LGPL-2.1-or-later |
| /* |
| * This file is part of libgpiod. |
| * |
| * Copyright (C) 2017-2018 Bartosz Golaszewski <bartekgola@gmail.com> |
| */ |
| |
| #include <getopt.h> |
| #include <gpiod.h> |
| #include <stdio.h> |
| #include <string.h> |
| |
| #include "tools-common.h" |
| |
| static const struct option longopts[] = { |
| { "help", no_argument, NULL, 'h' }, |
| { "version", no_argument, NULL, 'v' }, |
| { GETOPT_NULL_LONGOPT }, |
| }; |
| |
| static const char *const shortopts = "+hv"; |
| |
| static void print_help(void) |
| { |
| printf("Usage: %s [OPTIONS] <name>\n", get_progname()); |
| printf("Find a GPIO line by name. The output of this command can be used as input for gpioget/set.\n"); |
| printf("\n"); |
| printf("Options:\n"); |
| printf(" -h, --help:\t\tdisplay this message and exit\n"); |
| printf(" -v, --version:\tdisplay the version and exit\n"); |
| } |
| |
| int main(int argc, char **argv) |
| { |
| unsigned int offset; |
| int optc, opti, rv; |
| char chip[32]; |
| |
| for (;;) { |
| optc = getopt_long(argc, argv, shortopts, longopts, &opti); |
| if (optc < 0) |
| break; |
| |
| switch (optc) { |
| case 'h': |
| print_help(); |
| return EXIT_SUCCESS; |
| case 'v': |
| print_version(); |
| return EXIT_SUCCESS; |
| case '?': |
| die("try %s --help", get_progname()); |
| default: |
| abort(); |
| } |
| } |
| |
| argc -= optind; |
| argv += optind; |
| |
| if (argc != 1) |
| die("exactly one GPIO line name must be specified"); |
| |
| rv = gpiod_ctxless_find_line(argv[0], chip, sizeof(chip), &offset); |
| if (rv < 0) |
| die_perror("error performing the line lookup"); |
| else if (rv == 0) |
| return EXIT_FAILURE; |
| |
| printf("%s %u\n", chip, offset); |
| |
| return EXIT_SUCCESS; |
| } |