| 1 |
/** |
| 2 |
* @file protoApp.cpp |
| 3 |
* |
| 4 |
* @brief Base class for implementing protolib-based command-line applications |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "protoApp.h" |
| 8 |
#include <stdio.h> |
| 9 |
|
| 10 |
#ifndef _WIN32_WCE |
| 11 |
#include <signal.h> |
| 12 |
#endif // !_WIN32_WCE |
| 13 |
|
| 14 |
ProtoApp* ProtoApp::the_app = NULL; |
| 15 |
|
| 16 |
ProtoApp::ProtoApp() |
| 17 |
{ |
| 18 |
the_app = this; |
| 19 |
} |
| 20 |
|
| 21 |
ProtoApp::~ProtoApp() |
| 22 |
{ |
| 23 |
} |
| 24 |
|
| 25 |
bool ProtoApp::ProcessCommandString(const char* cmdline) |
| 26 |
{ |
| 27 |
char** argv = new char*[1]; |
| 28 |
if (argv) |
| 29 |
{ |
| 30 |
if (!(argv[0] = new char[strlen("protoApp") + 1])) //dummy argument which is usually pointer to app |
| 31 |
{ |
| 32 |
delete[] argv; |
| 33 |
PLOG(PL_ERROR, "protoApp: memory allocation error: %s\n", GetErrorString()); |
| 34 |
return false; |
| 35 |
} |
| 36 |
strcpy(argv[0], "protoApp"); |
| 37 |
} |
| 38 |
else |
| 39 |
{ |
| 40 |
PLOG(PL_ERROR, "protoApp: memory allocation error: %s\n", GetErrorString()); |
| 41 |
return false; |
| 42 |
} |
| 43 |
// 2) Parse "cmdline" for additional arguments |
| 44 |
int argc = 1; |
| 45 |
const char* ptr = cmdline; |
| 46 |
char fieldBuffer[PATH_MAX]; |
| 47 |
while (*ptr != '\0') |
| 48 |
{ |
| 49 |
// Skip leading white space |
| 50 |
while (('\t' == *ptr) || (' ' == *ptr)) ptr++; |
| 51 |
if (1 == sscanf(ptr, "%s", fieldBuffer)) |
| 52 |
{ |
| 53 |
// Look for "quoted" args to group args with spaces |
| 54 |
if ('"' == fieldBuffer[0]) |
| 55 |
{ |
| 56 |
// Copy contents of quote into fieldBuffer |
| 57 |
ptr++; // skip '"' character |
| 58 |
const char* end = strchr(ptr, '"'); |
| 59 |
if (!end) |
| 60 |
{ |
| 61 |
PLOG(PL_ERROR, "protoApp: Error parsing command line: Unterminated quoted string\n"); |
| 62 |
for (int i = 0; i < argc; i++) delete[] argv[i]; |
| 63 |
if (argv) delete[] argv; |
| 64 |
return false; |
| 65 |
} |
| 66 |
unsigned int len = (unsigned int)(end - ptr); |
| 67 |
len = len < (PATH_MAX - 1) ? len : PATH_MAX-1; |
| 68 |
memcpy(fieldBuffer, ptr, len); |
| 69 |
fieldBuffer[len] = '\0'; |
| 70 |
ptr = end + 1; |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
ptr += strlen(fieldBuffer); |
| 75 |
} |
| 76 |
argc++; |
| 77 |
char** tempv = new char*[argc]; |
| 78 |
if (!tempv) |
| 79 |
{ |
| 80 |
argc--; |
| 81 |
for (int i = 0; i < argc; i++) delete[] argv[i]; |
| 82 |
if (argv) delete[] argv; |
| 83 |
PLOG(PL_ERROR, "protoApp: memory allocation error: %s\n", GetErrorString()); |
| 84 |
return false; |
| 85 |
} |
| 86 |
if (argv) |
| 87 |
{ |
| 88 |
memcpy(tempv, argv, (argc-1)*sizeof(char*)); |
| 89 |
delete[] argv; |
| 90 |
} |
| 91 |
argv = tempv; |
| 92 |
if (!(argv[argc-1] = new char[strlen(fieldBuffer)+1])) |
| 93 |
{ |
| 94 |
for (int i = 0; i < argc; i++) delete[] argv[i]; |
| 95 |
if (argv) delete[] argv; |
| 96 |
PLOG(PL_ERROR, "protoApp: memory allocation error: %s\n", GetErrorString()); |
| 97 |
return false; |
| 98 |
} |
| 99 |
strcpy(argv[argc-1], fieldBuffer); |
| 100 |
} // end if (sscanf(%s)) |
| 101 |
} // end while (ptr); |
| 102 |
bool result = ProcessCommands(argc, argv); |
| 103 |
for (int i = 0; i < argc; i++) delete[] argv[i]; |
| 104 |
if (argv) delete[] argv; |
| 105 |
return result; |
| 106 |
} // end ProtoApp::ProcessCommandString() |
| 107 |
|
| 108 |
|
| 109 |
#ifndef __rtems__ |
| 110 |
|
| 111 |
#ifdef WIN32 |
| 112 |
int PASCAL Win32ProtoMain(HINSTANCE instance, HINSTANCE prevInst, LPSTR cmdline, int cmdshow) |
| 113 |
{ |
| 114 |
#ifdef _UNICODE |
| 115 |
int len = wcslen((wchar_t*) cmdline); |
| 116 |
len = wcstombs(cmdline, (wchar_t*)cmdline, len); |
| 117 |
cmdline[len] = '\0'; |
| 118 |
#endif // _UNICODE |
| 119 |
// Scan command line for "background" command |
| 120 |
bool background = false; |
| 121 |
char* ptr = cmdline; |
| 122 |
char fieldBuffer[MAX_PATH]; |
| 123 |
while ('\0' != *ptr) |
| 124 |
{ |
| 125 |
// Skip leading white space |
| 126 |
while (('\t' == *ptr) || (' ' == *ptr)) ptr++; |
| 127 |
if (1 == sscanf(ptr, "%s", fieldBuffer)) |
| 128 |
{ |
| 129 |
if (!strcmp(fieldBuffer, "background")) |
| 130 |
{ |
| 131 |
background = true; |
| 132 |
// Remove "background" from cmdline |
| 133 |
size_t len = strlen(ptr) + 1; |
| 134 |
size_t bgLen = strlen("background"); |
| 135 |
memmove(ptr, ptr+bgLen, len-bgLen); |
| 136 |
break; |
| 137 |
} |
| 138 |
ptr += strlen(fieldBuffer); |
| 139 |
} |
| 140 |
} |
| 141 |
bool pauseForUser = true; |
| 142 |
if (background) |
| 143 |
pauseForUser = false; |
| 144 |
else |
| 145 |
OpenDebugWindow(); // open a WIN32 console |
| 146 |
#ifdef _WIN32_WCE |
| 147 |
if (!background) Sleep(1000); // give our WinCE debug window time to start up |
| 148 |
#endif // _WIN32-WCE |
| 149 |
|
| 150 |
// Convert WIN32 cmdline into argc, argv form (skipping "background" cmd) |
| 151 |
// 1) Set argv[0] to dummy app name ("protoApp") |
| 152 |
char** argv = new char*[1]; |
| 153 |
if (argv) |
| 154 |
{ |
| 155 |
if (!(argv[0] = new char[strlen("protoApp") + 1])) |
| 156 |
{ |
| 157 |
delete[] argv; |
| 158 |
PLOG(PL_ERROR, "protoApp: memory allocation error: %s\n", GetErrorString()); |
| 159 |
fprintf(stderr, "Program Finished - Hit <Enter> to exit"); |
| 160 |
getchar(); |
| 161 |
return -1; |
| 162 |
} |
| 163 |
strcpy(argv[0], "protoApp"); |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
PLOG(PL_ERROR, "protoApp: memory allocation error: %s\n", GetErrorString()); |
| 168 |
fprintf(stderr, "Program Finished - Hit <Enter> to exit"); |
| 169 |
getchar(); |
| 170 |
return -1; |
| 171 |
} |
| 172 |
// 2) Parse "cmdline" for additional arguments |
| 173 |
int argc = 1; |
| 174 |
ptr = cmdline; |
| 175 |
while (*ptr != '\0') |
| 176 |
{ |
| 177 |
// Skip leading white space |
| 178 |
while (('\t' == *ptr) || (' ' == *ptr)) ptr++; |
| 179 |
if (1 == sscanf(ptr, "%s", fieldBuffer)) |
| 180 |
{ |
| 181 |
// Look for "quoted" args to group args with spaces |
| 182 |
if ('"' == fieldBuffer[0]) |
| 183 |
{ |
| 184 |
// Copy contents of quote into fieldBuffer |
| 185 |
ptr++; // skip '"' character |
| 186 |
char* end = strchr(ptr, '"'); |
| 187 |
if (!end) |
| 188 |
{ |
| 189 |
PLOG(PL_ERROR, "protoApp: Error parsing command line: Unterminated quoted string\n"); |
| 190 |
for (int i = 0; i < argc; i++) delete[] argv[i]; |
| 191 |
if (argv) delete[] argv; |
| 192 |
fprintf(stderr, "Program Finished - Hit <Enter> to exit"); |
| 193 |
getchar(); |
| 194 |
return -1; |
| 195 |
} |
| 196 |
unsigned int len = (unsigned int)(end - ptr); |
| 197 |
len = len < (MAX_PATH - 1) ? len : MAX_PATH-1; |
| 198 |
memcpy(fieldBuffer, ptr, len); |
| 199 |
fieldBuffer[len] = '\0'; |
| 200 |
ptr = end + 1; |
| 201 |
} |
| 202 |
else |
| 203 |
{ |
| 204 |
ptr += strlen(fieldBuffer); |
| 205 |
} |
| 206 |
|
| 207 |
argc++; |
| 208 |
char** tempv = new char*[argc]; |
| 209 |
if (!tempv) |
| 210 |
{ |
| 211 |
argc--; |
| 212 |
for (int i = 0; i < argc; i++) delete[] argv[i]; |
| 213 |
if (argv) delete[] argv; |
| 214 |
PLOG(PL_ERROR, "protoApp: memory allocation error: %s\n", GetErrorString()); |
| 215 |
fprintf(stderr, "Program Finished - Hit <Enter> to exit"); |
| 216 |
getchar(); |
| 217 |
return -1; |
| 218 |
} |
| 219 |
if (argv) |
| 220 |
{ |
| 221 |
memcpy(tempv, argv, (argc-1)*sizeof(char*)); |
| 222 |
delete[] argv; |
| 223 |
} |
| 224 |
argv = tempv; |
| 225 |
if (!(argv[argc-1] = new char[strlen(fieldBuffer)+1])) |
| 226 |
{ |
| 227 |
for (int i = 0; i < argc; i++) delete[] argv[i]; |
| 228 |
if (argv) delete[] argv; |
| 229 |
PLOG(PL_ERROR, "protoApp: memory allocation error: %s\n", GetErrorString()); |
| 230 |
fprintf(stderr, "Program Finished - Hit <Enter> to exit"); |
| 231 |
getchar(); |
| 232 |
return -1; |
| 233 |
} |
| 234 |
strcpy(argv[argc-1], fieldBuffer); |
| 235 |
} // end if (sscanf(%s)) |
| 236 |
} // end while (ptr); |
| 237 |
int result = ProtoMain(argc, argv, pauseForUser); |
| 238 |
for (int i = 0; i < argc; i++) delete[] argv[i]; |
| 239 |
if (argv) delete[] argv; |
| 240 |
return result; |
| 241 |
} // end Win32ProtoMain() |
| 242 |
|
| 243 |
/** |
| 244 |
* @brief This WIN32 code lets us build a console app if we like |
| 245 |
*/ |
| 246 |
int Win32ProtoMainConsole(int argc, char* argv[]) |
| 247 |
{ |
| 248 |
bool pauseForUser = false; |
| 249 |
#ifndef _WIN32_WCE |
| 250 |
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); |
| 251 |
if (INVALID_HANDLE_VALUE != hConsoleOutput) |
| 252 |
{ |
| 253 |
CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 254 |
GetConsoleScreenBufferInfo(hConsoleOutput, &csbi); |
| 255 |
pauseForUser = ((csbi.dwCursorPosition.X==0) && (csbi.dwCursorPosition.Y==0)); |
| 256 |
if ((csbi.dwSize.X<=0) || (csbi.dwSize.Y <= 0)) pauseForUser = false; |
| 257 |
} |
| 258 |
else |
| 259 |
{ |
| 260 |
// We're not a "console" application, so create one |
| 261 |
// This could be commented out or made a command-line option |
| 262 |
OpenDebugWindow(); |
| 263 |
pauseForUser = true; |
| 264 |
} |
| 265 |
#endif // _WIN32_WCE |
| 266 |
return ProtoMain(argc, argv, pauseForUser); |
| 267 |
} // end Win32ProtoMainConsole() |
| 268 |
#endif // WIN32 |
| 269 |
|
| 270 |
int ProtoMain(int argc, char* argv[], bool pauseForUser) |
| 271 |
{ |
| 272 |
int exitCode = 0; |
| 273 |
ProtoApp* theApp = ProtoApp::GetApp(); |
| 274 |
|
| 275 |
if (!theApp) |
| 276 |
{ |
| 277 |
fprintf(stderr, "protoApp: error: no app was instantiated\n"); |
| 278 |
return -1; |
| 279 |
} |
| 280 |
|
| 281 |
#ifdef WIN32 |
| 282 |
if (pauseForUser) theApp->Win32Init(); |
| 283 |
#endif // WIN32 |
| 284 |
|
| 285 |
// Pass command-line options to application instance and startup |
| 286 |
if (!theApp->OnStartup(argc, argv)) |
| 287 |
{ |
| 288 |
theApp->OnShutdown(); |
| 289 |
//fprintf(stderr, "protoApp: Error on startup!\n"); |
| 290 |
if (pauseForUser) |
| 291 |
{ |
| 292 |
#ifdef _WIN32_WCE |
| 293 |
MessageBox(NULL, _T("Program Finished - Hit <OK> to exit"), |
| 294 |
_T("ProtoDebug Notice"), MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND); |
| 295 |
#else |
| 296 |
fprintf(stderr, "Program Finished - Hit <Enter> to exit"); |
| 297 |
getchar(); |
| 298 |
#endif // if/else _WIN32_WCE |
| 299 |
} |
| 300 |
return -1; |
| 301 |
} |
| 302 |
#ifndef _WIN32_WCE |
| 303 |
signal(SIGTERM, ProtoApp::SignalHandler); |
| 304 |
signal(SIGINT, ProtoApp::SignalHandler); |
| 305 |
#endif // _WIN32_WCE |
| 306 |
|
| 307 |
exitCode = theApp->Run(); |
| 308 |
|
| 309 |
theApp->OnShutdown(); |
| 310 |
if (pauseForUser) |
| 311 |
{ |
| 312 |
#ifdef _WIN32_WCE |
| 313 |
MessageBox(NULL, _T("Program Finished - Hit <OK> to exit"), |
| 314 |
_T("ProtoDebug Notice"), MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND); |
| 315 |
#else |
| 316 |
fprintf(stderr, "Program Finished - Hit <Enter> to exit"); |
| 317 |
getchar(); |
| 318 |
#endif // if/else _WIN32_WCE |
| 319 |
} |
| 320 |
//TRACE("ProtoMain() exiting with code %d...\n", exitCode); |
| 321 |
|
| 322 |
#ifdef USE_PROTO_CHECK |
| 323 |
ProtoCheckLogAllocations(stderr); // TBD - output to proto debug log? |
| 324 |
#endif // USE_PROTO_CHECK |
| 325 |
|
| 326 |
return exitCode; // exitCode contains "signum" causing exit |
| 327 |
} // end ProtoMain(); |
| 328 |
|
| 329 |
#endif // !__rtems__ |
| 330 |
|
| 331 |
#ifndef _WIN32_WCE |
| 332 |
void ProtoApp::SignalHandler(int sigNum) |
| 333 |
{ |
| 334 |
#ifndef __rtems__ |
| 335 |
switch(sigNum) |
| 336 |
{ |
| 337 |
case SIGTERM: |
| 338 |
case SIGINT: |
| 339 |
{ |
| 340 |
ProtoApp* theApp = ProtoApp::GetApp(); |
| 341 |
if (theApp) |
| 342 |
theApp->Stop(sigNum); // causes theApp's main loop to exit |
| 343 |
break; |
| 344 |
} |
| 345 |
|
| 346 |
default: |
| 347 |
fprintf(stderr, "protoApp: Unexpected signal: %d\n", sigNum); |
| 348 |
break; |
| 349 |
} |
| 350 |
#endif // !__rtems__ |
| 351 |
} // end ProtoApp::SignalHandler() |
| 352 |
#endif // !_WIN32_WCE |