{"id":23071683,"url":"https://github.com/nthnn/myshell","last_synced_at":"2026-05-02T04:39:57.298Z","repository":{"id":266953826,"uuid":"899861311","full_name":"nthnn/MyShell","owner":"nthnn","description":"A modern C++ library that provides a robust, cross-platform interface for shell process interaction that allows seamless execution and interaction with shell commands across Windows, Linux, and macOS operating systems.","archived":false,"fork":false,"pushed_at":"2024-12-12T09:26:30.000Z","size":27,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-09T00:15:19.459Z","etag":null,"topics":["cpp-17","cpp-library","cpp-programming","interactive","shell","shell-integration"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nthnn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-07T07:55:02.000Z","updated_at":"2024-12-12T09:24:47.000Z","dependencies_parsed_at":"2024-12-07T08:37:21.606Z","dependency_job_id":null,"html_url":"https://github.com/nthnn/MyShell","commit_stats":null,"previous_names":["nthnn/myshell"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nthnn%2FMyShell","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nthnn%2FMyShell/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nthnn%2FMyShell/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nthnn%2FMyShell/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nthnn","download_url":"https://codeload.github.com/nthnn/MyShell/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246989504,"owners_count":20865305,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp-17","cpp-library","cpp-programming","interactive","shell","shell-integration"],"created_at":"2024-12-16T07:16:43.349Z","updated_at":"2026-05-02T04:39:57.267Z","avatar_url":"https://github.com/nthnn.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MyShell: Cross-Platform Shell Interaction Library\n\n![Build CI for Linux](https://github.com/nthnn/MyShell/actions/workflows/linux_ci.yml/badge.svg)\n![Build CI for MacOS](https://github.com/nthnn/MyShell/actions/workflows/macos_ci.yml/badge.svg)\n![Build CI for Windows](https://github.com/nthnn/MyShell/actions/workflows/windows_ci.yml/badge.svg)\n\nMyShell is a modern C++ library that provides a robust, cross-platform interface for shell process interaction. It allows seamless execution and interaction with shell commands across Windows, Linux, and macOS operating systems.\n\n- **Cross-Platform Compatibility**: Works consistently across Windows, Linux, and macOS\n- **Bidirectional Communication**: Full support for stdin, stdout, and stderr streams\n- **Real-time Output Processing**: Non-blocking I/O with efficient output buffering\n- **Process Management**: Monitor process status and retrieve exit codes\n- **Resource Safety**: RAII-compliant with automatic resource cleanup\n- **Thread Safety**: Thread-safe output handling with mutex protection\n- **Error Handling**: Comprehensive error reporting using C++ exceptions\n\n## Usage\n\nSimply copy `myshell.hpp` and `myshell.cpp` into your project and include them in your build system.\n\n### Basic Example\n\n```cpp\n#include \u003cchrono\u003e\n#include \u003ciostream\u003e\n#include \u003cmyshell.hpp\u003e\n\nint main() {\n    try {\n        // Create shell instance withcommand\n        MyShell shell(\n            #ifdef _WIN32\n            \"dir\"\n            #else\n            \"ls -ral\"\n            #endif\n        );\n\n        // Give some time for output to be collected\n        std::this_thread::sleep_for(std::chrono::milliseconds(20));\n\n        // Read output\n        std::string output = shell.readShellOutputStream();\n        std::string error = shell.readShellErrorStream();\n\n        // Wait for process completion\n        while(!shell.hasExited());\n\n        // Print results\n        std::cout \u003c\u003c \"Exit Code: \" \u003c\u003c shell.exitCode() \u003c\u003c std::endl;\n        std::cout \u003c\u003c \"Output:\\n\" \u003c\u003c output \u003c\u003c std::endl;\n\n        if(!error.empty())\n            std::cout \u003c\u003c \"Error:\\n\" \u003c\u003c error \u003c\u003c std::endl;\n    }\n    catch(const std::exception\u0026 e) {\n        std::cerr \u003c\u003c \"Error: \" \u003c\u003c e.what() \u003c\u003c std::endl;\n    }\n}\n```\n\n### Interactive Example\n\n```cpp\n#include \u003ciostream\u003e\n#include \u003cmyshell.hpp\u003e\n#include \u003cthread\u003e\n\nint main() {\n    try {\n        // Create shell instance withcommand\n        MyShell shell(\"TERM=dumb vim -u NONE -n test.txt\");\n\n        shell.writeToShell(\"i\");        // Insert mode\n        shell.writeToShell(\"Hi\");       // Write the text\n        shell.writeToShell(\"\\u001b\");   // Exit insert mode (ESC key)\n        shell.writeToShell(\":wq\");      // Write command to save and quit\n        shell.writeToShell(\"\\n\");\n\n        // Wait for process completion\n        while(!shell.hasExited());\n\n        std::cout \u003c\u003c \"Process exited with code: \" \u003c\u003c shell.exitCode() \u003c\u003c std::endl;\n    }\n    catch(const std::exception\u0026 e) {\n        std::cerr \u003c\u003c \"Error: \" \u003c\u003c e.what() \u003c\u003c std::endl;\n    }\n}\n```\n\n## API Reference\n\n### Constructor\n\n```cpp\nMyShell(std::string uuid, std::string command);\n```\n\n- **command**: Command to execute\n\n### Methods\n\n```cpp\nstd::string readShellOutputStream()\n```\n\nReturns accumulated stdout data and clears the internal buffer. *Non-blocking operation*\n\n```cpp\nstd::string readShellErrorStream()\n```\n\nReturns accumulated stderr data and clears the internal buffer. *Non-blocking operation*\n\n```cpp\nvoid writeToShell(std::string input)\n```\n\nWrites input to the shell's stdin. Throws std::system_error if write fails.\n\n```cpp\nvoid forceExit()\n```\n\nForcefully terminates the shell process. Sets exit code to 1.\n\n```cpp\nbool hasExited()\n```\n\nReturns true if the process has exited. Updates internal exit code.\n\n```cpp\nint exitCode()\n```\n\nReturns the process exit code. Returns 0 if process is still running.\n\n```cpp\nint processId()\n```\n\nReturns the system process ID\n\n## Platform-Specific Considerations\n\n### Windows OS Considerations\n\n- Uses Windows API (CreateProcess, pipes)\n- Supports both console and GUI applications\n- UTF-8 encoding support\n\n### Linux/macOS Considerations\n\n- Uses POSIX API (fork, exec, pipes)\n- Full terminal support\n- Signal handling support\n\n### Error Handling\n\nThe library uses C++ exceptions for error reporting:\n\n- `std::system_error` for system-related errors.\n- `std::runtime_error` for general errors.\n- All error messages include detailed descriptions\n\n### Performance Considerations\n\n- Non-blocking I/O operations\n- Efficient buffer management\n- Minimal CPU usage while waiting for output\n- Thread-safe output handling\n\n### Thread Safety\n\n- Safe for concurrent reading of output/error streams\n- Safe for concurrent writing to input stream\n- Thread-safe process status checking\n\n## Common Issues and Solutions\n\n### Process Hanging\n\nIf the process seems to hang, ensure you're:\n\n- Reading both stdout and stderr streams\n- Not filling up the output buffers\n- Properly handling interactive processes\n\n### Memory Usage\n\nThe library efficiently manages memory by:\n\n- Using move semantics for string handling\n- Clearing buffers after reading\n- Automatic resource cleanup\n\n### Platform-Specific Issues\n\n1. Windows:\n    - Use proper line endings (\\r\\n)\n    - Consider code page settings\n    - Handle Unicode properly\n\n2. Linux/macOS:\n    - Consider terminal settings\n    - Handle signals appropriately\n    - Check file permissions\n\n## Contributing\n\nContributions are welcome! Feel free to submit a pull request or open an issue for bugs or feature requests.\n\n## License\n\nMyShell is licensed under the [GNU General Public License v3](LICENSE). You are free to use, modify, and distribute this library under the terms of the license.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnthnn%2Fmyshell","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnthnn%2Fmyshell","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnthnn%2Fmyshell/lists"}