aboutsummaryrefslogtreecommitdiff
path: root/src/fake86/console.c
blob: 03edebbd7e4a84fa578a79cde40b374ee39b32f3 (plain) (blame)
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
/*
  Fake86: A portable, open-source 8086 PC emulator.
  Copyright (C)2010-2013 Mike Chambers

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/* console.c: functions for a simple interactive console on stdio. */

#include "config.h"
#include <SDL/SDL.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#ifdef _WIN32
#include <conio.h>
#define strcmpi _strcmpi
#else
#define strcmpi strcasecmp
#endif

uint8_t inputline[1024];
uint16_t inputptr = 0;
extern uint8_t running;

extern uint8_t insertdisk (uint8_t drivenum, char *filename);
extern void ejectdisk (uint8_t drivenum);

void waitforcmd (uint8_t *dst, uint16_t maxlen) {
#ifdef _WIN32
	uint16_t inputptr;
	uint8_t cc;

	inputptr = 0;
	maxlen -= 2;
	inputline[0] = 0;
	while (running) {
			if (_kbhit () ) {
					cc = (uint8_t) _getch ();
					switch (cc) {
							case 0:
							case 9:
							case 10:
								break;
							case 8: //backspace
								if (inputptr > 0) {
										printf ("%c %c", 8, 8);
										inputline[--inputptr] = 0;
									}
								break;
							case 13: //enter
								printf ("\n");
								return;
							default:
								if (inputptr < maxlen) {
										inputline[inputptr++] = cc;
										inputline[inputptr] = 0;
										printf ("%c",cc);
									}
						}
				}
			SDL_Delay(10); //don't waste CPU time while in the polling loop
		}
#else
	gets (dst);
#endif
}

void consolehelp () {
	printf ("\nConsole command summary:\n");
	printf ("  The console is not very robust yet. There are only a few commands:\n\n");
	printf ("    change fd0        Mount a new image file on first floppy drive.\n");
	printf ("                      Entering a blank line just ejects any current image file.\n");
	printf ("    change fd1        Mount a new image file on first floppy drive.\n");
	printf ("                      Entering a blank line just ejects any current image file.\n");
	printf ("    help              This help display.\n");
	printf ("    quit              Immediately abort emulation and close Fake86.\n");
}

#ifdef _WIN32
void runconsole (void *dummy) {
#else
void *runconsole (void *dummy) {
#endif
	printf ("\nFake86 management console\n");
	printf ("Type \"help\" for a summary of commands.\n");
	while (running) {
			printf ("\n>");
			waitforcmd (inputline, sizeof(inputline) );
			if (strcmpi ( (const char *) inputline, "change fd0") == 0) {
					printf ("Path to new image file: ");
					waitforcmd (inputline, sizeof(inputline) );
					if (strlen (inputline) > 0) {
							insertdisk (0, (char *) inputline);
						}
					else {
							ejectdisk (0);
							printf ("Floppy image ejected from first drive.\n");
						}
				}
			else if (strcmpi ( (const char *) inputline, "change fd1") == 0) {
					printf ("Path to new image file: ");
					waitforcmd (inputline, sizeof(inputline) );
					if (strlen (inputline) > 0) {
							insertdisk (1, (char *) inputline);
						}
					else {
							ejectdisk (1);
							printf ("Floppy image ejected from second drive.\n");
						}
				}
			else if (strcmpi ( (const char *) inputline, "help") == 0) {
					consolehelp ();
				}
			else if (strcmpi ( (const char *) inputline, "quit") == 0) {
					running = 0;
				}
			else printf("Invalid command was entered.\n");
		}
}