aboutsummaryrefslogtreecommitdiff
path: root/src/makeauto.c
blob: 6e8ff71c5c0824b34a64676ca7135247f9d70164 (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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "fujichat.h"
#include "common.h"
#include "features.h"

#define DRIVER_LIST_LEN (sizeof(rs232_drivers) / sizeof(rs232_drivers[0]))
static char *rs232_drivers[][2] = {
	{ NULL, "(No driver; manual setup)" },
	{ "D:BOBVERT.SER", "SIO2PC or R:Verter" },
	{ "D:ATARI850.SER", "Atari 850" },
	{ "D:PRCONN.SER", "P:R: Connection" },
};

/* APPEND_BUF_SIZE should be >= the size of the largest .SER file */
#define APPEND_BUF_SIZE 2048

void reboot(void) {
	asm("jmp $e477");
}

static char get_yesno(char *prompt, char dflt) {
	char buf[5];

	printf("%s [%c/%c]: ",
			prompt,
			(dflt ? 'Y' : 'y'),
			(dflt ? 'n' : 'N'));

	fflush(stdout);
	get_line(buf, 4);
	switch(*buf) {
		case 'y':
		case 'Y':
			return 1;

		case 'n':
		case 'N':
			return 0;

		default:
			return dflt;
	}
}

//char append_to(FILE *to, char *src) {
//	int bytes;
//	char ret;
//	static char buf[APPEND_BUF_SIZE];
//	FILE *from = fopen(src, "rb");
//
//	printf("append_to: %s\n", src);
//
//	if(!from) {
//		perror(src);
//		return 0;
//	}
//
//	while( (bytes = fread(buf, 1, APPEND_BUF_SIZE, from)) ) {
//		printf("%d bytes in, eof? %d\n", bytes, feof(from));
//		bytes = fwrite(buf, 1, bytes, to);
//		printf("%d bytes out\n", bytes);
//	}
//
//	ret = feof(from); /* 1 = no error */
//	fclose(from);
//
//	return ret;
//}

char append_to(FILE *to, char *src) {
	int bytes = 0, c;
	char ret;
	// static char buf[APPEND_BUF_SIZE];
	FILE *from = fopen(src, "rb");

	printf("append_to: %s\n", src);

	if(!from) {
		perror(src);
		return 0;
	}

	/* painfully slow, sorry! TODO: figure out why fread() returns
		extra bytes after EOF (probably because the file doesn't
		end on a sector boundary, or something equally lame) */
	while( (c = fgetc(from)) != EOF ) {
		fputc(c, to);
		++bytes;
	}

	ret = feof(from); /* 1 = no error */
	fclose(from);

	printf("bytes=%d, ret=%d\n", bytes, ret);
	return ret;
}

char append_files(char *dst, char *src1, char *src2) {
	char ret;
	FILE *dfile = fopen(dst, "wb");

	if(dfile) {
		ret = (append_to(dfile, src1) && append_to(dfile, src2));
		fclose(dfile);
		return ret;
	} else {
		perror(dst);
		return 0;
	}
}

static void rs232_driver_menu() {
	static char buf[256];
	int i;

	puts("Select your serial port type\n");
	for(i=0; i<DRIVER_LIST_LEN; ++i) {
		printf("%d: %s\n",
				i + 1,
				rs232_drivers[i][1]);
	}

	puts("\nEnter number from list, or the\ndriver filename if not listed.");
	fputs("[2]: ", stdout);
	fflush(stdout);

	do {
		i = 0;
		get_line(buf, 255);

		if(!*buf) *buf = '2';

		if(*buf == '1') {
			return;
		} else if(*buf >= '2' && *buf <= '9') {
			i = *buf - '1';
			if(i < DRIVER_LIST_LEN) {
				strcpy(buf, rs232_drivers[i][0]);
				i = 0;
			} else {
				putchar(A_BEL);
				i = 1;
			}
		} /* else use whatever they entered as a filename */
	} while(i);

	// if(!rename("D:AUTORUN.SYS", "D:AUTORUN.BAK")) {
		// puts("Renamed AUTORUN.SYS to AUTORUN.BAK");
	// }

	do {
		printf("\nCreating AUTORUN.SYS from\n%s and LOADMENU.COM\n", buf);
		i = !append_files("D:AUTORUN.SYS", buf, "D:LOADMENU.COM");
		if(i) {
			i = get_yesno("\xfdRetry", 1);
			if(!i) {
				puts("Failed to change driver");
				return;
			}
		}
	} while(i);

	puts("You must reboot to use the new driver.");
	if(get_yesno("Reboot now", 1))
		reboot();

	// rs232_already_loaded = scan_hatabs('R');
}

void main(int, char **) {
	rs232_driver_menu();
	exit(0);
}