The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2008 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | import os |
| 17 | import select |
| 18 | import sys |
| 19 | |
| 20 | active = False |
| 21 | |
| 22 | def RunPager(globalConfig): |
| 23 | global active |
| 24 | |
Shawn O. Pearce | 8f82a4f | 2009-04-01 07:24:22 -0700 | [diff] [blame] | 25 | 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] | 26 | return |
| 27 | pager = _SelectPager(globalConfig) |
| 28 | if pager == '' or pager == 'cat': |
| 29 | return |
| 30 | |
| 31 | # This process turns into the pager; a child it forks will |
| 32 | # do the real processing and output back to the pager. This |
| 33 | # is necessary to keep the pager in control of the tty. |
| 34 | # |
| 35 | try: |
| 36 | r, w = os.pipe() |
| 37 | pid = os.fork() |
| 38 | if not pid: |
| 39 | os.dup2(w, 1) |
| 40 | os.dup2(w, 2) |
| 41 | os.close(r) |
| 42 | os.close(w) |
| 43 | active = True |
| 44 | return |
| 45 | |
| 46 | os.dup2(r, 0) |
| 47 | os.close(r) |
| 48 | os.close(w) |
| 49 | |
| 50 | _BecomePager(pager) |
| 51 | except Exception: |
| 52 | print >>sys.stderr, "fatal: cannot start pager '%s'" % pager |
David Pursehouse | 01f443d | 2012-10-03 19:11:28 +0900 | [diff] [blame] | 53 | sys.exit(255) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | |
| 55 | def _SelectPager(globalConfig): |
| 56 | try: |
| 57 | return os.environ['GIT_PAGER'] |
| 58 | except KeyError: |
| 59 | pass |
| 60 | |
| 61 | pager = globalConfig.GetString('core.pager') |
| 62 | if pager: |
| 63 | return pager |
| 64 | |
| 65 | try: |
| 66 | return os.environ['PAGER'] |
| 67 | except KeyError: |
| 68 | pass |
| 69 | |
| 70 | return 'less' |
| 71 | |
| 72 | def _BecomePager(pager): |
| 73 | # Delaying execution of the pager until we have output |
| 74 | # ready works around a long-standing bug in popularly |
| 75 | # available versions of 'less', a better 'more'. |
| 76 | # |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 77 | _a, _b, _c = select.select([0], [], [0]) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 78 | |
| 79 | os.environ['LESS'] = 'FRSX' |
| 80 | |
| 81 | try: |
| 82 | os.execvp(pager, [pager]) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 83 | except OSError: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 84 | os.execv('/bin/sh', ['sh', '-c', pager]) |