diff --git a/research/mini-shell/completions.cpp b/research/mini-shell/completions.cpp index 3d07570..b3b45de 100644 --- a/research/mini-shell/completions.cpp +++ b/research/mini-shell/completions.cpp @@ -7,6 +7,7 @@ // #include "utils.h" #include "completions.h" +#include ReadLineWithHistory::ReadLineWithHistory(EditTextInterface& text) diff --git a/research/mini-shell/completions.cpp b/research/mini-shell/completions.cpp index 3d07570..b3b45de 100644 --- a/research/mini-shell/completions.cpp +++ b/research/mini-shell/completions.cpp @@ -7,6 +7,7 @@ // #include "utils.h" #include "completions.h" +#include ReadLineWithHistory::ReadLineWithHistory(EditTextInterface& text) diff --git a/research/mini-shell/filesystem.cpp b/research/mini-shell/filesystem.cpp index 4d8c8f5..ffd1a49 100644 --- a/research/mini-shell/filesystem.cpp +++ b/research/mini-shell/filesystem.cpp @@ -87,10 +87,14 @@ entries.relativePath = directory; // entries.path = simplifiedPath(getCurrentDirectory() + directory); // TODO: test while ((ent = readdir(dir)) != NULL) { +#ifdef _WIN32 + const uint32_t FILE_ATTRIB_DIR = 0x10; + bool isDirectory = dir->dd_dta.attrib & FILE_ATTRIB_DIR; +#else + bool isDirectory = ent->d_type & DT_DIR; +#endif if (hidden || ent->d_name[0] != '.') - entries.entries.push_back(DirectoryEntrySet::DirectoryEntryMember(ent->d_name, - (ent->d_type & DT_DIR) ? DirectoryEntrySet::DirectoryEntryMember::Directory : DirectoryEntrySet::DirectoryEntryMember::File - )); + entries.entries.push_back(DirectoryEntrySet::DirectoryEntryMember(ent->d_name, isDirectory ? DirectoryEntrySet::DirectoryEntryMember::Directory : DirectoryEntrySet::DirectoryEntryMember::File)); } closedir(dir); return true; diff --git a/research/mini-shell/completions.cpp b/research/mini-shell/completions.cpp index 3d07570..b3b45de 100644 --- a/research/mini-shell/completions.cpp +++ b/research/mini-shell/completions.cpp @@ -7,6 +7,7 @@ // #include "utils.h" #include "completions.h" +#include ReadLineWithHistory::ReadLineWithHistory(EditTextInterface& text) diff --git a/research/mini-shell/filesystem.cpp b/research/mini-shell/filesystem.cpp index 4d8c8f5..ffd1a49 100644 --- a/research/mini-shell/filesystem.cpp +++ b/research/mini-shell/filesystem.cpp @@ -87,10 +87,14 @@ entries.relativePath = directory; // entries.path = simplifiedPath(getCurrentDirectory() + directory); // TODO: test while ((ent = readdir(dir)) != NULL) { +#ifdef _WIN32 + const uint32_t FILE_ATTRIB_DIR = 0x10; + bool isDirectory = dir->dd_dta.attrib & FILE_ATTRIB_DIR; +#else + bool isDirectory = ent->d_type & DT_DIR; +#endif if (hidden || ent->d_name[0] != '.') - entries.entries.push_back(DirectoryEntrySet::DirectoryEntryMember(ent->d_name, - (ent->d_type & DT_DIR) ? DirectoryEntrySet::DirectoryEntryMember::Directory : DirectoryEntrySet::DirectoryEntryMember::File - )); + entries.entries.push_back(DirectoryEntrySet::DirectoryEntryMember(ent->d_name, isDirectory ? DirectoryEntrySet::DirectoryEntryMember::Directory : DirectoryEntrySet::DirectoryEntryMember::File)); } closedir(dir); return true; diff --git a/research/mini-shell/keyboard.cpp b/research/mini-shell/keyboard.cpp index 4cd4c1c..07dcad3 100755 --- a/research/mini-shell/keyboard.cpp +++ b/research/mini-shell/keyboard.cpp @@ -9,10 +9,16 @@ // UNIX #include -#include #include -#include - +#ifndef _WIN32 +# include +# include +#else +# define WIN32_LEAN_AND_MEAN +# include +# include +#endif +#include struct RawKeyboard::Data { @@ -21,8 +27,10 @@ ESCAPE_CODE_SEQ1 = 27, ESCAPE_CODE_SEQ2 = 91 }; +#ifndef _WIN32 int keyFd; struct termios savedFlags; +#endif bool read(unsigned int& ch, bool wait = true); }; @@ -30,6 +38,13 @@ bool RawKeyboard::Data::read(unsigned int& ch, bool wait) { ch = 0; +#ifdef _WIN32 + if (wait || _kbhit()) { + ch = _getch(); + return true; + } + return false; +#else if (wait) { fd_set rdSet; FD_ZERO(&rdSet); @@ -37,25 +52,30 @@ select(keyFd + 1, &rdSet, NULL, NULL, NULL); } return ::read(keyFd, &ch, 1) != -1; +#endif } RawKeyboard::RawKeyboard() : data(new Data) { +#ifndef _WIN32 data->keyFd = open("/dev/tty", O_RDONLY | O_NONBLOCK); tcgetattr(data->keyFd, &data->savedFlags); // save state struct termios rawFlags = data->savedFlags; cfmakeraw(&rawFlags); // uncook the input rawFlags.c_oflag = data->savedFlags.c_oflag; // restore output flags (only want to uncook the input) tcsetattr(data->keyFd, TCSANOW, &rawFlags); +#endif } RawKeyboard::~RawKeyboard() { +#ifndef _WIN32 tcsetattr(data->keyFd, TCSANOW, &data->savedFlags); // restore state close(data->keyFd); +#endif delete data; } @@ -85,4 +105,3 @@ return ch; } - diff --git a/research/mini-shell/completions.cpp b/research/mini-shell/completions.cpp index 3d07570..b3b45de 100644 --- a/research/mini-shell/completions.cpp +++ b/research/mini-shell/completions.cpp @@ -7,6 +7,7 @@ // #include "utils.h" #include "completions.h" +#include ReadLineWithHistory::ReadLineWithHistory(EditTextInterface& text) diff --git a/research/mini-shell/filesystem.cpp b/research/mini-shell/filesystem.cpp index 4d8c8f5..ffd1a49 100644 --- a/research/mini-shell/filesystem.cpp +++ b/research/mini-shell/filesystem.cpp @@ -87,10 +87,14 @@ entries.relativePath = directory; // entries.path = simplifiedPath(getCurrentDirectory() + directory); // TODO: test while ((ent = readdir(dir)) != NULL) { +#ifdef _WIN32 + const uint32_t FILE_ATTRIB_DIR = 0x10; + bool isDirectory = dir->dd_dta.attrib & FILE_ATTRIB_DIR; +#else + bool isDirectory = ent->d_type & DT_DIR; +#endif if (hidden || ent->d_name[0] != '.') - entries.entries.push_back(DirectoryEntrySet::DirectoryEntryMember(ent->d_name, - (ent->d_type & DT_DIR) ? DirectoryEntrySet::DirectoryEntryMember::Directory : DirectoryEntrySet::DirectoryEntryMember::File - )); + entries.entries.push_back(DirectoryEntrySet::DirectoryEntryMember(ent->d_name, isDirectory ? DirectoryEntrySet::DirectoryEntryMember::Directory : DirectoryEntrySet::DirectoryEntryMember::File)); } closedir(dir); return true; diff --git a/research/mini-shell/keyboard.cpp b/research/mini-shell/keyboard.cpp index 4cd4c1c..07dcad3 100755 --- a/research/mini-shell/keyboard.cpp +++ b/research/mini-shell/keyboard.cpp @@ -9,10 +9,16 @@ // UNIX #include -#include #include -#include - +#ifndef _WIN32 +# include +# include +#else +# define WIN32_LEAN_AND_MEAN +# include +# include +#endif +#include struct RawKeyboard::Data { @@ -21,8 +27,10 @@ ESCAPE_CODE_SEQ1 = 27, ESCAPE_CODE_SEQ2 = 91 }; +#ifndef _WIN32 int keyFd; struct termios savedFlags; +#endif bool read(unsigned int& ch, bool wait = true); }; @@ -30,6 +38,13 @@ bool RawKeyboard::Data::read(unsigned int& ch, bool wait) { ch = 0; +#ifdef _WIN32 + if (wait || _kbhit()) { + ch = _getch(); + return true; + } + return false; +#else if (wait) { fd_set rdSet; FD_ZERO(&rdSet); @@ -37,25 +52,30 @@ select(keyFd + 1, &rdSet, NULL, NULL, NULL); } return ::read(keyFd, &ch, 1) != -1; +#endif } RawKeyboard::RawKeyboard() : data(new Data) { +#ifndef _WIN32 data->keyFd = open("/dev/tty", O_RDONLY | O_NONBLOCK); tcgetattr(data->keyFd, &data->savedFlags); // save state struct termios rawFlags = data->savedFlags; cfmakeraw(&rawFlags); // uncook the input rawFlags.c_oflag = data->savedFlags.c_oflag; // restore output flags (only want to uncook the input) tcsetattr(data->keyFd, TCSANOW, &rawFlags); +#endif } RawKeyboard::~RawKeyboard() { +#ifndef _WIN32 tcsetattr(data->keyFd, TCSANOW, &data->savedFlags); // restore state close(data->keyFd); +#endif delete data; } @@ -85,4 +105,3 @@ return ch; } - diff --git a/research/mini-shell/main.cpp b/research/mini-shell/main.cpp index b879eca..b0c9ddd 100644 --- a/research/mini-shell/main.cpp +++ b/research/mini-shell/main.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #define hash(str) \ @@ -540,7 +541,9 @@ case 'C': printf("%i", (dateTime->tm_year + 1900) / 100); break; // century case 'y': printf("%i", (dateTime->tm_year + 1900) % 100); break; // short year case 'i': printf("%i", dateTime->tm_yday + 1); break; // day from beginning of year (1 based) +#ifndef _WIN32 case 'Z': printf("%s", dateTime->tm_zone); break; // timezone +#endif // Compound formats case 'r': process_date_format(tim, dateTime, "%l:%M:%S %p"); break; @@ -561,7 +564,9 @@ case 'u': process_date_format(tim, dateTime, "%w"); break; case 'G': process_date_format(tim, dateTime, "%Y"); break; case 'H': process_date_format(tim, dateTime, "%k"); break; +#ifndef _WIN32 case 'z': printf("+%i%02i", (int)dateTime->tm_gmtoff / 3600, ((int)dateTime->tm_gmtoff / 60) % 60); break; // tz offset +#endif case 'U': printf("%i", (((7+dateTime->tm_yday) - dateTime->tm_wday)/7)); break; // week of year - Sunday start - starts with week 0 case 'W': printf("%i", (((7+dateTime->tm_yday) - ((dateTime->tm_wday+6)%7))/7)); break; // week of year - Monday start - starts with week 0 case 'V': printf("%i", (dateTime->tm_yday - (((dateTime->tm_wday+6)%7) + 1) + 10) / 7); break; // ISO week, TODO: incorrect around start/end of year