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