libcon.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | ✗ | void libcon_pause() | |
| 13 | { | ||
| 14 | ✗ | if (confirmclose) | |
| 15 | { | ||
| 16 | ✗ | confirmclose=false; | |
| 17 | #if defined(_WIN32) | ||
| 18 | ✗ | system("pause"); | |
| 19 | #else | ||
| 20 | ✗ | printf("Press Enter to continue"); | |
| 21 | ✗ | getchar(); | |
| 22 | #endif | ||
| 23 | ✗ | confirmclose=true; | |
| 24 | } | ||
| 25 | ✗ | } | |
| 26 | |||
| 27 | ✗ | void libcon_badusage() | |
| 28 | { | ||
| 29 | ✗ | printf("usage: %s %s", progname, usage); | |
| 30 | ✗ | exit(1); | |
| 31 | } | ||
| 32 | |||
| 33 | 1400 | static const char * getarg(bool tellusage, const char * defval= nullptr) | |
| 34 | { | ||
| 35 | 1400 | if (!argsleft) | |
| 36 | { | ||
| 37 | ✗ | if (tellusage) libcon_badusage(); | |
| 38 | ✗ | return defval; | |
| 39 | } | ||
| 40 | 1400 | args++; | |
| 41 | 1400 | argsleft--; | |
| 42 | 1400 | return args[0]; | |
| 43 | } | ||
| 44 | |||
| 45 | 400 | static const char * getfname(bool tellusage, const char * defval= nullptr) | |
| 46 | { | ||
| 47 | 400 | 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 | ✗ | static const char * requirestrfromuser(const char * question, bool filename) | |
| 66 | { | ||
| 67 | ✗ | confirmclose=false; | |
| 68 | ✗ | char * rval=(char*)malloc(256); | |
| 69 | ✗ | *rval=0; | |
| 70 | ✗ | while (!strchr(rval, '\n') || *rval=='\n') | |
| 71 | { | ||
| 72 | ✗ | *rval=0; | |
| 73 | ✗ | printf("%s ", question); | |
| 74 | ✗ | (void)fgets(rval, 250, stdin); | |
| 75 | } | ||
| 76 | ✗ | *strchr(rval, '\n')=0; | |
| 77 | ✗ | 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 | ✗ | return rval; | |
| 87 | } | ||
| 88 | |||
| 89 | ✗ | static const char * requeststrfromuser(const char * question, bool filename, const char * defval) | |
| 90 | { | ||
| 91 | ✗ | confirmclose=false; | |
| 92 | ✗ | char * rval=(char*)malloc(256); | |
| 93 | ✗ | *rval=0; | |
| 94 | ✗ | printf("%s ", question); | |
| 95 | ✗ | (void)fgets(rval, 250, stdin); | |
| 96 | ✗ | *strchr(rval, '\n')=0; | |
| 97 | ✗ | confirmclose=true; | |
| 98 | ✗ | 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 | 200 | void libcon_init(int argc, char ** argv, const char * usage_) | |
| 111 | { | ||
| 112 | 200 | progname=argv[0]; | |
| 113 | 200 | args=argv; | |
| 114 | 200 | argsleft=argc-1; | |
| 115 | 200 | usage=usage_; | |
| 116 | 200 | libcon_interactive=(!argsleft); | |
| 117 | #if defined(_WIN32) | ||
| 118 | 100 | if (libcon_interactive) atexit(libcon_pause); | |
| 119 | #endif | ||
| 120 | 200 | } | |
| 121 | |||
| 122 | ✗ | const char * libcon_require(const char * desc) | |
| 123 | { | ||
| 124 | ✗ | if (libcon_interactive) return requirestrfromuser(desc, false); | |
| 125 | ✗ | else return getarg(true); | |
| 126 | } | ||
| 127 | |||
| 128 | 200 | const char * libcon_require_filename(const char * desc) | |
| 129 | { | ||
| 130 | 200 | if (libcon_interactive) return requirestrfromuser(desc, true); | |
| 131 | 200 | else return getfname(true); | |
| 132 | } | ||
| 133 | |||
| 134 | ✗ | const char * libcon_optional(const char * desc, const char * defval) | |
| 135 | { | ||
| 136 | ✗ | if (libcon_interactive) return requeststrfromuser(desc, false, defval); | |
| 137 | ✗ | else return getarg(false, defval); | |
| 138 | } | ||
| 139 | |||
| 140 | 200 | const char * libcon_optional_filename(const char * desc, const char * defval) | |
| 141 | { | ||
| 142 | 200 | if (libcon_interactive) return requeststrfromuser(desc, true, defval); | |
| 143 | 200 | else return getfname(false, defval); | |
| 144 | } | ||
| 145 | |||
| 146 | 1000 | const char * libcon_option() | |
| 147 | { | ||
| 148 | 1000 | if (!libcon_interactive && argsleft && args[1][0]=='-') return getarg(false); | |
| 149 | 200 | return nullptr; | |
| 150 | } | ||
| 151 | |||
| 152 | 200 | const char * libcon_option_value() | |
| 153 | { | ||
| 154 | 200 | if (!libcon_interactive) return getarg(false); | |
| 155 | ✗ | return nullptr; | |
| 156 | } | ||
| 157 | |||
| 158 | ✗ | const char * libcon_question(const char * desc, const char * defval) | |
| 159 | { | ||
| 160 | ✗ | if (libcon_interactive) return libcon_optional(desc, defval); | |
| 161 | ✗ | return defval; | |
| 162 | } | ||
| 163 | |||
| 164 | ✗ | bool libcon_question_bool(const char * desc, bool defval) | |
| 165 | { | ||
| 166 | ✗ | if (!libcon_interactive) return defval; | |
| 167 | while (true) | ||
| 168 | { | ||
| 169 | ✗ | const char * answer=requeststrfromuser(desc, false, defval?"y":"n"); | |
| 170 | ✗ | if (!stricmp(answer, "y") || !stricmp(answer, "yes")) return true; | |
| 171 | ✗ | if (!stricmp(answer, "n") || !stricmp(answer, "no")) return false; | |
| 172 | ✗ | } | |
| 173 | } | ||
| 174 | |||
| 175 | 200 | void libcon_end() | |
| 176 | { | ||
| 177 | 200 | if (!libcon_interactive && argsleft) libcon_badusage(); | |
| 178 | 200 | } | |
| 179 |