Branch data Line data Source code
1 : : #include "std-includes.h"
2 : : #include "libcon.h"
3 : : #include <signal.h>
4 : :
5 : : static char * progname;
6 : : static char ** args;
7 : : static int argsleft;
8 : : bool libcon_interactive;
9 : : static const char * usage;
10 : :
11 : : static volatile bool confirmclose=true;
12 : 0 : void libcon_pause()
13 : : {
14 [ # # ]: 0 : if (confirmclose)
15 : : {
16 : 0 : confirmclose=false;
17 : : #if defined(_WIN32)
18 : : system("pause");
19 : : #else
20 : 0 : printf("Press Enter to continue");
21 : 0 : getchar();
22 : : #endif
23 : 0 : confirmclose=true;
24 : : }
25 : 0 : }
26 : :
27 : 0 : void libcon_badusage()
28 : : {
29 : 0 : printf("usage: %s %s", progname, usage);
30 : 0 : exit(1);
31 : : }
32 : :
33 : 665 : static const char * getarg(bool tellusage, const char * defval= nullptr)
34 : : {
35 [ - + ]: 665 : if (!argsleft)
36 : : {
37 [ # # ]: 0 : if (tellusage) libcon_badusage();
38 : : return defval;
39 : : }
40 : 665 : args++;
41 : 665 : argsleft--;
42 : 665 : return args[0];
43 : : }
44 : :
45 : : static const char * getfname(bool tellusage, const char * defval= nullptr)
46 : : {
47 : 190 : return getarg(tellusage, defval);
48 : : //char * rval=malloc(char, 256);
49 : : //char * rvalend=rval;
50 : : //*rval=0;
51 : : //while (!strchr(rval, '.'))
52 : : //{
53 : : // char * thisword=getarg(false, nullptr);
54 : : // if (!thisword)
55 : : // {
56 : : // if (tellusage) libcon_badusage();
57 : : // else return defval;
58 : : // }
59 : : // if (rval!=rvalend) *(rvalend++)=' ';
60 : : // rvalend+=sprintf(rvalend, "%s", thisword);
61 : : //}
62 : : //return rval;
63 : : }
64 : :
65 : 0 : static const char * requirestrfromuser(const char * question, bool filename)
66 : : {
67 : 0 : confirmclose=false;
68 : 0 : char * rval=(char*)malloc(256);
69 : 0 : *rval=0;
70 [ # # # # ]: 0 : while (!strchr(rval, '\n') || *rval=='\n')
71 : : {
72 : 0 : *rval=0;
73 : 0 : printf("%s ", question);
74 : 0 : (void)fgets(rval, 250, stdin);
75 : : }
76 : 0 : *strchr(rval, '\n')=0;
77 : 0 : confirmclose=true;
78 : : #ifdef _WIN32
79 : : if (filename && rval[0]=='"' && rval[2]==':')
80 : : {
81 : : char * rvalend=strchr(rval, '\0');
82 : : if (rvalend[-1]=='"') rvalend[-1]='\0';
83 : : return rval+1;
84 : : }
85 : : #endif
86 : 0 : return rval;
87 : : }
88 : :
89 : 0 : static const char * requeststrfromuser(const char * question, bool filename, const char * defval)
90 : : {
91 : 0 : confirmclose=false;
92 : 0 : char * rval=(char*)malloc(256);
93 : 0 : *rval=0;
94 : 0 : printf("%s ", question);
95 : 0 : (void)fgets(rval, 250, stdin);
96 : 0 : *strchr(rval, '\n')=0;
97 : 0 : confirmclose=true;
98 [ # # ]: 0 : if (!*rval) return defval;
99 : : #ifdef _WIN32
100 : : if (filename && rval[0]=='"' && rval[2]==':')
101 : : {
102 : : char * rvalend=strchr(rval, '\0');
103 : : if (rvalend[-1]=='"') rvalend[-1]='\0';
104 : : return rval+1;
105 : : }
106 : : #endif
107 : : return rval;
108 : : }
109 : :
110 : 95 : void libcon_init(int argc, char ** argv, const char * usage_)
111 : : {
112 : 95 : progname=argv[0];
113 : 95 : args=argv;
114 : 95 : argsleft=argc-1;
115 : 95 : usage=usage_;
116 : 95 : libcon_interactive=(!argsleft);
117 : : #if defined(_WIN32)
118 : : if (libcon_interactive) atexit(libcon_pause);
119 : : #endif
120 : 95 : }
121 : :
122 : 0 : const char * libcon_require(const char * desc)
123 : : {
124 [ # # ]: 0 : if (libcon_interactive) return requirestrfromuser(desc, false);
125 : 0 : else return getarg(true);
126 : : }
127 : :
128 : 95 : const char * libcon_require_filename(const char * desc)
129 : : {
130 [ - + ]: 95 : if (libcon_interactive) return requirestrfromuser(desc, true);
131 : 95 : else return getfname(true);
132 : : }
133 : :
134 : 0 : const char * libcon_optional(const char * desc, const char * defval)
135 : : {
136 [ # # ]: 0 : if (libcon_interactive) return requeststrfromuser(desc, false, defval);
137 : 0 : else return getarg(false, defval);
138 : : }
139 : :
140 : 95 : const char * libcon_optional_filename(const char * desc, const char * defval)
141 : : {
142 [ - + ]: 95 : if (libcon_interactive) return requeststrfromuser(desc, true, defval);
143 : 95 : else return getfname(false, defval);
144 : : }
145 : :
146 : 475 : const char * libcon_option()
147 : : {
148 [ + - + - : 475 : if (!libcon_interactive && argsleft && args[1][0]=='-') return getarg(false);
+ + ]
149 : : return nullptr;
150 : : }
151 : :
152 : 95 : const char * libcon_option_value()
153 : : {
154 [ + - ]: 95 : if (!libcon_interactive) return getarg(false);
155 : : return nullptr;
156 : : }
157 : :
158 : 0 : const char * libcon_question(const char * desc, const char * defval)
159 : : {
160 [ # # ]: 0 : if (libcon_interactive) return libcon_optional(desc, defval);
161 : : return defval;
162 : : }
163 : :
164 : 0 : bool libcon_question_bool(const char * desc, bool defval)
165 : : {
166 [ # # ]: 0 : if (!libcon_interactive) return defval;
167 : : while (true)
168 : : {
169 [ # # ]: 0 : const char * answer=requeststrfromuser(desc, false, defval?"y":"n");
170 [ # # # # ]: 0 : if (!stricmp(answer, "y") || !stricmp(answer, "yes")) return true;
171 [ # # # # ]: 0 : if (!stricmp(answer, "n") || !stricmp(answer, "no")) return false;
172 : : }
173 : : }
174 : :
175 : 95 : void libcon_end()
176 : : {
177 [ + - - + ]: 95 : if (!libcon_interactive && argsleft) libcon_badusage();
178 : 95 : }
|