xref: /PHP-8.2/ext/readline/readline.c (revision 0accfd1f)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Thies C. Arntzen <thies@thieso.net>                          |
14    +----------------------------------------------------------------------+
15 */
16 
17 /* {{{ includes & prototypes */
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include "php.h"
24 #include "php_readline.h"
25 #include "readline_cli.h"
26 #include "readline_arginfo.h"
27 
28 #if HAVE_LIBREADLINE || HAVE_LIBEDIT
29 
30 #ifndef HAVE_RL_COMPLETION_MATCHES
31 #define rl_completion_matches completion_matches
32 #endif
33 
34 #ifdef HAVE_LIBEDIT
35 #include <editline/readline.h>
36 #else
37 #include <readline/readline.h>
38 #include <readline/history.h>
39 #endif
40 
41 #if HAVE_RL_CALLBACK_READ_CHAR
42 
43 static zval _prepped_callback;
44 
45 #endif
46 
47 static zval _readline_completion;
48 static zval _readline_array;
49 
50 PHP_MINIT_FUNCTION(readline);
51 PHP_MSHUTDOWN_FUNCTION(readline);
52 PHP_RSHUTDOWN_FUNCTION(readline);
53 PHP_MINFO_FUNCTION(readline);
54 
55 /* }}} */
56 
57 /* {{{ module stuff */
58 zend_module_entry readline_module_entry = {
59 	STANDARD_MODULE_HEADER,
60 	"readline",
61 	ext_functions,
62 	PHP_MINIT(readline),
63 	PHP_MSHUTDOWN(readline),
64 	NULL,
65 	PHP_RSHUTDOWN(readline),
66 	PHP_MINFO(readline),
67 	PHP_READLINE_VERSION,
68 	STANDARD_MODULE_PROPERTIES
69 };
70 
71 #ifdef COMPILE_DL_READLINE
72 ZEND_GET_MODULE(readline)
73 #endif
74 
PHP_MINIT_FUNCTION(readline)75 PHP_MINIT_FUNCTION(readline)
76 {
77 #if HAVE_LIBREADLINE
78 		/* libedit don't need this call which set the tty in cooked mode */
79 	using_history();
80 #endif
81 	ZVAL_UNDEF(&_readline_completion);
82 #if HAVE_RL_CALLBACK_READ_CHAR
83 	ZVAL_UNDEF(&_prepped_callback);
84 #endif
85 
86 	register_readline_symbols(module_number);
87 
88 	return PHP_MINIT(cli_readline)(INIT_FUNC_ARGS_PASSTHRU);
89 }
90 
PHP_MSHUTDOWN_FUNCTION(readline)91 PHP_MSHUTDOWN_FUNCTION(readline)
92 {
93 	return PHP_MSHUTDOWN(cli_readline)(SHUTDOWN_FUNC_ARGS_PASSTHRU);
94 }
95 
PHP_RSHUTDOWN_FUNCTION(readline)96 PHP_RSHUTDOWN_FUNCTION(readline)
97 {
98 	zval_ptr_dtor(&_readline_completion);
99 	ZVAL_UNDEF(&_readline_completion);
100 #if HAVE_RL_CALLBACK_READ_CHAR
101 	if (Z_TYPE(_prepped_callback) != IS_UNDEF) {
102 		rl_callback_handler_remove();
103 		zval_ptr_dtor(&_prepped_callback);
104 		ZVAL_UNDEF(&_prepped_callback);
105 	}
106 #endif
107 
108 	return SUCCESS;
109 }
110 
PHP_MINFO_FUNCTION(readline)111 PHP_MINFO_FUNCTION(readline)
112 {
113 	PHP_MINFO(cli_readline)(ZEND_MODULE_INFO_FUNC_ARGS_PASSTHRU);
114 }
115 
116 /* }}} */
117 
118 /* {{{ Reads a line */
PHP_FUNCTION(readline)119 PHP_FUNCTION(readline)
120 {
121 	char *prompt = NULL;
122 	size_t prompt_len;
123 	char *result;
124 
125 	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &prompt, &prompt_len)) {
126 		RETURN_THROWS();
127 	}
128 
129 	result = readline(prompt);
130 
131 	if (! result) {
132 		RETURN_FALSE;
133 	} else {
134 		RETVAL_STRING(result);
135 		free(result);
136 	}
137 }
138 
139 /* }}} */
140 
141 #define SAFE_STRING(s) ((s)?(char*)(s):"")
142 
143 /* {{{ Gets/sets various internal readline variables. */
PHP_FUNCTION(readline_info)144 PHP_FUNCTION(readline_info)
145 {
146 	zend_string *what = NULL;
147 	zval *value = NULL;
148 	size_t oldval;
149 	char *oldstr;
150 
151 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!z!", &what, &value) == FAILURE) {
152 		RETURN_THROWS();
153 	}
154 
155 	if (!what) {
156 		array_init(return_value);
157 		add_assoc_string(return_value,"line_buffer",SAFE_STRING(rl_line_buffer));
158 		add_assoc_long(return_value,"point",rl_point);
159 #ifndef PHP_WIN32
160 		add_assoc_long(return_value,"end",rl_end);
161 #endif
162 #ifdef HAVE_LIBREADLINE
163 		add_assoc_long(return_value,"mark",rl_mark);
164 		add_assoc_long(return_value,"done",rl_done);
165 		add_assoc_long(return_value,"pending_input",rl_pending_input);
166 		add_assoc_string(return_value,"prompt",SAFE_STRING(rl_prompt));
167 		add_assoc_string(return_value,"terminal_name",(char *)SAFE_STRING(rl_terminal_name));
168 		add_assoc_str(return_value, "completion_append_character",
169 			rl_completion_append_character == 0
170 				? ZSTR_EMPTY_ALLOC()
171 				: ZSTR_CHAR(rl_completion_append_character));
172 		add_assoc_bool(return_value,"completion_suppress_append",rl_completion_suppress_append);
173 #endif
174 #if HAVE_ERASE_EMPTY_LINE
175 		add_assoc_long(return_value,"erase_empty_line",rl_erase_empty_line);
176 #endif
177 #ifndef PHP_WIN32
178 		add_assoc_string(return_value,"library_version",(char *)SAFE_STRING(rl_library_version));
179 #endif
180 		add_assoc_string(return_value,"readline_name",(char *)SAFE_STRING(rl_readline_name));
181 		add_assoc_long(return_value,"attempted_completion_over",rl_attempted_completion_over);
182 	} else {
183 		if (zend_string_equals_literal_ci(what,"line_buffer")) {
184 			oldstr = rl_line_buffer;
185 			if (value) {
186 				/* XXX if (rl_line_buffer) free(rl_line_buffer); */
187 				if (!try_convert_to_string(value)) {
188 					RETURN_THROWS();
189 				}
190 				rl_line_buffer = strdup(Z_STRVAL_P(value));
191 			}
192 			RETVAL_STRING(SAFE_STRING(oldstr));
193 		} else if (zend_string_equals_literal_ci(what, "point")) {
194 			RETVAL_LONG(rl_point);
195 #ifndef PHP_WIN32
196 		} else if (zend_string_equals_literal_ci(what, "end")) {
197 			RETVAL_LONG(rl_end);
198 #endif
199 #ifdef HAVE_LIBREADLINE
200 		} else if (zend_string_equals_literal_ci(what, "mark")) {
201 			RETVAL_LONG(rl_mark);
202 		} else if (zend_string_equals_literal_ci(what, "done")) {
203 			oldval = rl_done;
204 			if (value) {
205 				rl_done = zval_get_long(value);
206 			}
207 			RETVAL_LONG(oldval);
208 		} else if (zend_string_equals_literal_ci(what, "pending_input")) {
209 			oldval = rl_pending_input;
210 			if (value) {
211 				if (!try_convert_to_string(value)) {
212 					RETURN_THROWS();
213 				}
214 				rl_pending_input = Z_STRVAL_P(value)[0];
215 			}
216 			RETVAL_LONG(oldval);
217 		} else if (zend_string_equals_literal_ci(what, "prompt")) {
218 			RETVAL_STRING(SAFE_STRING(rl_prompt));
219 		} else if (zend_string_equals_literal_ci(what, "terminal_name")) {
220 			RETVAL_STRING((char *)SAFE_STRING(rl_terminal_name));
221 		} else if (zend_string_equals_literal_ci(what, "completion_suppress_append")) {
222 			oldval = rl_completion_suppress_append;
223 			if (value) {
224 				rl_completion_suppress_append = zend_is_true(value);
225 			}
226 			RETVAL_BOOL(oldval);
227 		} else if (zend_string_equals_literal_ci(what, "completion_append_character")) {
228 			oldval = rl_completion_append_character;
229 			if (value) {
230 				if (!try_convert_to_string(value)) {
231 					RETURN_THROWS();
232 				}
233 				rl_completion_append_character = (int)Z_STRVAL_P(value)[0];
234 			}
235 			RETVAL_INTERNED_STR(
236 				oldval == 0 ? ZSTR_EMPTY_ALLOC() : ZSTR_CHAR(oldval));
237 #endif
238 #if HAVE_ERASE_EMPTY_LINE
239 		} else if (zend_string_equals_literal_ci(what, "erase_empty_line")) {
240 			oldval = rl_erase_empty_line;
241 			if (value) {
242 				rl_erase_empty_line = zval_get_long(value);
243 			}
244 			RETVAL_LONG(oldval);
245 #endif
246 #ifndef PHP_WIN32
247 		} else if (zend_string_equals_literal_ci(what,"library_version")) {
248 			RETVAL_STRING((char *)SAFE_STRING(rl_library_version));
249 #endif
250 		} else if (zend_string_equals_literal_ci(what, "readline_name")) {
251 			oldstr = (char*)rl_readline_name;
252 			if (value) {
253 				/* XXX if (rl_readline_name) free(rl_readline_name); */
254 				if (!try_convert_to_string(value)) {
255 					RETURN_THROWS();
256 				}
257 				rl_readline_name = strdup(Z_STRVAL_P(value));
258 			}
259 			RETVAL_STRING(SAFE_STRING(oldstr));
260 		} else if (zend_string_equals_literal_ci(what, "attempted_completion_over")) {
261 			oldval = rl_attempted_completion_over;
262 			if (value) {
263 				rl_attempted_completion_over = zval_get_long(value);
264 			}
265 			RETVAL_LONG(oldval);
266 		}
267 	}
268 }
269 
270 /* }}} */
271 /* {{{ Adds a line to the history */
PHP_FUNCTION(readline_add_history)272 PHP_FUNCTION(readline_add_history)
273 {
274 	char *arg;
275 	size_t arg_len;
276 
277 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
278 		RETURN_THROWS();
279 	}
280 
281 	add_history(arg);
282 
283 	RETURN_TRUE;
284 }
285 
286 /* }}} */
287 /* {{{ Clears the history */
PHP_FUNCTION(readline_clear_history)288 PHP_FUNCTION(readline_clear_history)
289 {
290 	if (zend_parse_parameters_none() == FAILURE) {
291 		RETURN_THROWS();
292 	}
293 
294 #if HAVE_LIBEDIT
295 	/* clear_history is the only function where rl_initialize
296 	   is not call to ensure correct allocation */
297 	using_history();
298 #endif
299 	clear_history();
300 
301 	RETURN_TRUE;
302 }
303 
304 /* }}} */
305 
306 #ifdef HAVE_HISTORY_LIST
307 /* {{{ Lists the history */
PHP_FUNCTION(readline_list_history)308 PHP_FUNCTION(readline_list_history)
309 {
310 	HIST_ENTRY **history;
311 
312 	if (zend_parse_parameters_none() == FAILURE) {
313 		RETURN_THROWS();
314 	}
315 
316 	array_init(return_value);
317 
318 #if defined(HAVE_LIBEDIT) && defined(PHP_WIN32) /* Winedit on Windows */
319 	history = history_list();
320 
321 	if (history) {
322 		int i, n = history_length();
323 		for (i = 0; i < n; i++) {
324 				add_next_index_string(return_value, history[i]->line);
325 		}
326 	}
327 
328 #elif defined(HAVE_LIBEDIT) /* libedit */
329 	{
330 		HISTORY_STATE *hs;
331 		int i;
332 
333 		using_history();
334 		hs = history_get_history_state();
335 		if (hs && hs->length) {
336 			history = history_list();
337 			if (history) {
338 				for (i = 0; i < hs->length; i++) {
339 					add_next_index_string(return_value, history[i]->line);
340 				}
341 			}
342 		}
343 		free(hs);
344 	}
345 
346 #else /* readline */
347 	history = history_list();
348 
349 	if (history) {
350 		int i;
351 		for (i = 0; history[i]; i++) {
352 			add_next_index_string(return_value, history[i]->line);
353 		}
354 	}
355 #endif
356 }
357 /* }}} */
358 #endif
359 
360 /* {{{ Reads the history */
PHP_FUNCTION(readline_read_history)361 PHP_FUNCTION(readline_read_history)
362 {
363 	char *arg = NULL;
364 	size_t arg_len;
365 
366 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|p!", &arg, &arg_len) == FAILURE) {
367 		RETURN_THROWS();
368 	}
369 
370 	if (arg && php_check_open_basedir(arg)) {
371 		RETURN_FALSE;
372 	}
373 
374 	/* XXX from & to NYI */
375 	if (read_history(arg)) {
376 		/* If filename is NULL, then read from `~/.history' */
377 		RETURN_FALSE;
378 	} else {
379 		RETURN_TRUE;
380 	}
381 }
382 
383 /* }}} */
384 /* {{{ Writes the history */
PHP_FUNCTION(readline_write_history)385 PHP_FUNCTION(readline_write_history)
386 {
387 	char *arg = NULL;
388 	size_t arg_len;
389 
390 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|p!", &arg, &arg_len) == FAILURE) {
391 		RETURN_THROWS();
392 	}
393 
394 	if (arg && php_check_open_basedir(arg)) {
395 		RETURN_FALSE;
396 	}
397 
398 	if (write_history(arg)) {
399 		RETURN_FALSE;
400 	} else {
401 		RETURN_TRUE;
402 	}
403 }
404 
405 /* }}} */
406 /* {{{ Readline completion function? */
407 
_readline_command_generator(const char * text,int state)408 static char *_readline_command_generator(const char *text, int state)
409 {
410 	HashTable  *myht = Z_ARRVAL(_readline_array);
411 	zval *entry;
412 
413 	if (!state) {
414 		zend_hash_internal_pointer_reset(myht);
415 	}
416 
417 	while ((entry = zend_hash_get_current_data(myht)) != NULL) {
418 		zend_hash_move_forward(myht);
419 
420 		convert_to_string(entry);
421 		if (strncmp (Z_STRVAL_P(entry), text, strlen(text)) == 0) {
422 			return (strdup(Z_STRVAL_P(entry)));
423 		}
424 	}
425 
426 	return NULL;
427 }
428 
_readline_string_zval(zval * ret,const char * str)429 static void _readline_string_zval(zval *ret, const char *str)
430 {
431 	if (str) {
432 		ZVAL_STRING(ret, (char*)str);
433 	} else {
434 		ZVAL_NULL(ret);
435 	}
436 }
437 
_readline_long_zval(zval * ret,long l)438 static void _readline_long_zval(zval *ret, long l)
439 {
440 	ZVAL_LONG(ret, l);
441 }
442 
php_readline_completion_cb(const char * text,int start,int end)443 char **php_readline_completion_cb(const char *text, int start, int end)
444 {
445 	zval params[3];
446 	char **matches = NULL;
447 
448 	_readline_string_zval(&params[0], text);
449 	_readline_long_zval(&params[1], start);
450 	_readline_long_zval(&params[2], end);
451 
452 	if (call_user_function(NULL, NULL, &_readline_completion, &_readline_array, 3, params) == SUCCESS) {
453 		if (Z_TYPE(_readline_array) == IS_ARRAY) {
454 			SEPARATE_ARRAY(&_readline_array);
455 			if (zend_hash_num_elements(Z_ARRVAL(_readline_array))) {
456 				matches = rl_completion_matches(text,_readline_command_generator);
457 			} else {
458 				/* libedit will read matches[2] */
459 				matches = calloc(3, sizeof(char *));
460 				if (!matches) {
461 					return NULL;
462 				}
463 				matches[0] = strdup("");
464 			}
465 		}
466 	}
467 
468 	zval_ptr_dtor(&params[0]);
469 	zval_ptr_dtor(&_readline_array);
470 
471 	return matches;
472 }
473 
PHP_FUNCTION(readline_completion_function)474 PHP_FUNCTION(readline_completion_function)
475 {
476 	zend_fcall_info fci;
477 	zend_fcall_info_cache fcc;
478 
479 	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "f", &fci, &fcc)) {
480 		RETURN_THROWS();
481 	}
482 
483 	zval_ptr_dtor(&_readline_completion);
484 	ZVAL_COPY(&_readline_completion, &fci.function_name);
485 
486 	/* NOTE: The rl_attempted_completion_function variable (and others) are part of the readline library, not php */
487 	rl_attempted_completion_function = php_readline_completion_cb;
488 	if (rl_attempted_completion_function == NULL) {
489 		RETURN_FALSE;
490 	}
491 	RETURN_TRUE;
492 }
493 
494 /* }}} */
495 
496 #if HAVE_RL_CALLBACK_READ_CHAR
497 
php_rl_callback_handler(char * the_line)498 static void php_rl_callback_handler(char *the_line)
499 {
500 	zval params[1];
501 	zval dummy;
502 
503 	ZVAL_NULL(&dummy);
504 
505 	_readline_string_zval(&params[0], the_line);
506 
507 	call_user_function(NULL, NULL, &_prepped_callback, &dummy, 1, params);
508 
509 	zval_ptr_dtor(&params[0]);
510 	zval_ptr_dtor(&dummy);
511 }
512 
513 /* {{{ Initializes the readline callback interface and terminal, prints the prompt and returns immediately */
PHP_FUNCTION(readline_callback_handler_install)514 PHP_FUNCTION(readline_callback_handler_install)
515 {
516 	char *prompt;
517 	zend_fcall_info fci;
518 	zend_fcall_info_cache fcc;
519 	size_t prompt_len;
520 
521 	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "sf", &prompt, &prompt_len, &fci, &fcc)) {
522 		RETURN_THROWS();
523 	}
524 
525 	if (Z_TYPE(_prepped_callback) != IS_UNDEF) {
526 		rl_callback_handler_remove();
527 		zval_ptr_dtor(&_prepped_callback);
528 	}
529 
530 	ZVAL_COPY(&_prepped_callback, &fci.function_name);
531 
532 	rl_callback_handler_install(prompt, php_rl_callback_handler);
533 
534 	RETURN_TRUE;
535 }
536 /* }}} */
537 
538 /* {{{ Informs the readline callback interface that a character is ready for input */
PHP_FUNCTION(readline_callback_read_char)539 PHP_FUNCTION(readline_callback_read_char)
540 {
541 	if (zend_parse_parameters_none() == FAILURE) {
542 		RETURN_THROWS();
543 	}
544 
545 	if (Z_TYPE(_prepped_callback) != IS_UNDEF) {
546 		rl_callback_read_char();
547 	}
548 }
549 /* }}} */
550 
551 /* {{{ Removes a previously installed callback handler and restores terminal settings */
PHP_FUNCTION(readline_callback_handler_remove)552 PHP_FUNCTION(readline_callback_handler_remove)
553 {
554 	if (zend_parse_parameters_none() == FAILURE) {
555 		RETURN_THROWS();
556 	}
557 
558 	if (Z_TYPE(_prepped_callback) != IS_UNDEF) {
559 		rl_callback_handler_remove();
560 		zval_ptr_dtor(&_prepped_callback);
561 		ZVAL_UNDEF(&_prepped_callback);
562 		RETURN_TRUE;
563 	}
564 	RETURN_FALSE;
565 }
566 /* }}} */
567 
568 /* {{{ Ask readline to redraw the display */
PHP_FUNCTION(readline_redisplay)569 PHP_FUNCTION(readline_redisplay)
570 {
571 	if (zend_parse_parameters_none() == FAILURE) {
572 		RETURN_THROWS();
573 	}
574 
575 #if HAVE_LIBEDIT
576 	/* seems libedit doesn't take care of rl_initialize in rl_redisplay
577 	 * see bug #72538 */
578 	using_history();
579 #endif
580 	rl_redisplay();
581 }
582 /* }}} */
583 
584 #endif
585 
586 #if HAVE_RL_ON_NEW_LINE
587 /* {{{ Inform readline that the cursor has moved to a new line */
PHP_FUNCTION(readline_on_new_line)588 PHP_FUNCTION(readline_on_new_line)
589 {
590 	if (zend_parse_parameters_none() == FAILURE) {
591 		RETURN_THROWS();
592 	}
593 
594 	rl_on_new_line();
595 }
596 /* }}} */
597 
598 #endif
599 
600 
601 #endif /* HAVE_LIBREADLINE */
602