1from __future__ import absolute_import
2
3import os
4
5from . import (
6    encoding,
7    pycompat,
8    util,
9    win32,
10)
11
12try:
13    import _winreg as winreg  # pytype: disable=import-error
14
15    winreg.CloseKey
16except ImportError:
17    # py2 only
18    import winreg  # pytype: disable=import-error
19
20# MS-DOS 'more' is the only pager available by default on Windows.
21fallbackpager = b'more'
22
23
24def systemrcpath():
25    '''return default os-specific hgrc search path'''
26    rcpath = []
27    filename = win32.executablepath()
28    # Use mercurial.ini found in directory with hg.exe
29    progrc = os.path.join(os.path.dirname(filename), b'mercurial.ini')
30    rcpath.append(progrc)
31
32    def _processdir(progrcd):
33        if os.path.isdir(progrcd):
34            for f, kind in sorted(util.listdir(progrcd)):
35                if f.endswith(b'.rc'):
36                    rcpath.append(os.path.join(progrcd, f))
37
38    # Use hgrc.d found in directory with hg.exe
39    _processdir(os.path.join(os.path.dirname(filename), b'hgrc.d'))
40
41    # treat a PROGRAMDATA directory as equivalent to /etc/mercurial
42    programdata = encoding.environ.get(b'PROGRAMDATA')
43    if programdata:
44        programdata = os.path.join(programdata, b'Mercurial')
45        _processdir(os.path.join(programdata, b'hgrc.d'))
46
47        ini = os.path.join(programdata, b'mercurial.ini')
48        if os.path.isfile(ini):
49            rcpath.append(ini)
50
51        ini = os.path.join(programdata, b'hgrc')
52        if os.path.isfile(ini):
53            rcpath.append(ini)
54
55    # next look for a system rcpath in the registry
56    value = util.lookupreg(
57        b'SOFTWARE\\Mercurial', None, winreg.HKEY_LOCAL_MACHINE
58    )
59    if value and isinstance(value, bytes):
60        value = util.localpath(value)
61        for p in value.split(pycompat.ospathsep):
62            if p.lower().endswith(b'mercurial.ini'):
63                rcpath.append(p)
64            else:
65                _processdir(p)
66    return rcpath
67
68
69def userrcpath():
70    '''return os-specific hgrc search path to the user dir'''
71    home = _legacy_expanduser(b'~')
72    path = [os.path.join(home, b'mercurial.ini'), os.path.join(home, b'.hgrc')]
73    userprofile = encoding.environ.get(b'USERPROFILE')
74    if userprofile and userprofile != home:
75        path.append(os.path.join(userprofile, b'mercurial.ini'))
76        path.append(os.path.join(userprofile, b'.hgrc'))
77    return path
78
79
80def _legacy_expanduser(path):
81    """Expand ~ and ~user constructs in the pre 3.8 style"""
82
83    # Python 3.8+ changed the expansion of '~' from HOME to USERPROFILE.  See
84    # https://bugs.python.org/issue36264.  It also seems to capitalize the drive
85    # letter, as though it was processed through os.path.realpath().
86    if not path.startswith(b'~'):
87        return path
88
89    i, n = 1, len(path)
90    while i < n and path[i] not in b'\\/':
91        i += 1
92
93    if b'HOME' in encoding.environ:
94        userhome = encoding.environ[b'HOME']
95    elif b'USERPROFILE' in encoding.environ:
96        userhome = encoding.environ[b'USERPROFILE']
97    elif b'HOMEPATH' not in encoding.environ:
98        return path
99    else:
100        try:
101            drive = encoding.environ[b'HOMEDRIVE']
102        except KeyError:
103            drive = b''
104        userhome = os.path.join(drive, encoding.environ[b'HOMEPATH'])
105
106    if i != 1:  # ~user
107        userhome = os.path.join(os.path.dirname(userhome), path[1:i])
108
109    return userhome + path[i:]
110
111
112def termsize(ui):
113    return win32.termsize()
114