cake
/
p7utils
Archived
1
0
Fork 0

Corrected to use cominit.

This commit is contained in:
Thomas Touhey 2017-01-11 22:58:24 +01:00
parent 804f9dbc70
commit 847d31231d
9 changed files with 87 additions and 40 deletions

View File

@ -12,7 +12,7 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
/* ************************************************************************** */
/* Help and version messages */
@ -45,9 +45,11 @@ static const char help_main[] =
"General options:\n"
" -h, --help Display the help page of the (sub)command and quit.\n"
" -v, --version Display the version message and quit.\n"
" --device dev The calculator device (ttyUSBx).\n"
" By default, will use the first appropriate device found.\n"
" --storage abc0 The storage device with which to interact (fls0, crd0).\n"
" --com <port> The USB-serial port, if you want to connect to a\n"
" calculator connected (1 to 20).\n"
" If this option isn't given, then a USB calculator will\n"
" be searched.\n"
" --storage <abc0> The storage device with which to interact (fls0, crd0).\n"
" Default storage device is '" QUOTE(DEFAULT_STORAGE) "'.\n"
" --no-exit Does not terminate connection when action is completed.\n"
" --no-init Does not initialize connection (should only be used\n"
@ -166,7 +168,7 @@ int parse_args(int ac, char **av, args_t *args)
.dirname = NULL, .filename = NULL,
.newdir = NULL, .newname = NULL,
.local = NULL, .force = 0,
.device = NULL,
.com = 0,
.storage = QUOTE(DEFAULT_STORAGE),
.noinit = 0, .noexit = 0};
@ -175,7 +177,7 @@ int parse_args(int ac, char **av, args_t *args)
struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{"device", required_argument, NULL, 'p'},
{"com", required_argument, NULL, 'c'},
{"storage", required_argument, NULL, 's'},
{"force", no_argument, NULL, 'f'},
{"output", required_argument, NULL, 'o'},
@ -191,7 +193,7 @@ int parse_args(int ac, char **av, args_t *args)
/* get all options */
int c; opterr = 0;
int help = 0;
const char *s_out = NULL, *s_dir = NULL, *s_todir = NULL;
const char *s_out = NULL, *s_dir = NULL, *s_todir = NULL, *s_com = NULL;
while ((c = getopt_long(ac, av, short_options, long_options, NULL)) != -1) {
switch (c) {
/* help */
@ -210,8 +212,8 @@ int parse_args(int ac, char **av, args_t *args)
/* destination directory name */
case 't': s_todir = optarg; break;
/* device */
case 'p': args->device = optarg; break;
/* com port */
case 'c': s_com = optarg; break;
/* storage */
case 's': args->storage = optarg; break;
/* force no initialization */
@ -227,8 +229,8 @@ int parse_args(int ac, char **av, args_t *args)
log("-d, --directory: expected an argument\n");
else if (optopt == 't')
log("-t, --to: expected an argument\n");
else if (optopt == 'p')
log("--device: expected an argument\n");
else if (optopt == 'c')
log("--com: expected an argument\n");
else if (optopt == 's')
log("--storage: expected an argument\n");
else
@ -238,6 +240,19 @@ int parse_args(int ac, char **av, args_t *args)
}
}
/* check com port */
if (s_com) {
if (!isdigit(s_com[0])) {
log("-c, --com: expected a number\n");
return (0);
}
args->com = atoi(s_com);
if (args->com < 1 || args->com > 20) {
log("-c, --com: COM port number should be between 1 and 20\n");
return (0);
}
}
/* get non-option arguments (subcommand and parameters) */
int aac = ac - optind;
char **aav = &av[optind];
@ -323,7 +338,7 @@ int parse_args(int ac, char **av, args_t *args)
else if (!(args->local = fopen(args->localpath, fpmode))) {
log("Could not open local file : %s\n", strerror(errno));
if (fpmode[0] == 'w')
unlink(args->localpath);
remove(args->localpath);
return (0);
}
}

View File

@ -187,7 +187,7 @@ int main(int ac, char **av)
/* Initialize libp7 and communication */
p7_handle_t *handle; int err;
if (args.device) err = p7_cinit(&handle, 1, !args.noinit, args.device, 0);
if (args.com) err = p7_cominit(&handle, 1, !args.noinit, args.com);
else err = p7_init(&handle, 1, !args.noinit);
if (err) {
/* display error */
@ -259,7 +259,7 @@ int main(int ac, char **av)
case p7_error_nocalc: log(error_disconnected); break;
case p7_error_unsupported: log(error_unsupported); break;
case p7_error_unsupported_device:
log(error_unsupported_device, args.device); break;
log(error_unsupported_device, args.storage); break;
default: log(error_unplanned, p7_strerror(err));
}

View File

@ -34,7 +34,7 @@ typedef struct {
int force;
/* other options */
const char *device;
int com;
const char *storage;
int noinit;
int noexit;

View File

@ -10,6 +10,7 @@
#include "main.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <getopt.h>
#include <errno.h>
@ -40,8 +41,10 @@ static const char help_main[] =
"General options:\n"
" -h, --help Display the help page of the (sub)command and quit.\n"
" -v, --version Display the version message and quit.\n"
" --device dev The calculator device (usually /dev/ttyUSBx).\n"
" By default, will use the first appropriate device found.\n"
" --com <port> The USB-serial port, if you want to connect to a\n"
" calculator connected (1 to 20).\n"
" If this option isn't given, then a USB calculator will\n"
" be searched.\n"
"\n"
"Type \"" QUOTE(BIN) " <subcommand> --help\" for some help about a subcommand.\n"
"Report bugs to " QUOTE(MAINTAINER) ".";
@ -75,8 +78,7 @@ FOOT;
if (help || pc != (NARGS)) { \
puts(help_##CMD); \
return (1); \
} \
}
}}
/**
* parse_args:
@ -94,7 +96,7 @@ int parse_args(int ac, char **av, args_t *args)
{
/* initialize args */
*args = (args_t){
.device = NULL,
.com = 0,
.local = NULL, .localpath = NULL
};
@ -103,7 +105,7 @@ int parse_args(int ac, char **av, args_t *args)
const struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{"device", required_argument, NULL, 'p'},
{"com", required_argument, NULL, 'c'},
{"output", required_argument, NULL, 'o'},
{NULL, 0, NULL, 0}
};
@ -111,7 +113,7 @@ int parse_args(int ac, char **av, args_t *args)
/* get all options */
int c; opterr = 0;
int help = 0, version = 0;
const char *s_out = "os.bin";
const char *s_out = "os.bin", *s_com = 0;
while ((c = getopt_long(ac, av, short_options, long_options, NULL)) != -1) {
switch (c) {
/* help */
@ -119,7 +121,7 @@ int parse_args(int ac, char **av, args_t *args)
/* version */
case 'v': version = 1; break;
/* device */
case 'p': args->device = optarg; break;
case 'c': s_com = optarg; break;
/* output */
case 'o': s_out = optarg; break;
@ -127,14 +129,27 @@ int parse_args(int ac, char **av, args_t *args)
case '?':
if (optopt == 'o')
log("-o, --output: expected an argument\n");
else if (optopt == 'p')
log("--device: expected an argument\n");
else if (optopt == 'c')
log("--com: expected an argument\n");
else
break;
return (1);
}
}
/* check com port */
if (s_com) {
if (!isdigit(s_com[0])) {
log("-c, --com: expected a number\n");
return (0);
}
args->com = atoi(s_com);
if (args->com < 1 || args->com > 20) {
log("-c, --com: COM port number should be between 1 and 20\n");
return (0);
}
}
/* check for version */
if (version) {
puts(version_message);

View File

@ -58,7 +58,7 @@ int main(int ac, char **av)
/* Initialize libp7 and communication */
p7_handle_t *handle; int err;
if (args.device) err = p7_cinit(&handle, 1, 1, args.device, 0);
if (args.com) err = p7_cominit(&handle, 1, 1, args.com);
else err = p7_init(&handle, 1, 1);
if (err) {
/* displaying error */

View File

@ -38,7 +38,7 @@ typedef struct {
menu_t menu;
/* others */
char *device;
int com;
FILE *local; const char *localpath;
} args_t;

View File

@ -9,6 +9,7 @@
/* ************************************************************************** */
#include "main.h"
#include <stdlib.h>
#include <ctype.h>
#include <getopt.h>
/* ************************************************************************** */
@ -33,8 +34,10 @@ static const char help_main[] =
"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"
" --com <port> The USB-serial port, if you want to connect to a\n"
" calculator connected (1 to 20).\n"
" If this option isn't given, then a USB calculator will\n"
" be searched.\n"
" -z ZOOM Change the zoom (1 to 16)\n"
" By default, the zoom will be " QUOTE(DEFAULT_ZOOM) ".\n"
"\n"
@ -58,10 +61,10 @@ static const char help_main[] =
* @return 0 if ok, other if not.
*/
int parse_args(int ac, char **av, const char **device, int *zoom)
int parse_args(int ac, char **av, int *com, int *zoom)
{
/* initialize args */
*device = NULL;
*com = 0;
*zoom = DEFAULT_ZOOM;
/* define options */
@ -69,7 +72,7 @@ int parse_args(int ac, char **av, const char **device, int *zoom)
const struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{"device", required_argument, NULL, 'd'},
{"com", required_argument, NULL, 'c'},
{"zoom", required_argument, NULL, 'z'},
{NULL, 0, NULL, 0}
};
@ -77,14 +80,15 @@ int parse_args(int ac, char **av, const char **device, int *zoom)
/* get all options */
int c; opterr = 0;
int help = 0, version = 0;
const char *s_com = NULL;
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;
/* com port */
case 'c': s_com = optarg; break;
/* zoom */
case 'z':
*zoom = atoi(optarg);
@ -98,14 +102,27 @@ int parse_args(int ac, char **av, const char **device, int *zoom)
case '?':
if (optopt == 'z')
log("-z, --zoom: expected an argument\n");
else if (optopt == 'd')
log("--device: expected an argument\n");
else if (optopt == 'c')
log("--com: expected an argument\n");
else
break;
return (1);
}
}
/* check com port */
if (s_com) {
if (!isdigit(s_com[0])) {
log("-c, --com: expected a number\n");
return (0);
}
*com = atoi(s_com);
if (*com < 1 || *com > 20) {
log("-c, --com: COM port number should be between 1 and 20\n");
return (0);
}
}
/* check if there is any parameter */
if (ac - optind)
help = 1;

View File

@ -122,13 +122,13 @@ static int display_callback(int w, int h, uint32_t **pixels)
int main(int ac, char **av)
{
/* parse args */
const char *device;
if (parse_args(ac, av, &device, &zoom))
int com;
if (parse_args(ac, av, &com, &zoom))
return (0);
/* Initialize libp7 */
p7_handle_t *handle; int err;
if (device) err = p7_cinit(&handle, 0, 0, device, 0);
if (com) err = p7_cominit(&handle, 0, 0, com);
else err = p7_init(&handle, 0, 0);
if (err) {
/* display error */

View File

@ -15,6 +15,6 @@
# define log(S, ...) fprintf(stderr, S, ##__VA_ARGS__)
/* all functions */
int parse_args(int ac, char **av, const char **device, int *zoom);
int parse_args(int ac, char **av, int *com, int *zoom);
#endif /* MAIN_H */