Mike Frysinger | f601376 | 2019-06-13 02:30:51 -0400 | [diff] [blame] | 1 | # -*- coding:utf-8 -*- |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2008 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 17 | from __future__ import print_function |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | import os |
| 19 | import select |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 20 | import subprocess |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | import sys |
| 22 | |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 23 | import platform_utils |
| 24 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 25 | active = False |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 26 | pager_process = None |
| 27 | old_stdout = None |
| 28 | old_stderr = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | |
| 30 | def RunPager(globalConfig): |
Shawn O. Pearce | 8f82a4f | 2009-04-01 07:24:22 -0700 | [diff] [blame] | 31 | if not os.isatty(0) or not os.isatty(1): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | return |
| 33 | pager = _SelectPager(globalConfig) |
| 34 | if pager == '' or pager == 'cat': |
| 35 | return |
| 36 | |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 37 | if platform_utils.isWindows(): |
David Pursehouse | 03ae992 | 2020-02-12 14:14:57 +0900 | [diff] [blame] | 38 | _PipePager(pager) |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 39 | else: |
| 40 | _ForkPager(pager) |
| 41 | |
| 42 | def TerminatePager(): |
| 43 | global pager_process, old_stdout, old_stderr |
| 44 | if pager_process: |
| 45 | sys.stdout.flush() |
| 46 | sys.stderr.flush() |
| 47 | pager_process.stdin.close() |
David Pursehouse | 03ae992 | 2020-02-12 14:14:57 +0900 | [diff] [blame] | 48 | pager_process.wait() |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 49 | pager_process = None |
| 50 | # Restore initial stdout/err in case there is more output in this process |
| 51 | # after shutting down the pager process |
| 52 | sys.stdout = old_stdout |
| 53 | sys.stderr = old_stderr |
| 54 | |
| 55 | def _PipePager(pager): |
| 56 | global pager_process, old_stdout, old_stderr |
| 57 | assert pager_process is None, "Only one active pager process at a time" |
| 58 | # Create pager process, piping stdout/err into its stdin |
| 59 | pager_process = subprocess.Popen([pager], stdin=subprocess.PIPE, stdout=sys.stdout, stderr=sys.stderr) |
| 60 | old_stdout = sys.stdout |
| 61 | old_stderr = sys.stderr |
| 62 | sys.stdout = pager_process.stdin |
| 63 | sys.stderr = pager_process.stdin |
| 64 | |
| 65 | def _ForkPager(pager): |
| 66 | global active |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 67 | # This process turns into the pager; a child it forks will |
| 68 | # do the real processing and output back to the pager. This |
| 69 | # is necessary to keep the pager in control of the tty. |
| 70 | # |
| 71 | try: |
| 72 | r, w = os.pipe() |
| 73 | pid = os.fork() |
| 74 | if not pid: |
| 75 | os.dup2(w, 1) |
| 76 | os.dup2(w, 2) |
| 77 | os.close(r) |
| 78 | os.close(w) |
| 79 | active = True |
| 80 | return |
| 81 | |
| 82 | os.dup2(r, 0) |
| 83 | os.close(r) |
| 84 | os.close(w) |
| 85 | |
| 86 | _BecomePager(pager) |
| 87 | except Exception: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 88 | print("fatal: cannot start pager '%s'" % pager, file=sys.stderr) |
David Pursehouse | 01f443d | 2012-10-03 19:11:28 +0900 | [diff] [blame] | 89 | sys.exit(255) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 90 | |
| 91 | def _SelectPager(globalConfig): |
| 92 | try: |
| 93 | return os.environ['GIT_PAGER'] |
| 94 | except KeyError: |
| 95 | pass |
| 96 | |
| 97 | pager = globalConfig.GetString('core.pager') |
| 98 | if pager: |
| 99 | return pager |
| 100 | |
| 101 | try: |
| 102 | return os.environ['PAGER'] |
| 103 | except KeyError: |
| 104 | pass |
| 105 | |
| 106 | return 'less' |
| 107 | |
| 108 | def _BecomePager(pager): |
| 109 | # Delaying execution of the pager until we have output |
| 110 | # ready works around a long-standing bug in popularly |
| 111 | # available versions of 'less', a better 'more'. |
| 112 | # |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 113 | _a, _b, _c = select.select([0], [], [0]) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 114 | |
| 115 | os.environ['LESS'] = 'FRSX' |
| 116 | |
| 117 | try: |
| 118 | os.execvp(pager, [pager]) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 119 | except OSError: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 120 | os.execv('/bin/sh', ['sh', '-c', pager]) |