-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibcon.cpp
More file actions
177 lines (160 loc) · 3.82 KB
/
libcon.cpp
File metadata and controls
177 lines (160 loc) · 3.82 KB
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
#undef printf//I compile Asar with -Dprintf=DO_NOT_USE -Dputs=DO_NOT_USE to make sure that no debug codes sneak past. They're legitime in here, though.
#undef puts
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "libcon.h"
#include <signal.h>
static char * progname;
static char ** args;
static int argsleft;
bool libcon_interactive;
static const char * usage;
static volatile bool confirmclose=true;
void libcon_pause()
{
if (confirmclose)
{
confirmclose=false;
#if defined(_WIN32)
system("pause");
#else
printf("Press Enter to continue");
getchar();
#endif
confirmclose=true;
}
}
void libcon_badusage()
{
printf("usage: %s %s", progname, usage);
exit(1);
}
static const char * getarg(bool tellusage, const char * defval=NULL)
{
if (!argsleft)
{
if (tellusage) libcon_badusage();
return defval;
}
args++;
argsleft--;
return args[0];
}
static const char * getfname(bool tellusage, const char * defval=NULL)
{
return getarg(tellusage, defval);
//char * rval=malloc(char, 256);
//char * rvalend=rval;
//*rval=0;
//while (!strchr(rval, '.'))
//{
// char * thisword=getarg(false, NULL);
// if (!thisword)
// {
// if (tellusage) libcon_badusage();
// else return defval;
// }
// if (rval!=rvalend) *(rvalend++)=' ';
// rvalend+=sprintf(rvalend, "%s", thisword);
//}
//return rval;
}
static const char * requirestrfromuser(const char * question, bool filename)
{
confirmclose=false;
char * rval=(char*)malloc(256);
*rval=0;
while (!strchr(rval, '\n') || *rval=='\n')
{
*rval=0;
printf("%s ", question);
(void)fgets(rval, 250, stdin);
}
*strchr(rval, '\n')=0;
confirmclose=true;
#ifdef _WIN32
if (filename && rval[0]=='"' && rval[2]==':')
{
char * rvalend=strchr(rval, '\0');
if (rvalend[-1]=='"') rvalend[-1]='\0';
return rval+1;
}
#endif
return rval;
}
static const char * requeststrfromuser(const char * question, bool filename, const char * defval)
{
confirmclose=false;
char * rval=(char*)malloc(256);
*rval=0;
printf("%s ", question);
(void)fgets(rval, 250, stdin);
*strchr(rval, '\n')=0;
confirmclose=true;
if (!*rval) return defval;
#ifdef _WIN32
if (filename && rval[0]=='"' && rval[2]==':')
{
char * rvalend=strchr(rval, '\0');
if (rvalend[-1]=='"') rvalend[-1]='\0';
return rval+1;
}
#endif
return rval;
}
void libcon_init(int argc, char ** argv, const char * usage_)
{
progname=argv[0];
args=argv;
argsleft=argc-1;
usage=usage_;
libcon_interactive=(!argsleft);
#if defined(_WIN32)
if (libcon_interactive) atexit(libcon_pause);
#endif
}
const char * libcon_require(const char * desc)
{
if (libcon_interactive) return requirestrfromuser(desc, false);
else return getarg(true);
}
const char * libcon_require_filename(const char * desc)
{
if (libcon_interactive) return requirestrfromuser(desc, true);
else return getfname(true);
}
const char * libcon_optional(const char * desc, const char * defval)
{
if (libcon_interactive) return requeststrfromuser(desc, false, defval);
else return getarg(false, defval);
}
const char * libcon_optional_filename(const char * desc, const char * defval)
{
if (libcon_interactive) return requeststrfromuser(desc, true, defval);
else return getfname(false, defval);
}
const char * libcon_option()
{
if (!libcon_interactive && args[1][0]=='-') return getarg(false);
return NULL;
}
const char * libcon_question(const char * desc, const char * defval)
{
if (libcon_interactive) return libcon_optional(desc, defval);
return defval;
}
bool libcon_question_bool(const char * desc, bool defval)
{
if (!libcon_interactive) return defval;
while (true)
{
const char * answer=requeststrfromuser(desc, false, defval?"y":"n");
if (!stricmp(answer, "y") || !stricmp(answer, "yes")) return true;
if (!stricmp(answer, "n") || !stricmp(answer, "no")) return false;
}
}
void libcon_end()
{
if (!libcon_interactive && argsleft) libcon_badusage();
}