歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 實現Linux下上下鍵和命令補全

實現Linux下上下鍵和命令補全

日期:2017/2/28 16:17:42   编辑:Linux教程

可以用readline庫

安裝readline

在eclipse中編譯使用readline需要在連接時加上-Ireadline -Itermcap

#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>

static char *line_read = (char *) NULL;

char *rl_gets() {
if (line_read) {
free(line_read);
line_read = (char *) NULL;
}
line_read = readline("Please Enter:");

if (line_read && *line_read)
add_history(line_read);
return (line_read);
}

int main() {
char *mline;
mline = rl_gets();
printf("%s\n", mline);
}

教學資料

http://www.linuxidc.com/Linux/2011-04/34579p3.htm

用libedit庫

用這庫需要安裝libedit和ncurses-5.6 readline是GPL的,這兩個是非GPL的

一個linux命令示列

  1. /* fileman.c -- A tiny application which demonstrates how to use the
  2. GNU Readline library. This application interactively allows users
  3. to manipulate files and their modes.
  4. NOTE: this was taken from the GNU Readline documentation and ported
  5. to libedit. A commad to output the history list was added.
  6. */
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/file.h>
  10. #include <sys/stat.h>
  11. #include <sys/errno.h>
  12. #include <ctype.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <locale.h>
  17. #include <time.h>
  18. /* GNU readline
  19. #include <readline/readline.h>
  20. #include <readline/history.h>
  21. */
  22. #include <editline/readline.h>
  23. void * xmalloc(size_t size);
  24. void too_dangerous(char *caller);
  25. void initialize_readline();
  26. int execute_line(char *line);
  27. int valid_argument(char *caller, char *arg);
  28. typedef int rl_icpfunc_t(char *);
  29. /* The names of functions that actually do the manipulation. */
  30. int com_list(char *);
  31. int com_view(char *);
  32. int com_history(char *);
  33. int com_rename(char *);
  34. int com_stat(char *);
  35. int com_pwd(char *);
  36. int com_delete(char *);
  37. int com_help(char *);
  38. int com_cd(char *);
  39. int com_quit(char *);
  40. /* A structure which contains information on the commands this program
  41. can understand. */
  42. typedef struct {
  43. char *name; /* User printable name of the function. */
  44. rl_icpfunc_t *func; /* Function to call to do the job. */
  45. char *doc; /* Documentation for this function. */
  46. } COMMAND;
  47. COMMAND commands[] = { { "cd", com_cd, "Change to directory DIR" }, { "delete",
  48. com_delete, "Delete FILE" }, { "help", com_help, "Display this text" },
  49. { "?", com_help, "Synonym for `help'" }, { "list", com_list,
  50. "List files in DIR" },
  51. { "ls", com_list, "Synonym for `list'" }, { "pwd", com_pwd,
  52. "Print the current working directory" }, { "quit", com_quit,
  53. "Quit using Fileman" }, { "rename", com_rename,
  54. "Rename FILE to NEWNAME" }, { "stat", com_stat,
  55. "Print out statistics on FILE" }, { "view", com_view,
  56. "View the contents of FILE" }, { "history", com_history,
  57. "List editline history" }, { (char *) NULL,
  58. (rl_icpfunc_t *) NULL, (char *) NULL } };
  59. /* Forward declarations. */
  60. char *stripwhite(char *string);
  61. COMMAND *find_command(char *name);
  62. /* The name of this program, as taken from argv[0]. */
  63. char *progname;
  64. /* When non-zero, this means the user is done using this program. */
  65. int done;
  66. char *
  67. dupstr(char* s) {
  68. char *r;
  69. r = (char*)xmalloc(strlen(s) + 1);
  70. strcpy(r, s);
  71. return (r);
  72. }
  73. int main(int argc, char **argv) {
  74. char *line, *s;
  75. progname = argv[0];
  76. setlocale(LC_CTYPE, "");
  77. initialize_readline(); /* Bind our completer. */
  78. stifle_history(7);
  79. /* Loop reading and executing lines until the user quits. */
  80. for (; done == 0;) {
  81. line = readline("FileMan: ");
  82. if (!line)
  83. break;
  84. /* Remove leading and trailing whitespace from the line.
  85. Then, if there is anything left, add it to the history list
  86. and execute it. */
  87. s = stripwhite(line);
  88. if (*s) {
  89. char* expansion;
  90. int result;
  91. result = history_expand(s, &expansion);
  92. if (result < 0 || result == 2) {
  93. fprintf(stderr, "%s\n", expansion);
  94. } else {
  95. add_history(expansion);
  96. execute_line(expansion);
  97. }
  98. free(expansion);
  99. }
  100. free(line);
  101. }
  102. exit(0);
  103. return 0;
  104. }
  105. /* Execute a command line. */
  106. int execute_line(char *line) {
  107. register int i;
  108. COMMAND *command;
  109. char *word;
  110. /* Isolate the command word. */
  111. i = 0;
  112. while (line[i] && isspace(line[i]))
  113. i++;
  114. word = line + i;
  115. while (line[i] && !isspace(line[i]))
  116. i++;
  117. if (line[i])
  118. line[i++] = '\0';
  119. command = find_command(word);
  120. if (!command) {
  121. fprintf(stderr, "%s: No such command for FileMan.\n", word);
  122. return (-1);
  123. }
  124. /* Get argument to command, if any. */
  125. while (isspace(line[i]))
  126. i++;
  127. word = line + i;
  128. /* Call the function. */
  129. return ((*(command->func))(word));
  130. }
  131. /* Look up NAME as the name of a command, and return a pointer to that
  132. command. Return a NULL pointer if NAME isn't a command name. */
  133. COMMAND *
  134. find_command(char *name) {
  135. register int i;
  136. for (i = 0; commands[i].name; i++)
  137. if (strcmp(name, commands[i].name) == 0)
  138. return (&commands[i]);
  139. return ((COMMAND *) NULL);
  140. }
  141. /* Strip whitespace from the start and end of STRING. Return a pointer
  142. into STRING. */
  143. char *
  144. stripwhite(char *string) {
  145. register char *s, *t;
  146. for (s = string; isspace(*s); s++)
  147. ;
  148. if (*s == 0)
  149. return (s);
  150. t = s + strlen(s) - 1;
  151. while (t > s && isspace(*t))
  152. t--;
  153. *++t = '\0';
  154. return s;
  155. }
  156. /* **************************************************************** */
  157. /* */
  158. /* Interface to Readline Completion */
  159. /* */
  160. /* **************************************************************** */
  161. char *command_generator(const char *, int);
  162. char **fileman_completion(const char *, int, int);
  163. /* Tell the GNU Readline library how to complete. We want to try to
  164. complete on command names if this is the first word in the line, or
  165. on filenames if not. */
  166. void initialize_readline() {
  167. /* Allow conditional parsing of the ~/.inputrc file. */
  168. rl_readline_name = "FileMan";
  169. /* Tell the completer that we want a crack first. */
  170. rl_attempted_completion_function = fileman_completion;
  171. }
  172. /* Attempt to complete on the contents of TEXT. START and END
  173. bound the region of rl_line_buffer that contains the word to
  174. complete. TEXT is the word to complete. We can use the entire
  175. contents of rl_line_buffer in case we want to do some simple
  176. parsing. Returnthe array of matches, or NULL if there aren't any. */
  177. char **
  178. fileman_completion(const char* text, int start, int end) {
  179. char **matches;
  180. matches = (char **) NULL;
  181. /* If this word is at the start of the line, then it is a command
  182. to complete. Otherwise it is the name of a file in the current
  183. directory. */
  184. if (start == 0)
  185. /* TODO */
  186. matches = completion_matches(text, command_generator);
  187. /* matches = rl_completion_matches (text, command_generator); */
  188. return (matches);
  189. }
  190. /* Generator function for command completion. STATE lets us
  191. know whether to start from scratch; without any state
  192. (i.e. STATE == 0), then we start at the top of the list. */
  193. char *
  194. command_generator(const char *text, int state) {
  195. static int list_index, len;
  196. char *name;
  197. /* If this is a new word to complete, initialize now. This
  198. includes saving the length of TEXT for efficiency, and
  199. initializing the index variable to 0. */
  200. if (!state) {
  201. list_index = 0;
  202. len = strlen(text);
  203. }
  204. /* Return the next name which partially matches from the
  205. command list. */
  206. while (name = commands[list_index].name) {
  207. list_index++;
  208. if (strncmp(name, text, len) == 0)
  209. return (dupstr(name));
  210. }
  211. /* If no names matched, then return NULL. */
  212. return ((char *) NULL);
  213. }
  214. /* **************************************************************** */
  215. /* */
  216. /* FileMan Commands */
  217. /* */
  218. /* **************************************************************** */
  219. /* String to pass to system (). This is for the LIST, VIEW and RENAME
  220. commands. */
  221. static char syscom[1024];
  222. /* List the file(s) named in arg. */
  223. int com_list(char *arg) {
  224. if (!arg)
  225. arg = "";
  226. sprintf(syscom, "ls -FClg %s", arg);
  227. return (system(syscom));
  228. }
  229. int com_view(char *arg) {
  230. if (!valid_argument("view", arg))
  231. return 1;
  232. sprintf(syscom, "more %s", arg);
  233. return (system(syscom));
  234. }
  235. int com_history(char* arg) {
  236. HIST_ENTRY *he;
  237. /* rewind history */
  238. while (next_history())
  239. ;
  240. for (he = current_history(); he != NULL; he = previous_history()) {
  241. //printf("%5d %s\n", *((int*)he->data) - 1, he->line);
  242. printf("%s\n", he->line);
  243. }
  244. return 0;
  245. }
  246. int com_rename(char *arg) {
  247. too_dangerous("rename");
  248. return (1);
  249. }
  250. int com_stat(char *arg) {
  251. struct stat finfo;
  252. if (!valid_argument("stat", arg))
  253. return (1);
  254. if (stat(arg, &finfo) == -1) {
  255. perror(arg);
  256. return (1);
  257. }
  258. printf("Statistics for `%s':\n", arg);
  259. printf("%s has %ld link%s, and is %lld byte%s in length.\n", arg,
  260. (long) finfo.st_nlink, (finfo.st_nlink == 1) ? "" : "s",
  261. (long long) finfo.st_size, (finfo.st_size == 1) ? "" : "s");
  262. printf("Inode Last Change at: %s", ctime(&finfo.st_ctime));
  263. printf(" Last access at: %s", ctime(&finfo.st_atime));
  264. printf(" Last modified at: %s", ctime(&finfo.st_mtime));
  265. return (0);
  266. }
  267. int com_delete(char *arg) {
  268. too_dangerous("delete");
  269. return (1);
  270. }
  271. /* Print out help for ARG, or for all of the commands if ARG is
  272. not present. */
  273. int com_help(char *arg) {
  274. register int i;
  275. int printed = 0;
  276. for (i = 0; commands[i].name; i++) {
  277. if (!*arg || (strcmp(arg, commands[i].name) == 0)) {
  278. printf("%s\t\t%s.\n", commands[i].name, commands[i].doc);
  279. printed++;
  280. }
  281. }
  282. if (!printed) {
  283. printf("No commands match `%s'. Possibilties are:\n", arg);
  284. for (i = 0; commands[i].name; i++) {
  285. /* Print in six columns. */
  286. if (printed == 6) {
  287. printed = 0;
  288. printf("\n");
  289. }
  290. printf("%s\t", commands[i].name);
  291. printed++;
  292. }
  293. if (printed)
  294. printf("\n");
  295. }
  296. return (0);
  297. }
  298. /* Change to the directory ARG. */
  299. int com_cd(char *arg) {
  300. if (chdir(arg) == -1) {
  301. perror(arg);
  302. return 1;
  303. }
  304. com_pwd("");
  305. return (0);
  306. }
  307. /* Print out the current working directory. */
  308. int com_pwd(char* ignore) {
  309. char dir[1024], *s;
  310. s = (char*) getcwd(dir, sizeof(dir) - 1);
  311. if (s == 0) {
  312. printf("Error getting pwd: %s\n", dir);
  313. return 1;
  314. }
  315. printf("Current directory is %s\n", dir);
  316. return 0;
  317. }
  318. /* The user wishes to quit using this program. Just set DONE
  319. non-zero. */
  320. int com_quit(char *arg) {
  321. done = 1;
  322. return (0);
  323. }
  324. /* Function which tells you that you can't do this. */
  325. void too_dangerous(char *caller) {
  326. fprintf(stderr, "%s: Too dangerous for me to distribute.\n", caller);
  327. fprintf(stderr, "Write it yourself.\n");
  328. }
  329. /* Return non-zero if ARG is a valid argument for CALLER,
  330. else print an error message and return zero. */
  331. int valid_argument(char *caller, char *arg) {
  332. if (!arg || !*arg) {
  333. fprintf(stderr, "%s: Argument required.\n", caller);
  334. return (0);
  335. }
  336. return (1);
  337. }
  338. void *
  339. xmalloc(size_t size) {
  340. register void *value = (void*) malloc(size);
  341. if (value == 0)
  342. fprintf(stderr, "virtual memory exhausted");
  343. return value;
  344. }
Copyright © Linux教程網 All Rights Reserved