First Commit of my working state
[simh.git] / sim_readline.c
CommitLineData
196ba1fc
PH
1/* sim_readline.c: libreadline wrapper
2
3 Written by Philipp Hachtmann
4
5
6 01-OCT-08 PH Include stdio.h
7 19-SEP-08 PH Initial version
8
9
10*/
11
12#ifdef HAVE_READLINE
13
14#include <stdio.h>
15#include <readline/readline.h>
16#include <readline/history.h>
17#include "sim_defs.h"
18
19#ifdef READLINE_SAVED_HISTORY
20
21#include <stdlib.h>
22#include <string.h>
23#include <ctype.h>
24
25#define HISTORYFILE ".simh_history"
26
27/* Simulator name */
28extern char sim_name[];
29
30static char historyfile[240];
31
32#endif
33
34void readline_read_history(){
35
36#ifdef READLINE_SAVED_HISTORY
37
38 char filename[40];
39 int c;
40
41#endif
42
43 using_history();
44
45#ifdef READLINE_SAVED_HISTORY
46
47 /* Generate lower case file name like ".nova_history" */
48 strcpy(filename,".");
49 strncat (filename,sim_name,30);
50 filename[39]=0;
51 for (c=0;c<strlen(filename);c++)
52 filename[c]=tolower(filename[c]);
53 strcat(filename,"_history");
54
55 char * tmp=getenv("HOME");
56 historyfile[0]=historyfile[200]=0;
57
58 if (tmp!=NULL){
59 strncat(historyfile,tmp,100);
60 strcat(historyfile,"/");
61 }
62
63 strcat(historyfile,filename);
64
65 read_history(historyfile);
66
67#endif
68
69}
70
71void readline_write_history(){
72#ifdef READLINE_SAVED_HISTORY
73 write_history(historyfile);
74#endif
75}
76
77/*
78 readline_read_line read line
79
80 Inputs:
81 cptr = pointer to buffer
82 size = maximum size
83 prompt = prompt string to use
84 Outputs:
85 optr = pointer to first non-blank character
86 NULL if EOF
87*/
88
89char *readline_read_line(char *cpre, int32 size, const char * prompt){
90
91 char * templine, *result;
92
93 templine=readline(prompt);
94
95 /* Action for EOF */
96 if (templine==NULL) return NULL;
97
98 /* For security reasons */
99 cpre[size-1]=0;
100
101 /* Copy the line data to the buffer */
102 strncpy(cpre,templine,size-1);
103
104 /* Free the temporary buffer allocated by readline() */
105 free(templine);
106
107 result=cpre;
108
109
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)
114 add_history(cpre);
115 } else {
116 add_history(cpre);
117 }
118 }
119
120 /* Skip spaces and TAB characters */
121 while ((*result==' ')||(*result=='\t')) result++;
122
123 /* We're finished here */
124 return result;
125}
126
127
128#endif