finished windows support

This commit is contained in:
Joseph Aquino 2025-12-26 02:13:48 -05:00
parent 41d2d2121f
commit 5b10cc1038
5 changed files with 21 additions and 20 deletions

View File

@ -1,19 +1,19 @@
cmake_minimum_required(VERSION 3.10)
project(shell)
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/windows)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/linux)
endif()
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
add_executable(shell src/main.cpp
src/shellUtils.cpp
include/shellUtils.h)
target_include_directories(shell PRIVATE include)
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT shell)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
endif()

View File

@ -1,5 +1,5 @@
mkdir build
cd build
cmake -S .. -B .
cmake --build .
cmake --build . --config Release
PAUSE

View File

@ -25,7 +25,7 @@ namespace sh
Command getCommand(std::string_view input);
std::optional<std::string> isExec(const std::string& input);
std::optional<std::filesystem::path> isExec(const std::string& input);
void printType(const std::string& input);
}

View File

@ -6,6 +6,8 @@
#include "shellUtils.h"
#define WIN_EXEC .append(".exe")
int main()
{
std::cout << std::unitbuf;
@ -17,7 +19,7 @@ int main()
std::string input;
std::cout << "$ ";
std::getline(std::cin, input);
std::optional<std::string> result;
std::optional<std::filesystem::path> result;
switch (sh::Command command = sh::getCommand(input))
{
case sh::Command::exit:
@ -27,7 +29,7 @@ int main()
std::cout << input.substr(5) << "\n";
break;
case sh::Command::unknown:
result = sh::isExec(input.substr(0, input.find(' ')));
result = sh::isExec(input.substr(0, input.find(' '))WIN_EXEC);
if (result)
{
std::system(input.c_str());
@ -38,7 +40,7 @@ int main()
}
break;
case sh::Command::type:
sh::printType(input.substr(5));
sh::printType(input.substr(5)WIN_EXEC);
break;
}
}

View File

@ -21,7 +21,7 @@ namespace sh
}
}
std::optional<std::string> isExec(const std::string& input)
std::optional<std::filesystem::path> isExec(const std::string& input)
{
const std::string_view pathEnv {std::getenv("PATH")};
@ -34,10 +34,9 @@ namespace sh
size_t nextDelimIndex{pathEnv.find(delimiter)};
while (nextDelimIndex != std::string::npos)
{
std::string currentPath {pathEnv.substr(startCharIndex, nextDelimIndex - startCharIndex)};
std::string fullPath;
fullPath.append(currentPath).append("/").append(input);
std::filesystem::path currentPath {pathEnv.substr(startCharIndex, nextDelimIndex - startCharIndex)};
std::filesystem::path fullPath;
fullPath = currentPath / input;
namespace fs = std::filesystem;
if (!fs::exists(fullPath))