aboutsummaryrefslogtreecommitdiffstats
path: root/sisinstall/sismain.cpp
blob: c845408f2c1f0d9025d729ae110b15512e63aa63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "sisfile.h"
#include "sisinstaller.h"
#include "psion.h"
#include "fakepsion.h"

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#if HAVE_LIBNEWT
# include <newt.h>
#endif

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <getopt.h>

bool usenewt = false;

static void error(int line)
{
#if HAVE_LIBNEWT
	if (usenewt)
		{
		newtPopHelpLine();
		newtPushHelpLine(_("Got an error, Press any key to exit."));
		newtWaitForKey();
		newtFinished();
		}
#endif
	fprintf(stderr, _("Error %d on line %d: %s\n"), errno, line,
		strerror(errno));
	exit(1);
}

static struct option opts[] = {
	{ "help",     no_argument,       0, 'h' },
	{ "version",  no_argument,       0, 'V' },
	{ "verbose", required_argument, 0, 'v' },
	{ "dry-run",  no_argument,       0, 'n' },
#if HAVE_LIBNEWT
	{ "newt",     no_argument,       0, 'w' },
#endif
	{ NULL,       0,                 0, 0 },
};

void printHelp()
{
	printf(
	_("Usage: sisinstall [OPTIONS]... SISFILE\n"
	"\n"
	"Supported options:\n"
	"\n"
	" -h, --help              Display this text.\n"
	" -V, --version           Print version and exit.\n"
	" -v, --verbose=LEVEL     Set the verbosity level, by default 0.\n"
	" -n, --dry-run           Just parse the file.\n"
#if HAVE_LIBNEWT
	" -w, --newt              Use the Newt interface.\n"
#endif
	));
}

int main(int argc, char* argv[])
{
	char* filename = 0;
	char option;
	bool dryrun = false;

#ifdef LC_ALL
	setlocale(LC_ALL, "");
#endif
	textdomain(PACKAGE);

	while (1)
		{
		option = getopt_long(argc, argv,
#if HAVE_LIBNEWT
							 "hnv:Vw"
#else
							 "hnv:V"
#endif
							 , opts, NULL);
		if (option == -1)
			break;
		switch (option)
			{
			case 'h':
			case '?':
				printHelp();
				exit(0);
			case 'v':
				logLevel = atoi(optarg);
				break;
			case 'n':
				dryrun = true;
				break;
#if HAVE_LIBNEWT
			case 'w':
				usenewt = true;
				break;
#endif
			case 'V':
				printf(_("sisinstall version 0.1\n"));
				exit(0);
			}
		}
#if HAVE_LIBNEWT
	if (usenewt)
		{
		newtInit();
		newtCls();
		}
#endif
	if (optind < argc)
	{
		filename = argv[optind];
#if HAVE_LIBNEWT
		if (usenewt)
			{
			char helpline[256];
			sprintf(helpline,
					_("Installing sis file %s%s.\n"),
					filename,
				   dryrun ? _(", not really") : "");
			newtPushHelpLine(helpline);
//			newtWaitForKey();
			}
		else
#endif
			printf(_("Installing sis file %s%s.\n"), filename,
				   dryrun ? _(", not really") : "");
	}
	else
	{
#if HAVE_LIBNEWT
	    if (usenewt)
		newtFinished();
#endif
	    fprintf(stderr, _("Missing SIS filename\n"));
	    exit(1);
	}
	struct stat st;
	if (-1 == stat(filename, &st))
		error(__LINE__);
	off_t len = st.st_size;
	if (logLevel >= 2)
		printf(_("File is %d bytes long\n"), len);
	uint8_t* buf = new uint8_t[len];
	int fd = open(filename, O_RDONLY);
	if (-1 == fd)
		error(__LINE__);
	if (-1 == read(fd, buf, len))
		error(__LINE__);
	close(fd);
	Psion* psion;
	if (dryrun)
		psion = new FakePsion();
	else
		psion = new Psion();
	if (!psion->connect())
		{
		printf(_("Couldn't connect with the Psion\n"));
		}
	else
		{
		createCRCTable();
		SISFile sisFile;
		SisRC rc = sisFile.fillFrom(buf, len);
		if (rc == SIS_OK)
			{
//			if (!dryrun)
				{
				SISInstaller installer;
				installer.setPsion(psion);
#if HAVE_LIBNEWT
				installer.useNewt(usenewt);
#endif
				installer.run(&sisFile, buf, len);
				}
			}
		else
			{
			printf(_("Could not parse the sis file.\n"));
			}
		psion->disconnect();
		}

#if HAVE_LIBNEWT
	if (usenewt)
		{
		newtPopHelpLine();
		newtPushHelpLine(_("Installation complete. Press any key to exit."));
		newtWaitForKey();
		newtFinished();
		}
#endif
	return 0;
}