]>
Commit | Line | Data |
---|---|---|
c7de829c WD |
1 | #include <stdio.h> |
2 | #include <readline/readline.h> | |
3 | #include <readline/history.h> | |
4 | #include <malloc.h> | |
5 | ||
6 | #define PROMPT ">" | |
7 | ||
8 | ||
9 | void | |
10 | getline(char *buf,int *num,int max_num) | |
11 | { | |
12 | static int line_len = 0; | |
13 | static char *line = NULL; | |
14 | static char *line_pointer = NULL; | |
15 | static int len = 0; | |
16 | int tmp_len; | |
17 | char *buff; | |
8bde7f77 | 18 | |
c7de829c WD |
19 | if (len <= 0) { |
20 | buff = readline(PROMPT); | |
21 | add_history(buff); | |
8bde7f77 | 22 | |
c7de829c | 23 | if ((tmp_len = strlen(buff)) > line_len) { |
8bde7f77 WD |
24 | free(line); |
25 | line = malloc(tmp_len); | |
26 | line_len = tmp_len; | |
c7de829c WD |
27 | } |
28 | sprintf(line,"%s\n",buff); | |
29 | free(buff); | |
30 | line_pointer = line; | |
31 | len = strlen(line); | |
32 | } | |
8bde7f77 WD |
33 | |
34 | *num = max_num > len? len : max_num; | |
35 | strncpy(buf,line_pointer,*num); | |
c7de829c WD |
36 | line_pointer = line_pointer + *num; |
37 | len = len - *num; | |
38 | } |