~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/win32utils.py

  • Committer: Martin Pool
  • Date: 2010-02-17 06:53:19 UTC
  • mfrom: (4913.5.27 cmdline-splitter)
  • mto: This revision was merged to the branch mainline in revision 5046.
  • Revision ID: mbp@sourcefrog.net-20100217065319-8vbnn4mvyk5herhl
Merge and add news for lp:392248

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import struct
26
26
import sys
27
27
 
 
28
from bzrlib import cmdline
28
29
 
29
30
# Windows version
30
31
if sys.platform == 'win32':
522
523
            trace.mutter('Unable to set hidden attribute on %r: %s', path, e)
523
524
 
524
525
 
525
 
 
526
 
class UnicodeShlex(object):
527
 
    """This is a very simplified version of shlex.shlex.
528
 
 
529
 
    The main change is that it supports non-ascii input streams. The internal
530
 
    structure is quite simplified relative to shlex.shlex, since we aren't
531
 
    trying to handle multiple input streams, etc. In fact, we don't use a
532
 
    file-like api either.
533
 
    """
534
 
 
535
 
    def __init__(self, uni_string):
536
 
        self._input = uni_string
537
 
        self._input_iter = iter(self._input)
538
 
        self._whitespace_match = re.compile(u'\s').match
539
 
        self._word_match = re.compile(u'\S').match
540
 
        self._quote_chars = u'"'
541
 
        # self._quote_match = re.compile(u'[\'"]').match
542
 
        self._escape_match = lambda x: None # Never matches
543
 
        self._escape = '\\'
544
 
        # State can be
545
 
        #   ' ' - after whitespace, starting a new token
546
 
        #   'a' - after text, currently working on a token
547
 
        #   '"' - after ", currently in a "-delimited quoted section
548
 
        #   "\" - after '\', checking the next char
549
 
        self._state = ' '
550
 
        self._token = [] # Current token being parsed
551
 
 
552
 
    def _get_token(self):
553
 
        # Were there quote chars as part of this token?
554
 
        quoted = False
555
 
        quoted_state = None
556
 
        for nextchar in self._input_iter:
557
 
            if self._state == ' ':
558
 
                if self._whitespace_match(nextchar):
559
 
                    # if self._token: return token
560
 
                    continue
561
 
                elif nextchar in self._quote_chars:
562
 
                    self._state = nextchar # quoted state
563
 
                elif self._word_match(nextchar):
564
 
                    self._token.append(nextchar)
565
 
                    self._state = 'a'
566
 
                else:
567
 
                    raise AssertionError('wtttf?')
568
 
            elif self._state in self._quote_chars:
569
 
                quoted = True
570
 
                if nextchar == self._state: # End of quote
571
 
                    self._state = 'a' # posix allows 'foo'bar to translate to
572
 
                                      # foobar
573
 
                elif self._state == '"' and nextchar == self._escape:
574
 
                    quoted_state = self._state
575
 
                    self._state = nextchar
576
 
                else:
577
 
                    self._token.append(nextchar)
578
 
            elif self._state == self._escape:
579
 
                if nextchar == '\\':
580
 
                    self._token.append('\\')
581
 
                elif nextchar == '"':
582
 
                    self._token.append(nextchar)
583
 
                else:
584
 
                    self._token.append('\\' + nextchar)
585
 
                self._state = quoted_state
586
 
            elif self._state == 'a':
587
 
                if self._whitespace_match(nextchar):
588
 
                    if self._token:
589
 
                        break # emit this token
590
 
                    else:
591
 
                        continue # no token to emit
592
 
                elif nextchar in self._quote_chars:
593
 
                    # Start a new quoted section
594
 
                    self._state = nextchar
595
 
                # escape?
596
 
                elif (self._word_match(nextchar)
597
 
                      or nextchar in self._quote_chars
598
 
                      # or whitespace_split?
599
 
                      ):
600
 
                    self._token.append(nextchar)
601
 
                else:
602
 
                    raise AssertionError('state == "a", char: %r'
603
 
                                         % (nextchar,))
604
 
            else:
605
 
                raise AssertionError('unknown state: %r' % (self._state,))
606
 
        result = ''.join(self._token)
607
 
        self._token = []
608
 
        if not quoted and result == '':
609
 
            result = None
610
 
        return quoted, result
611
 
 
612
 
    def __iter__(self):
613
 
        return self
614
 
 
615
 
    def next(self):
616
 
        quoted, token = self._get_token()
617
 
        if token is None:
618
 
            raise StopIteration
619
 
        return quoted, token
620
 
 
621
 
 
622
 
def _command_line_to_argv(command_line):
623
 
    """Convert a Unicode command line into a set of argv arguments.
624
 
 
625
 
    This does wildcard expansion, etc. It is intended to make wildcards act
626
 
    closer to how they work in posix shells, versus how they work by default on
627
 
    Windows.
628
 
    """
629
 
    s = UnicodeShlex(command_line)
630
 
    # Now that we've split the content, expand globs
 
526
def _command_line_to_argv(command_line, single_quotes_allowed=False):
 
527
    """Convert a Unicode command line into a list of argv arguments.
 
528
 
 
529
    It performs wildcard expansion to make wildcards act closer to how they
 
530
    work in posix shells, versus how they work by default on Windows. Quoted
 
531
    arguments are left untouched.
 
532
 
 
533
    :param command_line: The unicode string to split into an arg list.
 
534
    :param single_quotes_allowed: Whether single quotes are accepted as quoting
 
535
                                  characters like double quotes. False by
 
536
                                  default.
 
537
    :return: A list of unicode strings.
 
538
    """
 
539
    s = cmdline.Splitter(command_line, single_quotes_allowed=single_quotes_allowed)
 
540
    # Now that we've split the content, expand globs if necessary
631
541
    # TODO: Use 'globbing' instead of 'glob.glob', this gives us stuff like
632
542
    #       '**/' style globs
633
543
    args = []
641
551
 
642
552
if has_ctypes and winver != 'Windows 98':
643
553
    def get_unicode_argv():
644
 
        LPCWSTR = ctypes.c_wchar_p
645
 
        INT = ctypes.c_int
646
 
        POINTER = ctypes.POINTER
647
 
        prototype = ctypes.WINFUNCTYPE(LPCWSTR)
648
 
        GetCommandLine = prototype(("GetCommandLineW",
649
 
                                    ctypes.windll.kernel32))
650
 
        prototype = ctypes.WINFUNCTYPE(POINTER(LPCWSTR), LPCWSTR, POINTER(INT))
651
 
        command_line = GetCommandLine()
 
554
        prototype = ctypes.WINFUNCTYPE(ctypes.c_wchar_p)
 
555
        GetCommandLineW = prototype(("GetCommandLineW",
 
556
                                     ctypes.windll.kernel32))
 
557
        command_line = GetCommandLineW()
 
558
        if command_line is None:
 
559
            raise ctypes.WinError()
652
560
        # Skip the first argument, since we only care about parameters
653
561
        argv = _command_line_to_argv(command_line)[1:]
654
562
        if getattr(sys, 'frozen', None) is None: