cmdline.c (362181) | cmdline.c (369302) |
---|---|
1/* 2 * cmdline.c : Helpers for command-line programs. 3 * 4 * ==================================================================== 5 * Licensed to the Apache Software Foundation (ASF) under one 6 * or more contributor license agreements. See the NOTICE file 7 * distributed with this work for additional information 8 * regarding copyright ownership. The ASF licenses this file --- 25 unchanged lines hidden (view full) --- 34#else 35#include <crtdbg.h> 36#include <io.h> 37#include <conio.h> 38#endif 39 40#include <apr.h> /* for STDIN_FILENO */ 41#include <apr_errno.h> /* for apr_strerror */ | 1/* 2 * cmdline.c : Helpers for command-line programs. 3 * 4 * ==================================================================== 5 * Licensed to the Apache Software Foundation (ASF) under one 6 * or more contributor license agreements. See the NOTICE file 7 * distributed with this work for additional information 8 * regarding copyright ownership. The ASF licenses this file --- 25 unchanged lines hidden (view full) --- 34#else 35#include <crtdbg.h> 36#include <io.h> 37#include <conio.h> 38#endif 39 40#include <apr.h> /* for STDIN_FILENO */ 41#include <apr_errno.h> /* for apr_strerror */ |
42#include <apr_version.h> 43#if APR_VERSION_AT_LEAST(1,5,0) |
|
42#include <apr_escape.h> | 44#include <apr_escape.h> |
45#else 46#include "private/svn_dep_compat.h" 47#endif |
|
43#include <apr_general.h> /* for apr_initialize/apr_terminate */ 44#include <apr_strings.h> /* for apr_snprintf */ | 48#include <apr_general.h> /* for apr_initialize/apr_terminate */ 49#include <apr_strings.h> /* for apr_snprintf */ |
50#include <apr_env.h> /* for apr_env_get */ |
|
45#include <apr_pools.h> 46#include <apr_signal.h> 47 48#include "svn_cmdline.h" 49#include "svn_ctype.h" 50#include "svn_dso.h" 51#include "svn_dirent_uri.h" 52#include "svn_hash.h" --- 1177 unchanged lines hidden (view full) --- 1230 else if (force_interactive) 1231 return TRUE; 1232 1233 return !non_interactive; 1234} 1235 1236 1237/* Helper for the edit_externally functions. Set *EDITOR to some path to an | 51#include <apr_pools.h> 52#include <apr_signal.h> 53 54#include "svn_cmdline.h" 55#include "svn_ctype.h" 56#include "svn_dso.h" 57#include "svn_dirent_uri.h" 58#include "svn_hash.h" --- 1177 unchanged lines hidden (view full) --- 1236 else if (force_interactive) 1237 return TRUE; 1238 1239 return !non_interactive; 1240} 1241 1242 1243/* Helper for the edit_externally functions. Set *EDITOR to some path to an |
1238 editor binary. Sources to search include: the EDITOR_CMD argument 1239 (if not NULL), $SVN_EDITOR, the runtime CONFIG variable (if CONFIG | 1244 editor binary, in native C string on Unix/Linux platforms and in UTF-8 1245 string on Windows platform. Sources to search include: the EDITOR_CMD 1246 argument (if not NULL), $SVN_EDITOR, the runtime CONFIG variable (if CONFIG |
1240 is not NULL), $VISUAL, $EDITOR. Return 1241 SVN_ERR_CL_NO_EXTERNAL_EDITOR if no binary can be found. */ 1242static svn_error_t * 1243find_editor_binary(const char **editor, 1244 const char *editor_cmd, | 1247 is not NULL), $VISUAL, $EDITOR. Return 1248 SVN_ERR_CL_NO_EXTERNAL_EDITOR if no binary can be found. */ 1249static svn_error_t * 1250find_editor_binary(const char **editor, 1251 const char *editor_cmd, |
1245 apr_hash_t *config) | 1252 apr_hash_t *config, 1253 apr_pool_t *pool) |
1246{ 1247 const char *e; | 1254{ 1255 const char *e; |
1256 const char *e_cfg; |
|
1248 struct svn_config_t *cfg; | 1257 struct svn_config_t *cfg; |
1258 apr_status_t status; |
|
1249 1250 /* Use the editor specified on the command line via --editor-cmd, if any. */ | 1259 1260 /* Use the editor specified on the command line via --editor-cmd, if any. */ |
1261#ifdef WIN32 1262 /* On Windows, editor_cmd is transcoded to the system active code page 1263 because we use main() as a entry point without APR's (or our own) wrapper 1264 in command line tools. */ 1265 if (editor_cmd) 1266 { 1267 SVN_ERR(svn_utf_cstring_to_utf8(&e, editor_cmd, pool)); 1268 } 1269 else 1270 { 1271 e = NULL; 1272 } 1273#else |
|
1251 e = editor_cmd; | 1274 e = editor_cmd; |
1275#endif |
|
1252 1253 /* Otherwise look for the Subversion-specific environment variable. */ 1254 if (! e) | 1276 1277 /* Otherwise look for the Subversion-specific environment variable. */ 1278 if (! e) |
1255 e = getenv("SVN_EDITOR"); | 1279 { 1280 status = apr_env_get((char **)&e, "SVN_EDITOR", pool); 1281 if (status || ! *e) 1282 { 1283 e = NULL; 1284 } 1285 } |
1256 1257 /* If not found then fall back on the config file. */ 1258 if (! e) 1259 { 1260 cfg = config ? svn_hash_gets(config, SVN_CONFIG_CATEGORY_CONFIG) : NULL; | 1286 1287 /* If not found then fall back on the config file. */ 1288 if (! e) 1289 { 1290 cfg = config ? svn_hash_gets(config, SVN_CONFIG_CATEGORY_CONFIG) : NULL; |
1261 svn_config_get(cfg, &e, SVN_CONFIG_SECTION_HELPERS, | 1291 svn_config_get(cfg, &e_cfg, SVN_CONFIG_SECTION_HELPERS, |
1262 SVN_CONFIG_OPTION_EDITOR_CMD, NULL); | 1292 SVN_CONFIG_OPTION_EDITOR_CMD, NULL); |
1293#ifdef WIN32 1294 if (e_cfg) 1295 { 1296 /* On Windows, we assume that config values are set in system active 1297 code page, so we need transcode it here. */ 1298 SVN_ERR(svn_utf_cstring_to_utf8(&e, e_cfg, pool)); 1299 } 1300#else 1301 e = e_cfg; 1302#endif |
|
1263 } 1264 1265 /* If not found yet then try general purpose environment variables. */ 1266 if (! e) | 1303 } 1304 1305 /* If not found yet then try general purpose environment variables. */ 1306 if (! e) |
1267 e = getenv("VISUAL"); | 1307 { 1308 status = apr_env_get((char**)&e, "VISUAL", pool); 1309 if (status || ! *e) 1310 { 1311 e = NULL; 1312 } 1313 } |
1268 if (! e) | 1314 if (! e) |
1269 e = getenv("EDITOR"); | 1315 { 1316 status = apr_env_get((char**)&e, "EDITOR", pool); 1317 if (status || ! *e) 1318 { 1319 e = NULL; 1320 } 1321 } |
1270 1271#ifdef SVN_CLIENT_EDITOR 1272 /* If still not found then fall back on the hard-coded default. */ 1273 if (! e) 1274 e = SVN_CLIENT_EDITOR; 1275#endif 1276 1277 /* Error if there is no editor specified */ --- 117 unchanged lines hidden (view full) --- 1395 1396svn_error_t * 1397svn_cmdline__edit_file_externally(const char *path, 1398 const char *editor_cmd, 1399 apr_hash_t *config, 1400 apr_pool_t *pool) 1401{ 1402 const char *editor, *cmd, *base_dir, *file_name, *base_dir_apr; | 1322 1323#ifdef SVN_CLIENT_EDITOR 1324 /* If still not found then fall back on the hard-coded default. */ 1325 if (! e) 1326 e = SVN_CLIENT_EDITOR; 1327#endif 1328 1329 /* Error if there is no editor specified */ --- 117 unchanged lines hidden (view full) --- 1447 1448svn_error_t * 1449svn_cmdline__edit_file_externally(const char *path, 1450 const char *editor_cmd, 1451 apr_hash_t *config, 1452 apr_pool_t *pool) 1453{ 1454 const char *editor, *cmd, *base_dir, *file_name, *base_dir_apr; |
1455 const char *file_name_local; 1456#ifdef WIN32 1457 const WCHAR *wcmd; 1458#endif |
|
1403 char *old_cwd; 1404 int sys_err; 1405 apr_status_t apr_err; 1406 1407 svn_dirent_split(&base_dir, &file_name, path, pool); 1408 | 1459 char *old_cwd; 1460 int sys_err; 1461 apr_status_t apr_err; 1462 1463 svn_dirent_split(&base_dir, &file_name, path, pool); 1464 |
1409 SVN_ERR(find_editor_binary(&editor, editor_cmd, config)); | 1465 SVN_ERR(find_editor_binary(&editor, editor_cmd, config, pool)); |
1410 1411 apr_err = apr_filepath_get(&old_cwd, APR_FILEPATH_NATIVE, pool); 1412 if (apr_err) 1413 return svn_error_wrap_apr(apr_err, _("Can't get working directory")); 1414 1415 /* APR doesn't like "" directories */ 1416 if (base_dir[0] == '\0') 1417 base_dir_apr = "."; 1418 else 1419 SVN_ERR(svn_path_cstring_from_utf8(&base_dir_apr, base_dir, pool)); 1420 1421 apr_err = apr_filepath_set(base_dir_apr, pool); 1422 if (apr_err) 1423 return svn_error_wrap_apr 1424 (apr_err, _("Can't change working directory to '%s'"), base_dir); 1425 | 1466 1467 apr_err = apr_filepath_get(&old_cwd, APR_FILEPATH_NATIVE, pool); 1468 if (apr_err) 1469 return svn_error_wrap_apr(apr_err, _("Can't get working directory")); 1470 1471 /* APR doesn't like "" directories */ 1472 if (base_dir[0] == '\0') 1473 base_dir_apr = "."; 1474 else 1475 SVN_ERR(svn_path_cstring_from_utf8(&base_dir_apr, base_dir, pool)); 1476 1477 apr_err = apr_filepath_set(base_dir_apr, pool); 1478 if (apr_err) 1479 return svn_error_wrap_apr 1480 (apr_err, _("Can't change working directory to '%s'"), base_dir); 1481 |
1482 SVN_ERR(svn_path_cstring_from_utf8(&file_name_local, 1483 escape_path(pool, file_name), pool)); |
|
1426 /* editor is explicitly documented as being interpreted by the user's shell, 1427 and as such should already be quoted/escaped as needed. */ | 1484 /* editor is explicitly documented as being interpreted by the user's shell, 1485 and as such should already be quoted/escaped as needed. */ |
1428 cmd = apr_psprintf(pool, "%s %s", editor, escape_path(pool, file_name)); | 1486#ifndef WIN32 1487 cmd = apr_psprintf(pool, "%s %s", editor, file_name_local); |
1429 sys_err = system(cmd); | 1488 sys_err = system(cmd); |
1489#else 1490 cmd = apr_psprintf(pool, "\"%s %s\"", editor, file_name_local); 1491 SVN_ERR(svn_utf__win32_utf8_to_utf16(&wcmd, cmd, NULL, pool)); 1492 sys_err = _wsystem(wcmd); 1493#endif |
|
1430 1431 apr_err = apr_filepath_set(old_cwd, pool); 1432 if (apr_err) 1433 svn_handle_error2(svn_error_wrap_apr 1434 (apr_err, _("Can't restore working directory")), 1435 stderr, TRUE /* fatal */, "svn: "); 1436 1437 if (sys_err) --- 15 unchanged lines hidden (view full) --- 1453 const char *filename, 1454 apr_hash_t *config, 1455 svn_boolean_t as_text, 1456 const char *encoding, 1457 apr_pool_t *pool) 1458{ 1459 const char *editor; 1460 const char *cmd; | 1494 1495 apr_err = apr_filepath_set(old_cwd, pool); 1496 if (apr_err) 1497 svn_handle_error2(svn_error_wrap_apr 1498 (apr_err, _("Can't restore working directory")), 1499 stderr, TRUE /* fatal */, "svn: "); 1500 1501 if (sys_err) --- 15 unchanged lines hidden (view full) --- 1517 const char *filename, 1518 apr_hash_t *config, 1519 svn_boolean_t as_text, 1520 const char *encoding, 1521 apr_pool_t *pool) 1522{ 1523 const char *editor; 1524 const char *cmd; |
1525#ifdef WIN32 1526 const WCHAR *wcmd; 1527#endif |
|
1461 apr_file_t *tmp_file; 1462 const char *tmpfile_name; 1463 const char *tmpfile_native; 1464 const char *base_dir_apr; 1465 svn_string_t *translated_contents; 1466 apr_status_t apr_err; 1467 apr_size_t written; 1468 apr_finfo_t finfo_before, finfo_after; 1469 svn_error_t *err = SVN_NO_ERROR; 1470 char *old_cwd; 1471 int sys_err; 1472 svn_boolean_t remove_file = TRUE; 1473 | 1528 apr_file_t *tmp_file; 1529 const char *tmpfile_name; 1530 const char *tmpfile_native; 1531 const char *base_dir_apr; 1532 svn_string_t *translated_contents; 1533 apr_status_t apr_err; 1534 apr_size_t written; 1535 apr_finfo_t finfo_before, finfo_after; 1536 svn_error_t *err = SVN_NO_ERROR; 1537 char *old_cwd; 1538 int sys_err; 1539 svn_boolean_t remove_file = TRUE; 1540 |
1474 SVN_ERR(find_editor_binary(&editor, editor_cmd, config)); | 1541 SVN_ERR(find_editor_binary(&editor, editor_cmd, config, pool)); |
1475 1476 /* Convert file contents from UTF-8/LF if desired. */ 1477 if (as_text) 1478 { 1479 const char *translated; 1480 SVN_ERR(svn_subst_translate_cstring2(contents->data, &translated, 1481 APR_EOL_STR, FALSE, 1482 NULL, FALSE, pool)); --- 93 unchanged lines hidden (view full) --- 1576 1577 /* Stat it again to get the mtime we actually set. */ 1578 err = svn_io_stat(&finfo_before, tmpfile_name, 1579 APR_FINFO_MTIME | APR_FINFO_SIZE, pool); 1580 if (err) 1581 goto cleanup; 1582 1583 /* Prepare the editor command line. */ | 1542 1543 /* Convert file contents from UTF-8/LF if desired. */ 1544 if (as_text) 1545 { 1546 const char *translated; 1547 SVN_ERR(svn_subst_translate_cstring2(contents->data, &translated, 1548 APR_EOL_STR, FALSE, 1549 NULL, FALSE, pool)); --- 93 unchanged lines hidden (view full) --- 1643 1644 /* Stat it again to get the mtime we actually set. */ 1645 err = svn_io_stat(&finfo_before, tmpfile_name, 1646 APR_FINFO_MTIME | APR_FINFO_SIZE, pool); 1647 if (err) 1648 goto cleanup; 1649 1650 /* Prepare the editor command line. */ |
1584 err = svn_utf_cstring_from_utf8(&tmpfile_native, tmpfile_name, pool); | 1651 err = svn_utf_cstring_from_utf8(&tmpfile_native, 1652 escape_path(pool, tmpfile_name), pool); |
1585 if (err) 1586 goto cleanup; 1587 1588 /* editor is explicitly documented as being interpreted by the user's shell, 1589 and as such should already be quoted/escaped as needed. */ | 1653 if (err) 1654 goto cleanup; 1655 1656 /* editor is explicitly documented as being interpreted by the user's shell, 1657 and as such should already be quoted/escaped as needed. */ |
1590 cmd = apr_psprintf(pool, "%s %s", editor, escape_path(pool, tmpfile_native)); | 1658#ifndef WIN32 1659 cmd = apr_psprintf(pool, "%s %s", editor, tmpfile_native); 1660#else 1661 cmd = apr_psprintf(pool, "\"%s %s\"", editor, tmpfile_native); 1662#endif |
1591 1592 /* If the caller wants us to leave the file around, return the path 1593 of the file we'll use, and make a note not to destroy it. */ 1594 if (tmpfile_left) 1595 { 1596 *tmpfile_left = svn_dirent_join(base_dir, tmpfile_name, pool); 1597 remove_file = FALSE; 1598 } 1599 1600 /* Now, run the editor command line. */ | 1663 1664 /* If the caller wants us to leave the file around, return the path 1665 of the file we'll use, and make a note not to destroy it. */ 1666 if (tmpfile_left) 1667 { 1668 *tmpfile_left = svn_dirent_join(base_dir, tmpfile_name, pool); 1669 remove_file = FALSE; 1670 } 1671 1672 /* Now, run the editor command line. */ |
1673#ifndef WIN32 |
|
1601 sys_err = system(cmd); | 1674 sys_err = system(cmd); |
1675#else 1676 SVN_ERR(svn_utf__win32_utf8_to_utf16(&wcmd, cmd, NULL, pool)); 1677 sys_err = _wsystem(wcmd); 1678#endif |
|
1602 if (sys_err != 0) 1603 { 1604 /* Extracting any meaning from sys_err is platform specific, so just 1605 use the raw value. */ 1606 err = svn_error_createf(SVN_ERR_EXTERNAL_PROGRAM, NULL, 1607 _("system('%s') returned %d"), cmd, sys_err); 1608 goto cleanup; 1609 } --- 204 unchanged lines hidden --- | 1679 if (sys_err != 0) 1680 { 1681 /* Extracting any meaning from sys_err is platform specific, so just 1682 use the raw value. */ 1683 err = svn_error_createf(SVN_ERR_EXTERNAL_PROGRAM, NULL, 1684 _("system('%s') returned %d"), cmd, sys_err); 1685 goto cleanup; 1686 } --- 204 unchanged lines hidden --- |