/* ***************************************************************************** * mcsfile/args.c -- mcsfile command-line arguments parsing utility. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * This file is part of p7utils. * p7utils is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2.0 of the License, * or (at your option) any later version. * * p7utils is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with p7utils; if not, see . * ************************************************************************** */ #include "main.h" #include #define Q(x) #x #define QUOTE(x) Q(x) /* ************************************************************************** */ /* Help and version messages */ /* ************************************************************************** */ /* Help message */ static const char *help_message = "Usage: mcsfile [--version|-v] [--help|-h] \n" "\n" "Reads mcs files in a g1m file.\n" "\n" "Options are:\n" " -h, --help Display this help message.\n" " -v, --version Display the version message.\n" "\n" "Report bugs to " QUOTE(MAINTAINER) "."; /* Version message */ static const char *version_message = "mcsfile - from p7utils v" QUOTE(VERSION) " (licensed under GPLv2)\n" "Maintained by " QUOTE(MAINTAINER) ".\n" "\n" "This is free software; see the source for copying conditions.\n" "There is NO warranty; not even for MERCHANTABILITY or\n" "FITNESS FOR A PARTICULAR PURPOSE."; /* ************************************************************************** */ /* Main function */ /* ************************************************************************** */ /** * parse_args: * Args parsing main function. * * @arg ac the arguments count. * @arg av the arguments values. * @arg files pointer to the file paths tab to allocate. * @return if execution should stop. */ int parse_args(int ac, char **av, const char **path) { /* getopt elements */ const char *optstring = "hv"; const struct option longopts[] = { {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'v'}, {} }; /* get options */ int c; opterr = 0; int help = 0, version = 0; while ((c = getopt_long(ac, av, optstring, longopts, NULL)) != -1) switch (c) { case 'h': help = 1; break; case 'v': version = 1; break; default: switch (optopt) { default: fprintf(stderr, "-%c: unknown option.\n", optopt); } return (1); } /* check parameters */ int pc = ac - optind; char **pv = &av[optind]; if (pc != 1) help = 1; else *path = *pv; /* display version or help message */ if (version) { puts(version_message); return (1); } else if (help) { puts(help_message); return (1); } /* no error */ return (0); }