Skip to content

Commit 7efb559

Browse files
committed
Only quote php --ini values when out is tty
A comment in php#18527 mentioned that many scripts use `php—-ini` to bootstrap information about the system. Searching on GitHub confirms that many places will not work with quotes out of the box: https://github.com/search?q=content%3A%22php%20--ini%22&type=code. Many seem to be variants on this pattern ``` $ echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"` ``` To preserve these existing scripts, we can detect when the output is a TTY to optionally quote them. This is the new behavior: Output is a tty: ``` $ ./sapi/cli/php --ini Configuration File (php.ini) Path: "/usr/local/lib" Loaded Configuration File: (none) Scan for additional .ini files in: (none) Additional .ini files parsed: (none) ``` Output is not a tty: ``` $ ./sapi/cli/php --ini Configuration File (php.ini) Path: /usr/local/lib Loaded Configuration File: (none) Scan for additional .ini files in: (none) Additional .ini files parsed: (none) ```
1 parent 419b9a7 commit 7efb559

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ PHP NEWS
1414
. Drop support for -z CLI/CGI flag. (nielsdos)
1515
. Fixed GH-17956 - development server 404 page does not adapt to mobiles.
1616
(pascalchevrel)
17+
. Changed - `php --ini` now quotes some values with double quotes, to
18+
help humans debug unexpected whitespace, when stdout is a tty.
19+
(schneems)
1720

1821
- CURL:
1922
. Added CURLFOLLOW_ALL, CURLFOLLOW_OBEYCODE and CURLFOLLOW_FIRSTONLY

sapi/cli/php_cli.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,17 +1106,28 @@ static int do_cli(int argc, char **argv) /* {{{ */
11061106

11071107
case PHP_CLI_MODE_SHOW_INI_CONFIG:
11081108
{
1109-
zend_printf("Configuration File (php.ini) Path: \"%s\"\n", PHP_CONFIG_FILE_PATH);
1110-
if (php_ini_opened_path) {
1111-
zend_printf("Loaded Configuration File: \"%s\"\n", php_ini_opened_path);
1112-
} else {
1113-
zend_printf("Loaded Configuration File: (none)\n");
1114-
}
1115-
if (php_ini_scanned_path) {
1116-
zend_printf("Scan for additional .ini files in: \"%s\"\n", php_ini_scanned_path);
1117-
} else {
1118-
zend_printf("Scan for additional .ini files in: (none)\n");
1119-
}
1109+
int is_tty = 0;
1110+
#ifdef HAVE_UNISTD_H
1111+
is_tty = isatty(STDOUT_FILENO);
1112+
#elif defined(PHP_WIN32)
1113+
is_tty = php_win32_consofileno_is_console(STDOUT_FILENO) && php_win32_console_fileno_has_vt100(STDOUT_FILENO);
1114+
#endif
1115+
char *quote = is_tty ? "\"" : "";
1116+
zend_printf("Configuration File (php.ini) Path: %s%s%s\n",
1117+
quote,
1118+
PHP_CONFIG_FILE_PATH,
1119+
quote
1120+
);
1121+
zend_printf("Loaded Configuration File: %s%s%s\n",
1122+
php_ini_opened_path ? quote : "",
1123+
php_ini_opened_path ? php_ini_opened_path : "(none)",
1124+
php_ini_opened_path ? quote : ""
1125+
);
1126+
zend_printf("Scan for additional .ini files in: %s%s%s\n",
1127+
php_ini_scanned_files ? quote : "",
1128+
php_ini_scanned_files ? php_ini_scanned_files : "(none)",
1129+
php_ini_scanned_files ? quote : ""
1130+
);
11201131
zend_printf("Additional .ini files parsed: %s\n", php_ini_scanned_files ? php_ini_scanned_files : "(none)");
11211132
break;
11221133
}

0 commit comments

Comments
 (0)