Generalize flushing console buffer

Summary:
This generalizes concept introduced in D3543.
It helps under HPHP and it is useful also in PHP for outputs of size 1 (lower than our buffer size).

Test Plan:
  id(new PhutilArgumentParser(array('', '--recon')))->parseStandardArguments();
  echo "a";
  PhutilConsole::getConsole()->writeOut('b');

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D3562
Add a method to determine console width

Summary:
I'm making `arc tasks` smarter about width-based printing for T1831 and want access to the console width to do so.

Getting it is kind of tricky without curses, which no one has. Get it to work reasonably.

Test Plan: Ran function, got correct output for my terminal.

Reviewers: btrahan, vrana

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T1831

Differential Revision: https://secure.phabricator.com/D3567
Add phutil_utf8_codepoints(), phutil_utf8_console_strlen()

Summary: Reasonably approximate a console-aware display width function for arbitrary utf8 strings so I can make D3568 work correctly.

Test Plan: Unit tests.

Reviewers: btrahan, vrana

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T1831

Differential Revision: https://secure.phabricator.com/D3569
Handle invalid input in PHP lexer

Test Plan: `function<void>`

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D3589
Throw from HTTPSFuture before starting service

Summary:
I've found unterminated service call in error log.
This is not probably it but anyway...

Test Plan: Sending this diff.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D3612
Show URI in HTTP/HTTPSFuture exceptions

Summary:
These are difficult to debug when they stem from misconfigurations and get reported via email. Include the URI that we're trying to request in the exception.

See: https://groups.google.com/forum/?fromgroups=#!topic/phabricator-dev/e31s0kQFAeQ

Test Plan:
Tried to connect to bogus HTTP and HTTPS hosts. Verified errors include URI:

  $ ./scripts/daemon/launch_daemon.php --conduit-uri=http://xvxclnalnlskdnv.com/api/ PhutilNiceDaemon
  Bringing daemon 'PhutilNiceDaemon' online...
  PHP Fatal error:  Uncaught exception 'HTTPFutureResponseStatusTransport' with message '[Transport/4] (http://xvxclnalnlskdnv.com/api/daemon.launched) Connection could not be initiated. This usually indicates a DNS problem: verify the domain name is correct, that you can perform a DNS lookup for it from this machine. (Did you add the domain to `/etc/hosts` on some other machine, but not this one?) This might also indicate that you specified the wrong port.' in /INSECURE/devtools/libphutil/src/future/http/HTTPFuture.php:238

  $ ./scripts/daemon/launch_daemon.php --conduit-uri=https://xvxclnalnlskdnv.com/api/ PhutilNiceDaemon
  Bringing daemon 'PhutilNiceDaemon' online...
  PHP Fatal error:  Uncaught exception 'HTTPFutureResponseStatusCURL' with message '[cURL/6] (https://xvxclnalnlskdnv.com/api/daemon.launched) <CURLE_COULDNT_RESOLVE_HOST> There was an error resolving the server hostname. Check that you are connected to the internet and that DNS is correctly configured. (Did you add the domain to `/etc/hosts` on some other machine, but not this one?)' in /INSECURE/devtools/libphutil/src/future/http/HTTPSFuture.php:159

Reviewers: vrana, btrahan

Reviewed By: btrahan

CC: aran

Differential Revision: https://secure.phabricator.com/D3623
When starting the Phutil lexer in an initial state, push it onto the stack

Summary:
When permissively lexing PHP files, we put the lexer in an initial "php" state (i.e., has see '<?php') if there's no `<?` in the text. This allows us to correctly highlight PHP diffs which don't include full context.

Previously, under Pygments, we would stick "<?php" on the front and then remove it afterward, but that was messy. Now, we just start the lexer in the right state.

However, we currently set the state to the initial state, rather than pushing it onto the stack. This means a snippet like `?>` causes us to pop the state and trigger the "last state was popped" exception.

Instead push the state on to the stack, so if it is popped there's still a 'start' underneath it.

Test Plan: Added a unit test which covers this case. The test fails without this change and passes with it.

Reviewers: vrana, btrahan

Reviewed By: vrana

CC: aran

Maniphest Tasks: T1864

Differential Revision: https://secure.phabricator.com/D3648
Write to STDERR instead of php://stderr

Summary:
It doesn't print anything under HPHP.
I've reported it but in the meantime...

Also the semantics of `file_put_contents('php://stderr')` is somewhat weird.
It suggests that the whole error channel is overwritten by the message which is not the case.

Test Plan:
  $ bin/storage --dry-run

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Maniphest Tasks: T1261

Differential Revision: https://secure.phabricator.com/D3661
Properly escape argument parser exception

Test Plan:
  $ex = new PhutilArgumentUsageException('**test**');
  id(new PhutilArgumentParser(array('')))->printUsageException($ex);

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D3664
Provide class for stopping writing to undeclared properties

Summary:
This generalizes D3607 and future D3601.

It should be trait since PHP 5.4.

Test Plan: Will use it.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin, btrahan

Differential Revision: https://secure.phabricator.com/D3618
Add simple table Remarkup rule

Summary:
Remarkup rules should have these features:

- Easy to write by hand.
- Easy to read in plain text.
- Spellchecker friendly.

Current table rule has none of it.

Test Plan: New test, looked at rendered output in Phabricator.

Reviewers: epriestley

Reviewed By: epriestley

CC: btrahan, aran, Korvin

Differential Revision: https://secure.phabricator.com/D3725
Support multiline commands in console syntax highlighter

Summary: @edward uses this in doc comments.

Test Plan:
  a/ $ b \
  c/ $ d # e
  f

Reviewers: epriestley, edward

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D3747
Use class name and code in aggregate exception

Summary: I plan to use it.

Test Plan:
  throw new PhutilAggregateException('', array(new Exception('a'), new Exception('b')));

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D3755
Stop treating "\r" as a newline

Summary:
None of Git, Mercurial or SVN treat "\r" on its own as a newline under Unix/OS X or Windows. If you diff a file with \r newlines, you get a 1-line change. Do the same thing they do.

(Arguably we could split this into two functions like `phutil_split_lines()` and `phutil_split_vcs_lines()` or something, but I'm happy to call "\r" on its own a non-newline.)

Test Plan: Unit tests.

Reviewers: vrana, btrahan

Reviewed By: vrana

CC: aran

Maniphest Tasks: T1857

Differential Revision: https://secure.phabricator.com/D3757
Allow getting exceptions from PhutilAggregateException

Test Plan: Will use it.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D3760
Use keys in aggregate exception

Test Plan: Will use it.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D3762
Merge remote-tracking branch 'upstream/master'