summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/stdio-frontend.c
blob: ec448224aae2c3e2104de850cf55c14761780fc6 (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
208
209
210
211
212
/*
 * Guillaume Cottenceau (gc@mandrakesoft.com)
 *
 * Copyright 2000 MandrakeSoft
 *
 * This software may be freely redistributed under the terms of the GNU
 * public license.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/*
 * Portions from Erik Troan (ewt@redhat.com)
 *
 * Copyright 1996 Red Hat Software 
 *
 */


/*
 * Each different frontend must implement all functions defined in frontend.h
 */


#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <fcntl.h>
#include "stage1.h"
#include "log.h"
#include "newt.h"

#include "frontend.h"


void init_frontend(void)
{
	printf("Welcome to Linux-Mandrake (" VERSION ") " __DATE__ " " __TIME__ "\n");
}


void finish_frontend(void)
{
}

static void get_any_response(void)
{
	unsigned char t;
	fflush(stdout);
	read(0, &t, 1);
	fcntl(0, F_SETFL, O_NONBLOCK);
	while (read(0, &t, 1) > 0);
	fcntl(0, F_SETFL, 0);
}
	
static int get_int_response(void)
{
	int i = 0; /* (0) tied to Cancel */
	fflush(stdout);
	scanf(" %d", &i);
	return i;
}

static void blocking_msg(char *type, char *fmt, va_list args)
{
	printf(type);
	vprintf(fmt, args);
	get_any_response();
	printf("\n");
}

void error_message(char *msg, ...)
{
	va_list args;
	va_start(args, msg);
	va_end(args);
	blocking_msg("> Error! ", msg, args);
}

void info_message(char *msg, ...)
{
	va_list args;
	va_start(args, msg);
	va_end(args);
	blocking_msg("> Info! ", msg, args);
}

void wait_message(char *msg, ...)
{
	va_list args;
	printf("Please wait: ");
	va_start(args, msg);
	vprintf(msg, args);
	va_end(args);
	fflush(stdout);
}

void remove_wait_message(void)
{
	printf("\n");
}


static int size_progress;
static int actually_drawn;

void init_progression(char *msg, int size)
{
	size_progress = size;
	actually_drawn = 0;
	printf("%s\n[", msg);
}

void update_progression(int current_size)
{
	while ((int)((current_size*40)/size_progress) > actually_drawn) {
		printf(".");
		actually_drawn++;
		if (actually_drawn == 10)
			printf("(25%%)");
		if (actually_drawn == 20)
			printf("(50%%)");
		if (actually_drawn == 30)
			printf("(75%%)");
	}
	fflush(stdout);
}

void end_progression(void)
{
	printf("]\n");
}


enum return_type ask_from_list_comments(char *msg, char ** elems, char ** elems_comments, char ** choice)
{
	char ** sav_elems = elems;
	int i, j;

	printf("> %s\n(0) Cancel\n", msg);
	i = 1;
	while (elems && *elems) {
		printf("(%d) %s (%s)\n", i, *elems, *elems_comments);
		i++;
		elems++;
		elems_comments++;
	}

	printf("? ");

	j = get_int_response();

	if (j == 0)
		return RETURN_BACK;

	if (j >= 1 && j < i) {
		*choice = strdup(sav_elems[j-1]);
		return RETURN_OK;
	}

	return RETURN_ERROR;
}


enum return_type ask_from_list(char *msg, char ** elems, char ** choice)
{
	char ** sav_elems = elems;
	int i, j;

	printf("> %s\n(0) Cancel\n", msg);
	i = 1;
	while (elems && *elems) {
		printf("(%d) %s\n", i, *elems);
		i++;
		elems++;
	}

	printf("? "); 

	j = get_int_response();

	if (j == 0)
		return RETURN_BACK;

	if (j >= 1 && j < i) {
		*choice = strdup(sav_elems[j-1]);
		return RETURN_OK;
	}

	return RETURN_ERROR;
}


enum return_type ask_yes_no(char *msg)
{
	int j;

	printf("> %s\n(0) Yes\n(1) No\n(2) Back\n? ", msg);

	j = get_int_response();

	if (j == 0)
		return RETURN_OK;
	else if (j == 2)
		return RETURN_BACK;
	else return RETURN_ERROR;
}