119 lines
3.8 KiB
C
119 lines
3.8 KiB
C
/* ************************************************************************** */
|
|
/* _____ _ */
|
|
/* p7screen/args.c |_ _|__ _ _| |__ ___ _ _ */
|
|
/* | Project: p7utils | |/ _ \| | | | '_ \ / _ \ | | | */
|
|
/* | | (_) | |_| | | | | __/ |_| | */
|
|
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
|
|
/* Last updated: 2016/12/21 23:46:41 |___/ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#include "main.h"
|
|
#include <stdlib.h>
|
|
#include <getopt.h>
|
|
|
|
/* ************************************************************************** */
|
|
/* Help and version messages */
|
|
/* ************************************************************************** */
|
|
/* Version message */
|
|
static const char version_message[] =
|
|
"p7screen - 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.";
|
|
|
|
/* Help message */
|
|
static const char help_main[] =
|
|
"Usage: p7screen [--help|-h] [--version|-v]\n"
|
|
" [--device /dev/omg0]\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"
|
|
" --device dev The calculator device (/dev/ttyUSBx).\n"
|
|
" By default, will use the first appropriate device found.\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 device pointer to the device
|
|
* @arg zoom pointer the zoom
|
|
* @arg args the parsed args pointer
|
|
* @return 0 if ok, other if not.
|
|
*/
|
|
|
|
int parse_args(int ac, char **av, const char **device, int *zoom)
|
|
{
|
|
/* initialize args */
|
|
*device = NULL;
|
|
*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'},
|
|
{"device", required_argument, NULL, 'd'},
|
|
{"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;
|
|
/* device */
|
|
case 'd': *device = optarg; 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 if (optopt == 'd')
|
|
log("--device: 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);
|
|
}
|