/
ui_stdio.c
163 lines (123 loc) · 3.36 KB
1
2
#include <stdio.h>
3
4
#include <stdlib.h>
#include <string.h>
5
#include <ctype.h>
6
7
8
9
#include "platform.h"
#include "ui.h"
10
static void ui_title_stdio(const char *str)
11
{
12
13
printf("=== %s ===\n\n", str);
} /* ui_title_stdio */
14
15
16
static void ui_real_deinit_stdio(void)
17
18
19
{
printf("\n\nHit enter to quit.\n\n");
getchar();
20
} /* ui_deinit_stdio */
21
22
23
static void ui_pump_stdio(void)
24
25
{
/* no-op. */
26
} /* ui_pump_stdio */
27
28
29
static void ui_add_to_log_stdio(const char *str, int debugging)
30
31
{
printf("%s%s\n", debugging ? "debug: " : "", str);
32
} /* ui_add_to_log_stdio */
33
34
35
static void ui_fatal_stdio(const char *str)
36
37
{
fprintf(stderr, "\n%s\n\n", str);
38
} /* ui_fatal_stdio */
39
40
41
static void ui_success_stdio(const char *str)
42
43
{
fprintf(stderr, "\n%s\n\n", str);
44
} /* ui_success_stdio */
45
46
47
static void ui_total_progress_stdio(int percent)
48
49
50
51
52
53
54
55
56
{
static int lastpercent = -1;
if (percent != lastpercent)
{
lastpercent = percent;
printf(".");
if (percent == 100)
printf("\n");
} /* if */
57
} /* ui_total_progress_stdio */
58
59
60
static void ui_status_stdio(const char *str)
61
{
62
printf("Current operation: %s\n", str);
63
} /* ui_status_stdio */
64
65
66
static int ui_prompt_yn_stdio(const char *question)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
{
int c;
while (1)
{
printf("%s", question);
c = toupper(getchar());
if (c == 'N')
return(0);
else if ((c == 'Y') || (c == '\r') || (c == '\n'))
return(1);
printf("\n");
} /* while */
return(1);
81
} /* ui_prompt_yn_stdio */
82
83
84
static int ui_prompt_ny_stdio(const char *question)
85
86
87
88
89
90
91
92
93
94
95
96
97
98
{
int c;
while (1)
{
printf("%s", question);
c = toupper(getchar());
if (c == 'Y')
return 1;
else if ((c == 'N') || (c == '\r') || (c == '\n'))
return 0;
printf("\n");
} /* while */
return(0);
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
} /* ui_prompt_ny_stdio */
static void ui_msgbox_stdio(const char *str)
{
printf("\n\n-----\n%s\n-----\n\nHit enter to continue.\n\n", str);
getchar();
} /* ui_msgbox_stdio */
static int ui_file_picker_stdio(char *buf, size_t bufsize)
{
while (1)
{
size_t len;
puts("Please enter the path, or blank to cancel.\n> ");
buf[0] = '\0';
fgets(buf, bufsize, stdin);
len = strlen(buf);
while ((len > 0) && ((buf[len-1] == '\n') || (buf[len-1] == '\r')))
buf[--len] = '\0';
if (len == 0)
return(0); /* user "cancelled". */
if (file_exists(buf))
return(1); /* user entered valid path. */
puts("That path does not exist. Please try again.\n\n");
} /* while */
return(0); /* should never hit this. */
} /* ui_file_picker_stdio */
static int ui_show_readme_stdio(const char *fname,const char *text)
{
/*
* Cheat for now and push this off to "less", which will warn if the
* readme is a binary file and prompt the user about avoiding it.
*/
size_t allocsize = strlen(fname) + 32;
char *cmd = (char *) alloca(allocsize);
if (!cmd)
{
_fatal("Out of memory.");
return(0);
} /* if */
snprintf(cmd, allocsize, "less %s", fname);
system(cmd); /* !!! FIXME: error check? */
return(1);
} /* ui_show_readme_stdio */
int ui_init_stdio(void)
{
UI_SET_FUNC_POINTERS(stdio);
return(1); /* always succeeds. */
} /* ui_init */
161
162
/* end of ui_stdio.c ... */