plptools
Loading...
Searching...
No Matches
sismain.cpp
Go to the documentation of this file.
1/*
2 * This file is part of plptools.
3 *
4 * Copyright (C) 2002 Daniel Brahneborg <basic@chello.se>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
18 *
19 */
20
21#include "config.h"
22
23#include "sisfile.h"
24#include "sisinstaller.h"
25#include "psion.h"
26#include "fakepsion.h"
27
28#include <cstdint>
29#include <errno.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <stdint.h>
33#include <stdlib.h>
34#include <stdio.h>
35#include <string.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38
39#ifndef _GNU_SOURCE
40#define _GNU_SOURCE
41#endif
42#include <getopt.h>
43
44static void error(int line) {
45 fprintf(stderr, _("Error %d on line %d: %s\n"), errno, line, strerror(errno));
46 exit(1);
47}
48
49static struct option opts[] = {
50 { "help", no_argument, nullptr, 'h' },
51 { "version", no_argument, nullptr, 'V' },
52 { "verbose", required_argument, nullptr, 'v' },
53 { "dry-run", no_argument, nullptr, 'n' },
54 { NULL, 0, nullptr, 0 },
55};
56
57void printHelp() {
58 printf("%s",
59 _("Usage: sisinstall [OPTIONS]... SISFILE\n"
60 "\n"
61 "Supported options:\n"
62 "\n"
63 " -h, --help Display this text.\n"
64 " -V, --version Print version and exit.\n"
65 " -v, --verbose=LEVEL Set the verbosity level, by default 0.\n"
66 " -n, --dry-run Just parse the file.\n"));
67}
68
69int main(int argc, char* argv[]) {
70 char* filename = nullptr;
71 char option;
72 bool dryrun = false;
73
74#ifdef LC_ALL
75 setlocale(LC_ALL, "");
76#endif
77 textdomain(PACKAGE);
78
79 while (1) {
80 option = getopt_long(argc, argv, "hnv:V", opts, NULL);
81 if (option == -1) {
82 break;
83 }
84 switch (option) {
85 case 'h':
86 case '?':
87 printHelp();
88 exit(0);
89 case 'v':
90 logLevel = atoi(optarg);
91 break;
92 case 'n':
93 dryrun = true;
94 break;
95 case 'V':
96 printf("%s %s\n", _("sisinstall Version"), VERSION);
97 exit(0);
98 }
99 }
100 if (optind < argc) {
101 filename = argv[optind];
102 printf(_("Installing sis file %s%s.\n"), filename, dryrun ? _(", not really") : "");
103 } else {
104 fprintf(stderr, "%s", _("Missing SIS filename\n"));
105 exit(1);
106 }
107 struct stat st;
108 if (-1 == stat(filename, &st)) {
109 error(__LINE__);
110 }
111 off_t len = st.st_size;
112 if (logLevel >= 2) {
113 printf(_("File is %jd bytes long\n"), static_cast<intmax_t>(len));
114 }
115 uint8_t* buf = new uint8_t[len];
116 int fd = open(filename, O_RDONLY);
117 if (-1 == fd) {
118 error(__LINE__);
119 }
120 if (-1 == read(fd, buf, len)) {
121 error(__LINE__);
122 }
123 close(fd);
124 Psion* psion;
125 if (dryrun) {
126 psion = new FakePsion();
127 } else {
128 psion = new Psion();
129 }
130 if (!psion->connect()) {
131 printf("%s", _("Couldn't connect with the Psion\n"));
132 } else {
134 SISFile sisFile;
135 SisRC rc = sisFile.fillFrom(buf, len);
136 if (rc == SIS_OK) {
137 SISInstaller installer;
138 installer.setPsion(psion);
139 installer.run(&sisFile, buf, len);
140 } else {
141 printf("%s", _("Could not parse the sis file.\n"));
142 }
143 psion->disconnect();
144 }
145
146 return 0;
147}
A dummy version of the Psion proxy, mainly for testing the installer.
Definition: fakepsion.h:27
Semi smart proxy for communicating with a Psion.
Definition: psion.h:33
virtual void disconnect()
Definition: psion.cpp:91
virtual bool connect()
Definition: psion.cpp:44
The top level container of a SIS file.
Definition: sisfile.h:34
SisRC fillFrom(uint8_t *buf, off_t len)
Populate the fields.
Definition: sisfile.cpp:48
A minimal SIS installer.
Definition: sisinstaller.h:36
void setPsion(Psion *psion)
Set the Psion manager.
SisRC run(SISFile *file, uint8_t *buf, off_t len)
#define _(String)
Definition: plpintl.h:34
#define textdomain(Domain)
Definition: plpintl.h:36
void printHelp()
Definition: sismain.cpp:57
int main(int argc, char *argv[])
Definition: sismain.cpp:69
static void error(int line)
Definition: sismain.cpp:44
static struct option opts[]
Definition: sismain.cpp:49
void createCRCTable()
Definition: sistypes.cpp:27
int logLevel
Definition: sistypes.cpp:25
SisRC
Return Codes.
Definition: sistypes.h:27
@ SIS_OK
Definition: sistypes.h:28