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/p7os/main.c

121 lines
3.9 KiB
C

/* ************************************************************************** */
/* _____ _ */
/* p7os/main.c |_ _|__ _ _| |__ ___ _ _ */
/* | Project: p7utils | |/ _ \| | | | '_ \ / _ \ | | | */
/* | | (_) | |_| | | | | __/ |_| | */
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
/* Last updated: 2017/01/16 23:55:54 |___/ */
/* */
/* ************************************************************************** */
#include "main.h"
#include <unistd.h>
/* ************************************************************************** */
/* Error messages */
/* ************************************************************************** */
/* Couldn't initialize connexion to calculator. */
static const char error_noconnexion[] =
"Could not connect to the calculator.\n"
"- Is it plugged in and in Receive mode/OS Update?\n"
"- Have you tried changing the cable ?\n";
/* Calculator was found but program wasn't allowed to communicate with it. */
static const char error_noaccess[] =
"Could not get access to the calculator.\n"
"Install the appropriate udev rule, or run as root.\n";
/* The calculator acted in a weird way. */
static const char error_unplanned[] =
"The calculator didn't act as planned: %s.\n"
"Stop receive mode on calculator and start it again before re-running " \
QUOTE(BIN) ".\n";
/* Unsupported operation -> OS Update, not receive mode! */
static const char error_unsupported[] =
"Required operation was unsupported.\n"
"- If you were using anything else than the *prepare* subcommand, are you\n"
" sure you've run *prepare* before?\n"
"- Otherwise, I gotta be honest, I'd like to know how you've managed that.\n";
/* ************************************************************************** */
/* Main function */
/* ************************************************************************** */
/**
* main:
* User entry point of the program.
*
* @arg ac arguments count
* @arg av arguments values
* @return return code (0 if OK)
*/
int main(int ac, char **av)
{
/* parse args */
args_t args;
if (parse_args(ac, av, &args))
return (0);
/* Initialize libp7 and communication */
p7_handle_t *handle = NULL; int err;
if (args.com) err = p7_cominit(&handle, P7_ACTIVE | P7_CHECK, args.com);
else err = p7_init(&handle, P7_ACTIVE | P7_CHECK);
if (err) goto init_error;
/* prepare */
if (!args.noprepare) {
/* make the preparation thing */
if (prepare_ops(handle, args.uexe)) {
if (args.local) fclose(args.local);
return (1);
}
handle = NULL;
/* was only about preparing? */
if (args.menu == mn_prepare_only)
return (0);
/* sleep a little, in case */
sleep(1);
/* re-open the handle */
if (args.com) err = p7_cominit(&handle, P7_ACTIVE | P7_CHECK, args.com);
else err = p7_init(&handle, P7_ACTIVE | P7_CHECK);
if (err) goto init_error;
}
/* check according to menu */
switch (args.menu) {
/* backup the thing menu */
case mn_get:
/* get the os */
fprintf(stderr, "Unimplemented, yet.\n"); err = 1;
fclose(args.local);
/* if there was an error, delete created file */
if (err) remove(args.localpath);
break;
}
/* exit libp7 */
p7_exit(handle, 1);
/* then we're good */
return (0);
init_error:
/* displaying error */
switch (err) {
case p7_error_nocalc: log(error_noconnexion); break;
case p7_error_noaccess: log(error_noaccess); break;
case p7_error_unsupported: log(error_unsupported); break;
default: log(error_unplanned, p7_strerror(err));
}
/* closing, removing if necessary */
if (args.localpath) {
fclose(args.local);
remove(args.localpath);
}
return (1);
}