用C编写命令行shell;第一次尝试使用 ncurses/C

2023-12-21

我正在开发一个类项目,在该项目中我必须编写一个具有以下要求的命令行 shell:

  • shell 必须能够读取缓冲的输入
  • 缓冲区应为 64 个字符
  • Error conditions should be handled
    • 超出缓冲区大小
    • 中断(当信号到达时)——请参阅 read() 的手册页
    • 无效输入(无法解析的字符、空行等)
    • 可能遇到的任何其他错误。
  • Shell 必须具有至少 20 项的历史记录,并且历史记录不得为静态大小。当历史缓冲区已满时,将删除最旧的项目并添加最新的项目。
  • 程序应该能够在前台或后台运行。 (使用 &)
  • Ctrl-D 将退出 shell
  • Ctrl-C 将打印完整的历史记录
  • 命令“历史”还将打印完整的历史记录。最新的项目将位于列表的底部。
  • 所有其他信号将被捕获并在 shell 中显示给用户
  • 程序将使用 read() 命令读取输入,除非支持箭头键

我选择实现历史循环的箭头键,因此我使用 ncurses 进行输入,而不是 read()。我认为我使用 strtok() 解析输入,使用 fork() 和 execvp() 运行进程做得很好,但我没有正确实现 ncurses。到目前为止,我所做的就是初始化一个新屏幕,显示提示,然后在按下任何键时出现段错误。不好。

我想问题一定出在我的设计上。我不太了解 ncurses。我应该在这个项目中使用哪种数据结构?我应该如何处理 ncurses 设置、拆卸以及其间的所有事情?窗口和屏幕有什么关系?我应该使用一个可全局访问的窗口/屏幕吗?另外,我一直在尝试使用 char* 作为输入缓冲区,使用 char** 作为命令历史记录,但我没有 C 经验,所以尽管阅读了 malloc、calloc 和 realloc,但我还是不确定在缓冲区和历史记录中存储命令的最佳方式。关于管理这些字符数组有什么建议吗?

tl;dr: 如何正确使用 ncurses 来制作命令行 shell,以及如何使用 C 处理命令内存管理?

我意识到这是一个相当沉重的问题。 :(

编辑:我已经看过了http://www.gnu.org/software/libc/manual/html_node/Implementing-a-Shell.html http://www.gnu.org/software/libc/manual/html_node/Implementing-a-Shell.html and http://www.linuxinfor.com/english/NCURSES-Programming/ http://www.linuxinfor.com/english/NCURSES-Programming/但 ncurses 文档实际上有太多的开销。我只是想利用它识别方向键的能力。


这是一些示例代码:

  1. 执行动态内存分配。

  2. 以非阻塞模式从控制台读取。

  3. 使用 VT100 代码将帧缓冲区打印到控制台。

它使用 GCC 在 Linux 上编译,没有警告或错误。它远非没有错误,但它应该给您一些关于可能性的想法。编译并运行它,按[向上]和[向下]将打印消息,输入字符并按[输入]将“执行”命令。

#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/** VT100 command to clear the screen. Use puts(VT100_CLEAR_SCREEN) to clear
 *  the screen. */
#define VT100_CLEAR_SCREEN "\033[2J"

/** VT100 command to reset the cursor to the top left hand corner of the
 *  screen. */
#define VT100_CURSOR_TO_ORIGIN "\033[H"

struct frame_s
{
    int x;
    int y;
    char *data;
};

static int draw_frame(struct frame_s *frame)
{
    int row;
    char *data;
    int attrib;

    puts(VT100_CLEAR_SCREEN);
    puts(VT100_CURSOR_TO_ORIGIN);

    for (row = 0, data = frame->data; row  < frame->y; row++, data += frame->x)
    {
        /*  0 for normal, 1 for bold, 7 for reverse. */
        attrib = 0;

        /*  The VT100 commands to move the cursor, set the attribute, and the
         *  actual frame line. */
        fprintf(stdout, "\033[%d;%dH\033[0m\033[%dm%.*s", row + 1, 0, attrib, frame->x, data);
        fflush(stdout);
    }

    return (0);
}

int main(void)
{
    const struct timespec timeout = { .tv_sec = 1, .tv_nsec = 0 };
    struct frame_s frame;
    struct termios tty_old;
    struct termios tty_new;
    unsigned char line[128];
    unsigned int count = 0;
    int ret;
    struct pollfd fds[1];
    sigset_t sigmask;
    struct tm *tp;
    time_t current_time;

    /*  Set up a little frame. */
    frame.x = 80;
    frame.y = 5;
    frame.data = malloc(frame.x * frame.y);

    if (frame.data == NULL)
    {
        fprintf(stderr, "No memory\n");
        exit (1);
    }

    memset(frame.data, ' ', frame.x * frame.y);

    /*  Get the terminal state. */
    tcgetattr(STDIN_FILENO, &tty_old);
    tty_new = tty_old;

    /*  Turn off "cooked" mode (line buffering) and set minimum characters
     *  to zero (i.e. non-blocking). */
    tty_new.c_lflag &= ~ICANON;
    tty_new.c_cc[VMIN] = 0;

    /*  Set the terminal attributes. */
    tcsetattr(STDIN_FILENO, TCSANOW, &tty_new);

    /*  Un-mask all signals while in ppoll() so any signal will cause
     *  ppoll() to return prematurely. */
    sigemptyset(&sigmask);

    fds[0].events = POLLIN;
    fds[0].fd = STDIN_FILENO;

    /*  Loop forever waiting for key presses. Update the output on every key
     *  press and every 1.0s (when ppoll() times out). */
    do
    {
        fds[0].revents = 0;
        ret = ppoll(fds, sizeof(fds) / sizeof(struct pollfd), &timeout, &sigmask);

        if (fds[0].revents & POLLIN)
        {
            ret = read(STDIN_FILENO, &line[count], sizeof(line) - count);

            if (ret > 0)
            {
                line[count + ret] = '\0';

                if (strcmp(&line[count], "\033[A") == 0)
                {
                    snprintf(frame.data, frame.x, "up");
                    count = 0;
                }
                else if (strcmp(&line[count], "\033[B") == 0)
                {
                    snprintf(frame.data, frame.x, "down");
                    count = 0;
                }
                else if (line[count] == 127) // backspace
                {
                    if (count != 0) { count -= ret;}
                }
                else if (line[count] == '\n')
                {
                    snprintf(frame.data, frame.x, "entered: %s", line);
                    count = 0;
                }
                else
                {
                    count += ret;
                }
            }
        }

        /*  Print the current time to the output buffer. */
        current_time = time(NULL);
        tp = localtime(&current_time);
        strftime(&frame.data[1 * frame.x], frame.x, "%Y/%m/%d %H:%M:%S", tp);

        /*  Print the command line. */
        line[count] = '\0';
        snprintf(&frame.data[(frame.y - 1) * frame.x], frame.x, "$ %s", line);

        draw_frame(&frame);
    }
    while (1);

    /*  Restore terminal and free resources. */
    tcsetattr(STDIN_FILENO, TCSANOW, &tty_old);
    free(frame.data);

    return (0);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

用C编写命令行shell;第一次尝试使用 ncurses/C 的相关文章

  • Fortran DLL 导入

    Fortran 中有一段代码罗伯特 L 帕克和菲利普 B 斯塔克 http www stat berkeley edu 7Estark Code sbvq f FORTRAN subroutine bv key m n a b bl bu
  • 剃刀 2 到剃刀 3 MVC 5

    我一直在开发 MVC 4 解决方案 并且一直在尝试将其升级到 MVC 5 我已按照概述的步骤操作here http www asp net mvc tutorials mvc 5 how to upgrade an aspnet mvc 4
  • 通过 Microsoft Graph 从 Azure AD 获取组中的用户

    我正在通过 Microsoft Graph 从 AzureAD 请求用户列表 我取回了 User 对象 但它们的 MemberOf 属性始终为 null 我认为我可以使用 Expand 来专门请求该属性 虽然它不会导致错误 但它也不会填充该
  • 从 Web 浏览器控件读取 Javascript 变量

    我正在尝试读取从表单上的 WebBrowser 控件加载和调用的 Javascript 变量的值 Example index html 引用名为 test js 的 javascript 在 test js 上 创建并填充了几个变量 然后i
  • Serilog 与 Autofac

    我有一个记录器包装器 我想使用以下配置将 serilog 注入其中 var logger new LoggerConfiguration WriteTo RollingFile AppDomain CurrentDomain GetData
  • 将二进制文件内容读取到 std::string 的最佳方法是什么? [复制]

    这个问题在这里已经有答案了 可能的重复 如何将 istream 与字符串一起使用 https stackoverflow com questions 6510923 how to use istream with strings std i
  • .NET 4.0 进程中的 clr.dll!LogHelp_TerminateOnAssert

    背景 我正在开发一个基于 WinForm 的 NET 4 0 桌面应用程序 该应用程序几乎没有线程和计时器 并对用户控件使用一些 GDI 处理 在我的开发过程中 我通常会查看 sysinternal 的 Process Explorer 以
  • 在硬件不足的情况下进行编码

    我目前正在使用 C 中的 SIMD 指令进行编码 并尝试使用 IDE 在实时编码时显示错误 拼写错误等 问题是 我使用的是 AVX512 指令 我的硬件不支持这些指令 只有我用于编译的服务器支持 有没有一种方法可以在 IDE 中进行错误检查
  • 通过模板参数向类添加方法

    我希望在类中拥有一个模板参数特定函数 取消启用enable if 它的名称保持不变 参数类型有所不同 尽管这应该不相关 因为只有一个被初始化 enum class MyCases CASE1 CASE2 template
  • C# Response.Write pdf 不适用于 Android 浏览器

    我目前在 Android 环境中使用 pdf 导出时遇到了巨大的问题 我正在使用报告查看器控件将报告呈现为字节数组 接下来我使用response binarywrite方法将字节流输出到浏览器 这适用于所有浏览器以及 iPhone 和 iP
  • ElementReference 对 Blazor 中条件创建的元素的引用

    我正在尝试将焦点设置为有条件呈现的输入控件 我正在设置ElementReference但它的 id 和 context 都是空的
  • 如何在类中使用常量类变量声明常量数组?

    如何在类中使用常量类变量声明常量数组 是否可以 我不想要动态数组 我的意思是这样的 class test const int size int array size public test size 50 int main test t 5
  • 使用日期时间作为文件名并随后解析文件名?

    我正在将文件写入硬盘 文件名是这样构建的 String Format 0 yyyy MM dd hh mm ss txt DateTime Now 例如 文件名是 2010 09 20 09 47 04 txt 现在我想在下拉列表中显示这些
  • 检查SQL Server数据库表中是否存在表或列

    在 SQL Server 数据库中创建列或表之前 我想检查所需的表和 或列是否存在 我已经四处搜寻 到目前为止发现了两种方法 我不想使用存储过程 通过使用SqlCommand ExecuteScalar 方法并捕获异常来确定表 列是否存在
  • 在异步方法中显示错误消息的更好方法

    事实上我们不能使用await关键字在catch块使得在 WinRT 中显示来自异步方法的错误消息变得非常尴尬 因为MessageDialogAPI 是异步的 理想情况下我希望能够这样写 private async Task DoSometh
  • 为什么%c前面需要加空格? [复制]

    这个问题在这里已经有答案了 下面的代码一编译就给出了奇怪的o p main char name 3 float price 3 int pages 3 i printf nEnter names prices and no of pages
  • ASP.NET:通过命名空间一次注册多个控件?

    是否可以在 aspx 文件中注册用户控件的完整命名空间 而不是单独注册每个控件 我创建了一堆用户控件并将它们收集到自己的命名空间 MyWebControls 中 如下所示 隐藏代码 namespace MyWebControls publi
  • C++中main函数可以调用自身吗?

    谁能告诉我下面的代码有什么问题吗 int main return main 我测试了一下 编译正确 它永远运行 幕后还有什么阴谋吗 TLDR 呼叫main导致未定义的行为 标准中使用的术语以及对程序员和编译器的影响似乎存在混淆 首先 单独的
  • C++ 中的无符号双精度?

    为什么 C 不支持无符号双精度语法 因为典型的浮点格式不支持无符号数 例如 参见此 IEEE 754 格式列表 http en wikipedia org wiki IEEE 754 2008 Formats 添加通用硬件不支持的数字格式只
  • ASP.NET 中的 ThreadStaticAttribute

    我有一个需要存储的组件static每个线程的值 它是一个通用组件 可以在许多场景中使用 而不仅仅是在 ASP NET 中 我想用 ThreadStatic 属性来实现我的目标 假设它在 ASP NET 场景中也能正常工作 因为我假设每个请求

随机推荐