aboutsummaryrefslogtreecommitdiffstats
path: root/sisinstall/sismain.cpp
blob: 8516b0f5c76e47dc791996a72948db6c05da7d55 (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
#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>

static void error(int line)
{
	fprintf(stderr, "Got errno = %d on line %d\n", errno, line);
	exit(1);
}

void main(int argc, char* argv[])
{
	char* filename = 0;
	char option;
	bool dryrun = false;
	while ((option = getopt(argc, argv, "nl:")) != -1)
		{
		switch (option)
			{
			case 'l':
				logLevel = atoi(optarg);
				break;
			case 'n':
				dryrun = true;
				break;
			}
		}
	if (optind < argc)
		{
		filename = argv[optind];
		printf("Installing sis file %s%s\n", filename,
			   dryrun ? ", not really" : "");
		}
	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);
	uchar* buf = new uchar[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");
		exit(1);
		}
	createCRCTable();
	SISFile sisFile;
	sisFile.fillFrom(buf);
	if (!dryrun)
		{
		SISInstaller installer;
		installer.setPsion(psion);
		installer.run(&sisFile, buf);
		}
	psion->disconnect();

	exit(0);
}