1 /* sim_readline.c: libreadline wrapper
3 Written by Philipp Hachtmann
6 01-OCT-08 PH Include stdio.h
7 19-SEP-08 PH Initial version
15 #include <readline/readline.h>
16 #include <readline/history.h>
19 #ifdef READLINE_SAVED_HISTORY
25 #define HISTORYFILE ".simh_history"
28 extern char sim_name
[];
30 static char historyfile
[240];
34 void readline_read_history(){
36 #ifdef READLINE_SAVED_HISTORY
45 #ifdef READLINE_SAVED_HISTORY
47 /* Generate lower case file name like ".nova_history" */
49 strncat (filename
,sim_name
,30);
51 for (c
=0;c
<strlen(filename
);c
++)
52 filename
[c
]=tolower(filename
[c
]);
53 strcat(filename
,"_history");
55 char * tmp
=getenv("HOME");
56 historyfile
[0]=historyfile
[200]=0;
59 strncat(historyfile
,tmp
,100);
60 strcat(historyfile
,"/");
63 strcat(historyfile
,filename
);
65 read_history(historyfile
);
71 void readline_write_history(){
72 #ifdef READLINE_SAVED_HISTORY
73 write_history(historyfile
);
78 readline_read_line read line
81 cptr = pointer to buffer
83 prompt = prompt string to use
85 optr = pointer to first non-blank character
89 char *readline_read_line(char *cpre
, int32 size
, const char * prompt
){
91 char * templine
, *result
;
93 templine
=readline(prompt
);
96 if (templine
==NULL
) return NULL
;
98 /* For security reasons */
101 /* Copy the line data to the buffer */
102 strncpy(cpre
,templine
,size
-1);
104 /* Free the temporary buffer allocated by readline() */
110 /* Add current line to history - And don't add repetitions! */
111 if ((strlen(cpre
))&&(strncmp("quit",cpre
,strlen(cpre
))!=0)){
112 if (current_history()){
113 if (strcmp(cpre
,current_history()->line
)!=0)
120 /* Skip spaces and TAB characters */
121 while ((*result
==' ')||(*result
=='\t')) result
++;
123 /* We're finished here */