[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
| 4 | /* |
---|
| 5 | * ABSTRACT: input from ttys, simulating fgets |
---|
| 6 | */ |
---|
| 7 | |
---|
[16f511] | 8 | #ifdef HAVE_CONFIG_H |
---|
[ba5e9e] | 9 | #include "singularconfig.h" |
---|
[16f511] | 10 | #endif /* HAVE_CONFIG_H */ |
---|
[599326] | 11 | #include <kernel/mod2.h> |
---|
[35aab3] | 12 | |
---|
| 13 | // ---------------------------------------- |
---|
| 14 | // system settings: |
---|
| 15 | |
---|
| 16 | #undef USE_READLINE4 |
---|
| 17 | |
---|
| 18 | //---------------------------------------- |
---|
| 19 | #ifdef ix86_Win |
---|
| 20 | #define READLINE_STATIC |
---|
| 21 | #endif |
---|
[599326] | 22 | #include <kernel/febase.h> |
---|
[b1dfaf] | 23 | #include <omalloc/omalloc.h> |
---|
[0f401f] | 24 | #include <misc/options.h> |
---|
[35aab3] | 25 | |
---|
| 26 | #ifdef HAVE_STATIC |
---|
| 27 | #undef HAVE_DYN_RL |
---|
| 28 | #endif |
---|
| 29 | |
---|
| 30 | #if defined(HAVE_DYN_RL) |
---|
| 31 | #include <unistd.h> |
---|
| 32 | #endif |
---|
| 33 | |
---|
[88d631] | 34 | static char * fe_fgets_stdin_init(const char *pr,char *s, int size); |
---|
| 35 | char * (*fe_fgets_stdin)(const char *pr,char *s, int size) |
---|
[35aab3] | 36 | = fe_fgets_stdin_init; |
---|
| 37 | |
---|
[b6e51b] | 38 | extern char *iiArithGetCmd(int); |
---|
| 39 | |
---|
[35aab3] | 40 | /* ===================================================================*/ |
---|
| 41 | /* = static/dymanic readline = */ |
---|
| 42 | /* ===================================================================*/ |
---|
[e860fa] | 43 | #if defined(HAVE_READLINE) || defined(HAVE_DYN_RL) || defined(HAVE_LIBREADLINE) |
---|
[35aab3] | 44 | |
---|
| 45 | #ifndef STDOUT_FILENO |
---|
| 46 | #define STDOUT_FILENO 1 |
---|
| 47 | #endif |
---|
| 48 | |
---|
| 49 | /* Generator function for command completion. STATE lets us know whether |
---|
| 50 | * to start from scratch; without any state (i.e. STATE == 0), then we |
---|
| 51 | * start at the top of the list. |
---|
| 52 | */ |
---|
| 53 | extern "C" |
---|
| 54 | char *command_generator (char *text, int state) |
---|
| 55 | { |
---|
| 56 | static int list_index, len; |
---|
| 57 | char *name; |
---|
| 58 | |
---|
| 59 | /* If this is a new word to complete, initialize now. This includes |
---|
| 60 | saving the length of TEXT for efficiency, and initializing the index |
---|
| 61 | variable to 0. */ |
---|
| 62 | if (state==0) |
---|
| 63 | { |
---|
| 64 | list_index = 1; |
---|
| 65 | len = strlen (text); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | /* Return the next name which partially matches from the command list. */ |
---|
[b6e51b] | 69 | while ((name = iiArithGetCmd(list_index))!=NULL) |
---|
[35aab3] | 70 | { |
---|
| 71 | list_index++; |
---|
| 72 | |
---|
| 73 | if (strncmp (name, text, len) == 0) |
---|
| 74 | return (strdup(name)); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /* If no names matched, then return NULL. */ |
---|
| 78 | return ((char *)NULL); |
---|
| 79 | } |
---|
| 80 | #endif |
---|
| 81 | |
---|
| 82 | /* ===================================================================*/ |
---|
| 83 | /* = static readline = */ |
---|
| 84 | /* ===================================================================*/ |
---|
| 85 | /* some procedure are shared with "dynamic readline" */ |
---|
[c19e54] | 86 | #if (defined(HAVE_READLINE) || defined(HAVE_LIBREADLINE) || defined(HAVE_DYN_RL)) |
---|
[35aab3] | 87 | #include <unistd.h> |
---|
| 88 | #include <stdio.h> |
---|
| 89 | #include <stdlib.h> |
---|
| 90 | #include <sys/types.h> |
---|
| 91 | #include <sys/file.h> |
---|
| 92 | #include <sys/stat.h> |
---|
| 93 | #include <sys/errno.h> |
---|
| 94 | |
---|
| 95 | // #undef READLINE_READLINE_H_OK |
---|
| 96 | |
---|
| 97 | extern "C" { |
---|
| 98 | typedef char * (*RL_PROC)(const char*,int); |
---|
[c19e54] | 99 | #ifdef READLINE_READLINE_H_OK |
---|
| 100 | #include <readline/readline.h> |
---|
| 101 | #ifdef HAVE_READLINE_HISTORY_H |
---|
| 102 | #include <readline/history.h> |
---|
| 103 | #endif |
---|
[35aab3] | 104 | #endif |
---|
[c19e54] | 105 | |
---|
| 106 | #ifdef RL_VERSION_MAJOR |
---|
| 107 | #if (RL_VERSION_MAJOR >= 4) |
---|
| 108 | #define USE_READLINE4 |
---|
| 109 | #endif |
---|
| 110 | #endif |
---|
| 111 | |
---|
| 112 | #ifndef USE_READLINE4 |
---|
| 113 | #define rl_filename_completion_function filename_completion_function |
---|
| 114 | #define rl_completion_matches completion_matches |
---|
| 115 | #endif |
---|
[a9c298] | 116 | #ifndef READLINE_READLINE_H_OK |
---|
[c19e54] | 117 | /* declare everything we need explicitely and do not rely on includes */ |
---|
| 118 | extern char * rl_readline_name; |
---|
| 119 | extern char *rl_line_buffer; |
---|
| 120 | char *rl_filename_completion_function(const char*, int); |
---|
| 121 | typedef char **CPPFunction (); |
---|
| 122 | |
---|
| 123 | extern char ** rl_completion_matches (const char*, RL_PROC); |
---|
| 124 | extern CPPFunction * rl_attempted_completion_function; |
---|
| 125 | extern FILE * rl_outstream; |
---|
[be2303] | 126 | extern char * readline (const char *); |
---|
[c19e54] | 127 | extern void add_history (char *); |
---|
| 128 | extern int write_history (); |
---|
| 129 | extern void using_history(); |
---|
| 130 | extern int read_history(char *); |
---|
| 131 | extern int history_total_bytes(); |
---|
| 132 | #endif /* READLINE_READLINE_H_OK */ |
---|
| 133 | |
---|
| 134 | typedef char * (*PROC)(); |
---|
[35aab3] | 135 | |
---|
| 136 | typedef char **RL_CPPFunction (const char*, int,int); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
[88d631] | 140 | char * fe_fgets_stdin_rl(const char *pr,char *s, int size); |
---|
[35aab3] | 141 | |
---|
| 142 | /* Tell the GNU Readline library how to complete. We want to try to complete |
---|
| 143 | on command names or on filenames if it is preceded by " */ |
---|
| 144 | |
---|
| 145 | /* Attempt to complete on the contents of TEXT. START and END show the |
---|
| 146 | * region of TEXT that contains the word to complete. We can use the |
---|
| 147 | * entire line in case we want to do some simple parsing. Return the |
---|
| 148 | * array of matches, or NULL if there aren't any. |
---|
| 149 | */ |
---|
[e860fa] | 150 | #if defined(HAVE_DYN_RL) |
---|
[a9c298] | 151 | extern "C" |
---|
[e860fa] | 152 | { |
---|
| 153 | int fe_init_dyn_rl(); |
---|
| 154 | char *(*fe_filename_completion_function)(); /* 3 */ |
---|
[41b8d6] | 155 | char *(* fe_readline) (char *); /* 4 */ |
---|
| 156 | void (*fe_add_history) (char *); /* 5 */ |
---|
[e860fa] | 157 | char ** fe_rl_readline_name; /* 6 */ |
---|
| 158 | char **fe_rl_line_buffer; /* 7 */ |
---|
[c19e54] | 159 | char **(*fe_completion_matches)(...); /* 8 */ |
---|
[e860fa] | 160 | CPPFunction **fe_rl_attempted_completion_function; /* 9 */ |
---|
| 161 | FILE ** fe_rl_outstream; /* 10 */ |
---|
| 162 | int (*fe_write_history) (); /* 11 */ |
---|
| 163 | int (*fe_history_total_bytes) (); /* 12 */ |
---|
| 164 | void (*fe_using_history) (); /* 13 */ |
---|
[41b8d6] | 165 | int (*fe_read_history) (char *); /* 14 */ |
---|
[e860fa] | 166 | |
---|
| 167 | } |
---|
| 168 | #endif |
---|
[35aab3] | 169 | char ** singular_completion (char *text, int start, int end) |
---|
| 170 | { |
---|
| 171 | /* If this word is not in a string, then it may be a command |
---|
| 172 | to complete. Otherwise it may be the name of a file in the current |
---|
| 173 | directory. */ |
---|
[e860fa] | 174 | #ifdef HAVE_DYN_RL |
---|
| 175 | #define x_rl_line_buffer (*fe_rl_line_buffer) |
---|
| 176 | #define x_rl_completion_matches (*fe_completion_matches) |
---|
| 177 | #define x_rl_filename_completion_function (*fe_filename_completion_function) |
---|
| 178 | #else |
---|
| 179 | #define x_rl_line_buffer rl_line_buffer |
---|
| 180 | #define x_rl_completion_matches rl_completion_matches |
---|
| 181 | #define x_rl_filename_completion_function rl_filename_completion_function |
---|
| 182 | #endif |
---|
| 183 | if (x_rl_line_buffer[start-1]=='"') |
---|
| 184 | return x_rl_completion_matches (text, (RL_PROC)x_rl_filename_completion_function); |
---|
| 185 | char **m=x_rl_completion_matches (text, (RL_PROC)command_generator); |
---|
| 186 | #undef x_rl_line_buffer |
---|
| 187 | #undef x_rl_completion_matches |
---|
[35aab3] | 188 | if (m==NULL) |
---|
| 189 | { |
---|
| 190 | m=(char **)malloc(2*sizeof(char*)); |
---|
| 191 | m[0]=(char *)malloc(end-start+2); |
---|
| 192 | strncpy(m[0],text,end-start+1); |
---|
| 193 | m[1]=NULL; |
---|
| 194 | } |
---|
| 195 | return m; |
---|
| 196 | } |
---|
| 197 | |
---|
[e860fa] | 198 | #ifndef HAVE_DYN_RL |
---|
[88d631] | 199 | char * fe_fgets_stdin_rl(const char *pr,char *s, int size) |
---|
[35aab3] | 200 | { |
---|
| 201 | if (!BVERBOSE(V_PROMPT)) |
---|
| 202 | { |
---|
| 203 | pr=""; |
---|
| 204 | } |
---|
| 205 | mflush(); |
---|
| 206 | |
---|
| 207 | char *line; |
---|
| 208 | line = readline (pr); |
---|
| 209 | |
---|
| 210 | if (line==NULL) |
---|
| 211 | return NULL; |
---|
| 212 | |
---|
| 213 | if (*line!='\0') |
---|
| 214 | { |
---|
| 215 | add_history (line); |
---|
| 216 | } |
---|
| 217 | int l=strlen(line); |
---|
| 218 | if (l>=size-1) |
---|
| 219 | { |
---|
| 220 | strncpy(s,line,size); |
---|
| 221 | } |
---|
| 222 | else |
---|
| 223 | { |
---|
| 224 | strncpy(s,line,l); |
---|
| 225 | s[l]='\n'; |
---|
| 226 | s[l+1]='\0'; |
---|
| 227 | } |
---|
| 228 | free (line); |
---|
| 229 | |
---|
| 230 | return s; |
---|
| 231 | } |
---|
| 232 | #endif |
---|
[e860fa] | 233 | #endif |
---|
[35aab3] | 234 | |
---|
| 235 | /* ===================================================================*/ |
---|
| 236 | /* = emulated readline = */ |
---|
| 237 | /* ===================================================================*/ |
---|
| 238 | #if !defined(HAVE_READLINE) && defined(HAVE_FEREAD) |
---|
| 239 | extern "C" { |
---|
[88d631] | 240 | char * fe_fgets_stdin_fe(const char *pr,char *s, int size); |
---|
[35aab3] | 241 | } |
---|
[88d631] | 242 | char * fe_fgets_stdin_emu(const char *pr,char *s, int size) |
---|
[35aab3] | 243 | { |
---|
| 244 | if (!BVERBOSE(V_PROMPT)) |
---|
| 245 | { |
---|
| 246 | pr=""; |
---|
| 247 | } |
---|
| 248 | mflush(); |
---|
| 249 | return fe_fgets_stdin_fe(pr,s,size); |
---|
| 250 | } |
---|
| 251 | #endif |
---|
| 252 | |
---|
| 253 | /* ===================================================================*/ |
---|
| 254 | /* = dynamic readline = */ |
---|
| 255 | /* ===================================================================*/ |
---|
| 256 | /* some procedure are shared with "static readline" */ |
---|
| 257 | #if defined(HAVE_DYN_RL) |
---|
[88d631] | 258 | char * fe_fgets_stdin_drl(const char *pr,char *s, int size) |
---|
[35aab3] | 259 | { |
---|
| 260 | if (!BVERBOSE(V_PROMPT)) |
---|
| 261 | { |
---|
| 262 | pr=""; |
---|
| 263 | } |
---|
| 264 | mflush(); |
---|
| 265 | |
---|
| 266 | char *line; |
---|
[80338a3] | 267 | line = (*fe_readline) ((char*)pr); |
---|
[35aab3] | 268 | |
---|
| 269 | if (line==NULL) |
---|
| 270 | return NULL; |
---|
| 271 | |
---|
| 272 | if (*line!='\0') |
---|
| 273 | { |
---|
| 274 | (*fe_add_history) (line); |
---|
| 275 | } |
---|
| 276 | int l=strlen(line); |
---|
| 277 | if (l>=size-1) |
---|
| 278 | { |
---|
| 279 | strncpy(s,line,size); |
---|
| 280 | } |
---|
| 281 | else |
---|
| 282 | { |
---|
| 283 | strncpy(s,line,l); |
---|
| 284 | s[l]='\n'; |
---|
| 285 | s[l+1]='\0'; |
---|
| 286 | } |
---|
| 287 | free (line); |
---|
| 288 | |
---|
| 289 | return s; |
---|
| 290 | } |
---|
| 291 | #endif |
---|
| 292 | |
---|
| 293 | /* ===================================================================*/ |
---|
| 294 | /* = fgets = */ |
---|
| 295 | /* ===================================================================*/ |
---|
[88d631] | 296 | char * fe_fgets(const char *pr,char *s, int size) |
---|
[35aab3] | 297 | { |
---|
| 298 | if (BVERBOSE(V_PROMPT)) |
---|
| 299 | { |
---|
[9ef9d0] | 300 | fprintf(stdout,"%s",pr); |
---|
[35aab3] | 301 | } |
---|
| 302 | mflush(); |
---|
| 303 | return fgets(s,size,stdin); |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | /* ===================================================================*/ |
---|
| 307 | /* = init for static rl, dyn. rl, emu. rl = */ |
---|
| 308 | /* ===================================================================*/ |
---|
[88d631] | 309 | static char * fe_fgets_stdin_init(const char *pr,char *s, int size) |
---|
[35aab3] | 310 | { |
---|
[e860fa] | 311 | #if (defined(HAVE_READLINE) || defined(HAVE_LIBREADLINE)) && !defined(HAVE_DYN_RL) && !defined(HAVE_FEREAD) |
---|
[35aab3] | 312 | /* Allow conditional parsing of the ~/.inputrc file. */ |
---|
| 313 | rl_readline_name = "Singular"; |
---|
| 314 | /* Tell the completer that we want a crack first. */ |
---|
| 315 | #ifdef USE_READLINE4 |
---|
| 316 | rl_attempted_completion_function = (rl_completion_func_t *)singular_completion; |
---|
| 317 | #else |
---|
| 318 | rl_attempted_completion_function = (CPPFunction *)singular_completion; |
---|
| 319 | #endif |
---|
| 320 | |
---|
| 321 | /* set the output stream */ |
---|
| 322 | if(!isatty(STDOUT_FILENO)) |
---|
| 323 | { |
---|
| 324 | #ifdef atarist |
---|
| 325 | rl_outstream = fopen( "/dev/tty", "w" ); |
---|
| 326 | #else |
---|
| 327 | rl_outstream = fopen( ttyname(fileno(stdin)), "w" ); |
---|
| 328 | #endif |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | /* try to read a history */ |
---|
| 332 | using_history(); |
---|
| 333 | char *p = getenv("SINGULARHIST"); |
---|
| 334 | if (p != NULL) |
---|
| 335 | { |
---|
| 336 | read_history (p); |
---|
| 337 | } |
---|
| 338 | fe_fgets_stdin=fe_fgets_stdin_rl; |
---|
| 339 | return(fe_fgets_stdin_rl(pr,s,size)); |
---|
| 340 | #endif |
---|
| 341 | #ifdef HAVE_DYN_RL |
---|
| 342 | /* do dynamic loading */ |
---|
| 343 | int res=fe_init_dyn_rl(); |
---|
| 344 | if (res!=0) |
---|
| 345 | { |
---|
| 346 | //if (res==1) |
---|
| 347 | // WarnS("dynamic loading of libreadline failed"); |
---|
| 348 | //else |
---|
| 349 | // Warn("dynamic loading failed: %d\n",res); |
---|
| 350 | if (res!=1) |
---|
| 351 | Warn("dynamic loading failed: %d\n",res); |
---|
| 352 | #ifdef HAVE_FEREAD |
---|
| 353 | fe_fgets_stdin=fe_fgets_stdin_emu; |
---|
| 354 | #else |
---|
| 355 | fe_fgets_stdin=fe_fgets; |
---|
| 356 | #endif |
---|
| 357 | return fe_fgets_stdin(pr,s,size); |
---|
| 358 | } |
---|
[e860fa] | 359 | else /* could load libreadline: */ |
---|
| 360 | { |
---|
| 361 | /* Allow conditional parsing of the ~/.inputrc file. */ |
---|
| 362 | *fe_rl_readline_name = "Singular"; |
---|
| 363 | /* Tell the completer that we want a crack first. */ |
---|
| 364 | *fe_rl_attempted_completion_function = (CPPFunction *)singular_completion; |
---|
| 365 | /* try to read a history */ |
---|
| 366 | (*fe_using_history)(); |
---|
| 367 | char *p = getenv("SINGULARHIST"); |
---|
| 368 | if (p != NULL) |
---|
| 369 | { |
---|
| 370 | (*fe_read_history) (p); |
---|
| 371 | } |
---|
| 372 | } |
---|
[35aab3] | 373 | |
---|
| 374 | /* set the output stream */ |
---|
| 375 | if(!isatty(STDOUT_FILENO)) |
---|
| 376 | { |
---|
| 377 | #ifdef atarist |
---|
| 378 | *fe_rl_outstream = fopen( "/dev/tty", "w" ); |
---|
| 379 | #else |
---|
| 380 | *fe_rl_outstream = fopen( ttyname(fileno(stdin)), "w" ); |
---|
| 381 | #endif |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | fe_fgets_stdin=fe_fgets_stdin_drl; |
---|
| 385 | return fe_fgets_stdin_drl(pr,s,size); |
---|
| 386 | #else |
---|
| 387 | #if !defined(HAVE_READLINE) && defined(HAVE_FEREAD) |
---|
| 388 | fe_fgets_stdin=fe_fgets_stdin_emu; |
---|
| 389 | return(fe_fgets_stdin_emu(pr,s,size)); |
---|
| 390 | #else |
---|
| 391 | fe_fgets_stdin=fe_fgets; |
---|
| 392 | return(fe_fgets(pr,s,size)); |
---|
| 393 | #endif |
---|
| 394 | #endif |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | /* ===================================================================*/ |
---|
| 398 | /* = batch mode = */ |
---|
| 399 | /* ===================================================================*/ |
---|
| 400 | /* dummy (for batch mode): */ |
---|
[2e4ec14] | 401 | char * fe_fgets_dummy(const char */*pr*/,char */*s*/, int /*size*/) |
---|
[35aab3] | 402 | { |
---|
| 403 | return NULL; |
---|
| 404 | } |
---|
| 405 | |
---|