cake
/
p7utils
Archived
1
0
Fork 0
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
p7utils/src/p7screen/args.c

122 lines
4.0 KiB
C

/* ************************************************************************** */
/* _____ _ */
/* p7screen/args.c |_ _|__ _ _| |__ ___ _ _ */
/* | Project: p7utils | |/ _ \| | | | '_ \ / _ \ | | | */
/* | | (_) | |_| | | | | __/ |_| | */
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
/* Last updated: 2017/01/16 23:55:55 |___/ */
/* */
/* ************************************************************************** */
#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"
" [--com <device>]\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"
" --com <device> The serial device, if you want to communicate with a\n"
" calculator connected using a USB-to-serial cable.\n"
" If this option isn't used, the program will look for a\n"
" calculator connected using direct USB.\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 **com, int *zoom)
{
/* initialize args */
*com = 0;
*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'},
{"com", required_argument, NULL, 'c'},
{"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;
/* com port */
case 'c': *com = 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 == 'c')
log("--com: 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);
}