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