118 lines
3.6 KiB
C
118 lines
3.6 KiB
C
/* *****************************************************************************
|
|
* p7screen/args.c -- p7screen argument parsing.
|
|
* Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
|
|
*
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
* ************************************************************************** */
|
|
#include "main.h"
|
|
#include <stdlib.h>
|
|
#include <ctype.h>
|
|
#include <getopt.h>
|
|
|
|
/* ************************************************************************** */
|
|
/* Help and version messages */
|
|
/* ************************************************************************** */
|
|
/* Version message */
|
|
static const char version_message[] =
|
|
QUOTE(BIN) " - from " QUOTE(NAME) " 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.";
|
|
|
|
/* Help message */
|
|
static const char help_main[] =
|
|
"Usage: " QUOTE(BIN) " [--help|-h] [--version|-v]\n"
|
|
"\n"
|
|
"Displays the streamed screen from a CASIO fx calculator.\n"
|
|
"\n"
|
|
"Options are:\n"
|
|
" -h, --help Display this help page\n"
|
|
" -v, --version Displays the version\n"
|
|
" -z ZOOM Change the zoom (1 to 16)\n"
|
|
" By default, the zoom will be " QUOTE(DEFAULT_ZOOM) ".\n"
|
|
"\n"
|
|
"Report bugs to " QUOTE(MAINTAINER) ".";
|
|
|
|
/* ************************************************************************** */
|
|
/* Main function */
|
|
/* ************************************************************************** */
|
|
/**
|
|
* parse_args:
|
|
* Args parsing main function.
|
|
*
|
|
* Inspired of the edits of my first experiment with getopt.
|
|
* Interesting, huh?
|
|
*
|
|
* @arg ac the arguments count
|
|
* @arg av the arguments values
|
|
* @arg zoom pointer the zoom
|
|
* @return 0 if ok, other if not.
|
|
*/
|
|
|
|
int parse_args(int ac, char **av, int *zoom)
|
|
{
|
|
/* initialize args */
|
|
*zoom = DEFAULT_ZOOM;
|
|
|
|
/* define options */
|
|
const char short_options[] = "hvz:";
|
|
const struct option long_options[] = {
|
|
{"help", no_argument, NULL, 'h'},
|
|
{"version", no_argument, NULL, 'v'},
|
|
{"zoom", required_argument, NULL, 'z'},
|
|
{NULL, 0, NULL, 0}
|
|
};
|
|
|
|
/* get all options */
|
|
int c; opterr = 0;
|
|
int help = 0, version = 0;
|
|
while ((c = getopt_long(ac, av, short_options, long_options, NULL)) != -1) {
|
|
switch (c) {
|
|
/* help */
|
|
case 'h': help = 1; break;
|
|
/* version */
|
|
case 'v': version = 1; break;
|
|
/* zoom */
|
|
case 'z':
|
|
*zoom = atoi(optarg);
|
|
if (*zoom <= 0 || *zoom > 16) {
|
|
log("-z, --zoom: should be between 1 and 16");
|
|
return (1);
|
|
}
|
|
break;
|
|
|
|
/* error (ignore) */
|
|
case '?':
|
|
if (optopt == 'z')
|
|
log("-z, --zoom: expected an argument\n");
|
|
else
|
|
break;
|
|
return (1);
|
|
}
|
|
}
|
|
|
|
/* check if there is any parameter */
|
|
if (ac - optind)
|
|
help = 1;
|
|
|
|
/* print help or version if required, and return */
|
|
if (version) puts(version_message);
|
|
else if (help) puts(help_main);
|
|
else return (0);
|
|
return (1);
|
|
}
|