如何使用C编程语言创build一个可以实现历史logging捕获向上箭头的shell?

如何使用C编程语言创build一个可以实现历史logging捕获向上箭头的shell?,第1张

概述如何使用C编程语言创build一个可以实现历史logging捕获向上箭头的shell?

我已经将以前的命令的历史保存在一个二维数组中。 但我不知道如何检查向上箭头。

如何使用实现此function的C编程语言(在linux中)?

触发从邮件dyamnically创build的ahref单击事件

如何追踪SIGFPE / Arithmeticexception

如何将x64机器码写入虚拟内存并在C ++中执行windows

将编码的std :: string从Base16转换为Base10?

两个过程之间的障碍

感谢nm的好建议。

以下是libreadline的使用示例:

// rltest.c #include <stdio.h> #include <stdlib.h> #include <readline.h> #include <history.h> int main(voID) { char* input,shell_prompt[100]; // Configure readline to auto-complete paths when the tab key is hit. rl_bind_key('t',rl_complete); // while work is not 0 program executes the loop int work = 1; printf("Commands to use: name,ver,exit n"); // loop for working with commands while(work) { // Build prompt string. snprintf(shell_prompt,sizeof(shell_prompt),"your command $ "); // display prompt and read input input = readline(shell_prompt); // Check for EOF. if (!input) break; // Add input to history. add_history(input); // Command analysis and execution if( 0 == strcmp(input,"exit") ) { printf("Bye!n"); work = 0; } if( 0 == strcmp(input,"name") ) { printf("I'm readline examplen"); } if( 0 == strcmp(input,"ver") ) { printf("My version is 0.1n"); } // ... // Free input for future use free(input); } return 0; }

编译这个例子:

1)安装readline库

apt-get install libreadline-dev

2)编译程序

gcc rltest.c -I/usr/include/readline -lreadline

试试下面的例子:

#include <stdio.h> #include <termios.h> // constants for sizes #define lines 5 #define MAX_LEN 50 // this is just for demonstration // read more about screen cleaning http://www.cplusplus.com/articles/4z18T05o/ voID ClearScreen() { printf("33[2J"); printf("33[0;0f"); } // function based on termios library // see more at http://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux static struct termios old,new; /* Initialize new terminal I/O settings */ voID initTermios(int echo) { tcgetattr(0,&old); /* grab old terminal I/O settings */ new = old; /* make new settings same as old settings */ new.c_lflag &= ~ICANON; /* disable buffered I/O */ new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */ tcsetattr(0,TCSANow,&new); /* use these new terminal I/O settings Now */ } /* Restore old terminal I/O settings */ voID resetTermios(voID) { tcsetattr(0,&old); } /* Read 1 character - echo defines echo mode */ char getch_(int echo) { char ch; initTermios(echo); ch = getchar(); resetTermios(); return ch; } int main(voID) { char strBuff[lines][MAX_LEN] = {"The first line","This is the longest line in this buffer","Just a mIDdle line","Short","The last line"}; char ch; // one character input buffer int cnt = 0; // number of current line to be shown // loop for showing lines ClearScreen(); do{ // show current line printf("%s",strBuff[cnt]); // read input (arrows. characters,etc. ch = getch_(0); // arrows detection in input if(ch == 27) { ch = getch_(0); if( ch == 91) { ch = getch_(0); if(ch == 66) // up arrow { cnt++; if(cnt >= lines) cnt = 0; } else if(ch == 65) // down arrow { cnt--; if(cnt < 0) cnt = lines-1; } } } // cleaning screen before next output ClearScreen(); }while(ch != 'q'); // press 'q' for exit }

总结

以上是内存溢出为你收集整理的如何使用C编程语言创build一个可以实现历史logging捕获向上箭头的shell?全部内容,希望文章能够帮你解决如何使用C编程语言创build一个可以实现历史logging捕获向上箭头的shell?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1278999.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-09
下一篇2022-06-09

发表评论

登录后才能评论

评论列表(0条)

    保存