Skip to content

Latest commit

 

History

History
170 lines (130 loc) · 3.53 KB

ui_stdio.c

File metadata and controls

170 lines (130 loc) · 3.53 KB
 
Mar 28, 2008
Mar 28, 2008
1
2
3
4
5
6
7
/**
* MojoPatch; a tool for updating data in the field.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
Jan 5, 2004
Jan 5, 2004
8
9
#include <stdio.h>
Apr 22, 2005
Apr 22, 2005
10
11
#include <stdlib.h>
#include <string.h>
Jul 14, 2004
Jul 14, 2004
12
#include <ctype.h>
Jan 5, 2004
Jan 5, 2004
13
14
15
16
#include "platform.h"
#include "ui.h"
Apr 22, 2005
Apr 22, 2005
17
static void ui_title_stdio(const char *str)
Jan 5, 2004
Jan 5, 2004
18
{
Apr 22, 2005
Apr 22, 2005
19
20
printf("=== %s ===\n\n", str);
} /* ui_title_stdio */
Jan 5, 2004
Jan 5, 2004
21
22
Apr 22, 2005
Apr 22, 2005
23
static void ui_real_deinit_stdio(void)
Jan 5, 2004
Jan 5, 2004
24
25
26
{
printf("\n\nHit enter to quit.\n\n");
getchar();
Apr 22, 2005
Apr 22, 2005
27
} /* ui_deinit_stdio */
Jan 5, 2004
Jan 5, 2004
28
29
Apr 22, 2005
Apr 22, 2005
30
static void ui_pump_stdio(void)
Jan 5, 2004
Jan 5, 2004
31
32
{
/* no-op. */
Apr 22, 2005
Apr 22, 2005
33
} /* ui_pump_stdio */
Jan 5, 2004
Jan 5, 2004
34
35
Apr 22, 2005
Apr 22, 2005
36
static void ui_add_to_log_stdio(const char *str, int debugging)
Jan 5, 2004
Jan 5, 2004
37
38
{
printf("%s%s\n", debugging ? "debug: " : "", str);
Apr 22, 2005
Apr 22, 2005
39
} /* ui_add_to_log_stdio */
Jan 5, 2004
Jan 5, 2004
40
41
Apr 22, 2005
Apr 22, 2005
42
static void ui_fatal_stdio(const char *str)
Jan 5, 2004
Jan 5, 2004
43
44
{
fprintf(stderr, "\n%s\n\n", str);
Apr 22, 2005
Apr 22, 2005
45
} /* ui_fatal_stdio */
Jan 5, 2004
Jan 5, 2004
46
47
Apr 22, 2005
Apr 22, 2005
48
static void ui_success_stdio(const char *str)
Jan 5, 2004
Jan 5, 2004
49
50
{
fprintf(stderr, "\n%s\n\n", str);
Apr 22, 2005
Apr 22, 2005
51
} /* ui_success_stdio */
Jan 5, 2004
Jan 5, 2004
52
53
Apr 22, 2005
Apr 22, 2005
54
static void ui_total_progress_stdio(int percent)
Jan 5, 2004
Jan 5, 2004
55
56
57
58
59
60
61
62
63
{
static int lastpercent = -1;
if (percent != lastpercent)
{
lastpercent = percent;
printf(".");
if (percent == 100)
printf("\n");
} /* if */
Apr 22, 2005
Apr 22, 2005
64
} /* ui_total_progress_stdio */
Jan 5, 2004
Jan 5, 2004
65
66
Apr 22, 2005
Apr 22, 2005
67
static void ui_status_stdio(const char *str)
Jan 5, 2004
Jan 5, 2004
68
{
Jul 14, 2004
Jul 14, 2004
69
printf("Current operation: %s\n", str);
Apr 22, 2005
Apr 22, 2005
70
} /* ui_status_stdio */
Jan 5, 2004
Jan 5, 2004
71
Jul 14, 2004
Jul 14, 2004
72
Apr 22, 2005
Apr 22, 2005
73
static int ui_prompt_yn_stdio(const char *question)
Jul 14, 2004
Jul 14, 2004
74
75
76
77
78
79
80
81
82
83
84
85
86
87
{
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
88
} /* ui_prompt_yn_stdio */
Jul 14, 2004
Jul 14, 2004
89
90
Apr 22, 2005
Apr 22, 2005
91
static int ui_prompt_ny_stdio(const char *question)
Jul 14, 2004
Jul 14, 2004
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{
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
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
} /* 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
168
Apr 22, 2005
Apr 22, 2005
169
/* end of ui_stdio.c ... */