32 #include <sys/utime.h>
40 #include <sys/select.h>
49 #include <sys/types.h>
53 #define _access access
68 void my_aux_sighandler(
int) {}
71 struct termios oldtio, curtio;
75 tcgetattr(0, &oldtio);
80 tcgetattr(0, &curtio);
81 curtio.c_lflag &= ~(ICANON | ECHO);
82 tcsetattr(0, TCSANOW, &curtio);
84 struct pollfd pfds[1];
88 pfds[0].events = POLLIN;
89 const int ret = poll(pfds, 1, 0);
92 tcsetattr(0, TCSANOW, &oldtio);
112 static std::mutex cs;
113 std::lock_guard<std::mutex> lock(cs);
119 char* org_tz = getenv(
"TZ");
140 #endif // HAVE_TIMEGM
145 #include <mrpt/version.h>
156 const char* source_date_epoch = MRPT_SOURCE_DATE_EPOCH;
159 unsigned long epoch = strtoul(source_date_epoch, &endptr, 10);
162 (epoch == std::numeric_limits<unsigned long>::max() || epoch == 0)) ||
163 (errno != 0 && epoch == 0)))
172 struct tm* build_time = gmtime(&
now);
173 const int year = build_time->tm_year + 1900;
174 const int month = build_time->tm_mon + 1;
175 const int day = build_time->tm_mday;
178 "%i-%02i-%02i %02i:%02i:%02i UTC", year, month, day,
179 build_time->tm_hour, build_time->tm_min, build_time->tm_sec);
197 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
213 char* buf,
size_t bufSize,
const char*
format, va_list args) noexcept
216 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
229 char* buf,
size_t bufSize,
const char*
format, va_list args) noexcept
231 #if defined(_MSC_VER)
232 #if (_MSC_VER >= 1400)
257 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
260 if (0 != ::fopen_s(&f, fileName,
mode))
275 if (!f)
THROW_EXCEPTION(
"Trying to close a nullptr 'FILE*' descriptor");
286 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
287 ::strcat_s(dest, destSize,
source);
301 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
302 ::strcpy_s(dest, destSize,
source);
314 return ::strcmp(str1, str2);
323 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
326 return ::strcmpi(str1, str2);
329 return ::strcasecmp(str1, str2);
338 return ::strncmp(str1, str2,
count);
346 #if defined(_MSC_VER)
349 return ::strncasecmp(str1, str2,
count);
357 void* dest,
size_t destSize,
const void*
src,
size_t copyCount) noexcept
359 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
360 ::memcpy_s(dest, destSize,
src, copyCount);
375 struct termios oldt, newt;
377 tcgetattr(STDIN_FILENO, &oldt);
379 newt.c_lflag &= ~(ICANON | ECHO);
380 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
382 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
393 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
394 return ::_kbhit() != 0;
412 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
414 result = ::vfprintf_s(fil, frm, ap);
418 result = ::vfprintf(fil, frm, ap);
430 std::cout << msg << std::endl;
440 int ret = ::system(
"cls");
442 int ret = ::system(
"clear");
445 cerr <<
"[mrpt::system::clearConsole] Error invoking 'clear screen' "
455 return (
int64_t)::strtol(nptr, endptr, base);
457 return (
int64_t)::strtoll(nptr, endptr, base);
467 return (
uint64_t)::strtoul(nptr, endptr, base);
469 return (
uint64_t)::strtoull(nptr, endptr, base);
482 static const int TS_NORMAL = 0;
483 static const int TS_BLUE = 1;
484 static const int TS_GREEN = 2;
485 static const int TS_RED = 4;
487 static int normal_attributes = -1;
489 GetStdHandle(changeStdErr ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
490 fflush(changeStdErr ? stderr : stdout);
492 if (normal_attributes < 0)
494 CONSOLE_SCREEN_BUFFER_INFO info;
495 GetConsoleScreenBufferInfo(hstdout, &info);
496 normal_attributes = info.wAttributes;
499 SetConsoleTextAttribute(
502 color == TS_NORMAL ? normal_attributes
503 : ((
color & TS_BLUE ? FOREGROUND_BLUE : 0) |
504 (
color & TS_GREEN ? FOREGROUND_GREEN : 0) |
505 (
color & TS_RED ? FOREGROUND_RED : 0) |
506 FOREGROUND_INTENSITY)));
509 static const uint8_t ansi_tab[] = {30, 34, 32, 36, 31, 35, 33, 37};
511 fflush(changeStdErr ? stdout : stderr);
512 if (
color != TS_NORMAL)
513 code = ansi_tab[
color & (TS_BLUE | TS_GREEN | TS_RED)];
514 fprintf(changeStdErr ? stdout : stderr,
"\x1b[%dm",
code);
519 " Mobile Robot Programming Toolkit (MRPT) "
521 " http://www.mrpt.org/ "
525 " Copyright (c) 2005-%Y, Individual contributors, see AUTHORS file "
527 " See: http://www.mrpt.org/Authors - All rights reserved. "
529 " Released under BSD License. See details in http://www.mrpt.org/License "
534 static bool sLicenseTextReady =
false;
537 if (!sLicenseTextReady)
544 timeinfo = localtime(&rawtime);
549 sLicenseTextReady =
true;
558 DWORD e = GetLastError();
559 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, e, 0, str,
sizeof(str), NULL);
576 PROCESS_INFORMATION PI;
577 memset(&SI, 0,
sizeof(STARTUPINFOA));
578 SI.cb =
sizeof(STARTUPINFOA);
580 NULL, (LPSTR)command.c_str(), NULL, NULL,
true, 0, NULL, NULL, &SI,
584 WaitForSingleObject(PI.hProcess, INFINITE);
595 return 0 == ::system(command.c_str());
601 #include <mrpt/mrpt_paths_config.h>
604 static bool mrpt_shared_first_call =
true;
607 if (mrpt_shared_first_call)
609 mrpt_shared_first_call =
false;
611 for (
int attempt = 0;; attempt++)
617 dir =
string(MRPT_SOURCE_BASE_DIRECTORY) +
621 dir =
string(MRPT_INSTALL_PREFIX_DIRECTORY) +
628 GetModuleFileNameA(
nullptr, curExe,
sizeof(curExe));
638 found_mrpt_shared_dir =
".";
642 found_mrpt_shared_dir = dir;
644 if (!found_mrpt_shared_dir.empty())
break;
648 return found_mrpt_shared_dir;
667 if (!(
in = popen(command.c_str(),
mode.c_str())))
669 sout <<
"Popen Execution failed!" << endl;
670 *output = sout.str();
676 while (fgets(buff,
sizeof(buff),
in) != NULL)
682 exit_code = pclose(
in);
688 HANDLE g_hChildStd_IN_Rd = NULL;
689 HANDLE g_hChildStd_IN_Wr = NULL;
690 HANDLE g_hChildStd_OUT_Rd = NULL;
691 HANDLE g_hChildStd_OUT_Wr = NULL;
693 HANDLE g_hInputFile = NULL;
694 SECURITY_ATTRIBUTES saAttr;
696 saAttr.nLength =
sizeof(SECURITY_ATTRIBUTES);
697 saAttr.bInheritHandle =
TRUE;
698 saAttr.lpSecurityDescriptor = NULL;
700 if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0))
705 if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
710 if (!CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0))
715 if (!SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0))
719 PROCESS_INFORMATION piProcInfo;
720 STARTUPINFOA siStartInfo;
725 ZeroMemory(&piProcInfo,
sizeof(PROCESS_INFORMATION));
731 ZeroMemory(&siStartInfo,
sizeof(STARTUPINFO));
732 siStartInfo.cb =
sizeof(STARTUPINFO);
733 siStartInfo.hStdError = g_hChildStd_OUT_Wr;
734 siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
735 siStartInfo.hStdInput = g_hChildStd_IN_Rd;
736 siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
739 bSuccess = CreateProcessA(
741 (LPSTR)command.c_str(),
762 DWORD dwAvailable = 0;
764 g_hChildStd_OUT_Rd, NULL, NULL, NULL, &dwAvailable, NULL);
768 g_hChildStd_OUT_Rd, chBuf,
sizeof(chBuf), &dwRead, NULL);
769 if (!bSuccess || dwRead == 0)
break;
770 sout.write(chBuf, dwRead);
775 if (GetExitCodeProcess(piProcInfo.hProcess, &exitval))
777 if (exitval != STILL_ACTIVE)
787 CloseHandle(piProcInfo.hProcess);
788 CloseHandle(piProcInfo.hThread);
799 *output = sout.str();