Skip to content

Latest commit

 

History

History
163 lines (123 loc) · 3.36 KB

ui_stdio.c

File metadata and controls

163 lines (123 loc) · 3.36 KB
 
Jan 5, 2004
Jan 5, 2004
1
2
#include <stdio.h>
Apr 22, 2005
Apr 22, 2005
3
4
#include <stdlib.h>
#include <string.h>
Jul 14, 2004
Jul 14, 2004
5
#include <ctype.h>
Jan 5, 2004
Jan 5, 2004
6
7
8
9
#include "platform.h"
#include "ui.h"
Apr 22, 2005
Apr 22, 2005
10
static void ui_title_stdio(const char *str)
Jan 5, 2004
Jan 5, 2004
11
{
Apr 22, 2005
Apr 22, 2005
12
13
printf("=== %s ===\n\n", str);
} /* ui_title_stdio */
Jan 5, 2004
Jan 5, 2004
14
15
Apr 22, 2005
Apr 22, 2005
16
static void ui_real_deinit_stdio(void)
Jan 5, 2004
Jan 5, 2004
17
18
19
{
printf("\n\nHit enter to quit.\n\n");
getchar();
Apr 22, 2005
Apr 22, 2005
20
} /* ui_deinit_stdio */
Jan 5, 2004
Jan 5, 2004
21
22
Apr 22, 2005
Apr 22, 2005
23
static void ui_pump_stdio(void)
Jan 5, 2004
Jan 5, 2004
24
25
{
/* no-op. */
Apr 22, 2005
Apr 22, 2005
26
} /* ui_pump_stdio */
Jan 5, 2004
Jan 5, 2004
27
28
Apr 22, 2005
Apr 22, 2005
29
static void ui_add_to_log_stdio(const char *str, int debugging)
Jan 5, 2004
Jan 5, 2004
30
31
{
printf("%s%s\n", debugging ? "debug: " : "", str);
Apr 22, 2005
Apr 22, 2005
32
} /* ui_add_to_log_stdio */
Jan 5, 2004
Jan 5, 2004
33
34
Apr 22, 2005
Apr 22, 2005
35
static void ui_fatal_stdio(const char *str)
Jan 5, 2004
Jan 5, 2004
36
37
{
fprintf(stderr, "\n%s\n\n", str);
Apr 22, 2005
Apr 22, 2005
38
} /* ui_fatal_stdio */
Jan 5, 2004
Jan 5, 2004
39
40
Apr 22, 2005
Apr 22, 2005
41
static void ui_success_stdio(const char *str)
Jan 5, 2004
Jan 5, 2004
42
43
{
fprintf(stderr, "\n%s\n\n", str);
Apr 22, 2005
Apr 22, 2005
44
} /* ui_success_stdio */
Jan 5, 2004
Jan 5, 2004
45
46
Apr 22, 2005
Apr 22, 2005
47
static void ui_total_progress_stdio(int percent)
Jan 5, 2004
Jan 5, 2004
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 */
Apr 22, 2005
Apr 22, 2005
57
} /* ui_total_progress_stdio */
Jan 5, 2004
Jan 5, 2004
58
59
Apr 22, 2005
Apr 22, 2005
60
static void ui_status_stdio(const char *str)
Jan 5, 2004
Jan 5, 2004
61
{
Jul 14, 2004
Jul 14, 2004
62
printf("Current operation: %s\n", str);
Apr 22, 2005
Apr 22, 2005
63
} /* ui_status_stdio */
Jan 5, 2004
Jan 5, 2004
64
Jul 14, 2004
Jul 14, 2004
65
Apr 22, 2005
Apr 22, 2005
66
static int ui_prompt_yn_stdio(const char *question)
Jul 14, 2004
Jul 14, 2004
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);
Apr 22, 2005
Apr 22, 2005
81
} /* ui_prompt_yn_stdio */
Jul 14, 2004
Jul 14, 2004
82
83
Apr 22, 2005
Apr 22, 2005
84
static int ui_prompt_ny_stdio(const char *question)
Jul 14, 2004
Jul 14, 2004
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);
Apr 22, 2005
Apr 22, 2005
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 */
Jul 14, 2004
Jul 14, 2004
161
Apr 22, 2005
Apr 22, 2005
162
/* end of ui_stdio.c ... */