diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 00000000..ea4e6439 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: nviennot diff --git a/.github/workflows/DragonflyBSD.yml b/.github/workflows/DragonflyBSD.yml new file mode 100644 index 00000000..f014da9b --- /dev/null +++ b/.github/workflows/DragonflyBSD.yml @@ -0,0 +1,41 @@ +name: DragonflyBSD +on: + push: + branches: + - '*' + paths: + - '**.c' + - '**.h' + - 'compat/*' + - '.github/workflows/DragonflyBSD.yml' + pull_request: + branches: + - '*' + paths: + - '**.c' + - '**.h' + - 'compat/*' + - '.github/workflows/DragonflyBSD.yml' + + + +jobs: + DragonflyBSD: + runs-on: macos-12 + steps: + - uses: actions/checkout@v2 + - uses: vmactions/dragonflybsd-vm@v0 + with: + prepare: | + pkg install -y automake autoconf libtool pkgconf libevent msgpack libssh gsed + usesh: true + run: | + gsed -i "s/OK/0/g" tty-term.c + autoupdate + ./autogen.sh + ./configure + make + make install + + + diff --git a/.github/workflows/FreeBSD.yml b/.github/workflows/FreeBSD.yml new file mode 100644 index 00000000..e879618e --- /dev/null +++ b/.github/workflows/FreeBSD.yml @@ -0,0 +1,40 @@ +name: FreeBSD +on: + push: + branches: + - '*' + paths: + - '**.c' + - '**.h' + - 'compat/*' + - '.github/workflows/FreeBSD.yml' + pull_request: + branches: + - '*' + paths: + - '**.c' + - '**.h' + - 'compat/*' + - '.github/workflows/FreeBSD.yml' + + + +jobs: + FreeBSD: + runs-on: macos-12 + steps: + - uses: actions/checkout@v2 + - uses: vmactions/freebsd-vm@v0 + with: + prepare: | + pkg install -y automake autoconf libtool pkgconf libevent msgpack libssh + usesh: true + run: | + autoupdate + ./autogen.sh + ./configure + make + make install + + + diff --git a/.github/workflows/MacOS.yml b/.github/workflows/MacOS.yml new file mode 100644 index 00000000..3dc5247e --- /dev/null +++ b/.github/workflows/MacOS.yml @@ -0,0 +1,37 @@ +name: MacOS +on: + push: + branches: + - '*' + paths: + - '**.c' + - '**.h' + - 'compat/*' + - '.github/workflows/MacOS.yml' + pull_request: + branches: + - '*' + paths: + - '**.c' + - '**.h' + - 'compat/*' + - '.github/workflows/MacOS.yml' + + + +jobs: + MacOS: + runs-on: macos-latest + steps: + - uses: actions/checkout@v2 + - run: | + brew install automake msgpack libssh + autoupdate + ./autogen.sh + ./configure + make + make install + + + + diff --git a/.github/workflows/NetBSD.yml b/.github/workflows/NetBSD.yml new file mode 100644 index 00000000..0bb0924a --- /dev/null +++ b/.github/workflows/NetBSD.yml @@ -0,0 +1,40 @@ +name: NetBSD +on: + push: + branches: + - '*' + paths: + - '**.c' + - '**.h' + - 'compat/*' + - '.github/workflows/NetBSD.yml' + pull_request: + branches: + - '*' + paths: + - '**.c' + - '**.h' + - 'compat/*' + - '.github/workflows/NetBSD.yml' + + + +jobs: + NetBSD: + runs-on: macos-12 + steps: + - uses: actions/checkout@v2 + - uses: vmactions/netbsd-vm@v0 + with: + prepare: | + pkg_add automake autoconf libtool pkgconf libevent msgpack libssh + usesh: true + run: | + autoupdate + ./autogen.sh + ./configure + make + make install + + + diff --git a/.github/workflows/OpenBSD.yml b/.github/workflows/OpenBSD.yml new file mode 100644 index 00000000..8fb7901b --- /dev/null +++ b/.github/workflows/OpenBSD.yml @@ -0,0 +1,50 @@ +name: OpenBSD +on: + push: + branches: + - '*' + paths: + - '**.c' + - '**.h' + - 'compat/*' + - '.github/workflows/OpenBSD.yml' + pull_request: + branches: + - '*' + paths: + - '**.c' + - '**.h' + - 'compat/*' + - '.github/workflows/OpenBSD.yml' + + + +jobs: + OpenBSD: + runs-on: macos-12 + steps: + - uses: actions/checkout@v2 + - uses: vmactions/openbsd-vm@v0 + with: + prepare: | + pkg_add automake-1.16.3 autoconf-2.71 libtool pkgconf libevent msgpack libssh curl + usesh: true + run: | + sed -i 's,,,' *.[ch] + curl https://raw.githubusercontent.com/openbsd/ports/master/sysutils/tmate/patches/patch-Makefile_am | patch + curl https://raw.githubusercontent.com/openbsd/ports/master/sysutils/tmate/patches/patch-server_c | patch + curl https://raw.githubusercontent.com/openbsd/ports/master/sysutils/tmate/patches/patch-tmate-debug_c | patch + curl https://raw.githubusercontent.com/openbsd/ports/master/sysutils/tmate/patches/patch-tmate_h | patch + curl https://raw.githubusercontent.com/openbsd/ports/master/sysutils/tmate/patches/patch-tmux_c | patch + curl https://raw.githubusercontent.com/openbsd/ports/master/sysutils/tmate/patches/patch-tmux_h | patch + curl https://raw.githubusercontent.com/openbsd/ports/master/sysutils/tmate/patches/patch-osdep-openbsd_c | patch + export AUTOMAKE_VERSION=1.16 + export AUTOCONF_VERSION=2.71 + autoupdate + ./autogen.sh + ./configure + make + make install + + + diff --git a/.github/workflows/Ubuntu.yml b/.github/workflows/Ubuntu.yml new file mode 100644 index 00000000..cecf39b9 --- /dev/null +++ b/.github/workflows/Ubuntu.yml @@ -0,0 +1,37 @@ +name: Ubuntu +on: + push: + branches: + - '*' + paths: + - '**.c' + - '**.h' + - 'compat/*' + - '.github/workflows/Ubuntu.yml' + pull_request: + branches: + - '*' + paths: + - '**.c' + - '**.h' + - 'compat/*' + - '.github/workflows/Ubuntu.yml' + + + +jobs: + Ubuntu: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: | + sudo apt-get install -y build-essential automake libtool libevent-dev libncurses5-dev libmsgpack-dev libssh-dev pkg-config + autoupdate + ACLOCAL_PATH=/usr/share/aclocal ./autogen.sh + ./configure + make + sudo make install + + + + diff --git a/.gitignore b/.gitignore index ba580eda..bcbdfe13 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,13 @@ tmux Makefile Makefile.in configure -tmux.1.* +tmate +cscope.* +ctags +*.log +tmate.1.* +downloads/ +ext/ +libssh-*/ +msgpack-*/ +releases/ diff --git a/.mailmap b/.mailmap index a7a401ff..b4956775 100644 --- a/.mailmap +++ b/.mailmap @@ -1,24 +1,36 @@ -Bob Beck beck -Igor Sobrado sobrado -Jacek Masiulaniec jacekm -Jason McIntyre jcm +Bob Beck beck +Claudio Jeker claudio +Igor Sobrado sobrado +Ingo Schwarze schwarze +Jacek Masiulaniec jacekm +Jason McIntyre jmc Joel Sing jsing -Marc Espie espie -Matthew Dempsky matthew -Matthias Kilian kili -Matthieu Herrb matthieu -Miod Vallat miod -Nicholas Marriott nicm -Nicholas Marriott no_author - -Okan Demirmen okan +Jonathan Gray jsg +Kenneth R Westerback krw +Marc Espie espie +Matthew Dempsky matthew +Matthias Kilian kili +Matthieu Herrb matthieu +Michael McConville mmcc +Miod Vallat miod +Nicholas Marriott Nicholas Marriott +Nicholas Marriott nicm +Nicholas Marriott no_author +Okan Demirmen okan Philip Guenther guenther -Pierre-Yves Ritschard pyr -Ray Lai ray +Pierre-Yves Ritschard pyr +Ray Lai ray Ryan McBride mcbride -Stefan Sperling stsp -Stuart Henderson sthen -Ted Unangst tedu -Theo Deraadt deraadt +Sebastian Benoit benno +Stefan Sperling stsp +Stuart Henderson sthen +Ted Unangst tedu +Theo de Raadt Theo Deraadt +Theo de Raadt deraadt Thomas Adam Thomas -William Yodlowsky william +Thomas Adam Thomas Adam +Thomas Adam n6tadam +Tim van der Molen tim +Tobias Stoeckmann tobias +Todd C Miller millert +William Yodlowsky william diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..5a83e0ed --- /dev/null +++ b/.travis.yml @@ -0,0 +1,40 @@ +language: c +services: +- docker + +matrix: + include: + - arch: amd64 + env: PLATFORM=amd64 + - arch: amd64 + env: PLATFORM=i386 + - arch: arm64 + env: PLATFORM=arm32v6 + - arch: arm64 + env: PLATFORM=arm32v7 + - arch: arm64 + env: PLATFORM=arm64v8 + - arch: s390x + env: PLATFORM=s390x + - arch: ppc64le + env: PLATFORM=ppc64le + +script: +- 'docker build . --tag local-$PLATFORM/tmate-build --build-arg PLATFORM=$PLATFORM' +# On arch=arm64, some directories are not setup correctly, and 'ruby -S gem +# install dpl' required by the release push scripts fails. +- 'if [ "$TRAVIS_TAG" ]; then sudo chown -R $USER: /var/lib/gems /usr/local/bin; fi' +- 'if [ "$TRAVIS_TAG" ]; then ./build_static_release.sh $TRAVIS_TAG $PLATFORM; fi' + +deploy: + provider: releases + api_key: + secure: T2109tjjOsrVLEpJZK/uxmO0AuDGXYFdN4AAsNTmVwu/W5dcX57Kk2TCgqDuLfD21iGGXP0U/OYHM06IfBDODBWCA9P8ASHYsenS7wIiFnvCEMbfzoAFyBMrXN2kNdM2+ho3aqc0xE2lQKOKDLxpGm5FZrzujscXXzxQjWBU5Hk= + skip_cleanup: true + overwrite: true + file_glob: true + file: releases/*.tar.* + on: + repo: tmate-io/tmate + branch: master + tags: true diff --git a/CHANGES b/CHANGES index abd1ac0a..db7be25b 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,113 @@ +CHANGES FROM 2.0 to 2.1 18 October 2015 + +Incompatible Changes +==================== + +* Mouse-mode has been rewritten. There's now no longer options for: + - mouse-resize-pane + - mouse-select-pane + - mouse-select-window + - mode-mouse + + Instead there is just one option: 'mouse' which turns on mouse support + entirely. +* 'default-terminal' is now a session option. Furthermore, if this is set + to 'screen-*' then emulate what screen does. If italics are wanted, this + can be set to 'tmux' but this is still new and not necessarily supported + on all platforms with older ncurses installs. +* The c0-* options for rate-limiting have been removed. Instead, a backoff + approach is used. + +Normal Changes +============== + +* New formats: + - session_activity + - window_linked + - window_activity_format + - session_alerts + - session_last_attached + - client_pid + - pid +* 'copy-selection', 'append-selection', 'start-named-buffer' now understand + an '-x' flag to prevent it exiting copying mode. +* 'select-pane' now understands '-P' to set window/pane background colours. +* 'renumber-windows' now understands windows which are unlinked. +* 'bind' now understands multiple key tables. Allows for key-chaining. +* 'select-layout' understands '-o' to undo the last layout change. +* The environment is updated when switching sessions as well as attaching. +* 'select-pane' now understands '-M' for marking a pane. This marked pane + can then be used with commands which understand src-pane specifiers + automatically. +* If a session/window target is prefixed with '=' then only an exact match + is considered. +* 'move-window' understands '-a'. +* 'update-environment' understands '-E' when attach-session is used on an + already attached client. +* 'show-environment' understands '-s' to output Bourne-compatible commands. +* New option: 'history-file' to save/restore command prompt history. +* Copy mode is exited if the history is cleared whilst in copy-mode. +* 'copy-mode' learned '-e' to exit copy-mode when scrolling to end. + +CHANGES FROM 1.9a to 2.0 6 March 2015 + +Incompatible Changes +==================== + +* The choose-list command has been removed. +* 'terminal-overrides' is now a server option, not a session option. +* 'message-limit' is now a server option, not a session option. +* 'monitor-content' option has been removed. +* 'pane_start_path' option has been removed. +* The "info" mechanism which used to (for some commands) provide feedback + has been removed, and like other commands, they now produce nothing on + success. + +Normal Changes +============== + +* tmux can now write an entry to utmp if the library 'utempter' is present + at compile time. +* set-buffer learned append mode (-a), and a corresponding + 'append-selection' command has been added to copy-mode. +* choose-mode now has the following commands which can be bound: + - start-of-list + - end-of-list + - top-line + - bottom-line + +* choose-buffer now understands UTF-8. +* Pane navigation has changed: + - The old way of always using the top or left if the choice is ambiguous. + - The new way of remembering the last used pane is annoying if the + layout is balanced and the leftmost is obvious to the user (because + clearly if we go right from the top-left in a tiled set of four we want + to end up in top-right, even if we were last using the bottom-right). + + So instead, use a combination of both: if there is only one possible + pane alongside the current pane, move to it, otherwise choose the most + recently used of the choice. +* 'set-buffer' can now be told to give names to buffers. +* The 'new-session', 'new-window', 'split-window', and 'respawn-pane' commands + now understand multiple arguments and handle quoting problems correctly. +* 'capture-pane' understands '-S-' to mean the start of the pane, and '-E-' to + mean the end of the pane. +* Support for function keys beyond F12 has changed. The following explains: + - F13-F24 are S-F1 to S-F12 + - F25-F36 are C-F1 to C-F12 + - F37-F48 are C-S-F1 to C-S-F12 + - F49-F60 are M-F1 to M-F12 + - F61-F63 are M-S-F1 to M-S-F3 + + Therefore, F13 becomes a binding of S-F1, etc. +* Support using pane id as part of session or window specifier (so % means + session-of-%1 or window-of-%1) and window id as part of session + (so @1 means session-of-@1). +* 'copy-pipe' command now understands formats via -F +* 'if-shell' command now understands formats via -F +* 'split-window' and 'join-window' understand -b to create the pane to the left + or above the target pane. + CHANGES FROM 1.9 to 1.9a 22 February 2014 NOTE: This is a bug-fix release to address some important bugs which just @@ -1865,5 +1975,3 @@ The list of older changes is below. emacs) that don't require scrolling regions (ESC[r) mostly work fine (including mutt, emacs). No status bar yet and no key remapping or other customisation. - -$Id$ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..2f6387c5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,37 @@ +ARG PLATFORM=amd64 +FROM ${PLATFORM}/alpine:3.10 AS build + +WORKDIR /build + +RUN apk add --no-cache wget cmake make gcc g++ linux-headers zlib-dev openssl-dev \ + automake autoconf libevent-dev ncurses-dev msgpack-c-dev libexecinfo-dev \ + ncurses-static libexecinfo-static libevent-static msgpack-c ncurses-libs \ + libevent libexecinfo openssl zlib + +RUN set -ex; \ + mkdir -p /src/libssh/build; \ + cd /src; \ + wget -O libssh.tar.xz https://www.libssh.org/files/0.9/libssh-0.9.0.tar.xz; \ + tar -xf libssh.tar.xz -C /src/libssh --strip-components=1; \ + cd /src/libssh/build; \ + cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr \ + -DWITH_SFTP=OFF -DWITH_SERVER=OFF -DWITH_PCAP=OFF \ + -DWITH_STATIC_LIB=ON -DWITH_GSSAPI=OFF ..; \ + make -j $(nproc); \ + make install + +COPY compat ./compat +COPY *.c *.h autogen.sh Makefile.am configure.ac ./ + +RUN ./autogen.sh && ./configure --enable-static +RUN make -j $(nproc) +RUN objcopy --only-keep-debug tmate tmate.symbols && chmod -x tmate.symbols && strip tmate +RUN ./tmate -V + +FROM alpine:3.9 + +RUN apk --no-cache add bash +RUN mkdir /build +ENV PATH=/build:$PATH +COPY --from=build /build/tmate.symbols /build +COPY --from=build /build/tmate /build diff --git a/FAQ b/FAQ index 2ca0a596..21e6167d 100644 --- a/FAQ +++ b/FAQ @@ -108,8 +108,8 @@ Check the latest version of tmux from Git to see if the problem is still reproducible. Sometimes the length of time between releases means a lot of fixes can be sitting in Git and the problem might already be fixed. -Please send bug reports by email to nicm@users.sourceforge.net or -tmux-users@lists.sourceforge.net. Please include as much of the following +Please send bug reports by email to nicholas.marriott@gmail.com or +tmux-users@googlegroups.com. Please include as much of the following information as possible: - the version of tmux you are running; @@ -123,7 +123,7 @@ information as possible: * Why doesn't tmux do $x? -Please send feature requests by email to nicm@users.sourceforge.net. +Please send feature requests by email to tmux-users@googlegroups.com. * Why do you use the screen terminal description inside tmux? It sucks. @@ -352,42 +352,33 @@ lock(1) or vlock(1)) by using the following: bind x set lock-command '/usr/bin/vlock' \; lock-client \; set lock-command 'tput civis && read -s -n1' -* vim displays reverse video instead of italics, while less displays italics - (or just regular text) instead of reverse. What's wrong? +* I don't see italics! Or less and vim show italics and reverse the wrong way round! -Screen's terminfo description lacks italics mode and has standout mode in its -place, but using the same escape sequence that urxvt uses for italics. This -means applications (like vim) looking for italics will not find it and might -turn to reverse in its place, while applications (like less) asking for -standout will end up with italics instead of reverse. To make applications -aware that tmux supports italics and to use a proper escape sequence for -standout, you'll need to create a new terminfo file with modified sgr, smso, -rmso, sitm and ritm entries: +GNU screen does not support italics and the "screen" terminfo description uses +the italics escape sequence incorrectly. - $ mkdir $HOME/.terminfo/ - $ screen_terminfo="screen" - $ infocmp "$screen_terminfo" | sed \ - -e 's/^screen[^|]*|[^,]*,/screen-it|screen with italics support,/' \ - -e 's/%?%p1%t;3%/%?%p1%t;7%/' \ - -e 's/smso=[^,]*,/smso=\\E[7m,/' \ - -e 's/rmso=[^,]*,/rmso=\\E[27m,/' \ - -e '$s/$/ sitm=\\E[3m, ritm=\\E[23m,/' > /tmp/screen.terminfo - $ tic /tmp/screen.terminfo +As of tmux 2.1, if default-terminal is set to "screen" or matches "screen-*", +tmux will behave like screen and italics will be disabled. + +To enable italics, create a new terminfo entry called "tmux" (some platforms +may already have this, you can check with "infocmp tmux"): + + $ cat <$(srcdir)/tmux.1.mdoc; \ + sed -e "s|@SYSCONFDIR@|$(sysconfdir)|g" $(srcdir)/tmate.1 \ + >$(srcdir)/tmate.1.mdoc; \ else \ - sed -e "s|@SYSCONFDIR@|$(sysconfdir)|g" $(srcdir)/tmux.1| \ - $(AWK) -f$(srcdir)/mdoc2man.awk >$(srcdir)/tmux.1.man; \ + sed -e "s|@SYSCONFDIR@|$(sysconfdir)|g" $(srcdir)/tmate.1| \ + $(AWK) -f$(srcdir)/mdoc2man.awk >$(srcdir)/tmate.1.man; \ fi $(mkdir_p) $(DESTDIR)$(mandir)/man1 - $(INSTALL_DATA) $(srcdir)/tmux.1.@MANFORMAT@ \ - $(DESTDIR)$(mandir)/man1/tmux.1 - -# Update SF web site. -upload-index.html: update-index.html - scp www/index.html www/main.css www/images/*.png \ - ${USER},tmux@web.sf.net:/home/groups/t/tm/tmux/htdocs - rm -f www/index.html www/images/small-* - -update-index.html: - (cd www/images && \ - rm -f small-* && \ - for i in *.png; do \ - convert "$$i" -resize 200x150 "small-$$i"; \ - done \ - ) - sed "s/%%RELEASE%%/${RELEASE}/g" www/index.html.in >www/index.html + $(INSTALL_DATA) $(srcdir)/tmate.1.@MANFORMAT@ \ + $(DESTDIR)$(mandir)/man1/tmate.1 diff --git a/README b/README index fad67b71..9141f240 100644 --- a/README +++ b/README @@ -4,12 +4,11 @@ tmux is a "terminal multiplexer", it enables a number of terminals (or windows) to be accessed and controlled from a single terminal. tmux is intended to be a simple, modern, BSD-licensed alternative to programs such as GNU screen. -This release runs on OpenBSD, FreeBSD, NetBSD, Linux and OS X and may still -run on Solaris and AIX (although they haven't been tested in a while). +This release runs on OpenBSD, FreeBSD, NetBSD, Linux, OS X and Solaris. tmux depends on libevent 2.x. Download it from: - http://www.monkey.org/~provos/libevent/ + http://libevent.org To build tmux from a release tarball, do: @@ -18,14 +17,13 @@ To build tmux from a release tarball, do: To get and build the latest from version control: - $ git clone git://git.code.sf.net/p/tmux/tmux-code tmux + $ git clone https://github.com/tmux/tmux.git $ cd tmux $ sh autogen.sh $ ./configure && make -For more information see https://sourceforge.net/scm/?type=git&group_id=200378 -and http://git-scm.com. Patches should be sent by email to the mailing list at -tmux-users@lists.sourceforge.net. +For more information see http://git-scm.com. Patches should be sent by email to +the mailing list at tmux-users@googlegroups.com. For documentation on using tmux, see the tmux.1 manpage. It can be viewed from the source tree with: @@ -35,26 +33,34 @@ the source tree with: Some common questions are answered in the FAQ file and a more extensive (but slightly out of date) guide is available in the OpenBSD FAQ at http://www.openbsd.org/faq/faq7.html#tmux. A rough todo list is in the TODO -file and some example configurations and a Vim syntax file are in the examples -directory. +file and an example configuration in example_tmux.conf. + +A vim(1) syntax file is available at: + + https://github.com/keith/tmux.vim + https://raw.githubusercontent.com/keith/tmux.vim/master/syntax/tmux.vim + +And a bash(1) completion file at: + + https://github.com/przepompownia/tmux-bash-completion For debugging, running tmux with -v or -vv will generate server and client log files in the current directory. -tmux mailing lists are available. Visit: +tmux mailing lists are available. For general discussion and bug reports: - https://sourceforge.net/mail/?group_id=200378 + https://groups.google.com/forum/#!forum/tmux-users + +And for Git commit emails: + + https://groups.google.com/forum/#!forum/tmux-git Bug reports, feature suggestions and especially code contributions are most welcome. Please send by email to: - tmux-users@lists.sourceforge.net + tmux-users@googlegroups.com -This file and the CHANGES, FAQ and TODO files are licensed under the ISC -license. Files under examples/ remain copyright their authors unless otherwise -stated in the file but permission has been received to distribute them with -tmux. All other files have a license and copyright notice at their start. +This file and the CHANGES, FAQ, SYNCING and TODO files are licensed under the +ISC license. All other files have a license and copyright notice at their start. --- Nicholas Marriott - -$Id$ +-- Nicholas Marriott diff --git a/README-tmux b/README-tmux new file mode 100644 index 00000000..acf15632 --- /dev/null +++ b/README-tmux @@ -0,0 +1,61 @@ +Welcome to tmux! + +tmux is a "terminal multiplexer", it enables a number of terminals (or windows) +to be accessed and controlled from a single terminal. tmux is intended to be a +simple, modern, BSD-licensed alternative to programs such as GNU screen. + +This release runs on OpenBSD, FreeBSD, NetBSD, Linux, OS X and Solaris. + +tmux depends on libevent 2.x. Download it from: + + http://www.monkey.org/~provos/libevent/ + +To build tmux from a release tarball, do: + + $ ./configure && make + $ sudo make install + +To get and build the latest from version control: + + $ git clone https://github.com/tmux/tmux.git + $ cd tmux + $ sh autogen.sh + $ ./configure && make + +For more information see http://git-scm.com. Patches should be sent by email to +the mailing list at tmux-users@googlegroups.com. + +For documentation on using tmux, see the tmux.1 manpage. It can be viewed from +the source tree with: + + $ nroff -mdoc tmux.1|less + +Some common questions are answered in the FAQ file and a more extensive (but +slightly out of date) guide is available in the OpenBSD FAQ at +http://www.openbsd.org/faq/faq7.html#tmux. A rough todo list is in the TODO +file and some example configurations and a Vim syntax file are in the examples +directory. + +For debugging, running tmux with -v or -vv will generate server and client log +files in the current directory. + +tmux mailing lists are available. For general discussion and bug reports: + + https://groups.google.com/forum/#!forum/tmux-users + +And for Git commit emails: + + https://groups.google.com/forum/#!forum/tmux-git + +Bug reports, feature suggestions and especially code contributions are most +welcome. Please send by email to: + + tmux-users@googlegroups.com + +This file and the CHANGES, FAQ, SYNCING and TODO files are licensed under +the ISC license. Files under examples/ remain copyright their authors unless +otherwise stated in the file but permission has been received to distribute +them with tmux. All other files have a license and copyright notice at their +start. + +-- Nicholas Marriott diff --git a/README.md b/README.md new file mode 100644 index 00000000..14bc9238 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +tmate +===== + +What is it? +----------- + +Tmate is a fork of tmux. It provides an instant pairing solution. + +License +------- + +tmate is built on top of tmux. tmux and tmate are BSD-licensed. diff --git a/SYNCING b/SYNCING index 218685c3..52591af7 100644 --- a/SYNCING +++ b/SYNCING @@ -1,10 +1,10 @@ Preamble ======== -Tmux on SourceForge has two git repositories [1] "tmux-code" and "tmux-openbsd". +Tmux portable relies on repositories "tmux" and "tmux-openbsd". Here's a description of them: -* "tmux-code" is the portable version, the one which contains code for other +* "tmux" is the portable version, the one which contains code for other operating systems, and autotools, etc., which isn't found or needed in the OpenBSD base system. @@ -17,9 +17,6 @@ repository will take at least that long to appear in this git repository. (It might take longer, depending on the CVS mirror used to import the OpenBSD code). -It is assumed that the person doing the sync has read/write access to the -tmux-code repository on SourceForge already. - If you've never used git before, git tracks meta-data about the committer and the author, as part of a commit, hence: @@ -37,11 +34,11 @@ this information has ever been set before. Cloning repositories ==================== -This involves having both tmux-code and tmux-openbsd cloned, as in: +This involves having both tmux and tmux-openbsd cloned, as in: % cd /some/where/useful -% git clone ssh://${USER}@git.code.sf.net/p/tmux/tmux -% git clone ssh://${USER}@git.code.sf.net/p/tmux/tmux-openbsd +% git clone https://github.com/tmux/tmux.git +% git clone https://github.com/ThomasAdam/tmux-openbsd.git Note that you do not need additional checkouts to manage the sync -- an existing clone of either repositories will suffice. So if you already have @@ -50,56 +47,56 @@ these checkouts existing, skip that. Adding in git-remotes ===================== -Because the portable "tmux-code" git repository and the "tmux-openbsd" +Because the portable "tmux" git repository and the "tmux-openbsd" repository do not inherently share any history between each other, the history has been faked between them. This "faking of history" is something which has to be told to git for the purposes of comparing the "tmux" and "tmux-openbsd" repositories for syncing. To do this, we must reference the -clone of the "tmux-openbsd" repository from the "tmux-code" repository, as +clone of the "tmux-openbsd" repository from the "tmux" repository, as shown by the following command: -% cd /path/to/tmux-code +% cd /path/to/tmux % git remote add obsd-tmux file:///path/to/tmux-openbsd So that now, the remote "obsd-tmux" can be used to reference branches and commits from the "tmux-openbsd" repository, but from the context of the -portable "tmux-code" repository, which makes sense because it's the "tmux" +portable "tmux" repository, which makes sense because it's the "tmux" repository which will have the updates applied to them. Fetching updates ================ To ensure the latest commits from "tmux-openbsd" can be found from within -"tmux-code", we have to ensure the "master" branch from "tmux-openbsd" is -up-to-date first, and then reference that update in "tmux-code", as in: +"tmux", we have to ensure the "master" branch from "tmux-openbsd" is +up-to-date first, and then reference that update in "tmux", as in: % cd /path/to/tmux-openbsd % git checkout master % git pull -Then back in "tmux-code": +Then back in "tmux": -% cd /path/to/tmux-code -% git fetch obsd-tmux-code +% cd /path/to/tmux +% git fetch obsd-tmux Creating the necessary branches =============================== -Now that "tmux-code" can see commits and branches from "tmux-openbsd" by way +Now that "tmux" can see commits and branches from "tmux-openbsd" by way of the remote name "obsd-tmux", we can now create the master branch from -"tmux-openbsd" in the "tmux-code" repository: +"tmux-openbsd" in the "tmux" repository: % git checkout -b obsd-master obsd-tmux/master Adding in the fake history points ================================= -To tie both the "master" branch from "tmux-code" and the "obsd-master" +To tie both the "master" branch from "tmux" and the "obsd-master" branch from "tmux-openbsd" together, the fake history points added to the -"tmux-code" repository need to be added. To do this, we must add an +"tmux" repository need to be added. To do this, we must add an additional refspec line, as in: -% cd /path/to/tmux-code +% cd /path/to/tmux % git config --add remote.origin.fetch '+refs/replace/*:refs/replace/*' % git fetch origin @@ -110,7 +107,7 @@ Make sure the "master" branch is checked out: % git checkout master -The following will show commits on OpenBSD not yet synched with "tmux-code": +The following will show commits on OpenBSD not yet synched with "tmux": % git log master..obsd-master @@ -154,30 +151,26 @@ Release tmux for next version 3. Tag with: - % git tag -a 1.X + % git tag -a 2.X - Where "1.X" is the next version. + Where "2.X" is the next version. Push the tag out with: - % git push 1.X + % git push 2.X -4. Build the tarball with make dist. Now that it's using autoconf there - shouldn't be any weird files (such as the original and rejection files - from patch(1)) but it doesn't hurt taking a quick look at it. +4. Build the tarball with 'make dist'. -5. Split the release changes into a new file. This should be named - tmux-$VERSION-readme to make sourceforge show it automagically in specific - parts of the project page. +5. Check the tarball. If it's good, go here to select the tag just pushed: -6. Upload the tarball and the above file. Make the tarball the default - download by selecting all operating systems under the file details. + https://github.com/tmux/tmux/tags -7. Run make update-index.html upload-index.html to replace %%VERSION%%. + Click the "Add release notes", upload the tarball and add a link in the + description field to the CHANGES file. -8. Bump version in configure.ac and uncomment "found_debug=yes" to create - a debug build by default. +7. Clone the tmux.github.io repository, and change the RELEASE version in + the Makefile. Commit it, and run 'make' to replace %%VERSION%%. Push + the result out. -9. Update freshmeat. - -[1] https://sourceforge.net/p/tmux/_list/git +8. Bump version in tmu/tmux.git configure.ac and uncomment "found_debug=yes" to + create a debug build by default. diff --git a/TODO b/TODO index a0a7570a..e0e3de6d 100644 --- a/TODO +++ b/TODO @@ -1,14 +1,12 @@ - command bits and pieces: - * why doesn't command-prompt work if made read-only? * allow multiple targets: fnmatch for -t/-c, for example detach all clients with -t* * add -c for new-session like new-window - * attach should take a pane and select it as well as attaching * ' and " should be parsed the same (eg "\e" vs '\e') in config and command prompt * last-pane across sessions - * exact match operator for targets (or break the substring match - and require eg x* instead of just x) + * list-keys should quote output so that bindings can just be used in + config file as-is - make command sequences more usable * don't require space after ; @@ -19,11 +17,9 @@ * way to set socket path from config file - format improvements: - * last bits of status_replace into formats? - * option to quote format (#{session_name:quoted}) + * option to quote format (#{q:session_name}) * formats need conditions for >0 (for #P) - * some way to pad # stuff with spaces, #!2T maybe - * last window update time and format for it + * some way to pad # stuff with spaces * formats to show if a window is linked into multiple sessions, into multiple attached sessions, and is the active window in multiple attached sessions? @@ -34,6 +30,9 @@ * flag to choose-* for sort order * choose mode would be better per client than per window? * two choices (first one then second, for swap-pane and join-pane) + * choose modes should ditch the key bindings and just have fixed keys, and + be more customized to their purpose (d to delete a buffer for choose-buffer, + a preview of buffer contents, etc) - improve monitor-*: * straighten out rules for multiple clients @@ -43,53 +42,45 @@ - improve mouse support: * bind commands to mouse in different areas? - * more fine-grained options? * commands executed when clicking on a pattern (URL) - * mouse-select-pane will screw up with !MODE_MOUSE_STANDARD (it sets - the flag on w/o checking the others before calling tty_update_mode) - * mouse can be like normal key bindings? - - {button-{1,2,3},wheel-{up,down}}-{status,pane,border} and - drag-{start,end}-{status,pane,border} plus the modifiers - - resize and copy can be special cases - once you call something - like copy-mode -M or resize-pane -M to start the drag, it tracks - mouse until you call -m to stop the drag. or just keep drags - entirely special? - - what happens with stuff that wants mouse inside? especially for - pane clicks which need to run command AND pass event through - (like mouse-select-pane). maybe just a flag to say whether it - always runs or only if pane hasn't taken mouse? or it could be - eg bind Button1Pane "select-pane -t=; send-keys -Mt=' - - also need a) some way to tell commands bound to key which - window or pane the mouse key binding applies to (maybe a new - special char in target, or pass targets through formats?) b) a - way to bind repeat count to mode keys so that wheel up/down can - do multiple lines c) send-keys -M to pass a mouse event through? - - what does the mouse->KEYC_* conversion and find-the-pane bit? - server_client_handle_key? - hooks! - warts on current naming: * display-time but message-fg/bg/attr * list-* vs show-* - * split-window -> split-pane?? + * split-window -> split-pane? - better UTF-8 support: - * window names and titles * message display * prompt input - * multibyte key input + * searching in copy mode - copy/paste improvements: * incremental searching - * append to buffer * paste w/o trailing whitespace * command to toggle selection not to move it in copy-mode + * regex searching + * copy-pipe should have -x as well + * copy mode key bindings should just be a standard key table, using + something like "copy-mode start-selection"; it could use + command-prompt for search, goto, etc: + + bind -Temacs command-prompt -p'Search Up: ' 'copy-mode search-up %%' + + it'd need a separate lookup, because modes are per-pane, perhaps a + table() cb to give the table name ("vi" or "emacs"). anything in the + table fires the command, anything not in the table is injected as a + key + * searching in copy mode should unwrap lines, so if you seach for "foobar" + then it should be found even if it is now "foo\nbar" (if the WRAP flag + is set on the line) + * {} to go to next/previous blank line in copy mode - layout stuff * way to tag a layout as a number/name * maybe keep last layout + size around and if size reverts just put it - back + back * revamp layouts: they are too complicated, should be more closely integrated, should support hints, layout sets should just be a special case of custom layouts, and we should support panes that are @@ -98,15 +89,14 @@ * way to set hints/limits about pane size for resizing * panning over window (window larger than visible) * a mode where one application can cross two panes (ie x|y, width = - COLUMNS/2 but height = ROWS * 2) - * general key to space cells out evenly (horiz or vert) within their - parent cell (could replace even-vert/even-horiz layouts) + COLUMNS/2 but height = ROWS * 2) + * general key to space cells out evenly (horiz or vert) within their + parent cell (could replace even-vert/even-horiz layouts) + * separate active panes for different clients - terminfo bits * use a better termcap internally instead of screen, perhaps xterm * use screen-256color when started on 256 colour terminal? - * need a tmux terminfo entry to document the extensions we are using in - upstream terminfo - code cleanup * instead of separate window and session options, just one master @@ -125,17 +115,13 @@ know about the client, notably: - is this the config file? (cmdq->c == NULL) - is this a command client? (cmdq->c != NULL && - cmdq->c->session == NULL) + cmdq->c->session == NULL) - is this a control client? - can i do stdin or stdout to this client? or even guarantee that cmdq->c != NULL and provide a better way to tell when in the config file - then we use cmdq->c if we need a client w/o a session else cmd_current_client * optimize pane redraws, 20120318184853.GK10965@yelena.nicm.ath.cx - * cmd_find_* could be much simpler - parse everything the same, only - difference is what to choose when not given a ":" or "." (such as a - plain "0" could be session, window or pane). So just cmd_find_target - with a type (session, window, or pane).. - miscellaneous * way to keep a job running just read its last line of output for #() @@ -146,10 +132,8 @@ comes from config for new sessions and windows. likewise, panes and jobs and run-shell and lock command all start with slightly different environments - * multiline status line? - * bind commands to key sequences -- make it so ALL keys go through a - table, first an implicit table in which C-b is the only default - binding to a command that says "next key from $othertable" and so - on. means -n can go away as well + * multiline status line? separate command prompt and status line? * customizable command aliases - * any remaining clients in wait-for should be woken when server exits + * automatic pane logging + * BCE? We are halfway there (output side is done for pane backgrounds), + just need to change how screen/grid handles erase diff --git a/alerts.c b/alerts.c new file mode 100644 index 00000000..cca0d815 --- /dev/null +++ b/alerts.c @@ -0,0 +1,294 @@ +/* $OpenBSD$ */ + +/* + * Copyright (c) 2015 Nicholas Marriott + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include + +#include "tmux.h" + +int alerts_fired; + +void alerts_timer(int, short, void *); +int alerts_enabled(struct window *, int); +void alerts_callback(int, short, void *); +void alerts_reset(struct window *); + +void alerts_run_hook(struct session *, struct winlink *, int); +int alerts_check_all(struct session *, struct winlink *); +int alerts_check_bell(struct session *, struct winlink *); +int alerts_check_activity(struct session *, struct winlink *); +int alerts_check_silence(struct session *, struct winlink *); +void alerts_ring_bell(struct session *); + +void +alerts_timer(__unused int fd, __unused short events, void *arg) +{ + struct window *w = arg; + + log_debug("@%u alerts timer expired", w->id); + alerts_reset(w); + alerts_queue(w, WINDOW_SILENCE); +} + +void +alerts_callback(__unused int fd, __unused short events, __unused void *arg) +{ + struct window *w; + struct session *s; + struct winlink *wl; + int flags, alerts; + + RB_FOREACH(w, windows, &windows) { + RB_FOREACH(s, sessions, &sessions) { + RB_FOREACH(wl, winlinks, &s->windows) { + if (wl->window != w) + continue; + flags = w->flags; + + alerts = alerts_check_all(s, wl); + + log_debug("%s:%d @%u alerts check, alerts %#x, " + "flags %#x", s->name, wl->idx, w->id, + alerts, flags); + } + } + } + alerts_fired = 0; +} + +void +alerts_run_hook(struct session *s, struct winlink *wl, int flags) +{ + struct cmd_find_state fs; + + if (cmd_find_from_winlink(&fs, s, wl) != 0) + return; + + if (flags & WINDOW_BELL) + hooks_run(s->hooks, NULL, &fs, "alert-bell"); + if (flags & WINDOW_SILENCE) + hooks_run(s->hooks, NULL, &fs, "alert-silence"); + if (flags & WINDOW_ACTIVITY) + hooks_run(s->hooks, NULL, &fs, "alert-activity"); +} + +int +alerts_check_all(struct session *s, struct winlink *wl) +{ + int alerts; + + alerts = alerts_check_bell(s, wl); + alerts |= alerts_check_activity(s, wl); + alerts |= alerts_check_silence(s, wl); + if (alerts != 0) { + alerts_run_hook(s, wl, alerts); + server_status_session(s); + } + + return (alerts); +} + +void +alerts_check_session(struct session *s) +{ + struct winlink *wl; + + RB_FOREACH(wl, winlinks, &s->windows) + alerts_check_all(s, wl); +} + +int +alerts_enabled(struct window *w, int flags) +{ + if (flags & WINDOW_BELL) + return (1); + if (flags & WINDOW_ACTIVITY) { + if (options_get_number(w->options, "monitor-activity")) + return (1); + } + if (flags & WINDOW_SILENCE) { + if (options_get_number(w->options, "monitor-silence") != 0) + return (1); + } + return (0); +} + +void +alerts_reset_all(void) +{ + struct window *w; + + RB_FOREACH(w, windows, &windows) + alerts_reset(w); +} + +void +alerts_reset(struct window *w) +{ + struct timeval tv; + + w->flags &= ~WINDOW_SILENCE; + event_del(&w->alerts_timer); + + timerclear(&tv); + tv.tv_sec = options_get_number(w->options, "monitor-silence"); + + log_debug("@%u alerts timer reset %u", w->id, (u_int)tv.tv_sec); + if (tv.tv_sec != 0) + event_add(&w->alerts_timer, &tv); +} + +void +alerts_queue(struct window *w, int flags) +{ + if (w->flags & WINDOW_ACTIVITY) + alerts_reset(w); + + if (!event_initialized(&w->alerts_timer)) + evtimer_set(&w->alerts_timer, alerts_timer, w); + + if (!alerts_fired) { + w->flags |= flags; + log_debug("@%u alerts flags added %#x", w->id, flags); + + if (alerts_enabled(w, flags)) { + log_debug("alerts check queued (by @%u)", w->id); + event_once(-1, EV_TIMEOUT, alerts_callback, NULL, NULL); + alerts_fired = 1; + } + } +} + +int +alerts_check_bell(struct session *s, struct winlink *wl) +{ + struct client *c; + struct window *w = wl->window; + int action, visual; + + if (!(w->flags & WINDOW_BELL)) + return (0); + if (s->curw != wl) { + wl->flags |= WINLINK_BELL; + w->flags &= ~WINDOW_BELL; + } + if (s->curw->window == w) + w->flags &= ~WINDOW_BELL; + + action = options_get_number(s->options, "bell-action"); + if (action == BELL_NONE) + return (0); + + visual = options_get_number(s->options, "visual-bell"); + TAILQ_FOREACH(c, &clients, entry) { + if (c->session != s || c->flags & CLIENT_CONTROL) + continue; + if (!visual) { + if ((action == BELL_CURRENT && + c->session->curw->window == w) || + (action == BELL_OTHER && + c->session->curw->window != w) || + action == BELL_ANY) + tty_putcode(&c->tty, TTYC_BEL); + continue; + } + if (action == BELL_CURRENT && c->session->curw->window == w) + status_message_set(c, "Bell in current window"); + else if (action == BELL_ANY || (action == BELL_OTHER && + c->session->curw->window != w)) + status_message_set(c, "Bell in window %d", wl->idx); + } + + return (WINDOW_BELL); +} + +int +alerts_check_activity(struct session *s, struct winlink *wl) +{ + struct client *c; + struct window *w = wl->window; + + if (s->curw->window == w) + w->flags &= ~WINDOW_ACTIVITY; + + if (!(w->flags & WINDOW_ACTIVITY) || wl->flags & WINLINK_ACTIVITY) + return (0); + if (s->curw == wl) + return (0); + + if (!options_get_number(w->options, "monitor-activity")) + return (0); + + if (options_get_number(s->options, "bell-on-alert")) + alerts_ring_bell(s); + wl->flags |= WINLINK_ACTIVITY; + + if (options_get_number(s->options, "visual-activity")) { + TAILQ_FOREACH(c, &clients, entry) { + if (c->session != s) + continue; + status_message_set(c, "Activity in window %d", wl->idx); + } + } + + return (WINDOW_ACTIVITY); +} + +int +alerts_check_silence(struct session *s, struct winlink *wl) +{ + struct client *c; + struct window *w = wl->window; + + if (s->curw->window == w) + w->flags &= ~WINDOW_SILENCE; + + if (!(w->flags & WINDOW_SILENCE) || wl->flags & WINLINK_SILENCE) + return (0); + if (s->curw == wl) + return (0); + + if (options_get_number(w->options, "monitor-silence") == 0) + return (0); + + if (options_get_number(s->options, "bell-on-alert")) + alerts_ring_bell(s); + wl->flags |= WINLINK_SILENCE; + + if (options_get_number(s->options, "visual-silence")) { + TAILQ_FOREACH(c, &clients, entry) { + if (c->session != s) + continue; + status_message_set(c, "Silence in window %d", wl->idx); + } + } + + return (WINDOW_SILENCE); +} + +void +alerts_ring_bell(struct session *s) +{ + struct client *c; + + TAILQ_FOREACH(c, &clients, entry) { + if (c->session == s && !(c->flags & CLIENT_CONTROL)) + tty_putcode(&c->tty, TTYC_BEL); + } +} diff --git a/arguments.c b/arguments.c index 05ff97eb..f7f8f737 100644 --- a/arguments.c +++ b/arguments.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2010 Nicholas Marriott + * Copyright (c) 2010 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -18,7 +18,6 @@ #include -#include #include #include #include @@ -29,6 +28,12 @@ * Manipulate command arguments. */ +struct args_entry { + u_char flag; + char *value; + RB_ENTRY(args_entry) entry; +}; + struct args_entry *args_find(struct args *, u_char); RB_GENERATE(args_tree, args_entry, entry, args_cmp); @@ -122,77 +127,73 @@ args_free(struct args *args) free(args); } -/* Print a set of arguments. */ -size_t -args_print(struct args *args, char *buf, size_t len) +/* Add to string. */ +static void printflike(3, 4) +args_print_add(char **buf, size_t *len, const char *fmt, ...) { - size_t off, used; + va_list ap; + char *s; + size_t slen; + + va_start(ap, fmt); + slen = xvasprintf(&s, fmt, ap); + va_end(ap); + + *len += slen; + *buf = xrealloc(*buf, *len); + + strlcat(*buf, s, *len); + free(s); +} + +/* Print a set of arguments. */ +char * +args_print(struct args *args) +{ + size_t len; + char *buf; int i; - const char *quotes; struct args_entry *entry; - /* There must be at least one byte at the start. */ - if (len == 0) - return (0); - off = 0; + len = 1; + buf = xcalloc(1, len); /* Process the flags first. */ - buf[off++] = '-'; RB_FOREACH(entry, args_tree, &args->tree) { if (entry->value != NULL) continue; - if (off == len - 1) { - buf[off] = '\0'; - return (len); - } - buf[off++] = entry->flag; - buf[off] = '\0'; + if (*buf == '\0') + args_print_add(&buf, &len, "-"); + args_print_add(&buf, &len, "%c", entry->flag); } - if (off == 1) - buf[--off] = '\0'; /* Then the flags with arguments. */ RB_FOREACH(entry, args_tree, &args->tree) { if (entry->value == NULL) continue; - if (off >= len) { - /* snprintf will have zero terminated. */ - return (len); - } - - if (strchr(entry->value, ' ') != NULL) - quotes = "\""; + if (*buf != '\0') + args_print_add(&buf, &len, " -%c ", entry->flag); else - quotes = ""; - used = xsnprintf(buf + off, len - off, "%s-%c %s%s%s", - off != 0 ? " " : "", entry->flag, quotes, entry->value, - quotes); - if (used > len - off) - used = len - off; - off += used; + args_print_add(&buf, &len, "-%c ", entry->flag); + if (strchr(entry->value, ' ') != NULL) + args_print_add(&buf, &len, "\"%s\"", entry->value); + else + args_print_add(&buf, &len, "%s", entry->value); } /* And finally the argument vector. */ for (i = 0; i < args->argc; i++) { - if (off >= len) { - /* snprintf will have zero terminated. */ - return (len); - } - + if (*buf != '\0') + args_print_add(&buf, &len, " "); if (strchr(args->argv[i], ' ') != NULL) - quotes = "\""; + args_print_add(&buf, &len, "\"%s\"", args->argv[i]); else - quotes = ""; - used = xsnprintf(buf + off, len - off, "%s%s%s%s", - off != 0 ? " " : "", quotes, args->argv[i], quotes); - if (used > len - off) - used = len - off; - off += used; + args_print_add(&buf, &len, "%s", args->argv[i]); } - return (off); + return (buf); } /* Return if an argument is present. */ diff --git a/array.h b/array.h index 2c70c310..209de0c5 100644 --- a/array.h +++ b/array.h @@ -1,7 +1,7 @@ -/* $Id$ */ +/* $OpenBSD$ */ /* - * Copyright (c) 2006 Nicholas Marriott + * Copyright (c) 2006 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above diff --git a/autogen.sh b/autogen.sh index 300b54db..cbf6df1f 100755 --- a/autogen.sh +++ b/autogen.sh @@ -1,5 +1,4 @@ #!/bin/sh -# $Id$ if [ "x$(uname)" = "xOpenBSD" ]; then [ -z "$AUTOMAKE_VERSION" ] && export AUTOMAKE_VERSION=1.10 diff --git a/build_static_release.sh b/build_static_release.sh new file mode 100755 index 00000000..9cb35435 --- /dev/null +++ b/build_static_release.sh @@ -0,0 +1,33 @@ +#!/bin/bash +set -eux + +# This is invoked by .travis.yml + +VERSION=$1 +PLATFORM=$2 + +SRC_VERSION=`cat configure.ac | grep AC_INIT | sed -E 's/^AC_INIT\(tmate, (.+)\)$/\1/'` + +if [ $SRC_VERSION != $VERSION ]; then + echo "Version mismatch: $SRC_VERSION != $VERSION" + exit 1 +fi + +RELEASE_NAME=tmate-$VERSION-static-linux-$PLATFORM +echo "Building $RELEASE_NAME" + +docker build . --tag local-$PLATFORM/tmate-build --build-arg PLATFORM=$PLATFORM + +mkdir -p releases +cd releases + +rm -rf $RELEASE_NAME +mkdir -p $RELEASE_NAME +docker run --rm local-$PLATFORM/tmate-build cat /build/tmate > $RELEASE_NAME/tmate +chmod +x $RELEASE_NAME/tmate +tar -cf - $RELEASE_NAME | xz > tmate-$VERSION-static-linux-$PLATFORM.tar.xz + +rm -rf $RELEASE_NAME-symbols +mkdir -p $RELEASE_NAME-symbols +docker run --rm local-$PLATFORM/tmate-build cat /build/tmate.symbols > $RELEASE_NAME-symbols/tmate.symbols +tar -cf - $RELEASE_NAME-symbols | xz > dbg-symbols-tmate-$VERSION-static-linux-$PLATFORM.tar.xz diff --git a/cfg.c b/cfg.c index b3b86e76..2410fbf9 100644 --- a/cfg.c +++ b/cfg.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -23,14 +23,66 @@ #include #include #include +#include #include "tmux.h" +#include "tmate.h" -struct cmd_q *cfg_cmd_q; -int cfg_finished; -int cfg_references; -ARRAY_DECL (, char *) cfg_causes = ARRAY_INITIALIZER; -struct client *cfg_client; +char *cfg_file; +#ifdef TMATE +char *tmate_cfg_file; +#endif +struct cmd_q *cfg_cmd_q; +int cfg_finished; +int cfg_references; +char **cfg_causes; +u_int cfg_ncauses; +struct client *cfg_client; + +void cfg_default_done(struct cmd_q *); + +void +set_cfg_file(const char *path) +{ + free(cfg_file); + cfg_file = xstrdup(path); +} + +void +start_cfg(void) +{ + char *cause = NULL; + const char *home; + + cfg_cmd_q = cmdq_new(NULL); + cfg_cmd_q->emptyfn = cfg_default_done; + + cfg_finished = 0; + cfg_references = 1; + + cfg_client = TAILQ_FIRST(&clients); + if (cfg_client != NULL) + cfg_client->references++; + + if (access(TMUX_CONF, R_OK) == 0) { + if (load_cfg(TMUX_CONF, cfg_cmd_q, &cause) == -1) + cfg_add_cause("%s: %s", TMUX_CONF, cause); + } else if (errno != ENOENT) + cfg_add_cause("%s: %s", TMUX_CONF, strerror(errno)); + + if (cfg_file == NULL && (home = find_home()) != NULL) { + xasprintf(&cfg_file, "%s/.tmate.conf", home); + if (access(cfg_file, R_OK) != 0 && errno == ENOENT) { + free(cfg_file); + cfg_file = NULL; + } + } + if (cfg_file != NULL && load_cfg(cfg_file, cfg_cmd_q, &cause) == -1) + cfg_add_cause("%s: %s", cfg_file, cause); + free(cause); + + cmdq_continue(cfg_cmd_q); +} int load_cfg(const char *path, struct cmd_q *cmdq, char **cause) @@ -49,7 +101,7 @@ load_cfg(const char *path, struct cmd_q *cmdq, char **cause) } found = 0; - while ((buf = fparseln(f, NULL, &line, delim, 0))) { + while ((buf = fparseln(f, NULL, &line, delim, 0)) != NULL) { log_debug("%s: %s", path, buf); /* Skip empty lines. */ @@ -74,7 +126,7 @@ load_cfg(const char *path, struct cmd_q *cmdq, char **cause) if (cmdlist == NULL) continue; - cmdq_append(cmdq, cmdlist); + cmdq_append(cmdq, cmdlist, NULL); cmd_list_free(cmdlist); found++; } @@ -83,13 +135,38 @@ load_cfg(const char *path, struct cmd_q *cmdq, char **cause) return (found); } +static void print_cfg_errors(void) +{ + u_int i; + + for (i = 0; i < cfg_ncauses; i++) { + tmate_info("%s", cfg_causes[i]); + free(cfg_causes[i]); + } + + free(cfg_causes); + cfg_causes = NULL; + cfg_ncauses = 0; +} + void -cfg_default_done(unused struct cmd_q *cmdq) +cfg_default_done(__unused struct cmd_q *cmdq) { if (--cfg_references != 0) return; cfg_finished = 1; +#ifdef TMATE + /* We do it this late, this way, CLI options take precedence over cfg file */ + tmate_load_cli_options(); + + tmate_session_start(); + if (tmate_foreground && cfg_ncauses) { + print_cfg_errors(); + exit(1); + } +#endif + if (!RB_EMPTY(&sessions)) cfg_show_causes(RB_MIN(sessions, &sessions)); @@ -106,55 +183,59 @@ cfg_default_done(unused struct cmd_q *cmdq) */ if (!TAILQ_EMPTY(&cfg_client->cmdq->queue)) cmdq_continue(cfg_client->cmdq); - cfg_client->references--; + server_client_unref(cfg_client); cfg_client = NULL; } } void -cfg_add_cause(const char* fmt, ...) +cfg_add_cause(const char *fmt, ...) { - va_list ap; - char* msg; + va_list ap; + char *msg; va_start(ap, fmt); xvasprintf(&msg, fmt, ap); - va_end (ap); + va_end(ap); - ARRAY_ADD(&cfg_causes, msg); + cfg_ncauses++; + cfg_causes = xreallocarray(cfg_causes, cfg_ncauses, sizeof *cfg_causes); + cfg_causes[cfg_ncauses - 1] = msg; } void cfg_print_causes(struct cmd_q *cmdq) { - char *cause; u_int i; - for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) { - cause = ARRAY_ITEM(&cfg_causes, i); - cmdq_print(cmdq, "%s", cause); - free(cause); + for (i = 0; i < cfg_ncauses; i++) { + cmdq_print(cmdq, "%s", cfg_causes[i]); + free(cfg_causes[i]); } - ARRAY_FREE(&cfg_causes); + + free(cfg_causes); + cfg_causes = NULL; + cfg_ncauses = 0; } void cfg_show_causes(struct session *s) { struct window_pane *wp; - char *cause; u_int i; - if (s == NULL || ARRAY_EMPTY(&cfg_causes)) + if (s == NULL || cfg_ncauses == 0) return; wp = s->curw->window->active; window_pane_set_mode(wp, &window_copy_mode); window_copy_init_for_output(wp); - for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) { - cause = ARRAY_ITEM(&cfg_causes, i); - window_copy_add(wp, "%s", cause); - free(cause); + for (i = 0; i < cfg_ncauses; i++) { + window_copy_add(wp, "%s", cfg_causes[i]); + free(cfg_causes[i]); } - ARRAY_FREE(&cfg_causes); + + free(cfg_causes); + cfg_causes = NULL; + cfg_ncauses = 0; } diff --git a/client.c b/client.c index 042ec920..c587ec0d 100644 --- a/client.c +++ b/client.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -32,10 +32,12 @@ #include #include "tmux.h" +#include "tmate.h" -struct imsgbuf client_ibuf; -struct event client_event; -struct event client_stdin; +struct tmuxproc *client_proc; +struct tmuxpeer *client_peer; +int client_flags; +struct event client_stdin; enum { CLIENT_EXIT_NONE, CLIENT_EXIT_DETACHED, @@ -46,40 +48,40 @@ enum { CLIENT_EXIT_EXITED, CLIENT_EXIT_SERVER_EXITED, } client_exitreason = CLIENT_EXIT_NONE; -int client_exitval; -enum msgtype client_exittype; -const char *client_exitsession; -int client_attached; +int client_exitval; +enum msgtype client_exittype; +const char *client_exitsession; +int client_attached; +__dead void client_exec(const char *,const char *); int client_get_lock(char *); -int client_connect(char *, int); -void client_send_identify(int); -int client_write_one(enum msgtype, int, const void *, size_t); -int client_write_server(enum msgtype, const void *, size_t); -void client_update_event(void); -void client_signal(int, short, void *); +int client_connect(struct event_base *, const char *, int); +void client_send_identify(const char *, const char *); void client_stdin_callback(int, short, void *); void client_write(int, const char *, size_t); -void client_callback(int, short, void *); -int client_dispatch_attached(void); -int client_dispatch_wait(void *); +void client_signal(int); +void client_dispatch(struct imsg *, void *); +void client_dispatch_attached(struct imsg *); +void client_dispatch_wait(struct imsg *, const char *); const char *client_exit_message(void); /* * Get server create lock. If already held then server start is happening in - * another client, so block until the lock is released and return -1 to - * retry. Ignore other errors - just continue and start the server without the - * lock. + * another client, so block until the lock is released and return -2 to + * retry. Return -1 on failure to continue and start the server anyway. */ int client_get_lock(char *lockfile) { int lockfd; - if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) - fatal("open failed"); log_debug("lock file is %s", lockfile); + if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) { + log_debug("open failed: %s", strerror(errno)); + return (-1); + } + if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) { log_debug("flock failed: %s", strerror(errno)); if (errno != EAGAIN) @@ -87,7 +89,7 @@ client_get_lock(char *lockfile) while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR) /* nothing */; close(lockfd); - return (-1); + return (-2); } log_debug("flock succeeded"); @@ -96,7 +98,7 @@ client_get_lock(char *lockfile) /* Connect client to server. */ int -client_connect(char *path, int start_server) +client_connect(struct event_base *base, const char *path, int start_server) { struct sockaddr_un sa; size_t size; @@ -114,10 +116,10 @@ client_connect(char *path, int start_server) retry: if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) - fatal("socket failed"); + return (-1); log_debug("trying connect"); - if (connect(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) { + if (connect(fd, (struct sockaddr *)&sa, sizeof sa) == -1) { log_debug("connect failed: %s", strerror(errno)); if (errno != ECONNREFUSED && errno != ENOENT) goto failed; @@ -127,12 +129,16 @@ retry: if (!locked) { xasprintf(&lockfile, "%s.lock", path); - if ((lockfd = client_get_lock(lockfile)) == -1) { - log_debug("didn't get lock"); + if ((lockfd = client_get_lock(lockfile)) < 0) { + log_debug("didn't get lock (%d)", lockfd); + free(lockfile); - goto retry; + lockfile = NULL; + + if (lockfd == -2) + goto retry; } - log_debug("got lock"); + log_debug("got lock (%d)", lockfd); /* * Always retry at least once, even if we got the lock, @@ -144,22 +150,26 @@ retry: goto retry; } - if (unlink(path) != 0 && errno != ENOENT) { + if (lockfd >= 0 && unlink(path) != 0 && errno != ENOENT) { free(lockfile); close(lockfd); return (-1); } - fd = server_start(lockfd, lockfile); + fd = server_start(base, lockfd, lockfile); } - if (locked) { + + if (locked && lockfd >= 0) { free(lockfile); close(lockfd); } - setblocking(fd, 0); return (fd); failed: + if (locked) { + free(lockfile); + close(lockfd); + } close(fd); return (-1); } @@ -201,28 +211,108 @@ client_exit_message(void) return ("unknown reason"); } +#ifdef TMATE +extern const struct cmd_entry cmd_attach_session_entry; +extern const struct cmd_entry cmd_new_session_entry; + +/* For foreground mode */ +static int __argc; +static const char **__argv; +#endif + +int run_headless_command(int argc, const char **argv, int flags, void (*err_callback)(const char *)) +{ + struct cmd_q *cmd_q; + struct cmd_list *cmdlist; + char *cause; + cmd_q = cmdq_new(NULL); /* No client */ + + if ((cmdlist = cmd_list_parse(argc, (char **)argv, NULL, 0, &cause)) == NULL) { + if (err_callback) + err_callback(cause); + return -1; + } + + cmdq_run(cmd_q, cmdlist, NULL); + cmd_list_free(cmdlist); + cmdq_free(cmd_q); + + if (flags & DEFER_ERRORS_CFG) + return 0; + + /* error messages land in cfg_causes */ + int ret = cfg_ncauses ? -1 : 0; + for (u_int i = 0; i < cfg_ncauses; i++) { + if (err_callback) + err_callback(cfg_causes[i]); + free(cfg_causes[i]); + } + + free(cfg_causes); + cfg_causes = NULL; + cfg_ncauses = 0; + + return ret; +} + +static void initial_client_cmd_err_callback(const char *cause) +{ + tmate_info("%s", cause); +} + +void run_initial_client_cmd(void) +{ + int argc = __argc; + const char **argv = __argv; + + const char *default_argv[] = {"new-session"}; + if (argc == 0) { + argc = 1; + argv = default_argv; + } + + if (run_headless_command(argc, argv, 0, initial_client_cmd_err_callback) < 0) + exit(1); +} + /* Client main loop. */ int -client_main(int argc, char **argv, int flags) +client_main(struct event_base *base, int argc, char **argv, int flags, + const char *shellcmd) { struct cmd *cmd; struct cmd_list *cmdlist; struct msg_command_data *data; int cmdflags, fd, i; + const char *ttynam, *cwd; pid_t ppid; enum msgtype msg; - char *cause; + char *cause, path[PATH_MAX]; struct termios tio, saved_tio; size_t size; +#ifdef TMATE + int cant_nest = 0; + __argc = argc; + __argv = (const char **)argv; +#endif + + /* Ignore SIGCHLD now or daemon() in the server will leave a zombie. */ + signal(SIGCHLD, SIG_IGN); + + /* Save the flags. */ + client_flags = flags; /* Set up the initial command. */ cmdflags = 0; - if (shell_cmd != NULL) { + if (shellcmd != NULL) { msg = MSG_SHELL; cmdflags = CMD_STARTSERVER; } else if (argc == 0) { msg = MSG_COMMAND; - cmdflags = CMD_STARTSERVER|CMD_CANTNEST; + cmdflags = CMD_STARTSERVER; +#ifdef TMATE + cant_nest = 1; +#endif } else { msg = MSG_COMMAND; @@ -240,52 +330,84 @@ client_main(int argc, char **argv, int flags) TAILQ_FOREACH(cmd, &cmdlist->list, qentry) { if (cmd->entry->flags & CMD_STARTSERVER) cmdflags |= CMD_STARTSERVER; - if (cmd->entry->flags & CMD_CANTNEST) - cmdflags |= CMD_CANTNEST; + +#ifdef TMATE + if (cmd->entry == &cmd_attach_session_entry || + cmd->entry == &cmd_new_session_entry) + cant_nest = 1; +#endif } cmd_list_free(cmdlist); } - /* - * Check if this could be a nested session, if the command can't nest: - * if the socket path matches $TMUX, this is probably the same server. - */ - if (shell_cmd == NULL && environ_path != NULL && - (cmdflags & CMD_CANTNEST) && - strcmp(socket_path, environ_path) == 0) { +#ifdef TMATE + if (cant_nest && getenv("TMUX")) { fprintf(stderr, "sessions should be nested with care, " - "unset $TMUX to force\n"); + "unset $TMUX to force\n"); return (1); } +#endif + + /* Create client process structure (starts logging). */ + client_proc = proc_start("client", base, 0, client_signal); /* Initialize the client socket and start the server. */ - fd = client_connect(socket_path, cmdflags & CMD_STARTSERVER); + fd = client_connect(base, socket_path, cmdflags & CMD_STARTSERVER); if (fd == -1) { - fprintf(stderr, "failed to connect to server: %s\n", - strerror(errno)); + if (errno == ECONNREFUSED) { + fprintf(stderr, "no server running on %s\n", + socket_path); + } else { +#ifdef TMATE + if (errno == ENOENT) + fprintf(stderr, "You must specify a socket name with -S. For example: \n" + " tmate -S /tmp/tmate.sock new-session -d\n" + " tmate -S /tmp/tmate.sock wait tmate-ready\n"); + else +#endif + fprintf(stderr, "error connecting to %s (%s)\n", + socket_path, strerror(errno)); + } return (1); } + client_peer = proc_add_peer(client_proc, fd, client_dispatch, + (void *)shellcmd); - /* Set process title, log and signals now this is the client. */ -#ifdef HAVE_SETPROCTITLE - setproctitle("client (%s)", socket_path); + /* Save these before pledge(). */ + if ((cwd = getcwd(path, sizeof path)) == NULL) { + if ((cwd = find_home()) == NULL) + cwd = "/"; + } + if ((ttynam = ttyname(STDIN_FILENO)) == NULL) + ttynam = ""; + +#ifdef __OpenBSD__ + /* + * Drop privileges for client. "proc exec" is needed for -c and for + * locking (which uses system(3)). + * + * "tty" is needed to restore termios(4) and also for some reason -CC + * does not work properly without it (input is not recognised). + * + * "sendfd" is dropped later in client_dispatch_wait(). + */ + if (pledge("stdio unix sendfd proc exec tty", NULL) != 0) + fatal("pledge failed"); #endif - logfile("client"); - /* Create imsg. */ - imsg_init(&client_ibuf, fd); - event_set(&client_event, fd, EV_READ, client_callback, shell_cmd); + /* Free stuff that is not used in the client. */ + options_free(global_options); + options_free(global_s_options); + options_free(global_w_options); + environ_free(global_environ); /* Create stdin handler. */ setblocking(STDIN_FILENO, 0); event_set(&client_stdin, STDIN_FILENO, EV_READ|EV_PERSIST, client_stdin_callback, NULL); - if (flags & CLIENT_CONTROLCONTROL) { - if (tcgetattr(STDIN_FILENO, &saved_tio) != 0) { - fprintf(stderr, "tcgetattr failed: %s\n", - strerror(errno)); - return (1); - } + if (client_flags & CLIENT_CONTROLCONTROL) { + if (tcgetattr(STDIN_FILENO, &saved_tio) != 0) + fatal("tcgetattr failed"); cfmakeraw(&tio); tio.c_iflag = ICRNL|IXANY; tio.c_oflag = OPOST|ONLCR; @@ -300,11 +422,8 @@ client_main(int argc, char **argv, int flags) tcsetattr(STDIN_FILENO, TCSANOW, &tio); } - /* Establish signal handlers. */ - set_signals(client_signal); - /* Send identify messages. */ - client_send_identify(flags); + client_send_identify(ttynam, cwd); /* Send first command. */ if (msg == MSG_COMMAND) { @@ -324,192 +443,80 @@ client_main(int argc, char **argv, int flags) size += sizeof *data; /* Send the command. */ - if (client_write_server(msg, data, size) != 0) { + if (proc_send(client_peer, msg, -1, data, size) != 0) { fprintf(stderr, "failed to send command\n"); free(data); return (1); } free(data); } else if (msg == MSG_SHELL) - client_write_server(msg, NULL, 0); + proc_send(client_peer, msg, -1, NULL, 0); - /* Set the event and dispatch. */ - client_update_event(); - event_dispatch(); + /* Start main loop. */ + proc_loop(client_proc, NULL); /* Print the exit message, if any, and exit. */ if (client_attached) { - if (client_exitreason != CLIENT_EXIT_NONE && !login_shell) + if (client_exitreason != CLIENT_EXIT_NONE) printf("[%s]\n", client_exit_message()); ppid = getppid(); if (client_exittype == MSG_DETACHKILL && ppid > 1) kill(ppid, SIGHUP); - } else if (flags & CLIENT_CONTROLCONTROL) { + } else if (client_flags & CLIENT_CONTROLCONTROL) { if (client_exitreason != CLIENT_EXIT_NONE) printf("%%exit %s\n", client_exit_message()); else printf("%%exit\n"); printf("\033\\"); tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio); - } + } else if (client_exitreason != CLIENT_EXIT_NONE) + fprintf(stderr, "%s\n", client_exit_message()); setblocking(STDIN_FILENO, 1); return (client_exitval); } /* Send identify messages to server. */ void -client_send_identify(int flags) +client_send_identify(const char *ttynam, const char *cwd) { - const char *s; + const char *s; char **ss; - int fd; + size_t sslen; + int fd, flags = client_flags; + pid_t pid; - client_write_one(MSG_IDENTIFY_FLAGS, -1, &flags, sizeof flags); + proc_send(client_peer, MSG_IDENTIFY_FLAGS, -1, &flags, sizeof flags); if ((s = getenv("TERM")) == NULL) s = ""; - client_write_one(MSG_IDENTIFY_TERM, -1, s, strlen(s) + 1); + proc_send(client_peer, MSG_IDENTIFY_TERM, -1, s, strlen(s) + 1); - if ((s = ttyname(STDIN_FILENO)) == NULL) - s = ""; - client_write_one(MSG_IDENTIFY_TTYNAME, -1, s, strlen(s) + 1); - - if ((fd = open(".", O_RDONLY)) == -1) - fd = open("/", O_RDONLY); - client_write_one(MSG_IDENTIFY_CWD, fd, NULL, 0); + proc_send(client_peer, MSG_IDENTIFY_TTYNAME, -1, ttynam, + strlen(ttynam) + 1); + proc_send(client_peer, MSG_IDENTIFY_CWD, -1, cwd, strlen(cwd) + 1); if ((fd = dup(STDIN_FILENO)) == -1) fatal("dup failed"); - client_write_one(MSG_IDENTIFY_STDIN, fd, NULL, 0); + proc_send(client_peer, MSG_IDENTIFY_STDIN, fd, NULL, 0); - for (ss = environ; *ss != NULL; ss++) - client_write_one(MSG_IDENTIFY_ENVIRON, -1, *ss, strlen(*ss) + 1); + pid = getpid(); + proc_send(client_peer, MSG_IDENTIFY_CLIENTPID, -1, &pid, sizeof pid); - client_write_one(MSG_IDENTIFY_DONE, -1, NULL, 0); - - client_update_event(); -} - -/* Helper to send one message. */ -int -client_write_one(enum msgtype type, int fd, const void *buf, size_t len) -{ - int retval; - - retval = imsg_compose(&client_ibuf, type, PROTOCOL_VERSION, -1, fd, - (void *)buf, len); - if (retval != 1) - return (-1); - return (0); -} - -/* Write a message to the server without a file descriptor. */ -int -client_write_server(enum msgtype type, const void *buf, size_t len) -{ - int retval; - - retval = client_write_one(type, -1, buf, len); - if (retval == 0) - client_update_event(); - return (retval); -} - -/* Update client event based on whether it needs to read or read and write. */ -void -client_update_event(void) -{ - short events; - - event_del(&client_event); - events = EV_READ; - if (client_ibuf.w.queued > 0) - events |= EV_WRITE; - event_set( - &client_event, client_ibuf.fd, events, client_callback, shell_cmd); - event_add(&client_event, NULL); -} - -/* Callback to handle signals in the client. */ -void -client_signal(int sig, unused short events, unused void *data) -{ - struct sigaction sigact; - int status; - - if (sig == SIGCHLD) - waitpid(WAIT_ANY, &status, WNOHANG); - else if (!client_attached) { - if (sig == SIGTERM) - event_loopexit(NULL); - } else { - switch (sig) { - case SIGHUP: - client_exitreason = CLIENT_EXIT_LOST_TTY; - client_exitval = 1; - client_write_server(MSG_EXITING, NULL, 0); - break; - case SIGTERM: - client_exitreason = CLIENT_EXIT_TERMINATED; - client_exitval = 1; - client_write_server(MSG_EXITING, NULL, 0); - break; - case SIGWINCH: - client_write_server(MSG_RESIZE, NULL, 0); - break; - case SIGCONT: - memset(&sigact, 0, sizeof sigact); - sigemptyset(&sigact.sa_mask); - sigact.sa_flags = SA_RESTART; - sigact.sa_handler = SIG_IGN; - if (sigaction(SIGTSTP, &sigact, NULL) != 0) - fatal("sigaction failed"); - client_write_server(MSG_WAKEUP, NULL, 0); - break; - } + for (ss = environ; *ss != NULL; ss++) { + sslen = strlen(*ss) + 1; + if (sslen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) + continue; + proc_send(client_peer, MSG_IDENTIFY_ENVIRON, -1, *ss, sslen); } - client_update_event(); -} - -/* Callback for client imsg read events. */ -void -client_callback(unused int fd, short events, void *data) -{ - ssize_t n; - int retval; - - if (events & EV_READ) { - if ((n = imsg_read(&client_ibuf)) == -1 || n == 0) - goto lost_server; - if (client_attached) - retval = client_dispatch_attached(); - else - retval = client_dispatch_wait(data); - if (retval != 0) { - event_loopexit(NULL); - return; - } - } - - if (events & EV_WRITE) { - if (msgbuf_write(&client_ibuf.w) <= 0 && errno != EAGAIN) - goto lost_server; - } - - client_update_event(); - return; - -lost_server: - client_exitreason = CLIENT_EXIT_LOST_SERVER; - client_exitval = 1; - event_loopexit(NULL); + proc_send(client_peer, MSG_IDENTIFY_DONE, -1, NULL, 0); } /* Callback for client stdin read events. */ void -client_stdin_callback(unused int fd, unused short events, unused void *data1) +client_stdin_callback(__unused int fd, __unused short events, + __unused void *arg) { struct msg_stdin_data data; @@ -517,10 +524,9 @@ client_stdin_callback(unused int fd, unused short events, unused void *data1) if (data.size < 0 && (errno == EINTR || errno == EAGAIN)) return; - client_write_server(MSG_STDIN, &data, sizeof data); + proc_send(client_peer, MSG_STDIN, -1, &data, sizeof data); if (data.size <= 0) event_del(&client_stdin); - client_update_event(); } /* Force write to file descriptor. */ @@ -541,174 +547,252 @@ client_write(int fd, const char *data, size_t size) } } -/* Dispatch imsgs when in wait state (before MSG_READY). */ -int -client_dispatch_wait(void *data0) +/* Run command in shell; used for -c. */ +__dead void +client_exec(const char *shell, const char *shellcmd) +{ + const char *name, *ptr; + char *argv0; + + log_debug("shell %s, command %s", shell, shellcmd); + + ptr = strrchr(shell, '/'); + if (ptr != NULL && *(ptr + 1) != '\0') + name = ptr + 1; + else + name = shell; + if (client_flags & CLIENT_LOGIN) + xasprintf(&argv0, "-%s", name); + else + xasprintf(&argv0, "%s", name); + setenv("SHELL", shell, 1); + + setblocking(STDIN_FILENO, 1); + setblocking(STDOUT_FILENO, 1); + setblocking(STDERR_FILENO, 1); + closefrom(STDERR_FILENO + 1); + + execl(shell, argv0, "-c", shellcmd, (char *) NULL); + fatal("execl failed"); +} + +/* Callback to handle signals in the client. */ +void +client_signal(int sig) +{ + struct sigaction sigact; + int status; + + if (sig == SIGCHLD) + waitpid(WAIT_ANY, &status, WNOHANG); + else if (!client_attached) { + if (sig == SIGTERM) + proc_exit(client_proc); + } else { + switch (sig) { + case SIGHUP: + client_exitreason = CLIENT_EXIT_LOST_TTY; + client_exitval = 1; + proc_send(client_peer, MSG_EXITING, -1, NULL, 0); + break; + case SIGTERM: + client_exitreason = CLIENT_EXIT_TERMINATED; + client_exitval = 1; + proc_send(client_peer, MSG_EXITING, -1, NULL, 0); + break; + case SIGWINCH: + proc_send(client_peer, MSG_RESIZE, -1, NULL, 0); + break; + case SIGCONT: + memset(&sigact, 0, sizeof sigact); + sigemptyset(&sigact.sa_mask); + sigact.sa_flags = SA_RESTART; + sigact.sa_handler = SIG_IGN; + if (sigaction(SIGTSTP, &sigact, NULL) != 0) + fatal("sigaction failed"); + proc_send(client_peer, MSG_WAKEUP, -1, NULL, 0); + break; + } + } +} + +/* Callback for client read events. */ +void +client_dispatch(struct imsg *imsg, void *arg) +{ + if (imsg == NULL) { + client_exitreason = CLIENT_EXIT_LOST_SERVER; + client_exitval = 1; + proc_exit(client_proc); + return; + } + + if (client_attached) + client_dispatch_attached(imsg); + else + client_dispatch_wait(imsg, arg); +} + +/* Dispatch imsgs when in wait state (before MSG_READY). */ +void +client_dispatch_wait(struct imsg *imsg, const char *shellcmd) { - struct imsg imsg; char *data; - ssize_t n, datalen; + ssize_t datalen; struct msg_stdout_data stdoutdata; struct msg_stderr_data stderrdata; int retval; +#ifdef __OpenBSD__ + static int pledge_applied; - for (;;) { - if ((n = imsg_get(&client_ibuf, &imsg)) == -1) - fatalx("imsg_get failed"); - if (n == 0) - return (0); + /* + * "sendfd" is no longer required once all of the identify messages + * have been sent. We know the server won't send us anything until that + * point (because we don't ask it to), so we can drop "sendfd" once we + * get the first message from the server. + */ + if (!pledge_applied) { + if (pledge("stdio unix proc exec tty", NULL) != 0) + fatal("pledge failed"); + pledge_applied = 1; + }; +#endif - data = imsg.data; - datalen = imsg.hdr.len - IMSG_HEADER_SIZE; + data = imsg->data; + datalen = imsg->hdr.len - IMSG_HEADER_SIZE; - log_debug("got %d from server", imsg.hdr.type); - switch (imsg.hdr.type) { - case MSG_EXIT: - case MSG_SHUTDOWN: - if (datalen != sizeof retval && datalen != 0) - fatalx("bad MSG_EXIT size"); - if (datalen == sizeof retval) { - memcpy(&retval, data, sizeof retval); - client_exitval = retval; - } - imsg_free(&imsg); - return (-1); - case MSG_READY: - if (datalen != 0) - fatalx("bad MSG_READY size"); - - event_del(&client_stdin); - client_attached = 1; - client_write_server(MSG_RESIZE, NULL, 0); - break; - case MSG_STDIN: - if (datalen != 0) - fatalx("bad MSG_STDIN size"); - - event_add(&client_stdin, NULL); - break; - case MSG_STDOUT: - if (datalen != sizeof stdoutdata) - fatalx("bad MSG_STDOUT size"); - memcpy(&stdoutdata, data, sizeof stdoutdata); - - client_write(STDOUT_FILENO, stdoutdata.data, - stdoutdata.size); - break; - case MSG_STDERR: - if (datalen != sizeof stderrdata) - fatalx("bad MSG_STDERR size"); - memcpy(&stderrdata, data, sizeof stderrdata); - - client_write(STDERR_FILENO, stderrdata.data, - stderrdata.size); - break; - case MSG_VERSION: - if (datalen != 0) - fatalx("bad MSG_VERSION size"); - - fprintf(stderr, "protocol version mismatch " - "(client %u, server %u)\n", PROTOCOL_VERSION, - imsg.hdr.peerid); - client_exitval = 1; - - imsg_free(&imsg); - return (-1); - case MSG_SHELL: - if (datalen == 0 || data[datalen - 1] != '\0') - fatalx("bad MSG_SHELL string"); - - clear_signals(0); - shell_exec(data, data0); - /* NOTREACHED */ - case MSG_DETACH: - case MSG_DETACHKILL: - client_write_server(MSG_EXITING, NULL, 0); - break; - case MSG_EXITED: - imsg_free(&imsg); - return (-1); + switch (imsg->hdr.type) { + case MSG_EXIT: + case MSG_SHUTDOWN: + if (datalen != sizeof retval && datalen != 0) + fatalx("bad MSG_EXIT size"); + if (datalen == sizeof retval) { + memcpy(&retval, data, sizeof retval); + client_exitval = retval; } + proc_exit(client_proc); + break; + case MSG_READY: + if (datalen != 0) + fatalx("bad MSG_READY size"); - imsg_free(&imsg); + event_del(&client_stdin); + client_attached = 1; + proc_send(client_peer, MSG_RESIZE, -1, NULL, 0); + break; + case MSG_STDIN: + if (datalen != 0) + fatalx("bad MSG_STDIN size"); + + event_add(&client_stdin, NULL); + break; + case MSG_STDOUT: + if (datalen != sizeof stdoutdata) + fatalx("bad MSG_STDOUT size"); + memcpy(&stdoutdata, data, sizeof stdoutdata); + + client_write(STDOUT_FILENO, stdoutdata.data, + stdoutdata.size); + break; + case MSG_STDERR: + if (datalen != sizeof stderrdata) + fatalx("bad MSG_STDERR size"); + memcpy(&stderrdata, data, sizeof stderrdata); + + client_write(STDERR_FILENO, stderrdata.data, + stderrdata.size); + break; + case MSG_VERSION: + if (datalen != 0) + fatalx("bad MSG_VERSION size"); + + fprintf(stderr, "protocol version mismatch " + "(client %d, server %u)\n", PROTOCOL_VERSION, + imsg->hdr.peerid & 0xff); + client_exitval = 1; + proc_exit(client_proc); + break; + case MSG_SHELL: + if (datalen == 0 || data[datalen - 1] != '\0') + fatalx("bad MSG_SHELL string"); + + clear_signals(0); + client_exec(data, shellcmd); + /* NOTREACHED */ + case MSG_DETACH: + case MSG_DETACHKILL: + proc_send(client_peer, MSG_EXITING, -1, NULL, 0); + break; + case MSG_EXITED: + proc_exit(client_proc); + break; } } /* Dispatch imsgs in attached state (after MSG_READY). */ -int -client_dispatch_attached(void) +void +client_dispatch_attached(struct imsg *imsg) { - struct imsg imsg; struct sigaction sigact; char *data; - ssize_t n, datalen; + ssize_t datalen; - for (;;) { - if ((n = imsg_get(&client_ibuf, &imsg)) == -1) - fatalx("imsg_get failed"); - if (n == 0) - return (0); + data = imsg->data; + datalen = imsg->hdr.len - IMSG_HEADER_SIZE; - data = imsg.data; - datalen = imsg.hdr.len - IMSG_HEADER_SIZE; + switch (imsg->hdr.type) { + case MSG_DETACH: + case MSG_DETACHKILL: + if (datalen == 0 || data[datalen - 1] != '\0') + fatalx("bad MSG_DETACH string"); - log_debug("got %d from server", imsg.hdr.type); - switch (imsg.hdr.type) { - case MSG_DETACH: - case MSG_DETACHKILL: - if (datalen == 0 || data[datalen - 1] != '\0') - fatalx("bad MSG_DETACH string"); + client_exitsession = xstrdup(data); + client_exittype = imsg->hdr.type; + if (imsg->hdr.type == MSG_DETACHKILL) + client_exitreason = CLIENT_EXIT_DETACHED_HUP; + else + client_exitreason = CLIENT_EXIT_DETACHED; + proc_send(client_peer, MSG_EXITING, -1, NULL, 0); + break; + case MSG_EXIT: + if (datalen != 0 && datalen != sizeof (int)) + fatalx("bad MSG_EXIT size"); - client_exitsession = xstrdup(data); - client_exittype = imsg.hdr.type; - if (imsg.hdr.type == MSG_DETACHKILL) - client_exitreason = CLIENT_EXIT_DETACHED_HUP; - else - client_exitreason = CLIENT_EXIT_DETACHED; - client_write_server(MSG_EXITING, NULL, 0); - break; - case MSG_EXIT: - if (datalen != 0 && datalen != sizeof (int)) - fatalx("bad MSG_EXIT size"); + proc_send(client_peer, MSG_EXITING, -1, NULL, 0); + client_exitreason = CLIENT_EXIT_EXITED; + break; + case MSG_EXITED: + if (datalen != 0) + fatalx("bad MSG_EXITED size"); - client_write_server(MSG_EXITING, NULL, 0); - client_exitreason = CLIENT_EXIT_EXITED; - break; - case MSG_EXITED: - if (datalen != 0) - fatalx("bad MSG_EXITED size"); + proc_exit(client_proc); + break; + case MSG_SHUTDOWN: + if (datalen != 0) + fatalx("bad MSG_SHUTDOWN size"); - imsg_free(&imsg); - return (-1); - case MSG_SHUTDOWN: - if (datalen != 0) - fatalx("bad MSG_SHUTDOWN size"); + proc_send(client_peer, MSG_EXITING, -1, NULL, 0); + client_exitreason = CLIENT_EXIT_SERVER_EXITED; + client_exitval = 1; + break; + case MSG_SUSPEND: + if (datalen != 0) + fatalx("bad MSG_SUSPEND size"); - client_write_server(MSG_EXITING, NULL, 0); - client_exitreason = CLIENT_EXIT_SERVER_EXITED; - client_exitval = 1; - break; - case MSG_SUSPEND: - if (datalen != 0) - fatalx("bad MSG_SUSPEND size"); + memset(&sigact, 0, sizeof sigact); + sigemptyset(&sigact.sa_mask); + sigact.sa_flags = SA_RESTART; + sigact.sa_handler = SIG_DFL; + if (sigaction(SIGTSTP, &sigact, NULL) != 0) + fatal("sigaction failed"); + kill(getpid(), SIGTSTP); + break; + case MSG_LOCK: + if (datalen == 0 || data[datalen - 1] != '\0') + fatalx("bad MSG_LOCK string"); - memset(&sigact, 0, sizeof sigact); - sigemptyset(&sigact.sa_mask); - sigact.sa_flags = SA_RESTART; - sigact.sa_handler = SIG_DFL; - if (sigaction(SIGTSTP, &sigact, NULL) != 0) - fatal("sigaction failed"); - kill(getpid(), SIGTSTP); - break; - case MSG_LOCK: - if (datalen == 0 || data[datalen - 1] != '\0') - fatalx("bad MSG_LOCK string"); - - system(data); - client_write_server(MSG_UNLOCK, NULL, 0); - break; - } - - imsg_free(&imsg); + system(data); + proc_send(client_peer, MSG_UNLOCK, -1, NULL, 0); + break; } } diff --git a/cmd-attach-session.c b/cmd-attach-session.c index a67ec82c..53c1df31 100644 --- a/cmd-attach-session.c +++ b/cmd-attach-session.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -33,52 +33,42 @@ enum cmd_retval cmd_attach_session_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_attach_session_entry = { - "attach-session", "attach", - "c:drt:", 0, 0, - "[-dr] [-c working-directory] " CMD_TARGET_SESSION_USAGE, - CMD_CANTNEST|CMD_STARTSERVER, - cmd_attach_session_exec + .name = "attach-session", + .alias = "attach", + + .args = { "c:dErt:", 0, 0 }, + .usage = "[-dEr] [-c working-directory] " CMD_TARGET_SESSION_USAGE, + + .tflag = CMD_SESSION_WITHPANE, + + .flags = CMD_STARTSERVER, + .exec = cmd_attach_session_exec }; enum cmd_retval -cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag, - const char *cflag) +cmd_attach_session(struct cmd_q *cmdq, int dflag, int rflag, const char *cflag, + int Eflag) { - struct session *s; - struct client *c; - struct winlink *wl = NULL; - struct window *w = NULL; - struct window_pane *wp = NULL; + struct session *s = cmdq->state.tflag.s; + struct client *c = cmdq->client, *c_loop; + struct winlink *wl = cmdq->state.tflag.wl; + struct window_pane *wp = cmdq->state.tflag.wp; const char *update; - char *cause; - u_int i; - int fd; + char *cause, *cwd; struct format_tree *ft; - char *cp; if (RB_EMPTY(&sessions)) { cmdq_error(cmdq, "no sessions"); return (CMD_RETURN_ERROR); } - if (tflag == NULL) { - if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) - return (CMD_RETURN_ERROR); - } else if (tflag[strcspn(tflag, ":.")] != '\0') { - if ((wl = cmd_find_pane(cmdq, tflag, &s, &wp)) == NULL) - return (CMD_RETURN_ERROR); - } else { - if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) - return (CMD_RETURN_ERROR); - w = cmd_lookup_windowid(tflag); - if (w == NULL && (wp = cmd_lookup_paneid(tflag)) != NULL) - w = wp->window; - if (w != NULL) - wl = winlink_find_by_window(&s->windows, w); - } - - if (cmdq->client == NULL) + if (c == NULL) return (CMD_RETURN_NORMAL); + if (server_client_check_nested(c)) { + cmdq_error(cmdq, "sessions should be nested with care, " + "unset $TMUX to force"); + return (CMD_RETURN_ERROR); + } if (wl != NULL) { if (wp != NULL) @@ -86,93 +76,79 @@ cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag, session_set_current(s, wl); } - if (cmdq->client->session != NULL) { + if (cflag != NULL) { + ft = format_create(cmdq, 0); + format_defaults(ft, c, s, wl, wp); + cwd = format_expand(ft, cflag); + format_free(ft); + + free((void *)s->cwd); + s->cwd = cwd; + } + + if (c->session != NULL) { if (dflag) { - /* - * Can't use server_write_session in case attaching to - * the same session as currently attached to. - */ - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session != s) + TAILQ_FOREACH(c_loop, &clients, entry) { + if (c_loop->session != s || c == c_loop) continue; - if (c == cmdq->client) - continue; - server_write_client(c, MSG_DETACH, - c->session->name, - strlen(c->session->name) + 1); + server_client_detach(c_loop, MSG_DETACH); } } - if (cflag != NULL) { - ft = format_create(); - format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, - NULL, NULL); - cp = format_expand(ft, cflag); - format_free(ft); - - fd = open(cp, O_RDONLY|O_DIRECTORY); - free(cp); - if (fd == -1) { - cmdq_error(cmdq, "bad working directory: %s", - strerror(errno)); - return (CMD_RETURN_ERROR); - } - close(s->cwd); - s->cwd = fd; + if (!Eflag) { + update = options_get_string(s->options, + "update-environment"); + environ_update(update, c->environ, s->environ); } - cmdq->client->session = s; - notify_attached_session_changed(cmdq->client); - session_update_activity(s); - server_redraw_client(cmdq->client); + c->session = s; + server_client_set_key_table(c, NULL); + status_timer_start(c); + notify_attached_session_changed(c); + session_update_activity(s, NULL); + gettimeofday(&s->last_attached_time, NULL); + server_redraw_client(c); s->curw->flags &= ~WINLINK_ALERTFLAGS; } else { - if (server_client_open(cmdq->client, &cause) != 0) { + if (server_client_open(c, &cause) != 0) { cmdq_error(cmdq, "open terminal failed: %s", cause); free(cause); return (CMD_RETURN_ERROR); } - if (cflag != NULL) { - ft = format_create(); - format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, - NULL, NULL); - cp = format_expand(ft, cflag); - format_free(ft); - - fd = open(cp, O_RDONLY|O_DIRECTORY); - free(cp); - if (fd == -1) { - cmdq_error(cmdq, "bad working directory: %s", - strerror(errno)); - return (CMD_RETURN_ERROR); - } - close(s->cwd); - s->cwd = fd; - } - if (rflag) - cmdq->client->flags |= CLIENT_READONLY; + c->flags |= CLIENT_READONLY; if (dflag) { - server_write_session(s, MSG_DETACH, s->name, - strlen(s->name) + 1); + TAILQ_FOREACH(c_loop, &clients, entry) { + if (c_loop->session != s || c == c_loop) + continue; + server_client_detach(c_loop, MSG_DETACH); + } } - update = options_get_string(&s->options, "update-environment"); - environ_update(update, &cmdq->client->environ, &s->environ); + if (!Eflag) { + update = options_get_string(s->options, + "update-environment"); + environ_update(update, c->environ, s->environ); + } - cmdq->client->session = s; - notify_attached_session_changed(cmdq->client); - session_update_activity(s); - server_redraw_client(cmdq->client); + c->session = s; + server_client_set_key_table(c, NULL); + status_timer_start(c); + notify_attached_session_changed(c); + session_update_activity(s, NULL); + gettimeofday(&s->last_attached_time, NULL); + server_redraw_client(c); s->curw->flags &= ~WINLINK_ALERTFLAGS; - server_write_ready(cmdq->client); + if (~c->flags & CLIENT_CONTROL) + proc_send(c->peer, MSG_READY, -1, NULL, 0); + hooks_run(c->session->hooks, c, NULL, "client-attached"); cmdq->client_exit = 0; } recalculate_sizes(); + alerts_check_session(s); server_update_socket(); return (CMD_RETURN_NORMAL); @@ -183,6 +159,6 @@ cmd_attach_session_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - return (cmd_attach_session(cmdq, args_get(args, 't'), - args_has(args, 'd'), args_has(args, 'r'), args_get(args, 'c'))); + return (cmd_attach_session(cmdq, args_has(args, 'd'), + args_has(args, 'r'), args_get(args, 'c'), args_has(args, 'E'))); } diff --git a/cmd-bind-key.c b/cmd-bind-key.c index 5d68d486..a829a2c5 100644 --- a/cmd-bind-key.c +++ b/cmd-bind-key.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -29,14 +29,19 @@ enum cmd_retval cmd_bind_key_exec(struct cmd *, struct cmd_q *); -enum cmd_retval cmd_bind_key_mode_table(struct cmd *, struct cmd_q *, int); +enum cmd_retval cmd_bind_key_mode_table(struct cmd *, struct cmd_q *, + key_code); const struct cmd_entry cmd_bind_key_entry = { - "bind-key", "bind", - "cnrt:", 1, -1, - "[-cnr] [-t mode-table] key command [arguments]", - 0, - cmd_bind_key_exec + .name = "bind-key", + .alias = "bind", + + .args = { "cnrt:T:", 1, -1 }, + .usage = "[-cnr] [-t mode-table] [-T key-table] key command " + "[arguments]", + + .flags = 0, + .exec = cmd_bind_key_exec }; enum cmd_retval @@ -45,7 +50,8 @@ cmd_bind_key_exec(struct cmd *self, struct cmd_q *cmdq) struct args *args = self->args; char *cause; struct cmd_list *cmdlist; - int key; + key_code key; + const char *tablename; if (args_has(args, 't')) { if (args->argc != 2 && args->argc != 3) { @@ -60,7 +66,7 @@ cmd_bind_key_exec(struct cmd *self, struct cmd_q *cmdq) } key = key_string_lookup_string(args->argv[0]); - if (key == KEYC_NONE) { + if (key == KEYC_NONE || key == KEYC_UNKNOWN) { cmdq_error(cmdq, "unknown key: %s", args->argv[0]); return (CMD_RETURN_ERROR); } @@ -68,6 +74,13 @@ cmd_bind_key_exec(struct cmd *self, struct cmd_q *cmdq) if (args_has(args, 't')) return (cmd_bind_key_mode_table(self, cmdq, key)); + if (args_has(args, 'T')) + tablename = args_get(args, 'T'); + else if (args_has(args, 'n')) + tablename = "root"; + else + tablename = "prefix"; + cmdlist = cmd_list_parse(args->argc - 1, args->argv + 1, NULL, 0, &cause); if (cmdlist == NULL) { @@ -76,14 +89,12 @@ cmd_bind_key_exec(struct cmd *self, struct cmd_q *cmdq) return (CMD_RETURN_ERROR); } - if (!args_has(args, 'n')) - key |= KEYC_PREFIX; - key_bindings_add(key, args_has(args, 'r'), cmdlist); + key_bindings_add(tablename, key, args_has(args, 'r'), cmdlist); return (CMD_RETURN_NORMAL); } enum cmd_retval -cmd_bind_key_mode_table(struct cmd *self, struct cmd_q *cmdq, int key) +cmd_bind_key_mode_table(struct cmd *self, struct cmd_q *cmdq, key_code key) { struct args *args = self->args; const char *tablename; @@ -104,18 +115,34 @@ cmd_bind_key_mode_table(struct cmd *self, struct cmd_q *cmdq, int key) return (CMD_RETURN_ERROR); } - if (cmd != MODEKEYCOPY_COPYPIPE) { - if (args->argc != 2) { - cmdq_error(cmdq, "no argument allowed"); - return (CMD_RETURN_ERROR); + switch (cmd) { + case MODEKEYCOPY_APPENDSELECTION: + case MODEKEYCOPY_COPYSELECTION: + case MODEKEYCOPY_STARTNAMEDBUFFER: + if (args->argc == 2) + arg = NULL; + else { + arg = args->argv[2]; + if (strcmp(arg, "-x") != 0) { + cmdq_error(cmdq, "unknown argument"); + return (CMD_RETURN_ERROR); + } } - arg = NULL; - } else { + break; + case MODEKEYCOPY_COPYPIPE: if (args->argc != 3) { cmdq_error(cmdq, "no argument given"); return (CMD_RETURN_ERROR); } arg = args->argv[2]; + break; + default: + if (args->argc != 2) { + cmdq_error(cmdq, "no argument allowed"); + return (CMD_RETURN_ERROR); + } + arg = NULL; + break; } mtmp.key = key; diff --git a/cmd-break-pane.c b/cmd-break-pane.c index a1da0a3a..d220d761 100644 --- a/cmd-break-pane.c +++ b/cmd-break-pane.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -31,65 +31,82 @@ enum cmd_retval cmd_break_pane_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_break_pane_entry = { - "break-pane", "breakp", - "dPF:t:", 0, 0, - "[-dP] [-F format] " CMD_TARGET_PANE_USAGE, - 0, - cmd_break_pane_exec + .name = "break-pane", + .alias = "breakp", + + .args = { "dPF:s:t:", 0, 0 }, + .usage = "[-dP] [-F format] [-s src-pane] [-t dst-window]", + + .sflag = CMD_PANE, + .tflag = CMD_WINDOW_INDEX, + + .flags = 0, + .exec = cmd_break_pane_exec }; enum cmd_retval cmd_break_pane_exec(struct cmd *self, struct cmd_q *cmdq) { +#ifdef TMATE + cmdq_error(cmdq, "break pane is not supported with tmate"); + return (CMD_RETURN_ERROR); +#else struct args *args = self->args; - struct winlink *wl; - struct session *s; - struct window_pane *wp; - struct window *w; + struct winlink *wl = cmdq->state.sflag.wl; + struct session *src_s = cmdq->state.sflag.s; + struct session *dst_s = cmdq->state.tflag.s; + struct window_pane *wp = cmdq->state.sflag.wp; + struct window *w = wl->window; char *name; char *cause; - int base_idx; + int idx = cmdq->state.tflag.idx; struct format_tree *ft; const char *template; char *cp; - if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp)) == NULL) - return (CMD_RETURN_ERROR); - - if (window_count_panes(wl->window) == 1) { - cmdq_error(cmdq, "can't break with only one pane"); + if (idx != -1 && winlink_find_by_index(&dst_s->windows, idx) != NULL) { + cmdq_error(cmdq, "index %d already in use", idx); return (CMD_RETURN_ERROR); } - w = wl->window; + if (window_count_panes(w) == 1) { + cmdq_error(cmdq, "can't break with only one pane"); + return (CMD_RETURN_ERROR); + } server_unzoom_window(w); TAILQ_REMOVE(&w->panes, wp, entry); window_lost_pane(w, wp); layout_close_pane(wp); - w = wp->window = window_create1(s->sx, s->sy); + w = wp->window = window_create1(dst_s->sx, dst_s->sy); TAILQ_INSERT_HEAD(&w->panes, wp, entry); w->active = wp; name = default_window_name(w); window_set_name(w, name); free(name); layout_init(w, wp); + wp->flags |= PANE_CHANGED; - base_idx = options_get_number(&s->options, "base-index"); - wl = session_attach(s, w, -1 - base_idx, &cause); /* can't fail */ + if (idx == -1) + idx = -1 - options_get_number(dst_s->options, "base-index"); + wl = session_attach(dst_s, w, idx, &cause); /* can't fail */ if (!args_has(self->args, 'd')) - session_select(s, wl->idx); + session_select(dst_s, wl->idx); - server_redraw_session(s); - server_status_session_group(s); + server_redraw_session(src_s); + if (src_s != dst_s) + server_redraw_session(dst_s); + server_status_session_group(src_s); + if (src_s != dst_s) + server_status_session_group(dst_s); if (args_has(args, 'P')) { if ((template = args_get(args, 'F')) == NULL) template = BREAK_PANE_TEMPLATE; - ft = format_create(); - format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, wl, wp); + ft = format_create(cmdq, 0); + format_defaults(ft, cmdq->state.c, dst_s, wl, wp); cp = format_expand(ft, template); cmdq_print(cmdq, "%s", cp); @@ -98,4 +115,5 @@ cmd_break_pane_exec(struct cmd *self, struct cmd_q *cmdq) format_free(ft); } return (CMD_RETURN_NORMAL); +#endif } diff --git a/cmd-capture-pane.c b/cmd-capture-pane.c index ce60b4c5..33f6cf08 100644 --- a/cmd-capture-pane.c +++ b/cmd-capture-pane.c @@ -36,12 +36,17 @@ char *cmd_capture_pane_history(struct args *, struct cmd_q *, struct window_pane *, size_t *); const struct cmd_entry cmd_capture_pane_entry = { - "capture-pane", "capturep", - "ab:CeE:JpPqS:t:", 0, 0, - "[-aCeJpPq] " CMD_BUFFER_USAGE " [-E end-line] [-S start-line]" - CMD_TARGET_PANE_USAGE, - 0, - cmd_capture_pane_exec + .name = "capture-pane", + .alias = "capturep", + + .args = { "ab:CeE:JpPqS:t:", 0, 0 }, + .usage = "[-aCeJpPq] " CMD_BUFFER_USAGE " [-E end-line] " + "[-S start-line]" CMD_TARGET_PANE_USAGE, + + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_capture_pane_exec }; char * @@ -57,15 +62,17 @@ char * cmd_capture_pane_pending(struct args *args, struct window_pane *wp, size_t *len) { - char *buf, *line, tmp[5]; - size_t linelen; - u_int i; + struct evbuffer *pending; + char *buf, *line, tmp[5]; + size_t linelen; + u_int i; - if (wp->ictx.since_ground == NULL) + pending = input_pending(wp); + if (pending == NULL) return (xstrdup("")); - line = EVBUFFER_DATA(wp->ictx.since_ground); - linelen = EVBUFFER_LENGTH(wp->ictx.since_ground); + line = EVBUFFER_DATA(pending); + linelen = EVBUFFER_LENGTH(pending); buf = xstrdup(""); if (args_has(args, 'C')) { @@ -74,7 +81,7 @@ cmd_capture_pane_pending(struct args *args, struct window_pane *wp, tmp[0] = line[i]; tmp[1] = '\0'; } else - xsnprintf(tmp, sizeof tmp, "\\%03o", line[i]); + xsnprintf(tmp, sizeof tmp, "\\%03hho", line[i]); buf = cmd_capture_pane_append(buf, len, tmp, strlen(tmp)); } @@ -173,14 +180,11 @@ cmd_capture_pane_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct client *c; - struct window_pane *wp; + struct window_pane *wp = cmdq->state.tflag.wp; char *buf, *cause; const char *bufname; size_t len; - if (cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp) == NULL) - return (CMD_RETURN_ERROR); - len = 0; if (args_has(args, 'P')) buf = cmd_capture_pane_pending(args, wp, &len); @@ -194,22 +198,23 @@ cmd_capture_pane_exec(struct cmd *self, struct cmd_q *cmdq) if (c == NULL || (c->session != NULL && !(c->flags & CLIENT_CONTROL))) { cmdq_error(cmdq, "can't write to stdout"); + free(buf); return (CMD_RETURN_ERROR); } evbuffer_add(c->stdout_data, buf, len); + free(buf); if (args_has(args, 'P') && len > 0) evbuffer_add(c->stdout_data, "\n", 1); - server_push_stdout(c); + server_client_push_stdout(c); } else { - bufname = NULL; if (args_has(args, 'b')) bufname = args_get(args, 'b'); if (paste_set(buf, len, bufname, &cause) != 0) { cmdq_error(cmdq, "%s", cause); - free(buf); free(cause); + free(buf); return (CMD_RETURN_ERROR); } } diff --git a/cmd-choose-buffer.c b/cmd-choose-buffer.c index 33125a0e..90872e90 100644 --- a/cmd-choose-buffer.c +++ b/cmd-choose-buffer.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2010 Nicholas Marriott + * Copyright (c) 2010 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -33,27 +33,31 @@ enum cmd_retval cmd_choose_buffer_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_choose_buffer_entry = { - "choose-buffer", NULL, - "F:t:", 0, 1, - CMD_TARGET_WINDOW_USAGE " [-F format] [template]", - 0, - cmd_choose_buffer_exec + .name = "choose-buffer", + .alias = NULL, + + .args = { "F:t:", 0, 1 }, + .usage = CMD_TARGET_WINDOW_USAGE " [-F format] [template]", + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_choose_buffer_exec }; enum cmd_retval cmd_choose_buffer_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct client *c; + struct client *c = cmdq->state.c; + struct winlink *wl = cmdq->state.tflag.wl; struct window_choose_data *cdata; - struct winlink *wl; struct paste_buffer *pb; char *action, *action_data; const char *template; u_int idx; - int utf8flag; - if ((c = cmd_current_client(cmdq)) == NULL) { + if (c == NULL) { cmdq_error(cmdq, "no client available"); return (CMD_RETURN_ERROR); } @@ -61,11 +65,7 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_q *cmdq) if ((template = args_get(args, 'F')) == NULL) template = CHOOSE_BUFFER_TEMPLATE; - if ((wl = cmd_find_window(cmdq, args_get(args, 't'), NULL)) == NULL) - return (CMD_RETURN_ERROR); - utf8flag = options_get_number(&wl->window->options, "utf8"); - - if (paste_get_top() == NULL) + if (paste_get_top(NULL) == NULL) return (CMD_RETURN_NORMAL); if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0) @@ -83,9 +83,9 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_q *cmdq) cdata->idx = idx; cdata->ft_template = xstrdup(template); - format_defaults_paste_buffer(cdata->ft, pb, utf8flag); + format_defaults_paste_buffer(cdata->ft, pb); - xasprintf(&action_data, "%s", pb->name); + xasprintf(&action_data, "%s", paste_buffer_name(pb)); cdata->command = cmd_template_replace(action, action_data, 1); free(action_data); diff --git a/cmd-choose-client.c b/cmd-choose-client.c index 3002f7ba..b9a24be6 100644 --- a/cmd-choose-client.c +++ b/cmd-choose-client.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -31,18 +31,23 @@ "#{client_tty}: #{session_name} " \ "[#{client_width}x#{client_height} #{client_termname}]" \ "#{?client_utf8, (utf8),}#{?client_readonly, (ro),} " \ - "(last used #{client_activity_string})" + "(last used #{t:client_activity})" enum cmd_retval cmd_choose_client_exec(struct cmd *, struct cmd_q *); void cmd_choose_client_callback(struct window_choose_data *); const struct cmd_entry cmd_choose_client_entry = { - "choose-client", NULL, - "F:t:", 0, 1, - CMD_TARGET_WINDOW_USAGE " [-F format] [template]", - 0, - cmd_choose_client_exec + .name = "choose-client", + .alias = NULL, + + .args = { "F:t:", 0, 1 }, + .usage = CMD_TARGET_WINDOW_USAGE " [-F format] [template]", + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_choose_client_exec }; struct cmd_choose_client_data { @@ -53,22 +58,19 @@ enum cmd_retval cmd_choose_client_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct client *c; + struct client *c = cmdq->state.c; struct client *c1; struct window_choose_data *cdata; - struct winlink *wl; + struct winlink *wl = cmdq->state.tflag.wl; const char *template; char *action; - u_int i, idx, cur; + u_int idx, cur; - if ((c = cmd_current_client(cmdq)) == NULL) { + if (c == NULL) { cmdq_error(cmdq, "no client available"); return (CMD_RETURN_ERROR); } - if ((wl = cmd_find_window(cmdq, args_get(args, 't'), NULL)) == NULL) - return (CMD_RETURN_ERROR); - if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0) return (CMD_RETURN_NORMAL); @@ -81,24 +83,24 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_q *cmdq) action = xstrdup("detach-client -t '%%'"); cur = idx = 0; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c1 = ARRAY_ITEM(&clients, i); - if (c1 == NULL || c1->session == NULL || c1->tty.path == NULL) + TAILQ_FOREACH(c1, &clients, entry) { + if (c1->session == NULL || c1->tty.path == NULL) continue; if (c1 == cmdq->client) cur = idx; - idx++; cdata = window_choose_data_create(TREE_OTHER, c, c->session); - cdata->idx = i; + cdata->idx = idx; cdata->ft_template = xstrdup(template); - format_add(cdata->ft, "line", "%u", i); + format_add(cdata->ft, "line", "%u", idx); format_defaults(cdata->ft, c1, NULL, NULL, NULL); cdata->command = cmd_template_replace(action, c1->tty.path, 1); window_choose_add(wl->window->active, cdata); + + idx++; } free(action); @@ -112,15 +114,19 @@ void cmd_choose_client_callback(struct window_choose_data *cdata) { struct client *c; + u_int idx; if (cdata == NULL) return; if (cdata->start_client->flags & CLIENT_DEAD) return; - if (cdata->idx > ARRAY_LENGTH(&clients) - 1) - return; - c = ARRAY_ITEM(&clients, cdata->idx); + idx = 0; + TAILQ_FOREACH(c, &clients, entry) { + if (idx == cdata->idx) + break; + idx++; + } if (c == NULL || c->session == NULL) return; diff --git a/cmd-choose-tree.c b/cmd-choose-tree.c index 823d0423..db9222ba 100644 --- a/cmd-choose-tree.c +++ b/cmd-choose-tree.c @@ -44,37 +44,52 @@ enum cmd_retval cmd_choose_tree_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_choose_tree_entry = { - "choose-tree", NULL, - "S:W:swub:c:t:", 0, 1, - "[-suw] [-b session-template] [-c window template] [-S format] " \ - "[-W format] " CMD_TARGET_WINDOW_USAGE, - 0, - cmd_choose_tree_exec + .name = "choose-tree", + .alias = NULL, + + .args = { "S:W:swub:c:t:", 0, 1 }, + .usage = "[-suw] [-b session-template] [-c window template] " + "[-S format] [-W format] " CMD_TARGET_WINDOW_USAGE, + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_choose_tree_exec }; const struct cmd_entry cmd_choose_session_entry = { - "choose-session", NULL, - "F:t:", 0, 1, - CMD_TARGET_WINDOW_USAGE " [-F format] [template]", - 0, - cmd_choose_tree_exec + .name = "choose-session", + .alias = NULL, + + .args = { "F:t:", 0, 1 }, + .usage = CMD_TARGET_WINDOW_USAGE " [-F format] [template]", + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_choose_tree_exec }; const struct cmd_entry cmd_choose_window_entry = { - "choose-window", NULL, - "F:t:", 0, 1, - CMD_TARGET_WINDOW_USAGE "[-F format] [template]", - 0, - cmd_choose_tree_exec + .name = "choose-window", + .alias = NULL, + + .args = { "F:t:", 0, 1 }, + .usage = CMD_TARGET_WINDOW_USAGE "[-F format] [template]", + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_choose_tree_exec }; enum cmd_retval cmd_choose_tree_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct winlink *wl, *wm; - struct session *s, *s2; - struct client *c; + struct client *c = cmdq->state.c; + struct winlink *wl = cmdq->state.tflag.wl, *wm; + struct session *s = cmdq->state.tflag.s, *s2; struct window_choose_data *wcd = NULL; const char *ses_template, *win_template; char *final_win_action, *cur_win_template; @@ -87,14 +102,11 @@ cmd_choose_tree_exec(struct cmd *self, struct cmd_q *cmdq) ses_template = win_template = NULL; ses_action = win_action = NULL; - if ((c = cmd_current_client(cmdq)) == NULL) { + if (c == NULL) { cmdq_error(cmdq, "no client available"); return (CMD_RETURN_ERROR); } - if ((wl = cmd_find_window(cmdq, args_get(args, 't'), &s)) == NULL) - return (CMD_RETURN_ERROR); - if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0) return (CMD_RETURN_NORMAL); diff --git a/cmd-clear-history.c b/cmd-clear-history.c index e1342880..62683ff6 100644 --- a/cmd-clear-history.c +++ b/cmd-clear-history.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -27,26 +27,29 @@ enum cmd_retval cmd_clear_history_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_clear_history_entry = { - "clear-history", "clearhist", - "t:", 0, 0, - CMD_TARGET_PANE_USAGE, - 0, - cmd_clear_history_exec + .name = "clear-history", + .alias = "clearhist", + + .args = { "t:", 0, 0 }, + .usage = CMD_TARGET_PANE_USAGE, + + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_clear_history_exec }; enum cmd_retval -cmd_clear_history_exec(struct cmd *self, struct cmd_q *cmdq) +cmd_clear_history_exec(__unused struct cmd *self, struct cmd_q *cmdq) { - struct args *args = self->args; - struct window_pane *wp; + struct window_pane *wp = cmdq->state.tflag.wp; struct grid *gd; - if (cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp) == NULL) - return (CMD_RETURN_ERROR); - gd = wp->base.grid; + gd = cmdq->state.tflag.wp->base.grid; - grid_move_lines(gd, 0, gd->hsize, gd->sy); - gd->hsize = 0; + if (wp->mode == &window_copy_mode) + window_pane_reset_mode(wp); + grid_clear_history(gd); return (CMD_RETURN_NORMAL); } diff --git a/cmd-command-prompt.c b/cmd-command-prompt.c index 22b1d84e..3ec22865 100644 --- a/cmd-command-prompt.c +++ b/cmd-command-prompt.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -35,11 +35,17 @@ int cmd_command_prompt_callback(void *, const char *); void cmd_command_prompt_free(void *); const struct cmd_entry cmd_command_prompt_entry = { - "command-prompt", NULL, - "I:p:t:", 0, 1, - "[-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " [template]", - 0, - cmd_command_prompt_exec + .name = "command-prompt", + .alias = NULL, + + .args = { "I:p:t:", 0, 1 }, + .usage = "[-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " " + "[template]", + + .tflag = CMD_CLIENT, + + .flags = 0, + .exec = cmd_command_prompt_exec }; struct cmd_command_prompt_cdata { @@ -58,13 +64,10 @@ cmd_command_prompt_exec(struct cmd *self, struct cmd_q *cmdq) struct args *args = self->args; const char *inputs, *prompts; struct cmd_command_prompt_cdata *cdata; - struct client *c; + struct client *c = cmdq->state.c; char *prompt, *ptr, *input = NULL; size_t n; - if ((c = cmd_find_client(cmdq, args_get(args, 't'), 0)) == NULL) - return (CMD_RETURN_ERROR); - if (c->prompt_string != NULL) return (CMD_RETURN_NORMAL); @@ -151,7 +154,7 @@ cmd_command_prompt_callback(void *data, const char *s) return (0); } - cmdq_run(c->cmdq, cmdlist); + cmdq_run(c->cmdq, cmdlist, NULL); cmd_list_free(cmdlist); if (c->prompt_callbackfn != (void *) &cmd_command_prompt_callback) diff --git a/cmd-confirm-before.c b/cmd-confirm-before.c index 0bf58449..b5a12821 100644 --- a/cmd-confirm-before.c +++ b/cmd-confirm-before.c @@ -34,11 +34,16 @@ int cmd_confirm_before_callback(void *, const char *); void cmd_confirm_before_free(void *); const struct cmd_entry cmd_confirm_before_entry = { - "confirm-before", "confirm", - "p:t:", 1, 1, - "[-p prompt] " CMD_TARGET_CLIENT_USAGE " command", - 0, - cmd_confirm_before_exec + .name = "confirm-before", + .alias = "confirm", + + .args = { "p:t:", 1, 1 }, + .usage = "[-p prompt] " CMD_TARGET_CLIENT_USAGE " command", + + .tflag = CMD_CLIENT, + + .flags = 0, + .exec = cmd_confirm_before_exec }; struct cmd_confirm_before_data { @@ -51,13 +56,10 @@ cmd_confirm_before_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct cmd_confirm_before_data *cdata; - struct client *c; + struct client *c = cmdq->state.c; char *cmd, *copy, *new_prompt, *ptr; const char *prompt; - if ((c = cmd_find_client(cmdq, args_get(args, 't'), 0)) == NULL) - return (CMD_RETURN_ERROR); - if ((prompt = args_get(args, 'p')) != NULL) xasprintf(&new_prompt, "%s ", prompt); else { @@ -105,7 +107,7 @@ cmd_confirm_before_callback(void *data, const char *s) return (0); } - cmdq_run(c->cmdq, cmdlist); + cmdq_run(c->cmdq, cmdlist, NULL); cmd_list_free(cmdlist); return (0); @@ -117,7 +119,7 @@ cmd_confirm_before_free(void *data) struct cmd_confirm_before_data *cdata = data; struct client *c = cdata->client; - c->references--; + server_client_unref(c); free(cdata->cmd); free(cdata); diff --git a/cmd-copy-mode.c b/cmd-copy-mode.c index b46a5a46..beb4d7c8 100644 --- a/cmd-copy-mode.c +++ b/cmd-copy-mode.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -27,29 +27,45 @@ enum cmd_retval cmd_copy_mode_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_copy_mode_entry = { - "copy-mode", NULL, - "t:u", 0, 0, - "[-u] " CMD_TARGET_PANE_USAGE, - 0, - cmd_copy_mode_exec + .name = "copy-mode", + .alias = NULL, + + .args = { "Met:u", 0, 0 }, + .usage = "[-Mu] " CMD_TARGET_PANE_USAGE, + + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_copy_mode_exec }; const struct cmd_entry cmd_clock_mode_entry = { - "clock-mode", NULL, - "t:", 0, 0, - CMD_TARGET_PANE_USAGE, - 0, - cmd_copy_mode_exec + .name = "clock-mode", + .alias = NULL, + + .args = { "t:", 0, 0 }, + .usage = CMD_TARGET_PANE_USAGE, + + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_copy_mode_exec }; enum cmd_retval cmd_copy_mode_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct window_pane *wp; + struct client *c = cmdq->client; + struct session *s; + struct window_pane *wp = cmdq->state.tflag.wp; - if (cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp) == NULL) - return (CMD_RETURN_ERROR); + if (args_has(args, 'M')) { + if ((wp = cmd_mouse_pane(&cmdq->item->mouse, &s, NULL)) == NULL) + return (CMD_RETURN_NORMAL); + if (c == NULL || c->session != s) + return (CMD_RETURN_NORMAL); + } if (self->entry == &cmd_clock_mode_entry) { window_pane_set_mode(wp, &window_clock_mode); @@ -59,7 +75,12 @@ cmd_copy_mode_exec(struct cmd *self, struct cmd_q *cmdq) if (wp->mode != &window_copy_mode) { if (window_pane_set_mode(wp, &window_copy_mode) != 0) return (CMD_RETURN_NORMAL); - window_copy_init_from_pane(wp); + window_copy_init_from_pane(wp, args_has(self->args, 'e')); + } + if (args_has(args, 'M')) { + if (wp->mode != NULL && wp->mode != &window_copy_mode) + return (CMD_RETURN_NORMAL); + window_copy_start_drag(c, &cmdq->item->mouse); } if (wp->mode == &window_copy_mode && args_has(self->args, 'u')) window_copy_pageup(wp); diff --git a/cmd-delete-buffer.c b/cmd-delete-buffer.c deleted file mode 100644 index 42268b78..00000000 --- a/cmd-delete-buffer.c +++ /dev/null @@ -1,57 +0,0 @@ -/* $OpenBSD$ */ - -/* - * Copyright (c) 2007 Nicholas Marriott - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER - * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING - * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include - -#include - -#include "tmux.h" - -/* - * Delete a paste buffer. - */ - -enum cmd_retval cmd_delete_buffer_exec(struct cmd *, struct cmd_q *); - -const struct cmd_entry cmd_delete_buffer_entry = { - "delete-buffer", "deleteb", - "b:", 0, 0, - CMD_BUFFER_USAGE, - 0, - cmd_delete_buffer_exec -}; - -enum cmd_retval -cmd_delete_buffer_exec(struct cmd *self, struct cmd_q *cmdq) -{ - struct args *args = self->args; - const char *bufname; - - if (!args_has(args, 'b')) { - paste_free_top(); - return (CMD_RETURN_NORMAL); - } - bufname = args_get(args, 'b'); - - if (paste_free_name(bufname) != 0) { - cmdq_error(cmdq, "no buffer %s", bufname); - return (CMD_RETURN_ERROR); - } - - return (CMD_RETURN_NORMAL); -} diff --git a/cmd-detach-client.c b/cmd-detach-client.c index 7f87d2c6..80c6555f 100644 --- a/cmd-detach-client.c +++ b/cmd-detach-client.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -29,36 +29,44 @@ enum cmd_retval cmd_detach_client_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_detach_client_entry = { - "detach-client", "detach", - "as:t:P", 0, 0, - "[-P] [-a] [-s target-session] " CMD_TARGET_CLIENT_USAGE, - CMD_READONLY, - cmd_detach_client_exec + .name = "detach-client", + .alias = "detach", + + .args = { "as:t:P", 0, 0 }, + .usage = "[-P] [-a] [-s target-session] " CMD_TARGET_CLIENT_USAGE, + + .sflag = CMD_SESSION, + .tflag = CMD_CLIENT, + + .flags = CMD_READONLY, + .exec = cmd_detach_client_exec }; const struct cmd_entry cmd_suspend_client_entry = { - "suspend-client", "suspendc", - "t:", 0, 0, - CMD_TARGET_CLIENT_USAGE, - 0, - cmd_detach_client_exec + .name = "suspend-client", + .alias = "suspendc", + + .args = { "t:", 0, 0 }, + .usage = CMD_TARGET_CLIENT_USAGE, + + .tflag = CMD_CLIENT, + + .flags = 0, + .exec = cmd_detach_client_exec }; enum cmd_retval cmd_detach_client_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct client *c, *cloop; + struct client *c = cmdq->state.c, *cloop; struct session *s; enum msgtype msgtype; - u_int i; if (self->entry == &cmd_suspend_client_entry) { - if ((c = cmd_find_client(cmdq, args_get(args, 't'), 0)) == NULL) - return (CMD_RETURN_ERROR); tty_stop_tty(&c->tty); c->flags |= CLIENT_SUSPENDED; - server_write_client(c, MSG_SUSPEND, NULL, 0); + proc_send(c->peer, MSG_SUSPEND, -1, NULL, 0); return (CMD_RETURN_NORMAL); } @@ -68,40 +76,22 @@ cmd_detach_client_exec(struct cmd *self, struct cmd_q *cmdq) msgtype = MSG_DETACH; if (args_has(args, 's')) { - s = cmd_find_session(cmdq, args_get(args, 's'), 0); - if (s == NULL) - return (CMD_RETURN_ERROR); - - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - cloop = ARRAY_ITEM(&clients, i); - if (cloop == NULL || cloop->session != s) - continue; - server_write_client(cloop, msgtype, - cloop->session->name, - strlen(cloop->session->name) + 1); + s = cmdq->state.sflag.s; + TAILQ_FOREACH(cloop, &clients, entry) { + if (cloop->session == s) + server_client_detach(cloop, msgtype); } return (CMD_RETURN_STOP); } - c = cmd_find_client(cmdq, args_get(args, 't'), 0); - if (c == NULL) - return (CMD_RETURN_ERROR); - if (args_has(args, 'a')) { - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - cloop = ARRAY_ITEM(&clients, i); - if (cloop == NULL || cloop->session == NULL) - continue; - if (cloop == c) - continue; - server_write_client(cloop, msgtype, - cloop->session->name, - strlen(cloop->session->name) + 1); + TAILQ_FOREACH(cloop, &clients, entry) { + if (cloop->session != NULL && cloop != c) + server_client_detach(cloop, msgtype); } return (CMD_RETURN_NORMAL); } - server_write_client(c, msgtype, c->session->name, - strlen(c->session->name) + 1); + server_client_detach(c, msgtype); return (CMD_RETURN_STOP); } diff --git a/cmd-display-message.c b/cmd-display-message.c index 0a61fd1e..a041b5a1 100644 --- a/cmd-display-message.c +++ b/cmd-display-message.c @@ -35,70 +35,47 @@ enum cmd_retval cmd_display_message_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_display_message_entry = { - "display-message", "display", - "c:pt:F:", 0, 1, - "[-p] [-c target-client] [-F format] " CMD_TARGET_PANE_USAGE - " [message]", - 0, - cmd_display_message_exec + .name = "display-message", + .alias = "display", + + .args = { "c:pt:F:", 0, 1 }, + .usage = "[-p] [-c target-client] [-F format] " + CMD_TARGET_PANE_USAGE " [message]", + + .cflag = CMD_CLIENT_CANFAIL, + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_display_message_exec }; enum cmd_retval cmd_display_message_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct client *c; - struct session *s; - struct winlink *wl; - struct window_pane *wp; + struct client *c = cmdq->state.c; + struct session *s = cmdq->state.tflag.s; + struct winlink *wl = cmdq->state.tflag.wl; + struct window_pane *wp = cmdq->state.tflag.wp; const char *template; char *msg; struct format_tree *ft; - char out[BUFSIZ]; - time_t t; - size_t len; - - if (args_has(args, 't')) { - wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp); - if (wl == NULL) - return (CMD_RETURN_ERROR); - } else { - wl = cmd_find_pane(cmdq, NULL, &s, &wp); - if (wl == NULL) - return (CMD_RETURN_ERROR); - } if (args_has(args, 'F') && args->argc != 0) { cmdq_error(cmdq, "only one of -F or argument must be given"); return (CMD_RETURN_ERROR); } - if (args_has(args, 'c')) { - c = cmd_find_client(cmdq, args_get(args, 'c'), 0); - if (c == NULL) - return (CMD_RETURN_ERROR); - } else { - c = cmd_current_client(cmdq); - if (c == NULL && !args_has(self->args, 'p')) { - cmdq_error(cmdq, "no client available"); - return (CMD_RETURN_ERROR); - } - } - template = args_get(args, 'F'); if (args->argc != 0) template = args->argv[0]; if (template == NULL) template = DISPLAY_MESSAGE_TEMPLATE; - ft = format_create(); + ft = format_create(cmdq, 0); format_defaults(ft, c, s, wl, wp); - t = time(NULL); - len = strftime(out, sizeof out, template, localtime(&t)); - out[len] = '\0'; - - msg = format_expand(ft, out); + msg = format_expand_time(ft, template, time(NULL)); if (args_has(self->args, 'p')) cmdq_print(cmdq, "%s", msg); else diff --git a/cmd-display-panes.c b/cmd-display-panes.c index 9ce89712..eed3611e 100644 --- a/cmd-display-panes.c +++ b/cmd-display-panes.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -27,23 +27,22 @@ enum cmd_retval cmd_display_panes_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_display_panes_entry = { - "display-panes", "displayp", - "t:", 0, 0, - CMD_TARGET_CLIENT_USAGE, - 0, - cmd_display_panes_exec + .name = "display-panes", + .alias = "displayp", + + .args = { "t:", 0, 0 }, + .usage = CMD_TARGET_CLIENT_USAGE, + + .tflag = CMD_CLIENT, + + .flags = 0, + .exec = cmd_display_panes_exec }; enum cmd_retval -cmd_display_panes_exec(struct cmd *self, struct cmd_q *cmdq) +cmd_display_panes_exec(__unused struct cmd *self, struct cmd_q *cmdq) { - struct args *args = self->args; - struct client *c; - - if ((c = cmd_find_client(cmdq, args_get(args, 't'), 0)) == NULL) - return (CMD_RETURN_ERROR); - - server_set_identify(c); + server_set_identify(cmdq->state.c); return (CMD_RETURN_NORMAL); } diff --git a/cmd-find-window.c b/cmd-find-window.c index 25155f7b..6324f26a 100644 --- a/cmd-find-window.c +++ b/cmd-find-window.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -48,22 +48,28 @@ void cmd_find_window_callback(struct window_choose_data *); CMD_FIND_WINDOW_BY_NAME) const struct cmd_entry cmd_find_window_entry = { - "find-window", "findw", - "F:CNt:T", 1, 4, - "[-CNT] [-F format] " CMD_TARGET_WINDOW_USAGE " match-string", - 0, - cmd_find_window_exec + .name = "find-window", + .alias = "findw", + + .args = { "F:CNt:T", 1, 4 }, + .usage = "[-CNT] [-F format] " CMD_TARGET_WINDOW_USAGE " match-string", + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_find_window_exec }; struct cmd_find_window_data { struct winlink *wl; char *list_ctx; u_int pane_id; + TAILQ_ENTRY(cmd_find_window_data) entry; }; -ARRAY_DECL(cmd_find_window_data_list, struct cmd_find_window_data); +TAILQ_HEAD(cmd_find_window_list, cmd_find_window_data); u_int cmd_find_window_match_flags(struct args *); -void cmd_find_window_match(struct cmd_find_window_data_list *, int, +void cmd_find_window_match(struct cmd_find_window_list *, int, struct winlink *, const char *, const char *); u_int @@ -87,16 +93,16 @@ cmd_find_window_match_flags(struct args *args) } void -cmd_find_window_match(struct cmd_find_window_data_list *find_list, +cmd_find_window_match(struct cmd_find_window_list *find_list, int match_flags, struct winlink *wl, const char *str, const char *searchstr) { - struct cmd_find_window_data find_data; + struct cmd_find_window_data *find_data; struct window_pane *wp; u_int i, line; char *sres; - memset(&find_data, 0, sizeof find_data); + find_data = xcalloc(1, sizeof *find_data); i = 0; TAILQ_FOREACH(wp, &wl->window->panes, entry) { @@ -104,53 +110,53 @@ cmd_find_window_match(struct cmd_find_window_data_list *find_list, if ((match_flags & CMD_FIND_WINDOW_BY_NAME) && fnmatch(searchstr, wl->window->name, 0) == 0) { - find_data.list_ctx = xstrdup(""); + find_data->list_ctx = xstrdup(""); break; } if ((match_flags & CMD_FIND_WINDOW_BY_TITLE) && fnmatch(searchstr, wp->base.title, 0) == 0) { - xasprintf(&find_data.list_ctx, + xasprintf(&find_data->list_ctx, "pane %u title: \"%s\"", i - 1, wp->base.title); break; } if (match_flags & CMD_FIND_WINDOW_BY_CONTENT && (sres = window_pane_search(wp, str, &line)) != NULL) { - xasprintf(&find_data.list_ctx, + xasprintf(&find_data->list_ctx, "pane %u line %u: \"%s\"", i - 1, line + 1, sres); free(sres); break; } } - if (find_data.list_ctx != NULL) { - find_data.wl = wl; - find_data.pane_id = i - 1; - ARRAY_ADD(find_list, find_data); - } + + if (find_data->list_ctx != NULL) { + find_data->wl = wl; + find_data->pane_id = i - 1; + TAILQ_INSERT_TAIL(find_list, find_data, entry); + } else + free(find_data); } enum cmd_retval cmd_find_window_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct client *c; + struct client *c = cmdq->state.c; struct window_choose_data *cdata; - struct session *s; - struct winlink *wl, *wm; - struct cmd_find_window_data_list find_list; + struct session *s = cmdq->state.tflag.s; + struct winlink *wl = cmdq->state.tflag.wl, *wm; + struct cmd_find_window_list find_list; + struct cmd_find_window_data *find_data; + struct cmd_find_window_data *find_data1; char *str, *searchstr; const char *template; u_int i, match_flags; - if ((c = cmd_current_client(cmdq)) == NULL) { + if (c == NULL) { cmdq_error(cmdq, "no client available"); return (CMD_RETURN_ERROR); } - s = c->session; - - if ((wl = cmd_find_window(cmdq, args_get(args, 't'), NULL)) == NULL) - return (CMD_RETURN_ERROR); if ((template = args_get(args, 'F')) == NULL) template = FIND_WINDOW_TEMPLATE; @@ -158,21 +164,20 @@ cmd_find_window_exec(struct cmd *self, struct cmd_q *cmdq) match_flags = cmd_find_window_match_flags(args); str = args->argv[0]; - ARRAY_INIT(&find_list); + TAILQ_INIT(&find_list); xasprintf(&searchstr, "*%s*", str); RB_FOREACH(wm, winlinks, &s->windows) cmd_find_window_match(&find_list, match_flags, wm, str, searchstr); free(searchstr); - if (ARRAY_LENGTH(&find_list) == 0) { + if (TAILQ_EMPTY(&find_list)) { cmdq_error(cmdq, "no windows matching: %s", str); - ARRAY_FREE(&find_list); return (CMD_RETURN_ERROR); } - if (ARRAY_LENGTH(&find_list) == 1) { - if (session_select(s, ARRAY_FIRST(&find_list).wl->idx) == 0) + if (TAILQ_NEXT(TAILQ_FIRST(&find_list), entry) == NULL) { + if (session_select(s, TAILQ_FIRST(&find_list)->wl->idx) == 0) server_redraw_session(s); recalculate_sizes(); goto out; @@ -181,30 +186,33 @@ cmd_find_window_exec(struct cmd *self, struct cmd_q *cmdq) if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0) goto out; - for (i = 0; i < ARRAY_LENGTH(&find_list); i++) { - wm = ARRAY_ITEM(&find_list, i).wl; - + i = 0; + TAILQ_FOREACH(find_data, &find_list, entry) { cdata = window_choose_data_create(TREE_OTHER, c, c->session); - cdata->idx = wm->idx; - cdata->wl = wm; + cdata->idx = find_data->wl->idx; + cdata->wl = find_data->wl; cdata->ft_template = xstrdup(template); - cdata->pane_id = ARRAY_ITEM(&find_list, i).pane_id; + cdata->pane_id = find_data->pane_id; format_add(cdata->ft, "line", "%u", i); format_add(cdata->ft, "window_find_matches", "%s", - ARRAY_ITEM(&find_list, i).list_ctx); - format_defaults(cdata->ft, NULL, s, wm, NULL); + find_data->list_ctx); + format_defaults(cdata->ft, NULL, s, find_data->wl, NULL); window_choose_add(wl->window->active, cdata); + + i++; } window_choose_ready(wl->window->active, 0, cmd_find_window_callback); out: - for (i = 0; i < ARRAY_LENGTH(&find_list); i++) - free(ARRAY_ITEM(&find_list, i).list_ctx); - ARRAY_FREE(&find_list); + TAILQ_FOREACH_SAFE(find_data, &find_list, entry, find_data1) { + free(find_data->list_ctx); + TAILQ_REMOVE(&find_list, find_data, entry); + free(find_data); + } return (CMD_RETURN_NORMAL); } diff --git a/cmd-find.c b/cmd-find.c new file mode 100644 index 00000000..f95c143b --- /dev/null +++ b/cmd-find.c @@ -0,0 +1,1257 @@ +/* $OpenBSD$ */ + +/* + * Copyright (c) 2015 Nicholas Marriott + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include +#include +#include +#include +#include + +#include "tmux.h" + +struct session *cmd_find_try_TMUX(struct client *, struct window *); +int cmd_find_client_better(struct client *, struct client *); +struct client *cmd_find_best_client(struct client **, u_int); +int cmd_find_session_better(struct session *, struct session *, + int); +struct session *cmd_find_best_session(struct session **, u_int, int); +int cmd_find_best_session_with_window(struct cmd_find_state *); +int cmd_find_best_winlink_with_window(struct cmd_find_state *); + +int cmd_find_current_session_with_client(struct cmd_find_state *); +int cmd_find_current_session(struct cmd_find_state *); +struct client *cmd_find_current_client(struct cmd_q *); + +const char *cmd_find_map_table(const char *[][2], const char *); + +int cmd_find_get_session(struct cmd_find_state *, const char *); +int cmd_find_get_window(struct cmd_find_state *, const char *); +int cmd_find_get_window_with_session(struct cmd_find_state *, const char *); +int cmd_find_get_window_with_pane(struct cmd_find_state *); +int cmd_find_get_pane(struct cmd_find_state *, const char *); +int cmd_find_get_pane_with_session(struct cmd_find_state *, const char *); +int cmd_find_get_pane_with_window(struct cmd_find_state *, const char *); + +const char *cmd_find_session_table[][2] = { + { NULL, NULL } +}; +const char *cmd_find_window_table[][2] = { + { "{start}", "^" }, + { "{last}", "!" }, + { "{end}", "$" }, + { "{next}", "+" }, + { "{previous}", "-" }, + { NULL, NULL } +}; +const char *cmd_find_pane_table[][2] = { + { "{last}", "!" }, + { "{next}", "+" }, + { "{previous}", "-" }, + { "{top}", "top" }, + { "{bottom}", "bottom" }, + { "{left}", "left" }, + { "{right}", "right" }, + { "{top-left}", "top-left" }, + { "{top-right}", "top-right" }, + { "{bottom-left}", "bottom-left" }, + { "{bottom-right}", "bottom-right" }, + { "{up-of}", "{up-of}" }, + { "{down-of}", "{down-of}" }, + { "{left-of}", "{left-of}" }, + { "{right-of}", "{right-of}" }, + { NULL, NULL } +}; + +/* Get session from TMUX if present. */ +struct session * +cmd_find_try_TMUX(struct client *c, struct window *w) +{ + struct environ_entry *envent; + char tmp[256]; + long long pid; + u_int session; + struct session *s; + + envent = environ_find(c->environ, "TMUX"); + if (envent == NULL) + return (NULL); + + if (sscanf(envent->value, "%255[^,],%lld,%d", tmp, &pid, &session) != 3) + return (NULL); + if (pid != getpid()) + return (NULL); + log_debug("client %p TMUX is %s (session @%u)", c, envent->value, + session); + + s = session_find_by_id(session); + if (s == NULL || (w != NULL && !session_has(s, w))) + return (NULL); + return (s); +} + +/* Is this client better? */ +int +cmd_find_client_better(struct client *c, struct client *than) +{ + if (than == NULL) + return (1); + return (timercmp(&c->activity_time, &than->activity_time, >)); +} + +/* Find best client from a list, or all if list is NULL. */ +struct client * +cmd_find_best_client(struct client **clist, u_int csize) +{ + struct client *c_loop, *c; + u_int i; + + c = NULL; + if (clist != NULL) { + for (i = 0; i < csize; i++) { + if (clist[i]->session == NULL) + continue; + if (cmd_find_client_better(clist[i], c)) + c = clist[i]; + } + } else { + TAILQ_FOREACH(c_loop, &clients, entry) { + if (c_loop->session == NULL) + continue; + if (cmd_find_client_better(c_loop, c)) + c = c_loop; + } + } + return (c); +} + +/* Is this session better? */ +int +cmd_find_session_better(struct session *s, struct session *than, int flags) +{ + int attached; + + if (than == NULL) + return (1); + if (flags & CMD_FIND_PREFER_UNATTACHED) { + attached = (~than->flags & SESSION_UNATTACHED); + if (attached && (s->flags & SESSION_UNATTACHED)) + return (1); + else if (!attached && (~s->flags & SESSION_UNATTACHED)) + return (0); + } + return (timercmp(&s->activity_time, &than->activity_time, >)); +} + +/* Find best session from a list, or all if list is NULL. */ +struct session * +cmd_find_best_session(struct session **slist, u_int ssize, int flags) +{ + struct session *s_loop, *s; + u_int i; + + s = NULL; + if (slist != NULL) { + for (i = 0; i < ssize; i++) { + if (cmd_find_session_better(slist[i], s, flags)) + s = slist[i]; + } + } else { + RB_FOREACH(s_loop, sessions, &sessions) { + if (cmd_find_session_better(s_loop, s, flags)) + s = s_loop; + } + } + return (s); +} + +/* Find best session and winlink for window. */ +int +cmd_find_best_session_with_window(struct cmd_find_state *fs) +{ + struct session **slist = NULL; + u_int ssize; + struct session *s; + + if (fs->cmdq != NULL && fs->cmdq->client != NULL) { + fs->s = cmd_find_try_TMUX(fs->cmdq->client, fs->w); + if (fs->s != NULL) + return (cmd_find_best_winlink_with_window(fs)); + } + + ssize = 0; + RB_FOREACH(s, sessions, &sessions) { + if (!session_has(s, fs->w)) + continue; + slist = xreallocarray(slist, ssize + 1, sizeof *slist); + slist[ssize++] = s; + } + if (ssize == 0) + goto fail; + fs->s = cmd_find_best_session(slist, ssize, fs->flags); + if (fs->s == NULL) + goto fail; + free(slist); + return (cmd_find_best_winlink_with_window(fs)); + +fail: + free(slist); + return (-1); +} + +/* + * Find the best winlink for a window (the current if it contains the pane, + * otherwise the first). + */ +int +cmd_find_best_winlink_with_window(struct cmd_find_state *fs) +{ + struct winlink *wl, *wl_loop; + + wl = NULL; + if (fs->s->curw->window == fs->w) + wl = fs->s->curw; + else { + RB_FOREACH(wl_loop, winlinks, &fs->s->windows) { + if (wl_loop->window == fs->w) { + wl = wl_loop; + break; + } + } + } + if (wl == NULL) + return (-1); + fs->wl = wl; + fs->idx = fs->wl->idx; + return (0); +} + +/* Find current session when we have an unattached client. */ +int +cmd_find_current_session_with_client(struct cmd_find_state *fs) +{ + struct window_pane *wp; + + /* + * If this is running in a pane, we can use that to limit the list of + * sessions to those containing that pane (we still use the current + * window in the best session). + */ + if (fs->cmdq != NULL && fs->cmdq->client->tty.path != NULL) { + RB_FOREACH(wp, window_pane_tree, &all_window_panes) { + if (strcmp(wp->tty, fs->cmdq->client->tty.path) == 0) + break; + } + } else + wp = NULL; + + /* Not running in a pane. We know nothing. Find the best session. */ + if (wp == NULL) + goto unknown_pane; + + /* Find the best session and winlink containing this pane. */ + fs->w = wp->window; + if (cmd_find_best_session_with_window(fs) != 0) { + if (wp != NULL) { + /* + * The window may have been destroyed but the pane + * still on all_window_panes due to something else + * holding a reference. + */ + goto unknown_pane; + } + return (-1); + } + + /* Use the current window and pane from this session. */ + fs->wl = fs->s->curw; + fs->idx = fs->wl->idx; + fs->w = fs->wl->window; + fs->wp = fs->w->active; + + return (0); + +unknown_pane: + fs->s = NULL; + if (fs->cmdq != NULL) + fs->s = cmd_find_try_TMUX(fs->cmdq->client, NULL); + if (fs->s == NULL) + fs->s = cmd_find_best_session(NULL, 0, fs->flags); + if (fs->s == NULL) + return (-1); + fs->wl = fs->s->curw; + fs->idx = fs->wl->idx; + fs->w = fs->wl->window; + fs->wp = fs->w->active; + + return (0); +} + +/* + * Work out the best current state. If this function succeeds, the state is + * guaranteed to be completely filled in. + */ +int +cmd_find_current_session(struct cmd_find_state *fs) +{ + /* If we know the current client, use it. */ + if (fs->cmdq != NULL && fs->cmdq->client != NULL) { + log_debug("%s: have client %p%s", __func__, fs->cmdq->client, + fs->cmdq->client->session == NULL ? "" : " (with session)"); + if (fs->cmdq->client->session == NULL) + return (cmd_find_current_session_with_client(fs)); + fs->s = fs->cmdq->client->session; + fs->wl = fs->s->curw; + fs->idx = fs->wl->idx; + fs->w = fs->wl->window; + fs->wp = fs->w->active; + return (0); + } + + /* We know nothing, find the best session and client. */ + fs->s = cmd_find_best_session(NULL, 0, fs->flags); + if (fs->s == NULL) + return (-1); + fs->wl = fs->s->curw; + fs->idx = fs->wl->idx; + fs->w = fs->wl->window; + fs->wp = fs->w->active; + + return (0); +} + +/* Work out the best current client. */ +struct client * +cmd_find_current_client(struct cmd_q *cmdq) +{ + struct cmd_find_state current; + struct session *s; + struct client *c, **clist = NULL; + u_int csize; + + /* If the queue client has a session, use it. */ + if (cmdq->client != NULL && cmdq->client->session != NULL) { + log_debug("%s: using cmdq %p client %p", __func__, cmdq, + cmdq->client); + return (cmdq->client); + } + + /* Otherwise find the current session. */ + cmd_find_clear_state(¤t, cmdq, 0); + if (cmd_find_current_session(¤t) != 0) + return (NULL); + + /* If it is attached, find the best of it's clients. */ + s = current.s; + log_debug("%s: current session $%u %s", __func__, s->id, s->name); + if (~s->flags & SESSION_UNATTACHED) { + csize = 0; + TAILQ_FOREACH(c, &clients, entry) { + if (c->session != s) + continue; + clist = xreallocarray(clist, csize + 1, sizeof *clist); + clist[csize++] = c; + } + if (csize != 0) { + c = cmd_find_best_client(clist, csize); + if (c != NULL) { + free(clist); + return (c); + } + } + free(clist); + } + + /* Otherwise pick best of all clients. */ + return (cmd_find_best_client(NULL, 0)); +} + +/* Maps string in table. */ +const char * +cmd_find_map_table(const char *table[][2], const char *s) +{ + u_int i; + + for (i = 0; table[i][0] != NULL; i++) { + if (strcmp(s, table[i][0]) == 0) + return (table[i][1]); + } + return (s); +} + +/* Find session from string. Fills in s. */ +int +cmd_find_get_session(struct cmd_find_state *fs, const char *session) +{ + struct session *s, *s_loop; + struct client *c; + + log_debug("%s: %s", __func__, session); + + /* Check for session ids starting with $. */ + if (*session == '$') { + fs->s = session_find_by_id_str(session); + if (fs->s == NULL) + return (-1); + return (0); + } + + /* Look for exactly this session. */ + fs->s = session_find(session); + if (fs->s != NULL) + return (0); + + /* Look for as a client. */ + c = cmd_find_client(NULL, session, 1); + if (c != NULL && c->session != NULL) { + fs->s = c->session; + return (0); + } + + /* Stop now if exact only. */ + if (fs->flags & CMD_FIND_EXACT_SESSION) + return (-1); + + /* Otherwise look for prefix. */ + s = NULL; + RB_FOREACH(s_loop, sessions, &sessions) { + if (strncmp(session, s_loop->name, strlen(session)) == 0) { + if (s != NULL) + return (-1); + s = s_loop; + } + } + if (s != NULL) { + fs->s = s; + return (0); + } + + /* Then as a pattern. */ + s = NULL; + RB_FOREACH(s_loop, sessions, &sessions) { + if (fnmatch(session, s_loop->name, 0) == 0) { + if (s != NULL) + return (-1); + s = s_loop; + } + } + if (s != NULL) { + fs->s = s; + return (0); + } + + return (-1); +} + +/* Find window from string. Fills in s, wl, w. */ +int +cmd_find_get_window(struct cmd_find_state *fs, const char *window) +{ + log_debug("%s: %s", __func__, window); + + /* Check for window ids starting with @. */ + if (*window == '@') { + fs->w = window_find_by_id_str(window); + if (fs->w == NULL) + return (-1); + return (cmd_find_best_session_with_window(fs)); + } + + /* Not a window id, so use the current session. */ + fs->s = fs->current->s; + + /* We now only need to find the winlink in this session. */ + if (cmd_find_get_window_with_session(fs, window) == 0) + return (0); + + /* Otherwise try as a session itself. */ + if (cmd_find_get_session(fs, window) == 0) { + fs->wl = fs->s->curw; + fs->w = fs->wl->window; + if (~fs->flags & CMD_FIND_WINDOW_INDEX) + fs->idx = fs->wl->idx; + return (0); + } + + return (-1); +} + +/* + * Find window from string, assuming it is in given session. Needs s, fills in + * wl and w. + */ +int +cmd_find_get_window_with_session(struct cmd_find_state *fs, const char *window) +{ + struct winlink *wl; + const char *errstr; + int idx, n, exact; + struct session *s; + + log_debug("%s: %s", __func__, window); + exact = (fs->flags & CMD_FIND_EXACT_WINDOW); + + /* + * Start with the current window as the default. So if only an index is + * found, the window will be the current. + */ + fs->wl = fs->s->curw; + fs->w = fs->wl->window; + + /* Check for window ids starting with @. */ + if (*window == '@') { + fs->w = window_find_by_id_str(window); + if (fs->w == NULL || !session_has(fs->s, fs->w)) + return (-1); + return (cmd_find_best_winlink_with_window(fs)); + } + + /* Try as an offset. */ + if (!exact && (window[0] == '+' || window[0] == '-')) { + if (window[1] != '\0') + n = strtonum(window + 1, 1, INT_MAX, NULL); + else + n = 1; + s = fs->s; + if (fs->flags & CMD_FIND_WINDOW_INDEX) { + if (window[0] == '+') { + if (INT_MAX - s->curw->idx < n) + return (-1); + fs->idx = s->curw->idx + n; + } else { + if (n < s->curw->idx) + return (-1); + fs->idx = s->curw->idx - n; + } + return (0); + } + if (window[0] == '+') + fs->wl = winlink_next_by_number(s->curw, s, n); + else + fs->wl = winlink_previous_by_number(s->curw, s, n); + if (fs->wl != NULL) { + fs->idx = fs->wl->idx; + fs->w = fs->wl->window; + return (0); + } + } + + /* Try special characters. */ + if (!exact) { + if (strcmp(window, "!") == 0) { + fs->wl = TAILQ_FIRST(&fs->s->lastw); + if (fs->wl == NULL) + return (-1); + fs->idx = fs->wl->idx; + fs->w = fs->wl->window; + return (0); + } else if (strcmp(window, "^") == 0) { + fs->wl = RB_MIN(winlinks, &fs->s->windows); + if (fs->wl == NULL) + return (-1); + fs->idx = fs->wl->idx; + fs->w = fs->wl->window; + return (0); + } else if (strcmp(window, "$") == 0) { + fs->wl = RB_MAX(winlinks, &fs->s->windows); + if (fs->wl == NULL) + return (-1); + fs->idx = fs->wl->idx; + fs->w = fs->wl->window; + return (0); + } + } + + /* First see if this is a valid window index in this session. */ + if (window[0] != '+' && window[0] != '-') { + idx = strtonum(window, 0, INT_MAX, &errstr); + if (errstr == NULL) { + if (fs->flags & CMD_FIND_WINDOW_INDEX) { + fs->idx = idx; + return (0); + } + fs->wl = winlink_find_by_index(&fs->s->windows, idx); + if (fs->wl != NULL) { + fs->w = fs->wl->window; + return (0); + } + } + } + + /* Look for exact matches, error if more than one. */ + fs->wl = NULL; + RB_FOREACH(wl, winlinks, &fs->s->windows) { + if (strcmp(window, wl->window->name) == 0) { + if (fs->wl != NULL) + return (-1); + fs->wl = wl; + } + } + if (fs->wl != NULL) { + fs->idx = fs->wl->idx; + fs->w = fs->wl->window; + return (0); + } + + /* Stop now if exact only. */ + if (exact) + return (-1); + + /* Try as the start of a window name, error if multiple. */ + fs->wl = NULL; + RB_FOREACH(wl, winlinks, &fs->s->windows) { + if (strncmp(window, wl->window->name, strlen(window)) == 0) { + if (fs->wl != NULL) + return (-1); + fs->wl = wl; + } + } + if (fs->wl != NULL) { + fs->idx = fs->wl->idx; + fs->w = fs->wl->window; + return (0); + } + + /* Now look for pattern matches, again error if multiple. */ + fs->wl = NULL; + RB_FOREACH(wl, winlinks, &fs->s->windows) { + if (fnmatch(window, wl->window->name, 0) == 0) { + if (fs->wl != NULL) + return (-1); + fs->wl = wl; + } + } + if (fs->wl != NULL) { + fs->idx = fs->wl->idx; + fs->w = fs->wl->window; + return (0); + } + + return (-1); +} + +/* Find window from given pane. Needs wp, fills in s and wl and w. */ +int +cmd_find_get_window_with_pane(struct cmd_find_state *fs) +{ + log_debug("%s", __func__); + + fs->w = fs->wp->window; + return (cmd_find_best_session_with_window(fs)); +} + +/* Find pane from string. Fills in s, wl, w, wp. */ +int +cmd_find_get_pane(struct cmd_find_state *fs, const char *pane) +{ + log_debug("%s: %s", __func__, pane); + + /* Check for pane ids starting with %. */ + if (*pane == '%') { + fs->wp = window_pane_find_by_id_str(pane); + if (fs->wp == NULL) + return (-1); + fs->w = fs->wp->window; + return (cmd_find_best_session_with_window(fs)); + } + + /* Not a pane id, so try the current session and window. */ + fs->s = fs->current->s; + fs->wl = fs->current->wl; + fs->idx = fs->current->idx; + fs->w = fs->current->w; + + /* We now only need to find the pane in this window. */ + if (cmd_find_get_pane_with_window(fs, pane) == 0) + return (0); + + /* Otherwise try as a window itself (this will also try as session). */ + if (cmd_find_get_window(fs, pane) == 0) { + fs->wp = fs->w->active; + return (0); + } + + return (-1); +} + +/* + * Find pane from string, assuming it is in given session. Needs s, fills in wl + * and w and wp. + */ +int +cmd_find_get_pane_with_session(struct cmd_find_state *fs, const char *pane) +{ + log_debug("%s: %s", __func__, pane); + + /* Check for pane ids starting with %. */ + if (*pane == '%') { + fs->wp = window_pane_find_by_id_str(pane); + if (fs->wp == NULL) + return (-1); + fs->w = fs->wp->window; + return (cmd_find_best_winlink_with_window(fs)); + } + + /* Otherwise use the current window. */ + fs->wl = fs->s->curw; + fs->idx = fs->wl->idx; + fs->w = fs->wl->window; + + /* Now we just need to look up the pane. */ + return (cmd_find_get_pane_with_window(fs, pane)); +} + +/* + * Find pane from string, assuming it is in the given window. Needs w, fills in + * wp. + */ +int +cmd_find_get_pane_with_window(struct cmd_find_state *fs, const char *pane) +{ + const char *errstr; + int idx; + struct window_pane *wp; + u_int n; + + log_debug("%s: %s", __func__, pane); + + /* Check for pane ids starting with %. */ + if (*pane == '%') { + fs->wp = window_pane_find_by_id_str(pane); + if (fs->wp == NULL || fs->wp->window != fs->w) + return (-1); + return (0); + } + + /* Try special characters. */ + if (strcmp(pane, "!") == 0) { + if (fs->w->last == NULL) + return (-1); + fs->wp = fs->w->last; + return (0); + } else if (strcmp(pane, "{up-of}") == 0) { + fs->wp = window_pane_find_up(fs->w->active); + if (fs->wp == NULL) + return (-1); + return (0); + } else if (strcmp(pane, "{down-of}") == 0) { + fs->wp = window_pane_find_down(fs->w->active); + if (fs->wp == NULL) + return (-1); + return (0); + } else if (strcmp(pane, "{left-of}") == 0) { + fs->wp = window_pane_find_left(fs->w->active); + if (fs->wp == NULL) + return (-1); + return (0); + } else if (strcmp(pane, "{right-of}") == 0) { + fs->wp = window_pane_find_right(fs->w->active); + if (fs->wp == NULL) + return (-1); + return (0); + } + + /* Try as an offset. */ + if (pane[0] == '+' || pane[0] == '-') { + if (pane[1] != '\0') + n = strtonum(pane + 1, 1, INT_MAX, NULL); + else + n = 1; + wp = fs->w->active; + if (pane[0] == '+') + fs->wp = window_pane_next_by_number(fs->w, wp, n); + else + fs->wp = window_pane_previous_by_number(fs->w, wp, n); + if (fs->wp != NULL) + return (0); + } + + /* Get pane by index. */ + idx = strtonum(pane, 0, INT_MAX, &errstr); + if (errstr == NULL) { + fs->wp = window_pane_at_index(fs->w, idx); + if (fs->wp != NULL) + return (0); + } + + /* Try as a description. */ + fs->wp = window_find_string(fs->w, pane); + if (fs->wp != NULL) + return (0); + + return (-1); +} + +/* Clear state. */ +void +cmd_find_clear_state(struct cmd_find_state *fs, struct cmd_q *cmdq, int flags) +{ + memset(fs, 0, sizeof *fs); + + fs->cmdq = cmdq; + fs->flags = flags; + + fs->idx = -1; +} + +/* Check if a state if valid. */ +int +cmd_find_valid_state(struct cmd_find_state *fs) +{ + struct winlink *wl; + + if (fs->s == NULL || fs->wl == NULL || fs->w == NULL || fs->wp == NULL) + return (0); + + if (!session_alive(fs->s)) + return (0); + + RB_FOREACH(wl, winlinks, &fs->s->windows) { + if (wl->window == fs->w && wl == fs->wl) + break; + } + if (wl == NULL) + return (0); + + if (fs->w != fs->wl->window) + return (0); + + if (!window_has_pane(fs->w, fs->wp)) + return (0); + return (window_pane_visible(fs->wp)); +} + +/* Copy a state. */ +void +cmd_find_copy_state(struct cmd_find_state *dst, struct cmd_find_state *src) +{ + dst->s = src->s; + dst->wl = src->wl; + dst->idx = src->idx; + dst->w = src->w; + dst->wp = src->wp; +} + +/* Log the result. */ +void +cmd_find_log_state(const char *prefix, struct cmd_find_state *fs) +{ + if (fs->s != NULL) + log_debug("%s: s=$%u", prefix, fs->s->id); + else + log_debug("%s: s=none", prefix); + if (fs->wl != NULL) { + log_debug("%s: wl=%u %d w=@%u %s", prefix, fs->wl->idx, + fs->wl->window == fs->w, fs->w->id, fs->w->name); + } else + log_debug("%s: wl=none", prefix); + if (fs->wp != NULL) + log_debug("%s: wp=%%%u", prefix, fs->wp->id); + else + log_debug("%s: wp=none", prefix); + if (fs->idx != -1) + log_debug("%s: idx=%d", prefix, fs->idx); + else + log_debug("%s: idx=none", prefix); +} + +/* Find state from a session. */ +int +cmd_find_from_session(struct cmd_find_state *fs, struct session *s) +{ + cmd_find_clear_state(fs, NULL, 0); + + fs->s = s; + fs->wl = fs->s->curw; + fs->w = fs->wl->window; + fs->wp = fs->w->active; + + cmd_find_log_state(__func__, fs); + return (0); +} + +/* Find state from a winlink. */ +int +cmd_find_from_winlink(struct cmd_find_state *fs, struct session *s, + struct winlink *wl) +{ + cmd_find_clear_state(fs, NULL, 0); + + fs->s = s; + fs->wl = wl; + fs->w = wl->window; + fs->wp = wl->window->active; + + cmd_find_log_state(__func__, fs); + return (0); +} + +/* Find state from a window. */ +int +cmd_find_from_window(struct cmd_find_state *fs, struct window *w) +{ + cmd_find_clear_state(fs, NULL, 0); + + fs->w = w; + if (cmd_find_best_session_with_window(fs) != 0) + return (-1); + if (cmd_find_best_winlink_with_window(fs) != 0) + return (-1); + + cmd_find_log_state(__func__, fs); + return (0); +} + +/* Find state from a pane. */ +int +cmd_find_from_pane(struct cmd_find_state *fs, struct window_pane *wp) +{ + if (cmd_find_from_window(fs, wp->window) != 0) + return (-1); + fs->wp = wp; + + cmd_find_log_state(__func__, fs); + return (0); +} + +/* Find current state. */ +int +cmd_find_current(struct cmd_find_state *fs, struct cmd_q *cmdq, int flags) +{ + cmd_find_clear_state(fs, cmdq, flags); + if (cmd_find_current_session(fs) != 0) { + if (~flags & CMD_FIND_QUIET) + cmdq_error(cmdq, "no current session"); + return (-1); + } + return (0); +} + +/* + * Split target into pieces and resolve for the given type. Fills in the given + * state. Returns 0 on success or -1 on error. + */ +int +cmd_find_target(struct cmd_find_state *fs, struct cmd_find_state *current, + struct cmd_q *cmdq, const char *target, enum cmd_find_type type, int flags) +{ + struct mouse_event *m; + char *colon, *period, *copy = NULL; + const char *session, *window, *pane; + + /* Log the arguments. */ + if (target == NULL) + log_debug("%s: target none, type %d", __func__, type); + else + log_debug("%s: target %s, type %d", __func__, target, type); + log_debug("%s: cmdq %p, flags %#x", __func__, cmdq, flags); + + /* Clear new state. */ + cmd_find_clear_state(fs, cmdq, flags); + + /* Find current state. */ + if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED)) + fs->current = &marked_pane; + else if (cmd_find_valid_state(&cmdq->current)) + fs->current = &cmdq->current; + else + fs->current = current; + + /* An empty or NULL target is the current. */ + if (target == NULL || *target == '\0') + goto current; + + /* Mouse target is a plain = or {mouse}. */ + if (strcmp(target, "=") == 0 || strcmp(target, "{mouse}") == 0) { + m = &cmdq->item->mouse; + switch (type) { + case CMD_FIND_PANE: + fs->wp = cmd_mouse_pane(m, &fs->s, &fs->wl); + if (fs->wp != NULL) + fs->w = fs->wl->window; + break; + case CMD_FIND_WINDOW: + case CMD_FIND_SESSION: + fs->wl = cmd_mouse_window(m, &fs->s); + if (fs->wl != NULL) { + fs->w = fs->wl->window; + fs->wp = fs->w->active; + } + break; + } + if (fs->wp == NULL) { + if (~flags & CMD_FIND_QUIET) + cmdq_error(cmdq, "no mouse target"); + goto error; + } + goto found; + } + + /* Marked target is a plain ~ or {marked}. */ + if (strcmp(target, "~") == 0 || strcmp(target, "{marked}") == 0) { + if (!server_check_marked()) { + if (~flags & CMD_FIND_QUIET) + cmdq_error(cmdq, "no marked target"); + goto error; + } + cmd_find_copy_state(fs, &marked_pane); + goto found; + } + + /* Find separators if they exist. */ + copy = xstrdup(target); + colon = strchr(copy, ':'); + if (colon != NULL) + *colon++ = '\0'; + if (colon == NULL) + period = strchr(copy, '.'); + else + period = strchr(colon, '.'); + if (period != NULL) + *period++ = '\0'; + + /* Set session, window and pane parts. */ + session = window = pane = NULL; + if (colon != NULL && period != NULL) { + session = copy; + window = colon; + pane = period; + } else if (colon != NULL && period == NULL) { + session = copy; + window = colon; + } else if (colon == NULL && period != NULL) { + window = copy; + pane = period; + } else { + if (*copy == '$') + session = copy; + else if (*copy == '@') + window = copy; + else if (*copy == '%') + pane = copy; + else { + switch (type) { + case CMD_FIND_SESSION: + session = copy; + break; + case CMD_FIND_WINDOW: + window = copy; + break; + case CMD_FIND_PANE: + pane = copy; + break; + } + } + } + + /* Set exact match flags. */ + if (session != NULL && *session == '=') { + session++; + fs->flags |= CMD_FIND_EXACT_SESSION; + } + if (window != NULL && *window == '=') { + window++; + fs->flags |= CMD_FIND_EXACT_WINDOW; + } + + /* Empty is the same as NULL. */ + if (session != NULL && *session == '\0') + session = NULL; + if (window != NULL && *window == '\0') + window = NULL; + if (pane != NULL && *pane == '\0') + pane = NULL; + + /* Map though conversion table. */ + if (session != NULL) + session = cmd_find_map_table(cmd_find_session_table, session); + if (window != NULL) + window = cmd_find_map_table(cmd_find_window_table, window); + if (pane != NULL) + pane = cmd_find_map_table(cmd_find_pane_table, pane); + + log_debug("target %s (flags %#x): session=%s, window=%s, pane=%s", + target, flags, session == NULL ? "none" : session, + window == NULL ? "none" : window, pane == NULL ? "none" : pane); + + /* No pane is allowed if want an index. */ + if (pane != NULL && (flags & CMD_FIND_WINDOW_INDEX)) { + if (~flags & CMD_FIND_QUIET) + cmdq_error(cmdq, "can't specify pane here"); + goto error; + } + + /* If the session isn't NULL, look it up. */ + if (session != NULL) { + /* This will fill in session. */ + if (cmd_find_get_session(fs, session) != 0) + goto no_session; + + /* If window and pane are NULL, use that session's current. */ + if (window == NULL && pane == NULL) { + fs->wl = fs->s->curw; + fs->idx = -1; + fs->w = fs->wl->window; + fs->wp = fs->w->active; + goto found; + } + + /* If window is present but pane not, find window in session. */ + if (window != NULL && pane == NULL) { + /* This will fill in winlink and window. */ + if (cmd_find_get_window_with_session(fs, window) != 0) + goto no_window; + fs->wp = fs->wl->window->active; + goto found; + } + + /* If pane is present but window not, find pane. */ + if (window == NULL && pane != NULL) { + /* This will fill in winlink and window and pane. */ + if (cmd_find_get_pane_with_session(fs, pane) != 0) + goto no_pane; + goto found; + } + + /* + * If window and pane are present, find both in session. This + * will fill in winlink and window. + */ + if (cmd_find_get_window_with_session(fs, window) != 0) + goto no_window; + /* This will fill in pane. */ + if (cmd_find_get_pane_with_window(fs, pane) != 0) + goto no_pane; + goto found; + } + + /* No session. If window and pane, try them. */ + if (window != NULL && pane != NULL) { + /* This will fill in session, winlink and window. */ + if (cmd_find_get_window(fs, window) != 0) + goto no_window; + /* This will fill in pane. */ + if (cmd_find_get_pane_with_window(fs, pane) != 0) + goto no_pane; + goto found; + } + + /* If just window is present, try it. */ + if (window != NULL && pane == NULL) { + /* This will fill in session, winlink and window. */ + if (cmd_find_get_window(fs, window) != 0) + goto no_window; + fs->wp = fs->wl->window->active; + goto found; + } + + /* If just pane is present, try it. */ + if (window == NULL && pane != NULL) { + /* This will fill in session, winlink, window and pane. */ + if (cmd_find_get_pane(fs, pane) != 0) + goto no_pane; + goto found; + } + +current: + /* Use the current session. */ + cmd_find_copy_state(fs, fs->current); + if (flags & CMD_FIND_WINDOW_INDEX) + fs->idx = -1; + goto found; + +error: + fs->current = NULL; + log_debug(" error"); + + free(copy); + return (-1); + +found: + fs->current = NULL; + cmd_find_log_state(__func__, fs); + + free(copy); + return (0); + +no_session: + if (~flags & CMD_FIND_QUIET) + cmdq_error(cmdq, "can't find session %s", session); + goto error; + +no_window: + if (~flags & CMD_FIND_QUIET) + cmdq_error(cmdq, "can't find window %s", window); + goto error; + +no_pane: + if (~flags & CMD_FIND_QUIET) + cmdq_error(cmdq, "can't find pane %s", pane); + goto error; +} + +/* Find the target client or report an error and return NULL. */ +struct client * +cmd_find_client(struct cmd_q *cmdq, const char *target, int quiet) +{ + struct client *c; + char *copy; + size_t size; + const char *path; + + /* A NULL argument means the current client. */ + if (cmdq != NULL && target == NULL) { + c = cmd_find_current_client(cmdq); + if (c == NULL && !quiet) + cmdq_error(cmdq, "no current client"); + log_debug("%s: no target, return %p", __func__, c); + return (c); + } + copy = xstrdup(target); + + /* Trim a single trailing colon if any. */ + size = strlen(copy); + if (size != 0 && copy[size - 1] == ':') + copy[size - 1] = '\0'; + + /* Check path of each client. */ + TAILQ_FOREACH(c, &clients, entry) { + if (c->session == NULL || c->tty.path == NULL) + continue; + path = c->tty.path; + + /* Try for exact match. */ + if (strcmp(copy, path) == 0) + break; + + /* Try without leading /dev. */ + if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0) + continue; + if (strcmp(copy, path + (sizeof _PATH_DEV) - 1) == 0) + break; + } + + /* If no client found, report an error. */ + if (c == NULL && !quiet) + cmdq_error(cmdq, "can't find client %s", copy); + + free(copy); + log_debug("%s: target %s, return %p", __func__, target, c); + return (c); +} diff --git a/cmd-if-shell.c b/cmd-if-shell.c index 8c6620da..3e2a5251 100644 --- a/cmd-if-shell.c +++ b/cmd-if-shell.c @@ -36,19 +36,28 @@ void cmd_if_shell_done(struct cmd_q *); void cmd_if_shell_free(void *); const struct cmd_entry cmd_if_shell_entry = { - "if-shell", "if", - "bFt:", 2, 3, - "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command [command]", - 0, - cmd_if_shell_exec + .name = "if-shell", + .alias = "if", + + .args = { "bFt:", 2, 3 }, + .usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command " + "[command]", + + .tflag = CMD_PANE_CANFAIL, + + .flags = 0, + .exec = cmd_if_shell_exec }; struct cmd_if_shell_data { - char *cmd_if; - char *cmd_else; - struct cmd_q *cmdq; - int bflag; - int started; + char *cmd_if; + char *cmd_else; + + struct cmd_q *cmdq; + struct mouse_event mouse; + + int bflag; + int references; }; enum cmd_retval @@ -58,24 +67,20 @@ cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq) struct cmd_if_shell_data *cdata; char *shellcmd, *cmd, *cause; struct cmd_list *cmdlist; - struct client *c; - struct session *s = NULL; - struct winlink *wl = NULL; - struct window_pane *wp = NULL; + struct session *s = cmdq->state.tflag.s; + struct winlink *wl = cmdq->state.tflag.wl; + struct window_pane *wp = cmdq->state.tflag.wp; struct format_tree *ft; + const char *cwd; - if (args_has(args, 't')) - wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp); - else { - c = cmd_find_client(cmdq, NULL, 1); - if (c != NULL && c->session != NULL) { - s = c->session; - wl = s->curw; - wp = wl->window->active; - } - } + if (cmdq->client != NULL && cmdq->client->session == NULL) + cwd = cmdq->client->cwd; + else if (s != NULL) + cwd = s->cwd; + else + cwd = NULL; - ft = format_create(); + ft = format_create(cmdq, 0); format_defaults(ft, NULL, s, wl, wp); shellcmd = format_expand(ft, args->argv[0]); format_free(ft); @@ -86,6 +91,7 @@ cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq) cmd = args->argv[1]; else if (args->argc == 3) cmd = args->argv[2]; + free(shellcmd); if (cmd == NULL) return (CMD_RETURN_NORMAL); if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) { @@ -95,24 +101,28 @@ cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq) } return (CMD_RETURN_ERROR); } - cmdq_run(cmdq, cmdlist); + cmdq_run(cmdq, cmdlist, &cmdq->item->mouse); cmd_list_free(cmdlist); return (CMD_RETURN_NORMAL); } cdata = xmalloc(sizeof *cdata); + cdata->cmd_if = xstrdup(args->argv[1]); if (args->argc == 3) cdata->cmd_else = xstrdup(args->argv[2]); else cdata->cmd_else = NULL; + cdata->bflag = args_has(args, 'b'); - cdata->started = 0; cdata->cmdq = cmdq; + memcpy(&cdata->mouse, &cmdq->item->mouse, sizeof cdata->mouse); cmdq->references++; - job_run(shellcmd, s, cmd_if_shell_callback, cmd_if_shell_free, cdata); + cdata->references = 1; + job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free, + cdata); free(shellcmd); if (cdata->bflag) @@ -128,7 +138,7 @@ cmd_if_shell_callback(struct job *job) struct cmd_list *cmdlist; char *cause, *cmd; - if (cmdq->dead) + if (cmdq->flags & CMD_Q_DEAD) return; if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0) @@ -146,13 +156,12 @@ cmd_if_shell_callback(struct job *job) return; } - cdata->started = 1; - cmdq1 = cmdq_new(cmdq->client); cmdq1->emptyfn = cmd_if_shell_done; cmdq1->data = cdata; - cmdq_run(cmdq1, cmdlist); + cdata->references++; + cmdq_run(cmdq1, cmdlist, &cdata->mouse); cmd_list_free(cmdlist); } @@ -164,12 +173,14 @@ cmd_if_shell_done(struct cmd_q *cmdq1) if (cmdq1->client_exit >= 0) cmdq->client_exit = cmdq1->client_exit; + cmdq_free(cmdq1); + + if (--cdata->references != 0) + return; if (!cmdq_free(cmdq) && !cdata->bflag) cmdq_continue(cmdq); - cmdq_free(cmdq1); - free(cdata->cmd_else); free(cdata->cmd_if); free(cdata); @@ -181,7 +192,7 @@ cmd_if_shell_free(void *data) struct cmd_if_shell_data *cdata = data; struct cmd_q *cmdq = cdata->cmdq; - if (cdata->started) + if (--cdata->references != 0) return; if (!cmdq_free(cmdq) && !cdata->bflag) diff --git a/cmd-join-pane.c b/cmd-join-pane.c index 5603541a..ea2ffbe3 100644 --- a/cmd-join-pane.c +++ b/cmd-join-pane.c @@ -2,7 +2,7 @@ /* * Copyright (c) 2011 George Nachman - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -33,19 +33,31 @@ enum cmd_retval cmd_join_pane_exec(struct cmd *, struct cmd_q *); enum cmd_retval join_pane(struct cmd *, struct cmd_q *, int); const struct cmd_entry cmd_join_pane_entry = { - "join-pane", "joinp", - "bdhvp:l:s:t:", 0, 0, - "[-bdhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane]", - 0, - cmd_join_pane_exec + .name = "join-pane", + .alias = "joinp", + + .args = { "bdhvp:l:s:t:", 0, 0 }, + .usage = "[-bdhv] [-p percentage|-l size] " CMD_SRCDST_PANE_USAGE, + + .sflag = CMD_PANE_MARKED, + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_join_pane_exec }; const struct cmd_entry cmd_move_pane_entry = { - "move-pane", "movep", - "bdhvp:l:s:t:", 0, 0, - "[-bdhv] [-p percentage|-l size] [-s src-pane] [-t dst-pane]", - 0, - cmd_join_pane_exec + .name = "move-pane", + .alias = "movep", + + .args = { "bdhvp:l:s:t:", 0, 0 }, + .usage = "[-bdhv] [-p percentage|-l size] " CMD_SRCDST_PANE_USAGE, + + .sflag = CMD_PANE, + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_join_pane_exec }; enum cmd_retval @@ -57,6 +69,10 @@ cmd_join_pane_exec(struct cmd *self, struct cmd_q *cmdq) enum cmd_retval join_pane(struct cmd *self, struct cmd_q *cmdq, int not_same_window) { +#ifdef TMATE + cmdq_error(cmdq, "join pane is not supported with tmate"); + return (CMD_RETURN_ERROR); +#else struct args *args = self->args; struct session *dst_s; struct winlink *src_wl, *dst_wl; @@ -67,16 +83,15 @@ join_pane(struct cmd *self, struct cmd_q *cmdq, int not_same_window) enum layout_type type; struct layout_cell *lc; - dst_wl = cmd_find_pane(cmdq, args_get(args, 't'), &dst_s, &dst_wp); - if (dst_wl == NULL) - return (CMD_RETURN_ERROR); + dst_s = cmdq->state.tflag.s; + dst_wl = cmdq->state.tflag.wl; + dst_wp = cmdq->state.tflag.wp; dst_w = dst_wl->window; dst_idx = dst_wl->idx; server_unzoom_window(dst_w); - src_wl = cmd_find_pane(cmdq, args_get(args, 's'), NULL, &src_wp); - if (src_wl == NULL) - return (CMD_RETURN_ERROR); + src_wl = cmdq->state.sflag.wl; + src_wp = cmdq->state.sflag.wp; src_w = src_wl->window; server_unzoom_window(src_w); @@ -147,4 +162,5 @@ join_pane(struct cmd *self, struct cmd_q *cmdq, int not_same_window) notify_window_layout_changed(dst_w); return (CMD_RETURN_NORMAL); +#endif } diff --git a/cmd-kill-pane.c b/cmd-kill-pane.c index f4735fd2..ebb2b8d6 100644 --- a/cmd-kill-pane.c +++ b/cmd-kill-pane.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -29,22 +29,24 @@ enum cmd_retval cmd_kill_pane_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_kill_pane_entry = { - "kill-pane", "killp", - "at:", 0, 0, - "[-a] " CMD_TARGET_PANE_USAGE, - 0, - cmd_kill_pane_exec + .name = "kill-pane", + .alias = "killp", + + .args = { "at:", 0, 0 }, + .usage = "[-a] " CMD_TARGET_PANE_USAGE, + + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_kill_pane_exec }; enum cmd_retval cmd_kill_pane_exec(struct cmd *self, struct cmd_q *cmdq) { - struct args *args = self->args; - struct winlink *wl; - struct window_pane *loopwp, *tmpwp, *wp; + struct winlink *wl = cmdq->state.tflag.wl; + struct window_pane *loopwp, *tmpwp, *wp = cmdq->state.tflag.wp; - if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp)) == NULL) - return (CMD_RETURN_ERROR); server_unzoom_window(wl->window); if (window_count_panes(wl->window) == 1) { diff --git a/cmd-kill-server.c b/cmd-kill-server.c index 07d94302..d1940c33 100644 --- a/cmd-kill-server.c +++ b/cmd-kill-server.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -30,23 +30,29 @@ enum cmd_retval cmd_kill_server_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_kill_server_entry = { - "kill-server", NULL, - "", 0, 0, - "", - 0, - cmd_kill_server_exec + .name = "kill-server", + .alias = NULL, + + .args = { "", 0, 0 }, + .usage = "", + + .flags = 0, + .exec = cmd_kill_server_exec }; const struct cmd_entry cmd_start_server_entry = { - "start-server", "start", - "", 0, 0, - "", - CMD_STARTSERVER, - cmd_kill_server_exec + .name = "start-server", + .alias = "start", + + .args = { "", 0, 0 }, + .usage = "", + + .flags = CMD_STARTSERVER, + .exec = cmd_kill_server_exec }; enum cmd_retval -cmd_kill_server_exec(struct cmd *self, unused struct cmd_q *cmdq) +cmd_kill_server_exec(struct cmd *self, __unused struct cmd_q *cmdq) { if (self->entry == &cmd_kill_server_entry) kill(getpid(), SIGTERM); diff --git a/cmd-kill-session.c b/cmd-kill-session.c index d7e2a219..c77e45bb 100644 --- a/cmd-kill-session.c +++ b/cmd-kill-session.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -30,27 +30,38 @@ enum cmd_retval cmd_kill_session_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_kill_session_entry = { - "kill-session", NULL, - "at:", 0, 0, - "[-a] " CMD_TARGET_SESSION_USAGE, - 0, - cmd_kill_session_exec + .name = "kill-session", + .alias = NULL, + + .args = { "aCt:", 0, 0 }, + .usage = "[-aC] " CMD_TARGET_SESSION_USAGE, + + .tflag = CMD_SESSION, + + .flags = 0, + .exec = cmd_kill_session_exec }; enum cmd_retval cmd_kill_session_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct session *s, *s2, *s3; + struct session *s, *sloop, *stmp; + struct winlink *wl; - if ((s = cmd_find_session(cmdq, args_get(args, 't'), 0)) == NULL) - return (CMD_RETURN_ERROR); + s = cmdq->state.tflag.s; - if (args_has(args, 'a')) { - RB_FOREACH_SAFE(s2, sessions, &sessions, s3) { - if (s != s2) { - server_destroy_session(s2); - session_destroy(s2); + if (args_has(args, 'C')) { + RB_FOREACH(wl, winlinks, &s->windows) { + wl->window->flags &= ~WINDOW_ALERTFLAGS; + wl->flags &= ~WINLINK_ALERTFLAGS; + } + server_redraw_session(s); + } else if (args_has(args, 'a')) { + RB_FOREACH_SAFE(sloop, sessions, &sessions, stmp) { + if (sloop != s) { + server_destroy_session(sloop); + session_destroy(sloop); } } } else { diff --git a/cmd-kill-window.c b/cmd-kill-window.c index d402acce..9d388ce5 100644 --- a/cmd-kill-window.c +++ b/cmd-kill-window.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -27,42 +27,41 @@ enum cmd_retval cmd_kill_window_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_kill_window_entry = { - "kill-window", "killw", - "at:", 0, 0, - "[-a] " CMD_TARGET_WINDOW_USAGE, - 0, - cmd_kill_window_exec + .name = "kill-window", + .alias = "killw", + + .args = { "at:", 0, 0 }, + .usage = "[-a] " CMD_TARGET_WINDOW_USAGE, + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_kill_window_exec }; const struct cmd_entry cmd_unlink_window_entry = { - "unlink-window", "unlinkw", - "kt:", 0, 0, - "[-k] " CMD_TARGET_WINDOW_USAGE, - 0, - cmd_kill_window_exec + .name = "unlink-window", + .alias = "unlinkw", + + .args = { "kt:", 0, 0 }, + .usage = "[-k] " CMD_TARGET_WINDOW_USAGE, + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_kill_window_exec }; enum cmd_retval cmd_kill_window_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct winlink *wl, *wl2, *wl3; - struct window *w; - struct session *s; - struct session_group *sg; - u_int references; - - if ((wl = cmd_find_window(cmdq, args_get(args, 't'), &s)) == NULL) - return (CMD_RETURN_ERROR); - w = wl->window; + struct winlink *wl = cmdq->state.tflag.wl, *wl2, *wl3; + struct window *w = wl->window; + struct session *s = cmdq->state.tflag.s; if (self->entry == &cmd_unlink_window_entry) { - sg = session_group_find(s); - if (sg != NULL) - references = session_group_count(sg); - else - references = 1; - if (!args_has(self->args, 'k') && w->references == references) { + if (!args_has(self->args, 'k') && !session_is_linked(s, w)) { cmdq_error(cmdq, "window only linked to one session"); return (CMD_RETURN_ERROR); } diff --git a/cmd-list-buffers.c b/cmd-list-buffers.c index 37571b80..238b2776 100644 --- a/cmd-list-buffers.c +++ b/cmd-list-buffers.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -33,15 +33,18 @@ enum cmd_retval cmd_list_buffers_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_list_buffers_entry = { - "list-buffers", "lsb", - "F:", 0, 0, - "[-F format]", - 0, - cmd_list_buffers_exec + .name = "list-buffers", + .alias = "lsb", + + .args = { "F:", 0, 0 }, + .usage = "[-F format]", + + .flags = 0, + .exec = cmd_list_buffers_exec }; enum cmd_retval -cmd_list_buffers_exec(unused struct cmd *self, struct cmd_q *cmdq) +cmd_list_buffers_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct paste_buffer *pb; @@ -54,8 +57,8 @@ cmd_list_buffers_exec(unused struct cmd *self, struct cmd_q *cmdq) pb = NULL; while ((pb = paste_walk(pb)) != NULL) { - ft = format_create(); - format_defaults_paste_buffer(ft, pb, 0); + ft = format_create(cmdq, 0); + format_defaults_paste_buffer(ft, pb); line = format_expand(ft, template); cmdq_print(cmdq, "%s", line); diff --git a/cmd-list-clients.c b/cmd-list-clients.c index 893a6d05..f318ac18 100644 --- a/cmd-list-clients.c +++ b/cmd-list-clients.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -36,11 +36,16 @@ enum cmd_retval cmd_list_clients_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_list_clients_entry = { - "list-clients", "lsc", - "F:t:", 0, 0, - "[-F format] " CMD_TARGET_SESSION_USAGE, - CMD_READONLY, - cmd_list_clients_exec + .name = "list-clients", + .alias = "lsc", + + .args = { "F:t:", 0, 0 }, + .usage = "[-F format] " CMD_TARGET_SESSION_USAGE, + + .tflag = CMD_SESSION, + + .flags = CMD_READONLY, + .exec = cmd_list_clients_exec }; enum cmd_retval @@ -51,29 +56,24 @@ cmd_list_clients_exec(struct cmd *self, struct cmd_q *cmdq) struct session *s; struct format_tree *ft; const char *template; - u_int i; + u_int idx; char *line; - if (args_has(args, 't')) { - s = cmd_find_session(cmdq, args_get(args, 't'), 0); - if (s == NULL) - return (CMD_RETURN_ERROR); - } else + if (args_has(args, 't')) + s = cmdq->state.tflag.s; + else s = NULL; if ((template = args_get(args, 'F')) == NULL) template = LIST_CLIENTS_TEMPLATE; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session == NULL) + idx = 0; + TAILQ_FOREACH(c, &clients, entry) { + if (c->session == NULL || (s != NULL && s != c->session)) continue; - if (s != NULL && s != c->session) - continue; - - ft = format_create(); - format_add(ft, "line", "%u", i); + ft = format_create(cmdq, 0); + format_add(ft, "line", "%u", idx); format_defaults(ft, c, NULL, NULL, NULL); line = format_expand(ft, template); @@ -81,6 +81,8 @@ cmd_list_clients_exec(struct cmd *self, struct cmd_q *cmdq) free(line); format_free(ft); + + idx++; } return (CMD_RETURN_NORMAL); diff --git a/cmd-list-keys.c b/cmd-list-keys.c index 0733ee28..37f53787 100644 --- a/cmd-list-keys.c +++ b/cmd-list-keys.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -18,6 +18,7 @@ #include +#include #include #include "tmux.h" @@ -29,80 +30,112 @@ enum cmd_retval cmd_list_keys_exec(struct cmd *, struct cmd_q *); enum cmd_retval cmd_list_keys_table(struct cmd *, struct cmd_q *); -enum cmd_retval cmd_list_keys_commands(struct cmd *, struct cmd_q *); +enum cmd_retval cmd_list_keys_commands(struct cmd_q *); const struct cmd_entry cmd_list_keys_entry = { - "list-keys", "lsk", - "t:", 0, 0, - "[-t key-table]", - 0, - cmd_list_keys_exec + .name = "list-keys", + .alias = "lsk", + + .args = { "t:T:", 0, 0 }, + .usage = "[-t mode-table] [-T key-table]", + + .flags = CMD_STARTSERVER, + .exec = cmd_list_keys_exec }; const struct cmd_entry cmd_list_commands_entry = { - "list-commands", "lscm", - "", 0, 0, - "", - 0, - cmd_list_keys_exec + .name = "list-commands", + .alias = "lscm", + + .args = { "", 0, 0 }, + .usage = "", + + .flags = CMD_STARTSERVER, + .exec = cmd_list_keys_exec }; enum cmd_retval cmd_list_keys_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; + struct key_table *table; struct key_binding *bd; - const char *key; - char tmp[BUFSIZ], flags[8]; - size_t used; - int width, keywidth; + const char *key, *tablename, *r; + char *cp, tmp[BUFSIZ]; + int repeat, width, tablewidth, keywidth; if (self->entry == &cmd_list_commands_entry) - return (cmd_list_keys_commands(self, cmdq)); + return (cmd_list_keys_commands(cmdq)); + +#ifdef TMATE + /* XXX TODO Really nasty hack, we really need our own client instance... */ + struct client fake_client; + if (!cmdq->client) { + cmdq->client = &fake_client; + cmdq->client->flags = 0; + cmdq->client->session = RB_MIN(sessions, &sessions); + } +#endif if (args_has(args, 't')) return (cmd_list_keys_table(self, cmdq)); - width = 0; - - RB_FOREACH(bd, key_bindings, &key_bindings) { - key = key_string_lookup_key(bd->key & ~KEYC_PREFIX); - if (key == NULL) - continue; - - keywidth = strlen(key); - if (!(bd->key & KEYC_PREFIX)) { - if (bd->can_repeat) - keywidth += 4; - else - keywidth += 3; - } else if (bd->can_repeat) - keywidth += 3; - if (keywidth > width) - width = keywidth; + tablename = args_get(args, 'T'); + if (tablename != NULL && key_bindings_get_table(tablename, 0) == NULL) { + cmdq_error(cmdq, "table %s doesn't exist", tablename); + return (CMD_RETURN_ERROR); } - RB_FOREACH(bd, key_bindings, &key_bindings) { - key = key_string_lookup_key(bd->key & ~KEYC_PREFIX); - if (key == NULL) + repeat = 0; + tablewidth = keywidth = 0; + RB_FOREACH(table, key_tables, &key_tables) { + if (tablename != NULL && strcmp(table->name, tablename) != 0) continue; + RB_FOREACH(bd, key_bindings, &table->key_bindings) { + key = key_string_lookup_key(bd->key); - *flags = '\0'; - if (!(bd->key & KEYC_PREFIX)) { if (bd->can_repeat) - xsnprintf(flags, sizeof flags, "-rn "); - else - xsnprintf(flags, sizeof flags, "-n "); - } else if (bd->can_repeat) - xsnprintf(flags, sizeof flags, "-r "); + repeat = 1; - used = xsnprintf(tmp, sizeof tmp, "%s%*s ", - flags, (int) (width - strlen(flags)), key); - if (used >= sizeof tmp) + width = utf8_cstrwidth(table->name); + if (width > tablewidth) + tablewidth = width; + width = utf8_cstrwidth(key); + if (width > keywidth) + keywidth = width; + } + } + + RB_FOREACH(table, key_tables, &key_tables) { + if (tablename != NULL && strcmp(table->name, tablename) != 0) continue; + RB_FOREACH(bd, key_bindings, &table->key_bindings) { + key = key_string_lookup_key(bd->key); - cmd_list_print(bd->cmdlist, tmp + used, (sizeof tmp) - used); - cmdq_print(cmdq, "bind-key %s", tmp); + if (!repeat) + r = ""; + else if (bd->can_repeat) + r = "-r "; + else + r = " "; + xsnprintf(tmp, sizeof tmp, "%s-T ", r); + + cp = utf8_padcstr(table->name, tablewidth); + strlcat(tmp, cp, sizeof tmp); + strlcat(tmp, " ", sizeof tmp); + free(cp); + + cp = utf8_padcstr(key, keywidth); + strlcat(tmp, cp, sizeof tmp); + strlcat(tmp, " ", sizeof tmp); + free(cp); + + cp = cmd_list_print(bd->cmdlist); + strlcat(tmp, cp, sizeof tmp); + free(cp); + + cmdq_print(cmdq, "bind-key %s", tmp); + } } return (CMD_RETURN_NORMAL); @@ -128,8 +161,6 @@ cmd_list_keys_table(struct cmd *self, struct cmd_q *cmdq) any_mode = 0; RB_FOREACH(mbind, mode_key_tree, mtab->tree) { key = key_string_lookup_key(mbind->key); - if (key == NULL) - continue; if (mbind->mode != 0) any_mode = 1; @@ -141,8 +172,6 @@ cmd_list_keys_table(struct cmd *self, struct cmd_q *cmdq) RB_FOREACH(mbind, mode_key_tree, mtab->tree) { key = key_string_lookup_key(mbind->key); - if (key == NULL) - continue; mode = ""; if (mbind->mode != 0) @@ -162,7 +191,7 @@ cmd_list_keys_table(struct cmd *self, struct cmd_q *cmdq) } enum cmd_retval -cmd_list_keys_commands(unused struct cmd *self, struct cmd_q *cmdq) +cmd_list_keys_commands(struct cmd_q *cmdq) { const struct cmd_entry **entryp; const struct cmd_entry *entry; diff --git a/cmd-list-panes.c b/cmd-list-panes.c index bd34344f..76e06a71 100644 --- a/cmd-list-panes.c +++ b/cmd-list-panes.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -19,7 +19,6 @@ #include #include -#include #include "tmux.h" @@ -30,39 +29,37 @@ enum cmd_retval cmd_list_panes_exec(struct cmd *, struct cmd_q *); void cmd_list_panes_server(struct cmd *, struct cmd_q *); -void cmd_list_panes_session( - struct cmd *, struct session *, struct cmd_q *, int); -void cmd_list_panes_window(struct cmd *, - struct session *, struct winlink *, struct cmd_q *, int); +void cmd_list_panes_session(struct cmd *, struct session *, struct cmd_q *, + int); +void cmd_list_panes_window(struct cmd *, struct session *, struct winlink *, + struct cmd_q *, int); const struct cmd_entry cmd_list_panes_entry = { - "list-panes", "lsp", - "asF:t:", 0, 0, - "[-as] [-F format] " CMD_TARGET_WINDOW_USAGE, - 0, - cmd_list_panes_exec + .name = "list-panes", + .alias = "lsp", + + .args = { "asF:t:", 0, 0 }, + .usage = "[-as] [-F format] " CMD_TARGET_WINDOW_USAGE, + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_list_panes_exec }; enum cmd_retval cmd_list_panes_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct session *s; - struct winlink *wl; + struct session *s = cmdq->state.tflag.s; + struct winlink *wl = cmdq->state.tflag.wl; if (args_has(args, 'a')) cmd_list_panes_server(self, cmdq); - else if (args_has(args, 's')) { - s = cmd_find_session(cmdq, args_get(args, 't'), 0); - if (s == NULL) - return (CMD_RETURN_ERROR); + else if (args_has(args, 's')) cmd_list_panes_session(self, s, cmdq, 1); - } else { - wl = cmd_find_window(cmdq, args_get(args, 't'), &s); - if (wl == NULL) - return (CMD_RETURN_ERROR); + else cmd_list_panes_window(self, s, wl, cmdq, 0); - } return (CMD_RETURN_NORMAL); } @@ -77,8 +74,8 @@ cmd_list_panes_server(struct cmd *self, struct cmd_q *cmdq) } void -cmd_list_panes_session( - struct cmd *self, struct session *s, struct cmd_q *cmdq, int type) +cmd_list_panes_session(struct cmd *self, struct session *s, struct cmd_q *cmdq, + int type) { struct winlink *wl; @@ -87,8 +84,8 @@ cmd_list_panes_session( } void -cmd_list_panes_window(struct cmd *self, - struct session *s, struct winlink *wl, struct cmd_q *cmdq, int type) +cmd_list_panes_window(struct cmd *self, struct session *s, struct winlink *wl, + struct cmd_q *cmdq, int type) { struct args *args = self->args; struct window_pane *wp; @@ -115,9 +112,9 @@ cmd_list_panes_window(struct cmd *self, "#{?pane_active, (active),}#{?pane_dead, (dead),}"; break; case 2: - template = "#{session_name}:#{window_index}.#{pane_index}: " - "[#{pane_width}x#{pane_height}] [history " - "#{history_size}/#{history_limit}, " + template = "#{session_name}:#{window_index}." + "#{pane_index}: [#{pane_width}x#{pane_height}] " + "[history #{history_size}/#{history_limit}, " "#{history_bytes} bytes] #{pane_id}" "#{?pane_active, (active),}#{?pane_dead, (dead),}"; break; @@ -126,7 +123,7 @@ cmd_list_panes_window(struct cmd *self, n = 0; TAILQ_FOREACH(wp, &wl->window->panes, entry) { - ft = format_create(); + ft = format_create(cmdq, 0); format_add(ft, "line", "%u", n); format_defaults(ft, NULL, s, wl, wp); diff --git a/cmd-list-sessions.c b/cmd-list-sessions.c index 8ad55d03..27e80dbc 100644 --- a/cmd-list-sessions.c +++ b/cmd-list-sessions.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -30,7 +30,7 @@ #define LIST_SESSIONS_TEMPLATE \ "#{session_name}: #{session_windows} windows " \ - "(created #{session_created_string}) " \ + "(created #{t:session_created}) " \ "[#{session_width}x#{session_height}]" \ "#{?session_grouped, (group ,}" \ "#{session_group}#{?session_grouped,),}" \ @@ -39,11 +39,14 @@ enum cmd_retval cmd_list_sessions_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_list_sessions_entry = { - "list-sessions", "ls", - "F:", 0, 0, - "[-F format]", - 0, - cmd_list_sessions_exec + .name = "list-sessions", + .alias = "ls", + + .args = { "F:", 0, 0 }, + .usage = "[-F format]", + + .flags = 0, + .exec = cmd_list_sessions_exec }; enum cmd_retval @@ -61,7 +64,7 @@ cmd_list_sessions_exec(struct cmd *self, struct cmd_q *cmdq) n = 0; RB_FOREACH(s, sessions, &sessions) { - ft = format_create(); + ft = format_create(cmdq, 0); format_add(ft, "line", "%u", n); format_defaults(ft, NULL, s, NULL, NULL); diff --git a/cmd-list-windows.c b/cmd-list-windows.c index 3f6b2a4c..11a5fddf 100644 --- a/cmd-list-windows.c +++ b/cmd-list-windows.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -46,27 +46,27 @@ void cmd_list_windows_session(struct cmd *, struct session *, struct cmd_q *, int); const struct cmd_entry cmd_list_windows_entry = { - "list-windows", "lsw", - "F:at:", 0, 0, - "[-a] [-F format] " CMD_TARGET_SESSION_USAGE, - 0, - cmd_list_windows_exec + .name = "list-windows", + .alias = "lsw", + + .args = { "F:at:", 0, 0 }, + .usage = "[-a] [-F format] " CMD_TARGET_SESSION_USAGE, + + .tflag = CMD_SESSION, + + .flags = 0, + .exec = cmd_list_windows_exec }; enum cmd_retval cmd_list_windows_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct session *s; if (args_has(args, 'a')) cmd_list_windows_server(self, cmdq); - else { - s = cmd_find_session(cmdq, args_get(args, 't'), 0); - if (s == NULL) - return (CMD_RETURN_ERROR); - cmd_list_windows_session(self, s, cmdq, 0); - } + else + cmd_list_windows_session(self, cmdq->state.tflag.s, cmdq, 0); return (CMD_RETURN_NORMAL); } @@ -81,8 +81,8 @@ cmd_list_windows_server(struct cmd *self, struct cmd_q *cmdq) } void -cmd_list_windows_session( - struct cmd *self, struct session *s, struct cmd_q *cmdq, int type) +cmd_list_windows_session(struct cmd *self, struct session *s, + struct cmd_q *cmdq, int type) { struct args *args = self->args; struct winlink *wl; @@ -105,7 +105,7 @@ cmd_list_windows_session( n = 0; RB_FOREACH(wl, winlinks, &s->windows) { - ft = format_create(); + ft = format_create(cmdq, 0); format_add(ft, "line", "%u", n); format_defaults(ft, NULL, s, wl, NULL); diff --git a/cmd-list.c b/cmd-list.c index 0c75ed49..e999c370 100644 --- a/cmd-list.c +++ b/cmd-list.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -99,25 +99,28 @@ cmd_list_free(struct cmd_list *cmdlist) free(cmdlist); } -size_t -cmd_list_print(struct cmd_list *cmdlist, char *buf, size_t len) +char * +cmd_list_print(struct cmd_list *cmdlist) { struct cmd *cmd; - size_t off, used; + char *buf, *this; + size_t len; + + len = 1; + buf = xcalloc(1, len); - off = 0; TAILQ_FOREACH(cmd, &cmdlist->list, qentry) { - if (off >= len) - break; - off += cmd_print(cmd, buf + off, len - off); - if (off >= len) - break; - if (TAILQ_NEXT(cmd, qentry) != NULL) { - used = xsnprintf(buf + off, len - off, " ; "); - if (used > len - off) - used = len - off; - off += used; - } + this = cmd_print(cmd); + + len += strlen(this) + 3; + buf = xrealloc(buf, len); + + strlcat(buf, this, len); + if (TAILQ_NEXT(cmd, qentry) != NULL) + strlcat(buf, " ; ", len); + + free(this); } - return (off); + + return (buf); } diff --git a/cmd-load-buffer.c b/cmd-load-buffer.c index 785a7011..de76b855 100644 --- a/cmd-load-buffer.c +++ b/cmd-load-buffer.c @@ -35,11 +35,14 @@ enum cmd_retval cmd_load_buffer_exec(struct cmd *, struct cmd_q *); void cmd_load_buffer_callback(struct client *, int, void *); const struct cmd_entry cmd_load_buffer_entry = { - "load-buffer", "loadb", - "b:", 1, 1, - CMD_BUFFER_USAGE " path", - 0, - cmd_load_buffer_exec + .name = "load-buffer", + .alias = "loadb", + + .args = { "b:", 1, 1 }, + .usage = CMD_BUFFER_USAGE " path", + + .flags = 0, + .exec = cmd_load_buffer_exec }; enum cmd_retval @@ -49,10 +52,10 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_q *cmdq) struct client *c = cmdq->client; struct session *s; FILE *f; - const char *path, *bufname; - char *pdata, *new_pdata, *cause; + const char *path, *bufname, *cwd; + char *pdata, *new_pdata, *cause, *file, resolved[PATH_MAX]; size_t psize; - int ch, error, cwd, fd; + int ch, error; bufname = NULL; if (args_has(args, 'b')) @@ -70,18 +73,26 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_q *cmdq) return (CMD_RETURN_WAIT); } - if (c != NULL && c->session == NULL) + if (c != NULL && c->session == NULL && c->cwd != NULL) cwd = c->cwd; - else if ((s = cmd_current_session(cmdq, 0)) != NULL) + else if ((s = c->session) != NULL && s->cwd != NULL) cwd = s->cwd; else - cwd = AT_FDCWD; + cwd = "."; - if ((fd = openat(cwd, path, O_RDONLY)) == -1 || - (f = fdopen(fd, "rb")) == NULL) { - if (fd != -1) - close(fd); - cmdq_error(cmdq, "%s: %s", path, strerror(errno)); + if (*path == '/') + file = xstrdup(path); + else + xasprintf(&file, "%s/%s", cwd, path); + if (realpath(file, resolved) == NULL && + strlcpy(resolved, file, sizeof resolved) >= sizeof resolved) { + cmdq_error(cmdq, "%s: %s", file, strerror(ENAMETOOLONG)); + return (CMD_RETURN_ERROR); + } + f = fopen(resolved, "rb"); + free(file); + if (f == NULL) { + cmdq_error(cmdq, "%s: %s", resolved, strerror(errno)); return (CMD_RETURN_ERROR); } @@ -97,7 +108,7 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_q *cmdq) pdata[psize++] = ch; } if (ferror(f)) { - cmdq_error(cmdq, "%s: read error", path); + cmdq_error(cmdq, "%s: read error", resolved); goto error; } if (pdata != NULL) @@ -125,14 +136,14 @@ void cmd_load_buffer_callback(struct client *c, int closed, void *data) { const char *bufname = data; - char *pdata, *cause; + char *pdata, *cause, *saved; size_t psize; if (!closed) return; c->stdin_callback = NULL; - c->references--; + server_client_unref(c); if (c->flags & CLIENT_DEAD) return; @@ -146,8 +157,13 @@ cmd_load_buffer_callback(struct client *c, int closed, void *data) if (paste_set(pdata, psize, bufname, &cause) != 0) { /* No context so can't use server_client_msg_error. */ + if (~c->flags & CLIENT_UTF8) { + saved = cause; + cause = utf8_sanitize(saved); + free(saved); + } evbuffer_add_printf(c->stderr_data, "%s", cause); - server_push_stderr(c); + server_client_push_stderr(c); free(pdata); free(cause); } diff --git a/cmd-lock-server.c b/cmd-lock-server.c index de76475d..01597169 100644 --- a/cmd-lock-server.c +++ b/cmd-lock-server.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -27,49 +27,52 @@ enum cmd_retval cmd_lock_server_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_lock_server_entry = { - "lock-server", "lock", - "", 0, 0, - "", - 0, - cmd_lock_server_exec + .name = "lock-server", + .alias = "lock", + + .args = { "", 0, 0 }, + .usage = "", + + .flags = 0, + .exec = cmd_lock_server_exec }; const struct cmd_entry cmd_lock_session_entry = { - "lock-session", "locks", - "t:", 0, 0, - CMD_TARGET_SESSION_USAGE, - 0, - cmd_lock_server_exec + .name = "lock-session", + .alias = "locks", + + .args = { "t:", 0, 0 }, + .usage = CMD_TARGET_SESSION_USAGE, + + .tflag = CMD_SESSION, + + .flags = 0, + .exec = cmd_lock_server_exec }; const struct cmd_entry cmd_lock_client_entry = { - "lock-client", "lockc", - "t:", 0, 0, - CMD_TARGET_CLIENT_USAGE, - 0, - cmd_lock_server_exec + .name = "lock-client", + .alias = "lockc", + + .args = { "t:", 0, 0 }, + .usage = CMD_TARGET_CLIENT_USAGE, + + .tflag = CMD_CLIENT, + + .flags = 0, + .exec = cmd_lock_server_exec }; enum cmd_retval -cmd_lock_server_exec(struct cmd *self, unused struct cmd_q *cmdq) +cmd_lock_server_exec(struct cmd *self, __unused struct cmd_q *cmdq) { - struct args *args = self->args; - struct client *c; - struct session *s; - if (self->entry == &cmd_lock_server_entry) server_lock(); - else if (self->entry == &cmd_lock_session_entry) { - s = cmd_find_session(cmdq, args_get(args, 't'), 0); - if (s == NULL) - return (CMD_RETURN_ERROR); - server_lock_session(s); - } else { - c = cmd_find_client(cmdq, args_get(args, 't'), 0); - if (c == NULL) - return (CMD_RETURN_ERROR); - server_lock_client(c); - } + else if (self->entry == &cmd_lock_session_entry) + server_lock_session(cmdq->state.tflag.s); + else + server_lock_client(cmdq->state.c); + recalculate_sizes(); return (CMD_RETURN_NORMAL); diff --git a/cmd-move-window.c b/cmd-move-window.c index 3064cd6e..fe023423 100644 --- a/cmd-move-window.c +++ b/cmd-move-window.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -29,48 +29,66 @@ enum cmd_retval cmd_move_window_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_move_window_entry = { - "move-window", "movew", - "dkrs:t:", 0, 0, - "[-dkr] " CMD_SRCDST_WINDOW_USAGE, - 0, - cmd_move_window_exec + .name = "move-window", + .alias = "movew", + + .args = { "adkrs:t:", 0, 0 }, + .usage = "[-dkr] " CMD_SRCDST_WINDOW_USAGE, + + .sflag = CMD_WINDOW, + .tflag = CMD_MOVEW_R, + + .flags = 0, + .exec = cmd_move_window_exec }; const struct cmd_entry cmd_link_window_entry = { - "link-window", "linkw", - "dks:t:", 0, 0, - "[-dk] " CMD_SRCDST_WINDOW_USAGE, - 0, - cmd_move_window_exec + .name = "link-window", + .alias = "linkw", + + .args = { "adks:t:", 0, 0 }, + .usage = "[-dk] " CMD_SRCDST_WINDOW_USAGE, + + .sflag = CMD_WINDOW, + .tflag = CMD_WINDOW_INDEX, + + .flags = 0, + .exec = cmd_move_window_exec }; enum cmd_retval cmd_move_window_exec(struct cmd *self, struct cmd_q *cmdq) { +#ifdef TMATE + cmdq_error(cmdq, "move window is not supported with tmate"); + return (CMD_RETURN_ERROR); +#else struct args *args = self->args; - struct session *src, *dst, *s; - struct winlink *wl; + struct session *src = cmdq->state.sflag.s; + struct session *dst = cmdq->state.tflag.s; + struct winlink *wl = cmdq->state.sflag.wl; char *cause; - int idx, kflag, dflag; + int idx = cmdq->state.tflag.idx, kflag, dflag, sflag; + + kflag = args_has(self->args, 'k'); + dflag = args_has(self->args, 'd'); if (args_has(args, 'r')) { - s = cmd_find_session(cmdq, args_get(args, 't'), 0); - if (s == NULL) - return (CMD_RETURN_ERROR); - - session_renumber_windows(s); + session_renumber_windows(dst); recalculate_sizes(); return (CMD_RETURN_NORMAL); } - if ((wl = cmd_find_window(cmdq, args_get(args, 's'), &src)) == NULL) - return (CMD_RETURN_ERROR); - if ((idx = cmd_find_index(cmdq, args_get(args, 't'), &dst)) == -2) - return (CMD_RETURN_ERROR); - kflag = args_has(self->args, 'k'); dflag = args_has(self->args, 'd'); + sflag = args_has(self->args, 's'); + + if (args_has(self->args, 'a')) { + if ((idx = winlink_shuffle_up(dst, dst->curw)) == -1) + return (CMD_RETURN_ERROR); + } + if (server_link_window(src, wl, dst, idx, kflag, !dflag, &cause) != 0) { cmdq_error(cmdq, "can't link window: %s", cause); @@ -79,7 +97,17 @@ cmd_move_window_exec(struct cmd *self, struct cmd_q *cmdq) } if (self->entry == &cmd_move_window_entry) server_unlink_window(src, wl); + + /* + * Renumber the winlinks in the src session only, the destination + * session already has the correct winlink id to us, either + * automatically or specified by -s. + */ + if (!sflag && options_get_number(src->options, "renumber-windows")) + session_renumber_windows(src); + recalculate_sizes(); return (CMD_RETURN_NORMAL); +#endif } diff --git a/cmd-new-session.c b/cmd-new-session.c index ec292fa8..fb2f7eab 100644 --- a/cmd-new-session.c +++ b/cmd-new-session.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -36,44 +36,56 @@ enum cmd_retval cmd_new_session_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_new_session_entry = { - "new-session", "new", - "Ac:dDF:n:Ps:t:x:y:", 0, -1, - "[-AdDP] [-c start-directory] [-F format] [-n window-name] " - "[-s session-name] " CMD_TARGET_SESSION_USAGE " [-x width] " - "[-y height] [command]", - CMD_STARTSERVER|CMD_CANTNEST, - cmd_new_session_exec + .name = "new-session", + .alias = "new", + + .args = { "Ac:dDEF:n:Ps:t:x:y:", 0, -1 }, + .usage = "[-AdDEP] [-c start-directory] [-F format] [-n window-name] " + "[-s session-name] " CMD_TARGET_SESSION_USAGE " [-x width] " + "[-y height] [command]", + + .tflag = CMD_SESSION_CANFAIL, + + .flags = CMD_STARTSERVER, + .exec = cmd_new_session_exec }; const struct cmd_entry cmd_has_session_entry = { - "has-session", "has", - "t:", 0, 0, - CMD_TARGET_SESSION_USAGE, - 0, - cmd_new_session_exec + .name = "has-session", + .alias = "has", + + .args = { "t:", 0, 0 }, + .usage = CMD_TARGET_SESSION_USAGE, + + .tflag = CMD_SESSION, + + .flags = 0, + .exec = cmd_new_session_exec }; enum cmd_retval cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct client *c = cmdq->client, *c0; - struct session *s, *groupwith; + struct client *c = cmdq->client; + struct session *s, *as; + struct session *groupwith = cmdq->state.tflag.s; struct window *w; - struct environ env; + struct environ *env; struct termios tio, *tiop; const char *newname, *target, *update, *errstr, *template; - const char *path; + const char *path, *cwd, *to_free = NULL; char **argv, *cmd, *cause, *cp; - int detached, already_attached, idx, cwd, fd = -1; - int argc; + int detached, already_attached, idx, argc; u_int sx, sy; struct format_tree *ft; struct environ_entry *envent; if (self->entry == &cmd_has_session_entry) { - if (cmd_find_session(cmdq, args_get(args, 't'), 0) == NULL) - return (CMD_RETURN_ERROR); + /* + * cmd_prepare() will fail if the session cannot be found, + * hence always return success here. + */ return (CMD_RETURN_NORMAL); } @@ -88,21 +100,29 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq) cmdq_error(cmdq, "bad session name: %s", newname); return (CMD_RETURN_ERROR); } - if (session_find(newname) != NULL) { + if ((as = session_find(newname)) != NULL) { if (args_has(args, 'A')) { - return (cmd_attach_session(cmdq, newname, - args_has(args, 'D'), 0, NULL)); + /* + * This cmdq is now destined for + * attach-session. Because attach-session + * will have already been prepared, copy this + * session into its tflag so it can be used. + */ + cmd_find_from_session(&cmdq->state.tflag, as); + return (cmd_attach_session(cmdq, + args_has(args, 'D'), 0, NULL, + args_has(args, 'E'))); } cmdq_error(cmdq, "duplicate session: %s", newname); return (CMD_RETURN_ERROR); } } - target = args_get(args, 't'); - if (target != NULL) { - groupwith = cmd_find_session(cmdq, target, 0); - if (groupwith == NULL) - return (CMD_RETURN_ERROR); + if ((target = args_get(args, 't')) != NULL) { + if (groupwith == NULL) { + cmdq_error(cmdq, "no such session: %s", target); + goto error; + } } else groupwith = NULL; @@ -111,6 +131,9 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq) if (c == NULL) detached = 1; + if (tmate_foreground) + detached = 1; + /* Is this client already attached? */ already_attached = 0; if (c != NULL && c->session != NULL) @@ -118,42 +141,30 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq) /* Get the new session working directory. */ if (args_has(args, 'c')) { - ft = format_create(); - format_defaults(ft, cmd_find_client(cmdq, NULL, 1), NULL, NULL, - NULL); - cp = format_expand(ft, args_get(args, 'c')); + ft = format_create(cmdq, 0); + format_defaults(ft, c, NULL, NULL, NULL); + to_free = cwd = format_expand(ft, args_get(args, 'c')); format_free(ft); - - if (cp != NULL && *cp != '\0') { - fd = open(cp, O_RDONLY|O_DIRECTORY); - free(cp); - if (fd == -1) { - cmdq_error(cmdq, "bad working directory: %s", - strerror(errno)); - return (CMD_RETURN_ERROR); - } - } else if (cp != NULL) - free(cp); - cwd = fd; - } else if (c != NULL && c->session == NULL) + } else if (c != NULL && c->session == NULL && c->cwd != NULL) cwd = c->cwd; - else if ((c0 = cmd_current_client(cmdq)) != NULL) - cwd = c0->session->cwd; - else { - fd = open(".", O_RDONLY); - cwd = fd; - } + else + cwd = "."; /* - * Save the termios settings, part of which is used for new windows in - * this session. + * If this is a new client, check for nesting and save the termios + * settings (part of which is used for new windows in this session). * - * This is read again with tcgetattr() rather than using tty.tio as if - * detached, tty_open won't be called. Because of this, it must be done - * before opening the terminal as that calls tcsetattr() to prepare for - * tmux taking over. + * tcgetattr() is used rather than using tty.tio since if the client is + * detached, tty_open won't be called. It must be done before opening + * the terminal as that calls tcsetattr() to prepare for tmux taking + * over. */ if (!detached && !already_attached && c->tty.fd != -1) { + if (server_client_check_nested(cmdq->client)) { + cmdq_error(cmdq, "sessions should be nested with care, " + "unset $TMUX to force"); + return (CMD_RETURN_ERROR); + } if (tcgetattr(c->tty.fd, &tio) != 0) fatal("tcgetattr failed"); tiop = &tio; @@ -191,7 +202,7 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq) goto error; } } - if (sy > 0 && options_get_number(&global_s_options, "status")) + if (sy > 0 && options_get_number(global_s_options, "status")) sy--; if (sx == 0) sx = 1; @@ -201,11 +212,11 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq) /* Figure out the command for the new window. */ argc = -1; argv = NULL; - if (target == NULL && args->argc != 0) { + if (!args_has(args, 't') && args->argc != 0) { argc = args->argc; argv = args->argv; - } else if (target == NULL) { - cmd = options_get_string(&global_s_options, "default-command"); + } else if (groupwith == NULL) { + cmd = options_get_string(global_s_options, "default-command"); if (cmd != NULL && *cmd != '\0') { argc = 1; argv = &cmd; @@ -217,34 +228,36 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq) path = NULL; if (c != NULL && c->session == NULL) - envent = environ_find(&c->environ, "PATH"); + envent = environ_find(c->environ, "PATH"); else - envent = environ_find(&global_environ, "PATH"); + envent = environ_find(global_environ, "PATH"); if (envent != NULL) path = envent->value; /* Construct the environment. */ - environ_init(&env); - update = options_get_string(&global_s_options, "update-environment"); - if (c != NULL) - environ_update(update, &c->environ, &env); + env = environ_create(); + if (c != NULL && !args_has(args, 'E')) { + update = options_get_string(global_s_options, + "update-environment"); + environ_update(update, c->environ, env); + } /* Create the new session. */ - idx = -1 - options_get_number(&global_s_options, "base-index"); - s = session_create(newname, argc, argv, path, cwd, &env, tiop, idx, sx, + idx = -1 - options_get_number(global_s_options, "base-index"); + s = session_create(newname, argc, argv, path, cwd, env, tiop, idx, sx, sy, &cause); + environ_free(env); if (s == NULL) { cmdq_error(cmdq, "create session failed: %s", cause); free(cause); goto error; } - environ_free(&env); /* Set the initial window name if one given. */ if (argc >= 0 && args_has(args, 'n')) { w = s->curw->window; window_set_name(w, args_get(args, 'n')); - options_set_number(&w->options, "automatic-rename", 0); + options_set_number(w->options, "automatic-rename", 0); } /* @@ -254,7 +267,7 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq) if (groupwith != NULL) { session_group_add(groupwith, s); session_group_synchronize_to(s); - session_select(s, RB_ROOT(&s->windows)->idx); + session_select(s, RB_MIN(winlinks, &s->windows)->idx); } /* @@ -262,13 +275,17 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq) * taking this session and needs to get MSG_READY and stay around. */ if (!detached) { - if (!already_attached) - server_write_ready(c); - else if (c->session != NULL) + if (!already_attached) { + if (~c->flags & CLIENT_CONTROL) + proc_send(c->peer, MSG_READY, -1, NULL, 0); + } else if (c->session != NULL) c->last_session = c->session; c->session = s; + server_client_set_key_table(c, NULL); + status_timer_start(c); notify_attached_session_changed(c); - session_update_activity(s); + session_update_activity(s, NULL); + gettimeofday(&s->last_attached_time, NULL); server_redraw_client(c); } recalculate_sizes(); @@ -286,9 +303,8 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq) if ((template = args_get(args, 'F')) == NULL) template = NEW_SESSION_TEMPLATE; - ft = format_create(); - format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, NULL, - NULL); + ft = format_create(cmdq, 0); + format_defaults(ft, c, s, NULL, NULL); cp = format_expand(ft, template); cmdq_print(cmdq, "%s", cp); @@ -300,12 +316,12 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq) if (!detached) cmdq->client_exit = 0; - if (fd != -1) - close(fd); + if (to_free != NULL) + free((void *)to_free); return (CMD_RETURN_NORMAL); error: - if (fd != -1) - close(fd); + if (to_free != NULL) + free((void *)to_free); return (CMD_RETURN_ERROR); } diff --git a/cmd-new-window.c b/cmd-new-window.c index c05a0ce8..2a647b9f 100644 --- a/cmd-new-window.c +++ b/cmd-new-window.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -35,57 +35,43 @@ enum cmd_retval cmd_new_window_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_new_window_entry = { - "new-window", "neww", - "ac:dF:kn:Pt:", 0, -1, - "[-adkP] [-c start-directory] [-F format] [-n window-name] " - CMD_TARGET_WINDOW_USAGE " [command]", - 0, - cmd_new_window_exec + .name = "new-window", + .alias = "neww", + + .args = { "ac:dF:kn:Pt:", 0, -1 }, + .usage = "[-adkP] [-c start-directory] [-F format] [-n window-name] " + CMD_TARGET_WINDOW_USAGE " [command]", + + .tflag = CMD_WINDOW_INDEX, + + .flags = 0, + .exec = cmd_new_window_exec }; enum cmd_retval cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct session *s; - struct winlink *wl; - const char *cmd, *path, *template; + struct session *s = cmdq->state.tflag.s; + struct winlink *wl = cmdq->state.tflag.wl; + struct client *c = cmdq->state.c; + int idx = cmdq->state.tflag.idx; + const char *cmd, *path, *template, *cwd, *to_free; char **argv, *cause, *cp; - int argc, idx, last, detached, cwd, fd = -1; + int argc, detached; struct format_tree *ft; struct environ_entry *envent; if (args_has(args, 'a')) { - wl = cmd_find_window(cmdq, args_get(args, 't'), &s); - if (wl == NULL) - return (CMD_RETURN_ERROR); - idx = wl->idx + 1; - - /* Find the next free index. */ - for (last = idx; last < INT_MAX; last++) { - if (winlink_find_by_index(&s->windows, last) == NULL) - break; - } - if (last == INT_MAX) { + if ((idx = winlink_shuffle_up(s, wl)) == -1) { cmdq_error(cmdq, "no free window indexes"); return (CMD_RETURN_ERROR); } - - /* Move everything from last - 1 to idx up a bit. */ - for (; last > idx; last--) { - wl = winlink_find_by_index(&s->windows, last - 1); - server_link_window(s, wl, s, last, 0, 0, NULL); - server_unlink_window(s, wl); - } - } else { - idx = cmd_find_index(cmdq, args_get(args, 't'), &s); - if (idx == -2) - return (CMD_RETURN_ERROR); } detached = args_has(args, 'd'); if (args->argc == 0) { - cmd = options_get_string(&s->options, "default-command"); + cmd = options_get_string(s->options, "default-command"); if (cmd != NULL && *cmd != '\0') { argc = 1; argv = (char **)&cmd; @@ -100,30 +86,18 @@ cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq) path = NULL; if (cmdq->client != NULL && cmdq->client->session == NULL) - envent = environ_find(&cmdq->client->environ, "PATH"); + envent = environ_find(cmdq->client->environ, "PATH"); else - envent = environ_find(&s->environ, "PATH"); + envent = environ_find(s->environ, "PATH"); if (envent != NULL) path = envent->value; + to_free = NULL; if (args_has(args, 'c')) { - ft = format_create(); - format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, NULL, - NULL); - cp = format_expand(ft, args_get(args, 'c')); + ft = format_create(cmdq, 0); + format_defaults(ft, c, s, NULL, NULL); + cwd = to_free = format_expand(ft, args_get(args, 'c')); format_free(ft); - - if (cp != NULL && *cp != '\0') { - fd = open(cp, O_RDONLY|O_DIRECTORY); - free(cp); - if (fd == -1) { - cmdq_error(cmdq, "bad working directory: %s", - strerror(errno)); - return (CMD_RETURN_ERROR); - } - } else if (cp != NULL) - free(cp); - cwd = fd; } else if (cmdq->client != NULL && cmdq->client->session == NULL) cwd = cmdq->client->cwd; else @@ -150,7 +124,7 @@ cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq) } if (idx == -1) - idx = -1 - options_get_number(&s->options, "base-index"); + idx = -1 - options_get_number(s->options, "base-index"); wl = session_new(s, args_get(args, 'n'), argc, argv, path, cwd, idx, &cause); if (wl == NULL) { @@ -168,9 +142,8 @@ cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq) if ((template = args_get(args, 'F')) == NULL) template = NEW_WINDOW_TEMPLATE; - ft = format_create(); - format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, wl, - NULL); + ft = format_create(cmdq, 0); + format_defaults(ft, c, s, wl, NULL); cp = format_expand(ft, template); cmdq_print(cmdq, "%s", cp); @@ -179,12 +152,12 @@ cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq) format_free(ft); } - if (fd != -1) - close(fd); + if (to_free != NULL) + free((void *)to_free); return (CMD_RETURN_NORMAL); error: - if (fd != -1) - close(fd); + if (to_free != NULL) + free((void *)to_free); return (CMD_RETURN_ERROR); } diff --git a/cmd-paste-buffer.c b/cmd-paste-buffer.c index 5d91aeff..e0c29d28 100644 --- a/cmd-paste-buffer.c +++ b/cmd-paste-buffer.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -33,31 +33,35 @@ void cmd_paste_buffer_filter(struct window_pane *, const char *, size_t, const char *, int); const struct cmd_entry cmd_paste_buffer_entry = { - "paste-buffer", "pasteb", - "db:prs:t:", 0, 0, - "[-dpr] [-s separator] " CMD_BUFFER_USAGE " " CMD_TARGET_PANE_USAGE, - 0, - cmd_paste_buffer_exec + .name = "paste-buffer", + .alias = "pasteb", + + .args = { "db:prs:t:", 0, 0 }, + .usage = "[-dpr] [-s separator] " CMD_BUFFER_USAGE " " + CMD_TARGET_PANE_USAGE, + + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_paste_buffer_exec }; enum cmd_retval cmd_paste_buffer_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct window_pane *wp; - struct session *s; + struct window_pane *wp = cmdq->state.tflag.wp; struct paste_buffer *pb; - const char *sepstr, *bufname; - - if (cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp) == NULL) - return (CMD_RETURN_ERROR); + const char *sepstr, *bufname, *bufdata, *bufend, *line; + size_t seplen, bufsize; + int bracket = args_has(args, 'p'); bufname = NULL; if (args_has(args, 'b')) bufname = args_get(args, 'b'); if (bufname == NULL) - pb = paste_get_top(); + pb = paste_get_top(NULL); else { pb = paste_get_name(bufname); if (pb == NULL) { @@ -66,7 +70,7 @@ cmd_paste_buffer_exec(struct cmd *self, struct cmd_q *cmdq) } } - if (pb != NULL) { + if (pb != NULL && ~wp->flags & PANE_INPUTOFF) { sepstr = args_get(args, 's'); if (sepstr == NULL) { if (args_has(args, 'r')) @@ -74,16 +78,33 @@ cmd_paste_buffer_exec(struct cmd *self, struct cmd_q *cmdq) else sepstr = "\r"; } - paste_send_pane(pb, wp, sepstr, args_has(args, 'p')); + seplen = strlen(sepstr); + + if (bracket && (wp->screen->mode & MODE_BRACKETPASTE)) + bufferevent_write(wp->event, "\033[200~", 6); + + bufdata = paste_buffer_data(pb, &bufsize); + bufend = bufdata + bufsize; + + for (;;) { + line = memchr(bufdata, '\n', bufend - bufdata); + if (line == NULL) + break; + + bufferevent_write(wp->event, bufdata, line - bufdata); + bufferevent_write(wp->event, sepstr, seplen); + + bufdata = line + 1; + } + if (bufdata != bufend) + bufferevent_write(wp->event, bufdata, bufend - bufdata); + + if (bracket && (wp->screen->mode & MODE_BRACKETPASTE)) + bufferevent_write(wp->event, "\033[201~", 6); } - /* Delete the buffer if -d. */ - if (args_has(args, 'd')) { - if (bufname == NULL) - paste_free_top(); - else - paste_free_name(bufname); - } + if (pb != NULL && args_has(args, 'd')) + paste_free(pb); return (CMD_RETURN_NORMAL); } diff --git a/cmd-pipe-pane.c b/cmd-pipe-pane.c index 49b156c6..0d13b356 100644 --- a/cmd-pipe-pane.c +++ b/cmd-pipe-pane.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -37,29 +37,30 @@ enum cmd_retval cmd_pipe_pane_exec(struct cmd *, struct cmd_q *); void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *); const struct cmd_entry cmd_pipe_pane_entry = { - "pipe-pane", "pipep", - "ot:", 0, 1, - "[-o] " CMD_TARGET_PANE_USAGE " [command]", - 0, - cmd_pipe_pane_exec + .name = "pipe-pane", + .alias = "pipep", + + .args = { "ot:", 0, 1 }, + .usage = "[-o] " CMD_TARGET_PANE_USAGE " [command]", + + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_pipe_pane_exec }; enum cmd_retval cmd_pipe_pane_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct client *c; - struct session *s; - struct winlink *wl; - struct window_pane *wp; + struct client *c = cmdq->state.c; + struct window_pane *wp = cmdq->state.tflag.wp; + struct session *s = cmdq->state.tflag.s; + struct winlink *wl = cmdq->state.tflag.wl; char *cmd; int old_fd, pipe_fd[2], null_fd; struct format_tree *ft; - if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp)) == NULL) - return (CMD_RETURN_ERROR); - c = cmd_find_client(cmdq, NULL, 1); - /* Destroy the old pipe. */ old_fd = wp->pipe_fd; if (wp->pipe_fd != -1) { @@ -88,7 +89,7 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmd_q *cmdq) } /* Expand the command. */ - ft = format_create(); + ft = format_create(cmdq, 0); format_defaults(ft, c, s, wl, wp); cmd = format_expand_time(ft, args->argv[0], time(NULL)); format_free(ft); @@ -141,8 +142,8 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmd_q *cmdq) } void -cmd_pipe_pane_error_callback( - unused struct bufferevent *bufev, unused short what, void *data) +cmd_pipe_pane_error_callback(__unused struct bufferevent *bufev, + __unused short what, void *data) { struct window_pane *wp = data; diff --git a/cmd-queue.c b/cmd-queue.c index 6be532a8..f08a724e 100644 --- a/cmd-queue.c +++ b/cmd-queue.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2013 Nicholas Marriott + * Copyright (c) 2013 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -20,9 +20,13 @@ #include #include +#include #include #include "tmux.h" +#include "tmate.h" + +static enum cmd_retval cmdq_continue_one(struct cmd_q *); /* Create new command queue. */ struct cmd_q * @@ -32,7 +36,7 @@ cmdq_new(struct client *c) cmdq = xcalloc(1, sizeof *cmdq); cmdq->references = 1; - cmdq->dead = 0; + cmdq->flags = 0; cmdq->client = c; cmdq->client_exit = -1; @@ -41,6 +45,9 @@ cmdq_new(struct client *c) cmdq->item = NULL; cmdq->cmd = NULL; + cmd_find_clear_state(&cmdq->current, NULL, 0); + cmdq->parent = NULL; + return (cmdq); } @@ -48,8 +55,11 @@ cmdq_new(struct client *c) int cmdq_free(struct cmd_q *cmdq) { - if (--cmdq->references != 0) - return (cmdq->dead); + if (--cmdq->references != 0) { + if (cmdq->flags & CMD_Q_DEAD) + return (1); + return (0); + } cmdq_flush(cmdq); free(cmdq); @@ -63,22 +73,32 @@ cmdq_print(struct cmd_q *cmdq, const char *fmt, ...) struct client *c = cmdq->client; struct window *w; va_list ap; + char *tmp, *msg; va_start(ap, fmt); if (c == NULL) /* nothing */; else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) { - evbuffer_add_vprintf(c->stdout_data, fmt, ap); - + if (~c->flags & CLIENT_UTF8) { + vasprintf(&tmp, fmt, ap); + msg = utf8_sanitize(tmp); + free(tmp); + evbuffer_add(c->stdout_data, msg, strlen(msg)); + free(msg); + } else + evbuffer_add_vprintf(c->stdout_data, fmt, ap); evbuffer_add(c->stdout_data, "\n", 1); - server_push_stdout(c); + server_client_push_stdout(c); } else { w = c->session->curw->window; if (w->active->mode != &window_copy_mode) { window_pane_reset_mode(w->active); window_pane_set_mode(w->active, &window_copy_mode); window_copy_init_for_output(w->active); +#ifdef TMATE + tmate_sync_copy_mode(w->active); +#endif } window_copy_vadd(w->active, fmt, ap); } @@ -95,18 +115,31 @@ cmdq_error(struct cmd_q *cmdq, const char *fmt, ...) va_list ap; char *msg; size_t msglen; + char *tmp; va_start(ap, fmt); msglen = xvasprintf(&msg, fmt, ap); va_end(ap); if (c == NULL) +#ifdef TMATE + if (cmd->file && cmd->line) + cfg_add_cause("%s:%u: %s", cmd->file, cmd->line, msg); + else + cfg_add_cause("%s", msg); +#else cfg_add_cause("%s:%u: %s", cmd->file, cmd->line, msg); +#endif else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) { + if (~c->flags & CLIENT_UTF8) { + tmp = msg; + msg = utf8_sanitize(tmp); + free(tmp); + msglen = strlen(msg); + } evbuffer_add(c->stderr_data, msg, msglen); evbuffer_add(c->stderr_data, "\n", 1); - - server_push_stderr(c); + server_client_push_stderr(c); c->retval = 1; } else { *msg = toupper((u_char) *msg); @@ -127,14 +160,14 @@ cmdq_guard(struct cmd_q *cmdq, const char *guard, int flags) evbuffer_add_printf(c->stdout_data, "%%%s %ld %u %d\n", guard, (long) cmdq->time, cmdq->number, flags); - server_push_stdout(c); + server_client_push_stdout(c); } /* Add command list to queue and begin processing if needed. */ void -cmdq_run(struct cmd_q *cmdq, struct cmd_list *cmdlist) +cmdq_run(struct cmd_q *cmdq, struct cmd_list *cmdlist, struct mouse_event *m) { - cmdq_append(cmdq, cmdlist); + cmdq_append(cmdq, cmdlist, m); if (cmdq->item == NULL) { cmdq->cmd = NULL; @@ -144,7 +177,7 @@ cmdq_run(struct cmd_q *cmdq, struct cmd_list *cmdlist) /* Add command list to queue. */ void -cmdq_append(struct cmd_q *cmdq, struct cmd_list *cmdlist) +cmdq_append(struct cmd_q *cmdq, struct cmd_list *cmdlist, struct mouse_event *m) { struct cmd_q_item *item; @@ -152,20 +185,65 @@ cmdq_append(struct cmd_q *cmdq, struct cmd_list *cmdlist) item->cmdlist = cmdlist; TAILQ_INSERT_TAIL(&cmdq->queue, item, qentry); cmdlist->references++; + + if (m != NULL) + memcpy(&item->mouse, m, sizeof item->mouse); + else + item->mouse.valid = 0; +} + +/* Process one command. */ +static enum cmd_retval +cmdq_continue_one(struct cmd_q *cmdq) +{ + struct cmd *cmd = cmdq->cmd; + enum cmd_retval retval; + char *tmp; + int flags = !!(cmd->flags & CMD_CONTROL); + +#ifdef TMATE + if (tmate_should_replicate_cmd(cmd->entry)) + tmate_exec_cmd(cmd); +#endif + + tmp = cmd_print(cmd); + log_debug("cmdq %p: %s", cmdq, tmp); + free(tmp); + + cmdq->time = time(NULL); + cmdq->number++; + + cmdq_guard(cmdq, "begin", flags); + + if (cmd_prepare_state(cmd, cmdq, NULL) != 0) + goto error; + retval = cmd->entry->exec(cmd, cmdq); + if (retval == CMD_RETURN_ERROR) + goto error; + + cmdq_guard(cmdq, "end", flags); + return (retval); + +error: + cmdq_guard(cmdq, "error", flags); + return (CMD_RETURN_ERROR); } /* Continue processing command queue. Returns 1 if finishes empty. */ int cmdq_continue(struct cmd_q *cmdq) { + struct client *c = cmdq->client; struct cmd_q_item *next; enum cmd_retval retval; - int empty, flags; - char s[1024]; + int empty; cmdq->references++; notify_disable(); + log_debug("continuing cmdq %p: flags %#x, client %p", cmdq, cmdq->flags, + c); + empty = TAILQ_EMPTY(&cmdq->queue); if (empty) goto empty; @@ -178,23 +256,7 @@ cmdq_continue(struct cmd_q *cmdq) do { while (cmdq->cmd != NULL) { - cmd_print(cmdq->cmd, s, sizeof s); - log_debug("cmdq %p: %s (client %d)", cmdq, s, - cmdq->client != NULL ? cmdq->client->ibuf.fd : -1); - - cmdq->time = time(NULL); - cmdq->number++; - - flags = !!(cmdq->cmd->flags & CMD_CONTROL); - cmdq_guard(cmdq, "begin", flags); - - retval = cmdq->cmd->entry->exec(cmdq->cmd, cmdq); - - if (retval == CMD_RETURN_ERROR) - cmdq_guard(cmdq, "error", flags); - else - cmdq_guard(cmdq, "end", flags); - + retval = cmdq_continue_one(cmdq); if (retval == CMD_RETURN_ERROR) break; if (retval == CMD_RETURN_WAIT) @@ -203,7 +265,6 @@ cmdq_continue(struct cmd_q *cmdq) cmdq_flush(cmdq); goto empty; } - cmdq->cmd = TAILQ_NEXT(cmdq->cmd, qentry); } next = TAILQ_NEXT(cmdq->item, qentry); @@ -244,3 +305,4 @@ cmdq_flush(struct cmd_q *cmdq) } cmdq->item = NULL; } + diff --git a/cmd-refresh-client.c b/cmd-refresh-client.c index f693872c..79e5aad0 100644 --- a/cmd-refresh-client.c +++ b/cmd-refresh-client.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -27,24 +27,26 @@ enum cmd_retval cmd_refresh_client_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_refresh_client_entry = { - "refresh-client", "refresh", - "C:St:", 0, 0, - "[-S] [-C size] " CMD_TARGET_CLIENT_USAGE, - 0, - cmd_refresh_client_exec + .name = "refresh-client", + .alias = "refresh", + + .args = { "C:St:", 0, 0 }, + .usage = "[-S] [-C size] " CMD_TARGET_CLIENT_USAGE, + + .tflag = CMD_CLIENT, + + .flags = 0, + .exec = cmd_refresh_client_exec }; enum cmd_retval cmd_refresh_client_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct client *c; + struct client *c = cmdq->state.c; const char *size; u_int w, h; - if ((c = cmd_find_client(cmdq, args_get(args, 't'), 0)) == NULL) - return (CMD_RETURN_ERROR); - if (args_has(args, 'C')) { if ((size = args_get(args, 'C')) == NULL) { cmdq_error(cmdq, "missing size"); @@ -66,10 +68,12 @@ cmd_refresh_client_exec(struct cmd *self, struct cmd_q *cmdq) if (tty_set_size(&c->tty, w, h)) recalculate_sizes(); } else if (args_has(args, 'S')) { - status_update_jobs(c); + c->flags |= CLIENT_STATUSFORCE; server_status_client(c); - } else + } else { + c->flags |= CLIENT_STATUSFORCE; server_redraw_client(c); + } return (CMD_RETURN_NORMAL); } diff --git a/cmd-rename-session.c b/cmd-rename-session.c index 481154ce..b40f44f7 100644 --- a/cmd-rename-session.c +++ b/cmd-rename-session.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -29,18 +29,23 @@ enum cmd_retval cmd_rename_session_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_rename_session_entry = { - "rename-session", "rename", - "t:", 1, 1, - CMD_TARGET_SESSION_USAGE " new-name", - 0, - cmd_rename_session_exec + .name = "rename-session", + .alias = "rename", + + .args = { "t:", 1, 1 }, + .usage = CMD_TARGET_SESSION_USAGE " new-name", + + .tflag = CMD_SESSION, + + .flags = 0, + .exec = cmd_rename_session_exec }; enum cmd_retval cmd_rename_session_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct session *s; + struct session *s = cmdq->state.tflag.s; const char *newname; newname = args->argv[0]; @@ -53,9 +58,6 @@ cmd_rename_session_exec(struct cmd *self, struct cmd_q *cmdq) return (CMD_RETURN_ERROR); } - if ((s = cmd_find_session(cmdq, args_get(args, 't'), 0)) == NULL) - return (CMD_RETURN_ERROR); - RB_REMOVE(sessions, &sessions, s); free(s->name); s->name = xstrdup(newname); diff --git a/cmd-rename-window.c b/cmd-rename-window.c index 2f677a48..a1f15eef 100644 --- a/cmd-rename-window.c +++ b/cmd-rename-window.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -29,25 +29,26 @@ enum cmd_retval cmd_rename_window_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_rename_window_entry = { - "rename-window", "renamew", - "t:", 1, 1, - CMD_TARGET_WINDOW_USAGE " new-name", - 0, - cmd_rename_window_exec + .name = "rename-window", + .alias = "renamew", + + .args = { "t:", 1, 1 }, + .usage = CMD_TARGET_WINDOW_USAGE " new-name", + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_rename_window_exec }; enum cmd_retval cmd_rename_window_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct session *s; - struct winlink *wl; - - if ((wl = cmd_find_window(cmdq, args_get(args, 't'), &s)) == NULL) - return (CMD_RETURN_ERROR); + struct winlink *wl = cmdq->state.tflag.wl; window_set_name(wl->window, args->argv[0]); - options_set_number(&wl->window->options, "automatic-rename", 0); + options_set_number(wl->window->options, "automatic-rename", 0); server_status_window(wl->window); diff --git a/cmd-resize-pane.c b/cmd-resize-pane.c index 42f0f39a..7ec65f10 100644 --- a/cmd-resize-pane.c +++ b/cmd-resize-pane.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -28,29 +28,45 @@ enum cmd_retval cmd_resize_pane_exec(struct cmd *, struct cmd_q *); +void cmd_resize_pane_mouse_update(struct client *, struct mouse_event *); + const struct cmd_entry cmd_resize_pane_entry = { - "resize-pane", "resizep", - "DLRt:Ux:y:Z", 0, 1, - "[-DLRUZ] [-x width] [-y height] " CMD_TARGET_PANE_USAGE " [adjustment]", - 0, - cmd_resize_pane_exec + .name = "resize-pane", + .alias = "resizep", + + .args = { "DLMRt:Ux:y:Z", 0, 1 }, + .usage = "[-DLMRUZ] [-x width] [-y height] " CMD_TARGET_PANE_USAGE " " + "[adjustment]", + + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_resize_pane_exec }; enum cmd_retval cmd_resize_pane_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct winlink *wl; - struct window *w; + struct window_pane *wp = cmdq->state.tflag.wp; + struct winlink *wl = cmdq->state.tflag.wl; + struct window *w = wl->window; + struct client *c = cmdq->client; + struct session *s = cmdq->state.tflag.s; const char *errstr; char *cause; - struct window_pane *wp; u_int adjust; int x, y; - if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp)) == NULL) - return (CMD_RETURN_ERROR); - w = wl->window; + if (args_has(args, 'M')) { + if (cmd_mouse_window(&cmdq->item->mouse, &s) == NULL) + return (CMD_RETURN_NORMAL); + if (c == NULL || c->session != s) + return (CMD_RETURN_NORMAL); + c->tty.mouse_drag_update = cmd_resize_pane_mouse_update; + cmd_resize_pane_mouse_update(c, &cmdq->item->mouse); + return (CMD_RETURN_NORMAL); + } if (args_has(args, 'Z')) { if (w->flags & WINDOW_ZOOMED) @@ -106,3 +122,50 @@ cmd_resize_pane_exec(struct cmd *self, struct cmd_q *cmdq) return (CMD_RETURN_NORMAL); } + +void +cmd_resize_pane_mouse_update(struct client *c, struct mouse_event *m) +{ + struct winlink *wl; + struct window_pane *wp; + int found; + u_int y, ly; + + wl = cmd_mouse_window(m, NULL); + if (wl == NULL) { + c->tty.mouse_drag_update = NULL; + return; + } + + y = m->y; + if (m->statusat == 0 && y > 0) + y--; + else if (m->statusat > 0 && y >= (u_int)m->statusat) + y = m->statusat - 1; + ly = m->ly; + if (m->statusat == 0 && ly > 0) + ly--; + else if (m->statusat > 0 && ly >= (u_int)m->statusat) + ly = m->statusat - 1; + + found = 0; + TAILQ_FOREACH(wp, &wl->window->panes, entry) { + if (!window_pane_visible(wp)) + continue; + + if (wp->xoff + wp->sx == m->lx && + wp->yoff <= 1 + ly && wp->yoff + wp->sy >= ly) { + layout_resize_pane(wp, LAYOUT_LEFTRIGHT, m->x - m->lx); + found = 1; + } + if (wp->yoff + wp->sy == ly && + wp->xoff <= 1 + m->lx && wp->xoff + wp->sx >= m->lx) { + layout_resize_pane(wp, LAYOUT_TOPBOTTOM, y - ly); + found = 1; + } + } + if (found) + server_redraw_window(wl->window); + else + c->tty.mouse_drag_update = NULL; +} diff --git a/cmd-respawn-pane.c b/cmd-respawn-pane.c index 47031539..ba2c1cd2 100644 --- a/cmd-respawn-pane.c +++ b/cmd-respawn-pane.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * Copyright (c) 2011 Marcel P. Partap * * Permission to use, copy, modify, and distribute this software for any @@ -31,43 +31,44 @@ enum cmd_retval cmd_respawn_pane_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_respawn_pane_entry = { - "respawn-pane", "respawnp", - "kt:", 0, -1, - "[-k] " CMD_TARGET_PANE_USAGE " [command]", - 0, - cmd_respawn_pane_exec + .name = "respawn-pane", + .alias = "respawnp", + + .args = { "kt:", 0, -1 }, + .usage = "[-k] " CMD_TARGET_PANE_USAGE " [command]", + + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_respawn_pane_exec }; enum cmd_retval cmd_respawn_pane_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct winlink *wl; - struct window *w; - struct window_pane *wp; - struct session *s; - struct environ env; + struct winlink *wl = cmdq->state.tflag.wl; + struct window *w = wl->window; + struct window_pane *wp = cmdq->state.tflag.wp; + struct session *s = cmdq->state.tflag.s; + struct environ *env; const char *path; char *cause; u_int idx; struct environ_entry *envent; - if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp)) == NULL) - return (CMD_RETURN_ERROR); - w = wl->window; - if (!args_has(self->args, 'k') && wp->fd != -1) { if (window_pane_index(wp, &idx) != 0) fatalx("index not found"); - cmdq_error(cmdq, "pane still active: %s:%u.%u", + cmdq_error(cmdq, "pane still active: %s:%d.%u", s->name, wl->idx, idx); return (CMD_RETURN_ERROR); } - environ_init(&env); - environ_copy(&global_environ, &env); - environ_copy(&s->environ, &env); - server_fill_environ(s, &env); + env = environ_create(); + environ_copy(global_environ, env); + environ_copy(s->environ, env); + server_fill_environ(s, env); window_pane_reset_mode(wp); screen_reinit(&wp->base); @@ -75,22 +76,22 @@ cmd_respawn_pane_exec(struct cmd *self, struct cmd_q *cmdq) path = NULL; if (cmdq->client != NULL && cmdq->client->session == NULL) - envent = environ_find(&cmdq->client->environ, "PATH"); + envent = environ_find(cmdq->client->environ, "PATH"); else - envent = environ_find(&s->environ, "PATH"); + envent = environ_find(s->environ, "PATH"); if (envent != NULL) path = envent->value; - if (window_pane_spawn(wp, args->argc, args->argv, path, NULL, -1, &env, + if (window_pane_spawn(wp, args->argc, args->argv, path, NULL, NULL, env, s->tio, &cause) != 0) { cmdq_error(cmdq, "respawn pane failed: %s", cause); free(cause); - environ_free(&env); + environ_free(env); return (CMD_RETURN_ERROR); } wp->flags |= PANE_REDRAW; server_status_window(w); - environ_free(&env); + environ_free(env); return (CMD_RETURN_NORMAL); } diff --git a/cmd-respawn-window.c b/cmd-respawn-window.c index 06102ed0..95fc0cb4 100644 --- a/cmd-respawn-window.c +++ b/cmd-respawn-window.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -30,44 +30,45 @@ enum cmd_retval cmd_respawn_window_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_respawn_window_entry = { - "respawn-window", "respawnw", - "kt:", 0, -1, - "[-k] " CMD_TARGET_WINDOW_USAGE " [command]", - 0, - cmd_respawn_window_exec + .name = "respawn-window", + .alias = "respawnw", + + .args = { "kt:", 0, -1 }, + .usage = "[-k] " CMD_TARGET_WINDOW_USAGE " [command]", + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_respawn_window_exec }; enum cmd_retval cmd_respawn_window_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct winlink *wl; - struct window *w; + struct session *s = cmdq->state.tflag.s; + struct winlink *wl = cmdq->state.tflag.wl; + struct window *w = wl->window; struct window_pane *wp; - struct session *s; - struct environ env; + struct environ *env; const char *path; char *cause; struct environ_entry *envent; - if ((wl = cmd_find_window(cmdq, args_get(args, 't'), &s)) == NULL) - return (CMD_RETURN_ERROR); - w = wl->window; - if (!args_has(self->args, 'k')) { TAILQ_FOREACH(wp, &w->panes, entry) { if (wp->fd == -1) continue; - cmdq_error(cmdq, - "window still active: %s:%d", s->name, wl->idx); + cmdq_error(cmdq, "window still active: %s:%d", s->name, + wl->idx); return (CMD_RETURN_ERROR); } } - environ_init(&env); - environ_copy(&global_environ, &env); - environ_copy(&s->environ, &env); - server_fill_environ(s, &env); + env = environ_create(); + environ_copy(global_environ, env); + environ_copy(s->environ, env); + server_fill_environ(s, env); wp = TAILQ_FIRST(&w->panes); TAILQ_REMOVE(&w->panes, wp, entry); @@ -78,18 +79,18 @@ cmd_respawn_window_exec(struct cmd *self, struct cmd_q *cmdq) path = NULL; if (cmdq->client != NULL && cmdq->client->session == NULL) - envent = environ_find(&cmdq->client->environ, "PATH"); + envent = environ_find(cmdq->client->environ, "PATH"); else - envent = environ_find(&s->environ, "PATH"); + envent = environ_find(s->environ, "PATH"); if (envent != NULL) path = envent->value; - if (window_pane_spawn(wp, args->argc, args->argv, path, NULL, -1, &env, + if (window_pane_spawn(wp, args->argc, args->argv, path, NULL, NULL, env, s->tio, &cause) != 0) { cmdq_error(cmdq, "respawn window failed: %s", cause); free(cause); - environ_free(&env); - server_destroy_pane(wp); + environ_free(env); + server_destroy_pane(wp, 0); return (CMD_RETURN_ERROR); } layout_init(w, wp); @@ -101,6 +102,6 @@ cmd_respawn_window_exec(struct cmd *self, struct cmd_q *cmdq) recalculate_sizes(); server_redraw_window(w); - environ_free(&env); + environ_free(env); return (CMD_RETURN_NORMAL); } diff --git a/cmd-rotate-window.c b/cmd-rotate-window.c index 859ff04a..94eca37d 100644 --- a/cmd-rotate-window.c +++ b/cmd-rotate-window.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -27,27 +27,27 @@ enum cmd_retval cmd_rotate_window_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_rotate_window_entry = { - "rotate-window", "rotatew", - "Dt:U", 0, 0, - "[-DU] " CMD_TARGET_WINDOW_USAGE, - 0, - cmd_rotate_window_exec + .name = "rotate-window", + .alias = "rotatew", + + .args = { "Dt:U", 0, 0 }, + .usage = "[-DU] " CMD_TARGET_WINDOW_USAGE, + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_rotate_window_exec }; enum cmd_retval cmd_rotate_window_exec(struct cmd *self, struct cmd_q *cmdq) { - struct args *args = self->args; - struct winlink *wl; - struct window *w; + struct winlink *wl = cmdq->state.tflag.wl; + struct window *w = wl->window; struct window_pane *wp, *wp2; struct layout_cell *lc; u_int sx, sy, xoff, yoff; - if ((wl = cmd_find_window(cmdq, args_get(args, 't'), NULL)) == NULL) - return (CMD_RETURN_ERROR); - w = wl->window; - if (args_has(self->args, 'D')) { wp = TAILQ_LAST(&w->panes, window_panes); TAILQ_REMOVE(&w->panes, wp, entry); diff --git a/cmd-run-shell.c b/cmd-run-shell.c index 5d6d178b..0bd8fc59 100644 --- a/cmd-run-shell.c +++ b/cmd-run-shell.c @@ -36,11 +36,16 @@ void cmd_run_shell_free(void *); void cmd_run_shell_print(struct job *, const char *); const struct cmd_entry cmd_run_shell_entry = { - "run-shell", "run", - "bt:", 1, 1, - "[-b] " CMD_TARGET_PANE_USAGE " shell-command", - 0, - cmd_run_shell_exec + .name = "run-shell", + .alias = "run", + + .args = { "bt:", 1, 1 }, + .usage = "[-b] " CMD_TARGET_PANE_USAGE " shell-command", + + .tflag = CMD_PANE_CANFAIL, + + .flags = 0, + .exec = cmd_run_shell_exec }; struct cmd_run_shell_data { @@ -75,25 +80,20 @@ cmd_run_shell_exec(struct cmd *self, struct cmd_q *cmdq) struct args *args = self->args; struct cmd_run_shell_data *cdata; char *shellcmd; - struct client *c; - struct session *s = NULL; - struct winlink *wl = NULL; - struct window_pane *wp = NULL; + struct session *s = cmdq->state.tflag.s; + struct winlink *wl = cmdq->state.tflag.wl; + struct window_pane *wp = cmdq->state.tflag.wp; struct format_tree *ft; + const char *cwd; - if (args_has(args, 't')) - wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp); - else { - c = cmd_find_client(cmdq, NULL, 1); - if (c != NULL && c->session != NULL) { - s = c->session; - wl = s->curw; - wp = wl->window->active; - } - } - - ft = format_create(); - format_defaults(ft, NULL, s, wl, wp); + if (cmdq->client != NULL && cmdq->client->session == NULL) + cwd = cmdq->client->cwd; + else if (s != NULL) + cwd = s->cwd; + else + cwd = NULL; + ft = format_create(cmdq, 0); + format_defaults(ft, cmdq->state.c, s, wl, wp); shellcmd = format_expand(ft, args->argv[0]); format_free(ft); @@ -105,7 +105,8 @@ cmd_run_shell_exec(struct cmd *self, struct cmd_q *cmdq) cdata->cmdq = cmdq; cmdq->references++; - job_run(shellcmd, s, cmd_run_shell_callback, cmd_run_shell_free, cdata); + job_run(shellcmd, s, cwd, cmd_run_shell_callback, cmd_run_shell_free, + cdata); if (cdata->bflag) return (CMD_RETURN_NORMAL); @@ -122,7 +123,7 @@ cmd_run_shell_callback(struct job *job) int retcode; u_int lines; - if (cmdq->dead) + if (cmdq->flags & CMD_Q_DEAD) return; cmd = cdata->cmd; diff --git a/cmd-save-buffer.c b/cmd-save-buffer.c index 47a263da..56472565 100644 --- a/cmd-save-buffer.c +++ b/cmd-save-buffer.c @@ -34,19 +34,25 @@ enum cmd_retval cmd_save_buffer_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_save_buffer_entry = { - "save-buffer", "saveb", - "ab:", 1, 1, - "[-a] " CMD_BUFFER_USAGE " path", - 0, - cmd_save_buffer_exec + .name = "save-buffer", + .alias = "saveb", + + .args = { "ab:", 1, 1 }, + .usage = "[-a] " CMD_BUFFER_USAGE " path", + + .flags = 0, + .exec = cmd_save_buffer_exec }; const struct cmd_entry cmd_show_buffer_entry = { - "show-buffer", "showb", - "b:", 0, 0, - CMD_BUFFER_USAGE, - 0, - cmd_save_buffer_exec + .name = "show-buffer", + .alias = "showb", + + .args = { "b:", 0, 0 }, + .usage = CMD_BUFFER_USAGE, + + .flags = 0, + .exec = cmd_save_buffer_exec }; enum cmd_retval @@ -56,14 +62,14 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq) struct client *c = cmdq->client; struct session *s; struct paste_buffer *pb; - const char *path, *bufname; - char *start, *end, *msg; - size_t size, used, msglen; - int cwd, fd; + const char *path, *bufname, *bufdata, *start, *end, *cwd; + const char *flags; + char *msg, *file, resolved[PATH_MAX]; + size_t size, used, msglen, bufsize; FILE *f; if (!args_has(args, 'b')) { - if ((pb = paste_get_top()) == NULL) { + if ((pb = paste_get_top(NULL)) == NULL) { cmdq_error(cmdq, "no buffers"); return (CMD_RETURN_ERROR); } @@ -75,6 +81,7 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq) return (CMD_RETURN_ERROR); } } + bufdata = paste_buffer_data(pb, &bufsize); if (self->entry == &cmd_show_buffer_entry) path = "-"; @@ -90,31 +97,35 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq) goto do_print; } - if (c != NULL && c->session == NULL) + if (c != NULL && c->session == NULL && c->cwd != NULL) cwd = c->cwd; - else if ((s = cmd_current_session(cmdq, 0)) != NULL) + else if ((s = c->session) != NULL && s->cwd != NULL) cwd = s->cwd; else - cwd = AT_FDCWD; + cwd = "."; - f = NULL; - if (args_has(self->args, 'a')) { - fd = openat(cwd, path, O_CREAT|O_RDWR|O_APPEND, 0600); - if (fd != -1) - f = fdopen(fd, "ab"); - } else { - fd = openat(cwd, path, O_CREAT|O_RDWR|O_TRUNC, 0600); - if (fd != -1) - f = fdopen(fd, "wb"); - } - if (f == NULL) { - if (fd != -1) - close(fd); - cmdq_error(cmdq, "%s: %s", path, strerror(errno)); + flags = "wb"; + if (args_has(self->args, 'a')) + flags = "ab"; + + if (*path == '/') + file = xstrdup(path); + else + xasprintf(&file, "%s/%s", cwd, path); + if (realpath(file, resolved) == NULL && + strlcpy(resolved, file, sizeof resolved) >= sizeof resolved) { + cmdq_error(cmdq, "%s: %s", file, strerror(ENAMETOOLONG)); return (CMD_RETURN_ERROR); } - if (fwrite(pb->data, 1, pb->size, f) != pb->size) { - cmdq_error(cmdq, "%s: fwrite error", path); + f = fopen(resolved, flags); + free(file); + if (f == NULL) { + cmdq_error(cmdq, "%s: %s", resolved, strerror(errno)); + return (CMD_RETURN_ERROR); + } + + if (fwrite(bufdata, 1, bufsize, f) != bufsize) { + cmdq_error(cmdq, "%s: write error", resolved); fclose(f); return (CMD_RETURN_ERROR); } @@ -123,25 +134,25 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq) return (CMD_RETURN_NORMAL); do_stdout: - evbuffer_add(c->stdout_data, pb->data, pb->size); - server_push_stdout(c); + evbuffer_add(c->stdout_data, bufdata, bufsize); + server_client_push_stdout(c); return (CMD_RETURN_NORMAL); do_print: - if (pb->size > (INT_MAX / 4) - 1) { + if (bufsize > (INT_MAX / 4) - 1) { cmdq_error(cmdq, "buffer too big"); return (CMD_RETURN_ERROR); } msg = NULL; used = 0; - while (used != pb->size) { - start = pb->data + used; - end = memchr(start, '\n', pb->size - used); + while (used != bufsize) { + start = bufdata + used; + end = memchr(start, '\n', bufsize - used); if (end != NULL) size = end - start; else - size = pb->size - used; + size = bufsize - used; msglen = size * 4 + 1; msg = xrealloc(msg, msglen); diff --git a/cmd-select-layout.c b/cmd-select-layout.c index 77137b74..44f01bb7 100644 --- a/cmd-select-layout.c +++ b/cmd-select-layout.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -18,6 +18,8 @@ #include +#include + #include "tmux.h" /* @@ -27,74 +29,111 @@ enum cmd_retval cmd_select_layout_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_select_layout_entry = { - "select-layout", "selectl", - "npt:", 0, 1, - "[-np] " CMD_TARGET_WINDOW_USAGE " [layout-name]", - 0, - cmd_select_layout_exec + .name = "select-layout", + .alias = "selectl", + + .args = { "nopt:", 0, 1 }, + .usage = "[-nop] " CMD_TARGET_WINDOW_USAGE " [layout-name]", + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_select_layout_exec }; const struct cmd_entry cmd_next_layout_entry = { - "next-layout", "nextl", - "t:", 0, 0, - CMD_TARGET_WINDOW_USAGE, - 0, - cmd_select_layout_exec + .name = "next-layout", + .alias = "nextl", + + .args = { "t:", 0, 0 }, + .usage = CMD_TARGET_WINDOW_USAGE, + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_select_layout_exec }; const struct cmd_entry cmd_previous_layout_entry = { - "previous-layout", "prevl", - "t:", 0, 0, - CMD_TARGET_WINDOW_USAGE, - 0, - cmd_select_layout_exec + .name = "previous-layout", + .alias = "prevl", + + .args = { "t:", 0, 0 }, + .usage = CMD_TARGET_WINDOW_USAGE, + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_select_layout_exec }; enum cmd_retval cmd_select_layout_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct winlink *wl; + struct winlink *wl = cmdq->state.tflag.wl; + struct window *w; const char *layoutname; + char *oldlayout; int next, previous, layout; - if ((wl = cmd_find_window(cmdq, args_get(args, 't'), NULL)) == NULL) - return (CMD_RETURN_ERROR); - server_unzoom_window(wl->window); + w = wl->window; + server_unzoom_window(w); next = self->entry == &cmd_next_layout_entry; - if (args_has(self->args, 'n')) + if (args_has(args, 'n')) next = 1; previous = self->entry == &cmd_previous_layout_entry; - if (args_has(self->args, 'p')) + if (args_has(args, 'p')) previous = 1; + oldlayout = w->old_layout; + w->old_layout = layout_dump(w->layout_root); + if (next || previous) { if (next) - layout = layout_set_next(wl->window); + layout_set_next(w); else - layout = layout_set_previous(wl->window); - server_redraw_window(wl->window); - return (CMD_RETURN_NORMAL); + layout_set_previous(w); + goto changed; } - if (args->argc == 0) - layout = wl->window->lastlayout; - else - layout = layout_set_lookup(args->argv[0]); - if (layout != -1) { - layout = layout_set_select(wl->window, layout); - server_redraw_window(wl->window); - return (CMD_RETURN_NORMAL); - } - - if (args->argc != 0) { - layoutname = args->argv[0]; - if (layout_parse(wl->window, layoutname) == -1) { - cmdq_error(cmdq, "can't set layout: %s", layoutname); - return (CMD_RETURN_ERROR); + if (!args_has(args, 'o')) { + if (args->argc == 0) + layout = w->lastlayout; + else + layout = layout_set_lookup(args->argv[0]); + if (layout != -1) { + layout_set_select(w, layout); + goto changed; } - server_redraw_window(wl->window); } + + if (args->argc != 0) + layoutname = args->argv[0]; + else if (args_has(args, 'o')) + layoutname = oldlayout; + else + layoutname = NULL; + + if (layoutname != NULL) { + if (layout_parse(w, layoutname) == -1) { + cmdq_error(cmdq, "can't set layout: %s", layoutname); + goto error; + } + goto changed; + } + + free(oldlayout); return (CMD_RETURN_NORMAL); + +changed: + free(oldlayout); + server_redraw_window(w); + return (CMD_RETURN_NORMAL); + +error: + free(w->old_layout); + w->old_layout = oldlayout; + return (CMD_RETURN_ERROR); } diff --git a/cmd-select-pane.c b/cmd-select-pane.c index 5810eeab..14d53d48 100644 --- a/cmd-select-pane.c +++ b/cmd-select-pane.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -27,32 +27,42 @@ enum cmd_retval cmd_select_pane_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_select_pane_entry = { - "select-pane", "selectp", - "DdeLlRt:U", 0, 0, - "[-DdeLlRU] " CMD_TARGET_PANE_USAGE, - 0, - cmd_select_pane_exec + .name = "select-pane", + .alias = "selectp", + + .args = { "DdegLlMmP:Rt:U", 0, 0 }, + .usage = "[-DdegLlMmRU] [-P style] " CMD_TARGET_PANE_USAGE, + + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_select_pane_exec }; const struct cmd_entry cmd_last_pane_entry = { - "last-pane", "lastp", - "det:", 0, 0, - "[-de] " CMD_TARGET_WINDOW_USAGE, - 0, - cmd_select_pane_exec + .name = "last-pane", + .alias = "lastp", + + .args = { "det:", 0, 0 }, + .usage = "[-de] " CMD_TARGET_WINDOW_USAGE, + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_select_pane_exec }; enum cmd_retval cmd_select_pane_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct winlink *wl; - struct window_pane *wp; + struct winlink *wl = cmdq->state.tflag.wl; + struct window *w = wl->window; + struct session *s = cmdq->state.tflag.s; + struct window_pane *wp = cmdq->state.tflag.wp, *lastwp, *markedwp; + const char *style; if (self->entry == &cmd_last_pane_entry || args_has(args, 'l')) { - wl = cmd_find_window(cmdq, args_get(args, 't'), NULL); - if (wl == NULL) - return (CMD_RETURN_ERROR); if (wl->window->last == NULL) { cmdq_error(cmdq, "no last pane"); @@ -60,48 +70,94 @@ cmd_select_pane_exec(struct cmd *self, struct cmd_q *cmdq) } if (args_has(self->args, 'e')) - wl->window->last->flags &= ~PANE_INPUTOFF; + w->last->flags &= ~PANE_INPUTOFF; else if (args_has(self->args, 'd')) - wl->window->last->flags |= PANE_INPUTOFF; + w->last->flags |= PANE_INPUTOFF; else { - server_unzoom_window(wl->window); - window_set_active_pane(wl->window, wl->window->last); - server_status_window(wl->window); - server_redraw_window_borders(wl->window); + server_unzoom_window(w); + window_redraw_active_switch(w, w->last); + if (window_set_active_pane(w, w->last)) { + server_status_window(w); + server_redraw_window_borders(w); + } } return (CMD_RETURN_NORMAL); } - if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp)) == NULL) - return (CMD_RETURN_ERROR); + if (args_has(args, 'm') || args_has(args, 'M')) { + if (args_has(args, 'm') && !window_pane_visible(wp)) + return (CMD_RETURN_NORMAL); + lastwp = marked_pane.wp; + if (args_has(args, 'M') || server_is_marked(s, wl, wp)) + server_clear_marked(); + else + server_set_marked(s, wl, wp); + markedwp = marked_pane.wp; + + if (lastwp != NULL) { + server_redraw_window_borders(lastwp->window); + server_status_window(lastwp->window); + } + if (markedwp != NULL) { + server_redraw_window_borders(markedwp->window); + server_status_window(markedwp->window); + } + return (CMD_RETURN_NORMAL); + } + + if (args_has(self->args, 'P') || args_has(self->args, 'g')) { + if (args_has(args, 'P')) { + style = args_get(args, 'P'); + if (style_parse(&grid_default_cell, &wp->colgc, + style) == -1) { + cmdq_error(cmdq, "bad style: %s", style); + return (CMD_RETURN_ERROR); + } + wp->flags |= PANE_REDRAW; + } + if (args_has(self->args, 'g')) + cmdq_print(cmdq, "%s", style_tostring(&wp->colgc)); + return (CMD_RETURN_NORMAL); + } + + if (args_has(self->args, 'L')) { + server_unzoom_window(wp->window); + wp = window_pane_find_left(wp); + } else if (args_has(self->args, 'R')) { + server_unzoom_window(wp->window); + wp = window_pane_find_right(wp); + } else if (args_has(self->args, 'U')) { + server_unzoom_window(wp->window); + wp = window_pane_find_up(wp); + } else if (args_has(self->args, 'D')) { + server_unzoom_window(wp->window); + wp = window_pane_find_down(wp); + } + if (wp == NULL) + return (CMD_RETURN_NORMAL); + + if (args_has(self->args, 'e')) { + wp->flags &= ~PANE_INPUTOFF; + return (CMD_RETURN_NORMAL); + } + if (args_has(self->args, 'd')) { + wp->flags |= PANE_INPUTOFF; + return (CMD_RETURN_NORMAL); + } + + if (wp == w->active) + return (CMD_RETURN_NORMAL); server_unzoom_window(wp->window); if (!window_pane_visible(wp)) { cmdq_error(cmdq, "pane not visible"); return (CMD_RETURN_ERROR); } - - if (args_has(self->args, 'L')) - wp = window_pane_find_left(wp); - else if (args_has(self->args, 'R')) - wp = window_pane_find_right(wp); - else if (args_has(self->args, 'U')) - wp = window_pane_find_up(wp); - else if (args_has(self->args, 'D')) - wp = window_pane_find_down(wp); - if (wp == NULL) { - cmdq_error(cmdq, "pane not found"); - return (CMD_RETURN_ERROR); - } - - if (args_has(self->args, 'e')) - wp->flags &= ~PANE_INPUTOFF; - else if (args_has(self->args, 'd')) - wp->flags |= PANE_INPUTOFF; - else if (window_set_active_pane(wl->window, wp)) { - server_status_window(wl->window); - server_redraw_window_borders(wl->window); + window_redraw_active_switch(w, wp); + if (window_set_active_pane(w, wp)) { + server_status_window(w); + server_redraw_window_borders(w); } return (CMD_RETURN_NORMAL); diff --git a/cmd-select-window.c b/cmd-select-window.c index f530f1fe..78228067 100644 --- a/cmd-select-window.c +++ b/cmd-select-window.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -29,43 +29,62 @@ enum cmd_retval cmd_select_window_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_select_window_entry = { - "select-window", "selectw", - "lnpTt:", 0, 0, - "[-lnpT] " CMD_TARGET_WINDOW_USAGE, - 0, - cmd_select_window_exec + .name = "select-window", + .alias = "selectw", + + .args = { "lnpTt:", 0, 0 }, + .usage = "[-lnpT] " CMD_TARGET_WINDOW_USAGE, + + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_select_window_exec }; const struct cmd_entry cmd_next_window_entry = { - "next-window", "next", - "at:", 0, 0, - "[-a] " CMD_TARGET_SESSION_USAGE, - 0, - cmd_select_window_exec + .name = "next-window", + .alias = "next", + + .args = { "at:", 0, 0 }, + .usage = "[-a] " CMD_TARGET_SESSION_USAGE, + + .tflag = CMD_SESSION, + + .flags = 0, + .exec = cmd_select_window_exec }; const struct cmd_entry cmd_previous_window_entry = { - "previous-window", "prev", - "at:", 0, 0, - "[-a] " CMD_TARGET_SESSION_USAGE, - 0, - cmd_select_window_exec + .name = "previous-window", + .alias = "prev", + + .args = { "at:", 0, 0 }, + .usage = "[-a] " CMD_TARGET_SESSION_USAGE, + + .tflag = CMD_SESSION, + + .flags = 0, + .exec = cmd_select_window_exec }; const struct cmd_entry cmd_last_window_entry = { - "last-window", "last", - "t:", 0, 0, - CMD_TARGET_SESSION_USAGE, - 0, - cmd_select_window_exec + .name = "last-window", + .alias = "last", + + .args = { "t:", 0, 0 }, + .usage = CMD_TARGET_SESSION_USAGE, + + .tflag = CMD_SESSION, + + .flags = 0, + .exec = cmd_select_window_exec }; enum cmd_retval cmd_select_window_exec(struct cmd *self, struct cmd_q *cmdq) { - struct args *args = self->args; - struct winlink *wl; - struct session *s; + struct winlink *wl = cmdq->state.tflag.wl; + struct session *s = cmdq->state.tflag.s; int next, previous, last, activity; next = self->entry == &cmd_next_window_entry; @@ -79,10 +98,6 @@ cmd_select_window_exec(struct cmd *self, struct cmd_q *cmdq) last = 1; if (next || previous || last) { - s = cmd_find_session(cmdq, args_get(args, 't'), 0); - if (s == NULL) - return (CMD_RETURN_ERROR); - activity = args_has(self->args, 'a'); if (next) { if (session_next(s, activity) != 0) { @@ -103,10 +118,6 @@ cmd_select_window_exec(struct cmd *self, struct cmd_q *cmdq) server_redraw_session(s); } else { - wl = cmd_find_window(cmdq, args_get(args, 't'), &s); - if (wl == NULL) - return (CMD_RETURN_ERROR); - /* * If -T and select-window is invoked on same window as * current, switch to previous window. diff --git a/cmd-send-keys.c b/cmd-send-keys.c index 7a4d97d5..92c75ec3 100644 --- a/cmd-send-keys.c +++ b/cmd-send-keys.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -30,68 +30,76 @@ enum cmd_retval cmd_send_keys_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_send_keys_entry = { - "send-keys", "send", - "lRt:", 0, -1, - "[-lR] " CMD_TARGET_PANE_USAGE " key ...", - 0, - cmd_send_keys_exec + .name = "send-keys", + .alias = "send", + + .args = { "lRMt:", 0, -1 }, + .usage = "[-lRM] " CMD_TARGET_PANE_USAGE " key ...", + + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_send_keys_exec }; const struct cmd_entry cmd_send_prefix_entry = { - "send-prefix", NULL, - "2t:", 0, 0, - "[-2] " CMD_TARGET_PANE_USAGE, - 0, - cmd_send_keys_exec + .name = "send-prefix", + .alias = NULL, + + .args = { "2t:", 0, 0 }, + .usage = "[-2] " CMD_TARGET_PANE_USAGE, + + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_send_keys_exec }; enum cmd_retval cmd_send_keys_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct window_pane *wp; - struct session *s; - struct input_ctx *ictx; - const u_char *str; - int i, key; + struct window_pane *wp = cmdq->state.tflag.wp; + struct session *s = cmdq->state.tflag.s; + struct mouse_event *m = &cmdq->item->mouse; + const u_char *keystr; + int i, literal; + key_code key; - if (cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp) == NULL) - return (CMD_RETURN_ERROR); - - if (self->entry == &cmd_send_prefix_entry) { - if (args_has(args, '2')) - key = options_get_number(&s->options, "prefix2"); - else - key = options_get_number(&s->options, "prefix"); - window_pane_key(wp, s, key); + if (args_has(args, 'M')) { + wp = cmd_mouse_pane(m, &s, NULL); + if (wp == NULL) { + cmdq_error(cmdq, "no mouse target"); + return (CMD_RETURN_ERROR); + } + window_pane_key(wp, NULL, s, m->key, m); return (CMD_RETURN_NORMAL); } - if (args_has(args, 'R')) { - ictx = &wp->ictx; - - memcpy(&ictx->cell, &grid_default_cell, sizeof ictx->cell); - memcpy(&ictx->old_cell, &ictx->cell, sizeof ictx->old_cell); - ictx->old_cx = 0; - ictx->old_cy = 0; - - if (wp->mode == NULL) - screen_write_start(&ictx->ctx, wp, &wp->base); + if (self->entry == &cmd_send_prefix_entry) { + if (args_has(args, '2')) + key = options_get_number(s->options, "prefix2"); else - screen_write_start(&ictx->ctx, NULL, &wp->base); - screen_write_reset(&ictx->ctx); - screen_write_stop(&ictx->ctx); + key = options_get_number(s->options, "prefix"); + window_pane_key(wp, NULL, s, key, NULL); + return (CMD_RETURN_NORMAL); } - for (i = 0; i < args->argc; i++) { - str = args->argv[i]; + if (args_has(args, 'R')) + input_reset(wp, 1); - if (!args_has(args, 'l') && - (key = key_string_lookup_string(str)) != KEYC_NONE) { - window_pane_key(wp, s, key); - } else { - for (; *str != '\0'; str++) - window_pane_key(wp, s, *str); + for (i = 0; i < args->argc; i++) { + literal = args_has(args, 'l'); + if (!literal) { + key = key_string_lookup_string(args->argv[i]); + if (key != KEYC_NONE && key != KEYC_UNKNOWN) + window_pane_key(wp, NULL, s, key, NULL); + else + literal = 1; + } + if (literal) { + for (keystr = args->argv[i]; *keystr != '\0'; keystr++) + window_pane_key(wp, NULL, s, *keystr, NULL); } } diff --git a/cmd-set-buffer.c b/cmd-set-buffer.c index 0ec362b3..1f0cf3d8 100644 --- a/cmd-set-buffer.c +++ b/cmd-set-buffer.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -24,17 +24,31 @@ #include "tmux.h" /* - * Add, set, or append to a paste buffer. + * Add, set, append to or delete a paste buffer. */ enum cmd_retval cmd_set_buffer_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_set_buffer_entry = { - "set-buffer", "setb", - "ab:n:", 0, 1, - "[-a] " CMD_BUFFER_USAGE " [-n new-buffer-name] data", - 0, - cmd_set_buffer_exec + .name = "set-buffer", + .alias = "setb", + + .args = { "ab:n:", 0, 1 }, + .usage = "[-a] " CMD_BUFFER_USAGE " [-n new-buffer-name] data", + + .flags = 0, + .exec = cmd_set_buffer_exec +}; + +const struct cmd_entry cmd_delete_buffer_entry = { + .name = "delete-buffer", + .alias = "deleteb", + + .args = { "b:", 0, 0 }, + .usage = CMD_BUFFER_USAGE, + + .flags = 0, + .exec = cmd_set_buffer_exec }; enum cmd_retval @@ -42,36 +56,39 @@ cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct paste_buffer *pb; - char *pdata, *cause; - const char *bufname; - size_t psize, newsize; + char *bufdata, *cause; + const char *bufname, *olddata; + size_t bufsize, newsize; - bufname = NULL; + bufname = args_get(args, 'b'); + if (bufname == NULL) + pb = NULL; + else + pb = paste_get_name(bufname); - if (args_has(args, 'n')) { - if (args->argc > 0) { - cmdq_error(cmdq, "don't provide data with n flag"); + if (self->entry == &cmd_delete_buffer_entry) { + if (pb == NULL) + pb = paste_get_top(&bufname); + if (pb == NULL) { + cmdq_error(cmdq, "no buffer"); return (CMD_RETURN_ERROR); } + paste_free(pb); + return (CMD_RETURN_NORMAL); + } - if (args_has(args, 'b')) - bufname = args_get(args, 'b'); - - if (bufname == NULL) { - pb = paste_get_top(); - if (pb == NULL) { - cmdq_error(cmdq, "no buffer"); - return (CMD_RETURN_ERROR); - } - bufname = pb->name; + if (args_has(args, 'n')) { + if (pb == NULL) + pb = paste_get_top(&bufname); + if (pb == NULL) { + cmdq_error(cmdq, "no buffer"); + return (CMD_RETURN_ERROR); } - if (paste_rename(bufname, args_get(args, 'n'), &cause) != 0) { cmdq_error(cmdq, "%s", cause); free(cause); return (CMD_RETURN_ERROR); } - return (CMD_RETURN_NORMAL); } @@ -79,37 +96,25 @@ cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq) cmdq_error(cmdq, "no data specified"); return (CMD_RETURN_ERROR); } - - psize = 0; - pdata = NULL; - - pb = NULL; - if ((newsize = strlen(args->argv[0])) == 0) return (CMD_RETURN_NORMAL); - if (args_has(args, 'b')) { - bufname = args_get(args, 'b'); - pb = paste_get_name(bufname); - } else if (args_has(args, 'a')) { - pb = paste_get_top(); - if (pb != NULL) - bufname = pb->name; - } + bufsize = 0; + bufdata = NULL; if (args_has(args, 'a') && pb != NULL) { - psize = pb->size; - pdata = xmalloc(psize); - memcpy(pdata, pb->data, psize); + olddata = paste_buffer_data(pb, &bufsize); + bufdata = xmalloc(bufsize); + memcpy(bufdata, olddata, bufsize); } - pdata = xrealloc(pdata, psize + newsize); - memcpy(pdata + psize, args->argv[0], newsize); - psize += newsize; + bufdata = xrealloc(bufdata, bufsize + newsize); + memcpy(bufdata + bufsize, args->argv[0], newsize); + bufsize += newsize; - if (paste_set(pdata, psize, bufname, &cause) != 0) { + if (paste_set(bufdata, bufsize, bufname, &cause) != 0) { cmdq_error(cmdq, "%s", cause); - free(pdata); + free(bufdata); free(cause); return (CMD_RETURN_ERROR); } diff --git a/cmd-set-environment.c b/cmd-set-environment.c index 83e63b48..ba295ea6 100644 --- a/cmd-set-environment.c +++ b/cmd-set-environment.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -30,20 +30,24 @@ enum cmd_retval cmd_set_environment_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_set_environment_entry = { - "set-environment", "setenv", - "grt:u", 1, 2, - "[-gru] " CMD_TARGET_SESSION_USAGE " name [value]", - 0, - cmd_set_environment_exec + .name = "set-environment", + .alias = "setenv", + + .args = { "grt:u", 1, 2 }, + .usage = "[-gru] " CMD_TARGET_SESSION_USAGE " name [value]", + + .tflag = CMD_SESSION_CANFAIL, + + .flags = 0, + .exec = cmd_set_environment_exec }; enum cmd_retval cmd_set_environment_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct session *s; struct environ *env; - const char *name, *value; + const char *name, *value, *target; name = args->argv[0]; if (*name == '\0') { @@ -61,11 +65,17 @@ cmd_set_environment_exec(struct cmd *self, struct cmd_q *cmdq) value = args->argv[1]; if (args_has(self->args, 'g')) - env = &global_environ; + env = global_environ; else { - if ((s = cmd_find_session(cmdq, args_get(args, 't'), 0)) == NULL) + if (cmdq->state.tflag.s == NULL) { + target = args_get(args, 't'); + if (target != NULL) + cmdq_error(cmdq, "no such session: %s", target); + else + cmdq_error(cmdq, "no current session"); return (CMD_RETURN_ERROR); - env = &s->environ; + } + env = cmdq->state.tflag.s->environ; } if (args_has(self->args, 'u')) { @@ -79,13 +89,13 @@ cmd_set_environment_exec(struct cmd *self, struct cmd_q *cmdq) cmdq_error(cmdq, "can't specify a value with -r"); return (CMD_RETURN_ERROR); } - environ_set(env, name, NULL); + environ_clear(env, name); } else { if (value == NULL) { cmdq_error(cmdq, "no value specified"); return (CMD_RETURN_ERROR); } - environ_set(env, name, value); + environ_set(env, name, "%s", value); } return (CMD_RETURN_NORMAL); diff --git a/cmd-set-hook.c b/cmd-set-hook.c new file mode 100644 index 00000000..8ef02f8c --- /dev/null +++ b/cmd-set-hook.c @@ -0,0 +1,120 @@ +/* $OpenBSD$ */ + +/* + * Copyright (c) 2012 Thomas Adam + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include +#include + +#include "tmux.h" + +/* + * Set or show global or session hooks. + */ + +enum cmd_retval cmd_set_hook_exec(struct cmd *, struct cmd_q *); + +const struct cmd_entry cmd_set_hook_entry = { + .name = "set-hook", + .alias = NULL, + + .args = { "gt:u", 1, 2 }, + .usage = "[-gu] " CMD_TARGET_SESSION_USAGE " hook-name [command]", + + .tflag = CMD_SESSION, + + .flags = 0, + .exec = cmd_set_hook_exec +}; + +const struct cmd_entry cmd_show_hooks_entry = { + .name = "show-hooks", + .alias = NULL, + + .args = { "gt:", 0, 1 }, + .usage = "[-g] " CMD_TARGET_SESSION_USAGE, + + .tflag = CMD_SESSION, + + .flags = 0, + .exec = cmd_set_hook_exec +}; + +enum cmd_retval +cmd_set_hook_exec(struct cmd *self, struct cmd_q *cmdq) +{ + struct args *args = self->args; + struct cmd_list *cmdlist; + struct hooks *hooks; + struct hook *hook; + char *cause, *tmp; + const char *name, *cmd; + + if (args_has(args, 'g')) + hooks = global_hooks; + else + hooks = cmdq->state.tflag.s->hooks; + + if (self->entry == &cmd_show_hooks_entry) { + hook = hooks_first(hooks); + while (hook != NULL) { + tmp = cmd_list_print(hook->cmdlist); + cmdq_print(cmdq, "%s -> %s", hook->name, tmp); + free(tmp); + + hook = hooks_next(hook); + } + return (CMD_RETURN_NORMAL); + } + + name = args->argv[0]; + if (*name == '\0') { + cmdq_error(cmdq, "invalid hook name"); + return (CMD_RETURN_ERROR); + } + if (args->argc < 2) + cmd = NULL; + else + cmd = args->argv[1]; + + if (args_has(args, 'u')) { + if (cmd != NULL) { + cmdq_error(cmdq, "command passed to unset hook: %s", + name); + return (CMD_RETURN_ERROR); + } + hooks_remove(hooks, name); + return (CMD_RETURN_NORMAL); + } + + if (cmd == NULL) { + cmdq_error(cmdq, "no command to set hook: %s", name); + return (CMD_RETURN_ERROR); + } + if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) { + if (cause != NULL) { + cmdq_error(cmdq, "%s", cause); + free(cause); + } + return (CMD_RETURN_ERROR); + } + hooks_add(hooks, name, cmdlist); + cmd_list_free(cmdlist); + + return (CMD_RETURN_NORMAL); +} diff --git a/cmd-set-option.c b/cmd-set-option.c index 01d691d5..b1771436 100644 --- a/cmd-set-option.c +++ b/cmd-set-option.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -65,33 +65,42 @@ struct options_entry *cmd_set_option_style(struct cmd *, struct cmd_q *, const char *); const struct cmd_entry cmd_set_option_entry = { - "set-option", "set", - "agoqst:uw", 1, 2, - "[-agosquw] [-t target-session|target-window] option [value]", - 0, - cmd_set_option_exec + .name = "set-option", + .alias = "set", + + .args = { "agoqst:uw", 1, 2 }, + .usage = "[-agosquw] [-t target-window] option [value]", + + .tflag = CMD_WINDOW_CANFAIL, + + .flags = 0, + .exec = cmd_set_option_exec }; const struct cmd_entry cmd_set_window_option_entry = { - "set-window-option", "setw", - "agoqt:u", 1, 2, - "[-agoqu] " CMD_TARGET_WINDOW_USAGE " option [value]", - 0, - cmd_set_option_exec + .name = "set-window-option", + .alias = "setw", + + .args = { "agoqt:u", 1, 2 }, + .usage = "[-agoqu] " CMD_TARGET_WINDOW_USAGE " option [value]", + + .tflag = CMD_WINDOW_CANFAIL, + + .flags = 0, + .exec = cmd_set_option_exec }; enum cmd_retval cmd_set_option_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - const struct options_table_entry *table, *oe; - struct session *s; - struct winlink *wl; - struct client *c; - struct options *oo; + struct session *s = cmdq->state.tflag.s; + struct winlink *wl = cmdq->state.tflag.wl; struct window *w; - const char *optstr, *valstr; - u_int i; + struct client *c; + const struct options_table_entry *oe; + struct options *oo; + const char *optstr, *valstr, *target; /* Get the option name and value. */ optstr = args->argv[0]; @@ -109,10 +118,13 @@ cmd_set_option_exec(struct cmd *self, struct cmd_q *cmdq) return (cmd_set_option_user(self, cmdq, optstr, valstr)); /* Find the option entry, try each table. */ - table = oe = NULL; - if (options_table_find(optstr, &table, &oe) != 0) { - cmdq_error(cmdq, "ambiguous option: %s", optstr); - return (CMD_RETURN_ERROR); + oe = NULL; + if (options_table_find(optstr, &oe) != 0) { + if (!args_has(args, 'q')) { + cmdq_error(cmdq, "ambiguous option: %s", optstr); + return (CMD_RETURN_ERROR); + } + return (CMD_RETURN_NORMAL); } if (oe == NULL) { if (!args_has(args, 'q')) { @@ -122,37 +134,35 @@ cmd_set_option_exec(struct cmd *self, struct cmd_q *cmdq) return (CMD_RETURN_NORMAL); } - /* Work out the tree from the table. */ - if (table == server_options_table) - oo = &global_options; - else if (table == window_options_table) { + /* Work out the tree from the scope of the option. */ + if (oe->scope == OPTIONS_TABLE_SERVER) + oo = global_options; + else if (oe->scope == OPTIONS_TABLE_WINDOW) { if (args_has(self->args, 'g')) - oo = &global_w_options; - else { - wl = cmd_find_window(cmdq, args_get(args, 't'), NULL); - if (wl == NULL) { - cmdq_error(cmdq, - "couldn't set '%s'%s", optstr, - (!args_has(args, 't') && !args_has(args, - 'g')) ? " need target window or -g" : ""); - return (CMD_RETURN_ERROR); - } - oo = &wl->window->options; - } - } else if (table == session_options_table) { + oo = global_w_options; + else if (wl == NULL) { + target = args_get(args, 't'); + if (target != NULL) { + cmdq_error(cmdq, "no such window: %s", + target); + } else + cmdq_error(cmdq, "no current window"); + return (CMD_RETURN_ERROR); + } else + oo = wl->window->options; + } else if (oe->scope == OPTIONS_TABLE_SESSION) { if (args_has(self->args, 'g')) - oo = &global_s_options; - else { - s = cmd_find_session(cmdq, args_get(args, 't'), 0); - if (s == NULL) { - cmdq_error(cmdq, - "couldn't set '%s'%s", optstr, - (!args_has(args, 't') && !args_has(args, - 'g')) ? " need target session or -g" : ""); - return (CMD_RETURN_ERROR); - } - oo = &s->options; - } + oo = global_s_options; + else if (s == NULL) { + target = args_get(args, 't'); + if (target != NULL) { + cmdq_error(cmdq, "no such session: %s", + target); + } else + cmdq_error(cmdq, "no current session"); + return (CMD_RETURN_ERROR); + } else + oo = s->options; } else { cmdq_error(cmdq, "unknown table"); return (CMD_RETURN_ERROR); @@ -174,23 +184,27 @@ cmd_set_option_exec(struct cmd *self, struct cmd_q *cmdq) return (CMD_RETURN_ERROR); } - /* Start or stop timers when automatic-rename changed. */ + /* Start or stop timers if necessary. */ if (strcmp(oe->name, "automatic-rename") == 0) { - for (i = 0; i < ARRAY_LENGTH(&windows); i++) { - if ((w = ARRAY_ITEM(&windows, i)) == NULL) - continue; - if (options_get_number(&w->options, "automatic-rename")) - queue_window_name(w); - else if (event_initialized(&w->name_timer)) - evtimer_del(&w->name_timer); + RB_FOREACH(w, windows, &windows) { + if (options_get_number(w->options, "automatic-rename")) + w->active->flags |= PANE_CHANGED; } } + if (strcmp(oe->name, "key-table") == 0) { + TAILQ_FOREACH(c, &clients, entry) + server_client_set_key_table(c, NULL); + } + if (strcmp(oe->name, "status") == 0 || + strcmp(oe->name, "status-interval") == 0) + status_timer_start_all(); + if (strcmp(oe->name, "monitor-silence") == 0) + alerts_reset_all(); /* Update sizes and redraw. May not need it but meh. */ recalculate_sizes(); - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c != NULL && c->session != NULL) + TAILQ_FOREACH(c, &clients, entry) { + if (c->session != NULL) server_redraw_client(c); } @@ -203,31 +217,23 @@ cmd_set_option_user(struct cmd *self, struct cmd_q *cmdq, const char *optstr, const char *valstr) { struct args *args = self->args; - struct session *s; - struct winlink *wl; + struct session *s = cmdq->state.tflag.s; + struct winlink *wl = cmdq->state.tflag.wl; struct options *oo; if (args_has(args, 's')) - oo = &global_options; + oo = global_options; else if (args_has(self->args, 'w') || self->entry == &cmd_set_window_option_entry) { if (args_has(self->args, 'g')) - oo = &global_w_options; - else { - wl = cmd_find_window(cmdq, args_get(args, 't'), NULL); - if (wl == NULL) - return (CMD_RETURN_ERROR); - oo = &wl->window->options; - } + oo = global_w_options; + else + oo = wl->window->options; } else { if (args_has(self->args, 'g')) - oo = &global_s_options; - else { - s = cmd_find_session(cmdq, args_get(args, 't'), 0); - if (s == NULL) - return (CMD_RETURN_ERROR); - oo = &s->options; - } + oo = global_s_options; + else + oo = s->options; } if (args_has(args, 'u')) { @@ -261,7 +267,6 @@ cmd_set_option_user(struct cmd *self, struct cmd_q *cmdq, const char *optstr, return (CMD_RETURN_NORMAL); } - /* Unset an option. */ int cmd_set_option_unset(struct cmd *self, struct cmd_q *cmdq, @@ -270,16 +275,25 @@ cmd_set_option_unset(struct cmd *self, struct cmd_q *cmdq, { struct args *args = self->args; - if (args_has(args, 'g')) { - cmdq_error(cmdq, "can't unset global option: %s", oe->name); - return (-1); - } if (value != NULL) { cmdq_error(cmdq, "value passed to unset option: %s", oe->name); return (-1); } - options_remove(oo, oe->name); + if (args_has(args, 'g') || oo == global_options) { + switch (oe->type) { + case OPTIONS_TABLE_STRING: + options_set_string(oo, oe->name, "%s", oe->default_str); + break; + case OPTIONS_TABLE_STYLE: + options_set_style(oo, oe->name, oe->default_str, 0); + break; + default: + options_set_number(oo, oe->name, oe->default_num); + break; + } + } else + options_remove(oo, oe->name); return (0); } @@ -291,9 +305,15 @@ cmd_set_option_set(struct cmd *self, struct cmd_q *cmdq, { struct options_entry *o; - if (oe->type != OPTIONS_TABLE_FLAG && value == NULL) { - cmdq_error(cmdq, "empty value"); - return (-1); + switch (oe->type) { + case OPTIONS_TABLE_FLAG: + case OPTIONS_TABLE_CHOICE: + break; + default: + if (value == NULL) { + cmdq_error(cmdq, "empty value"); + return (-1); + } } o = NULL; @@ -334,7 +354,7 @@ cmd_set_option_set(struct cmd *self, struct cmd_q *cmdq, /* Set a string option. */ struct options_entry * -cmd_set_option_string(struct cmd *self, unused struct cmd_q *cmdq, +cmd_set_option_string(struct cmd *self, __unused struct cmd_q *cmdq, const struct options_table_entry *oe, struct options *oo, const char *value) { @@ -356,7 +376,7 @@ cmd_set_option_string(struct cmd *self, unused struct cmd_q *cmdq, /* Set a number option. */ struct options_entry * -cmd_set_option_number(unused struct cmd *self, struct cmd_q *cmdq, +cmd_set_option_number(__unused struct cmd *self, struct cmd_q *cmdq, const struct options_table_entry *oe, struct options *oo, const char *value) { @@ -374,13 +394,14 @@ cmd_set_option_number(unused struct cmd *self, struct cmd_q *cmdq, /* Set a key option. */ struct options_entry * -cmd_set_option_key(unused struct cmd *self, struct cmd_q *cmdq, +cmd_set_option_key(__unused struct cmd *self, struct cmd_q *cmdq, const struct options_table_entry *oe, struct options *oo, const char *value) { - int key; + key_code key; - if ((key = key_string_lookup_string(value)) == KEYC_NONE) { + key = key_string_lookup_string(value); + if (key == KEYC_UNKNOWN) { cmdq_error(cmdq, "bad key: %s", value); return (NULL); } @@ -390,7 +411,7 @@ cmd_set_option_key(unused struct cmd *self, struct cmd_q *cmdq, /* Set a colour option. */ struct options_entry * -cmd_set_option_colour(unused struct cmd *self, struct cmd_q *cmdq, +cmd_set_option_colour(__unused struct cmd *self, struct cmd_q *cmdq, const struct options_table_entry *oe, struct options *oo, const char *value) { @@ -406,7 +427,7 @@ cmd_set_option_colour(unused struct cmd *self, struct cmd_q *cmdq, /* Set an attributes option. */ struct options_entry * -cmd_set_option_attributes(unused struct cmd *self, struct cmd_q *cmdq, +cmd_set_option_attributes(__unused struct cmd *self, struct cmd_q *cmdq, const struct options_table_entry *oe, struct options *oo, const char *value) { @@ -422,7 +443,7 @@ cmd_set_option_attributes(unused struct cmd *self, struct cmd_q *cmdq, /* Set a flag option. */ struct options_entry * -cmd_set_option_flag(unused struct cmd *self, struct cmd_q *cmdq, +cmd_set_option_flag(__unused struct cmd *self, struct cmd_q *cmdq, const struct options_table_entry *oe, struct options *oo, const char *value) { @@ -450,28 +471,34 @@ cmd_set_option_flag(unused struct cmd *self, struct cmd_q *cmdq, /* Set a choice option. */ struct options_entry * -cmd_set_option_choice(unused struct cmd *self, struct cmd_q *cmdq, +cmd_set_option_choice(__unused struct cmd *self, struct cmd_q *cmdq, const struct options_table_entry *oe, struct options *oo, const char *value) { const char **choicep; int n, choice = -1; - n = 0; - for (choicep = oe->choices; *choicep != NULL; choicep++) { - n++; - if (strncmp(*choicep, value, strlen(value)) != 0) - continue; + if (value == NULL) { + choice = options_get_number(oo, oe->name); + if (choice < 2) + choice = !choice; + } else { + n = 0; + for (choicep = oe->choices; *choicep != NULL; choicep++) { + n++; + if (strncmp(*choicep, value, strlen(value)) != 0) + continue; - if (choice != -1) { - cmdq_error(cmdq, "ambiguous value: %s", value); + if (choice != -1) { + cmdq_error(cmdq, "ambiguous value: %s", value); + return (NULL); + } + choice = n - 1; + } + if (choice == -1) { + cmdq_error(cmdq, "unknown value: %s", value); return (NULL); } - choice = n - 1; - } - if (choice == -1) { - cmdq_error(cmdq, "unknown value: %s", value); - return (NULL); } return (options_set_number(oo, oe->name, choice)); diff --git a/cmd-show-environment.c b/cmd-show-environment.c index 7737752f..29e89274 100644 --- a/cmd-show-environment.c +++ b/cmd-show-environment.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -27,30 +27,93 @@ * Show environment. */ -enum cmd_retval cmd_show_environment_exec(struct cmd *, struct cmd_q *); +enum cmd_retval cmd_show_environment_exec(struct cmd *, struct cmd_q *); + +char *cmd_show_environment_escape(struct environ_entry *); +void cmd_show_environment_print(struct cmd *, struct cmd_q *, + struct environ_entry *); const struct cmd_entry cmd_show_environment_entry = { - "show-environment", "showenv", - "gt:", 0, 1, - "[-g] " CMD_TARGET_SESSION_USAGE " [name]", - 0, - cmd_show_environment_exec + .name = "show-environment", + .alias = "showenv", + + .args = { "gst:", 0, 1 }, + .usage = "[-gs] " CMD_TARGET_SESSION_USAGE " [name]", + + .tflag = CMD_SESSION_CANFAIL, + + .flags = 0, + .exec = cmd_show_environment_exec }; +char * +cmd_show_environment_escape(struct environ_entry *envent) +{ + const char *value = envent->value; + char c, *out, *ret; + + out = ret = xmalloc(strlen(value) * 2 + 1); /* at most twice the size */ + while ((c = *value++) != '\0') { + /* POSIX interprets $ ` " and \ in double quotes. */ + if (c == '$' || c == '`' || c == '"' || c == '\\') + *out++ = '\\'; + *out++ = c; + } + *out = '\0'; + + return (ret); +} + +void +cmd_show_environment_print(struct cmd *self, struct cmd_q *cmdq, + struct environ_entry *envent) +{ + char *escaped; + + if (!args_has(self->args, 's')) { + if (envent->value != NULL) + cmdq_print(cmdq, "%s=%s", envent->name, envent->value); + else + cmdq_print(cmdq, "-%s", envent->name); + return; + } + + if (envent->value != NULL) { + escaped = cmd_show_environment_escape(envent); + cmdq_print(cmdq, "%s=\"%s\"; export %s;", envent->name, escaped, + envent->name); + free(escaped); + } else + cmdq_print(cmdq, "unset %s;", envent->name); +} + enum cmd_retval cmd_show_environment_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct session *s; struct environ *env; struct environ_entry *envent; + const char *target; + + if ((target = args_get(args, 't')) != NULL) { + if (cmdq->state.tflag.s == NULL) { + cmdq_error(cmdq, "no such session: %s", target); + return (CMD_RETURN_ERROR); + } + } if (args_has(self->args, 'g')) - env = &global_environ; + env = global_environ; else { - if ((s = cmd_find_session(cmdq, args_get(args, 't'), 0)) == NULL) + if (cmdq->state.tflag.s == NULL) { + target = args_get(args, 't'); + if (target != NULL) + cmdq_error(cmdq, "no such session: %s", target); + else + cmdq_error(cmdq, "no current session"); return (CMD_RETURN_ERROR); - env = &s->environ; + } + env = cmdq->state.tflag.s->environ; } if (args->argc != 0) { @@ -59,19 +122,14 @@ cmd_show_environment_exec(struct cmd *self, struct cmd_q *cmdq) cmdq_error(cmdq, "unknown variable: %s", args->argv[0]); return (CMD_RETURN_ERROR); } - if (envent->value != NULL) - cmdq_print(cmdq, "%s=%s", envent->name, envent->value); - else - cmdq_print(cmdq, "-%s", envent->name); + cmd_show_environment_print(self, cmdq, envent); return (CMD_RETURN_NORMAL); } - RB_FOREACH(envent, environ, env) { - if (envent->value != NULL) - cmdq_print(cmdq, "%s=%s", envent->name, envent->value); - else - cmdq_print(cmdq, "-%s", envent->name); + envent = environ_first(env); + while (envent != NULL) { + cmd_show_environment_print(self, cmdq, envent); + envent = environ_next(envent); } - return (CMD_RETURN_NORMAL); } diff --git a/cmd-show-messages.c b/cmd-show-messages.c index 308668f1..fa796eed 100644 --- a/cmd-show-messages.c +++ b/cmd-show-messages.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -31,133 +31,94 @@ enum cmd_retval cmd_show_messages_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_show_messages_entry = { - "show-messages", "showmsgs", - "IJTt:", 0, 0, - "[-IJT] " CMD_TARGET_CLIENT_USAGE, - 0, - cmd_show_messages_exec + .name = "show-messages", + .alias = "showmsgs", + + .args = { "JTt:", 0, 0 }, + .usage = "[-JT] " CMD_TARGET_CLIENT_USAGE, + + .tflag = CMD_CLIENT, + + .flags = 0, + .exec = cmd_show_messages_exec }; const struct cmd_entry cmd_server_info_entry = { - "server-info", "info", - "", 0, 0, - "", - 0, - cmd_show_messages_exec + .name = "server-info", + .alias = "info", + + .args = { "", 0, 0 }, + .usage = "", + + .flags = 0, + .exec = cmd_show_messages_exec }; -void cmd_show_messages_server(struct cmd_q *); -void cmd_show_messages_terminals(struct cmd_q *); -void cmd_show_messages_jobs(struct cmd_q *); +int cmd_show_messages_terminals(struct cmd_q *, int); +int cmd_show_messages_jobs(struct cmd_q *, int); -void -cmd_show_messages_server(struct cmd_q *cmdq) +int +cmd_show_messages_terminals(struct cmd_q *cmdq, int blank) { - char *tim; - - tim = ctime(&start_time); - *strchr(tim, '\n') = '\0'; - - cmdq_print(cmdq, "started %s", tim); - cmdq_print(cmdq, "socket path %s", socket_path); - cmdq_print(cmdq, "debug level %d", debug_level); - cmdq_print(cmdq, "protocol version %d", PROTOCOL_VERSION); -} - -void -cmd_show_messages_terminals(struct cmd_q *cmdq) -{ - struct tty_term *term; - const struct tty_term_code_entry *ent; - struct tty_code *code; - u_int i, n; - char out[80]; + struct tty_term *term; + u_int i, n; n = 0; LIST_FOREACH(term, &tty_terms, entry) { - cmdq_print(cmdq, - "Terminal %u: %s [references=%u, flags=0x%x]:", + if (blank) { + cmdq_print(cmdq, "%s", ""); + blank = 0; + } + cmdq_print(cmdq, "Terminal %u: %s [references=%u, flags=0x%x]:", n, term->name, term->references, term->flags); n++; - for (i = 0; i < NTTYCODE; i++) { - ent = &tty_term_codes[i]; - code = &term->codes[ent->code]; - switch (code->type) { - case TTYCODE_NONE: - cmdq_print(cmdq, "%4u: %s: [missing]", - ent->code, ent->name); - break; - case TTYCODE_STRING: - strnvis(out, code->value.string, sizeof out, - VIS_OCTAL|VIS_TAB|VIS_NL); - cmdq_print(cmdq, "%4u: %s: (string) %s", - ent->code, ent->name, out); - break; - case TTYCODE_NUMBER: - cmdq_print(cmdq, "%4u: %s: (number) %d", - ent->code, ent->name, code->value.number); - break; - case TTYCODE_FLAG: - cmdq_print(cmdq, "%4u: %s: (flag) %s", - ent->code, ent->name, - code->value.flag ? "true" : "false"); - break; - } - } + for (i = 0; i < tty_term_ncodes(); i++) + cmdq_print(cmdq, "%s", tty_term_describe(term, i)); } + return (n != 0); } -void -cmd_show_messages_jobs(struct cmd_q *cmdq) +int +cmd_show_messages_jobs(struct cmd_q *cmdq, int blank) { struct job *job; u_int n; n = 0; LIST_FOREACH(job, &all_jobs, lentry) { - cmdq_print(cmdq, - "Job %u: %s [fd=%d, pid=%d, status=%d]", + if (blank) { + cmdq_print(cmdq, "%s", ""); + blank = 0; + } + cmdq_print(cmdq, "Job %u: %s [fd=%d, pid=%d, status=%d]", n, job->cmd, job->fd, job->pid, job->status); n++; } + return (n != 0); } enum cmd_retval cmd_show_messages_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct client *c; + struct client *c = cmdq->state.c; struct message_entry *msg; char *tim; - u_int i; - int done; + int done, blank; - done = 0; - if (args_has(args, 'I') || self->entry == &cmd_server_info_entry) { - cmd_show_messages_server(cmdq); - done = 1; - } + done = blank = 0; if (args_has(args, 'T') || self->entry == &cmd_server_info_entry) { - if (done) - cmdq_print(cmdq, "%s", ""); - cmd_show_messages_terminals(cmdq); + blank = cmd_show_messages_terminals(cmdq, blank); done = 1; } if (args_has(args, 'J') || self->entry == &cmd_server_info_entry) { - if (done) - cmdq_print(cmdq, "%s", ""); - cmd_show_messages_jobs(cmdq); + cmd_show_messages_jobs(cmdq, blank); done = 1; } if (done) return (CMD_RETURN_NORMAL); - if ((c = cmd_find_client(cmdq, args_get(args, 't'), 0)) == NULL) - return (CMD_RETURN_ERROR); - - for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) { - msg = &ARRAY_ITEM(&c->message_log, i); - + TAILQ_FOREACH(msg, &c->message_log, entry) { tim = ctime(&msg->msg_time); *strchr(tim, '\n') = '\0'; diff --git a/cmd-show-options.c b/cmd-show-options.c index a5011e72..322f532c 100644 --- a/cmd-show-options.c +++ b/cmd-show-options.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -32,63 +32,80 @@ enum cmd_retval cmd_show_options_exec(struct cmd *, struct cmd_q *); enum cmd_retval cmd_show_options_one(struct cmd *, struct cmd_q *, struct options *, int); enum cmd_retval cmd_show_options_all(struct cmd *, struct cmd_q *, - const struct options_table_entry *, struct options *); + struct options *, enum options_table_scope); const struct cmd_entry cmd_show_options_entry = { - "show-options", "show", - "gqst:vw", 0, 1, - "[-gqsvw] [-t target-session|target-window] [option]", - 0, - cmd_show_options_exec + .name = "show-options", + .alias = "show", + + .args = { "gqst:vw", 0, 1 }, + .usage = "[-gqsvw] [-t target-session|target-window] [option]", + + .tflag = CMD_WINDOW_CANFAIL, + + .flags = 0, + .exec = cmd_show_options_exec }; const struct cmd_entry cmd_show_window_options_entry = { - "show-window-options", "showw", - "gvt:", 0, 1, - "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]", - 0, - cmd_show_options_exec + .name = "show-window-options", + .alias = "showw", + + .args = { "gvt:", 0, 1 }, + .usage = "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]", + + .tflag = CMD_WINDOW_CANFAIL, + + .flags = 0, + .exec = cmd_show_options_exec }; enum cmd_retval cmd_show_options_exec(struct cmd *self, struct cmd_q *cmdq) { - struct args *args = self->args; - struct session *s; - struct winlink *wl; - const struct options_table_entry *table; - struct options *oo; - int quiet; + struct args *args = self->args; + struct session *s = cmdq->state.tflag.s; + struct winlink *wl = cmdq->state.tflag.wl; + struct options *oo; + enum options_table_scope scope; + int quiet; + const char *target; if (args_has(self->args, 's')) { - oo = &global_options; - table = server_options_table; + oo = global_options; + scope = OPTIONS_TABLE_SERVER; } else if (args_has(self->args, 'w') || self->entry == &cmd_show_window_options_entry) { - table = window_options_table; + scope = OPTIONS_TABLE_WINDOW; if (args_has(self->args, 'g')) - oo = &global_w_options; - else { - wl = cmd_find_window(cmdq, args_get(args, 't'), NULL); - if (wl == NULL) - return (CMD_RETURN_ERROR); - oo = &wl->window->options; - } + oo = global_w_options; + else if (wl == NULL) { + target = args_get(args, 't'); + if (target != NULL) { + cmdq_error(cmdq, "no such window: %s", target); + } else + cmdq_error(cmdq, "no current window"); + return (CMD_RETURN_ERROR); + } else + oo = wl->window->options; } else { - table = session_options_table; + scope = OPTIONS_TABLE_SESSION; if (args_has(self->args, 'g')) - oo = &global_s_options; - else { - s = cmd_find_session(cmdq, args_get(args, 't'), 0); - if (s == NULL) - return (CMD_RETURN_ERROR); - oo = &s->options; - } + oo = global_s_options; + else if (s == NULL) { + target = args_get(args, 't'); + if (target != NULL) { + cmdq_error(cmdq, "no such session: %s", target); + } else + cmdq_error(cmdq, "no current session"); + return (CMD_RETURN_ERROR); + } else + oo = s->options; } quiet = args_has(self->args, 'q'); if (args->argc == 0) - return (cmd_show_options_all(self, cmdq, table, oo)); + return (cmd_show_options_all(self, cmdq, oo, scope)); else return (cmd_show_options_one(self, cmdq, oo, quiet)); } @@ -99,7 +116,7 @@ cmd_show_options_one(struct cmd *self, struct cmd_q *cmdq, { struct args *args = self->args; const char *name = args->argv[0]; - const struct options_table_entry *table, *oe; + const struct options_table_entry *oe; struct options_entry *o; const char *optval; @@ -118,14 +135,14 @@ retry: return (CMD_RETURN_NORMAL); } - table = oe = NULL; - if (options_table_find(name, &table, &oe) != 0) { + oe = NULL; + if (options_table_find(name, &oe) != 0) { cmdq_error(cmdq, "ambiguous option: %s", name); return (CMD_RETURN_ERROR); } if (oe == NULL) { if (quiet) - return (CMD_RETURN_NORMAL); + return (CMD_RETURN_NORMAL); cmdq_error(cmdq, "unknown option: %s", name); return (CMD_RETURN_ERROR); } @@ -144,30 +161,33 @@ retry: } enum cmd_retval -cmd_show_options_all(struct cmd *self, struct cmd_q *cmdq, - const struct options_table_entry *table, struct options *oo) +cmd_show_options_all(struct cmd *self, struct cmd_q *cmdq, struct options *oo, + enum options_table_scope scope) { const struct options_table_entry *oe; struct options_entry *o; const char *optval; + int vflag; - RB_FOREACH(o, options_tree, &oo->tree) { + o = options_first(oo); + while (o != NULL) { if (*o->name == '@') { if (args_has(self->args, 'v')) cmdq_print(cmdq, "%s", o->str); else cmdq_print(cmdq, "%s \"%s\"", o->name, o->str); } + o = options_next(o); } - for (oe = table; oe->name != NULL; oe++) { - if (oe->style != NULL) + vflag = args_has(self->args, 'v'); + for (oe = options_table; oe->name != NULL; oe++) { + if (oe->style != NULL || oe->scope != scope) continue; if ((o = options_find1(oo, oe->name)) == NULL) continue; - optval = options_table_print_entry(oe, o, - args_has(self->args, 'v')); - if (args_has(self->args, 'v')) + optval = options_table_print_entry(oe, o, vflag); + if (vflag) cmdq_print(cmdq, "%s", optval); else cmdq_print(cmdq, "%s %s", oe->name, optval); diff --git a/cmd-source-file.c b/cmd-source-file.c index e5710a0c..9d2d6d68 100644 --- a/cmd-source-file.c +++ b/cmd-source-file.c @@ -31,11 +31,14 @@ enum cmd_retval cmd_source_file_exec(struct cmd *, struct cmd_q *); void cmd_source_file_done(struct cmd_q *); const struct cmd_entry cmd_source_file_entry = { - "source-file", "source", - "", 1, 1, - "path", - 0, - cmd_source_file_exec + .name = "source-file", + .alias = "source", + + .args = { "", 1, 1 }, + .usage = "path", + + .flags = 0, + .exec = cmd_source_file_exec }; enum cmd_retval @@ -45,8 +48,7 @@ cmd_source_file_exec(struct cmd *self, struct cmd_q *cmdq) struct cmd_q *cmdq1; char *cause; - cmdq1 = cmdq_new(NULL); - cmdq1->client = cmdq->client; + cmdq1 = cmdq_new(cmdq->client); cmdq1->emptyfn = cmd_source_file_done; cmdq1->data = cmdq; diff --git a/cmd-split-window.c b/cmd-split-window.c index 1e7bc16a..93e23cdd 100644 --- a/cmd-split-window.c +++ b/cmd-split-window.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -35,44 +35,46 @@ enum cmd_retval cmd_split_window_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_split_window_entry = { - "split-window", "splitw", - "bc:dF:l:hp:Pt:v", 0, -1, - "[-bdhvP] [-c start-directory] [-F format] [-p percentage|-l size] " - CMD_TARGET_PANE_USAGE " [command]", - 0, - cmd_split_window_exec + .name = "split-window", + .alias = "splitw", + + .args = { "bc:dF:l:hp:Pt:v", 0, -1 }, + .usage = "[-bdhvP] [-c start-directory] [-F format] " + "[-p percentage|-l size] " CMD_TARGET_PANE_USAGE " [command]", + + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_split_window_exec }; enum cmd_retval cmd_split_window_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct session *s; - struct winlink *wl; - struct window *w; - struct window_pane *wp, *new_wp = NULL; - struct environ env; - const char *cmd, *path, *shell, *template; + struct session *s = cmdq->state.tflag.s; + struct winlink *wl = cmdq->state.tflag.wl; + struct window *w = wl->window; + struct window_pane *wp = cmdq->state.tflag.wp, *new_wp = NULL; + struct environ *env; + const char *cmd, *path, *shell, *template, *cwd, *to_free; char **argv, *cause, *new_cause, *cp; u_int hlimit; - int argc, size, percentage, cwd, fd = -1; + int argc, size, percentage; enum layout_type type; struct layout_cell *lc; struct format_tree *ft; struct environ_entry *envent; - if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp)) == NULL) - return (CMD_RETURN_ERROR); - w = wl->window; server_unzoom_window(w); - environ_init(&env); - environ_copy(&global_environ, &env); - environ_copy(&s->environ, &env); - server_fill_environ(s, &env); + env = environ_create(); + environ_copy(global_environ, env); + environ_copy(s->environ, env); + server_fill_environ(s, env); if (args->argc == 0) { - cmd = options_get_string(&s->options, "default-command"); + cmd = options_get_string(s->options, "default-command"); if (cmd != NULL && *cmd != '\0') { argc = 1; argv = (char **)&cmd; @@ -85,24 +87,12 @@ cmd_split_window_exec(struct cmd *self, struct cmd_q *cmdq) argv = args->argv; } + to_free = NULL; if (args_has(args, 'c')) { - ft = format_create(); - format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, NULL, - NULL); - cp = format_expand(ft, args_get(args, 'c')); + ft = format_create(cmdq, 0); + format_defaults(ft, cmdq->state.c, s, NULL, NULL); + to_free = cwd = format_expand(ft, args_get(args, 'c')); format_free(ft); - - if (cp != NULL && *cp != '\0') { - fd = open(cp, O_RDONLY|O_DIRECTORY); - free(cp); - if (fd == -1) { - cmdq_error(cmdq, "bad working directory: %s", - strerror(errno)); - return (CMD_RETURN_ERROR); - } - } else if (cp != NULL) - free(cp); - cwd = fd; } else if (cmdq->client != NULL && cmdq->client->session == NULL) cwd = cmdq->client->cwd; else @@ -134,9 +124,9 @@ cmd_split_window_exec(struct cmd *self, struct cmd_q *cmdq) else size = (wp->sx * percentage) / 100; } - hlimit = options_get_number(&s->options, "history-limit"); + hlimit = options_get_number(s->options, "history-limit"); - shell = options_get_string(&s->options, "default-shell"); + shell = options_get_string(s->options, "default-shell"); if (*shell == '\0' || areshell(shell)) shell = _PATH_BSHELL; @@ -146,19 +136,19 @@ cmd_split_window_exec(struct cmd *self, struct cmd_q *cmdq) goto error; } new_wp = window_add_pane(w, hlimit); + layout_assign_pane(lc, new_wp); path = NULL; if (cmdq->client != NULL && cmdq->client->session == NULL) - envent = environ_find(&cmdq->client->environ, "PATH"); + envent = environ_find(cmdq->client->environ, "PATH"); else - envent = environ_find(&s->environ, "PATH"); + envent = environ_find(s->environ, "PATH"); if (envent != NULL) path = envent->value; - if (window_pane_spawn(new_wp, argc, argv, path, shell, cwd, &env, + if (window_pane_spawn(new_wp, argc, argv, path, shell, cwd, env, s->tio, &cause) != 0) goto error; - layout_assign_pane(lc, new_wp); server_redraw_window(w); @@ -169,15 +159,14 @@ cmd_split_window_exec(struct cmd *self, struct cmd_q *cmdq) } else server_status_session(s); - environ_free(&env); + environ_free(env); if (args_has(args, 'P')) { if ((template = args_get(args, 'F')) == NULL) template = SPLIT_WINDOW_TEMPLATE; - ft = format_create(); - format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, wl, - new_wp); + ft = format_create(cmdq, 0); + format_defaults(ft, cmdq->state.c, s, wl, new_wp); cp = format_expand(ft, template); cmdq_print(cmdq, "%s", cp); @@ -187,17 +176,20 @@ cmd_split_window_exec(struct cmd *self, struct cmd_q *cmdq) } notify_window_layout_changed(w); - if (fd != -1) - close(fd); + if (to_free != NULL) + free((void *)to_free); return (CMD_RETURN_NORMAL); error: - environ_free(&env); - if (new_wp != NULL) + environ_free(env); + if (new_wp != NULL) { + layout_close_pane(new_wp); window_remove_pane(w, new_wp); + } cmdq_error(cmdq, "create pane failed: %s", cause); free(cause); - if (fd != -1) - close(fd); + + if (to_free != NULL) + free((void *)to_free); return (CMD_RETURN_ERROR); } diff --git a/cmd-string.c b/cmd-string.c index db1723cb..fdef4a82 100644 --- a/cmd-string.c +++ b/cmd-string.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -126,7 +126,7 @@ cmd_string_parse(const char *s, struct cmd_list **cmdlist, const char *file, whitespace = argv[0] + strcspn(argv[0], " \t"); if (equals == NULL || equals > whitespace) break; - environ_put(&global_environ, argv[0]); + environ_put(global_environ, argv[0]); argc--; memmove(argv, argv + 1, argc * (sizeof *argv)); } @@ -303,10 +303,14 @@ cmd_string_variable(const char *s, size_t *p) buf = xrealloc(buf, len + 1); buf[len] = '\0'; - envent = environ_find(&global_environ, buf); + envent = environ_find(global_environ, buf); free(buf); if (envent == NULL) return (xstrdup("")); +#ifdef TMATE + if (envent->value == NULL) + return (xstrdup("")); +#endif return (xstrdup(envent->value)); error: @@ -326,7 +330,7 @@ cmd_string_expand_tilde(const char *s, size_t *p) last = cmd_string_getc(s, p); if (last == EOF || last == '/' || last == ' '|| last == '\t') { - envent = environ_find(&global_environ, "HOME"); + envent = environ_find(global_environ, "HOME"); if (envent != NULL && *envent->value != '\0') home = envent->value; else if ((pw = getpwuid(getuid())) != NULL) diff --git a/cmd-swap-pane.c b/cmd-swap-pane.c index 918a2e4f..3539c95c 100644 --- a/cmd-swap-pane.c +++ b/cmd-swap-pane.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -29,53 +29,53 @@ enum cmd_retval cmd_swap_pane_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_swap_pane_entry = { - "swap-pane", "swapp", - "dDs:t:U", 0, 0, - "[-dDU] " CMD_SRCDST_PANE_USAGE, - 0, - cmd_swap_pane_exec + .name = "swap-pane", + .alias = "swapp", + + .args = { "dDs:t:U", 0, 0 }, + .usage = "[-dDU] " CMD_SRCDST_PANE_USAGE, + + .sflag = CMD_PANE_MARKED, + .tflag = CMD_PANE, + + .flags = 0, + .exec = cmd_swap_pane_exec }; enum cmd_retval cmd_swap_pane_exec(struct cmd *self, struct cmd_q *cmdq) { - struct args *args = self->args; - struct winlink *src_wl, *dst_wl; struct window *src_w, *dst_w; struct window_pane *tmp_wp, *src_wp, *dst_wp; struct layout_cell *src_lc, *dst_lc; u_int sx, sy, xoff, yoff; - dst_wl = cmd_find_pane(cmdq, args_get(args, 't'), NULL, &dst_wp); - if (dst_wl == NULL) - return (CMD_RETURN_ERROR); - dst_w = dst_wl->window; + dst_w = cmdq->state.tflag.wl->window; + dst_wp = cmdq->state.tflag.wp; + src_w = cmdq->state.sflag.wl->window; + src_wp = cmdq->state.sflag.wp; server_unzoom_window(dst_w); - if (!args_has(args, 's')) { + if (args_has(self->args, 'D')) { src_w = dst_w; - if (args_has(self->args, 'D')) { - src_wp = TAILQ_NEXT(dst_wp, entry); - if (src_wp == NULL) - src_wp = TAILQ_FIRST(&dst_w->panes); - } else if (args_has(self->args, 'U')) { - src_wp = TAILQ_PREV(dst_wp, window_panes, entry); - if (src_wp == NULL) - src_wp = TAILQ_LAST(&dst_w->panes, window_panes); - } else { - src_wl = cmd_find_pane(cmdq, NULL, NULL, &src_wp); - if (src_wl == NULL) - return (CMD_RETURN_ERROR); - src_w = src_wl->window; - } - } else { - src_wl = cmd_find_pane(cmdq, args_get(args, 's'), NULL, &src_wp); - if (src_wl == NULL) - return (CMD_RETURN_ERROR); - src_w = src_wl->window; + src_wp = TAILQ_NEXT(dst_wp, entry); + if (src_wp == NULL) + src_wp = TAILQ_FIRST(&dst_w->panes); + } else if (args_has(self->args, 'U')) { + src_w = dst_w; + src_wp = TAILQ_PREV(dst_wp, window_panes, entry); + if (src_wp == NULL) + src_wp = TAILQ_LAST(&dst_w->panes, window_panes); } server_unzoom_window(src_w); +#ifdef TMATE + if (src_w != dst_w) { + cmdq_error(cmdq, "swap pane on different window is not supported with tmate"); + return (CMD_RETURN_ERROR); + } +#endif + if (src_wp == dst_wp) return (CMD_RETURN_NORMAL); diff --git a/cmd-swap-window.c b/cmd-swap-window.c index 655b910c..5830a31e 100644 --- a/cmd-swap-window.c +++ b/cmd-swap-window.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -29,34 +29,41 @@ enum cmd_retval cmd_swap_window_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_swap_window_entry = { - "swap-window", "swapw", - "ds:t:", 0, 0, - "[-d] " CMD_SRCDST_WINDOW_USAGE, - 0, - cmd_swap_window_exec + .name = "swap-window", + .alias = "swapw", + + .args = { "ds:t:", 0, 0 }, + .usage = "[-d] " CMD_SRCDST_WINDOW_USAGE, + + .sflag = CMD_WINDOW_MARKED, + .tflag = CMD_WINDOW, + + .flags = 0, + .exec = cmd_swap_window_exec }; enum cmd_retval cmd_swap_window_exec(struct cmd *self, struct cmd_q *cmdq) { - struct args *args = self->args; - const char *target_src, *target_dst; +#ifdef TMATE + cmdq_error(cmdq, "swap window is not supported with tmate"); + return (CMD_RETURN_ERROR); +#else struct session *src, *dst; struct session_group *sg_src, *sg_dst; struct winlink *wl_src, *wl_dst; struct window *w; - target_src = args_get(args, 's'); - if ((wl_src = cmd_find_window(cmdq, target_src, &src)) == NULL) - return (CMD_RETURN_ERROR); - target_dst = args_get(args, 't'); - if ((wl_dst = cmd_find_window(cmdq, target_dst, &dst)) == NULL) - return (CMD_RETURN_ERROR); - + wl_src = cmdq->state.sflag.wl; + src = cmdq->state.sflag.s; sg_src = session_group_find(src); + + wl_dst = cmdq->state.tflag.wl; + dst = cmdq->state.tflag.s; sg_dst = session_group_find(dst); - if (src != dst && - sg_src != NULL && sg_dst != NULL && sg_src == sg_dst) { + + if (src != dst && sg_src != NULL && sg_dst != NULL && + sg_src == sg_dst) { cmdq_error(cmdq, "can't move window, sessions are grouped"); return (CMD_RETURN_ERROR); } @@ -82,4 +89,5 @@ cmd_swap_window_exec(struct cmd *self, struct cmd_q *cmdq) recalculate_sizes(); return (CMD_RETURN_NORMAL); +#endif } diff --git a/cmd-switch-client.c b/cmd-switch-client.c index 439f593b..6e2ee2a0 100644 --- a/cmd-switch-client.c +++ b/cmd-switch-client.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -30,35 +30,47 @@ enum cmd_retval cmd_switch_client_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_switch_client_entry = { - "switch-client", "switchc", - "lc:npt:r", 0, 0, - "[-lnpr] [-c target-client] [-t target-session]", - CMD_READONLY, - cmd_switch_client_exec + .name = "switch-client", + .alias = "switchc", + + .args = { "lc:Enpt:rT:", 0, 0 }, + .usage = "[-Elnpr] [-c target-client] [-t target-session] " + "[-T key-table]", + + .cflag = CMD_CLIENT, + .tflag = CMD_SESSION_WITHPANE, + + .flags = CMD_READONLY, + .exec = cmd_switch_client_exec }; enum cmd_retval cmd_switch_client_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; - struct client *c; - struct session *s = NULL; - struct winlink *wl = NULL; - struct window *w = NULL; - struct window_pane *wp = NULL; - const char *tflag; + struct cmd_state *state = &cmdq->state; + struct client *c = state->c; + struct session *s = cmdq->state.tflag.s; + struct window_pane *wp; + const char *tablename, *update; + struct key_table *table; - if ((c = cmd_find_client(cmdq, args_get(args, 'c'), 0)) == NULL) - return (CMD_RETURN_ERROR); + if (args_has(args, 'r')) + c->flags ^= CLIENT_READONLY; - if (args_has(args, 'r')) { - if (c->flags & CLIENT_READONLY) - c->flags &= ~CLIENT_READONLY; - else - c->flags |= CLIENT_READONLY; + tablename = args_get(args, 'T'); + if (tablename != NULL) { + table = key_bindings_get_table(tablename, 0); + if (table == NULL) { + cmdq_error(cmdq, "table %s doesn't exist", tablename); + return (CMD_RETURN_ERROR); + } + table->references++; + key_bindings_unref_table(c->keytable); + c->keytable = table; + return (CMD_RETURN_NORMAL); } - tflag = args_get(args, 't'); if (args_has(args, 'n')) { if ((s = session_next_session(c->session)) == NULL) { cmdq_error(cmdq, "can't find next session"); @@ -72,47 +84,41 @@ cmd_switch_client_exec(struct cmd *self, struct cmd_q *cmdq) } else if (args_has(args, 'l')) { if (c->last_session != NULL && session_alive(c->last_session)) s = c->last_session; + else + s = NULL; if (s == NULL) { cmdq_error(cmdq, "can't find last session"); return (CMD_RETURN_ERROR); } } else { - if (tflag == NULL) { - if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) - return (CMD_RETURN_ERROR); - } else if (tflag[strcspn(tflag, ":.")] != '\0') { - if ((wl = cmd_find_pane(cmdq, tflag, &s, &wp)) == NULL) - return (CMD_RETURN_ERROR); - } else { - if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) - return (CMD_RETURN_ERROR); - w = cmd_lookup_windowid(tflag); - if (w == NULL && - (wp = cmd_lookup_paneid(tflag)) != NULL) - w = wp->window; - if (w != NULL) - wl = winlink_find_by_window(&s->windows, w); - } - if (cmdq->client == NULL) return (CMD_RETURN_NORMAL); - - if (wl != NULL) { + if (state->tflag.wl != NULL) { + wp = state->tflag.wp; if (wp != NULL) window_set_active_pane(wp->window, wp); - session_set_current(s, wl); + session_set_current(s, state->tflag.wl); } } - if (c->session != NULL) + if (c != NULL && !args_has(args, 'E')) { + update = options_get_string(s->options, "update-environment"); + environ_update(update, c->environ, s->environ); + } + + if (c->session != NULL && c->session != s) c->last_session = c->session; c->session = s; - session_update_activity(s); + server_client_set_key_table(c, NULL); + status_timer_start(c); + session_update_activity(s, NULL); + gettimeofday(&s->last_attached_time, NULL); recalculate_sizes(); server_check_unattached(); server_redraw_client(c); s->curw->flags &= ~WINLINK_ALERTFLAGS; + alerts_check_session(s); return (CMD_RETURN_NORMAL); } diff --git a/cmd-unbind-key.c b/cmd-unbind-key.c index 710210ce..7452fd9f 100644 --- a/cmd-unbind-key.c +++ b/cmd-unbind-key.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -26,23 +26,27 @@ * Unbind key from command. */ -enum cmd_retval cmd_unbind_key_exec(struct cmd *, struct cmd_q *); -enum cmd_retval cmd_unbind_key_mode_table(struct cmd *, struct cmd_q *, int); +enum cmd_retval cmd_unbind_key_exec(struct cmd *, struct cmd_q *); +enum cmd_retval cmd_unbind_key_mode_table(struct cmd *, struct cmd_q *, + key_code); const struct cmd_entry cmd_unbind_key_entry = { - "unbind-key", "unbind", - "acnt:", 0, 1, - "[-acn] [-t mode-table] key", - 0, - cmd_unbind_key_exec + .name = "unbind-key", + .alias = "unbind", + + .args = { "acnt:T:", 0, 1 }, + .usage = "[-acn] [-t mode-table] [-T key-table] key", + + .flags = 0, + .exec = cmd_unbind_key_exec }; enum cmd_retval cmd_unbind_key_exec(struct cmd *self, struct cmd_q *cmdq) { - struct args *args = self->args; - struct key_binding *bd; - int key; + struct args *args = self->args; + key_code key; + const char *tablename; if (!args_has(args, 'a')) { if (args->argc != 1) { @@ -50,7 +54,7 @@ cmd_unbind_key_exec(struct cmd *self, struct cmd_q *cmdq) return (CMD_RETURN_ERROR); } key = key_string_lookup_string(args->argv[0]); - if (key == KEYC_NONE) { + if (key == KEYC_NONE || key == KEYC_UNKNOWN) { cmdq_error(cmdq, "unknown key: %s", args->argv[0]); return (CMD_RETURN_ERROR); } @@ -59,28 +63,43 @@ cmd_unbind_key_exec(struct cmd *self, struct cmd_q *cmdq) cmdq_error(cmdq, "key given with -a"); return (CMD_RETURN_ERROR); } - key = KEYC_NONE; + key = KEYC_UNKNOWN; } if (args_has(args, 't')) return (cmd_unbind_key_mode_table(self, cmdq, key)); - if (key == KEYC_NONE) { - while (!RB_EMPTY(&key_bindings)) { - bd = RB_ROOT(&key_bindings); - key_bindings_remove(bd->key); + if (key == KEYC_UNKNOWN) { + tablename = args_get(args, 'T'); + if (tablename == NULL) { + key_bindings_remove_table("root"); + key_bindings_remove_table("prefix"); + return (CMD_RETURN_NORMAL); } + if (key_bindings_get_table(tablename, 0) == NULL) { + cmdq_error(cmdq, "table %s doesn't exist", tablename); + return (CMD_RETURN_ERROR); + } + key_bindings_remove_table(tablename); return (CMD_RETURN_NORMAL); } - if (!args_has(args, 'n')) - key |= KEYC_PREFIX; - key_bindings_remove(key); + if (args_has(args, 'T')) { + tablename = args_get(args, 'T'); + if (key_bindings_get_table(tablename, 0) == NULL) { + cmdq_error(cmdq, "table %s doesn't exist", tablename); + return (CMD_RETURN_ERROR); + } + } else if (args_has(args, 'n')) + tablename = "root"; + else + tablename = "prefix"; + key_bindings_remove(tablename, key); return (CMD_RETURN_NORMAL); } enum cmd_retval -cmd_unbind_key_mode_table(struct cmd *self, struct cmd_q *cmdq, int key) +cmd_unbind_key_mode_table(struct cmd *self, struct cmd_q *cmdq, key_code key) { struct args *args = self->args; const char *tablename; @@ -93,7 +112,7 @@ cmd_unbind_key_mode_table(struct cmd *self, struct cmd_q *cmdq, int key) return (CMD_RETURN_ERROR); } - if (key == KEYC_NONE) { + if (key == KEYC_UNKNOWN) { while (!RB_EMPTY(mtab->tree)) { mbind = RB_ROOT(mtab->tree); RB_REMOVE(mode_key_tree, mtab->tree, mbind); diff --git a/cmd-wait-for.c b/cmd-wait-for.c index a3e85856..e460599c 100644 --- a/cmd-wait-for.c +++ b/cmd-wait-for.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2013 Nicholas Marriott + * Copyright (c) 2013 Nicholas Marriott * Copyright (c) 2013 Thiago de Arruda * * Permission to use, copy, modify, and distribute this software for any @@ -23,6 +23,7 @@ #include #include "tmux.h" +#include "tmate.h" /* * Block or wake a client on a named wait channel. @@ -31,16 +32,20 @@ enum cmd_retval cmd_wait_for_exec(struct cmd *, struct cmd_q *); const struct cmd_entry cmd_wait_for_entry = { - "wait-for", "wait", - "LSU", 1, 1, - "[-L|-S|-U] channel", - 0, - cmd_wait_for_exec + .name = "wait-for", + .alias = "wait", + + .args = { "LSU", 1, 1 }, + .usage = "[-L|-S|-U] channel", + + .flags = 0, + .exec = cmd_wait_for_exec }; struct wait_channel { const char *name; int locked; + int woken; TAILQ_HEAD(, cmd_q) waiters; TAILQ_HEAD(, cmd_q) lockers; @@ -69,6 +74,46 @@ enum cmd_retval cmd_wait_for_lock(struct cmd_q *, const char *, enum cmd_retval cmd_wait_for_unlock(struct cmd_q *, const char *, struct wait_channel *); +struct wait_channel *cmd_wait_for_add(const char *); +void cmd_wait_for_remove(struct wait_channel *wc); + +struct wait_channel * +cmd_wait_for_add(const char *name) +{ + struct wait_channel *wc; + + wc = xmalloc(sizeof *wc); + wc->name = xstrdup(name); + + wc->locked = 0; + wc->woken = 0; + + TAILQ_INIT(&wc->waiters); + TAILQ_INIT(&wc->lockers); + + RB_INSERT(wait_channels, &wait_channels, wc); + + log_debug("add wait channel %s", wc->name); + + return (wc); +} + +void +cmd_wait_for_remove(struct wait_channel *wc) +{ + if (wc->locked) + return; + if (!TAILQ_EMPTY(&wc->waiters) || !wc->woken) + return; + + log_debug("remove wait channel %s", wc->name); + + RB_REMOVE(wait_channels, &wait_channels, wc); + + free((void *)wc->name); + free(wc); +} + enum cmd_retval cmd_wait_for_exec(struct cmd *self, struct cmd_q *cmdq) { @@ -89,14 +134,42 @@ cmd_wait_for_exec(struct cmd *self, struct cmd_q *cmdq) } enum cmd_retval -cmd_wait_for_signal(struct cmd_q *cmdq, const char *name, +cmd_wait_for_signal(__unused struct cmd_q *cmdq, const char *name, struct wait_channel *wc) { struct cmd_q *wq, *wq1; + if (wc == NULL) + wc = cmd_wait_for_add(name); + + if (TAILQ_EMPTY(&wc->waiters) && !wc->woken) { + log_debug("signal wait channel %s, no waiters", wc->name); + wc->woken = 1; + return (CMD_RETURN_NORMAL); + } + log_debug("signal wait channel %s, with waiters", wc->name); + + TAILQ_FOREACH_SAFE(wq, &wc->waiters, waitentry, wq1) { + TAILQ_REMOVE(&wc->waiters, wq, waitentry); + if (!cmdq_free(wq)) + cmdq_continue(wq); + } + + cmd_wait_for_remove(wc); + return (CMD_RETURN_NORMAL); +} + +#ifdef TMATE +void signal_waiting_clients(const char *name) +{ + struct wait_channel *wc, wc0; + struct cmd_q *wq, *wq1; + + wc0.name = name; + wc = RB_FIND(wait_channels, &wait_channels, &wc0); + if (wc == NULL || TAILQ_EMPTY(&wc->waiters)) { - cmdq_error(cmdq, "no waiting clients on %s", name); - return (CMD_RETURN_ERROR); + return; } TAILQ_FOREACH_SAFE(wq, &wc->waiters, waitentry, wq1) { @@ -107,30 +180,37 @@ cmd_wait_for_signal(struct cmd_q *cmdq, const char *name, if (!wc->locked) { RB_REMOVE(wait_channels, &wait_channels, wc); - free((void *)wc->name); + free((void*) wc->name); free(wc); } - - return (CMD_RETURN_NORMAL); } +#endif enum cmd_retval cmd_wait_for_wait(struct cmd_q *cmdq, const char *name, struct wait_channel *wc) { - if (cmdq->client == NULL || cmdq->client->session != NULL) { + struct client *c = cmdq->client; + +#ifdef TMATE + if (!strcmp(name, "tmate-ready") && tmate_session.tmate_env_ready) + return (CMD_RETURN_NORMAL); +#endif + + if (c == NULL || c->session != NULL) { cmdq_error(cmdq, "not able to wait"); return (CMD_RETURN_ERROR); } - if (wc == NULL) { - wc = xmalloc(sizeof *wc); - wc->name = xstrdup(name); - wc->locked = 0; - TAILQ_INIT(&wc->waiters); - TAILQ_INIT(&wc->lockers); - RB_INSERT(wait_channels, &wait_channels, wc); + if (wc == NULL) + wc = cmd_wait_for_add(name); + + if (wc->woken) { + log_debug("wait channel %s already woken (%p)", wc->name, c); + cmd_wait_for_remove(wc); + return (CMD_RETURN_NORMAL); } + log_debug("wait channel %s not woken (%p)", wc->name, c); TAILQ_INSERT_TAIL(&wc->waiters, cmdq, waitentry); cmdq->references++; @@ -147,14 +227,8 @@ cmd_wait_for_lock(struct cmd_q *cmdq, const char *name, return (CMD_RETURN_ERROR); } - if (wc == NULL) { - wc = xmalloc(sizeof *wc); - wc->name = xstrdup(name); - wc->locked = 0; - TAILQ_INIT(&wc->waiters); - TAILQ_INIT(&wc->lockers); - RB_INSERT(wait_channels, &wait_channels, wc); - } + if (wc == NULL) + wc = cmd_wait_for_add(name); if (wc->locked) { TAILQ_INSERT_TAIL(&wc->lockers, cmdq, waitentry); @@ -183,11 +257,7 @@ cmd_wait_for_unlock(struct cmd_q *cmdq, const char *name, cmdq_continue(wq); } else { wc->locked = 0; - if (TAILQ_EMPTY(&wc->waiters)) { - RB_REMOVE(wait_channels, &wait_channels, wc); - free((void *)wc->name); - free(wc); - } + cmd_wait_for_remove(wc); } return (CMD_RETURN_NORMAL); @@ -205,13 +275,13 @@ cmd_wait_for_flush(void) if (!cmdq_free(wq)) cmdq_continue(wq); } - while ((wq = TAILQ_FIRST(&wc->lockers)) != NULL) { + wc->woken = 1; + TAILQ_FOREACH_SAFE(wq, &wc->lockers, waitentry, wq1) { TAILQ_REMOVE(&wc->lockers, wq, waitentry); if (!cmdq_free(wq)) cmdq_continue(wq); } - RB_REMOVE(wait_channels, &wait_channels, wc); - free((void *)wc->name); - free(wc); + wc->locked = 0; + cmd_wait_for_remove(wc); } } diff --git a/cmd.c b/cmd.c index eeffe4c7..28efa0c5 100644 --- a/cmd.c +++ b/cmd.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -27,6 +27,95 @@ #include "tmux.h" +extern const struct cmd_entry cmd_attach_session_entry; +extern const struct cmd_entry cmd_bind_key_entry; +extern const struct cmd_entry cmd_break_pane_entry; +extern const struct cmd_entry cmd_capture_pane_entry; +extern const struct cmd_entry cmd_choose_buffer_entry; +extern const struct cmd_entry cmd_choose_client_entry; +extern const struct cmd_entry cmd_choose_session_entry; +extern const struct cmd_entry cmd_choose_tree_entry; +extern const struct cmd_entry cmd_choose_window_entry; +extern const struct cmd_entry cmd_clear_history_entry; +extern const struct cmd_entry cmd_clock_mode_entry; +extern const struct cmd_entry cmd_command_prompt_entry; +extern const struct cmd_entry cmd_confirm_before_entry; +extern const struct cmd_entry cmd_copy_mode_entry; +extern const struct cmd_entry cmd_delete_buffer_entry; +extern const struct cmd_entry cmd_detach_client_entry; +extern const struct cmd_entry cmd_display_message_entry; +extern const struct cmd_entry cmd_display_panes_entry; +extern const struct cmd_entry cmd_down_pane_entry; +extern const struct cmd_entry cmd_find_window_entry; +extern const struct cmd_entry cmd_has_session_entry; +extern const struct cmd_entry cmd_if_shell_entry; +extern const struct cmd_entry cmd_join_pane_entry; +extern const struct cmd_entry cmd_kill_pane_entry; +extern const struct cmd_entry cmd_kill_server_entry; +extern const struct cmd_entry cmd_kill_session_entry; +extern const struct cmd_entry cmd_kill_window_entry; +extern const struct cmd_entry cmd_last_pane_entry; +extern const struct cmd_entry cmd_last_window_entry; +extern const struct cmd_entry cmd_link_window_entry; +extern const struct cmd_entry cmd_list_buffers_entry; +extern const struct cmd_entry cmd_list_clients_entry; +extern const struct cmd_entry cmd_list_commands_entry; +extern const struct cmd_entry cmd_list_keys_entry; +extern const struct cmd_entry cmd_list_panes_entry; +extern const struct cmd_entry cmd_list_sessions_entry; +extern const struct cmd_entry cmd_list_windows_entry; +extern const struct cmd_entry cmd_load_buffer_entry; +extern const struct cmd_entry cmd_lock_client_entry; +extern const struct cmd_entry cmd_lock_server_entry; +extern const struct cmd_entry cmd_lock_session_entry; +extern const struct cmd_entry cmd_move_pane_entry; +extern const struct cmd_entry cmd_move_window_entry; +extern const struct cmd_entry cmd_new_session_entry; +extern const struct cmd_entry cmd_new_window_entry; +extern const struct cmd_entry cmd_next_layout_entry; +extern const struct cmd_entry cmd_next_window_entry; +extern const struct cmd_entry cmd_paste_buffer_entry; +extern const struct cmd_entry cmd_pipe_pane_entry; +extern const struct cmd_entry cmd_previous_layout_entry; +extern const struct cmd_entry cmd_previous_window_entry; +extern const struct cmd_entry cmd_refresh_client_entry; +extern const struct cmd_entry cmd_rename_session_entry; +extern const struct cmd_entry cmd_rename_window_entry; +extern const struct cmd_entry cmd_resize_pane_entry; +extern const struct cmd_entry cmd_respawn_pane_entry; +extern const struct cmd_entry cmd_respawn_window_entry; +extern const struct cmd_entry cmd_rotate_window_entry; +extern const struct cmd_entry cmd_run_shell_entry; +extern const struct cmd_entry cmd_save_buffer_entry; +extern const struct cmd_entry cmd_select_layout_entry; +extern const struct cmd_entry cmd_select_pane_entry; +extern const struct cmd_entry cmd_select_window_entry; +extern const struct cmd_entry cmd_send_keys_entry; +extern const struct cmd_entry cmd_send_prefix_entry; +extern const struct cmd_entry cmd_server_info_entry; +extern const struct cmd_entry cmd_set_buffer_entry; +extern const struct cmd_entry cmd_set_environment_entry; +extern const struct cmd_entry cmd_set_hook_entry; +extern const struct cmd_entry cmd_set_option_entry; +extern const struct cmd_entry cmd_set_window_option_entry; +extern const struct cmd_entry cmd_show_buffer_entry; +extern const struct cmd_entry cmd_show_environment_entry; +extern const struct cmd_entry cmd_show_hooks_entry; +extern const struct cmd_entry cmd_show_messages_entry; +extern const struct cmd_entry cmd_show_options_entry; +extern const struct cmd_entry cmd_show_window_options_entry; +extern const struct cmd_entry cmd_source_file_entry; +extern const struct cmd_entry cmd_split_window_entry; +extern const struct cmd_entry cmd_start_server_entry; +extern const struct cmd_entry cmd_suspend_client_entry; +extern const struct cmd_entry cmd_swap_pane_entry; +extern const struct cmd_entry cmd_swap_window_entry; +extern const struct cmd_entry cmd_switch_client_entry; +extern const struct cmd_entry cmd_unbind_key_entry; +extern const struct cmd_entry cmd_unlink_window_entry; +extern const struct cmd_entry cmd_up_pane_entry; +extern const struct cmd_entry cmd_wait_for_entry; + const struct cmd_entry *cmd_table[] = { &cmd_attach_session_entry, &cmd_bind_key_entry, @@ -95,10 +184,12 @@ const struct cmd_entry *cmd_table[] = { &cmd_server_info_entry, &cmd_set_buffer_entry, &cmd_set_environment_entry, + &cmd_set_hook_entry, &cmd_set_option_entry, &cmd_set_window_option_entry, &cmd_show_buffer_entry, &cmd_show_environment_entry, + &cmd_show_hooks_entry, &cmd_show_messages_entry, &cmd_show_options_entry, &cmd_show_window_options_entry, @@ -115,22 +206,6 @@ const struct cmd_entry *cmd_table[] = { NULL }; -int cmd_session_better(struct session *, struct session *, int); -struct session *cmd_choose_session_list(struct sessionslist *); -struct session *cmd_choose_session(int); -struct client *cmd_choose_client(struct clients *); -struct client *cmd_lookup_client(const char *); -struct session *cmd_lookup_session(struct cmd_q *, const char *, int *); -struct session *cmd_lookup_session_id(const char *); -struct winlink *cmd_lookup_window(struct session *, const char *, int *); -int cmd_lookup_index(struct session *, const char *, int *); -struct winlink *cmd_lookup_winlink_windowid(struct session *, const char *); -struct session *cmd_window_session(struct cmd_q *, struct window *, - struct winlink **); -struct winlink *cmd_find_window_offset(const char *, struct session *, int *); -int cmd_find_index_offset(const char *, struct session *, int *); -struct window_pane *cmd_find_pane_offset(const char *, struct winlink *); - int cmd_pack_argv(int argc, char **argv, char *buf, size_t len) { @@ -273,12 +348,12 @@ cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause) return (NULL); } - args = args_parse(entry->args_template, argc, argv); + args = args_parse(entry->args.template, argc, argv); if (args == NULL) goto usage; - if (entry->args_lower != -1 && args->argc < entry->args_lower) + if (entry->args.lower != -1 && args->argc < entry->args.lower) goto usage; - if (entry->args_upper != -1 && args->argc > entry->args_upper) + if (entry->args.upper != -1 && args->argc > entry->args.upper) goto usage; cmd = xcalloc(1, sizeof *cmd); @@ -312,963 +387,260 @@ usage: return (NULL); } -size_t -cmd_print(struct cmd *cmd, char *buf, size_t len) +static int +cmd_prepare_state_flag(char c, const char *target, enum cmd_entry_flag flag, + struct cmd_q *cmdq, struct cmd_q *parent) { - size_t off, used; + int targetflags, error; + struct cmd_find_state *fs = NULL; + struct cmd_find_state *current = NULL; + struct cmd_find_state tmp; - off = xsnprintf(buf, len, "%s ", cmd->entry->name); - if (off + 1 < len) { - used = args_print(cmd->args, buf + off, len - off - 1); - if (used == 0) - off--; + if (flag == CMD_NONE || + flag == CMD_CLIENT || + flag == CMD_CLIENT_CANFAIL) + return (0); + + if (c == 't') + fs = &cmdq->state.tflag; + else if (c == 's') + fs = &cmdq->state.sflag; + + if (flag == CMD_SESSION_WITHPANE) { + if (target != NULL && target[strcspn(target, ":.")] != '\0') + flag = CMD_PANE; else - off += used; - buf[off] = '\0'; - } - return (off); -} - -/* - * Figure out the current session. Use: 1) the current session, if the command - * context has one; 2) the most recently used session containing the pty of the - * calling client, if any; 3) the session specified in the TMUX variable from - * the environment (as passed from the client); 4) the most recently used - * session from all sessions. - */ -struct session * -cmd_current_session(struct cmd_q *cmdq, int prefer_unattached) -{ - struct client *c = cmdq->client; - struct session *s; - struct sessionslist ss; - struct winlink *wl; - struct window_pane *wp; - const char *path; - int found; - - if (c != NULL && c->session != NULL) - return (c->session); - - /* - * If the name of the calling client's pty is known, build a list of - * the sessions that contain it and if any choose either the first or - * the newest. - */ - path = c == NULL ? NULL : c->tty.path; - if (path != NULL) { - ARRAY_INIT(&ss); - RB_FOREACH(s, sessions, &sessions) { - found = 0; - RB_FOREACH(wl, winlinks, &s->windows) { - TAILQ_FOREACH(wp, &wl->window->panes, entry) { - if (strcmp(wp->tty, path) == 0) { - found = 1; - break; - } - } - if (found) - break; - } - if (found) - ARRAY_ADD(&ss, s); - } - - s = cmd_choose_session_list(&ss); - ARRAY_FREE(&ss); - if (s != NULL) - return (s); + flag = CMD_SESSION; } - return (cmd_choose_session(prefer_unattached)); -} - -/* Is this session better? */ -int -cmd_session_better(struct session *s, struct session *best, - int prefer_unattached) -{ - if (best == NULL) - return (1); - if (prefer_unattached) { - if (!(best->flags & SESSION_UNATTACHED) && - (s->flags & SESSION_UNATTACHED)) - return (1); - else if ((best->flags & SESSION_UNATTACHED) && - !(s->flags & SESSION_UNATTACHED)) - return (0); - } - return (timercmp(&s->activity_time, &best->activity_time, >)); -} - -/* - * Find the most recently used session, preferring unattached if the flag is - * set. - */ -struct session * -cmd_choose_session(int prefer_unattached) -{ - struct session *s, *best; - - best = NULL; - RB_FOREACH(s, sessions, &sessions) { - if (cmd_session_better(s, best, prefer_unattached)) - best = s; - } - return (best); -} - -/* Find the most recently used session from a list. */ -struct session * -cmd_choose_session_list(struct sessionslist *ss) -{ - struct session *s, *sbest; - struct timeval *tv = NULL; - u_int i; - - sbest = NULL; - for (i = 0; i < ARRAY_LENGTH(ss); i++) { - if ((s = ARRAY_ITEM(ss, i)) == NULL) - continue; - - if (tv == NULL || timercmp(&s->activity_time, tv, >)) { - sbest = s; - tv = &s->activity_time; - } + targetflags = 0; + switch (flag) { + case CMD_SESSION: + case CMD_SESSION_CANFAIL: + case CMD_SESSION_PREFERUNATTACHED: + if (flag == CMD_SESSION_CANFAIL) + targetflags |= CMD_FIND_QUIET; + if (flag == CMD_SESSION_PREFERUNATTACHED) + targetflags |= CMD_FIND_PREFER_UNATTACHED; + break; + case CMD_MOVEW_R: + flag = CMD_WINDOW_INDEX; + /* FALLTHROUGH */ + case CMD_WINDOW: + case CMD_WINDOW_CANFAIL: + case CMD_WINDOW_MARKED: + case CMD_WINDOW_INDEX: + if (flag == CMD_WINDOW_CANFAIL) + targetflags |= CMD_FIND_QUIET; + if (flag == CMD_WINDOW_MARKED) + targetflags |= CMD_FIND_DEFAULT_MARKED; + if (flag == CMD_WINDOW_INDEX) + targetflags |= CMD_FIND_WINDOW_INDEX; + break; + case CMD_PANE: + case CMD_PANE_CANFAIL: + case CMD_PANE_MARKED: + if (flag == CMD_PANE_CANFAIL) + targetflags |= CMD_FIND_QUIET; + if (flag == CMD_PANE_MARKED) + targetflags |= CMD_FIND_DEFAULT_MARKED; + break; + default: + fatalx("unknown %cflag %d", c, flag); } - return (sbest); -} - -/* - * Find the current client. First try the current client if set, then pick the - * most recently used of the clients attached to the current session if any, - * then of all clients. - */ -struct client * -cmd_current_client(struct cmd_q *cmdq) -{ - struct session *s; - struct client *c; - struct clients cc; - u_int i; - - if (cmdq->client != NULL && cmdq->client->session != NULL) - return (cmdq->client); - - /* - * No current client set. Find the current session and return the - * newest of its clients. - */ - s = cmd_current_session(cmdq, 0); - if (s != NULL && !(s->flags & SESSION_UNATTACHED)) { - ARRAY_INIT(&cc); - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - if ((c = ARRAY_ITEM(&clients, i)) == NULL) - continue; - if (s == c->session) - ARRAY_ADD(&cc, c); - } - - c = cmd_choose_client(&cc); - ARRAY_FREE(&cc); - if (c != NULL) - return (c); - } - - return (cmd_choose_client(&clients)); -} - -/* Choose the most recently used client from a list. */ -struct client * -cmd_choose_client(struct clients *cc) -{ - struct client *c, *cbest; - struct timeval *tv = NULL; - u_int i; - - cbest = NULL; - for (i = 0; i < ARRAY_LENGTH(cc); i++) { - if ((c = ARRAY_ITEM(cc, i)) == NULL) - continue; - if (c->session == NULL) - continue; - - if (tv == NULL || timercmp(&c->activity_time, tv, >)) { - cbest = c; - tv = &c->activity_time; - } - } - - return (cbest); -} - -/* Find the target client or report an error and return NULL. */ -struct client * -cmd_find_client(struct cmd_q *cmdq, const char *arg, int quiet) -{ - struct client *c; - char *tmparg; - size_t arglen; - - /* A NULL argument means the current client. */ - if (arg == NULL) { - c = cmd_current_client(cmdq); - if (c == NULL && !quiet) - cmdq_error(cmdq, "no clients"); - return (c); - } - tmparg = xstrdup(arg); - - /* Trim a single trailing colon if any. */ - arglen = strlen(tmparg); - if (arglen != 0 && tmparg[arglen - 1] == ':') - tmparg[arglen - 1] = '\0'; - - /* Find the client, if any. */ - c = cmd_lookup_client(tmparg); - - /* If no client found, report an error. */ - if (c == NULL && !quiet) - cmdq_error(cmdq, "client not found: %s", tmparg); - - free(tmparg); - return (c); -} - -/* - * Lookup a client by device path. Either of a full match and a match without a - * leading _PATH_DEV ("/dev/") is accepted. - */ -struct client * -cmd_lookup_client(const char *name) -{ - struct client *c; - const char *path; - u_int i; - - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session == NULL || c->tty.path == NULL) - continue; - path = c->tty.path; - - /* Check for exact matches. */ - if (strcmp(name, path) == 0) - return (c); - - /* Check without leading /dev if present. */ - if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0) - continue; - if (strcmp(name, path + (sizeof _PATH_DEV) - 1) == 0) - return (c); - } - - return (NULL); -} - -/* Find the target session or report an error and return NULL. */ -struct session * -cmd_lookup_session_id(const char *arg) -{ - char *endptr; - long id; - - if (arg[0] != '$') - return (NULL); - id = strtol(arg + 1, &endptr, 10); - if (arg[1] != '\0' && *endptr == '\0') - return (session_find_by_id(id)); - return (NULL); -} - -/* Lookup a session by name. If no session is found, NULL is returned. */ -struct session * -cmd_lookup_session(struct cmd_q *cmdq, const char *name, int *ambiguous) -{ - struct session *s, *sfound; - struct window *w; - struct window_pane *wp; - - *ambiguous = 0; - - /* Look for $id first. */ - if ((s = cmd_lookup_session_id(name)) != NULL) - return (s); - - /* Try as pane or window id. */ - if ((wp = cmd_lookup_paneid(name)) != NULL) - return (cmd_window_session(cmdq, wp->window, NULL)); - if ((w = cmd_lookup_windowid(name)) != NULL) - return (cmd_window_session(cmdq, w, NULL)); - - /* - * Look for matches. First look for exact matches - session names must - * be unique so an exact match can't be ambigious and can just be - * returned. - */ - if ((s = session_find(name)) != NULL) - return (s); - - /* - * Otherwise look for partial matches, returning early if it is found to - * be ambiguous. - */ - sfound = NULL; - RB_FOREACH(s, sessions, &sessions) { - if (strncmp(name, s->name, strlen(name)) == 0 || - fnmatch(name, s->name, 0) == 0) { - if (sfound != NULL) { - *ambiguous = 1; - return (NULL); - } - sfound = s; - } - } - return (sfound); -} - -/* - * Lookup a window or return -1 if not found or ambigious. First try as an - * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in - * idx if the window index is a valid number but there is no window with that - * index. - */ -struct winlink * -cmd_lookup_window(struct session *s, const char *name, int *ambiguous) -{ - struct winlink *wl, *wlfound; - struct window *w; - struct window_pane *wp; - const char *errstr; - u_int idx; - - *ambiguous = 0; - - /* Try as pane or window id. */ - if ((wl = cmd_lookup_winlink_windowid(s, name)) != NULL) - return (wl); - - /* Lookup as pane or window id. */ - if ((wp = cmd_lookup_paneid(name)) != NULL) { - wl = winlink_find_by_window(&s->windows, wp->window); - if (wl != NULL) - return (wl); - } - if ((w = cmd_lookup_windowid(name)) != NULL) { - wl = winlink_find_by_window(&s->windows, w); - if (wl != NULL) - return (wl); - } - - /* First see if this is a valid window index in this session. */ - idx = strtonum(name, 0, INT_MAX, &errstr); - if (errstr == NULL) { - if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL) - return (wl); - } - - /* Look for exact matches, error if more than one. */ - wlfound = NULL; - RB_FOREACH(wl, winlinks, &s->windows) { - if (strcmp(name, wl->window->name) == 0) { - if (wlfound != NULL) { - *ambiguous = 1; - return (NULL); - } - wlfound = wl; - } - } - if (wlfound != NULL) - return (wlfound); - - /* Now look for pattern matches, again error if multiple. */ - wlfound = NULL; - RB_FOREACH(wl, winlinks, &s->windows) { - if (strncmp(name, wl->window->name, strlen(name)) == 0 || - fnmatch(name, wl->window->name, 0) == 0) { - if (wlfound != NULL) { - *ambiguous = 1; - return (NULL); - } - wlfound = wl; - } - } - if (wlfound != NULL) - return (wlfound); - - return (NULL); -} - -/* - * Find a window index - if the window doesn't exist, check if it is a - * potential index and return it anyway. - */ -int -cmd_lookup_index(struct session *s, const char *name, int *ambiguous) -{ - struct winlink *wl; - const char *errstr; - u_int idx; - - if ((wl = cmd_lookup_window(s, name, ambiguous)) != NULL) - return (wl->idx); - if (*ambiguous) - return (-1); - - idx = strtonum(name, 0, INT_MAX, &errstr); - if (errstr == NULL) - return (idx); - - return (-1); -} - -/* Lookup pane id. An initial % means a pane id. */ -struct window_pane * -cmd_lookup_paneid(const char *arg) -{ - const char *errstr; - u_int paneid; - - if (*arg != '%') - return (NULL); - - paneid = strtonum(arg + 1, 0, UINT_MAX, &errstr); - if (errstr != NULL) - return (NULL); - return (window_pane_find_by_id(paneid)); -} - -/* Lookup window id in a session. An initial @ means a window id. */ -struct winlink * -cmd_lookup_winlink_windowid(struct session *s, const char *arg) -{ - const char *errstr; - u_int windowid; - - if (*arg != '@') - return (NULL); - - windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr); - if (errstr != NULL) - return (NULL); - return (winlink_find_by_window_id(&s->windows, windowid)); -} - -/* Lookup window id. An initial @ means a window id. */ -struct window * -cmd_lookup_windowid(const char *arg) -{ - const char *errstr; - u_int windowid; - - if (*arg != '@') - return (NULL); - - windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr); - if (errstr != NULL) - return (NULL); - return (window_find_by_id(windowid)); -} - -/* Find session and winlink for window. */ -struct session * -cmd_window_session(struct cmd_q *cmdq, struct window *w, struct winlink **wlp) -{ - struct session *s; - struct sessionslist ss; - struct winlink *wl; - - /* If this window is in the current session, return that winlink. */ - s = cmd_current_session(cmdq, 0); - if (s != NULL) { - wl = winlink_find_by_window(&s->windows, w); - if (wl != NULL) { - if (wlp != NULL) - *wlp = wl; - return (s); - } - } - - /* Otherwise choose from all sessions with this window. */ - ARRAY_INIT(&ss); - RB_FOREACH(s, sessions, &sessions) { - if (winlink_find_by_window(&s->windows, w) != NULL) - ARRAY_ADD(&ss, s); - } - s = cmd_choose_session_list(&ss); - ARRAY_FREE(&ss); - if (wlp != NULL) - *wlp = winlink_find_by_window(&s->windows, w); - return (s); -} - -/* Find the target session or report an error and return NULL. */ -struct session * -cmd_find_session(struct cmd_q *cmdq, const char *arg, int prefer_unattached) -{ - struct session *s; - struct client *c; - char *tmparg; - size_t arglen; - int ambiguous; - - /* A NULL argument means the current session. */ - if (arg == NULL) { - if ((s = cmd_current_session(cmdq, prefer_unattached)) == NULL) - cmdq_error(cmdq, "can't establish current session"); - return (s); - } - - /* Trim a single trailing colon if any. */ - tmparg = xstrdup(arg); - arglen = strlen(tmparg); - if (arglen != 0 && tmparg[arglen - 1] == ':') - tmparg[arglen - 1] = '\0'; - - /* An empty session name is the current session. */ - if (*tmparg == '\0') { - free(tmparg); - if ((s = cmd_current_session(cmdq, prefer_unattached)) == NULL) - cmdq_error(cmdq, "can't establish current session"); - return (s); - } - - /* Find the session, if any. */ - s = cmd_lookup_session(cmdq, tmparg, &ambiguous); - - /* If it doesn't, try to match it as a client. */ - if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL) - s = c->session; - - /* If no session found, report an error. */ - if (s == NULL) { - if (ambiguous) - cmdq_error(cmdq, "more than one session: %s", tmparg); - else - cmdq_error(cmdq, "session not found: %s", tmparg); - } - - free(tmparg); - return (s); -} - -/* Find the target session and window or report an error and return NULL. */ -struct winlink * -cmd_find_window(struct cmd_q *cmdq, const char *arg, struct session **sp) -{ - struct session *s; - struct winlink *wl; - const char *winptr; - char *sessptr = NULL; - int ambiguous = 0; - - /* - * Find the current session. There must always be a current session, if - * it can't be found, report an error. - */ - if ((s = cmd_current_session(cmdq, 0)) == NULL) { - cmdq_error(cmdq, "can't establish current session"); - return (NULL); - } - - /* A NULL argument means the current session and window. */ - if (arg == NULL) { - if (sp != NULL) - *sp = s; - return (s->curw); - } - - /* Time to look at the argument. If it is empty, that is an error. */ - if (*arg == '\0') - goto not_found; - - /* Find the separating colon and split into window and session. */ - winptr = strchr(arg, ':'); - if (winptr == NULL) - goto no_colon; - winptr++; /* skip : */ - sessptr = xstrdup(arg); - *strchr(sessptr, ':') = '\0'; - - /* Try to lookup the session if present. */ - if (*sessptr != '\0') { - if ((s = cmd_lookup_session(cmdq, sessptr, &ambiguous)) == NULL) - goto no_session; - } - if (sp != NULL) - *sp = s; - - /* - * Then work out the window. An empty string is the current window, - * otherwise try special cases then to look it up in the session. - */ - if (*winptr == '\0') - wl = s->curw; - else if (winptr[0] == '!' && winptr[1] == '\0') - wl = TAILQ_FIRST(&s->lastw); - else if (winptr[0] == '^' && winptr[1] == '\0') - wl = RB_MIN(winlinks, &s->windows); - else if (winptr[0] == '$' && winptr[1] == '\0') - wl = RB_MAX(winlinks, &s->windows); - else if (winptr[0] == '+' || winptr[0] == '-') - wl = cmd_find_window_offset(winptr, s, &ambiguous); - else - wl = cmd_lookup_window(s, winptr, &ambiguous); - if (wl == NULL) - goto not_found; - - if (sessptr != NULL) - free(sessptr); - return (wl); - -no_colon: - /* - * No colon in the string, first try special cases, then as a window - * and lastly as a session. - */ - if (arg[0] == '!' && arg[1] == '\0') { - if ((wl = TAILQ_FIRST(&s->lastw)) == NULL) - goto not_found; - } else if (arg[0] == '+' || arg[0] == '-') { - if ((wl = cmd_find_window_offset(arg, s, &ambiguous)) == NULL) - goto lookup_session; - } else if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL) - goto lookup_session; - - if (sp != NULL) - *sp = s; - - return (wl); - -lookup_session: - if (ambiguous) - goto not_found; - if (*arg != '\0' && - (s = cmd_lookup_session(cmdq, arg, &ambiguous)) == NULL) - goto no_session; - - if (sp != NULL) - *sp = s; - - return (s->curw); - -no_session: - if (ambiguous) - cmdq_error(cmdq, "multiple sessions: %s", arg); - else - cmdq_error(cmdq, "session not found: %s", arg); - free(sessptr); - return (NULL); - -not_found: - if (ambiguous) - cmdq_error(cmdq, "multiple windows: %s", arg); - else - cmdq_error(cmdq, "window not found: %s", arg); - free(sessptr); - return (NULL); -} - -struct winlink * -cmd_find_window_offset(const char *winptr, struct session *s, int *ambiguous) -{ - struct winlink *wl; - int offset = 1; - - if (winptr[1] != '\0') - offset = strtonum(winptr + 1, 1, INT_MAX, NULL); - if (offset == 0) - wl = cmd_lookup_window(s, winptr, ambiguous); - else { - if (winptr[0] == '+') - wl = winlink_next_by_number(s->curw, s, offset); - else - wl = winlink_previous_by_number(s->curw, s, offset); - } - - return (wl); -} - -/* - * Find the target session and window index, whether or not it exists in the - * session. Return -2 on error or -1 if no window index is specified. This is - * used when parsing an argument for a window target that may not exist (for - * example if it is going to be created). - */ -int -cmd_find_index(struct cmd_q *cmdq, const char *arg, struct session **sp) -{ - struct session *s; - struct winlink *wl; - const char *winptr; - char *sessptr = NULL; - int idx, ambiguous = 0; - - /* - * Find the current session. There must always be a current session, if - * it can't be found, report an error. - */ - if ((s = cmd_current_session(cmdq, 0)) == NULL) { - cmdq_error(cmdq, "can't establish current session"); - return (-2); - } - - /* A NULL argument means the current session and "no window" (-1). */ - if (arg == NULL) { - if (sp != NULL) - *sp = s; - return (-1); - } - - /* Time to look at the argument. If it is empty, that is an error. */ - if (*arg == '\0') - goto not_found; - - /* Find the separating colon. If none, assume the current session. */ - winptr = strchr(arg, ':'); - if (winptr == NULL) - goto no_colon; - winptr++; /* skip : */ - sessptr = xstrdup(arg); - *strchr(sessptr, ':') = '\0'; - - /* Try to lookup the session if present. */ - if (sessptr != NULL && *sessptr != '\0') { - if ((s = cmd_lookup_session(cmdq, sessptr, &ambiguous)) == NULL) - goto no_session; - } - if (sp != NULL) - *sp = s; - - /* - * Then work out the window. An empty string is a new window otherwise - * try to look it up in the session. - */ - if (*winptr == '\0') - idx = -1; - else if (winptr[0] == '!' && winptr[1] == '\0') { - if ((wl = TAILQ_FIRST(&s->lastw)) == NULL) - goto not_found; - idx = wl->idx; - } else if (winptr[0] == '+' || winptr[0] == '-') { - if ((idx = cmd_find_index_offset(winptr, s, &ambiguous)) < 0) - goto invalid_index; - } else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1) - goto invalid_index; - - free(sessptr); - return (idx); - -no_colon: - /* - * No colon in the string, first try special cases, then as a window - * and lastly as a session. - */ - if (arg[0] == '!' && arg[1] == '\0') { - if ((wl = TAILQ_FIRST(&s->lastw)) == NULL) - goto not_found; - idx = wl->idx; - } else if (arg[0] == '+' || arg[0] == '-') { - if ((idx = cmd_find_index_offset(arg, s, &ambiguous)) < 0) - goto lookup_session; - } else if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1) - goto lookup_session; - - if (sp != NULL) - *sp = s; - - return (idx); - -lookup_session: - if (ambiguous) - goto not_found; - if (*arg != '\0' && - (s = cmd_lookup_session(cmdq, arg, &ambiguous)) == NULL) - goto no_session; - - if (sp != NULL) - *sp = s; - - return (-1); - -no_session: - if (ambiguous) - cmdq_error(cmdq, "multiple sessions: %s", arg); - else - cmdq_error(cmdq, "session not found: %s", arg); - free(sessptr); - return (-2); - -invalid_index: - if (ambiguous) - goto not_found; - cmdq_error(cmdq, "invalid index: %s", arg); - - free(sessptr); - return (-2); - -not_found: - if (ambiguous) - cmdq_error(cmdq, "multiple windows: %s", arg); - else - cmdq_error(cmdq, "window not found: %s", arg); - free(sessptr); - return (-2); -} - -int -cmd_find_index_offset(const char *winptr, struct session *s, int *ambiguous) -{ - int idx, offset = 1; - - if (winptr[1] != '\0') - offset = strtonum(winptr + 1, 1, INT_MAX, NULL); - if (offset == 0) - idx = cmd_lookup_index(s, winptr, ambiguous); - else { - if (winptr[0] == '+') { - if (s->curw->idx == INT_MAX) - idx = cmd_lookup_index(s, winptr, ambiguous); - else - idx = s->curw->idx + offset; - } else { - if (s->curw->idx == 0) - idx = cmd_lookup_index(s, winptr, ambiguous); - else - idx = s->curw->idx - offset; - } - } - - return (idx); -} - -/* - * Find the target session, window and pane number or report an error and - * return NULL. The pane number is separated from the session:window by a ., - * such as mysession:mywindow.0. - */ -struct winlink * -cmd_find_pane(struct cmd_q *cmdq, - const char *arg, struct session **sp, struct window_pane **wpp) -{ - struct session *s; - struct winlink *wl; - const char *period, *errstr; - char *winptr, *paneptr; - u_int idx; - - /* Get the current session. */ - if ((s = cmd_current_session(cmdq, 0)) == NULL) { - cmdq_error(cmdq, "can't establish current session"); - return (NULL); - } - if (sp != NULL) - *sp = s; - - /* A NULL argument means the current session, window and pane. */ - if (arg == NULL) { - *wpp = s->curw->window->active; - return (s->curw); - } - - /* Lookup as pane id. */ - if ((*wpp = cmd_lookup_paneid(arg)) != NULL) { - s = cmd_window_session(cmdq, (*wpp)->window, &wl); - if (sp != NULL) - *sp = s; - return (wl); - } - - /* Look for a separating period. */ - if ((period = strrchr(arg, '.')) == NULL) - goto no_period; - - /* Pull out the window part and parse it. */ - winptr = xstrdup(arg); - winptr[period - arg] = '\0'; - if (*winptr == '\0') - wl = s->curw; - else if ((wl = cmd_find_window(cmdq, winptr, sp)) == NULL) - goto error; - - /* Find the pane section and look it up. */ - paneptr = winptr + (period - arg) + 1; - if (*paneptr == '\0') - *wpp = wl->window->active; - else if (paneptr[0] == '+' || paneptr[0] == '-') - *wpp = cmd_find_pane_offset(paneptr, wl); - else if (paneptr[0] == '!' && paneptr[1] == '\0') { - if (wl->window->last == NULL) { - cmdq_error(cmdq, "no last pane"); - goto error; - } - *wpp = wl->window->last; + log_debug("%s: flag %c %d %#x", __func__, c, flag, targetflags); + if (parent != NULL) { + if (c == 't') + current = &parent->state.tflag; + else if (c == 's') + current = &parent->state.sflag; } else { - idx = strtonum(paneptr, 0, INT_MAX, &errstr); - if (errstr != NULL) - goto lookup_string; - *wpp = window_pane_at_index(wl->window, idx); - if (*wpp == NULL) - goto lookup_string; + error = cmd_find_current(&tmp, cmdq, targetflags); + if (error != 0 && ~targetflags & CMD_FIND_QUIET) + return (-1); + current = &tmp; } - free(winptr); - return (wl); - -lookup_string: - /* Try pane string description. */ - if ((*wpp = window_find_string(wl->window, paneptr)) == NULL) { - cmdq_error(cmdq, "can't find pane: %s", paneptr); - goto error; + switch (flag) { + case CMD_NONE: + case CMD_CLIENT: + case CMD_CLIENT_CANFAIL: + return (0); + case CMD_SESSION: + case CMD_SESSION_CANFAIL: + case CMD_SESSION_PREFERUNATTACHED: + case CMD_SESSION_WITHPANE: + error = cmd_find_target(fs, current, cmdq, target, + CMD_FIND_SESSION, targetflags); + if (error != 0 && ~targetflags & CMD_FIND_QUIET) + return (-1); + break; + case CMD_MOVEW_R: + error = cmd_find_target(fs, current, cmdq, target, + CMD_FIND_SESSION, CMD_FIND_QUIET); + if (error == 0) + break; + /* FALLTHROUGH */ + case CMD_WINDOW: + case CMD_WINDOW_CANFAIL: + case CMD_WINDOW_MARKED: + case CMD_WINDOW_INDEX: + error = cmd_find_target(fs, current, cmdq, target, + CMD_FIND_WINDOW, targetflags); + if (error != 0 && ~targetflags & CMD_FIND_QUIET) + return (-1); + break; + case CMD_PANE: + case CMD_PANE_CANFAIL: + case CMD_PANE_MARKED: + error = cmd_find_target(fs, current, cmdq, target, + CMD_FIND_PANE, targetflags); + if (error != 0 && ~targetflags & CMD_FIND_QUIET) + return (-1); + break; + default: + fatalx("unknown %cflag %d", c, flag); } - - free(winptr); - return (wl); - -no_period: - /* Try as a pane number alone. */ - idx = strtonum(arg, 0, INT_MAX, &errstr); - if (errstr != NULL) - goto lookup_window; - - /* Try index in the current session and window. */ - if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL) - goto lookup_window; - - return (s->curw); - -lookup_window: - /* Try pane string description. */ - if ((*wpp = window_find_string(s->curw->window, arg)) != NULL) - return (s->curw); - - /* Try as a window and use the active pane. */ - if ((wl = cmd_find_window(cmdq, arg, sp)) != NULL) - *wpp = wl->window->active; - return (wl); - -error: - free(winptr); - return (NULL); + return (0); } -struct window_pane * -cmd_find_pane_offset(const char *paneptr, struct winlink *wl) +int +cmd_prepare_state(struct cmd *cmd, struct cmd_q *cmdq, struct cmd_q *parent) { - struct window *w = wl->window; - struct window_pane *wp = w->active; - u_int offset = 1; + const struct cmd_entry *entry = cmd->entry; + struct cmd_state *state = &cmdq->state; + char *tmp; + enum cmd_entry_flag flag; + const char *s; + int error; - if (paneptr[1] != '\0') - offset = strtonum(paneptr + 1, 1, INT_MAX, NULL); - if (offset > 0) { - if (paneptr[0] == '+') - wp = window_pane_next_by_number(w, wp, offset); + tmp = cmd_print(cmd); + log_debug("preparing state for %s (client %p)", tmp, cmdq->client); + free(tmp); + + state->c = NULL; + cmd_find_clear_state(&state->tflag, NULL, 0); + cmd_find_clear_state(&state->sflag, NULL, 0); + + flag = cmd->entry->cflag; + if (flag == CMD_NONE) { + flag = cmd->entry->tflag; + if (flag == CMD_CLIENT || flag == CMD_CLIENT_CANFAIL) + s = args_get(cmd->args, 't'); else - wp = window_pane_previous_by_number(w, wp, offset); + s = NULL; + } else + s = args_get(cmd->args, 'c'); + switch (flag) { + case CMD_CLIENT: + state->c = cmd_find_client(cmdq, s, 0); + if (state->c == NULL) + return (-1); + break; + default: + state->c = cmd_find_client(cmdq, s, 1); + break; } + s = args_get(cmd->args, 't'); + log_debug("preparing -t state: target %s", s == NULL ? "none" : s); + + error = cmd_prepare_state_flag('t', s, entry->tflag, cmdq, parent); + if (error != 0) + return (error); + + s = args_get(cmd->args, 's'); + log_debug("preparing -s state: target %s", s == NULL ? "none" : s); + + error = cmd_prepare_state_flag('s', s, entry->sflag, cmdq, parent); + if (error != 0) + return (error); + + return (0); +} + +char * +cmd_print(struct cmd *cmd) +{ + char *out, *s; + + s = args_print(cmd->args); + if (*s != '\0') + xasprintf(&out, "%s %s", cmd->entry->name, s); + else + out = xstrdup(cmd->entry->name); + free(s); + + return (out); +} + +/* Adjust current mouse position for a pane. */ +int +cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp, + u_int *yp, int last) +{ + u_int x, y; + + if (last) { + x = m->lx; + y = m->ly; + } else { + x = m->x; + y = m->y; + } + + if (m->statusat == 0 && y > 0) + y--; + else if (m->statusat > 0 && y >= (u_int)m->statusat) + y = m->statusat - 1; + + if (x < wp->xoff || x >= wp->xoff + wp->sx) + return (-1); + if (y < wp->yoff || y >= wp->yoff + wp->sy) + return (-1); + + *xp = x - wp->xoff; + *yp = y - wp->yoff; + return (0); +} + +/* Get current mouse window if any. */ +struct winlink * +cmd_mouse_window(struct mouse_event *m, struct session **sp) +{ + struct session *s; + struct window *w; + + if (!m->valid || m->s == -1 || m->w == -1) + return (NULL); + if ((s = session_find_by_id(m->s)) == NULL) + return (NULL); + if ((w = window_find_by_id(m->w)) == NULL) + return (NULL); + + if (sp != NULL) + *sp = s; + return (winlink_find_by_window(&s->windows, w)); +} + +/* Get current mouse pane if any. */ +struct window_pane * +cmd_mouse_pane(struct mouse_event *m, struct session **sp, + struct winlink **wlp) +{ + struct winlink *wl; + struct window_pane *wp; + + if ((wl = cmd_mouse_window(m, sp)) == NULL) + return (NULL); + if ((wp = window_pane_find_by_id(m->wp)) == NULL) + return (NULL); + if (!window_has_pane(wl->window, wp)) + return (NULL); + + if (wlp != NULL) + *wlp = wl; return (wp); } diff --git a/colour.c b/colour.c index b5efd6f1..b349e2a5 100644 --- a/colour.c +++ b/colour.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -29,91 +29,305 @@ * of the 256 colour palette. */ -/* An RGB colour. */ struct colour_rgb { + u_char i; u_char r; u_char g; u_char b; }; -/* 256 colour RGB table, generated on first use. */ -struct colour_rgb *colour_rgb_256; +const struct colour_rgb colour_from_256[] = { + { 0, 0x00, 0x00, 0x00 }, { 1, 0x00, 0x00, 0x5f }, + { 2, 0x00, 0x00, 0x87 }, { 3, 0x00, 0x00, 0xaf }, + { 4, 0x00, 0x00, 0xd7 }, { 5, 0x00, 0x00, 0xff }, + { 6, 0x00, 0x5f, 0x00 }, { 7, 0x00, 0x5f, 0x5f }, + { 8, 0x00, 0x5f, 0x87 }, { 9, 0x00, 0x5f, 0xaf }, + { 10, 0x00, 0x5f, 0xd7 }, { 11, 0x00, 0x5f, 0xff }, + { 12, 0x00, 0x87, 0x00 }, { 13, 0x00, 0x87, 0x5f }, + { 14, 0x00, 0x87, 0x87 }, { 15, 0x00, 0x87, 0xaf }, + { 16, 0x00, 0x87, 0xd7 }, { 17, 0x00, 0x87, 0xff }, + { 18, 0x00, 0xaf, 0x00 }, { 19, 0x00, 0xaf, 0x5f }, + { 20, 0x00, 0xaf, 0x87 }, { 21, 0x00, 0xaf, 0xaf }, + { 22, 0x00, 0xaf, 0xd7 }, { 23, 0x00, 0xaf, 0xff }, + { 24, 0x00, 0xd7, 0x00 }, { 25, 0x00, 0xd7, 0x5f }, + { 26, 0x00, 0xd7, 0x87 }, { 27, 0x00, 0xd7, 0xaf }, + { 28, 0x00, 0xd7, 0xd7 }, { 29, 0x00, 0xd7, 0xff }, + { 30, 0x00, 0xff, 0x00 }, { 31, 0x00, 0xff, 0x5f }, + { 32, 0x00, 0xff, 0x87 }, { 33, 0x00, 0xff, 0xaf }, + { 34, 0x00, 0xff, 0xd7 }, { 35, 0x00, 0xff, 0xff }, + { 36, 0x5f, 0x00, 0x00 }, { 37, 0x5f, 0x00, 0x5f }, + { 38, 0x5f, 0x00, 0x87 }, { 39, 0x5f, 0x00, 0xaf }, + { 40, 0x5f, 0x00, 0xd7 }, { 41, 0x5f, 0x00, 0xff }, + { 42, 0x5f, 0x5f, 0x00 }, { 43, 0x5f, 0x5f, 0x5f }, + { 44, 0x5f, 0x5f, 0x87 }, { 45, 0x5f, 0x5f, 0xaf }, + { 46, 0x5f, 0x5f, 0xd7 }, { 47, 0x5f, 0x5f, 0xff }, + { 48, 0x5f, 0x87, 0x00 }, { 49, 0x5f, 0x87, 0x5f }, + { 50, 0x5f, 0x87, 0x87 }, { 51, 0x5f, 0x87, 0xaf }, + { 52, 0x5f, 0x87, 0xd7 }, { 53, 0x5f, 0x87, 0xff }, + { 54, 0x5f, 0xaf, 0x00 }, { 55, 0x5f, 0xaf, 0x5f }, + { 56, 0x5f, 0xaf, 0x87 }, { 57, 0x5f, 0xaf, 0xaf }, + { 58, 0x5f, 0xaf, 0xd7 }, { 59, 0x5f, 0xaf, 0xff }, + { 60, 0x5f, 0xd7, 0x00 }, { 61, 0x5f, 0xd7, 0x5f }, + { 62, 0x5f, 0xd7, 0x87 }, { 63, 0x5f, 0xd7, 0xaf }, + { 64, 0x5f, 0xd7, 0xd7 }, { 65, 0x5f, 0xd7, 0xff }, + { 66, 0x5f, 0xff, 0x00 }, { 67, 0x5f, 0xff, 0x5f }, + { 68, 0x5f, 0xff, 0x87 }, { 69, 0x5f, 0xff, 0xaf }, + { 70, 0x5f, 0xff, 0xd7 }, { 71, 0x5f, 0xff, 0xff }, + { 72, 0x87, 0x00, 0x00 }, { 73, 0x87, 0x00, 0x5f }, + { 74, 0x87, 0x00, 0x87 }, { 75, 0x87, 0x00, 0xaf }, + { 76, 0x87, 0x00, 0xd7 }, { 77, 0x87, 0x00, 0xff }, + { 78, 0x87, 0x5f, 0x00 }, { 79, 0x87, 0x5f, 0x5f }, + { 80, 0x87, 0x5f, 0x87 }, { 81, 0x87, 0x5f, 0xaf }, + { 82, 0x87, 0x5f, 0xd7 }, { 83, 0x87, 0x5f, 0xff }, + { 84, 0x87, 0x87, 0x00 }, { 85, 0x87, 0x87, 0x5f }, + { 86, 0x87, 0x87, 0x87 }, { 87, 0x87, 0x87, 0xaf }, + { 88, 0x87, 0x87, 0xd7 }, { 89, 0x87, 0x87, 0xff }, + { 90, 0x87, 0xaf, 0x00 }, { 91, 0x87, 0xaf, 0x5f }, + { 92, 0x87, 0xaf, 0x87 }, { 93, 0x87, 0xaf, 0xaf }, + { 94, 0x87, 0xaf, 0xd7 }, { 95, 0x87, 0xaf, 0xff }, + { 96, 0x87, 0xd7, 0x00 }, { 97, 0x87, 0xd7, 0x5f }, + { 98, 0x87, 0xd7, 0x87 }, { 99, 0x87, 0xd7, 0xaf }, + { 100, 0x87, 0xd7, 0xd7 }, { 101, 0x87, 0xd7, 0xff }, + { 102, 0x87, 0xff, 0x00 }, { 103, 0x87, 0xff, 0x5f }, + { 104, 0x87, 0xff, 0x87 }, { 105, 0x87, 0xff, 0xaf }, + { 106, 0x87, 0xff, 0xd7 }, { 107, 0x87, 0xff, 0xff }, + { 108, 0xaf, 0x00, 0x00 }, { 109, 0xaf, 0x00, 0x5f }, + { 110, 0xaf, 0x00, 0x87 }, { 111, 0xaf, 0x00, 0xaf }, + { 112, 0xaf, 0x00, 0xd7 }, { 113, 0xaf, 0x00, 0xff }, + { 114, 0xaf, 0x5f, 0x00 }, { 115, 0xaf, 0x5f, 0x5f }, + { 116, 0xaf, 0x5f, 0x87 }, { 117, 0xaf, 0x5f, 0xaf }, + { 118, 0xaf, 0x5f, 0xd7 }, { 119, 0xaf, 0x5f, 0xff }, + { 120, 0xaf, 0x87, 0x00 }, { 121, 0xaf, 0x87, 0x5f }, + { 122, 0xaf, 0x87, 0x87 }, { 123, 0xaf, 0x87, 0xaf }, + { 124, 0xaf, 0x87, 0xd7 }, { 125, 0xaf, 0x87, 0xff }, + { 126, 0xaf, 0xaf, 0x00 }, { 127, 0xaf, 0xaf, 0x5f }, + { 128, 0xaf, 0xaf, 0x87 }, { 129, 0xaf, 0xaf, 0xaf }, + { 130, 0xaf, 0xaf, 0xd7 }, { 131, 0xaf, 0xaf, 0xff }, + { 132, 0xaf, 0xd7, 0x00 }, { 133, 0xaf, 0xd7, 0x5f }, + { 134, 0xaf, 0xd7, 0x87 }, { 135, 0xaf, 0xd7, 0xaf }, + { 136, 0xaf, 0xd7, 0xd7 }, { 137, 0xaf, 0xd7, 0xff }, + { 138, 0xaf, 0xff, 0x00 }, { 139, 0xaf, 0xff, 0x5f }, + { 140, 0xaf, 0xff, 0x87 }, { 141, 0xaf, 0xff, 0xaf }, + { 142, 0xaf, 0xff, 0xd7 }, { 143, 0xaf, 0xff, 0xff }, + { 144, 0xd7, 0x00, 0x00 }, { 145, 0xd7, 0x00, 0x5f }, + { 146, 0xd7, 0x00, 0x87 }, { 147, 0xd7, 0x00, 0xaf }, + { 148, 0xd7, 0x00, 0xd7 }, { 149, 0xd7, 0x00, 0xff }, + { 150, 0xd7, 0x5f, 0x00 }, { 151, 0xd7, 0x5f, 0x5f }, + { 152, 0xd7, 0x5f, 0x87 }, { 153, 0xd7, 0x5f, 0xaf }, + { 154, 0xd7, 0x5f, 0xd7 }, { 155, 0xd7, 0x5f, 0xff }, + { 156, 0xd7, 0x87, 0x00 }, { 157, 0xd7, 0x87, 0x5f }, + { 158, 0xd7, 0x87, 0x87 }, { 159, 0xd7, 0x87, 0xaf }, + { 160, 0xd7, 0x87, 0xd7 }, { 161, 0xd7, 0x87, 0xff }, + { 162, 0xd7, 0xaf, 0x00 }, { 163, 0xd7, 0xaf, 0x5f }, + { 164, 0xd7, 0xaf, 0x87 }, { 165, 0xd7, 0xaf, 0xaf }, + { 166, 0xd7, 0xaf, 0xd7 }, { 167, 0xd7, 0xaf, 0xff }, + { 168, 0xd7, 0xd7, 0x00 }, { 169, 0xd7, 0xd7, 0x5f }, + { 170, 0xd7, 0xd7, 0x87 }, { 171, 0xd7, 0xd7, 0xaf }, + { 172, 0xd7, 0xd7, 0xd7 }, { 173, 0xd7, 0xd7, 0xff }, + { 174, 0xd7, 0xff, 0x00 }, { 175, 0xd7, 0xff, 0x5f }, + { 176, 0xd7, 0xff, 0x87 }, { 177, 0xd7, 0xff, 0xaf }, + { 178, 0xd7, 0xff, 0xd7 }, { 179, 0xd7, 0xff, 0xff }, + { 180, 0xff, 0x00, 0x00 }, { 181, 0xff, 0x00, 0x5f }, + { 182, 0xff, 0x00, 0x87 }, { 183, 0xff, 0x00, 0xaf }, + { 184, 0xff, 0x00, 0xd7 }, { 185, 0xff, 0x00, 0xff }, + { 186, 0xff, 0x5f, 0x00 }, { 187, 0xff, 0x5f, 0x5f }, + { 188, 0xff, 0x5f, 0x87 }, { 189, 0xff, 0x5f, 0xaf }, + { 190, 0xff, 0x5f, 0xd7 }, { 191, 0xff, 0x5f, 0xff }, + { 192, 0xff, 0x87, 0x00 }, { 193, 0xff, 0x87, 0x5f }, + { 194, 0xff, 0x87, 0x87 }, { 195, 0xff, 0x87, 0xaf }, + { 196, 0xff, 0x87, 0xd7 }, { 197, 0xff, 0x87, 0xff }, + { 198, 0xff, 0xaf, 0x00 }, { 199, 0xff, 0xaf, 0x5f }, + { 200, 0xff, 0xaf, 0x87 }, { 201, 0xff, 0xaf, 0xaf }, + { 202, 0xff, 0xaf, 0xd7 }, { 203, 0xff, 0xaf, 0xff }, + { 204, 0xff, 0xd7, 0x00 }, { 205, 0xff, 0xd7, 0x5f }, + { 206, 0xff, 0xd7, 0x87 }, { 207, 0xff, 0xd7, 0xaf }, + { 208, 0xff, 0xd7, 0xd7 }, { 209, 0xff, 0xd7, 0xff }, + { 210, 0xff, 0xff, 0x00 }, { 211, 0xff, 0xff, 0x5f }, + { 212, 0xff, 0xff, 0x87 }, { 213, 0xff, 0xff, 0xaf }, + { 214, 0xff, 0xff, 0xd7 }, { 215, 0xff, 0xff, 0xff }, + { 216, 0x08, 0x08, 0x08 }, { 217, 0x12, 0x12, 0x12 }, + { 218, 0x1c, 0x1c, 0x1c }, { 219, 0x26, 0x26, 0x26 }, + { 220, 0x30, 0x30, 0x30 }, { 221, 0x3a, 0x3a, 0x3a }, + { 222, 0x44, 0x44, 0x44 }, { 223, 0x4e, 0x4e, 0x4e }, + { 224, 0x58, 0x58, 0x58 }, { 225, 0x62, 0x62, 0x62 }, + { 226, 0x6c, 0x6c, 0x6c }, { 227, 0x76, 0x76, 0x76 }, + { 228, 0x80, 0x80, 0x80 }, { 229, 0x8a, 0x8a, 0x8a }, + { 230, 0x94, 0x94, 0x94 }, { 231, 0x9e, 0x9e, 0x9e }, + { 232, 0xa8, 0xa8, 0xa8 }, { 233, 0xb2, 0xb2, 0xb2 }, + { 234, 0xbc, 0xbc, 0xbc }, { 235, 0xc6, 0xc6, 0xc6 }, + { 236, 0xd0, 0xd0, 0xd0 }, { 237, 0xda, 0xda, 0xda }, + { 238, 0xe4, 0xe4, 0xe4 }, { 239, 0xee, 0xee, 0xee }, +}; +const struct colour_rgb colour_to_256[] = { + { 0, 0x00, 0x00, 0x00 }, { 1, 0x00, 0x00, 0x5f }, + { 2, 0x00, 0x00, 0x87 }, { 3, 0x00, 0x00, 0xaf }, + { 4, 0x00, 0x00, 0xd7 }, { 5, 0x00, 0x00, 0xff }, + { 6, 0x00, 0x5f, 0x00 }, { 7, 0x00, 0x5f, 0x5f }, + { 8, 0x00, 0x5f, 0x87 }, { 9, 0x00, 0x5f, 0xaf }, + { 10, 0x00, 0x5f, 0xd7 }, { 11, 0x00, 0x5f, 0xff }, + { 12, 0x00, 0x87, 0x00 }, { 13, 0x00, 0x87, 0x5f }, + { 14, 0x00, 0x87, 0x87 }, { 15, 0x00, 0x87, 0xaf }, + { 16, 0x00, 0x87, 0xd7 }, { 17, 0x00, 0x87, 0xff }, + { 18, 0x00, 0xaf, 0x00 }, { 19, 0x00, 0xaf, 0x5f }, + { 20, 0x00, 0xaf, 0x87 }, { 21, 0x00, 0xaf, 0xaf }, + { 22, 0x00, 0xaf, 0xd7 }, { 23, 0x00, 0xaf, 0xff }, + { 24, 0x00, 0xd7, 0x00 }, { 25, 0x00, 0xd7, 0x5f }, + { 26, 0x00, 0xd7, 0x87 }, { 27, 0x00, 0xd7, 0xaf }, + { 28, 0x00, 0xd7, 0xd7 }, { 29, 0x00, 0xd7, 0xff }, + { 30, 0x00, 0xff, 0x00 }, { 31, 0x00, 0xff, 0x5f }, + { 32, 0x00, 0xff, 0x87 }, { 33, 0x00, 0xff, 0xaf }, + { 34, 0x00, 0xff, 0xd7 }, { 35, 0x00, 0xff, 0xff }, + { 216, 0x08, 0x08, 0x08 }, { 217, 0x12, 0x12, 0x12 }, + { 218, 0x1c, 0x1c, 0x1c }, { 219, 0x26, 0x26, 0x26 }, + { 220, 0x30, 0x30, 0x30 }, { 221, 0x3a, 0x3a, 0x3a }, + { 222, 0x44, 0x44, 0x44 }, { 223, 0x4e, 0x4e, 0x4e }, + { 224, 0x58, 0x58, 0x58 }, { 36, 0x5f, 0x00, 0x00 }, + { 37, 0x5f, 0x00, 0x5f }, { 38, 0x5f, 0x00, 0x87 }, + { 39, 0x5f, 0x00, 0xaf }, { 40, 0x5f, 0x00, 0xd7 }, + { 41, 0x5f, 0x00, 0xff }, { 42, 0x5f, 0x5f, 0x00 }, + { 43, 0x5f, 0x5f, 0x5f }, { 44, 0x5f, 0x5f, 0x87 }, + { 45, 0x5f, 0x5f, 0xaf }, { 46, 0x5f, 0x5f, 0xd7 }, + { 47, 0x5f, 0x5f, 0xff }, { 48, 0x5f, 0x87, 0x00 }, + { 49, 0x5f, 0x87, 0x5f }, { 50, 0x5f, 0x87, 0x87 }, + { 51, 0x5f, 0x87, 0xaf }, { 52, 0x5f, 0x87, 0xd7 }, + { 53, 0x5f, 0x87, 0xff }, { 54, 0x5f, 0xaf, 0x00 }, + { 55, 0x5f, 0xaf, 0x5f }, { 56, 0x5f, 0xaf, 0x87 }, + { 57, 0x5f, 0xaf, 0xaf }, { 58, 0x5f, 0xaf, 0xd7 }, + { 59, 0x5f, 0xaf, 0xff }, { 60, 0x5f, 0xd7, 0x00 }, + { 61, 0x5f, 0xd7, 0x5f }, { 62, 0x5f, 0xd7, 0x87 }, + { 63, 0x5f, 0xd7, 0xaf }, { 64, 0x5f, 0xd7, 0xd7 }, + { 65, 0x5f, 0xd7, 0xff }, { 66, 0x5f, 0xff, 0x00 }, + { 67, 0x5f, 0xff, 0x5f }, { 68, 0x5f, 0xff, 0x87 }, + { 69, 0x5f, 0xff, 0xaf }, { 70, 0x5f, 0xff, 0xd7 }, + { 71, 0x5f, 0xff, 0xff }, { 225, 0x62, 0x62, 0x62 }, + { 226, 0x6c, 0x6c, 0x6c }, { 227, 0x76, 0x76, 0x76 }, + { 228, 0x80, 0x80, 0x80 }, { 72, 0x87, 0x00, 0x00 }, + { 73, 0x87, 0x00, 0x5f }, { 74, 0x87, 0x00, 0x87 }, + { 75, 0x87, 0x00, 0xaf }, { 76, 0x87, 0x00, 0xd7 }, + { 77, 0x87, 0x00, 0xff }, { 78, 0x87, 0x5f, 0x00 }, + { 79, 0x87, 0x5f, 0x5f }, { 80, 0x87, 0x5f, 0x87 }, + { 81, 0x87, 0x5f, 0xaf }, { 82, 0x87, 0x5f, 0xd7 }, + { 83, 0x87, 0x5f, 0xff }, { 84, 0x87, 0x87, 0x00 }, + { 85, 0x87, 0x87, 0x5f }, { 86, 0x87, 0x87, 0x87 }, + { 87, 0x87, 0x87, 0xaf }, { 88, 0x87, 0x87, 0xd7 }, + { 89, 0x87, 0x87, 0xff }, { 90, 0x87, 0xaf, 0x00 }, + { 91, 0x87, 0xaf, 0x5f }, { 92, 0x87, 0xaf, 0x87 }, + { 93, 0x87, 0xaf, 0xaf }, { 94, 0x87, 0xaf, 0xd7 }, + { 95, 0x87, 0xaf, 0xff }, { 96, 0x87, 0xd7, 0x00 }, + { 97, 0x87, 0xd7, 0x5f }, { 98, 0x87, 0xd7, 0x87 }, + { 99, 0x87, 0xd7, 0xaf }, { 100, 0x87, 0xd7, 0xd7 }, + { 101, 0x87, 0xd7, 0xff }, { 102, 0x87, 0xff, 0x00 }, + { 103, 0x87, 0xff, 0x5f }, { 104, 0x87, 0xff, 0x87 }, + { 105, 0x87, 0xff, 0xaf }, { 106, 0x87, 0xff, 0xd7 }, + { 107, 0x87, 0xff, 0xff }, { 229, 0x8a, 0x8a, 0x8a }, + { 230, 0x94, 0x94, 0x94 }, { 231, 0x9e, 0x9e, 0x9e }, + { 232, 0xa8, 0xa8, 0xa8 }, { 108, 0xaf, 0x00, 0x00 }, + { 109, 0xaf, 0x00, 0x5f }, { 110, 0xaf, 0x00, 0x87 }, + { 111, 0xaf, 0x00, 0xaf }, { 112, 0xaf, 0x00, 0xd7 }, + { 113, 0xaf, 0x00, 0xff }, { 114, 0xaf, 0x5f, 0x00 }, + { 115, 0xaf, 0x5f, 0x5f }, { 116, 0xaf, 0x5f, 0x87 }, + { 117, 0xaf, 0x5f, 0xaf }, { 118, 0xaf, 0x5f, 0xd7 }, + { 119, 0xaf, 0x5f, 0xff }, { 120, 0xaf, 0x87, 0x00 }, + { 121, 0xaf, 0x87, 0x5f }, { 122, 0xaf, 0x87, 0x87 }, + { 123, 0xaf, 0x87, 0xaf }, { 124, 0xaf, 0x87, 0xd7 }, + { 125, 0xaf, 0x87, 0xff }, { 126, 0xaf, 0xaf, 0x00 }, + { 127, 0xaf, 0xaf, 0x5f }, { 128, 0xaf, 0xaf, 0x87 }, + { 129, 0xaf, 0xaf, 0xaf }, { 130, 0xaf, 0xaf, 0xd7 }, + { 131, 0xaf, 0xaf, 0xff }, { 132, 0xaf, 0xd7, 0x00 }, + { 133, 0xaf, 0xd7, 0x5f }, { 134, 0xaf, 0xd7, 0x87 }, + { 135, 0xaf, 0xd7, 0xaf }, { 136, 0xaf, 0xd7, 0xd7 }, + { 137, 0xaf, 0xd7, 0xff }, { 138, 0xaf, 0xff, 0x00 }, + { 139, 0xaf, 0xff, 0x5f }, { 140, 0xaf, 0xff, 0x87 }, + { 141, 0xaf, 0xff, 0xaf }, { 142, 0xaf, 0xff, 0xd7 }, + { 143, 0xaf, 0xff, 0xff }, { 233, 0xb2, 0xb2, 0xb2 }, + { 234, 0xbc, 0xbc, 0xbc }, { 235, 0xc6, 0xc6, 0xc6 }, + { 236, 0xd0, 0xd0, 0xd0 }, { 144, 0xd7, 0x00, 0x00 }, + { 145, 0xd7, 0x00, 0x5f }, { 146, 0xd7, 0x00, 0x87 }, + { 147, 0xd7, 0x00, 0xaf }, { 148, 0xd7, 0x00, 0xd7 }, + { 149, 0xd7, 0x00, 0xff }, { 150, 0xd7, 0x5f, 0x00 }, + { 151, 0xd7, 0x5f, 0x5f }, { 152, 0xd7, 0x5f, 0x87 }, + { 153, 0xd7, 0x5f, 0xaf }, { 154, 0xd7, 0x5f, 0xd7 }, + { 155, 0xd7, 0x5f, 0xff }, { 156, 0xd7, 0x87, 0x00 }, + { 157, 0xd7, 0x87, 0x5f }, { 158, 0xd7, 0x87, 0x87 }, + { 159, 0xd7, 0x87, 0xaf }, { 160, 0xd7, 0x87, 0xd7 }, + { 161, 0xd7, 0x87, 0xff }, { 162, 0xd7, 0xaf, 0x00 }, + { 163, 0xd7, 0xaf, 0x5f }, { 164, 0xd7, 0xaf, 0x87 }, + { 165, 0xd7, 0xaf, 0xaf }, { 166, 0xd7, 0xaf, 0xd7 }, + { 167, 0xd7, 0xaf, 0xff }, { 168, 0xd7, 0xd7, 0x00 }, + { 169, 0xd7, 0xd7, 0x5f }, { 170, 0xd7, 0xd7, 0x87 }, + { 171, 0xd7, 0xd7, 0xaf }, { 172, 0xd7, 0xd7, 0xd7 }, + { 173, 0xd7, 0xd7, 0xff }, { 174, 0xd7, 0xff, 0x00 }, + { 175, 0xd7, 0xff, 0x5f }, { 176, 0xd7, 0xff, 0x87 }, + { 177, 0xd7, 0xff, 0xaf }, { 178, 0xd7, 0xff, 0xd7 }, + { 179, 0xd7, 0xff, 0xff }, { 237, 0xda, 0xda, 0xda }, + { 238, 0xe4, 0xe4, 0xe4 }, { 239, 0xee, 0xee, 0xee }, + { 180, 0xff, 0x00, 0x00 }, { 181, 0xff, 0x00, 0x5f }, + { 182, 0xff, 0x00, 0x87 }, { 183, 0xff, 0x00, 0xaf }, + { 184, 0xff, 0x00, 0xd7 }, { 185, 0xff, 0x00, 0xff }, + { 186, 0xff, 0x5f, 0x00 }, { 187, 0xff, 0x5f, 0x5f }, + { 188, 0xff, 0x5f, 0x87 }, { 189, 0xff, 0x5f, 0xaf }, + { 190, 0xff, 0x5f, 0xd7 }, { 191, 0xff, 0x5f, 0xff }, + { 192, 0xff, 0x87, 0x00 }, { 193, 0xff, 0x87, 0x5f }, + { 194, 0xff, 0x87, 0x87 }, { 195, 0xff, 0x87, 0xaf }, + { 196, 0xff, 0x87, 0xd7 }, { 197, 0xff, 0x87, 0xff }, + { 198, 0xff, 0xaf, 0x00 }, { 199, 0xff, 0xaf, 0x5f }, + { 200, 0xff, 0xaf, 0x87 }, { 201, 0xff, 0xaf, 0xaf }, + { 202, 0xff, 0xaf, 0xd7 }, { 203, 0xff, 0xaf, 0xff }, + { 204, 0xff, 0xd7, 0x00 }, { 205, 0xff, 0xd7, 0x5f }, + { 206, 0xff, 0xd7, 0x87 }, { 207, 0xff, 0xd7, 0xaf }, + { 208, 0xff, 0xd7, 0xd7 }, { 209, 0xff, 0xd7, 0xff }, + { 210, 0xff, 0xff, 0x00 }, { 211, 0xff, 0xff, 0x5f }, + { 212, 0xff, 0xff, 0x87 }, { 213, 0xff, 0xff, 0xaf }, + { 214, 0xff, 0xff, 0xd7 }, { 215, 0xff, 0xff, 0xff }, +}; -void colour_rgb_generate256(void); -u_int colour_rgb_distance(struct colour_rgb *, struct colour_rgb *); -int colour_rgb_find(struct colour_rgb *); +int colour_cmp_rgb(const void *, const void *); -/* Generate 256 colour RGB table. */ -void -colour_rgb_generate256(void) +/* Compare function for bsearch(). */ +int +colour_cmp_rgb(const void *lhs0, const void *rhs0) { - struct colour_rgb *rgb; - u_int i, r, g, b; + const struct colour_rgb *lhs = lhs0, *rhs = rhs0; - /* - * Allocate the table. The first 16 colours are often changed by users - * and terminals so don't include them. - */ - colour_rgb_256 = xcalloc(240, sizeof *colour_rgb_256); + if (lhs->r < rhs->r) + return (-1); + if (lhs->r > rhs->r) + return (1); - /* Add the colours first. */ - r = g = b = 0; - for (i = 240; i > 24; i--) { - rgb = &colour_rgb_256[240 - i]; + if (lhs->g < rhs->g) + return (-1); + if (lhs->g > rhs->g) + return (1); - if (r != 0) - rgb->r = (r * 40) + 55; - if (g != 0) - rgb->g = (g * 40) + 55; - if (b != 0) - rgb->b = (b * 40) + 55; + if (lhs->b < rhs->b) + return (-1); + if (lhs->b > rhs->b) + return (1); - b++; - if (b > 5) { - b = 0; - g++; - } - if (g > 5) { - g = 0; - r++; - } - } - - /* Then add the greys. */ - for (i = 24; i > 0; i--) { - rgb = &colour_rgb_256[240 - i]; - - rgb->r = 8 + (24 - i) * 10; - rgb->g = 8 + (24 - i) * 10; - rgb->b = 8 + (24 - i) * 10; - } -} - -/* Get colour RGB distance. */ -u_int -colour_rgb_distance(struct colour_rgb *rgb1, struct colour_rgb *rgb2) -{ - int r, g, b; - - r = rgb1->r - rgb2->r; - g = rgb1->g - rgb2->g; - b = rgb1->b - rgb2->b; - return (r * r + g * g + b * b); + return (0); } /* Work out the nearest colour from the 256 colour set. */ int -colour_rgb_find(struct colour_rgb *rgb) +colour_find_rgb(u_char r, u_char g, u_char b) { - u_int distance, lowest, colour, i; + struct colour_rgb rgb = { .r = r, .g = g, .b = b }, *found; + u_int distance, lowest, colour, i; + int dr, dg, db; - if (colour_rgb_256 == NULL) - colour_rgb_generate256(); + found = bsearch(&rgb, colour_to_256, nitems(colour_to_256), + sizeof colour_to_256[0], colour_cmp_rgb); + if (found != NULL) + return (16 + found->i); colour = 16; lowest = UINT_MAX; for (i = 0; i < 240; i++) { - distance = colour_rgb_distance(&colour_rgb_256[i], rgb); + dr = (int)colour_from_256[i].r - r; + dg = (int)colour_from_256[i].g - g; + db = (int)colour_from_256[i].b - b; + + distance = dr * dr + dg * dg + db * db; if (distance < lowest) { lowest = distance; colour = 16 + i; @@ -147,7 +361,7 @@ colour_tostring(int c) static char s[32]; if (c & 0x100) { - xsnprintf(s, sizeof s, "colour%u", c & ~0x100); + xsnprintf(s, sizeof s, "colour%d", c & ~0x100); return (s); } @@ -194,20 +408,20 @@ colour_tostring(int c) int colour_fromstring(const char *s) { - const char *errstr; - const char *cp; - struct colour_rgb rgb; - int n; + const char *errstr; + const char *cp; + int n; + u_char r, g, b; if (*s == '#' && strlen(s) == 7) { for (cp = s + 1; isxdigit((u_char) *cp); cp++) ; if (*cp != '\0') return (-1); - n = sscanf(s + 1, "%2hhx%2hhx%2hhx", &rgb.r, &rgb.g, &rgb.b); + n = sscanf(s + 1, "%2hhx%2hhx%2hhx", &r, &g, &b); if (n != 3) return (-1); - return (colour_rgb_find(&rgb) | 0x100); + return (colour_find_rgb(r, g, b) | 0x100); } if (strncasecmp(s, "colour", (sizeof "colour") - 1) == 0) { @@ -217,47 +431,39 @@ colour_fromstring(const char *s) return (n | 0x100); } - if (strcasecmp(s, "black") == 0 || (s[0] == '0' && s[1] == '\0')) + if (strcasecmp(s, "black") == 0 || strcmp(s, "0") == 0) return (0); - if (strcasecmp(s, "red") == 0 || (s[0] == '1' && s[1] == '\0')) + if (strcasecmp(s, "red") == 0 || strcmp(s, "1") == 0) return (1); - if (strcasecmp(s, "green") == 0 || (s[0] == '2' && s[1] == '\0')) + if (strcasecmp(s, "green") == 0 || strcmp(s, "2") == 0) return (2); - if (strcasecmp(s, "yellow") == 0 || (s[0] == '3' && s[1] == '\0')) + if (strcasecmp(s, "yellow") == 0 || strcmp(s, "3") == 0) return (3); - if (strcasecmp(s, "blue") == 0 || (s[0] == '4' && s[1] == '\0')) + if (strcasecmp(s, "blue") == 0 || strcmp(s, "4") == 0) return (4); - if (strcasecmp(s, "magenta") == 0 || (s[0] == '5' && s[1] == '\0')) + if (strcasecmp(s, "magenta") == 0 || strcmp(s, "5") == 0) return (5); - if (strcasecmp(s, "cyan") == 0 || (s[0] == '6' && s[1] == '\0')) + if (strcasecmp(s, "cyan") == 0 || strcmp(s, "6") == 0) return (6); - if (strcasecmp(s, "white") == 0 || (s[0] == '7' && s[1] == '\0')) + if (strcasecmp(s, "white") == 0 || strcmp(s, "7") == 0) return (7); - if (strcasecmp(s, "default") == 0 || (s[0] == '8' && s[1] == '\0')) + if (strcasecmp(s, "default") == 0 || strcmp(s, "8") == 0) return (8); - if (strcasecmp(s, "brightblack") == 0 || - (s[0] == '9' && s[1] == '0' && s[2] == '\0')) + if (strcasecmp(s, "brightblack") == 0 || strcmp(s, "90") == 0) return (90); - if (strcasecmp(s, "brightred") == 0 || - (s[0] == '9' && s[1] == '1' && s[2] == '\0')) + if (strcasecmp(s, "brightred") == 0 || strcmp(s, "91") == 0) return (91); - if (strcasecmp(s, "brightgreen") == 0 || - (s[0] == '9' && s[1] == '2' && s[2] == '\0')) + if (strcasecmp(s, "brightgreen") == 0 || strcmp(s, "92") == 0) return (92); - if (strcasecmp(s, "brightyellow") == 0 || - (s[0] == '9' && s[1] == '3' && s[2] == '\0')) + if (strcasecmp(s, "brightyellow") == 0 || strcmp(s, "93") == 0) return (93); - if (strcasecmp(s, "brightblue") == 0 || - (s[0] == '9' && s[1] == '4' && s[2] == '\0')) + if (strcasecmp(s, "brightblue") == 0 || strcmp(s, "94") == 0) return (94); - if (strcasecmp(s, "brightmagenta") == 0 || - (s[0] == '9' && s[1] == '5' && s[2] == '\0')) + if (strcasecmp(s, "brightmagenta") == 0 || strcmp(s, "95") == 0) return (95); - if (strcasecmp(s, "brightcyan") == 0 || - (s[0] == '9' && s[1] == '6' && s[2] == '\0')) + if (strcasecmp(s, "brightcyan") == 0 || strcmp(s, "96") == 0) return (96); - if (strcasecmp(s, "brightwhite") == 0 || - (s[0] == '9' && s[1] == '7' && s[2] == '\0')) + if (strcasecmp(s, "brightwhite") == 0 || strcmp(s, "97") == 0) return (97); return (-1); } diff --git a/compat.h b/compat.h index 5f3ff8fc..b2267d37 100644 --- a/compat.h +++ b/compat.h @@ -1,7 +1,5 @@ -/* $Id$ */ - /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -23,6 +21,9 @@ #define __attribute__(a) #endif +#ifndef __unused +#define __unused __attribute__ ((__unused__)) +#endif #ifndef __dead #define __dead __attribute__ ((__noreturn__)) #endif @@ -187,11 +188,6 @@ typedef uint64_t u_int64_t; #define flock(fd, op) (0) #endif -#ifndef HAVE_BZERO -#undef bzero -#define bzero(buf, len) memset(buf, 0, len); -#endif - #ifndef HAVE_CLOSEFROM /* closefrom.c */ void closefrom(int); @@ -262,13 +258,18 @@ int unsetenv(const char *); #ifndef HAVE_CFMAKERAW /* cfmakeraw.c */ -void cfmakeraw(struct termios *); +void cfmakeraw(struct termios *); #endif #ifndef HAVE_OPENAT /* openat.c */ #define AT_FDCWD -100 -int openat(int, const char *, int, ...); +int openat(int, const char *, int, ...); +#endif + +#ifndef HAVE_REALLOCARRAY +/* reallocarray.c */ +void *reallocarray(void *, size_t, size_t size); #endif #ifdef HAVE_GETOPT diff --git a/compat/asprintf.c b/compat/asprintf.c index 09020b35..5ed1c48c 100644 --- a/compat/asprintf.c +++ b/compat/asprintf.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006 Nicholas Marriott + * Copyright (c) 2006 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above diff --git a/compat/b64_ntop.c b/compat/b64_ntop.c index 2b4dc2d4..fd8930f5 100644 --- a/compat/b64_ntop.c +++ b/compat/b64_ntop.c @@ -52,6 +52,8 @@ #include #include +#include "compat.h" + #define Assert(Cond) if (!(Cond)) abort() static const char Base64[] = @@ -122,7 +124,7 @@ static const char Pad64 = '='; */ int -b64_ntop(uint8_t const *src, size_t srclength, char *target, size_t targsize) { +b64_ntop(const char *src, size_t srclength, char *target, size_t targsize) { size_t datalength = 0; uint8_t input[3]; uint8_t output[4]; diff --git a/compat/cfmakeraw.c b/compat/cfmakeraw.c index 85b2c9bc..d8794081 100644 --- a/compat/cfmakeraw.c +++ b/compat/cfmakeraw.c @@ -1,6 +1,6 @@ /* * Copyright (c) 2013 Dagobert Michelsen - * Copyright (c) 2013 Nicholas Marriott + * Copyright (c) 2013 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above diff --git a/compat/closefrom.c b/compat/closefrom.c index 591769dd..8c650aa5 100644 --- a/compat/closefrom.c +++ b/compat/closefrom.c @@ -14,8 +14,6 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include "tmux.h" - #ifndef HAVE_CLOSEFROM #include @@ -47,6 +45,8 @@ # endif #endif +#include "tmux.h" + #ifndef OPEN_MAX # define OPEN_MAX 256 #endif diff --git a/compat/fgetln.c b/compat/fgetln.c index a5c2489d..0ad6378a 100644 --- a/compat/fgetln.c +++ b/compat/fgetln.c @@ -1,43 +1,26 @@ -/* $NetBSD: fgetln.c,v 1.3 2007/08/07 02:06:58 lukem Exp $ */ - -/*- - * Copyright (c) 1998 The NetBSD Foundation, Inc. - * All rights reserved. +/* + * Copyright (c) 2015 Joerg Jung * - * This code is derived from software contributed to The NetBSD Foundation - * by Christos Zoulas. + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of The NetBSD Foundation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include +/* + * portable fgetln() version, NOT reentrant + */ -#include #include #include -#include +#include #include "tmux.h" @@ -45,41 +28,34 @@ char * fgetln(FILE *fp, size_t *len) { static char *buf = NULL; - static size_t bufsiz = 0; - char *ptr; + static size_t bufsz = 0; + size_t r = 0; + char *p; + int c, e; - - if (buf == NULL) { - bufsiz = BUFSIZ; - if ((buf = malloc(bufsiz)) == NULL) - return NULL; - } - - if (fgets(buf, bufsiz, fp) == NULL) + if (!fp || !len) { + errno = EINVAL; return NULL; - - *len = 0; - while ((ptr = strchr(&buf[*len], '\n')) == NULL) { - size_t nbufsiz = bufsiz + BUFSIZ; - char *nbuf = realloc(buf, nbufsiz); - - if (nbuf == NULL) { - int oerrno = errno; - free(buf); - errno = oerrno; - buf = NULL; - return NULL; - } else - buf = nbuf; - - *len = bufsiz; - if (fgets(&buf[bufsiz], BUFSIZ, fp) == NULL) - return buf; - - bufsiz = nbufsiz; } - - *len = (ptr - buf) + 1; - return buf; + if (!buf) { + if (!(buf = calloc(1, BUFSIZ))) + return NULL; + bufsz = BUFSIZ; + } + while ((c = getc(fp)) != EOF) { + buf[r++] = c; + if (r == bufsz) { + if (!(p = reallocarray(buf, 2, bufsz))) { + e = errno; + free(buf); + errno = e; + buf = NULL, bufsz = 0; + return NULL; + } + buf = p, bufsz = 2 * bufsz; + } + if (c == '\n') + break; + } + return (*len = r) ? buf : NULL; } - diff --git a/compat/forkpty-aix.c b/compat/forkpty-aix.c index fd558eb8..2557ebf7 100644 --- a/compat/forkpty-aix.c +++ b/compat/forkpty-aix.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -40,6 +40,10 @@ forkpty(int *master, unused char *name, struct termios *tio, struct winsize *ws) if ((path = ttyname(*master)) == NULL) goto out; + + if (name != NULL) + strlcpy(name, path, TTY_NAME_MAX); + if ((slave = open(path, O_RDWR|O_NOCTTY)) == -1) goto out; diff --git a/compat/forkpty-hpux.c b/compat/forkpty-hpux.c index 59130e1b..09b27d08 100644 --- a/compat/forkpty-hpux.c +++ b/compat/forkpty-hpux.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above diff --git a/compat/forkpty-sunos.c b/compat/forkpty-sunos.c index 554e51ac..9abda46c 100644 --- a/compat/forkpty-sunos.c +++ b/compat/forkpty-sunos.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above diff --git a/compat/getopt.c b/compat/getopt.c index a93f368a..9cd3b3d2 100644 --- a/compat/getopt.c +++ b/compat/getopt.c @@ -52,7 +52,7 @@ char *BSDoptarg; /* argument associated with option */ int BSDgetopt(int nargc, char *const *nargv, const char *ostr) { - static char *place = EMSG; /* option letter processing */ + static const char *place = EMSG; /* option letter processing */ char *oli; /* option letter list index */ if (ostr == NULL) @@ -94,7 +94,7 @@ BSDgetopt(int nargc, char *const *nargv, const char *ostr) } else { /* need an argument */ if (*place) /* no white space */ - BSDoptarg = place; + BSDoptarg = (char *)place; else if (nargc <= ++BSDoptind) { /* no arg */ place = EMSG; if (*ostr == ':') @@ -105,7 +105,7 @@ BSDgetopt(int nargc, char *const *nargv, const char *ostr) __progname, BSDoptopt); return (BADCH); } - else /* white space */ + else /* white space */ BSDoptarg = nargv[BSDoptind]; place = EMSG; ++BSDoptind; diff --git a/compat/imsg-buffer.c b/compat/imsg-buffer.c index 450fdf1b..241c4f4d 100644 --- a/compat/imsg-buffer.c +++ b/compat/imsg-buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg-buffer.c,v 1.4 2014/06/30 00:25:17 deraadt Exp $ */ +/* $OpenBSD: imsg-buffer.c,v 1.7 2015/07/12 18:40:49 nicm Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -17,7 +17,6 @@ */ #include -#include #include #include @@ -75,7 +74,7 @@ ibuf_realloc(struct ibuf *buf, size_t len) /* on static buffers max is eq size and so the following fails */ if (buf->wpos + len > buf->max) { - errno = ENOMEM; + errno = ERANGE; return (-1); } @@ -150,7 +149,7 @@ ibuf_write(struct msgbuf *msgbuf) unsigned int i = 0; ssize_t n; - bzero(&iov, sizeof(iov)); + memset(&iov, 0, sizeof(iov)); TAILQ_FOREACH(buf, &msgbuf->bufs, entry) { if (i >= IOV_MAX) break; @@ -234,8 +233,9 @@ msgbuf_write(struct msgbuf *msgbuf) char buf[CMSG_SPACE(sizeof(int))]; } cmsgbuf; - bzero(&iov, sizeof(iov)); - bzero(&msg, sizeof(msg)); + memset(&iov, 0, sizeof(iov)); + memset(&msg, 0, sizeof(msg)); + memset(&cmsgbuf, 0, sizeof(cmsgbuf)); TAILQ_FOREACH(buf, &msgbuf->bufs, entry) { if (i >= IOV_MAX) break; diff --git a/compat/imsg.c b/compat/imsg.c index af0da44f..6c9bee68 100644 --- a/compat/imsg.c +++ b/compat/imsg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.c,v 1.6 2014/06/30 00:26:22 deraadt Exp $ */ +/* $OpenBSD: imsg.c,v 1.9 2015/07/12 18:40:49 nicm Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -17,7 +17,6 @@ */ #include -#include #include #include @@ -35,12 +34,10 @@ int imsg_get_fd(struct imsgbuf *); int available_fds(unsigned int); -/* TA: 2014-09-08: Note that the original code calls getdtablecount() which is - * OpenBSD specific. Until such time that it's ported elsewhere from - * , I've mimicked what OpenSMTPD are doing, by using available_fds() - * instead. +/* + * The original code calls getdtablecount() which is OpenBSD specific. Use + * available_fds() from OpenSMTPD instead. */ - int available_fds(unsigned int n) { @@ -54,8 +51,12 @@ available_fds(unsigned int n) for (i = 0; i < n; i++) { fds[i] = -1; if ((fds[i] = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - ret = 1; - break; + if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) + fds[i] = socket(AF_INET6, SOCK_DGRAM, 0); + if (fds[i] < 0) { + ret = 1; + break; + } } } @@ -69,7 +70,7 @@ void imsg_init(struct imsgbuf *ibuf, int fd) { msgbuf_init(&ibuf->w); - bzero(&ibuf->r, sizeof(ibuf->r)); + memset(&ibuf->r, 0, sizeof(ibuf->r)); ibuf->fd = fd; ibuf->w.fd = fd; ibuf->pid = getpid(); @@ -90,7 +91,8 @@ imsg_read(struct imsgbuf *ibuf) int fd; struct imsg_fd *ifd; - bzero(&msg, sizeof(msg)); + memset(&msg, 0, sizeof(msg)); + memset(&cmsgbuf, 0, sizeof(cmsgbuf)); iov.iov_base = ibuf->r.buf + ibuf->r.wpos; iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos; @@ -318,7 +320,7 @@ int imsg_flush(struct imsgbuf *ibuf) { while (ibuf->w.queued) - if (msgbuf_write(&ibuf->w) < 0) + if (msgbuf_write(&ibuf->w) <= 0) return (-1); return (0); } diff --git a/compat/openat.c b/compat/openat.c index 5cd9e551..d003e53d 100644 --- a/compat/openat.c +++ b/compat/openat.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Nicholas Marriott + * Copyright (c) 2013 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -40,8 +40,12 @@ openat(int fd, const char *path, int flags, ...) dotfd = open(".", O_RDONLY); if (dotfd == -1) return (-1); - if (fchdir(fd) != 0) + if (fchdir(fd) != 0) { + saved_errno = errno; + close(dotfd); + errno = saved_errno; return (-1); + } } retval = open(path, flags, mode); diff --git a/compat/reallocarray.c b/compat/reallocarray.c new file mode 100644 index 00000000..f4705fcd --- /dev/null +++ b/compat/reallocarray.c @@ -0,0 +1,40 @@ +/* $OpenBSD: reallocarray.c,v 1.3 2015/09/13 08:31:47 guenther Exp $ */ +/* + * Copyright (c) 2008 Otto Moerbeek + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include + +#include "tmux.h" + +/* + * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX + * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW + */ +#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) + +void * +reallocarray(void *optr, size_t nmemb, size_t size) +{ + if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && + nmemb > 0 && SIZE_MAX / nmemb < size) { + errno = ENOMEM; + return NULL; + } + return realloc(optr, size * nmemb); +} diff --git a/compat/setenv.c b/compat/setenv.c index 6c7d29ec..b16b08cf 100644 --- a/compat/setenv.c +++ b/compat/setenv.c @@ -1,6 +1,6 @@ /* * Copyright (c) 2010 Dagobert Michelsen - * Copyright (c) 2010 Nicholas Marriott + * Copyright (c) 2010 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above diff --git a/compat/vis.c b/compat/vis.c index 97eb5271..82b42be9 100644 --- a/compat/vis.c +++ b/compat/vis.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vis.c,v 1.19 2005/09/01 17:15:49 millert Exp $ */ +/* $OpenBSD: vis.c,v 1.24 2015/07/20 01:52:28 millert Exp $ */ /*- * Copyright (c) 1989, 1993 * The Regents of the University of California. All rights reserved. @@ -29,14 +29,17 @@ */ #include -#include +#include #include +#include #include +#include #include "tmux.h" #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') -#define isvisible(c) \ +#define isvisible(c,flag) \ + (((c) == '\\' || (flag & VIS_ALL) == 0) && \ (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \ (((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') || \ (flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) || \ @@ -45,7 +48,7 @@ ((flag & VIS_NL) == 0 && (c) == '\n') || \ ((flag & VIS_SAFE) && ((c) == '\b' || \ (c) == '\007' || (c) == '\r' || \ - isgraph((u_char)(c))))) + isgraph((u_char)(c)))))) /* * vis - visually encode characters @@ -53,10 +56,11 @@ char * vis(char *dst, int c, int flag, int nextc) { - if (isvisible(c)) { - *dst++ = c; - if (c == '\\' && (flag & VIS_NOSLASH) == 0) + if (isvisible(c, flag)) { + if ((c == '"' && (flag & VIS_DQ) != 0) || + (c == '\\' && (flag & VIS_NOSLASH) == 0)) *dst++ = '\\'; + *dst++ = c; *dst = '\0'; return (dst); } @@ -136,10 +140,10 @@ done: /* * strvis, strnvis, strvisx - visually encode characters from src into dst - * + * * Dst must be 4 times the size of src to account for possible * expansion. The length of dst, not including the trailing NULL, - * is returned. + * is returned. * * Strnvis will write no more than siz-1 bytes (and will NULL terminate). * The number of bytes needed to fully encode the string is returned. @@ -168,19 +172,18 @@ strnvis(char *dst, const char *src, size_t siz, int flag) i = 0; for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) { - if (isvisible(c)) { - i = 1; - *dst++ = c; - if (c == '\\' && (flag & VIS_NOSLASH) == 0) { + if (isvisible(c, flag)) { + if ((c == '"' && (flag & VIS_DQ) != 0) || + (c == '\\' && (flag & VIS_NOSLASH) == 0)) { /* need space for the extra '\\' */ - if (dst < end) - *dst++ = '\\'; - else { - dst--; + if (dst + 1 >= end) { i = 2; break; } + *dst++ = '\\'; } + i = 1; + *dst++ = c; src++; } else { i = vis(tbuf, c, flag, *++src) - tbuf; @@ -203,6 +206,25 @@ strnvis(char *dst, const char *src, size_t siz, int flag) return (dst - start); } +int +stravis(char **outp, const char *src, int flag) +{ + char *buf; + int len, serrno; + + buf = calloc(4, strlen(src) + 1); + if (buf == NULL) + return -1; + len = strvis(buf, src, flag); + serrno = errno; + *outp = realloc(buf, len + 1); + if (*outp == NULL) { + *outp = buf; + errno = serrno; + } + return (len); +} + int strvisx(char *dst, const char *src, size_t len, int flag) { diff --git a/compat/vis.h b/compat/vis.h index d6ff235d..9f12d236 100644 --- a/compat/vis.h +++ b/compat/vis.h @@ -1,4 +1,4 @@ -/* $OpenBSD: vis.h,v 1.11 2005/08/09 19:38:31 millert Exp $ */ +/* $OpenBSD: vis.h,v 1.15 2015/07/20 01:52:27 millert Exp $ */ /* $NetBSD: vis.h,v 1.4 1994/10/26 00:56:41 cgd Exp $ */ /*- @@ -50,6 +50,8 @@ #define VIS_NL 0x10 /* also encode newline */ #define VIS_WHITE (VIS_SP | VIS_TAB | VIS_NL) #define VIS_SAFE 0x20 /* only encode "unsafe" characters */ +#define VIS_DQ 0x200 /* backslash-escape double quotes */ +#define VIS_ALL 0x400 /* encode all characters */ /* * other @@ -73,6 +75,7 @@ char *vis(char *, int, int, int); int strvis(char *, const char *, int); +int stravis(char **, const char *, int); int strnvis(char *, const char *, size_t, int); int strvisx(char *, const char *, size_t, int); int strunvis(char *, const char *); diff --git a/configure.ac b/configure.ac index 4a321d5c..028d5559 100644 --- a/configure.ac +++ b/configure.ac @@ -1,10 +1,8 @@ -# $Id$ +# configure.ac -# Miscellaneous autofoo bullshit. -AC_INIT(tmux, 2.0) -RELEASE=1.9a -AC_SUBST(RELEASE) +AC_INIT(tmate, 2.4.0) +AM_SILENT_RULES([yes]) AC_CONFIG_AUX_DIR(etc) AM_INIT_AUTOMAKE([foreign subdir-objects]) @@ -21,19 +19,28 @@ AM_PROG_CC_C_O AC_PROG_CPP AC_PROG_EGREP AC_PROG_INSTALL +PKG_PROG_PKG_CONFIG # Default tmux.conf goes in /etc not ${prefix}/etc. test "$sysconfdir" = '${prefix}/etc' && sysconfdir=/etc -# Is this a debug build? +# Is this --enable-debug? found_debug=yes AC_ARG_ENABLE( debug, - AC_HELP_STRING(--enable-debug, create a debug build), + AC_HELP_STRING(--enable-debug, enable debug build flags), found_debug=$enable_debug ) AM_CONDITIONAL(IS_DEBUG, test "x$found_debug" = xyes) +# Is this --enable-coverage? +AC_ARG_ENABLE( + coverage, + AC_HELP_STRING(--enable-coverage, enable coverage build flags), + found_coverage=$enable_coverage +) +AM_CONDITIONAL(IS_COVERAGE, test "x$found_coverage" = xyes) + # Is this a static build? AC_ARG_ENABLE( static, @@ -41,24 +48,25 @@ AC_ARG_ENABLE( found_static=$enable_static ) if test "x$found_static" = xyes; then - LDFLAGS="$LDFLAGS -static" + # XXX Static build are only doable with the musl library + PKG_CONFIG="pkg-config --static" + + CFLAGS="$CFLAGS -flto" + LDFLAGS="$LDFLAGS -flto -static -no-pie" + + PKG_CHECK_MODULES([ZLIB], [zlib], [ + CPPFLAGS="$ZLIB_CFLAGS $CPPFLAGS" + LIBS="$ZLIB_LIBS $LIBS" + ]) + + PKG_CHECK_MODULES([LIBCRYPTO], [libcrypto], [ + CPPFLAGS="$LIBCRYPTO_CFLAGS $CPPFLAGS" + LIBS="$LIBCRYPTO_LIBS $LIBS" + ]) fi # Is this gcc? AM_CONDITIONAL(IS_GCC, test "x$GCC" = xyes) -AC_MSG_CHECKING(for gcc that whines about -I) -AC_EGREP_CPP( - yes, - [ - #if __GNUC__ > 3 - yes - #endif - ], - found_gcc4=yes, - found_gcc4=no -) -AM_CONDITIONAL(IS_GCC4, test "x$found_gcc4" = xyes) -AC_MSG_RESULT($found_gcc4) # Is this Sun CC? AC_EGREP_CPP( @@ -95,6 +103,7 @@ AC_CHECK_HEADERS( bitstring.h \ curses.h \ dirent.h \ + execinfo.h \ fcntl.h \ inttypes.h \ libutil.h \ @@ -111,10 +120,16 @@ AC_CHECK_HEADERS( ] ) +# Look for library needed for flock. +AC_SEARCH_LIBS(flock, bsd) + +# Look for library needed for backtrace +AC_SEARCH_LIBS(backtrace, execinfo) + # Check for some functions that are replaced or omitted. AC_CHECK_FUNCS( [ \ - bzero \ + backtrace \ dirfd \ flock \ setproctitle \ @@ -148,22 +163,68 @@ if test "x$found_libevent" = xno; then AC_MSG_ERROR("libevent not found") fi -# Look for curses. -AC_SEARCH_LIBS( - setupterm, - [terminfo curses ncurses tinfo], - found_curses=yes, - found_curses=no +# Look for ncurses +PKG_CHECK_MODULES( + LIBNCURSES, + ncurses, + [ + CPPFLAGS="$LIBNCURSES_CFLAGS $CPPFLAGS" + LIBS="$LIBNCURSES_LIBS $LIBS" + found_curses=yes + ], + [ + AC_SEARCH_LIBS( + setupterm, + [ncurses curses terminfo], + found_curses=yes, + found_curses=no + ) + ] ) if test "x$found_curses" = xno; then AC_MSG_ERROR("curses not found") fi # Look for utempter. -AC_CHECK_HEADER(utempter.h, have_utempter=yes, have_utempter=no) -if test "x$have_utempter" = xyes; then - AC_DEFINE(HAVE_UTEMPTER) - LIBS="$LIBS -lutempter" +AC_CHECK_HEADER(utempter.h, found_utempter=yes, found_utempter=no) +if test "x$found_utempter" = xyes; then + AC_SEARCH_LIBS( + utempter_add_record, + utempter, + found_utempter=yes, + found_utempter=no + ) + if test "x$found_utempter" = xyes; then + AC_DEFINE(HAVE_UTEMPTER) + fi +fi + +PKG_CHECK_MODULES( + MSGPACK, + msgpack >= 1.1.0, + [ + CPPFLAGS="$MSGPACK_CFLAGS $CPPFLAGS" + LIBS="$MSGPACK_LIBS $LIBS" + found_msgpack=yes + ], + found_msgpack=no +) +if test "x$found_msgpack" = xno; then + AC_MSG_ERROR("msgpack >= 1.1.0 not found") +fi + +PKG_CHECK_MODULES( + LIBSSH, + libssh >= 0.8.4, + [ + CPPFLAGS="$LIBSSH_CFLAGS $CPPFLAGS" + LIBS="$LIBSSH_LIBS $LIBS" + found_libssh=yes + ], + found_libssh=no +) +if test "x$found_libssh" = xno; then + AC_MSG_ERROR("libssh >= 0.8.4 not found") fi # Check for b64_ntop. @@ -339,22 +400,22 @@ if test "x$found_strtonum" = xyes; then fi AM_CONDITIONAL(NO_STRTONUM, [test "x$found_strtonum" = xno]) -# Look for strnvis, compat/{vis,unvis}.c used if missing. -AC_CHECK_FUNC(strnvis, found_strnvis=yes, found_strnvis=no) -if test "x$found_strnvis" = xyes; then +# Look for stravis, compat/{vis,unvis}.c used if missing. +AC_CHECK_FUNC(stravis, found_stravis=yes, found_stravis=no) +if test "x$found_stravis" = xyes; then AC_MSG_CHECKING(if strnvis is broken) AC_EGREP_HEADER([strnvis\(char \*, const char \*, size_t, int\)], vis.h, AC_MSG_RESULT(no), - [found_strnvis=no]) - if test "x$found_strnvis" = xno; then + [found_stravis=no]) + if test "x$found_stravis" = xno; then AC_MSG_RESULT(yes) fi fi -if test "x$found_strnvis" = xyes; then +if test "x$found_stravis" = xyes; then AC_DEFINE(HAVE_VIS) fi -AM_CONDITIONAL(NO_VIS, [test "x$found_strnvis" = xno]) +AM_CONDITIONAL(NO_VIS, [test "x$found_stravis" = xno]) # Look for cfmakeraw, compat/cfmakeraw.c used if missing. AC_CHECK_FUNC(cfmakeraw, found_cfmakeraw=yes, found_cfmakeraw=no) @@ -370,6 +431,13 @@ if test "x$found_openat" = xyes; then fi AM_CONDITIONAL(NO_OPENAT, [test "x$found_openat" = xno]) +# Look for reallocarray, compat/reallocarray.c used if missing. +AC_CHECK_FUNC(reallocarray, found_reallocarray=yes, found_reallocarray=no) +if test "x$found_reallocarray" = xyes; then + AC_DEFINE(HAVE_REALLOCARRAY) +fi +AM_CONDITIONAL(NO_REALLOCARRAY, [test "x$found_reallocarray" = xno]) + # Look for getopt. glibc's getopt does not enforce argument order and the ways # of making it do so are stupid, so just use our own instead. AC_CHECK_FUNC(getopt, found_getopt=yes, found_getopt=no) @@ -486,6 +554,7 @@ case "$host_os" in *linux*) AC_MSG_RESULT(linux) PLATFORM=linux + AC_DEFINE(IS_LINUX) ;; *freebsd*) AC_MSG_RESULT(freebsd) diff --git a/control-notify.c b/control-notify.c index 747ef5b4..c28d0fc8 100644 --- a/control-notify.c +++ b/control-notify.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2012 Nicholas Marriott + * Copyright (c) 2012 Nicholas Marriott * Copyright (c) 2012 George Nachman * * Permission to use, copy, modify, and distribute this software for any @@ -19,6 +19,8 @@ #include +#include + #include "tmux.h" #define CONTROL_SHOULD_NOTIFY_CLIENT(c) \ @@ -64,11 +66,13 @@ control_notify_window_layout_changed(struct window *w) struct session *s; struct format_tree *ft; struct winlink *wl; - u_int i; const char *template; + char *expanded; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); + template = "%layout-change #{window_id} #{window_layout} " + "#{window_visible_layout} #{window_flags}"; + + TAILQ_FOREACH(c, &clients, entry) { if (!CONTROL_SHOULD_NOTIFY_CLIENT(c) || c->session == NULL) continue; s = c->session; @@ -83,27 +87,26 @@ control_notify_window_layout_changed(struct window *w) */ if (w->layout_root == NULL) continue; - template = "%layout-change #{window_id} #{window_layout}"; - ft = format_create(); + ft = format_create(NULL, 0); wl = winlink_find_by_window(&s->windows, w); if (wl != NULL) { format_defaults(ft, c, NULL, wl, NULL); - control_write(c, "%s", format_expand(ft, template)); + expanded = format_expand(ft, template); + control_write(c, "%s", expanded); + free(expanded); } format_free(ft); } } void -control_notify_window_unlinked(unused struct session *s, struct window *w) +control_notify_window_unlinked(__unused struct session *s, struct window *w) { struct client *c; struct session *cs; - u_int i; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); + TAILQ_FOREACH(c, &clients, entry) { if (!CONTROL_SHOULD_NOTIFY_CLIENT(c) || c->session == NULL) continue; cs = c->session; @@ -116,14 +119,12 @@ control_notify_window_unlinked(unused struct session *s, struct window *w) } void -control_notify_window_linked(unused struct session *s, struct window *w) +control_notify_window_linked(__unused struct session *s, struct window *w) { struct client *c; struct session *cs; - u_int i; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); + TAILQ_FOREACH(c, &clients, entry) { if (!CONTROL_SHOULD_NOTIFY_CLIENT(c) || c->session == NULL) continue; cs = c->session; @@ -140,10 +141,8 @@ control_notify_window_renamed(struct window *w) { struct client *c; struct session *cs; - u_int i; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); + TAILQ_FOREACH(c, &clients, entry) { if (!CONTROL_SHOULD_NOTIFY_CLIENT(c) || c->session == NULL) continue; cs = c->session; @@ -174,10 +173,8 @@ void control_notify_session_renamed(struct session *s) { struct client *c; - u_int i; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); + TAILQ_FOREACH(c, &clients, entry) { if (!CONTROL_SHOULD_NOTIFY_CLIENT(c)) continue; @@ -186,13 +183,11 @@ control_notify_session_renamed(struct session *s) } void -control_notify_session_created(unused struct session *s) +control_notify_session_created(__unused struct session *s) { struct client *c; - u_int i; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); + TAILQ_FOREACH(c, &clients, entry) { if (!CONTROL_SHOULD_NOTIFY_CLIENT(c)) continue; @@ -201,13 +196,11 @@ control_notify_session_created(unused struct session *s) } void -control_notify_session_close(unused struct session *s) +control_notify_session_close(__unused struct session *s) { struct client *c; - u_int i; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); + TAILQ_FOREACH(c, &clients, entry) { if (!CONTROL_SHOULD_NOTIFY_CLIENT(c)) continue; diff --git a/control.c b/control.c index 0ace6c12..f6dedca8 100644 --- a/control.c +++ b/control.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2012 Nicholas Marriott + * Copyright (c) 2012 Nicholas Marriott * Copyright (c) 2012 George Nachman * * Permission to use, copy, modify, and distribute this software for any @@ -37,7 +37,7 @@ control_write(struct client *c, const char *fmt, ...) va_end(ap); evbuffer_add(c->stdout_data, "\n", 1); - server_push_stdout(c); + server_client_push_stdout(c); } /* Write a buffer, adding a terminal newline. Empties buffer. */ @@ -46,12 +46,12 @@ control_write_buffer(struct client *c, struct evbuffer *buffer) { evbuffer_add_buffer(c->stdout_data, buffer); evbuffer_add(c->stdout_data, "\n", 1); - server_push_stdout(c); + server_client_push_stdout(c); } /* Control input callback. Read lines and fire commands. */ void -control_callback(struct client *c, int closed, unused void *data) +control_callback(struct client *c, int closed, __unused void *data) { char *line, *cause; struct cmd_list *cmdlist; @@ -81,7 +81,7 @@ control_callback(struct client *c, int closed, unused void *data) } else { TAILQ_FOREACH(cmd, &cmdlist->list, qentry) cmd->flags |= CMD_CONTROL; - cmdq_run(c->cmdq, cmdlist); + cmdq_run(c->cmdq, cmdlist, NULL); cmd_list_free(cmdlist); } diff --git a/environ.c b/environ.c index 0dd91783..d855f8b5 100644 --- a/environ.c +++ b/environ.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -27,6 +27,9 @@ * Environment - manipulate a set of environment variables. */ +RB_HEAD(environ, environ_entry); +int environ_cmp(struct environ_entry *, struct environ_entry *); +RB_PROTOTYPE(environ, environ_entry, entry, environ_cmp); RB_GENERATE(environ, environ_entry, entry, environ_cmp); int @@ -36,25 +39,42 @@ environ_cmp(struct environ_entry *envent1, struct environ_entry *envent2) } /* Initialise the environment. */ -void -environ_init(struct environ *env) +struct environ * +environ_create(void) { + struct environ *env; + + env = xcalloc(1, sizeof *env); RB_INIT(env); + + return (env); } /* Free an environment. */ void environ_free(struct environ *env) { - struct environ_entry *envent; + struct environ_entry *envent, *envent1; - while (!RB_EMPTY(env)) { - envent = RB_ROOT(env); + RB_FOREACH_SAFE(envent, environ, env, envent1) { RB_REMOVE(environ, env, envent); free(envent->name); free(envent->value); free(envent); } + free(env); +} + +struct environ_entry * +environ_first(struct environ *env) +{ + return (RB_MIN(environ, env)); +} + +struct environ_entry * +environ_next(struct environ_entry *envent) +{ + return (RB_NEXT(environ, env, envent)); } /* Copy one environment into another. */ @@ -63,8 +83,12 @@ environ_copy(struct environ *srcenv, struct environ *dstenv) { struct environ_entry *envent; - RB_FOREACH(envent, environ, srcenv) - environ_set(dstenv, envent->name, envent->value); + RB_FOREACH(envent, environ, srcenv) { + if (envent->value == NULL) + environ_clear(dstenv, envent->name); + else + environ_set(dstenv, envent->name, "%s", envent->value); + } } /* Find an environment variable. */ @@ -79,23 +103,37 @@ environ_find(struct environ *env, const char *name) /* Set an environment variable. */ void -environ_set(struct environ *env, const char *name, const char *value) +environ_set(struct environ *env, const char *name, const char *fmt, ...) +{ + struct environ_entry *envent; + va_list ap; + + va_start(ap, fmt); + if ((envent = environ_find(env, name)) != NULL) { + free(envent->value); + xvasprintf(&envent->value, fmt, ap); + } else { + envent = xmalloc(sizeof *envent); + envent->name = xstrdup(name); + xvasprintf(&envent->value, fmt, ap); + RB_INSERT(environ, env, envent); + } + va_end(ap); +} + +/* Clear an environment variable. */ +void +environ_clear(struct environ *env, const char *name) { struct environ_entry *envent; if ((envent = environ_find(env, name)) != NULL) { free(envent->value); - if (value != NULL) - envent->value = xstrdup(value); - else - envent->value = NULL; + envent->value = NULL; } else { envent = xmalloc(sizeof *envent); envent->name = xstrdup(name); - if (value != NULL) - envent->value = xstrdup(value); - else - envent->value = NULL; + envent->value = NULL; RB_INSERT(environ, env, envent); } } @@ -114,7 +152,7 @@ environ_put(struct environ *env, const char *var) name = xstrdup(var); name[strcspn(name, "=")] = '\0'; - environ_set(env, name, value); + environ_set(env, name, "%s", value); free(name); } @@ -146,9 +184,9 @@ environ_update(const char *vars, struct environ *srcenv, copyvars = next = xstrdup(vars); while ((var = strsep(&next, " ")) != NULL) { if ((envent = environ_find(srcenv, var)) == NULL) - environ_set(dstenv, var, NULL); + environ_clear(dstenv, var); else - environ_set(dstenv, envent->name, envent->value); + environ_set(dstenv, envent->name, "%s", envent->value); } free(copyvars); } @@ -157,23 +195,16 @@ environ_update(const char *vars, struct environ *srcenv, void environ_push(struct environ *env) { - ARRAY_DECL(, char *) varlist; - struct environ_entry *envent; - char **varp, *var; - u_int i; + struct environ_entry *envent; + char *v; - ARRAY_INIT(&varlist); - for (varp = environ; *varp != NULL; varp++) { - var = xstrdup(*varp); - var[strcspn(var, "=")] = '\0'; - ARRAY_ADD(&varlist, var); + while (*environ != NULL) { + v = xstrdup(*environ); + v[strcspn(v, "=")] = '\0'; + + unsetenv(v); + free(v); } - for (i = 0; i < ARRAY_LENGTH(&varlist); i++) { - var = ARRAY_ITEM(&varlist, i); - unsetenv(var); - free(var); - } - ARRAY_FREE(&varlist); RB_FOREACH(envent, environ, env) { if (envent->value != NULL) diff --git a/example_tmux.conf b/example_tmux.conf new file mode 100644 index 00000000..f659a3c2 --- /dev/null +++ b/example_tmux.conf @@ -0,0 +1,66 @@ +# +# Example .tmux.conf +# +# By Nicholas Marriott. Public domain. +# + +# Some tweaks to the status line +set -g status-bg green +set -g status-right "%H:%M" +set -g window-status-current-attr "underscore" + +# No bells at all +set -g bell-action none + +# Lock after 15 minutes +set -g lock-after-time 1800 + +# Keep windows around after they exit +set -g remain-on-exit on + +# Turn on xterm-keys so that additional function keys get escape sequences +set -g xterm-keys on + +# Change the prefix key to C-a +set -g prefix C-a +unbind C-b +bind C-a send-prefix + +# Turn the mouse on, but without copy mode dragging +set -g mouse on +unbind -n MouseDrag1Pane +unbind -temacs-copy MouseDrag1Pane + +# Some extra key bindings to select higher numbered windows +bind F1 selectw -t:10 +bind F2 selectw -t:11 +bind F3 selectw -t:12 +bind F4 selectw -t:13 +bind F5 selectw -t:14 +bind F6 selectw -t:15 +bind F7 selectw -t:16 +bind F8 selectw -t:17 +bind F9 selectw -t:18 +bind F10 selectw -t:19 +bind F11 selectw -t:20 +bind F12 selectw -t:21 + +# Keys to toggle monitoring activity in a window, and synchronize-panes +bind m set monitor-activity +bind y set synchronize-panes\; display 'synchronize-panes #{?synchronize-panes,on,off}' + +# Keys to hide and show a window name from the status line +bind '-' set window-status-format '#I'\; set window-status-current-format '#I' +bind '+' set window-status-format '#I:#W#F'\; set window-status-current-format '#I:#W#F' + +# Create a single default session +new -d -s0 -nirssi 'exec irssi' +set -t0:0 monitor-activity on +set -t0:0 aggressive-resize on +neww -d -ntodo 'exec emacs ~/TODO' +setw -t0:1 aggressive-resize on +neww -d -nmutt 'exec mutt' +setw -t0:2 aggressive-resize on +neww -d +neww -d +neww -d diff --git a/examples/bash_completion_tmux.sh b/examples/bash_completion_tmux.sh deleted file mode 100644 index 74728b91..00000000 --- a/examples/bash_completion_tmux.sh +++ /dev/null @@ -1,105 +0,0 @@ -# START tmux completion -# This file is in the public domain -# See: http://www.debian-administration.org/articles/317 for how to write more. -# Usage: Put "source bash_completion_tmux.sh" into your .bashrc -_tmux() -{ - local cur prev opts - COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - prev="${COMP_WORDS[COMP_CWORD-1]}" - - opts=" \ - attach-session \ - bind-key \ - break-pane \ - capture-pane \ - choose-client \ - choose-session \ - choose-window \ - clear-history \ - clock-mode \ - command-prompt \ - confirm-before \ - copy-buffer \ - copy-mode \ - delete-buffer \ - detach-client \ - display-message \ - display-panes \ - down-pane \ - find-window \ - has-session \ - if-shell \ - join-pane \ - kill-pane \ - kill-server \ - kill-session \ - kill-window \ - last-window \ - link-window \ - list-buffers \ - list-clients \ - list-commands \ - list-keys \ - list-panes \ - list-sessions \ - list-windows \ - load-buffer \ - lock-client \ - lock-server \ - lock-session \ - move-window \ - new-session \ - new-window \ - next-layout \ - next-window \ - paste-buffer \ - pipe-pane \ - previous-layout \ - previous-window \ - refresh-client \ - rename-session \ - rename-window \ - resize-pane \ - respawn-window \ - rotate-window \ - run-shell \ - save-buffer \ - select-layout \ - select-pane \ - select-prompt \ - select-window \ - send-keys \ - send-prefix \ - server-info \ - set-buffer \ - set-environment \ - set-option \ - set-window-option \ - show-buffer \ - show-environment \ - show-messages \ - show-options \ - show-window-options \ - source-file \ - split-window \ - start-server \ - suspend-client \ - swap-pane \ - swap-window \ - switch-client \ - unbind-key \ - unlink-window \ - up-pane" - - COMPREPLY=($(compgen -W "${opts}" -- ${cur})) - return 0 - -} -complete -F _tmux tmux - -# END tmux completion - - - diff --git a/examples/h-boetes.conf b/examples/h-boetes.conf deleted file mode 100644 index 2aa86dc5..00000000 --- a/examples/h-boetes.conf +++ /dev/null @@ -1,42 +0,0 @@ -# $Id: h-boetes.conf,v 1.2 2009-10-25 21:45:26 nicm Exp $ -# -# From Han Boetes. - -set -g default-command zsh -set -g status-right "#(uptime|awk '{print $11}') #(date)" - -# Statusbar properties. -set -g display-time 3000 -set -g status-bg black -set -g status-fg cyan -set-window-option -g window-status-current-attr bright,reverse -set-window-option -g window-status-current-bg cyan -set-window-option -g window-status-current-fg black - -# Use c-t instead of c-b as the prefix -unbind C-b -set -g prefix C-t -bind C-t send-prefix -bind t send-prefix - -# Bind function keys. -bind -n F1 select-window -t 1 -bind -n F2 select-window -t 2 -bind -n F3 select-window -t 3 -bind -n F4 select-window -t 4 -bind -n F5 select-window -t 5 -bind -n F6 select-window -t 6 -bind -n F7 select-window -t 7 -bind -n F8 select-window -t 8 - -# All new windows started at startup. -new emacs -neww irssi -neww mutt -neww -neww -neww -neww -neww - -select-window -t 1 diff --git a/examples/n-marriott.conf b/examples/n-marriott.conf deleted file mode 100644 index 6a047ec9..00000000 --- a/examples/n-marriott.conf +++ /dev/null @@ -1,110 +0,0 @@ -# $Id: n-marriott.conf,v 1.11 2009-11-24 19:03:59 nicm Exp $ -# -# By Nicholas Marriott. Public domain. - -# Default global options. -set -g status-bg green -set -g status-right "%H:%M" # %d-%b-%y -set -g bell-action none -set -g lock-after-time 1800 - -# Default global window options. -setw -g remain-on-exit on -setw -g window-status-current-attr "underscore" -#setw -g xterm-keys on - -# Prefix key. -set -g prefix C-a -unbind C-b -bind C-a send-prefix - -# Keys to switch session. -bind Q switchc -t0 -bind W switchc -t1 -bind E switchc -t2 - -# Other key bindings. -bind F1 selectw -t:10 -bind F2 selectw -t:11 -bind F3 selectw -t:12 -bind F4 selectw -t:13 -bind F5 selectw -t:14 -bind F6 selectw -t:15 -bind F7 selectw -t:16 -bind F8 selectw -t:17 -bind F9 selectw -t:18 -bind F10 selectw -t:19 -bind F11 selectw -t:20 -bind F12 selectw -t:21 - -bind m setw monitor-activity - -bind y setw force-width 81 -bind u setw force-width 0 - -bind -n F1 run-shell 'mpc toggle >/dev/null 2>&1' -bind -n F2 run-shell 'mpc' -bind -n F3 run-shell 'mpc prev >/dev/null 2>&1' -bind -n F4 run-shell 'mpc next >/dev/null 2>&1' -bind -n F5 run-shell 'mpc volume -5 >/dev/null 2>&1' -bind -n F6 run-shell 'mpc volume +5 >/dev/null 2>&1' - -# Hide and show window name from status line -bind '-' setw window-status-format '#I'\; setw window-status-current-format '#I' -bind '+' setw window-status-format '#I:#W#F'\; setw window-status-current-format '#I:#W#F' - -# First session. -new -d -s0 -nirssi 'exec ssh -t natalya exec sh ~/bin/tmux-start' -setw -t0:0 monitor-activity on -setw -t0:0 aggressive-resize on -set -t0 status-bg green -neww -d -ntodo 'exec emacs ~/TODO' -setw -t0:1 aggressive-resize on -neww -d -ntodo2 'exec emacs ~/TODO2' -setw -t0:2 aggressive-resize on -neww -d -nncmpc 'exec ncmpc -f ~/.ncmpc.conf' -setw -t0:3 aggressive-resize on -neww -d -nmutt 'exec mutt' -setw -t0:4 aggressive-resize on -neww -d -neww -d -neww -d -neww -d -neww -d -neww -d -neww -d -neww -d -neww -d -neww -d -neww -d -neww -d - -# Second session. -new -d -s1 -set -t1 status-bg cyan -linkw -dk -t0 -s0:0 -linkw -dk -t1 -s0:1 -linkw -dk -t2 -s0:2 -linkw -dk -t3 -s0:3 -linkw -dk -t4 -s0:4 -neww -d -neww -d -neww -d -neww -d -neww -d -neww -d - -# Third session. -new -d -s2 -set -t2 status-bg yellow -linkw -dk -t0 -s0:0 -linkw -dk -t1 -s0:1 -linkw -dk -t2 -s0:2 -linkw -dk -t3 -s0:3 -linkw -dk -t4 -s0:4 -neww -d -neww -d -neww -d -neww -d -neww -d -neww -d diff --git a/examples/screen-keys.conf b/examples/screen-keys.conf deleted file mode 100644 index ce149290..00000000 --- a/examples/screen-keys.conf +++ /dev/null @@ -1,102 +0,0 @@ -# $Id: screen-keys.conf,v 1.7 2010-07-31 11:39:13 nicm Exp $ -# -# By Nicholas Marriott. Public domain. -# -# This configuration file binds many of the common GNU screen key bindings to -# appropriate tmux key bindings. Note that for some key bindings there is no -# tmux analogue and also that this set omits binding some commands available in -# tmux but not in screen. -# -# Note this is only a selection of key bindings and they are in addition to the -# normal tmux key bindings. This is intended as an example not as to be used -# as-is. - -# Set the prefix to ^A. -unbind C-b -set -g prefix ^A -bind a send-prefix - -# Bind appropriate commands similar to screen. -# lockscreen ^X x -unbind ^X -bind ^X lock-server -unbind x -bind x lock-server - -# screen ^C c -unbind ^C -bind ^C new-window -unbind c -bind c new-window - -# detach ^D d -unbind ^D -bind ^D detach - -# displays * -unbind * -bind * list-clients - -# next ^@ ^N sp n -unbind ^@ -bind ^@ next-window -unbind ^N -bind ^N next-window -unbind " " -bind " " next-window -unbind n -bind n next-window - -# title A -unbind A -bind A command-prompt "rename-window %%" - -# other ^A -unbind ^A -bind ^A last-window - -# prev ^H ^P p ^? -unbind ^H -bind ^H previous-window -unbind ^P -bind ^P previous-window -unbind p -bind p previous-window -unbind BSpace -bind BSpace previous-window - -# windows ^W w -unbind ^W -bind ^W list-windows -unbind w -bind w list-windows - -# quit \ -unbind '\' -bind '\' confirm-before "kill-server" - -# kill K k -unbind K -bind K confirm-before "kill-window" -unbind k -bind k confirm-before "kill-window" - -# redisplay ^L l -unbind ^L -bind ^L refresh-client -unbind l -bind l refresh-client - -# split -v | -unbind | -bind | split-window - -# :kB: focus up -unbind Tab -bind Tab select-pane -t:.+ -unbind BTab -bind BTab select-pane -t:.- - -# " windowlist -b -unbind '"' -bind '"' choose-window diff --git a/examples/t-williams.conf b/examples/t-williams.conf deleted file mode 100644 index 0a2cc3f5..00000000 --- a/examples/t-williams.conf +++ /dev/null @@ -1,104 +0,0 @@ -# $Id: t-williams.conf,v 1.1 2009-11-02 18:59:28 nicm Exp $ -# -# ~/.tmux.conf - tmux terminal multiplexer config -# Thayer Williams (http://cinderwick.ca) -# "Feel free to do whatever you like with it." - -# I typically start tmux from ~/.xinitrc with the following: -# -# urxvt -e bash -c "tmux attach -d -t mysession" & -# -# and recall it any time thereafter with xbindkeys (Mod4+s): -# -# "urxvt -e bash -c 'tmux attach -d -t mysession'" -# m:0x50 + c:39 - - -# set prefix key to ctrl+a until I have time to adapt -unbind C-b -set -g prefix C-a - -# send the prefix to client inside window (ala nested sessions) -bind-key a send-prefix - -# toggle last window like screen -bind-key C-a last-window - -# confirm before killing a window or the server -bind-key k confirm kill-window -bind-key K confirm kill-server - -# toggle statusbar -bind-key b set-option status - -# ctrl+left/right cycles thru windows -bind-key -n C-right next -bind-key -n C-left prev - -# open a man page in new window -bind / command-prompt "split-window 'exec man %%'" - -# quick view of processes -bind '~' split-window "exec htop" - -# scrollback buffer n lines -set -g history-limit 5000 - -# listen for activity on all windows -set -g bell-action any - -# on-screen time for display-panes in ms -set -g display-panes-time 2000 - -# start window indexing at one instead of zero -set -g base-index 1 - -# enable wm window titles -set -g set-titles on - -# wm window title string (uses statusbar variables) -set -g set-titles-string "tmux.#I.#W" - -# session initialization -new -s mysession mutt -neww -t 2 -neww -d -t 3 -neww -d -t 5 mocp -neww -d -t 6 rtorrent -selectw -t 1 - -# statusbar -------------------------------------------------------------- - -set -g display-time 2000 - -# default statusbar colors -set -g status-fg white -set -g status-bg default -set -g status-attr default - -# default window title colors -set-window-option -g window-status-fg cyan -set-window-option -g window-status-bg default -set-window-option -g window-status-attr dim - -# active window title colors -set-window-option -g window-status-current-fg white -set-window-option -g window-status-current-bg default -set-window-option -g window-status-current-attr bright - -# command/message line colors -set -g message-fg white -set -g message-bg black -set -g message-attr bright - -# center align the window list -set -g status-justify centre - -# show some useful stats but only when tmux is started -# outside of Xorg, otherwise dwm statusbar shows these already -set -g status-right "" -set -g status-left "" -if '[ -z "$DISPLAY" ]' 'set -g status-left "[#[fg=green] #H #[default]]"' -if '[ -z "$DISPLAY" ]' 'set -g status-right "[ #[fg=magenta]#(cat /proc/loadavg | cut -d \" \" -f 1,2,3)#[default] ][ #[fg=cyan,bright]%a %Y-%m-%d %H:%M #[default]]"' -if '[ -z "$DISPLAY" ]' 'set -g status-right-length 50' - diff --git a/examples/tmux.vim b/examples/tmux.vim deleted file mode 100644 index 4d64514a..00000000 --- a/examples/tmux.vim +++ /dev/null @@ -1,292 +0,0 @@ -" Vim syntax file -" Language: tmux(1) configuration file -" Maintainer: Tiago Cunha -" Last Change: $Date: 2010-07-27 18:29:07 $ -" License: This file is placed in the public domain. -" -" To install this file: -" -" - Drop the file in the syntax directory into runtimepath (such as -" ~/.vim/syntax/tmux.vim). -" - Make the filetype recognisable by adding the following to filetype.vim -" (~/.vim/filetype.vim): -" -" augroup filetypedetect -" au BufNewFile,BufRead .tmux.conf*,tmux.conf* setf tmux -" augroup END -" -" - Switch on syntax highlighting by adding "syntax enable" to .vimrc. -" - -if version < 600 - syntax clear -elseif exists("b:current_syntax") - finish -endif - -setlocal iskeyword+=- -syntax case match - -syn keyword tmuxAction any current none -syn keyword tmuxBoolean off on - -syn keyword tmuxCmds - \ attach[-session] - \ bind[-key] - \ break-pane - \ breakp - \ capture-pane - \ capturep - \ choose-buffer - \ choose-client - \ choose-list - \ choose-session - \ choose-tree - \ choose-window - \ clear-history - \ clearhist - \ clock-mode - \ command-prompt - \ confirm[-before] - \ copy-mode - \ delete-buffer - \ deleteb - \ detach[-client] - \ display[-message] - \ display-panes - \ displayp - \ find-window - \ findw - \ has[-session] - \ if[-shell] - \ join-pane - \ joinp - \ kill-pane - \ killp - \ kill-server - \ kill-session - \ kill-window - \ killw - \ last-pane - \ lastp - \ last[-window] - \ link-window - \ linkw - \ list-buffers - \ lsb - \ list-clients - \ lsc - \ list-commands - \ lscm - \ list-keys - \ lsk - \ list-panes - \ lsp - \ list-sessions - \ ls - \ list-windows - \ lsw - \ load-buffer - \ loadb - \ lock-client - \ lockc - \ lock[-server] - \ lock-session - \ locks - \ move-pane - \ movep - \ move-window - \ movew - \ new[-session] - \ next-layout - \ nextl - \ next[-window] - \ paste-buffer - \ pasteb - \ pipe-pane - \ pipep - \ previous-layout - \ prevl - \ prev[ious-window] - \ refresh[-client] - \ rename[-session] - \ rename-window - \ renamew - \ resize-pane - \ resizep - \ respawn-pane - \ respawnp - \ respawn-window - \ respawnw - \ rotate-window - \ rotatew - \ run[-shell] - \ save-buffer - \ saveb - \ select-layout - \ selectl - \ select-pane - \ selectp - \ select-window - \ selectw - \ send[-keys] - \ send-prefix - \ server-info - \ info - \ set-buffer - \ setb - \ set-environment - \ setenv - \ set[-option] - \ set-window-option - \ setw - \ show-buffer - \ showb - \ show-environment - \ showenv - \ show-messages - \ showmsgs - \ show[-options] - \ show-window-options - \ showw - \ source[-file] - \ split-window - \ splitw - \ start[-server] - \ suspend-client - \ suspendc - \ swap-pane - \ swapp - \ swap-window - \ swapw - \ switch-client - \ switchc - \ unbind[-key] - \ unlink-window - \ unlinkw - \ wait[-for] - -syn keyword tmuxOptsSet - \ assume-paste-time - \ base-index - \ bell-action - \ bell-on-alert - \ buffer-limit - \ default-command - \ default-shell - \ default-terminal - \ destroy-unattached - \ detach-on-destroy - \ display-panes-active-colour - \ display-panes-colour - \ display-panes-time - \ display-time - \ escape-time - \ exit-unattached - \ focus-events - \ history-limit - \ lock-after-time - \ lock-command - \ lock-server - \ message-command-style - \ message-limit - \ message-style - \ mouse-resize-pane - \ mouse-select-pane - \ mouse-select-window - \ mouse-utf8 - \ pane-active-border-style - \ pane-border-style - \ prefix - \ prefix2 - \ quiet - \ renumber-windows - \ repeat-time - \ set-clipboard - \ set-remain-on-exit - \ set-titles - \ set-titles-string - \ status - \ status-interval - \ status-justify - \ status-keys - \ status-left - \ status-left-length - \ status-left-style - \ status-position - \ status-right - \ status-right-length - \ status-utf8 - \ staus-right-style - \ terminal-overrides - \ update-environment - \ visual-activity - \ visual-bell - \ visual-content - \ visual-silence - \ word-separators - -syn keyword tmuxOptsSetw - \ aggressive-resize - \ allow-rename - \ alternate-screen - \ automatic-rename - \ c0-change-interval - \ c0-change-trigger - \ clock-mode-colour - \ clock-mode-style - \ force-height - \ force-width - \ main-pane-height - \ main-pane-width - \ mode-keys - \ mode-mouse - \ mode-style - \ monitor-activity - \ monitor-content - \ monitor-silence - \ other-pane-height - \ other-pane-width - \ pane-base-index - \ remain-on-exit - \ synchronize-panes - \ utf8 - \ window-status-activity-style - \ window-status-bell-style - \ window-status-content-style - \ window-status-current-format - \ window-status-current-style - \ window-status-format - \ window-status-last-style - \ window-status-separator - \ window-status-style - \ wrap-search - \ xterm-keys - -syn keyword tmuxTodo FIXME NOTE TODO XXX contained - -syn match tmuxKey /\(C-\|M-\|\^\)\+\S\+/ display -syn match tmuxNumber /\d\+/ display -syn match tmuxOptions /\s-\a\+/ display -syn match tmuxVariable /\w\+=/ display -syn match tmuxVariableExpansion /\${\=\w\+}\=/ display - -syn region tmuxComment start=/#/ end=/$/ contains=tmuxTodo display oneline -syn region tmuxString start=/"/ end=/"/ display oneline -syn region tmuxString start=/'/ end=/'/ display oneline - -hi def link tmuxAction Boolean -hi def link tmuxBoolean Boolean -hi def link tmuxCmds Keyword -hi def link tmuxComment Comment -hi def link tmuxKey Special -hi def link tmuxNumber Number -hi def link tmuxOptions Identifier -hi def link tmuxOptsSet Function -hi def link tmuxOptsSetw Function -hi def link tmuxString String -hi def link tmuxTodo Todo -hi def link tmuxVariable Constant -hi def link tmuxVariableExpansion Constant - -let b:current_syntax = "tmux" diff --git a/examples/tmux_backup.sh b/examples/tmux_backup.sh deleted file mode 100644 index bc0bf370..00000000 --- a/examples/tmux_backup.sh +++ /dev/null @@ -1,81 +0,0 @@ -#!/bin/bash -# -# By Victor Orlikowski. Public domain. -# -# This script maintains snapshots of each pane's -# history buffer, for each tmux session you are running. -# -# It is intended to be run by cron, on whatever interval works -# for you. - -# Maximum number of snapshots to keep. -max_backups=12 -# Names of sessions you may wish to exclude from snapshotting, -# space separated. -ignore_sessions="" -# The directory into which you want your snapshots placed. -# The default is probably "good enough." -backup_dir=~/.tmux_backup/snapshot - -######################################################################## - -# Rotate previous backups. -i=${max_backups} -while [[ ${i} != 0 ]] ; do -if [ -d ${backup_dir}.${i} ] ; then - if [[ ${i} = ${max_backups} ]] ; then - rm -r ${backup_dir}.${i} - else - mv ${backup_dir}.${i} ${backup_dir}.$((${i}+1)) - fi -fi -i=$((${i}-1)) -done - -if [ -d ${backup_dir} ] ; then - mv ${backup_dir} ${backup_dir}.1 -fi - -## Dump hardcopy from all windows in all available tmux sessions. -unset TMUX -for session in $(tmux list-sessions | cut -d' ' -f1 | sed -e 's/:$//') ; do - for ignore_session in ${ignore_sessions} ; do - if [ ${session} = ${ignore_session} ] ; then - continue 2 - fi - done - - # Session name can contain the colon character (":"). - # This can screw up addressing of windows within tmux, since - # target windows are specified as target-session:target-window. - # - # We use uuidgen to create a "safe" temporary session name, - # which we then use to create a "detached" session that "links" - # to the "real" session that we want to back up. - tmpsession=$(uuidgen) - tmux new-session -d -s "$tmpsession" -t "$session" - HISTSIZE=$(tmux show-options -g -t "$tmpsession" | grep "history-limit" | awk '{print $2}') - for win in $(tmux list-windows -t "$tmpsession" | grep -v "^\s" | cut -d' ' -f1 | sed -e 's/:$//'); do - session_dir=$(echo "$session" | sed -e 's/ /_/g' | sed -e 's%/%|%g') - win_spec="$tmpsession":"$win" - - if [ ! -d ${backup_dir}/${session_dir}/${win} ] ; then - mkdir -p ${backup_dir}/${session_dir}/${win} - fi - - for pane in $(tmux list-panes -t "$win_spec" | cut -d' ' -f1 | sed -e 's/:$//'); do - pane_path=${backup_dir}/${session_dir}/${win}/${pane} - pane_spec="$win_spec"."$pane" - - tmux capture-pane -t "$pane_spec" -S -${HISTSIZE} - tmux save-buffer ${pane_path} - - if [ ! -s ${pane_path} ] ; then - sleep 1 - rm ${pane_path} - fi - done - done - tmux kill-session -t "$tmpsession" - -done diff --git a/examples/vim-keys.conf b/examples/vim-keys.conf deleted file mode 100644 index d587d0bf..00000000 --- a/examples/vim-keys.conf +++ /dev/null @@ -1,36 +0,0 @@ -# $Id: vim-keys.conf,v 1.2 2010-09-18 09:36:15 nicm Exp $ -# -# vim-keys.conf, v1.2 2010/09/12 -# -# By Daniel Thau. Public domain. -# -# This configuration file binds many vi- and vim-like bindings to the -# appropriate tmux key bindings. Note that for many key bindings there is no -# tmux analogue. This is intended for tmux 1.3, which handles pane selection -# differently from the previous versions - -# split windows like vim -# vim's definition of a horizontal/vertical split is reversed from tmux's -bind s split-window -v -bind v split-window -h - -# move around panes with hjkl, as one would in vim after pressing ctrl-w -bind h select-pane -L -bind j select-pane -D -bind k select-pane -U -bind l select-pane -R - -# resize panes like vim -# feel free to change the "1" to however many lines you want to resize by, only -# one at a time can be slow -bind < resize-pane -L 1 -bind > resize-pane -R 1 -bind - resize-pane -D 1 -bind + resize-pane -U 1 - -# bind : to command-prompt like vim -# this is the default in tmux already -bind : command-prompt - -# vi-style controls for copy mode -setw -g mode-keys vi diff --git a/examples/xterm-keys.vim b/examples/xterm-keys.vim deleted file mode 100644 index 5672c26a..00000000 --- a/examples/xterm-keys.vim +++ /dev/null @@ -1,49 +0,0 @@ -" tmux.vim - Set xterm input codes passed by tmux -" Author: Mark Oteiza -" License: Public domain -" Description: Simple plugin that assigns some xterm(1)-style keys to escape -" sequences passed by tmux when "xterm-keys" is set to "on". Inspired by an -" example given by Chris Johnsen at: -" https://stackoverflow.com/a/15471820 -" -" Documentation: help:xterm-modifier-keys man:tmux(1) - -if exists("g:loaded_tmux") || &cp - finish -endif -let g:loaded_tmux = 1 - -function! s:SetXtermCapabilities() - set ttymouse=sgr - - execute "set =\e[1;*A" - execute "set =\e[1;*B" - execute "set =\e[1;*C" - execute "set =\e[1;*D" - - execute "set =\e[1;*H" - execute "set =\e[1;*F" - - execute "set =\e[2;*~" - execute "set =\e[3;*~" - execute "set =\e[5;*~" - execute "set =\e[6;*~" - - execute "set =\e[1;*P" - execute "set =\e[1;*Q" - execute "set =\e[1;*R" - execute "set =\e[1;*S" - - execute "set =\e[15;*~" - execute "set =\e[17;*~" - execute "set =\e[18;*~" - execute "set =\e[19;*~" - execute "set =\e[20;*~" - execute "set =\e[21;*~" - execute "set =\e[23;*~" - execute "set =\e[24;*~" -endfunction - -if exists('$TMUX') - call s:SetXtermCapabilities() -endif diff --git a/format.c b/format.c index 776de960..f771c85f 100644 --- a/format.c +++ b/format.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2011 Nicholas Marriott + * Copyright (c) 2011 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -30,15 +31,39 @@ #include #include "tmux.h" +#include "tmate.h" /* * Build a list of key-value pairs and use them to expand #{key} entries in a * string. */ +struct format_entry; +typedef void (*format_cb)(struct format_tree *, struct format_entry *); + +void format_job_callback(struct job *); +char *format_job_get(struct format_tree *, const char *); +void format_job_timer(int, short, void *); + +void format_cb_host(struct format_tree *, struct format_entry *); +void format_cb_host_short(struct format_tree *, struct format_entry *); +void format_cb_pid(struct format_tree *, struct format_entry *); +void format_cb_session_alerts(struct format_tree *, struct format_entry *); +void format_cb_window_layout(struct format_tree *, struct format_entry *); +void format_cb_window_visible_layout(struct format_tree *, + struct format_entry *); +void format_cb_start_command(struct format_tree *, struct format_entry *); +void format_cb_current_command(struct format_tree *, struct format_entry *); +void format_cb_current_path(struct format_tree *, struct format_entry *); +void format_cb_history_bytes(struct format_tree *, struct format_entry *); +void format_cb_pane_tabs(struct format_tree *, struct format_entry *); + +char *format_find(struct format_tree *, const char *, int); +void format_add_cb(struct format_tree *, const char *, format_cb); +void format_add_tv(struct format_tree *, const char *, struct timeval *); int format_replace(struct format_tree *, const char *, size_t, char **, size_t *, size_t *); -char *format_get_command(struct window_pane *); +char *format_time_string(time_t); void format_defaults_pane_tabs(struct format_tree *, struct window_pane *); void format_defaults_session(struct format_tree *, struct session *); @@ -46,30 +71,65 @@ void format_defaults_client(struct format_tree *, struct client *); void format_defaults_winlink(struct format_tree *, struct session *, struct winlink *); +/* Entry in format job tree. */ +struct format_job { + const char *cmd; + + time_t last; + char *out; + + struct job *job; + int status; + + RB_ENTRY(format_job) entry; +}; + +/* Format job tree. */ +struct event format_job_event; +int format_job_cmp(struct format_job *, struct format_job *); +RB_HEAD(format_job_tree, format_job) format_jobs = RB_INITIALIZER(); +RB_PROTOTYPE(format_job_tree, format_job, entry, format_job_cmp); +RB_GENERATE(format_job_tree, format_job, entry, format_job_cmp); + +/* Format job tree comparison function. */ +int +format_job_cmp(struct format_job *fj1, struct format_job *fj2) +{ + return (strcmp(fj1->cmd, fj2->cmd)); +} + +/* Format modifiers. */ +#define FORMAT_TIMESTRING 0x1 +#define FORMAT_BASENAME 0x2 +#define FORMAT_DIRNAME 0x4 +#define FORMAT_SUBSTITUTE 0x8 + /* Entry in format tree. */ struct format_entry { - char *key; - char *value; - - RB_ENTRY(format_entry) entry; + char *key; + char *value; + time_t t; + format_cb cb; + RB_ENTRY(format_entry) entry; }; -/* Tree of format entries. */ +/* Format entry tree. */ struct format_tree { - struct window *w; - struct session *s; + struct window *w; + struct session *s; + struct window_pane *wp; - RB_HEAD(format_rb_tree, format_entry) tree; + int flags; + + RB_HEAD(format_entry_tree, format_entry) tree; }; +int format_entry_cmp(struct format_entry *, struct format_entry *); +RB_PROTOTYPE(format_entry_tree, format_entry, entry, format_entry_cmp); +RB_GENERATE(format_entry_tree, format_entry, entry, format_entry_cmp); -/* Format key-value replacement entry. */ -int format_cmp(struct format_entry *, struct format_entry *); -RB_PROTOTYPE(format_rb_tree, format_entry, entry, format_cmp); -RB_GENERATE(format_rb_tree, format_entry, entry, format_cmp); - -/* Format tree comparison function. */ +/* Format entry tree comparison function. */ int -format_cmp(struct format_entry *fe1, struct format_entry *fe2) +format_entry_cmp(struct format_entry *fe1, struct format_entry *fe2) { return (strcmp(fe1->key, fe2->key)); } @@ -134,22 +194,316 @@ const char *format_lower[] = { NULL /* z */ }; +/* Format job callback. */ +void +format_job_callback(struct job *job) +{ + struct format_job *fj = job->data; + char *line, *buf; + size_t len; + struct client *c; + + fj->job = NULL; + free(fj->out); + + buf = NULL; + if ((line = evbuffer_readline(job->event->input)) == NULL) { + len = EVBUFFER_LENGTH(job->event->input); + buf = xmalloc(len + 1); + if (len != 0) + memcpy(buf, EVBUFFER_DATA(job->event->input), len); + buf[len] = '\0'; + } else + buf = line; + fj->out = buf; + + if (fj->status) { + TAILQ_FOREACH(c, &clients, entry) + server_status_client(c); + fj->status = 0; + } + + log_debug("%s: %s: %s", __func__, fj->cmd, fj->out); +} + +/* Find a job. */ +char * +format_job_get(struct format_tree *ft, const char *cmd) +{ + struct format_job fj0, *fj; + time_t t; + + fj0.cmd = cmd; + if ((fj = RB_FIND(format_job_tree, &format_jobs, &fj0)) == NULL) { + fj = xcalloc(1, sizeof *fj); + fj->cmd = xstrdup(cmd); + + xasprintf(&fj->out, "<'%s' not ready>", fj->cmd); + + RB_INSERT(format_job_tree, &format_jobs, fj); + } + + t = time(NULL); + if (fj->job == NULL && ((ft->flags & FORMAT_FORCE) || fj->last != t)) { + fj->job = job_run(fj->cmd, NULL, NULL, format_job_callback, + NULL, fj); + if (fj->job == NULL) { + free(fj->out); + xasprintf(&fj->out, "<'%s' didn't start>", fj->cmd); + } + fj->last = t; + } + + if (ft->flags & FORMAT_STATUS) + fj->status = 1; + + return (format_expand(ft, fj->out)); +} + +/* Remove old jobs. */ +void +format_job_timer(__unused int fd, __unused short events, __unused void *arg) +{ + struct format_job *fj, *fj1; + time_t now; + struct timeval tv = { .tv_sec = 60 }; + + now = time(NULL); + RB_FOREACH_SAFE(fj, format_job_tree, &format_jobs, fj1) { + if (fj->last > now || now - fj->last < 3600) + continue; + RB_REMOVE(format_job_tree, &format_jobs, fj); + + log_debug("%s: %s", __func__, fj->cmd); + + if (fj->job != NULL) + job_free(fj->job); + + free((void *)fj->cmd); + free(fj->out); + + free(fj); + } + + evtimer_del(&format_job_event); + evtimer_add(&format_job_event, &tv); +} + +/* Callback for host. */ +void +format_cb_host(__unused struct format_tree *ft, struct format_entry *fe) +{ + char host[HOST_NAME_MAX + 1]; + + if (gethostname(host, sizeof host) != 0) + fe->value = xstrdup(""); + else + fe->value = xstrdup(host); +} + +/* Callback for host_short. */ +void +format_cb_host_short(__unused struct format_tree *ft, struct format_entry *fe) +{ + char host[HOST_NAME_MAX + 1], *cp; + + if (gethostname(host, sizeof host) != 0) + fe->value = xstrdup(""); + else { + if ((cp = strchr(host, '.')) != NULL) + *cp = '\0'; + fe->value = xstrdup(host); + } +} + +/* Callback for pid. */ +void +format_cb_pid(__unused struct format_tree *ft, struct format_entry *fe) +{ + xasprintf(&fe->value, "%ld", (long)getpid()); +} + +/* Callback for session_alerts. */ +void +format_cb_session_alerts(struct format_tree *ft, struct format_entry *fe) +{ + struct session *s = ft->s; + struct winlink *wl; + char alerts[256], tmp[16]; + + if (s == NULL) + return; + + *alerts = '\0'; + RB_FOREACH(wl, winlinks, &s->windows) { + if ((wl->flags & WINLINK_ALERTFLAGS) == 0) + continue; + xsnprintf(tmp, sizeof tmp, "%u", wl->idx); + + if (*alerts != '\0') + strlcat(alerts, ",", sizeof alerts); + strlcat(alerts, tmp, sizeof alerts); + if (wl->flags & WINLINK_ACTIVITY) + strlcat(alerts, "#", sizeof alerts); + if (wl->flags & WINLINK_BELL) + strlcat(alerts, "!", sizeof alerts); + if (wl->flags & WINLINK_SILENCE) + strlcat(alerts, "~", sizeof alerts); + } + fe->value = xstrdup(alerts); +} + +/* Callback for window_layout. */ +void +format_cb_window_layout(struct format_tree *ft, struct format_entry *fe) +{ + struct window *w = ft->w; + + if (w == NULL) + return; + + if (w->saved_layout_root != NULL) + fe->value = layout_dump(w->saved_layout_root); + else + fe->value = layout_dump(w->layout_root); +} + +/* Callback for window_visible_layout. */ +void +format_cb_window_visible_layout(struct format_tree *ft, struct format_entry *fe) +{ + struct window *w = ft->w; + + if (w == NULL) + return; + + fe->value = layout_dump(w->layout_root); +} + +/* Callback for pane_start_command. */ +void +format_cb_start_command(struct format_tree *ft, struct format_entry *fe) +{ + struct window_pane *wp = ft->wp; + + if (wp == NULL) + return; + + fe->value = cmd_stringify_argv(wp->argc, wp->argv); +} + +/* Callback for pane_current_command. */ +void +format_cb_current_command(struct format_tree *ft, struct format_entry *fe) +{ + struct window_pane *wp = ft->wp; + char *cmd; + + if (wp == NULL) + return; + + cmd = osdep_get_name(wp->fd, wp->tty); + if (cmd == NULL || *cmd == '\0') { + free(cmd); + cmd = cmd_stringify_argv(wp->argc, wp->argv); + if (cmd == NULL || *cmd == '\0') { + free(cmd); + cmd = xstrdup(wp->shell); + } + } + fe->value = parse_window_name(cmd); + free(cmd); +} + +/* Callback for pane_current_path. */ +void +format_cb_current_path(struct format_tree *ft, struct format_entry *fe) +{ + struct window_pane *wp = ft->wp; + char *cwd; + + if (wp == NULL) + return; + + cwd = osdep_get_cwd(wp->fd); + if (cwd != NULL) + fe->value = xstrdup(cwd); +} + +/* Callback for history_bytes. */ +void +format_cb_history_bytes(struct format_tree *ft, struct format_entry *fe) +{ + struct window_pane *wp = ft->wp; + struct grid *gd; + struct grid_line *gl; + unsigned long long size; + u_int i; + + if (wp == NULL) + return; + gd = wp->base.grid; + + size = 0; + for (i = 0; i < gd->hsize; i++) { + gl = &gd->linedata[i]; + size += gl->cellsize * sizeof *gl->celldata; + size += gl->extdsize * sizeof *gl->extddata; + } + size += gd->hsize * sizeof *gd->linedata; + + xasprintf(&fe->value, "%llu", size); +} + +/* Callback for pane_tabs. */ +void +format_cb_pane_tabs(struct format_tree *ft, struct format_entry *fe) +{ + struct window_pane *wp = ft->wp; + struct evbuffer *buffer; + u_int i; + int size; + + if (wp == NULL) + return; + + buffer = evbuffer_new(); + for (i = 0; i < wp->base.grid->sx; i++) { + if (!bit_test(wp->base.tabs, i)) + continue; + + if (EVBUFFER_LENGTH(buffer) > 0) + evbuffer_add(buffer, ",", 1); + evbuffer_add_printf(buffer, "%u", i); + } + size = EVBUFFER_LENGTH(buffer); + xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer)); + evbuffer_free(buffer); +} + /* Create a new tree. */ struct format_tree * -format_create(void) +format_create(struct cmd_q *cmdq, int flags) { struct format_tree *ft; - char host[HOST_NAME_MAX+1], *ptr; + + if (!event_initialized(&format_job_event)) { + evtimer_set(&format_job_event, format_job_timer, NULL); + format_job_timer(-1, 0, NULL); + } ft = xcalloc(1, sizeof *ft); RB_INIT(&ft->tree); + ft->flags = flags; - if (gethostname(host, sizeof host) == 0) { - format_add(ft, "host", "%s", host); - if ((ptr = strchr(host, '.')) != NULL) - *ptr = '\0'; - format_add(ft, "host_short", "%s", host); - } + format_add_cb(ft, "host", format_cb_host); + format_add_cb(ft, "host_short", format_cb_host_short); + format_add_cb(ft, "pid", format_cb_pid); + format_add(ft, "socket_path", "%s", socket_path); + format_add_tv(ft, "start_time", &start_time); + + if (cmdq != NULL && cmdq->cmd != NULL) + format_add(ft, "command_name", "%s", cmdq->cmd->entry->name); return (ft); } @@ -160,8 +514,8 @@ format_free(struct format_tree *ft) { struct format_entry *fe, *fe1; - RB_FOREACH_SAFE(fe, format_rb_tree, &ft->tree, fe1) { - RB_REMOVE(format_rb_tree, &ft->tree, fe); + RB_FOREACH_SAFE(fe, format_entry_tree, &ft->tree, fe1) { + RB_REMOVE(format_entry_tree, &ft->tree, fe); free(fe->value); free(fe->key); free(fe); @@ -181,53 +535,160 @@ format_add(struct format_tree *ft, const char *key, const char *fmt, ...) fe = xmalloc(sizeof *fe); fe->key = xstrdup(key); + fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe); + if (fe_now != NULL) { + free(fe->key); + free(fe); + free(fe_now->value); + fe = fe_now; + } + + fe->cb = NULL; + fe->t = 0; + va_start(ap, fmt); xvasprintf(&fe->value, fmt, ap); va_end(ap); +} - fe_now = RB_INSERT(format_rb_tree, &ft->tree, fe); +/* Add a key and time. */ +void +format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv) +{ + struct format_entry *fe; + struct format_entry *fe_now; + + fe = xmalloc(sizeof *fe); + fe->key = xstrdup(key); + + fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe); if (fe_now != NULL) { - free(fe_now->value); - fe_now->value = fe->value; free(fe->key); free(fe); + free(fe_now->value); + fe = fe_now; } + + fe->cb = NULL; + fe->t = tv->tv_sec; + + fe->value = NULL; +} + +/* Add a key and function. */ +void +format_add_cb(struct format_tree *ft, const char *key, format_cb cb) +{ + struct format_entry *fe; + struct format_entry *fe_now; + + fe = xmalloc(sizeof *fe); + fe->key = xstrdup(key); + + fe_now = RB_INSERT(format_entry_tree, &ft->tree, fe); + if (fe_now != NULL) { + free(fe->key); + free(fe); + free(fe_now->value); + fe = fe_now; + } + + fe->cb = cb; + fe->t = 0; + + fe->value = NULL; } /* Find a format entry. */ -const char * -format_find(struct format_tree *ft, const char *key) +char * +format_find(struct format_tree *ft, const char *key, int modifiers) { struct format_entry *fe, fe_find; struct options_entry *o; - static char s[16]; + struct environ_entry *envent; + static char s[64]; + const char *found; + char *copy, *saved; - o = options_find(&global_options, key); - if (o == NULL && ft->w != NULL) - o = options_find(&ft->w->options, key); - if (o == NULL) - o = options_find(&global_w_options, key); - if (o == NULL && ft->s != NULL) - o = options_find(&ft->s->options, key); - if (o == NULL) - o = options_find(&global_s_options, key); - if (o != NULL) { - switch (o->type) { - case OPTIONS_STRING: - return (o->str); - case OPTIONS_NUMBER: - snprintf(s, sizeof s, "%lld", o->num); - return (s); - case OPTIONS_STYLE: - return (style_tostring(&o->style)); + found = NULL; + + if (~modifiers & FORMAT_TIMESTRING) { + o = options_find(global_options, key); + if (o == NULL && ft->w != NULL) + o = options_find(ft->w->options, key); + if (o == NULL) + o = options_find(global_w_options, key); + if (o == NULL && ft->s != NULL) + o = options_find(ft->s->options, key); + if (o == NULL) + o = options_find(global_s_options, key); + if (o != NULL) { + switch (o->type) { + case OPTIONS_STRING: + found = o->str; + goto found; + case OPTIONS_NUMBER: + xsnprintf(s, sizeof s, "%lld", o->num); + found = s; + goto found; + case OPTIONS_STYLE: + found = style_tostring(&o->style); + goto found; + } } } fe_find.key = (char *) key; - fe = RB_FIND(format_rb_tree, &ft->tree, &fe_find); - if (fe == NULL) + fe = RB_FIND(format_entry_tree, &ft->tree, &fe_find); + if (fe != NULL) { + if (modifiers & FORMAT_TIMESTRING) { + if (fe->t == 0) + return (NULL); + ctime_r(&fe->t, s); + s[strcspn(s, "\n")] = '\0'; + found = s; + goto found; + } + if (fe->t != 0) { + xsnprintf(s, sizeof s, "%lld", (long long)fe->t); + found = s; + goto found; + } + if (fe->value == NULL && fe->cb != NULL) + fe->cb(ft, fe); + found = fe->value; + goto found; + } + + if (~modifiers & FORMAT_TIMESTRING) { + envent = NULL; + if (ft->s != NULL) + envent = environ_find(ft->s->environ, key); + if (envent == NULL) + envent = environ_find(global_environ, key); + if (envent != NULL) { + found = envent->value; + goto found; + } + } + + return (NULL); + +found: + if (found == NULL) return (NULL); - return (fe->value); + copy = xstrdup(found); + if (modifiers & FORMAT_BASENAME) { + saved = copy; + copy = xstrdup(basename(saved)); + free(saved); + } + if (modifiers & FORMAT_DIRNAME) { + saved = copy; + copy = xstrdup(dirname(saved)); + free(saved); + } + return (copy); } /* @@ -238,10 +699,11 @@ int format_replace(struct format_tree *ft, const char *key, size_t keylen, char **buf, size_t *len, size_t *off) { - char *copy, *copy0, *endptr, *ptr, *saved, *trimmed; - const char *value; - size_t valuelen; - u_long limit = 0; + char *copy, *copy0, *endptr, *ptr, *found, *new, *value; + char *from = NULL, *to = NULL; + size_t valuelen, newlen, fromlen, tolen, used; + long limit = 0; + int modifiers = 0, brackets; /* Make a copy of the key. */ copy0 = copy = xmalloc(keylen + 1); @@ -249,24 +711,57 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen, copy[keylen] = '\0'; /* Is there a length limit or whatnot? */ - if (!islower((u_char) *copy) && *copy != '@' && *copy != '?') { - while (*copy != ':' && *copy != '\0') { - switch (*copy) { - case '=': - errno = 0; - limit = strtoul(copy + 1, &endptr, 10); - if (errno == ERANGE && limit == ULONG_MAX) - goto fail; - copy = endptr; - break; - default: - copy++; - break; - } + switch (copy[0]) { + case '=': + errno = 0; + limit = strtol(copy + 1, &endptr, 10); + if (errno == ERANGE && (limit == LONG_MIN || limit == LONG_MAX)) + break; + if (*endptr != ':') + break; + copy = endptr + 1; + break; + case 'b': + if (copy[1] != ':') + break; + modifiers |= FORMAT_BASENAME; + copy += 2; + break; + case 'd': + if (copy[1] != ':') + break; + modifiers |= FORMAT_DIRNAME; + copy += 2; + break; + case 't': + if (copy[1] != ':') + break; + modifiers |= FORMAT_TIMESTRING; + copy += 2; + break; + case 's': + if (copy[1] != '/') + break; + from = copy + 2; + for (copy = from; *copy != '\0' && *copy != '/'; copy++) + /* nothing */; + if (copy[0] != '/' || copy == from) { + copy = copy0; + break; } - if (*copy != ':') - goto fail; - copy++; + copy[0] = '\0'; + to = copy + 1; + for (copy = to; *copy != '\0' && *copy != '/'; copy++) + /* nothing */; + if (copy[0] != '/' || copy[1] != ':') { + copy = copy0; + break; + } + copy[0] = '\0'; + + modifiers |= FORMAT_SUBSTITUTE; + copy += 2; + break; } /* @@ -279,38 +774,75 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen, goto fail; *ptr = '\0'; - value = format_find(ft, copy + 1); - if (value != NULL && *value != '\0' && - (value[0] != '0' || value[1] != '\0')) { - value = ptr + 1; - ptr = strchr(value, ','); - if (ptr == NULL) - goto fail; - *ptr = '\0'; - } else { - ptr = strchr(ptr + 1, ','); - if (ptr == NULL) - goto fail; - value = ptr + 1; + value = ptr + 1; + found = format_find(ft, copy + 1, modifiers); + + brackets = 0; + for (ptr = ptr + 1; *ptr != '\0'; ptr++) { + if (*ptr == '{') + brackets++; + if (*ptr == '}') + brackets--; + if (*ptr == ',' && brackets == 0) + break; } - saved = format_expand(ft, value); - value = saved; + if (*ptr == '\0') + goto fail; + + if (found != NULL && *found != '\0' && + (found[0] != '0' || found[1] != '\0')) { + *ptr = '\0'; + } else + value = ptr + 1; + value = format_expand(ft, value); + free(found); } else { - value = format_find(ft, copy); + value = format_find(ft, copy, modifiers); if (value == NULL) - value = ""; - saved = NULL; + value = xstrdup(""); + } + + /* Perform substitution if any. */ + if (modifiers & FORMAT_SUBSTITUTE) { + fromlen = strlen(from); + tolen = strlen(to); + + newlen = strlen(value) + 1; + copy = new = xmalloc(newlen); + for (ptr = value; *ptr != '\0'; /* nothing */) { + if (strncmp(ptr, from, fromlen) != 0) { + *new++ = *ptr++; + continue; + } + used = new - copy; + + newlen += tolen; + copy = xrealloc(copy, newlen); + + new = copy + used; + memcpy(new, to, tolen); + + new += tolen; + ptr += fromlen; + } + *new = '\0'; + free(value); + value = copy; } /* Truncate the value if needed. */ - if (limit != 0) { - value = trimmed = utf8_trimcstr(value, limit); - free(saved); - saved = trimmed; + if (limit > 0) { + new = utf8_trimcstr(value, limit); + free(value); + value = new; + } else if (limit < 0) { + new = utf8_rtrimcstr(value, -limit); + free(value); + value = new; } - valuelen = strlen(value); /* Expand the buffer and copy in the value. */ + valuelen = strlen(value); while (*len - *off < valuelen + 1) { *buf = xreallocarray(*buf, 2, *len); *len *= 2; @@ -318,7 +850,7 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen, memcpy(*buf + *off, value, valuelen); *off += valuelen; - free(saved); + free(value); free(copy0); return (0); @@ -335,7 +867,7 @@ format_expand_time(struct format_tree *ft, const char *fmt, time_t t) size_t tmplen; struct tm *tm; - if (fmt == NULL) + if (fmt == NULL || *fmt == '\0') return (xstrdup("")); tm = localtime(&t); @@ -358,14 +890,18 @@ format_expand_time(struct format_tree *ft, const char *fmt, time_t t) char * format_expand(struct format_tree *ft, const char *fmt) { - char *buf; - const char *ptr, *s; - size_t off, len, n; + char *buf, *tmp, *cmd, *out; + const char *ptr, *s, *saved = fmt; + size_t off, len, n, outlen; int ch, brackets; if (fmt == NULL) return (xstrdup("")); +#ifdef TMATE + tmate_format(ft); +#endif + len = 64; buf = xmalloc(len); off = 0; @@ -383,6 +919,40 @@ format_expand(struct format_tree *ft, const char *fmt) ch = (u_char) *fmt++; switch (ch) { + case '(': + brackets = 1; + for (ptr = fmt; *ptr != '\0'; ptr++) { + if (*ptr == '(') + brackets++; + if (*ptr == ')' && --brackets == 0) + break; + } + if (*ptr != ')' || brackets != 0) + break; + n = ptr - fmt; + + tmp = xmalloc(n + 1); + memcpy(tmp, fmt, n); + tmp[n] = '\0'; + cmd = format_expand(ft, tmp); + + out = format_job_get(ft, cmd); + outlen = strlen(out); + + free(cmd); + free(tmp); + + while (len - off < outlen + 1) { + buf = xreallocarray(buf, 2, len); + len *= 2; + } + memcpy(buf + off, out, outlen); + off += outlen; + + free(out); + + fmt += n + 1; + continue; case '{': brackets = 1; for (ptr = fmt; *ptr != '\0'; ptr++) { @@ -431,29 +1001,10 @@ format_expand(struct format_tree *ft, const char *fmt) } buf[off] = '\0'; + log_debug("format '%s' -> '%s'", saved, buf); return (buf); } -/* Get command name for format. */ -char * -format_get_command(struct window_pane *wp) -{ - char *cmd, *out; - - cmd = osdep_get_name(wp->fd, wp->tty); - if (cmd == NULL || *cmd == '\0') { - free(cmd); - cmd = cmd_stringify_argv(wp->argc, wp->argv); - if (cmd == NULL || *cmd == '\0') { - free(cmd); - cmd = xstrdup(wp->shell); - } - } - out = parse_window_name(cmd); - free(cmd); - return (out); -} - /* Set defaults for any of arguments that are not NULL. */ void format_defaults(struct format_tree *ft, struct client *c, struct session *s, @@ -481,8 +1032,6 @@ void format_defaults_session(struct format_tree *ft, struct session *s) { struct session_group *sg; - char *tim; - time_t t; ft->s = s; @@ -497,47 +1046,45 @@ format_defaults_session(struct format_tree *ft, struct session *s) if (sg != NULL) format_add(ft, "session_group", "%u", session_group_index(sg)); - t = s->creation_time.tv_sec; - format_add(ft, "session_created", "%lld", (long long) t); - tim = ctime(&t); - *strchr(tim, '\n') = '\0'; - format_add(ft, "session_created_string", "%s", tim); + format_add_tv(ft, "session_created", &s->creation_time); + format_add_tv(ft, "session_last_attached", &s->last_attached_time); + format_add_tv(ft, "session_activity", &s->activity_time); format_add(ft, "session_attached", "%u", s->attached); - format_add(ft, "session_many_attached", "%u", s->attached > 1); + format_add(ft, "session_many_attached", "%d", s->attached > 1); + + format_add_cb(ft, "session_alerts", format_cb_session_alerts); } /* Set default format keys for a client. */ void format_defaults_client(struct format_tree *ft, struct client *c) { - char *tim; - time_t t; struct session *s; + const char *name; if (ft->s == NULL) ft->s = c->session; + format_add(ft, "client_pid", "%ld", (long) c->pid); format_add(ft, "client_height", "%u", c->tty.sy); format_add(ft, "client_width", "%u", c->tty.sx); if (c->tty.path != NULL) format_add(ft, "client_tty", "%s", c->tty.path); if (c->tty.termname != NULL) format_add(ft, "client_termname", "%s", c->tty.termname); + format_add(ft, "client_control_mode", "%d", + !!(c->flags & CLIENT_CONTROL)); - t = c->creation_time.tv_sec; - format_add(ft, "client_created", "%lld", (long long) t); - tim = ctime(&t); - *strchr(tim, '\n') = '\0'; - format_add(ft, "client_created_string", "%s", tim); + format_add_tv(ft, "client_created", &c->creation_time); + format_add_tv(ft, "client_activity", &c->activity_time); - t = c->activity_time.tv_sec; - format_add(ft, "client_activity", "%lld", (long long) t); - tim = ctime(&t); - *strchr(tim, '\n') = '\0'; - format_add(ft, "client_activity_string", "%s", tim); - - format_add(ft, "client_prefix", "%d", !!(c->flags & CLIENT_PREFIX)); + name = server_client_get_key_table(c); + if (strcmp(c->keytable->name, name) == 0) + format_add(ft, "client_prefix", "%d", 0); + else + format_add(ft, "client_prefix", "%d", 1); + format_add(ft, "client_key_table", "%s", c->keytable->name); if (c->tty.flags & TTY_UTF8) format_add(ft, "client_utf8", "%d", 1); @@ -561,22 +1108,19 @@ format_defaults_client(struct format_tree *ft, struct client *c) void format_defaults_window(struct format_tree *ft, struct window *w) { - char *layout; - ft->w = w; - layout = layout_dump(w); - + format_add_tv(ft, "window_activity", &w->activity_time); format_add(ft, "window_id", "@%u", w->id); format_add(ft, "window_name", "%s", w->name); format_add(ft, "window_width", "%u", w->sx); format_add(ft, "window_height", "%u", w->sy); - format_add(ft, "window_layout", "%s", layout); + format_add_cb(ft, "window_layout", format_cb_window_layout); + format_add_cb(ft, "window_visible_layout", + format_cb_window_visible_layout); format_add(ft, "window_panes", "%u", window_count_panes(w)); - format_add(ft, "window_zoomed_flag", "%u", + format_add(ft, "window_zoomed_flag", "%d", !!(w->flags & WINDOW_ZOOMED)); - - free(layout); } /* Set default format keys for a winlink. */ @@ -598,63 +1142,34 @@ format_defaults_winlink(struct format_tree *ft, struct session *s, format_add(ft, "window_flags", "%s", flags); format_add(ft, "window_active", "%d", wl == s->curw); - format_add(ft, "window_bell_flag", "%u", + format_add(ft, "window_bell_flag", "%d", !!(wl->flags & WINLINK_BELL)); - format_add(ft, "window_activity_flag", "%u", + format_add(ft, "window_activity_flag", "%d", !!(wl->flags & WINLINK_ACTIVITY)); - format_add(ft, "window_silence_flag", "%u", + format_add(ft, "window_silence_flag", "%d", !!(wl->flags & WINLINK_SILENCE)); - format_add(ft, "window_last_flag", "%u", + format_add(ft, "window_last_flag", "%d", !!(wl == TAILQ_FIRST(&s->lastw))); + format_add(ft, "window_linked", "%d", session_is_linked(s, wl->window)); free(flags); } -/* Add window pane tabs. */ -void -format_defaults_pane_tabs(struct format_tree *ft, struct window_pane *wp) -{ - struct evbuffer *buffer; - u_int i; - - buffer = evbuffer_new(); - for (i = 0; i < wp->base.grid->sx; i++) { - if (!bit_test(wp->base.tabs, i)) - continue; - - if (EVBUFFER_LENGTH(buffer) > 0) - evbuffer_add(buffer, ",", 1); - evbuffer_add_printf(buffer, "%d", i); - } - - format_add(ft, "pane_tabs", "%.*s", (int) EVBUFFER_LENGTH(buffer), - EVBUFFER_DATA(buffer)); - evbuffer_free(buffer); -} - /* Set default format keys for a window pane. */ void format_defaults_pane(struct format_tree *ft, struct window_pane *wp) { - struct grid *gd = wp->base.grid; - struct grid_line *gl; - unsigned long long size; - u_int i, idx; - char *cmd, *cwd; - int status; + struct grid *gd = wp->base.grid; + u_int idx; + int status, scroll_position; if (ft->w == NULL) ft->w = wp->window; + ft->wp = wp; - size = 0; - for (i = 0; i < gd->hsize; i++) { - gl = &gd->linedata[i]; - size += gl->cellsize * sizeof *gl->celldata; - } - size += gd->hsize * sizeof *gd->linedata; format_add(ft, "history_size", "%u", gd->hsize); format_add(ft, "history_limit", "%u", gd->hlimit); - format_add(ft, "history_bytes", "%llu", size); + format_add_cb(ft, "history_bytes", format_cb_history_bytes); if (window_pane_index(wp, &idx) != 0) fatalx("index not found"); @@ -681,32 +1196,26 @@ format_defaults_pane(struct format_tree *ft, struct window_pane *wp) format_add(ft, "pane_in_mode", "%d", wp->screen != &wp->base); format_add(ft, "pane_synchronized", "%d", - !!options_get_number(&wp->window->options, "synchronize-panes")); + !!options_get_number(wp->window->options, "synchronize-panes")); - if (wp->tty != NULL) - format_add(ft, "pane_tty", "%s", wp->tty); + format_add(ft, "pane_tty", "%s", wp->tty); format_add(ft, "pane_pid", "%ld", (long) wp->pid); - if ((cwd = osdep_get_cwd(wp->fd)) != NULL) - format_add(ft, "pane_current_path", "%s", cwd); - if ((cmd = cmd_stringify_argv(wp->argc, wp->argv)) != NULL) { - format_add(ft, "pane_start_command", "%s", cmd); - free(cmd); - } - if ((cmd = format_get_command(wp)) != NULL) { - format_add(ft, "pane_current_command", "%s", cmd); - free(cmd); - } + format_add_cb(ft, "pane_start_command", format_cb_start_command); + format_add_cb(ft, "pane_current_command", format_cb_current_command); + format_add_cb(ft, "pane_current_path", format_cb_current_path); - format_add(ft, "cursor_x", "%d", wp->base.cx); - format_add(ft, "cursor_y", "%d", wp->base.cy); - format_add(ft, "scroll_region_upper", "%d", wp->base.rupper); - format_add(ft, "scroll_region_lower", "%d", wp->base.rlower); - format_add(ft, "saved_cursor_x", "%d", wp->ictx.old_cx); - format_add(ft, "saved_cursor_y", "%d", wp->ictx.old_cy); + format_add(ft, "cursor_x", "%u", wp->base.cx); + format_add(ft, "cursor_y", "%u", wp->base.cy); + format_add(ft, "scroll_region_upper", "%u", wp->base.rupper); + format_add(ft, "scroll_region_lower", "%u", wp->base.rlower); + + scroll_position = window_copy_scroll_position(wp); + if (scroll_position != -1) + format_add(ft, "scroll_position", "%d", scroll_position); format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0); - format_add(ft, "alternate_saved_x", "%d", wp->saved_cx); - format_add(ft, "alternate_saved_y", "%d", wp->saved_cy); + format_add(ft, "alternate_saved_x", "%u", wp->saved_cx); + format_add(ft, "alternate_saved_y", "%u", wp->saved_cy); format_add(ft, "cursor_flag", "%d", !!(wp->base.mode & MODE_CURSOR)); @@ -719,27 +1228,28 @@ format_defaults_pane(struct format_tree *ft, struct window_pane *wp) format_add(ft, "wrap_flag", "%d", !!(wp->base.mode & MODE_WRAP)); + format_add(ft, "mouse_any_flag", "%d", + !!(wp->base.mode & (MODE_MOUSE_STANDARD|MODE_MOUSE_BUTTON))); format_add(ft, "mouse_standard_flag", "%d", !!(wp->base.mode & MODE_MOUSE_STANDARD)); format_add(ft, "mouse_button_flag", "%d", !!(wp->base.mode & MODE_MOUSE_BUTTON)); - format_add(ft, "mouse_utf8_flag", "%d", - !!(wp->base.mode & MODE_MOUSE_UTF8)); - format_defaults_pane_tabs(ft, wp); + format_add_cb(ft, "pane_tabs", format_cb_pane_tabs); } /* Set default format keys for paste buffer. */ void -format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb, - int utf8flag) +format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb) { + size_t bufsize; char *s; - format_add(ft, "buffer_size", "%zu", pb->size); - format_add(ft, "buffer_name", "%s", pb->name); + paste_buffer_data(pb, &bufsize); + format_add(ft, "buffer_size", "%zu", bufsize); + format_add(ft, "buffer_name", "%s", paste_buffer_name(pb)); - s = paste_make_sample(pb, utf8flag); + s = paste_make_sample(pb); format_add(ft, "buffer_sample", "%s", s); free(s); } diff --git a/grid-cell.c b/grid-cell.c deleted file mode 100644 index 09643a9c..00000000 --- a/grid-cell.c +++ /dev/null @@ -1,55 +0,0 @@ -/* $OpenBSD$ */ - -/* - * Copyright (c) 2012 Nicholas Marriott - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER - * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING - * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include - -#include - -#include "tmux.h" - -/* Get cell width. */ -u_int -grid_cell_width(const struct grid_cell *gc) -{ - return (gc->xstate >> 4); -} - -/* Get cell data. */ -void -grid_cell_get(const struct grid_cell *gc, struct utf8_data *ud) -{ - ud->size = gc->xstate & 0xf; - ud->width = gc->xstate >> 4; - memcpy(ud->data, gc->xdata, ud->size); -} - -/* Set cell data. */ -void -grid_cell_set(struct grid_cell *gc, const struct utf8_data *ud) -{ - memcpy(gc->xdata, ud->data, ud->size); - gc->xstate = (ud->width << 4) | ud->size; -} - -/* Set a single character as cell data. */ -void -grid_cell_one(struct grid_cell *gc, u_char ch) -{ - *gc->xdata = ch; - gc->xstate = (1 << 4) | 1; -} diff --git a/grid-view.c b/grid-view.c index badabd56..0989f800 100644 --- a/grid-view.c +++ b/grid-view.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -30,24 +30,17 @@ #define grid_view_x(gd, x) (x) #define grid_view_y(gd, y) ((gd)->hsize + (y)) -/* Get cell for reading. */ -const struct grid_cell * -grid_view_peek_cell(struct grid *gd, u_int px, u_int py) +/* Get cell. */ +void +grid_view_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc) { - return (grid_peek_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py))); -} - -/* Get cell for writing. */ -struct grid_cell * -grid_view_get_cell(struct grid *gd, u_int px, u_int py) -{ - return (grid_get_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py))); + grid_get_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py), gc); } /* Set cell. */ void -grid_view_set_cell( - struct grid *gd, u_int px, u_int py, const struct grid_cell *gc) +grid_view_set_cell(struct grid *gd, u_int px, u_int py, + const struct grid_cell *gc) { grid_set_cell(gd, grid_view_x(gd, px), grid_view_y(gd, py), gc); } diff --git a/grid.c b/grid.c index ef7c374b..0be0254f 100644 --- a/grid.c +++ b/grid.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -36,28 +36,34 @@ */ /* Default grid cell data. */ -const struct grid_cell grid_default_cell = { 0, 0, 8, 8, (1 << 4) | 1, " " }; - -#define grid_put_cell(gd, px, py, gc) do { \ - memcpy(&gd->linedata[py].celldata[px], \ - gc, sizeof gd->linedata[py].celldata[px]); \ -} while (0) -#define grid_put_utf8(gd, px, py, gc) do { \ - memcpy(&gd->linedata[py].utf8data[px], \ - gc, sizeof gd->linedata[py].utf8data[px]); \ -} while (0) +const struct grid_cell grid_default_cell = { + 0, 0, { .fg = 8 }, { .bg = 8 }, { { ' ' }, 0, 1, 1 } +}; +const struct grid_cell_entry grid_default_entry = { + 0, { .data = { 0, 8, 8, ' ' } } +}; int grid_check_y(struct grid *, u_int); -#ifdef DEBUG -int -grid_check_y(struct grid *gd, u_int py) +void grid_reflow_copy(struct grid_line *, u_int, struct grid_line *l, + u_int, u_int); +void grid_reflow_join(struct grid *, u_int *, struct grid_line *, u_int); +void grid_reflow_split(struct grid *, u_int *, struct grid_line *, u_int, + u_int); +void grid_reflow_move(struct grid *, u_int *, struct grid_line *); +size_t grid_string_cells_fg(const struct grid_cell *, int *); +size_t grid_string_cells_bg(const struct grid_cell *, int *); +void grid_string_cells_code(const struct grid_cell *, + const struct grid_cell *, char *, size_t, int); + +/* Copy default into a cell. */ +static void +grid_clear_cell(struct grid *gd, u_int px, u_int py) { - if ((py) >= (gd)->hsize + (gd)->sy) - log_fatalx("y out of range: %u", py); - return (0); + gd->linedata[py].celldata[px] = grid_default_entry; } -#else + +/* Check grid y position. */ int grid_check_y(struct grid *gd, u_int py) { @@ -67,16 +73,6 @@ grid_check_y(struct grid *gd, u_int py) } return (0); } -#endif - -void grid_reflow_join(struct grid *, u_int *, struct grid_line *, u_int); -void grid_reflow_split(struct grid *, u_int *, struct grid_line *, u_int, - u_int); -void grid_reflow_move(struct grid *, u_int *, struct grid_line *); -size_t grid_string_cells_fg(const struct grid_cell *, int *); -size_t grid_string_cells_bg(const struct grid_cell *, int *); -void grid_string_cells_code(const struct grid_cell *, - const struct grid_cell *, char *, size_t, int); /* Create a new grid. */ struct grid * @@ -108,6 +104,7 @@ grid_destroy(struct grid *gd) for (yy = 0; yy < gd->hsize + gd->sy; yy++) { gl = &gd->linedata[yy]; free(gl->celldata); + free(gl->extddata); } free(gd->linedata); @@ -120,7 +117,7 @@ int grid_compare(struct grid *ga, struct grid *gb) { struct grid_line *gla, *glb; - struct grid_cell *gca, *gcb; + struct grid_cell gca, gcb; u_int xx, yy; if (ga->sx != gb->sx || ga->sy != gb->sy) @@ -131,10 +128,10 @@ grid_compare(struct grid *ga, struct grid *gb) glb = &gb->linedata[yy]; if (gla->cellsize != glb->cellsize) return (1); - for (xx = 0; xx < ga->sx; xx++) { - gca = &gla->celldata[xx]; - gcb = &glb->celldata[xx]; - if (memcmp(gca, gcb, sizeof (struct grid_cell)) != 0) + for (xx = 0; xx < gla->cellsize; xx++) { + grid_get_cell(ga, xx, yy, &gca); + grid_get_cell(gb, xx, yy, &gcb); + if (memcmp(&gca, &gcb, sizeof (struct grid_cell)) != 0) return (1); } } @@ -179,6 +176,18 @@ grid_scroll_history(struct grid *gd) gd->hsize++; } +/* Clear the history. */ +void +grid_clear_history(struct grid *gd) +{ + grid_clear_lines(gd, 0, gd->hsize); + grid_move_lines(gd, 0, gd->hsize, gd->sy); + + gd->hsize = 0; + gd->linedata = xreallocarray(gd->linedata, gd->sy, + sizeof *gd->linedata); +} + /* Scroll a region up, moving the top line into the history. */ void grid_scroll_history_region(struct grid *gd, u_int upper, u_int lower) @@ -225,7 +234,7 @@ grid_expand_line(struct grid *gd, u_int py, u_int sx) gl->celldata = xreallocarray(gl->celldata, sx, sizeof *gl->celldata); for (xx = gl->cellsize; xx < sx; xx++) - grid_put_cell(gd, xx, py, &grid_default_cell); + grid_clear_cell(gd, xx, py); gl->cellsize = sx; } @@ -239,37 +248,77 @@ grid_peek_line(struct grid *gd, u_int py) } /* Get cell for reading. */ -const struct grid_cell * -grid_peek_cell(struct grid *gd, u_int px, u_int py) +void +grid_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc) { - if (grid_check_y(gd, py) != 0) - return (&grid_default_cell); + struct grid_line *gl; + struct grid_cell_entry *gce; - if (px >= gd->linedata[py].cellsize) - return (&grid_default_cell); - return (&gd->linedata[py].celldata[px]); -} + if (grid_check_y(gd, py) != 0 || px >= gd->linedata[py].cellsize) { + memcpy(gc, &grid_default_cell, sizeof *gc); + return; + } -/* Get cell at relative position (for writing). */ -struct grid_cell * -grid_get_cell(struct grid *gd, u_int px, u_int py) -{ - if (grid_check_y(gd, py) != 0) - return (NULL); + gl = &gd->linedata[py]; + gce = &gl->celldata[px]; - grid_expand_line(gd, py, px + 1); - return (&gd->linedata[py].celldata[px]); + if (gce->flags & GRID_FLAG_EXTENDED) { + if (gce->offset >= gl->extdsize) + memcpy(gc, &grid_default_cell, sizeof *gc); + else + memcpy(gc, &gl->extddata[gce->offset], sizeof *gc); + return; + } + + gc->flags = gce->flags & ~GRID_FLAG_EXTENDED; + gc->attr = gce->data.attr; + gc->fg = gce->data.fg; + gc->bg = gce->data.bg; + utf8_set(&gc->data, gce->data.data); } /* Set cell at relative position. */ void grid_set_cell(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc) { + struct grid_line *gl; + struct grid_cell_entry *gce; + struct grid_cell *gcp; + int extended; + if (grid_check_y(gd, py) != 0) return; grid_expand_line(gd, py, px + 1); - grid_put_cell(gd, px, py, gc); + + gl = &gd->linedata[py]; + gce = &gl->celldata[px]; + + extended = (gce->flags & GRID_FLAG_EXTENDED); + if (!extended && (gc->data.size != 1 || gc->data.width != 1)) + extended = 1; + if (!extended && (gc->flags & (GRID_FLAG_FGRGB|GRID_FLAG_BGRGB))) + extended = 1; + if (extended) { + if (~gce->flags & GRID_FLAG_EXTENDED) { + gl->extddata = xreallocarray(gl->extddata, + gl->extdsize + 1, sizeof *gl->extddata); + gce->offset = gl->extdsize++; + gce->flags = gc->flags | GRID_FLAG_EXTENDED; + } + + if (gce->offset >= gl->extdsize) + fatalx("offset too big"); + gcp = &gl->extddata[gce->offset]; + memcpy(gcp, gc, sizeof *gcp); + return; + } + + gce->flags = gc->flags & ~GRID_FLAG_EXTENDED; + gce->data.attr = gc->attr; + gce->data.fg = gc->fg; + gce->data.bg = gc->bg; + gce->data.data = gc->data.data[0]; } /* Clear area. */ @@ -301,7 +350,7 @@ grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny) for (xx = px; xx < px + nx; xx++) { if (xx >= gd->linedata[yy].cellsize) break; - grid_put_cell(gd, xx, yy, &grid_default_cell); + grid_clear_cell(gd, xx, yy); } } } @@ -324,6 +373,7 @@ grid_clear_lines(struct grid *gd, u_int py, u_int ny) for (yy = py; yy < py + ny; yy++) { gl = &gd->linedata[yy]; free(gl->celldata); + free(gl->extddata); memset(gl, 0, sizeof *gl); } } @@ -353,8 +403,8 @@ grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny) grid_clear_lines(gd, yy, 1); } - memmove( - &gd->linedata[dy], &gd->linedata[py], ny * (sizeof *gd->linedata)); + memmove(&gd->linedata[dy], &gd->linedata[py], + ny * (sizeof *gd->linedata)); /* Wipe any lines that have been moved (without freeing them). */ for (yy = py; yy < py + ny; yy++) { @@ -380,14 +430,14 @@ grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx) grid_expand_line(gd, py, px + nx); grid_expand_line(gd, py, dx + nx); - memmove( - &gl->celldata[dx], &gl->celldata[px], nx * sizeof *gl->celldata); + memmove(&gl->celldata[dx], &gl->celldata[px], + nx * sizeof *gl->celldata); /* Wipe any cells that have been moved. */ for (xx = px; xx < px + nx; xx++) { if (xx >= dx && xx < dx + nx) continue; - grid_put_cell(gd, xx, py, &grid_default_cell); + grid_clear_cell(gd, xx, py); } } @@ -402,31 +452,37 @@ grid_string_cells_fg(const struct grid_cell *gc, int *values) values[n++] = 38; values[n++] = 5; values[n++] = gc->fg; + } else if (gc->flags & GRID_FLAG_FGRGB) { + values[n++] = 38; + values[n++] = 2; + values[n++] = gc->fg_rgb.r; + values[n++] = gc->fg_rgb.g; + values[n++] = gc->fg_rgb.b; } else { switch (gc->fg) { - case 0: - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - values[n++] = gc->fg + 30; - break; - case 8: - values[n++] = 39; - break; - case 90: - case 91: - case 92: - case 93: - case 94: - case 95: - case 96: - case 97: - values[n++] = gc->fg; - break; + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + values[n++] = gc->fg + 30; + break; + case 8: + values[n++] = 39; + break; + case 90: + case 91: + case 92: + case 93: + case 94: + case 95: + case 96: + case 97: + values[n++] = gc->fg; + break; } } return (n); @@ -443,6 +499,12 @@ grid_string_cells_bg(const struct grid_cell *gc, int *values) values[n++] = 48; values[n++] = 5; values[n++] = gc->bg; + } else if (gc->flags & GRID_FLAG_BGRGB) { + values[n++] = 48; + values[n++] = 2; + values[n++] = gc->bg_rgb.r; + values[n++] = gc->bg_rgb.g; + values[n++] = gc->bg_rgb.b; } else { switch (gc->bg) { case 0: @@ -482,7 +544,7 @@ void grid_string_cells_code(const struct grid_cell *lastgc, const struct grid_cell *gc, char *buf, size_t len, int escape_c0) { - int oldc[16], newc[16], s[32]; + int oldc[64], newc[64], s[128]; size_t noldc, nnewc, n, i; u_int attr = gc->attr; u_int lastattr = lastgc->attr; @@ -569,9 +631,8 @@ char * grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx, struct grid_cell **lastgc, int with_codes, int escape_c0, int trim) { - const struct grid_cell *gc; + struct grid_cell gc; static struct grid_cell lastgc1; - struct utf8_data ud; const char *data; char *buf, code[128]; size_t len, off, size, codelen; @@ -591,21 +652,20 @@ grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx, for (xx = px; xx < px + nx; xx++) { if (gl == NULL || xx >= gl->cellsize) break; - gc = &gl->celldata[xx]; - if (gc->flags & GRID_FLAG_PADDING) + grid_get_cell(gd, xx, py, &gc); + if (gc.flags & GRID_FLAG_PADDING) continue; - grid_cell_get(gc, &ud); if (with_codes) { - grid_string_cells_code(*lastgc, gc, code, sizeof code, + grid_string_cells_code(*lastgc, &gc, code, sizeof code, escape_c0); codelen = strlen(code); - memcpy(*lastgc, gc, sizeof *gc); + memcpy(*lastgc, &gc, sizeof **lastgc); } else codelen = 0; - data = ud.data; - size = ud.size; + data = gc.data.data; + size = gc.data.size; if (escape_c0 && size == 1 && *data == '\\') { data = "\\\\"; size = 2; @@ -657,10 +717,19 @@ grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy, memcpy(dstl, srcl, sizeof *dstl); if (srcl->cellsize != 0) { - dstl->celldata = xcalloc( + dstl->celldata = xreallocarray(NULL, srcl->cellsize, sizeof *dstl->celldata); memcpy(dstl->celldata, srcl->celldata, srcl->cellsize * sizeof *dstl->celldata); + } else + dstl->celldata = NULL; + + if (srcl->extdsize != 0) { + dstl->extdsize = srcl->extdsize; + dstl->extddata = xreallocarray(NULL, dstl->extdsize, + sizeof *dstl->extddata); + memcpy(dstl->extddata, srcl->extddata, dstl->extdsize * + sizeof *dstl->extddata); } sy++; @@ -668,6 +737,31 @@ grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy, } } +/* Copy a section of a line. */ +void +grid_reflow_copy(struct grid_line *dst_gl, u_int to, struct grid_line *src_gl, + u_int from, u_int to_copy) +{ + struct grid_cell_entry *gce; + u_int i, was; + + memcpy(&dst_gl->celldata[to], &src_gl->celldata[from], + to_copy * sizeof *dst_gl->celldata); + + for (i = to; i < to + to_copy; i++) { + gce = &dst_gl->celldata[i]; + if (~gce->flags & GRID_FLAG_EXTENDED) + continue; + was = gce->offset; + + dst_gl->extddata = xreallocarray(dst_gl->extddata, + dst_gl->extdsize + 1, sizeof *dst_gl->extddata); + gce->offset = dst_gl->extdsize++; + memcpy(&dst_gl->extddata[gce->offset], &src_gl->extddata[was], + sizeof *dst_gl->extddata); + } +} + /* Join line data. */ void grid_reflow_join(struct grid *dst, u_int *py, struct grid_line *src_gl, @@ -692,8 +786,7 @@ grid_reflow_join(struct grid *dst, u_int *py, struct grid_line *src_gl, dst_gl->cellsize = nx; /* Append as much as possible. */ - memcpy(&dst_gl->celldata[ox], &src_gl->celldata[0], - to_copy * sizeof src_gl->celldata[0]); + grid_reflow_copy(dst_gl, ox, src_gl, 0, to_copy); /* If there is any left in the source, split it. */ if (src_gl->cellsize > to_copy) { @@ -732,8 +825,7 @@ grid_reflow_split(struct grid *dst, u_int *py, struct grid_line *src_gl, dst_gl->flags |= GRID_LINE_WRAPPED; /* Copy the data. */ - memcpy(&dst_gl->celldata[0], &src_gl->celldata[offset], - to_copy * sizeof dst_gl->celldata[0]); + grid_reflow_copy(dst_gl, 0, src_gl, offset, to_copy); /* Move offset and reduce old line size. */ offset += to_copy; @@ -763,6 +855,7 @@ grid_reflow_move(struct grid *dst, u_int *py, struct grid_line *src_gl) /* Clear old line. */ src_gl->celldata = NULL; + src_gl->extddata = NULL; } /* @@ -792,7 +885,7 @@ grid_reflow(struct grid *dst, struct grid *src, u_int new_x) /* Previous was wrapped. Try to join. */ grid_reflow_join(dst, &py, src_gl, new_x); } - previous_wrapped = src_gl->flags & GRID_LINE_WRAPPED; + previous_wrapped = (src_gl->flags & GRID_LINE_WRAPPED); } grid_destroy(src); diff --git a/hooks.c b/hooks.c new file mode 100644 index 00000000..c1a016ae --- /dev/null +++ b/hooks.c @@ -0,0 +1,226 @@ +/* $OpenBSD$ */ + +/* + * Copyright (c) 2012 Thomas Adam + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include +#include + +#include "tmux.h" + +struct hooks { + RB_HEAD(hooks_tree, hook) tree; + struct hooks *parent; +}; + +static int hooks_cmp(struct hook *, struct hook *); +RB_PROTOTYPE(hooks_tree, hook, entry, hooks_cmp); +RB_GENERATE(hooks_tree, hook, entry, hooks_cmp); + +static struct hook *hooks_find1(struct hooks *, const char *); +static void hooks_free1(struct hooks *, struct hook *); +static void hooks_emptyfn(struct cmd_q *); + +static int +hooks_cmp(struct hook *hook1, struct hook *hook2) +{ + return (strcmp(hook1->name, hook2->name)); +} + +struct hooks * +hooks_get(struct session *s) +{ + if (s != NULL) + return (s->hooks); + return (global_hooks); +} + +struct hooks * +hooks_create(struct hooks *parent) +{ + struct hooks *hooks; + + hooks = xcalloc(1, sizeof *hooks); + RB_INIT(&hooks->tree); + hooks->parent = parent; + return (hooks); +} + +static void +hooks_free1(struct hooks *hooks, struct hook *hook) +{ + RB_REMOVE(hooks_tree, &hooks->tree, hook); + cmd_list_free(hook->cmdlist); + free((char *)hook->name); + free(hook); +} + +void +hooks_free(struct hooks *hooks) +{ + struct hook *hook, *hook1; + + RB_FOREACH_SAFE(hook, hooks_tree, &hooks->tree, hook1) + hooks_free1(hooks, hook); + free(hooks); +} + +struct hook * +hooks_first(struct hooks *hooks) +{ + return (RB_MIN(hooks_tree, &hooks->tree)); +} + +struct hook * +hooks_next(struct hook *hook) +{ + return (RB_NEXT(hooks_tree, &hooks->tree, hook)); +} + +void +hooks_add(struct hooks *hooks, const char *name, struct cmd_list *cmdlist) +{ + struct hook *hook; + + if ((hook = hooks_find1(hooks, name)) != NULL) + hooks_free1(hooks, hook); + + hook = xcalloc(1, sizeof *hook); + hook->name = xstrdup(name); + hook->cmdlist = cmdlist; + hook->cmdlist->references++; + RB_INSERT(hooks_tree, &hooks->tree, hook); +} + +void +hooks_remove(struct hooks *hooks, const char *name) +{ + struct hook *hook; + + if ((hook = hooks_find1(hooks, name)) != NULL) + hooks_free1(hooks, hook); +} + +static struct hook * +hooks_find1(struct hooks *hooks, const char *name) +{ + struct hook hook; + + hook.name = name; + return (RB_FIND(hooks_tree, &hooks->tree, &hook)); +} + +struct hook * +hooks_find(struct hooks *hooks, const char *name) +{ + struct hook hook0, *hook; + + hook0.name = name; + hook = RB_FIND(hooks_tree, &hooks->tree, &hook0); + while (hook == NULL) { + hooks = hooks->parent; + if (hooks == NULL) + break; + hook = RB_FIND(hooks_tree, &hooks->tree, &hook0); + } + return (hook); +} + +static void +hooks_emptyfn(struct cmd_q *hooks_cmdq) +{ + struct cmd_q *cmdq = hooks_cmdq->data; + + if (cmdq != NULL) { + if (hooks_cmdq->client_exit >= 0) + cmdq->client_exit = hooks_cmdq->client_exit; + if (!cmdq_free(cmdq)) + cmdq_continue(cmdq); + } + cmdq_free(hooks_cmdq); +} + +int +hooks_run(struct hooks *hooks, struct client *c, struct cmd_find_state *fs, + const char *fmt, ...) +{ + struct hook *hook; + struct cmd_q *hooks_cmdq; + va_list ap; + char *name; + + va_start(ap, fmt); + xvasprintf(&name, fmt, ap); + va_end(ap); + + hook = hooks_find(hooks, name); + if (hook == NULL) { + free(name); + return (-1); + } + log_debug("running hook %s", name); + free(name); + + hooks_cmdq = cmdq_new(c); + hooks_cmdq->flags |= CMD_Q_NOHOOKS; + + if (fs != NULL) + cmd_find_copy_state(&hooks_cmdq->current, fs); + hooks_cmdq->parent = NULL; + + cmdq_run(hooks_cmdq, hook->cmdlist, NULL); + cmdq_free(hooks_cmdq); + return (0); +} + +int +hooks_wait(struct hooks *hooks, struct cmd_q *cmdq, struct cmd_find_state *fs, + const char *fmt, ...) +{ + struct hook *hook; + struct cmd_q *hooks_cmdq; + va_list ap; + char *name; + + va_start(ap, fmt); + xvasprintf(&name, fmt, ap); + va_end(ap); + + hook = hooks_find(hooks, name); + if (hook == NULL) { + free(name); + return (-1); + } + log_debug("running hook %s (parent %p)", name, cmdq); + free(name); + + hooks_cmdq = cmdq_new(cmdq->client); + hooks_cmdq->flags |= CMD_Q_NOHOOKS; + + if (fs != NULL) + cmd_find_copy_state(&hooks_cmdq->current, fs); + hooks_cmdq->parent = cmdq; + + hooks_cmdq->emptyfn = hooks_emptyfn; + hooks_cmdq->data = cmdq; + + if (cmdq != NULL) + cmdq->references++; + cmdq_run(hooks_cmdq, hook->cmdlist, NULL); + return (0); +} diff --git a/input-keys.c b/input-keys.c index ef652ee1..9538e876 100644 --- a/input-keys.c +++ b/input-keys.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -30,8 +30,10 @@ * direction with output). */ +void input_key_mouse(struct window_pane *, struct mouse_event *); + struct input_key_ent { - int key; + key_code key; const char *data; int flags; @@ -132,27 +134,58 @@ const struct input_key_ent input_keys[] = { { KEYC_KP_PERIOD, ".", 0 }, }; +/* Split a character into two UTF-8 bytes. */ +static size_t +input_split2(u_int c, u_char *dst) +{ + if (c > 0x7f) { + dst[0] = (c >> 6) | 0xc0; + dst[1] = (c & 0x3f) | 0x80; + return (2); + } + dst[0] = c; + return (1); +} + /* Translate a key code into an output key sequence. */ void -input_key(struct window_pane *wp, int key) +input_key(struct window_pane *wp, key_code key, struct mouse_event *m) { - const struct input_key_ent *ike; - u_int i; - size_t dlen; - char *out; - u_char ch; + const struct input_key_ent *ike; + u_int i; + size_t dlen; + char *out; + key_code justkey; + struct utf8_data ud; - log_debug("writing key 0x%x", key); + log_debug("writing key 0x%llx (%s) to %%%u", key, + key_string_lookup_key(key), wp->id); + + /* If this is a mouse key, pass off to mouse function. */ + if (KEYC_IS_MOUSE(key)) { + if (m != NULL && m->wp != -1 && (u_int)m->wp == wp->id) + input_key_mouse(wp, m); + return; + } /* * If this is a normal 7-bit key, just send it, with a leading escape - * if necessary. + * if necessary. If it is a UTF-8 key, split it and send it. */ - if (key != KEYC_NONE && (key & ~KEYC_ESCAPE) < 0x100) { + justkey = (key & ~KEYC_ESCAPE); + if (justkey <= 0x7f) { if (key & KEYC_ESCAPE) bufferevent_write(wp->event, "\033", 1); - ch = key & ~KEYC_ESCAPE; - bufferevent_write(wp->event, &ch, 1); + ud.data[0] = justkey; + bufferevent_write(wp->event, &ud.data[0], 1); + return; + } + if (justkey > 0x7f && justkey < KEYC_BASE) { + if (utf8_split(justkey, &ud) != UTF8_DONE) + return; + if (key & KEYC_ESCAPE) + bufferevent_write(wp->event, "\033", 1); + bufferevent_write(wp->event, ud.data, ud.size); return; } @@ -160,7 +193,7 @@ input_key(struct window_pane *wp, int key) * Then try to look this up as an xterm key, if the flag to output them * is set. */ - if (options_get_number(&wp->window->options, "xterm-keys")) { + if (options_get_number(wp->window->options, "xterm-keys")) { if ((out = xterm_keys_lookup(key)) != NULL) { bufferevent_write(wp->event, out, strlen(out)); free(out); @@ -185,11 +218,11 @@ input_key(struct window_pane *wp, int key) break; } if (i == nitems(input_keys)) { - log_debug("key 0x%x missing", key); + log_debug("key 0x%llx missing", key); return; } dlen = strlen(ike->data); - log_debug("found key 0x%x: \"%s\"", key, ike->data); + log_debug("found key 0x%llx: \"%s\"", key, ike->data); /* Prefix a \033 for escape. */ if (key & KEYC_ESCAPE) @@ -199,55 +232,50 @@ input_key(struct window_pane *wp, int key) /* Translate mouse and output. */ void -input_mouse(struct window_pane *wp, struct session *s, struct mouse_event *m) +input_key_mouse(struct window_pane *wp, struct mouse_event *m) { - char buf[40]; - size_t len; - struct paste_buffer *pb; - int event; + char buf[40]; + size_t len; + u_int x, y; - if (wp->screen->mode & ALL_MOUSE_MODES) { - /* - * Use the SGR (1006) extension only if the application - * requested it and the underlying terminal also sent the event - * in this format (this is because an old style mouse release - * event cannot be converted into the new SGR format, since the - * released button is unknown). Otherwise pretend that tmux - * doesn't speak this extension, and fall back to the UTF-8 - * (1005) extension if the application requested, or to the - * legacy format. - */ - if (m->sgr && (wp->screen->mode & MODE_MOUSE_SGR)) { - len = xsnprintf(buf, sizeof buf, "\033[<%d;%d;%d%c", - m->sgr_xb, m->x + 1, m->y + 1, - m->sgr_rel ? 'm' : 'M'); - } else if (wp->screen->mode & MODE_MOUSE_UTF8) { - len = xsnprintf(buf, sizeof buf, "\033[M"); - len += utf8_split2(m->xb + 32, &buf[len]); - len += utf8_split2(m->x + 33, &buf[len]); - len += utf8_split2(m->y + 33, &buf[len]); - } else { - if (m->xb > 223) - return; - len = xsnprintf(buf, sizeof buf, "\033[M"); - buf[len++] = m->xb + 32; - buf[len++] = m->x + 33; - buf[len++] = m->y + 33; - } - bufferevent_write(wp->event, buf, len); + if ((wp->screen->mode & ALL_MOUSE_MODES) == 0) + return; + if (!window_pane_visible(wp)) + return; + if (cmd_mouse_at(wp, m, &x, &y, 0) != 0) return; - } - if (options_get_number(&wp->window->options, "mode-mouse") != 1) + /* If this pane is not in button mode, discard motion events. */ + if (!(wp->screen->mode & MODE_MOUSE_BUTTON) && (m->b & MOUSE_MASK_DRAG)) return; - event = m->event & (MOUSE_EVENT_CLICK|MOUSE_EVENT_WHEEL); - if (wp->mode == NULL && m->button == 1 && event == MOUSE_EVENT_CLICK) { - pb = paste_get_top(); - if (pb != NULL) - paste_send_pane(pb, wp, "\r", 1); - } else if (window_pane_set_mode(wp, &window_copy_mode) == 0) { - window_copy_init_from_pane(wp); - if (wp->mode->mouse != NULL) - wp->mode->mouse(wp, s, m); + + /* + * Use the SGR (1006) extension only if the application requested it + * and the underlying terminal also sent the event in this format (this + * is because an old style mouse release event cannot be converted into + * the new SGR format, since the released button is unknown). Otherwise + * pretend that tmux doesn't speak this extension, and fall back to the + * UTF-8 (1005) extension if the application requested, or to the + * legacy format. + */ + if (m->sgr_type != ' ' && (wp->screen->mode & MODE_MOUSE_SGR)) { + len = xsnprintf(buf, sizeof buf, "\033[<%u;%u;%u%c", + m->sgr_b, x + 1, y + 1, m->sgr_type); + } else if (wp->screen->mode & MODE_MOUSE_UTF8) { + if (m->b > 0x7ff - 32 || x > 0x7ff - 33 || y > 0x7ff - 33) + return; + len = xsnprintf(buf, sizeof buf, "\033[M"); + len += input_split2(m->b + 32, &buf[len]); + len += input_split2(x + 33, &buf[len]); + len += input_split2(y + 33, &buf[len]); + } else { + if (m->b > 223) + return; + len = xsnprintf(buf, sizeof buf, "\033[M"); + buf[len++] = m->b + 32; + buf[len++] = x + 33; + buf[len++] = y + 33; } + log_debug("writing mouse %.*s to %%%u", (int)len, buf, wp->id); + bufferevent_write(wp->event, buf, len); } diff --git a/input.c b/input.c index de11f629..18c8eb9a 100644 --- a/input.c +++ b/input.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -20,6 +20,7 @@ #include #include +#include #include "tmux.h" @@ -43,14 +44,63 @@ * pretty stupid but not supporting it is more trouble than it is worth. * * - Special handling for ESC inside a DCS to allow arbitrary byte sequences to - * be passed to the underlying teminal(s). + * be passed to the underlying terminals. */ +/* Input parser cell. */ +struct input_cell { + struct grid_cell cell; + int set; + int g0set; /* 1 if ACS */ + int g1set; /* 1 if ACS */ +}; + +/* Input parser context. */ +struct input_ctx { + struct window_pane *wp; + struct screen_write_ctx ctx; + + struct input_cell cell; + + struct input_cell old_cell; + u_int old_cx; + u_int old_cy; + + u_char interm_buf[4]; + size_t interm_len; + + u_char param_buf[64]; + size_t param_len; + +#define INPUT_BUF_START 32 +#define INPUT_BUF_LIMIT 1048576 + u_char *input_buf; + size_t input_len; + size_t input_space; + + int param_list[24]; /* -1 not present */ + u_int param_list_len; + + struct utf8_data utf8data; + + int ch; + int flags; +#define INPUT_DISCARD 0x1 + + const struct input_state *state; + + /* + * All input received since we were last in the ground state. Sent to + * control clients on connection. + */ + struct evbuffer *since_ground; +}; + /* Helper functions. */ struct input_transition; int input_split(struct input_ctx *); int input_get(struct input_ctx *, u_int, int, int); -void input_reply(struct input_ctx *, const char *, ...); +void printflike(2, 3) input_reply(struct input_ctx *, const char *, ...); void input_set_state(struct window_pane *, const struct input_transition *); void input_reset_cell(struct input_ctx *); @@ -77,6 +127,8 @@ void input_csi_dispatch_rm_private(struct input_ctx *); void input_csi_dispatch_sm(struct input_ctx *); void input_csi_dispatch_sm_private(struct input_ctx *); void input_csi_dispatch_winops(struct input_ctx *); +void input_csi_dispatch_sgr_256(struct input_ctx *, int, u_int *); +void input_csi_dispatch_sgr_rgb(struct input_ctx *, int, u_int *); void input_csi_dispatch_sgr(struct input_ctx *); int input_dcs_dispatch(struct input_ctx *); int input_utf8_open(struct input_ctx *); @@ -394,11 +446,11 @@ const struct input_transition input_state_ground_table[] = { { 0x1c, 0x1f, input_c0_dispatch, NULL }, { 0x20, 0x7e, input_print, NULL }, { 0x7f, 0x7f, NULL, NULL }, - { 0x80, 0xc1, input_print, NULL }, + { 0x80, 0xc1, NULL, NULL }, { 0xc2, 0xdf, input_utf8_open, &input_state_utf8_one }, { 0xe0, 0xef, input_utf8_open, &input_state_utf8_two }, { 0xf0, 0xf4, input_utf8_open, &input_state_utf8_three }, - { 0xf5, 0xff, input_print, NULL }, + { 0xf5, 0xff, NULL, NULL }, { -1, -1, NULL, NULL } }; @@ -706,44 +758,73 @@ input_reset_cell(struct input_ctx *ictx) void input_init(struct window_pane *wp) { - struct input_ctx *ictx = &wp->ictx; + struct input_ctx *ictx; + + ictx = wp->ictx = xcalloc(1, sizeof *ictx); + + ictx->input_space = INPUT_BUF_START; + ictx->input_buf = xmalloc(INPUT_BUF_START); + + ictx->since_ground = evbuffer_new(); + + input_reset(wp, 0); +} + +/* Destroy input parser. */ +void +input_free(struct window_pane *wp) +{ + struct input_ctx *ictx = wp->ictx; + + free(ictx->input_buf); + evbuffer_free(ictx->since_ground); + + free (ictx); + wp->ictx = NULL; +} + +/* Reset input state and clear screen. */ +void +input_reset(struct window_pane *wp, int clear) +{ + struct input_ctx *ictx = wp->ictx; input_reset_cell(ictx); + if (clear) { + if (wp->mode == NULL) + screen_write_start(&ictx->ctx, wp, &wp->base); + else + screen_write_start(&ictx->ctx, NULL, &wp->base); + screen_write_reset(&ictx->ctx); + screen_write_stop(&ictx->ctx); + } + *ictx->interm_buf = '\0'; ictx->interm_len = 0; *ictx->param_buf = '\0'; ictx->param_len = 0; - ictx->input_space = INPUT_BUF_START; - ictx->input_buf = xmalloc(INPUT_BUF_START); - *ictx->input_buf = '\0'; ictx->input_len = 0; ictx->state = &input_state_ground; ictx->flags = 0; - - ictx->since_ground = evbuffer_new(); } -/* Destroy input parser. */ -void -input_free(struct window_pane *wp) +/* Return pending data. */ +struct evbuffer * +input_pending(struct window_pane *wp) { - if (wp == NULL) - return; - - free(wp->ictx.input_buf); - evbuffer_free(wp->ictx.since_ground); + return (wp->ictx->since_ground); } /* Change input state. */ void input_set_state(struct window_pane *wp, const struct input_transition *itr) { - struct input_ctx *ictx = &wp->ictx; + struct input_ctx *ictx = wp->ictx; if (ictx->state->exit != NULL) ictx->state->exit(ictx); @@ -756,7 +837,7 @@ input_set_state(struct window_pane *wp, const struct input_transition *itr) void input_parse(struct window_pane *wp) { - struct input_ctx *ictx = &wp->ictx; + struct input_ctx *ictx = wp->ictx; const struct input_transition *itr; struct evbuffer *evb = wp->event->input; u_char *buf; @@ -765,8 +846,8 @@ input_parse(struct window_pane *wp) if (EVBUFFER_LENGTH(evb) == 0) return; - wp->window->flags |= WINDOW_ACTIVITY; - wp->window->flags &= ~WINDOW_SILENCE; + window_update_activity(wp->window); + wp->flags |= PANE_CHANGED; /* * Open the screen. Use NULL wp if there is a mode set as don't want to @@ -783,10 +864,12 @@ input_parse(struct window_pane *wp) notify_input(wp, evb); off = 0; + log_debug("%s: %%%u %s, %zu bytes: %.*s", __func__, wp->id, + ictx->state->name, len, (int)len, buf); + /* Parse the input. */ while (off < len) { ictx->ch = buf[off++]; - log_debug("%s: '%c' %s", __func__, ictx->ch, ictx->state->name); /* Find the transition. */ itr = ictx->state->transitions; @@ -797,7 +880,7 @@ input_parse(struct window_pane *wp) } if (itr->first == -1 || itr->last == -1) { /* No transition? Eh? */ - fatalx("No transition from state!"); + fatalx("no transition from state"); } /* @@ -925,7 +1008,7 @@ input_print(struct input_ctx *ictx) else ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET; - grid_cell_one(&ictx->cell.cell, ictx->ch); + utf8_set(&ictx->cell.cell.data, ictx->ch); screen_write_cell(&ictx->ctx, &ictx->cell.cell); ictx->cell.cell.attr &= ~GRID_ATTR_CHARSET; @@ -990,19 +1073,18 @@ input_c0_dispatch(struct input_ctx *ictx) struct screen_write_ctx *sctx = &ictx->ctx; struct window_pane *wp = ictx->wp; struct screen *s = sctx->s; - u_int trigger; - log_debug("%s: '%c", __func__, ictx->ch); + log_debug("%s: '%c'", __func__, ictx->ch); switch (ictx->ch) { case '\000': /* NUL */ break; case '\007': /* BEL */ - wp->window->flags |= WINDOW_BELL; + alerts_queue(wp->window, WINDOW_BELL); break; case '\010': /* BS */ screen_write_backspace(sctx); - goto count_c0; + break; case '\011': /* HT */ /* Don't tab beyond the end of the line. */ if (s->cx >= screen_size_x(s) - 1) @@ -1019,10 +1101,10 @@ input_c0_dispatch(struct input_ctx *ictx) case '\013': /* VT */ case '\014': /* FF */ screen_write_linefeed(sctx, 0); - goto count_c0; + break; case '\015': /* CR */ screen_write_carriagereturn(sctx); - goto count_c0; + break; case '\016': /* SO */ ictx->cell.set = 1; break; @@ -1034,15 +1116,6 @@ input_c0_dispatch(struct input_ctx *ictx) break; } - return (0); - -count_c0: - trigger = options_get_number(&wp->window->options, "c0-change-trigger"); - if (trigger != 0 && ++wp->changes >= trigger) { - wp->flags |= PANE_DROP; - window_pane_timer_start(wp); - } - return (0); } @@ -1127,6 +1200,7 @@ input_csi_dispatch(struct input_ctx *ictx) struct screen *s = sctx->s; struct input_table_entry *entry; int n, m; + u_int cx; if (ictx->flags & INPUT_DISCARD) return (0); @@ -1145,12 +1219,16 @@ input_csi_dispatch(struct input_ctx *ictx) switch (entry->type) { case INPUT_CSI_CBT: /* Find the previous tab point, n times. */ + cx = s->cx; + if (cx > screen_size_x(s) - 1) + cx = screen_size_x(s) - 1; n = input_get(ictx, 0, 1, 1); - while (s->cx > 0 && n-- > 0) { + while (cx > 0 && n-- > 0) { do - s->cx--; - while (s->cx > 0 && !bit_test(s->tabs, s->cx)); + cx--; + while (cx > 0 && !bit_test(s->tabs, cx)); } + s->cx = cx; break; case INPUT_CSI_CUB: screen_write_cursorleft(sctx, input_get(ictx, 0, 1, 1)); @@ -1530,7 +1608,7 @@ input_csi_dispatch_winops(struct input_ctx *ictx) return; break; case 18: - input_reply(ictx, "\033[8;%u;%u", wp->sy, wp->sx); + input_reply(ictx, "\033[8;%u;%ut", wp->sy, wp->sx); break; default: log_debug("%s: unknown '%c'", __func__, ictx->ch); @@ -1540,13 +1618,78 @@ input_csi_dispatch_winops(struct input_ctx *ictx) } } +/* Handle CSI SGR for 256 colours. */ +void +input_csi_dispatch_sgr_256(struct input_ctx *ictx, int fgbg, u_int *i) +{ + struct grid_cell *gc = &ictx->cell.cell; + int c; + + (*i)++; + c = input_get(ictx, *i, 0, -1); + if (c == -1) { + if (fgbg == 38) { + gc->flags &= ~(GRID_FLAG_FG256|GRID_FLAG_FGRGB); + gc->fg = 8; + } else if (fgbg == 48) { + gc->flags &= ~(GRID_FLAG_BG256|GRID_FLAG_BGRGB); + gc->bg = 8; + } + } else { + if (fgbg == 38) { + gc->flags |= GRID_FLAG_FG256; + gc->flags &= ~GRID_FLAG_FGRGB; + gc->fg = c; + } else if (fgbg == 48) { + gc->flags |= GRID_FLAG_BG256; + gc->flags &= ~GRID_FLAG_BGRGB; + gc->bg = c; + } + } +} + +/* Handle CSI SGR for RGB colours. */ +void +input_csi_dispatch_sgr_rgb(struct input_ctx *ictx, int fgbg, u_int *i) +{ + struct grid_cell *gc = &ictx->cell.cell; + int r, g, b; + + (*i)++; + r = input_get(ictx, *i, 0, -1); + if (r == -1 || r > 255) + return; + (*i)++; + g = input_get(ictx, *i, 0, -1); + if (g == -1 || g > 255) + return; + (*i)++; + b = input_get(ictx, *i, 0, -1); + if (b == -1 || b > 255) + return; + + if (fgbg == 38) { + gc->flags &= ~GRID_FLAG_FG256; + gc->flags |= GRID_FLAG_FGRGB; + gc->fg_rgb.r = r; + gc->fg_rgb.g = g; + gc->fg_rgb.b = b; + } else if (fgbg == 48) { + gc->flags &= ~GRID_FLAG_BG256; + gc->flags |= GRID_FLAG_BGRGB; + gc->bg_rgb.r = r; + gc->bg_rgb.g = g; + gc->bg_rgb.b = b; + } +} + /* Handle CSI SGR. */ void input_csi_dispatch_sgr(struct input_ctx *ictx) { struct grid_cell *gc = &ictx->cell.cell; u_int i; - int n, m; + int n; if (ictx->param_list_len == 0) { memcpy(gc, &grid_default_cell, sizeof *gc); @@ -1558,28 +1701,13 @@ input_csi_dispatch_sgr(struct input_ctx *ictx) if (n == 38 || n == 48) { i++; - if (input_get(ictx, i, 0, -1) != 5) - continue; - - i++; - m = input_get(ictx, i, 0, -1); - if (m == -1) { - if (n == 38) { - gc->flags &= ~GRID_FLAG_FG256; - gc->fg = 8; - } else if (n == 48) { - gc->flags &= ~GRID_FLAG_BG256; - gc->bg = 8; - } - - } else { - if (n == 38) { - gc->flags |= GRID_FLAG_FG256; - gc->fg = m; - } else if (n == 48) { - gc->flags |= GRID_FLAG_BG256; - gc->bg = m; - } + switch (input_get(ictx, i, 0, -1)) { + case 2: + input_csi_dispatch_sgr_rgb(ictx, n, &i); + break; + case 5: + input_csi_dispatch_sgr_256(ictx, n, &i); + break; } continue; } @@ -1633,11 +1761,11 @@ input_csi_dispatch_sgr(struct input_ctx *ictx) case 35: case 36: case 37: - gc->flags &= ~GRID_FLAG_FG256; + gc->flags &= ~(GRID_FLAG_FG256|GRID_FLAG_FGRGB); gc->fg = n - 30; break; case 39: - gc->flags &= ~GRID_FLAG_FG256; + gc->flags &= ~(GRID_FLAG_FG256|GRID_FLAG_FGRGB); gc->fg = 8; break; case 40: @@ -1648,11 +1776,11 @@ input_csi_dispatch_sgr(struct input_ctx *ictx) case 45: case 46: case 47: - gc->flags &= ~GRID_FLAG_BG256; + gc->flags &= ~(GRID_FLAG_BG256|GRID_FLAG_BGRGB); gc->bg = n - 40; break; case 49: - gc->flags &= ~GRID_FLAG_BG256; + gc->flags &= ~(GRID_FLAG_BG256|GRID_FLAG_BGRGB); gc->bg = 8; break; case 90: @@ -1663,7 +1791,7 @@ input_csi_dispatch_sgr(struct input_ctx *ictx) case 95: case 96: case 97: - gc->flags &= ~GRID_FLAG_FG256; + gc->flags &= ~(GRID_FLAG_FG256|GRID_FLAG_FGRGB); gc->fg = n; break; case 100: @@ -1674,7 +1802,7 @@ input_csi_dispatch_sgr(struct input_ctx *ictx) case 105: case 106: case 107: - gc->flags &= ~GRID_FLAG_BG256; + gc->flags &= ~(GRID_FLAG_BG256|GRID_FLAG_BGRGB); gc->bg = n - 10; break; } @@ -1717,7 +1845,7 @@ void input_exit_osc(struct input_ctx *ictx) { u_char *p = ictx->input_buf; - int option; + u_int option; if (ictx->flags & INPUT_DISCARD) return; @@ -1788,12 +1916,12 @@ input_exit_rename(struct input_ctx *ictx) { if (ictx->flags & INPUT_DISCARD) return; - if (!options_get_number(&ictx->wp->window->options, "allow-rename")) + if (!options_get_number(ictx->wp->window->options, "allow-rename")) return; log_debug("%s: \"%s\"", __func__, ictx->input_buf); window_set_name(ictx->wp->window, ictx->input_buf); - options_set_number(&ictx->wp->window->options, "automatic-rename", 0); + options_set_number(ictx->wp->window->options, "automatic-rename", 0); server_status_window(ictx->wp->window); } @@ -1802,14 +1930,13 @@ input_exit_rename(struct input_ctx *ictx) int input_utf8_open(struct input_ctx *ictx) { - if (!options_get_number(&ictx->wp->window->options, "utf8")) { - /* Print, and do not switch state. */ - input_print(ictx); - return (-1); - } - log_debug("%s", __func__); + struct utf8_data *ud = &ictx->utf8data; + + if (utf8_open(ud, ictx->ch) != UTF8_MORE) + fatalx("UTF-8 open invalid %#x", ictx->ch); + + log_debug("%s %hhu", __func__, ud->size); - utf8_open(&ictx->utf8data, ictx->ch); return (0); } @@ -1817,9 +1944,13 @@ input_utf8_open(struct input_ctx *ictx) int input_utf8_add(struct input_ctx *ictx) { + struct utf8_data *ud = &ictx->utf8data; + + if (utf8_append(ud, ictx->ch) != UTF8_MORE) + fatalx("UTF-8 add invalid %#x", ictx->ch); + log_debug("%s", __func__); - utf8_append(&ictx->utf8data, ictx->ch); return (0); } @@ -1827,11 +1958,21 @@ input_utf8_add(struct input_ctx *ictx) int input_utf8_close(struct input_ctx *ictx) { - log_debug("%s", __func__); + struct utf8_data *ud = &ictx->utf8data; - utf8_append(&ictx->utf8data, ictx->ch); + if (utf8_append(ud, ictx->ch) != UTF8_DONE) { + /* + * An error here could be invalid UTF-8 or it could be a + * nonprintable character for which we can't get the + * width. Drop it. + */ + return (0); + } - grid_cell_set(&ictx->cell.cell, &ictx->utf8data); + log_debug("%s %hhu '%*s' (width %hhu)", __func__, ud->size, + (int)ud->size, ud->data, ud->width); + + utf8_copy(&ictx->cell.cell.data, ud); screen_write_cell(&ictx->ctx, &ictx->cell.cell); return (0); diff --git a/job.c b/job.c index 023a1d47..957897f3 100644 --- a/job.c +++ b/job.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -20,10 +20,10 @@ #include #include +#include #include #include #include -#include #include "tmux.h" @@ -40,34 +40,40 @@ struct joblist all_jobs = LIST_HEAD_INITIALIZER(all_jobs); /* Start a job running, if it isn't already. */ struct job * -job_run(const char *cmd, struct session *s, +job_run(const char *cmd, struct session *s, const char *cwd, void (*callbackfn)(struct job *), void (*freefn)(void *), void *data) { struct job *job; - struct environ env; + struct environ *env; pid_t pid; int nullfd, out[2]; + const char *home; if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, out) != 0) return (NULL); - environ_init(&env); - environ_copy(&global_environ, &env); + env = environ_create(); + environ_copy(global_environ, env); if (s != NULL) - environ_copy(&s->environ, &env); - server_fill_environ(s, &env); + environ_copy(s->environ, env); + server_fill_environ(s, env); switch (pid = fork()) { case -1: - environ_free(&env); + environ_free(env); close(out[0]); close(out[1]); return (NULL); case 0: /* child */ clear_signals(1); - environ_push(&env); - environ_free(&env); + if (cwd == NULL || chdir(cwd) != 0) { + if ((home = find_home()) == NULL || chdir(home) != 0) + chdir("/"); + } + + environ_push(env); + environ_free(env); if (dup2(out[1], STDIN_FILENO) == -1) fatal("dup2 failed"); @@ -92,10 +98,12 @@ job_run(const char *cmd, struct session *s, } /* parent */ - environ_free(&env); + environ_free(env); close(out[1]); job = xmalloc(sizeof *job); + job->state = JOB_RUNNING; + job->cmd = xstrdup(cmd); job->pid = pid; job->status = 0; @@ -141,7 +149,7 @@ job_free(struct job *job) /* Called when output buffer falls below low watermark (default is 0). */ void -job_write_callback(unused struct bufferevent *bufev, void *data) +job_write_callback(__unused struct bufferevent *bufev, void *data) { struct job *job = data; size_t len = EVBUFFER_LENGTH(EVBUFFER_OUTPUT(job->event)); @@ -157,20 +165,20 @@ job_write_callback(unused struct bufferevent *bufev, void *data) /* Job buffer error callback. */ void -job_callback(unused struct bufferevent *bufev, unused short events, void *data) +job_callback(__unused struct bufferevent *bufev, __unused short events, + void *data) { struct job *job = data; log_debug("job error %p: %s, pid %ld", job, job->cmd, (long) job->pid); - if (job->pid == -1) { + if (job->state == JOB_DEAD) { if (job->callbackfn != NULL) job->callbackfn(job); job_free(job); } else { bufferevent_disable(job->event, EV_READ); - close(job->fd); - job->fd = -1; + job->state = JOB_CLOSED; } } @@ -182,10 +190,12 @@ job_died(struct job *job, int status) job->status = status; - if (job->fd == -1) { + if (job->state == JOB_CLOSED) { if (job->callbackfn != NULL) job->callbackfn(job); job_free(job); - } else + } else { job->pid = -1; + job->state = JOB_DEAD; + } } diff --git a/key-bindings.c b/key-bindings.c index 00f73d73..0d13385d 100644 --- a/key-bindings.c +++ b/key-bindings.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -25,66 +25,130 @@ #include "tmux.h" RB_GENERATE(key_bindings, key_binding, entry, key_bindings_cmp); +RB_GENERATE(key_tables, key_table, entry, key_table_cmp); +struct key_tables key_tables = RB_INITIALIZER(&key_tables); -struct key_bindings key_bindings; +int +key_table_cmp(struct key_table *e1, struct key_table *e2) +{ + return (strcmp(e1->name, e2->name)); +} int key_bindings_cmp(struct key_binding *bd1, struct key_binding *bd2) { - int key1, key2; - - key1 = bd1->key & ~KEYC_PREFIX; - key2 = bd2->key & ~KEYC_PREFIX; - if (key1 != key2) - return (key1 - key2); - - if (bd1->key & KEYC_PREFIX && !(bd2->key & KEYC_PREFIX)) + if (bd1->key < bd2->key) return (-1); - if (bd2->key & KEYC_PREFIX && !(bd1->key & KEYC_PREFIX)) + if (bd1->key > bd2->key) return (1); return (0); } -struct key_binding * -key_bindings_lookup(int key) +struct key_table * +key_bindings_get_table(const char *name, int create) { - struct key_binding bd; + struct key_table table_find, *table; - bd.key = key; - return (RB_FIND(key_bindings, &key_bindings, &bd)); + table_find.name = name; + table = RB_FIND(key_tables, &key_tables, &table_find); + if (table != NULL || !create) + return (table); + + table = xmalloc(sizeof *table); + table->name = xstrdup(name); + RB_INIT(&table->key_bindings); + + table->references = 1; /* one reference in key_tables */ + RB_INSERT(key_tables, &key_tables, table); + + return (table); } void -key_bindings_add(int key, int can_repeat, struct cmd_list *cmdlist) +key_bindings_unref_table(struct key_table *table) { struct key_binding *bd; + struct key_binding *bd1; - key_bindings_remove(key); + if (--table->references != 0) + return; + + RB_FOREACH_SAFE(bd, key_bindings, &table->key_bindings, bd1) { + RB_REMOVE(key_bindings, &table->key_bindings, bd); + cmd_list_free(bd->cmdlist); + free(bd); + } + + free((void *)table->name); + free(table); +} + +void +key_bindings_add(const char *name, key_code key, int can_repeat, + struct cmd_list *cmdlist) +{ + struct key_table *table; + struct key_binding bd_find, *bd; + + table = key_bindings_get_table(name, 1); + + bd_find.key = key; + bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find); + if (bd != NULL) { + RB_REMOVE(key_bindings, &table->key_bindings, bd); + cmd_list_free(bd->cmdlist); + free(bd); + } bd = xmalloc(sizeof *bd); bd->key = key; - RB_INSERT(key_bindings, &key_bindings, bd); + RB_INSERT(key_bindings, &table->key_bindings, bd); bd->can_repeat = can_repeat; bd->cmdlist = cmdlist; } void -key_bindings_remove(int key) +key_bindings_remove(const char *name, key_code key) { - struct key_binding *bd; + struct key_table *table; + struct key_binding bd_find, *bd; - if ((bd = key_bindings_lookup(key)) == NULL) + table = key_bindings_get_table(name, 0); + if (table == NULL) return; - RB_REMOVE(key_bindings, &key_bindings, bd); + + bd_find.key = key; + bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find); + if (bd == NULL) + return; + + RB_REMOVE(key_bindings, &table->key_bindings, bd); cmd_list_free(bd->cmdlist); free(bd); + + if (RB_EMPTY(&table->key_bindings)) { + RB_REMOVE(key_tables, &key_tables, table); + key_bindings_unref_table(table); + } +} + +void +key_bindings_remove_table(const char *name) +{ + struct key_table *table; + + table = key_bindings_get_table(name, 0); + if (table != NULL) { + RB_REMOVE(key_tables, &key_tables, table); + key_bindings_unref_table(table); + } } void key_bindings_init(void) { - static const char* defaults[] = { + static const char *defaults[] = { "bind C-b send-prefix", "bind C-o rotate-window", "bind C-z suspend-client", @@ -101,22 +165,23 @@ key_bindings_init(void) "bind , command-prompt -I'#W' \"rename-window '%%'\"", "bind - delete-buffer", "bind . command-prompt \"move-window -t '%%'\"", - "bind 0 select-window -t:0", - "bind 1 select-window -t:1", - "bind 2 select-window -t:2", - "bind 3 select-window -t:3", - "bind 4 select-window -t:4", - "bind 5 select-window -t:5", - "bind 6 select-window -t:6", - "bind 7 select-window -t:7", - "bind 8 select-window -t:8", - "bind 9 select-window -t:9", + "bind 0 select-window -t:=0", + "bind 1 select-window -t:=1", + "bind 2 select-window -t:=2", + "bind 3 select-window -t:=3", + "bind 4 select-window -t:=4", + "bind 5 select-window -t:=5", + "bind 6 select-window -t:=6", + "bind 7 select-window -t:=7", + "bind 8 select-window -t:=8", + "bind 9 select-window -t:=9", "bind : command-prompt", "bind \\; last-pane", "bind = choose-buffer", "bind ? list-keys", "bind D choose-client", "bind L switch-client -l", + "bind M select-pane -M", "bind [ copy-mode", "bind ] paste-buffer", "bind c new-window", @@ -124,6 +189,7 @@ key_bindings_init(void) "bind f command-prompt \"find-window '%%'\"", "bind i display-message", "bind l last-window", + "bind m select-pane -m", "bind n next-window", "bind o select-pane -t:.+", "bind p previous-window", @@ -158,29 +224,36 @@ key_bindings_init(void) "bind -r C-Down resize-pane -D", "bind -r C-Left resize-pane -L", "bind -r C-Right resize-pane -R", + "bind -n MouseDown1Pane select-pane -t=\\; send-keys -M", + "bind -n MouseDrag1Border resize-pane -M", + "bind -n MouseDown1Status select-window -t=", + "bind -n WheelDownStatus next-window", + "bind -n WheelUpStatus previous-window", + "bind -n MouseDrag1Pane if -Ft= '#{mouse_any_flag}' 'if -Ft= \"#{pane_in_mode}\" \"copy-mode -M\" \"send-keys -M\"' 'copy-mode -M'", + "bind -n MouseDown3Pane if-shell -Ft= '#{mouse_any_flag}' 'select-pane -t=; send-keys -M' 'select-pane -mt='", + "bind -n WheelUpPane if-shell -Ft= '#{mouse_any_flag}' 'send-keys -M' 'if -Ft= \"#{pane_in_mode}\" \"send-keys -M\" \"copy-mode -et=\"'", }; u_int i; struct cmd_list *cmdlist; - char* cause; + char *cause; int error; struct cmd_q *cmdq; - RB_INIT(&key_bindings); - cmdq = cmdq_new(NULL); for (i = 0; i < nitems(defaults); i++) { error = cmd_string_parse(defaults[i], &cmdlist, "", i, &cause); if (error != 0) fatalx("bad default key"); - cmdq_run(cmdq, cmdlist); + cmdq_run(cmdq, cmdlist, NULL); cmd_list_free(cmdlist); } cmdq_free(cmdq); } void -key_bindings_dispatch(struct key_binding *bd, struct client *c) +key_bindings_dispatch(struct key_binding *bd, struct client *c, + struct mouse_event *m) { struct cmd *cmd; int readonly; @@ -195,5 +268,5 @@ key_bindings_dispatch(struct key_binding *bd, struct client *c) return; } - cmdq_run(c->cmdq, bd->cmdlist); + cmdq_run(c->cmdq, bd->cmdlist, m); } diff --git a/key-string.c b/key-string.c index db968279..119035a0 100644 --- a/key-string.c +++ b/key-string.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -22,12 +22,12 @@ #include "tmux.h" -int key_string_search_table(const char *); -int key_string_get_modifiers(const char **); +static key_code key_string_search_table(const char *); +static key_code key_string_get_modifiers(const char **); const struct { const char *string; - int key; + key_code key; } key_string_table[] = { /* Function keys. */ { "F1", KEYC_F1 }, @@ -82,10 +82,26 @@ const struct { { "KPEnter", KEYC_KP_ENTER }, { "KP0", KEYC_KP_ZERO }, { "KP.", KEYC_KP_PERIOD }, + + /* Mouse keys. */ + KEYC_MOUSE_STRING(MOUSEDOWN1, MouseDown1), + KEYC_MOUSE_STRING(MOUSEDOWN2, MouseDown2), + KEYC_MOUSE_STRING(MOUSEDOWN3, MouseDown3), + KEYC_MOUSE_STRING(MOUSEUP1, MouseUp1), + KEYC_MOUSE_STRING(MOUSEUP2, MouseUp2), + KEYC_MOUSE_STRING(MOUSEUP3, MouseUp3), + KEYC_MOUSE_STRING(MOUSEDRAG1, MouseDrag1), + KEYC_MOUSE_STRING(MOUSEDRAG2, MouseDrag2), + KEYC_MOUSE_STRING(MOUSEDRAG3, MouseDrag3), + KEYC_MOUSE_STRING(MOUSEDRAGEND1, MouseDragEnd1), + KEYC_MOUSE_STRING(MOUSEDRAGEND2, MouseDragEnd2), + KEYC_MOUSE_STRING(MOUSEDRAGEND3, MouseDragEnd3), + KEYC_MOUSE_STRING(WHEELUP, WheelUp), + KEYC_MOUSE_STRING(WHEELDOWN, WheelDown), }; /* Find key string in table. */ -int +static key_code key_string_search_table(const char *string) { u_int i; @@ -94,14 +110,14 @@ key_string_search_table(const char *string) if (strcasecmp(string, key_string_table[i].string) == 0) return (key_string_table[i].key); } - return (KEYC_NONE); + return (KEYC_UNKNOWN); } /* Find modifiers. */ -int +static key_code key_string_get_modifiers(const char **string) { - int modifiers; + key_code modifiers; modifiers = 0; while (((*string)[0] != '\0') && (*string)[1] == '-') { @@ -125,18 +141,27 @@ key_string_get_modifiers(const char **string) } /* Lookup a string and convert to a key value. */ -int +key_code key_string_lookup_string(const char *string) { static const char *other = "!#()+,-.0123456789:;<=>?'\r\t"; - int key, modifiers; + key_code key; u_short u; int size; + key_code modifiers; + struct utf8_data ud; + u_int i; + enum utf8_state more; + wchar_t wc; + + /* Is this no key? */ + if (strcasecmp(string, "None") == 0) + return (KEYC_NONE); /* Is this a hexadecimal value? */ if (string[0] == '0' && string[1] == 'x') { if (sscanf(string + 2, "%hx%n", &u, &size) != 1 || size > 4) - return (KEYC_NONE); + return (KEYC_UNKNOWN); return (u); } @@ -148,18 +173,31 @@ key_string_lookup_string(const char *string) } modifiers |= key_string_get_modifiers(&string); if (string[0] == '\0') - return (KEYC_NONE); + return (KEYC_UNKNOWN); /* Is this a standard ASCII key? */ - if (string[1] == '\0') { - key = (u_char) string[0]; - if (key < 32 || key == 127 || key > 255) - return (KEYC_NONE); + if (string[1] == '\0' && (u_char)string[0] <= 127) { + key = (u_char)string[0]; + if (key < 32 || key == 127) + return (KEYC_UNKNOWN); } else { + /* Try as a UTF-8 key. */ + if ((more = utf8_open(&ud, (u_char)*string)) == UTF8_MORE) { + if (strlen(string) != ud.size) + return (KEYC_UNKNOWN); + for (i = 1; i < ud.size; i++) + more = utf8_append(&ud, (u_char)string[i]); + if (more != UTF8_DONE) + return (KEYC_UNKNOWN); + if (utf8_combine(&ud, &wc) != UTF8_DONE) + return (KEYC_UNKNOWN); + return (wc | modifiers); + } + /* Otherwise look the key up in the table. */ key = key_string_search_table(string); - if (key == KEYC_NONE) - return (KEYC_NONE); + if (key == KEYC_UNKNOWN) + return (KEYC_UNKNOWN); } /* Convert the standard control keys. */ @@ -173,7 +211,7 @@ key_string_lookup_string(const char *string) else if (key == 63) key = KEYC_BSPACE; else - return (KEYC_NONE); + return (KEYC_UNKNOWN); modifiers &= ~KEYC_CTRL; } @@ -182,17 +220,24 @@ key_string_lookup_string(const char *string) /* Convert a key code into string format, with prefix if necessary. */ const char * -key_string_lookup_key(int key) +key_string_lookup_key(key_code key) { - static char out[24]; - char tmp[8]; - u_int i; + static char out[24]; + char tmp[8]; + u_int i; + struct utf8_data ud; *out = '\0'; /* Handle no key. */ if (key == KEYC_NONE) - return ("none"); + return ("None"); + + /* Handle special keys. */ + if (key == KEYC_UNKNOWN) + return ("Unknown"); + if (key == KEYC_MOUSE) + return ("Mouse"); /* * Special case: display C-@ as C-Space. Could do this below in @@ -222,21 +267,32 @@ key_string_lookup_key(int key) return (out); } + /* Is this a UTF-8 key? */ + if (key > 127 && key < KEYC_BASE) { + if (utf8_split(key, &ud) == UTF8_DONE) { + memcpy(out, ud.data, ud.size); + out[ud.size] = '\0'; + return (out); + } + } + /* Invalid keys are errors. */ - if (key == 127 || key > 255) - return (NULL); + if (key == 127 || key > 255) { + snprintf(out, sizeof out, "Invalid#%llx", key); + return (out); + } /* Check for standard or control key. */ - if (key >= 0 && key <= 32) { + if (key <= 32) { if (key == 0 || key > 26) - xsnprintf(tmp, sizeof tmp, "C-%c", 64 + key); + xsnprintf(tmp, sizeof tmp, "C-%c", (int)(64 + key)); else - xsnprintf(tmp, sizeof tmp, "C-%c", 96 + key); + xsnprintf(tmp, sizeof tmp, "C-%c", (int)(96 + key)); } else if (key >= 32 && key <= 126) { tmp[0] = key; tmp[1] = '\0'; } else if (key >= 128) - xsnprintf(tmp, sizeof tmp, "\\%o", key); + xsnprintf(tmp, sizeof tmp, "\\%llo", key); strlcat(out, tmp, sizeof out); return (out); diff --git a/layout-custom.c b/layout-custom.c index c9cf49c2..99c6c3ce 100644 --- a/layout-custom.c +++ b/layout-custom.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2010 Nicholas Marriott + * Copyright (c) 2010 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -55,12 +55,12 @@ layout_checksum(const char *layout) /* Dump layout as a string. */ char * -layout_dump(struct window *w) +layout_dump(struct layout_cell *root) { char layout[BUFSIZ], *out; *layout = '\0'; - if (layout_append(w->layout_root, layout, sizeof layout) != 0) + if (layout_append(root, layout, sizeof layout) != 0) return (NULL); xasprintf(&out, "%04x,%s", layout_checksum(layout), layout); diff --git a/layout-set.c b/layout-set.c index 64b655a0..bd1304ce 100644 --- a/layout-set.c +++ b/layout-set.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -44,12 +44,6 @@ const struct { { "tiled", layout_set_tiled }, }; -const char * -layout_set_name(u_int layout) -{ - return (layout_sets[layout].name); -} - int layout_set_lookup(const char *name) { @@ -251,10 +245,10 @@ layout_set_main_h(struct window *w) width = (w->sx - (n - 1)) / columns; /* Get the main pane height and add one for separator line. */ - mainheight = options_get_number(&w->options, "main-pane-height") + 1; + mainheight = options_get_number(w->options, "main-pane-height") + 1; /* Get the optional other pane height and add one for separator line. */ - otherheight = options_get_number(&w->options, "other-pane-height") + 1; + otherheight = options_get_number(w->options, "other-pane-height") + 1; /* * If an other pane height was specified, honour it so long as it @@ -372,10 +366,10 @@ layout_set_main_v(struct window *w) height = (w->sy - (n - 1)) / rows; /* Get the main pane width and add one for separator line. */ - mainwidth = options_get_number(&w->options, "main-pane-width") + 1; + mainwidth = options_get_number(w->options, "main-pane-width") + 1; /* Get the optional other pane width and add one for separator line. */ - otherwidth = options_get_number(&w->options, "other-pane-width") + 1; + otherwidth = options_get_number(w->options, "other-pane-width") + 1; /* * If an other pane width was specified, honour it so long as it diff --git a/layout.c b/layout.c index b91b86cd..31dce95c 100644 --- a/layout.c +++ b/layout.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -85,9 +85,9 @@ layout_print_cell(struct layout_cell *lc, const char *hdr, u_int n) { struct layout_cell *lcchild; - log_debug( - "%s:%*s%p type %u [parent %p] wp=%p [%u,%u %ux%u]", hdr, n, " ", lc, - lc->type, lc->parent, lc->wp, lc->xoff, lc->yoff, lc->sx, lc->sy); + log_debug("%s:%*s%p type %u [parent %p] wp=%p [%u,%u %ux%u]", hdr, n, + " ", lc, lc->type, lc->parent, lc->wp, lc->xoff, lc->yoff, lc->sx, + lc->sy); switch (lc->type) { case LAYOUT_LEFTRIGHT: case LAYOUT_TOPBOTTOM: @@ -100,8 +100,8 @@ layout_print_cell(struct layout_cell *lc, const char *hdr, u_int n) } void -layout_set_size( - struct layout_cell *lc, u_int sx, u_int sy, u_int xoff, u_int yoff) +layout_set_size(struct layout_cell *lc, u_int sx, u_int sy, u_int xoff, + u_int yoff) { lc->sx = sx; lc->sy = sy; @@ -519,62 +519,10 @@ layout_resize_pane(struct window_pane *wp, enum layout_type type, int change) notify_window_layout_changed(wp->window); } -/* Resize pane based on mouse events. */ -void -layout_resize_pane_mouse(struct client *c) -{ - struct window *w; - struct window_pane *wp; - struct mouse_event *m = &c->tty.mouse; - int pane_border; - - w = c->session->curw->window; - - pane_border = 0; - if (m->event & MOUSE_EVENT_DRAG && m->flags & MOUSE_RESIZE_PANE) { - TAILQ_FOREACH(wp, &w->panes, entry) { - if (!window_pane_visible(wp)) - continue; - - if (wp->xoff + wp->sx == m->lx && - wp->yoff <= 1 + m->ly && - wp->yoff + wp->sy >= m->ly) { - layout_resize_pane(wp, LAYOUT_LEFTRIGHT, - m->x - m->lx); - pane_border = 1; - } - if (wp->yoff + wp->sy == m->ly && - wp->xoff <= 1 + m->lx && - wp->xoff + wp->sx >= m->lx) { - layout_resize_pane(wp, LAYOUT_TOPBOTTOM, - m->y - m->ly); - pane_border = 1; - } - } - if (pane_border) - server_redraw_window(w); - } else if (m->event & MOUSE_EVENT_DOWN) { - TAILQ_FOREACH(wp, &w->panes, entry) { - if ((wp->xoff + wp->sx == m->x && - wp->yoff <= 1 + m->y && - wp->yoff + wp->sy >= m->y) || - (wp->yoff + wp->sy == m->y && - wp->xoff <= 1 + m->x && - wp->xoff + wp->sx >= m->x)) { - pane_border = 1; - } - } - } - if (pane_border) - m->flags |= MOUSE_RESIZE_PANE; - else - m->flags &= ~MOUSE_RESIZE_PANE; -} - /* Helper function to grow pane. */ int -layout_resize_pane_grow( - struct layout_cell *lc, enum layout_type type, int needed) +layout_resize_pane_grow(struct layout_cell *lc, enum layout_type type, + int needed) { struct layout_cell *lcadd, *lcremove; u_int size; @@ -614,8 +562,8 @@ layout_resize_pane_grow( /* Helper function to shrink pane. */ int -layout_resize_pane_shrink( - struct layout_cell *lc, enum layout_type type, int needed) +layout_resize_pane_shrink(struct layout_cell *lc, enum layout_type type, + int needed) { struct layout_cell *lcadd, *lcremove; u_int size; @@ -657,8 +605,8 @@ layout_assign_pane(struct layout_cell *lc, struct window_pane *wp) * split. This must be followed by layout_assign_pane before much else happens! **/ struct layout_cell * -layout_split_pane( - struct window_pane *wp, enum layout_type type, int size, int insert_before) +layout_split_pane(struct window_pane *wp, enum layout_type type, int size, + int insert_before) { struct layout_cell *lc, *lcparent, *lcnew, *lc1, *lc2; u_int sx, sy, xoff, yoff, size1, size2; @@ -738,6 +686,8 @@ layout_split_pane( case LAYOUT_LEFTRIGHT: if (size < 0) size2 = ((sx + 1) / 2) - 1; + else if (insert_before) + size2 = sx - size - 1; else size2 = size; if (size2 < PANE_MINIMUM) @@ -751,6 +701,8 @@ layout_split_pane( case LAYOUT_TOPBOTTOM: if (size < 0) size2 = ((sy + 1) / 2) - 1; + else if (insert_before) + size2 = sy - size - 1; else size2 = size; if (size2 < PANE_MINIMUM) diff --git a/log.c b/log.c index 066165e8..45aaea46 100644 --- a/log.c +++ b/log.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -17,46 +17,83 @@ */ #include -#include #include #include #include #include +#include #include "tmux.h" -FILE *log_file; +static FILE *log_file; +static int log_level; -void log_event_cb(int, const char *); -void log_vwrite(const char *, va_list); +static void log_event_cb(int, const char *); +static void log_vwrite(const char *, va_list); + +static int is_log_stdout(void) +{ + return fileno(log_file) <= 2; +} /* Log callback for libevent. */ -void -log_event_cb(unused int severity, const char *msg) +static void +log_event_cb(__unused int severity, const char *msg) { log_debug("%s", msg); } -/* Open logging to file. */ +/* Increment log level. */ void -log_open(const char *path) +log_add_level(void) { - log_file = fopen(path, "w"); - if (log_file == NULL) + log_level++; +} + +/* Get log level. */ +int +log_get_level(void) +{ + return (log_level); +} + +void +log_open_fp(FILE *f) +{ + if (log_file == f) return; + if (log_file != NULL && !is_log_stdout()) + fclose(log_file); + + log_file = f; + setvbuf(log_file, NULL, _IOLBF, 0); event_set_log_callback(log_event_cb); +} - tzset(); +/* Open logging to file. */ +void +log_open(const char *name) +{ + char *path; + + if (log_level == 0) + return; + + xasprintf(&path, "tmate-%s-%ld.log", name, (long)getpid()); + FILE *f = fopen(path, "w"); + free(path); + if (f) + log_open_fp(f); } /* Close logging. */ void log_close(void) { - if (log_file != NULL) + if (log_file != NULL && !is_log_stdout()) fclose(log_file); log_file = NULL; @@ -64,36 +101,56 @@ log_close(void) } /* Write a log message. */ -void +__attribute__((__format__(__printf__, 1, 0))) +static void log_vwrite(const char *msg, va_list ap) { - char *fmt; + char *fmt, *out; + struct timeval tv; if (log_file == NULL) return; - if (asprintf(&fmt, "%s\n", msg) == -1) + if (vasprintf(&fmt, msg, ap) == -1) exit(1); - if (vfprintf(log_file, fmt, ap) == -1) + if (stravis(&out, fmt, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL) == -1) exit(1); + + gettimeofday(&tv, NULL); + + if (is_log_stdout()) { + if (fprintf(log_file, "%s\n", out) == -1) + exit(1); + } else { + if (fprintf(log_file, "%lld.%06d %s\n", (long long)tv.tv_sec, + (int)tv.tv_usec, out) == -1) + exit(1); + } + fflush(log_file); + + free(out); free(fmt); } /* Log a debug message. */ void -log_debug(const char *msg, ...) +log_emit(int level, const char *msg, ...) { va_list ap; + if (log_level < level) + return; + va_start(ap, msg); log_vwrite(msg, ap); va_end(ap); } /* Log a critical error with error string and die. */ +__attribute__((__format__(__printf__, 1, 0))) __dead void -log_fatal(const char *msg, ...) +fatal(const char *msg, ...) { char *fmt; va_list ap; @@ -101,13 +158,15 @@ log_fatal(const char *msg, ...) va_start(ap, msg); if (asprintf(&fmt, "fatal: %s: %s", msg, strerror(errno)) == -1) exit(1); - log_vwrite(fmt, ap); + msg = fmt; + log_vwrite(msg, ap); exit(1); } /* Log a critical error and die. */ +__attribute__((__format__(__printf__, 1, 0))) __dead void -log_fatalx(const char *msg, ...) +fatalx(const char *msg, ...) { char *fmt; va_list ap; @@ -115,6 +174,7 @@ log_fatalx(const char *msg, ...) va_start(ap, msg); if (asprintf(&fmt, "fatal: %s", msg) == -1) exit(1); - log_vwrite(fmt, ap); + msg = fmt; + log_vwrite(msg, ap); exit(1); } diff --git a/logo/LICENSE b/logo/LICENSE new file mode 100644 index 00000000..3f44eb59 --- /dev/null +++ b/logo/LICENSE @@ -0,0 +1,13 @@ +Copyright (c) 2015, Jason Long + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/logo/favicon.ico b/logo/favicon.ico new file mode 100644 index 00000000..6e5398a7 Binary files /dev/null and b/logo/favicon.ico differ diff --git a/logo/tmux-logo-1-color.eps b/logo/tmux-logo-1-color.eps new file mode 100644 index 00000000..20018e67 --- /dev/null +++ b/logo/tmux-logo-1-color.eps @@ -0,0 +1,922 @@ +%!PS-Adobe-3.0 EPSF-3.0 +%APL_DSC_Encoding: UTF8 +%APLProducer: (Version 10.10.3 (Build 14D136) Quartz PS Context) +%%Title: (Unknown) +%%Creator: (Unknown) +%%CreationDate: (Unknown) +%%For: (Unknown) +%%DocumentData: Clean7Bit +%%LanguageLevel: 2 +%%Pages: 1 +%%BoundingBox: 0 0 608 160 +%%EndComments +%%BeginProlog +%%BeginFile: cg-pdf.ps +%%Copyright: Copyright 2000-2004 Apple Computer Incorporated. +%%Copyright: All Rights Reserved. +currentpacking true setpacking +/cg_md 141 dict def +cg_md begin +/L3? languagelevel 3 ge def +/bd{bind def}bind def +/ld{load def}bd +/xs{exch store}bd +/xd{exch def}bd +/cmmtx matrix def +mark +/sc/setcolor +/scs/setcolorspace +/dr/defineresource +/fr/findresource +/T/true +/F/false +/d/setdash +/w/setlinewidth +/J/setlinecap +/j/setlinejoin +/M/setmiterlimit +/i/setflat +/rc/rectclip +/rf/rectfill +/rs/rectstroke +/f/fill +/f*/eofill +/sf/selectfont +/s/show +/xS/xshow +/yS/yshow +/xyS/xyshow +/S/stroke +/m/moveto +/l/lineto +/c/curveto +/h/closepath +/n/newpath +/q/gsave +/Q/grestore +counttomark 2 idiv +{ld}repeat pop +/SC{ + /ColorSpace fr scs +}bd +/sopr /setoverprint where{pop/setoverprint}{/pop}ifelse ld +/soprm /setoverprintmode where{pop/setoverprintmode}{/pop}ifelse ld +/cgmtx matrix def +/sdmtx{cgmtx currentmatrix pop}bd +/CM {cgmtx setmatrix}bd +/cm {cmmtx astore CM concat}bd +/W{clip newpath}bd +/W*{eoclip newpath}bd +statusdict begin product end dup (HP) anchorsearch{ + pop pop pop + true +}{ + pop + (hp) anchorsearch{ + pop pop true + }{ + pop false + }ifelse +}ifelse +{ + { + { + pop pop + (0)dup 0 4 -1 roll put + F charpath + }cshow + } +}{ + {F charpath} +}ifelse +/cply exch bd +/cps {cply stroke}bd +/pgsave 0 def +/bp{/pgsave save store}bd +/ep{pgsave restore showpage}def +/re{4 2 roll m 1 index 0 rlineto 0 exch rlineto neg 0 rlineto h}bd +/scrdict 10 dict def +/scrmtx matrix def +/patarray 0 def +/createpat{patarray 3 1 roll put}bd +/makepat{ +scrmtx astore pop +gsave +initgraphics +CM +patarray exch get +scrmtx +makepattern +grestore +setpattern +}bd +/cg_BeginEPSF{ + userdict save/cg_b4_Inc_state exch put + userdict/cg_endepsf/cg_EndEPSF load put + count userdict/cg_op_count 3 -1 roll put + countdictstack dup array dictstack userdict/cg_dict_array 3 -1 roll put + 3 sub{end}repeat + /showpage {} def + 0 setgray 0 setlinecap 1 setlinewidth 0 setlinejoin + 10 setmiterlimit [] 0 setdash newpath + false setstrokeadjust false setoverprint +}bd +/cg_EndEPSF{ + countdictstack 3 sub { end } repeat + cg_dict_array 3 1 index length 3 sub getinterval + {begin}forall + count userdict/cg_op_count get sub{pop}repeat + userdict/cg_b4_Inc_state get restore + F setpacking +}bd +/cg_biproc{currentfile/RunLengthDecode filter}bd +/cg_aiproc{currentfile/ASCII85Decode filter/RunLengthDecode filter}bd +/ImageDataSource 0 def +L3?{ + /cg_mibiproc{pop pop/ImageDataSource{cg_biproc}def}bd + /cg_miaiproc{pop pop/ImageDataSource{cg_aiproc}def}bd +}{ + /ImageBandMask 0 def + /ImageBandData 0 def + /cg_mibiproc{ + string/ImageBandMask xs + string/ImageBandData xs + /ImageDataSource{[currentfile/RunLengthDecode filter dup ImageBandMask/readstring cvx + /pop cvx dup ImageBandData/readstring cvx/pop cvx]cvx bind}bd + }bd + /cg_miaiproc{ + string/ImageBandMask xs + string/ImageBandData xs + /ImageDataSource{[currentfile/ASCII85Decode filter/RunLengthDecode filter + dup ImageBandMask/readstring cvx + /pop cvx dup ImageBandData/readstring cvx/pop cvx]cvx bind}bd + }bd +}ifelse +/imsave 0 def +/BI{save/imsave xd mark}bd +/EI{imsave restore}bd +/ID{ +counttomark 2 idiv +dup 2 add +dict begin +{def} repeat +pop +/ImageType 1 def +/ImageMatrix[Width 0 0 Height neg 0 Height]def +currentdict dup/ImageMask known{ImageMask}{F}ifelse exch +L3?{ + dup/MaskedImage known + { + pop + << + /ImageType 3 + /InterleaveType 2 + /DataDict currentdict + /MaskDict + << /ImageType 1 + /Width Width + /Height Height + /ImageMatrix ImageMatrix + /BitsPerComponent 1 + /Decode [0 1] + currentdict/Interpolate known + {/Interpolate Interpolate}if + >> + >> + }if +}if +exch +{imagemask}{image}ifelse +end +}bd +/cguidfix{statusdict begin mark version end +{cvr}stopped{cleartomark 0}{exch pop}ifelse +2012 lt{dup findfont dup length dict begin +{1 index/FID ne 2 index/UniqueID ne and +{def} {pop pop} ifelse}forall +currentdict end definefont pop +}{pop}ifelse +}bd +/t_array 0 def +/t_i 0 def +/t_c 1 string def +/x_proc{ + exch t_array t_i get add exch moveto + /t_i t_i 1 add store +}bd +/y_proc{ + t_array t_i get add moveto + /t_i t_i 1 add store +}bd +/xy_proc{ + + t_array t_i 2 copy 1 add get 3 1 roll get + 4 -1 roll add 3 1 roll add moveto + /t_i t_i 2 add store +}bd +/sop 0 def +/cp_proc/x_proc ld +/base_charpath +{ + /t_array xs + /t_i 0 def + { + t_c 0 3 -1 roll put + currentpoint + t_c cply sop + cp_proc + }forall + /t_array 0 def +}bd +/sop/stroke ld +/nop{}def +/xsp/base_charpath ld +/ysp{/cp_proc/y_proc ld base_charpath/cp_proc/x_proc ld}bd +/xysp{/cp_proc/xy_proc ld base_charpath/cp_proc/x_proc ld}bd +/xmp{/sop/nop ld /cp_proc/x_proc ld base_charpath/sop/stroke ld}bd +/ymp{/sop/nop ld /cp_proc/y_proc ld base_charpath/sop/stroke ld}bd +/xymp{/sop/nop ld /cp_proc/xy_proc ld base_charpath/sop/stroke ld}bd +/refnt{ +findfont dup length dict copy dup +/Encoding 4 -1 roll put +definefont pop +}bd +/renmfont{ +findfont dup length dict copy definefont pop +}bd +L3? dup dup{save exch}if +/Range 0 def +/DataSource 0 def +/val 0 def +/nRange 0 def +/mulRange 0 def +/d0 0 def +/r0 0 def +/di 0 def +/ri 0 def +/a0 0 def +/a1 0 def +/r1 0 def +/r2 0 def +/dx 0 def +/Nsteps 0 def +/sh3tp 0 def +/ymax 0 def +/ymin 0 def +/xmax 0 def +/xmin 0 def +/setupFunEval +{ + begin + /nRange Range length 2 idiv store + /mulRange + + [ + 0 1 nRange 1 sub + { + 2 mul/nDim2 xd + Range nDim2 get + Range nDim2 1 add get + 1 index sub + + 255 div + exch + }for + ]store + end +}bd +/FunEval +{ + begin + + nRange mul /val xd + + 0 1 nRange 1 sub + { + dup 2 mul/nDim2 xd + val + add DataSource exch get + mulRange nDim2 get mul + mulRange nDim2 1 add get + add + }for + end +}bd +/max +{ + 2 copy lt + {exch pop}{pop}ifelse +}bd +/sh2 +{ + /Coords load aload pop + 3 index 3 index translate + + 3 -1 roll sub + 3 1 roll exch + sub + 2 copy + dup mul exch dup mul add sqrt + dup + scale + atan + + rotate + + /Function load setupFunEval + + + clippath {pathbbox}stopped {0 0 0 0}if newpath + /ymax xs + /xmax xs + /ymin xs + /xmin xs + currentdict/Extend known + { + /Extend load 0 get + { + 0/Function load FunEval sc + xmin ymin xmin abs ymax ymin sub rectfill + }if + }if + + /Nsteps/Function load/Size get 0 get 1 sub store + /dx 1 Nsteps div store + gsave + /di ymax ymin sub store + /Function load + + 0 1 Nsteps + { + 1 index FunEval sc + 0 ymin dx di rectfill + dx 0 translate + }for + pop + grestore + currentdict/Extend known + { + /Extend load 1 get + { + Nsteps/Function load FunEval sc + 1 ymin xmax 1 sub abs ymax ymin sub rectfill + }if + }if +}bd +/shp +{ + 4 copy + + dup 0 gt{ + 0 exch a1 a0 arc + }{ + pop 0 moveto + }ifelse + dup 0 gt{ + 0 exch a0 a1 arcn + }{ + pop 0 lineto + }ifelse + + fill + + dup 0 gt{ + 0 exch a0 a1 arc + }{ + pop 0 moveto + }ifelse + dup 0 gt{ + 0 exch a1 a0 arcn + }{ + pop 0 lineto + }ifelse + + fill +}bd +/calcmaxs +{ + + xmin dup mul ymin dup mul add sqrt + xmax dup mul ymin dup mul add sqrt + xmin dup mul ymax dup mul add sqrt + xmax dup mul ymax dup mul add sqrt + max max max +}bd +/sh3 +{ + /Coords load aload pop + 5 index 5 index translate + 3 -1 roll 6 -1 roll sub + 3 -1 roll 5 -1 roll sub + 2 copy dup mul exch dup mul add sqrt + /dx xs + 2 copy 0 ne exch 0 ne or + { + + exch atan rotate + }{ + pop pop + }ifelse + + /r2 xs + /r1 xs + /Function load + dup/Size get 0 get 1 sub + /Nsteps xs + setupFunEval + + + + + + dx r2 add r1 lt{ + + 0 + }{ + dx r1 add r2 le + { + 1 + }{ + r1 r2 eq + { + 2 + }{ + 3 + }ifelse + }ifelse + }ifelse + /sh3tp xs + clippath {pathbbox}stopped {0 0 0 0}if + newpath + /ymax xs + /xmax xs + /ymin xs + /xmin xs + + dx dup mul r2 r1 sub dup mul sub dup 0 gt + { + sqrt r2 r1 sub atan + /a0 exch 180 exch sub store + /a1 a0 neg store + }{ + pop + /a0 0 store + /a1 360 store + }ifelse + currentdict/Extend known + { + /Extend load 0 get r1 0 gt and + { + 0/Function load FunEval sc + + + + + { + { + dx 0 r1 360 0 arcn + xmin ymin moveto + xmax ymin lineto + xmax ymax lineto + xmin ymax lineto + xmin ymin lineto + eofill + } + { + r1 0 gt{0 0 r1 0 360 arc fill}if + } + { + + + + + 0 r1 xmin abs r1 add neg r1 shp + } + { + + + r2 r1 gt{ + + 0 r1 + r1 neg r2 r1 sub div dx mul + 0 + shp + }{ + + + + 0 r1 calcmaxs + dup + + r2 add dx mul dx r1 r2 sub sub div + neg + exch 1 index + abs exch sub + shp + }ifelse + } + }sh3tp get exec + }if + }if + + /d0 0 store + /r0 r1 store + /di dx Nsteps div store + /ri r2 r1 sub Nsteps div store + /Function load + 0 1 Nsteps + { + 1 index FunEval sc + d0 di add r0 ri add d0 r0 shp + { + + d0 0 r0 a1 a0 arc + d0 di add 0 r0 ri add a0 a1 arcn + fill + + + d0 0 r0 a0 a1 arc + d0 di add 0 r0 ri add a1 a0 arcn + fill + }pop + + + /d0 d0 di add store + /r0 r0 ri add store + }for + pop + + currentdict/Extend known + { + /Extend load 1 get r2 0 gt and + { + Nsteps/Function load FunEval sc + + + + + { + { + dx 0 r2 0 360 arc fill + } + { + dx 0 r2 360 0 arcn + xmin ymin moveto + xmax ymin lineto + xmax ymax lineto + xmin ymax lineto + xmin ymin lineto + eofill + } + { + + + xmax abs r1 add r1 dx r1 shp + } + { + + r2 r1 gt{ + + + + calcmaxs dup + + r1 add dx mul dx r2 r1 sub sub div + exch 1 index + exch sub + dx r2 + shp + }{ + + r1 neg r2 r1 sub div dx mul + 0 + dx + r2 + shp + }ifelse + } + } + sh3tp get exec + }if + }if +}bd +/sh +{ + begin + /ShadingType load dup dup 2 eq exch 3 eq or + { + gsave + newpath + /ColorSpace load scs + currentdict/BBox known + { + /BBox load aload pop + 2 index sub + 3 index + 3 -1 roll exch sub + exch rectclip + }if + 2 eq + {sh2}{sh3}ifelse + grestore + }{ + + pop + (DEBUG: shading type unimplemented\n)print flush + }ifelse + end +}bd +{restore}if not dup{save exch}if + L3?{ + /sh/shfill ld + /csq/clipsave ld + /csQ/cliprestore ld + }if +{restore}if +end +setpacking +%%EndFile +%%EndProlog +%%BeginSetup +%%EndSetup +%%Page: 1 1 +%%PageBoundingBox: 0 0 608 160 +%%BeginPageSetup +cg_md begin +bp +sdmtx +[ /CIEBasedABC 4 dict dup begin +/WhitePoint [ 0.9505 1.0000 1.0891 ] def +/DecodeABC [ +{ 1.0 0.0 3 -1 roll 1 index 1 index le { exch pop} { pop } ifelse + 1 index 1 index ge { exch pop } { pop } ifelse < +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000001010101010101010101010101 +0101010101010101010101010101010101010101010101020202020202020202 +0202020202020202020202020202020202030303030303030303030303030303 +0303030303030304040404040404040404040404040404040404050505050505 +0505050505050505050506060606060606060606060606060607070707070707 +0707070707070708080808080808080808080808090909090909090909090909 +0a0a0a0a0a0a0a0a0a0a0a0b0b0b0b0b0b0b0b0b0b0b0c0c0c0c0c0c0c0c0c0c +0d0d0d0d0d0d0d0d0d0d0e0e0e0e0e0e0e0e0e0f0f0f0f0f0f0f0f0f10101010 +1010101010111111111111111112121212121212121313131313131313141414 +1414141414151515151515151616161616161616171717171717171818181818 +18181919191919191a1a1a1a1a1a1a1b1b1b1b1b1b1c1c1c1c1c1c1c1d1d1d1d +1d1d1e1e1e1e1e1e1f1f1f1f1f1f202020202020212121212121222222222223 +2323232323242424242425252525252526262626262727272727282828282829 +292929292a2a2a2a2a2b2b2b2b2b2c2c2c2c2c2d2d2d2d2d2e2e2e2e2e2f2f2f +2f2f303030303131313131323232323333333333343434343535353535363636 +36373737373838383839393939393a3a3a3a3b3b3b3b3c3c3c3c3d3d3d3d3e3e +3e3e3f3f3f3f4040404041414141424242424343434444444445454545464646 +4647474748484848494949494a4a4a4b4b4b4b4c4c4c4d4d4d4d4e4e4e4f4f4f +4f50505051515151525252535353535454545555555656565657575758585859 +59595a5a5a5a5b5b5b5c5c5c5d5d5d5e5e5e5f5f5f6060606061616162626263 +63636464646565656666666767676868686969696a6a6a6b6b6b6c6c6d6d6d6e +6e6e6f6f6f707070717171727273737374747475757576767677777878787979 +797a7a7b7b7b7c7c7c7d7d7e7e7e7f7f7f808081818182828283838484848585 +86868687878888888989898a8a8b8b8b8c8c8d8d8d8e8e8f8f90909091919292 +9293939494949595969697979798989999999a9a9b9b9c9c9c9d9d9e9e9f9f9f +a0a0a1a1a2a2a3a3a3a4a4a5a5a6a6a6a7a7a8a8a9a9aaaaabababacacadadae +aeafafb0b0b0b1b1b2b2b3b3b4b4b5b5b6b6b6b7b7b8b8b9b9bababbbbbcbcbd +bdbebebebfbfc0c0c1c1c2c2c3c3c4c4c5c5c6c6c7c7c8c8c9c9cacacbcbcccc +cdcdcececfcfd0d0d1d1d2d2d3d3d4d4d5d5d6d6d7d7d8d8d9d9dadadbdcdcdd +dddededfdfe0e0e1e1e2e2e3e3e4e4e5e6e6e7e7e8e8e9e9eaeaebebecededee +eeefeff0f0f1f1f2f3f3f4f4f5f5f6f6f7f8f8f9f9fafafbfcfcfdfdfefeffff +> dup length 1 sub 3 -1 roll mul dup dup floor cvi exch ceiling + cvi 3 index exch get 4 -1 roll 3 -1 roll get + dup 3 1 roll sub 3 -1 roll dup floor cvi sub mul add 255 div } bind + +{ 1.0 0.0 3 -1 roll 1 index 1 index le { exch pop} { pop } ifelse + 1 index 1 index ge { exch pop } { pop } ifelse < +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000001010101010101010101010101 +0101010101010101010101010101010101010101010101020202020202020202 +0202020202020202020202020202020202030303030303030303030303030303 +0303030303030304040404040404040404040404040404040404050505050505 +0505050505050505050506060606060606060606060606060607070707070707 +0707070707070708080808080808080808080808090909090909090909090909 +0a0a0a0a0a0a0a0a0a0a0a0b0b0b0b0b0b0b0b0b0b0b0c0c0c0c0c0c0c0c0c0c +0d0d0d0d0d0d0d0d0d0d0e0e0e0e0e0e0e0e0e0f0f0f0f0f0f0f0f0f10101010 +1010101010111111111111111112121212121212121313131313131313141414 +1414141414151515151515151616161616161616171717171717171818181818 +18181919191919191a1a1a1a1a1a1a1b1b1b1b1b1b1c1c1c1c1c1c1c1d1d1d1d +1d1d1e1e1e1e1e1e1f1f1f1f1f1f202020202020212121212121222222222223 +2323232323242424242425252525252526262626262727272727282828282829 +292929292a2a2a2a2a2b2b2b2b2b2c2c2c2c2c2d2d2d2d2d2e2e2e2e2e2f2f2f +2f2f303030303131313131323232323333333333343434343535353535363636 +36373737373838383839393939393a3a3a3a3b3b3b3b3c3c3c3c3d3d3d3d3e3e +3e3e3f3f3f3f4040404041414141424242424343434444444445454545464646 +4647474748484848494949494a4a4a4b4b4b4b4c4c4c4d4d4d4d4e4e4e4f4f4f +4f50505051515151525252535353535454545555555656565657575758585859 +59595a5a5a5a5b5b5b5c5c5c5d5d5d5e5e5e5f5f5f6060606061616162626263 +63636464646565656666666767676868686969696a6a6a6b6b6b6c6c6d6d6d6e +6e6e6f6f6f707070717171727273737374747475757576767677777878787979 +797a7a7b7b7b7c7c7c7d7d7e7e7e7f7f7f808081818182828283838484848585 +86868687878888888989898a8a8b8b8b8c8c8d8d8d8e8e8f8f90909091919292 +9293939494949595969697979798989999999a9a9b9b9c9c9c9d9d9e9e9f9f9f +a0a0a1a1a2a2a3a3a3a4a4a5a5a6a6a6a7a7a8a8a9a9aaaaabababacacadadae +aeafafb0b0b0b1b1b2b2b3b3b4b4b5b5b6b6b6b7b7b8b8b9b9bababbbbbcbcbd +bdbebebebfbfc0c0c1c1c2c2c3c3c4c4c5c5c6c6c7c7c8c8c9c9cacacbcbcccc +cdcdcececfcfd0d0d1d1d2d2d3d3d4d4d5d5d6d6d7d7d8d8d9d9dadadbdcdcdd +dddededfdfe0e0e1e1e2e2e3e3e4e4e5e6e6e7e7e8e8e9e9eaeaebebecededee +eeefeff0f0f1f1f2f3f3f4f4f5f5f6f6f7f8f8f9f9fafafbfcfcfdfdfefeffff +> dup length 1 sub 3 -1 roll mul dup dup floor cvi exch ceiling + cvi 3 index exch get 4 -1 roll 3 -1 roll get + dup 3 1 roll sub 3 -1 roll dup floor cvi sub mul add 255 div } bind + +{ 1.0 0.0 3 -1 roll 1 index 1 index le { exch pop} { pop } ifelse + 1 index 1 index ge { exch pop } { pop } ifelse < +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000001010101010101010101010101 +0101010101010101010101010101010101010101010101020202020202020202 +0202020202020202020202020202020202030303030303030303030303030303 +0303030303030304040404040404040404040404040404040404050505050505 +0505050505050505050506060606060606060606060606060607070707070707 +0707070707070708080808080808080808080808090909090909090909090909 +0a0a0a0a0a0a0a0a0a0a0a0b0b0b0b0b0b0b0b0b0b0b0c0c0c0c0c0c0c0c0c0c +0d0d0d0d0d0d0d0d0d0d0e0e0e0e0e0e0e0e0e0f0f0f0f0f0f0f0f0f10101010 +1010101010111111111111111112121212121212121313131313131313141414 +1414141414151515151515151616161616161616171717171717171818181818 +18181919191919191a1a1a1a1a1a1a1b1b1b1b1b1b1c1c1c1c1c1c1c1d1d1d1d +1d1d1e1e1e1e1e1e1f1f1f1f1f1f202020202020212121212121222222222223 +2323232323242424242425252525252526262626262727272727282828282829 +292929292a2a2a2a2a2b2b2b2b2b2c2c2c2c2c2d2d2d2d2d2e2e2e2e2e2f2f2f +2f2f303030303131313131323232323333333333343434343535353535363636 +36373737373838383839393939393a3a3a3a3b3b3b3b3c3c3c3c3d3d3d3d3e3e +3e3e3f3f3f3f4040404041414141424242424343434444444445454545464646 +4647474748484848494949494a4a4a4b4b4b4b4c4c4c4d4d4d4d4e4e4e4f4f4f +4f50505051515151525252535353535454545555555656565657575758585859 +59595a5a5a5a5b5b5b5c5c5c5d5d5d5e5e5e5f5f5f6060606061616162626263 +63636464646565656666666767676868686969696a6a6a6b6b6b6c6c6d6d6d6e +6e6e6f6f6f707070717171727273737374747475757576767677777878787979 +797a7a7b7b7b7c7c7c7d7d7e7e7e7f7f7f808081818182828283838484848585 +86868687878888888989898a8a8b8b8b8c8c8d8d8d8e8e8f8f90909091919292 +9293939494949595969697979798989999999a9a9b9b9c9c9c9d9d9e9e9f9f9f +a0a0a1a1a2a2a3a3a3a4a4a5a5a6a6a6a7a7a8a8a9a9aaaaabababacacadadae +aeafafb0b0b0b1b1b2b2b3b3b4b4b5b5b6b6b6b7b7b8b8b9b9bababbbbbcbcbd +bdbebebebfbfc0c0c1c1c2c2c3c3c4c4c5c5c6c6c7c7c8c8c9c9cacacbcbcccc +cdcdcececfcfd0d0d1d1d2d2d3d3d4d4d5d5d6d6d7d7d8d8d9d9dadadbdcdcdd +dddededfdfe0e0e1e1e2e2e3e3e4e4e5e6e6e7e7e8e8e9e9eaeaebebecededee +eeefeff0f0f1f1f2f3f3f4f4f5f5f6f6f7f8f8f9f9fafafbfcfcfdfdfefeffff +> dup length 1 sub 3 -1 roll mul dup dup floor cvi exch ceiling + cvi 3 index exch get 4 -1 roll 3 -1 roll get + dup 3 1 roll sub 3 -1 roll dup floor cvi sub mul add 255 div } bind +] def +/MatrixABC [ 0.4124 0.2126 0.0193 0.3576 0.7151 0.1192 0.1805 0.0722 0.9508 ] def +/RangeLMN [ 0.0 0.9505 0.0 1.0000 0.0 1.0891 ] def +end ] /Cs1 exch/ColorSpace dr pop +%%EndPageSetup +0.60000002 i +/Cs1 SC +0.23529412 0.23529412 0.23529412 sc +q +2 15.003872 m +2 7.8149743 7.8157911 2 14.998466 2 c +145.00154 2 l +152.17584 2 158 7.8244047 158 15.003872 c +160 15.003872 m +160 6.7174625 153.27803 0 145.00154 0 c +14.998466 0 l +6.7150416 0 0 6.7065849 0 15.003872 c +2 14 m +0 16 l +160 16 l +158 14 l +160 14 m +0 14 l +W +0 0 608 160 rc +-5 21 m +165 21 l +165 -5 l +-5 -5 l +h +f +Q +q +83 90 m +83 160 l +77 160 l +77 14 l +83 14 l +83 84 l +160 84 l +160 90 l +83 90 l +h +0 144.99352 m +0 153.28137 6.7219648 160 14.998466 160 c +145.00154 160 l +153.28496 160 160 153.27509 160 144.99352 c +160 14 l +0 14 l +0 144.99352 l +h +0 144.99352 m +W* +0 0 608 160 rc +-5 165 m +165 165 l +165 9 l +-5 9 l +h +f +Q +q +242 12 m +230.85426 12.165432 222.63789 15.032893 217.12346 20.7679 c +211.60902 26.502909 208.85185 34.995007 209 46.244446 c +209 98 l +189 98 l +189 113 l +209 113 l +209 146 l +225 146 l +225 113 l +262 112.91358 l +262 98.024689 l +225 98 l +225 46.079014 l +225.39507 39.571983 226.99422 34.802074 230.1926 31.769136 c +233.39096 28.736198 237.58186 27.219753 242.76543 27.219753 c +245.19179 27.219753 247.89381 27.49547 250.87161 28.046913 c +253.8494 28.598356 256.88229 29.425508 259.97037 30.528395 c +263.27902 15.97037 l +259.52921 14.646907 255.86215 13.681896 252.27777 13.075309 c +248.69341 12.468721 245.19179 12.165432 241.77284 12.165432 c +242 12 l +h +295 14 m +278.15918 14 l +278 112.91358 l +295 112.91358 l +295 96.370369 l +295 96.370369 304.76617 106.29628 309.67401 109.60493 c +314.58185 112.9136 320.34436 114.5679 326.96167 114.5679 c +332.80695 114.5679 337.79745 112.94117 341.93326 109.68765 c +346.06909 106.43414 349.01926 101.99509 350.78387 96.370369 c +350.78387 96.370369 361.92294 106.29628 366.99622 109.60493 c +372.06952 112.9136 378.08014 114.5679 385.02832 114.5679 c +392.74854 114.5679 398.92459 111.72801 403.55673 106.04815 c +408.18884 100.36829 410.50488 92.730911 410 83.139999 c +410 14 l +394 14 l +393.96167 80.488892 l +393.96167 87.106209 392.69336 91.820976 390.15674 94.633331 c +387.62009 97.445694 383.98062 98.851852 379.23822 98.851852 c +374.49579 98.851852 369.78104 97.418121 365.09375 94.550621 c +360.40649 91.683113 356.24316 87.878212 353 83.135803 c +353 14 l +336.06042 14 l +336.06042 80.488892 l +336.06042 87.106209 334.79211 91.820976 332.25549 94.633331 c +329.71884 97.445694 326.07938 98.851852 321.33698 98.851852 c +316.59457 98.851852 311.87979 97.418121 307.19254 94.550621 c +302.50525 91.683113 298.34192 87.878212 295 83.135803 c +295 14 l +h +438.9505 21.264198 m +433.10519 27.440361 430.18259 35.656738 430 46 c +430 113 l +447 113 l +447 49.220001 l +446.7258 42.16375 448.3801 36.814835 451.68875 33.175308 c +454.99741 29.535784 459.85004 27.716049 466.2468 27.716049 c +471.32007 27.716049 476.36569 29.177351 481.38382 32.099998 c +486.40195 35.022648 491.39243 39.35141 496 45.086418 c +496 112.91358 l +513 113 l +512.89862 14 l +496 14 l +496 30.197531 l +496 30.197531 485.13364 20.271622 479.89493 16.962963 c +474.65622 13.654305 468.78345 12 462.2764 12 c +452.57101 12 444.79578 15.088035 438.9505 21.264198 c +h +588.65784 14 m +564.50476 51.041977 l +541.84052 14 l +523.4776 14 l +556.56403 63.449383 l +524.63562 113 l +543.164 113 l +566.15906 77.180244 l +589.48499 113 l +607.84796 113 l +574.43066 64.441978 l +607.18622 14 l +588.65784 14 l +h +588.65784 14 m +W* +0 0 608 160 rc +184 151 m +612.84796 151 l +612.84796 7 l +184 7 l +h +f +ep +end +%%Trailer +%%EOF diff --git a/logo/tmux-logo-1-color.svg b/logo/tmux-logo-1-color.svg new file mode 100644 index 00000000..2e6dda44 --- /dev/null +++ b/logo/tmux-logo-1-color.svg @@ -0,0 +1,18 @@ + + + + logomark + wordmark copy 3 + Created with Sketch. + + + + + + + + + + + + + \ No newline at end of file diff --git a/logo/tmux-logo-huge.png b/logo/tmux-logo-huge.png new file mode 100644 index 00000000..141796d4 Binary files /dev/null and b/logo/tmux-logo-huge.png differ diff --git a/logo/tmux-logo-large.png b/logo/tmux-logo-large.png new file mode 100644 index 00000000..44dce469 Binary files /dev/null and b/logo/tmux-logo-large.png differ diff --git a/logo/tmux-logo-medium.png b/logo/tmux-logo-medium.png new file mode 100644 index 00000000..be2fa5c7 Binary files /dev/null and b/logo/tmux-logo-medium.png differ diff --git a/logo/tmux-logo-small.png b/logo/tmux-logo-small.png new file mode 100644 index 00000000..a13e3d7f Binary files /dev/null and b/logo/tmux-logo-small.png differ diff --git a/logo/tmux-logo.eps b/logo/tmux-logo.eps new file mode 100644 index 00000000..23db6a09 --- /dev/null +++ b/logo/tmux-logo.eps @@ -0,0 +1,925 @@ +%!PS-Adobe-3.0 EPSF-3.0 +%APL_DSC_Encoding: UTF8 +%APLProducer: (Version 10.10.3 (Build 14D136) Quartz PS Context) +%%Title: (Unknown) +%%Creator: (Unknown) +%%CreationDate: (Unknown) +%%For: (Unknown) +%%DocumentData: Clean7Bit +%%LanguageLevel: 2 +%%Pages: 1 +%%BoundingBox: 0 0 608 160 +%%EndComments +%%BeginProlog +%%BeginFile: cg-pdf.ps +%%Copyright: Copyright 2000-2004 Apple Computer Incorporated. +%%Copyright: All Rights Reserved. +currentpacking true setpacking +/cg_md 141 dict def +cg_md begin +/L3? languagelevel 3 ge def +/bd{bind def}bind def +/ld{load def}bd +/xs{exch store}bd +/xd{exch def}bd +/cmmtx matrix def +mark +/sc/setcolor +/scs/setcolorspace +/dr/defineresource +/fr/findresource +/T/true +/F/false +/d/setdash +/w/setlinewidth +/J/setlinecap +/j/setlinejoin +/M/setmiterlimit +/i/setflat +/rc/rectclip +/rf/rectfill +/rs/rectstroke +/f/fill +/f*/eofill +/sf/selectfont +/s/show +/xS/xshow +/yS/yshow +/xyS/xyshow +/S/stroke +/m/moveto +/l/lineto +/c/curveto +/h/closepath +/n/newpath +/q/gsave +/Q/grestore +counttomark 2 idiv +{ld}repeat pop +/SC{ + /ColorSpace fr scs +}bd +/sopr /setoverprint where{pop/setoverprint}{/pop}ifelse ld +/soprm /setoverprintmode where{pop/setoverprintmode}{/pop}ifelse ld +/cgmtx matrix def +/sdmtx{cgmtx currentmatrix pop}bd +/CM {cgmtx setmatrix}bd +/cm {cmmtx astore CM concat}bd +/W{clip newpath}bd +/W*{eoclip newpath}bd +statusdict begin product end dup (HP) anchorsearch{ + pop pop pop + true +}{ + pop + (hp) anchorsearch{ + pop pop true + }{ + pop false + }ifelse +}ifelse +{ + { + { + pop pop + (0)dup 0 4 -1 roll put + F charpath + }cshow + } +}{ + {F charpath} +}ifelse +/cply exch bd +/cps {cply stroke}bd +/pgsave 0 def +/bp{/pgsave save store}bd +/ep{pgsave restore showpage}def +/re{4 2 roll m 1 index 0 rlineto 0 exch rlineto neg 0 rlineto h}bd +/scrdict 10 dict def +/scrmtx matrix def +/patarray 0 def +/createpat{patarray 3 1 roll put}bd +/makepat{ +scrmtx astore pop +gsave +initgraphics +CM +patarray exch get +scrmtx +makepattern +grestore +setpattern +}bd +/cg_BeginEPSF{ + userdict save/cg_b4_Inc_state exch put + userdict/cg_endepsf/cg_EndEPSF load put + count userdict/cg_op_count 3 -1 roll put + countdictstack dup array dictstack userdict/cg_dict_array 3 -1 roll put + 3 sub{end}repeat + /showpage {} def + 0 setgray 0 setlinecap 1 setlinewidth 0 setlinejoin + 10 setmiterlimit [] 0 setdash newpath + false setstrokeadjust false setoverprint +}bd +/cg_EndEPSF{ + countdictstack 3 sub { end } repeat + cg_dict_array 3 1 index length 3 sub getinterval + {begin}forall + count userdict/cg_op_count get sub{pop}repeat + userdict/cg_b4_Inc_state get restore + F setpacking +}bd +/cg_biproc{currentfile/RunLengthDecode filter}bd +/cg_aiproc{currentfile/ASCII85Decode filter/RunLengthDecode filter}bd +/ImageDataSource 0 def +L3?{ + /cg_mibiproc{pop pop/ImageDataSource{cg_biproc}def}bd + /cg_miaiproc{pop pop/ImageDataSource{cg_aiproc}def}bd +}{ + /ImageBandMask 0 def + /ImageBandData 0 def + /cg_mibiproc{ + string/ImageBandMask xs + string/ImageBandData xs + /ImageDataSource{[currentfile/RunLengthDecode filter dup ImageBandMask/readstring cvx + /pop cvx dup ImageBandData/readstring cvx/pop cvx]cvx bind}bd + }bd + /cg_miaiproc{ + string/ImageBandMask xs + string/ImageBandData xs + /ImageDataSource{[currentfile/ASCII85Decode filter/RunLengthDecode filter + dup ImageBandMask/readstring cvx + /pop cvx dup ImageBandData/readstring cvx/pop cvx]cvx bind}bd + }bd +}ifelse +/imsave 0 def +/BI{save/imsave xd mark}bd +/EI{imsave restore}bd +/ID{ +counttomark 2 idiv +dup 2 add +dict begin +{def} repeat +pop +/ImageType 1 def +/ImageMatrix[Width 0 0 Height neg 0 Height]def +currentdict dup/ImageMask known{ImageMask}{F}ifelse exch +L3?{ + dup/MaskedImage known + { + pop + << + /ImageType 3 + /InterleaveType 2 + /DataDict currentdict + /MaskDict + << /ImageType 1 + /Width Width + /Height Height + /ImageMatrix ImageMatrix + /BitsPerComponent 1 + /Decode [0 1] + currentdict/Interpolate known + {/Interpolate Interpolate}if + >> + >> + }if +}if +exch +{imagemask}{image}ifelse +end +}bd +/cguidfix{statusdict begin mark version end +{cvr}stopped{cleartomark 0}{exch pop}ifelse +2012 lt{dup findfont dup length dict begin +{1 index/FID ne 2 index/UniqueID ne and +{def} {pop pop} ifelse}forall +currentdict end definefont pop +}{pop}ifelse +}bd +/t_array 0 def +/t_i 0 def +/t_c 1 string def +/x_proc{ + exch t_array t_i get add exch moveto + /t_i t_i 1 add store +}bd +/y_proc{ + t_array t_i get add moveto + /t_i t_i 1 add store +}bd +/xy_proc{ + + t_array t_i 2 copy 1 add get 3 1 roll get + 4 -1 roll add 3 1 roll add moveto + /t_i t_i 2 add store +}bd +/sop 0 def +/cp_proc/x_proc ld +/base_charpath +{ + /t_array xs + /t_i 0 def + { + t_c 0 3 -1 roll put + currentpoint + t_c cply sop + cp_proc + }forall + /t_array 0 def +}bd +/sop/stroke ld +/nop{}def +/xsp/base_charpath ld +/ysp{/cp_proc/y_proc ld base_charpath/cp_proc/x_proc ld}bd +/xysp{/cp_proc/xy_proc ld base_charpath/cp_proc/x_proc ld}bd +/xmp{/sop/nop ld /cp_proc/x_proc ld base_charpath/sop/stroke ld}bd +/ymp{/sop/nop ld /cp_proc/y_proc ld base_charpath/sop/stroke ld}bd +/xymp{/sop/nop ld /cp_proc/xy_proc ld base_charpath/sop/stroke ld}bd +/refnt{ +findfont dup length dict copy dup +/Encoding 4 -1 roll put +definefont pop +}bd +/renmfont{ +findfont dup length dict copy definefont pop +}bd +L3? dup dup{save exch}if +/Range 0 def +/DataSource 0 def +/val 0 def +/nRange 0 def +/mulRange 0 def +/d0 0 def +/r0 0 def +/di 0 def +/ri 0 def +/a0 0 def +/a1 0 def +/r1 0 def +/r2 0 def +/dx 0 def +/Nsteps 0 def +/sh3tp 0 def +/ymax 0 def +/ymin 0 def +/xmax 0 def +/xmin 0 def +/setupFunEval +{ + begin + /nRange Range length 2 idiv store + /mulRange + + [ + 0 1 nRange 1 sub + { + 2 mul/nDim2 xd + Range nDim2 get + Range nDim2 1 add get + 1 index sub + + 255 div + exch + }for + ]store + end +}bd +/FunEval +{ + begin + + nRange mul /val xd + + 0 1 nRange 1 sub + { + dup 2 mul/nDim2 xd + val + add DataSource exch get + mulRange nDim2 get mul + mulRange nDim2 1 add get + add + }for + end +}bd +/max +{ + 2 copy lt + {exch pop}{pop}ifelse +}bd +/sh2 +{ + /Coords load aload pop + 3 index 3 index translate + + 3 -1 roll sub + 3 1 roll exch + sub + 2 copy + dup mul exch dup mul add sqrt + dup + scale + atan + + rotate + + /Function load setupFunEval + + + clippath {pathbbox}stopped {0 0 0 0}if newpath + /ymax xs + /xmax xs + /ymin xs + /xmin xs + currentdict/Extend known + { + /Extend load 0 get + { + 0/Function load FunEval sc + xmin ymin xmin abs ymax ymin sub rectfill + }if + }if + + /Nsteps/Function load/Size get 0 get 1 sub store + /dx 1 Nsteps div store + gsave + /di ymax ymin sub store + /Function load + + 0 1 Nsteps + { + 1 index FunEval sc + 0 ymin dx di rectfill + dx 0 translate + }for + pop + grestore + currentdict/Extend known + { + /Extend load 1 get + { + Nsteps/Function load FunEval sc + 1 ymin xmax 1 sub abs ymax ymin sub rectfill + }if + }if +}bd +/shp +{ + 4 copy + + dup 0 gt{ + 0 exch a1 a0 arc + }{ + pop 0 moveto + }ifelse + dup 0 gt{ + 0 exch a0 a1 arcn + }{ + pop 0 lineto + }ifelse + + fill + + dup 0 gt{ + 0 exch a0 a1 arc + }{ + pop 0 moveto + }ifelse + dup 0 gt{ + 0 exch a1 a0 arcn + }{ + pop 0 lineto + }ifelse + + fill +}bd +/calcmaxs +{ + + xmin dup mul ymin dup mul add sqrt + xmax dup mul ymin dup mul add sqrt + xmin dup mul ymax dup mul add sqrt + xmax dup mul ymax dup mul add sqrt + max max max +}bd +/sh3 +{ + /Coords load aload pop + 5 index 5 index translate + 3 -1 roll 6 -1 roll sub + 3 -1 roll 5 -1 roll sub + 2 copy dup mul exch dup mul add sqrt + /dx xs + 2 copy 0 ne exch 0 ne or + { + + exch atan rotate + }{ + pop pop + }ifelse + + /r2 xs + /r1 xs + /Function load + dup/Size get 0 get 1 sub + /Nsteps xs + setupFunEval + + + + + + dx r2 add r1 lt{ + + 0 + }{ + dx r1 add r2 le + { + 1 + }{ + r1 r2 eq + { + 2 + }{ + 3 + }ifelse + }ifelse + }ifelse + /sh3tp xs + clippath {pathbbox}stopped {0 0 0 0}if + newpath + /ymax xs + /xmax xs + /ymin xs + /xmin xs + + dx dup mul r2 r1 sub dup mul sub dup 0 gt + { + sqrt r2 r1 sub atan + /a0 exch 180 exch sub store + /a1 a0 neg store + }{ + pop + /a0 0 store + /a1 360 store + }ifelse + currentdict/Extend known + { + /Extend load 0 get r1 0 gt and + { + 0/Function load FunEval sc + + + + + { + { + dx 0 r1 360 0 arcn + xmin ymin moveto + xmax ymin lineto + xmax ymax lineto + xmin ymax lineto + xmin ymin lineto + eofill + } + { + r1 0 gt{0 0 r1 0 360 arc fill}if + } + { + + + + + 0 r1 xmin abs r1 add neg r1 shp + } + { + + + r2 r1 gt{ + + 0 r1 + r1 neg r2 r1 sub div dx mul + 0 + shp + }{ + + + + 0 r1 calcmaxs + dup + + r2 add dx mul dx r1 r2 sub sub div + neg + exch 1 index + abs exch sub + shp + }ifelse + } + }sh3tp get exec + }if + }if + + /d0 0 store + /r0 r1 store + /di dx Nsteps div store + /ri r2 r1 sub Nsteps div store + /Function load + 0 1 Nsteps + { + 1 index FunEval sc + d0 di add r0 ri add d0 r0 shp + { + + d0 0 r0 a1 a0 arc + d0 di add 0 r0 ri add a0 a1 arcn + fill + + + d0 0 r0 a0 a1 arc + d0 di add 0 r0 ri add a1 a0 arcn + fill + }pop + + + /d0 d0 di add store + /r0 r0 ri add store + }for + pop + + currentdict/Extend known + { + /Extend load 1 get r2 0 gt and + { + Nsteps/Function load FunEval sc + + + + + { + { + dx 0 r2 0 360 arc fill + } + { + dx 0 r2 360 0 arcn + xmin ymin moveto + xmax ymin lineto + xmax ymax lineto + xmin ymax lineto + xmin ymin lineto + eofill + } + { + + + xmax abs r1 add r1 dx r1 shp + } + { + + r2 r1 gt{ + + + + calcmaxs dup + + r1 add dx mul dx r2 r1 sub sub div + exch 1 index + exch sub + dx r2 + shp + }{ + + r1 neg r2 r1 sub div dx mul + 0 + dx + r2 + shp + }ifelse + } + } + sh3tp get exec + }if + }if +}bd +/sh +{ + begin + /ShadingType load dup dup 2 eq exch 3 eq or + { + gsave + newpath + /ColorSpace load scs + currentdict/BBox known + { + /BBox load aload pop + 2 index sub + 3 index + 3 -1 roll exch sub + exch rectclip + }if + 2 eq + {sh2}{sh3}ifelse + grestore + }{ + + pop + (DEBUG: shading type unimplemented\n)print flush + }ifelse + end +}bd +{restore}if not dup{save exch}if + L3?{ + /sh/shfill ld + /csq/clipsave ld + /csQ/cliprestore ld + }if +{restore}if +end +setpacking +%%EndFile +%%EndProlog +%%BeginSetup +%%EndSetup +%%Page: 1 1 +%%PageBoundingBox: 0 0 608 160 +%%BeginPageSetup +cg_md begin +bp +sdmtx +[ /CIEBasedABC 4 dict dup begin +/WhitePoint [ 0.9505 1.0000 1.0891 ] def +/DecodeABC [ +{ 1.0 0.0 3 -1 roll 1 index 1 index le { exch pop} { pop } ifelse + 1 index 1 index ge { exch pop } { pop } ifelse < +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000001010101010101010101010101 +0101010101010101010101010101010101010101010101020202020202020202 +0202020202020202020202020202020202030303030303030303030303030303 +0303030303030304040404040404040404040404040404040404050505050505 +0505050505050505050506060606060606060606060606060607070707070707 +0707070707070708080808080808080808080808090909090909090909090909 +0a0a0a0a0a0a0a0a0a0a0a0b0b0b0b0b0b0b0b0b0b0b0c0c0c0c0c0c0c0c0c0c +0d0d0d0d0d0d0d0d0d0d0e0e0e0e0e0e0e0e0e0f0f0f0f0f0f0f0f0f10101010 +1010101010111111111111111112121212121212121313131313131313141414 +1414141414151515151515151616161616161616171717171717171818181818 +18181919191919191a1a1a1a1a1a1a1b1b1b1b1b1b1c1c1c1c1c1c1c1d1d1d1d +1d1d1e1e1e1e1e1e1f1f1f1f1f1f202020202020212121212121222222222223 +2323232323242424242425252525252526262626262727272727282828282829 +292929292a2a2a2a2a2b2b2b2b2b2c2c2c2c2c2d2d2d2d2d2e2e2e2e2e2f2f2f +2f2f303030303131313131323232323333333333343434343535353535363636 +36373737373838383839393939393a3a3a3a3b3b3b3b3c3c3c3c3d3d3d3d3e3e +3e3e3f3f3f3f4040404041414141424242424343434444444445454545464646 +4647474748484848494949494a4a4a4b4b4b4b4c4c4c4d4d4d4d4e4e4e4f4f4f +4f50505051515151525252535353535454545555555656565657575758585859 +59595a5a5a5a5b5b5b5c5c5c5d5d5d5e5e5e5f5f5f6060606061616162626263 +63636464646565656666666767676868686969696a6a6a6b6b6b6c6c6d6d6d6e +6e6e6f6f6f707070717171727273737374747475757576767677777878787979 +797a7a7b7b7b7c7c7c7d7d7e7e7e7f7f7f808081818182828283838484848585 +86868687878888888989898a8a8b8b8b8c8c8d8d8d8e8e8f8f90909091919292 +9293939494949595969697979798989999999a9a9b9b9c9c9c9d9d9e9e9f9f9f +a0a0a1a1a2a2a3a3a3a4a4a5a5a6a6a6a7a7a8a8a9a9aaaaabababacacadadae +aeafafb0b0b0b1b1b2b2b3b3b4b4b5b5b6b6b6b7b7b8b8b9b9bababbbbbcbcbd +bdbebebebfbfc0c0c1c1c2c2c3c3c4c4c5c5c6c6c7c7c8c8c9c9cacacbcbcccc +cdcdcececfcfd0d0d1d1d2d2d3d3d4d4d5d5d6d6d7d7d8d8d9d9dadadbdcdcdd +dddededfdfe0e0e1e1e2e2e3e3e4e4e5e6e6e7e7e8e8e9e9eaeaebebecededee +eeefeff0f0f1f1f2f3f3f4f4f5f5f6f6f7f8f8f9f9fafafbfcfcfdfdfefeffff +> dup length 1 sub 3 -1 roll mul dup dup floor cvi exch ceiling + cvi 3 index exch get 4 -1 roll 3 -1 roll get + dup 3 1 roll sub 3 -1 roll dup floor cvi sub mul add 255 div } bind + +{ 1.0 0.0 3 -1 roll 1 index 1 index le { exch pop} { pop } ifelse + 1 index 1 index ge { exch pop } { pop } ifelse < +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000001010101010101010101010101 +0101010101010101010101010101010101010101010101020202020202020202 +0202020202020202020202020202020202030303030303030303030303030303 +0303030303030304040404040404040404040404040404040404050505050505 +0505050505050505050506060606060606060606060606060607070707070707 +0707070707070708080808080808080808080808090909090909090909090909 +0a0a0a0a0a0a0a0a0a0a0a0b0b0b0b0b0b0b0b0b0b0b0c0c0c0c0c0c0c0c0c0c +0d0d0d0d0d0d0d0d0d0d0e0e0e0e0e0e0e0e0e0f0f0f0f0f0f0f0f0f10101010 +1010101010111111111111111112121212121212121313131313131313141414 +1414141414151515151515151616161616161616171717171717171818181818 +18181919191919191a1a1a1a1a1a1a1b1b1b1b1b1b1c1c1c1c1c1c1c1d1d1d1d +1d1d1e1e1e1e1e1e1f1f1f1f1f1f202020202020212121212121222222222223 +2323232323242424242425252525252526262626262727272727282828282829 +292929292a2a2a2a2a2b2b2b2b2b2c2c2c2c2c2d2d2d2d2d2e2e2e2e2e2f2f2f +2f2f303030303131313131323232323333333333343434343535353535363636 +36373737373838383839393939393a3a3a3a3b3b3b3b3c3c3c3c3d3d3d3d3e3e +3e3e3f3f3f3f4040404041414141424242424343434444444445454545464646 +4647474748484848494949494a4a4a4b4b4b4b4c4c4c4d4d4d4d4e4e4e4f4f4f +4f50505051515151525252535353535454545555555656565657575758585859 +59595a5a5a5a5b5b5b5c5c5c5d5d5d5e5e5e5f5f5f6060606061616162626263 +63636464646565656666666767676868686969696a6a6a6b6b6b6c6c6d6d6d6e +6e6e6f6f6f707070717171727273737374747475757576767677777878787979 +797a7a7b7b7b7c7c7c7d7d7e7e7e7f7f7f808081818182828283838484848585 +86868687878888888989898a8a8b8b8b8c8c8d8d8d8e8e8f8f90909091919292 +9293939494949595969697979798989999999a9a9b9b9c9c9c9d9d9e9e9f9f9f +a0a0a1a1a2a2a3a3a3a4a4a5a5a6a6a6a7a7a8a8a9a9aaaaabababacacadadae +aeafafb0b0b0b1b1b2b2b3b3b4b4b5b5b6b6b6b7b7b8b8b9b9bababbbbbcbcbd +bdbebebebfbfc0c0c1c1c2c2c3c3c4c4c5c5c6c6c7c7c8c8c9c9cacacbcbcccc +cdcdcececfcfd0d0d1d1d2d2d3d3d4d4d5d5d6d6d7d7d8d8d9d9dadadbdcdcdd +dddededfdfe0e0e1e1e2e2e3e3e4e4e5e6e6e7e7e8e8e9e9eaeaebebecededee +eeefeff0f0f1f1f2f3f3f4f4f5f5f6f6f7f8f8f9f9fafafbfcfcfdfdfefeffff +> dup length 1 sub 3 -1 roll mul dup dup floor cvi exch ceiling + cvi 3 index exch get 4 -1 roll 3 -1 roll get + dup 3 1 roll sub 3 -1 roll dup floor cvi sub mul add 255 div } bind + +{ 1.0 0.0 3 -1 roll 1 index 1 index le { exch pop} { pop } ifelse + 1 index 1 index ge { exch pop } { pop } ifelse < +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000001010101010101010101010101 +0101010101010101010101010101010101010101010101020202020202020202 +0202020202020202020202020202020202030303030303030303030303030303 +0303030303030304040404040404040404040404040404040404050505050505 +0505050505050505050506060606060606060606060606060607070707070707 +0707070707070708080808080808080808080808090909090909090909090909 +0a0a0a0a0a0a0a0a0a0a0a0b0b0b0b0b0b0b0b0b0b0b0c0c0c0c0c0c0c0c0c0c +0d0d0d0d0d0d0d0d0d0d0e0e0e0e0e0e0e0e0e0f0f0f0f0f0f0f0f0f10101010 +1010101010111111111111111112121212121212121313131313131313141414 +1414141414151515151515151616161616161616171717171717171818181818 +18181919191919191a1a1a1a1a1a1a1b1b1b1b1b1b1c1c1c1c1c1c1c1d1d1d1d +1d1d1e1e1e1e1e1e1f1f1f1f1f1f202020202020212121212121222222222223 +2323232323242424242425252525252526262626262727272727282828282829 +292929292a2a2a2a2a2b2b2b2b2b2c2c2c2c2c2d2d2d2d2d2e2e2e2e2e2f2f2f +2f2f303030303131313131323232323333333333343434343535353535363636 +36373737373838383839393939393a3a3a3a3b3b3b3b3c3c3c3c3d3d3d3d3e3e +3e3e3f3f3f3f4040404041414141424242424343434444444445454545464646 +4647474748484848494949494a4a4a4b4b4b4b4c4c4c4d4d4d4d4e4e4e4f4f4f +4f50505051515151525252535353535454545555555656565657575758585859 +59595a5a5a5a5b5b5b5c5c5c5d5d5d5e5e5e5f5f5f6060606061616162626263 +63636464646565656666666767676868686969696a6a6a6b6b6b6c6c6d6d6d6e +6e6e6f6f6f707070717171727273737374747475757576767677777878787979 +797a7a7b7b7b7c7c7c7d7d7e7e7e7f7f7f808081818182828283838484848585 +86868687878888888989898a8a8b8b8b8c8c8d8d8d8e8e8f8f90909091919292 +9293939494949595969697979798989999999a9a9b9b9c9c9c9d9d9e9e9f9f9f +a0a0a1a1a2a2a3a3a3a4a4a5a5a6a6a6a7a7a8a8a9a9aaaaabababacacadadae +aeafafb0b0b0b1b1b2b2b3b3b4b4b5b5b6b6b6b7b7b8b8b9b9bababbbbbcbcbd +bdbebebebfbfc0c0c1c1c2c2c3c3c4c4c5c5c6c6c7c7c8c8c9c9cacacbcbcccc +cdcdcececfcfd0d0d1d1d2d2d3d3d4d4d5d5d6d6d7d7d8d8d9d9dadadbdcdcdd +dddededfdfe0e0e1e1e2e2e3e3e4e4e5e6e6e7e7e8e8e9e9eaeaebebecededee +eeefeff0f0f1f1f2f3f3f4f4f5f5f6f6f7f8f8f9f9fafafbfcfcfdfdfefeffff +> dup length 1 sub 3 -1 roll mul dup dup floor cvi exch ceiling + cvi 3 index exch get 4 -1 roll 3 -1 roll get + dup 3 1 roll sub 3 -1 roll dup floor cvi sub mul add 255 div } bind +] def +/MatrixABC [ 0.4124 0.2126 0.0193 0.3576 0.7151 0.1192 0.1805 0.0722 0.9508 ] def +/RangeLMN [ 0.0 0.9505 0.0 1.0000 0.0 1.0891 ] def +end ] /Cs1 exch/ColorSpace dr pop +%%EndPageSetup +0.60000002 i +/Cs1 SC +0.10588235 0.72549021 0.12156863 sc +q +0 44 m +160 44 l +160 15.003872 l +160 6.7174625 153.27803 0 145.00154 0 c +14.998466 0 l +6.7150416 0 0 6.7065849 0 15.003872 c +0 44 l +h +0 44 m +160 44 l +160 14 l +0 14 l +0 44 l +h +0 44 m +W* +0 0 608 160 rc +-5 49 m +165 49 l +165 -5 l +-5 -5 l +h +f +Q +0.23529412 0.23529412 0.23529412 sc +q +83 90 m +83 160 l +77 160 l +77 14 l +83 14 l +83 84 l +160 84 l +160 90 l +83 90 l +h +0 144.99352 m +0 153.28137 6.7219648 160 14.998466 160 c +145.00154 160 l +153.28496 160 160 153.27509 160 144.99352 c +160 14 l +0 14 l +0 144.99352 l +h +0 144.99352 m +W* +0 0 608 160 rc +-5 165 m +165 165 l +165 9 l +-5 9 l +h +f +Q +0.10588235 0.72549021 0.12156863 sc +q +241.77284 12.165432 m +230.85426 12.165432 222.63789 15.032893 217.12346 20.7679 c +211.60902 26.502909 208.85185 34.995007 208.85185 46.244446 c +208.85185 98.024689 l +189 98.024689 l +189 112.91358 l +208.85185 112.91358 l +208.85185 146 l +225.39507 146 l +225.39507 112.91358 l +261.79013 112.91358 l +261.79013 98.024689 l +225.39507 98.024689 l +225.39507 46.079014 l +225.39507 39.571983 226.99422 34.802074 230.1926 31.769136 c +233.39096 28.736198 237.58186 27.219753 242.76543 27.219753 c +245.19179 27.219753 247.89381 27.49547 250.87161 28.046913 c +253.8494 28.598356 256.88229 29.425508 259.97037 30.528395 c +263.27902 15.97037 l +259.52921 14.646907 255.86215 13.681896 252.27777 13.075309 c +248.69341 12.468721 245.19179 12.165432 241.77284 12.165432 c +241.77284 12.165432 l +h +294.70239 13.654321 m +278.15918 13.654321 l +278.15918 112.91358 l +294.70239 112.91358 l +294.70239 96.370369 l +294.70239 96.370369 304.76617 106.29628 309.67401 109.60493 c +314.58185 112.9136 320.34436 114.5679 326.96167 114.5679 c +332.80695 114.5679 337.79745 112.94117 341.93326 109.68765 c +346.06909 106.43414 349.01926 101.99509 350.78387 96.370369 c +350.78387 96.370369 361.92294 106.29628 366.99622 109.60493 c +372.06952 112.9136 378.08014 114.5679 385.02832 114.5679 c +392.74854 114.5679 398.92459 111.72801 403.55673 106.04815 c +408.18884 100.36829 410.50488 92.730911 410.50488 83.135803 c +410.50488 13.654321 l +393.96167 13.654321 l +393.96167 80.488892 l +393.96167 87.106209 392.69336 91.820976 390.15674 94.633331 c +387.62009 97.445694 383.98062 98.851852 379.23822 98.851852 c +374.49579 98.851852 369.78104 97.418121 365.09375 94.550621 c +360.40649 91.683113 356.24316 87.878212 352.60364 83.135803 c +352.60364 13.654321 l +336.06042 13.654321 l +336.06042 80.488892 l +336.06042 87.106209 334.79211 91.820976 332.25549 94.633331 c +329.71884 97.445694 326.07938 98.851852 321.33698 98.851852 c +316.59457 98.851852 311.87979 97.418121 307.19254 94.550621 c +302.50525 91.683113 298.34192 87.878212 294.70239 83.135803 c +294.70239 13.654321 l +h +438.9505 21.264198 m +433.10519 27.440361 430.18259 35.656738 430.18259 45.913582 c +430.18259 112.91358 l +446.7258 112.91358 l +446.7258 49.222221 l +446.7258 42.16375 448.3801 36.814835 451.68875 33.175308 c +454.99741 29.535784 459.85004 27.716049 466.2468 27.716049 c +471.32007 27.716049 476.36569 29.177351 481.38382 32.099998 c +486.40195 35.022648 491.39243 39.35141 496.35544 45.086418 c +496.35544 112.91358 l +512.89862 112.91358 l +512.89862 13.654321 l +496.35544 13.654321 l +496.35544 30.197531 l +496.35544 30.197531 485.13364 20.271622 479.89493 16.962963 c +474.65622 13.654305 468.78345 12 462.2764 12 c +452.57101 12 444.79578 15.088035 438.9505 21.264198 c +h +588.65784 13.654321 m +564.50476 51.041977 l +541.84052 13.654321 l +523.4776 13.654321 l +556.56403 63.449383 l +524.63562 112.91358 l +543.164 112.91358 l +566.15906 77.180244 l +589.48499 112.91358 l +607.84796 112.91358 l +574.43066 64.441978 l +607.18622 13.654321 l +588.65784 13.654321 l +h +588.65784 13.654321 m +W* +0 0 608 160 rc +184 151 m +612.84796 151 l +612.84796 7 l +184 7 l +h +f +ep +end +%%Trailer +%%EOF diff --git a/logo/tmux-logo.svg b/logo/tmux-logo.svg new file mode 100644 index 00000000..061cddd9 --- /dev/null +++ b/logo/tmux-logo.svg @@ -0,0 +1,18 @@ + + + + logomark + wordmark + Created with Sketch. + + + + + + + + + + + + + \ No newline at end of file diff --git a/logo/tmux-logomark.eps b/logo/tmux-logomark.eps new file mode 100644 index 00000000..8924983b --- /dev/null +++ b/logo/tmux-logomark.eps @@ -0,0 +1,829 @@ +%!PS-Adobe-3.0 EPSF-3.0 +%APL_DSC_Encoding: UTF8 +%APLProducer: (Version 10.10.3 (Build 14D136) Quartz PS Context) +%%Title: (Unknown) +%%Creator: (Unknown) +%%CreationDate: (Unknown) +%%For: (Unknown) +%%DocumentData: Clean7Bit +%%LanguageLevel: 2 +%%Pages: 1 +%%BoundingBox: 0 0 160 160 +%%EndComments +%%BeginProlog +%%BeginFile: cg-pdf.ps +%%Copyright: Copyright 2000-2004 Apple Computer Incorporated. +%%Copyright: All Rights Reserved. +currentpacking true setpacking +/cg_md 141 dict def +cg_md begin +/L3? languagelevel 3 ge def +/bd{bind def}bind def +/ld{load def}bd +/xs{exch store}bd +/xd{exch def}bd +/cmmtx matrix def +mark +/sc/setcolor +/scs/setcolorspace +/dr/defineresource +/fr/findresource +/T/true +/F/false +/d/setdash +/w/setlinewidth +/J/setlinecap +/j/setlinejoin +/M/setmiterlimit +/i/setflat +/rc/rectclip +/rf/rectfill +/rs/rectstroke +/f/fill +/f*/eofill +/sf/selectfont +/s/show +/xS/xshow +/yS/yshow +/xyS/xyshow +/S/stroke +/m/moveto +/l/lineto +/c/curveto +/h/closepath +/n/newpath +/q/gsave +/Q/grestore +counttomark 2 idiv +{ld}repeat pop +/SC{ + /ColorSpace fr scs +}bd +/sopr /setoverprint where{pop/setoverprint}{/pop}ifelse ld +/soprm /setoverprintmode where{pop/setoverprintmode}{/pop}ifelse ld +/cgmtx matrix def +/sdmtx{cgmtx currentmatrix pop}bd +/CM {cgmtx setmatrix}bd +/cm {cmmtx astore CM concat}bd +/W{clip newpath}bd +/W*{eoclip newpath}bd +statusdict begin product end dup (HP) anchorsearch{ + pop pop pop + true +}{ + pop + (hp) anchorsearch{ + pop pop true + }{ + pop false + }ifelse +}ifelse +{ + { + { + pop pop + (0)dup 0 4 -1 roll put + F charpath + }cshow + } +}{ + {F charpath} +}ifelse +/cply exch bd +/cps {cply stroke}bd +/pgsave 0 def +/bp{/pgsave save store}bd +/ep{pgsave restore showpage}def +/re{4 2 roll m 1 index 0 rlineto 0 exch rlineto neg 0 rlineto h}bd +/scrdict 10 dict def +/scrmtx matrix def +/patarray 0 def +/createpat{patarray 3 1 roll put}bd +/makepat{ +scrmtx astore pop +gsave +initgraphics +CM +patarray exch get +scrmtx +makepattern +grestore +setpattern +}bd +/cg_BeginEPSF{ + userdict save/cg_b4_Inc_state exch put + userdict/cg_endepsf/cg_EndEPSF load put + count userdict/cg_op_count 3 -1 roll put + countdictstack dup array dictstack userdict/cg_dict_array 3 -1 roll put + 3 sub{end}repeat + /showpage {} def + 0 setgray 0 setlinecap 1 setlinewidth 0 setlinejoin + 10 setmiterlimit [] 0 setdash newpath + false setstrokeadjust false setoverprint +}bd +/cg_EndEPSF{ + countdictstack 3 sub { end } repeat + cg_dict_array 3 1 index length 3 sub getinterval + {begin}forall + count userdict/cg_op_count get sub{pop}repeat + userdict/cg_b4_Inc_state get restore + F setpacking +}bd +/cg_biproc{currentfile/RunLengthDecode filter}bd +/cg_aiproc{currentfile/ASCII85Decode filter/RunLengthDecode filter}bd +/ImageDataSource 0 def +L3?{ + /cg_mibiproc{pop pop/ImageDataSource{cg_biproc}def}bd + /cg_miaiproc{pop pop/ImageDataSource{cg_aiproc}def}bd +}{ + /ImageBandMask 0 def + /ImageBandData 0 def + /cg_mibiproc{ + string/ImageBandMask xs + string/ImageBandData xs + /ImageDataSource{[currentfile/RunLengthDecode filter dup ImageBandMask/readstring cvx + /pop cvx dup ImageBandData/readstring cvx/pop cvx]cvx bind}bd + }bd + /cg_miaiproc{ + string/ImageBandMask xs + string/ImageBandData xs + /ImageDataSource{[currentfile/ASCII85Decode filter/RunLengthDecode filter + dup ImageBandMask/readstring cvx + /pop cvx dup ImageBandData/readstring cvx/pop cvx]cvx bind}bd + }bd +}ifelse +/imsave 0 def +/BI{save/imsave xd mark}bd +/EI{imsave restore}bd +/ID{ +counttomark 2 idiv +dup 2 add +dict begin +{def} repeat +pop +/ImageType 1 def +/ImageMatrix[Width 0 0 Height neg 0 Height]def +currentdict dup/ImageMask known{ImageMask}{F}ifelse exch +L3?{ + dup/MaskedImage known + { + pop + << + /ImageType 3 + /InterleaveType 2 + /DataDict currentdict + /MaskDict + << /ImageType 1 + /Width Width + /Height Height + /ImageMatrix ImageMatrix + /BitsPerComponent 1 + /Decode [0 1] + currentdict/Interpolate known + {/Interpolate Interpolate}if + >> + >> + }if +}if +exch +{imagemask}{image}ifelse +end +}bd +/cguidfix{statusdict begin mark version end +{cvr}stopped{cleartomark 0}{exch pop}ifelse +2012 lt{dup findfont dup length dict begin +{1 index/FID ne 2 index/UniqueID ne and +{def} {pop pop} ifelse}forall +currentdict end definefont pop +}{pop}ifelse +}bd +/t_array 0 def +/t_i 0 def +/t_c 1 string def +/x_proc{ + exch t_array t_i get add exch moveto + /t_i t_i 1 add store +}bd +/y_proc{ + t_array t_i get add moveto + /t_i t_i 1 add store +}bd +/xy_proc{ + + t_array t_i 2 copy 1 add get 3 1 roll get + 4 -1 roll add 3 1 roll add moveto + /t_i t_i 2 add store +}bd +/sop 0 def +/cp_proc/x_proc ld +/base_charpath +{ + /t_array xs + /t_i 0 def + { + t_c 0 3 -1 roll put + currentpoint + t_c cply sop + cp_proc + }forall + /t_array 0 def +}bd +/sop/stroke ld +/nop{}def +/xsp/base_charpath ld +/ysp{/cp_proc/y_proc ld base_charpath/cp_proc/x_proc ld}bd +/xysp{/cp_proc/xy_proc ld base_charpath/cp_proc/x_proc ld}bd +/xmp{/sop/nop ld /cp_proc/x_proc ld base_charpath/sop/stroke ld}bd +/ymp{/sop/nop ld /cp_proc/y_proc ld base_charpath/sop/stroke ld}bd +/xymp{/sop/nop ld /cp_proc/xy_proc ld base_charpath/sop/stroke ld}bd +/refnt{ +findfont dup length dict copy dup +/Encoding 4 -1 roll put +definefont pop +}bd +/renmfont{ +findfont dup length dict copy definefont pop +}bd +L3? dup dup{save exch}if +/Range 0 def +/DataSource 0 def +/val 0 def +/nRange 0 def +/mulRange 0 def +/d0 0 def +/r0 0 def +/di 0 def +/ri 0 def +/a0 0 def +/a1 0 def +/r1 0 def +/r2 0 def +/dx 0 def +/Nsteps 0 def +/sh3tp 0 def +/ymax 0 def +/ymin 0 def +/xmax 0 def +/xmin 0 def +/setupFunEval +{ + begin + /nRange Range length 2 idiv store + /mulRange + + [ + 0 1 nRange 1 sub + { + 2 mul/nDim2 xd + Range nDim2 get + Range nDim2 1 add get + 1 index sub + + 255 div + exch + }for + ]store + end +}bd +/FunEval +{ + begin + + nRange mul /val xd + + 0 1 nRange 1 sub + { + dup 2 mul/nDim2 xd + val + add DataSource exch get + mulRange nDim2 get mul + mulRange nDim2 1 add get + add + }for + end +}bd +/max +{ + 2 copy lt + {exch pop}{pop}ifelse +}bd +/sh2 +{ + /Coords load aload pop + 3 index 3 index translate + + 3 -1 roll sub + 3 1 roll exch + sub + 2 copy + dup mul exch dup mul add sqrt + dup + scale + atan + + rotate + + /Function load setupFunEval + + + clippath {pathbbox}stopped {0 0 0 0}if newpath + /ymax xs + /xmax xs + /ymin xs + /xmin xs + currentdict/Extend known + { + /Extend load 0 get + { + 0/Function load FunEval sc + xmin ymin xmin abs ymax ymin sub rectfill + }if + }if + + /Nsteps/Function load/Size get 0 get 1 sub store + /dx 1 Nsteps div store + gsave + /di ymax ymin sub store + /Function load + + 0 1 Nsteps + { + 1 index FunEval sc + 0 ymin dx di rectfill + dx 0 translate + }for + pop + grestore + currentdict/Extend known + { + /Extend load 1 get + { + Nsteps/Function load FunEval sc + 1 ymin xmax 1 sub abs ymax ymin sub rectfill + }if + }if +}bd +/shp +{ + 4 copy + + dup 0 gt{ + 0 exch a1 a0 arc + }{ + pop 0 moveto + }ifelse + dup 0 gt{ + 0 exch a0 a1 arcn + }{ + pop 0 lineto + }ifelse + + fill + + dup 0 gt{ + 0 exch a0 a1 arc + }{ + pop 0 moveto + }ifelse + dup 0 gt{ + 0 exch a1 a0 arcn + }{ + pop 0 lineto + }ifelse + + fill +}bd +/calcmaxs +{ + + xmin dup mul ymin dup mul add sqrt + xmax dup mul ymin dup mul add sqrt + xmin dup mul ymax dup mul add sqrt + xmax dup mul ymax dup mul add sqrt + max max max +}bd +/sh3 +{ + /Coords load aload pop + 5 index 5 index translate + 3 -1 roll 6 -1 roll sub + 3 -1 roll 5 -1 roll sub + 2 copy dup mul exch dup mul add sqrt + /dx xs + 2 copy 0 ne exch 0 ne or + { + + exch atan rotate + }{ + pop pop + }ifelse + + /r2 xs + /r1 xs + /Function load + dup/Size get 0 get 1 sub + /Nsteps xs + setupFunEval + + + + + + dx r2 add r1 lt{ + + 0 + }{ + dx r1 add r2 le + { + 1 + }{ + r1 r2 eq + { + 2 + }{ + 3 + }ifelse + }ifelse + }ifelse + /sh3tp xs + clippath {pathbbox}stopped {0 0 0 0}if + newpath + /ymax xs + /xmax xs + /ymin xs + /xmin xs + + dx dup mul r2 r1 sub dup mul sub dup 0 gt + { + sqrt r2 r1 sub atan + /a0 exch 180 exch sub store + /a1 a0 neg store + }{ + pop + /a0 0 store + /a1 360 store + }ifelse + currentdict/Extend known + { + /Extend load 0 get r1 0 gt and + { + 0/Function load FunEval sc + + + + + { + { + dx 0 r1 360 0 arcn + xmin ymin moveto + xmax ymin lineto + xmax ymax lineto + xmin ymax lineto + xmin ymin lineto + eofill + } + { + r1 0 gt{0 0 r1 0 360 arc fill}if + } + { + + + + + 0 r1 xmin abs r1 add neg r1 shp + } + { + + + r2 r1 gt{ + + 0 r1 + r1 neg r2 r1 sub div dx mul + 0 + shp + }{ + + + + 0 r1 calcmaxs + dup + + r2 add dx mul dx r1 r2 sub sub div + neg + exch 1 index + abs exch sub + shp + }ifelse + } + }sh3tp get exec + }if + }if + + /d0 0 store + /r0 r1 store + /di dx Nsteps div store + /ri r2 r1 sub Nsteps div store + /Function load + 0 1 Nsteps + { + 1 index FunEval sc + d0 di add r0 ri add d0 r0 shp + { + + d0 0 r0 a1 a0 arc + d0 di add 0 r0 ri add a0 a1 arcn + fill + + + d0 0 r0 a0 a1 arc + d0 di add 0 r0 ri add a1 a0 arcn + fill + }pop + + + /d0 d0 di add store + /r0 r0 ri add store + }for + pop + + currentdict/Extend known + { + /Extend load 1 get r2 0 gt and + { + Nsteps/Function load FunEval sc + + + + + { + { + dx 0 r2 0 360 arc fill + } + { + dx 0 r2 360 0 arcn + xmin ymin moveto + xmax ymin lineto + xmax ymax lineto + xmin ymax lineto + xmin ymin lineto + eofill + } + { + + + xmax abs r1 add r1 dx r1 shp + } + { + + r2 r1 gt{ + + + + calcmaxs dup + + r1 add dx mul dx r2 r1 sub sub div + exch 1 index + exch sub + dx r2 + shp + }{ + + r1 neg r2 r1 sub div dx mul + 0 + dx + r2 + shp + }ifelse + } + } + sh3tp get exec + }if + }if +}bd +/sh +{ + begin + /ShadingType load dup dup 2 eq exch 3 eq or + { + gsave + newpath + /ColorSpace load scs + currentdict/BBox known + { + /BBox load aload pop + 2 index sub + 3 index + 3 -1 roll exch sub + exch rectclip + }if + 2 eq + {sh2}{sh3}ifelse + grestore + }{ + + pop + (DEBUG: shading type unimplemented\n)print flush + }ifelse + end +}bd +{restore}if not dup{save exch}if + L3?{ + /sh/shfill ld + /csq/clipsave ld + /csQ/cliprestore ld + }if +{restore}if +end +setpacking +%%EndFile +%%EndProlog +%%BeginSetup +%%EndSetup +%%Page: 1 1 +%%PageBoundingBox: 0 0 160 160 +%%BeginPageSetup +cg_md begin +bp +sdmtx +[ /CIEBasedABC 4 dict dup begin +/WhitePoint [ 0.9505 1.0000 1.0891 ] def +/DecodeABC [ +{ 1.0 0.0 3 -1 roll 1 index 1 index le { exch pop} { pop } ifelse + 1 index 1 index ge { exch pop } { pop } ifelse < +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000001010101010101010101010101 +0101010101010101010101010101010101010101010101020202020202020202 +0202020202020202020202020202020202030303030303030303030303030303 +0303030303030304040404040404040404040404040404040404050505050505 +0505050505050505050506060606060606060606060606060607070707070707 +0707070707070708080808080808080808080808090909090909090909090909 +0a0a0a0a0a0a0a0a0a0a0a0b0b0b0b0b0b0b0b0b0b0b0c0c0c0c0c0c0c0c0c0c +0d0d0d0d0d0d0d0d0d0d0e0e0e0e0e0e0e0e0e0f0f0f0f0f0f0f0f0f10101010 +1010101010111111111111111112121212121212121313131313131313141414 +1414141414151515151515151616161616161616171717171717171818181818 +18181919191919191a1a1a1a1a1a1a1b1b1b1b1b1b1c1c1c1c1c1c1c1d1d1d1d +1d1d1e1e1e1e1e1e1f1f1f1f1f1f202020202020212121212121222222222223 +2323232323242424242425252525252526262626262727272727282828282829 +292929292a2a2a2a2a2b2b2b2b2b2c2c2c2c2c2d2d2d2d2d2e2e2e2e2e2f2f2f +2f2f303030303131313131323232323333333333343434343535353535363636 +36373737373838383839393939393a3a3a3a3b3b3b3b3c3c3c3c3d3d3d3d3e3e +3e3e3f3f3f3f4040404041414141424242424343434444444445454545464646 +4647474748484848494949494a4a4a4b4b4b4b4c4c4c4d4d4d4d4e4e4e4f4f4f +4f50505051515151525252535353535454545555555656565657575758585859 +59595a5a5a5a5b5b5b5c5c5c5d5d5d5e5e5e5f5f5f6060606061616162626263 +63636464646565656666666767676868686969696a6a6a6b6b6b6c6c6d6d6d6e +6e6e6f6f6f707070717171727273737374747475757576767677777878787979 +797a7a7b7b7b7c7c7c7d7d7e7e7e7f7f7f808081818182828283838484848585 +86868687878888888989898a8a8b8b8b8c8c8d8d8d8e8e8f8f90909091919292 +9293939494949595969697979798989999999a9a9b9b9c9c9c9d9d9e9e9f9f9f +a0a0a1a1a2a2a3a3a3a4a4a5a5a6a6a6a7a7a8a8a9a9aaaaabababacacadadae +aeafafb0b0b0b1b1b2b2b3b3b4b4b5b5b6b6b6b7b7b8b8b9b9bababbbbbcbcbd +bdbebebebfbfc0c0c1c1c2c2c3c3c4c4c5c5c6c6c7c7c8c8c9c9cacacbcbcccc +cdcdcececfcfd0d0d1d1d2d2d3d3d4d4d5d5d6d6d7d7d8d8d9d9dadadbdcdcdd +dddededfdfe0e0e1e1e2e2e3e3e4e4e5e6e6e7e7e8e8e9e9eaeaebebecededee +eeefeff0f0f1f1f2f3f3f4f4f5f5f6f6f7f8f8f9f9fafafbfcfcfdfdfefeffff +> dup length 1 sub 3 -1 roll mul dup dup floor cvi exch ceiling + cvi 3 index exch get 4 -1 roll 3 -1 roll get + dup 3 1 roll sub 3 -1 roll dup floor cvi sub mul add 255 div } bind + +{ 1.0 0.0 3 -1 roll 1 index 1 index le { exch pop} { pop } ifelse + 1 index 1 index ge { exch pop } { pop } ifelse < +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000001010101010101010101010101 +0101010101010101010101010101010101010101010101020202020202020202 +0202020202020202020202020202020202030303030303030303030303030303 +0303030303030304040404040404040404040404040404040404050505050505 +0505050505050505050506060606060606060606060606060607070707070707 +0707070707070708080808080808080808080808090909090909090909090909 +0a0a0a0a0a0a0a0a0a0a0a0b0b0b0b0b0b0b0b0b0b0b0c0c0c0c0c0c0c0c0c0c +0d0d0d0d0d0d0d0d0d0d0e0e0e0e0e0e0e0e0e0f0f0f0f0f0f0f0f0f10101010 +1010101010111111111111111112121212121212121313131313131313141414 +1414141414151515151515151616161616161616171717171717171818181818 +18181919191919191a1a1a1a1a1a1a1b1b1b1b1b1b1c1c1c1c1c1c1c1d1d1d1d +1d1d1e1e1e1e1e1e1f1f1f1f1f1f202020202020212121212121222222222223 +2323232323242424242425252525252526262626262727272727282828282829 +292929292a2a2a2a2a2b2b2b2b2b2c2c2c2c2c2d2d2d2d2d2e2e2e2e2e2f2f2f +2f2f303030303131313131323232323333333333343434343535353535363636 +36373737373838383839393939393a3a3a3a3b3b3b3b3c3c3c3c3d3d3d3d3e3e +3e3e3f3f3f3f4040404041414141424242424343434444444445454545464646 +4647474748484848494949494a4a4a4b4b4b4b4c4c4c4d4d4d4d4e4e4e4f4f4f +4f50505051515151525252535353535454545555555656565657575758585859 +59595a5a5a5a5b5b5b5c5c5c5d5d5d5e5e5e5f5f5f6060606061616162626263 +63636464646565656666666767676868686969696a6a6a6b6b6b6c6c6d6d6d6e +6e6e6f6f6f707070717171727273737374747475757576767677777878787979 +797a7a7b7b7b7c7c7c7d7d7e7e7e7f7f7f808081818182828283838484848585 +86868687878888888989898a8a8b8b8b8c8c8d8d8d8e8e8f8f90909091919292 +9293939494949595969697979798989999999a9a9b9b9c9c9c9d9d9e9e9f9f9f +a0a0a1a1a2a2a3a3a3a4a4a5a5a6a6a6a7a7a8a8a9a9aaaaabababacacadadae +aeafafb0b0b0b1b1b2b2b3b3b4b4b5b5b6b6b6b7b7b8b8b9b9bababbbbbcbcbd +bdbebebebfbfc0c0c1c1c2c2c3c3c4c4c5c5c6c6c7c7c8c8c9c9cacacbcbcccc +cdcdcececfcfd0d0d1d1d2d2d3d3d4d4d5d5d6d6d7d7d8d8d9d9dadadbdcdcdd +dddededfdfe0e0e1e1e2e2e3e3e4e4e5e6e6e7e7e8e8e9e9eaeaebebecededee +eeefeff0f0f1f1f2f3f3f4f4f5f5f6f6f7f8f8f9f9fafafbfcfcfdfdfefeffff +> dup length 1 sub 3 -1 roll mul dup dup floor cvi exch ceiling + cvi 3 index exch get 4 -1 roll 3 -1 roll get + dup 3 1 roll sub 3 -1 roll dup floor cvi sub mul add 255 div } bind + +{ 1.0 0.0 3 -1 roll 1 index 1 index le { exch pop} { pop } ifelse + 1 index 1 index ge { exch pop } { pop } ifelse < +0000000000000000000000000000000000000000000000000000000000000000 +0000000000000000000000000000000000000001010101010101010101010101 +0101010101010101010101010101010101010101010101020202020202020202 +0202020202020202020202020202020202030303030303030303030303030303 +0303030303030304040404040404040404040404040404040404050505050505 +0505050505050505050506060606060606060606060606060607070707070707 +0707070707070708080808080808080808080808090909090909090909090909 +0a0a0a0a0a0a0a0a0a0a0a0b0b0b0b0b0b0b0b0b0b0b0c0c0c0c0c0c0c0c0c0c +0d0d0d0d0d0d0d0d0d0d0e0e0e0e0e0e0e0e0e0f0f0f0f0f0f0f0f0f10101010 +1010101010111111111111111112121212121212121313131313131313141414 +1414141414151515151515151616161616161616171717171717171818181818 +18181919191919191a1a1a1a1a1a1a1b1b1b1b1b1b1c1c1c1c1c1c1c1d1d1d1d +1d1d1e1e1e1e1e1e1f1f1f1f1f1f202020202020212121212121222222222223 +2323232323242424242425252525252526262626262727272727282828282829 +292929292a2a2a2a2a2b2b2b2b2b2c2c2c2c2c2d2d2d2d2d2e2e2e2e2e2f2f2f +2f2f303030303131313131323232323333333333343434343535353535363636 +36373737373838383839393939393a3a3a3a3b3b3b3b3c3c3c3c3d3d3d3d3e3e +3e3e3f3f3f3f4040404041414141424242424343434444444445454545464646 +4647474748484848494949494a4a4a4b4b4b4b4c4c4c4d4d4d4d4e4e4e4f4f4f +4f50505051515151525252535353535454545555555656565657575758585859 +59595a5a5a5a5b5b5b5c5c5c5d5d5d5e5e5e5f5f5f6060606061616162626263 +63636464646565656666666767676868686969696a6a6a6b6b6b6c6c6d6d6d6e +6e6e6f6f6f707070717171727273737374747475757576767677777878787979 +797a7a7b7b7b7c7c7c7d7d7e7e7e7f7f7f808081818182828283838484848585 +86868687878888888989898a8a8b8b8b8c8c8d8d8d8e8e8f8f90909091919292 +9293939494949595969697979798989999999a9a9b9b9c9c9c9d9d9e9e9f9f9f +a0a0a1a1a2a2a3a3a3a4a4a5a5a6a6a6a7a7a8a8a9a9aaaaabababacacadadae +aeafafb0b0b0b1b1b2b2b3b3b4b4b5b5b6b6b6b7b7b8b8b9b9bababbbbbcbcbd +bdbebebebfbfc0c0c1c1c2c2c3c3c4c4c5c5c6c6c7c7c8c8c9c9cacacbcbcccc +cdcdcececfcfd0d0d1d1d2d2d3d3d4d4d5d5d6d6d7d7d8d8d9d9dadadbdcdcdd +dddededfdfe0e0e1e1e2e2e3e3e4e4e5e6e6e7e7e8e8e9e9eaeaebebecededee +eeefeff0f0f1f1f2f3f3f4f4f5f5f6f6f7f8f8f9f9fafafbfcfcfdfdfefeffff +> dup length 1 sub 3 -1 roll mul dup dup floor cvi exch ceiling + cvi 3 index exch get 4 -1 roll 3 -1 roll get + dup 3 1 roll sub 3 -1 roll dup floor cvi sub mul add 255 div } bind +] def +/MatrixABC [ 0.4124 0.2126 0.0193 0.3576 0.7151 0.1192 0.1805 0.0722 0.9508 ] def +/RangeLMN [ 0.0 0.9505 0.0 1.0000 0.0 1.0891 ] def +end ] /Cs1 exch/ColorSpace dr pop +%%EndPageSetup +0.60000002 i +/Cs1 SC +0.10588235 0.72549021 0.12156863 sc +q +0 44 m +160 44 l +160 15.003872 l +160 6.7174625 153.27803 0 145.00154 0 c +14.998466 0 l +6.7150416 0 0 6.7065849 0 15.003872 c +0 44 l +h +0 44 m +160 44 l +160 14 l +0 14 l +0 44 l +h +0 44 m +W* +0 0 160 160 rc +-5 49 m +165 49 l +165 -5 l +-5 -5 l +h +f +Q +0.23529412 0.23529412 0.23529412 sc +q +83 90 m +83 160 l +77 160 l +77 14 l +83 14 l +83 84 l +160 84 l +160 90 l +83 90 l +h +0 144.99352 m +0 153.28137 6.7219648 160 14.998466 160 c +145.00154 160 l +153.28496 160 160 153.27509 160 144.99352 c +160 14 l +0 14 l +0 144.99352 l +h +0 144.99352 m +W* +0 0 160 160 rc +-5 165 m +165 165 l +165 9 l +-5 9 l +h +f +ep +end +%%Trailer +%%EOF diff --git a/logo/tmux-logomark.svg b/logo/tmux-logomark.svg new file mode 100644 index 00000000..c543709d --- /dev/null +++ b/logo/tmux-logomark.svg @@ -0,0 +1,15 @@ + + + + logomark copy + Created with Sketch. + + + + + + + + + + \ No newline at end of file diff --git a/mode-key.c b/mode-key.c index 72d66f37..aed161bb 100644 --- a/mode-key.c +++ b/mode-key.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -38,6 +38,20 @@ * (any matching MODEKEYEDIT_SWITCHMODE*) are special-cased to do this. */ +/* Entry in the default mode key tables. */ +struct mode_key_entry { + key_code key; + + /* + * Editing mode for vi: 0 is edit mode, keys not in the table are + * returned as MODEKEY_OTHER; 1 is command mode, keys not in the table + * are returned as MODEKEY_NONE. This is also matched on, allowing some + * keys to be bound in edit mode. + */ + int mode; + enum mode_key_cmd cmd; +}; + /* Edit keys command strings. */ const struct mode_key_cmdstr mode_key_cmdstr_edit[] = { { MODEKEYEDIT_BACKSPACE, "backspace" }, @@ -251,6 +265,10 @@ const struct mode_key_entry mode_key_vi_choice[] = { { KEYC_RIGHT, 0, MODEKEYCHOICE_TREE_EXPAND }, { KEYC_LEFT | KEYC_CTRL, 0, MODEKEYCHOICE_TREE_COLLAPSE_ALL }, { KEYC_RIGHT | KEYC_CTRL, 0, MODEKEYCHOICE_TREE_EXPAND_ALL }, + { KEYC_MOUSEDOWN1_PANE, 0, MODEKEYCHOICE_CHOOSE }, + { KEYC_MOUSEDOWN3_PANE, 0, MODEKEYCHOICE_TREE_TOGGLE }, + { KEYC_WHEELUP_PANE, 0, MODEKEYCHOICE_UP }, + { KEYC_WHEELDOWN_PANE, 0, MODEKEYCHOICE_DOWN }, { 0, -1, 0 } }; @@ -326,6 +344,10 @@ const struct mode_key_entry mode_key_vi_copy[] = { { KEYC_RIGHT, 0, MODEKEYCOPY_RIGHT }, { KEYC_UP | KEYC_CTRL, 0, MODEKEYCOPY_SCROLLUP }, { KEYC_UP, 0, MODEKEYCOPY_UP }, + { KEYC_WHEELUP_PANE, 0, MODEKEYCOPY_SCROLLUP }, + { KEYC_WHEELDOWN_PANE, 0, MODEKEYCOPY_SCROLLDOWN }, + { KEYC_MOUSEDRAG1_PANE, 0, MODEKEYCOPY_STARTSELECTION }, + { KEYC_MOUSEDRAGEND1_PANE, 0, MODEKEYCOPY_COPYSELECTION }, { 0, -1, 0 } }; @@ -405,6 +427,10 @@ const struct mode_key_entry mode_key_emacs_choice[] = { { KEYC_RIGHT, 0, MODEKEYCHOICE_TREE_EXPAND }, { KEYC_LEFT | KEYC_CTRL, 0, MODEKEYCHOICE_TREE_COLLAPSE_ALL }, { KEYC_RIGHT | KEYC_CTRL, 0, MODEKEYCHOICE_TREE_EXPAND_ALL }, + { KEYC_MOUSEDOWN1_PANE, 0, MODEKEYCHOICE_CHOOSE }, + { KEYC_MOUSEDOWN3_PANE, 0, MODEKEYCHOICE_TREE_TOGGLE }, + { KEYC_WHEELUP_PANE, 0, MODEKEYCHOICE_UP }, + { KEYC_WHEELDOWN_PANE, 0, MODEKEYCHOICE_DOWN }, { 0, -1, 0 } }; @@ -467,6 +493,10 @@ const struct mode_key_entry mode_key_emacs_copy[] = { { KEYC_UP | KEYC_CTRL, 0, MODEKEYCOPY_SCROLLUP }, { KEYC_UP | KEYC_ESCAPE, 0, MODEKEYCOPY_HALFPAGEUP }, { KEYC_UP, 0, MODEKEYCOPY_UP }, + { KEYC_WHEELUP_PANE, 0, MODEKEYCOPY_SCROLLUP }, + { KEYC_WHEELDOWN_PANE, 0, MODEKEYCOPY_SCROLLDOWN }, + { KEYC_MOUSEDRAG1_PANE, 0, MODEKEYCOPY_STARTSELECTION }, + { KEYC_MOUSEDRAGEND1_PANE, 0, MODEKEYCOPY_COPYSELECTION }, { 0, -1, 0 } }; @@ -495,9 +525,15 @@ RB_GENERATE(mode_key_tree, mode_key_binding, entry, mode_key_cmp); int mode_key_cmp(struct mode_key_binding *mbind1, struct mode_key_binding *mbind2) { - if (mbind1->mode != mbind2->mode) - return (mbind1->mode - mbind2->mode); - return (mbind1->key - mbind2->key); + if (mbind1->mode < mbind2->mode) + return (-1); + if (mbind1->mode > mbind2->mode) + return (1); + if (mbind1->key < mbind2->key) + return (-1); + if (mbind1->key > mbind2->key) + return (1); + return (0); } const char * @@ -560,7 +596,7 @@ mode_key_init(struct mode_key_data *mdata, struct mode_key_tree *mtree) } enum mode_key_cmd -mode_key_lookup(struct mode_key_data *mdata, int key, const char **arg) +mode_key_lookup(struct mode_key_data *mdata, key_code key, const char **arg) { struct mode_key_binding *mbind, mtmp; diff --git a/names.c b/names.c index 1ceb83c0..a03f6f5b 100644 --- a/names.c +++ b/names.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -25,43 +25,76 @@ #include "tmux.h" -void window_name_callback(unused int, unused short, void *); +void name_time_callback(int, short, void *); +int name_time_expired(struct window *, struct timeval *); void -queue_window_name(struct window *w) +name_time_callback(__unused int fd, __unused short events, void *arg) { - struct timeval tv; + struct window *w = arg; - tv.tv_sec = 0; - tv.tv_usec = NAME_INTERVAL * 1000L; + /* The event loop will call check_window_name for us on the way out. */ + log_debug("@%u name timer expired", w->id); +} - if (event_initialized(&w->name_timer)) - evtimer_del(&w->name_timer); - evtimer_set(&w->name_timer, window_name_callback, w); - evtimer_add(&w->name_timer, &tv); +int +name_time_expired(struct window *w, struct timeval *tv) +{ + struct timeval offset; + + timersub(tv, &w->name_time, &offset); + if (offset.tv_sec != 0 || offset.tv_usec > NAME_INTERVAL) + return (0); + return (NAME_INTERVAL - offset.tv_usec); } void -window_name_callback(unused int fd, unused short events, void *data) +check_window_name(struct window *w) { - struct window *w = data; + struct timeval tv, next; char *name; + int left; if (w->active == NULL) return; - if (!options_get_number(&w->options, "automatic-rename")) { - if (event_initialized(&w->name_timer)) - event_del(&w->name_timer); + if (!options_get_number(w->options, "automatic-rename")) + return; + + if (~w->active->flags & PANE_CHANGED) { + log_debug("@%u active pane not changed", w->id); return; } - queue_window_name(w); + log_debug("@%u active pane changed", w->id); + + gettimeofday(&tv, NULL); + left = name_time_expired(w, &tv); + if (left != 0) { + if (!event_initialized(&w->name_event)) + evtimer_set(&w->name_event, name_time_callback, w); + if (!evtimer_pending(&w->name_event, NULL)) { + log_debug("@%u name timer queued (%d left)", w->id, left); + timerclear(&next); + next.tv_usec = left; + event_add(&w->name_event, &next); + } else + log_debug("@%u name timer already queued (%d left)", w->id, left); + return; + } + memcpy(&w->name_time, &tv, sizeof w->name_time); + if (event_initialized(&w->name_event)) + evtimer_del(&w->name_event); + + w->active->flags &= ~PANE_CHANGED; name = format_window_name(w); if (strcmp(name, w->name) != 0) { + log_debug("@%u new name %s (was %s)", w->id, name, w->name); window_set_name(w, name); server_status_window(w); - } + } else + log_debug("@%u name not changed (still %s)", w->id, w->name); + free(name); } @@ -85,11 +118,11 @@ format_window_name(struct window *w) struct format_tree *ft; char *fmt, *name; - ft = format_create(); + ft = format_create(NULL, 0); format_defaults_window(ft, w); format_defaults_pane(ft, w->active); - fmt = options_get_string(&w->options, "automatic-rename-format"); + fmt = options_get_string(w->options, "automatic-rename-format"); name = format_expand(ft, fmt); format_free(ft); diff --git a/notify.c b/notify.c index 47945aec..696a62bf 100644 --- a/notify.c +++ b/notify.c @@ -120,9 +120,9 @@ notify_drain(void) } if (ne->client != NULL) - ne->client->references--; + server_client_unref(ne->client); if (ne->session != NULL) - ne->session->references--; + session_unref(ne->session); if (ne->window != NULL) window_remove_ref(ne->window); @@ -135,7 +135,6 @@ void notify_input(struct window_pane *wp, struct evbuffer *input) { struct client *c; - u_int i; /* * notify_input() is not queued and only does anything when @@ -144,9 +143,8 @@ notify_input(struct window_pane *wp, struct evbuffer *input) if (!notify_enabled) return; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c != NULL && (c->flags & CLIENT_CONTROL)) + TAILQ_FOREACH(c, &clients, entry) { + if (c->flags & CLIENT_CONTROL) control_notify_input(c, wp, input); } } diff --git a/options-table.c b/options-table.c index 2bcf29b4..e3362ba8 100644 --- a/options-table.c +++ b/options-table.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2011 Nicholas Marriott + * Copyright (c) 2011 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -35,9 +35,6 @@ const char *options_table_mode_keys_list[] = { "emacs", "vi", NULL }; -const char *options_table_mode_mouse_list[] = { - "off", "on", "copy-mode", NULL -}; const char *options_table_clock_mode_style_list[] = { "12", "24", NULL }; @@ -51,20 +48,32 @@ const char *options_table_status_position_list[] = { "top", "bottom", NULL }; const char *options_table_bell_action_list[] = { - "none", "any", "current", NULL + "none", "any", "current", "other", NULL }; /* Server options. */ -const struct options_table_entry server_options_table[] = { +const struct options_table_entry options_table[] = { { .name = "buffer-limit", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_SERVER, .minimum = 1, .maximum = INT_MAX, .default_num = 20 }, + { .name = "default-terminal", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SERVER, +#ifdef TMATE + .default_str = "screen-256color" +#else + .default_str = "screen" +#endif + }, + { .name = "escape-time", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_SERVER, .minimum = 0, .maximum = INT_MAX, .default_num = 500 @@ -72,46 +81,57 @@ const struct options_table_entry server_options_table[] = { { .name = "exit-unattached", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_SERVER, .default_num = 0 }, { .name = "focus-events", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_SERVER, .default_num = 0 }, + { .name = "history-file", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SERVER, + .default_str = "" + }, + { .name = "message-limit", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_SERVER, .minimum = 0, .maximum = INT_MAX, +#ifdef TMATE + .default_num = 500 +#else .default_num = 100 +#endif }, { .name = "quiet", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_SERVER, .default_num = 0 }, { .name = "set-clipboard", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_SERVER, .default_num = 1 }, { .name = "terminal-overrides", .type = OPTIONS_TABLE_STRING, - .default_str = "*256col*:colors=256" - ",xterm*:XT:Ms=\\E]52;%p1%s;%p2%s\\007" + .scope = OPTIONS_TABLE_SERVER, + .default_str = "xterm*:XT:Ms=\\E]52;%p1%s;%p2%s\\007" ":Cs=\\E]12;%p1%s\\007:Cr=\\E]112\\007" ":Ss=\\E[%p1%d q:Se=\\E[2 q,screen*:XT" }, - { .name = NULL } -}; - -/* Session options. */ -const struct options_table_entry session_options_table[] = { { .name = "assume-paste-time", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_SESSION, .minimum = 0, .maximum = INT_MAX, .default_num = 1, @@ -119,6 +139,7 @@ const struct options_table_entry session_options_table[] = { { .name = "base-index", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_SESSION, .minimum = 0, .maximum = INT_MAX, .default_num = 0 @@ -126,52 +147,56 @@ const struct options_table_entry session_options_table[] = { { .name = "bell-action", .type = OPTIONS_TABLE_CHOICE, + .scope = OPTIONS_TABLE_SESSION, .choices = options_table_bell_action_list, .default_num = BELL_ANY }, { .name = "bell-on-alert", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0 }, { .name = "default-command", .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SESSION, .default_str = "" }, { .name = "default-shell", .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SESSION, .default_str = _PATH_BSHELL }, - { .name = "default-terminal", - .type = OPTIONS_TABLE_STRING, - .default_str = "screen" - }, - { .name = "destroy-unattached", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0 }, { .name = "detach-on-destroy", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_SESSION, .default_num = 1 }, { .name = "display-panes-active-colour", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_SESSION, .default_num = 1 }, { .name = "display-panes-colour", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_SESSION, .default_num = 4 }, { .name = "display-panes-time", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_SESSION, .minimum = 1, .maximum = INT_MAX, .default_num = 1000 @@ -179,20 +204,29 @@ const struct options_table_entry session_options_table[] = { { .name = "display-time", .type = OPTIONS_TABLE_NUMBER, - .minimum = 1, + .scope = OPTIONS_TABLE_SESSION, + .minimum = 0, .maximum = INT_MAX, .default_num = 750 }, { .name = "history-limit", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_SESSION, .minimum = 0, .maximum = INT_MAX, .default_num = 2000 }, + { .name = "key-table", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SESSION, + .default_str = "root" + }, + { .name = "lock-after-time", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_SESSION, .minimum = 0, .maximum = INT_MAX, .default_num = 0 @@ -200,97 +234,91 @@ const struct options_table_entry session_options_table[] = { { .name = "lock-command", .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SESSION, .default_str = "lock -np" }, - { .name = "lock-server", - .type = OPTIONS_TABLE_FLAG, - .default_num = 1 - }, - { .name = "message-attr", .type = OPTIONS_TABLE_ATTRIBUTES, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0, .style = "message-style" }, { .name = "message-bg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_SESSION, .default_num = 3, .style = "message-style" }, { .name = "message-command-attr", .type = OPTIONS_TABLE_ATTRIBUTES, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0, .style = "message-command-style" }, { .name = "message-command-bg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0, .style = "message-command-style" }, { .name = "message-command-fg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_SESSION, .default_num = 3, .style = "message-command-style" }, { .name = "message-command-style", .type = OPTIONS_TABLE_STYLE, + .scope = OPTIONS_TABLE_SESSION, .default_str = "bg=black,fg=yellow" }, { .name = "message-fg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0, .style = "message-style" }, { .name = "message-style", .type = OPTIONS_TABLE_STYLE, + .scope = OPTIONS_TABLE_SESSION, .default_str = "bg=yellow,fg=black" }, - { .name = "mouse-resize-pane", - .type = OPTIONS_TABLE_FLAG, - .default_num = 0 - }, - - { .name = "mouse-select-pane", - .type = OPTIONS_TABLE_FLAG, - .default_num = 0 - }, - - { .name = "mouse-select-window", - .type = OPTIONS_TABLE_FLAG, - .default_num = 0 - }, - - { .name = "mouse-utf8", + { .name = "mouse", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0 }, { .name = "prefix", .type = OPTIONS_TABLE_KEY, + .scope = OPTIONS_TABLE_SESSION, .default_num = '\002', }, { .name = "prefix2", .type = OPTIONS_TABLE_KEY, + .scope = OPTIONS_TABLE_SESSION, .default_num = KEYC_NONE, }, { .name = "renumber-windows", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0 }, { .name = "repeat-time", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_SESSION, .minimum = 0, .maximum = SHRT_MAX, .default_num = 500 @@ -298,44 +326,52 @@ const struct options_table_entry session_options_table[] = { { .name = "set-remain-on-exit", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0 }, { .name = "set-titles", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0 }, { .name = "set-titles-string", .type = OPTIONS_TABLE_STRING, - .default_str = "#S:#I:#W - \"#T\"" + .scope = OPTIONS_TABLE_SESSION, + .default_str = "#S:#I:#W - \"#T\" #{session_alerts}" }, { .name = "status", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_SESSION, .default_num = 1 }, { .name = "status-attr", .type = OPTIONS_TABLE_ATTRIBUTES, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0, .style = "status-style" }, { .name = "status-bg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_SESSION, .default_num = 2, .style = "status-style" }, { .name = "status-fg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0, .style = "status-style" }, { .name = "status-interval", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_SESSION, .minimum = 0, .maximum = INT_MAX, .default_num = 15 @@ -343,41 +379,48 @@ const struct options_table_entry session_options_table[] = { { .name = "status-justify", .type = OPTIONS_TABLE_CHOICE, + .scope = OPTIONS_TABLE_SESSION, .choices = options_table_status_justify_list, .default_num = 0 }, { .name = "status-keys", .type = OPTIONS_TABLE_CHOICE, + .scope = OPTIONS_TABLE_SESSION, .choices = options_table_status_keys_list, .default_num = MODEKEY_EMACS }, { .name = "status-left", .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SESSION, .default_str = "[#S] " }, { .name = "status-left-attr", .type = OPTIONS_TABLE_ATTRIBUTES, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0, .style = "status-left-style" }, { .name = "status-left-bg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_SESSION, .default_num = 8, .style = "status-left-style" }, { .name = "status-left-fg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_SESSION, .default_num = 8, .style = "status-left-style" }, { .name = "status-left-length", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_SESSION, .minimum = 0, .maximum = SHRT_MAX, .default_num = 10 @@ -385,40 +428,47 @@ const struct options_table_entry session_options_table[] = { { .name = "status-left-style", .type = OPTIONS_TABLE_STYLE, + .scope = OPTIONS_TABLE_SESSION, .default_str = "default" }, { .name = "status-position", .type = OPTIONS_TABLE_CHOICE, + .scope = OPTIONS_TABLE_SESSION, .choices = options_table_status_position_list, .default_num = 1 }, { .name = "status-right", .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SESSION, .default_str = " \"#{=21:pane_title}\" %H:%M %d-%b-%y" }, { .name = "status-right-attr", .type = OPTIONS_TABLE_ATTRIBUTES, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0, .style = "status-right-style" }, { .name = "status-right-bg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_SESSION, .default_num = 8, .style = "status-right-style" }, { .name = "status-right-fg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_SESSION, .default_num = 8, .style = "status-right-style" }, { .name = "status-right-length", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_SESSION, .minimum = 0, .maximum = SHRT_MAX, .default_num = 40 @@ -426,21 +476,19 @@ const struct options_table_entry session_options_table[] = { { .name = "status-right-style", .type = OPTIONS_TABLE_STYLE, + .scope = OPTIONS_TABLE_SESSION, .default_str = "default" }, { .name = "status-style", .type = OPTIONS_TABLE_STYLE, + .scope = OPTIONS_TABLE_SESSION, .default_str = "bg=green,fg=black" }, - { .name = "status-utf8", - .type = OPTIONS_TABLE_FLAG, - .default_num = 0 /* overridden in main() */ - }, - { .name = "update-environment", .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SESSION, .default_str = "DISPLAY SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID " "SSH_CONNECTION WINDOWID XAUTHORITY" @@ -448,82 +496,75 @@ const struct options_table_entry session_options_table[] = { { .name = "visual-activity", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0 }, { .name = "visual-bell", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0 }, { .name = "visual-silence", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_SESSION, .default_num = 0 }, { .name = "word-separators", .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SESSION, .default_str = " -_@" }, - { .name = NULL } -}; - -/* Window options. */ -const struct options_table_entry window_options_table[] = { { .name = "aggressive-resize", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 0 }, { .name = "allow-rename", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 1 }, { .name = "alternate-screen", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 1 }, { .name = "automatic-rename", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 1 }, { .name = "automatic-rename-format", .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_WINDOW, .default_str = "#{?pane_in_mode,[tmux],#{pane_current_command}}" "#{?pane_dead,[dead],}" }, - { .name = "c0-change-trigger", - .type = OPTIONS_TABLE_NUMBER, - .default_num = 250, - .minimum = 0, - .maximum = USHRT_MAX - }, - - { .name = "c0-change-interval", - .type = OPTIONS_TABLE_NUMBER, - .default_num = 100, - .minimum = 1, - .maximum = USHRT_MAX - }, - { .name = "clock-mode-colour", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 4 }, { .name = "clock-mode-style", .type = OPTIONS_TABLE_CHOICE, + .scope = OPTIONS_TABLE_WINDOW, .choices = options_table_clock_mode_style_list, .default_num = 1 }, { .name = "force-height", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_WINDOW, .minimum = 0, .maximum = INT_MAX, .default_num = 0 @@ -531,6 +572,7 @@ const struct options_table_entry window_options_table[] = { { .name = "force-width", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_WINDOW, .minimum = 0, .maximum = INT_MAX, .default_num = 0 @@ -538,6 +580,7 @@ const struct options_table_entry window_options_table[] = { { .name = "main-pane-height", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_WINDOW, .minimum = 1, .maximum = INT_MAX, .default_num = 24 @@ -545,6 +588,7 @@ const struct options_table_entry window_options_table[] = { { .name = "main-pane-width", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_WINDOW, .minimum = 1, .maximum = INT_MAX, .default_num = 80 @@ -552,46 +596,47 @@ const struct options_table_entry window_options_table[] = { { .name = "mode-attr", .type = OPTIONS_TABLE_ATTRIBUTES, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 0, .style = "mode-style" }, { .name = "mode-bg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 3, .style = "mode-style" }, { .name = "mode-fg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 0, .style = "mode-style" }, { .name = "mode-keys", .type = OPTIONS_TABLE_CHOICE, + .scope = OPTIONS_TABLE_WINDOW, .choices = options_table_mode_keys_list, .default_num = MODEKEY_EMACS }, - { .name = "mode-mouse", - .type = OPTIONS_TABLE_CHOICE, - .choices = options_table_mode_mouse_list, - .default_num = 0 - }, - { .name = "mode-style", .type = OPTIONS_TABLE_STYLE, + .scope = OPTIONS_TABLE_WINDOW, .default_str = "bg=yellow,fg=black" }, { .name = "monitor-activity", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 0 }, { .name = "monitor-silence", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_WINDOW, .minimum = 0, .maximum = INT_MAX, .default_num = 0 @@ -599,6 +644,7 @@ const struct options_table_entry window_options_table[] = { { .name = "other-pane-height", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_WINDOW, .minimum = 0, .maximum = INT_MAX, .default_num = 0 @@ -606,6 +652,7 @@ const struct options_table_entry window_options_table[] = { { .name = "other-pane-width", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_WINDOW, .minimum = 0, .maximum = INT_MAX, .default_num = 0 @@ -613,23 +660,27 @@ const struct options_table_entry window_options_table[] = { { .name = "pane-active-border-bg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 8, .style = "pane-active-border-style" }, { .name = "pane-active-border-fg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 2, .style = "pane-active-border-style" }, { .name = "pane-active-border-style", .type = OPTIONS_TABLE_STYLE, + .scope = OPTIONS_TABLE_WINDOW, .default_str = "fg=green" }, { .name = "pane-base-index", .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_WINDOW, .minimum = 0, .maximum = USHRT_MAX, .default_num = 0 @@ -637,187 +688,331 @@ const struct options_table_entry window_options_table[] = { { .name = "pane-border-bg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 8, .style = "pane-border-style" }, { .name = "pane-border-fg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 8, .style = "pane-border-style" }, { .name = "pane-border-style", .type = OPTIONS_TABLE_STYLE, + .scope = OPTIONS_TABLE_WINDOW, .default_str = "default" }, { .name = "remain-on-exit", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 0 }, { .name = "synchronize-panes", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 0 }, - { .name = "utf8", - .type = OPTIONS_TABLE_FLAG, - .default_num = 0 /* overridden in main() */ + { .name = "window-active-style", + .type = OPTIONS_TABLE_STYLE, + .scope = OPTIONS_TABLE_WINDOW, + .default_str = "default" + }, + + { .name = "window-style", + .type = OPTIONS_TABLE_STYLE, + .scope = OPTIONS_TABLE_WINDOW, + .default_str = "default" }, { .name = "window-status-activity-attr", .type = OPTIONS_TABLE_ATTRIBUTES, + .scope = OPTIONS_TABLE_WINDOW, .default_num = GRID_ATTR_REVERSE, .style = "window-status-activity-style" }, { .name = "window-status-activity-bg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 8, .style = "window-status-activity-style" }, { .name = "window-status-activity-fg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 8, .style = "window-status-activity-style" }, { .name = "window-status-activity-style", .type = OPTIONS_TABLE_STYLE, + .scope = OPTIONS_TABLE_WINDOW, .default_str = "reverse" }, { .name = "window-status-attr", .type = OPTIONS_TABLE_ATTRIBUTES, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 0, .style = "window-status-style" }, { .name = "window-status-bell-attr", .type = OPTIONS_TABLE_ATTRIBUTES, + .scope = OPTIONS_TABLE_WINDOW, .default_num = GRID_ATTR_REVERSE, .style = "window-status-bell-style" }, { .name = "window-status-bell-bg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 8, .style = "window-status-bell-style" }, { .name = "window-status-bell-fg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 8, .style = "window-status-bell-style" }, { .name = "window-status-bell-style", .type = OPTIONS_TABLE_STYLE, + .scope = OPTIONS_TABLE_WINDOW, .default_str = "reverse" }, { .name = "window-status-bg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 8, .style = "window-status-style" }, { .name = "window-status-current-attr", .type = OPTIONS_TABLE_ATTRIBUTES, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 0, .style = "window-status-current-style" }, { .name = "window-status-current-bg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 8, .style = "window-status-current-style" }, { .name = "window-status-current-fg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 8, .style = "window-status-current-style" }, { .name = "window-status-current-format", .type = OPTIONS_TABLE_STRING, - .default_str = "#I:#W#F" + .scope = OPTIONS_TABLE_WINDOW, + .default_str = "#I:#W#{?window_flags,#{window_flags}, }" }, { .name = "window-status-current-style", .type = OPTIONS_TABLE_STYLE, + .scope = OPTIONS_TABLE_WINDOW, .default_str = "default" }, { .name = "window-status-fg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 8, .style = "window-status-style" }, { .name = "window-status-format", .type = OPTIONS_TABLE_STRING, - .default_str = "#I:#W#F" + .scope = OPTIONS_TABLE_WINDOW, + .default_str = "#I:#W#{?window_flags,#{window_flags}, }" }, { .name = "window-status-last-attr", .type = OPTIONS_TABLE_ATTRIBUTES, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 0, .style = "window-status-last-style" }, { .name = "window-status-last-bg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 8, .style = "window-status-last-style" }, { .name = "window-status-last-fg", .type = OPTIONS_TABLE_COLOUR, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 8, .style = "window-status-last-style" }, { .name = "window-status-last-style", .type = OPTIONS_TABLE_STYLE, + .scope = OPTIONS_TABLE_WINDOW, .default_str = "default" }, { .name = "window-status-separator", .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_WINDOW, .default_str = " " }, { .name = "window-status-style", .type = OPTIONS_TABLE_STYLE, + .scope = OPTIONS_TABLE_WINDOW, .default_str = "default" }, { .name = "wrap-search", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 1 }, { .name = "xterm-keys", .type = OPTIONS_TABLE_FLAG, + .scope = OPTIONS_TABLE_WINDOW, .default_num = 0 }, +#ifdef TMATE + { .name = "tmate-identity", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SERVER, + .default_str = "" + }, + + { .name = "tmate-server-host", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SERVER, + .default_str = "ssh.tmate.io" + }, + + { .name = "tmate-server-port", + .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_SERVER, + .minimum = 1, + .maximum = 65535, + .default_num = 22 + }, + + { .name = "tmate-server-dsa-fingerprint", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SERVER, + .default_str = "obsolete" + }, + + { .name = "tmate-server-rsa-fingerprint", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SERVER, + .default_str = "SHA256:Hthk2T/M/Ivqfk1YYUn5ijC2Att3+UPzD7Rn72P5VWs" + }, + + { .name = "tmate-server-ecdsa-fingerprint", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SERVER, + .default_str = "SHA256:8GmKHYHEJ6n0TEdciHeEGkKOigQfCFuBULdt6vZIhDc" + }, + + { .name = "tmate-server-ed25519-fingerprint", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SERVER, + .default_str = "SHA256:jfttvoypkHiQYUqUCwKeqd9d1fJj/ZiQlFOHVl6E9sI" + }, + + { .name = "tmate-display-time", + .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_SESSION, + .minimum = 1, + .maximum = INT_MAX, + .default_num = 15000 + }, + + { .name = "tmate-webhook-userdata", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SERVER, + .default_str = "" + }, + + { .name = "tmate-webhook-url", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SERVER, + .default_str = "" + }, + + { .name = "tmate-api-key", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SERVER, + .default_str = "" + }, + + { .name = "tmate-session-name", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SERVER, + .default_str = "" + }, + + { .name = "tmate-session-name-ro", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SERVER, + .default_str = "" + }, + + { .name = "tmate-authorized-keys", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SERVER, + .default_str = "" + }, + + { .name = "tmate-set", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SERVER, + .default_str = "" + }, + + { .name = "tmate-foreground-restart", + .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_SERVER, + .minimum = 0, + .maximum = 1, + .default_num = 1 + }, +#endif + { .name = NULL } }; /* Populate an options tree from a table. */ void -options_table_populate_tree( - const struct options_table_entry *table, struct options *oo) +options_table_populate_tree(enum options_table_scope scope, struct options *oo) { const struct options_table_entry *oe; - for (oe = table; oe->name != NULL; oe++) { + for (oe = options_table; oe->name != NULL; oe++) { + if (oe->scope == OPTIONS_TABLE_NONE) + fatalx("no scope for %s", oe->name); + if (oe->scope != scope) + continue; switch (oe->type) { case OPTIONS_TABLE_STRING: options_set_string(oo, oe->name, "%s", oe->default_str); @@ -883,33 +1078,22 @@ options_table_print_entry(const struct options_table_entry *oe, /* Find an option. */ int -options_table_find( - const char *optstr, const struct options_table_entry **table, - const struct options_table_entry **oe) +options_table_find(const char *optstr, const struct options_table_entry **oe) { - static const struct options_table_entry *tables[] = { - server_options_table, - window_options_table, - session_options_table - }; const struct options_table_entry *oe_loop; - u_int i; - for (i = 0; i < nitems(tables); i++) { - for (oe_loop = tables[i]; oe_loop->name != NULL; oe_loop++) { - if (strncmp(oe_loop->name, optstr, strlen(optstr)) != 0) - continue; + for (oe_loop = options_table; oe_loop->name != NULL; oe_loop++) { + if (strncmp(oe_loop->name, optstr, strlen(optstr)) != 0) + continue; - /* If already found, ambiguous. */ - if (*oe != NULL) - return (-1); - *oe = oe_loop; - *table = tables[i]; + /* If already found, ambiguous. */ + if (*oe != NULL) + return (-1); + *oe = oe_loop; - /* Bail now if an exact match. */ - if (strcmp((*oe)->name, optstr) == 0) - break; - } + /* Bail now if an exact match. */ + if (strcmp(oe_loop->name, optstr) == 0) + break; } return (0); } diff --git a/options.c b/options.c index 030cfd51..df79ac4b 100644 --- a/options.c +++ b/options.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -29,34 +29,64 @@ * a red-black tree. */ +struct options { + RB_HEAD(options_tree, options_entry) tree; + struct options *parent; +}; + +static int options_cmp(struct options_entry *, struct options_entry *); +RB_PROTOTYPE(options_tree, options_entry, entry, options_cmp); RB_GENERATE(options_tree, options_entry, entry, options_cmp); -int +static void options_free1(struct options *, struct options_entry *); + +static int options_cmp(struct options_entry *o1, struct options_entry *o2) { return (strcmp(o1->name, o2->name)); } -void -options_init(struct options *oo, struct options *parent) +struct options * +options_create(struct options *parent) { + struct options *oo; + + oo = xcalloc(1, sizeof *oo); RB_INIT(&oo->tree); oo->parent = parent; + return (oo); +} + +static void +options_free1(struct options *oo, struct options_entry *o) +{ + RB_REMOVE(options_tree, &oo->tree, o); + free((char *)o->name); + if (o->type == OPTIONS_STRING) + free(o->str); + free(o); } void options_free(struct options *oo) { - struct options_entry *o; + struct options_entry *o, *o1; - while (!RB_EMPTY(&oo->tree)) { - o = RB_ROOT(&oo->tree); - RB_REMOVE(options_tree, &oo->tree, o); - free(o->name); - if (o->type == OPTIONS_STRING) - free(o->str); - free(o); - } + RB_FOREACH_SAFE (o, options_tree, &oo->tree, o1) + options_free1(oo, o); + free(oo); +} + +struct options_entry * +options_first(struct options *oo) +{ + return (RB_MIN(options_tree, &oo->tree)); +} + +struct options_entry * +options_next(struct options_entry *o) +{ + return (RB_NEXT(options_tree, &oo->tree, o)); } struct options_entry * @@ -64,7 +94,7 @@ options_find1(struct options *oo, const char *name) { struct options_entry p; - p.name = (char *) name; + p.name = (char *)name; return (RB_FIND(options_tree, &oo->tree, &p)); } @@ -73,7 +103,7 @@ options_find(struct options *oo, const char *name) { struct options_entry *o, p; - p.name = (char *) name; + p.name = (char *)name; o = RB_FIND(options_tree, &oo->tree, &p); while (o == NULL) { oo = oo->parent; @@ -89,14 +119,8 @@ options_remove(struct options *oo, const char *name) { struct options_entry *o; - if ((o = options_find1(oo, name)) == NULL) - return; - - RB_REMOVE(options_tree, &oo->tree, o); - free(o->name); - if (o->type == OPTIONS_STRING) - free(o->str); - free(o); + if ((o = options_find1(oo, name)) != NULL) + options_free1(oo, o); } struct options_entry * @@ -126,9 +150,9 @@ options_get_string(struct options *oo, const char *name) struct options_entry *o; if ((o = options_find(oo, name)) == NULL) - fatalx("missing option"); + fatalx("missing option %s", name); if (o->type != OPTIONS_STRING) - fatalx("option not a string"); + fatalx("option %s not a string", name); return (o->str); } @@ -156,9 +180,9 @@ options_get_number(struct options *oo, const char *name) struct options_entry *o; if ((o = options_find(oo, name)) == NULL) - fatalx("missing option"); + fatalx("missing option %s", name); if (o->type != OPTIONS_NUMBER) - fatalx("option not a number"); + fatalx("option %s not a number", name); return (o->num); } @@ -196,8 +220,8 @@ options_get_style(struct options *oo, const char *name) struct options_entry *o; if ((o = options_find(oo, name)) == NULL) - fatalx("missing option"); + fatalx("missing option %s", name); if (o->type != OPTIONS_STYLE) - fatalx("option not a style"); + fatalx("option %s not a style", name); return (&o->style); } diff --git a/osdep-aix.c b/osdep-aix.c index 24051ab8..e1ce4918 100644 --- a/osdep-aix.c +++ b/osdep-aix.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2011 Nicholas Marriott + * Copyright (c) 2011 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -16,21 +16,75 @@ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include +#include +#include -#include +#include +#include +#include #include "tmux.h" char * -osdep_get_name(unused int fd, unused char *tty) +osdep_get_name(__unused int fd, char *tty) { - return (NULL); + struct psinfo p; + char *path; + ssize_t bytes; + int f, ttyfd, retval; + pid_t pgrp; + + if ((ttyfd = open(tty, O_RDONLY|O_NOCTTY)) == -1) + return (NULL); + + retval = ioctl(ttyfd, TIOCGPGRP, &pgrp); + close(ttyfd); + if (retval == -1) + return (NULL); + + xasprintf(&path, "/proc/%u/psinfo", (u_int) pgrp); + f = open(path, O_RDONLY); + free(path); + if (f < 0) + return (NULL); + + bytes = read(f, &p, sizeof(p)); + close(f); + if (bytes != sizeof(p)) + return (NULL); + + return (xstrdup(p.pr_fname)); } char * -osdep_get_cwd(unused int fd) +osdep_get_cwd(int fd) { + static char target[MAXPATHLEN + 1]; + char *path; + const char *ttypath; + ssize_t n; + pid_t pgrp; + int len, retval, ttyfd; + + if ((ttypath = ptsname(fd)) == NULL) + return (NULL); + if ((ttyfd = open(ttypath, O_RDONLY|O_NOCTTY)) == -1) + return (NULL); + + retval = ioctl(ttyfd, TIOCGPGRP, &pgrp); + close(ttyfd); + if (retval == -1) + return (NULL); + + xasprintf(&path, "/proc/%u/cwd", (u_int) pgrp); + n = readlink(path, target, MAXPATHLEN); + free(path); + if (n > 0) { + target[n] = '\0'; + if ((len = strlen(target)) > 1 && target[len - 1] == '/') + target[len - 1] = '\0'; + return (target); + } return (NULL); } diff --git a/osdep-cygwin.c b/osdep-cygwin.c index 91bc5fc9..60630b33 100644 --- a/osdep-cygwin.c +++ b/osdep-cygwin.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -27,7 +27,7 @@ #include "tmux.h" char * -osdep_get_name(int fd, unused char *tty) +osdep_get_name(int fd, __unused char *tty) { FILE *f; char *path, *buf; diff --git a/osdep-darwin.c b/osdep-darwin.c index 7be70375..53b48138 100644 --- a/osdep-darwin.c +++ b/osdep-darwin.c @@ -28,10 +28,12 @@ char *osdep_get_name(int, char *); char *osdep_get_cwd(int); struct event_base *osdep_event_init(void); -#define unused __attribute__ ((unused)) +#ifndef __unused +#define __unused __attribute__ ((__unused__)) +#endif char * -osdep_get_name(int fd, unused char *tty) +osdep_get_name(int fd, __unused char *tty) { struct proc_bsdinfo bsdinfo; pid_t pgrp; diff --git a/osdep-dragonfly.c b/osdep-dragonfly.c index f9b0efcf..879034e8 100644 --- a/osdep-dragonfly.c +++ b/osdep-dragonfly.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above diff --git a/osdep-freebsd.c b/osdep-freebsd.c index d7f419b8..067ba565 100644 --- a/osdep-freebsd.c +++ b/osdep-freebsd.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above diff --git a/osdep-hpux.c b/osdep-hpux.c index 4baa6d49..16993b93 100644 --- a/osdep-hpux.c +++ b/osdep-hpux.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -23,13 +23,13 @@ #include "tmux.h" char * -osdep_get_name(unused int fd, unused char *tty) +osdep_get_name(__unused int fd, __unused char *tty) { return (NULL); } char * -osdep_get_cwd(unused int fd) +osdep_get_cwd(__unused int fd) { return (NULL); } diff --git a/osdep-linux.c b/osdep-linux.c index ad4c11cd..42712dea 100644 --- a/osdep-linux.c +++ b/osdep-linux.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -28,7 +28,7 @@ #include "tmux.h" char * -osdep_get_name(int fd, unused char *tty) +osdep_get_name(int fd, __unused char *tty) { FILE *f; char *path, *buf; diff --git a/osdep-netbsd.c b/osdep-netbsd.c index 15686860..d8aa41b5 100644 --- a/osdep-netbsd.c +++ b/osdep-netbsd.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above diff --git a/osdep-openbsd.c b/osdep-openbsd.c index 414228b7..1de876a4 100644 --- a/osdep-openbsd.c +++ b/osdep-openbsd.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above diff --git a/osdep-unknown.c b/osdep-unknown.c index 4de1ee1e..bc59f569 100644 --- a/osdep-unknown.c +++ b/osdep-unknown.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -23,7 +23,7 @@ #include "tmux.h" char * -osdep_get_name(unused int fd, unused char *tty) +osdep_get_name(__unused int fd, __unused char *tty) { return (NULL); } diff --git a/paste.c b/paste.c index 998b975a..e4eae877 100644 --- a/paste.c +++ b/paste.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -29,6 +29,18 @@ * string! */ +struct paste_buffer { + char *data; + size_t size; + + char *name; + int automatic; + u_int order; + + RB_ENTRY(paste_buffer) name_entry; + RB_ENTRY(paste_buffer) time_entry; +}; + u_int paste_next_index; u_int paste_next_order; u_int paste_num_automatic; @@ -59,6 +71,22 @@ paste_cmp_times(const struct paste_buffer *a, const struct paste_buffer *b) return (0); } +/* Get paste buffer name. */ +const char * +paste_buffer_name(struct paste_buffer *pb) +{ + return (pb->name); +} + +/* Get paste buffer data. */ +const char * +paste_buffer_data(struct paste_buffer *pb, size_t *size) +{ + if (size != NULL) + *size = pb->size; + return (pb->data); +} + /* Walk paste buffers by name. */ struct paste_buffer * paste_walk(struct paste_buffer *pb) @@ -70,28 +98,18 @@ paste_walk(struct paste_buffer *pb) /* Get the most recent automatic buffer. */ struct paste_buffer * -paste_get_top(void) +paste_get_top(const char **name) { struct paste_buffer *pb; pb = RB_MIN(paste_time_tree, &paste_by_time); if (pb == NULL) return (NULL); + if (name != NULL) + *name = pb->name; return (pb); } -/* Free the most recent buffer. */ -int -paste_free_top(void) -{ - struct paste_buffer *pb; - - pb = paste_get_top(); - if (pb == NULL) - return (-1); - return (paste_free_name(pb->name)); -} - /* Get a paste buffer by name. */ struct paste_buffer * paste_get_name(const char *name) @@ -105,20 +123,10 @@ paste_get_name(const char *name) return (RB_FIND(paste_name_tree, &paste_by_name, &pbfind)); } -/* Free a paste buffer by name. */ -int -paste_free_name(const char *name) +/* Free a paste buffer. */ +void +paste_free(struct paste_buffer *pb) { - struct paste_buffer *pb, pbfind; - - if (name == NULL || *name == '\0') - return (-1); - - pbfind.name = (char *)name; - pb = RB_FIND(paste_name_tree, &paste_by_name, &pbfind); - if (pb == NULL) - return (-1); - RB_REMOVE(paste_name_tree, &paste_by_name, pb); RB_REMOVE(paste_time_tree, &paste_by_time, pb); if (pb->automatic) @@ -127,7 +135,6 @@ paste_free_name(const char *name) free(pb->data); free(pb->name); free(pb); - return (0); } /* @@ -143,12 +150,12 @@ paste_add(char *data, size_t size) if (size == 0) return; - limit = options_get_number(&global_options, "buffer-limit"); + limit = options_get_number(global_options, "buffer-limit"); RB_FOREACH_REVERSE_SAFE(pb, paste_time_tree, &paste_by_time, pb1) { if (paste_num_automatic < limit) break; if (pb->automatic) - paste_free_name(pb->name); + paste_free(pb); } pb = xmalloc(sizeof *pb); @@ -226,7 +233,7 @@ paste_rename(const char *oldname, const char *newname, char **cause) int paste_set(char *data, size_t size, const char *name, char **cause) { - struct paste_buffer *pb; + struct paste_buffer *pb, *old; if (cause != NULL) *cause = NULL; @@ -246,10 +253,6 @@ paste_set(char *data, size_t size, const char *name, char **cause) return (-1); } - pb = paste_get_name(name); - if (pb != NULL) - paste_free_name(name); - pb = xmalloc(sizeof *pb); pb->name = xstrdup(name); @@ -260,6 +263,9 @@ paste_set(char *data, size_t size, const char *name, char **cause) pb->automatic = 0; pb->order = paste_next_order++; + if ((old = paste_get_name(name)) != NULL) + paste_free(old); + RB_INSERT(paste_name_tree, &paste_by_name, pb); RB_INSERT(paste_time_tree, &paste_by_time, pb); @@ -268,7 +274,7 @@ paste_set(char *data, size_t size, const char *name, char **cause) /* Convert start of buffer into a nice string. */ char * -paste_make_sample(struct paste_buffer *pb, int utf8flag) +paste_make_sample(struct paste_buffer *pb) { char *buf; size_t len, used; @@ -280,40 +286,8 @@ paste_make_sample(struct paste_buffer *pb, int utf8flag) len = width; buf = xreallocarray(NULL, len, 4 + 4); - if (utf8flag) - used = utf8_strvis(buf, pb->data, len, flags); - else - used = strvisx(buf, pb->data, len, flags); + used = utf8_strvis(buf, pb->data, len, flags); if (pb->size > width || used > width) strlcpy(buf + width, "...", 4); return (buf); } - -/* Paste into a window pane, filtering '\n' according to separator. */ -void -paste_send_pane(struct paste_buffer *pb, struct window_pane *wp, - const char *sep, int bracket) -{ - const char *data = pb->data, *end = data + pb->size, *lf; - size_t seplen; - - if (wp->flags & PANE_INPUTOFF) - return; - - if (bracket && (wp->screen->mode & MODE_BRACKETPASTE)) - bufferevent_write(wp->event, "\033[200~", 6); - - seplen = strlen(sep); - while ((lf = memchr(data, '\n', end - data)) != NULL) { - if (lf != data) - bufferevent_write(wp->event, data, lf - data); - bufferevent_write(wp->event, sep, seplen); - data = lf + 1; - } - - if (end != data) - bufferevent_write(wp->event, data, end - data); - - if (bracket && (wp->screen->mode & MODE_BRACKETPASTE)) - bufferevent_write(wp->event, "\033[201~", 6); -} diff --git a/presentations/tmux_asiabsdcon11.odt b/presentations/tmux_asiabsdcon11.odt new file mode 100644 index 00000000..ac9f0934 Binary files /dev/null and b/presentations/tmux_asiabsdcon11.odt differ diff --git a/presentations/tmux_asiabsdcon11.pdf b/presentations/tmux_asiabsdcon11.pdf new file mode 100644 index 00000000..76cd9b25 Binary files /dev/null and b/presentations/tmux_asiabsdcon11.pdf differ diff --git a/presentations/tmux_linuxtag_2011.odp b/presentations/tmux_linuxtag_2011.odp new file mode 100644 index 00000000..0b6c9cf1 Binary files /dev/null and b/presentations/tmux_linuxtag_2011.odp differ diff --git a/proc.c b/proc.c new file mode 100644 index 00000000..edd4a6f8 --- /dev/null +++ b/proc.c @@ -0,0 +1,274 @@ +/* $OpenBSD$ */ + +/* + * Copyright (c) 2015 Nicholas Marriott + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "tmux.h" + +struct tmuxproc { + const char *name; + int exit; + + void (*signalcb)(int); +}; + +struct tmuxpeer { + struct tmuxproc *parent; + + struct imsgbuf ibuf; + struct event event; + + int flags; +#define PEER_BAD 0x1 + + void (*dispatchcb)(struct imsg *, void *); + void *arg; +}; + +static int peer_check_version(struct tmuxpeer *, struct imsg *); +static void proc_update_event(struct tmuxpeer *); + +static void +proc_event_cb(__unused int fd, short events, void *arg) +{ + struct tmuxpeer *peer = arg; + ssize_t n; + struct imsg imsg; + + if (!(peer->flags & PEER_BAD) && (events & EV_READ)) { + if (((n = imsg_read(&peer->ibuf)) == -1 && errno != EAGAIN) || + n == 0) { + peer->dispatchcb(NULL, peer->arg); + return; + } + for (;;) { + if ((n = imsg_get(&peer->ibuf, &imsg)) == -1) { + peer->dispatchcb(NULL, peer->arg); + return; + } + if (n == 0) + break; + log_debug("peer %p message %d", peer, imsg.hdr.type); + + if (peer_check_version(peer, &imsg) != 0) { + if (imsg.fd != -1) + close(imsg.fd); + imsg_free(&imsg); + break; + } + + peer->dispatchcb(&imsg, peer->arg); + imsg_free(&imsg); + } + } + + if (events & EV_WRITE) { + if (msgbuf_write(&peer->ibuf.w) <= 0 && errno != EAGAIN) { + peer->dispatchcb(NULL, peer->arg); + return; + } + } + + if ((peer->flags & PEER_BAD) && peer->ibuf.w.queued == 0) { + peer->dispatchcb(NULL, peer->arg); + return; + } + + proc_update_event(peer); +} + +static void +proc_signal_cb(int signo, __unused short events, void *arg) +{ + struct tmuxproc *tp = arg; + + tp->signalcb(signo); +} + +static int +peer_check_version(struct tmuxpeer *peer, struct imsg *imsg) +{ + int version; + + version = imsg->hdr.peerid & 0xff; + if (imsg->hdr.type != MSG_VERSION && version != PROTOCOL_VERSION) { + log_debug("peer %p bad version %d", peer, version); + + proc_send(peer, MSG_VERSION, -1, NULL, 0); + peer->flags |= PEER_BAD; + + return (-1); + } + return (0); +} + +static void +proc_update_event(struct tmuxpeer *peer) +{ + short events; + + event_del(&peer->event); + + events = EV_READ; + if (peer->ibuf.w.queued > 0) + events |= EV_WRITE; + event_set(&peer->event, peer->ibuf.fd, events, proc_event_cb, peer); + + event_add(&peer->event, NULL); +} + +int +proc_send(struct tmuxpeer *peer, enum msgtype type, int fd, const void *buf, + size_t len) +{ + struct imsgbuf *ibuf = &peer->ibuf; + void *vp = (void *)buf; + int retval; + + if (peer->flags & PEER_BAD) + return (-1); + log_debug("sending message %d to peer %p (%zu bytes)", type, peer, len); + + retval = imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, fd, vp, len); + if (retval != 1) + return (-1); + proc_update_event(peer); + return (0); +} + +int +proc_send_s(struct tmuxpeer *peer, enum msgtype type, const char *s) +{ + return (proc_send(peer, type, -1, s, strlen(s) + 1)); +} + +struct tmuxproc * +proc_start(const char *name, struct event_base *base, int forkflag, + void (*signalcb)(int)) +{ + struct tmuxproc *tp; + struct utsname u; + + if (forkflag && !tmate_foreground) { + switch (fork()) { + case -1: + fatal("fork failed"); + case 0: + break; + default: + return (NULL); + } + if (daemon(1, 0) != 0) + fatal("daemon failed"); + + clear_signals(0); + if (event_reinit(base) != 0) + fatalx("event_reinit failed"); + } + + if (tmate_foreground) { + if (forkflag) + clear_signals(0); + log_open_fp(stdout); + } else { + log_open(name); + } + +#ifdef HAVE_SETPROCTITLE + setproctitle("%s (%s)", name, socket_path); +#endif + + if (uname(&u) < 0) + memset(&u, 0, sizeof u); + + log_debug("%s started (%ld): socket %s, protocol %d", name, + (long)getpid(), socket_path, PROTOCOL_VERSION); + log_debug("on %s %s %s; libevent %s (%s)", u.sysname, u.release, + u.version, event_get_version(), event_get_method()); + + tp = xcalloc(1, sizeof *tp); + tp->name = xstrdup(name); + + tp->signalcb = signalcb; + set_signals(proc_signal_cb, tp); + + return (tp); +} + +void +proc_loop(struct tmuxproc *tp, int (*loopcb)(void)) +{ + log_debug("%s loop enter", tp->name); + do + event_loop(EVLOOP_ONCE); + while (!tp->exit && (loopcb == NULL || !loopcb ())); + log_debug("%s loop exit", tp->name); +} + +void +proc_exit(struct tmuxproc *tp) +{ + tp->exit = 1; +} + +struct tmuxpeer * +proc_add_peer(struct tmuxproc *tp, int fd, + void (*dispatchcb)(struct imsg *, void *), void *arg) +{ + struct tmuxpeer *peer; + + peer = xcalloc(1, sizeof *peer); + peer->parent = tp; + + peer->dispatchcb = dispatchcb; + peer->arg = arg; + + imsg_init(&peer->ibuf, fd); + event_set(&peer->event, fd, EV_READ, proc_event_cb, peer); + + log_debug("add peer %p: %d (%p)", peer, fd, arg); + + proc_update_event(peer); + return (peer); +} + +void +proc_remove_peer(struct tmuxpeer *peer) +{ + log_debug("remove peer %p", peer); + + event_del(&peer->event); + imsg_clear(&peer->ibuf); + + close(peer->ibuf.fd); + free(peer); +} + +void +proc_kill_peer(struct tmuxpeer *peer) +{ + peer->flags |= PEER_BAD; +} diff --git a/resize.c b/resize.c index 9ad73c81..04b95c1f 100644 --- a/resize.c +++ b/resize.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -21,6 +21,7 @@ #include #include "tmux.h" +#include "tmate.h" /* * Recalculate window and session sizes. @@ -49,19 +50,26 @@ recalculate_sizes(void) struct client *c; struct window *w; struct window_pane *wp; - u_int i, j, ssx, ssy, has, limit; + u_int ssx, ssy, has, limit; int flag, has_status, is_zoomed, forced; +#ifdef TMATE + int tmate_sx = tmate_session.min_sx; + int tmate_sy = tmate_session.min_sy; +#endif RB_FOREACH(s, sessions, &sessions) { - has_status = options_get_number(&s->options, "status"); + has_status = options_get_number(s->options, "status"); s->attached = 0; ssx = ssy = UINT_MAX; - for (j = 0; j < ARRAY_LENGTH(&clients); j++) { - c = ARRAY_ITEM(&clients, j); - if (c == NULL || c->flags & CLIENT_SUSPENDED) + TAILQ_FOREACH(c, &clients, entry) { + if (c->flags & CLIENT_SUSPENDED) continue; if (c->session == s) { +#ifdef TMATE + if (c->flags & CLIENT_FORCE_STATUS) + has_status = 1; +#endif if (c->tty.sx < ssx) ssx = c->tty.sx; if (has_status && @@ -73,6 +81,17 @@ recalculate_sizes(void) s->attached++; } } + +#ifdef TMATE + /* We assume a single session */ + if (tmate_sx > 0 && tmate_sy > 0) { + if ((u_int)tmate_sx < ssx) + ssx = tmate_sx; + if ((u_int)tmate_sy < ssy) + ssy = tmate_sy; + } +#endif + if (ssx == UINT_MAX || ssy == UINT_MAX) { s->flags |= SESSION_UNATTACHED; continue; @@ -92,11 +111,10 @@ recalculate_sizes(void) s->sy = ssy; } - for (i = 0; i < ARRAY_LENGTH(&windows); i++) { - w = ARRAY_ITEM(&windows, i); - if (w == NULL || w->active == NULL) + RB_FOREACH(w, windows, &windows) { + if (w->active == NULL) continue; - flag = options_get_number(&w->options, "aggressive-resize"); + flag = options_get_number(w->options, "aggressive-resize"); ssx = ssy = UINT_MAX; RB_FOREACH(s, sessions, &sessions) { @@ -105,7 +123,7 @@ recalculate_sizes(void) if (flag) has = s->curw->window == w; else - has = session_has(s, w) != NULL; + has = session_has(s, w); if (has) { if (s->sx < ssx) ssx = s->sx; @@ -117,12 +135,12 @@ recalculate_sizes(void) continue; forced = 0; - limit = options_get_number(&w->options, "force-width"); + limit = options_get_number(w->options, "force-width"); if (limit >= PANE_MINIMUM && ssx > limit) { ssx = limit; forced |= WINDOW_FORCEWIDTH; } - limit = options_get_number(&w->options, "force-height"); + limit = options_get_number(w->options, "force-height"); if (limit >= PANE_MINIMUM && ssy > limit) { ssy = limit; forced |= WINDOW_FORCEHEIGHT; diff --git a/screen-redraw.c b/screen-redraw.c index c2b2ece6..4201b022 100644 --- a/screen-redraw.c +++ b/screen-redraw.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -26,13 +26,13 @@ int screen_redraw_cell_border1(struct window_pane *, u_int, u_int); int screen_redraw_cell_border(struct client *, u_int, u_int); int screen_redraw_check_cell(struct client *, u_int, u_int, struct window_pane **); -int screen_redraw_check_active(u_int, u_int, int, struct window *, - struct window_pane *); +int screen_redraw_check_is(u_int, u_int, int, struct window *, + struct window_pane *, struct window_pane *); void screen_redraw_draw_borders(struct client *, int, u_int); void screen_redraw_draw_panes(struct client *, u_int); void screen_redraw_draw_status(struct client *, u_int); -void screen_redraw_draw_number(struct client *, struct window_pane *); +void screen_redraw_draw_number(struct client *, struct window_pane *, u_int); #define CELL_INSIDE 0 #define CELL_LEFTRIGHT 1 @@ -175,13 +175,13 @@ screen_redraw_check_cell(struct client *c, u_int px, u_int py, return (CELL_OUTSIDE); } -/* Check active pane indicator. */ +/* Check if the border of a particular pane. */ int -screen_redraw_check_active(u_int px, u_int py, int type, struct window *w, - struct window_pane *wp) +screen_redraw_check_is(u_int px, u_int py, int type, struct window *w, + struct window_pane *wantwp, struct window_pane *wp) { /* Is this off the active pane border? */ - if (screen_redraw_cell_border1(w->active, px, py) != 1) + if (screen_redraw_cell_border1(wantwp, px, py) != 1) return (0); /* If there are more than two panes, that's enough. */ @@ -196,7 +196,7 @@ screen_redraw_check_active(u_int px, u_int py, int type, struct window *w, if (wp->xoff == 0 && wp->sx == w->sx) { /* This can either be the top pane or the bottom pane. */ if (wp->yoff == 0) { /* top pane */ - if (wp == w->active) + if (wp == wantwp) return (px <= wp->sx / 2); return (px > wp->sx / 2); } @@ -207,7 +207,7 @@ screen_redraw_check_active(u_int px, u_int py, int type, struct window *w, if (wp->yoff == 0 && wp->sy == w->sy) { /* This can either be the left pane or the right pane. */ if (wp->xoff == 0) { /* left pane */ - if (wp == w->active) + if (wp == wantwp) return (py <= wp->sy / 2); return (py > wp->sy / 2); } @@ -222,7 +222,7 @@ void screen_redraw_screen(struct client *c, int draw_panes, int draw_status, int draw_borders) { - struct options *oo = &c->session->options; + struct options *oo = c->session->options; struct tty *tty = &c->tty; u_int top; int status, spos; @@ -266,7 +266,7 @@ screen_redraw_pane(struct client *c, struct window_pane *wp) yoff++; for (i = 0; i < wp->sy; i++) - tty_draw_line(&c->tty, wp->screen, i, wp->xoff, yoff); + tty_draw_pane(&c->tty, wp, i, wp->xoff, yoff); tty_reset(&c->tty); } @@ -274,13 +274,15 @@ screen_redraw_pane(struct client *c, struct window_pane *wp) void screen_redraw_draw_borders(struct client *c, int status, u_int top) { - struct window *w = c->session->curw->window; - struct options *oo = &w->options; + struct session *s = c->session; + struct window *w = s->curw->window; + struct options *oo = w->options; struct tty *tty = &c->tty; - struct window_pane *wp; - struct grid_cell active_gc, other_gc, msg_gc; + struct window_pane *wp = NULL; + struct grid_cell m_active_gc, active_gc, m_other_gc, other_gc; + struct grid_cell msg_gc; u_int i, j, type, msgx = 0, msgy = 0; - int small, flags; + int active, small, flags; char msg[256]; const char *tmp; size_t msglen = 0; @@ -314,18 +316,32 @@ screen_redraw_draw_borders(struct client *c, int status, u_int top) style_apply(&active_gc, oo, "pane-active-border-style"); active_gc.attr = other_gc.attr = GRID_ATTR_CHARSET; + memcpy(&m_other_gc, &other_gc, sizeof m_other_gc); + m_other_gc.attr ^= GRID_ATTR_REVERSE; + memcpy(&m_active_gc, &active_gc, sizeof m_active_gc); + m_active_gc.attr ^= GRID_ATTR_REVERSE; + for (j = 0; j < tty->sy - status; j++) { for (i = 0; i < tty->sx; i++) { type = screen_redraw_check_cell(c, i, j, &wp); if (type == CELL_INSIDE) continue; - if (type == CELL_OUTSIDE && - small && i > msgx && j == msgy) + if (type == CELL_OUTSIDE && small && + i > msgx && j == msgy) continue; - if (screen_redraw_check_active(i, j, type, w, wp)) - tty_attributes(tty, &active_gc); + active = screen_redraw_check_is(i, j, type, w, + w->active, wp); + if (server_is_marked(s, s->curw, marked_pane.wp) && + screen_redraw_check_is(i, j, type, w, + marked_pane.wp, wp)) { + if (active) + tty_attributes(tty, &m_active_gc, NULL); + else + tty_attributes(tty, &m_other_gc, NULL); + } else if (active) + tty_attributes(tty, &active_gc, NULL); else - tty_attributes(tty, &other_gc); + tty_attributes(tty, &other_gc, NULL); tty_cursor(tty, i, top + j); tty_putc(tty, CELL_BORDERS[type]); } @@ -333,7 +349,7 @@ screen_redraw_draw_borders(struct client *c, int status, u_int top) if (small) { memcpy(&msg_gc, &grid_default_cell, sizeof msg_gc); - tty_attributes(tty, &msg_gc); + tty_attributes(tty, &msg_gc, NULL); tty_cursor(tty, msgx, msgy); tty_puts(tty, msg); } @@ -346,17 +362,15 @@ screen_redraw_draw_panes(struct client *c, u_int top) struct window *w = c->session->curw->window; struct tty *tty = &c->tty; struct window_pane *wp; - struct screen *s; u_int i; TAILQ_FOREACH(wp, &w->panes, entry) { if (!window_pane_visible(wp)) continue; - s = wp->screen; for (i = 0; i < wp->sy; i++) - tty_draw_line(tty, s, i, wp->xoff, top + wp->yoff); + tty_draw_pane(tty, wp, i, wp->xoff, top + wp->yoff); if (c->flags & CLIENT_IDENTIFY) - screen_redraw_draw_number(c, wp); + screen_redraw_draw_number(c, wp, top); } } @@ -367,18 +381,18 @@ screen_redraw_draw_status(struct client *c, u_int top) struct tty *tty = &c->tty; if (top) - tty_draw_line(tty, &c->status, 0, 0, 0); + tty_draw_line(tty, NULL, &c->status, 0, 0, 0); else - tty_draw_line(tty, &c->status, 0, 0, tty->sy - 1); + tty_draw_line(tty, NULL, &c->status, 0, 0, tty->sy - 1); } /* Draw number on a pane. */ void -screen_redraw_draw_number(struct client *c, struct window_pane *wp) +screen_redraw_draw_number(struct client *c, struct window_pane *wp, u_int top) { struct tty *tty = &c->tty; struct session *s = c->session; - struct options *oo = &s->options; + struct options *oo = s->options; struct window *w = wp->window; struct grid_cell gc; u_int idx, px, py, i, j, xoff, yoff; @@ -398,6 +412,9 @@ screen_redraw_draw_number(struct client *c, struct window_pane *wp) px = wp->sx / 2; py = wp->sy / 2; xoff = wp->xoff; yoff = wp->yoff; + if (top) + yoff++; + if (wp->sx < len * 6 || wp->sy < 5) { tty_cursor(tty, xoff + px - len / 2, yoff + py); goto draw_text; @@ -411,7 +428,7 @@ screen_redraw_draw_number(struct client *c, struct window_pane *wp) colour_set_bg(&gc, active_colour); else colour_set_bg(&gc, colour); - tty_attributes(tty, &gc); + tty_attributes(tty, &gc, wp); for (ptr = buf; *ptr != '\0'; ptr++) { if (*ptr < '0' || *ptr > '9') continue; @@ -438,7 +455,7 @@ draw_text: colour_set_fg(&gc, active_colour); else colour_set_fg(&gc, colour); - tty_attributes(tty, &gc); + tty_attributes(tty, &gc, wp); tty_puts(tty, buf); tty_cursor(tty, 0, 0); diff --git a/screen-write.c b/screen-write.c index e38c9f53..e58d744c 100644 --- a/screen-write.c +++ b/screen-write.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -42,11 +42,10 @@ screen_write_start(struct screen_write_ctx *ctx, struct window_pane *wp, /* Finish writing. */ void -screen_write_stop(unused struct screen_write_ctx *ctx) +screen_write_stop(__unused struct screen_write_ctx *ctx) { } - /* Reset screen state. */ void screen_write_reset(struct screen_write_ctx *ctx) @@ -68,13 +67,13 @@ void screen_write_putc(struct screen_write_ctx *ctx, struct grid_cell *gc, u_char ch) { - grid_cell_one(gc, ch); + utf8_set(&gc->data, ch); screen_write_cell(ctx, gc); } /* Calculate string length, with embedded formatting. */ size_t -screen_write_cstrlen(int utf8flag, const char *fmt, ...) +screen_write_cstrlen(const char *fmt, ...) { va_list ap; char *msg, *msg2, *ptr, *ptr2; @@ -99,7 +98,7 @@ screen_write_cstrlen(int utf8flag, const char *fmt, ...) } *ptr2 = '\0'; - size = screen_write_strlen(utf8flag, "%s", msg2); + size = screen_write_strlen("%s", msg2); free(msg); free(msg2); @@ -109,13 +108,14 @@ screen_write_cstrlen(int utf8flag, const char *fmt, ...) /* Calculate string length. */ size_t -screen_write_strlen(int utf8flag, const char *fmt, ...) +screen_write_strlen(const char *fmt, ...) { va_list ap; char *msg; - struct utf8_data utf8data; + struct utf8_data ud; u_char *ptr; size_t left, size = 0; + enum utf8_state more; va_start(ap, fmt); xvasprintf(&msg, fmt, ap); @@ -123,19 +123,21 @@ screen_write_strlen(int utf8flag, const char *fmt, ...) ptr = msg; while (*ptr != '\0') { - if (utf8flag && *ptr > 0x7f && utf8_open(&utf8data, *ptr)) { + if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) { ptr++; left = strlen(ptr); - if (left < utf8data.size - 1) + if (left < (size_t)ud.size - 1) break; - while (utf8_append(&utf8data, *ptr)) + while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE) ptr++; ptr++; - size += utf8data.width; + if (more == UTF8_DONE) + size += ud.width; } else { - size++; + if (*ptr > 0x1f && *ptr < 0x7f) + size++; ptr++; } } @@ -152,64 +154,67 @@ screen_write_puts(struct screen_write_ctx *ctx, struct grid_cell *gc, va_list ap; va_start(ap, fmt); - screen_write_vnputs(ctx, -1, gc, 0, fmt, ap); + screen_write_vnputs(ctx, -1, gc, fmt, ap); va_end(ap); } /* Write string with length limit (-1 for unlimited). */ void screen_write_nputs(struct screen_write_ctx *ctx, ssize_t maxlen, - struct grid_cell *gc, int utf8flag, const char *fmt, ...) + struct grid_cell *gc, const char *fmt, ...) { va_list ap; va_start(ap, fmt); - screen_write_vnputs(ctx, maxlen, gc, utf8flag, fmt, ap); + screen_write_vnputs(ctx, maxlen, gc, fmt, ap); va_end(ap); } void screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen, - struct grid_cell *gc, int utf8flag, const char *fmt, va_list ap) + struct grid_cell *gc, const char *fmt, va_list ap) { char *msg; - struct utf8_data utf8data; + struct utf8_data ud; u_char *ptr; size_t left, size = 0; + enum utf8_state more; xvasprintf(&msg, fmt, ap); ptr = msg; while (*ptr != '\0') { - if (utf8flag && *ptr > 0x7f && utf8_open(&utf8data, *ptr)) { + if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) { ptr++; left = strlen(ptr); - if (left < utf8data.size - 1) + if (left < (size_t)ud.size - 1) break; - while (utf8_append(&utf8data, *ptr)) + while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE) ptr++; ptr++; - if (maxlen > 0 && - size + utf8data.width > (size_t) maxlen) { - while (size < (size_t) maxlen) { - screen_write_putc(ctx, gc, ' '); - size++; + if (more == UTF8_DONE) { + if (maxlen > 0 && + size + ud.width > (size_t) maxlen) { + while (size < (size_t) maxlen) { + screen_write_putc(ctx, gc, ' '); + size++; + } + break; } - break; - } - size += utf8data.width; + size += ud.width; - grid_cell_set(gc, &utf8data); - screen_write_cell(ctx, gc); + utf8_copy(&gc->data, &ud); + screen_write_cell(ctx, gc); + } } else { if (maxlen > 0 && size + 1 > (size_t) maxlen) break; if (*ptr == '\001') gc->attr ^= GRID_ATTR_CHARSET; - else { + else if (*ptr > 0x1f && *ptr < 0x7f) { size++; screen_write_putc(ctx, gc, *ptr); } @@ -222,15 +227,16 @@ screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen, /* Write string, similar to nputs, but with embedded formatting (#[]). */ void -screen_write_cnputs(struct screen_write_ctx *ctx, - ssize_t maxlen, struct grid_cell *gc, int utf8flag, const char *fmt, ...) +screen_write_cnputs(struct screen_write_ctx *ctx, ssize_t maxlen, + struct grid_cell *gc, const char *fmt, ...) { struct grid_cell lgc; - struct utf8_data utf8data; + struct utf8_data ud; va_list ap; char *msg; u_char *ptr, *last; size_t left, size = 0; + enum utf8_state more; va_start(ap, fmt); xvasprintf(&msg, fmt, ap); @@ -254,34 +260,38 @@ screen_write_cnputs(struct screen_write_ctx *ctx, continue; } - if (utf8flag && *ptr > 0x7f && utf8_open(&utf8data, *ptr)) { + if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) { ptr++; left = strlen(ptr); - if (left < utf8data.size - 1) + if (left < (size_t)ud.size - 1) break; - while (utf8_append(&utf8data, *ptr)) + while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE) ptr++; ptr++; - if (maxlen > 0 && - size + utf8data.width > (size_t) maxlen) { - while (size < (size_t) maxlen) { - screen_write_putc(ctx, gc, ' '); - size++; + if (more == UTF8_DONE) { + if (maxlen > 0 && + size + ud.width > (size_t) maxlen) { + while (size < (size_t) maxlen) { + screen_write_putc(ctx, gc, ' '); + size++; + } + break; } - break; - } - size += utf8data.width; + size += ud.width; - grid_cell_set(&lgc, &utf8data); - screen_write_cell(ctx, &lgc); + utf8_copy(&lgc.data, &ud); + screen_write_cell(ctx, &lgc); + } } else { if (maxlen > 0 && size + 1 > (size_t) maxlen) break; - size++; - screen_write_putc(ctx, &lgc, *ptr); + if (*ptr > 0x1f && *ptr < 0x7f) { + size++; + screen_write_putc(ctx, &lgc, *ptr); + } ptr++; } } @@ -297,8 +307,7 @@ screen_write_copy(struct screen_write_ctx *ctx, struct screen *s = ctx->s; struct grid *gd = src->grid; struct grid_line *gl; - const struct grid_cell *gc; - struct utf8_data ud; + struct grid_cell gc; u_int xx, yy, cx, cy, ax, bx; cx = s->cx; @@ -322,12 +331,8 @@ screen_write_copy(struct screen_write_ctx *ctx, bx = px + nx; for (xx = ax; xx < bx; xx++) { - if (xx >= gl->cellsize) - gc = &grid_default_cell; - else - gc = &gl->celldata[xx]; - grid_cell_get(gc, &ud); - screen_write_cell(ctx, gc); + grid_get_cell(gd, xx, yy, &gc); + screen_write_cell(ctx, &gc); } if (px + nx == gd->sx && px + nx > gl->cellsize) screen_write_clearendofline(ctx); @@ -340,12 +345,12 @@ screen_write_copy(struct screen_write_ctx *ctx, /* Set up context for TTY command. */ void -screen_write_initctx( - struct screen_write_ctx *ctx, struct tty_ctx *ttyctx, int save_last) +screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx, + int save_last) { struct screen *s = ctx->s; struct grid *gd = s->grid; - const struct grid_cell *gc; + struct grid_cell gc; u_int xx; ttyctx->wp = ctx->wp; @@ -360,14 +365,14 @@ screen_write_initctx( return; /* Save the last cell on the screen. */ - gc = &grid_default_cell; + memcpy(&gc, &grid_default_cell, sizeof gc); for (xx = 1; xx <= screen_size_x(s); xx++) { - gc = grid_view_peek_cell(gd, screen_size_x(s) - xx, s->cy); - if (!(gc->flags & GRID_FLAG_PADDING)) + grid_view_get_cell(gd, screen_size_x(s) - xx, s->cy, &gc); + if (~gc.flags & GRID_FLAG_PADDING) break; } ttyctx->last_width = xx; - memcpy(&ttyctx->last_cell, gc, sizeof ttyctx->last_cell); + memcpy(&ttyctx->last_cell, &gc, sizeof ttyctx->last_cell); } /* Set a mode. */ @@ -505,7 +510,7 @@ screen_write_alignmenttest(struct screen_write_ctx *ctx) screen_write_initctx(ctx, &ttyctx, 0); memcpy(&gc, &grid_default_cell, sizeof gc); - grid_cell_one(&gc, 'E'); + utf8_set(&gc.data, 'E'); for (yy = 0; yy < screen_size_y(s); yy++) { for (xx = 0; xx < screen_size_x(s); xx++) @@ -762,8 +767,8 @@ screen_write_reverseindex(struct screen_write_ctx *ctx) /* Set scroll region. */ void -screen_write_scrollregion( - struct screen_write_ctx *ctx, u_int rupper, u_int rlower) +screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper, + u_int rlower) { struct screen *s = ctx->s; @@ -795,6 +800,8 @@ screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped) gl = &s->grid->linedata[s->grid->hsize + s->cy]; if (wrapped) gl->flags |= GRID_LINE_WRAPPED; + else + gl->flags &= ~GRID_LINE_WRAPPED; if (s->cy == s->rlower) grid_view_scroll_region_up(s->grid, s->rupper, s->rlower); @@ -867,16 +874,16 @@ screen_write_clearscreen(struct screen_write_ctx *ctx) { struct screen *s = ctx->s; struct tty_ctx ttyctx; + u_int sx = screen_size_x(s); + u_int sy = screen_size_y(s); screen_write_initctx(ctx, &ttyctx, 0); /* Scroll into history if it is enabled. */ if (s->grid->flags & GRID_HISTORY) grid_view_clear_history(s->grid); - else { - grid_view_clear( - s->grid, 0, 0, screen_size_x(s), screen_size_y(s)); - } + else + grid_view_clear(s->grid, 0, 0, sx, sy); tty_write(tty_cmd_clearscreen, &ttyctx); } @@ -900,14 +907,13 @@ screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc) struct grid *gd = s->grid; struct tty_ctx ttyctx; u_int width, xx, last; - struct grid_cell tmp_gc, *tmp_gcp; - struct utf8_data ud; + struct grid_cell tmp_gc; int insert; /* Ignore padding. */ if (gc->flags & GRID_FLAG_PADDING) return; - width = grid_cell_width(gc); + width = gc->data.width; /* * If this is a wide character and there is no room on the screen, for @@ -924,8 +930,7 @@ screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc) * there is space. */ if (width == 0) { - grid_cell_get(gc, &ud); - if (screen_write_combine(ctx, &ud) == 0) { + if (screen_write_combine(ctx, &gc->data) == 0) { screen_write_initctx(ctx, &ttyctx, 0); tty_write(tty_cmd_utf8character, &ttyctx); } @@ -960,11 +965,11 @@ screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc) * If the new character is UTF-8 wide, fill in padding cells. Have * already ensured there is enough room. */ - for (xx = s->cx + 1; xx < s->cx + width; xx++) { - tmp_gcp = grid_view_get_cell(gd, xx, s->cy); - if (tmp_gcp != NULL) - tmp_gcp->flags |= GRID_FLAG_PADDING; - } + memcpy(&tmp_gc, &grid_default_cell, sizeof tmp_gc); + tmp_gc.flags |= GRID_FLAG_PADDING; + tmp_gc.data.width = 0; + for (xx = s->cx + 1; xx < s->cx + width; xx++) + grid_view_set_cell(gd, xx, s->cy, &tmp_gc); /* Set the cell. */ grid_view_set_cell(gd, s->cx, s->cy, gc); @@ -986,11 +991,12 @@ screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc) } if (screen_check_selection(s, s->cx - width, s->cy)) { memcpy(&tmp_gc, &s->sel.cell, sizeof tmp_gc); - grid_cell_get(gc, &ud); - grid_cell_set(&tmp_gc, &ud); + utf8_copy(&tmp_gc.data, &gc->data); tmp_gc.attr = tmp_gc.attr & ~GRID_ATTR_CHARSET; tmp_gc.attr |= gc->attr & GRID_ATTR_CHARSET; - tmp_gc.flags = gc->flags & ~(GRID_FLAG_FG256|GRID_FLAG_BG256); + tmp_gc.flags = gc->flags; + tmp_gc.flags &= ~(GRID_FLAG_FGRGB|GRID_FLAG_BGRGB); + tmp_gc.flags &= ~(GRID_FLAG_FG256|GRID_FLAG_BG256); tmp_gc.flags |= s->sel.cell.flags & (GRID_FLAG_FG256|GRID_FLAG_BG256); ttyctx.cell = &tmp_gc; @@ -1007,8 +1013,7 @@ screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud) { struct screen *s = ctx->s; struct grid *gd = s->grid; - struct grid_cell *gc; - struct utf8_data ud1; + struct grid_cell gc; /* Can't combine if at 0. */ if (s->cx == 0) @@ -1019,17 +1024,18 @@ screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud) fatalx("UTF-8 data empty"); /* Retrieve the previous cell. */ - gc = grid_view_get_cell(gd, s->cx - 1, s->cy); - grid_cell_get(gc, &ud1); + grid_view_get_cell(gd, s->cx - 1, s->cy, &gc); /* Check there is enough space. */ - if (ud1.size + ud->size > sizeof ud1.data) + if (gc.data.size + ud->size > sizeof gc.data.data) return (-1); - /* Append the data and set the cell. */ - memcpy(ud1.data + ud1.size, ud->data, ud->size); - ud1.size += ud->size; - grid_cell_set(gc, &ud1); + /* Append the data. */ + memcpy(gc.data.data + gc.data.size, ud->data, ud->size); + gc.data.size += ud->size; + + /* Set the new cell. */ + grid_view_set_cell(gd, s->cx - 1, s->cy, &gc); return (0); } @@ -1048,11 +1054,11 @@ screen_write_overwrite(struct screen_write_ctx *ctx, u_int width) { struct screen *s = ctx->s; struct grid *gd = s->grid; - const struct grid_cell *gc; + struct grid_cell gc; u_int xx; - gc = grid_view_peek_cell(gd, s->cx, s->cy); - if (gc->flags & GRID_FLAG_PADDING) { + grid_view_get_cell(gd, s->cx, s->cy, &gc); + if (gc.flags & GRID_FLAG_PADDING) { /* * A padding cell, so clear any following and leading padding * cells back to the character. Don't overwrite the current @@ -1060,8 +1066,8 @@ screen_write_overwrite(struct screen_write_ctx *ctx, u_int width) */ xx = s->cx + 1; while (--xx > 0) { - gc = grid_view_peek_cell(gd, xx, s->cy); - if (!(gc->flags & GRID_FLAG_PADDING)) + grid_view_get_cell(gd, xx, s->cy, &gc); + if (~gc.flags & GRID_FLAG_PADDING) break; grid_view_set_cell(gd, xx, s->cy, &grid_default_cell); } @@ -1076,8 +1082,8 @@ screen_write_overwrite(struct screen_write_ctx *ctx, u_int width) */ xx = s->cx + width - 1; while (++xx < screen_size_x(s)) { - gc = grid_view_peek_cell(gd, xx, s->cy); - if (!(gc->flags & GRID_FLAG_PADDING)) + grid_view_get_cell(gd, xx, s->cy, &gc); + if (~gc.flags & GRID_FLAG_PADDING) break; grid_view_set_cell(gd, xx, s->cy, &grid_default_cell); } diff --git a/screen.c b/screen.c index c1df95ad..e002b96e 100644 --- a/screen.c +++ b/screen.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -18,7 +18,6 @@ #include -#include #include #include #include @@ -32,14 +31,8 @@ void screen_resize_y(struct screen *, u_int); void screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit) { - char host[HOST_NAME_MAX+1]; - s->grid = grid_create(sx, sy, hlimit); - - if (gethostname(host, sizeof(host)) == 0) - s->title = xstrdup(host); - else - s->title = xstrdup(""); + s->title = xstrdup(""); s->cstyle = 0; s->ccolour = xstrdup(""); @@ -101,10 +94,10 @@ screen_set_cursor_style(struct screen *s, u_int style) /* Set screen cursor colour. */ void -screen_set_cursor_colour(struct screen *s, const char *colour_string) +screen_set_cursor_colour(struct screen *s, const char *colour) { free(s->ccolour); - s->ccolour = xstrdup(colour_string); + s->ccolour = xstrdup(colour); } /* Set screen title. */ @@ -201,8 +194,6 @@ screen_resize_y(struct screen *s, u_int sy) * Now just increase the history size, if possible, to take * over the lines which are left. If history is off, delete * lines from the top. - * - * XXX Should apply history limit? */ available = s->cy; if (gd->flags & GRID_HISTORY) diff --git a/server-client.c b/server-client.c index 3ca99077..21cccc85 100644 --- a/server-client.c +++ b/server-client.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -28,41 +29,93 @@ #include #include "tmux.h" +#include "tmate.h" -void server_client_check_focus(struct window_pane *); -void server_client_check_resize(struct window_pane *); -void server_client_check_mouse(struct client *, struct window_pane *); -void server_client_repeat_timer(int, short, void *); -void server_client_check_exit(struct client *); -void server_client_check_redraw(struct client *); -void server_client_set_title(struct client *); -void server_client_reset_state(struct client *); -int server_client_assume_paste(struct session *); +void server_client_free(int, short, void *); +void server_client_check_focus(struct window_pane *); +void server_client_check_resize(struct window_pane *); +key_code server_client_check_mouse(struct client *); +void server_client_repeat_timer(int, short, void *); +void server_client_check_exit(struct client *); +void server_client_check_redraw(struct client *); +void server_client_set_title(struct client *); +void server_client_reset_state(struct client *); +int server_client_assume_paste(struct session *); -int server_client_msg_dispatch(struct client *); -void server_client_msg_command(struct client *, struct imsg *); -void server_client_msg_identify(struct client *, struct imsg *); -void server_client_msg_shell(struct client *); +void server_client_dispatch(struct imsg *, void *); +void server_client_dispatch_command(struct client *, struct imsg *); +void server_client_dispatch_identify(struct client *, struct imsg *); +void server_client_dispatch_shell(struct client *); + +/* Check if this client is inside this server. */ +int +server_client_check_nested(struct client *c) +{ + struct environ_entry *envent; + struct window_pane *wp; + + if (c->tty.path == NULL) + return (0); + + envent = environ_find(c->environ, "TMUX"); + if (envent == NULL || *envent->value == '\0') + return (0); + + RB_FOREACH(wp, window_pane_tree, &all_window_panes) { + if (strcmp(wp->tty, c->tty.path) == 0) + return (1); + } + return (0); +} + +/* Set client key table. */ +void +server_client_set_key_table(struct client *c, const char *name) +{ + if (name == NULL) + name = server_client_get_key_table(c); + + key_bindings_unref_table(c->keytable); + c->keytable = key_bindings_get_table(name, 1); + c->keytable->references++; +} + +/* Get default key table. */ +const char * +server_client_get_key_table(struct client *c) +{ + struct session *s = c->session; + const char *name; + + if (s == NULL) + return ("root"); + + name = options_get_string(s->options, "key-table"); + if (*name == '\0') + return ("root"); + return (name); +} /* Create a new client. */ void server_client_create(int fd) { struct client *c; - u_int i; setblocking(fd, 0); c = xcalloc(1, sizeof *c); - c->references = 0; - imsg_init(&c->ibuf, fd); - server_update_event(c); + c->references = 1; + c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c); if (gettimeofday(&c->creation_time, NULL) != 0) fatal("gettimeofday failed"); memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time); - environ_init(&c->environ); + c->environ = environ_create(); + + c->fd = -1; + c->cwd = NULL; c->cmdq = cmdq_new(c); c->cmdq->client_exit = 1; @@ -80,35 +133,23 @@ server_client_create(int fd) c->tty.sy = 24; screen_init(&c->status, c->tty.sx, 1, 0); - RB_INIT(&c->status_new); - RB_INIT(&c->status_old); c->message_string = NULL; - ARRAY_INIT(&c->message_log); + TAILQ_INIT(&c->message_log); c->prompt_string = NULL; c->prompt_buffer = NULL; c->prompt_index = 0; - c->tty.mouse.xb = c->tty.mouse.button = 3; - c->tty.mouse.x = c->tty.mouse.y = -1; - c->tty.mouse.lx = c->tty.mouse.ly = -1; - c->tty.mouse.sx = c->tty.mouse.sy = -1; - c->tty.mouse.event = MOUSE_EVENT_UP; - c->tty.mouse.flags = 0; - c->flags |= CLIENT_FOCUSED; + c->keytable = key_bindings_get_table("root", 1); + c->keytable->references++; + evtimer_set(&c->repeat_timer, server_client_repeat_timer, c); - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - if (ARRAY_ITEM(&clients, i) == NULL) { - ARRAY_SET(&clients, i, c); - return; - } - } - ARRAY_ADD(&clients, c); - log_debug("new client %d", fd); + TAILQ_INSERT_TAIL(&clients, c, entry); + log_debug("new client %p", c); } /* Open client terminal if needed. */ @@ -138,14 +179,18 @@ server_client_open(struct client *c, char **cause) void server_client_lost(struct client *c) { - struct message_entry *msg; - u_int i; + struct message_entry *msg, *msg1; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - if (ARRAY_ITEM(&clients, i) == c) - ARRAY_SET(&clients, i, NULL); - } - log_debug("lost client %d", c->ibuf.fd); + c->flags |= CLIENT_DEAD; + + status_prompt_clear(c); + status_message_clear(c); + + if (c->stdin_callback != NULL) + c->stdin_callback(c, 1, c->stdin_callback_data); + + TAILQ_REMOVE(&clients, c, entry); + log_debug("lost client %p", c); /* * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called @@ -161,50 +206,42 @@ server_client_lost(struct client *c) if (c->stderr_data != c->stdout_data) evbuffer_free(c->stderr_data); - status_free_jobs(&c->status_new); - status_free_jobs(&c->status_old); + if (event_initialized(&c->status_timer)) + evtimer_del(&c->status_timer); screen_free(&c->status); free(c->title); - close(c->cwd); + free((void *)c->cwd); evtimer_del(&c->repeat_timer); + key_bindings_unref_table(c->keytable); + if (event_initialized(&c->identify_timer)) evtimer_del(&c->identify_timer); free(c->message_string); if (event_initialized(&c->message_timer)) evtimer_del(&c->message_timer); - for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) { - msg = &ARRAY_ITEM(&c->message_log, i); + TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) { free(msg->msg); + TAILQ_REMOVE(&c->message_log, msg, entry); + free(msg); } - ARRAY_FREE(&c->message_log); free(c->prompt_string); free(c->prompt_buffer); - c->cmdq->dead = 1; + c->cmdq->flags |= CMD_Q_DEAD; cmdq_free(c->cmdq); c->cmdq = NULL; - environ_free(&c->environ); + environ_free(c->environ); - close(c->ibuf.fd); - imsg_clear(&c->ibuf); - if (event_initialized(&c->event)) - event_del(&c->event); + proc_remove_peer(c->peer); + c->peer = NULL; - for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) { - if (ARRAY_ITEM(&dead_clients, i) == NULL) { - ARRAY_SET(&dead_clients, i, c); - break; - } - } - if (i == ARRAY_LENGTH(&dead_clients)) - ARRAY_ADD(&dead_clients, c); - c->flags |= CLIENT_DEAD; + server_client_unref(c); server_add_accept(0); /* may be more file descriptors now */ @@ -213,132 +250,306 @@ server_client_lost(struct client *c) server_update_socket(); } -/* Process a single client event. */ +/* Remove reference from a client. */ void -server_client_callback(int fd, short events, void *data) +server_client_unref(struct client *c) { - struct client *c = data; + log_debug("unref client %p (%d references)", c, c->references); - if (c->flags & CLIENT_DEAD) - return; - - if (fd == c->ibuf.fd) { - if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) <= 0 && - errno != EAGAIN) - goto client_lost; - - if (c->flags & CLIENT_BAD) { - if (c->ibuf.w.queued == 0) - goto client_lost; - return; - } - - if (events & EV_READ && server_client_msg_dispatch(c) != 0) - goto client_lost; - } - - server_push_stdout(c); - server_push_stderr(c); - - server_update_event(c); - return; - -client_lost: - server_client_lost(c); + c->references--; + if (c->references == 0) + event_once(-1, EV_TIMEOUT, server_client_free, c, NULL); } -/* Handle client status timer. */ +/* Free dead client. */ void -server_client_status_timer(void) +server_client_free(__unused int fd, __unused short events, void *arg) { - struct client *c; - struct session *s; - struct timeval tv; - u_int i; - int interval; - time_t difference; + struct client *c = arg; - if (gettimeofday(&tv, NULL) != 0) - fatal("gettimeofday failed"); + log_debug("free client %p (%d references)", c, c->references); - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session == NULL) - continue; - if (c->message_string != NULL || c->prompt_string != NULL) { - /* - * Don't need timed redraw for messages/prompts so bail - * now. The status timer isn't reset when they are - * redrawn anyway. - */ - continue; - } - s = c->session; + if (c->references == 0) + free(c); +} - if (!options_get_number(&s->options, "status")) - continue; - interval = options_get_number(&s->options, "status-interval"); +/* Detach a client. */ +void +server_client_detach(struct client *c, enum msgtype msgtype) +{ + struct session *s = c->session; - difference = tv.tv_sec - c->status_timer.tv_sec; - if (interval != 0 && difference >= interval) { - status_update_jobs(c); - c->flags |= CLIENT_STATUS; - } - } + if (s == NULL) + return; + + hooks_run(c->session->hooks, c, NULL, "client-detached"); + proc_send_s(c->peer, msgtype, s->name); } /* Check for mouse keys. */ -void -server_client_check_mouse(struct client *c, struct window_pane *wp) +key_code +server_client_check_mouse(struct client *c) { - struct session *s = c->session; - struct options *oo = &s->options; - struct mouse_event *m = &c->tty.mouse; - int statusat; + struct session *s = c->session; + struct mouse_event *m = &c->tty.mouse; + struct window *w; + struct window_pane *wp; + enum { NOTYPE, DOWN, UP, DRAG, WHEEL } type = NOTYPE; + enum { NOWHERE, PANE, STATUS, BORDER } where = NOWHERE; + u_int x, y, b; + key_code key; - statusat = status_at_line(c); + log_debug("mouse %02x at %u,%u (last %u,%u) (%d)", m->b, m->x, m->y, + m->lx, m->ly, c->tty.mouse_drag_flag); - /* Is this a window selection click on the status line? */ - if (statusat != -1 && m->y == (u_int)statusat && - options_get_number(oo, "mouse-select-window")) { - if (m->event & MOUSE_EVENT_CLICK) { - status_set_window_at(c, m->x); - } else if (m->event == MOUSE_EVENT_WHEEL) { - if (m->wheel == MOUSE_WHEEL_UP) - session_previous(c->session, 0); - else if (m->wheel == MOUSE_WHEEL_DOWN) - session_next(c->session, 0); - server_redraw_session(s); + /* What type of event is this? */ + if (MOUSE_DRAG(m->b)) { + type = DRAG; + if (c->tty.mouse_drag_flag) { + x = m->x, y = m->y, b = m->b; + log_debug("drag update at %u,%u", x, y); + } else { + x = m->lx, y = m->ly, b = m->lb; + log_debug("drag start at %u,%u", x, y); } - recalculate_sizes(); - return; + } else if (MOUSE_WHEEL(m->b)) { + type = WHEEL; + x = m->x, y = m->y, b = m->b; + log_debug("wheel at %u,%u", x, y); + } else if (MOUSE_BUTTONS(m->b) == 3) { + type = UP; + x = m->x, y = m->y, b = m->lb; + log_debug("up at %u,%u", x, y); + } else { + type = DOWN; + x = m->x, y = m->y, b = m->b; + log_debug("down at %u,%u", x, y); + } + if (type == NOTYPE) + return (KEYC_UNKNOWN); + + /* Always save the session. */ + m->s = s->id; + + /* Is this on the status line? */ + m->statusat = status_at_line(c); + if (m->statusat != -1 && y == (u_int)m->statusat) { + w = status_get_window_at(c, x); + if (w == NULL) + return (KEYC_UNKNOWN); + m->w = w->id; + where = STATUS; + } else + m->w = -1; + + /* Not on status line. Adjust position and check for border or pane. */ + if (where == NOWHERE) { + if (m->statusat == 0 && y > 0) + y--; + else if (m->statusat > 0 && y >= (u_int)m->statusat) + y = m->statusat - 1; + + TAILQ_FOREACH(wp, &s->curw->window->panes, entry) { + if ((wp->xoff + wp->sx == x && + wp->yoff <= 1 + y && + wp->yoff + wp->sy >= y) || + (wp->yoff + wp->sy == y && + wp->xoff <= 1 + x && + wp->xoff + wp->sx >= x)) + break; + } + if (wp != NULL) + where = BORDER; + else { + wp = window_get_active_at(s->curw->window, x, y); + if (wp != NULL) { + where = PANE; + log_debug("mouse at %u,%u is on pane %%%u", + x, y, wp->id); + } + } + if (where == NOWHERE) + return (KEYC_UNKNOWN); + m->wp = wp->id; + m->w = wp->window->id; + } else + m->wp = -1; + + /* Stop dragging if needed. */ + if (type != DRAG && c->tty.mouse_drag_flag) { + if (c->tty.mouse_drag_release != NULL) + c->tty.mouse_drag_release(c, m); + + c->tty.mouse_drag_update = NULL; + c->tty.mouse_drag_release = NULL; + + /* + * End a mouse drag by passing a MouseDragEnd key corresponding + * to the button that started the drag. + */ + switch (c->tty.mouse_drag_flag) { + case 1: + if (where == PANE) + key = KEYC_MOUSEDRAGEND1_PANE; + if (where == STATUS) + key = KEYC_MOUSEDRAGEND1_STATUS; + if (where == BORDER) + key = KEYC_MOUSEDRAGEND1_BORDER; + break; + case 2: + if (where == PANE) + key = KEYC_MOUSEDRAGEND2_PANE; + if (where == STATUS) + key = KEYC_MOUSEDRAGEND2_STATUS; + if (where == BORDER) + key = KEYC_MOUSEDRAGEND2_BORDER; + break; + case 3: + if (where == PANE) + key = KEYC_MOUSEDRAGEND3_PANE; + if (where == STATUS) + key = KEYC_MOUSEDRAGEND3_STATUS; + if (where == BORDER) + key = KEYC_MOUSEDRAGEND3_BORDER; + break; + default: + key = KEYC_MOUSE; + break; + } + c->tty.mouse_drag_flag = 0; + + return (key); } - /* - * Not on status line - adjust mouse position if status line is at the - * top and limit if at the bottom. From here on a struct mouse - * represents the offset onto the window itself. - */ - if (statusat == 0 && m->y > 0) - m->y--; - else if (statusat > 0 && m->y >= (u_int)statusat) - m->y = statusat - 1; + /* Convert to a key binding. */ + key = KEYC_UNKNOWN; + switch (type) { + case NOTYPE: + break; + case DRAG: + if (c->tty.mouse_drag_update != NULL) + c->tty.mouse_drag_update(c, m); + else { + switch (MOUSE_BUTTONS(b)) { + case 0: + if (where == PANE) + key = KEYC_MOUSEDRAG1_PANE; + if (where == STATUS) + key = KEYC_MOUSEDRAG1_STATUS; + if (where == BORDER) + key = KEYC_MOUSEDRAG1_BORDER; + break; + case 1: + if (where == PANE) + key = KEYC_MOUSEDRAG2_PANE; + if (where == STATUS) + key = KEYC_MOUSEDRAG2_STATUS; + if (where == BORDER) + key = KEYC_MOUSEDRAG2_BORDER; + break; + case 2: + if (where == PANE) + key = KEYC_MOUSEDRAG3_PANE; + if (where == STATUS) + key = KEYC_MOUSEDRAG3_STATUS; + if (where == BORDER) + key = KEYC_MOUSEDRAG3_BORDER; + break; + } + } - /* Is this a pane selection? */ - if (options_get_number(oo, "mouse-select-pane") && - (m->event == MOUSE_EVENT_DOWN || m->event == MOUSE_EVENT_WHEEL)) { - window_set_active_at(wp->window, m->x, m->y); - server_status_window(wp->window); - server_redraw_window_borders(wp->window); - wp = wp->window->active; /* may have changed */ + /* + * Begin a drag by setting the flag to a non-zero value that + * corresponds to the mouse button in use. + */ + c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1; + break; + case WHEEL: + if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) { + if (where == PANE) + key = KEYC_WHEELUP_PANE; + if (where == STATUS) + key = KEYC_WHEELUP_STATUS; + if (where == BORDER) + key = KEYC_WHEELUP_BORDER; + } else { + if (where == PANE) + key = KEYC_WHEELDOWN_PANE; + if (where == STATUS) + key = KEYC_WHEELDOWN_STATUS; + if (where == BORDER) + key = KEYC_WHEELDOWN_BORDER; + } + break; + case UP: + switch (MOUSE_BUTTONS(b)) { + case 0: + if (where == PANE) + key = KEYC_MOUSEUP1_PANE; + if (where == STATUS) + key = KEYC_MOUSEUP1_STATUS; + if (where == BORDER) + key = KEYC_MOUSEUP1_BORDER; + break; + case 1: + if (where == PANE) + key = KEYC_MOUSEUP2_PANE; + if (where == STATUS) + key = KEYC_MOUSEUP2_STATUS; + if (where == BORDER) + key = KEYC_MOUSEUP2_BORDER; + break; + case 2: + if (where == PANE) + key = KEYC_MOUSEUP3_PANE; + if (where == STATUS) + key = KEYC_MOUSEUP3_STATUS; + if (where == BORDER) + key = KEYC_MOUSEUP3_BORDER; + break; + } + break; + case DOWN: + switch (MOUSE_BUTTONS(b)) { + case 0: + if (where == PANE) + key = KEYC_MOUSEDOWN1_PANE; + if (where == STATUS) + key = KEYC_MOUSEDOWN1_STATUS; + if (where == BORDER) + key = KEYC_MOUSEDOWN1_BORDER; + break; + case 1: + if (where == PANE) + key = KEYC_MOUSEDOWN2_PANE; + if (where == STATUS) + key = KEYC_MOUSEDOWN2_STATUS; + if (where == BORDER) + key = KEYC_MOUSEDOWN2_BORDER; + break; + case 2: + if (where == PANE) + key = KEYC_MOUSEDOWN3_PANE; + if (where == STATUS) + key = KEYC_MOUSEDOWN3_STATUS; + if (where == BORDER) + key = KEYC_MOUSEDOWN3_BORDER; + break; + } + break; } + if (key == KEYC_UNKNOWN) + return (KEYC_UNKNOWN); - /* Check if trying to resize pane. */ - if (options_get_number(oo, "mouse-resize-pane")) - layout_resize_pane_mouse(c); + /* Apply modifiers if any. */ + if (b & MOUSE_MASK_META) + key |= KEYC_ESCAPE; + if (b & MOUSE_MASK_CTRL) + key |= KEYC_CTRL; + if (b & MOUSE_MASK_SHIFT) + key |= KEYC_SHIFT; - /* Update last and pass through to client. */ - window_pane_mouse(wp, c->session, m); + return (key); } /* Is this fast enough to probably be a paste? */ @@ -348,46 +559,47 @@ server_client_assume_paste(struct session *s) struct timeval tv; int t; - if ((t = options_get_number(&s->options, "assume-paste-time")) == 0) + if ((t = options_get_number(s->options, "assume-paste-time")) == 0) return (0); timersub(&s->activity_time, &s->last_activity_time, &tv); - if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) - return (1); + if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) { + log_debug("session %s pasting (flag %d)", s->name, + !!(s->flags & SESSION_PASTING)); + if (s->flags & SESSION_PASTING) + return (1); + s->flags |= SESSION_PASTING; + return (0); + } + log_debug("session %s not pasting", s->name); + s->flags &= ~SESSION_PASTING; return (0); } /* Handle data key input from client. */ void -server_client_handle_key(struct client *c, int key) +server_client_handle_key(struct client *c, key_code key) { - struct session *s; + struct mouse_event *m = &c->tty.mouse; + struct session *s = c->session; struct window *w; struct window_pane *wp; struct timeval tv; - struct key_binding *bd; - int xtimeout, isprefix, ispaste; + struct key_table *table; + struct key_binding bd_find, *bd; + int xtimeout; /* Check the client is good to accept input. */ - if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0) + if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0) return; - - if (c->session == NULL) - return; - s = c->session; + w = s->curw->window; /* Update the activity timer. */ if (gettimeofday(&c->activity_time, NULL) != 0) fatal("gettimeofday failed"); + session_update_activity(s, &c->activity_time); - memcpy(&s->last_activity_time, &s->activity_time, - sizeof s->last_activity_time); - memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time); - - w = c->session->curw->window; - wp = w->active; - - /* Special case: number keys jump to pane in identify mode. */ + /* Number keys jump to pane in identify mode. */ if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') { if (c->flags & CLIENT_READONLY) return; @@ -401,6 +613,9 @@ server_client_handle_key(struct client *c, int key) /* Handle status line. */ if (!(c->flags & CLIENT_READONLY)) { +#ifdef TMATE + if (!(c->flags & CLIENT_FORCE_STATUS)) +#endif status_message_clear(c); server_clear_identify(c); } @@ -414,78 +629,108 @@ server_client_handle_key(struct client *c, int key) if (key == KEYC_MOUSE) { if (c->flags & CLIENT_READONLY) return; - server_client_check_mouse(c, wp); - return; - } - - /* Is this a prefix key? */ - if (key == options_get_number(&s->options, "prefix")) - isprefix = 1; - else if (key == options_get_number(&s->options, "prefix2")) - isprefix = 1; - else - isprefix = 0; - - /* Treat prefix as a regular key when pasting is detected. */ - ispaste = server_client_assume_paste(s); - if (ispaste) - isprefix = 0; - - /* No previous prefix key. */ - if (!(c->flags & CLIENT_PREFIX)) { - if (isprefix) { - c->flags |= CLIENT_PREFIX; - server_status_client(c); + key = server_client_check_mouse(c); + if (key == KEYC_UNKNOWN) return; - } - /* Try as a non-prefix key binding. */ - if (ispaste || (bd = key_bindings_lookup(key)) == NULL) { - if (!(c->flags & CLIENT_READONLY)) - window_pane_key(wp, s, key); - } else - key_bindings_dispatch(bd, c); - return; - } + m->valid = 1; + m->key = key; - /* Prefix key already pressed. Reset prefix and lookup key. */ - c->flags &= ~CLIENT_PREFIX; - server_status_client(c); - if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) { - /* If repeating, treat this as a key, else ignore. */ - if (c->flags & CLIENT_REPEAT) { + if (!options_get_number(s->options, "mouse")) + goto forward; + } else + m->valid = 0; + + /* Treat everything as a regular key when pasting is detected. */ + if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s)) + goto forward; + +retry: + /* Try to see if there is a key binding in the current table. */ + bd_find.key = key; + bd = RB_FIND(key_bindings, &c->keytable->key_bindings, &bd_find); + if (bd != NULL) { + /* + * Key was matched in this table. If currently repeating but a + * non-repeating binding was found, stop repeating and try + * again in the root table. + */ + if ((c->flags & CLIENT_REPEAT) && !bd->can_repeat) { + server_client_set_key_table(c, NULL); c->flags &= ~CLIENT_REPEAT; - if (isprefix) - c->flags |= CLIENT_PREFIX; - else if (!(c->flags & CLIENT_READONLY)) - window_pane_key(wp, s, key); + server_status_client(c); + goto retry; } + + /* + * Take a reference to this table to make sure the key binding + * doesn't disappear. + */ + table = c->keytable; + table->references++; + + /* + * If this is a repeating key, start the timer. Otherwise reset + * the client back to the root table. + */ + xtimeout = options_get_number(s->options, "repeat-time"); + if (xtimeout != 0 && bd->can_repeat) { + c->flags |= CLIENT_REPEAT; + + tv.tv_sec = xtimeout / 1000; + tv.tv_usec = (xtimeout % 1000) * 1000L; + evtimer_del(&c->repeat_timer); + evtimer_add(&c->repeat_timer, &tv); + } else { + c->flags &= ~CLIENT_REPEAT; + server_client_set_key_table(c, NULL); + } + server_status_client(c); + + /* Dispatch the key binding. */ + key_bindings_dispatch(bd, c, m); + key_bindings_unref_table(table); return; } - /* If already repeating, but this key can't repeat, skip it. */ - if (c->flags & CLIENT_REPEAT && !bd->can_repeat) { + /* + * No match in this table. If repeating, switch the client back to the + * root table and try again. + */ + if (c->flags & CLIENT_REPEAT) { + server_client_set_key_table(c, NULL); c->flags &= ~CLIENT_REPEAT; - if (isprefix) - c->flags |= CLIENT_PREFIX; - else if (!(c->flags & CLIENT_READONLY)) - window_pane_key(wp, s, key); + server_status_client(c); + goto retry; + } + + /* If no match and we're not in the root table, that's it. */ + if (strcmp(c->keytable->name, server_client_get_key_table(c)) != 0) { + server_client_set_key_table(c, NULL); + server_status_client(c); return; } - /* If this key can repeat, reset the repeat flags and timer. */ - xtimeout = options_get_number(&s->options, "repeat-time"); - if (xtimeout != 0 && bd->can_repeat) { - c->flags |= CLIENT_PREFIX|CLIENT_REPEAT; - - tv.tv_sec = xtimeout / 1000; - tv.tv_usec = (xtimeout % 1000) * 1000L; - evtimer_del(&c->repeat_timer); - evtimer_add(&c->repeat_timer, &tv); + /* + * No match, but in the root table. Prefix switches to the prefix table + * and everything else is passed through. + */ + if (key == (key_code)options_get_number(s->options, "prefix") || + key == (key_code)options_get_number(s->options, "prefix2")) { + server_client_set_key_table(c, "prefix"); + server_status_client(c); + return; } - /* Dispatch the command. */ - key_bindings_dispatch(bd, c); +forward: + if (c->flags & CLIENT_READONLY) + return; + if (KEYC_IS_MOUSE(key)) + wp = cmd_mouse_pane(m, NULL, NULL); + else + wp = w->active; + if (wp != NULL) + window_pane_key(wp, c, s, key, m); } /* Client functions that need to happen every loop. */ @@ -495,13 +740,11 @@ server_client_loop(void) struct client *c; struct window *w; struct window_pane *wp; - u_int i; - - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL) - continue; +#ifdef TMATE + int tmate_should_sync_layout = 0; +#endif + TAILQ_FOREACH(c, &clients, entry) { server_client_check_exit(c); if (c->session != NULL) { server_client_check_redraw(c); @@ -513,10 +756,13 @@ server_client_loop(void) * Any windows will have been redrawn as part of clients, so clear * their flags now. Also check pane focus and resize. */ - for (i = 0; i < ARRAY_LENGTH(&windows); i++) { - w = ARRAY_ITEM(&windows, i); - if (w == NULL) - continue; + RB_FOREACH(w, windows, &windows) { +#ifdef TMATE + if (w->flags & WINDOW_REDRAW) + tmate_should_sync_layout = 1; + if (w->tmate_last_sync_active_pane != w->active) + tmate_should_sync_layout = 1; +#endif w->flags &= ~WINDOW_REDRAW; TAILQ_FOREACH(wp, &w->panes, entry) { @@ -526,7 +772,13 @@ server_client_loop(void) } wp->flags &= ~PANE_REDRAW; } + check_window_name(w); } + +#ifdef TMATE + if (tmate_should_sync_layout) + tmate_sync_layout(); +#endif } /* Check if pane should be resized. */ @@ -562,12 +814,11 @@ server_client_check_resize(struct window_pane *wp) void server_client_check_focus(struct window_pane *wp) { - u_int i; struct client *c; int push; /* Are focus events off? */ - if (!options_get_number(&global_options, "focus-events")) + if (!options_get_number(global_options, "focus-events")) return; /* Do we need to push the focus state? */ @@ -590,12 +841,8 @@ server_client_check_focus(struct window_pane *wp) * If our window is the current window in any focused clients with an * attached session, we're focused. */ - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session == NULL) - continue; - - if (!(c->flags & CLIENT_FOCUSED)) + TAILQ_FOREACH(c, &clients, entry) { + if (c->session == NULL || !(c->flags & CLIENT_FOCUSED)) continue; if (c->session->flags & SESSION_UNATTACHED) continue; @@ -631,8 +878,7 @@ server_client_reset_state(struct client *c) struct window *w = c->session->curw->window; struct window_pane *wp = w->active; struct screen *s = wp->screen; - struct options *oo = &c->session->options; - struct options *wo = &w->options; + struct options *oo = c->session->options; int status, mode, o; if (c->flags & CLIENT_SUSPENDED) @@ -644,6 +890,10 @@ server_client_reset_state(struct client *c) tty_region(&c->tty, 0, c->tty.sy - 1); status = options_get_number(oo, "status"); +#ifdef TMATE + if (c->flags & CLIENT_FORCE_STATUS) + status = 1; +#endif if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status) tty_cursor(&c->tty, 0, 0); else { @@ -652,42 +902,12 @@ server_client_reset_state(struct client *c) } /* - * Resizing panes with the mouse requires at least button mode to give - * a smooth appearance. + * Set mouse mode if requested. To support dragging, always use button + * mode. */ mode = s->mode; - if ((c->tty.mouse.flags & MOUSE_RESIZE_PANE) && - !(mode & MODE_MOUSE_BUTTON)) - mode |= MODE_MOUSE_BUTTON; - - /* - * Any mode will do for mouse-select-pane, but set standard mode if - * none. - */ - if ((mode & ALL_MOUSE_MODES) == 0) { - if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL && - options_get_number(oo, "mouse-select-pane")) - mode |= MODE_MOUSE_STANDARD; - else if (options_get_number(oo, "mouse-resize-pane")) - mode |= MODE_MOUSE_STANDARD; - else if (options_get_number(oo, "mouse-select-window")) - mode |= MODE_MOUSE_STANDARD; - else if (options_get_number(wo, "mode-mouse")) - mode |= MODE_MOUSE_STANDARD; - } - - /* - * Set UTF-8 mouse input if required. If the terminal is UTF-8, the - * user has set mouse-utf8 and any mouse mode is in effect, turn on - * UTF-8 mouse input. If the receiving terminal hasn't requested it - * (that is, it isn't in s->mode), then it'll be converted in - * input_mouse. - */ - if ((c->tty.flags & TTY_UTF8) && - (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8")) - mode |= MODE_MOUSE_UTF8; - else - mode &= ~MODE_MOUSE_UTF8; + if (options_get_number(oo, "mouse")) + mode = (mode & ~ALL_MOUSE_MODES) | MODE_MOUSE_BUTTON; /* Set the terminal mode and reset attributes. */ tty_update_mode(&c->tty, mode, s); @@ -696,14 +916,14 @@ server_client_reset_state(struct client *c) /* Repeat time callback. */ void -server_client_repeat_timer(unused int fd, unused short events, void *data) +server_client_repeat_timer(__unused int fd, __unused short events, void *data) { struct client *c = data; if (c->flags & CLIENT_REPEAT) { - if (c->flags & CLIENT_PREFIX) - server_status_client(c); - c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT); + server_client_set_key_table(c, NULL); + c->flags &= ~CLIENT_REPEAT; + server_status_client(c); } } @@ -721,7 +941,7 @@ server_client_check_exit(struct client *c) if (EVBUFFER_LENGTH(c->stderr_data) != 0) return; - server_write_client(c, MSG_EXIT, &c->retval, sizeof c->retval); + proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval); c->flags &= ~CLIENT_EXIT; } @@ -730,17 +950,15 @@ void server_client_check_redraw(struct client *c) { struct session *s = c->session; + struct tty *tty = &c->tty; struct window_pane *wp; int flags, redraw; if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED)) return; - flags = c->tty.flags & TTY_FREEZE; - c->tty.flags &= ~TTY_FREEZE; - if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) { - if (options_get_number(&s->options, "set-titles")) + if (options_get_number(s->options, "set-titles")) server_client_set_title(c); if (c->message_string != NULL) @@ -753,29 +971,42 @@ server_client_check_redraw(struct client *c) c->flags &= ~CLIENT_STATUS; } + flags = tty->flags & (TTY_FREEZE|TTY_NOCURSOR); + tty->flags = (tty->flags & ~TTY_FREEZE) | TTY_NOCURSOR; + if (c->flags & CLIENT_REDRAW) { + tty_update_mode(tty, tty->mode, NULL); screen_redraw_screen(c, 1, 1, 1); c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS); } else if (c->flags & CLIENT_REDRAWWINDOW) { + tty_update_mode(tty, tty->mode, NULL); TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) screen_redraw_pane(c, wp); c->flags &= ~CLIENT_REDRAWWINDOW; } else { TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) { - if (wp->flags & PANE_REDRAW) + if (wp->flags & PANE_REDRAW) { + tty_update_mode(tty, tty->mode, NULL); screen_redraw_pane(c, wp); + } } } - if (c->flags & CLIENT_BORDERS) + if (c->flags & CLIENT_BORDERS) { + tty_update_mode(tty, tty->mode, NULL); screen_redraw_screen(c, 0, 0, 1); + } - if (c->flags & CLIENT_STATUS) + if (c->flags & CLIENT_STATUS) { + tty_update_mode(tty, tty->mode, NULL); screen_redraw_screen(c, 0, 1, 0); + } - c->tty.flags |= flags; + tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags; + tty_update_mode(tty, tty->mode, NULL); - c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS); + c->flags &= ~(CLIENT_REDRAW|CLIENT_BORDERS|CLIENT_STATUS| + CLIENT_STATUSFORCE); } /* Set client title. */ @@ -787,9 +1018,9 @@ server_client_set_title(struct client *c) char *title; struct format_tree *ft; - template = options_get_string(&s->options, "set-titles-string"); + template = options_get_string(s->options, "set-titles-string"); - ft = format_create(); + ft = format_create(NULL, 0); format_defaults(ft, c, NULL, NULL, NULL); title = format_expand_time(ft, template, time(NULL)); @@ -804,120 +1035,111 @@ server_client_set_title(struct client *c) } /* Dispatch message from client. */ -int -server_client_msg_dispatch(struct client *c) +void +server_client_dispatch(struct imsg *imsg, void *arg) { - struct imsg imsg; + struct client *c = arg; struct msg_stdin_data stdindata; const char *data; - ssize_t n, datalen; + ssize_t datalen; + struct session *s; - if ((n = imsg_read(&c->ibuf)) == -1 || n == 0) - return (-1); + if (c->flags & CLIENT_DEAD) + return; - for (;;) { - if ((n = imsg_get(&c->ibuf, &imsg)) == -1) - return (-1); - if (n == 0) - return (0); + if (imsg == NULL) { + server_client_lost(c); + return; + } - data = imsg.data; - datalen = imsg.hdr.len - IMSG_HEADER_SIZE; + data = imsg->data; + datalen = imsg->hdr.len - IMSG_HEADER_SIZE; - if (imsg.hdr.peerid != PROTOCOL_VERSION) { - server_write_client(c, MSG_VERSION, NULL, 0); - c->flags |= CLIENT_BAD; - if (imsg.fd != -1) - close(imsg.fd); - imsg_free(&imsg); - continue; + switch (imsg->hdr.type) { + case MSG_IDENTIFY_FLAGS: + case MSG_IDENTIFY_TERM: + case MSG_IDENTIFY_TTYNAME: + case MSG_IDENTIFY_CWD: + case MSG_IDENTIFY_STDIN: + case MSG_IDENTIFY_ENVIRON: + case MSG_IDENTIFY_CLIENTPID: + case MSG_IDENTIFY_DONE: + server_client_dispatch_identify(c, imsg); + break; + case MSG_COMMAND: + server_client_dispatch_command(c, imsg); + break; + case MSG_STDIN: + if (datalen != sizeof stdindata) + fatalx("bad MSG_STDIN size"); + memcpy(&stdindata, data, sizeof stdindata); + + if (c->stdin_callback == NULL) + break; + if (stdindata.size <= 0) + c->stdin_closed = 1; + else { + evbuffer_add(c->stdin_data, stdindata.data, + stdindata.size); } + c->stdin_callback(c, c->stdin_closed, + c->stdin_callback_data); + break; + case MSG_RESIZE: + if (datalen != 0) + fatalx("bad MSG_RESIZE size"); - log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd); - switch (imsg.hdr.type) { - case MSG_IDENTIFY_FLAGS: - case MSG_IDENTIFY_TERM: - case MSG_IDENTIFY_TTYNAME: - case MSG_IDENTIFY_CWD: - case MSG_IDENTIFY_STDIN: - case MSG_IDENTIFY_ENVIRON: - case MSG_IDENTIFY_DONE: - server_client_msg_identify(c, &imsg); + if (c->flags & CLIENT_CONTROL) break; - case MSG_COMMAND: - server_client_msg_command(c, &imsg); - break; - case MSG_STDIN: - if (datalen != sizeof stdindata) - fatalx("bad MSG_STDIN size"); - memcpy(&stdindata, data, sizeof stdindata); - - if (c->stdin_callback == NULL) - break; - if (stdindata.size <= 0) - c->stdin_closed = 1; - else { - evbuffer_add(c->stdin_data, stdindata.data, - stdindata.size); - } - c->stdin_callback(c, c->stdin_closed, - c->stdin_callback_data); - break; - case MSG_RESIZE: - if (datalen != 0) - fatalx("bad MSG_RESIZE size"); - - if (c->flags & CLIENT_CONTROL) - break; - if (tty_resize(&c->tty)) { - recalculate_sizes(); - server_redraw_client(c); - } - break; - case MSG_EXITING: - if (datalen != 0) - fatalx("bad MSG_EXITING size"); - - c->session = NULL; - tty_close(&c->tty); - server_write_client(c, MSG_EXITED, NULL, 0); - break; - case MSG_WAKEUP: - case MSG_UNLOCK: - if (datalen != 0) - fatalx("bad MSG_WAKEUP size"); - - if (!(c->flags & CLIENT_SUSPENDED)) - break; - c->flags &= ~CLIENT_SUSPENDED; - - if (c->tty.fd == -1) /* exited in the meantime */ - break; - - if (gettimeofday(&c->activity_time, NULL) != 0) - fatal("gettimeofday"); - if (c->session != NULL) - session_update_activity(c->session); - - tty_start_tty(&c->tty); - server_redraw_client(c); + if (tty_resize(&c->tty)) { recalculate_sizes(); - break; - case MSG_SHELL: - if (datalen != 0) - fatalx("bad MSG_SHELL size"); - - server_client_msg_shell(c); - break; + server_redraw_client(c); } + if (c->session != NULL) + hooks_run(c->session->hooks, c, NULL, "client-resized"); + break; + case MSG_EXITING: + if (datalen != 0) + fatalx("bad MSG_EXITING size"); - imsg_free(&imsg); + c->session = NULL; + tty_close(&c->tty); + proc_send(c->peer, MSG_EXITED, -1, NULL, 0); + break; + case MSG_WAKEUP: + case MSG_UNLOCK: + if (datalen != 0) + fatalx("bad MSG_WAKEUP size"); + + if (!(c->flags & CLIENT_SUSPENDED)) + break; + c->flags &= ~CLIENT_SUSPENDED; + + if (c->tty.fd == -1) /* exited in the meantime */ + break; + s = c->session; + + if (gettimeofday(&c->activity_time, NULL) != 0) + fatal("gettimeofday failed"); + if (s != NULL) + session_update_activity(s, &c->activity_time); + + tty_start_tty(&c->tty); + server_redraw_client(c); + recalculate_sizes(); + break; + case MSG_SHELL: + if (datalen != 0) + fatalx("bad MSG_SHELL size"); + + server_client_dispatch_shell(c); + break; } } /* Handle command message. */ void -server_client_msg_command(struct client *c, struct imsg *imsg) +server_client_dispatch_command(struct client *c, struct imsg *imsg) { struct msg_command_data data; char *buf; @@ -955,9 +1177,9 @@ server_client_msg_command(struct client *c, struct imsg *imsg) cmd_free_argv(argc, argv); if (c != cfg_client || cfg_finished) - cmdq_run(c->cmdq, cmdlist); + cmdq_run(c->cmdq, cmdlist, NULL); else - cmdq_append(c->cmdq, cmdlist); + cmdq_append(c->cmdq, cmdlist, NULL); cmd_list_free(cmdlist); return; @@ -970,9 +1192,9 @@ error: /* Handle identify message. */ void -server_client_msg_identify(struct client *c, struct imsg *imsg) +server_client_dispatch_identify(struct client *c, struct imsg *imsg) { - const char *data; + const char *data, *home; size_t datalen; int flags; @@ -988,32 +1210,49 @@ server_client_msg_identify(struct client *c, struct imsg *imsg) fatalx("bad MSG_IDENTIFY_FLAGS size"); memcpy(&flags, data, sizeof flags); c->flags |= flags; + log_debug("client %p IDENTIFY_FLAGS %#x", c, flags); break; case MSG_IDENTIFY_TERM: if (datalen == 0 || data[datalen - 1] != '\0') fatalx("bad MSG_IDENTIFY_TERM string"); c->term = xstrdup(data); + log_debug("client %p IDENTIFY_TERM %s", c, data); break; case MSG_IDENTIFY_TTYNAME: if (datalen == 0 || data[datalen - 1] != '\0') fatalx("bad MSG_IDENTIFY_TTYNAME string"); c->ttyname = xstrdup(data); + log_debug("client %p IDENTIFY_TTYNAME %s", c, data); break; case MSG_IDENTIFY_CWD: - if (datalen != 0) - fatalx("bad MSG_IDENTIFY_CWD size"); - c->cwd = imsg->fd; + if (datalen == 0 || data[datalen - 1] != '\0') + fatalx("bad MSG_IDENTIFY_CWD string"); + if (access(data, X_OK) == 0) + c->cwd = xstrdup(data); + else if ((home = find_home()) != NULL) + c->cwd = xstrdup(home); + else + c->cwd = xstrdup("/"); + log_debug("client %p IDENTIFY_CWD %s", c, data); break; case MSG_IDENTIFY_STDIN: if (datalen != 0) fatalx("bad MSG_IDENTIFY_STDIN size"); c->fd = imsg->fd; + log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd); break; case MSG_IDENTIFY_ENVIRON: if (datalen == 0 || data[datalen - 1] != '\0') fatalx("bad MSG_IDENTIFY_ENVIRON string"); if (strchr(data, '=') != NULL) - environ_put(&c->environ, data); + environ_put(c->environ, data); + log_debug("client %p IDENTIFY_ENVIRON %s", c, data); + break; + case MSG_IDENTIFY_CLIENTPID: + if (datalen != sizeof c->pid) + fatalx("bad MSG_IDENTIFY_CLIENTPID size"); + memcpy(&c->pid, data, sizeof c->pid); + log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid); break; default: break; @@ -1025,7 +1264,6 @@ server_client_msg_identify(struct client *c, struct imsg *imsg) #ifdef __CYGWIN__ c->fd = open(c->ttyname, O_RDWR|O_NOCTTY); - c->cwd = open(".", O_RDONLY); #endif if (c->flags & CLIENT_CONTROL) { @@ -1036,10 +1274,9 @@ server_client_msg_identify(struct client *c, struct imsg *imsg) if (c->flags & CLIENT_CONTROLCONTROL) evbuffer_add_printf(c->stdout_data, "\033P1000p"); - server_write_client(c, MSG_STDIN, NULL, 0); + proc_send(c->peer, MSG_STDIN, -1, NULL, 0); c->tty.fd = -1; - c->tty.log_fd = -1; close(c->fd); c->fd = -1; @@ -1049,12 +1286,11 @@ server_client_msg_identify(struct client *c, struct imsg *imsg) if (c->fd == -1) return; - if (!isatty(c->fd)) { + if (tty_init(&c->tty, c, c->fd, c->term) != 0) { close(c->fd); c->fd = -1; return; } - tty_init(&c->tty, c, c->fd, c->term); if (c->flags & CLIENT_UTF8) c->tty.flags |= TTY_UTF8; if (c->flags & CLIENT_256COLOURS) @@ -1068,14 +1304,101 @@ server_client_msg_identify(struct client *c, struct imsg *imsg) /* Handle shell message. */ void -server_client_msg_shell(struct client *c) +server_client_dispatch_shell(struct client *c) { const char *shell; - shell = options_get_string(&global_s_options, "default-shell"); + shell = options_get_string(global_s_options, "default-shell"); if (*shell == '\0' || areshell(shell)) shell = _PATH_BSHELL; - server_write_client(c, MSG_SHELL, shell, strlen(shell) + 1); + proc_send_s(c->peer, MSG_SHELL, shell); - c->flags |= CLIENT_BAD; /* it will die after exec */ + proc_kill_peer(c->peer); +} + +/* Event callback to push more stdout data if any left. */ +static void +server_client_stdout_cb(__unused int fd, __unused short events, void *arg) +{ + struct client *c = arg; + + if (~c->flags & CLIENT_DEAD) + server_client_push_stdout(c); + server_client_unref(c); +} + +/* Push stdout to client if possible. */ +void +server_client_push_stdout(struct client *c) +{ + struct msg_stdout_data data; + size_t sent, left; + + left = EVBUFFER_LENGTH(c->stdout_data); + while (left != 0) { + sent = left; + if (sent > sizeof data.data) + sent = sizeof data.data; + memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent); + data.size = sent; + + if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0) + break; + evbuffer_drain(c->stdout_data, sent); + + left = EVBUFFER_LENGTH(c->stdout_data); + log_debug("%s: client %p, sent %zu, left %zu", __func__, c, + sent, left); + } + if (left != 0) { + c->references++; + event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL); + log_debug("%s: client %p, queued", __func__, c); + } +} + +/* Event callback to push more stderr data if any left. */ +static void +server_client_stderr_cb(__unused int fd, __unused short events, void *arg) +{ + struct client *c = arg; + + if (~c->flags & CLIENT_DEAD) + server_client_push_stderr(c); + server_client_unref(c); +} + +/* Push stderr to client if possible. */ +void +server_client_push_stderr(struct client *c) +{ + struct msg_stderr_data data; + size_t sent, left; + + if (c->stderr_data == c->stdout_data) { + server_client_push_stdout(c); + return; + } + + left = EVBUFFER_LENGTH(c->stderr_data); + while (left != 0) { + sent = left; + if (sent > sizeof data.data) + sent = sizeof data.data; + memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent); + data.size = sent; + + if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0) + break; + evbuffer_drain(c->stderr_data, sent); + + left = EVBUFFER_LENGTH(c->stderr_data); + log_debug("%s: client %p, sent %zu, left %zu", __func__, c, + sent, left); + } + if (left != 0) { + c->references++; + event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL); + log_debug("%s: client %p, queued", __func__, c); + } } diff --git a/server-fn.c b/server-fn.c index 1ed3a01f..78bc2bea 100644 --- a/server-fn.c +++ b/server-fn.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -17,6 +17,7 @@ */ #include +#include #include #include @@ -31,61 +32,19 @@ void server_callback_identify(int, short, void *); void server_fill_environ(struct session *s, struct environ *env) { - char var[PATH_MAX], *term; - u_int idx; - long pid; + char *term; + u_int idx; + long pid; if (s != NULL) { - term = options_get_string(&s->options, "default-terminal"); - environ_set(env, "TERM", term); + term = options_get_string(global_options, "default-terminal"); + environ_set(env, "TERM", "%s", term); idx = s->id; } else - idx = -1; + idx = (u_int)-1; pid = getpid(); - xsnprintf(var, sizeof var, "%s,%ld,%d", socket_path, pid, idx); - environ_set(env, "TMUX", var); -} - -void -server_write_ready(struct client *c) -{ - if (c->flags & CLIENT_CONTROL) - return; - server_write_client(c, MSG_READY, NULL, 0); -} - -int -server_write_client(struct client *c, enum msgtype type, const void *buf, - size_t len) -{ - struct imsgbuf *ibuf = &c->ibuf; - int error; - - if (c->flags & CLIENT_BAD) - return (-1); - log_debug("writing %d to client %d", type, c->ibuf.fd); - error = imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, -1, - (void *) buf, len); - if (error == 1) - server_update_event(c); - return (error == 1 ? 0 : -1); -} - -void -server_write_session(struct session *s, enum msgtype type, const void *buf, - size_t len) -{ - struct client *c; - u_int i; - - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session == NULL) - continue; - if (c->session == s) - server_write_client(c, type, buf, len); - } + environ_set(env, "TMUX", "%s,%ld,%u", socket_path, pid, idx); } void @@ -104,12 +63,8 @@ void server_redraw_session(struct session *s) { struct client *c; - u_int i; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session == NULL) - continue; + TAILQ_FOREACH(c, &clients, entry) { if (c->session == s) server_redraw_client(c); } @@ -132,12 +87,8 @@ void server_status_session(struct session *s) { struct client *c; - u_int i; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session == NULL) - continue; + TAILQ_FOREACH(c, &clients, entry) { if (c->session == s) server_status_client(c); } @@ -160,13 +111,9 @@ void server_redraw_window(struct window *w) { struct client *c; - u_int i; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session == NULL) - continue; - if (c->session->curw->window == w) + TAILQ_FOREACH(c, &clients, entry) { + if (c->session != NULL && c->session->curw->window == w) server_redraw_client(c); } w->flags |= WINDOW_REDRAW; @@ -176,13 +123,9 @@ void server_redraw_window_borders(struct window *w) { struct client *c; - u_int i; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session == NULL) - continue; - if (c->session->curw->window == w) + TAILQ_FOREACH(c, &clients, entry) { + if (c->session != NULL && c->session->curw->window == w) c->flags |= CLIENT_BORDERS; } } @@ -199,7 +142,7 @@ server_status_window(struct window *w) */ RB_FOREACH(s, sessions, &sessions) { - if (session_has(s, w) != NULL) + if (session_has(s, w)) server_status_session(s); } } @@ -208,13 +151,10 @@ void server_lock(void) { struct client *c; - u_int i; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session == NULL) - continue; - server_lock_client(c); + TAILQ_FOREACH(c, &clients, entry) { + if (c->session != NULL) + server_lock_client(c); } } @@ -222,13 +162,10 @@ void server_lock_session(struct session *s) { struct client *c; - u_int i; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session == NULL || c->session != s) - continue; - server_lock_client(c); + TAILQ_FOREACH(c, &clients, entry) { + if (c->session == s) + server_lock_client(c); } } @@ -243,7 +180,7 @@ server_lock_client(struct client *c) if (c->flags & CLIENT_SUSPENDED) return; - cmd = options_get_string(&c->session->options, "lock-command"); + cmd = options_get_string(c->session->options, "lock-command"); if (strlen(cmd) + 1 > MAX_IMSGSIZE - IMSG_HEADER_SIZE) return; @@ -253,7 +190,7 @@ server_lock_client(struct client *c) tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_E3)); c->flags |= CLIENT_SUSPENDED; - server_write_client(c, MSG_LOCK, cmd, strlen(cmd) + 1); + proc_send_s(c->peer, MSG_LOCK, cmd); } void @@ -268,7 +205,7 @@ server_kill_window(struct window *w) s = next_s; next_s = RB_NEXT(sessions, &sessions, s); - if (session_has(s, w) == NULL) + if (!session_has(s, w)) continue; server_unzoom_window(w); while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) { @@ -279,7 +216,7 @@ server_kill_window(struct window *w) server_redraw_session_group(s); } - if (options_get_number(&s->options, "renumber-windows")) { + if (options_get_number(s->options, "renumber-windows")) { if ((sg = session_group_find(s)) != NULL) { TAILQ_FOREACH(target_s, &sg->sessions, gentry) session_renumber_windows(target_s); @@ -332,7 +269,7 @@ server_link_window(struct session *src, struct winlink *srcwl, } if (dstidx == -1) - dstidx = -1 - options_get_number(&dst->options, "base-index"); + dstidx = -1 - options_get_number(dst->options, "base-index"); dstwl = session_attach(dst, srcwl->window, dstidx, cause); if (dstwl == NULL) return (-1); @@ -354,12 +291,13 @@ server_unlink_window(struct session *s, struct winlink *wl) } void -server_destroy_pane(struct window_pane *wp) +server_destroy_pane(struct window_pane *wp, int hooks) { struct window *w = wp->window; int old_fd; struct screen_write_ctx ctx; struct grid_cell gc; + struct cmd_find_state fs; old_fd = wp->fd; if (wp->fd != -1) { @@ -371,7 +309,7 @@ server_destroy_pane(struct window_pane *wp) wp->fd = -1; } - if (options_get_number(&w->options, "remain-on-exit")) { + if (options_get_number(w->options, "remain-on-exit")) { if (old_fd == -1) return; screen_write_start(&ctx, wp, &wp->base); @@ -383,6 +321,9 @@ server_destroy_pane(struct window_pane *wp) screen_write_puts(&ctx, &gc, "Pane is dead"); screen_write_stop(&ctx); wp->flags |= PANE_REDRAW; + + if (hooks && cmd_find_from_pane(&fs, wp) == 0) + hooks_run(hooks_get(fs.s), NULL, &fs, "pane-died"); return; } @@ -390,6 +331,9 @@ server_destroy_pane(struct window_pane *wp) layout_close_pane(wp); window_remove_pane(w, wp); + if (hooks && cmd_find_from_window(&fs, w) == 0) + hooks_run(hooks_get(fs.s), NULL, &fs, "pane-exited"); + if (TAILQ_EMPTY(&w->panes)) server_kill_window(w); else @@ -433,16 +377,14 @@ server_destroy_session(struct session *s) { struct client *c; struct session *s_new; - u_int i; - if (!options_get_number(&s->options, "detach-on-destroy")) + if (!options_get_number(s->options, "detach-on-destroy")) s_new = server_next_session(s); else s_new = NULL; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session != s) + TAILQ_FOREACH(c, &clients, entry) { + if (c->session != s) continue; if (s_new == NULL) { c->session = NULL; @@ -450,9 +392,13 @@ server_destroy_session(struct session *s) } else { c->last_session = NULL; c->session = s_new; + server_client_set_key_table(c, NULL); + status_timer_start(c); notify_attached_session_changed(c); - session_update_activity(s_new); + session_update_activity(s_new, NULL); + gettimeofday(&s_new->last_attached_time, NULL); server_redraw_client(c); + alerts_check_session(s_new); } } recalculate_sizes(); @@ -470,7 +416,7 @@ server_check_unattached(void) RB_FOREACH(s, sessions, &sessions) { if (!(s->flags & SESSION_UNATTACHED)) continue; - if (options_get_number (&s->options, "destroy-unattached")) + if (options_get_number (s->options, "destroy-unattached")) session_destroy(s); } } @@ -481,7 +427,7 @@ server_set_identify(struct client *c) struct timeval tv; int delay; - delay = options_get_number(&c->session->options, "display-panes-time"); + delay = options_get_number(c->session->options, "display-panes-time"); tv.tv_sec = delay / 1000; tv.tv_usec = (delay % 1000) * 1000L; @@ -506,73 +452,13 @@ server_clear_identify(struct client *c) } void -server_callback_identify(unused int fd, unused short events, void *data) +server_callback_identify(__unused int fd, __unused short events, void *data) { struct client *c = data; server_clear_identify(c); } -void -server_update_event(struct client *c) -{ - short events; - - events = 0; - if (!(c->flags & CLIENT_BAD)) - events |= EV_READ; - if (c->ibuf.w.queued > 0) - events |= EV_WRITE; - if (event_initialized(&c->event)) - event_del(&c->event); - event_set(&c->event, c->ibuf.fd, events, server_client_callback, c); - event_add(&c->event, NULL); -} - -/* Push stdout to client if possible. */ -void -server_push_stdout(struct client *c) -{ - struct msg_stdout_data data; - size_t size; - - size = EVBUFFER_LENGTH(c->stdout_data); - if (size == 0) - return; - if (size > sizeof data.data) - size = sizeof data.data; - - memcpy(data.data, EVBUFFER_DATA(c->stdout_data), size); - data.size = size; - - if (server_write_client(c, MSG_STDOUT, &data, sizeof data) == 0) - evbuffer_drain(c->stdout_data, size); -} - -/* Push stderr to client if possible. */ -void -server_push_stderr(struct client *c) -{ - struct msg_stderr_data data; - size_t size; - - if (c->stderr_data == c->stdout_data) { - server_push_stdout(c); - return; - } - size = EVBUFFER_LENGTH(c->stderr_data); - if (size == 0) - return; - if (size > sizeof data.data) - size = sizeof data.data; - - memcpy(data.data, EVBUFFER_DATA(c->stderr_data), size); - data.size = size; - - if (server_write_client(c, MSG_STDERR, &data, sizeof data) == 0) - evbuffer_drain(c->stderr_data, size); -} - /* Set stdin callback. */ int server_set_stdin_callback(struct client *c, void (*cb)(struct client *, int, @@ -599,7 +485,7 @@ server_set_stdin_callback(struct client *c, void (*cb)(struct client *, int, if (c->stdin_closed) c->stdin_callback(c, 1, c->stdin_callback_data); - server_write_client(c, MSG_STDIN, NULL, 0); + proc_send(c->peer, MSG_STDIN, -1, NULL, 0); return (0); } @@ -607,7 +493,8 @@ server_set_stdin_callback(struct client *c, void (*cb)(struct client *, int, void server_unzoom_window(struct window *w) { - window_unzoom(w); - server_redraw_window(w); - server_status_window(w); + if (window_unzoom(w) == 0) { + server_redraw_window(w); + server_status_window(w); + } } diff --git a/server-window.c b/server-window.c deleted file mode 100644 index a14c3150..00000000 --- a/server-window.c +++ /dev/null @@ -1,197 +0,0 @@ -/* $OpenBSD$ */ - -/* - * Copyright (c) 2009 Nicholas Marriott - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER - * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING - * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include - -#include -#include -#include - -#include "tmux.h" - -int server_window_check_bell(struct session *, struct winlink *); -int server_window_check_activity(struct session *, struct winlink *); -int server_window_check_silence(struct session *, struct winlink *); -void ring_bell(struct session *); - -/* Window functions that need to happen every loop. */ -void -server_window_loop(void) -{ - struct window *w; - struct winlink *wl; - struct session *s; - u_int i; - - for (i = 0; i < ARRAY_LENGTH(&windows); i++) { - w = ARRAY_ITEM(&windows, i); - if (w == NULL) - continue; - - RB_FOREACH(s, sessions, &sessions) { - wl = session_has(s, w); - if (wl == NULL) - continue; - - if (server_window_check_bell(s, wl) || - server_window_check_activity(s, wl) || - server_window_check_silence(s, wl)) - server_status_session(s); - } - } -} - -/* Check for bell in window. */ -int -server_window_check_bell(struct session *s, struct winlink *wl) -{ - struct client *c; - struct window *w = wl->window; - u_int i; - int action, visual; - - if (!(w->flags & WINDOW_BELL) || wl->flags & WINLINK_BELL) - return (0); - if (s->curw != wl || s->flags & SESSION_UNATTACHED) - wl->flags |= WINLINK_BELL; - if (s->flags & SESSION_UNATTACHED) - return (0); - if (s->curw->window == w) - w->flags &= ~WINDOW_BELL; - - visual = options_get_number(&s->options, "visual-bell"); - action = options_get_number(&s->options, "bell-action"); - if (action == BELL_NONE) - return (0); - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session != s || c->flags & CLIENT_CONTROL) - continue; - if (!visual) { - if (c->session->curw->window == w || action == BELL_ANY) - tty_bell(&c->tty); - continue; - } - if (c->session->curw->window == w) - status_message_set(c, "Bell in current window"); - else if (action == BELL_ANY) - status_message_set(c, "Bell in window %u", wl->idx); - } - - return (1); -} - -/* Check for activity in window. */ -int -server_window_check_activity(struct session *s, struct winlink *wl) -{ - struct client *c; - struct window *w = wl->window; - u_int i; - - if (s->curw->window == w) - w->flags &= ~WINDOW_ACTIVITY; - - if (!(w->flags & WINDOW_ACTIVITY) || wl->flags & WINLINK_ACTIVITY) - return (0); - if (s->curw == wl && !(s->flags & SESSION_UNATTACHED)) - return (0); - - if (!options_get_number(&w->options, "monitor-activity")) - return (0); - - if (options_get_number(&s->options, "bell-on-alert")) - ring_bell(s); - wl->flags |= WINLINK_ACTIVITY; - - if (options_get_number(&s->options, "visual-activity")) { - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session != s) - continue; - status_message_set(c, "Activity in window %u", wl->idx); - } - } - - return (1); -} - -/* Check for silence in window. */ -int -server_window_check_silence(struct session *s, struct winlink *wl) -{ - struct client *c; - struct window *w = wl->window; - struct timeval timer; - u_int i; - int silence_interval, timer_difference; - - if (!(w->flags & WINDOW_SILENCE) || wl->flags & WINLINK_SILENCE) - return (0); - - if (s->curw == wl && !(s->flags & SESSION_UNATTACHED)) { - /* - * Reset the timer for this window if we've focused it. We - * don't want the timer tripping as soon as we've switched away - * from this window. - */ - if (gettimeofday(&w->silence_timer, NULL) != 0) - fatal("gettimeofday failed."); - - return (0); - } - - silence_interval = options_get_number(&w->options, "monitor-silence"); - if (silence_interval == 0) - return (0); - - if (gettimeofday(&timer, NULL) != 0) - fatal("gettimeofday"); - timer_difference = timer.tv_sec - w->silence_timer.tv_sec; - if (timer_difference <= silence_interval) - return (0); - - if (options_get_number(&s->options, "bell-on-alert")) - ring_bell(s); - wl->flags |= WINLINK_SILENCE; - - if (options_get_number(&s->options, "visual-silence")) { - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session != s) - continue; - status_message_set(c, "Silence in window %u", wl->idx); - } - } - - return (1); -} - -/* Ring terminal bell. */ -void -ring_bell(struct session *s) -{ - struct client *c; - u_int i; - - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c != NULL && c->session == s && !(c->flags & CLIENT_CONTROL)) - tty_bell(&c->tty); - } -} diff --git a/server.c b/server.c index 9b11e019..05706769 100644 --- a/server.c +++ b/server.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -30,39 +30,73 @@ #include #include #include -#include #include #include #include #include "tmux.h" +#include "tmate.h" /* * Main server functions. */ -/* Client list. */ -struct clients clients; -struct clients dead_clients; +struct clients clients; -int server_fd; -int server_shutdown; -struct event server_ev_accept; -struct event server_ev_second; +struct tmuxproc *server_proc; +int server_fd; +int server_exit; +struct event server_ev_accept; -int server_create_socket(void); -void server_loop(void); -int server_should_shutdown(void); -void server_send_shutdown(void); -void server_clean_dead(void); -void server_accept_callback(int, short, void *); -void server_signal_callback(int, short, void *); -void server_child_signal(void); -void server_child_exited(pid_t, int); -void server_child_stopped(pid_t, int); -void server_second_callback(int, short, void *); -void server_lock_server(void); -void server_lock_sessions(void); +struct cmd_find_state marked_pane; + +int server_create_socket(void); +int server_loop(void); +int server_should_exit(void); +void server_send_exit(void); +void server_accept(int, short, void *); +void server_signal(int); +void server_child_signal(void); +void server_child_exited(pid_t, int); +void server_child_stopped(pid_t, int); + +/* Set marked pane. */ +void +server_set_marked(struct session *s, struct winlink *wl, struct window_pane *wp) +{ + cmd_find_clear_state(&marked_pane, NULL, 0); + marked_pane.s = s; + marked_pane.wl = wl; + marked_pane.w = wl->window; + marked_pane.wp = wp; +} + +/* Clear marked pane. */ +void +server_clear_marked(void) +{ + cmd_find_clear_state(&marked_pane, NULL, 0); +} + +/* Is this the marked pane? */ +int +server_is_marked(struct session *s, struct winlink *wl, struct window_pane *wp) +{ + if (s == NULL || wl == NULL || wp == NULL) + return (0); + if (marked_pane.s != s || marked_pane.wl != wl) + return (0); + if (marked_pane.wp != wp) + return (0); + return (server_check_marked()); +} + +/* Check if the marked pane is still valid. */ +int +server_check_marked(void) +{ + return (cmd_find_valid_state(&marked_pane)); +} /* Create server socket. */ int @@ -78,150 +112,131 @@ server_create_socket(void) size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path); if (size >= sizeof sa.sun_path) { errno = ENAMETOOLONG; - fatal("socket failed"); + return (-1); } unlink(sa.sun_path); if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) - fatal("socket failed"); + return (-1); mask = umask(S_IXUSR|S_IXGRP|S_IRWXO); - if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) - fatal("bind failed"); + if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) == -1) + return (-1); umask(mask); if (listen(fd, 16) == -1) - fatal("listen failed"); + return (-1); setblocking(fd, 0); - server_update_socket(); - return (fd); } +#ifdef TMATE +static void tmate_set_editor_mode(void) +{ + switch (options_get_number(global_s_options, "status-keys")) { + case MODEKEY_EMACS: tmate_exec_cmd_args(4, (const char *[]){"set-option", "-g", "status-keys", "emacs"}); break; + case MODEKEY_VI: tmate_exec_cmd_args(4, (const char *[]){"set-option", "-g", "status-keys", "vi"}); break; + } + + switch (options_get_number(global_w_options, "mode-keys")) { + case MODEKEY_EMACS: tmate_exec_cmd_args(4, (const char *[]){"set-window-option", "-g", "status-keys", "emacs"}); break; + case MODEKEY_VI: tmate_exec_cmd_args(4, (const char *[]){"set-window-option", "-g", "status-keys", "vi"}); break; + } +} +#endif + /* Fork new server. */ int -server_start(int lockfd, char *lockfile) +server_start(struct event_base *base, int lockfd, char *lockfile) { - int pair[2]; - struct timeval tv; - char *cause; + int pair[2]; - /* The first client is special and gets a socketpair; create it. */ - if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0) - fatal("socketpair failed"); - log_debug("starting server"); + if (!tmate_foreground) + if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0) + fatal("socketpair failed"); - switch (fork()) { - case -1: - fatal("fork failed"); - case 0: - break; - default: + server_proc = proc_start("server", base, 1, server_signal); + if (server_proc == NULL) { close(pair[1]); return (pair[0]); } + close(pair[0]); - /* - * Must daemonise before loading configuration as the PID changes so - * $TMUX would be wrong for sessions created in the config file. - */ - if (daemon(1, 0) != 0) - fatal("daemon failed"); + if (log_get_level() > 3) + tty_create_log(); - /* event_init() was called in our parent, need to reinit. */ - if (event_reinit(ev_base) != 0) - fatal("event_reinit failed"); - clear_signals(0); - - logfile("server"); - log_debug("server started, pid %ld", (long) getpid()); - - ARRAY_INIT(&windows); - RB_INIT(&all_window_panes); - ARRAY_INIT(&clients); - ARRAY_INIT(&dead_clients); - RB_INIT(&sessions); - RB_INIT(&dead_sessions); - TAILQ_INIT(&session_groups); - mode_key_init_trees(); - key_bindings_init(); - utf8_build(); - - start_time = time(NULL); - log_debug("socket path %s", socket_path); -#ifdef HAVE_SETPROCTITLE - setproctitle("server (%s)", socket_path); +#ifdef __OpenBSD__ + if (pledge("stdio rpath wpath cpath fattr unix getpw recvfd proc exec " + "tty ps", NULL) != 0) + fatal("pledge failed"); #endif + RB_INIT(&windows); + RB_INIT(&all_window_panes); + TAILQ_INIT(&clients); + RB_INIT(&sessions); + TAILQ_INIT(&session_groups); + mode_key_init_trees(); + +#ifdef TMATE + tmate_session_init(base); +#endif + + key_bindings_init(); + + gettimeofday(&start_time, NULL); + server_fd = server_create_socket(); - server_client_create(pair[1]); + if (server_fd == -1) + fatal("couldn't create socket"); + server_update_socket(); + if (!tmate_foreground) + server_client_create(pair[1]); - unlink(lockfile); - free(lockfile); - close(lockfd); - - cfg_cmd_q = cmdq_new(NULL); - cfg_cmd_q->emptyfn = cfg_default_done; - cfg_finished = 0; - cfg_references = 1; - cfg_client = ARRAY_FIRST(&clients); - if (cfg_client != NULL) - cfg_client->references++; - - if (access(TMUX_CONF, R_OK) == 0) { - if (load_cfg(TMUX_CONF, cfg_cmd_q, &cause) == -1) - cfg_add_cause("%s: %s", TMUX_CONF, cause); - } else if (errno != ENOENT) - cfg_add_cause("%s: %s", TMUX_CONF, strerror(errno)); - if (cfg_file != NULL) { - if (load_cfg(cfg_file, cfg_cmd_q, &cause) == -1) - cfg_add_cause("%s: %s", cfg_file, cause); + if (lockfd >= 0) { + unlink(lockfile); + free(lockfile); + close(lockfd); } - cmdq_continue(cfg_cmd_q); + +#ifdef TMATE + tmate_set_editor_mode(); +#endif + start_cfg(); + + status_prompt_load_history(); server_add_accept(0); - memset(&tv, 0, sizeof tv); - tv.tv_sec = 1; - evtimer_set(&server_ev_second, server_second_callback, NULL); - evtimer_add(&server_ev_second, &tv); + if (tmate_foreground) + run_initial_client_cmd(); + + proc_loop(server_proc, server_loop); + status_prompt_save_history(); +#ifdef TMATE + unlink(socket_path); +#endif - set_signals(server_signal_callback); - server_loop(); exit(0); } -/* Main server loop. */ -void +/* Server loop callback. */ +int server_loop(void) -{ - while (!server_should_shutdown()) { - event_loop(EVLOOP_ONCE); - - server_window_loop(); - server_client_loop(); - - server_clean_dead(); - } -} - -/* Check if the server should exit (no more clients or sessions). */ -int -server_should_shutdown(void) { struct client *c; - u_int i; - if (!options_get_number(&global_options, "exit-unattached")) { + server_client_loop(); + + if (!options_get_number(global_options, "exit-unattached")) { if (!RB_EMPTY(&sessions)) return (0); } - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c != NULL && c->session != NULL) + TAILQ_FOREACH(c, &clients, entry) { + if (c->session != NULL) return (0); } @@ -230,69 +245,31 @@ server_should_shutdown(void) * clients but don't actually exit until they've gone. */ cmd_wait_for_flush(); - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - if (ARRAY_ITEM(&clients, i) != NULL) - return (0); - } + if (!TAILQ_EMPTY(&clients)) + return (0); return (1); } -/* Shutdown the server by killing all clients and windows. */ +/* Exit the server by killing all clients and windows. */ void -server_send_shutdown(void) +server_send_exit(void) { - struct client *c; - struct session *s, *next_s; - u_int i; + struct client *c, *c1; + struct session *s, *s1; cmd_wait_for_flush(); - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c != NULL) { - if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED)) - server_client_lost(c); - else - server_write_client(c, MSG_SHUTDOWN, NULL, 0); - c->session = NULL; - } + TAILQ_FOREACH_SAFE(c, &clients, entry, c1) { + if (c->flags & CLIENT_SUSPENDED) + server_client_lost(c); + else + proc_send(c->peer, MSG_SHUTDOWN, -1, NULL, 0); + c->session = NULL; } - s = RB_MIN(sessions, &sessions); - while (s != NULL) { - next_s = RB_NEXT(sessions, &sessions, s); + RB_FOREACH_SAFE(s, sessions, &sessions, s1) session_destroy(s); - s = next_s; - } -} - -/* Free dead, unreferenced clients and sessions. */ -void -server_clean_dead(void) -{ - struct session *s, *next_s; - struct client *c; - u_int i; - - s = RB_MIN(sessions, &dead_sessions); - while (s != NULL) { - next_s = RB_NEXT(sessions, &dead_sessions, s); - if (s->references == 0) { - RB_REMOVE(sessions, &dead_sessions, s); - free(s->name); - free(s); - } - s = next_s; - } - - for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) { - c = ARRAY_ITEM(&dead_clients, i); - if (c == NULL || c->references != 0) - continue; - ARRAY_SET(&dead_clients, i, NULL); - free(c); - } } /* Update socket execute permissions based on whether sessions are attached. */ @@ -333,7 +310,7 @@ server_update_socket(void) /* Callback for server socket. */ void -server_accept_callback(int fd, short events, unused void *data) +server_accept(int fd, short events, __unused void *data) { struct sockaddr_storage sa; socklen_t slen = sizeof sa; @@ -354,7 +331,7 @@ server_accept_callback(int fd, short events, unused void *data) } fatal("accept failed"); } - if (server_shutdown) { + if (server_exit) { close(newfd); return; } @@ -374,32 +351,39 @@ server_add_accept(int timeout) event_del(&server_ev_accept); if (timeout == 0) { - event_set(&server_ev_accept, - server_fd, EV_READ, server_accept_callback, NULL); + event_set(&server_ev_accept, server_fd, EV_READ, server_accept, + NULL); event_add(&server_ev_accept, NULL); } else { - event_set(&server_ev_accept, - server_fd, EV_TIMEOUT, server_accept_callback, NULL); + event_set(&server_ev_accept, server_fd, EV_TIMEOUT, + server_accept, NULL); event_add(&server_ev_accept, &tv); } } /* Signal handler. */ void -server_signal_callback(int sig, unused short events, unused void *data) +server_signal(int sig) { + int fd; + switch (sig) { + case SIGINT: case SIGTERM: - server_shutdown = 1; - server_send_shutdown(); + server_exit = 1; + server_send_exit(); break; case SIGCHLD: server_child_signal(); break; case SIGUSR1: event_del(&server_ev_accept); - close(server_fd); - server_fd = server_create_socket(); + fd = server_create_socket(); + if (fd != -1) { + close(server_fd); + server_fd = fd; + server_update_socket(); + } server_add_accept(0); break; } @@ -432,18 +416,15 @@ server_child_signal(void) void server_child_exited(pid_t pid, int status) { - struct window *w; + struct window *w, *w1; struct window_pane *wp; struct job *job; - u_int i; - for (i = 0; i < ARRAY_LENGTH(&windows); i++) { - if ((w = ARRAY_ITEM(&windows, i)) == NULL) - continue; + RB_FOREACH_SAFE(w, windows, &windows, w1) { TAILQ_FOREACH(wp, &w->panes, entry) { if (wp->pid == pid) { wp->status = status; - server_destroy_pane(wp); + server_destroy_pane(wp, 1); break; } } @@ -463,14 +444,11 @@ server_child_stopped(pid_t pid, int status) { struct window *w; struct window_pane *wp; - u_int i; if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU) return; - for (i = 0; i < ARRAY_LENGTH(&windows); i++) { - if ((w = ARRAY_ITEM(&windows, i)) == NULL) - continue; + RB_FOREACH(w, windows, &windows) { TAILQ_FOREACH(wp, &w->panes, entry) { if (wp->pid == pid) { if (killpg(pid, SIGCONT) != 0) @@ -479,77 +457,3 @@ server_child_stopped(pid_t pid, int status) } } } - -/* Handle once-per-second timer events. */ -void -server_second_callback(unused int fd, unused short events, unused void *arg) -{ - struct window *w; - struct window_pane *wp; - struct timeval tv; - u_int i; - - if (options_get_number(&global_s_options, "lock-server")) - server_lock_server(); - else - server_lock_sessions(); - - for (i = 0; i < ARRAY_LENGTH(&windows); i++) { - w = ARRAY_ITEM(&windows, i); - if (w == NULL) - continue; - - TAILQ_FOREACH(wp, &w->panes, entry) { - if (wp->mode != NULL && wp->mode->timer != NULL) - wp->mode->timer(wp); - } - } - - server_client_status_timer(); - - evtimer_del(&server_ev_second); - memset(&tv, 0, sizeof tv); - tv.tv_sec = 1; - evtimer_add(&server_ev_second, &tv); -} - -/* Lock the server if ALL sessions have hit the time limit. */ -void -server_lock_server(void) -{ - struct session *s; - int timeout; - time_t t; - - t = time(NULL); - RB_FOREACH(s, sessions, &sessions) { - if (s->flags & SESSION_UNATTACHED) - continue; - timeout = options_get_number(&s->options, "lock-after-time"); - if (timeout <= 0 || t <= s->activity_time.tv_sec + timeout) - return; /* not timed out */ - } - - server_lock(); - recalculate_sizes(); -} - -/* Lock any sessions which have timed out. */ -void -server_lock_sessions(void) -{ - struct session *s; - int timeout; - time_t t; - - t = time(NULL); - RB_FOREACH(s, sessions, &sessions) { - if (s->flags & SESSION_UNATTACHED) - continue; - timeout = options_get_number(&s->options, "lock-after-time"); - if (timeout > 0 && t > s->activity_time.tv_sec + timeout) { - server_lock_session(s); - recalculate_sizes(); - } - } -} diff --git a/session.c b/session.c index 3ac9fb2b..879eb657 100644 --- a/session.c +++ b/session.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -25,13 +25,16 @@ #include #include "tmux.h" +#include "tmate.h" -/* Global session list. */ struct sessions sessions; -struct sessions dead_sessions; u_int next_session_id; struct session_groups session_groups; +void session_free(int, short, void *); + +void session_lock_timer(int, short, void *); + struct winlink *session_next_alert(struct winlink *); struct winlink *session_previous_alert(struct winlink *); @@ -69,6 +72,22 @@ session_find(const char *name) return (RB_FIND(sessions, &sessions, &s)); } +/* Find session by id parsed from a string. */ +struct session * +session_find_by_id_str(const char *s) +{ + const char *errstr; + u_int id; + + if (*s != '$') + return (NULL); + + id = strtonum(s + 1, 0, UINT_MAX, &errstr); + if (errstr != NULL) + return (NULL); + return (session_find_by_id(id)); +} + /* Find session by id. */ struct session * session_find_by_id(u_int id) @@ -85,30 +104,35 @@ session_find_by_id(u_int id) /* Create a new session. */ struct session * session_create(const char *name, int argc, char **argv, const char *path, - int cwd, struct environ *env, struct termios *tio, int idx, u_int sx, - u_int sy, char **cause) + const char *cwd, struct environ *env, struct termios *tio, int idx, + u_int sx, u_int sy, char **cause) { struct session *s; struct winlink *wl; - s = xmalloc(sizeof *s); - s->references = 0; +#ifdef TMATE + if (next_session_id != 0) { + xasprintf(cause, "multi sessions is not supported with tmate"); + return NULL; + } +#endif + + s = xcalloc(1, sizeof *s); + s->references = 1; s->flags = 0; - if (gettimeofday(&s->creation_time, NULL) != 0) - fatal("gettimeofday failed"); - session_update_activity(s); - - s->cwd = dup(cwd); + s->cwd = xstrdup(cwd); s->curw = NULL; TAILQ_INIT(&s->lastw); RB_INIT(&s->windows); - options_init(&s->options, &global_s_options); - environ_init(&s->environ); + s->environ = environ_create(); if (env != NULL) - environ_copy(env, &s->environ); + environ_copy(env, s->environ); + + s->options = options_create(global_s_options); + s->hooks = hooks_create(global_hooks); s->tio = NULL; if (tio != NULL) { @@ -132,6 +156,12 @@ session_create(const char *name, int argc, char **argv, const char *path, } RB_INSERT(sessions, &sessions, s); + log_debug("new session %s $%u", s->name, s->id); + + if (gettimeofday(&s->creation_time, NULL) != 0) + fatal("gettimeofday failed"); + session_update_activity(s, &s->creation_time); + if (argc >= 0) { wl = session_new(s, NULL, argc, argv, path, cwd, idx, cause); if (wl == NULL) { @@ -147,6 +177,66 @@ session_create(const char *name, int argc, char **argv, const char *path, return (s); } +/* Remove a reference from a session. */ +void +session_unref(struct session *s) +{ + log_debug("session %s has %d references", s->name, s->references); + + s->references--; + if (s->references == 0) + event_once(-1, EV_TIMEOUT, session_free, s, NULL); +} + +/* Free session. */ +void +session_free(__unused int fd, __unused short events, void *arg) +{ + struct session *s = arg; + + log_debug("session %s freed (%d references)", s->name, s->references); + + if (s->references == 0) { + environ_free(s->environ); + + options_free(s->options); + hooks_free(s->hooks); + + free(s->name); + free(s); + } +} + +static void maybe_restart_session(void) +{ + int fg_restart = options_get_number(global_options, "tmate-foreground-restart"); + if (!fg_restart) + return; + + /* + * throttle restarts. This is a blocking sleep. It's + * simpler than using a timer, but fairly harmless + * from a blocking perspective. + */ + usleep(500*1000); + next_session_id = 0; + run_initial_client_cmd(); + + tmate_info("Session shell restarted"); + + struct session *s; + s = RB_MIN(sessions, &sessions); + if (!s) + return; + + struct window_pane *wp; + wp = s->curw->window->active; + window_pane_set_mode(wp, &window_copy_mode); + window_copy_init_for_output(wp); + window_copy_add(wp, "%s", "Session shell restarted"); + window_copy_add(wp, "%s", "Note: press the following sequence to disconnect from SSH: ~."); +} + /* Destroy a session. */ void session_destroy(struct session *s) @@ -160,9 +250,10 @@ session_destroy(struct session *s) free(s->tio); + if (event_initialized(&s->lock_timer)) + event_del(&s->lock_timer); + session_group_remove(s); - environ_free(&s->environ); - options_free(&s->options); while (!TAILQ_EMPTY(&s->lastw)) winlink_stack_remove(&s->lastw, TAILQ_FIRST(&s->lastw)); @@ -172,9 +263,18 @@ session_destroy(struct session *s) winlink_remove(&s->windows, wl); } - close(s->cwd); + free((void *)s->cwd); - RB_INSERT(sessions, &dead_sessions, s); + session_unref(s); + +#ifdef TMATE + if (tmate_foreground && !server_exit) { + maybe_restart_session(); + } else { + tmate_info("Session closed"); + tmate_write_fin(); + } +#endif } /* Check a session name is valid: not empty and no colons or periods. */ @@ -184,12 +284,50 @@ session_check_name(const char *name) return (*name != '\0' && name[strcspn(name, ":.")] == '\0'); } -/* Update session active time. */ +/* Lock session if it has timed out. */ void -session_update_activity(struct session *s) +session_lock_timer(__unused int fd, __unused short events, void *arg) { - if (gettimeofday(&s->activity_time, NULL) != 0) - fatal("gettimeofday"); + struct session *s = arg; + + if (s->flags & SESSION_UNATTACHED) + return; + + log_debug("session %s locked, activity time %lld", s->name, + (long long)s->activity_time.tv_sec); + + server_lock_session(s); + recalculate_sizes(); +} + +/* Update activity time. */ +void +session_update_activity(struct session *s, struct timeval *from) +{ + struct timeval *last = &s->last_activity_time; + struct timeval tv; + + memcpy(last, &s->activity_time, sizeof *last); + if (from == NULL) + gettimeofday(&s->activity_time, NULL); + else + memcpy(&s->activity_time, from, sizeof s->activity_time); + + log_debug("session %s activity %lld.%06d (last %lld.%06d)", s->name, + (long long)s->activity_time.tv_sec, (int)s->activity_time.tv_usec, + (long long)last->tv_sec, (int)last->tv_usec); + + if (evtimer_initialized(&s->lock_timer)) + evtimer_del(&s->lock_timer); + else + evtimer_set(&s->lock_timer, session_lock_timer, s); + + if (~s->flags & SESSION_UNATTACHED) { + timerclear(&tv); + tv.tv_sec = options_get_number(s->options, "lock-after-time"); + if (tv.tv_sec != 0) + evtimer_add(&s->lock_timer, &tv); + } } /* Find the next usable session. */ @@ -229,11 +367,11 @@ session_previous_session(struct session *s) /* Create a new window on a session. */ struct winlink * session_new(struct session *s, const char *name, int argc, char **argv, - const char *path, int cwd, int idx, char **cause) + const char *path, const char *cwd, int idx, char **cause) { struct window *w; struct winlink *wl; - struct environ env; + struct environ *env; const char *shell; u_int hlimit; @@ -242,29 +380,29 @@ session_new(struct session *s, const char *name, int argc, char **argv, return (NULL); } - environ_init(&env); - environ_copy(&global_environ, &env); - environ_copy(&s->environ, &env); - server_fill_environ(s, &env); + env = environ_create(); + environ_copy(global_environ, env); + environ_copy(s->environ, env); + server_fill_environ(s, env); - shell = options_get_string(&s->options, "default-shell"); + shell = options_get_string(s->options, "default-shell"); if (*shell == '\0' || areshell(shell)) shell = _PATH_BSHELL; - hlimit = options_get_number(&s->options, "history-limit"); - w = window_create(name, argc, argv, path, shell, cwd, &env, s->tio, + hlimit = options_get_number(s->options, "history-limit"); + w = window_create(name, argc, argv, path, shell, cwd, env, s->tio, s->sx, s->sy, hlimit, cause); if (w == NULL) { winlink_remove(&s->windows, wl); - environ_free(&env); + environ_free(env); return (NULL); } winlink_set_window(wl, w); notify_window_linked(s, w); - environ_free(&env); + environ_free(env); - if (options_get_number(&s->options, "set-remain-on-exit")) - options_set_number(&w->options, "remain-on-exit", 1); + if (options_get_number(s->options, "set-remain-on-exit")) + options_set_number(w->options, "remain-on-exit", 1); session_group_synchronize_from(s); return (wl); @@ -283,6 +421,10 @@ session_attach(struct session *s, struct window *w, int idx, char **cause) winlink_set_window(wl, w); notify_window_linked(s, w); +#ifdef TMATE + tmate_sync_layout(); +#endif + session_group_synchronize_from(s); return (wl); } @@ -299,6 +441,11 @@ session_detach(struct session *s, struct winlink *wl) notify_window_unlinked(s, wl->window); winlink_stack_remove(&s->lastw, wl); winlink_remove(&s->windows, wl); + +#ifdef TMATE + tmate_sync_layout(); +#endif + session_group_synchronize_from(s); if (RB_EMPTY(&s->windows)) { session_destroy(s); @@ -308,16 +455,30 @@ session_detach(struct session *s, struct winlink *wl) } /* Return if session has window. */ -struct winlink * +int session_has(struct session *s, struct window *w) { struct winlink *wl; RB_FOREACH(wl, winlinks, &s->windows) { if (wl->window == w) - return (wl); + return (1); } - return (NULL); + return (0); +} + +/* + * Return 1 if a window is linked outside this session (not including session + * groups). The window must be in this session! + */ +int +session_is_linked(struct session *s, struct window *w) +{ + struct session_group *sg; + + if ((sg = session_group_find(s)) != NULL) + return (w->references != session_group_count(sg)); + return (w->references != 1); } struct winlink * @@ -420,6 +581,12 @@ session_set_current(struct session *s, struct winlink *wl) winlink_stack_push(&s->lastw, s->curw); s->curw = wl; winlink_clear_flags(wl); + +#ifdef TMATE + tmate_sync_layout(); +#endif + + window_update_activity(wl->window); return (0); } @@ -454,6 +621,7 @@ session_group_index(struct session_group *sg) } fatalx("session group not found"); + for(;;); } /* @@ -612,7 +780,7 @@ session_renumber_windows(struct session *s) RB_INIT(&s->windows); /* Start renumbering from the base-index if it's set. */ - new_idx = options_get_number(&s->options, "base-index"); + new_idx = options_get_number(s->options, "base-index"); new_curw_idx = 0; /* Go through the winlinks and assign new indexes. */ diff --git a/signal.c b/signal.c index 7e6268a7..f20a0257 100644 --- a/signal.c +++ b/signal.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * Copyright (c) 2010 Romain Francoise * * Permission to use, copy, modify, and distribute this software for any @@ -24,6 +24,7 @@ #include "tmux.h" +struct event ev_sigint; struct event ev_sighup; struct event ev_sigchld; struct event ev_sigcont; @@ -32,7 +33,7 @@ struct event ev_sigusr1; struct event ev_sigwinch; void -set_signals(void(*handler)(int, short, unused void *)) +set_signals(void (*handler)(int, short, void *), void *arg) { struct sigaction sigact; @@ -40,26 +41,34 @@ set_signals(void(*handler)(int, short, unused void *)) sigemptyset(&sigact.sa_mask); sigact.sa_flags = SA_RESTART; sigact.sa_handler = SIG_IGN; +#ifndef TMATE if (sigaction(SIGINT, &sigact, NULL) != 0) fatal("sigaction failed"); +#endif if (sigaction(SIGPIPE, &sigact, NULL) != 0) fatal("sigaction failed"); if (sigaction(SIGUSR2, &sigact, NULL) != 0) fatal("sigaction failed"); +#ifndef TMATE if (sigaction(SIGTSTP, &sigact, NULL) != 0) fatal("sigaction failed"); +#endif - signal_set(&ev_sighup, SIGHUP, handler, NULL); +#ifdef TMATE + signal_set(&ev_sigint, SIGINT, handler, arg); + signal_add(&ev_sigint, NULL); +#endif + signal_set(&ev_sighup, SIGHUP, handler, arg); signal_add(&ev_sighup, NULL); - signal_set(&ev_sigchld, SIGCHLD, handler, NULL); + signal_set(&ev_sigchld, SIGCHLD, handler, arg); signal_add(&ev_sigchld, NULL); - signal_set(&ev_sigcont, SIGCONT, handler, NULL); + signal_set(&ev_sigcont, SIGCONT, handler, arg); signal_add(&ev_sigcont, NULL); - signal_set(&ev_sigterm, SIGTERM, handler, NULL); + signal_set(&ev_sigterm, SIGTERM, handler, arg); signal_add(&ev_sigterm, NULL); - signal_set(&ev_sigusr1, SIGUSR1, handler, NULL); + signal_set(&ev_sigusr1, SIGUSR1, handler, arg); signal_add(&ev_sigusr1, NULL); - signal_set(&ev_sigwinch, SIGWINCH, handler, NULL); + signal_set(&ev_sigwinch, SIGWINCH, handler, arg); signal_add(&ev_sigwinch, NULL); } @@ -72,16 +81,24 @@ clear_signals(int after_fork) sigemptyset(&sigact.sa_mask); sigact.sa_flags = SA_RESTART; sigact.sa_handler = SIG_DFL; +#ifndef TMATE if (sigaction(SIGINT, &sigact, NULL) != 0) fatal("sigaction failed"); +#endif if (sigaction(SIGPIPE, &sigact, NULL) != 0) fatal("sigaction failed"); if (sigaction(SIGUSR2, &sigact, NULL) != 0) fatal("sigaction failed"); +#ifndef TMATE if (sigaction(SIGTSTP, &sigact, NULL) != 0) fatal("sigaction failed"); +#endif if (after_fork) { +#ifdef TMATE + if (sigaction(SIGINT, &sigact, NULL) != 0) + fatal("sigaction failed"); +#endif if (sigaction(SIGHUP, &sigact, NULL) != 0) fatal("sigaction failed"); if (sigaction(SIGCHLD, &sigact, NULL) != 0) @@ -95,6 +112,7 @@ clear_signals(int after_fork) if (sigaction(SIGWINCH, &sigact, NULL) != 0) fatal("sigaction failed"); } else { + event_del(&ev_sigint); event_del(&ev_sighup); event_del(&ev_sigchld); event_del(&ev_sigcont); diff --git a/status.c b/status.c index 5f8895fb..3a553945 100644 --- a/status.c +++ b/status.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -28,36 +28,169 @@ #include #include "tmux.h" +#include "tmate.h" -char *status_redraw_get_left(struct client *, time_t, int, struct grid_cell *, +char *status_redraw_get_left(struct client *, time_t, struct grid_cell *, + size_t *); +char *status_redraw_get_right(struct client *, time_t, struct grid_cell *, size_t *); -char *status_redraw_get_right(struct client *, time_t, int, - struct grid_cell *, size_t *); -char *status_find_job(struct client *, char **); -void status_job_free(void *); -void status_job_callback(struct job *); char *status_print(struct client *, struct winlink *, time_t, struct grid_cell *); char *status_replace(struct client *, struct winlink *, const char *, time_t); -void status_replace1(struct client *, char **, char **, char *, size_t); void status_message_callback(int, short, void *); +void status_timer_callback(int, short, void *); const char *status_prompt_up_history(u_int *); const char *status_prompt_down_history(u_int *); void status_prompt_add_history(const char *); -char *status_prompt_complete(const char *); + +const char **status_prompt_complete_list(u_int *, const char *); +char *status_prompt_complete_prefix(const char **, u_int); +char *status_prompt_complete(struct session *, const char *); + +char *status_prompt_find_history_file(void); /* Status prompt history. */ -ARRAY_DECL(, char *) status_prompt_history = ARRAY_INITIALIZER; +#define PROMPT_HISTORY 100 +char **status_prompt_hlist; +u_int status_prompt_hsize; -/* Status output tree. */ -RB_GENERATE(status_out_tree, status_out, entry, status_out_cmp); - -/* Output tree comparison function. */ -int -status_out_cmp(struct status_out *so1, struct status_out *so2) +/* Find the history file to load/save from/to. */ +char * +status_prompt_find_history_file(void) { - return (strcmp(so1->cmd, so2->cmd)); + const char *home, *history_file; + char *path; + + history_file = options_get_string(global_options, "history-file"); + if (*history_file == '\0') + return (NULL); + if (*history_file == '/') + return (xstrdup(history_file)); + + if (history_file[0] != '~' || history_file[1] != '/') + return (NULL); + if ((home = find_home()) == NULL) + return (NULL); + xasprintf(&path, "%s%s", home, history_file + 1); + return (path); +} + +/* Load status prompt history from file. */ +void +status_prompt_load_history(void) +{ + FILE *f; + char *history_file, *line, *tmp; + size_t length; + + if ((history_file = status_prompt_find_history_file()) == NULL) + return; + log_debug("loading history from %s", history_file); + + f = fopen(history_file, "r"); + if (f == NULL) { + log_debug("%s: %s", history_file, strerror(errno)); + free(history_file); + return; + } + free(history_file); + + for (;;) { + if ((line = fgetln(f, &length)) == NULL) + break; + + if (length > 0) { + if (line[length - 1] == '\n') { + line[length - 1] = '\0'; + status_prompt_add_history(line); + } else { + tmp = xmalloc(length + 1); + memcpy(tmp, line, length); + tmp[length] = '\0'; + status_prompt_add_history(tmp); + free(tmp); + } + } + } + fclose(f); +} + +/* Save status prompt history to file. */ +void +status_prompt_save_history(void) +{ + FILE *f; + u_int i; + char *history_file; + + if ((history_file = status_prompt_find_history_file()) == NULL) + return; + log_debug("saving history to %s", history_file); + + f = fopen(history_file, "w"); + if (f == NULL) { + log_debug("%s: %s", history_file, strerror(errno)); + free(history_file); + return; + } + free(history_file); + + for (i = 0; i < status_prompt_hsize; i++) { + fputs(status_prompt_hlist[i], f); + fputc('\n', f); + } + fclose(f); + +} + +/* Status timer callback. */ +void +status_timer_callback(__unused int fd, __unused short events, void *arg) +{ + struct client *c = arg; + struct session *s = c->session; + struct timeval tv; + + evtimer_del(&c->status_timer); + + if (s == NULL) + return; + + if (c->message_string == NULL && c->prompt_string == NULL) + c->flags |= CLIENT_STATUS; + + timerclear(&tv); + tv.tv_sec = options_get_number(s->options, "status-interval"); + + if (tv.tv_sec != 0) + evtimer_add(&c->status_timer, &tv); + log_debug("client %p, status interval %d", c, (int)tv.tv_sec); +} + +/* Start status timer for client. */ +void +status_timer_start(struct client *c) +{ + struct session *s = c->session; + + if (event_initialized(&c->status_timer)) + evtimer_del(&c->status_timer); + else + evtimer_set(&c->status_timer, status_timer_callback, c); + + if (s != NULL && options_get_number(s->options, "status")) + status_timer_callback(-1, 0, c); +} + +/* Start status timer for all clients. */ +void +status_timer_start_all(void) +{ + struct client *c; + + TAILQ_FOREACH(c, &clients, entry) + status_timer_start(c); } /* Get screen line of status line. -1 means off. */ @@ -66,31 +199,31 @@ status_at_line(struct client *c) { struct session *s = c->session; - if (!options_get_number(&s->options, "status")) + if (!options_get_number(s->options, "status")) return (-1); - if (options_get_number(&s->options, "status-position") == 0) + if (options_get_number(s->options, "status-position") == 0) return (0); return (c->tty.sy - 1); } /* Retrieve options for left string. */ char * -status_redraw_get_left(struct client *c, time_t t, int utf8flag, - struct grid_cell *gc, size_t *size) +status_redraw_get_left(struct client *c, time_t t, struct grid_cell *gc, + size_t *size) { struct session *s = c->session; const char *template; char *left; size_t leftlen; - style_apply_update(gc, &s->options, "status-left-style"); + style_apply_update(gc, s->options, "status-left-style"); - template = options_get_string(&s->options, "status-left"); + template = options_get_string(s->options, "status-left"); left = status_replace(c, NULL, template, t); - *size = options_get_number(&s->options, "status-left-length"); - leftlen = screen_write_cstrlen(utf8flag, "%s", left); + *size = options_get_number(s->options, "status-left-length"); + leftlen = screen_write_cstrlen("%s", left); if (leftlen < *size) *size = leftlen; return (left); @@ -98,29 +231,29 @@ status_redraw_get_left(struct client *c, time_t t, int utf8flag, /* Retrieve options for right string. */ char * -status_redraw_get_right(struct client *c, time_t t, int utf8flag, - struct grid_cell *gc, size_t *size) +status_redraw_get_right(struct client *c, time_t t, struct grid_cell *gc, + size_t *size) { struct session *s = c->session; const char *template; char *right; size_t rightlen; - style_apply_update(gc, &s->options, "status-right-style"); + style_apply_update(gc, s->options, "status-right-style"); - template = options_get_string(&s->options, "status-right"); + template = options_get_string(s->options, "status-right"); right = status_replace(c, NULL, template, t); - *size = options_get_number(&s->options, "status-right-length"); - rightlen = screen_write_cstrlen(utf8flag, "%s", right); + *size = options_get_number(s->options, "status-right-length"); + rightlen = screen_write_cstrlen("%s", right); if (rightlen < *size) *size = rightlen; return (right); } -/* Set window at window list position. */ -void -status_set_window_at(struct client *c, u_int x) +/* Get window at window list position. */ +struct window * +status_get_window_at(struct client *c, u_int x) { struct session *s = c->session; struct winlink *wl; @@ -129,13 +262,14 @@ status_set_window_at(struct client *c, u_int x) x += c->wlmouse; RB_FOREACH(wl, winlinks, &s->windows) { - oo = &wl->window->options; - + oo = wl->window->options; len = strlen(options_get_string(oo, "window-status-separator")); - if (x < wl->status_width && session_select(s, wl->idx) == 0) - server_redraw_session(s); + + if (x < wl->status_width) + return (wl->window); x -= wl->status_width + len; } + return (NULL); } /* Draw status for client on the last lines of given context. */ @@ -153,21 +287,22 @@ status_redraw(struct client *c) u_int offset, needed; u_int wlstart, wlwidth, wlavailable, wloffset, wlsize; size_t llen, rlen, seplen; - int larrow, rarrow, utf8flag; + int larrow, rarrow; /* No status line? */ - if (c->tty.sy == 0 || !options_get_number(&s->options, "status")) + if (c->tty.sy == 0 || !options_get_number(s->options, "status")) +#ifdef TMATE + if (c->tty.sy == 0 || !(c->flags & CLIENT_FORCE_STATUS)) +#endif return (1); left = right = NULL; larrow = rarrow = 0; - /* Update status timer. */ - if (gettimeofday(&c->status_timer, NULL) != 0) - fatal("gettimeofday failed"); - t = c->status_timer.tv_sec; + /* Store current time. */ + t = time(NULL); /* Set up default colour. */ - style_apply(&stdgc, &s->options, "status-style"); + style_apply(&stdgc, s->options, "status-style"); /* Create the target screen. */ memcpy(&old_status, &c->status, sizeof old_status); @@ -181,14 +316,15 @@ status_redraw(struct client *c) if (c->tty.sy <= 1) goto out; - /* Get UTF-8 flag. */ - utf8flag = options_get_number(&s->options, "status-utf8"); - /* Work out left and right strings. */ memcpy(&lgc, &stdgc, sizeof lgc); - left = status_redraw_get_left(c, t, utf8flag, &lgc, &llen); + left = status_redraw_get_left(c, t, &lgc, &llen); memcpy(&rgc, &stdgc, sizeof rgc); - right = status_redraw_get_right(c, t, utf8flag, &rgc, &rlen); + right = status_redraw_get_right(c, t, &rgc, &rlen); + +#ifdef TMATE + tmate_status(left, right); +#endif /* * Figure out how much space we have for the window list. If there @@ -209,15 +345,14 @@ status_redraw(struct client *c) free(wl->status_text); memcpy(&wl->status_cell, &stdgc, sizeof wl->status_cell); wl->status_text = status_print(c, wl, t, &wl->status_cell); - wl->status_width = - screen_write_cstrlen(utf8flag, "%s", wl->status_text); + wl->status_width = screen_write_cstrlen("%s", wl->status_text); if (wl == s->curw) wloffset = wlwidth; - oo = &wl->window->options; + oo = wl->window->options; sep = options_get_string(oo, "window-status-separator"); - seplen = screen_write_strlen(utf8flag, "%s", sep); + seplen = screen_write_strlen("%s", sep); wlwidth += wl->status_width + seplen; } @@ -227,12 +362,12 @@ status_redraw(struct client *c) /* And draw the window list into it. */ screen_write_start(&ctx, NULL, &window_list); RB_FOREACH(wl, winlinks, &s->windows) { - screen_write_cnputs(&ctx, - -1, &wl->status_cell, utf8flag, "%s", wl->status_text); + screen_write_cnputs(&ctx, -1, &wl->status_cell, "%s", + wl->status_text); - oo = &wl->window->options; + oo = wl->window->options; sep = options_get_string(oo, "window-status-separator"); - screen_write_nputs(&ctx, -1, &stdgc, utf8flag, "%s", sep); + screen_write_nputs(&ctx, -1, &stdgc, "%s", sep); } screen_write_stop(&ctx); @@ -304,7 +439,7 @@ draw: /* Draw the left string and arrow. */ screen_write_cursormove(&ctx, 0, 0); if (llen != 0) - screen_write_cnputs(&ctx, llen, &lgc, utf8flag, "%s", left); + screen_write_cnputs(&ctx, llen, &lgc, "%s", left); if (larrow != 0) { memcpy(&gc, &stdgc, sizeof gc); if (larrow == -1) @@ -322,7 +457,7 @@ draw: } else screen_write_cursormove(&ctx, c->tty.sx - rlen, 0); if (rlen != 0) - screen_write_cnputs(&ctx, rlen, &rgc, utf8flag, "%s", right); + screen_write_cnputs(&ctx, rlen, &rgc, "%s", right); /* Figure out the offset for the window list. */ if (llen != 0) @@ -330,7 +465,7 @@ draw: else wloffset = 0; if (wlwidth < wlavailable) { - switch (options_get_number(&s->options, "status-justify")) { + switch (options_get_number(s->options, "status-justify")) { case 1: /* centred */ wloffset += (wlavailable - wlwidth) / 2; break; @@ -362,252 +497,34 @@ out: return (1); } -/* Replace a single special sequence (prefixed by #). */ -void -status_replace1(struct client *c, char **iptr, char **optr, char *out, - size_t outsize) -{ - char ch, tmp[256], *ptr, *endptr; - size_t ptrlen; - long limit; - - errno = 0; - limit = strtol(*iptr, &endptr, 10); - if ((limit == 0 && errno != EINVAL) || - (limit == LONG_MIN && errno != ERANGE) || - (limit == LONG_MAX && errno != ERANGE) || - limit != 0) - *iptr = endptr; - if (limit <= 0) - limit = LONG_MAX; - - switch (*(*iptr)++) { - case '(': - if ((ptr = status_find_job(c, iptr)) == NULL) - return; - goto do_replace; - case '[': - /* - * Embedded style, handled at display time. Leave present and - * skip input until ]. - */ - ch = ']'; - goto skip_to; - case '{': - ptr = (char *) "#{"; - goto do_replace; - default: - xsnprintf(tmp, sizeof tmp, "#%c", *(*iptr - 1)); - ptr = tmp; - goto do_replace; - } - - return; - -do_replace: - ptrlen = strlen(ptr); - if ((size_t) limit < ptrlen) - ptrlen = limit; - - if (*optr + ptrlen >= out + outsize - 1) - return; - while (ptrlen > 0 && *ptr != '\0') { - *(*optr)++ = *ptr++; - ptrlen--; - } - - return; - -skip_to: - *(*optr)++ = '#'; - - (*iptr)--; /* include ch */ - while (**iptr != ch && **iptr != '\0') { - if (*optr >= out + outsize - 1) - break; - *(*optr)++ = *(*iptr)++; - } -} - /* Replace special sequences in fmt. */ char * status_replace(struct client *c, struct winlink *wl, const char *fmt, time_t t) { - static char out[BUFSIZ]; - char in[BUFSIZ], ch, *iptr, *optr, *expanded; - size_t len; struct format_tree *ft; + char *expanded; if (fmt == NULL) return (xstrdup("")); - len = strftime(in, sizeof in, fmt, localtime(&t)); - in[len] = '\0'; - - iptr = in; - optr = out; - - while (*iptr != '\0') { - if (optr >= out + (sizeof out) - 1) - break; - ch = *iptr++; - - if (ch != '#' || *iptr == '\0') { - *optr++ = ch; - continue; - } - status_replace1(c, &iptr, &optr, out, sizeof out); - } - *optr = '\0'; - - ft = format_create(); + if (c->flags & CLIENT_STATUSFORCE) + ft = format_create(NULL, FORMAT_STATUS|FORMAT_FORCE); + else + ft = format_create(NULL, FORMAT_STATUS); format_defaults(ft, c, NULL, wl, NULL); - expanded = format_expand(ft, out); + + expanded = format_expand_time(ft, fmt, t); + format_free(ft); return (expanded); } -/* Figure out job name and get its result, starting it off if necessary. */ -char * -status_find_job(struct client *c, char **iptr) -{ - struct status_out *so, so_find; - char *cmd; - int lastesc; - size_t len; - - if (**iptr == '\0') - return (NULL); - if (**iptr == ')') { /* no command given */ - (*iptr)++; - return (NULL); - } - - cmd = xmalloc(strlen(*iptr) + 1); - len = 0; - - lastesc = 0; - for (; **iptr != '\0'; (*iptr)++) { - if (!lastesc && **iptr == ')') - break; /* unescaped ) is the end */ - if (!lastesc && **iptr == '\\') { - lastesc = 1; - continue; /* skip \ if not escaped */ - } - lastesc = 0; - cmd[len++] = **iptr; - } - if (**iptr == '\0') /* no terminating ) */ { - free(cmd); - return (NULL); - } - (*iptr)++; /* skip final ) */ - cmd[len] = '\0'; - - /* First try in the new tree. */ - so_find.cmd = cmd; - so = RB_FIND(status_out_tree, &c->status_new, &so_find); - if (so != NULL && so->out != NULL) { - free(cmd); - return (so->out); - } - - /* If not found at all, start the job and add to the tree. */ - if (so == NULL) { - job_run(cmd, NULL, status_job_callback, status_job_free, c); - c->references++; - - so = xmalloc(sizeof *so); - so->cmd = xstrdup(cmd); - so->out = NULL; - RB_INSERT(status_out_tree, &c->status_new, so); - } - - /* Lookup in the old tree. */ - so_find.cmd = cmd; - so = RB_FIND(status_out_tree, &c->status_old, &so_find); - free(cmd); - if (so != NULL) - return (so->out); - return (NULL); -} - -/* Free job tree. */ -void -status_free_jobs(struct status_out_tree *sotree) -{ - struct status_out *so, *so_next; - - so_next = RB_MIN(status_out_tree, sotree); - while (so_next != NULL) { - so = so_next; - so_next = RB_NEXT(status_out_tree, sotree, so); - - RB_REMOVE(status_out_tree, sotree, so); - free(so->out); - free(so->cmd); - free(so); - } -} - -/* Update jobs on status interval. */ -void -status_update_jobs(struct client *c) -{ - /* Free the old tree. */ - status_free_jobs(&c->status_old); - - /* Move the new to old. */ - memcpy(&c->status_old, &c->status_new, sizeof c->status_old); - RB_INIT(&c->status_new); -} - -/* Free status job. */ -void -status_job_free(void *data) -{ - struct client *c = data; - - c->references--; -} - -/* Job has finished: save its result. */ -void -status_job_callback(struct job *job) -{ - struct client *c = job->data; - struct status_out *so, so_find; - char *line, *buf; - size_t len; - - if (c->flags & CLIENT_DEAD) - return; - - so_find.cmd = job->cmd; - so = RB_FIND(status_out_tree, &c->status_new, &so_find); - if (so == NULL || so->out != NULL) - return; - - buf = NULL; - if ((line = evbuffer_readline(job->event->input)) == NULL) { - len = EVBUFFER_LENGTH(job->event->input); - buf = xmalloc(len + 1); - if (len != 0) - memcpy(buf, EVBUFFER_DATA(job->event->input), len); - buf[len] = '\0'; - } else - buf = line; - - so->out = buf; - server_status_client(c); -} - /* Return winlink status line entry and adjust gc as necessary. */ char * status_print(struct client *c, struct winlink *wl, time_t t, struct grid_cell *gc) { - struct options *oo = &wl->window->options; + struct options *oo = wl->window->options; struct session *s = c->session; const char *fmt; char *text; @@ -635,10 +552,12 @@ void status_message_set(struct client *c, const char *fmt, ...) { struct timeval tv; - struct message_entry *msg; + struct message_entry *msg, *msg1; va_list ap; int delay; - u_int i, limit; + u_int limit; + + limit = options_get_number(global_options, "message-limit"); status_prompt_clear(c); status_message_clear(c); @@ -647,29 +566,30 @@ status_message_set(struct client *c, const char *fmt, ...) xvasprintf(&c->message_string, fmt, ap); va_end(ap); - ARRAY_EXPAND(&c->message_log, 1); - msg = &ARRAY_LAST(&c->message_log); + msg = xcalloc(1, sizeof *msg); msg->msg_time = time(NULL); + msg->msg_num = c->message_next++; msg->msg = xstrdup(c->message_string); + TAILQ_INSERT_TAIL(&c->message_log, msg, entry); - limit = options_get_number(&global_options, "message-limit"); - if (ARRAY_LENGTH(&c->message_log) > limit) { - limit = ARRAY_LENGTH(&c->message_log) - limit; - for (i = 0; i < limit; i++) { - msg = &ARRAY_FIRST(&c->message_log); - free(msg->msg); - ARRAY_REMOVE(&c->message_log, 0); - } + TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) { + if (msg->msg_num + limit >= c->message_next) + break; + free(msg->msg); + TAILQ_REMOVE(&c->message_log, msg, entry); + free(msg); } - delay = options_get_number(&c->session->options, "display-time"); - tv.tv_sec = delay / 1000; - tv.tv_usec = (delay % 1000) * 1000L; + delay = options_get_number(c->session->options, "display-time"); + if (delay > 0) { + tv.tv_sec = delay / 1000; + tv.tv_usec = (delay % 1000) * 1000L; - if (event_initialized(&c->message_timer)) - evtimer_del(&c->message_timer); - evtimer_set(&c->message_timer, status_message_callback, c); - evtimer_add(&c->message_timer, &tv); + if (event_initialized(&c->message_timer)) + evtimer_del(&c->message_timer); + evtimer_set(&c->message_timer, status_message_callback, c); + evtimer_add(&c->message_timer, &tv); + } c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE); c->flags |= CLIENT_STATUS; @@ -686,6 +606,12 @@ status_message_clear(struct client *c) c->message_string = NULL; c->tty.flags &= ~(TTY_NOCURSOR|TTY_FREEZE); +#ifdef TMATE + if (c->flags & CLIENT_FORCE_STATUS) { + c->flags &= ~CLIENT_FORCE_STATUS; + recalculate_sizes(); + } +#endif c->flags |= CLIENT_REDRAW; /* screen was frozen and may have changed */ screen_reinit(&c->status); @@ -693,7 +619,7 @@ status_message_clear(struct client *c) /* Clear status line message after timer expires. */ void -status_message_callback(unused int fd, unused short event, void *data) +status_message_callback(__unused int fd, __unused short event, void *data) { struct client *c = data; @@ -709,25 +635,22 @@ status_message_redraw(struct client *c) struct screen old_status; size_t len; struct grid_cell gc; - int utf8flag; if (c->tty.sx == 0 || c->tty.sy == 0) return (0); memcpy(&old_status, &c->status, sizeof old_status); screen_init(&c->status, c->tty.sx, 1, 0); - utf8flag = options_get_number(&s->options, "status-utf8"); - - len = screen_write_strlen(utf8flag, "%s", c->message_string); + len = screen_write_strlen("%s", c->message_string); if (len > c->tty.sx) len = c->tty.sx; - style_apply(&gc, &s->options, "message-style"); + style_apply(&gc, s->options, "message-style"); screen_write_start(&ctx, NULL, &c->status); screen_write_cursormove(&ctx, 0, 0); - screen_write_nputs(&ctx, len, &gc, utf8flag, "%s", c->message_string); + screen_write_nputs(&ctx, len, &gc, "%s", c->message_string); for (; len < c->tty.sx; len++) screen_write_putc(&ctx, &gc, ' '); @@ -751,7 +674,7 @@ status_prompt_set(struct client *c, const char *msg, const char *input, int keys; time_t t; - ft = format_create(); + ft = format_create(NULL, 0); format_defaults(ft, c, NULL, NULL, NULL); t = time(NULL); @@ -771,7 +694,7 @@ status_prompt_set(struct client *c, const char *msg, const char *input, c->prompt_flags = flags; - keys = options_get_number(&c->session->options, "status-keys"); + keys = options_get_number(c->session->options, "status-keys"); if (keys == MODEKEY_EMACS) mode_key_init(&c->prompt_mdata, &mode_key_tree_emacs_edit); else @@ -812,7 +735,7 @@ status_prompt_update(struct client *c, const char *msg, const char *input) struct format_tree *ft; time_t t; - ft = format_create(); + ft = format_create(NULL, 0); format_defaults(ft, c, NULL, NULL, NULL); t = time(NULL); @@ -838,43 +761,40 @@ status_prompt_redraw(struct client *c) struct session *s = c->session; struct screen old_status; size_t i, size, left, len, off; - struct grid_cell gc, *gcp; - int utf8flag; + struct grid_cell gc; if (c->tty.sx == 0 || c->tty.sy == 0) return (0); memcpy(&old_status, &c->status, sizeof old_status); screen_init(&c->status, c->tty.sx, 1, 0); - utf8flag = options_get_number(&s->options, "status-utf8"); - - len = screen_write_strlen(utf8flag, "%s", c->prompt_string); + len = screen_write_strlen("%s", c->prompt_string); if (len > c->tty.sx) len = c->tty.sx; off = 0; /* Change colours for command mode. */ if (c->prompt_mdata.mode == 1) - style_apply(&gc, &s->options, "message-command-style"); + style_apply(&gc, s->options, "message-command-style"); else - style_apply(&gc, &s->options, "message-style"); + style_apply(&gc, s->options, "message-style"); screen_write_start(&ctx, NULL, &c->status); screen_write_cursormove(&ctx, 0, 0); - screen_write_nputs(&ctx, len, &gc, utf8flag, "%s", c->prompt_string); + screen_write_nputs(&ctx, len, &gc, "%s", c->prompt_string); left = c->tty.sx - len; if (left != 0) { - size = screen_write_strlen(utf8flag, "%s", c->prompt_buffer); + size = screen_write_strlen("%s", c->prompt_buffer); if (c->prompt_index >= left) { off = c->prompt_index - left + 1; if (c->prompt_index == size) left--; size = left; } - screen_write_nputs( - &ctx, left, &gc, utf8flag, "%s", c->prompt_buffer + off); + screen_write_nputs(&ctx, left, &gc, "%s", c->prompt_buffer + + off); for (i = len + size; i < c->tty.sx; i++) screen_write_putc(&ctx, &gc, ' '); @@ -884,8 +804,9 @@ status_prompt_redraw(struct client *c) /* Apply fake cursor. */ off = len + c->prompt_index - off; - gcp = grid_view_get_cell(c->status.grid, off, 0); - gcp->attr ^= GRID_ATTR_REVERSE; + grid_view_get_cell(c->status.grid, off, 0, &gc); + gc.attr ^= GRID_ATTR_REVERSE; + grid_view_set_cell(c->status.grid, off, 0, &gc); if (grid_compare(c->status.grid, old_status.grid) == 0) { screen_free(&old_status); @@ -897,16 +818,15 @@ status_prompt_redraw(struct client *c) /* Handle keys in prompt. */ void -status_prompt_key(struct client *c, int key) +status_prompt_key(struct client *c, key_code key) { struct session *sess = c->session; - struct options *oo = &sess->options; + struct options *oo = sess->options; struct paste_buffer *pb; char *s, *first, *last, word[64], swapc; - const char *histstr; - const char *wsep = NULL; + const char *histstr, *bufdata, *wsep = NULL; u_char ch; - size_t size, n, off, idx; + size_t size, n, off, idx, bufsize; size = strlen(c->prompt_buffer); switch (mode_key_lookup(&c->prompt_mdata, key, NULL)) { @@ -974,7 +894,7 @@ status_prompt_key(struct client *c, int key) word[last - first] = '\0'; /* And try to complete it. */ - if ((s = status_prompt_complete(word)) == NULL) + if ((s = status_prompt_complete(sess, word)) == NULL) break; /* Trim out word. */ @@ -1155,24 +1075,25 @@ status_prompt_key(struct client *c, int key) c->flags |= CLIENT_STATUS; break; case MODEKEYEDIT_PASTE: - if ((pb = paste_get_top()) == NULL) + if ((pb = paste_get_top(NULL)) == NULL) break; - for (n = 0; n < pb->size; n++) { - ch = (u_char) pb->data[n]; + bufdata = paste_buffer_data(pb, &bufsize); + for (n = 0; n < bufsize; n++) { + ch = (u_char)bufdata[n]; if (ch < 32 || ch == 127) break; } c->prompt_buffer = xrealloc(c->prompt_buffer, size + n + 1); if (c->prompt_index == size) { - memcpy(c->prompt_buffer + c->prompt_index, pb->data, n); + memcpy(c->prompt_buffer + c->prompt_index, bufdata, n); c->prompt_index += n; c->prompt_buffer[c->prompt_index] = '\0'; } else { memmove(c->prompt_buffer + c->prompt_index + n, c->prompt_buffer + c->prompt_index, size + 1 - c->prompt_index); - memcpy(c->prompt_buffer + c->prompt_index, pb->data, n); + memcpy(c->prompt_buffer + c->prompt_index, bufdata, n); c->prompt_index += n; } @@ -1201,7 +1122,7 @@ status_prompt_key(struct client *c, int key) status_prompt_clear(c); break; case MODEKEY_OTHER: - if ((key & 0xff00) != 0 || key < 32 || key == 127) + if (key <= 0x1f || key >= 0x7f) break; c->prompt_buffer = xrealloc(c->prompt_buffer, size + 2); @@ -1216,8 +1137,8 @@ status_prompt_key(struct client *c, int key) } if (c->prompt_flags & PROMPT_SINGLE) { - if (c->prompt_callbackfn( - c->prompt_data, c->prompt_buffer) == 0) + if (c->prompt_callbackfn(c->prompt_data, + c->prompt_buffer) == 0) status_prompt_clear(c); } @@ -1232,114 +1153,223 @@ status_prompt_key(struct client *c, int key) const char * status_prompt_up_history(u_int *idx) { - u_int size; - /* - * History runs from 0 to size - 1. - * - * Index is from 0 to size. Zero is empty. + * History runs from 0 to size - 1. Index is from 0 to size. Zero is + * empty. */ - size = ARRAY_LENGTH(&status_prompt_history); - if (size == 0 || *idx == size) + if (status_prompt_hsize == 0 || *idx == status_prompt_hsize) return (NULL); (*idx)++; - return (ARRAY_ITEM(&status_prompt_history, size - *idx)); + return (status_prompt_hlist[status_prompt_hsize - *idx]); } /* Get next line from the history. */ const char * status_prompt_down_history(u_int *idx) { - u_int size; - - size = ARRAY_LENGTH(&status_prompt_history); - if (size == 0 || *idx == 0) + if (status_prompt_hsize == 0 || *idx == 0) return (""); (*idx)--; if (*idx == 0) return (""); - return (ARRAY_ITEM(&status_prompt_history, size - *idx)); + return (status_prompt_hlist[status_prompt_hsize - *idx]); } /* Add line to the history. */ void status_prompt_add_history(const char *line) { - u_int size; + size_t size; - size = ARRAY_LENGTH(&status_prompt_history); - if (size > 0 && strcmp(ARRAY_LAST(&status_prompt_history), line) == 0) + if (status_prompt_hsize > 0 && + strcmp(status_prompt_hlist[status_prompt_hsize - 1], line) == 0) return; - if (size == PROMPT_HISTORY) { - free(ARRAY_FIRST(&status_prompt_history)); - ARRAY_REMOVE(&status_prompt_history, 0); + if (status_prompt_hsize == PROMPT_HISTORY) { + free(status_prompt_hlist[0]); + + size = (PROMPT_HISTORY - 1) * sizeof *status_prompt_hlist; + memmove(&status_prompt_hlist[0], &status_prompt_hlist[1], size); + + status_prompt_hlist[status_prompt_hsize - 1] = xstrdup(line); + return; } - ARRAY_ADD(&status_prompt_history, xstrdup(line)); + status_prompt_hlist = xreallocarray(status_prompt_hlist, + status_prompt_hsize + 1, sizeof *status_prompt_hlist); + status_prompt_hlist[status_prompt_hsize++] = xstrdup(line); +} + +/* Build completion list. */ +const char ** +status_prompt_complete_list(u_int *size, const char *s) +{ + const char **list = NULL, **layout; + const struct cmd_entry **cmdent; + const struct options_table_entry *oe; + const char *layouts[] = { + "even-horizontal", "even-vertical", "main-horizontal", + "main-vertical", "tiled", NULL + }; + + *size = 0; + for (cmdent = cmd_table; *cmdent != NULL; cmdent++) { + if (strncmp((*cmdent)->name, s, strlen(s)) == 0) { + list = xreallocarray(list, (*size) + 1, sizeof *list); + list[(*size)++] = (*cmdent)->name; + } + } + for (oe = options_table; oe->name != NULL; oe++) { + if (strncmp(oe->name, s, strlen(s)) == 0) { + list = xreallocarray(list, (*size) + 1, sizeof *list); + list[(*size)++] = oe->name; + } + } + for (layout = layouts; *layout != NULL; layout++) { + if (strncmp(*layout, s, strlen(s)) == 0) { + list = xreallocarray(list, (*size) + 1, sizeof *list); + list[(*size)++] = *layout; + } + } + return (list); +} + +/* Find longest prefix. */ +char * +status_prompt_complete_prefix(const char **list, u_int size) +{ + char *out; + u_int i; + size_t j; + + out = xstrdup(list[0]); + for (i = 1; i < size; i++) { + j = strlen(list[i]); + if (j > strlen(out)) + j = strlen(out); + for (; j > 0; j--) { + if (out[j - 1] != list[i][j - 1]) + out[j - 1] = '\0'; + } + } + return (out); } /* Complete word. */ char * -status_prompt_complete(const char *s) +status_prompt_complete(struct session *sess, const char *s) { - const struct cmd_entry **cmdent; - const struct options_table_entry *oe; - ARRAY_DECL(, const char *) list; - char *prefix, *s2; - u_int i; - size_t j; + const char **list = NULL, *colon; + u_int size = 0, i; + struct session *s_loop; + struct winlink *wl; + struct window *w; + char *copy, *out, *tmp; if (*s == '\0') return (NULL); + out = NULL; - /* First, build a list of all the possible matches. */ - ARRAY_INIT(&list); - for (cmdent = cmd_table; *cmdent != NULL; cmdent++) { - if (strncmp((*cmdent)->name, s, strlen(s)) == 0) - ARRAY_ADD(&list, (*cmdent)->name); - } - for (oe = server_options_table; oe->name != NULL; oe++) { - if (strncmp(oe->name, s, strlen(s)) == 0) - ARRAY_ADD(&list, oe->name); - } - for (oe = session_options_table; oe->name != NULL; oe++) { - if (strncmp(oe->name, s, strlen(s)) == 0) - ARRAY_ADD(&list, oe->name); - } - for (oe = window_options_table; oe->name != NULL; oe++) { - if (strncmp(oe->name, s, strlen(s)) == 0) - ARRAY_ADD(&list, oe->name); + if (strncmp(s, "-t", 2) != 0 && strncmp(s, "-s", 2) != 0) { + list = status_prompt_complete_list(&size, s); + if (size == 0) + out = NULL; + else if (size == 1) + xasprintf(&out, "%s ", list[0]); + else + out = status_prompt_complete_prefix(list, size); + free(list); + return (out); } + copy = xstrdup(s); - /* If none, bail now. */ - if (ARRAY_LENGTH(&list) == 0) { - ARRAY_FREE(&list); - return (NULL); - } + colon = ":"; + if (copy[strlen(copy) - 1] == ':') + copy[strlen(copy) - 1] = '\0'; + else + colon = ""; + s = copy + 2; - /* If an exact match, return it, with a trailing space. */ - if (ARRAY_LENGTH(&list) == 1) { - xasprintf(&s2, "%s ", ARRAY_FIRST(&list)); - ARRAY_FREE(&list); - return (s2); - } - - /* Now loop through the list and find the longest common prefix. */ - prefix = xstrdup(ARRAY_FIRST(&list)); - for (i = 1; i < ARRAY_LENGTH(&list); i++) { - s = ARRAY_ITEM(&list, i); - - j = strlen(s); - if (j > strlen(prefix)) - j = strlen(prefix); - for (; j > 0; j--) { - if (prefix[j - 1] != s[j - 1]) - prefix[j - 1] = '\0'; + RB_FOREACH(s_loop, sessions, &sessions) { + if (strncmp(s_loop->name, s, strlen(s)) == 0) { + list = xreallocarray(list, size + 2, sizeof *list); + list[size++] = s_loop->name; } } + if (size == 1) { + out = xstrdup(list[0]); + if (session_find(list[0]) != NULL) + colon = ":"; + } else if (size != 0) + out = status_prompt_complete_prefix(list, size); + if (out != NULL) { + xasprintf(&tmp, "-%c%s%s", copy[1], out, colon); + out = tmp; + goto found; + } - ARRAY_FREE(&list); - return (prefix); + colon = ""; + if (*s == ':') { + RB_FOREACH(wl, winlinks, &sess->windows) { + xasprintf(&tmp, ":%s", wl->window->name); + if (strncmp(tmp, s, strlen(s)) == 0){ + list = xreallocarray(list, size + 1, + sizeof *list); + list[size++] = tmp; + continue; + } + free(tmp); + + xasprintf(&tmp, ":%d", wl->idx); + if (strncmp(tmp, s, strlen(s)) == 0) { + list = xreallocarray(list, size + 1, + sizeof *list); + list[size++] = tmp; + continue; + } + free(tmp); + } + } else { + RB_FOREACH(s_loop, sessions, &sessions) { + RB_FOREACH(wl, winlinks, &s_loop->windows) { + w = wl->window; + + xasprintf(&tmp, "%s:%s", s_loop->name, w->name); + if (strncmp(tmp, s, strlen(s)) == 0) { + list = xreallocarray(list, size + 1, + sizeof *list); + list[size++] = tmp; + continue; + } + free(tmp); + + xasprintf(&tmp, "%s:%d", s_loop->name, wl->idx); + if (strncmp(tmp, s, strlen(s)) == 0) { + list = xreallocarray(list, size + 1, + sizeof *list); + list[size++] = tmp; + continue; + } + free(tmp); + } + } + } + if (size == 1) { + out = xstrdup(list[0]); + colon = " "; + } else if (size != 0) + out = status_prompt_complete_prefix(list, size); + if (out != NULL) { + xasprintf(&tmp, "-%c%s%s", copy[1], out, colon); + out = tmp; + } + + for (i = 0; i < size; i++) + free((void *)list[i]); + +found: + free(copy); + free(list); + return (out); } diff --git a/style.c b/style.c index d8ab07a3..151c2912 100644 --- a/style.c +++ b/style.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * Copyright (c) 2014 Tiago Cunha * * Permission to use, copy, modify, and distribute this software for any @@ -160,13 +160,21 @@ style_update_new(struct options *oo, const char *name, const char *newname) { int value; struct grid_cell *gc; + struct options_entry *o; /* It's a colour or attribute, but with no -style equivalent. */ if (newname == NULL) return; - gc = options_get_style(oo, newname); - value = options_get_number(oo, name); + o = options_find1(oo, newname); + if (o == NULL) + o = options_set_style(oo, newname, "default", 0); + gc = &o->style; + + o = options_find1(oo, name); + if (o == NULL) + o = options_set_number(oo, name, 8); + value = o->num; if (strstr(name, "-bg") != NULL) colour_set_bg(gc, value); @@ -244,3 +252,15 @@ style_apply_update(struct grid_cell *gc, struct options *oo, const char *name) if (gcp->attr != 0) gc->attr |= gcp->attr; } + +/* Check if two styles are the same. */ +int +style_equal(const struct grid_cell *gc1, const struct grid_cell *gc2) +{ + return gc1->fg == gc2->fg && + gc1->bg == gc2->bg && + (gc1->flags & ~GRID_FLAG_PADDING) == + (gc2->flags & ~GRID_FLAG_PADDING) && + (gc1->attr & ~GRID_ATTR_CHARSET) == + (gc2->attr & ~GRID_ATTR_CHARSET); +} diff --git a/tmate-debug.c b/tmate-debug.c new file mode 100644 index 00000000..81850284 --- /dev/null +++ b/tmate-debug.c @@ -0,0 +1,112 @@ +#ifdef HAVE_EXECINFO_H +#include +#endif +#include +#include +#include +#include +#include "tmate.h" + +#ifndef HAVE_BACKTRACE + +void tmate_print_stack_trace(void) {} +void tmate_catch_sigsegv(void) {} +void tmate_preload_trace_lib(void) {} + +#else + +#if DEBUG + +static int print_resolved_stack_frame(const char *frame) +{ + char file[100]; + char cmd[200]; + char output[300]; + char address[20]; + char *line; + FILE *ps; + + static regex_t _regex; + static regex_t *regex; + regmatch_t matches[3]; + + if (!regex) { + if (regcomp(&_regex, "(.+)\\(\\) \\[([^]]+)\\]", REG_EXTENDED)) + return -1; + regex = &_regex; + } + + if (regexec(regex, frame, 3, matches, 0)) + return -1; + + memcpy(file, &frame[matches[1].rm_so], matches[1].rm_eo - matches[1].rm_so); + file[matches[1].rm_eo - matches[1].rm_so] = 0; + + memcpy(address, &frame[matches[2].rm_so], matches[2].rm_eo - matches[2].rm_so); + address[matches[2].rm_eo - matches[2].rm_so] = 0; + + sprintf(cmd, "addr2line -e %s %s -f -p -s", file, address); + + ps = popen(cmd, "r"); + if (!ps) + return -1; + + line = fgets(output, sizeof(output), ps); + pclose(ps); + + if (!line) + return -1; + + line[strlen(line)-1] = 0; /* remove \n */ + tmate_debug("%s(%s) [%s]", file, line, address); + return 0; +} +#endif + +void tmate_print_stack_trace(void) +{ + void *array[20]; + size_t size; + char **strings; + size_t i; + + size = backtrace (array, 20); + strings = backtrace_symbols (array, size); + + tmate_info ("============ %zd stack frames ============", size); + + for (i = 1; i < size; i++) { +#if DEBUG + if (print_resolved_stack_frame(strings[i]) < 0) +#endif + tmate_info("%s", strings[i]); + } + + free (strings); +} + +static void handle_crash(int sig) +{ + /* TODO send stack trace to server */ + const char *what = sig == SIGSEGV ? "SIGSEGV" : "SIGABRT"; + tmate_info("%s printing stack trace", what); + tmate_print_stack_trace(); + + /* Reraise */ + signal(sig, NULL); + kill(getpid(), sig); +} + +void tmate_catch_sigsegv(void) +{ + signal(SIGSEGV, handle_crash); + signal(SIGABRT, handle_crash); +} + +void tmate_preload_trace_lib(void) +{ + void *array[1]; + backtrace(array, 1); +} + +#endif diff --git a/tmate-decoder.c b/tmate-decoder.c new file mode 100644 index 00000000..3b1e31af --- /dev/null +++ b/tmate-decoder.c @@ -0,0 +1,211 @@ +#include "tmate.h" +#include "tmate-protocol.h" + +static void handle_notify(__unused struct tmate_session *session, + struct tmate_unpacker *uk) +{ + char *msg = unpack_string(uk); + tmate_status_message("%s", msg); + free(msg); +} + +static void handle_legacy_pane_key(__unused struct tmate_session *_session, + struct tmate_unpacker *uk) +{ + struct session *s; + struct window *w; + struct window_pane *wp; + + int key = unpack_int(uk); + + s = RB_MIN(sessions, &sessions); + if (!s) + return; + + w = s->curw->window; + if (!w) + return; + + wp = w->active; + if (!wp) + return; + + window_pane_key(wp, NULL, s, key, NULL); +} + +static struct window_pane *find_window_pane(struct session *s, int pane_id) +{ + struct window *w; + + if (pane_id != -1) + return window_pane_find_by_id(pane_id); + + w = s->curw->window; + if (!w) + return NULL; + + return w->active; +} + +static void handle_pane_key(__unused struct tmate_session *_session, + struct tmate_unpacker *uk) +{ + struct session *s; + struct window_pane *wp; + + int pane_id = unpack_int(uk); + key_code key = unpack_int(uk); + + s = RB_MIN(sessions, &sessions); + if (!s) + return; + + wp = find_window_pane(s, pane_id); + if (!wp) + return; + + window_pane_key(wp, NULL, s, key, NULL); +} + +static void handle_resize(struct tmate_session *session, + struct tmate_unpacker *uk) +{ + session->min_sx = unpack_int(uk); + session->min_sy = unpack_int(uk); + recalculate_sizes(); +} + +extern char **cfg_causes; +extern u_int cfg_ncauses; + +static void handle_exec_cmd_str(__unused struct tmate_session *session, + struct tmate_unpacker *uk) +{ + struct cmd_q *cmd_q; + struct cmd_list *cmdlist; + char *cause; + u_int i; + + int client_id = unpack_int(uk); + char *cmd_str = unpack_string(uk); + + if (cmd_string_parse(cmd_str, &cmdlist, NULL, 0, &cause) != 0) { + tmate_failed_cmd(client_id, cause); + free(cause); + goto out; + } + + cmd_q = cmdq_new(NULL); + cmdq_run(cmd_q, cmdlist, NULL); + cmd_list_free(cmdlist); + cmdq_free(cmd_q); + + /* error messages land in cfg_causes */ + for (i = 0; i < cfg_ncauses; i++) { + tmate_failed_cmd(client_id, cfg_causes[i]); + free(cfg_causes[i]); + } + + free(cfg_causes); + cfg_causes = NULL; + cfg_ncauses = 0; + +out: + free(cmd_str); +} + +static void handle_exec_cmd(__unused struct tmate_session *session, + struct tmate_unpacker *uk) +{ + struct cmd_q *cmd_q; + struct cmd_list *cmdlist; + struct cmd *cmd; + char *cause; + u_int i; + unsigned int argc; + char **argv; + + int client_id = unpack_int(uk); + + argc = uk->argc; + argv = xmalloc(sizeof(char *) * argc); + for (i = 0; i < argc; i++) + argv[i] = unpack_string(uk); + + cmd = cmd_parse(argc, argv, NULL, 0, &cause); + if (!cmd) { + tmate_failed_cmd(client_id, cause); + free(cause); + goto out; + } + + cmdlist = xcalloc(1, sizeof *cmdlist); + cmdlist->references = 1; + TAILQ_INIT(&cmdlist->list); + TAILQ_INSERT_TAIL(&cmdlist->list, cmd, qentry); + + cmd_q = cmdq_new(NULL); + cmdq_run(cmd_q, cmdlist, NULL); + cmd_list_free(cmdlist); + cmdq_free(cmd_q); + + /* error messages land in cfg_causes */ + for (i = 0; i < cfg_ncauses; i++) { + tmate_failed_cmd(client_id, cfg_causes[i]); + free(cfg_causes[i]); + } + + free(cfg_causes); + cfg_causes = NULL; + cfg_ncauses = 0; + +out: + cmd_free_argv(argc, argv); +} + +static void maybe_save_reconnection_data(struct tmate_session *session, + const char *name, const char *value) +{ + if (!strcmp(name, "tmate_reconnection_data")) { + free(session->reconnection_data); + session->reconnection_data = xstrdup(value); + } +} + +static void handle_set_env(struct tmate_session *session, + struct tmate_unpacker *uk) +{ + char *name = unpack_string(uk); + char *value = unpack_string(uk); + + tmate_set_env(name, value); + maybe_save_reconnection_data(session, name, value); + + free(name); + free(value); +} + +static void handle_ready(struct tmate_session *session, + __unused struct tmate_unpacker *uk) +{ + session->tmate_env_ready = 1; + signal_waiting_clients("tmate-ready"); +} + +void tmate_dispatch_slave_message(struct tmate_session *session, + struct tmate_unpacker *uk) +{ + int cmd = unpack_int(uk); + switch (cmd) { +#define dispatch(c, f) case c: f(session, uk); break + dispatch(TMATE_IN_NOTIFY, handle_notify); + dispatch(TMATE_IN_LEGACY_PANE_KEY, handle_legacy_pane_key); + dispatch(TMATE_IN_RESIZE, handle_resize); + dispatch(TMATE_IN_EXEC_CMD_STR, handle_exec_cmd_str); + dispatch(TMATE_IN_SET_ENV, handle_set_env); + dispatch(TMATE_IN_READY, handle_ready); + dispatch(TMATE_IN_PANE_KEY, handle_pane_key); + dispatch(TMATE_IN_EXEC_CMD, handle_exec_cmd); + default: tmate_info("Bad message type: %d", cmd); + } +} diff --git a/tmate-encoder.c b/tmate-encoder.c new file mode 100644 index 00000000..e1373451 --- /dev/null +++ b/tmate-encoder.c @@ -0,0 +1,490 @@ +#include +#include "tmate.h" +#include "tmate-protocol.h" +#include "window-copy.h" + +#define pack(what, ...) _pack(&tmate_session.encoder, what, ##__VA_ARGS__) + +void tmate_write_header(void) +{ + pack(array, 3); + pack(int, TMATE_OUT_HEADER); + pack(int, TMATE_PROTOCOL_VERSION); + pack(string, VERSION); +} + +void tmate_write_uname(void) +{ + struct utsname name; + if (uname(&name) < 0) { + tmate_debug("uname() failed"); + return; + } + + pack(array, 6); + pack(int, TMATE_OUT_UNAME); + pack(string, name.sysname); + pack(string, name.nodename); + pack(string, name.release); + pack(string, name.version); + pack(string, name.machine); +} + +void tmate_write_ready(void) +{ + pack(array, 1); + pack(int, TMATE_OUT_READY); +} + +void tmate_sync_layout(void) +{ + struct session *s; + struct winlink *wl; + struct window *w; + struct window_pane *wp; + int num_panes = 0; + int num_windows = 0; + int active_pane_id; + int active_window_idx = -1; + + /* + * TODO this can get a little heavy. + * We are shipping the full layout whenever a window name changes, + * that is, at every shell command. + * Might be better to do something incremental. + */ + + /* + * We only allow one session, it makes our lives easier. + * Especially when the HTML5 client will come along. + * We make no distinction between a winlink and its window except + * that we send the winlink idx to draw the status bar properly. + */ + + s = RB_MIN(sessions, &sessions); + if (!s) + return; + + num_windows = 0; + RB_FOREACH(wl, winlinks, &s->windows) { + if (wl->window) + num_windows++; + } + + if (!num_windows) + return; + + pack(array, 5); + pack(int, TMATE_OUT_SYNC_LAYOUT); + + pack(int, s->sx); + pack(int, s->sy); + + pack(array, num_windows); + RB_FOREACH(wl, winlinks, &s->windows) { + w = wl->window; + if (!w) + continue; + + w->tmate_last_sync_active_pane = NULL; + active_pane_id = -1; + + if (active_window_idx == -1) + active_window_idx = wl->idx; + + pack(array, 4); + pack(int, wl->idx); + pack(string, w->name); + + num_panes = 0; + TAILQ_FOREACH(wp, &w->panes, entry) + num_panes++; + + pack(array, num_panes); + TAILQ_FOREACH(wp, &w->panes, entry) { + pack(array, 5); + pack(int, wp->id); + pack(int, wp->sx); + pack(int, wp->sy); + pack(int, wp->xoff); + pack(int, wp->yoff); + + if (wp == w->active) { + w->tmate_last_sync_active_pane = wp; + active_pane_id = wp->id; + } + + } + pack(int, active_pane_id); + } + + if (s->curw) + active_window_idx = s->curw->idx; + + pack(int, active_window_idx); +} + +/* TODO add a buffer for pty_data ? */ + +#define TMATE_MAX_PTY_SIZE (16*1024) + +void tmate_pty_data(struct window_pane *wp, const char *buf, size_t len) +{ + size_t to_write; + + while (len > 0) { + to_write = len < TMATE_MAX_PTY_SIZE ? len : TMATE_MAX_PTY_SIZE; + + pack(array, 3); + pack(int, TMATE_OUT_PTY_DATA); + pack(int, wp->id); + pack(str, to_write); + pack(str_body, buf, to_write); + + buf += to_write; + len -= to_write; + } +} + +extern const struct cmd_entry cmd_bind_key_entry; +extern const struct cmd_entry cmd_unbind_key_entry; +extern const struct cmd_entry cmd_set_option_entry; +extern const struct cmd_entry cmd_set_window_option_entry; + +static const struct cmd_entry *replicated_cmds[] = { + &cmd_bind_key_entry, + &cmd_unbind_key_entry, + &cmd_set_option_entry, + &cmd_set_window_option_entry, + NULL +}; + +int tmate_should_replicate_cmd(const struct cmd_entry *cmd) +{ + const struct cmd_entry **ptr; + + for (ptr = replicated_cmds; *ptr; ptr++) + if (*ptr == cmd) + return 1; + return 0; +} + +#define sc (&session->saved_tmux_cmds) +#define SAVED_TMUX_CMD_INITIAL_SIZE 256 +static void __tmate_exec_cmd_args(int argc, const char **argv); + +static void append_saved_cmd(struct tmate_session *session, + int argc, const char **argv) +{ + if (!sc->cmds) { + sc->capacity = SAVED_TMUX_CMD_INITIAL_SIZE; + sc->cmds = xmalloc(sizeof(*sc->cmds) * sc->capacity); + sc->tail = 0; + } + + if (sc->tail == sc->capacity) { + sc->capacity *= 2; + sc->cmds = xrealloc(sc->cmds, sizeof(*sc->cmds) * sc->capacity); + } + + sc->cmds[sc->tail].argc = argc; + sc->cmds[sc->tail].argv = cmd_copy_argv(argc, (char **)argv); + + sc->tail++; +} + +static void replay_saved_cmd(struct tmate_session *session) +{ + unsigned int i; + for (i = 0; i < sc->tail; i++) + __tmate_exec_cmd_args(sc->cmds[i].argc, (const char **)sc->cmds[i].argv); +} +#undef sc + +struct args_entry { + u_char flag; + char *value; + RB_ENTRY(args_entry) entry; +}; + +static void extract_cmd(struct cmd *cmd, int *_argc, char ***_argv) +{ + struct args_entry *entry; + struct args* args = cmd->args; + int argc = 0; + char **argv; + int next = 0, i; + + argc++; /* cmd name */ + RB_FOREACH(entry, args_tree, &args->tree) { + argc++; + if (entry->value != NULL) + argc++; + } + argc += args->argc; + argv = xmalloc(sizeof(char *) * argc); + + argv[next++] = xstrdup(cmd->entry->name); + + RB_FOREACH(entry, args_tree, &args->tree) { + xasprintf(&argv[next++], "-%c", entry->flag); + if (entry->value != NULL) + argv[next++] = xstrdup(entry->value); + } + + for (i = 0; i < args->argc; i++) + argv[next++] = xstrdup(args->argv[i]); + + *_argc = argc; + *_argv = argv; +} + +static void __tmate_exec_cmd_args(int argc, const char **argv) +{ + int i; + + pack(array, argc + 1); + pack(int, TMATE_OUT_EXEC_CMD); + + for (i = 0; i < argc; i++) + pack(string, argv[i]); +} + +void tmate_exec_cmd_args(int argc, const char **argv) +{ + __tmate_exec_cmd_args(argc, argv); + append_saved_cmd(&tmate_session, argc, argv); +} + +void tmate_set_val(const char *name, const char *value) +{ + char *buf; + xasprintf(&buf, "%s=%s", name, value); + tmate_exec_cmd_args(3, (const char *[]){"set-option", "tmate-set", buf}); + free(buf); +} + +void tmate_exec_cmd(struct cmd *cmd) +{ + int argc; + char **argv; + + extract_cmd(cmd, &argc, &argv); + tmate_exec_cmd_args(argc, (const char **)argv); + cmd_free_argv(argc, argv); +} + +void tmate_failed_cmd(int client_id, const char *cause) +{ + pack(array, 3); + pack(int, TMATE_OUT_FAILED_CMD); + pack(int, client_id); + pack(string, cause); +} + +void tmate_status(const char *left, const char *right) +{ + static char *old_left, *old_right; + + if (old_left && !strcmp(old_left, left) && + old_right && !strcmp(old_right, right)) + return; + + pack(array, 3); + pack(int, TMATE_OUT_STATUS); + pack(string, left); + pack(string, right); + + free(old_left); + free(old_right); + old_left = xstrdup(left); + old_right = xstrdup(right); +} + +void tmate_sync_copy_mode(struct window_pane *wp) +{ + struct window_copy_mode_data *data = wp->modedata; + + pack(array, 3); + pack(int, TMATE_OUT_SYNC_COPY_MODE); + + pack(int, wp->id); + + if (wp->mode != &window_copy_mode || + data->inputtype == WINDOW_COPY_PASSWORD) { + pack(array, 0); + return; + } + pack(array, 6); + pack(int, data->backing == &wp->base); + + pack(int, data->oy); + pack(int, data->cx); + pack(int, data->cy); + + if (data->screen.sel.flag) { + pack(array, 3); + pack(int, data->selx); + pack(int, -data->sely + screen_hsize(data->backing) + + screen_size_y(data->backing) - 1); + pack(int, data->rectflag); + } else + pack(array, 0); + + if (data->inputprompt) { + pack(array, 3); + pack(int, data->inputtype); + pack(string, data->inputprompt); + pack(string, data->inputstr); + } else + pack(array, 0); +} + +void tmate_write_copy_mode(struct window_pane *wp, const char *str) +{ + pack(array, 3); + pack(int, TMATE_OUT_WRITE_COPY_MODE); + pack(int, wp->id); + pack(string, str); +} + +void tmate_write_fin(void) +{ + pack(array, 1); + pack(int, TMATE_OUT_FIN); +} + +static void do_snapshot_grid(struct grid *grid, unsigned int max_history_lines) +{ + struct grid_line *line; + struct grid_cell gc; + unsigned int line_i, i; + unsigned int max_lines; + size_t str_len; + + max_lines = max_history_lines + grid->sy; + +#define grid_num_lines(grid) (grid->hsize + grid->sy) + + if (grid_num_lines(grid) > max_lines) + line_i = grid_num_lines(grid) - max_lines; + else + line_i = 0; + + pack(array, grid_num_lines(grid) - line_i); + for (; line_i < grid_num_lines(grid); line_i++) { + line = &grid->linedata[line_i]; + + pack(array, 2); + str_len = 0; + for (i = 0; i < line->cellsize; i++) { + grid_get_cell(grid, i, line_i, &gc); + str_len += gc.data.size; + } + + pack(str, str_len); + for (i = 0; i < line->cellsize; i++) { + grid_get_cell(grid, i, line_i, &gc); + pack(str_body, gc.data.data, gc.data.size); + } + + pack(array, line->cellsize); + for (i = 0; i < line->cellsize; i++) { + grid_get_cell(grid, i, line_i, &gc); + pack(unsigned_int, ((gc.flags << 24) | + (gc.attr << 16) | + (gc.bg << 8) | + gc.fg )); + } + } + +} + +static void do_snapshot_pane(struct window_pane *wp, unsigned int max_history_lines) +{ + struct screen *screen = &wp->base; + + pack(array, 4); + pack(int, wp->id); + + pack(unsigned_int, screen->mode); + + pack(array, 3); + pack(int, screen->cx); + pack(int, screen->cy); + do_snapshot_grid(screen->grid, max_history_lines); + + if (wp->saved_grid) { + pack(array, 3); + pack(int, wp->saved_cx); + pack(int, wp->saved_cy); + do_snapshot_grid(wp->saved_grid, max_history_lines); + } else { + pack(nil); + } +} + +static void tmate_send_session_snapshot(unsigned int max_history_lines) +{ + struct session *s; + struct winlink *wl; + struct window *w; + struct window_pane *pane; + int num_panes; + + pack(array, 2); + pack(int, TMATE_OUT_SNAPSHOT); + + s = RB_MIN(sessions, &sessions); + if (!s) + tmate_fatal("no session?"); + + num_panes = 0; + RB_FOREACH(wl, winlinks, &s->windows) { + w = wl->window; + if (!w) + continue; + + TAILQ_FOREACH(pane, &w->panes, entry) + num_panes++; + } + + pack(array, num_panes); + RB_FOREACH(wl, winlinks, &s->windows) { + w = wl->window; + if (!w) + continue; + + TAILQ_FOREACH(pane, &w->panes, entry) + do_snapshot_pane(pane, max_history_lines); + } +} + +static void tmate_send_reconnection_data(struct tmate_session *session) +{ + if (!session->reconnection_data) + return; + + pack(array, 2); + pack(int, TMATE_OUT_RECONNECT); + pack(string, session->reconnection_data); +} + +#define RECONNECTION_MAX_HISTORY_LINE 300 + +void tmate_send_reconnection_state(struct tmate_session *session) +{ + /* Start with a fresh encoder */ + tmate_encoder_destroy(&session->encoder); + tmate_encoder_init(&session->encoder, NULL, session); + + tmate_write_header(); + tmate_send_reconnection_data(session); + replay_saved_cmd(session); + /* TODO send all option variables */ + tmate_write_uname(); + tmate_write_ready(); + + tmate_sync_layout(); + tmate_send_session_snapshot(RECONNECTION_MAX_HISTORY_LINE); +} diff --git a/tmate-env.c b/tmate-env.c new file mode 100644 index 00000000..92abf069 --- /dev/null +++ b/tmate-env.c @@ -0,0 +1,36 @@ +#include "tmate.h" + +struct tmate_env { + TAILQ_ENTRY(tmate_env) entry; + char *name; + char *value; +}; + +TAILQ_HEAD(, tmate_env) tmate_env_list; + +void tmate_set_env(const char *name, const char *value) +{ + struct tmate_env *tmate_env; + + TAILQ_FOREACH(tmate_env, &tmate_env_list, entry) { + if (!strcmp(tmate_env->name, name)) { + free(tmate_env->value); + tmate_env->value = xstrdup(value); + return; + } + } + + tmate_env = xmalloc(sizeof(*tmate_env)); + tmate_env->name = xstrdup(name); + tmate_env->value = xstrdup(value); + TAILQ_INSERT_HEAD(&tmate_env_list, tmate_env, entry); +} + +void tmate_format(struct format_tree *ft) +{ + struct tmate_env *tmate_env; + + TAILQ_FOREACH(tmate_env, &tmate_env_list, entry) { + format_add(ft, tmate_env->name, "%s", tmate_env->value); + } +} diff --git a/tmate-msg.c b/tmate-msg.c new file mode 100644 index 00000000..4ee0a5de --- /dev/null +++ b/tmate-msg.c @@ -0,0 +1,96 @@ +#include +#include "tmate.h" + +void status_message_callback(int, short, void *); + +/* Very similar to status.c:status_message_set */ + +static void tmate_status_message_client(struct client *c, const char *message) +{ + struct timeval tv; + struct message_entry *msg, *msg1; + int delay; + u_int limit; + + limit = options_get_number(global_options, "message-limit"); + delay = options_get_number(c->session ? c->session->options : global_s_options, + "tmate-display-time"); + + status_prompt_clear(c); + status_message_clear(c); + + xasprintf(&c->message_string, "[tmate] %s", message); + + msg = xcalloc(1, sizeof *msg); + msg->msg_time = time(NULL); + msg->msg_num = c->message_next++; + msg->msg = xstrdup(c->message_string); + TAILQ_INSERT_TAIL(&c->message_log, msg, entry); + + TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) { + if (msg->msg_num + limit >= c->message_next) + break; + free(msg->msg); + TAILQ_REMOVE(&c->message_log, msg, entry); + free(msg); + } + + if (delay > 0) { + tv.tv_sec = delay / 1000; + tv.tv_usec = (delay % 1000) * 1000L; + + if (event_initialized(&c->message_timer)) + evtimer_del(&c->message_timer); + evtimer_set(&c->message_timer, status_message_callback, c); + evtimer_add(&c->message_timer, &tv); + } + + c->flags |= CLIENT_STATUS | CLIENT_FORCE_STATUS; + + recalculate_sizes(); +} + +static void tmate_status_message_session(const char *message) +{ + if (tmate_foreground) + return; + + struct session *s; + s = RB_MIN(sessions, &sessions); + if (!s) { + cfg_add_cause("%s", message); + return; + } + + struct window_pane *wp; + wp = s->curw->window->active; + if (wp->mode == &window_copy_mode) + window_copy_add(wp, "%s", message); +} + +void __tmate_status_message(const char *fmt, va_list ap) +{ + struct client *c; + char *message; + + xvasprintf(&message, fmt, ap); + tmate_info("%s", message); + + TAILQ_FOREACH(c, &clients, entry) { + if (c && !(c->flags & CLIENT_READONLY)) + tmate_status_message_client(c, message); + } + + tmate_status_message_session(message); + + free(message); +} + +void tmate_status_message(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + __tmate_status_message(fmt, ap); + va_end(ap); +} diff --git a/tmate-msgpack.c b/tmate-msgpack.c new file mode 100644 index 00000000..c41b07ee --- /dev/null +++ b/tmate-msgpack.c @@ -0,0 +1,224 @@ +#include "tmate.h" +#include "tmate-protocol.h" + +static void on_encoder_buffer_ready(__unused evutil_socket_t fd, + __unused short what, void *arg) +{ + struct tmate_encoder *encoder = arg; + + encoder->ev_active = false; + if (encoder->ready_callback) + encoder->ready_callback(encoder->userdata, encoder->buffer); +} + +static int on_encoder_write(void *userdata, const char *buf, size_t len) +{ + struct tmate_encoder *encoder = userdata; + + if (evbuffer_add(encoder->buffer, buf, len) < 0) + tmate_fatal("Cannot buffer encoded data"); + + if (!encoder->ev_active) { + event_active(encoder->ev_buffer, EV_READ, 0); + encoder->ev_active = true; + } + + return 0; +} + +/* Really sad hack, but we can get away with it */ +#define tmate_encoder_from_pk(pk) ((struct tmate_encoder *)pk) + +void msgpack_pack_string(msgpack_packer *pk, const char *str) +{ + size_t len = strlen(str); + + msgpack_pack_str(pk, len); + msgpack_pack_str_body(pk, str, len); +} + +void msgpack_pack_boolean(msgpack_packer *pk, bool value) +{ + if (value) + msgpack_pack_true(pk); + else + msgpack_pack_false(pk); +} + +void tmate_encoder_init(struct tmate_encoder *encoder, + tmate_encoder_write_cb *callback, + void *userdata) +{ + msgpack_packer_init(&encoder->pk, encoder, &on_encoder_write); + encoder->buffer = evbuffer_new(); + encoder->ready_callback = callback; + encoder->userdata = userdata; + + if (!encoder->buffer) + tmate_fatal("Can't allocate buffer"); + + encoder->ev_buffer = event_new(tmate_session.ev_base, -1, + EV_READ | EV_PERSIST, on_encoder_buffer_ready, encoder); + if (!encoder->ev_buffer) + tmate_fatal("Can't allocate event"); + + event_add(encoder->ev_buffer, NULL); + + encoder->ev_active = false; +} + +void tmate_encoder_destroy(struct tmate_encoder *encoder) +{ + /* encoder->pk doesn't need any cleanup */ + evbuffer_free(encoder->buffer); + event_del(encoder->ev_buffer); + event_free(encoder->ev_buffer); + memset(encoder, 0, sizeof(*encoder)); +} + +void tmate_encoder_set_ready_callback(struct tmate_encoder *encoder, + tmate_encoder_write_cb *callback, + void *userdata) +{ + encoder->ready_callback = callback; + encoder->userdata = userdata; + if (encoder->ready_callback) + encoder->ready_callback(encoder->userdata, encoder->buffer); +} + +void tmate_decoder_error(void) +{ + /* TODO Don't kill the session, disconnect */ + tmate_print_stack_trace(); + tmate_fatal("Received a bad message"); +} + +void init_unpacker(struct tmate_unpacker *uk, msgpack_object obj) +{ + if (obj.type != MSGPACK_OBJECT_ARRAY) + tmate_decoder_error(); + + uk->argv = obj.via.array.ptr; + uk->argc = obj.via.array.size; +} + +int64_t unpack_int(struct tmate_unpacker *uk) +{ + int64_t val; + + if (uk->argc == 0) + tmate_decoder_error(); + + if (uk->argv[0].type != MSGPACK_OBJECT_POSITIVE_INTEGER && + uk->argv[0].type != MSGPACK_OBJECT_NEGATIVE_INTEGER) + tmate_decoder_error(); + + val = uk->argv[0].via.i64; + + uk->argv++; + uk->argc--; + + return val; +} + +bool unpack_bool(struct tmate_unpacker *uk) +{ + bool val; + + if (uk->argc == 0) + tmate_decoder_error(); + + if (uk->argv[0].type != MSGPACK_OBJECT_BOOLEAN) + tmate_decoder_error(); + + val = uk->argv[0].via.boolean; + + uk->argv++; + uk->argc--; + + return val; +} + +void unpack_buffer(struct tmate_unpacker *uk, const char **buf, size_t *len) +{ + if (uk->argc == 0) + tmate_decoder_error(); + + if (uk->argv[0].type != MSGPACK_OBJECT_STR && + uk->argv[0].type != MSGPACK_OBJECT_BIN) + tmate_decoder_error(); + + *len = uk->argv[0].via.str.size; + *buf = uk->argv[0].via.str.ptr; + + uk->argv++; + uk->argc--; +} + +char *unpack_string(struct tmate_unpacker *uk) +{ + const char *buf; + char *alloc_buf; + size_t len; + + unpack_buffer(uk, &buf, &len); + + alloc_buf = xmalloc(len + 1); + memcpy(alloc_buf, buf, len); + alloc_buf[len] = '\0'; + + return alloc_buf; +} + +void unpack_array(struct tmate_unpacker *uk, struct tmate_unpacker *nested) +{ + if (uk->argc == 0) + tmate_decoder_error(); + + init_unpacker(nested, uk->argv[0]); + + uk->argv++; + uk->argc--; +} + +#define UNPACKER_RESERVE_SIZE 1024 + +void tmate_decoder_init(struct tmate_decoder *decoder, tmate_decoder_reader *reader, + void *userdata) +{ + if (!msgpack_unpacker_init(&decoder->unpacker, UNPACKER_RESERVE_SIZE)) + tmate_fatal("Cannot initialize the unpacker"); + decoder->reader = reader; + decoder->userdata = userdata; +} + +void tmate_decoder_destroy(struct tmate_decoder *decoder) +{ + msgpack_unpacker_destroy(&decoder->unpacker); + memset(decoder, 0, sizeof(*decoder)); +} + +void tmate_decoder_get_buffer(struct tmate_decoder *decoder, + char **buf, size_t *len) +{ + if (!msgpack_unpacker_reserve_buffer(&decoder->unpacker, UNPACKER_RESERVE_SIZE)) + tmate_fatal("cannot expand decoder buffer"); + + *buf = msgpack_unpacker_buffer(&decoder->unpacker); + *len = msgpack_unpacker_buffer_capacity(&decoder->unpacker); +} + +void tmate_decoder_commit(struct tmate_decoder *decoder, size_t len) +{ + struct tmate_unpacker _uk, *uk = &_uk; + msgpack_unpacked result; + + msgpack_unpacker_buffer_consumed(&decoder->unpacker, len); + + msgpack_unpacked_init(&result); + while (msgpack_unpacker_next(&decoder->unpacker, &result)) { + init_unpacker(uk, result.data); + decoder->reader(decoder->userdata, uk); + } + msgpack_unpacked_destroy(&result); +} diff --git a/tmate-protocol.h b/tmate-protocol.h new file mode 100644 index 00000000..81716bb2 --- /dev/null +++ b/tmate-protocol.h @@ -0,0 +1,107 @@ +#ifndef TMATE_PROTOCOL_H +#define TMATE_PROTOCOL_H + +enum tmate_control_out_msg_types { + TMATE_CTL_HEADER, + TMATE_CTL_DEAMON_OUT_MSG, + TMATE_CTL_SNAPSHOT, + TMATE_CTL_CLIENT_JOIN, + TMATE_CTL_CLIENT_LEFT, + TMATE_CTL_EXEC, + TMATE_CTL_LATENCY, +}; + +/* +[TMATE_CTL_HEADER, int: ctl_proto_version, string: ip_address, string: pubkey, + string: session_token, string: session_token_ro, string: ssh_cmd_fmt] + string: client_version, int: client_protocol_version] +[TMATE_CTL_DEAMON_OUT_MSG, object: msg] +[TMATE_CTL_SNAPSHOT, [[int: pane_id, [int: cur_x, int: cur_y], int: mode, + [[string: line_utf8, [int: char_attr, ...]], ...], ...], ...]] +[TMATE_CTL_CLIENT_JOIN, int: client_id, string: ip_address, string: pubkey, boolean: readonly] +[TMATE_CTL_CLIENT_LEFT, int: client_id] +[TMATE_CTL_EXEC, string: username, string: ip_address, string: pubkey, string: command] +[TMATE_CTL_LATENCY, int: client_id, int: latency_ms] // client_id == -1: tmate host +*/ + +enum tmate_control_in_msg_types { + TMATE_CTL_DEAMON_FWD_MSG, + TMATE_CTL_REQUEST_SNAPSHOT, + TMATE_CTL_PANE_KEYS, + TMATE_CTL_RESIZE, + TMATE_CTL_EXEC_RESPONSE, + TMATE_CTL_RENAME_SESSION, +}; + +/* +[TMATE_CTL_DEAMON_FWD_MSG, object: msg] +[TMATE_CTL_REQUEST_SNAPSHOT, int: max_history_lines] +[TMATE_CTL_PANE_KEYS, int: pane_id, string: keys] +[TMATE_CTL_RESIZE, int: sx, int: sy] // sx == -1: no clients +[TMATE_CTL_EXEC_RESPONSE, int: exit_code, string: message] +[TMATE_CTL_RENAME_SESSION, string: stoken, string: stoken_ro] +*/ + +enum tmate_daemon_out_msg_types { + TMATE_OUT_HEADER, + TMATE_OUT_SYNC_LAYOUT, + TMATE_OUT_PTY_DATA, + TMATE_OUT_EXEC_CMD_STR, + TMATE_OUT_FAILED_CMD, + TMATE_OUT_STATUS, + TMATE_OUT_SYNC_COPY_MODE, + TMATE_OUT_WRITE_COPY_MODE, + TMATE_OUT_FIN, + TMATE_OUT_READY, + TMATE_OUT_RECONNECT, + TMATE_OUT_SNAPSHOT, + TMATE_OUT_EXEC_CMD, + TMATE_OUT_UNAME, +}; + +/* +[TMATE_OUT_HEADER, int: proto_version, string: version] +[TMATE_OUT_SYNC_LAYOUT, [int: sx, int: sy, [[int: win_id, string: win_name, + [[int: pane_id, int: sx, int: sy, int: xoff, int: yoff], ...], + int: active_pane_id], ...], int: active_win_id] +[TMATE_OUT_PTY_DATA, int: pane_id, binary: buffer] +[TMATE_OUT_EXEC_CMD_STR, string: cmd] +[TMATE_OUT_FAILED_CMD, int: client_id, string: cause] +[TMATE_OUT_STATUS, string: left, string: right] +[TMATE_OUT_SYNC_COPY_MODE, int: pane_id, [int: backing, int: oy, int: cx, int: cy, + [int: selx, int: sely, int: flags], + [int: type, string: input_prompt, string: input_str]]) + // Any of the array can be [] +[TMATE_OUT_WRITE_COPY_MODE, int: pane_id, string: str] +[TMATE_OUT_FIN] +[TMATE_OUT_READY] +[TMATE_OUT_RECONNECT, string: reconnection_data] +[TMATE_OUT_SNAPSHOT, ...] +[TMATE_OUT_EXEC_CMD, string: cmd_name, ...string: args] +[TMATE_OUT_UNAME, string: name.sysname, string: name.nodename, + string: name.release, string: name.version, string: name.machine] +*/ + +enum tmate_daemon_in_msg_types { + TMATE_IN_NOTIFY, + TMATE_IN_LEGACY_PANE_KEY, + TMATE_IN_RESIZE, + TMATE_IN_EXEC_CMD_STR, + TMATE_IN_SET_ENV, + TMATE_IN_READY, + TMATE_IN_PANE_KEY, + TMATE_IN_EXEC_CMD, +}; + +/* +[TMATE_IN_NOTIFY, string: msg] +[TMATE_IN_PANE_KEY, int: key] +[TMATE_IN_RESIZE, int: sx, int: sy] // sx == -1: no clients +[TMATE_IN_EXEC_CMD_STR, int: client_id, string: cmd] +[TMATE_IN_SET_ENV, string: name, string: value] +[TMATE_IN_READY] +[TMATE_IN_PANE_KEY, int: pane_id, uint64 keycode] // pane_id == -1: active pane +[TMATE_IN_EXEC_CMD, int: client_id, ...string: args] +*/ + +#endif diff --git a/tmate-session.c b/tmate-session.c new file mode 100644 index 00000000..5ba0896c --- /dev/null +++ b/tmate-session.c @@ -0,0 +1,260 @@ +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include "tmate.h" + +#define TMATE_DNS_RETRY_TIMEOUT 2 +#define TMATE_RECONNECT_RETRY_TIMEOUT 2 + +struct tmate_session tmate_session; + +static void lookup_and_connect(void); + +static void on_dns_retry(__unused evutil_socket_t fd, __unused short what, + void *arg) +{ + struct tmate_session *session = arg; + + assert(session->ev_dns_retry); + event_free(session->ev_dns_retry); + session->ev_dns_retry = NULL; + + lookup_and_connect(); +} + +static void dns_cb(int errcode, struct evutil_addrinfo *addr, void *ptr) +{ + struct evutil_addrinfo *ai; + const char *host = ptr; + + evdns_base_free(tmate_session.ev_dnsbase, 0); + tmate_session.ev_dnsbase = NULL; + + if (errcode) { + struct tmate_session *session = &tmate_session; + + if (session->ev_dns_retry) + return; + + struct timeval tv = { .tv_sec = TMATE_DNS_RETRY_TIMEOUT, .tv_usec = 0 }; + + session->ev_dns_retry = evtimer_new(session->ev_base, on_dns_retry, session); + if (!session->ev_dns_retry) + tmate_fatal("out of memory"); + evtimer_add(session->ev_dns_retry, &tv); + + tmate_status_message("%s lookup failure. Retrying in %d seconds (%s)", + host, TMATE_DNS_RETRY_TIMEOUT, + evutil_gai_strerror(errcode)); + return; + } + + tmate_status_message("Connecting to %s...", host); + + int i, num_clients = 0; + for (ai = addr; ai; ai = ai->ai_next) + num_clients++; + + struct tmate_ssh_client *ssh_clients[num_clients]; + + for (ai = addr, i = 0; ai; ai = ai->ai_next, i++) { + char buf[128]; + const char *ip = NULL; + if (ai->ai_family == AF_INET) { + struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr; + ip = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, 128); + } else if (ai->ai_family == AF_INET6) { + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ai->ai_addr; + ip = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, 128); + } + + ssh_clients[i] = tmate_ssh_client_alloc(&tmate_session, ip); + } + + for (i = 0; i < num_clients; i++) + connect_ssh_client(ssh_clients[i]); + + evutil_freeaddrinfo(addr); +} + +static void lookup_and_connect(void) +{ + struct evutil_addrinfo hints; + const char *tmate_server_host; + + assert(!tmate_session.ev_dnsbase); + tmate_session.ev_dnsbase = evdns_base_new(tmate_session.ev_base, 1); + if (!tmate_session.ev_dnsbase) + tmate_fatal("Cannot initialize the DNS lookup service"); + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_flags = EVUTIL_AI_ADDRCONFIG; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + + tmate_server_host = options_get_string(global_options, + "tmate-server-host"); + tmate_debug("Looking up %s...", tmate_server_host); + (void)evdns_getaddrinfo(tmate_session.ev_dnsbase, tmate_server_host, NULL, + &hints, dns_cb, (void *)tmate_server_host); +} + +static void __tmate_session_init(struct tmate_session *session, + struct event_base *base) +{ + memset(session, 0, sizeof(*session)); + + session->ev_base = base; + + /* + * Early initialization of encoder because we need to parse + * config files to get the server configs, but while we are parsing + * config files, we need to buffer bind commands and all for the + * slave. + * Decoder is setup later. + */ + tmate_encoder_init(&session->encoder, NULL, &tmate_session); + + session->min_sx = -1; + session->min_sy = -1; + + TAILQ_INIT(&session->clients); +} + +void tmate_session_init(struct event_base *base) +{ + __tmate_session_init(&tmate_session, base); + tmate_write_header(); +} + +static void send_authorized_keys(void) +{ + char *path; + path = options_get_string(global_options, "tmate-authorized-keys"); + if (strlen(path) == 0) + return; + + path = xstrdup(path); + tmate_info("Using %s for access control", path); + + FILE *f; + char *line; + size_t len; + + if (path[0] == '~' && path[1] == '/') { + const char *home = find_home(); + if (home) { + char *new_path; + xasprintf(&new_path, "%s%s", home, &path[1]); + free(path); + path = new_path; + } + } + + if ((f = fopen(path, "r")) == NULL) { + cfg_add_cause("%s: %s", path, strerror(errno)); + free(path); + return; + } + + while ((line = fparseln(f, &len, NULL, NULL, 0)) != NULL) { + if (len == 0) + continue; + tmate_set_val("authorized_keys", line); + free(line); + } + + if (ferror(f)) + cfg_add_cause("%s: %s", path, strerror(errno)); + + fclose(f); + free(path); +} + +void tmate_session_start(void) +{ + /* + * We split init and start because: + * - We need to process the tmux config file during the connection as + * we are setting up the tmate identity. + * - While we are parsing the config file, we need to be able to + * serialize it, and so we need a worker encoder. + */ + if (tmate_foreground) { + tmate_set_val("foreground", "true"); + tmate_info("To connect to the session locally, run: tmate -S %s attach", socket_path); + } else { + cfg_add_cause("%s", "Tip: if you wish to use tmate only for remote access, run: tmate -F"); + cfg_add_cause("%s", "To see the following messages again, run in a tmate session: tmate show-messages"); + cfg_add_cause("%s", "Press or to continue"); + cfg_add_cause("%s", "---------------------------------------------------------------------"); + } + + send_authorized_keys(); + tmate_write_uname(); + tmate_write_ready(); + lookup_and_connect(); +} + +static void on_reconnect_retry(__unused evutil_socket_t fd, __unused short what, void *arg) +{ + struct tmate_session *session = arg; + + assert(session->ev_connection_retry); + event_free(session->ev_connection_retry); + session->ev_connection_retry = NULL; + + if (session->last_server_ip) { + /* + * We have a previous server ip. Let's try that again first, + * but then connect to any server if it fails again. + */ + struct tmate_ssh_client *c = tmate_ssh_client_alloc(session, + session->last_server_ip); + connect_ssh_client(c); + free(session->last_server_ip); + session->last_server_ip = NULL; + } else { + lookup_and_connect(); + } +} + +void tmate_reconnect_session(struct tmate_session *session, const char *message) +{ + /* + * We no longer have an SSH connection. Time to reconnect. + * We'll reuse some of the session information if we can, + * and we'll try to reconnect to the same server if possible, + * to avoid an SSH connection string change. + */ + struct timeval tv = { .tv_sec = TMATE_RECONNECT_RETRY_TIMEOUT, .tv_usec = 0 }; + + if (session->ev_connection_retry) + return; + + session->ev_connection_retry = evtimer_new(session->ev_base, on_reconnect_retry, session); + if (!session->ev_connection_retry) + tmate_fatal("out of memory"); + evtimer_add(session->ev_connection_retry, &tv); + + if (message && !tmate_foreground) + tmate_status_message("Reconnecting... (%s)", message); + else + tmate_status_message("Reconnecting..."); + + /* + * This says that we'll need to send a snapshot of the current state. + */ + session->reconnected = true; +} diff --git a/tmate-ssh-client.c b/tmate-ssh-client.c new file mode 100644 index 00000000..bed79e95 --- /dev/null +++ b/tmate-ssh-client.c @@ -0,0 +1,585 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "tmate.h" +#include "window-copy.h" + +static void on_ssh_client_event(struct tmate_ssh_client *client); +static void __on_ssh_client_event(evutil_socket_t fd, short what, void *arg); + +static void printflike(2, 3) kill_ssh_client(struct tmate_ssh_client *client, + const char *fmt, ...); +static void printflike(2, 3) kill_ssh_client(struct tmate_ssh_client *client, + const char *fmt, ...); + +static void read_channel(struct tmate_ssh_client *client) +{ + struct tmate_decoder *decoder = &client->tmate_session->decoder; + char *buf; + ssize_t len; + + for (;;) { + tmate_decoder_get_buffer(decoder, &buf, &len); + len = ssh_channel_read_nonblocking(client->channel, buf, len, 0); + if (len < 0) { + kill_ssh_client(client, "Error reading from channel: %s", + ssh_get_error(client->session)); + break; + } + + if (len == 0) + break; + + tmate_decoder_commit(decoder, len); + } +} + +static void on_decoder_read(void *userdata, struct tmate_unpacker *uk) +{ + struct tmate_ssh_client *client = userdata; + tmate_dispatch_slave_message(client->tmate_session, uk); +} + +static void on_encoder_write(void *userdata, struct evbuffer *buffer) +{ + struct tmate_ssh_client *client = userdata; + ssize_t len, written; + unsigned char *buf; + + if (!client->channel) + return; + + for(;;) { + len = evbuffer_get_length(buffer); + if (!len) + break; + + buf = evbuffer_pullup(buffer, -1); + + written = ssh_channel_write(client->channel, buf, len); + if (written < 0) { + kill_ssh_client(client, "Error writing to channel: %s", + ssh_get_error(client->session)); + break; + } + + evbuffer_drain(buffer, written); + } +} + +static void on_ssh_auth_server_complete(struct tmate_ssh_client *connected_client) +{ + /* + * The first ssh connection succeeded. Hopefully this one offers the + * best latency. We can now kill the other ssh clients that are trying + * to connect. + */ + struct tmate_session *session = connected_client->tmate_session; + struct tmate_ssh_client *client, *tmp_client; + + TAILQ_FOREACH_SAFE(client, &session->clients, node, tmp_client) { + if (client == connected_client) + continue; + + kill_ssh_client(client, NULL); + } +} + +static char *get_identity(void) +{ + char *identity; + + identity = options_get_string(global_options, "tmate-identity"); + if (!strlen(identity)) + return NULL; + + if (strchr(identity, '/')) + identity = xstrdup(identity); + else + xasprintf(&identity, "%%d/%s", identity); + + return identity; +} + +static int passphrase_callback(__unused const char *prompt, char *buf, size_t len, + __unused int echo, __unused int verify, void *userdata) +{ + struct tmate_ssh_client *client = userdata; + + client->tmate_session->need_passphrase = 1; + + if (client->tmate_session->passphrase) + strlcpy(buf, client->tmate_session->passphrase, len); + else + strcpy(buf, ""); + + return 0; +} + +static void on_passphrase_read(const char *passphrase, void *private) +{ + struct tmate_ssh_client *client = private; + + client->tmate_session->passphrase = xstrdup(passphrase); + on_ssh_client_event(client); +} + +static void request_passphrase(struct tmate_ssh_client *client) +{ + struct window_pane *wp; + struct window_copy_mode_data *data; + + /* + * We'll display the prompt on the first pane. + * It doesn't make much sense, but it's simpler to reuse the copy mode + * and its key parsing logic compared to rolling something on our own. + */ + wp = RB_MIN(window_pane_tree, &all_window_panes); + + if (wp->mode) { + data = wp->modedata; + if (data->inputtype == WINDOW_COPY_PASSWORD) { + /* We are already requesting the passphrase */ + return; + } + window_pane_reset_mode(wp); + } + + window_pane_set_mode(wp, &window_copy_mode); + window_copy_init_from_pane(wp, 0); + data = wp->modedata; + + data->inputtype = WINDOW_COPY_PASSWORD; + data->inputprompt = "SSH key passphrase"; + + mode_key_init(&data->mdata, &mode_key_tree_vi_edit); + + window_copy_update_selection(wp, 1); + window_copy_redraw_screen(wp); + + data->password_cb = on_passphrase_read; + data->password_cb_private = client; +} + +#define KEEPALIVE_IDLE 30 +#define KEEPALIVE_CNT 4 +#define KEEPALIVE_INTVL 11 +#define WRITE_TIMEOUT 80 + +static void tune_socket_opts(int fd) +{ +#define SSO(level, optname, val) ({ \ + int _flag = val; \ + if (setsockopt(fd, level, optname, &(_flag), sizeof(int)) < 0) { \ + /* If the connection has been closed, we'll get EINVAL */ \ + if (errno != EINVAL) \ + tmate_info("setsockopt(" #level ", " #optname ", %d) failed %s", val, strerror(errno)); \ + } \ +}) + + SSO(IPPROTO_IP, IP_TOS, 0x10); /* IPTOS_LOWDELAY */ + SSO(IPPROTO_TCP, TCP_NODELAY, 1); + SSO(SOL_SOCKET, SO_KEEPALIVE, 1); +#ifdef TCP_KEEPALIVE + /* + * The TCP_KEEPALIVE options enable to specify the amount of time, in + * seconds, that the connection must be idle before keepalive probes + * (if enabled) are sent. + */ + SSO(IPPROTO_TCP, TCP_KEEPALIVE, KEEPALIVE_IDLE); +#endif +#ifdef TCP_KEEPIDLE + /* + * Same as TCP_KEEPALIVE, but on different systems + */ + SSO(IPPROTO_TCP, TCP_KEEPIDLE, KEEPALIVE_IDLE); +#endif +#ifdef TCP_KEEPCNT + /* + * When keepalive probes are enabled, this option will set the number + * of times a keepalive probe should be repeated if the peer is not + * responding. After this many probes, the connection will be closed. + */ + SSO(IPPROTO_TCP, TCP_KEEPCNT, KEEPALIVE_CNT); +#endif +#ifdef TCP_KEEPINTVL + /* + * When keepalive probes are enabled, this option will set the amount + * of time in seconds between successive keepalives sent to probe an + * unresponsive peer. + */ + SSO(IPPROTO_TCP, TCP_KEEPINTVL, KEEPALIVE_INTVL); +#endif +#ifdef TCP_USER_TIMEOUT + /* + * This option takes an unsigned int as an argument. When the + * value is greater than 0, it specifies the maximum amount of + * time in milliseconds that transmitted data may remain + * unacknowledged before TCP will forcibly close the + * corresponding connection and return ETIMEDOUT to the + * application. + */ + SSO(IPPROTO_TCP, TCP_USER_TIMEOUT, 1000*WRITE_TIMEOUT); +#endif +#undef SSO +} + +static void init_conn_fd(struct tmate_ssh_client *client) +{ + int fd; + + if (client->ev_ssh) + return; + + if ((fd = ssh_get_fd(client->session)) < 0) + return; + + tune_socket_opts(fd); + + client->ev_ssh = event_new(client->tmate_session->ev_base, fd, + EV_READ | EV_PERSIST, + __on_ssh_client_event, client); + if (!client->ev_ssh) + tmate_fatal("out of memory"); + event_add(client->ev_ssh, NULL); +} + +static void on_ssh_client_event(struct tmate_ssh_client *client) +{ + ssh_session session = client->session; + ssh_channel channel = client->channel; + + switch (client->state) { + case SSH_INIT: { + client->session = session = ssh_new(); + if (!session) { + tmate_fatal("cannot ssh_new()"); + return; + } + + ssh_set_callbacks(session, &client->ssh_callbacks); + + int verbosity = SSH_LOG_NOLOG + log_get_level(); + int port = options_get_number(global_options, "tmate-server-port"); + + ssh_set_blocking(session, 0); + ssh_options_set(session, SSH_OPTIONS_HOST, client->server_ip); + ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); + ssh_options_set(session, SSH_OPTIONS_PORT, &port); + ssh_options_set(session, SSH_OPTIONS_USER, "tmate"); + ssh_options_set(session, SSH_OPTIONS_COMPRESSION, "yes"); + + char *identity; + if ((identity = get_identity())) { + /* + * FIXME libssh will continue with the next set of + * keys if the identity has a passphrase and the + * regular one doesn't. + */ + ssh_options_set(session, SSH_OPTIONS_IDENTITY, identity); + + /* Do not use keys from ssh-agent. */ + unsetenv("SSH_AUTH_SOCK"); + free(identity); + } + + client->state = SSH_CONNECT; + } + // fall through + + case SSH_CONNECT: + switch (ssh_connect(session)) { + case SSH_AGAIN: + init_conn_fd(client); + return; + case SSH_ERROR: + kill_ssh_client(client, "Error connecting: %s", + ssh_get_error(session)); + return; + case SSH_OK: + init_conn_fd(client); + + tmate_debug("Establishing connection to %s", client->server_ip); + client->state = SSH_AUTH_SERVER; + } + // fall through + + case SSH_AUTH_SERVER: { + ssh_key pubkey; + enum ssh_keytypes_e key_type; + unsigned char *hash; + ssize_t hash_len; + char *hash_str; + const char *server_hash_str; + int match; + +#if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0) + if (ssh_get_server_publickey(session, &pubkey) < 0) + tmate_fatal("ssh_get_server_publickey"); +#else + if (ssh_get_publickey(session, &pubkey) < 0) + tmate_fatal("ssh_get_publickey"); +#endif + + if (ssh_get_publickey_hash(pubkey, SSH_PUBLICKEY_HASH_SHA256, + &hash, &hash_len) < 0) { + kill_ssh_client(client, "Failed to get server fingerprint"); + return; + } + + hash_str = ssh_get_fingerprint_hash(SSH_PUBLICKEY_HASH_SHA256, + hash, hash_len); + if (!hash_str) + tmate_fatal("malloc failed"); + + key_type = ssh_key_type(pubkey); + + switch (key_type) { + case SSH_KEYTYPE_RSA: + server_hash_str = options_get_string(global_options, + "tmate-server-rsa-fingerprint"); + break; + case SSH_KEYTYPE_ECDSA: +#if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 9, 0) + case SSH_KEYTYPE_ECDSA_P256: + case SSH_KEYTYPE_ECDSA_P384: + case SSH_KEYTYPE_ECDSA_P521: +#endif + server_hash_str = options_get_string(global_options, + "tmate-server-ecdsa-fingerprint"); + break; + case SSH_KEYTYPE_ED25519: + server_hash_str = options_get_string(global_options, + "tmate-server-ed25519-fingerprint"); + break; + default: + server_hash_str = ""; + } + + match = !strcmp(hash_str, server_hash_str); + if (!match) { + kill_ssh_client(client, "Server fingerprint not recognized: " + "`%s', expected `%s'", server_hash_str, hash_str); + } + + ssh_key_free(pubkey); + ssh_clean_pubkey_hash(&hash); + free(hash_str); + + if (!match) + return; + + /* + * At this point, we abort other connection attempts to the + * other tmate servers, since we have reached the fastest one. + * We need to do it before we ask the user its passphrase, + * otherwise the speed test would be biased. + */ + tmate_debug("Connected to %s", client->server_ip); + on_ssh_auth_server_complete(client); + + client->state = SSH_AUTH_CLIENT_NONE; + } + // fall through + + case SSH_AUTH_CLIENT_NONE: + switch (ssh_userauth_none(session, NULL)) { + case SSH_AUTH_AGAIN: + return; + case SSH_AUTH_ERROR: + kill_ssh_client(client, "Auth error: %s", ssh_get_error(session)); + return; + case SSH_AUTH_SUCCESS: + tmate_debug("Auth successful via none method"); + client->state = SSH_NEW_CHANNEL; + goto SSH_NEW_CHANNEL; + case SSH_AUTH_PARTIAL: + case SSH_AUTH_DENIED: + client->state = SSH_AUTH_CLIENT_PUBKEY; + } + // fall through + + case SSH_AUTH_CLIENT_PUBKEY: + client->tried_passphrase = client->tmate_session->passphrase; + switch (ssh_userauth_publickey_auto(session, NULL, client->tried_passphrase)) { + case SSH_AUTH_AGAIN: + return; + case SSH_AUTH_PARTIAL: + case SSH_AUTH_INFO: + case SSH_AUTH_DENIED: + if (client->tmate_session->need_passphrase) { + request_passphrase(client); + } else { + kill_ssh_client(client, "SSH keys not found." + " Run 'ssh-keygen' to create keys."); + return; + } + + if (client->tried_passphrase) + tmate_status_message("Can't load SSH key." + " Try typing passphrase again in case of typo. ctrl-c to abort."); + return; + case SSH_AUTH_ERROR: + kill_ssh_client(client, "Auth error: %s", ssh_get_error(session)); + return; + case SSH_AUTH_SUCCESS: + tmate_debug("Auth successful with pubkey"); + client->state = SSH_NEW_CHANNEL; + } + // fall through + +SSH_NEW_CHANNEL: + case SSH_NEW_CHANNEL: + client->channel = channel = ssh_channel_new(session); + if (!channel) { + tmate_fatal("cannot ssh_channel_new()"); + return; + } + client->state = SSH_OPEN_CHANNEL; + // fall through + + case SSH_OPEN_CHANNEL: + switch (ssh_channel_open_session(channel)) { + case SSH_AGAIN: + return; + case SSH_ERROR: + kill_ssh_client(client, "Error opening channel: %s", + ssh_get_error(session)); + return; + case SSH_OK: + tmate_debug("Session opened, initializing tmate"); + client->state = SSH_BOOTSTRAP; + } + // fall through + + case SSH_BOOTSTRAP: + switch (ssh_channel_request_subsystem(channel, "tmate")) { + case SSH_AGAIN: + return; + case SSH_ERROR: + kill_ssh_client(client, "Error initializing tmate: %s", + ssh_get_error(session)); + return; + case SSH_OK: + tmate_debug("Ready"); + + /* Writes are now performed in a blocking fashion */ + ssh_set_blocking(session, 1); + + client->state = SSH_READY; + + if (client->tmate_session->reconnected) + tmate_send_reconnection_state(client->tmate_session); + + tmate_encoder_set_ready_callback(&client->tmate_session->encoder, + on_encoder_write, client); + tmate_decoder_init(&client->tmate_session->decoder, + on_decoder_read, client); + + free(client->tmate_session->last_server_ip); + client->tmate_session->last_server_ip = xstrdup(client->server_ip); + } + // fall through + + case SSH_READY: + read_channel(client); + } +} + +static void __on_ssh_client_event(__unused evutil_socket_t fd, __unused short what, void *arg) +{ + on_ssh_client_event(arg); +} + +static void kill_ssh_client(struct tmate_ssh_client *client, + const char *fmt, ...) +{ + bool last_client; + va_list ap; + char *message = NULL; + + TAILQ_REMOVE(&client->tmate_session->clients, client, node); + last_client = TAILQ_EMPTY(&client->tmate_session->clients); + + if (fmt && last_client) { + va_start(ap, fmt); + xvasprintf(&message, fmt, ap); + va_end(ap); + tmate_status_message("%s", message); + } + + tmate_debug("SSH client killed (%s)", client->server_ip); + + if (client->ev_ssh) { + event_del(client->ev_ssh); + event_free(client->ev_ssh); + client->ev_ssh = NULL; + } + + if (client->state == SSH_READY) { + tmate_encoder_set_ready_callback(&client->tmate_session->encoder, NULL, NULL); + tmate_decoder_destroy(&client->tmate_session->decoder); + + client->tmate_session->min_sx = -1; + client->tmate_session->min_sy = -1; + recalculate_sizes(); + } + + if (client->session) { + /* ssh_free() also frees the associated channels. */ + ssh_free(client->session); + client->session = NULL; + client->channel = NULL; + } + + if (last_client) + tmate_reconnect_session(client->tmate_session, message); + + free(client->server_ip); + free(client); +} + +void connect_ssh_client(struct tmate_ssh_client *client) +{ + assert(!client->session); + client->state = SSH_INIT; + on_ssh_client_event(client); +} + +static void ssh_log_function(int priority, const char *function, + const char *buffer, __unused void *userdata) +{ + tmate_debug("[%d] [%s] %s", priority, function, buffer); +} + +struct tmate_ssh_client *tmate_ssh_client_alloc(struct tmate_session *session, + const char *server_ip) + +{ + struct tmate_ssh_client *client; + client = xmalloc(sizeof(*client)); + memset(client, 0, sizeof(*client)); + + ssh_set_log_callback(ssh_log_function); + + memset(&client->ssh_callbacks, 0, sizeof(client->ssh_callbacks)); + ssh_callbacks_init(&client->ssh_callbacks); + client->ssh_callbacks.userdata = client; + client->ssh_callbacks.auth_function = passphrase_callback; + + client->tmate_session = session; + TAILQ_INSERT_TAIL(&session->clients, client, node); + + client->server_ip = xstrdup(server_ip); + client->state = SSH_NONE; + client->session = NULL; + client->channel = NULL; + + return client; +} diff --git a/tmate.1 b/tmate.1 new file mode 120000 index 00000000..44d7405b --- /dev/null +++ b/tmate.1 @@ -0,0 +1 @@ +tmux.1 \ No newline at end of file diff --git a/tmate.h b/tmate.h new file mode 100644 index 00000000..f4783dc8 --- /dev/null +++ b/tmate.h @@ -0,0 +1,218 @@ +#ifndef TMATE_H +#define TMATE_H + +#include +#include +#include +#include +#include + +#include "tmux.h" + +#define tmate_debug(...) log_emit(LOG_DEBUG, __VA_ARGS__) +#define tmate_info(...) log_emit(LOG_INFO, __VA_ARGS__) +#define tmate_fatal(...) fatalx( __VA_ARGS__) + +/* tmate-msgpack.c */ + +typedef void tmate_encoder_write_cb(void *userdata, struct evbuffer *buffer); + +struct tmate_encoder { + msgpack_packer pk; + tmate_encoder_write_cb *ready_callback; + void *userdata; + struct evbuffer *buffer; + struct event *ev_buffer; + bool ev_active; +}; + +extern void tmate_encoder_init(struct tmate_encoder *encoder, + tmate_encoder_write_cb *callback, + void *userdata); +extern void tmate_encoder_destroy(struct tmate_encoder *encoder); +extern void tmate_encoder_set_ready_callback(struct tmate_encoder *encoder, + tmate_encoder_write_cb *callback, + void *userdata); + +extern void msgpack_pack_string(msgpack_packer *pk, const char *str); +extern void msgpack_pack_boolean(msgpack_packer *pk, bool value); + +#define _pack(enc, what, ...) msgpack_pack_##what(&(enc)->pk, ##__VA_ARGS__) + +struct tmate_unpacker; +struct tmate_decoder; +typedef void tmate_decoder_reader(void *userdata, struct tmate_unpacker *uk); + +struct tmate_decoder { + struct msgpack_unpacker unpacker; + tmate_decoder_reader *reader; + void *userdata; +}; + +extern void tmate_decoder_init(struct tmate_decoder *decoder, tmate_decoder_reader *reader, void *userdata); +extern void tmate_decoder_destroy(struct tmate_decoder *decoder); +extern void tmate_decoder_get_buffer(struct tmate_decoder *decoder, char **buf, size_t *len); +extern void tmate_decoder_commit(struct tmate_decoder *decoder, size_t len); + +struct tmate_unpacker { + int argc; + msgpack_object *argv; +}; + +extern void init_unpacker(struct tmate_unpacker *uk, msgpack_object obj); +extern void tmate_decoder_error(void); +extern int64_t unpack_int(struct tmate_unpacker *uk); +extern bool unpack_bool(struct tmate_unpacker *uk); +extern void unpack_buffer(struct tmate_unpacker *uk, const char **buf, size_t *len); +extern char *unpack_string(struct tmate_unpacker *uk); +extern void unpack_array(struct tmate_unpacker *uk, struct tmate_unpacker *nested); + +#define unpack_each(nested_uk, tmp_uk, uk) \ + for (unpack_array(uk, tmp_uk); \ + (tmp_uk)->argc > 0 && (init_unpacker(nested_uk, (tmp_uk)->argv[0]), 1); \ + (tmp_uk)->argv++, (tmp_uk)->argc--) + +/* tmate-encoder.c */ + +#define TMATE_PROTOCOL_VERSION 6 + +struct tmate_session; + +extern void tmate_write_header(void); +extern void tmate_write_uname(void); +extern void tmate_write_ready(void); +extern void tmate_sync_layout(void); +extern void tmate_pty_data(struct window_pane *wp, const char *buf, size_t len); +extern int tmate_should_replicate_cmd(const struct cmd_entry *cmd); +extern void tmate_set_val(const char *name, const char *value); +extern void tmate_exec_cmd_args(int argc, const char **argv); +extern void tmate_exec_cmd(struct cmd *cmd); +extern void tmate_failed_cmd(int client_id, const char *cause); +extern void tmate_status(const char *left, const char *right); +extern void tmate_sync_copy_mode(struct window_pane *wp); +extern void tmate_write_copy_mode(struct window_pane *wp, const char *str); +extern void tmate_write_fin(void); +extern void tmate_send_reconnection_state(struct tmate_session *session); + +/* tmate-decoder.c */ + +struct tmate_session; +extern void tmate_dispatch_slave_message(struct tmate_session *session, + struct tmate_unpacker *uk); + +/* tmate-ssh-client.c */ + +enum tmate_ssh_client_state_types { + SSH_NONE, + SSH_INIT, + SSH_CONNECT, + SSH_AUTH_SERVER, + SSH_AUTH_CLIENT_NONE, + SSH_AUTH_CLIENT_PUBKEY, + SSH_NEW_CHANNEL, + SSH_OPEN_CHANNEL, + SSH_BOOTSTRAP, + SSH_READY, +}; + +struct tmate_ssh_client { + /* XXX The "session" word is used for three things: + * - the ssh session + * - the tmate sesssion + * - the tmux session + * A tmux session is associated 1:1 with a tmate session. + * An ssh session belongs to a tmate session, and a tmate session + * has one ssh session, except during bootstrapping where + * there is one ssh session per tmate server, and the first one wins. + */ + struct tmate_session *tmate_session; + TAILQ_ENTRY(tmate_ssh_client) node; + + char *server_ip; + + int state; + + /* + * ssh_callbacks is allocated because the libssh API sucks (userdata + * has to be in the struct itself). + */ + struct ssh_callbacks_struct ssh_callbacks; + char *tried_passphrase; + ssh_session session; + ssh_channel channel; + + struct event *ev_ssh; +}; +TAILQ_HEAD(tmate_ssh_clients, tmate_ssh_client); + +extern void connect_ssh_client(struct tmate_ssh_client *client); +extern struct tmate_ssh_client *tmate_ssh_client_alloc(struct tmate_session *session, + const char *server_ip); + +/* tmate-session.c */ + +struct tmate_session { + struct event_base *ev_base; + struct evdns_base *ev_dnsbase; + struct event *ev_dns_retry; + + struct tmate_encoder encoder; + struct tmate_decoder decoder; + + /* True when the slave has sent all the environment variables */ + int tmate_env_ready; + + int min_sx; + int min_sy; + + /* + * This list contains one connection per IP. The first connected + * client wins, and saved in *client. When we have a winner, the + * losers are disconnected and killed. + */ + struct tmate_ssh_clients clients; + int need_passphrase; + char *passphrase; + + bool reconnected; + struct event *ev_connection_retry; + char *last_server_ip; + char *reconnection_data; + /* + * When we reconnect, instead of serializing the key bindings and + * options, we replay all the tmux commands we replicated. + * It may be a little innacurate to replicate the state, but + * it's much easier. + */ + struct { + unsigned int capacity; + unsigned int tail; + struct { + int argc; + char **argv; + } *cmds; + } saved_tmux_cmds; +}; + +extern struct tmate_session tmate_session; +extern void tmate_session_init(struct event_base *base); +extern void tmate_session_start(void); +extern void tmate_reconnect_session(struct tmate_session *session, const char *message); + +/* tmate-debug.c */ +extern void tmate_print_stack_trace(void); +extern void tmate_catch_sigsegv(void); +extern void tmate_preload_trace_lib(void); + +/* tmate-msg.c */ + +extern void __tmate_status_message(const char *fmt, va_list ap); +extern void printflike(1, 2) tmate_status_message(const char *fmt, ...); + +/* tmate-env.c */ + +extern int tmate_has_received_env(void); +extern void tmate_set_env(const char *name, const char *value); +extern void tmate_format(struct format_tree *ft); + +#endif diff --git a/tmux.1 b/tmux.1 index f615dd0f..55f88c98 100644 --- a/tmux.1 +++ b/tmux.1 @@ -1,6 +1,6 @@ -.\" $Id$ +.\" $OpenBSD$ .\" -.\" Copyright (c) 2007 Nicholas Marriott +.\" Copyright (c) 2007 Nicholas Marriott .\" .\" Permission to use, copy, modify, and distribute this software for any .\" purpose with or without fee is hereby granted, provided that the above @@ -15,15 +15,16 @@ .\" OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" .Dd $Mdocdate: March 25 2013 $ -.Dt TMUX 1 +.Dt TMATE 1 .Os .Sh NAME -.Nm tmux -.Nd terminal multiplexer +.Nm tmate +.Nd terminal multiplexer with instant terminal sharing .Sh SYNOPSIS -.Nm tmux +.Nm tmate .Bk -words -.Op Fl 2lCquvV +.Op show-messages +.Op Fl 2CluvV .Op Fl c Ar shell-command .Op Fl f Ar file .Op Fl L Ar socket-name @@ -32,13 +33,24 @@ .Ek .Sh DESCRIPTION .Nm -is a terminal multiplexer: +is a terminal multiplexer with instant terminal sharing: it enables a number of terminals to be created, accessed, and -controlled from a single screen. +controlled from a single screen and be shared with another mates. .Nm may be detached from a screen and continue running in the background, -then later reattached. +then later reattached, like as a daemon. +.Pp +.Nm +provides an instant pairing solution, allowing you to share a terminal +with one or several teammates. Together with a voice call, it's almost like +pairing in person. The terminal sharing works by using SSH connections to +backend servers maintained by tmate upstream developers; teammates need to be +given a randomly-generated token to be able to join a session. +.Pp +.Nm +is a modified version of tmux, and uses the same configurations such as +keybindings, color schemes, etc. .Pp When .Nm @@ -47,9 +59,9 @@ is started it creates a new with a single .Em window and displays it on screen. -A status line at the bottom of the screen -shows information on the current session -and is used to enter interactive commands. +A status line at the bottom of the screen shows information +on the current session, such as ssh command to share with +your mate, and is used to enter interactive commands. .Pp A session is a single collection of .Em pseudo terminals @@ -61,7 +73,7 @@ A window occupies the entire screen and may be split into rectangular panes, each of which is a separate pseudo terminal (the -.Xr pty 7 +.Xr pty 4 manual page documents the technical details of pseudo terminals). Any number of .Nm @@ -80,7 +92,7 @@ key strokes). .Nm may be reattached using: .Pp -.Dl $ tmux attach +.Dl $ tmate attach .Pp In .Nm , @@ -98,6 +110,10 @@ The options are as follows: Force .Nm to assume the terminal supports 256 colours. +.It Fl a Ar file +Limit access to the public keys listed in the +.Ar file +given as argument. .It Fl C Start in control mode (see the .Sx CONTROL MODE @@ -126,7 +142,9 @@ By default, loads the system configuration file from .Pa @SYSCONFDIR@/tmux.conf , if present, then looks for a user configuration file at -.Pa ~/.tmux.conf . +.Pa ~/.tmux.conf +and +.Pa ~/.tmate.conf . .Pp The configuration file is a set of .Nm @@ -143,11 +161,10 @@ session created, and continues to process the rest of the configuration file. .It Fl L Ar socket-name .Nm stores the server socket in a directory under -.Ev TMUX_TMPDIR , -.Ev TMPDIR -if it is unset, or +.Ev TMUX_TMPDIR +or .Pa /tmp -if both are unset. +if it is unset. The default socket is named .Em default . This option allows a different socket name to be specified, allowing several @@ -163,11 +180,14 @@ If the socket is accidentally removed, the .Dv SIGUSR1 signal may be sent to the .Nm -server process to recreate it. +server process to recreate it (note that this will fail if any parent +directories are missing). .It Fl l Behave as a login shell. This flag currently has no effect and is for compatibility with other shells -when using tmux as a login shell. +when using +.Nm +as a login shell. .It Fl S Ar socket-path Specify a full alternative path to the server socket. If @@ -190,13 +210,11 @@ flag explicitly informs .Nm that UTF-8 is supported. .Pp -If the server is started from a client passed -.Fl u -or where UTF-8 is detected, the -.Ic utf8 -and -.Ic status-utf8 -options are enabled in the global window and session options respectively. +Note that +.Nm +itself always accepts UTF-8; this controls whether it will send UTF-8 +characters to the terminal it is running (if not, they are replaced by +.Ql _ ) . .It Fl v Request verbose logging. This option may be specified multiple times for increasing verbosity. @@ -301,6 +319,12 @@ Change to the previous window. Briefly display pane indexes. .It r Force redraw of the attached client. +.It m +Mark the current pane (see +.Ic select-pane +.Fl m ) . +.It M +Clear the marked pane. .It s Select a new session for the attached client interactively. .It t @@ -354,6 +378,8 @@ This section contains a list of the commands supported by .Nm . Most commands accept the optional .Fl t +(and sometimes +.Fl s ) argument with one of .Ar target-client , .Ar target-session @@ -361,41 +387,63 @@ argument with one of or .Ar target-pane . These specify the client, session, window or pane which a command should affect. +.Pp .Ar target-client is the name of the -.Xr pty 7 +.Xr pty 4 file to which the client is connected, for example either of .Pa /dev/ttyp1 or .Pa ttyp1 for the client attached to .Pa /dev/ttyp1 . -If no client is specified, the current client is chosen, if possible, or an -error is reported. +If no client is specified, +.Nm +attempts to work out the client currently in use; if that fails, an error is +reported. Clients may be listed with the .Ic list-clients command. .Pp .Ar target-session -is the session id prefixed with a $, the name of a session (as listed by the +is tried as, in order: +.Bl -enum -offset Ds +.It +A session ID prefixed with a $. +.It +An exact name of a session (as listed by the .Ic list-sessions -command), or the name of a client with the same syntax as -.Ar target-client , -in which case the session attached to the client is used. -When looking for the session name, -.Nm -initially searches for an exact match; if none is found, the session names -are checked for any for which -.Ar target-session -is a prefix or for which it matches as an +command). +.It +The start of a session name, for example +.Ql mysess +would match a session named +.Ql mysession . +.It +An .Xr fnmatch 3 -pattern. -If a single match is found, it is used as the target session; multiple matches +pattern which is matched against the session name. +.El +.Pp +If the session name is prefixed with an +.Ql = , +only an exact match is accepted (so +.Ql =mysess +will only match exactly +.Ql mysess , +not +.Ql mysession ) . +.Pp +If a single session is found, it is used as the target session; multiple matches produce an error. If a session is omitted, the current session is used if available; if no current session is available, the most recently used is chosen. .Pp .Ar target-window +(or +.Ar src-window +or +.Ar dst-window ) specifies a window in the form .Em session Ns \&: Ns Em window . .Em session @@ -403,12 +451,32 @@ follows the same rules as for .Ar target-session , and .Em window -is looked for in order: as a window index, for example mysession:1; -as a window ID, such as @1; -as an exact window name, such as mysession:mywindow; then as an +is looked for in order as: +.Bl -enum -offset Ds +.It +A special token, listed below. +.It +A window index, for example +.Ql mysession:1 +is window 1 in session +.Ql mysession . +.It +A window ID, such as @1. +.It +An exact window name, such as +.Ql mysession:mywindow . +.It +The start of a window name, such as +.Ql mysession:mywin . +.It +As an .Xr fnmatch 3 -pattern or the start of a window name, such as mysession:mywin* or -mysession:mywin. +pattern matched against the window name. +.El +.Pp +Like sessions, a +.Ql = +prefix will do an exact match only. An empty window name specifies the next unused index if appropriate (for example the .Ic new-window @@ -418,53 +486,51 @@ commands) otherwise the current window in .Em session is chosen. -The special character -.Ql \&! -uses the last (previously current) window, -.Ql ^ -selects the highest numbered window, -.Ql $ -selects the lowest numbered window, and -.Ql + -and -.Ql - -select the next window or the previous window by number. -When the argument does not contain a colon, -.Nm -first attempts to parse it as window; if that fails, an attempt is made to -match a session. +.Pp +The following special tokens are available to indicate particular windows. +Each has a single-character alternative form. +.Bl -column "XXXXXXXXXX" "X" +.It Sy "Token" Ta Sy "" Ta Sy "Meaning" +.It Li "{start}" Ta "^" Ta "The lowest-numbered window" +.It Li "{end}" Ta "$" Ta "The highest-numbered window" +.It Li "{last}" Ta "!" Ta "The last (previously current) window" +.It Li "{next}" Ta "+" Ta "The next window by number" +.It Li "{previous}" Ta "-" Ta "The previous window by number" +.El .Pp .Ar target-pane -takes a similar form to +(or +.Ar src-pane +or +.Ar dst-pane ) +may be a pane ID or takes a similar form to .Ar target-window -but with the optional addition of a period followed by a pane index, for -example: mysession:mywindow.1. +but with the optional addition of a period followed by a pane index or pane ID, +for example: +.Ql mysession:mywindow.1 . If the pane index is omitted, the currently active pane in the specified window is used. -If neither a colon nor period appears, -.Nm -first attempts to use the argument as a pane index; if that fails, it is looked -up as for -.Ar target-window . -A -.Ql + , -.Ql - -or -.Ql \&! -indicate the next, previous or last pane. -One of the strings -.Em top , -.Em bottom , -.Em left , -.Em right , -.Em top-left , -.Em top-right , -.Em bottom-left -or -.Em bottom-right -may be used instead of a pane index. +The following special tokens are available for the pane index: +.Bl -column "XXXXXXXXXXXXXX" "X" +.It Sy "Token" Ta Sy "" Ta Sy "Meaning" +.It Li "{last}" Ta "!" Ta "The last (previously active) pane" +.It Li "{next}" Ta "+" Ta "The next pane by number" +.It Li "{previous}" Ta "-" Ta "The previous pane by number" +.It Li "{top}" Ta "" Ta "The top pane" +.It Li "{bottom}" Ta "" Ta "The bottom pane" +.It Li "{left}" Ta "" Ta "The leftmost pane" +.It Li "{right}" Ta "" Ta "The rightmost pane" +.It Li "{top-left}" Ta "" Ta "The top-left pane" +.It Li "{top-right}" Ta "" Ta "The top-right pane" +.It Li "{bottom-left}" Ta "" Ta "The bottom-left pane" +.It Li "{bottom-right}" Ta "" Ta "The bottom-right pane" +.It Li "{up-of}" Ta "" Ta "The pane above the active pane" +.It Li "{down-of}" Ta "" Ta "The pane below the active pane" +.It Li "{left-of}" Ta "" Ta "The pane to the left of the active pane" +.It Li "{right-of}" Ta "" Ta "The pane to the right of the active pane" +.El .Pp -The special characters +The tokens .Ql + and .Ql - @@ -473,19 +539,55 @@ may be followed by an offset, for example: select-window -t:+2 .Ed .Pp -When dealing with a session that doesn't contain sequential window indexes, -they will be correctly skipped. +In addition, +.Em target-session , +.Em target-window +or +.Em target-pane +may consist entirely of the token +.Ql {mouse} +(alternative form +.Ql = ) +to specify the most recent mouse event +(see the +.Sx MOUSE SUPPORT +section) +or +.Ql {marked} +(alternative form +.Ql ~ ) +to specify the marked pane (see +.Ic select-pane +.Fl m ) . .Pp +Sessions, window and panes are each numbered with a unique ID; session IDs are +prefixed with a +.Ql $ , +windows with a +.Ql @ , +and panes with a +.Ql % . +These are unique and are unchanged for the life of the session, window or pane +in the .Nm -also gives each pane created in a server an identifier consisting of a -.Ql % -and a number, starting from zero. -A pane's identifier is unique for the life of the -.Nm -server and is passed to the child process of the pane in the +server. +The pane ID is passed to the child process of the pane in the .Ev TMUX_PANE environment variable. -It may be used alone to target a pane or the window containing it. +IDs may be displayed using the +.Ql session_id , +.Ql window_id , +or +.Ql pane_id +formats (see the +.Sx FORMATS +section) and the +.Ic display-message , +.Ic list-sessions , +.Ic list-windows +or +.Ic list-panes +commands. .Pp .Ar shell-command arguments are @@ -515,7 +617,7 @@ to be given as multiple arguments and executed directly (without This can avoid issues with shell quoting. For example: .Bd -literal -offset indent -$ tmux new-window vi /etc/passwd +$ tmate new-window vi /etc/passwd .Ed .Pp Will run @@ -534,7 +636,7 @@ bind-key F1 set-window-option force-width 81 Or if using .Xr sh 1 : .Bd -literal -offset indent -$ tmux bind-key F1 set-window-option force-width 81 +$ tmate bind-key F1 set-window-option force-width 81 .Ed .Pp Multiple commands may be specified together as part of a @@ -566,11 +668,11 @@ bind-key R source-file ~/.tmux.conf \e; \e Or from .Xr sh 1 : .Bd -literal -offset indent -$ tmux kill-window -t :1 +$ tmate kill-window -t :1 -$ tmux new-window \e; split-window -d +$ tmate new-window \e; split-window -d -$ tmux new-session -d 'vi /etc/passwd' \e; split-window -d \e; attach +$ tmate new-session -d 'vi /etc/passwd' \e; split-window -d \e; attach .Ed .Sh CLIENTS AND SESSIONS The @@ -597,7 +699,7 @@ section. The following commands are available to manage clients and sessions: .Bl -tag -width Ds .It Xo Ic attach-session -.Op Fl dr +.Op Fl dEr .Op Fl c Ar working-directory .Op Fl t Ar target-session .Xc @@ -636,9 +738,14 @@ session. .Fl c will set the session working directory (used for new windows) to .Ar working-directory . +.Pp +If +.Fl E +is used, the +.Ic update-environment +option will not be applied. .It Xo Ic detach-client -.Op Fl P -.Op Fl a +.Op Fl aP .Op Fl s Ar target-session .Op Fl t Ar target-client .Xc @@ -664,7 +771,7 @@ Kill the .Nm server and clients and destroy all sessions. .It Xo Ic kill-session -.Op Fl a +.Op Fl aC .Op Fl t Ar target-session .Xc Destroy the given session, closing any windows linked to it and no other @@ -672,6 +779,10 @@ sessions, and detaching all clients attached to it. If .Fl a is given, all sessions but the specified one is killed. +The +.Fl C +flag clears alerts (bell, activity, or silence) in all windows linked to the +session. .It Xo Ic list-clients .Op Fl F Ar format .Op Fl t Ar target-session @@ -710,7 +821,7 @@ command. Lock all clients attached to .Ar target-session . .It Xo Ic new-session -.Op Fl AdDP +.Op Fl AdDEP .Op Fl c Ar start-directory .Op Fl F Ar format .Op Fl n Ar window-name @@ -740,7 +851,7 @@ and specify the size of the initial window (80 by 24 if not given). .Pp If run from a terminal, any -.Xr termios 3 +.Xr termios 4 special characters are saved and used for new windows in the new session. .Pp The @@ -751,7 +862,7 @@ behave like .Ic attach-session if .Ar session-name -already exists; in the case, +already exists; in this case, .Fl D behaves like .Fl d @@ -766,13 +877,12 @@ with .Ar target-session . This means they share the same set of windows - all windows from .Ar target-session -are linked to the new session and any subsequent new windows or windows being -closed are applied to both sessions. +are linked to the new session, any new windows are linked to both sessions and +any windows closed removed from both sessions. The current and previous window and any session options remain independent and either session may be killed without affecting the other. -Giving .Fl n -or +and .Ar shell-command are invalid if .Fl t @@ -785,6 +895,12 @@ By default, it uses the format .Ql #{session_name}: but a different format may be specified with .Fl F . +.Pp +If +.Fl E +is used, the +.Ic update-environment +option will not be applied. .It Xo Ic refresh-client .Op Fl S .Op Fl t Ar target-client @@ -804,7 +920,7 @@ is specified, only update the client's status bar. Rename the session to .Ar new-name . .It Xo Ic show-messages -.Op Fl IJT +.Op Fl JT .Op Fl t Ar target-client .Xc .D1 (alias: Ic showmsgs ) @@ -817,11 +933,10 @@ With .Fl t , display the log for .Ar target-client . -.Fl I , .Fl J and .Fl T -show debugging information about the running server, jobs and terminals. +show debugging information about jobs and terminals. .It Ic source-file Ar path .D1 (alias: Ic source ) Execute commands from @@ -839,9 +954,10 @@ Suspend a client by sending .Dv SIGTSTP (tty stop). .It Xo Ic switch-client -.Op Fl lnpr +.Op Fl Elnpr .Op Fl c Ar target-client .Op Fl t Ar target-session +.Op Fl T Ar key-table .Xc .D1 (alias: Ic switchc ) Switch the current session for client @@ -859,6 +975,28 @@ respectively. toggles whether a client is read-only (see the .Ic attach-session command). +.Pp +If +.Fl E +is used, +.Ic update-environment +option will not be applied. +.Pp +.Fl T +sets the client's key table; the next key from the client will be interpreted from +.Ar key-table . +This may be used to configure multiple prefix keys, or to bind commands to +sequences of keys. +For example, to make typing +.Ql abc +run the +.Ic list-keys +command: +.Bd -literal -offset indent +bind-key -Ttable2 c list-keys +bind-key -Ttable1 b switch-client -Ttable2 +bind-key -Troot a switch-client -Ttable1 +.Ed .El .Sh WINDOWS AND PANES A @@ -991,15 +1129,27 @@ command and keys modified or removed with .Ic bind-key and .Ic unbind-key . -One command accepts an argument, -.Ic copy-pipe , -which copies the selection and pipes it to a command. +If +.Ic append-selection , +.Ic copy-selection , +or +.Ic start-named-buffer +are given the +.Fl x +flag, +.Nm +will not exit copy mode after copying. +.Ic copy-pipe +copies the selection and pipes it to a command. For example the following will bind +.Ql C-w +not to exit after copying and .Ql C-q to copy the selection into .Pa /tmp as well as the paste buffer: .Bd -literal -offset indent +bind-key -temacs-copy C-w copy-selection -x bind-key -temacs-copy C-q copy-pipe "cat >/tmp/out" .Ed .Pp @@ -1011,13 +1161,26 @@ The synopsis for the command is: .Bl -tag -width Ds .It Xo Ic copy-mode -.Op Fl u +.Op Fl Meu .Op Fl t Ar target-pane .Xc Enter copy mode. The .Fl u option scrolls one page up. +.Fl M +begins a mouse drag (only valid if bound to a mouse key binding, see +.Sx MOUSE SUPPORT ) . +.Fl e +specifies that scrolling to the bottom of the history (to the visible screen) +should exit copy mode. +While in copy mode, pressing a key other than those used for scrolling will +disable this behaviour. +This is intended to allow fast scrolling through a pane's history, for +example with: +.Bd -literal -offset indent +bind PageUp copy-mode -eu +.Ed .El .Pp Each window displayed by @@ -1093,10 +1256,10 @@ command displays the layout of each window in a form suitable for use with .Ic select-layout . For example: .Bd -literal -offset indent -$ tmux list-windows +$ tmate list-windows 0: ksh [159x48] layout: bb62,159x48,0,0{79x48,0,0,79x48,80,0} -$ tmux select-layout bb62,159x48,0,0{79x48,0,0,79x48,80,0} +$ tmate select-layout bb62,159x48,0,0{79x48,0,0,79x48,80,0} .Ed .Pp .Nm @@ -1109,12 +1272,14 @@ Commands related to windows and panes are as follows: .It Xo Ic break-pane .Op Fl dP .Op Fl F Ar format -.Op Fl t Ar target-pane +.Op Fl s Ar src-pane +.Op Fl t Ar dst-window .Xc .D1 (alias: Ic breakp ) Break -.Ar target-pane -off from its containing window to make it the only pane in a new window. +.Ar src-pane +off from its containing window to make it the only pane in +.Ar dst-window . If .Fl d is given, the new window does not become the current window. @@ -1180,7 +1345,7 @@ interactively from a list. After a client is chosen, .Ql %% is replaced by the client -.Xr pty 7 +.Xr pty 4 path in .Ar template and the result executed as a command. @@ -1373,6 +1538,13 @@ option causes .Ar src-pane to be joined to left of or above .Ar dst-pane . +.Pp +If +.Fl s +is omitted and a marked pane is present (see +.Ic select-pane +.Fl m ) , +the marked pane is used rather than the current pane. .It Xo Ic kill-pane .Op Fl a .Op Fl t Ar target-pane @@ -1413,7 +1585,7 @@ If no .Ar target-session is specified, select the last window of the current session. .It Xo Ic link-window -.Op Fl dk +.Op Fl adk .Op Fl s Ar src-window .Op Fl t Ar dst-window .Xc @@ -1427,6 +1599,10 @@ If is specified and no such window exists, the .Ar src-window is linked there. +With +.Fl a , +the window is moved to the next index up (following windows +are moved if necessary). If .Fl k is given and @@ -1492,7 +1668,7 @@ and .Ar dst-pane may belong to the same window. .It Xo Ic move-window -.Op Fl rdk +.Op Fl ardk .Op Fl s Ar src-window .Op Fl t Ar dst-window .Xc @@ -1635,7 +1811,7 @@ Rename the current window, or the window at if specified, to .Ar new-name . .It Xo Ic resize-pane -.Op Fl DLRUZ +.Op Fl DLMRUZ .Op Fl t Ar target-pane .Op Fl x Ar width .Op Fl y Ar height @@ -1664,6 +1840,10 @@ With .Fl Z , the active pane is toggled between zoomed (occupying the whole of the window) and unzoomed (its normal position in the layout). +.Pp +.Fl M +begins mouse resizing (only valid if bound to a mouse key binding, see +.Sx MOUSE SUPPORT ) . .It Xo Ic respawn-pane .Op Fl k .Op Fl t Ar target-pane @@ -1704,7 +1884,7 @@ lower) with .Fl U or downward (numerically higher). .It Xo Ic select-layout -.Op Fl np +.Op Fl nop .Op Fl t Ar target-window .Op Ar layout-name .Xc @@ -1721,15 +1901,20 @@ are equivalent to the and .Ic previous-layout commands. +.Fl o +applies the last set layout if possible (undoes the most recent layout change). .It Xo Ic select-pane -.Op Fl DdeLlRU +.Op Fl DdegLlMmRU +.Op Fl P Ar style .Op Fl t Ar target-pane .Xc .D1 (alias: Ic selectp ) Make pane .Ar target-pane the active pane in window -.Ar target-window . +.Ar target-window , +or set its style (with +.Fl P ) . If one of .Fl D , .Fl L , @@ -1746,6 +1931,36 @@ command. enables or .Fl d disables input to the pane. +.Pp +.Fl m +and +.Fl M +are used to set and clear the +.Em marked pane . +There is one marked pane at a time, setting a new marked pane clears the last. +The marked pane is the default target for +.Fl s +to +.Ic join-pane , +.Ic swap-pane +and +.Ic swap-window . +.Pp +Each pane has a style: by default the +.Ic window-style +and +.Ic window-active-style +options are used, +.Ic select-pane +.Fl P +sets the style for a single pane. +For example, to set the pane 1 background to red: +.Bd -literal -offset indent +select-pane -t:.1 -P 'bg=red' +.Ed +.Pp +.Fl g +shows the current pane style. .It Xo Ic select-window .Op Fl lnpT .Op Fl t Ar target-window @@ -1819,6 +2034,13 @@ swaps with the next pane (after it numerically). instructs .Nm not to change the active pane. +.Pp +If +.Fl s +is omitted and a marked pane is present (see +.Ic select-pane +.Fl m ) , +the marked pane is used rather than the current pane. .It Xo Ic swap-window .Op Fl d .Op Fl s Ar src-window @@ -1830,6 +2052,15 @@ This is similar to except the source and destination windows are swapped. It is an error if no window exists at .Ar src-window . +.Pp +Like +.Ic swap-pane , +if +.Fl s +is omitted and a marked pane is present (see +.Ic select-pane +.Fl m ) , +the window containing the marked pane is used rather than the current window. .It Xo Ic unlink-window .Op Fl k .Op Fl t Ar target-window @@ -1897,6 +2128,7 @@ Commands related to key bindings are as follows: .It Xo Ic bind-key .Op Fl cnr .Op Fl t Ar mode-table +.Op Fl T Ar key-table .Ar key Ar command Op Ar arguments .Xc .D1 (alias: Ic bind ) @@ -1904,16 +2136,40 @@ Bind key .Ar key to .Ar command . -By default (without -.Fl t ) -the primary key bindings are modified (those normally activated with the prefix -key); in this case, if -.Fl n -is specified, it is not necessary to use the prefix key, -.Ar command +Keys are bound in a key table. +By default (without -T), the key is bound in +the +.Em prefix +key table. +This table is used for keys pressed after the prefix key (for example, +by default +.Ql c is bound to -.Ar key -alone. +.Ic new-window +in the +.Em prefix +table, so +.Ql C-b c +creates a new window). +The +.Em root +table is used for keys pressed without the prefix key: binding +.Ql c +to +.Ic new-window +in the +.Em root +table (not recommended) means a plain +.Ql c +will create a new window. +.Fl n +is an alias +for +.Fl T Ar root . +Keys may also be bound in custom key tables and the +.Ic switch-client +.Fl T +command used to switch to them from a key binding. The .Fl r flag indicates this key may repeat, see the @@ -1929,21 +2185,33 @@ is bound in the binding for command mode with .Fl c or for normal mode without. +See the +.Sx WINDOWS AND PANES +section and the +.Ic list-keys +command for information on mode key bindings. +.Pp To view the default bindings and possible commands, see the .Ic list-keys command. -.It Ic list-keys Op Fl t Ar key-table +.It Xo Ic list-keys +.Op Fl t Ar mode-table +.Op Fl T Ar key-table +.Xc .D1 (alias: Ic lsk ) List all key bindings. Without -.Fl t -the primary key bindings - those executed when preceded by the prefix key - -are printed. +.Fl T +all key tables are printed. +With +.Fl T +only +.Ar key-table . .Pp With .Fl t , the key bindings in -.Ar key-table +.Ar mode-table are listed; this may be one of: .Em vi-edit , .Em emacs-edit , @@ -1953,7 +2221,7 @@ are listed; this may be one of: or .Em emacs-copy . .It Xo Ic send-keys -.Op Fl lR +.Op Fl lMR .Op Fl t Ar target-pane .Ar key Ar ... .Xc @@ -1974,6 +2242,10 @@ All arguments are sent sequentially from first to last. The .Fl R flag causes the terminal state to be reset. +.Pp +.Fl M +passes through a mouse event (only valid if bound to a mouse key binding, see +.Sx MOUSE SUPPORT ) . .It Xo Ic send-prefix .Op Fl 2 .Op Fl t Ar target-pane @@ -1984,31 +2256,22 @@ the secondary prefix key, to a window as if it was pressed. .It Xo Ic unbind-key .Op Fl acn .Op Fl t Ar mode-table +.Op Fl T Ar key-table .Ar key .Xc .D1 (alias: Ic unbind ) Unbind the command bound to .Ar key . -Without +.Fl c , +.Fl n , +.Fl T +and .Fl t -the primary key bindings are modified; in this case, if -.Fl n -is specified, the command bound to -.Ar key -without a prefix (if any) is removed. +are the same as for +.Ic bind-key . If .Fl a is present, all key bindings are removed. -.Pp -If -.Fl t -is present, -.Ar key -in -.Ar mode-table -is unbound: the binding for command mode with -.Fl c -or for normal mode without. .El .Sh OPTIONS The appearance and behaviour of @@ -2062,10 +2325,10 @@ also supports user options which are prefixed with a User options may have any name, so long as they are prefixed with .Ql \&@ , and be set to any string. -For example +For example: .Bd -literal -offset indent -$ tmux setw -q @foo "abc123" -$ tmux showw -v @foo +$ tmate setw -q @foo "abc123" +$ tmate showw -v @foo abc123 .Ed .Pp @@ -2085,23 +2348,21 @@ command), a server option with .Fl s , otherwise a session option. -.Pp If .Fl g -is specified, the global session or window option is set. +is given, the global session or window option is set. The .Fl u flag unsets an option, so a session inherits the option from the global -options. -It is not possible to unset a global option. +options (or with +.Fl g , +restores a global option to the default). .Pp The .Fl o -flag prevents setting an option that is already set. -.Pp -The +flag prevents setting an option that is already set and the .Fl q -flag suppresses errors about unknown options. +flag suppresses errors about unknown or ambiguous options. .Pp With .Fl a , @@ -2142,6 +2403,20 @@ Available server options are: Set the number of buffers; as new buffers are added to the top of the stack, old ones are removed from the bottom if necessary to maintain this maximum length. +.It Ic default-terminal Ar terminal +Set the default terminal for new windows created in this session - the +default value of the +.Ev TERM +environment variable. +For +.Nm +to work correctly, this +.Em must +be set to +.Ql screen , +.Ql tmux , +.Ql tmate +or a derivative of them. .It Ic escape-time Ar time Set the time in milliseconds for which .Nm @@ -2160,6 +2435,10 @@ passed through to applications running in .Nm . Attached clients should be detached and attached again after changing this option. +.It Ic history-file Ar path +If not empty, a file to which +.Nm +will write command prompt history on exit and load it from on start. .It Ic message-limit Ar number Set the number of error or information messages to save in the message log for each client. @@ -2238,16 +2517,18 @@ Set the base index from which an unused index should be searched when a new window is created. The default is zero. .It Xo Ic bell-action -.Op Ic any | none | current +.Op Ic any | none | current | other .Xc Set action on window bell. .Ic any means a bell in any window linked to a session causes a bell in the current window of that session, .Ic none -means all bells are ignored and +means all bells are ignored, .Ic current -means only bells in windows other than the current window are ignored. +means only bells in windows other than the current window are ignored and +.Ic other +means bells in the current window are ignored but not those in other windows. .It Xo Ic bell-on-alert .Op Ic on | off .Xc @@ -2281,18 +2562,6 @@ or This option should be configured when .Nm is used as a login shell. -.It Ic default-terminal Ar terminal -Set the default terminal for new windows created in this session - the -default value of the -.Ev TERM -environment variable. -For -.Nm -to work correctly, this -.Em must -be set to -.Ql screen -or a derivative of it. .It Xo Ic destroy-unattached .Op Ic on | off .Xc @@ -2320,20 +2589,24 @@ command appear. .It Ic display-time Ar time Set the amount of time for which status line messages and other on-screen indicators are displayed. +If set to 0, messages and indicators are displayed until a key is pressed. .Ar time is in milliseconds. .It Ic history-limit Ar lines Set the maximum number of lines held in window history. This setting applies only to new windows - existing window histories are not resized and retain the limit at the point they were created. +.It Ic key-table Ar key-table +Set the default key table to +.Ar key-table +instead of +.Em root . .It Ic lock-after-time Ar number Lock the session (like the .Ic lock-session command) after .Ar number -seconds of inactivity, or the entire server (all sessions) if the -.Ic lock-server -option is set. +seconds of inactivity. The default is not to lock (set to 0). .It Ic lock-command Ar shell-command Command to run when locking each client. @@ -2341,19 +2614,6 @@ The default is to run .Xr lock 1 with .Fl np . -.It Xo Ic lock-server -.Op Ic on | off -.Xc -If this option is -.Ic on -(the default), -instead of each session locking individually as each has been -idle for -.Ic lock-after-time , -the entire server will lock after -.Em all -sessions would have locked. -This has no effect as a session option; it must be set as a global option. .It Ic message-command-style Ar style Set status line message command style, where .Ar style @@ -2422,33 +2682,30 @@ For how to specify see the .Ic message-command-style option. -.It Xo Ic mouse-resize-pane +.It Xo Ic mouse .Op Ic on | off .Xc If on, .Nm -captures the mouse and allows panes to be resized by dragging on their borders. -.It Xo Ic mouse-select-pane -.Op Ic on | off -.Xc -If on, -.Nm -captures the mouse and when a window is split into multiple panes the mouse may -be used to select the current pane. -The mouse click is also passed through to the application as normal. -.It Xo Ic mouse-select-window -.Op Ic on | off -.Xc -If on, clicking the mouse on a window name in the status line will select that -window. -.It Xo Ic mouse-utf8 -.Op Ic on | off -.Xc -If enabled, request mouse input as UTF-8 on UTF-8 terminals. +captures the mouse and allows mouse events to be bound as key bindings. +See the +.Sx MOUSE SUPPORT +section for details. .It Ic prefix Ar key Set the key accepted as a prefix key. +In addition to the standard keys described under +.Sx KEY BINDINGS , +.Ic prefix +can be set to the special key +.Ql None +to set no prefix. .It Ic prefix2 Ar key Set a secondary key accepted as a prefix key. +Like +.Ic prefix , +.Ic prefix2 +can be set to +.Ql None . .It Xo Ic renumber-windows .Op Ic on | off .Xc @@ -2493,19 +2750,17 @@ and .Xr terminfo 5 entries if they exist. .Nm -automatically sets these to the \ee]2;...\e007 sequence if -the terminal appears to be an xterm. +automatically sets these to the \ee]0;...\e007 sequence if +the terminal appears to be +.Xr xterm 1 . This option is off by default. -Note that elinks -will only attempt to set the window title if the STY environment -variable is set. .It Ic set-titles-string Ar string String used to set the window title if .Ic set-titles is on. -Character sequences are replaced as for the -.Ic status-left -option. +Formats are expanded, see the +.Sx FORMATS +section. .It Xo Ic status .Op Ic on | off .Xc @@ -2545,25 +2800,10 @@ will be expanded. It may also contain any of the following special character sequences: .Bl -column "Character pair" "Replaced with" -offset indent .It Sy "Character pair" Ta Sy "Replaced with" -.It Li "#(shell-command)" Ta "First line of the command's output" .It Li "#[attributes]" Ta "Colour or attribute change" .It Li "##" Ta "A literal" Ql # .El .Pp -The #(shell-command) form executes -.Ql shell-command -and inserts the first line of its output. -Note that shell commands are only executed once at the interval specified by -the -.Ic status-interval -option: if the status line is redrawn in the meantime, the previous result is -used. -Shell commands are executed with the -.Nm -global environment set (see the -.Sx ENVIRONMENT -section). -.Pp For details on how the names and titles can be set see the .Sx "NAMES AND TITLES" section. @@ -2577,12 +2817,6 @@ Examples are: #[fg=yellow,bold]#(apm -l)%%#[default] [#S] .Ed .Pp -By default, UTF-8 in -.Ar string -is not interpreted, to enable UTF-8, use the -.Ic status-utf8 -option. -.Pp The default is .Ql "[#S] " . .It Ic status-left-length Ar length @@ -2611,10 +2845,8 @@ As with .Ic status-left , .Ar string will be passed to -.Xr strftime 3 , -character pairs are replaced, and UTF-8 is dependent on the -.Ic status-utf8 -option. +.Xr strftime 3 +and character pairs are replaced. .It Ic status-right-length Ar length Set the maximum .Ar length @@ -2634,17 +2866,6 @@ For how to specify see the .Ic message-command-style option. -.It Xo Ic status-utf8 -.Op Ic on | off -.Xc -Instruct -.Nm -to treat top-bit-set characters in the -.Ic status-left -and -.Ic status-right -strings as UTF-8; notably, this is important for wide characters. -This option defaults to off. .It Ic update-environment Ar variables Set a space-separated string containing a list of environment variables to be copied into the session environment when a new session is created or an @@ -2724,7 +2945,7 @@ and poor for interactive programs such as shells. .Op Ic on | off .Xc Allow programs to change the window name using a terminal escape -sequence (\\033k...\\033\\\\). +sequence (\eek...\ee\e\e). The default is on. .Pp .It Xo Ic alternate-screen @@ -2771,24 +2992,6 @@ used when the .Ic automatic-rename option is enabled. .Pp -.It Ic c0-change-interval Ar interval -.It Ic c0-change-trigger Ar trigger -These two options configure a simple form of rate limiting for a pane. -If -.Nm -sees more than -.Ar trigger -C0 sequences that modify the screen (for example, carriage returns, linefeeds -or backspaces) in one millisecond, it will stop updating the pane immediately and -instead redraw it entirely every -.Ar interval -milliseconds. -This helps to prevent fast output (such as -.Xr yes 1 ) -overwhelming the terminal. -The default is a trigger of 250 and an interval of 100. -A trigger of zero disables the rate limiting. -.Pp .It Ic clock-mode-colour Ar colour Set clock colour. .Pp @@ -2828,18 +3031,6 @@ or contains .Ql vi . .Pp -.It Xo Ic mode-mouse -.Op Ic on | off | copy-mode -.Xc -Mouse state in modes. -If on, the mouse may be used to enter copy mode and copy a selection by -dragging, to enter copy mode and scroll with the mouse wheel, or to select an -option in choice mode. -If set to -.Em copy-mode , -the mouse behaves as set to on, but cannot be used to enter copy -mode. -.Pp .It Ic mode-style Ar style Set window modes style. For how to specify @@ -2921,12 +3112,13 @@ command. Duplicate input to any pane to all other panes in the same window (only for panes that are not in any special mode). .Pp -.It Xo Ic utf8 -.Op Ic on | off -.Xc -Instructs -.Nm -to expect UTF-8 sequences to appear in this window. +.It Ic window-active-style Ar style +Set the style for the window's active pane. +For how to specify +.Ar style , +see the +.Ic message-command-style +option. .Pp .It Ic window-status-activity-style Ar style Set status line style for windows with an activity alert. @@ -2985,6 +3177,14 @@ see the .Ic message-command-style option. .Pp +.It Ic window-style Ar style +Set the default window style. +For how to specify +.Ar style , +see the +.Ic message-command-style +option. +.Pp .It Xo Ic xterm-keys .Op Ic on | off .Xc @@ -3040,6 +3240,120 @@ is used. .Fl v shows only the option value, not the name. .El +.Sh HOOKS +.Nm +allows commands to run on various triggers, called +.Em hooks . +Each hook has a +.Em name . +The following hooks are available: +.Bl -tag -width "XXXXXXXXXXXXXXXX" +.It alert-activity +Run when a window has activity. +See +.Ic monitor-activity . +.It alert-bell +Run when a window has received a bell. +.It alert-silence +Run when a window has been silent. +See +.Ic monitor-silence . +.It client-attached +Run when a client is attached. +.It client-detached +Run when a client is detached +.It client-resized +Run when a client is resized. +.It pane-died +Run when the program running in a pane exits, but +.Ic remain-on-exit +is on so the pane has not closed. +.It pane-exited +Run when the program running in a pane exits. +.El +.Pp +Hooks are managed with these commands: +.Bl -tag -width Ds +.It Xo Ic set-hook +.Op Fl g +.Op Fl t Ar target-session +.Ar hook-name +.Ar command +.Xc +Sets hook +.Ar hook-name +to +.Ar command . +If +.Fl g +is given, +.Em hook-name +is added to the global list of hooks, otherwise it is added to the session +hooks (for +.Ar target-session +with +.Fl t ) . +Like options, session hooks inherit from the global ones. +.It Xo Ic show-hooks +.Op Fl g +.Op Fl t Ar target-session +.Xc +Shows the global list of hooks with +.Fl g , +otherwise the session hooks. +.El +.Sh MOUSE SUPPORT +If the +.Ic mouse +option is on (the default is off), +.Nm +allows mouse events to be bound as keys. +The name of each key is made up of a mouse event (such as +.Ql MouseUp1 ) +and a location suffix (one of +.Ql Pane +for the contents of a pane, +.Ql Border +for a pane border or +.Ql Status +for the status line). +The following mouse events are available: +.Bl -column "MouseDown1" "MouseDrag1" "WheelDown" -offset indent +.It Li "MouseDown1" Ta "MouseUp1" Ta "MouseDrag1" Ta "MouseDragEnd1" +.It Li "MouseDown2" Ta "MouseUp2" Ta "MouseDrag2" Ta "MouseDragEnd2" +.It Li "MouseDown3" Ta "MouseUp3" Ta "MouseDrag3" Ta "MouseDragEnd3" +.It Li "WheelUp" Ta "WheelDown" Ta "" Ta "" +.El +.Pp +Each should be suffixed with a location, for example +.Ql MouseDown1Status . +.Pp +The special token +.Ql {mouse} +or +.Ql = +may be used as +.Ar target-window +or +.Ar target-pane +in commands bound to mouse key bindings. +It resolves to the window or pane over which the mouse event took place +(for example, the window in the status line over which button 1 was released for a +.Ql MouseUp1Status +binding, or the pane over which the wheel was scrolled for a +.Ql WheelDownPane +binding). +.Pp +The +.Ic send-keys +.Fl M +flag may be used to forward a mouse event to a pane. +.Pp +The default key bindings allow the mouse to be used to select and resize panes, +to copy text and to change window using the status line. +These take effect if the +.Ic mouse +option is turned on. .Sh FORMATS Certain commands accept the .Fl F @@ -3083,12 +3397,59 @@ if is enabled, or .Ql no if not. +.Pp A limit may be placed on the length of the resultant string by prefixing it by an .Ql = , -a number and a colon, so -.Ql #{=10:pane_title} -will include at most the first 10 characters of the pane title. +a number and a colon. +Positive numbers count from the start of the string and negative from the end, +so +.Ql #{=5:pane_title} +will include at most the first 5 characters of the pane title, or +.Ql #{=-5:pane_title} +the last 5 characters. +Prefixing a time variable with +.Ql t: +will convert it to a string, so if +.Ql #{window_activity} +gives +.Ql 1445765102 , +.Ql #{t:window_activity} +gives +.Ql Sun Oct 25 09:25:02 2015 . +The +.Ql b: +and +.Ql d: +prefixes are +.Xr basename 3 +and +.Xr dirname 3 +of the variable respectively. +A prefix of the form +.Ql s/foo/bar/: +will substitute +.Ql foo +with +.Ql bar +throughout. +.Pp +In addition, the first line of a shell command's output may be inserted using +.Ql #() . +For example, +.Ql #(uptime) +will insert the system's uptime. +When constructing formats, +.Nm +does not wait for +.Ql #() +commands to finish; instead, the previous result from running the same command is used, +or a placeholder if the command has not been run before. +Commands are executed with the +.Nm +global environment set (see the +.Sx ENVIRONMENT +section). .Pp The following variables are available, where appropriate: .Bl -column "XXXXXXXXXXXXXXXXXXX" "XXXXX" @@ -3099,11 +3460,12 @@ The following variables are available, where appropriate: .It Li "buffer_sample" Ta "" Ta "Sample of start of buffer" .It Li "buffer_size" Ta "" Ta "Size of the specified buffer in bytes" .It Li "client_activity" Ta "" Ta "Integer time client last had activity" -.It Li "client_activity_string" Ta "" Ta "String time client last had activity" .It Li "client_created" Ta "" Ta "Integer time client created" -.It Li "client_created_string" Ta "" Ta "String time client created" +.It Li "client_control_mode" Ta "" Ta "1 if client is in control mode" .It Li "client_height" Ta "" Ta "Height of client" +.It Li "client_key_table" Ta "" Ta "Current key table" .It Li "client_last_session" Ta "" Ta "Name of the client's last session" +.It Li "client_pid" Ta "" Ta "PID of client process" .It Li "client_prefix" Ta "" Ta "1 if prefix key has been pressed" .It Li "client_readonly" Ta "" Ta "1 if client is readonly" .It Li "client_session" Ta "" Ta "Name of the client's session" @@ -3111,6 +3473,7 @@ The following variables are available, where appropriate: .It Li "client_tty" Ta "" Ta "Pseudo terminal of client" .It Li "client_utf8" Ta "" Ta "1 if client supports utf8" .It Li "client_width" Ta "" Ta "Width of client" +.It Li "command_name" Ta "" Ta "Name of command in use, if any" .It Li "cursor_flag" Ta "" Ta "Pane cursor flag" .It Li "cursor_x" Ta "" Ta "Cursor X position in pane" .It Li "cursor_y" Ta "" Ta "Cursor Y position in pane" @@ -3126,7 +3489,6 @@ The following variables are available, where appropriate: .It Li "mouse_any_flag" Ta "" Ta "Pane mouse any flag" .It Li "mouse_button_flag" Ta "" Ta "Pane mouse button flag" .It Li "mouse_standard_flag" Ta "" Ta "Pane mouse standard flag" -.It Li "mouse_utf8_flag" Ta "" Ta "Pane mouse UTF-8 flag" .It Li "pane_active" Ta "" Ta "1 if active pane" .It Li "pane_bottom" Ta "" Ta "Bottom of pane" .It Li "pane_current_command" Ta "" Ta "Current command if available" @@ -3148,13 +3510,15 @@ The following variables are available, where appropriate: .It Li "pane_top" Ta "" Ta "Top of pane" .It Li "pane_tty" Ta "" Ta "Pseudo terminal of pane" .It Li "pane_width" Ta "" Ta "Width of pane" -.It Li "saved_cursor_x" Ta "" Ta "Saved cursor X in pane" -.It Li "saved_cursor_y" Ta "" Ta "Saved cursor Y in pane" +.It Li "pid" Ta "" Ta "Server PID" .It Li "scroll_region_lower" Ta "" Ta "Bottom of scroll region in pane" .It Li "scroll_region_upper" Ta "" Ta "Top of scroll region in pane" +.It Li "scroll_position" Ta "" Ta "Scroll position in copy mode" +.It Li "session_alerts" Ta "" Ta "List of window indexes with alerts" .It Li "session_attached" Ta "" Ta "Number of clients session is attached to" +.It Li "session_activity" Ta "" Ta "Integer time of session last activity" .It Li "session_created" Ta "" Ta "Integer time session created" -.It Li "session_created_string" Ta "" Ta "String time session created" +.It Li "session_last_attached" Ta "" Ta "Integer time session last attached" .It Li "session_group" Ta "" Ta "Number of session group" .It Li "session_grouped" Ta "" Ta "1 if session in a group" .It Li "session_height" Ta "" Ta "Height of session" @@ -3163,8 +3527,10 @@ The following variables are available, where appropriate: .It Li "session_name" Ta "#S" Ta "Name of session" .It Li "session_width" Ta "" Ta "Width of session" .It Li "session_windows" Ta "" Ta "Number of windows in session" +.It Li "socket_path" Ta "" "Server socket path" +.It Li "start_time" Ta "" Ta "Server start time" +.It Li "window_activity" Ta "" Ta "Integer time of window last activity" .It Li "window_active" Ta "" Ta "1 if window active" -.It Li "window_activity_flag" Ta "" Ta "1 if window has activity alert" .It Li "window_bell_flag" Ta "" Ta "1 if window has bell" .It Li "window_find_matches" Ta "" Ta "Matched data from the find-window" .It Li "window_flags" Ta "#F" Ta "Window flags" @@ -3172,10 +3538,12 @@ The following variables are available, where appropriate: .It Li "window_id" Ta "" Ta "Unique window ID" .It Li "window_index" Ta "#I" Ta "Index of window" .It Li "window_last_flag" Ta "" Ta "1 if window is the last used" -.It Li "window_layout" Ta "" Ta "Window layout description" +.It Li "window_layout" Ta "" Ta "Window layout description, ignoring zoomed window panes" +.It Li "window_linked" Ta "" Ta "1 if window is linked across sessions" .It Li "window_name" Ta "#W" Ta "Name of window" .It Li "window_panes" Ta "" Ta "Number of panes in window" .It Li "window_silence_flag" Ta "" Ta "1 if window has silence alert" +.It Li "window_visible_layout" Ta "" Ta "Window layout description, respecting zoomed window panes" .It Li "window_width" Ta "" Ta "Width of window" .It Li "window_zoomed_flag" Ta "" Ta "1 if window is zoomed" .It Li "wrap_flag" Ta "" Ta "Pane wrap flag" @@ -3281,7 +3649,7 @@ flag unsets a variable. indicates the variable is to be removed from the environment before starting a new process. .It Xo Ic show-environment -.Op Fl g +.Op Fl gs .Op Fl t Ar target-session .Op Ar variable .Xc @@ -3295,6 +3663,9 @@ If is omitted, all variables are shown. Variables removed from the environment are prefixed with .Ql - . +If +.Fl s +is used, the output is formatted as a set of Bourne shell commands. .El .Sh STATUS LINE .Nm @@ -3330,6 +3701,7 @@ The flag is one of the following symbols appended to the window name: .It Li "#" Ta "Window is monitored and activity has been detected." .It Li "!" Ta "A bell has occurred in the window." .It Li "~" Ta "The window has been silent for the monitor-silence interval." +.It Li "M" Ta "The window contains the marked pane." .It Li "Z" Ta "The window's active pane is zoomed." .El .Pp @@ -3681,7 +4053,7 @@ This command only works from outside .El .Sh TERMINFO EXTENSIONS .Nm -understands some extensions to +understands some unofficial extensions to .Xr terminfo 5 : .Bl -tag -width Ds .It Em Cs , Cr @@ -3705,10 +4077,12 @@ $ printf '\e033[4 q' If .Em Se is not set, \&Ss with argument 0 will be used to reset the cursor style instead. +.It Em \&Tc +Indicate that the terminal supports the +.Ql direct colour +RGB escape sequence (for example, \ee[38;2;255;255;255m). .It Em \&Ms -This sequence can be used by -.Nm -to store the current buffer in the host terminal's selection (clipboard). +Store the current buffer in the host terminal's selection (clipboard). See the .Em set-clipboard option above and the @@ -3762,12 +4136,16 @@ or an error occurred. If present, .Ar reason describes why the client exited. -.It Ic %layout-change Ar window-id Ar window-layout +.It Ic %layout-change Ar window-id Ar window-layout Ar window-visible-layout Ar window-flags The layout of a window with ID .Ar window-id changed. The new layout is .Ar window-layout . +The window's visible layout is +.Ar window-visible-layout +and the window flags are +.Ar window-flags . .It Ic %output Ar pane-id Ar value A window pane produced output. .Ar value @@ -3815,18 +4193,18 @@ To create a new session running .Xr vi 1 : .Pp -.Dl $ tmux new-session vi +.Dl $ tmate new-session vi .Pp Most commands have a shorter form, known as an alias. For new-session, this is .Ic new : .Pp -.Dl $ tmux new vi +.Dl $ tmate new vi .Pp Alternatively, the shortest unambiguous form of a command is accepted. If there are several options, they are listed: .Bd -literal -offset indent -$ tmux n +$ tmate n ambiguous command: n, could be: new-session, new-window, next-window .Ed .Pp @@ -3856,7 +4234,7 @@ A session may be detached using .Xr ssh 1 disconnection) and reattached with: .Pp -.Dl $ tmux attach-session +.Dl $ tmate attach-session .Pp Typing .Ql C-b \&? @@ -3899,6 +4277,6 @@ bind-key / command-prompt "split-window 'exec man %%'" bind-key S command-prompt "new-window -n %1 'ssh %1'" .Ed .Sh SEE ALSO -.Xr pty 7 +.Xr pty 4 .Sh AUTHORS -.An Nicholas Marriott Aq Mt nicm@users.sourceforge.net +.An Nicholas Marriott Aq Mt nicholas.marriott@gmail.com diff --git a/tmux.c b/tmux.c index 8b675761..730c2dfc 100644 --- a/tmux.c +++ b/tmux.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -19,66 +19,62 @@ #include #include +#include #include #include #include #include +#include #include #include #include #include +#include #include #include "tmux.h" +#include "tmate.h" -#if defined(DEBUG) && defined(__OpenBSD__) -extern char *malloc_options; -#endif +struct options *global_options; /* server options */ +struct options *global_s_options; /* session options */ +struct options *global_w_options; /* window options */ +struct environ *global_environ; +struct hooks *global_hooks; -struct options global_options; /* server options */ -struct options global_s_options; /* session options */ -struct options global_w_options; /* window options */ -struct environ global_environ; - -struct event_base *ev_base; - -char *cfg_file; -char *shell_cmd; -int debug_level; -time_t start_time; -char socket_path[PATH_MAX]; -int login_shell; -char *environ_path; +struct timeval start_time; +const char *socket_path; __dead void usage(void); -char *makesocketpath(const char *); +static char *make_label(const char *); #ifndef HAVE___PROGNAME -char *__progname = (char *) "tmux"; +char *__progname = (char *) "tmate"; +#endif + +#ifdef TMATE +int tmate_foreground; #endif __dead void usage(void) { fprintf(stderr, - "usage: %s [-2lquvV] [-c shell-command] [-f file] [-L socket-name]\n" - " [-S socket-path] [command [flags]]\n", - __progname); + "Usage: %s [options] [tmux-command [flags]]\n" + "\n" + "Basic options:\n" + " -n specify the session token instead of getting a random one\n" + " -r same, but for the read-only token\n" + " -k specify an api-key, necessary for using named sessions on tmate.io\n" + " -F set the foreground mode, useful for setting remote access\n" + " -f set the config file path\n" + " -S set the socket path, useful to issue commands to a running tmate instance\n" + " -a limit access to ssh public keys listed in provided file\n" + " -v set verbosity (can be repeated)\n" + " -V print version\n" + ,__progname); exit(1); } -void -logfile(const char *name) -{ - char *path; - - if (debug_level > 0) { - xasprintf(&path, "tmux-%s-%ld.log", name, (long) getpid()); - log_open(path); - free(path); - } -} - const char * getshell(void) { @@ -125,40 +121,60 @@ areshell(const char *shell) return (0); } -char * -makesocketpath(const char *label) +static char * +make_label(const char *label) { - char base[PATH_MAX], realbase[PATH_MAX], *path, *s; - struct stat sb; - u_int uid; + char *base, resolved[PATH_MAX], *path, *s; + struct stat sb; + u_int uid; + int saved_errno; +#ifdef TMATE + int do_random_label = label == NULL; +#endif + + if (label == NULL) + label = "default"; uid = getuid(); + if ((s = getenv("TMUX_TMPDIR")) != NULL && *s != '\0') - xsnprintf(base, sizeof base, "%s/tmux-%u", s, uid); - else if ((s = getenv("TMPDIR")) != NULL && *s != '\0') - xsnprintf(base, sizeof base, "%s/tmux-%u", s, uid); + xasprintf(&base, "%s/tmate-%u", s, uid); else - xsnprintf(base, sizeof base, "%s/tmux-%u", _PATH_TMP, uid); + xasprintf(&base, "%s/tmate-%u", _PATH_TMP, uid); if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST) - return (NULL); + goto fail; if (lstat(base, &sb) != 0) - return (NULL); + goto fail; if (!S_ISDIR(sb.st_mode)) { errno = ENOTDIR; - return (NULL); + goto fail; } if (sb.st_uid != uid || (sb.st_mode & S_IRWXO) != 0) { errno = EACCES; - return (NULL); + goto fail; } - if (realpath(base, realbase) == NULL) - strlcpy(realbase, base, sizeof realbase); +#ifdef TMATE + if (do_random_label) + label = "XXXXXX"; +#endif - xasprintf(&path, "%s/%s", realbase, label); + if (realpath(base, resolved) == NULL) + strlcpy(resolved, base, sizeof resolved); + xasprintf(&path, "%s/%s", resolved, label); +#ifdef TMATE + if (do_random_label) + mktemp(path); +#endif return (path); + +fail: + saved_errno = errno; + free(base); + errno = saved_errno; + return (NULL); } void @@ -175,59 +191,88 @@ setblocking(int fd, int state) } } -__dead void -shell_exec(const char *shell, const char *shellcmd) +const char * +find_home(void) { - const char *shellname, *ptr; - char *argv0; + struct passwd *pw; + static const char *home; - ptr = strrchr(shell, '/'); - if (ptr != NULL && *(ptr + 1) != '\0') - shellname = ptr + 1; - else - shellname = shell; - if (login_shell) - xasprintf(&argv0, "-%s", shellname); - else - xasprintf(&argv0, "%s", shellname); - setenv("SHELL", shell, 1); + if (home != NULL) + return (home); - setblocking(STDIN_FILENO, 1); - setblocking(STDOUT_FILENO, 1); - setblocking(STDERR_FILENO, 1); - closefrom(STDERR_FILENO + 1); + home = getenv("HOME"); + if (home == NULL || *home == '\0') { + pw = getpwuid(getuid()); + if (pw != NULL) + home = pw->pw_dir; + else + home = NULL; + } - execl(shell, argv0, "-c", shellcmd, (char *) NULL); - fatal("execl failed"); + return (home); } +#ifdef TMATE +static char *api_key; +static char *session_name; +static char *session_name_ro; +static char *authorized_keys; + +void tmate_load_cli_options(void) +{ +#define SET_OPT(name, val) ({\ + if (val) { \ + run_headless_command(3, (const char *[]){"set-option", name, val}, DEFER_ERRORS_CFG, NULL); \ + free(val); \ + val = NULL; \ + } \ +}) + SET_OPT("tmate-api-key", api_key); + SET_OPT("tmate-session-name", session_name); + SET_OPT("tmate-session-name-ro", session_name_ro); + SET_OPT("tmate-authorized-keys", authorized_keys); +#undef SET_OPT +} +#endif + int main(int argc, char **argv) { - struct passwd *pw; - char *s, *path, *label, **var, tmp[PATH_MAX]; - char in[256]; - const char *home; - long long pid; - int opt, flags, keys, session; + char *path, *label, **var, tmp[PATH_MAX], *shellcmd = NULL; + const char *s; + int opt, flags, keys; -#if defined(DEBUG) && defined(__OpenBSD__) - malloc_options = (char *) "AFGJPX"; -#endif + if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL && + setlocale(LC_CTYPE, "C.UTF-8") == NULL) { + if (setlocale(LC_CTYPE, "") == NULL) + errx(1, "invalid LC_ALL, LC_CTYPE or LANG"); + s = nl_langinfo(CODESET); + if (strcasecmp(s, "UTF-8") != 0 && strcasecmp(s, "UTF8") != 0) + errx(1, "need UTF-8 locale (LC_CTYPE) but have %s", s); + } setlocale(LC_TIME, ""); + tzset(); + + if (**argv == '-') + flags = CLIENT_LOGIN; + else + flags = 0; + +#ifdef TMATE + tmate_catch_sigsegv(); + flags |= CLIENT_256COLOURS | CLIENT_UTF8; +#endif - flags = 0; label = path = NULL; - login_shell = (**argv == '-'); - while ((opt = getopt(argc, argv, "2c:Cdf:lL:qS:uUVv")) != -1) { + while ((opt = getopt(argc, argv, "h2c:CdFf:lL:qS:uUVvk:n:r:a:")) != -1) { switch (opt) { case '2': flags |= CLIENT_256COLOURS; break; case 'c': - free(shell_cmd); - shell_cmd = xstrdup(optarg); + free(shellcmd); + shellcmd = xstrdup(optarg); break; case 'C': if (flags & CLIENT_CONTROL) @@ -239,11 +284,10 @@ main(int argc, char **argv) printf("%s %s\n", __progname, VERSION); exit(0); case 'f': - free(cfg_file); - cfg_file = xstrdup(optarg); + set_cfg_file(optarg); break; case 'l': - login_shell = 1; + flags |= CLIENT_LOGIN; break; case 'L': free(label); @@ -259,8 +303,26 @@ main(int argc, char **argv) flags |= CLIENT_UTF8; break; case 'v': - debug_level++; + log_add_level(); break; + case 'F': + tmate_foreground = 1; + log_add_level(); + unsetenv("TMUX"); + break; + case 'k': + api_key = xstrdup(optarg); + break; + case 'n': + session_name = xstrdup(optarg); + break; + case 'r': + session_name_ro = xstrdup(optarg); + break; + case 'a': + authorized_keys = xstrdup(optarg); + break; + case 'h': default: usage(); } @@ -268,49 +330,54 @@ main(int argc, char **argv) argc -= optind; argv += optind; - if (shell_cmd != NULL && argc != 0) + if (shellcmd != NULL && argc != 0) usage(); - if (!(flags & CLIENT_UTF8)) { - /* - * If the user has set whichever of LC_ALL, LC_CTYPE or LANG - * exist (in that order) to contain UTF-8, it is a safe - * assumption that either they are using a UTF-8 terminal, or - * if not they know that output from UTF-8-capable programs may - * be wrong. - */ - if ((s = getenv("LC_ALL")) == NULL || *s == '\0') { - if ((s = getenv("LC_CTYPE")) == NULL || *s == '\0') - s = getenv("LANG"); - } - if (s != NULL && (strcasestr(s, "UTF-8") != NULL || - strcasestr(s, "UTF8") != NULL)) +#ifdef __OpenBSD__ + if (pledge("stdio rpath wpath cpath flock fattr unix getpw sendfd " + "recvfd proc exec tty ps", NULL) != 0) + err(1, "pledge"); +#endif + + /* + * tmux is a UTF-8 terminal, so if TMUX is set, assume UTF-8. + * Otherwise, if the user has set LC_ALL, LC_CTYPE or LANG to contain + * UTF-8, it is a safe assumption that either they are using a UTF-8 + * terminal, or if not they know that output from UTF-8-capable + * programs may be wrong. + */ + if (getenv("TMUX") != NULL) + flags |= CLIENT_UTF8; + else { + s = getenv("LC_ALL"); + if (s == NULL || *s == '\0') + s = getenv("LC_CTYPE"); + if (s == NULL || *s == '\0') + s = getenv("LANG"); + if (s == NULL || *s == '\0') + s = ""; + if (strcasestr(s, "UTF-8") != NULL || + strcasestr(s, "UTF8") != NULL) flags |= CLIENT_UTF8; } - environ_init(&global_environ); + global_hooks = hooks_create(NULL); + + global_environ = environ_create(); for (var = environ; *var != NULL; var++) - environ_put(&global_environ, *var); + environ_put(global_environ, *var); if (getcwd(tmp, sizeof tmp) != NULL) - environ_set(&global_environ, "PWD", tmp); + environ_set(global_environ, "PWD", "%s", tmp); - options_init(&global_options, NULL); - options_table_populate_tree(server_options_table, &global_options); + global_options = options_create(NULL); + options_table_populate_tree(OPTIONS_TABLE_SERVER, global_options); - options_init(&global_s_options, NULL); - options_table_populate_tree(session_options_table, &global_s_options); - options_set_string(&global_s_options, "default-shell", "%s", - getshell()); + global_s_options = options_create(NULL); + options_table_populate_tree(OPTIONS_TABLE_SESSION, global_s_options); + options_set_string(global_s_options, "default-shell", "%s", getshell()); - options_init(&global_w_options, NULL); - options_table_populate_tree(window_options_table, &global_w_options); - - /* Enable UTF-8 if the first client is on UTF-8 terminal. */ - if (flags & CLIENT_UTF8) { - options_set_number(&global_s_options, "status-utf8", 1); - options_set_number(&global_s_options, "mouse-utf8", 1); - options_set_number(&global_w_options, "utf8", 1); - } + global_w_options = options_create(NULL); + options_table_populate_tree(OPTIONS_TABLE_WINDOW, global_w_options); /* Override keys to vi if VISUAL or EDITOR are set. */ if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) { @@ -320,70 +387,29 @@ main(int argc, char **argv) keys = MODEKEY_VI; else keys = MODEKEY_EMACS; - options_set_number(&global_s_options, "status-keys", keys); - options_set_number(&global_w_options, "mode-keys", keys); + options_set_number(global_s_options, "status-keys", keys); + options_set_number(global_w_options, "mode-keys", keys); } - /* Locate the configuration file. */ - if (cfg_file == NULL) { - home = getenv("HOME"); - if (home == NULL || *home == '\0') { - pw = getpwuid(getuid()); - if (pw != NULL) - home = pw->pw_dir; - else - home = NULL; - } - if (home != NULL) { - xasprintf(&cfg_file, "%s/.tmux.conf", home); - if (access(cfg_file, R_OK) != 0 && errno == ENOENT) { - free(cfg_file); - cfg_file = NULL; - } - } - } - - /* Get path from environment. */ - s = getenv("TMUX"); - if (s != NULL && sscanf(s, "%255[^,],%lld,%d", in, &pid, &session) == 3) - environ_path = xstrdup(in); - /* - * Figure out the socket path. If specified on the command-line with -S - * or -L, use it, otherwise try $TMUX or assume -L default. + * If socket is specified on the command-line with -S or -L, it is + * used. Otherwise, $TMUX is checked and if that fails "default" is + * used. */ - if (path == NULL) { - /* If no -L, use the environment. */ - if (label == NULL) { - if (environ_path != NULL) - path = xstrdup(environ_path); - else - label = xstrdup("default"); - } - - /* -L or default set. */ - if (label != NULL) { - if ((path = makesocketpath(label)) == NULL) { - fprintf(stderr, "can't create socket: %s\n", - strerror(errno)); - exit(1); - } + if (path == NULL && label == NULL) { + s = getenv("TMUX"); + if (s != NULL && *s != '\0' && *s != ',') { + path = xstrdup(s); + path[strcspn (path, ",")] = '\0'; } } - free(label); - - if (strlcpy(socket_path, path, sizeof socket_path) >= sizeof socket_path) { - fprintf(stderr, "socket path too long: %s\n", path); + if (path == NULL && (path = make_label(label)) == NULL) { + fprintf(stderr, "can't create socket: %s\n", strerror(errno)); exit(1); } - free(path); - -#ifdef HAVE_SETPROCTITLE - /* Set process title. */ - setproctitle("%s (%s)", __progname, socket_path); -#endif + socket_path = path; + free(label); /* Pass control to the client. */ - ev_base = osdep_event_init(); - exit(client_main(argc, argv, flags)); + exit(client_main(event_init(), argc, argv, flags, shellcmd)); } diff --git a/tmux.h b/tmux.h index e296ac7c..b38615b6 100644 --- a/tmux.h +++ b/tmux.h @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -19,6 +19,8 @@ #ifndef TMUX_H #define TMUX_H +#define TMATE + #define PROTOCOL_VERSION 8 #include @@ -29,20 +31,31 @@ #include #include #include +#include #ifdef HAVE_UTEMPTER #include #endif -#include "array.h" - #include "compat.h" +#include "xmalloc.h" extern char *__progname; extern char **environ; -/* Default prompt history length. */ -#define PROMPT_HISTORY 100 +struct client; +struct environ; +struct input_ctx; +struct mouse_event; +struct options; +struct session; +struct tmuxpeer; +struct tmuxproc; + +/* Default global configuration file. */ +#ifndef TMUX_CONF +#define TMUX_CONF "/etc/tmux.conf" +#endif /* * Minimum layout cell size, NOT including separator line. The scroll region @@ -50,21 +63,18 @@ extern char **environ; */ #define PANE_MINIMUM 2 -/* Automatic name refresh interval, in milliseconds. */ -#define NAME_INTERVAL 500 +/* Automatic name refresh interval, in microseconds. Must be < 1 second. */ +#define NAME_INTERVAL 500000 /* - * UTF-8 data size. This must be big enough to hold combined characters as well - * as single. + * READ_SIZE is the maximum size of data to hold from a pty (the event high + * watermark). READ_BACKOFF is the amount of data waiting to be output to a tty + * before pty reads will be backed off. READ_TIME is how long to back off + * before the next read (in microseconds) if a tty is above READ_BACKOFF. */ -#define UTF8_SIZE 9 - -/* Fatal errors. */ -#define fatal(msg) log_fatal("%s: %s", __func__, msg); -#define fatalx(msg) log_fatalx("%s: %s", __func__, msg); - -/* Definition to shut gcc up about unused arguments. */ -#define unused __attribute__ ((unused)) +#define READ_SIZE 1024 +#define READ_BACKOFF 512 +#define READ_TIME 100 /* Attribute to make gcc check printf-like arguments. */ #define printflike(a, b) __attribute__ ((format (printf, a, b))) @@ -78,25 +88,64 @@ extern char **environ; #define BELL_NONE 0 #define BELL_ANY 1 #define BELL_CURRENT 2 +#define BELL_OTHER 3 /* Special key codes. */ -#define KEYC_NONE 0xfff -#define KEYC_BASE 0x1000 +#define KEYC_NONE 0xffff00000000ULL +#define KEYC_UNKNOWN 0xfffe00000000ULL +#define KEYC_BASE 0x100000000000ULL /* Key modifier bits. */ -#define KEYC_ESCAPE 0x2000 -#define KEYC_CTRL 0x4000 -#define KEYC_SHIFT 0x8000 -#define KEYC_PREFIX 0x10000 +#define KEYC_ESCAPE 0x200000000000ULL +#define KEYC_CTRL 0x400000000000ULL +#define KEYC_SHIFT 0x800000000000ULL /* Mask to obtain key w/o modifiers. */ -#define KEYC_MASK_MOD (KEYC_ESCAPE|KEYC_CTRL|KEYC_SHIFT|KEYC_PREFIX) +#define KEYC_MASK_MOD (KEYC_ESCAPE|KEYC_CTRL|KEYC_SHIFT) #define KEYC_MASK_KEY (~KEYC_MASK_MOD) -/* Other key codes. */ -enum key_code { - /* Mouse key. */ - KEYC_MOUSE = KEYC_BASE, +/* Is this a mouse key? */ +#define KEYC_IS_MOUSE(key) (((key) & KEYC_MASK_KEY) >= KEYC_MOUSE && \ + ((key) & KEYC_MASK_KEY) < KEYC_BSPACE) + +/* Mouse key codes. */ +#define KEYC_MOUSE_KEY(name) \ + KEYC_ ## name ## _PANE, \ + KEYC_ ## name ## _STATUS, \ + KEYC_ ## name ## _BORDER +#define KEYC_MOUSE_STRING(name, s) \ + { #s "Pane", KEYC_ ## name ## _PANE }, \ + { #s "Status", KEYC_ ## name ## _STATUS }, \ + { #s "Border", KEYC_ ## name ## _BORDER } + +/* + * A single key. This can be ASCII or Unicode or one of the keys starting at + * KEYC_BASE. + */ +typedef unsigned long long key_code; + +/* Special key codes. */ +enum { + /* Focus events. */ + KEYC_FOCUS_IN = KEYC_BASE, + KEYC_FOCUS_OUT, + + /* Mouse keys. */ + KEYC_MOUSE, /* unclassified mouse event */ + KEYC_MOUSE_KEY(MOUSEDOWN1), + KEYC_MOUSE_KEY(MOUSEDOWN2), + KEYC_MOUSE_KEY(MOUSEDOWN3), + KEYC_MOUSE_KEY(MOUSEUP1), + KEYC_MOUSE_KEY(MOUSEUP2), + KEYC_MOUSE_KEY(MOUSEUP3), + KEYC_MOUSE_KEY(MOUSEDRAG1), + KEYC_MOUSE_KEY(MOUSEDRAG2), + KEYC_MOUSE_KEY(MOUSEDRAG3), + KEYC_MOUSE_KEY(MOUSEDRAGEND1), + KEYC_MOUSE_KEY(MOUSEDRAGEND2), + KEYC_MOUSE_KEY(MOUSEDRAGEND3), + KEYC_MOUSE_KEY(WHEELUP), + KEYC_MOUSE_KEY(WHEELDOWN), /* Backspace key. */ KEYC_BSPACE, @@ -145,15 +194,13 @@ enum key_code { KEYC_KP_ENTER, KEYC_KP_ZERO, KEYC_KP_PERIOD, - - KEYC_FOCUS_IN, - KEYC_FOCUS_OUT, }; /* Termcap codes. */ enum tty_code_code { TTYC_AX = 0, TTYC_ACSC, /* acs_chars, ac */ + TTYC_BCE, /* back_color_erase, ut */ TTYC_BEL, /* bell, bl */ TTYC_BLINK, /* enter_blink_mode, mb */ TTYC_BOLD, /* enter_bold_mode, md */ @@ -348,37 +395,12 @@ enum tty_code_code { TTYC_SMSO, /* enter_standout_mode, so */ TTYC_SMUL, /* enter_underline_mode, us */ TTYC_SS, /* set cursor style, Ss */ + TTYC_TC, /* 24-bit "true" colour, Tc */ TTYC_TSL, /* to_status_line, tsl */ TTYC_VPA, /* row_address, cv */ TTYC_XENL, /* eat_newline_glitch, xn */ TTYC_XT, /* xterm(1)-compatible title, XT */ }; -#define NTTYCODE (TTYC_XT + 1) - -/* Termcap types. */ -enum tty_code_type { - TTYCODE_NONE = 0, - TTYCODE_STRING, - TTYCODE_NUMBER, - TTYCODE_FLAG, -}; - -/* Termcap code. */ -struct tty_code { - enum tty_code_type type; - union { - char *string; - int number; - int flag; - } value; -}; - -/* Entry in terminal code table. */ -struct tty_term_code_entry { - enum tty_code_code code; - enum tty_code_type type; - const char *name; -}; /* Message codes. */ enum msgtype { @@ -387,10 +409,12 @@ enum msgtype { MSG_IDENTIFY_FLAGS = 100, MSG_IDENTIFY_TERM, MSG_IDENTIFY_TTYNAME, - MSG_IDENTIFY_CWD, + MSG_IDENTIFY_OLDCWD, /* unused */ MSG_IDENTIFY_STDIN, MSG_IDENTIFY_ENVIRON, MSG_IDENTIFY_DONE, + MSG_IDENTIFY_CLIENTPID, + MSG_IDENTIFY_CWD, MSG_COMMAND = 200, MSG_DETACH, @@ -543,20 +567,6 @@ enum mode_key_cmd { MODEKEYCOPY_UP, }; -/* Entry in the default mode key tables. */ -struct mode_key_entry { - int key; - - /* - * Editing mode for vi: 0 is edit mode, keys not in the table are - * returned as MODEKEY_OTHER; 1 is command mode, keys not in the table - * are returned as MODEKEY_NONE. This is also matched on, allowing some - * keys to be bound in edit mode. - */ - int mode; - enum mode_key_cmd cmd; -}; - /* Data required while mode keys are in use. */ struct mode_key_data { struct mode_key_tree *tree; @@ -567,7 +577,7 @@ struct mode_key_data { /* Binding between a key and a command. */ struct mode_key_binding { - int key; + key_code key; int mode; enum mode_key_cmd cmd; @@ -584,6 +594,7 @@ struct mode_key_cmdstr { }; /* Named mode key table description. */ +struct mode_key_entry; struct mode_key_table { const char *name; const struct mode_key_cmdstr *cmdstr; @@ -607,14 +618,23 @@ struct mode_key_table { #define ALL_MOUSE_MODES (MODE_MOUSE_STANDARD|MODE_MOUSE_BUTTON) -/* A single UTF-8 character. */ +/* + * A single UTF-8 character. UTF8_SIZE must be big enough to hold at least one + * combining character as well. +*/ +#define UTF8_SIZE 9 struct utf8_data { u_char data[UTF8_SIZE]; - size_t have; - size_t size; + u_char have; + u_char size; - u_int width; + u_char width; /* 0xff if invalid */ +} __packed; +enum utf8_state { + UTF8_MORE, + UTF8_DONE, + UTF8_ERROR }; /* Grid attributes. */ @@ -631,46 +651,86 @@ struct utf8_data { #define GRID_FLAG_FG256 0x1 #define GRID_FLAG_BG256 0x2 #define GRID_FLAG_PADDING 0x4 +#define GRID_FLAG_EXTENDED 0x8 +#define GRID_FLAG_FGRGB 0x10 +#define GRID_FLAG_BGRGB 0x20 /* Grid line flags. */ #define GRID_LINE_WRAPPED 0x1 +/* Grid cell RGB colours. */ +struct grid_cell_rgb { + u_char r; + u_char g; + u_char b; +}; + /* Grid cell data. */ struct grid_cell { - u_char attr; - u_char flags; - u_char fg; - u_char bg; + u_char flags; + u_char attr; + union { + u_char fg; + struct grid_cell_rgb fg_rgb; + }; + union { + u_char bg; + struct grid_cell_rgb bg_rgb; + }; + struct utf8_data data; - u_char xstate; /* top 4 bits width, bottom 4 bits size */ - u_char xdata[UTF8_SIZE]; +}; +struct grid_cell_entry { + u_char flags; + union { + u_int offset; + struct { + u_char attr; + u_char fg; + u_char bg; + u_char data; + } data; + }; } __packed; /* Grid line. */ struct grid_line { - u_int cellsize; - struct grid_cell *celldata; + u_int cellsize; + struct grid_cell_entry *celldata; - int flags; + u_int extdsize; + struct grid_cell *extddata; + + int flags; } __packed; /* Entire grid of cells. */ struct grid { - int flags; -#define GRID_HISTORY 0x1 /* scroll lines into history */ + int flags; +#define GRID_HISTORY 0x1 /* scroll lines into history */ - u_int sx; - u_int sy; + u_int sx; + u_int sy; - u_int hsize; - u_int hlimit; + u_int hsize; + u_int hlimit; - struct grid_line *linedata; + struct grid_line *linedata; +}; + +/* Hook data structures. */ +struct hook { + const char *name; + + struct cmd_q *cmdq; + struct cmd_list *cmdlist; + + RB_ENTRY(hook) entry; }; /* Option data structures. */ struct options_entry { - char *name; + const char *name; enum { OPTIONS_STRING, @@ -679,19 +739,20 @@ struct options_entry { } type; char *str; - long long num; + long long num; struct grid_cell style; RB_ENTRY(options_entry) entry; }; -struct options { - RB_HEAD(options_tree, options_entry) tree; - struct options *parent; -}; - /* Scheduled job. */ struct job { + enum { + JOB_RUNNING, + JOB_DEAD, + JOB_CLOSED + } state; + char *cmd; pid_t pid; int status; @@ -762,70 +823,16 @@ struct screen_write_ctx { #define screen_hsize(s) ((s)->grid->hsize) #define screen_hlimit(s) ((s)->grid->hlimit) -/* Input parser cell. */ -struct input_cell { - struct grid_cell cell; - int set; - int g0set; /* 1 if ACS */ - int g1set; /* 1 if ACS */ -}; - -/* Input parser context. */ -struct input_ctx { - struct window_pane *wp; - struct screen_write_ctx ctx; - - struct input_cell cell; - - struct input_cell old_cell; - u_int old_cx; - u_int old_cy; - - u_char interm_buf[4]; - size_t interm_len; - - u_char param_buf[64]; - size_t param_len; - -#define INPUT_BUF_START 32 -#define INPUT_BUF_LIMIT 1048576 - u_char *input_buf; - size_t input_len; - size_t input_space; - - int param_list[24]; /* -1 not present */ - u_int param_list_len; - - struct utf8_data utf8data; - - int ch; - int flags; -#define INPUT_DISCARD 0x1 - - const struct input_state *state; - - /* - * All input received since we were last in the ground state. Sent to - * control clients on connection. - */ - struct evbuffer *since_ground; -}; - /* * Window mode. Windows can be in several modes and this is used to call the * right function to handle input and output. */ -struct session; -struct window; -struct mouse_event; struct window_mode { struct screen *(*init)(struct window_pane *); void (*free)(struct window_pane *); void (*resize)(struct window_pane *, u_int, u_int); - void (*key)(struct window_pane *, struct session *, int); - void (*mouse)(struct window_pane *, - struct session *, struct mouse_event *); - void (*timer)(struct window_pane *); + void (*key)(struct window_pane *, struct client *, struct session *, + key_code, struct mouse_event *); }; /* Structures for choose mode. */ @@ -844,20 +851,12 @@ struct window_choose_data { struct winlink *wl; int pane_id; - char *ft_template; + char *ft_template; struct format_tree *ft; char *command; }; -struct window_choose_mode_item { - struct window_choose_data *wcd; - char *name; - int pos; - int state; -#define TREE_EXPANDED 0x1 -}; - /* Child window structure. */ struct window_pane { u_int id; @@ -881,29 +880,33 @@ struct window_pane { #define PANE_RESIZE 0x8 #define PANE_FOCUSPUSH 0x10 #define PANE_INPUTOFF 0x20 +#define PANE_CHANGED 0x40 int argc; char **argv; char *shell; - int cwd; + const char *cwd; pid_t pid; char tty[TTY_NAME_MAX]; int status; - u_int changes; - struct event changes_timer; - u_int changes_redraw; - int fd; struct bufferevent *event; + struct event timer; - struct input_ctx ictx; + struct input_ctx *ictx; + + struct grid_cell colgc; int pipe_fd; struct bufferevent *pipe_event; size_t pipe_off; +#ifdef TMATE + size_t tmate_off; +#endif + struct screen *screen; struct screen base; @@ -921,15 +924,22 @@ struct window_pane { }; TAILQ_HEAD(window_panes, window_pane); RB_HEAD(window_pane_tree, window_pane); -ARRAY_DECL(window_pane_list, struct window_pane *); /* Window structure. */ struct window { u_int id; - char *name; - struct event name_timer; - struct timeval silence_timer; + char *name; + struct event name_event; + struct timeval name_time; + + struct event alerts_timer; + + struct timeval activity_time; + +#ifdef TMATE + struct window_pane *tmate_last_sync_active_pane; +#endif struct window_pane *active; struct window_pane *last; struct window_panes panes; @@ -937,6 +947,7 @@ struct window { int lastlayout; struct layout_cell *layout_root; struct layout_cell *saved_layout_root; + char *old_layout; u_int sx; u_int sy; @@ -951,11 +962,13 @@ struct window { #define WINDOW_FORCEHEIGHT 0x4000 #define WINDOW_ALERTFLAGS (WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_SILENCE) - struct options options; + struct options *options; u_int references; + + RB_ENTRY(window) entry; }; -ARRAY_DECL(windows, struct window *); +RB_HEAD(windows, window); /* Entry on local window list. */ struct winlink { @@ -966,7 +979,7 @@ struct winlink { struct grid_cell status_cell; char *status_text; - int flags; + int flags; #define WINLINK_BELL 0x1 #define WINLINK_ACTIVITY 0x2 #define WINLINK_SILENCE 0x4 @@ -1006,19 +1019,6 @@ struct layout_cell { TAILQ_ENTRY(layout_cell) entry; }; -/* Paste buffer. */ -struct paste_buffer { - char *data; - size_t size; - - char *name; - int automatic; - u_int order; - - RB_ENTRY(paste_buffer) name_entry; - RB_ENTRY(paste_buffer) time_entry; -}; - /* Environment variable. */ struct environ_entry { char *name; @@ -1026,7 +1026,6 @@ struct environ_entry { RB_ENTRY(environ_entry) entry; }; -RB_HEAD(environ, environ_entry); /* Client session. */ struct session_group { @@ -1040,12 +1039,15 @@ struct session { u_int id; char *name; - int cwd; + const char *cwd; struct timeval creation_time; + struct timeval last_attached_time; struct timeval activity_time; struct timeval last_activity_time; + struct event lock_timer; + u_int sx; u_int sy; @@ -1053,16 +1055,18 @@ struct session { struct winlink_stack lastw; struct winlinks windows; - struct options options; + struct hooks *hooks; + struct options *options; #define SESSION_UNATTACHED 0x1 /* not attached to any clients */ +#define SESSION_PASTING 0x2 int flags; - u_int attached; + u_int attached; struct termios *tio; - struct environ environ; + struct environ *environ; int references; @@ -1070,34 +1074,6 @@ struct session { RB_ENTRY(session) entry; }; RB_HEAD(sessions, session); -ARRAY_DECL(sessionslist, struct session *); - -/* TTY information. */ -struct tty_key { - char ch; - int key; - - struct tty_key *left; - struct tty_key *right; - - struct tty_key *next; -}; - -struct tty_term { - char *name; - u_int references; - - char acs[UCHAR_MAX + 1][2]; - - struct tty_code codes[NTTYCODE]; - -#define TERM_256COLOURS 0x1 -#define TERM_EARLYWRAP 0x2 - int flags; - - LIST_ENTRY(tty_term) entry; -}; -LIST_HEAD(tty_terms, tty_term); /* Mouse button masks. */ #define MOUSE_MASK_BUTTONS 3 @@ -1109,61 +1085,68 @@ LIST_HEAD(tty_terms, tty_term); /* Mouse wheel states. */ #define MOUSE_WHEEL_UP 0 -#define MOUSE_WHEEL_DOWN 1 +#define MOUSE_WHEEL_DOWN 64 -/* Mouse wheel multipler. */ -#define MOUSE_WHEEL_SCALE 3 +/* Mouse helpers. */ +#define MOUSE_BUTTONS(b) ((b) & MOUSE_MASK_BUTTONS) +#define MOUSE_WHEEL(b) ((b) & MOUSE_MASK_WHEEL) +#define MOUSE_DRAG(b) ((b) & MOUSE_MASK_DRAG) +#define MOUSE_RELEASE(b) (((b) & MOUSE_MASK_BUTTONS) == 3) -/* Mouse event bits. */ -#define MOUSE_EVENT_DOWN 0x1 -#define MOUSE_EVENT_DRAG 0x2 -#define MOUSE_EVENT_UP 0x4 -#define MOUSE_EVENT_CLICK 0x8 -#define MOUSE_EVENT_WHEEL 0x10 - -/* Mouse flag bits. */ -#define MOUSE_RESIZE_PANE 0x1 - -/* - * Mouse input. When sent by xterm: - * - * - buttons are in the bottom two bits: 0 = b1; 1 = b2; 2 = b3; 3 = released - * - bits 3, 4 and 5 are for keys - * - bit 6 is set for dragging - * - bit 7 for buttons 4 and 5 - * - * With the SGR 1006 extension the released button becomes known. Store these - * in separate fields and store the value converted to the old format in xb. - */ +/* Mouse input. */ struct mouse_event { - u_int xb; + int valid; - u_int x; - u_int lx; - u_int sx; + key_code key; + int statusat; - u_int y; - u_int ly; - u_int sy; + u_int x; + u_int y; + u_int b; - u_int sgr; /* whether the input arrived in SGR format */ - u_int sgr_xb; /* only for SGR: the unmangled button */ - u_int sgr_rel; /* only for SGR: if it is a release event */ + u_int lx; + u_int ly; + u_int lb; - u_int button; - u_int clicks; - u_int scroll; + int s; + int w; + int wp; - int wheel; - int event; - int flags; + u_int sgr_type; + u_int sgr_b; }; +/* TTY information. */ +struct tty_key { + char ch; + key_code key; + + struct tty_key *left; + struct tty_key *right; + + struct tty_key *next; +}; + +struct tty_code; +struct tty_term { + char *name; + u_int references; + + char acs[UCHAR_MAX + 1][2]; + + struct tty_code *codes; + +#define TERM_256COLOURS 0x1 +#define TERM_EARLYWRAP 0x2 + int flags; + + LIST_ENTRY(tty_term) entry; +}; +LIST_HEAD(tty_terms, tty_term); + struct tty { struct client *client; - char *path; - u_int class; u_int sx; u_int sy; @@ -1184,8 +1167,6 @@ struct tty { int fd; struct bufferevent *event; - int log_fd; - struct termios tio; struct grid_cell cell; @@ -1202,12 +1183,17 @@ struct tty { int term_flags; struct mouse_event mouse; + int mouse_drag_flag; + void (*mouse_drag_update)(struct client *, + struct mouse_event *); + void (*mouse_drag_release)(struct client *, + struct mouse_event *); struct event key_timer; struct tty_key *key_tree; }; -/* TTY command context and function pointer. */ +/* TTY command context. */ struct tty_ctx { struct window_pane *wp; @@ -1237,23 +1223,17 @@ struct tty_ctx { /* Saved message entry. */ struct message_entry { - char *msg; - time_t msg_time; + char *msg; + u_int msg_num; + time_t msg_time; + TAILQ_ENTRY(message_entry) entry; }; -/* Status output data from a job. */ -struct status_out { - char *cmd; - char *out; - - RB_ENTRY(status_out) entry; -}; -RB_HEAD(status_out_tree, status_out); - /* Client connection. */ struct client { - struct imsgbuf ibuf; + struct tmuxpeer *peer; + pid_t pid; int fd; struct event event; int retval; @@ -1261,10 +1241,10 @@ struct client { struct timeval creation_time; struct timeval activity_time; - struct environ environ; + struct environ *environ; char *title; - int cwd; + const char *cwd; char *term; char *ttyname; @@ -1273,25 +1253,23 @@ struct client { void (*stdin_callback)(struct client *, int, void *); void *stdin_callback_data; struct evbuffer *stdin_data; - int stdin_closed; + int stdin_closed; struct evbuffer *stdout_data; struct evbuffer *stderr_data; struct event repeat_timer; - struct status_out_tree status_old; - struct status_out_tree status_new; - struct timeval status_timer; + struct event status_timer; struct screen status; #define CLIENT_TERMINAL 0x1 -#define CLIENT_PREFIX 0x2 +#define CLIENT_LOGIN 0x2 #define CLIENT_EXIT 0x4 #define CLIENT_REDRAW 0x8 #define CLIENT_STATUS 0x10 #define CLIENT_REPEAT 0x20 #define CLIENT_SUSPENDED 0x40 -#define CLIENT_BAD 0x80 +/* 0x80 unused */ #define CLIENT_IDENTIFY 0x100 #define CLIENT_DEAD 0x200 #define CLIENT_BORDERS 0x400 @@ -1303,13 +1281,20 @@ struct client { #define CLIENT_UTF8 0x10000 #define CLIENT_256COLOURS 0x20000 #define CLIENT_IDENTIFIED 0x40000 +#define CLIENT_STATUSFORCE 0x80000 +#ifdef TMATE +/* TODO investigate if we can merge with CLIENT_STATUSFORCE */ +#define CLIENT_FORCE_STATUS 0x800000 +#endif int flags; + struct key_table *keytable; struct event identify_timer; char *message_string; struct event message_timer; - ARRAY_DECL(, struct message_entry) message_log; + u_int message_next; + TAILQ_HEAD(, message_entry) message_log; char *prompt_string; char *prompt_buffer; @@ -1317,7 +1302,7 @@ struct client { int (*prompt_callbackfn)(void *, const char *); void (*prompt_freefn)(void *); void *prompt_data; - u_int prompt_hindex; + u_int prompt_hindex; #define PROMPT_SINGLE 0x1 int prompt_flags; @@ -1331,21 +1316,51 @@ struct client { struct cmd_q *cmdq; int references; + + TAILQ_ENTRY(client) entry; }; -ARRAY_DECL(clients, struct client *); +TAILQ_HEAD(clients, client); /* Parsed arguments structures. */ -struct args_entry { - u_char flag; - char *value; - RB_ENTRY(args_entry) entry; -}; +struct args_entry; RB_HEAD(args_tree, args_entry); - struct args { struct args_tree tree; - int argc; - char **argv; + int argc; + char **argv; +}; + +/* Command find structures. */ +enum cmd_find_type { + CMD_FIND_PANE, + CMD_FIND_WINDOW, + CMD_FIND_SESSION, +}; +struct cmd_find_state { + struct cmd_q *cmdq; + int flags; + struct cmd_find_state *current; + + struct session *s; + struct winlink *wl; + struct window *w; + struct window_pane *wp; + int idx; +}; + +/* Command find flags. */ +#define CMD_FIND_PREFER_UNATTACHED 0x1 +#define CMD_FIND_QUIET 0x2 +#define CMD_FIND_WINDOW_INDEX 0x4 +#define CMD_FIND_DEFAULT_MARKED 0x8 +#define CMD_FIND_EXACT_SESSION 0x10 +#define CMD_FIND_EXACT_WINDOW 0x20 + +/* Context for command being executed. */ +struct cmd_state { + struct client *c; + struct cmd_find_state tflag; + struct cmd_find_state sflag; }; /* Command and list of commands. */ @@ -1361,9 +1376,10 @@ struct cmd { TAILQ_ENTRY(cmd) qentry; }; + struct cmd_list { - int references; - TAILQ_HEAD(, cmd) list; + int references; + TAILQ_HEAD(, cmd) list; }; /* Command return values. */ @@ -1377,6 +1393,9 @@ enum cmd_retval { /* Command queue entry. */ struct cmd_q_item { struct cmd_list *cmdlist; + + struct mouse_event mouse; + TAILQ_ENTRY(cmd_q_item) qentry; }; TAILQ_HEAD(cmd_q_items, cmd_q_item); @@ -1384,7 +1403,10 @@ TAILQ_HEAD(cmd_q_items, cmd_q_item); /* Command queue. */ struct cmd_q { int references; - int dead; + int flags; +#define CMD_Q_DEAD 0x1 +#define CMD_Q_REENTRY 0x2 +#define CMD_Q_NOHOOKS 0x4 struct client *client; int client_exit; @@ -1392,6 +1414,10 @@ struct cmd_q { struct cmd_q_items queue; struct cmd_q_item *item; struct cmd *cmd; + struct cmd_q *parent; + + struct cmd_find_state current; + struct cmd_state state; time_t time; u_int number; @@ -1399,38 +1425,76 @@ struct cmd_q { void (*emptyfn)(struct cmd_q *); void *data; - TAILQ_ENTRY(cmd_q) waitentry; + TAILQ_ENTRY(cmd_q) waitentry; +}; + +/* Command -c, -t or -s flags. */ +enum cmd_entry_flag { + CMD_NONE, + + CMD_CLIENT, + CMD_CLIENT_CANFAIL, + + CMD_SESSION, + CMD_SESSION_CANFAIL, + CMD_SESSION_PREFERUNATTACHED, + CMD_SESSION_WITHPANE, + + CMD_WINDOW, + CMD_WINDOW_CANFAIL, + CMD_WINDOW_MARKED, + CMD_WINDOW_INDEX, + + CMD_PANE, + CMD_PANE_CANFAIL, + CMD_PANE_MARKED, + + CMD_MOVEW_R, }; /* Command definition. */ struct cmd_entry { - const char *name; - const char *alias; + const char *name; + const char *alias; - const char *args_template; - int args_lower; - int args_upper; + struct { + const char *template; + int lower; + int upper; + } args; + const char *usage; - const char *usage; + enum cmd_entry_flag tflag; + enum cmd_entry_flag sflag; + enum cmd_entry_flag cflag; #define CMD_STARTSERVER 0x1 -#define CMD_CANTNEST 0x2 -#define CMD_READONLY 0x4 +#define CMD_READONLY 0x2 int flags; - enum cmd_retval (*exec)(struct cmd *, struct cmd_q *); + enum cmd_retval (*exec)(struct cmd *, struct cmd_q *); }; -/* Key binding. */ +/* Key binding and key table. */ struct key_binding { - int key; - struct cmd_list *cmdlist; - int can_repeat; + key_code key; + struct cmd_list *cmdlist; + int can_repeat; - RB_ENTRY(key_binding) entry; + RB_ENTRY(key_binding) entry; }; RB_HEAD(key_bindings, key_binding); +struct key_table { + const char *name; + struct key_bindings key_bindings; + + u_int references; + + RB_ENTRY(key_table) entry; +}; +RB_HEAD(key_tables, key_table); + /* * Option table entries. The option table is the user-visible part of the * option, as opposed to the internal options (struct option) which are just @@ -1446,19 +1510,26 @@ enum options_table_type { OPTIONS_TABLE_CHOICE, OPTIONS_TABLE_STYLE }; +enum options_table_scope { + OPTIONS_TABLE_NONE, + OPTIONS_TABLE_SERVER, + OPTIONS_TABLE_SESSION, + OPTIONS_TABLE_WINDOW, +}; struct options_table_entry { - const char *name; - enum options_table_type type; + const char *name; + enum options_table_type type; + enum options_table_scope scope; - u_int minimum; - u_int maximum; - const char **choices; + u_int minimum; + u_int maximum; + const char **choices; - const char *default_str; - long long default_num; + const char *default_str; + long long default_num; - const char *style; + const char *style; }; /* Common command usages. */ @@ -1473,43 +1544,70 @@ struct options_table_entry { #define CMD_BUFFER_USAGE "[-b buffer-name]" /* tmux.c */ -extern struct options global_options; -extern struct options global_s_options; -extern struct options global_w_options; -extern struct environ global_environ; -extern struct event_base *ev_base; -extern char *cfg_file; -extern char *shell_cmd; -extern int debug_level; -extern time_t start_time; -extern char socket_path[PATH_MAX]; -extern int login_shell; -extern char *environ_path; -void logfile(const char *); +extern struct hooks *global_hooks; +extern struct options *global_options; +extern struct options *global_s_options; +extern struct options *global_w_options; +extern struct environ *global_environ; +extern struct timeval start_time; +extern const char *socket_path; +#ifdef TMATE +extern int tmate_foreground; +void tmate_load_cli_options(void); +#endif const char *getshell(void); int checkshell(const char *); int areshell(const char *); void setblocking(int, int); -__dead void shell_exec(const char *, const char *); +const char *find_home(void); + +/* proc.c */ +struct imsg; +int proc_send(struct tmuxpeer *, enum msgtype, int, const void *, size_t); +int proc_send_s(struct tmuxpeer *, enum msgtype, const char *); +struct tmuxproc *proc_start(const char *, struct event_base *, int, + void (*)(int)); +void proc_loop(struct tmuxproc *, int (*)(void)); +void proc_exit(struct tmuxproc *); +struct tmuxpeer *proc_add_peer(struct tmuxproc *, int, + void (*)(struct imsg *, void *), void *); +void proc_remove_peer(struct tmuxpeer *); +void proc_kill_peer(struct tmuxpeer *); /* cfg.c */ -extern struct cmd_q *cfg_cmd_q; extern int cfg_finished; extern int cfg_references; extern struct client *cfg_client; +extern char **cfg_causes; +extern u_int cfg_ncauses; +void start_cfg(void); int load_cfg(const char *, struct cmd_q *, char **); -void cfg_default_done(struct cmd_q *); -void cfg_add_cause(const char *, ...); +void set_cfg_file(const char *); +void printflike(1, 2) cfg_add_cause(const char *, ...); void cfg_print_causes(struct cmd_q *); void cfg_show_causes(struct session *); +/* paste.c */ +struct paste_buffer; +const char *paste_buffer_name(struct paste_buffer *); +const char *paste_buffer_data(struct paste_buffer *, size_t *); +struct paste_buffer *paste_walk(struct paste_buffer *); +struct paste_buffer *paste_get_top(const char **); +struct paste_buffer *paste_get_name(const char *); +void paste_free(struct paste_buffer *); +void paste_add(char *, size_t); +int paste_rename(const char *, const char *, char **); +int paste_set(char *, size_t, const char *, char **); +char *paste_make_sample(struct paste_buffer *); + /* format.c */ +#define FORMAT_STATUS 0x1 +#define FORMAT_FORCE 0x2 struct format_tree; -struct format_tree *format_create(void); +struct format_tree *format_create(struct cmd_q *, int); void format_free(struct format_tree *); void printflike(3, 4) format_add(struct format_tree *, const char *, const char *, ...); -const char *format_find(struct format_tree *, const char *); char *format_expand_time(struct format_tree *, const char *, time_t); char *format_expand(struct format_tree *, const char *); void format_defaults(struct format_tree *, struct client *, @@ -1518,7 +1616,23 @@ void format_defaults_window(struct format_tree *, struct window *); void format_defaults_pane(struct format_tree *, struct window_pane *); void format_defaults_paste_buffer(struct format_tree *, - struct paste_buffer *, int); + struct paste_buffer *); + +/* hooks.c */ +struct hook; +struct hooks *hooks_get(struct session *); +struct hooks *hooks_create(struct hooks *); +void hooks_free(struct hooks *); +struct hook *hooks_first(struct hooks *); +struct hook *hooks_next(struct hook *); +void hooks_add(struct hooks *, const char *, struct cmd_list *); +void hooks_copy(struct hooks *, struct hooks *); +void hooks_remove(struct hooks *, const char *); +struct hook *hooks_find(struct hooks *, const char *); +int printflike(4, 5) hooks_run(struct hooks *, struct client *, + struct cmd_find_state *, const char *, ...); +int printflike(4, 5) hooks_wait(struct hooks *, struct cmd_q *, + struct cmd_find_state *, const char *, ...); /* mode-key.c */ extern const struct mode_key_table mode_key_tables[]; @@ -1537,7 +1651,8 @@ enum mode_key_cmd mode_key_fromstring(const struct mode_key_cmdstr *, const struct mode_key_table *mode_key_findtable(const char *); void mode_key_init_trees(void); void mode_key_init(struct mode_key_data *, struct mode_key_tree *); -enum mode_key_cmd mode_key_lookup(struct mode_key_data *, int, const char **); +enum mode_key_cmd mode_key_lookup(struct mode_key_data *, key_code, + const char **); /* notify.c */ void notify_enable(void); @@ -1553,10 +1668,10 @@ void notify_session_created(struct session *); void notify_session_closed(struct session *); /* options.c */ -int options_cmp(struct options_entry *, struct options_entry *); -RB_PROTOTYPE(options_tree, options_entry, entry, options_cmp); -void options_init(struct options *, struct options *); +struct options *options_create(struct options *); void options_free(struct options *); +struct options_entry *options_first(struct options *); +struct options_entry *options_next(struct options_entry *); struct options_entry *options_find1(struct options *, const char *); struct options_entry *options_find(struct options *, const char *); void options_remove(struct options *, const char *); @@ -1571,40 +1686,40 @@ struct options_entry *options_set_style(struct options *, const char *, struct grid_cell *options_get_style(struct options *, const char *); /* options-table.c */ -extern const struct options_table_entry server_options_table[]; -extern const struct options_table_entry session_options_table[]; -extern const struct options_table_entry window_options_table[]; -void options_table_populate_tree(const struct options_table_entry *, - struct options *); +extern const struct options_table_entry options_table[]; +void options_table_populate_tree(enum options_table_scope, struct options *); const char *options_table_print_entry(const struct options_table_entry *, struct options_entry *, int); -int options_table_find(const char *, const struct options_table_entry **, - const struct options_table_entry **); +int options_table_find(const char *, const struct options_table_entry **); /* job.c */ extern struct joblist all_jobs; -struct job *job_run(const char *, struct session *, +struct job *job_run(const char *, struct session *, const char *, void (*)(struct job *), void (*)(void *), void *); void job_free(struct job *); void job_died(struct job *, int); /* environ.c */ -int environ_cmp(struct environ_entry *, struct environ_entry *); -RB_PROTOTYPE(environ, environ_entry, entry, environ_cmp); -void environ_init(struct environ *); +struct environ *environ_create(void); void environ_free(struct environ *); +struct environ_entry *environ_first(struct environ *); +struct environ_entry *environ_next(struct environ_entry *); void environ_copy(struct environ *, struct environ *); struct environ_entry *environ_find(struct environ *, const char *); -void environ_set(struct environ *, const char *, const char *); +void printflike(3, 4) environ_set(struct environ *, const char *, const char *, + ...); +void environ_clear(struct environ *, const char *); void environ_put(struct environ *, const char *); void environ_unset(struct environ *, const char *); void environ_update(const char *, struct environ *, struct environ *); void environ_push(struct environ *); /* tty.c */ +void tty_create_log(void); void tty_init_termios(int, struct termios *, struct bufferevent *); void tty_raw(struct tty *, const char *); -void tty_attributes(struct tty *, const struct grid_cell *); +void tty_attributes(struct tty *, const struct grid_cell *, + const struct window_pane *); void tty_reset(struct tty *); void tty_region_pane(struct tty *, const struct tty_ctx *, u_int, u_int); void tty_region(struct tty *, u_int, u_int); @@ -1619,21 +1734,24 @@ void tty_putcode_ptr2(struct tty *, enum tty_code_code, const void *, void tty_puts(struct tty *, const char *); void tty_putc(struct tty *, u_char); void tty_putn(struct tty *, const void *, size_t, u_int); -void tty_init(struct tty *, struct client *, int, char *); +int tty_init(struct tty *, struct client *, int, char *); int tty_resize(struct tty *); int tty_set_size(struct tty *, u_int, u_int); -void tty_set_class(struct tty *, u_int); void tty_start_tty(struct tty *); void tty_stop_tty(struct tty *); void tty_set_title(struct tty *, const char *); void tty_update_mode(struct tty *, int, struct screen *); void tty_force_cursor_colour(struct tty *, const char *); -void tty_draw_line(struct tty *, struct screen *, u_int, u_int, u_int); +void tty_draw_pane(struct tty *, const struct window_pane *, u_int, u_int, + u_int); +void tty_draw_line(struct tty *, const struct window_pane *, struct screen *, + u_int, u_int, u_int); int tty_open(struct tty *, char **); void tty_close(struct tty *); void tty_free(struct tty *); -void tty_write( - void (*)(struct tty *, const struct tty_ctx *), struct tty_ctx *); +void tty_write(void (*)(struct tty *, const struct tty_ctx *), + struct tty_ctx *); +int tty_client_ready(struct client *, struct window_pane *wp); void tty_cmd_alignmenttest(struct tty *, const struct tty_ctx *); void tty_cmd_cell(struct tty *, const struct tty_ctx *); void tty_cmd_clearendofline(struct tty *, const struct tty_ctx *); @@ -1653,45 +1771,32 @@ void tty_cmd_utf8character(struct tty *, const struct tty_ctx *); void tty_cmd_reverseindex(struct tty *, const struct tty_ctx *); void tty_cmd_setselection(struct tty *, const struct tty_ctx *); void tty_cmd_rawstring(struct tty *, const struct tty_ctx *); -void tty_bell(struct tty *); /* tty-term.c */ extern struct tty_terms tty_terms; -extern const struct tty_term_code_entry tty_term_codes[NTTYCODE]; +u_int tty_term_ncodes(void); struct tty_term *tty_term_find(char *, int, char **); void tty_term_free(struct tty_term *); int tty_term_has(struct tty_term *, enum tty_code_code); const char *tty_term_string(struct tty_term *, enum tty_code_code); const char *tty_term_string1(struct tty_term *, enum tty_code_code, int); -const char *tty_term_string2( - struct tty_term *, enum tty_code_code, int, int); -const char *tty_term_ptr1( - struct tty_term *, enum tty_code_code, const void *); +const char *tty_term_string2(struct tty_term *, enum tty_code_code, int, + int); +const char *tty_term_ptr1(struct tty_term *, enum tty_code_code, + const void *); const char *tty_term_ptr2(struct tty_term *, enum tty_code_code, const void *, const void *); int tty_term_number(struct tty_term *, enum tty_code_code); int tty_term_flag(struct tty_term *, enum tty_code_code); +const char *tty_term_describe(struct tty_term *, enum tty_code_code); /* tty-acs.c */ const char *tty_acs_get(struct tty *, u_char); /* tty-keys.c */ -void tty_keys_build(struct tty *); -void tty_keys_free(struct tty *); -int tty_keys_next(struct tty *); - -/* paste.c */ -struct paste_buffer *paste_walk(struct paste_buffer *); -struct paste_buffer *paste_get_top(void); -struct paste_buffer *paste_get_name(const char *); -int paste_free_top(void); -int paste_free_name(const char *); -void paste_add(char *, size_t); -int paste_rename(const char *, const char *, char **); -int paste_set(char *, size_t, const char *, char **); -char *paste_make_sample(struct paste_buffer *, int); -void paste_send_pane(struct paste_buffer *, struct window_pane *, - const char *, int); +void tty_keys_build(struct tty *); +void tty_keys_free(struct tty *); +key_code tty_keys_next(struct tty *); /* arguments.c */ int args_cmp(struct args_entry *, struct args_entry *); @@ -1699,12 +1804,33 @@ RB_PROTOTYPE(args_tree, args_entry, entry, args_cmp); struct args *args_create(int, ...); struct args *args_parse(const char *, int, char **); void args_free(struct args *); -size_t args_print(struct args *, char *, size_t); +char *args_print(struct args *); int args_has(struct args *, u_char); void args_set(struct args *, u_char, const char *); const char *args_get(struct args *, u_char); -long long args_strtonum( - struct args *, u_char, long long, long long, char **); +long long args_strtonum(struct args *, u_char, long long, long long, + char **); + +/* cmd-find.c */ +int cmd_find_current(struct cmd_find_state *, struct cmd_q *, + int); +int cmd_find_target(struct cmd_find_state *, + struct cmd_find_state *, struct cmd_q *, const char *, + enum cmd_find_type, int); +struct client *cmd_find_client(struct cmd_q *, const char *, int); +void cmd_find_clear_state(struct cmd_find_state *, struct cmd_q *, + int); +int cmd_find_valid_state(struct cmd_find_state *); +void cmd_find_copy_state(struct cmd_find_state *, + struct cmd_find_state *); +void cmd_find_log_state(const char *, struct cmd_find_state *); +int cmd_find_from_session(struct cmd_find_state *, + struct session *); +int cmd_find_from_winlink(struct cmd_find_state *, + struct session *, struct winlink *); +int cmd_find_from_window(struct cmd_find_state *, struct window *); +int cmd_find_from_pane(struct cmd_find_state *, + struct window_pane *); /* cmd.c */ int cmd_pack_argv(int, char **, char *, size_t); @@ -1713,116 +1839,25 @@ char **cmd_copy_argv(int, char **); void cmd_free_argv(int, char **); char *cmd_stringify_argv(int, char **); struct cmd *cmd_parse(int, char **, const char *, u_int, char **); -size_t cmd_print(struct cmd *, char *, size_t); -struct session *cmd_current_session(struct cmd_q *, int); -struct client *cmd_current_client(struct cmd_q *); -struct client *cmd_find_client(struct cmd_q *, const char *, int); -struct session *cmd_find_session(struct cmd_q *, const char *, int); -struct winlink *cmd_find_window(struct cmd_q *, const char *, - struct session **); -int cmd_find_index(struct cmd_q *, const char *, - struct session **); -struct winlink *cmd_find_pane(struct cmd_q *, const char *, struct session **, - struct window_pane **); +int cmd_prepare_state(struct cmd *, struct cmd_q *, + struct cmd_q *); +char *cmd_print(struct cmd *); +int cmd_mouse_at(struct window_pane *, struct mouse_event *, + u_int *, u_int *, int); +struct winlink *cmd_mouse_window(struct mouse_event *, struct session **); +struct window_pane *cmd_mouse_pane(struct mouse_event *, struct session **, + struct winlink **); char *cmd_template_replace(const char *, const char *, int); -struct window *cmd_lookup_windowid(const char *); -struct window_pane *cmd_lookup_paneid(const char *); extern const struct cmd_entry *cmd_table[]; -extern const struct cmd_entry cmd_attach_session_entry; -extern const struct cmd_entry cmd_bind_key_entry; -extern const struct cmd_entry cmd_break_pane_entry; -extern const struct cmd_entry cmd_capture_pane_entry; -extern const struct cmd_entry cmd_choose_buffer_entry; -extern const struct cmd_entry cmd_choose_client_entry; -extern const struct cmd_entry cmd_choose_session_entry; -extern const struct cmd_entry cmd_choose_tree_entry; -extern const struct cmd_entry cmd_choose_window_entry; -extern const struct cmd_entry cmd_clear_history_entry; -extern const struct cmd_entry cmd_clock_mode_entry; -extern const struct cmd_entry cmd_command_prompt_entry; -extern const struct cmd_entry cmd_confirm_before_entry; -extern const struct cmd_entry cmd_copy_mode_entry; -extern const struct cmd_entry cmd_delete_buffer_entry; -extern const struct cmd_entry cmd_detach_client_entry; -extern const struct cmd_entry cmd_display_message_entry; -extern const struct cmd_entry cmd_display_panes_entry; -extern const struct cmd_entry cmd_down_pane_entry; -extern const struct cmd_entry cmd_find_window_entry; -extern const struct cmd_entry cmd_has_session_entry; -extern const struct cmd_entry cmd_if_shell_entry; -extern const struct cmd_entry cmd_join_pane_entry; -extern const struct cmd_entry cmd_kill_pane_entry; -extern const struct cmd_entry cmd_kill_server_entry; -extern const struct cmd_entry cmd_kill_session_entry; -extern const struct cmd_entry cmd_kill_window_entry; -extern const struct cmd_entry cmd_last_pane_entry; -extern const struct cmd_entry cmd_last_window_entry; -extern const struct cmd_entry cmd_link_window_entry; -extern const struct cmd_entry cmd_list_buffers_entry; -extern const struct cmd_entry cmd_list_clients_entry; -extern const struct cmd_entry cmd_list_commands_entry; -extern const struct cmd_entry cmd_list_keys_entry; -extern const struct cmd_entry cmd_list_panes_entry; -extern const struct cmd_entry cmd_list_sessions_entry; -extern const struct cmd_entry cmd_list_windows_entry; -extern const struct cmd_entry cmd_load_buffer_entry; -extern const struct cmd_entry cmd_lock_client_entry; -extern const struct cmd_entry cmd_lock_server_entry; -extern const struct cmd_entry cmd_lock_session_entry; -extern const struct cmd_entry cmd_move_pane_entry; -extern const struct cmd_entry cmd_move_window_entry; -extern const struct cmd_entry cmd_new_session_entry; -extern const struct cmd_entry cmd_new_window_entry; -extern const struct cmd_entry cmd_next_layout_entry; -extern const struct cmd_entry cmd_next_window_entry; -extern const struct cmd_entry cmd_paste_buffer_entry; -extern const struct cmd_entry cmd_pipe_pane_entry; -extern const struct cmd_entry cmd_previous_layout_entry; -extern const struct cmd_entry cmd_previous_window_entry; -extern const struct cmd_entry cmd_refresh_client_entry; -extern const struct cmd_entry cmd_rename_session_entry; -extern const struct cmd_entry cmd_rename_window_entry; -extern const struct cmd_entry cmd_resize_pane_entry; -extern const struct cmd_entry cmd_respawn_pane_entry; -extern const struct cmd_entry cmd_respawn_window_entry; -extern const struct cmd_entry cmd_rotate_window_entry; -extern const struct cmd_entry cmd_run_shell_entry; -extern const struct cmd_entry cmd_save_buffer_entry; -extern const struct cmd_entry cmd_select_layout_entry; -extern const struct cmd_entry cmd_select_pane_entry; -extern const struct cmd_entry cmd_select_window_entry; -extern const struct cmd_entry cmd_send_keys_entry; -extern const struct cmd_entry cmd_send_prefix_entry; -extern const struct cmd_entry cmd_server_info_entry; -extern const struct cmd_entry cmd_set_buffer_entry; -extern const struct cmd_entry cmd_set_environment_entry; -extern const struct cmd_entry cmd_set_option_entry; -extern const struct cmd_entry cmd_set_window_option_entry; -extern const struct cmd_entry cmd_show_buffer_entry; -extern const struct cmd_entry cmd_show_environment_entry; -extern const struct cmd_entry cmd_show_messages_entry; -extern const struct cmd_entry cmd_show_options_entry; -extern const struct cmd_entry cmd_show_window_options_entry; -extern const struct cmd_entry cmd_source_file_entry; -extern const struct cmd_entry cmd_split_window_entry; -extern const struct cmd_entry cmd_start_server_entry; -extern const struct cmd_entry cmd_suspend_client_entry; -extern const struct cmd_entry cmd_swap_pane_entry; -extern const struct cmd_entry cmd_swap_window_entry; -extern const struct cmd_entry cmd_switch_client_entry; -extern const struct cmd_entry cmd_unbind_key_entry; -extern const struct cmd_entry cmd_unlink_window_entry; -extern const struct cmd_entry cmd_up_pane_entry; -extern const struct cmd_entry cmd_wait_for_entry; /* cmd-attach-session.c */ -enum cmd_retval cmd_attach_session(struct cmd_q *, const char *, int, int, - const char *); +enum cmd_retval cmd_attach_session(struct cmd_q *, int, int, const char *, + int); /* cmd-list.c */ struct cmd_list *cmd_list_parse(int, char **, const char *, u_int, char **); void cmd_list_free(struct cmd_list *); -size_t cmd_list_print(struct cmd_list *, char *, size_t); +char *cmd_list_print(struct cmd_list *); /* cmd-queue.c */ struct cmd_q *cmdq_new(struct client *); @@ -1830,8 +1865,10 @@ int cmdq_free(struct cmd_q *); void printflike(2, 3) cmdq_print(struct cmd_q *, const char *, ...); void printflike(2, 3) cmdq_error(struct cmd_q *, const char *, ...); void cmdq_guard(struct cmd_q *, const char *, int); -void cmdq_run(struct cmd_q *, struct cmd_list *); -void cmdq_append(struct cmd_q *, struct cmd_list *); +void cmdq_run(struct cmd_q *, struct cmd_list *, + struct mouse_event *); +void cmdq_append(struct cmd_q *, struct cmd_list *, + struct mouse_event *); int cmdq_continue(struct cmd_q *); void cmdq_flush(struct cmd_q *); @@ -1840,51 +1877,72 @@ int cmd_string_parse(const char *, struct cmd_list **, const char *, u_int, char **); /* cmd-wait-for.c */ +#ifdef TMATE +void signal_waiting_clients(const char *name); +#endif void cmd_wait_for_flush(void); /* client.c */ -int client_main(int, char **, int); +#define DEFER_ERRORS_CFG 1 +int run_headless_command(int argc, const char **argv, int flags, void (*err_callback)(const char *)); +void run_initial_client_cmd(void); +int client_main(struct event_base *, int, char **, int, const char *); /* key-bindings.c */ -extern struct key_bindings key_bindings; -int key_bindings_cmp(struct key_binding *, struct key_binding *); RB_PROTOTYPE(key_bindings, key_binding, entry, key_bindings_cmp); -struct key_binding *key_bindings_lookup(int); -void key_bindings_add(int, int, struct cmd_list *); -void key_bindings_remove(int); +RB_PROTOTYPE(key_tables, key_table, entry, key_table_cmp); +extern struct key_tables key_tables; +int key_table_cmp(struct key_table *, struct key_table *); +int key_bindings_cmp(struct key_binding *, struct key_binding *); +struct key_table *key_bindings_get_table(const char *, int); +void key_bindings_unref_table(struct key_table *); +void key_bindings_add(const char *, key_code, int, struct cmd_list *); +void key_bindings_remove(const char *, key_code); +void key_bindings_remove_table(const char *); void key_bindings_init(void); -void key_bindings_dispatch(struct key_binding *, struct client *); +void key_bindings_dispatch(struct key_binding *, struct client *, + struct mouse_event *); /* key-string.c */ -int key_string_lookup_string(const char *); -const char *key_string_lookup_key(int); +key_code key_string_lookup_string(const char *); +const char *key_string_lookup_key(key_code); + +/* alerts.c */ +void alerts_reset_all(void); +void alerts_queue(struct window *, int); +void alerts_check_session(struct session *); /* server.c */ +extern int server_exit; +extern struct tmuxproc *server_proc; extern struct clients clients; -extern struct clients dead_clients; -int server_start(int, char *); +extern struct cmd_find_state marked_pane; +void server_set_marked(struct session *, struct winlink *, + struct window_pane *); +void server_clear_marked(void); +int server_is_marked(struct session *, struct winlink *, + struct window_pane *); +int server_check_marked(void); +int server_start(struct event_base *, int, char *); void server_update_socket(void); void server_add_accept(int); /* server-client.c */ -void server_client_handle_key(struct client *, int); +void server_client_set_key_table(struct client *, const char *); +const char *server_client_get_key_table(struct client *); +int server_client_check_nested(struct client *); +void server_client_handle_key(struct client *, key_code); void server_client_create(int); -int server_client_open(struct client *, char **); +int server_client_open(struct client *, char **); +void server_client_unref(struct client *); void server_client_lost(struct client *); -void server_client_callback(int, short, void *); -void server_client_status_timer(void); +void server_client_detach(struct client *, enum msgtype); void server_client_loop(void); - -/* server-window.c */ -void server_window_loop(void); +void server_client_push_stdout(struct client *); +void server_client_push_stderr(struct client *); /* server-fn.c */ void server_fill_environ(struct session *, struct environ *); -void server_write_ready(struct client *); -int server_write_client(struct client *, enum msgtype, const void *, - size_t); -void server_write_session(struct session *, enum msgtype, const void *, - size_t); void server_redraw_client(struct client *); void server_status_client(struct client *); void server_redraw_session(struct session *); @@ -1897,31 +1955,25 @@ void server_status_window(struct window *); void server_lock(void); void server_lock_session(struct session *); void server_lock_client(struct client *); -int server_unlock(const char *); void server_kill_window(struct window *); int server_link_window(struct session *, struct winlink *, struct session *, int, int, int, char **); void server_unlink_window(struct session *, struct winlink *); -void server_destroy_pane(struct window_pane *); +void server_destroy_pane(struct window_pane *, int); void server_destroy_session_group(struct session *); void server_destroy_session(struct session *); void server_check_unattached(void); void server_set_identify(struct client *); void server_clear_identify(struct client *); -void server_update_event(struct client *); -void server_push_stdout(struct client *); -void server_push_stderr(struct client *); int server_set_stdin_callback(struct client *, void (*)(struct client *, int, void *), void *, char **); void server_unzoom_window(struct window *); /* status.c */ -int status_out_cmp(struct status_out *, struct status_out *); -RB_PROTOTYPE(status_out_tree, status_out, entry, status_out_cmp); +void status_timer_start(struct client *); +void status_timer_start_all(void); int status_at_line(struct client *); -void status_free_jobs(struct status_out_tree *); -void status_update_jobs(struct client *); -void status_set_window_at(struct client *, u_int); +struct window *status_get_window_at(struct client *, u_int); int status_redraw(struct client *); void printflike(2, 3) status_message_set(struct client *, const char *, ...); void status_message_clear(struct client *); @@ -1930,8 +1982,10 @@ void status_prompt_set(struct client *, const char *, const char *, int (*)(void *, const char *), void (*)(void *), void *, int); void status_prompt_clear(struct client *); int status_prompt_redraw(struct client *); -void status_prompt_key(struct client *, int); +void status_prompt_key(struct client *, key_code); void status_prompt_update(struct client *, const char *, const char *); +void status_prompt_load_history(void); +void status_prompt_save_history(void); /* resize.c */ void recalculate_sizes(void); @@ -1939,18 +1993,19 @@ void recalculate_sizes(void); /* input.c */ void input_init(struct window_pane *); void input_free(struct window_pane *); +void input_reset(struct window_pane *, int); +struct evbuffer *input_pending(struct window_pane *); void input_parse(struct window_pane *); /* input-key.c */ -void input_key(struct window_pane *, int); -void input_mouse(struct window_pane *, struct session *, - struct mouse_event *); +void input_key(struct window_pane *, key_code, struct mouse_event *); /* xterm-keys.c */ -char *xterm_keys_lookup(int); -int xterm_keys_find(const char *, size_t, size_t *, int *); +char *xterm_keys_lookup(key_code); +int xterm_keys_find(const char *, size_t, size_t *, key_code *); /* colour.c */ +int colour_find_rgb(u_char, u_char, u_char); void colour_set_fg(struct grid_cell *, int); void colour_set_bg(struct grid_cell *, int); const char *colour_tostring(int); @@ -1963,17 +2018,16 @@ int attributes_fromstring(const char *); /* grid.c */ extern const struct grid_cell grid_default_cell; -extern const struct grid_cell grid_marker_cell; struct grid *grid_create(u_int, u_int, u_int); void grid_destroy(struct grid *); int grid_compare(struct grid *, struct grid *); void grid_collect_history(struct grid *); void grid_scroll_history(struct grid *); void grid_scroll_history_region(struct grid *, u_int, u_int); +void grid_clear_history(struct grid *); void grid_expand_line(struct grid *, u_int, u_int); -const struct grid_cell *grid_peek_cell(struct grid *, u_int, u_int); const struct grid_line *grid_peek_line(struct grid *, u_int); -struct grid_cell *grid_get_cell(struct grid *, u_int, u_int); +void grid_get_cell(struct grid *, u_int, u_int, struct grid_cell *); void grid_set_cell(struct grid *, u_int, u_int, const struct grid_cell *); void grid_clear(struct grid *, u_int, u_int, u_int, u_int); void grid_clear_lines(struct grid *, u_int, u_int); @@ -1981,21 +2035,14 @@ void grid_move_lines(struct grid *, u_int, u_int, u_int); void grid_move_cells(struct grid *, u_int, u_int, u_int, u_int); char *grid_string_cells(struct grid *, u_int, u_int, u_int, struct grid_cell **, int, int, int); -void grid_duplicate_lines( - struct grid *, u_int, struct grid *, u_int, u_int); +void grid_duplicate_lines(struct grid *, u_int, struct grid *, u_int, + u_int); u_int grid_reflow(struct grid *, struct grid *, u_int); -/* grid-cell.c */ -u_int grid_cell_width(const struct grid_cell *); -void grid_cell_get(const struct grid_cell *, struct utf8_data *); -void grid_cell_set(struct grid_cell *, const struct utf8_data *); -void grid_cell_one(struct grid_cell *, u_char); - /* grid-view.c */ -const struct grid_cell *grid_view_peek_cell(struct grid *, u_int, u_int); -struct grid_cell *grid_view_get_cell(struct grid *, u_int, u_int); -void grid_view_set_cell( - struct grid *, u_int, u_int, const struct grid_cell *); +void grid_view_get_cell(struct grid *, u_int, u_int, struct grid_cell *); +void grid_view_set_cell(struct grid *, u_int, u_int, + const struct grid_cell *); void grid_view_clear_history(struct grid *); void grid_view_clear(struct grid *, u_int, u_int, u_int, u_int); void grid_view_scroll_region_up(struct grid *, u_int, u_int); @@ -2009,24 +2056,24 @@ void grid_view_delete_cells(struct grid *, u_int, u_int, u_int); char *grid_view_string_cells(struct grid *, u_int, u_int, u_int); /* screen-write.c */ -void screen_write_start( - struct screen_write_ctx *, struct window_pane *, struct screen *); +void screen_write_start(struct screen_write_ctx *, struct window_pane *, + struct screen *); void screen_write_stop(struct screen_write_ctx *); void screen_write_reset(struct screen_write_ctx *); -size_t printflike(2, 3) screen_write_cstrlen(int, const char *, ...); -void printflike(5, 6) screen_write_cnputs(struct screen_write_ctx *, - ssize_t, struct grid_cell *, int, const char *, ...); -size_t printflike(2, 3) screen_write_strlen(int, const char *, ...); +size_t printflike(1, 2) screen_write_cstrlen(const char *, ...); +void printflike(4, 5) screen_write_cnputs(struct screen_write_ctx *, + ssize_t, struct grid_cell *, const char *, ...); +size_t printflike(1, 2) screen_write_strlen(const char *, ...); void printflike(3, 4) screen_write_puts(struct screen_write_ctx *, struct grid_cell *, const char *, ...); -void printflike(5, 6) screen_write_nputs(struct screen_write_ctx *, - ssize_t, struct grid_cell *, int, const char *, ...); -void screen_write_vnputs(struct screen_write_ctx *, - ssize_t, struct grid_cell *, int, const char *, va_list); -void screen_write_putc( - struct screen_write_ctx *, struct grid_cell *, u_char); -void screen_write_copy(struct screen_write_ctx *, - struct screen *, u_int, u_int, u_int, u_int); +void printflike(4, 5) screen_write_nputs(struct screen_write_ctx *, + ssize_t, struct grid_cell *, const char *, ...); +void screen_write_vnputs(struct screen_write_ctx *, ssize_t, + struct grid_cell *, const char *, va_list); +void screen_write_putc(struct screen_write_ctx *, struct grid_cell *, + u_char); +void screen_write_copy(struct screen_write_ctx *, struct screen *, u_int, + u_int, u_int, u_int); void screen_write_backspace(struct screen_write_ctx *); void screen_write_mode_set(struct screen_write_ctx *, int); void screen_write_mode_clear(struct screen_write_ctx *, int); @@ -2047,7 +2094,6 @@ void screen_write_cursormove(struct screen_write_ctx *, u_int, u_int); void screen_write_reverseindex(struct screen_write_ctx *); void screen_write_scrollregion(struct screen_write_ctx *, u_int, u_int); void screen_write_linefeed(struct screen_write_ctx *, int); -void screen_write_linefeedscreen(struct screen_write_ctx *, int); void screen_write_carriagereturn(struct screen_write_ctx *); void screen_write_clearendofscreen(struct screen_write_ctx *); void screen_write_clearstartofscreen(struct screen_write_ctx *); @@ -2079,6 +2125,8 @@ void screen_reflow(struct screen *, u_int); /* window.c */ extern struct windows windows; extern struct window_pane_tree all_window_panes; +int window_cmp(struct window *, struct window *); +RB_PROTOTYPE(windows, window, entry, window_cmp); int winlink_cmp(struct winlink *, struct winlink *); RB_PROTOTYPE(winlinks, winlink, entry, winlink_cmp); int window_pane_cmp(struct window_pane *, struct window_pane *); @@ -2099,17 +2147,20 @@ struct winlink *winlink_previous_by_number(struct winlink *, struct session *, int); void winlink_stack_push(struct winlink_stack *, struct winlink *); void winlink_stack_remove(struct winlink_stack *, struct winlink *); -int window_index(struct window *, u_int *); +struct window *window_find_by_id_str(const char *); struct window *window_find_by_id(u_int); +void window_update_activity(struct window *); struct window *window_create1(u_int, u_int); struct window *window_create(const char *, int, char **, const char *, - const char *, int, struct environ *, struct termios *, - u_int, u_int, u_int, char **); + const char *, const char *, struct environ *, + struct termios *, u_int, u_int, u_int, char **); void window_destroy(struct window *); struct window_pane *window_get_active_at(struct window *, u_int, u_int); -void window_set_active_at(struct window *, u_int, u_int); struct window_pane *window_find_string(struct window *, const char *); +int window_has_pane(struct window *, struct window_pane *); int window_set_active_pane(struct window *, struct window_pane *); +void window_redraw_active_switch(struct window *, + struct window_pane *); struct window_pane *window_add_pane(struct window *, u_int); void window_resize(struct window *, u_int, u_int); int window_zoom(struct window_pane *); @@ -2118,33 +2169,32 @@ void window_lost_pane(struct window *, struct window_pane *); void window_remove_pane(struct window *, struct window_pane *); struct window_pane *window_pane_at_index(struct window *, u_int); struct window_pane *window_pane_next_by_number(struct window *, - struct window_pane *, u_int); + struct window_pane *, u_int); struct window_pane *window_pane_previous_by_number(struct window *, - struct window_pane *, u_int); + struct window_pane *, u_int); int window_pane_index(struct window_pane *, u_int *); u_int window_count_panes(struct window *); void window_destroy_panes(struct window *); +struct window_pane *window_pane_find_by_id_str(const char *); struct window_pane *window_pane_find_by_id(u_int); struct window_pane *window_pane_create(struct window *, u_int, u_int, u_int); void window_pane_destroy(struct window_pane *); -void window_pane_timer_start(struct window_pane *); int window_pane_spawn(struct window_pane *, int, char **, - const char *, const char *, int, struct environ *, + const char *, const char *, const char *, struct environ *, struct termios *, char **); void window_pane_resize(struct window_pane *, u_int, u_int); void window_pane_alternate_on(struct window_pane *, struct grid_cell *, int); void window_pane_alternate_off(struct window_pane *, struct grid_cell *, int); -int window_pane_set_mode( - struct window_pane *, const struct window_mode *); +int window_pane_set_mode(struct window_pane *, + const struct window_mode *); void window_pane_reset_mode(struct window_pane *); -void window_pane_key(struct window_pane *, struct session *, int); -void window_pane_mouse(struct window_pane *, - struct session *, struct mouse_event *); +void window_pane_key(struct window_pane *, struct client *, + struct session *, key_code, struct mouse_event *); int window_pane_visible(struct window_pane *); -char *window_pane_search( - struct window_pane *, const char *, u_int *); +char *window_pane_search(struct window_pane *, const char *, + u_int *); char *window_printable_flags(struct session *, struct winlink *); struct window_pane *window_pane_find_up(struct window_pane *); struct window_pane *window_pane_find_down(struct window_pane *); @@ -2153,23 +2203,24 @@ struct window_pane *window_pane_find_right(struct window_pane *); void window_set_name(struct window *, const char *); void window_remove_ref(struct window *); void winlink_clear_flags(struct winlink *); +int winlink_shuffle_up(struct session *, struct winlink *); /* layout.c */ u_int layout_count_cells(struct layout_cell *); struct layout_cell *layout_create_cell(struct layout_cell *); void layout_free_cell(struct layout_cell *); void layout_print_cell(struct layout_cell *, const char *, u_int); -void layout_destroy_cell(struct layout_cell *, struct layout_cell **); -void layout_set_size( - struct layout_cell *, u_int, u_int, u_int, u_int); -void layout_make_leaf( - struct layout_cell *, struct window_pane *); +void layout_destroy_cell(struct layout_cell *, + struct layout_cell **); +void layout_set_size(struct layout_cell *, u_int, u_int, u_int, + u_int); +void layout_make_leaf(struct layout_cell *, struct window_pane *); void layout_make_node(struct layout_cell *, enum layout_type); void layout_fix_offsets(struct layout_cell *); void layout_fix_panes(struct window *, u_int, u_int); u_int layout_resize_check(struct layout_cell *, enum layout_type); -void layout_resize_adjust( - struct layout_cell *, enum layout_type, int); +void layout_resize_adjust(struct layout_cell *, enum layout_type, + int); void layout_init(struct window *, struct window_pane *); void layout_free(struct window *); void layout_resize(struct window *, u_int, u_int); @@ -2177,23 +2228,20 @@ void layout_resize_pane(struct window_pane *, enum layout_type, int); void layout_resize_pane_to(struct window_pane *, enum layout_type, u_int); -void layout_resize_pane_mouse(struct client *); void layout_assign_pane(struct layout_cell *, struct window_pane *); -struct layout_cell *layout_split_pane( - struct window_pane *, enum layout_type, int, int); +struct layout_cell *layout_split_pane(struct window_pane *, enum layout_type, + int, int); void layout_close_pane(struct window_pane *); /* layout-custom.c */ -char *layout_dump(struct window *); +char *layout_dump(struct layout_cell *); int layout_parse(struct window *, const char *); /* layout-set.c */ -const char *layout_set_name(u_int); int layout_set_lookup(const char *); u_int layout_set_select(struct window *, u_int); u_int layout_set_next(struct window *); u_int layout_set_previous(struct window *); -void layout_set_active_changed(struct window *); /* window-clock.c */ extern const struct window_mode window_clock_mode; @@ -2201,11 +2249,13 @@ extern const char window_clock_table[14][5][5]; /* window-copy.c */ extern const struct window_mode window_copy_mode; -void window_copy_init_from_pane(struct window_pane *); +void window_copy_init_from_pane(struct window_pane *, int); void window_copy_init_for_output(struct window_pane *); void printflike(2, 3) window_copy_add(struct window_pane *, const char *, ...); void window_copy_vadd(struct window_pane *, const char *, va_list); void window_copy_pageup(struct window_pane *); +void window_copy_start_drag(struct client *, struct mouse_event *); +int window_copy_scroll_position(struct window_pane *); /* window-choose.c */ extern const struct window_mode window_choose_mode; @@ -2228,13 +2278,13 @@ void window_choose_collapse_all(struct window_pane *); void window_choose_set_current(struct window_pane *, u_int); /* names.c */ -void queue_window_name(struct window *); +void check_window_name(struct window *); char *default_window_name(struct window *); char *format_window_name(struct window *); char *parse_window_name(const char *); /* signal.c */ -void set_signals(void(*)(int, short, void *)); +void set_signals(void(*)(int, short, void *), void *); void clear_signals(int); /* control.c */ @@ -2256,27 +2306,29 @@ void control_notify_session_close(struct session *); /* session.c */ extern struct sessions sessions; -extern struct sessions dead_sessions; extern struct session_groups session_groups; int session_cmp(struct session *, struct session *); RB_PROTOTYPE(sessions, session, entry, session_cmp); int session_alive(struct session *); struct session *session_find(const char *); +struct session *session_find_by_id_str(const char *); struct session *session_find_by_id(u_int); struct session *session_create(const char *, int, char **, const char *, - int, struct environ *, struct termios *, int, u_int, - u_int, char **); + const char *, struct environ *, struct termios *, int, + u_int, u_int, char **); void session_destroy(struct session *); +void session_unref(struct session *); int session_check_name(const char *); -void session_update_activity(struct session *); +void session_update_activity(struct session *, struct timeval *); struct session *session_next_session(struct session *); struct session *session_previous_session(struct session *); struct winlink *session_new(struct session *, const char *, int, char **, - const char *, int, int, char **); + const char *, const char *, int, char **); struct winlink *session_attach(struct session *, struct window *, int, char **); int session_detach(struct session *, struct winlink *); -struct winlink *session_has(struct session *, struct window *); +int session_has(struct session *, struct window *); +int session_is_linked(struct session *, struct window *); int session_next(struct session *, int); int session_previous(struct session *, int); int session_select(struct session *, int); @@ -2293,17 +2345,20 @@ void session_group_synchronize1(struct session *, struct session *); void session_renumber_windows(struct session *); /* utf8.c */ -void utf8_build(void); void utf8_set(struct utf8_data *, u_char); -int utf8_open(struct utf8_data *, u_char); -int utf8_append(struct utf8_data *, u_char); -u_int utf8_combine(const struct utf8_data *); -u_int utf8_split2(u_int, u_char *); +void utf8_copy(struct utf8_data *, const struct utf8_data *); +enum utf8_state utf8_open(struct utf8_data *, u_char); +enum utf8_state utf8_append(struct utf8_data *, u_char); +enum utf8_state utf8_combine(const struct utf8_data *, wchar_t *); +enum utf8_state utf8_split(wchar_t, struct utf8_data *); int utf8_strvis(char *, const char *, size_t, int); +char *utf8_sanitize(const char *); struct utf8_data *utf8_fromcstr(const char *); char *utf8_tocstr(struct utf8_data *); u_int utf8_cstrwidth(const char *); +char *utf8_rtrimcstr(const char *, u_int); char *utf8_trimcstr(const char *, u_int); +char *utf8_padcstr(const char *, u_int); /* osdep-*.c */ char *osdep_get_name(int, char *); @@ -2311,22 +2366,18 @@ char *osdep_get_cwd(int); struct event_base *osdep_event_init(void); /* log.c */ -void log_open(const char *); -void log_close(void); -void printflike(1, 2) log_debug(const char *, ...); -__dead void printflike(1, 2) log_fatal(const char *, ...); -__dead void printflike(1, 2) log_fatalx(const char *, ...); - -/* xmalloc.c */ -char *xstrdup(const char *); -void *xcalloc(size_t, size_t); -void *xmalloc(size_t); -void *xrealloc(void *, size_t); -void *xreallocarray(void *, size_t, size_t); -int printflike(2, 3) xasprintf(char **, const char *, ...); -int xvasprintf(char **, const char *, va_list); -int printflike(3, 4) xsnprintf(char *, size_t, const char *, ...); -int xvsnprintf(char *, size_t, const char *, va_list); +void log_add_level(void); +int log_get_level(void); +void log_open_fp(FILE *f); +void log_open(const char *); +void log_close(void); +#define LOG_ERROR 0 +#define LOG_INFO 1 +#define LOG_DEBUG 2 +#define log_debug(...) log_emit(LOG_DEBUG+1, __VA_ARGS__) +void printflike(2, 3) log_emit(int level, const char *, ...); +__dead void printflike(1, 2) fatal(const char *, ...); +__dead void printflike(1, 2) fatalx(const char *, ...); /* style.c */ int style_parse(const struct grid_cell *, @@ -2339,5 +2390,7 @@ void style_apply(struct grid_cell *, struct options *, const char *); void style_apply_update(struct grid_cell *, struct options *, const char *); +int style_equal(const struct grid_cell *, + const struct grid_cell *); #endif /* TMUX_H */ diff --git a/tools/check-compat.sh b/tools/check-compat.sh deleted file mode 100644 index b7603a80..00000000 --- a/tools/check-compat.sh +++ /dev/null @@ -1,5 +0,0 @@ -# $Id$ - -grep "#include" compat.h|while read line; do - grep "$line" *.[ch] compat/*.[ch] -done diff --git a/tools/cmp-cvs.sh b/tools/cmp-cvs.sh index 104ded6b..5429d769 100644 --- a/tools/cmp-cvs.sh +++ b/tools/cmp-cvs.sh @@ -1,4 +1,4 @@ -# $Id$ +#!/bin/ksh rm diff.out touch diff.out diff --git a/tools/fuzz.c b/tools/fuzz.c deleted file mode 100644 index 39a2a4db..00000000 --- a/tools/fuzz.c +++ /dev/null @@ -1,31 +0,0 @@ -#include - -#include -#include -#include -#include - -int -main(void) -{ - time_t t; - int i; - - setvbuf(stdout, NULL, _IONBF, 0); - - t = time(NULL); - srandom((u_int) t); - - for (;;) { - putchar('\033'); - - for (i = 0; i < random() % 25; i++) { - if (i > 22) - putchar(';'); - else - putchar(random() % 256); - } - - /* usleep(100); */ - } -} diff --git a/tools/putty-utf8.sh b/tools/putty-utf8.sh deleted file mode 100644 index 73cb5fb1..00000000 --- a/tools/putty-utf8.sh +++ /dev/null @@ -1 +0,0 @@ -echo -ne \\033%G\\033[?47h\\033%G\\033[?47l diff --git a/tty-acs.c b/tty-acs.c index 5d03c3eb..7fd265d4 100644 --- a/tty-acs.c +++ b/tty-acs.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2010 Nicholas Marriott + * Copyright (c) 2010 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above diff --git a/tty-keys.c b/tty-keys.c index a987c44b..105f99f7 100644 --- a/tty-keys.c +++ b/tty-keys.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -33,20 +33,19 @@ * into a ternary tree. */ -void tty_keys_add1(struct tty_key **, const char *, int); -void tty_keys_add(struct tty *, const char *, int); +void tty_keys_add1(struct tty_key **, const char *, key_code); +void tty_keys_add(struct tty *, const char *, key_code); void tty_keys_free1(struct tty_key *); -struct tty_key *tty_keys_find1( - struct tty_key *, const char *, size_t, size_t *); +struct tty_key *tty_keys_find1(struct tty_key *, const char *, size_t, + size_t *); struct tty_key *tty_keys_find(struct tty *, const char *, size_t, size_t *); void tty_keys_callback(int, short, void *); int tty_keys_mouse(struct tty *, const char *, size_t, size_t *); -int tty_keys_device(struct tty *, const char *, size_t, size_t *); /* Default raw keys. */ struct tty_default_key_raw { const char *string; - int key; + key_code key; }; const struct tty_default_key_raw tty_default_raw_keys[] = { /* @@ -166,7 +165,7 @@ const struct tty_default_key_raw tty_default_raw_keys[] = { /* Default terminfo(5) keys. */ struct tty_default_key_code { enum tty_code_code code; - int key; + key_code key; }; const struct tty_default_key_code tty_default_code_keys[] = { /* Function keys. */ @@ -318,7 +317,7 @@ const struct tty_default_key_code tty_default_code_keys[] = { /* Add key to tree. */ void -tty_keys_add(struct tty *tty, const char *s, int key) +tty_keys_add(struct tty *tty, const char *s, key_code key) { struct tty_key *tk; size_t size; @@ -326,17 +325,17 @@ tty_keys_add(struct tty *tty, const char *s, int key) keystr = key_string_lookup_key(key); if ((tk = tty_keys_find(tty, s, strlen(s), &size)) == NULL) { - log_debug("new key %s: 0x%x (%s)", s, key, keystr); + log_debug("new key %s: 0x%llx (%s)", s, key, keystr); tty_keys_add1(&tty->key_tree, s, key); } else { - log_debug("replacing key %s: 0x%x (%s)", s, key, keystr); + log_debug("replacing key %s: 0x%llx (%s)", s, key, keystr); tk->key = key; } } /* Add next node to the tree. */ void -tty_keys_add1(struct tty_key **tkp, const char *s, int key) +tty_keys_add1(struct tty_key **tkp, const char *s, key_code key) { struct tty_key *tk; @@ -345,7 +344,7 @@ tty_keys_add1(struct tty_key **tkp, const char *s, int key) if (tk == NULL) { tk = *tkp = xcalloc(1, sizeof *tk); tk->ch = *s; - tk->key = KEYC_NONE; + tk->key = KEYC_UNKNOWN; } /* Find the next entry. */ @@ -445,7 +444,7 @@ tty_keys_find1(struct tty_key *tk, const char *buf, size_t len, size_t *size) (*size)++; /* At the end of the string, return the current node. */ - if (len == 0 || (tk->next == NULL && tk->key != KEYC_NONE)) + if (len == 0 || (tk->next == NULL && tk->key != KEYC_UNKNOWN)) return (tk); /* Move into the next tree for the following character. */ @@ -465,34 +464,29 @@ tty_keys_find1(struct tty_key *tk, const char *buf, size_t len, size_t *size) * Process at least one key in the buffer and invoke tty->key_callback. Return * 0 if there are no further keys, or 1 if there could be more in the buffer. */ -int +key_code tty_keys_next(struct tty *tty) { - struct tty_key *tk; - struct timeval tv; - const char *buf; - size_t len, size; - cc_t bspace; - int key, delay, expired = 0; + struct tty_key *tk; + struct timeval tv; + const char *buf; + size_t len, size; + cc_t bspace; + int delay, expired = 0; + key_code key; + struct utf8_data ud; + enum utf8_state more; + u_int i; + wchar_t wc; /* Get key buffer. */ buf = EVBUFFER_DATA(tty->event->input); len = EVBUFFER_LENGTH(tty->event->input); + if (len == 0) return (0); log_debug("keys are %zu (%.*s)", len, (int) len, buf); - /* Is this device attributes response? */ - switch (tty_keys_device(tty, buf, len, &size)) { - case 0: /* yes */ - key = KEYC_NONE; - goto complete_key; - case -1: /* no, or not valid */ - break; - case 1: /* partial */ - goto partial_key; - } - /* Is this a mouse key press? */ switch (tty_keys_mouse(tty, buf, len, &size)) { case 0: /* yes */ @@ -501,6 +495,7 @@ tty_keys_next(struct tty *tty) case -1: /* no, or not valid */ break; case -2: /* yes, but we don't care. */ + key = KEYC_MOUSE; goto discard_key; case 1: /* partial */ goto partial_key; @@ -540,14 +535,35 @@ first_key: if (tk->next != NULL) goto partial_key; key = tk->key; - if (key != KEYC_NONE) + if (key != KEYC_UNKNOWN) key |= KEYC_ESCAPE; goto complete_key; } } + /* Is this valid UTF-8? */ + if ((more = utf8_open(&ud, (u_char)*buf) == UTF8_MORE)) { + size = ud.size; + if (len < size) { + if (expired) + goto discard_key; + goto partial_key; + } + for (i = 1; i < size; i++) + more = utf8_append(&ud, (u_char)buf[i]); + if (more != UTF8_DONE) + goto discard_key; + + if (utf8_combine(&ud, &wc) != UTF8_DONE) + goto discard_key; + key = wc; + + log_debug("UTF-8 key %.*s %#llx", (int)size, buf, key); + goto complete_key; + } + /* No key found, take first. */ - key = (u_char) *buf; + key = (u_char)*buf; size = 1; /* @@ -575,7 +591,7 @@ partial_key: } /* Get the time period. */ - delay = options_get_number(&global_options, "escape-time"); + delay = options_get_number(global_options, "escape-time"); tv.tv_sec = delay / 1000; tv.tv_usec = (delay % 1000) * 1000L; @@ -589,7 +605,7 @@ partial_key: return (0); complete_key: - log_debug("complete key %.*s %#x", (int) size, buf, key); + log_debug("complete key %.*s %#llx", (int)size, buf, key); /* Remove data from buffer. */ evbuffer_drain(tty->event->input, size); @@ -609,13 +625,13 @@ complete_key: } /* Fire the key. */ - if (key != KEYC_NONE) + if (key != KEYC_UNKNOWN) server_client_handle_key(tty->client, key); return (1); discard_key: - log_debug("discard key %.*s %#x", (int) size, buf, key); + log_debug("discard key %.*s %#llx", (int)size, buf, key); /* Remove data from buffer. */ evbuffer_drain(tty->event->input, size); @@ -625,7 +641,7 @@ discard_key: /* Key timer callback. */ void -tty_keys_callback(unused int fd, unused short events, void *data) +tty_keys_callback(__unused int fd, __unused short events, void *data) { struct tty *tty = data; @@ -643,9 +659,8 @@ int tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size) { struct mouse_event *m = &tty->mouse; - struct utf8_data utf8data; - u_int i, value, x, y, b, sgr, sgr_b, sgr_rel; - unsigned char c; + u_int i, x, y, b, sgr_b; + u_char sgr_type, c; /* * Standard mouse sequences are \033[M followed by three characters @@ -661,7 +676,8 @@ tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size) */ *size = 0; - x = y = b = sgr = sgr_b = sgr_rel = 0; + x = y = b = sgr_b = 0; + sgr_type = ' '; /* First two bytes are always \033[. */ if (buf[0] != '\033') @@ -674,8 +690,8 @@ tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size) return (1); /* - * Third byte is M in old standard and UTF-8 extension, < in SGR - * extension. + * Third byte is M in old standard (and UTF-8 extension which we do not + * support), < in SGR extension. */ if (buf[2] == 'M') { /* Read the three inputs. */ @@ -683,32 +699,15 @@ tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size) for (i = 0; i < 3; i++) { if (len <= *size) return (1); - - if (tty->mode & MODE_MOUSE_UTF8) { - if (utf8_open(&utf8data, buf[*size])) { - if (utf8data.size != 2) - return (-1); - (*size)++; - if (len <= *size) - return (1); - utf8_append(&utf8data, buf[*size]); - value = utf8_combine(&utf8data); - } else - value = (u_char) buf[*size]; - (*size)++; - } else { - value = (u_char) buf[*size]; - (*size)++; - } - + c = (u_char)buf[(*size)++]; if (i == 0) - b = value; + b = c; else if (i == 1) - x = value; + x = c; else - y = value; + y = c; } - log_debug("mouse input: %.*s", (int) *size, buf); + log_debug("mouse input: %.*s", (int)*size, buf); /* Check and return the mouse input. */ if (b < 32) @@ -748,22 +747,26 @@ tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size) while (1) { if (len <= *size) return (1); - c = (u_char) buf[(*size)++]; + c = (u_char)buf[(*size)++]; if (c == 'M' || c == 'm') break; if (c < '0' || c > '9') return (-1); y = 10 * y + (c - '0'); } - log_debug("mouse input (sgr): %.*s", (int) *size, buf); + log_debug("mouse input (SGR): %.*s", (int)*size, buf); /* Check and return the mouse input. */ if (x < 1 || y < 1) return (-1); x--; y--; - sgr = 1; - sgr_rel = (c == 'm'); + b = sgr_b; + + /* Type is M for press, m for release. */ + sgr_type = c; + if (sgr_type == 'm') + b |= 3; /* * Some terminals (like PuTTY 0.63) mistakenly send @@ -771,121 +774,20 @@ tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size) * Discard it before it reaches any program running inside * tmux. */ - if (sgr_rel && (sgr_b & 64)) + if (sgr_type == 'm' && (sgr_b & 64)) return (-2); - - /* Figure out what b would be in old format. */ - b = sgr_b; - if (sgr_rel) - b |= 3; } else return (-1); - /* Fill in mouse structure. */ - if (~m->event & MOUSE_EVENT_WHEEL) { - m->lx = m->x; - m->ly = m->y; - } - m->xb = b; - m->sgr = sgr; - m->sgr_xb = sgr_b; - m->sgr_rel = sgr_rel; + /* Fill mouse event. */ + m->lx = m->x; m->x = x; + m->ly = m->y; m->y = y; - if (b & MOUSE_MASK_WHEEL) { - if (b & MOUSE_MASK_SHIFT) - m->scroll = 1; - else - m->scroll = MOUSE_WHEEL_SCALE; - if (b & MOUSE_MASK_META) - m->scroll *= MOUSE_WHEEL_SCALE; - if (b & MOUSE_MASK_CTRL) - m->scroll *= MOUSE_WHEEL_SCALE; - - b &= MOUSE_MASK_BUTTONS; - if (b == 0) - m->wheel = MOUSE_WHEEL_UP; - else if (b == 1) - m->wheel = MOUSE_WHEEL_DOWN; - m->event = MOUSE_EVENT_WHEEL; - - m->button = 3; - } else if ((b & MOUSE_MASK_BUTTONS) == 3) { - if (~m->event & MOUSE_EVENT_DRAG && x == m->sx && y == m->sy) { - m->event = MOUSE_EVENT_CLICK; - m->clicks = (m->clicks + 1) % 3; - } else - m->event = MOUSE_EVENT_DRAG; - m->event |= MOUSE_EVENT_UP; - } else { - if (b & MOUSE_MASK_DRAG) - m->event = MOUSE_EVENT_DRAG; - else { - m->event = MOUSE_EVENT_DOWN; - if (x != m->sx || y != m->sy) - m->clicks = 0; - } - m->button = (b & MOUSE_MASK_BUTTONS); - } - m->sx = x; - m->sy = y; - - return (0); -} - -/* - * Handle device attributes input. Returns 0 for success, -1 for failure, 1 for - * partial. - */ -int -tty_keys_device(struct tty *tty, const char *buf, size_t len, size_t *size) -{ - u_int i, class; - char tmp[64], *endptr; - - /* - * Primary device attributes are \033[?a;b and secondary are - * \033[>a;b;c. - */ - - *size = 0; - - /* First three bytes are always \033[?. */ - if (buf[0] != '\033') - return (-1); - if (len == 1) - return (1); - if (buf[1] != '[') - return (-1); - if (len == 2) - return (1); - if (buf[2] != '>' && buf[2] != '?') - return (-1); - if (len == 3) - return (1); - - /* Copy the rest up to a 'c'. */ - for (i = 0; i < (sizeof tmp) - 1 && buf[3 + i] != 'c'; i++) { - if (3 + i == len) - return (1); - tmp[i] = buf[3 + i]; - } - if (i == (sizeof tmp) - 1) - return (-1); - tmp[i] = '\0'; - *size = 4 + i; - - /* Only primary is of interest. */ - if (buf[2] != '?') - return (0); - - /* Convert service class. */ - class = strtoul(tmp, &endptr, 10); - if (*endptr != ';') - class = 0; - - log_debug("received service class %u", class); - tty_set_class(tty, class); + m->lb = m->b; + m->b = b; + m->sgr_type = sgr_type; + m->sgr_b = sgr_b; return (0); } diff --git a/tty-term.c b/tty-term.c index 365da5fb..a3a26369 100644 --- a/tty-term.c +++ b/tty-term.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -18,9 +18,9 @@ #include -#ifdef HAVE_CURSES_H +#if defined(HAVE_CURSES_H) #include -#else +#elif defined(HAVE_NCURSES_H) #include #endif #include @@ -35,209 +35,238 @@ char *tty_term_strip(const char *); struct tty_terms tty_terms = LIST_HEAD_INITIALIZER(tty_terms); -const struct tty_term_code_entry tty_term_codes[NTTYCODE] = { - { TTYC_ACSC, TTYCODE_STRING, "acsc" }, - { TTYC_AX, TTYCODE_FLAG, "AX" }, - { TTYC_BEL, TTYCODE_STRING, "bel" }, - { TTYC_BLINK, TTYCODE_STRING, "blink" }, - { TTYC_BOLD, TTYCODE_STRING, "bold" }, - { TTYC_CIVIS, TTYCODE_STRING, "civis" }, - { TTYC_CLEAR, TTYCODE_STRING, "clear" }, - { TTYC_CNORM, TTYCODE_STRING, "cnorm" }, - { TTYC_COLORS, TTYCODE_NUMBER, "colors" }, - { TTYC_CR, TTYCODE_STRING, "Cr" }, - { TTYC_CS, TTYCODE_STRING, "Cs" }, - { TTYC_CSR, TTYCODE_STRING, "csr" }, - { TTYC_CUB, TTYCODE_STRING, "cub" }, - { TTYC_CUB1, TTYCODE_STRING, "cub1" }, - { TTYC_CUD, TTYCODE_STRING, "cud" }, - { TTYC_CUD1, TTYCODE_STRING, "cud1" }, - { TTYC_CUF, TTYCODE_STRING, "cuf" }, - { TTYC_CUF1, TTYCODE_STRING, "cuf1" }, - { TTYC_CUP, TTYCODE_STRING, "cup" }, - { TTYC_CUU, TTYCODE_STRING, "cuu" }, - { TTYC_CUU1, TTYCODE_STRING, "cuu1" }, - { TTYC_CVVIS, TTYCODE_STRING, "cvvis" }, - { TTYC_DCH, TTYCODE_STRING, "dch" }, - { TTYC_DCH1, TTYCODE_STRING, "dch1" }, - { TTYC_DIM, TTYCODE_STRING, "dim" }, - { TTYC_DL, TTYCODE_STRING, "dl" }, - { TTYC_DL1, TTYCODE_STRING, "dl1" }, - { TTYC_E3, TTYCODE_STRING, "E3" }, - { TTYC_ECH, TTYCODE_STRING, "ech" }, - { TTYC_EL, TTYCODE_STRING, "el" }, - { TTYC_EL1, TTYCODE_STRING, "el1" }, - { TTYC_ENACS, TTYCODE_STRING, "enacs" }, - { TTYC_FSL, TTYCODE_STRING, "fsl" }, - { TTYC_HOME, TTYCODE_STRING, "home" }, - { TTYC_HPA, TTYCODE_STRING, "hpa" }, - { TTYC_ICH, TTYCODE_STRING, "ich" }, - { TTYC_ICH1, TTYCODE_STRING, "ich1" }, - { TTYC_IL, TTYCODE_STRING, "il" }, - { TTYC_IL1, TTYCODE_STRING, "il1" }, - { TTYC_INVIS, TTYCODE_STRING, "invis" }, - { TTYC_IS1, TTYCODE_STRING, "is1" }, - { TTYC_IS2, TTYCODE_STRING, "is2" }, - { TTYC_IS3, TTYCODE_STRING, "is3" }, - { TTYC_KCBT, TTYCODE_STRING, "kcbt" }, - { TTYC_KCUB1, TTYCODE_STRING, "kcub1" }, - { TTYC_KCUD1, TTYCODE_STRING, "kcud1" }, - { TTYC_KCUF1, TTYCODE_STRING, "kcuf1" }, - { TTYC_KCUU1, TTYCODE_STRING, "kcuu1" }, - { TTYC_KDC2, TTYCODE_STRING, "kDC" }, - { TTYC_KDC3, TTYCODE_STRING, "kDC3" }, - { TTYC_KDC4, TTYCODE_STRING, "kDC4" }, - { TTYC_KDC5, TTYCODE_STRING, "kDC5" }, - { TTYC_KDC6, TTYCODE_STRING, "kDC6" }, - { TTYC_KDC7, TTYCODE_STRING, "kDC7" }, - { TTYC_KDCH1, TTYCODE_STRING, "kdch1" }, - { TTYC_KDN2, TTYCODE_STRING, "kDN" }, - { TTYC_KDN3, TTYCODE_STRING, "kDN3" }, - { TTYC_KDN4, TTYCODE_STRING, "kDN4" }, - { TTYC_KDN5, TTYCODE_STRING, "kDN5" }, - { TTYC_KDN6, TTYCODE_STRING, "kDN6" }, - { TTYC_KDN7, TTYCODE_STRING, "kDN7" }, - { TTYC_KEND, TTYCODE_STRING, "kend" }, - { TTYC_KEND2, TTYCODE_STRING, "kEND" }, - { TTYC_KEND3, TTYCODE_STRING, "kEND3" }, - { TTYC_KEND4, TTYCODE_STRING, "kEND4" }, - { TTYC_KEND5, TTYCODE_STRING, "kEND5" }, - { TTYC_KEND6, TTYCODE_STRING, "kEND6" }, - { TTYC_KEND7, TTYCODE_STRING, "kEND7" }, - { TTYC_KF1, TTYCODE_STRING, "kf1" }, - { TTYC_KF10, TTYCODE_STRING, "kf10" }, - { TTYC_KF11, TTYCODE_STRING, "kf11" }, - { TTYC_KF12, TTYCODE_STRING, "kf12" }, - { TTYC_KF13, TTYCODE_STRING, "kf13" }, - { TTYC_KF14, TTYCODE_STRING, "kf14" }, - { TTYC_KF15, TTYCODE_STRING, "kf15" }, - { TTYC_KF16, TTYCODE_STRING, "kf16" }, - { TTYC_KF17, TTYCODE_STRING, "kf17" }, - { TTYC_KF18, TTYCODE_STRING, "kf18" }, - { TTYC_KF19, TTYCODE_STRING, "kf19" }, - { TTYC_KF2, TTYCODE_STRING, "kf2" }, - { TTYC_KF20, TTYCODE_STRING, "kf20" }, - { TTYC_KF21, TTYCODE_STRING, "kf21" }, - { TTYC_KF22, TTYCODE_STRING, "kf22" }, - { TTYC_KF23, TTYCODE_STRING, "kf23" }, - { TTYC_KF24, TTYCODE_STRING, "kf24" }, - { TTYC_KF25, TTYCODE_STRING, "kf25" }, - { TTYC_KF26, TTYCODE_STRING, "kf26" }, - { TTYC_KF27, TTYCODE_STRING, "kf27" }, - { TTYC_KF28, TTYCODE_STRING, "kf28" }, - { TTYC_KF29, TTYCODE_STRING, "kf29" }, - { TTYC_KF3, TTYCODE_STRING, "kf3" }, - { TTYC_KF30, TTYCODE_STRING, "kf30" }, - { TTYC_KF31, TTYCODE_STRING, "kf31" }, - { TTYC_KF32, TTYCODE_STRING, "kf32" }, - { TTYC_KF33, TTYCODE_STRING, "kf33" }, - { TTYC_KF34, TTYCODE_STRING, "kf34" }, - { TTYC_KF35, TTYCODE_STRING, "kf35" }, - { TTYC_KF36, TTYCODE_STRING, "kf36" }, - { TTYC_KF37, TTYCODE_STRING, "kf37" }, - { TTYC_KF38, TTYCODE_STRING, "kf38" }, - { TTYC_KF39, TTYCODE_STRING, "kf39" }, - { TTYC_KF4, TTYCODE_STRING, "kf4" }, - { TTYC_KF40, TTYCODE_STRING, "kf40" }, - { TTYC_KF41, TTYCODE_STRING, "kf41" }, - { TTYC_KF42, TTYCODE_STRING, "kf42" }, - { TTYC_KF43, TTYCODE_STRING, "kf43" }, - { TTYC_KF44, TTYCODE_STRING, "kf44" }, - { TTYC_KF45, TTYCODE_STRING, "kf45" }, - { TTYC_KF46, TTYCODE_STRING, "kf46" }, - { TTYC_KF47, TTYCODE_STRING, "kf47" }, - { TTYC_KF48, TTYCODE_STRING, "kf48" }, - { TTYC_KF49, TTYCODE_STRING, "kf49" }, - { TTYC_KF5, TTYCODE_STRING, "kf5" }, - { TTYC_KF50, TTYCODE_STRING, "kf50" }, - { TTYC_KF51, TTYCODE_STRING, "kf51" }, - { TTYC_KF52, TTYCODE_STRING, "kf52" }, - { TTYC_KF53, TTYCODE_STRING, "kf53" }, - { TTYC_KF54, TTYCODE_STRING, "kf54" }, - { TTYC_KF55, TTYCODE_STRING, "kf55" }, - { TTYC_KF56, TTYCODE_STRING, "kf56" }, - { TTYC_KF57, TTYCODE_STRING, "kf57" }, - { TTYC_KF58, TTYCODE_STRING, "kf58" }, - { TTYC_KF59, TTYCODE_STRING, "kf59" }, - { TTYC_KF6, TTYCODE_STRING, "kf6" }, - { TTYC_KF60, TTYCODE_STRING, "kf60" }, - { TTYC_KF61, TTYCODE_STRING, "kf61" }, - { TTYC_KF62, TTYCODE_STRING, "kf62" }, - { TTYC_KF63, TTYCODE_STRING, "kf63" }, - { TTYC_KF7, TTYCODE_STRING, "kf7" }, - { TTYC_KF8, TTYCODE_STRING, "kf8" }, - { TTYC_KF9, TTYCODE_STRING, "kf9" }, - { TTYC_KHOM2, TTYCODE_STRING, "kHOM" }, - { TTYC_KHOM3, TTYCODE_STRING, "kHOM3" }, - { TTYC_KHOM4, TTYCODE_STRING, "kHOM4" }, - { TTYC_KHOM5, TTYCODE_STRING, "kHOM5" }, - { TTYC_KHOM6, TTYCODE_STRING, "kHOM6" }, - { TTYC_KHOM7, TTYCODE_STRING, "kHOM7" }, - { TTYC_KHOME, TTYCODE_STRING, "khome" }, - { TTYC_KIC2, TTYCODE_STRING, "kIC" }, - { TTYC_KIC3, TTYCODE_STRING, "kIC3" }, - { TTYC_KIC4, TTYCODE_STRING, "kIC4" }, - { TTYC_KIC5, TTYCODE_STRING, "kIC5" }, - { TTYC_KIC6, TTYCODE_STRING, "kIC6" }, - { TTYC_KIC7, TTYCODE_STRING, "kIC7" }, - { TTYC_KICH1, TTYCODE_STRING, "kich1" }, - { TTYC_KLFT2, TTYCODE_STRING, "kLFT" }, - { TTYC_KLFT3, TTYCODE_STRING, "kLFT3" }, - { TTYC_KLFT4, TTYCODE_STRING, "kLFT4" }, - { TTYC_KLFT5, TTYCODE_STRING, "kLFT5" }, - { TTYC_KLFT6, TTYCODE_STRING, "kLFT6" }, - { TTYC_KLFT7, TTYCODE_STRING, "kLFT7" }, - { TTYC_KMOUS, TTYCODE_STRING, "kmous" }, - { TTYC_KNP, TTYCODE_STRING, "knp" }, - { TTYC_KNXT2, TTYCODE_STRING, "kNXT" }, - { TTYC_KNXT3, TTYCODE_STRING, "kNXT3" }, - { TTYC_KNXT4, TTYCODE_STRING, "kNXT4" }, - { TTYC_KNXT5, TTYCODE_STRING, "kNXT5" }, - { TTYC_KNXT6, TTYCODE_STRING, "kNXT6" }, - { TTYC_KNXT7, TTYCODE_STRING, "kNXT7" }, - { TTYC_KPP, TTYCODE_STRING, "kpp" }, - { TTYC_KPRV2, TTYCODE_STRING, "kPRV" }, - { TTYC_KPRV3, TTYCODE_STRING, "kPRV3" }, - { TTYC_KPRV4, TTYCODE_STRING, "kPRV4" }, - { TTYC_KPRV5, TTYCODE_STRING, "kPRV5" }, - { TTYC_KPRV6, TTYCODE_STRING, "kPRV6" }, - { TTYC_KPRV7, TTYCODE_STRING, "kPRV7" }, - { TTYC_KRIT2, TTYCODE_STRING, "kRIT" }, - { TTYC_KRIT3, TTYCODE_STRING, "kRIT3" }, - { TTYC_KRIT4, TTYCODE_STRING, "kRIT4" }, - { TTYC_KRIT5, TTYCODE_STRING, "kRIT5" }, - { TTYC_KRIT6, TTYCODE_STRING, "kRIT6" }, - { TTYC_KRIT7, TTYCODE_STRING, "kRIT7" }, - { TTYC_KUP2, TTYCODE_STRING, "kUP" }, - { TTYC_KUP3, TTYCODE_STRING, "kUP3" }, - { TTYC_KUP4, TTYCODE_STRING, "kUP4" }, - { TTYC_KUP5, TTYCODE_STRING, "kUP5" }, - { TTYC_KUP6, TTYCODE_STRING, "kUP6" }, - { TTYC_KUP7, TTYCODE_STRING, "kUP7" }, - { TTYC_MS, TTYCODE_STRING, "Ms" }, - { TTYC_OP, TTYCODE_STRING, "op" }, - { TTYC_REV, TTYCODE_STRING, "rev" }, - { TTYC_RI, TTYCODE_STRING, "ri" }, - { TTYC_RMACS, TTYCODE_STRING, "rmacs" }, - { TTYC_RMCUP, TTYCODE_STRING, "rmcup" }, - { TTYC_RMKX, TTYCODE_STRING, "rmkx" }, - { TTYC_SE, TTYCODE_STRING, "Se" }, - { TTYC_SETAB, TTYCODE_STRING, "setab" }, - { TTYC_SETAF, TTYCODE_STRING, "setaf" }, - { TTYC_SGR0, TTYCODE_STRING, "sgr0" }, - { TTYC_SITM, TTYCODE_STRING, "sitm" }, - { TTYC_SMACS, TTYCODE_STRING, "smacs" }, - { TTYC_SMCUP, TTYCODE_STRING, "smcup" }, - { TTYC_SMKX, TTYCODE_STRING, "smkx" }, - { TTYC_SMSO, TTYCODE_STRING, "smso" }, - { TTYC_SMUL, TTYCODE_STRING, "smul" }, - { TTYC_SS, TTYCODE_STRING, "Ss" }, - { TTYC_TSL, TTYCODE_STRING, "tsl" }, - { TTYC_VPA, TTYCODE_STRING, "vpa" }, - { TTYC_XENL, TTYCODE_FLAG, "xenl" }, - { TTYC_XT, TTYCODE_FLAG, "XT" }, +enum tty_code_type { + TTYCODE_NONE = 0, + TTYCODE_STRING, + TTYCODE_NUMBER, + TTYCODE_FLAG, }; +struct tty_code { + enum tty_code_type type; + union { + char *string; + int number; + int flag; + } value; +}; + +struct tty_term_code_entry { + enum tty_code_type type; + const char *name; +}; + +const struct tty_term_code_entry tty_term_codes[] = { + [TTYC_ACSC] = { TTYCODE_STRING, "acsc" }, + [TTYC_AX] = { TTYCODE_FLAG, "AX" }, + [TTYC_BCE] = { TTYCODE_FLAG, "bce" }, + [TTYC_BEL] = { TTYCODE_STRING, "bel" }, + [TTYC_BLINK] = { TTYCODE_STRING, "blink" }, + [TTYC_BOLD] = { TTYCODE_STRING, "bold" }, + [TTYC_CIVIS] = { TTYCODE_STRING, "civis" }, + [TTYC_CLEAR] = { TTYCODE_STRING, "clear" }, + [TTYC_CNORM] = { TTYCODE_STRING, "cnorm" }, + [TTYC_COLORS] = { TTYCODE_NUMBER, "colors" }, + [TTYC_CR] = { TTYCODE_STRING, "Cr" }, + [TTYC_CS] = { TTYCODE_STRING, "Cs" }, + [TTYC_CSR] = { TTYCODE_STRING, "csr" }, + [TTYC_CUB] = { TTYCODE_STRING, "cub" }, + [TTYC_CUB1] = { TTYCODE_STRING, "cub1" }, + [TTYC_CUD] = { TTYCODE_STRING, "cud" }, + [TTYC_CUD1] = { TTYCODE_STRING, "cud1" }, + [TTYC_CUF] = { TTYCODE_STRING, "cuf" }, + [TTYC_CUF1] = { TTYCODE_STRING, "cuf1" }, + [TTYC_CUP] = { TTYCODE_STRING, "cup" }, + [TTYC_CUU] = { TTYCODE_STRING, "cuu" }, + [TTYC_CUU1] = { TTYCODE_STRING, "cuu1" }, + [TTYC_CVVIS] = { TTYCODE_STRING, "cvvis" }, + [TTYC_DCH] = { TTYCODE_STRING, "dch" }, + [TTYC_DCH1] = { TTYCODE_STRING, "dch1" }, + [TTYC_DIM] = { TTYCODE_STRING, "dim" }, + [TTYC_DL] = { TTYCODE_STRING, "dl" }, + [TTYC_DL1] = { TTYCODE_STRING, "dl1" }, + [TTYC_E3] = { TTYCODE_STRING, "E3" }, + [TTYC_ECH] = { TTYCODE_STRING, "ech" }, + [TTYC_EL] = { TTYCODE_STRING, "el" }, + [TTYC_EL1] = { TTYCODE_STRING, "el1" }, + [TTYC_ENACS] = { TTYCODE_STRING, "enacs" }, + [TTYC_FSL] = { TTYCODE_STRING, "fsl" }, + [TTYC_HOME] = { TTYCODE_STRING, "home" }, + [TTYC_HPA] = { TTYCODE_STRING, "hpa" }, + [TTYC_ICH] = { TTYCODE_STRING, "ich" }, + [TTYC_ICH1] = { TTYCODE_STRING, "ich1" }, + [TTYC_IL] = { TTYCODE_STRING, "il" }, + [TTYC_IL1] = { TTYCODE_STRING, "il1" }, + [TTYC_INVIS] = { TTYCODE_STRING, "invis" }, + [TTYC_IS1] = { TTYCODE_STRING, "is1" }, + [TTYC_IS2] = { TTYCODE_STRING, "is2" }, + [TTYC_IS3] = { TTYCODE_STRING, "is3" }, + [TTYC_KCBT] = { TTYCODE_STRING, "kcbt" }, + [TTYC_KCUB1] = { TTYCODE_STRING, "kcub1" }, + [TTYC_KCUD1] = { TTYCODE_STRING, "kcud1" }, + [TTYC_KCUF1] = { TTYCODE_STRING, "kcuf1" }, + [TTYC_KCUU1] = { TTYCODE_STRING, "kcuu1" }, + [TTYC_KDC2] = { TTYCODE_STRING, "kDC" }, + [TTYC_KDC3] = { TTYCODE_STRING, "kDC3" }, + [TTYC_KDC4] = { TTYCODE_STRING, "kDC4" }, + [TTYC_KDC5] = { TTYCODE_STRING, "kDC5" }, + [TTYC_KDC6] = { TTYCODE_STRING, "kDC6" }, + [TTYC_KDC7] = { TTYCODE_STRING, "kDC7" }, + [TTYC_KDCH1] = { TTYCODE_STRING, "kdch1" }, + [TTYC_KDN2] = { TTYCODE_STRING, "kDN" }, + [TTYC_KDN3] = { TTYCODE_STRING, "kDN3" }, + [TTYC_KDN4] = { TTYCODE_STRING, "kDN4" }, + [TTYC_KDN5] = { TTYCODE_STRING, "kDN5" }, + [TTYC_KDN6] = { TTYCODE_STRING, "kDN6" }, + [TTYC_KDN7] = { TTYCODE_STRING, "kDN7" }, + [TTYC_KEND] = { TTYCODE_STRING, "kend" }, + [TTYC_KEND2] = { TTYCODE_STRING, "kEND" }, + [TTYC_KEND3] = { TTYCODE_STRING, "kEND3" }, + [TTYC_KEND4] = { TTYCODE_STRING, "kEND4" }, + [TTYC_KEND5] = { TTYCODE_STRING, "kEND5" }, + [TTYC_KEND6] = { TTYCODE_STRING, "kEND6" }, + [TTYC_KEND7] = { TTYCODE_STRING, "kEND7" }, + [TTYC_KF1] = { TTYCODE_STRING, "kf1" }, + [TTYC_KF10] = { TTYCODE_STRING, "kf10" }, + [TTYC_KF11] = { TTYCODE_STRING, "kf11" }, + [TTYC_KF12] = { TTYCODE_STRING, "kf12" }, + [TTYC_KF13] = { TTYCODE_STRING, "kf13" }, + [TTYC_KF14] = { TTYCODE_STRING, "kf14" }, + [TTYC_KF15] = { TTYCODE_STRING, "kf15" }, + [TTYC_KF16] = { TTYCODE_STRING, "kf16" }, + [TTYC_KF17] = { TTYCODE_STRING, "kf17" }, + [TTYC_KF18] = { TTYCODE_STRING, "kf18" }, + [TTYC_KF19] = { TTYCODE_STRING, "kf19" }, + [TTYC_KF2] = { TTYCODE_STRING, "kf2" }, + [TTYC_KF20] = { TTYCODE_STRING, "kf20" }, + [TTYC_KF21] = { TTYCODE_STRING, "kf21" }, + [TTYC_KF22] = { TTYCODE_STRING, "kf22" }, + [TTYC_KF23] = { TTYCODE_STRING, "kf23" }, + [TTYC_KF24] = { TTYCODE_STRING, "kf24" }, + [TTYC_KF25] = { TTYCODE_STRING, "kf25" }, + [TTYC_KF26] = { TTYCODE_STRING, "kf26" }, + [TTYC_KF27] = { TTYCODE_STRING, "kf27" }, + [TTYC_KF28] = { TTYCODE_STRING, "kf28" }, + [TTYC_KF29] = { TTYCODE_STRING, "kf29" }, + [TTYC_KF3] = { TTYCODE_STRING, "kf3" }, + [TTYC_KF30] = { TTYCODE_STRING, "kf30" }, + [TTYC_KF31] = { TTYCODE_STRING, "kf31" }, + [TTYC_KF32] = { TTYCODE_STRING, "kf32" }, + [TTYC_KF33] = { TTYCODE_STRING, "kf33" }, + [TTYC_KF34] = { TTYCODE_STRING, "kf34" }, + [TTYC_KF35] = { TTYCODE_STRING, "kf35" }, + [TTYC_KF36] = { TTYCODE_STRING, "kf36" }, + [TTYC_KF37] = { TTYCODE_STRING, "kf37" }, + [TTYC_KF38] = { TTYCODE_STRING, "kf38" }, + [TTYC_KF39] = { TTYCODE_STRING, "kf39" }, + [TTYC_KF4] = { TTYCODE_STRING, "kf4" }, + [TTYC_KF40] = { TTYCODE_STRING, "kf40" }, + [TTYC_KF41] = { TTYCODE_STRING, "kf41" }, + [TTYC_KF42] = { TTYCODE_STRING, "kf42" }, + [TTYC_KF43] = { TTYCODE_STRING, "kf43" }, + [TTYC_KF44] = { TTYCODE_STRING, "kf44" }, + [TTYC_KF45] = { TTYCODE_STRING, "kf45" }, + [TTYC_KF46] = { TTYCODE_STRING, "kf46" }, + [TTYC_KF47] = { TTYCODE_STRING, "kf47" }, + [TTYC_KF48] = { TTYCODE_STRING, "kf48" }, + [TTYC_KF49] = { TTYCODE_STRING, "kf49" }, + [TTYC_KF5] = { TTYCODE_STRING, "kf5" }, + [TTYC_KF50] = { TTYCODE_STRING, "kf50" }, + [TTYC_KF51] = { TTYCODE_STRING, "kf51" }, + [TTYC_KF52] = { TTYCODE_STRING, "kf52" }, + [TTYC_KF53] = { TTYCODE_STRING, "kf53" }, + [TTYC_KF54] = { TTYCODE_STRING, "kf54" }, + [TTYC_KF55] = { TTYCODE_STRING, "kf55" }, + [TTYC_KF56] = { TTYCODE_STRING, "kf56" }, + [TTYC_KF57] = { TTYCODE_STRING, "kf57" }, + [TTYC_KF58] = { TTYCODE_STRING, "kf58" }, + [TTYC_KF59] = { TTYCODE_STRING, "kf59" }, + [TTYC_KF6] = { TTYCODE_STRING, "kf6" }, + [TTYC_KF60] = { TTYCODE_STRING, "kf60" }, + [TTYC_KF61] = { TTYCODE_STRING, "kf61" }, + [TTYC_KF62] = { TTYCODE_STRING, "kf62" }, + [TTYC_KF63] = { TTYCODE_STRING, "kf63" }, + [TTYC_KF7] = { TTYCODE_STRING, "kf7" }, + [TTYC_KF8] = { TTYCODE_STRING, "kf8" }, + [TTYC_KF9] = { TTYCODE_STRING, "kf9" }, + [TTYC_KHOM2] = { TTYCODE_STRING, "kHOM" }, + [TTYC_KHOM3] = { TTYCODE_STRING, "kHOM3" }, + [TTYC_KHOM4] = { TTYCODE_STRING, "kHOM4" }, + [TTYC_KHOM5] = { TTYCODE_STRING, "kHOM5" }, + [TTYC_KHOM6] = { TTYCODE_STRING, "kHOM6" }, + [TTYC_KHOM7] = { TTYCODE_STRING, "kHOM7" }, + [TTYC_KHOME] = { TTYCODE_STRING, "khome" }, + [TTYC_KIC2] = { TTYCODE_STRING, "kIC" }, + [TTYC_KIC3] = { TTYCODE_STRING, "kIC3" }, + [TTYC_KIC4] = { TTYCODE_STRING, "kIC4" }, + [TTYC_KIC5] = { TTYCODE_STRING, "kIC5" }, + [TTYC_KIC6] = { TTYCODE_STRING, "kIC6" }, + [TTYC_KIC7] = { TTYCODE_STRING, "kIC7" }, + [TTYC_KICH1] = { TTYCODE_STRING, "kich1" }, + [TTYC_KLFT2] = { TTYCODE_STRING, "kLFT" }, + [TTYC_KLFT3] = { TTYCODE_STRING, "kLFT3" }, + [TTYC_KLFT4] = { TTYCODE_STRING, "kLFT4" }, + [TTYC_KLFT5] = { TTYCODE_STRING, "kLFT5" }, + [TTYC_KLFT6] = { TTYCODE_STRING, "kLFT6" }, + [TTYC_KLFT7] = { TTYCODE_STRING, "kLFT7" }, + [TTYC_KMOUS] = { TTYCODE_STRING, "kmous" }, + [TTYC_KNP] = { TTYCODE_STRING, "knp" }, + [TTYC_KNXT2] = { TTYCODE_STRING, "kNXT" }, + [TTYC_KNXT3] = { TTYCODE_STRING, "kNXT3" }, + [TTYC_KNXT4] = { TTYCODE_STRING, "kNXT4" }, + [TTYC_KNXT5] = { TTYCODE_STRING, "kNXT5" }, + [TTYC_KNXT6] = { TTYCODE_STRING, "kNXT6" }, + [TTYC_KNXT7] = { TTYCODE_STRING, "kNXT7" }, + [TTYC_KPP] = { TTYCODE_STRING, "kpp" }, + [TTYC_KPRV2] = { TTYCODE_STRING, "kPRV" }, + [TTYC_KPRV3] = { TTYCODE_STRING, "kPRV3" }, + [TTYC_KPRV4] = { TTYCODE_STRING, "kPRV4" }, + [TTYC_KPRV5] = { TTYCODE_STRING, "kPRV5" }, + [TTYC_KPRV6] = { TTYCODE_STRING, "kPRV6" }, + [TTYC_KPRV7] = { TTYCODE_STRING, "kPRV7" }, + [TTYC_KRIT2] = { TTYCODE_STRING, "kRIT" }, + [TTYC_KRIT3] = { TTYCODE_STRING, "kRIT3" }, + [TTYC_KRIT4] = { TTYCODE_STRING, "kRIT4" }, + [TTYC_KRIT5] = { TTYCODE_STRING, "kRIT5" }, + [TTYC_KRIT6] = { TTYCODE_STRING, "kRIT6" }, + [TTYC_KRIT7] = { TTYCODE_STRING, "kRIT7" }, + [TTYC_KUP2] = { TTYCODE_STRING, "kUP" }, + [TTYC_KUP3] = { TTYCODE_STRING, "kUP3" }, + [TTYC_KUP4] = { TTYCODE_STRING, "kUP4" }, + [TTYC_KUP5] = { TTYCODE_STRING, "kUP5" }, + [TTYC_KUP6] = { TTYCODE_STRING, "kUP6" }, + [TTYC_KUP7] = { TTYCODE_STRING, "kUP7" }, + [TTYC_MS] = { TTYCODE_STRING, "Ms" }, + [TTYC_OP] = { TTYCODE_STRING, "op" }, + [TTYC_REV] = { TTYCODE_STRING, "rev" }, + [TTYC_RI] = { TTYCODE_STRING, "ri" }, + [TTYC_RMACS] = { TTYCODE_STRING, "rmacs" }, + [TTYC_RMCUP] = { TTYCODE_STRING, "rmcup" }, + [TTYC_RMKX] = { TTYCODE_STRING, "rmkx" }, + [TTYC_SE] = { TTYCODE_STRING, "Se" }, + [TTYC_SETAB] = { TTYCODE_STRING, "setab" }, + [TTYC_SETAF] = { TTYCODE_STRING, "setaf" }, + [TTYC_SGR0] = { TTYCODE_STRING, "sgr0" }, + [TTYC_SITM] = { TTYCODE_STRING, "sitm" }, + [TTYC_SMACS] = { TTYCODE_STRING, "smacs" }, + [TTYC_SMCUP] = { TTYCODE_STRING, "smcup" }, + [TTYC_SMKX] = { TTYCODE_STRING, "smkx" }, + [TTYC_SMSO] = { TTYCODE_STRING, "smso" }, + [TTYC_SMUL] = { TTYCODE_STRING, "smul" }, + [TTYC_SS] = { TTYCODE_STRING, "Ss" }, + [TTYC_TC] = { TTYCODE_FLAG, "Tc" }, + [TTYC_TSL] = { TTYCODE_STRING, "tsl" }, + [TTYC_VPA] = { TTYCODE_STRING, "vpa" }, + [TTYC_XENL] = { TTYCODE_FLAG, "xenl" }, + [TTYC_XT] = { TTYCODE_FLAG, "XT" }, +}; + +u_int +tty_term_ncodes(void) +{ + return (nitems(tty_term_codes)); +} + char * tty_term_strip(const char *s) { @@ -311,11 +340,11 @@ tty_term_override(struct tty_term *term, const char *overrides) log_debug("%s override: %s %s", term->name, entstr, removeflag ? "@" : val); - for (i = 0; i < NTTYCODE; i++) { + for (i = 0; i < tty_term_ncodes(); i++) { ent = &tty_term_codes[i]; if (strcmp(entstr, ent->name) != 0) continue; - code = &term->codes[ent->code]; + code = &term->codes[i]; if (removeflag) { code->type = TTYCODE_NONE; @@ -374,19 +403,19 @@ tty_term_find(char *name, int fd, char **cause) term->name = xstrdup(name); term->references = 1; term->flags = 0; - memset(term->codes, 0, sizeof term->codes); + term->codes = xcalloc (tty_term_ncodes(), sizeof *term->codes); LIST_INSERT_HEAD(&tty_terms, term, entry); /* Set up curses terminal. */ if (setupterm(name, fd, &error) != OK) { switch (error) { case 1: - xasprintf( - cause, "can't use hardcopy terminal: %s", name); + xasprintf(cause, "can't use hardcopy terminal: %s", + name); break; case 0: - xasprintf( - cause, "missing or unsuitable terminal: %s", name); + xasprintf(cause, "missing or unsuitable terminal: %s", + name); break; case -1: xasprintf(cause, "can't find terminfo database"); @@ -399,10 +428,10 @@ tty_term_find(char *name, int fd, char **cause) } /* Fill in codes. */ - for (i = 0; i < NTTYCODE; i++) { + for (i = 0; i < tty_term_ncodes(); i++) { ent = &tty_term_codes[i]; - code = &term->codes[ent->code]; + code = &term->codes[i]; code->type = TTYCODE_NONE; switch (ent->type) { case TTYCODE_NONE: @@ -426,13 +455,13 @@ tty_term_find(char *name, int fd, char **cause) if (n == -1) break; code->type = TTYCODE_FLAG; - code->value.number = n; + code->value.flag = n; break; } } /* Apply terminal overrides. */ - s = options_get_string(&global_options, "terminal-overrides"); + s = options_get_string(global_options, "terminal-overrides"); tty_term_override(term, s); /* Delete curses data. */ @@ -512,10 +541,12 @@ tty_term_free(struct tty_term *term) LIST_REMOVE(term, entry); - for (i = 0; i < NTTYCODE; i++) { + for (i = 0; i < tty_term_ncodes(); i++) { if (term->codes[i].type == TTYCODE_STRING) free(term->codes[i].value.string); } + free(term->codes); + free(term->name); free(term); } @@ -532,11 +563,10 @@ tty_term_string(struct tty_term *term, enum tty_code_code code) if (!tty_term_has(term, code)) return (""); if (term->codes[code].type != TTYCODE_STRING) - log_fatalx("not a string: %d", code); + fatalx("not a string: %d", code); return (term->codes[code].value.string); } -/* No vtparm. Fucking curses. */ const char * tty_term_string1(struct tty_term *term, enum tty_code_code code, int a) { @@ -568,7 +598,7 @@ tty_term_number(struct tty_term *term, enum tty_code_code code) if (!tty_term_has(term, code)) return (0); if (term->codes[code].type != TTYCODE_NUMBER) - log_fatalx("not a number: %d", code); + fatalx("not a number: %d", code); return (term->codes[code].value.number); } @@ -578,6 +608,38 @@ tty_term_flag(struct tty_term *term, enum tty_code_code code) if (!tty_term_has(term, code)) return (0); if (term->codes[code].type != TTYCODE_FLAG) - log_fatalx("not a flag: %d", code); + fatalx("not a flag: %d", code); return (term->codes[code].value.flag); } + +const char * +tty_term_describe(struct tty_term *term, enum tty_code_code code) +{ + static char s[256]; + char out[128]; + + switch (term->codes[code].type) { + case TTYCODE_NONE: + xsnprintf(s, sizeof s, "%4u: %s: [missing]", + code, tty_term_codes[code].name); + break; + case TTYCODE_STRING: + strnvis(out, term->codes[code].value.string, sizeof out, + VIS_OCTAL|VIS_TAB|VIS_NL); + xsnprintf(s, sizeof s, "%4u: %s: (string) %s", + code, tty_term_codes[code].name, + out); + break; + case TTYCODE_NUMBER: + xsnprintf(s, sizeof s, "%4u: %s: (number) %d", + code, tty_term_codes[code].name, + term->codes[code].value.number); + break; + case TTYCODE_FLAG: + xsnprintf(s, sizeof s, "%4u: %s: (flag) %s", + code, tty_term_codes[code].name, + term->codes[code].value.flag ? "true" : "false"); + break; + } + return (s); +} diff --git a/tty.c b/tty.c index 1bb89811..bbde3541 100644 --- a/tty.c +++ b/tty.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -31,10 +31,20 @@ #include "tmux.h" +static int tty_log_fd = -1; + void tty_read_callback(struct bufferevent *, void *); void tty_error_callback(struct bufferevent *, short, void *); +static int tty_same_fg(const struct grid_cell *, const struct grid_cell *); +static int tty_same_bg(const struct grid_cell *, const struct grid_cell *); +static int tty_same_colours(const struct grid_cell *, const struct grid_cell *); +static int tty_is_fg(const struct grid_cell *, int); +static int tty_is_bg(const struct grid_cell *, int); + +void tty_set_italics(struct tty *); int tty_try_256(struct tty *, u_char, const char *); +int tty_try_rgb(struct tty *, const struct grid_cell_rgb *, const char *); void tty_colours(struct tty *, const struct grid_cell *); void tty_check_fg(struct tty *, struct grid_cell *); @@ -43,11 +53,14 @@ void tty_colours_fg(struct tty *, const struct grid_cell *); void tty_colours_bg(struct tty *, const struct grid_cell *); int tty_large_region(struct tty *, const struct tty_ctx *); +int tty_fake_bce(const struct tty *, const struct window_pane *); void tty_redraw_region(struct tty *, const struct tty_ctx *); -void tty_emulate_repeat( - struct tty *, enum tty_code_code, enum tty_code_code, u_int); +void tty_emulate_repeat(struct tty *, enum tty_code_code, enum tty_code_code, + u_int); void tty_repeat_space(struct tty *, u_int); -void tty_cell(struct tty *, const struct grid_cell *); +void tty_cell(struct tty *, const struct grid_cell *, + const struct window_pane *); +void tty_default_colours(struct grid_cell *, const struct window_pane *); #define tty_use_acs(tty) \ (tty_term_has((tty)->term, TTYC_ACSC) && !((tty)->flags & TTY_UTF8)) @@ -55,13 +68,95 @@ void tty_cell(struct tty *, const struct grid_cell *); #define tty_pane_full_width(tty, ctx) \ ((ctx)->xoff == 0 && screen_size_x((ctx)->wp->screen) >= (tty)->sx) +static int +tty_same_fg(const struct grid_cell *gc1, const struct grid_cell *gc2) +{ + int flags1, flags2; + + flags1 = (gc1->flags & (GRID_FLAG_FG256|GRID_FLAG_FGRGB)); + flags2 = (gc2->flags & (GRID_FLAG_FG256|GRID_FLAG_FGRGB)); + + if (flags1 != flags2) + return (0); + + if (flags1 & GRID_FLAG_FGRGB) { + if (gc1->fg_rgb.r != gc2->fg_rgb.r) + return (0); + if (gc1->fg_rgb.g != gc2->fg_rgb.g) + return (0); + if (gc1->fg_rgb.b != gc2->fg_rgb.b) + return (0); + return (1); + } + return (gc1->fg == gc2->fg); +} + +static int +tty_same_bg(const struct grid_cell *gc1, const struct grid_cell *gc2) +{ + int flags1, flags2; + + flags1 = (gc1->flags & (GRID_FLAG_BG256|GRID_FLAG_BGRGB)); + flags2 = (gc2->flags & (GRID_FLAG_BG256|GRID_FLAG_BGRGB)); + + if (flags1 != flags2) + return (0); + + if (flags1 & GRID_FLAG_BGRGB) { + if (gc1->bg_rgb.r != gc2->bg_rgb.r) + return (0); + if (gc1->bg_rgb.g != gc2->bg_rgb.g) + return (0); + if (gc1->bg_rgb.b != gc2->bg_rgb.b) + return (0); + return (1); + } + return (gc1->bg == gc2->bg); +} + +static int +tty_same_colours(const struct grid_cell *gc1, const struct grid_cell *gc2) +{ + return (tty_same_fg(gc1, gc2) && tty_same_bg(gc1, gc2)); +} + +static int +tty_is_fg(const struct grid_cell *gc, int c) +{ + if (gc->flags & (GRID_FLAG_FG256|GRID_FLAG_FGRGB)) + return (0); + return (gc->fg == c); +} + +static int +tty_is_bg(const struct grid_cell *gc, int c) +{ + if (gc->flags & (GRID_FLAG_BG256|GRID_FLAG_BGRGB)) + return (0); + return (gc->bg == c); +} + void +tty_create_log(void) +{ + char name[64]; + + xsnprintf(name, sizeof name, "tmate-out-%ld.log", (long)getpid()); + + tty_log_fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644); + if (tty_log_fd != -1 && fcntl(tty_log_fd, F_SETFD, FD_CLOEXEC) == -1) + fatal("fcntl failed"); +} + +int tty_init(struct tty *tty, struct client *c, int fd, char *term) { char *path; + if (!isatty(fd)) + return (-1); + memset(tty, 0, sizeof *tty); - tty->log_fd = -1; if (term == NULL || *term == '\0') tty->termname = xstrdup("unknown"); @@ -71,13 +166,15 @@ tty_init(struct tty *tty, struct client *c, int fd, char *term) tty->client = c; if ((path = ttyname(fd)) == NULL) - fatalx("ttyname failed"); + return (-1); tty->path = xstrdup(path); tty->cstyle = 0; tty->ccolour = xstrdup(""); tty->flags = 0; tty->term_flags = 0; + + return (0); } int @@ -130,17 +227,6 @@ tty_set_size(struct tty *tty, u_int sx, u_int sy) { int tty_open(struct tty *tty, char **cause) { - char out[64]; - int fd; - - if (debug_level > 3) { - xsnprintf(out, sizeof out, "tmux-out-%ld.log", (long) getpid()); - fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644); - if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) - fatal("fcntl failed"); - tty->log_fd = fd; - } - tty->term = tty_term_find(tty->termname, tty->fd, cause); if (tty->term == NULL) { tty_close(tty); @@ -150,8 +236,8 @@ tty_open(struct tty *tty, char **cause) tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_TIMER); - tty->event = bufferevent_new( - tty->fd, tty_read_callback, NULL, tty_error_callback, tty); + tty->event = bufferevent_new(tty->fd, tty_read_callback, NULL, + tty_error_callback, tty); tty_start_tty(tty); @@ -161,7 +247,7 @@ tty_open(struct tty *tty, char **cause) } void -tty_read_callback(unused struct bufferevent *bufev, void *data) +tty_read_callback(__unused struct bufferevent *bufev, void *data) { struct tty *tty = data; @@ -170,8 +256,8 @@ tty_read_callback(unused struct bufferevent *bufev, void *data) } void -tty_error_callback( - unused struct bufferevent *bufev, unused short what, unused void *data) +tty_error_callback(__unused struct bufferevent *bufev, __unused short what, + __unused void *data) { } @@ -217,14 +303,13 @@ tty_start_tty(struct tty *tty) tty_putcode(tty, TTYC_CNORM); if (tty_term_has(tty->term, TTYC_KMOUS)) - tty_puts(tty, "\033[?1000l\033[?1006l\033[?1005l"); + tty_puts(tty, "\033[?1000l\033[?1002l\033[?1006l\033[?1005l"); - if (tty_term_has(tty->term, TTYC_XT)) { - if (options_get_number(&global_options, "focus-events")) { + if (tty_term_flag(tty->term, TTYC_XT)) { + if (options_get_number(global_options, "focus-events")) { tty->flags |= TTY_FOCUS; tty_puts(tty, "\033[?1004h"); } - tty_puts(tty, "\033[c"); } tty->cx = UINT_MAX; @@ -238,14 +323,10 @@ tty_start_tty(struct tty *tty) tty->flags |= TTY_STARTED; tty_force_cursor_colour(tty, ""); -} -void -tty_set_class(struct tty *tty, u_int class) -{ - if (tty->class != 0) - return; - tty->class = class; + tty->mouse_drag_flag = 0; + tty->mouse_drag_update = NULL; + tty->mouse_drag_release = NULL; } void @@ -287,9 +368,9 @@ tty_stop_tty(struct tty *tty) tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM)); if (tty_term_has(tty->term, TTYC_KMOUS)) - tty_raw(tty, "\033[?1000l\033[?1006l\033[?1005l"); + tty_raw(tty, "\033[?1000l\033[?1002l\033[?1006l\033[?1005l"); - if (tty_term_has(tty->term, TTYC_XT)) { + if (tty_term_flag(tty->term, TTYC_XT)) { if (tty->flags & TTY_FOCUS) { tty->flags &= ~TTY_FOCUS; tty_raw(tty, "\033[?1004l"); @@ -304,11 +385,6 @@ tty_stop_tty(struct tty *tty) void tty_close(struct tty *tty) { - if (tty->log_fd != -1) { - close(tty->log_fd); - tty->log_fd = -1; - } - if (event_initialized(&tty->key_timer)) evtimer_del(&tty->key_timer); tty_stop_tty(tty); @@ -334,10 +410,8 @@ tty_free(struct tty *tty) tty_close(tty); free(tty->ccolour); - if (tty->path != NULL) - free(tty->path); - if (tty->termname != NULL) - free(tty->termname); + free(tty->path); + free(tty->termname); } void @@ -404,8 +478,8 @@ tty_puts(struct tty *tty, const char *s) return; bufferevent_write(tty->event, s, strlen(s)); - if (tty->log_fd != -1) - write(tty->log_fd, s, strlen(s)); + if (tty_log_fd != -1) + write(tty_log_fd, s, strlen(s)); } void @@ -436,19 +510,34 @@ tty_putc(struct tty *tty, u_char ch) tty->cx++; } - if (tty->log_fd != -1) - write(tty->log_fd, &ch, 1); + if (tty_log_fd != -1) + write(tty_log_fd, &ch, 1); } void tty_putn(struct tty *tty, const void *buf, size_t len, u_int width) { bufferevent_write(tty->event, buf, len); - if (tty->log_fd != -1) - write(tty->log_fd, buf, len); + if (tty_log_fd != -1) + write(tty_log_fd, buf, len); tty->cx += width; } +void +tty_set_italics(struct tty *tty) +{ + const char *s; + + if (tty_term_has(tty->term, TTYC_SITM)) { + s = options_get_string(global_options, "default-terminal"); + if (strcmp(s, "screen") != 0 && strncmp(s, "screen-", 7) != 0) { + tty_putcode(tty, TTYC_SITM); + return; + } + } + tty_putcode(tty, TTYC_SMSO); +} + void tty_set_title(struct tty *tty, const char *title) { @@ -477,24 +566,27 @@ tty_update_mode(struct tty *tty, int mode, struct screen *s) { int changed; - if (strcmp(s->ccolour, tty->ccolour)) + if (s != NULL && strcmp(s->ccolour, tty->ccolour) != 0) tty_force_cursor_colour(tty, s->ccolour); if (tty->flags & TTY_NOCURSOR) mode &= ~MODE_CURSOR; changed = mode ^ tty->mode; - if (changed & (MODE_CURSOR|MODE_BLINKING)) { - if (mode & MODE_CURSOR) { - if (mode & MODE_BLINKING && - tty_term_has(tty->term, TTYC_CVVIS)) - tty_putcode(tty, TTYC_CVVIS); - else - tty_putcode(tty, TTYC_CNORM); - } else + if (changed & MODE_BLINKING) { + if (tty_term_has(tty->term, TTYC_CVVIS)) + tty_putcode(tty, TTYC_CVVIS); + else + tty_putcode(tty, TTYC_CNORM); + changed |= MODE_CURSOR; + } + if (changed & MODE_CURSOR) { + if (mode & MODE_CURSOR) + tty_putcode(tty, TTYC_CNORM); + else tty_putcode(tty, TTYC_CIVIS); } - if (tty->cstyle != s->cstyle) { + if (s != NULL && tty->cstyle != s->cstyle) { if (tty_term_has(tty->term, TTYC_SS)) { if (s->cstyle == 0 && tty_term_has(tty->term, TTYC_SE)) @@ -504,21 +596,15 @@ tty_update_mode(struct tty *tty, int mode, struct screen *s) } tty->cstyle = s->cstyle; } - if (changed & (ALL_MOUSE_MODES|MODE_MOUSE_UTF8)) { + if (changed & ALL_MOUSE_MODES) { if (mode & ALL_MOUSE_MODES) { /* - * Enable the UTF-8 (1005) extension if configured to. * Enable the SGR (1006) extension unconditionally, as * this is safe from misinterpretation. Do it in this * order, because in some terminals it's the last one * that takes effect and SGR is the preferred one. */ - if (mode & MODE_MOUSE_UTF8) - tty_puts(tty, "\033[?1005h"); - else - tty_puts(tty, "\033[?1005l"); tty_puts(tty, "\033[?1006h"); - if (mode & MODE_MOUSE_BUTTON) tty_puts(tty, "\033[?1002h"); else if (mode & MODE_MOUSE_STANDARD) @@ -528,10 +614,7 @@ tty_update_mode(struct tty *tty, int mode, struct screen *s) tty_puts(tty, "\033[?1002l"); else if (tty->mode & MODE_MOUSE_STANDARD) tty_puts(tty, "\033[?1000l"); - tty_puts(tty, "\033[?1006l"); - if (tty->mode & MODE_MOUSE_UTF8) - tty_puts(tty, "\033[?1005l"); } } if (changed & MODE_KKEYPAD) { @@ -574,13 +657,30 @@ tty_repeat_space(struct tty *tty, u_int n) * pane. */ int -tty_large_region(unused struct tty *tty, const struct tty_ctx *ctx) +tty_large_region(__unused struct tty *tty, const struct tty_ctx *ctx) { struct window_pane *wp = ctx->wp; return (ctx->orlower - ctx->orupper >= screen_size_y(wp->screen) / 2); } +/* + * Return if BCE is needed but the terminal doesn't have it - it'll need to be + * emulated. + */ +int +tty_fake_bce(const struct tty *tty, const struct window_pane *wp) +{ + struct grid_cell gc; + + memcpy(&gc, &grid_default_cell, sizeof gc); + tty_default_colours(&gc, wp); + + if (gc.bg == 8 && !(gc.flags & GRID_FLAG_BG256)) + return (0); + return (!tty_term_flag(tty->term, TTYC_BCE)); +} + /* * Redraw scroll region using data from screen (already updated). Used when * CSR not supported, or window is a pane that doesn't take up the full @@ -604,23 +704,32 @@ tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx) if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) { for (i = ctx->ocy; i < screen_size_y(s); i++) - tty_draw_line(tty, s, i, ctx->xoff, ctx->yoff); + tty_draw_pane(tty, wp, i, ctx->xoff, ctx->yoff); } else { for (i = ctx->orupper; i <= ctx->orlower; i++) - tty_draw_line(tty, s, i, ctx->xoff, ctx->yoff); + tty_draw_pane(tty, wp, i, ctx->xoff, ctx->yoff); } } void -tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy) +tty_draw_pane(struct tty *tty, const struct window_pane *wp, u_int py, u_int ox, + u_int oy) { - const struct grid_cell *gc; - struct grid_line *gl; - struct grid_cell tmpgc; - struct utf8_data ud; - u_int i, sx; + tty_draw_line(tty, wp, wp->screen, py, ox, oy); +} - tty_update_mode(tty, tty->mode & ~MODE_CURSOR, s); +void +tty_draw_line(struct tty *tty, const struct window_pane *wp, + struct screen *s, u_int py, u_int ox, u_int oy) +{ + struct grid_cell gc; + struct grid_line *gl; + u_int i, sx; + int flags; + + flags = tty->flags & TTY_NOCURSOR; + tty->flags |= TTY_NOCURSOR; + tty_update_mode(tty, tty->mode, s); sx = screen_size_x(s); if (sx > s->grid->linedata[s->grid->hsize + py].cellsize) @@ -629,7 +738,7 @@ tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy) sx = tty->sx; /* - * Don't move the cursor to the start permission if it will wrap there + * Don't move the cursor to the start position if it will wrap there * itself. */ gl = NULL; @@ -641,42 +750,52 @@ tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy) tty_cursor(tty, ox, oy + py); for (i = 0; i < sx; i++) { - gc = grid_view_peek_cell(s->grid, i, py); + grid_view_get_cell(s->grid, i, py, &gc); if (screen_check_selection(s, i, py)) { - memcpy(&tmpgc, &s->sel.cell, sizeof tmpgc); - grid_cell_get(gc, &ud); - grid_cell_set(&tmpgc, &ud); - tmpgc.flags = gc->flags & - ~(GRID_FLAG_FG256|GRID_FLAG_BG256); - tmpgc.flags |= s->sel.cell.flags & + gc.flags &= ~(GRID_FLAG_FG256|GRID_FLAG_BG256); + gc.flags |= s->sel.cell.flags & (GRID_FLAG_FG256|GRID_FLAG_BG256); - tty_cell(tty, &tmpgc); - } else - tty_cell(tty, gc); + } + tty_cell(tty, &gc, wp); } - if (sx >= tty->sx) { - tty_update_mode(tty, tty->mode, s); - return; - } - tty_reset(tty); + if (sx < tty->sx) { + tty_attributes(tty, &grid_default_cell, wp); - tty_cursor(tty, ox + sx, oy + py); - if (sx != screen_size_x(s) && ox + screen_size_x(s) >= tty->sx && - tty_term_has(tty->term, TTYC_EL)) - tty_putcode(tty, TTYC_EL); - else - tty_repeat_space(tty, screen_size_x(s) - sx); + tty_cursor(tty, ox + sx, oy + py); + if (sx != screen_size_x(s) && + ox + screen_size_x(s) >= tty->sx && + tty_term_has(tty->term, TTYC_EL) && + !tty_fake_bce(tty, wp)) + tty_putcode(tty, TTYC_EL); + else + tty_repeat_space(tty, screen_size_x(s) - sx); + } + + tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags; tty_update_mode(tty, tty->mode, s); } +int +tty_client_ready(struct client *c, struct window_pane *wp) +{ + if (c->session == NULL || c->tty.term == NULL) + return (0); + if (c->flags & CLIENT_SUSPENDED) + return (0); + if (c->tty.flags & TTY_FREEZE) + return (0); + if (c->session->curw->window != wp->window) + return (0); + return (1); +} + void -tty_write( - void (*cmdfn)(struct tty *, const struct tty_ctx *), struct tty_ctx *ctx) +tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *), + struct tty_ctx *ctx) { struct window_pane *wp = ctx->wp; struct client *c; - u_int i; /* wp can be NULL if updating the screen but not the terminal. */ if (wp == NULL) @@ -687,15 +806,8 @@ tty_write( if (!window_pane_visible(wp) || wp->flags & PANE_DROP) return; - for (i = 0; i < ARRAY_LENGTH(&clients); i++) { - c = ARRAY_ITEM(&clients, i); - if (c == NULL || c->session == NULL || c->tty.term == NULL) - continue; - if (c->flags & CLIENT_SUSPENDED) - continue; - if (c->tty.flags & TTY_FREEZE) - continue; - if (c->session->curw->window != wp->window) + TAILQ_FOREACH(c, &clients, entry) { + if (!tty_client_ready(c, wp)) continue; ctx->xoff = wp->xoff; @@ -713,19 +825,19 @@ tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx) struct window_pane *wp = ctx->wp; if (!tty_pane_full_width(tty, ctx)) { - tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff); + tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff); return; } - tty_reset(tty); + tty_attributes(tty, &grid_default_cell, wp); tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy); - if (tty_term_has(tty->term, TTYC_ICH) || - tty_term_has(tty->term, TTYC_ICH1)) + if (!tty_fake_bce(tty, wp) && (tty_term_has(tty->term, TTYC_ICH) || + tty_term_has(tty->term, TTYC_ICH1))) tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num); else - tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff); + tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff); } void @@ -733,14 +845,14 @@ tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx) { struct window_pane *wp = ctx->wp; - if (!tty_pane_full_width(tty, ctx) || + if (!tty_pane_full_width(tty, ctx) || tty_fake_bce(tty, wp) || (!tty_term_has(tty->term, TTYC_DCH) && !tty_term_has(tty->term, TTYC_DCH1))) { - tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff); + tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff); return; } - tty_reset(tty); + tty_attributes(tty, &grid_default_cell, wp); tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy); @@ -754,11 +866,11 @@ tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx) { u_int i; - tty_reset(tty); + tty_attributes(tty, &grid_default_cell, ctx->wp); tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy); - if (tty_term_has(tty->term, TTYC_ECH)) + if (tty_term_has(tty->term, TTYC_ECH) && !tty_fake_bce(tty, ctx->wp)) tty_putcode1(tty, TTYC_ECH, ctx->num); else { for (i = 0; i < ctx->num; i++) @@ -769,14 +881,14 @@ tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx) void tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx) { - if (!tty_pane_full_width(tty, ctx) || + if (!tty_pane_full_width(tty, ctx) || tty_fake_bce(tty, ctx->wp) || !tty_term_has(tty->term, TTYC_CSR) || !tty_term_has(tty->term, TTYC_IL1)) { tty_redraw_region(tty, ctx); return; } - tty_reset(tty); + tty_attributes(tty, &grid_default_cell, ctx->wp); tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower); tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy); @@ -787,14 +899,14 @@ tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx) void tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx) { - if (!tty_pane_full_width(tty, ctx) || + if (!tty_pane_full_width(tty, ctx) || tty_fake_bce(tty, ctx->wp) || !tty_term_has(tty->term, TTYC_CSR) || !tty_term_has(tty->term, TTYC_DL1)) { tty_redraw_region(tty, ctx); return; } - tty_reset(tty); + tty_attributes(tty, &grid_default_cell, ctx->wp); tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower); tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy); @@ -808,11 +920,12 @@ tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx) struct window_pane *wp = ctx->wp; struct screen *s = wp->screen; - tty_reset(tty); + tty_attributes(tty, &grid_default_cell, wp); tty_cursor_pane(tty, ctx, 0, ctx->ocy); - if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) + if (tty_pane_full_width(tty, ctx) && !tty_fake_bce(tty, wp) && + tty_term_has(tty->term, TTYC_EL)) tty_putcode(tty, TTYC_EL); else tty_repeat_space(tty, screen_size_x(s)); @@ -824,11 +937,12 @@ tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx) struct window_pane *wp = ctx->wp; struct screen *s = wp->screen; - tty_reset(tty); + tty_attributes(tty, &grid_default_cell, wp); tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy); - if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) + if (tty_pane_full_width(tty, ctx) && + tty_term_has(tty->term, TTYC_EL) && !tty_fake_bce(tty, wp)) tty_putcode(tty, TTYC_EL); else tty_repeat_space(tty, screen_size_x(s) - ctx->ocx); @@ -837,9 +951,10 @@ tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx) void tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx) { - tty_reset(tty); + tty_attributes(tty, &grid_default_cell, ctx->wp); - if (ctx->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) { + if (ctx->xoff == 0 && tty_term_has(tty->term, TTYC_EL1) && + !tty_fake_bce(tty, ctx->wp)) { tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy); tty_putcode(tty, TTYC_EL1); } else { @@ -854,14 +969,14 @@ tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx) if (ctx->ocy != ctx->orupper) return; - if (!tty_pane_full_width(tty, ctx) || + if (!tty_pane_full_width(tty, ctx) || tty_fake_bce(tty, ctx->wp) || !tty_term_has(tty->term, TTYC_CSR) || !tty_term_has(tty->term, TTYC_RI)) { tty_redraw_region(tty, ctx); return; } - tty_reset(tty); + tty_attributes(tty, &grid_default_cell, ctx->wp); tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower); tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper); @@ -877,7 +992,7 @@ tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx) if (ctx->ocy != ctx->orlower) return; - if (!tty_pane_full_width(tty, ctx) || + if (!tty_pane_full_width(tty, ctx) || tty_fake_bce(tty, wp) || !tty_term_has(tty->term, TTYC_CSR)) { if (tty_large_region(tty, ctx)) wp->flags |= PANE_REDRAW; @@ -894,7 +1009,7 @@ tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx) if (ctx->num && !(tty->term->flags & TERM_EARLYWRAP)) return; - tty_reset(tty); + tty_attributes(tty, &grid_default_cell, wp); tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower); tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy); @@ -909,12 +1024,13 @@ tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx) struct screen *s = wp->screen; u_int i, j; - tty_reset(tty); + tty_attributes(tty, &grid_default_cell, wp); tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1); tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy); - if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) { + if (tty_pane_full_width(tty, ctx) && + tty_term_has(tty->term, TTYC_EL) && !tty_fake_bce(tty, wp)) { tty_putcode(tty, TTYC_EL); if (ctx->ocy != screen_size_y(s) - 1) { tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1); @@ -942,12 +1058,13 @@ tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx) struct screen *s = wp->screen; u_int i, j; - tty_reset(tty); + tty_attributes(tty, &grid_default_cell, wp); tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1); tty_cursor_pane(tty, ctx, 0, 0); - if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) { + if (tty_pane_full_width(tty, ctx) && + tty_term_has(tty->term, TTYC_EL) && !tty_fake_bce(tty, wp)) { for (i = 0; i < ctx->ocy; i++) { tty_putcode(tty, TTYC_EL); tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1); @@ -969,12 +1086,13 @@ tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx) struct screen *s = wp->screen; u_int i, j; - tty_reset(tty); + tty_attributes(tty, &grid_default_cell, wp); tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1); tty_cursor_pane(tty, ctx, 0, 0); - if (tty_pane_full_width(tty, ctx) && tty_term_has(tty->term, TTYC_EL)) { + if (tty_pane_full_width(tty, ctx) && + tty_term_has(tty->term, TTYC_EL) && !tty_fake_bce(tty, wp)) { for (i = 0; i < screen_size_y(s); i++) { tty_putcode(tty, TTYC_EL); if (i != screen_size_y(s) - 1) { @@ -997,7 +1115,7 @@ tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx) struct screen *s = wp->screen; u_int i, j; - tty_reset(tty); + tty_attributes(tty, &grid_default_cell, wp); tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1); @@ -1019,7 +1137,7 @@ tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx) tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower); /* Is the cursor in the very last position? */ - width = grid_cell_width(ctx->cell); + width = ctx->cell->data.width; if (ctx->ocx > wp->sx - width) { if (ctx->xoff != 0 || wp->sx != tty->sx) { /* @@ -1036,14 +1154,14 @@ tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx) * move as far left as possible and redraw the last * cell to move into the last position. */ - cx = screen_size_x(s) - grid_cell_width(&ctx->last_cell); + cx = screen_size_x(s) - ctx->last_cell.data.width; tty_cursor_pane(tty, ctx, cx, ctx->ocy); - tty_cell(tty, &ctx->last_cell); + tty_cell(tty, &ctx->last_cell, wp); } } else tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy); - tty_cell(tty, ctx->cell); + tty_cell(tty, ctx->cell, wp); } void @@ -1055,7 +1173,7 @@ tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx) * Cannot rely on not being a partial character, so just redraw the * whole line. */ - tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff); + tty_draw_pane(tty, wp, ctx->ocy, ctx->xoff, ctx->yoff); } void @@ -1088,15 +1206,15 @@ tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx) tty->cx = tty->cy = UINT_MAX; tty->rupper = tty->rlower = UINT_MAX; - tty_reset(tty); + tty_attributes(tty, &grid_default_cell, ctx->wp); tty_cursor(tty, 0, 0); } void -tty_cell(struct tty *tty, const struct grid_cell *gc) +tty_cell(struct tty *tty, const struct grid_cell *gc, + const struct window_pane *wp) { - struct utf8_data ud; - u_int i; + u_int i; /* Skip last character if terminal is stupid. */ if (tty->term->flags & TERM_EARLYWRAP && @@ -1108,26 +1226,25 @@ tty_cell(struct tty *tty, const struct grid_cell *gc) return; /* Set the attributes. */ - tty_attributes(tty, gc); + tty_attributes(tty, gc, wp); /* Get the cell and if ASCII write with putc to do ACS translation. */ - grid_cell_get(gc, &ud); - if (ud.size == 1) { - if (*ud.data < 0x20 || *ud.data == 0x7f) + if (gc->data.size == 1) { + if (*gc->data.data < 0x20 || *gc->data.data == 0x7f) return; - tty_putc(tty, *ud.data); + tty_putc(tty, *gc->data.data); return; } /* If not UTF-8, write _. */ if (!(tty->flags & TTY_UTF8)) { - for (i = 0; i < ud.width; i++) + for (i = 0; i < gc->data.width; i++) tty_putc(tty, '_'); return; } /* Write the data. */ - tty_putn(tty, ud.data, ud.size, ud.width); + tty_putn(tty, gc->data.data, gc->data.size, gc->data.width); } void @@ -1146,8 +1263,8 @@ tty_reset(struct tty *tty) /* Set region inside pane. */ void -tty_region_pane( - struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower) +tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper, + u_int rlower) { tty_region(tty, ctx->yoff + rupper, ctx->yoff + rlower); } @@ -1312,12 +1429,14 @@ out: } void -tty_attributes(struct tty *tty, const struct grid_cell *gc) +tty_attributes(struct tty *tty, const struct grid_cell *gc, + const struct window_pane *wp) { struct grid_cell *tc = &tty->cell, gc2; u_char changed; memcpy(&gc2, gc, sizeof gc2); + tty_default_colours(&gc2, wp); /* * If no setab, try to use the reverse attribute as a best-effort for a @@ -1357,12 +1476,8 @@ tty_attributes(struct tty *tty, const struct grid_cell *gc) tty_putcode(tty, TTYC_BOLD); if (changed & GRID_ATTR_DIM) tty_putcode(tty, TTYC_DIM); - if (changed & GRID_ATTR_ITALICS) { - if (tty_term_has(tty->term, TTYC_SITM)) - tty_putcode(tty, TTYC_SITM); - else - tty_putcode(tty, TTYC_SMSO); - } + if (changed & GRID_ATTR_ITALICS) + tty_set_italics(tty); if (changed & GRID_ATTR_UNDERSCORE) tty_putcode(tty, TTYC_SMUL); if (changed & GRID_ATTR_BLINK) @@ -1383,12 +1498,10 @@ void tty_colours(struct tty *tty, const struct grid_cell *gc) { struct grid_cell *tc = &tty->cell; - u_char fg = gc->fg, bg = gc->bg, flags = gc->flags; int have_ax, fg_default, bg_default; /* No changes? Nothing is necessary. */ - if (fg == tc->fg && bg == tc->bg && - ((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256)) == 0) + if (tty_same_colours(gc, tc)) return; /* @@ -1397,8 +1510,8 @@ tty_colours(struct tty *tty, const struct grid_cell *gc) * case if only one is default need to fall onward to set the other * colour. */ - fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256)); - bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256)); + fg_default = tty_is_fg(gc, 8); + bg_default = tty_is_bg(gc, 8); if (fg_default || bg_default) { /* * If don't have AX but do have op, send sgr0 (op can't @@ -1407,51 +1520,59 @@ tty_colours(struct tty *tty, const struct grid_cell *gc) * * Otherwise, try to set the default colour only as needed. */ - have_ax = tty_term_has(tty->term, TTYC_AX); + have_ax = tty_term_flag(tty->term, TTYC_AX); if (!have_ax && tty_term_has(tty->term, TTYC_OP)) tty_reset(tty); else { - if (fg_default && - (tc->fg != 8 || tc->flags & GRID_FLAG_FG256)) { + if (fg_default && !tty_is_fg(tc, 8)) { if (have_ax) tty_puts(tty, "\033[39m"); - else if (tc->fg != 7 || - tc->flags & GRID_FLAG_FG256) + else if (!tty_is_fg(tc, 7)) tty_putcode1(tty, TTYC_SETAF, 7); tc->fg = 8; - tc->flags &= ~GRID_FLAG_FG256; + tc->flags &= ~(GRID_FLAG_FG256|GRID_FLAG_FGRGB); } - if (bg_default && - (tc->bg != 8 || tc->flags & GRID_FLAG_BG256)) { + if (bg_default && !tty_is_bg(tc, 8)) { if (have_ax) tty_puts(tty, "\033[49m"); - else if (tc->bg != 0 || - tc->flags & GRID_FLAG_BG256) + else if (!tty_is_bg(tc, 0)) tty_putcode1(tty, TTYC_SETAB, 0); tc->bg = 8; - tc->flags &= ~GRID_FLAG_BG256; + tc->flags &= ~(GRID_FLAG_BG256|GRID_FLAG_BGRGB); } } } /* Set the foreground colour. */ - if (!fg_default && (fg != tc->fg || - ((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256)))) + if (!fg_default && !tty_same_fg(gc, tc)) tty_colours_fg(tty, gc); /* * Set the background colour. This must come after the foreground as * tty_colour_fg() can call tty_reset(). */ - if (!bg_default && (bg != tc->bg || - ((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256)))) + if (!bg_default && !tty_same_bg(gc, tc)) tty_colours_bg(tty, gc); } void tty_check_fg(struct tty *tty, struct grid_cell *gc) { - u_int colours; + struct grid_cell_rgb *rgb = &gc->fg_rgb; + u_int colours; + + /* Is this a 24-bit colour? */ + if (gc->flags & GRID_FLAG_FGRGB) { + /* Not a 24-bit terminal? Translate to 256-colour palette. */ + if (!tty_term_flag(tty->term, TTYC_TC)) { + gc->flags &= ~GRID_FLAG_FGRGB; + gc->flags |= GRID_FLAG_FG256; + gc->fg = colour_find_rgb(rgb->r, rgb->g, rgb->b); + } + else + return; + } + colours = tty_term_number(tty->term, TTYC_COLORS); /* Is this a 256-colour colour? */ if (gc->flags & GRID_FLAG_FG256) { @@ -1461,7 +1582,10 @@ tty_check_fg(struct tty *tty, struct grid_cell *gc) gc->fg = colour_256to16(gc->fg); if (gc->fg & 8) { gc->fg &= 7; - gc->attr |= GRID_ATTR_BRIGHT; + if (colours >= 16) + gc->fg += 90; + else + gc->attr |= GRID_ATTR_BRIGHT; } else gc->attr &= ~GRID_ATTR_BRIGHT; gc->flags &= ~GRID_FLAG_FG256; @@ -1470,7 +1594,6 @@ tty_check_fg(struct tty *tty, struct grid_cell *gc) } /* Is this an aixterm colour? */ - colours = tty_term_number(tty->term, TTYC_COLORS); if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) { gc->fg -= 90; gc->attr |= GRID_ATTR_BRIGHT; @@ -1480,7 +1603,21 @@ tty_check_fg(struct tty *tty, struct grid_cell *gc) void tty_check_bg(struct tty *tty, struct grid_cell *gc) { - u_int colours; + struct grid_cell_rgb *rgb = &gc->bg_rgb; + u_int colours; + + /* Is this a 24-bit colour? */ + if (gc->flags & GRID_FLAG_BGRGB) { + /* Not a 24-bit terminal? Translate to 256-colour palette. */ + if (!tty_term_flag(tty->term, TTYC_TC)) { + gc->flags &= ~GRID_FLAG_BGRGB; + gc->flags |= GRID_FLAG_BG256; + gc->bg = colour_find_rgb(rgb->r, rgb->g, rgb->b); + } + else + return; + } + colours = tty_term_number(tty->term, TTYC_COLORS); /* Is this a 256-colour colour? */ if (gc->flags & GRID_FLAG_BG256) { @@ -1492,20 +1629,19 @@ tty_check_bg(struct tty *tty, struct grid_cell *gc) if (!(tty->term->flags & TERM_256COLOURS) && !(tty->term_flags & TERM_256COLOURS)) { gc->bg = colour_256to16(gc->bg); - if (gc->bg & 8) + if (gc->bg & 8) { gc->bg &= 7; - gc->attr &= ~GRID_ATTR_BRIGHT; + if (colours >= 16) + gc->fg += 90; + } gc->flags &= ~GRID_FLAG_BG256; } return; } /* Is this an aixterm colour? */ - colours = tty_term_number(tty->term, TTYC_COLORS); - if (gc->bg >= 90 && gc->bg <= 97 && colours < 16) { + if (gc->bg >= 90 && gc->bg <= 97 && colours < 16) gc->bg -= 90; - gc->attr |= GRID_ATTR_BRIGHT; - } } void @@ -1515,12 +1651,21 @@ tty_colours_fg(struct tty *tty, const struct grid_cell *gc) u_char fg = gc->fg; char s[32]; + tc->flags &= ~(GRID_FLAG_FG256|GRID_FLAG_FGRGB); + + /* Is this a 24-bit colour? */ + if (gc->flags & GRID_FLAG_FGRGB) { + if (tty_try_rgb(tty, &gc->fg_rgb, "38") == 0) + goto save_fg; + /* Should not get here, already converted in tty_check_fg. */ + return; + } + /* Is this a 256-colour colour? */ if (gc->flags & GRID_FLAG_FG256) { - /* Try as 256 colours. */ if (tty_try_256(tty, fg, "38") == 0) goto save_fg; - /* Else already handled by tty_check_fg. */ + /* Should not get here, already converted in tty_check_fg. */ return; } @@ -1536,9 +1681,12 @@ tty_colours_fg(struct tty *tty, const struct grid_cell *gc) save_fg: /* Save the new values in the terminal current cell. */ - tc->fg = fg; - tc->flags &= ~GRID_FLAG_FG256; - tc->flags |= gc->flags & GRID_FLAG_FG256; + if (gc->flags & GRID_FLAG_FGRGB) + memcpy(&tc->fg_rgb, &gc->fg_rgb, sizeof tc->fg_rgb); + else + tc->fg = fg; + tc->flags &= ~(GRID_FLAG_FGRGB|GRID_FLAG_FG256); + tc->flags |= (gc->flags & (GRID_FLAG_FG256|GRID_FLAG_FGRGB)); } void @@ -1548,25 +1696,27 @@ tty_colours_bg(struct tty *tty, const struct grid_cell *gc) u_char bg = gc->bg; char s[32]; + /* Is this a 24-bit colour? */ + if (gc->flags & GRID_FLAG_BGRGB) { + if (tty_try_rgb(tty, &gc->bg_rgb, "48") == 0) + goto save_bg; + /* Should not get here, already converted in tty_check_bg. */ + return; + } + /* Is this a 256-colour colour? */ if (gc->flags & GRID_FLAG_BG256) { - /* Try as 256 colours. */ if (tty_try_256(tty, bg, "48") == 0) goto save_bg; - /* Else already handled by tty_check_bg. */ + /* Should not get here, already converted in tty_check_bg. */ return; } /* Is this an aixterm bright colour? */ if (bg >= 90 && bg <= 97) { - /* 16 colour terminals or above only. */ - if (tty_term_number(tty->term, TTYC_COLORS) >= 16) { - xsnprintf(s, sizeof s, "\033[%dm", bg + 10); - tty_puts(tty, s); - goto save_bg; - } - bg -= 90; - /* no such thing as a bold background */ + xsnprintf(s, sizeof s, "\033[%dm", bg + 10); + tty_puts(tty, s); + goto save_bg; } /* Otherwise set the background colour. */ @@ -1574,9 +1724,12 @@ tty_colours_bg(struct tty *tty, const struct grid_cell *gc) save_bg: /* Save the new values in the terminal current cell. */ - tc->bg = bg; - tc->flags &= ~GRID_FLAG_BG256; - tc->flags |= gc->flags & GRID_FLAG_BG256; + if (gc->flags & GRID_FLAG_BGRGB) + memcpy(&tc->bg_rgb, &gc->bg_rgb, sizeof tc->bg_rgb); + else + tc->bg = bg; + tc->flags &= ~(GRID_FLAG_BGRGB|GRID_FLAG_BG256); + tc->flags |= (gc->flags & (GRID_FLAG_BG256|GRID_FLAG_BGRGB)); } int @@ -1585,32 +1738,88 @@ tty_try_256(struct tty *tty, u_char colour, const char *type) char s[32]; /* - * If the terminfo entry has 256 colours, assume that setaf and setab - * work correctly. + * If the user has specified -2 to the client, setaf and setab may not + * work (or they may not want to use them), so send the usual sequence. */ - if (tty->term->flags & TERM_256COLOURS) { - if (*type == '3') - tty_putcode1(tty, TTYC_SETAF, colour); - else - tty_putcode1(tty, TTYC_SETAB, colour); - return (0); - } + if (tty->term_flags & TERM_256COLOURS) + goto fallback; /* - * If the user has specified -2 to the client, setaf and setab may not - * work, so send the usual sequence. + * If the terminfo entry has 256 colours and setaf and setab exist, + * assume that they work correctly. */ - if (tty->term_flags & TERM_256COLOURS) { - xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour); - tty_puts(tty, s); + if (tty->term->flags & TERM_256COLOURS) { + if (*type == '3') { + if (!tty_term_has(tty->term, TTYC_SETAF)) + goto fallback; + tty_putcode1(tty, TTYC_SETAF, colour); + } else { + if (!tty_term_has(tty->term, TTYC_SETAB)) + goto fallback; + tty_putcode1(tty, TTYC_SETAB, colour); + } return (0); } return (-1); + +fallback: + xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour); + tty_puts(tty, s); + return (0); +} + +int +tty_try_rgb(struct tty *tty, const struct grid_cell_rgb *rgb, const char *type) +{ + char s[32]; + + if (!tty_term_flag(tty->term, TTYC_TC)) + return (-1); + + xsnprintf(s, sizeof s, "\033[%s;2;%hhu;%hhu;%hhum", type, rgb->r, + rgb->g, rgb->b); + tty_puts(tty, s); + return (0); } void -tty_bell(struct tty *tty) +tty_default_colours(struct grid_cell *gc, const struct window_pane *wp) { - tty_putcode(tty, TTYC_BEL); + const struct grid_cell *agc, *pgc, *wgc; + + if (wp == NULL) + return; + + pgc = &wp->colgc; + agc = options_get_style(wp->window->options, "window-active-style"); + wgc = options_get_style(wp->window->options, "window-style"); + + if (gc->fg == 8 && !(gc->flags & GRID_FLAG_FG256)) { + if (pgc->fg != 8 || (pgc->flags & GRID_FLAG_FG256)) { + gc->fg = pgc->fg; + gc->flags |= (pgc->flags & GRID_FLAG_FG256); + } else if (wp == wp->window->active && + (agc->fg != 8 || (agc->flags & GRID_FLAG_FG256))) { + gc->fg = agc->fg; + gc->flags |= (agc->flags & GRID_FLAG_FG256); + } else { + gc->fg = wgc->fg; + gc->flags |= (wgc->flags & GRID_FLAG_FG256); + } + } + + if (gc->bg == 8 && !(gc->flags & GRID_FLAG_BG256)) { + if (pgc->bg != 8 || (pgc->flags & GRID_FLAG_BG256)) { + gc->bg = pgc->bg; + gc->flags |= (pgc->flags & GRID_FLAG_BG256); + } else if (wp == wp->window->active && + (agc->bg != 8 || (agc->flags & GRID_FLAG_BG256))) { + gc->bg = agc->bg; + gc->flags |= (agc->flags & GRID_FLAG_BG256); + } else { + gc->bg = wgc->bg; + gc->flags |= (wgc->flags & GRID_FLAG_BG256); + } + } } diff --git a/utf8.c b/utf8.c index 39e708ba..c0407576 100644 --- a/utf8.c +++ b/utf8.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2008 Nicholas Marriott + * Copyright (c) 2008 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -20,195 +20,38 @@ #include #include +#include #include "tmux.h" -struct utf8_width_entry { - u_int first; - u_int last; - - int width; - - struct utf8_width_entry *left; - struct utf8_width_entry *right; -}; - -/* Random order. Not optimal but it'll do for now... */ -struct utf8_width_entry utf8_width_table[] = { - { 0x00951, 0x00954, 0, NULL, NULL }, - { 0x00ccc, 0x00ccd, 0, NULL, NULL }, - { 0x0fff9, 0x0fffb, 0, NULL, NULL }, - { 0x20000, 0x2fffd, 2, NULL, NULL }, - { 0x00ebb, 0x00ebc, 0, NULL, NULL }, - { 0x01932, 0x01932, 0, NULL, NULL }, - { 0x0070f, 0x0070f, 0, NULL, NULL }, - { 0x00a70, 0x00a71, 0, NULL, NULL }, - { 0x02329, 0x02329, 2, NULL, NULL }, - { 0x00acd, 0x00acd, 0, NULL, NULL }, - { 0x00ac7, 0x00ac8, 0, NULL, NULL }, - { 0x00a3c, 0x00a3c, 0, NULL, NULL }, - { 0x009cd, 0x009cd, 0, NULL, NULL }, - { 0x00591, 0x005bd, 0, NULL, NULL }, - { 0x01058, 0x01059, 0, NULL, NULL }, - { 0x0ffe0, 0x0ffe6, 2, NULL, NULL }, - { 0x01100, 0x0115f, 2, NULL, NULL }, - { 0x0fe20, 0x0fe23, 0, NULL, NULL }, - { 0x0302a, 0x0302f, 0, NULL, NULL }, - { 0x01772, 0x01773, 0, NULL, NULL }, - { 0x005bf, 0x005bf, 0, NULL, NULL }, - { 0x006ea, 0x006ed, 0, NULL, NULL }, - { 0x00bc0, 0x00bc0, 0, NULL, NULL }, - { 0x00962, 0x00963, 0, NULL, NULL }, - { 0x01732, 0x01734, 0, NULL, NULL }, - { 0x00d41, 0x00d43, 0, NULL, NULL }, - { 0x01b42, 0x01b42, 0, NULL, NULL }, - { 0x00a41, 0x00a42, 0, NULL, NULL }, - { 0x00eb4, 0x00eb9, 0, NULL, NULL }, - { 0x00b01, 0x00b01, 0, NULL, NULL }, - { 0x00e34, 0x00e3a, 0, NULL, NULL }, - { 0x03040, 0x03098, 2, NULL, NULL }, - { 0x0093c, 0x0093c, 0, NULL, NULL }, - { 0x00c4a, 0x00c4d, 0, NULL, NULL }, - { 0x01032, 0x01032, 0, NULL, NULL }, - { 0x00f37, 0x00f37, 0, NULL, NULL }, - { 0x00901, 0x00902, 0, NULL, NULL }, - { 0x00cbf, 0x00cbf, 0, NULL, NULL }, - { 0x0a806, 0x0a806, 0, NULL, NULL }, - { 0x00dd2, 0x00dd4, 0, NULL, NULL }, - { 0x00f71, 0x00f7e, 0, NULL, NULL }, - { 0x01752, 0x01753, 0, NULL, NULL }, - { 0x1d242, 0x1d244, 0, NULL, NULL }, - { 0x005c1, 0x005c2, 0, NULL, NULL }, - { 0x0309b, 0x0a4cf, 2, NULL, NULL }, - { 0xe0100, 0xe01ef, 0, NULL, NULL }, - { 0x017dd, 0x017dd, 0, NULL, NULL }, - { 0x00600, 0x00603, 0, NULL, NULL }, - { 0x009e2, 0x009e3, 0, NULL, NULL }, - { 0x00cc6, 0x00cc6, 0, NULL, NULL }, - { 0x0a80b, 0x0a80b, 0, NULL, NULL }, - { 0x01712, 0x01714, 0, NULL, NULL }, - { 0x00b3c, 0x00b3c, 0, NULL, NULL }, - { 0x01b00, 0x01b03, 0, NULL, NULL }, - { 0x007eb, 0x007f3, 0, NULL, NULL }, - { 0xe0001, 0xe0001, 0, NULL, NULL }, - { 0x1d185, 0x1d18b, 0, NULL, NULL }, - { 0x0feff, 0x0feff, 0, NULL, NULL }, - { 0x01b36, 0x01b3a, 0, NULL, NULL }, - { 0x01920, 0x01922, 0, NULL, NULL }, - { 0x00670, 0x00670, 0, NULL, NULL }, - { 0x00f90, 0x00f97, 0, NULL, NULL }, - { 0x01927, 0x01928, 0, NULL, NULL }, - { 0x0200b, 0x0200f, 0, NULL, NULL }, - { 0x0ff00, 0x0ff60, 2, NULL, NULL }, - { 0x0f900, 0x0faff, 2, NULL, NULL }, - { 0x0fb1e, 0x0fb1e, 0, NULL, NULL }, - { 0x00cbc, 0x00cbc, 0, NULL, NULL }, - { 0x00eb1, 0x00eb1, 0, NULL, NULL }, - { 0x10a38, 0x10a3a, 0, NULL, NULL }, - { 0x007a6, 0x007b0, 0, NULL, NULL }, - { 0x00f80, 0x00f84, 0, NULL, NULL }, - { 0x005c4, 0x005c5, 0, NULL, NULL }, - { 0x0ac00, 0x0d7a3, 2, NULL, NULL }, - { 0x017c9, 0x017d3, 0, NULL, NULL }, - { 0x00d4d, 0x00d4d, 0, NULL, NULL }, - { 0x1d167, 0x1d169, 0, NULL, NULL }, - { 0x01036, 0x01037, 0, NULL, NULL }, - { 0xe0020, 0xe007f, 0, NULL, NULL }, - { 0x00f35, 0x00f35, 0, NULL, NULL }, - { 0x017b4, 0x017b5, 0, NULL, NULL }, - { 0x0206a, 0x0206f, 0, NULL, NULL }, - { 0x00c46, 0x00c48, 0, NULL, NULL }, - { 0x01939, 0x0193b, 0, NULL, NULL }, - { 0x01dc0, 0x01dca, 0, NULL, NULL }, - { 0x10a0c, 0x10a0f, 0, NULL, NULL }, - { 0x0102d, 0x01030, 0, NULL, NULL }, - { 0x017c6, 0x017c6, 0, NULL, NULL }, - { 0x00ec8, 0x00ecd, 0, NULL, NULL }, - { 0x00b41, 0x00b43, 0, NULL, NULL }, - { 0x017b7, 0x017bd, 0, NULL, NULL }, - { 0x1d173, 0x1d182, 0, NULL, NULL }, - { 0x00a47, 0x00a48, 0, NULL, NULL }, - { 0x0232a, 0x0232a, 2, NULL, NULL }, - { 0x01b3c, 0x01b3c, 0, NULL, NULL }, - { 0x10a01, 0x10a03, 0, NULL, NULL }, - { 0x00ae2, 0x00ae3, 0, NULL, NULL }, - { 0x00483, 0x00486, 0, NULL, NULL }, - { 0x0135f, 0x0135f, 0, NULL, NULL }, - { 0x01a17, 0x01a18, 0, NULL, NULL }, - { 0x006e7, 0x006e8, 0, NULL, NULL }, -#ifndef __APPLE__ - { 0x03099, 0x0309a, 0, NULL, NULL }, -#endif - { 0x00b4d, 0x00b4d, 0, NULL, NULL }, - { 0x00ce2, 0x00ce3, 0, NULL, NULL }, - { 0x00bcd, 0x00bcd, 0, NULL, NULL }, - { 0x00610, 0x00615, 0, NULL, NULL }, - { 0x00f99, 0x00fbc, 0, NULL, NULL }, - { 0x009c1, 0x009c4, 0, NULL, NULL }, - { 0x00730, 0x0074a, 0, NULL, NULL }, - { 0x00300, 0x0036f, 0, NULL, NULL }, - { 0x03030, 0x0303e, 2, NULL, NULL }, - { 0x01b34, 0x01b34, 0, NULL, NULL }, - { 0x1d1aa, 0x1d1ad, 0, NULL, NULL }, - { 0x00dca, 0x00dca, 0, NULL, NULL }, - { 0x006d6, 0x006e4, 0, NULL, NULL }, - { 0x00f86, 0x00f87, 0, NULL, NULL }, - { 0x00b3f, 0x00b3f, 0, NULL, NULL }, - { 0x0fe30, 0x0fe6f, 2, NULL, NULL }, - { 0x01039, 0x01039, 0, NULL, NULL }, - { 0x0094d, 0x0094d, 0, NULL, NULL }, - { 0x00c55, 0x00c56, 0, NULL, NULL }, - { 0x00488, 0x00489, 0, NULL, NULL }, - { 0x00e47, 0x00e4e, 0, NULL, NULL }, - { 0x00a81, 0x00a82, 0, NULL, NULL }, - { 0x00ac1, 0x00ac5, 0, NULL, NULL }, - { 0x0202a, 0x0202e, 0, NULL, NULL }, - { 0x00dd6, 0x00dd6, 0, NULL, NULL }, - { 0x018a9, 0x018a9, 0, NULL, NULL }, - { 0x0064b, 0x0065e, 0, NULL, NULL }, - { 0x00abc, 0x00abc, 0, NULL, NULL }, - { 0x00b82, 0x00b82, 0, NULL, NULL }, - { 0x00f39, 0x00f39, 0, NULL, NULL }, - { 0x020d0, 0x020ef, 0, NULL, NULL }, - { 0x01dfe, 0x01dff, 0, NULL, NULL }, - { 0x30000, 0x3fffd, 2, NULL, NULL }, - { 0x00711, 0x00711, 0, NULL, NULL }, - { 0x0fe00, 0x0fe0f, 0, NULL, NULL }, - { 0x0180b, 0x0180d, 0, NULL, NULL }, - { 0x10a3f, 0x10a3f, 0, NULL, NULL }, - { 0x00981, 0x00981, 0, NULL, NULL }, - { 0x0a825, 0x0a826, 0, NULL, NULL }, - { 0x00941, 0x00948, 0, NULL, NULL }, - { 0x01b6b, 0x01b73, 0, NULL, NULL }, - { 0x00e31, 0x00e31, 0, NULL, NULL }, - { 0x0fe10, 0x0fe19, 2, NULL, NULL }, - { 0x00a01, 0x00a02, 0, NULL, NULL }, - { 0x00a4b, 0x00a4d, 0, NULL, NULL }, - { 0x00f18, 0x00f19, 0, NULL, NULL }, - { 0x00fc6, 0x00fc6, 0, NULL, NULL }, - { 0x02e80, 0x03029, 2, NULL, NULL }, - { 0x00b56, 0x00b56, 0, NULL, NULL }, - { 0x009bc, 0x009bc, 0, NULL, NULL }, - { 0x005c7, 0x005c7, 0, NULL, NULL }, - { 0x02060, 0x02063, 0, NULL, NULL }, - { 0x00c3e, 0x00c40, 0, NULL, NULL }, - { 0x10a05, 0x10a06, 0, NULL, NULL }, -}; - -struct utf8_width_entry *utf8_width_root = NULL; - -int utf8_overlap(struct utf8_width_entry *, struct utf8_width_entry *); -u_int utf8_combine(const struct utf8_data *); -u_int utf8_width(const struct utf8_data *); +static int utf8_width(wchar_t); /* Set a single character. */ void -utf8_set(struct utf8_data *utf8data, u_char ch) +utf8_set(struct utf8_data *ud, u_char ch) { - *utf8data->data = ch; - utf8data->size = 1; + u_int i; - utf8data->width = 1; + *ud->data = ch; + ud->have = 1; + ud->size = 1; + + ud->width = 1; + + for (i = ud->size; i < sizeof ud->data; i++) + ud->data[i] = '\0'; +} + +/* Copy UTF-8 character. */ +void +utf8_copy(struct utf8_data *to, const struct utf8_data *from) +{ + u_int i; + + memcpy(to, from, sizeof *to); + + for (i = to->size; i < sizeof to->data; i++) + to->data[i] = '\0'; } /* @@ -217,150 +60,97 @@ utf8_set(struct utf8_data *utf8data, u_char ch) * 11000010-11011111 C2-DF start of 2-byte sequence * 11100000-11101111 E0-EF start of 3-byte sequence * 11110000-11110100 F0-F4 start of 4-byte sequence - * - * Returns 1 if more UTF-8 to come, 0 if not UTF-8. */ -int -utf8_open(struct utf8_data *utf8data, u_char ch) +enum utf8_state +utf8_open(struct utf8_data *ud, u_char ch) { - memset(utf8data, 0, sizeof *utf8data); + memset(ud, 0, sizeof *ud); if (ch >= 0xc2 && ch <= 0xdf) - utf8data->size = 2; + ud->size = 2; else if (ch >= 0xe0 && ch <= 0xef) - utf8data->size = 3; + ud->size = 3; else if (ch >= 0xf0 && ch <= 0xf4) - utf8data->size = 4; + ud->size = 4; else - return (0); - utf8_append(utf8data, ch); - return (1); + return (UTF8_ERROR); + utf8_append(ud, ch); + return (UTF8_MORE); } -/* - * Append character to UTF-8, closing if finished. - * - * Returns 1 if more UTF-8 data to come, 0 if finished. - */ -int -utf8_append(struct utf8_data *utf8data, u_char ch) +/* Append character to UTF-8, closing if finished. */ +enum utf8_state +utf8_append(struct utf8_data *ud, u_char ch) { - if (utf8data->have >= utf8data->size) + wchar_t wc; + int width; + + if (ud->have >= ud->size) fatalx("UTF-8 character overflow"); - if (utf8data->size > sizeof utf8data->data) + if (ud->size > sizeof ud->data) fatalx("UTF-8 character size too large"); - utf8data->data[utf8data->have++] = ch; - if (utf8data->have != utf8data->size) - return (1); + if (ud->have != 0 && (ch & 0xc0) != 0x80) + ud->width = 0xff; - utf8data->width = utf8_width(utf8data); - return (0); + ud->data[ud->have++] = ch; + if (ud->have != ud->size) + return (UTF8_MORE); + + if (ud->width == 0xff) + return (UTF8_ERROR); + + if (utf8_combine(ud, &wc) != UTF8_DONE) + return (UTF8_ERROR); + if ((width = utf8_width(wc)) < 0) + return (UTF8_ERROR); + ud->width = width; + + return (UTF8_DONE); } -/* Check if two width tree entries overlap. */ -int -utf8_overlap(struct utf8_width_entry *item1, struct utf8_width_entry *item2) +/* Get width of Unicode character. */ +static int +utf8_width(wchar_t wc) { - if (item1->first >= item2->first && item1->first <= item2->last) - return (1); - if (item1->last >= item2->first && item1->last <= item2->last) - return (1); - if (item2->first >= item1->first && item2->first <= item1->last) - return (1); - if (item2->last >= item1->first && item2->last <= item1->last) - return (1); - return (0); + int width; + + width = wcwidth(wc); + if (width < 0 || width > 0xff) + return (-1); + return (width); } -/* Build UTF-8 width tree. */ -void -utf8_build(void) +/* Combine UTF-8 into Unicode. */ +enum utf8_state +utf8_combine(const struct utf8_data *ud, wchar_t *wc) { - struct utf8_width_entry **ptr, *item, *node; - u_int i, j; - - for (i = 0; i < nitems(utf8_width_table); i++) { - item = &utf8_width_table[i]; - - for (j = 0; j < nitems(utf8_width_table); j++) { - if (i != j && utf8_overlap(item, &utf8_width_table[j])) - log_fatalx("utf8 overlap: %u %u", i, j); - } - - ptr = &utf8_width_root; - while (*ptr != NULL) { - node = *ptr; - if (item->last < node->first) - ptr = &(node->left); - else if (item->first > node->last) - ptr = &(node->right); - } - *ptr = item; + switch (mbtowc(wc, ud->data, ud->size)) { + case -1: + mbtowc(NULL, NULL, MB_CUR_MAX); + return (UTF8_ERROR); + case 0: + return (UTF8_ERROR); + default: + return (UTF8_DONE); } } -/* Combine UTF-8 into 32-bit Unicode. */ -u_int -utf8_combine(const struct utf8_data *utf8data) +/* Split Unicode into UTF-8. */ +enum utf8_state +utf8_split(wchar_t wc, struct utf8_data *ud) { - u_int value; + char s[MB_LEN_MAX]; + int slen; - value = 0xff; - switch (utf8data->size) { - case 1: - value = utf8data->data[0]; - break; - case 2: - value = utf8data->data[1] & 0x3f; - value |= (utf8data->data[0] & 0x1f) << 6; - break; - case 3: - value = utf8data->data[2] & 0x3f; - value |= (utf8data->data[1] & 0x3f) << 6; - value |= (utf8data->data[0] & 0x0f) << 12; - break; - case 4: - value = utf8data->data[3] & 0x3f; - value |= (utf8data->data[2] & 0x3f) << 6; - value |= (utf8data->data[1] & 0x3f) << 12; - value |= (utf8data->data[0] & 0x07) << 18; - break; - } - return (value); -} + slen = wctomb(s, wc); + if (slen <= 0 || slen > (int)sizeof ud->data) + return (UTF8_ERROR); -/* Split a two-byte UTF-8 character. */ -u_int -utf8_split2(u_int uc, u_char *ptr) -{ - if (uc > 0x7f) { - ptr[0] = (uc >> 6) | 0xc0; - ptr[1] = (uc & 0x3f) | 0x80; - return (2); - } - ptr[0] = uc; - return (1); -} + memcpy(ud->data, s, slen); + ud->size = slen; -/* Lookup width of UTF-8 data in tree. */ -u_int -utf8_width(const struct utf8_data *utf8data) -{ - struct utf8_width_entry *item; - u_int value; - - value = utf8_combine(utf8data); - - item = utf8_width_root; - while (item != NULL) { - if (value < item->first) - item = item->left; - else if (value > item->last) - item = item->right; - else - return (item->width); - } - return (1); + ud->width = utf8_width(wc); + return (UTF8_DONE); } /* @@ -371,28 +161,26 @@ utf8_width(const struct utf8_data *utf8data) int utf8_strvis(char *dst, const char *src, size_t len, int flag) { - struct utf8_data utf8data; + struct utf8_data ud; const char *start, *end; - int more; + enum utf8_state more; size_t i; start = dst; end = src + len; while (src < end) { - if (utf8_open(&utf8data, *src)) { - more = 1; - while (++src < end && more) - more = utf8_append(&utf8data, *src); - if (!more) { + if ((more = utf8_open(&ud, *src)) == UTF8_MORE) { + while (++src < end && more == UTF8_MORE) + more = utf8_append(&ud, *src); + if (more == UTF8_DONE) { /* UTF-8 character finished. */ - for (i = 0; i < utf8data.size; i++) - *dst++ = utf8data.data[i]; + for (i = 0; i < ud.size; i++) + *dst++ = ud.data[i]; continue; - } else if (utf8data.have > 0) { - /* Not a complete UTF-8 character. */ - src -= utf8data.have; } + /* Not a complete, valid UTF-8 character. */ + src -= ud.have; } if (src < end - 1) dst = vis(dst, src[0], flag, src[1]); @@ -405,6 +193,49 @@ utf8_strvis(char *dst, const char *src, size_t len, int flag) return (dst - start); } +/* + * Sanitize a string, changing any UTF-8 characters to '_'. Caller should free + * the returned string. Anything not valid printable ASCII or UTF-8 is + * stripped. + */ +char * +utf8_sanitize(const char *src) +{ + char *dst; + size_t n; + enum utf8_state more; + struct utf8_data ud; + u_int i; + + dst = NULL; + + n = 0; + while (*src != '\0') { + dst = xreallocarray(dst, n + 1, sizeof *dst); + if ((more = utf8_open(&ud, *src)) == UTF8_MORE) { + while (*++src != '\0' && more == UTF8_MORE) + more = utf8_append(&ud, *src); + if (more == UTF8_DONE) { + dst = xreallocarray(dst, n + ud.width, + sizeof *dst); + for (i = 0; i < ud.width; i++) + dst[n++] = '_'; + continue; + } + src -= ud.have; + } + if (*src > 0x1f && *src < 0x7f) + dst[n++] = *src; + else + dst[n++] = '_'; + src++; + } + + dst = xreallocarray(dst, n + 1, sizeof *dst); + dst[n] = '\0'; + return (dst); +} + /* * Convert a string into a buffer of UTF-8 characters. Terminated by size == 0. * Caller frees. @@ -414,27 +245,25 @@ utf8_fromcstr(const char *src) { struct utf8_data *dst; size_t n; - int more; + enum utf8_state more; dst = NULL; n = 0; while (*src != '\0') { dst = xreallocarray(dst, n + 1, sizeof *dst); - if (utf8_open(&dst[n], *src)) { - more = 1; - while (*++src != '\0' && more) + if ((more = utf8_open(&dst[n], *src)) == UTF8_MORE) { + while (*++src != '\0' && more == UTF8_MORE) more = utf8_append(&dst[n], *src); - if (!more) { + if (more == UTF8_DONE) { n++; continue; } src -= dst[n].have; } utf8_set(&dst[n], *src); - src++; - n++; + src++; } dst = xreallocarray(dst, n + 1, sizeof *dst); @@ -469,21 +298,21 @@ utf8_cstrwidth(const char *s) { struct utf8_data tmp; u_int width; - int more; + enum utf8_state more; width = 0; while (*s != '\0') { - if (utf8_open(&tmp, *s)) { - more = 1; - while (*++s != '\0' && more) + if ((more = utf8_open(&tmp, *s)) == UTF8_MORE) { + while (*++s != '\0' && more == UTF8_MORE) more = utf8_append(&tmp, *s); - if (!more) { + if (more == UTF8_DONE) { width += tmp.width; continue; } s -= tmp.have; } - width++; + if (*s > 0x1f && *s != 0x7f) + width++; s++; } return (width); @@ -512,3 +341,61 @@ utf8_trimcstr(const char *s, u_int width) free(tmp); return (out); } + +/* Trim UTF-8 string to width. Caller frees. */ +char * +utf8_rtrimcstr(const char *s, u_int width) +{ + struct utf8_data *tmp, *next, *end; + char *out; + u_int at; + + tmp = utf8_fromcstr(s); + + for (end = tmp; end->size != 0; end++) + /* nothing */; + if (end == tmp) { + free(tmp); + return (xstrdup("")); + } + next = end - 1; + + at = 0; + for (;;) + { + if (at + next->width > width) { + next++; + break; + } + at += next->width; + + if (next == tmp) + break; + next--; + } + + out = utf8_tocstr(next); + free(tmp); + return (out); +} + +/* Pad UTF-8 string to width. Caller frees. */ +char * +utf8_padcstr(const char *s, u_int width) +{ + size_t slen; + char *out; + u_int n, i; + + n = utf8_cstrwidth(s); + if (n >= width) + return (xstrdup(s)); + + slen = strlen(s); + out = xmalloc(slen + 1 + (width - n)); + memcpy(out, s, slen); + for (i = n; i < width; i++) + out[slen++] = ' '; + out[slen] = '\0'; + return (out); +} diff --git a/window-choose.c b/window-choose.c index 69141676..7a727aac 100644 --- a/window-choose.c +++ b/window-choose.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -22,27 +22,29 @@ #include #include +#include "array.h" #include "tmux.h" struct screen *window_choose_init(struct window_pane *); void window_choose_free(struct window_pane *); void window_choose_resize(struct window_pane *, u_int, u_int); -void window_choose_key(struct window_pane *, struct session *, int); -void window_choose_mouse( - struct window_pane *, struct session *, struct mouse_event *); +void window_choose_key(struct window_pane *, struct client *, + struct session *, key_code, struct mouse_event *); void window_choose_default_callback(struct window_choose_data *); +struct window_choose_mode_item *window_choose_get_item(struct window_pane *, + key_code, struct mouse_event *); -void window_choose_fire_callback( - struct window_pane *, struct window_choose_data *); +void window_choose_fire_callback(struct window_pane *, + struct window_choose_data *); void window_choose_redraw_screen(struct window_pane *); -void window_choose_write_line( - struct window_pane *, struct screen_write_ctx *, u_int); +void window_choose_write_line(struct window_pane *, + struct screen_write_ctx *, u_int); void window_choose_scroll_up(struct window_pane *); void window_choose_scroll_down(struct window_pane *); -void window_choose_collapse(struct window_pane *, struct session *); +void window_choose_collapse(struct window_pane *, struct session *, u_int); void window_choose_expand(struct window_pane *, struct session *, u_int); enum window_choose_input_type { @@ -55,8 +57,14 @@ const struct window_mode window_choose_mode = { window_choose_free, window_choose_resize, window_choose_key, - window_choose_mouse, - NULL, +}; + +struct window_choose_mode_item { + struct window_choose_data *wcd; + char *name; + int pos; + int state; +#define TREE_EXPANDED 0x1 }; struct window_choose_mode_data { @@ -78,9 +86,9 @@ struct window_choose_mode_data { void window_choose_free1(struct window_choose_mode_data *); int window_choose_key_index(struct window_choose_mode_data *, u_int); -int window_choose_index_key(struct window_choose_mode_data *, int); +int window_choose_index_key(struct window_choose_mode_data *, key_code); void window_choose_prompt_input(enum window_choose_input_type, - const char *, struct window_pane *, int); + const char *, struct window_pane *, key_code); void window_choose_reset_top(struct window_pane *, u_int); void @@ -98,7 +106,7 @@ window_choose_add(struct window_pane *wp, struct window_choose_data *wcd) item->pos = ARRAY_LENGTH(&data->list) - 1; item->state = 0; - data->width = xsnprintf(tmp, sizeof tmp , "%u", item->pos); + data->width = xsnprintf(tmp, sizeof tmp , "%d", item->pos); } void @@ -160,10 +168,8 @@ window_choose_init(struct window_pane *wp) s = &data->screen; screen_init(s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0); s->mode &= ~MODE_CURSOR; - if (options_get_number(&wp->window->options, "mode-mouse")) - s->mode |= MODE_MOUSE_STANDARD; - keys = options_get_number(&wp->window->options, "mode-keys"); + keys = options_get_number(wp->window->options, "mode-keys"); if (keys == MODEKEY_EMACS) mode_key_init(&data->mdata, &mode_key_tree_emacs_choice); else @@ -180,7 +186,7 @@ window_choose_data_create(int type, struct client *c, struct session *s) wcd = xmalloc(sizeof *wcd); wcd->type = type; - wcd->ft = format_create(); + wcd->ft = format_create(NULL, 0); wcd->ft_template = NULL; wcd->command = NULL; @@ -202,11 +208,11 @@ window_choose_data_create(int type, struct client *c, struct session *s) void window_choose_data_free(struct window_choose_data *wcd) { - wcd->start_client->references--; - wcd->start_session->references--; + server_client_unref(wcd->start_client); + session_unref(wcd->start_session); if (wcd->tree_session != NULL) - wcd->tree_session->references--; + session_unref(wcd->tree_session); free(wcd->ft_template); format_free(wcd->ft); @@ -237,7 +243,7 @@ window_choose_data_run(struct window_choose_data *cdata) return; } - cmdq_run(cdata->start_client->cmdq, cmdlist); + cmdq_run(cdata->start_client->cmdq, cmdlist, NULL); cmd_list_free(cmdlist); } @@ -293,8 +299,8 @@ window_choose_resize(struct window_pane *wp, u_int sx, u_int sy) } void -window_choose_fire_callback( - struct window_pane *wp, struct window_choose_data *wcd) +window_choose_fire_callback(struct window_pane *wp, + struct window_choose_data *wcd) { struct window_choose_mode_data *data = wp->modedata; @@ -308,7 +314,7 @@ window_choose_fire_callback( void window_choose_prompt_input(enum window_choose_input_type input_type, - const char *prompt, struct window_pane *wp, int key) + const char *prompt, struct window_pane *wp, key_code key) { struct window_choose_mode_data *data = wp->modedata; size_t input_len; @@ -325,7 +331,7 @@ window_choose_prompt_input(enum window_choose_input_type input_type, } void -window_choose_collapse(struct window_pane *wp, struct session *s) +window_choose_collapse(struct window_pane *wp, struct session *s, u_int pos) { struct window_choose_mode_data *data = wp->modedata; struct window_choose_mode_item *item, *chosen; @@ -335,7 +341,7 @@ window_choose_collapse(struct window_pane *wp, struct session *s) ARRAY_DECL(, struct window_choose_mode_item) list_copy; ARRAY_INIT(&list_copy); - chosen = &ARRAY_ITEM(&data->list, data->selected); + chosen = &ARRAY_ITEM(&data->list, pos); chosen->state &= ~TREE_EXPANDED; /* @@ -383,7 +389,7 @@ window_choose_collapse_all(struct window_pane *wp) chosen = ARRAY_ITEM(&data->list, data->selected).wcd->start_session; RB_FOREACH(s, sessions, &sessions) - window_choose_collapse(wp, s); + window_choose_collapse(wp, s, data->selected); /* Reset the selection back to the starting session. */ for (i = 0; i < ARRAY_LENGTH(&data->list); i++) { @@ -483,8 +489,28 @@ window_choose_expand(struct window_pane *wp, struct session *s, u_int pos) } } +struct window_choose_mode_item * +window_choose_get_item(struct window_pane *wp, key_code key, + struct mouse_event *m) +{ + struct window_choose_mode_data *data = wp->modedata; + u_int x, y, idx; + + if (!KEYC_IS_MOUSE(key)) + return (&ARRAY_ITEM(&data->list, data->selected)); + + if (cmd_mouse_at(wp, m, &x, &y, 0) != 0) + return (NULL); + + idx = data->top + y; + if (idx >= ARRAY_LENGTH(&data->list)) + return (NULL); + return (&ARRAY_ITEM(&data->list, idx)); +} + void -window_choose_key(struct window_pane *wp, unused struct session *sess, int key) +window_choose_key(struct window_pane *wp, __unused struct client *c, + __unused struct session *sess, key_code key, struct mouse_event *m) { struct window_choose_mode_data *data = wp->modedata; struct screen *s = &data->screen; @@ -533,23 +559,28 @@ window_choose_key(struct window_pane *wp, unused struct session *sess, int key) window_choose_fire_callback(wp, NULL); break; case MODEKEYCHOICE_CHOOSE: - item = &ARRAY_ITEM(&data->list, data->selected); + if ((item = window_choose_get_item(wp, key, m)) == NULL) + break; window_choose_fire_callback(wp, item->wcd); break; case MODEKEYCHOICE_TREE_TOGGLE: - item = &ARRAY_ITEM(&data->list, data->selected); - if (item->state & TREE_EXPANDED) - window_choose_collapse(wp, item->wcd->tree_session); - else { + if ((item = window_choose_get_item(wp, key, m)) == NULL) + break; + if (item->state & TREE_EXPANDED) { + window_choose_collapse(wp, item->wcd->tree_session, + data->selected); + } else { window_choose_expand(wp, item->wcd->tree_session, data->selected); } window_choose_redraw_screen(wp); break; case MODEKEYCHOICE_TREE_COLLAPSE: - item = &ARRAY_ITEM(&data->list, data->selected); + if ((item = window_choose_get_item(wp, key, m)) == NULL) + break; if (item->state & TREE_EXPANDED) { - window_choose_collapse(wp, item->wcd->tree_session); + window_choose_collapse(wp, item->wcd->tree_session, + data->selected); window_choose_redraw_screen(wp); } break; @@ -557,7 +588,8 @@ window_choose_key(struct window_pane *wp, unused struct session *sess, int key) window_choose_collapse_all(wp); break; case MODEKEYCHOICE_TREE_EXPAND: - item = &ARRAY_ITEM(&data->list, data->selected); + if ((item = window_choose_get_item(wp, key, m)) == NULL) + break; if (!(item->state & TREE_EXPANDED)) { window_choose_expand(wp, item->wcd->tree_session, data->selected); @@ -582,10 +614,10 @@ window_choose_key(struct window_pane *wp, unused struct session *sess, int key) window_choose_scroll_up(wp); else { screen_write_start(&ctx, wp, NULL); - window_choose_write_line( - wp, &ctx, data->selected - data->top); - window_choose_write_line( - wp, &ctx, data->selected + 1 - data->top); + window_choose_write_line(wp, &ctx, + data->selected - data->top); + window_choose_write_line(wp, &ctx, + data->selected + 1 - data->top); screen_write_stop(&ctx); } break; @@ -602,10 +634,10 @@ window_choose_key(struct window_pane *wp, unused struct session *sess, int key) if (data->selected < data->top + screen_size_y(s)) { screen_write_start(&ctx, wp, NULL); - window_choose_write_line( - wp, &ctx, data->selected - data->top); - window_choose_write_line( - wp, &ctx, data->selected - 1 - data->top); + window_choose_write_line(wp, &ctx, + data->selected - data->top); + window_choose_write_line(wp, &ctx, + data->selected - 1 - data->top); screen_write_stop(&ctx); } else window_choose_scroll_down(wp); @@ -617,8 +649,8 @@ window_choose_key(struct window_pane *wp, unused struct session *sess, int key) data->selected--; window_choose_scroll_up(wp); screen_write_start(&ctx, wp, NULL); - window_choose_write_line( - wp, &ctx, screen_size_y(s) - 1); + window_choose_write_line(wp, &ctx, + screen_size_y(s) - 1); screen_write_stop(&ctx); } else window_choose_scroll_up(wp); @@ -712,65 +744,22 @@ window_choose_key(struct window_pane *wp, unused struct session *sess, int key) } void -window_choose_mouse(struct window_pane *wp, struct session *sess, - struct mouse_event *m) -{ - struct window_choose_mode_data *data = wp->modedata; - struct screen *s = &data->screen; - struct window_choose_mode_item *item; - u_int idx, i, n; - - if (m->event == MOUSE_EVENT_WHEEL) { - /* - * Multiple line scrolling by default is annoying, so scale - * m->scroll back down. - */ - n = m->scroll; - if (n >= MOUSE_WHEEL_SCALE) - n /= MOUSE_WHEEL_SCALE; - for (i = 0; i < n; i++) { - if (m->wheel == MOUSE_WHEEL_UP) - window_choose_key(wp, sess, KEYC_UP); - else - window_choose_key(wp, sess, KEYC_DOWN); - } - return; - } - - if (~m->event & MOUSE_EVENT_CLICK) - return; - if (m->x >= screen_size_x(s)) - return; - if (m->y >= screen_size_y(s)) - return; - - idx = data->top + m->y; - if (idx >= ARRAY_LENGTH(&data->list)) - return; - data->selected = idx; - - item = &ARRAY_ITEM(&data->list, data->selected); - window_choose_fire_callback(wp, item->wcd); -} - -void -window_choose_write_line( - struct window_pane *wp, struct screen_write_ctx *ctx, u_int py) +window_choose_write_line(struct window_pane *wp, struct screen_write_ctx *ctx, + u_int py) { struct window_choose_mode_data *data = wp->modedata; struct window_choose_mode_item *item; - struct options *oo = &wp->window->options; + struct options *oo = wp->window->options; struct screen *s = &data->screen; struct grid_cell gc; size_t last, xoff = 0; char hdr[32], label[32]; - int utf8flag, key; + int key; if (data->callbackfn == NULL) fatalx("called before callback assigned"); last = screen_size_y(s) - 1; - utf8flag = options_get_number(&wp->window->options, "utf8"); memcpy(&gc, &grid_default_cell, sizeof gc); if (data->selected == data->top + py) style_apply(&gc, oo, "mode-style"); @@ -787,7 +776,7 @@ window_choose_write_line( xsnprintf(label, sizeof label, "(%c)", key); else xsnprintf(label, sizeof label, "(%d)", item->pos); - screen_write_nputs(ctx, screen_size_x(s) - 1, &gc, utf8flag, + screen_write_nputs(ctx, screen_size_x(s) - 1, &gc, "%*s %s %s", data->width + 2, label, /* * Add indication to tree if necessary about whether it's @@ -832,7 +821,7 @@ window_choose_key_index(struct window_choose_mode_data *data, u_int idx) } int -window_choose_index_key(struct window_choose_mode_data *data, int key) +window_choose_index_key(struct window_choose_mode_data *data, key_code key) { static const char keys[] = "0123456789" "abcdefghijklmnopqrstuvwxyz" @@ -845,7 +834,7 @@ window_choose_index_key(struct window_choose_mode_data *data, int key) mkey = mode_key_lookup(&data->mdata, *ptr, NULL); if (mkey != MODEKEY_NONE && mkey != MODEKEY_OTHER) continue; - if (key == *ptr) + if (key == (key_code)*ptr) return (idx); idx++; } diff --git a/window-clock.c b/window-clock.c index ede8df5b..4cc58684 100644 --- a/window-clock.c +++ b/window-clock.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -27,9 +27,10 @@ struct screen *window_clock_init(struct window_pane *); void window_clock_free(struct window_pane *); void window_clock_resize(struct window_pane *, u_int, u_int); -void window_clock_key(struct window_pane *, struct session *, int); -void window_clock_timer(struct window_pane *); +void window_clock_key(struct window_pane *, struct client *, + struct session *, key_code, struct mouse_event *); +void window_clock_timer_callback(int, short, void *); void window_clock_draw_screen(struct window_pane *); const struct window_mode window_clock_mode = { @@ -37,13 +38,12 @@ const struct window_mode window_clock_mode = { window_clock_free, window_clock_resize, window_clock_key, - NULL, - window_clock_timer, }; struct window_clock_mode_data { struct screen screen; time_t tim; + struct event timer; }; const char window_clock_table[14][5][5] = { @@ -119,15 +119,42 @@ const char window_clock_table[14][5][5] = { { 1,0,0,0,1 } }, }; +void +window_clock_timer_callback(__unused int fd, __unused short events, void *arg) +{ + struct window_pane *wp = arg; + struct window_clock_mode_data *data = wp->modedata; + struct tm now, then; + time_t t; + struct timeval tv = { .tv_sec = 1 }; + + evtimer_del(&data->timer); + evtimer_add(&data->timer, &tv); + + t = time(NULL); + gmtime_r(&t, &now); + gmtime_r(&data->tim, &then); + if (now.tm_min == then.tm_min) + return; + data->tim = t; + + window_clock_draw_screen(wp); + server_redraw_window(wp->window); +} + struct screen * window_clock_init(struct window_pane *wp) { struct window_clock_mode_data *data; struct screen *s; + struct timeval tv = { .tv_sec = 1 }; wp->modedata = data = xmalloc(sizeof *data); data->tim = time(NULL); + evtimer_set(&data->timer, window_clock_timer_callback, wp); + evtimer_add(&data->timer, &tv); + s = &data->screen; screen_init(s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0); s->mode &= ~MODE_CURSOR; @@ -142,6 +169,7 @@ window_clock_free(struct window_pane *wp) { struct window_clock_mode_data *data = wp->modedata; + evtimer_del(&data->timer); screen_free(&data->screen); free(data); } @@ -157,30 +185,13 @@ window_clock_resize(struct window_pane *wp, u_int sx, u_int sy) } void -window_clock_key( - struct window_pane *wp, unused struct session *sess, unused int key) +window_clock_key(struct window_pane *wp, __unused struct client *c, + __unused struct session *sess, __unused key_code key, + __unused struct mouse_event *m) { window_pane_reset_mode(wp); } -void -window_clock_timer(struct window_pane *wp) -{ - struct window_clock_mode_data *data = wp->modedata; - struct tm now, then; - time_t t; - - t = time(NULL); - gmtime_r(&t, &now); - gmtime_r(&data->tim, &then); - if (now.tm_min == then.tm_min) - return; - data->tim = t; - - window_clock_draw_screen(wp); - server_redraw_window(wp->window); -} - void window_clock_draw_screen(struct window_pane *wp) { @@ -194,8 +205,8 @@ window_clock_draw_screen(struct window_pane *wp) struct tm *tm; u_int i, j, x, y, idx; - colour = options_get_number(&wp->window->options, "clock-mode-colour"); - style = options_get_number(&wp->window->options, "clock-mode-style"); + colour = options_get_number(wp->window->options, "clock-mode-colour"); + style = options_get_number(wp->window->options, "clock-mode-style"); screen_write_start(&ctx, NULL, s); @@ -223,7 +234,6 @@ window_clock_draw_screen(struct window_pane *wp) screen_write_puts(&ctx, &gc, "%s", tim); } - screen_write_stop(&ctx); return; } diff --git a/window-copy.c b/window-copy.c index 223df88a..939d3e19 100644 --- a/window-copy.c +++ b/window-copy.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -23,15 +23,16 @@ #include #include "tmux.h" +#include "tmate.h" struct screen *window_copy_init(struct window_pane *); void window_copy_free(struct window_pane *); +void window_copy_pagedown(struct window_pane *); void window_copy_resize(struct window_pane *, u_int, u_int); -void window_copy_key(struct window_pane *, struct session *, int); -int window_copy_key_input(struct window_pane *, int); -int window_copy_key_numeric_prefix(struct window_pane *, int); -void window_copy_mouse(struct window_pane *, struct session *, - struct mouse_event *); +void window_copy_key(struct window_pane *, struct client *, struct session *, + key_code, struct mouse_event *); +int window_copy_key_input(struct window_pane *, key_code); +int window_copy_key_numeric_prefix(struct window_pane *, key_code); void window_copy_redraw_selection(struct window_pane *, u_int); void window_copy_redraw_lines(struct window_pane *, u_int, u_int); @@ -76,86 +77,25 @@ void window_copy_cursor_up(struct window_pane *, int); void window_copy_cursor_down(struct window_pane *, int); void window_copy_cursor_jump(struct window_pane *); void window_copy_cursor_jump_back(struct window_pane *); -void window_copy_cursor_jump_to(struct window_pane *); -void window_copy_cursor_jump_to_back(struct window_pane *); +void window_copy_cursor_jump_to(struct window_pane *, int); +void window_copy_cursor_jump_to_back(struct window_pane *, int); void window_copy_cursor_next_word(struct window_pane *, const char *); void window_copy_cursor_next_word_end(struct window_pane *, const char *); void window_copy_cursor_previous_word(struct window_pane *, const char *); void window_copy_scroll_up(struct window_pane *, u_int); void window_copy_scroll_down(struct window_pane *, u_int); void window_copy_rectangle_toggle(struct window_pane *); +void window_copy_drag_update(struct client *, struct mouse_event *); +void window_copy_drag_release(struct client *, struct mouse_event *); const struct window_mode window_copy_mode = { window_copy_init, window_copy_free, window_copy_resize, window_copy_key, - window_copy_mouse, - NULL, }; -enum window_copy_input_type { - WINDOW_COPY_OFF, - WINDOW_COPY_NAMEDBUFFER, - WINDOW_COPY_NUMERICPREFIX, - WINDOW_COPY_SEARCHUP, - WINDOW_COPY_SEARCHDOWN, - WINDOW_COPY_JUMPFORWARD, - WINDOW_COPY_JUMPBACK, - WINDOW_COPY_JUMPTOFORWARD, - WINDOW_COPY_JUMPTOBACK, - WINDOW_COPY_GOTOLINE, -}; - -/* - * Copy-mode's visible screen (the "screen" field) is filled from one of - * two sources: the original contents of the pane (used when we - * actually enter via the "copy-mode" command, to copy the contents of - * the current pane), or else a series of lines containing the output - * from an output-writing tmux command (such as any of the "show-*" or - * "list-*" commands). - * - * In either case, the full content of the copy-mode grid is pointed at - * by the "backing" field, and is copied into "screen" as needed (that - * is, when scrolling occurs). When copy-mode is backed by a pane, - * backing points directly at that pane's screen structure (&wp->base); - * when backed by a list of output-lines from a command, it points at - * a newly-allocated screen structure (which is deallocated when the - * mode ends). - */ -struct window_copy_mode_data { - struct screen screen; - - struct screen *backing; - int backing_written; /* backing display has started */ - - struct mode_key_data mdata; - - u_int oy; - - u_int selx; - u_int sely; - - u_int rectflag; /* are we in rectangle copy mode? */ - - u_int cx; - u_int cy; - - u_int lastcx; /* position in last line with content */ - u_int lastsx; /* size of last line with content */ - - enum window_copy_input_type inputtype; - const char *inputprompt; - char *inputstr; - - int numprefix; - - enum window_copy_input_type searchtype; - char *searchstr; - - enum window_copy_input_type jumptype; - char jumpchar; -}; +#include "window-copy.h" struct screen * window_copy_init(struct window_pane *wp) @@ -175,6 +115,7 @@ window_copy_init(struct window_pane *wp) data->backing_written = 0; data->rectflag = 0; + data->scroll_exit = 0; data->inputtype = WINDOW_COPY_OFF; data->inputprompt = NULL; @@ -192,10 +133,8 @@ window_copy_init(struct window_pane *wp) s = &data->screen; screen_init(s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0); - if (options_get_number(&wp->window->options, "mode-mouse")) - s->mode |= MODE_MOUSE_STANDARD; - keys = options_get_number(&wp->window->options, "mode-keys"); + keys = options_get_number(wp->window->options, "mode-keys"); if (keys == MODEKEY_EMACS) mode_key_init(&data->mdata, &mode_key_tree_emacs_copy); else @@ -204,11 +143,15 @@ window_copy_init(struct window_pane *wp) data->backing = NULL; +#ifdef TMATE + data->password_cb = NULL; +#endif + return (s); } void -window_copy_init_from_pane(struct window_pane *wp) +window_copy_init_from_pane(struct window_pane *wp, int scroll_exit) { struct window_copy_mode_data *data = wp->modedata; struct screen *s = &data->screen; @@ -221,6 +164,7 @@ window_copy_init_from_pane(struct window_pane *wp) data->backing = &wp->base; data->cx = data->backing->cx; data->cy = data->backing->cy; + data->scroll_exit = scroll_exit; s->cx = data->cx; s->cy = data->cy; @@ -230,6 +174,10 @@ window_copy_init_from_pane(struct window_pane *wp) window_copy_write_line(wp, &ctx, i); screen_write_cursormove(&ctx, data->cx, data->cy); screen_write_stop(&ctx); + +#ifdef TMATE + tmate_sync_copy_mode(wp); +#endif } void @@ -279,13 +227,15 @@ window_copy_vadd(struct window_pane *wp, const char *fmt, va_list ap) struct screen *backing = data->backing; struct screen_write_ctx back_ctx, ctx; struct grid_cell gc; - int utf8flag; u_int old_hsize, old_cy; +#ifdef TMATE + char *msg; +#endif + if (backing == &wp->base) return; - utf8flag = options_get_number(&wp->window->options, "utf8"); memcpy(&gc, &grid_default_cell, sizeof gc); old_hsize = screen_hsize(data->backing); @@ -300,7 +250,14 @@ window_copy_vadd(struct window_pane *wp, const char *fmt, va_list ap) } else data->backing_written = 1; old_cy = backing->cy; - screen_write_vnputs(&back_ctx, 0, &gc, utf8flag, fmt, ap); +#ifdef TMATE + xvasprintf(&msg, fmt, ap); + screen_write_nputs(&back_ctx, 0, &gc, "%s", msg); + tmate_write_copy_mode(wp, msg); + free(msg); +#else + screen_write_vnputs(&back_ctx, 0, &gc, fmt, ap); +#endif screen_write_stop(&back_ctx); data->oy += screen_hsize(data->backing) - old_hsize; @@ -325,15 +282,80 @@ window_copy_pageup(struct window_pane *wp) { struct window_copy_mode_data *data = wp->modedata; struct screen *s = &data->screen; - u_int n; + u_int n, ox, oy; + + oy = screen_hsize(data->backing) + data->cy - data->oy; + ox = window_copy_find_length(wp, oy); + + if (s->sel.lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely) + window_copy_other_end(wp); + + if (data->cx != ox) { + data->lastcx = data->cx; + data->lastsx = ox; + } + data->cx = data->lastcx; n = 1; if (screen_size_y(s) > 2) n = screen_size_y(s) - 2; + if (data->oy + n > screen_hsize(data->backing)) data->oy = screen_hsize(data->backing); else data->oy += n; + + if (!data->screen.sel.flag || !data->rectflag) { + u_int py = screen_hsize(data->backing) + data->cy - data->oy; + u_int px = window_copy_find_length(wp, py); + if ((data->cx >= data->lastsx && data->cx != px) || data->cx > px) + window_copy_cursor_end_of_line(wp); + } + + window_copy_update_selection(wp, 1); + window_copy_redraw_screen(wp); +} + +void +window_copy_pagedown(struct window_pane *wp) +{ + struct window_copy_mode_data *data = wp->modedata; + struct screen *s = &data->screen; + u_int n, ox, oy; + + oy = screen_hsize(data->backing) + data->cy - data->oy; + ox = window_copy_find_length(wp, oy); + + if (s->sel.lineflag == LINE_SEL_RIGHT_LEFT && oy == data->sely) + window_copy_other_end(wp); + + if (data->cx != ox) { + data->lastcx = data->cx; + data->lastsx = ox; + } + data->cx = data->lastcx; + + n = 1; + if (screen_size_y(s) > 2) + n = screen_size_y(s) - 2; + + if (data->oy < n) + data->oy = 0; + else + data->oy -= n; + + if (!data->screen.sel.flag || !data->rectflag) { + u_int py = screen_hsize(data->backing) + data->cy - data->oy; + u_int px = window_copy_find_length(wp, py); + if ((data->cx >= data->lastsx && data->cx != px) || data->cx > px) + window_copy_cursor_end_of_line(wp); + } + + if (data->scroll_exit && data->oy == 0) { + window_pane_reset_mode(wp); + return; + } + window_copy_update_selection(wp, 1); window_copy_redraw_screen(wp); } @@ -365,20 +387,21 @@ window_copy_resize(struct window_pane *wp, u_int sx, u_int sy) window_copy_redraw_screen(wp); } -void -window_copy_key(struct window_pane *wp, struct session *sess, int key) +static void +__window_copy_key(struct window_pane *wp, struct client *c, struct session *sess, + key_code key, struct mouse_event *m) { const char *word_separators; struct window_copy_mode_data *data = wp->modedata; struct screen *s = &data->screen; - u_int n; - int np, keys; + u_int n, np; + int keys; enum mode_key_cmd cmd; const char *arg, *ss; - np = data->numprefix; - if (np <= 0) - np = 1; + np = 1; + if (data->numprefix > 0) + np = data->numprefix; if (data->inputtype == WINDOW_COPY_JUMPFORWARD || data->inputtype == WINDOW_COPY_JUMPBACK || @@ -397,11 +420,11 @@ window_copy_key(struct window_pane *wp, struct session *sess, int key) } if (data->inputtype == WINDOW_COPY_JUMPTOFORWARD) { for (; np != 0; np--) - window_copy_cursor_jump_to(wp); + window_copy_cursor_jump_to(wp, 0); } if (data->inputtype == WINDOW_COPY_JUMPTOBACK) { for (; np != 0; np--) - window_copy_cursor_jump_to_back(wp); + window_copy_cursor_jump_to_back(wp, 0); } } data->jumptype = data->inputtype; @@ -420,12 +443,23 @@ window_copy_key(struct window_pane *wp, struct session *sess, int key) } cmd = mode_key_lookup(&data->mdata, key, &arg); + if (cmd != MODEKEYCOPY_PREVIOUSPAGE && + cmd != MODEKEYCOPY_NEXTPAGE && + cmd != MODEKEYCOPY_SCROLLUP && + cmd != MODEKEYCOPY_SCROLLDOWN && + cmd != MODEKEYCOPY_HALFPAGEUP && + cmd != MODEKEYCOPY_HALFPAGEDOWN) + data->scroll_exit = 0; switch (cmd) { case MODEKEYCOPY_APPENDSELECTION: if (sess != NULL) { window_copy_append_selection(wp, NULL); - window_pane_reset_mode(wp); - return; + if (arg == NULL) { + window_pane_reset_mode(wp); + return; + } + window_copy_clear_selection(wp); + window_copy_redraw_screen(wp); } break; case MODEKEYCOPY_CANCEL: @@ -458,23 +492,18 @@ window_copy_key(struct window_pane *wp, struct session *sess, int key) case MODEKEYCOPY_SCROLLDOWN: for (; np != 0; np--) window_copy_cursor_down(wp, 1); + if (data->scroll_exit && data->oy == 0) { + window_pane_reset_mode(wp); + return; + } break; case MODEKEYCOPY_PREVIOUSPAGE: for (; np != 0; np--) window_copy_pageup(wp); break; case MODEKEYCOPY_NEXTPAGE: - n = 1; - if (screen_size_y(s) > 2) - n = screen_size_y(s) - 2; - for (; np != 0; np--) { - if (data->oy < n) - data->oy = 0; - else - data->oy -= n; - } - window_copy_update_selection(wp, 1); - window_copy_redraw_screen(wp); + for (; np != 0; np--) + window_copy_pagedown(wp); break; case MODEKEYCOPY_HALFPAGEUP: n = screen_size_y(s) / 2; @@ -495,6 +524,10 @@ window_copy_key(struct window_pane *wp, struct session *sess, int key) else data->oy -= n; } + if (data->scroll_exit && data->oy == 0) { + window_pane_reset_mode(wp); + return; + } window_copy_update_selection(wp, 1); window_copy_redraw_screen(wp); break; @@ -531,9 +564,14 @@ window_copy_key(struct window_pane *wp, struct session *sess, int key) window_copy_redraw_screen(wp); break; case MODEKEYCOPY_STARTSELECTION: - s->sel.lineflag = LINE_SEL_NONE; - window_copy_start_selection(wp); - window_copy_redraw_screen(wp); + if (KEYC_IS_MOUSE(key)) { + if (c != NULL) + window_copy_start_drag(c, m); + } else { + s->sel.lineflag = LINE_SEL_NONE; + window_copy_start_selection(wp); + window_copy_redraw_screen(wp); + } break; case MODEKEYCOPY_SELECTLINE: s->sel.lineflag = LINE_SEL_LEFT_RIGHT; @@ -572,8 +610,12 @@ window_copy_key(struct window_pane *wp, struct session *sess, int key) case MODEKEYCOPY_COPYSELECTION: if (sess != NULL) { window_copy_copy_selection(wp, NULL); - window_pane_reset_mode(wp); - return; + if (arg == NULL) { + window_pane_reset_mode(wp); + return; + } + window_copy_clear_selection(wp); + window_copy_redraw_screen(wp); } break; case MODEKEYCOPY_STARTOFLINE: @@ -595,13 +637,13 @@ window_copy_key(struct window_pane *wp, struct session *sess, int key) break; case MODEKEYCOPY_NEXTWORD: word_separators = - options_get_string(&sess->options, "word-separators"); + options_get_string(sess->options, "word-separators"); for (; np != 0; np--) window_copy_cursor_next_word(wp, word_separators); break; case MODEKEYCOPY_NEXTWORDEND: word_separators = - options_get_string(&sess->options, "word-separators"); + options_get_string(sess->options, "word-separators"); for (; np != 0; np--) window_copy_cursor_next_word_end(wp, word_separators); break; @@ -611,7 +653,7 @@ window_copy_key(struct window_pane *wp, struct session *sess, int key) break; case MODEKEYCOPY_PREVIOUSWORD: word_separators = - options_get_string(&sess->options, "word-separators"); + options_get_string(sess->options, "word-separators"); for (; np != 0; np--) window_copy_cursor_previous_word(wp, word_separators); break; @@ -630,10 +672,10 @@ window_copy_key(struct window_pane *wp, struct session *sess, int key) window_copy_cursor_jump_back(wp); } else if (data->jumptype == WINDOW_COPY_JUMPTOFORWARD) { for (; np != 0; np--) - window_copy_cursor_jump_to(wp); + window_copy_cursor_jump_to(wp, 1); } else if (data->jumptype == WINDOW_COPY_JUMPTOBACK) { for (; np != 0; np--) - window_copy_cursor_jump_to_back(wp); + window_copy_cursor_jump_to_back(wp, 1); } break; case MODEKEYCOPY_JUMPREVERSE: @@ -645,10 +687,10 @@ window_copy_key(struct window_pane *wp, struct session *sess, int key) window_copy_cursor_jump(wp); } else if (data->jumptype == WINDOW_COPY_JUMPTOFORWARD) { for (; np != 0; np--) - window_copy_cursor_jump_to_back(wp); + window_copy_cursor_jump_to_back(wp, 1); } else if (data->jumptype == WINDOW_COPY_JUMPTOBACK) { for (; np != 0; np--) - window_copy_cursor_jump_to(wp); + window_copy_cursor_jump_to(wp, 1); } break; case MODEKEYCOPY_JUMPBACK: @@ -680,6 +722,10 @@ window_copy_key(struct window_pane *wp, struct session *sess, int key) case MODEKEYCOPY_SEARCHAGAIN: case MODEKEYCOPY_SEARCHREVERSE: switch (data->searchtype) { +#ifdef TMATE + case WINDOW_COPY_PASSWORD: + break; +#endif case WINDOW_COPY_OFF: case WINDOW_COPY_GOTOLINE: case WINDOW_COPY_JUMPFORWARD: @@ -688,7 +734,6 @@ window_copy_key(struct window_pane *wp, struct session *sess, int key) case WINDOW_COPY_JUMPTOBACK: case WINDOW_COPY_NAMEDBUFFER: case WINDOW_COPY_NUMERICPREFIX: - break; case WINDOW_COPY_SEARCHUP: ss = data->searchstr; if (cmd == MODEKEYCOPY_SEARCHAGAIN) { @@ -718,6 +763,7 @@ window_copy_key(struct window_pane *wp, struct session *sess, int key) goto input_on; case MODEKEYCOPY_STARTNAMEDBUFFER: data->inputtype = WINDOW_COPY_NAMEDBUFFER; + data->inputexit = (arg == NULL); data->inputprompt = "Buffer"; *data->inputstr = '\0'; goto input_on; @@ -742,7 +788,7 @@ window_copy_key(struct window_pane *wp, struct session *sess, int key) return; input_on: - keys = options_get_number(&wp->window->options, "mode-keys"); + keys = options_get_number(wp->window->options, "mode-keys"); if (keys == MODEKEY_EMACS) mode_key_init(&data->mdata, &mode_key_tree_emacs_edit); else @@ -752,7 +798,7 @@ input_on: return; input_off: - keys = options_get_number(&wp->window->options, "mode-keys"); + keys = options_get_number(wp->window->options, "mode-keys"); if (keys == MODEKEY_EMACS) mode_key_init(&data->mdata, &mode_key_tree_emacs_copy); else @@ -764,12 +810,23 @@ input_off: window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1); } +void +window_copy_key(struct window_pane *wp, struct client *c, struct session *sess, + key_code key, struct mouse_event *m) +{ + __window_copy_key(wp, c, sess, key, m); +#ifdef TMATE + tmate_sync_copy_mode(wp); +#endif +} + int -window_copy_key_input(struct window_pane *wp, int key) +window_copy_key_input(struct window_pane *wp, key_code key) { struct window_copy_mode_data *data = wp->modedata; struct screen *s = &data->screen; - size_t inputlen, n; + const char *bufdata; + size_t inputlen, n, bufsize; int np; struct paste_buffer *pb; u_char ch; @@ -787,17 +844,18 @@ window_copy_key_input(struct window_pane *wp, int key) *data->inputstr = '\0'; break; case MODEKEYEDIT_PASTE: - if ((pb = paste_get_top()) == NULL) + if ((pb = paste_get_top(NULL)) == NULL) break; - for (n = 0; n < pb->size; n++) { - ch = (u_char) pb->data[n]; + bufdata = paste_buffer_data(pb, &bufsize); + for (n = 0; n < bufsize; n++) { + ch = (u_char)bufdata[n]; if (ch < 32 || ch == 127) break; } inputlen = strlen(data->inputstr); data->inputstr = xrealloc(data->inputstr, inputlen + n + 1); - memcpy(data->inputstr + inputlen, pb->data, n); + memcpy(data->inputstr + inputlen, bufdata, n); data->inputstr[inputlen + n] = '\0'; break; case MODEKEYEDIT_ENTER: @@ -828,12 +886,26 @@ window_copy_key_input(struct window_pane *wp, int key) case WINDOW_COPY_NAMEDBUFFER: window_copy_copy_selection(wp, data->inputstr); *data->inputstr = '\0'; - window_pane_reset_mode(wp); - return (0); + if (data->inputexit) { + window_pane_reset_mode(wp); + return (0); + } + window_copy_clear_selection(wp); + window_copy_redraw_screen(wp); + break; case WINDOW_COPY_GOTOLINE: window_copy_goto_line(wp, data->inputstr); *data->inputstr = '\0'; break; +#ifdef TMATE + case WINDOW_COPY_PASSWORD: + if (data->password_cb) { + data->password_cb(data->inputstr, + data->password_cb_private); + } + window_pane_reset_mode(wp); + return 0; +#endif } data->numprefix = -1; return (1); @@ -855,7 +927,7 @@ window_copy_key_input(struct window_pane *wp, int key) } int -window_copy_key_numeric_prefix(struct window_pane *wp, int key) +window_copy_key_numeric_prefix(struct window_pane *wp, key_code key) { struct window_copy_mode_data *data = wp->modedata; struct screen *s = &data->screen; @@ -872,75 +944,6 @@ window_copy_key_numeric_prefix(struct window_pane *wp, int key) return (0); } -void -window_copy_mouse(struct window_pane *wp, struct session *sess, - struct mouse_event *m) -{ - struct window_copy_mode_data *data = wp->modedata; - struct screen *s = &data->screen; - u_int i, old_cy; - - if (m->x >= screen_size_x(s)) - return; - if (m->y >= screen_size_y(s)) - return; - - /* If mouse wheel (buttons 4 and 5), scroll. */ - if (m->event == MOUSE_EVENT_WHEEL) { - for (i = 0; i < m->scroll; i++) { - if (m->wheel == MOUSE_WHEEL_UP) - window_copy_cursor_up(wp, 1); - else { - window_copy_cursor_down(wp, 1); - - /* - * We reached the bottom, leave copy mode, but - * only if no selection is in progress. - */ - if (data->oy == 0 && !s->sel.flag && - s->sel.lineflag == LINE_SEL_NONE) - goto reset_mode; - } - } - return; - } - - /* - * If already reading motion, move the cursor while buttons are still - * pressed, or stop the selection on their release. - */ - if (s->mode & MODE_MOUSE_BUTTON) { - if (~m->event & MOUSE_EVENT_UP) { - old_cy = data->cy; - window_copy_update_cursor(wp, m->x, m->y); - if (window_copy_update_selection(wp, 1)) - window_copy_redraw_selection(wp, old_cy); - return; - } - goto reset_mode; - } - - /* Otherwise if other buttons pressed, start selection and motion. */ - if (~m->event & MOUSE_EVENT_UP) { - s->mode &= ~MODE_MOUSE_STANDARD; - s->mode |= MODE_MOUSE_BUTTON; - - window_copy_update_cursor(wp, m->x, m->y); - window_copy_start_selection(wp); - window_copy_redraw_screen(wp); - } - - return; - -reset_mode: - s->mode &= ~MODE_MOUSE_BUTTON; - s->mode |= MODE_MOUSE_STANDARD; - if (sess != NULL) { - window_copy_copy_selection(wp, NULL); - window_pane_reset_mode(wp); - } -} - void window_copy_scroll_to(struct window_pane *wp, u_int px, u_int py) { @@ -971,21 +974,21 @@ int window_copy_search_compare(struct grid *gd, u_int px, u_int py, struct grid *sgd, u_int spx, int cis) { - const struct grid_cell *gc, *sgc; - struct utf8_data ud, sud; + struct grid_cell gc, sgc; + const struct utf8_data *ud, *sud; - gc = grid_peek_cell(gd, px, py); - grid_cell_get(gc, &ud); - sgc = grid_peek_cell(sgd, spx, 0); - grid_cell_get(sgc, &sud); + grid_get_cell(gd, px, py, &gc); + ud = &gc.data; + grid_get_cell(sgd, spx, 0, &sgc); + sud = &sgc.data; - if (ud.size != sud.size || ud.width != sud.width) + if (ud->size != sud->size || ud->width != sud->width) return (0); - if (cis && ud.size == 1) - return (tolower(ud.data[0]) == sud.data[0]); + if (cis && ud->size == 1) + return (tolower(ud->data[0]) == sud->data[0]); - return (memcmp(ud.data, sud.data, ud.size) == 0); + return (memcmp(ud->data, sud->data, ud->size) == 0); } int @@ -1048,19 +1051,23 @@ window_copy_search_up(struct window_pane *wp, const char *searchstr) struct grid_cell gc; size_t searchlen; u_int i, last, fx, fy, px; - int utf8flag, n, wrapped, wrapflag, cis; + int n, wrapped, wrapflag, cis; const char *ptr; +#ifdef TMATE + if (!searchstr) + return; +#endif + if (*searchstr == '\0') return; - utf8flag = options_get_number(&wp->window->options, "utf8"); - wrapflag = options_get_number(&wp->window->options, "wrap-search"); - searchlen = screen_write_strlen(utf8flag, "%s", searchstr); + wrapflag = options_get_number(wp->window->options, "wrap-search"); + searchlen = screen_write_strlen("%s", searchstr); screen_init(&ss, searchlen, 1, 0); screen_write_start(&ctx, NULL, &ss); memcpy(&gc, &grid_default_cell, sizeof gc); - screen_write_nputs(&ctx, -1, &gc, utf8flag, "%s", searchstr); + screen_write_nputs(&ctx, -1, &gc, "%s", searchstr); screen_write_stop(&ctx); fx = data->cx; @@ -1115,19 +1122,23 @@ window_copy_search_down(struct window_pane *wp, const char *searchstr) struct grid_cell gc; size_t searchlen; u_int i, first, fx, fy, px; - int utf8flag, n, wrapped, wrapflag, cis; + int n, wrapped, wrapflag, cis; const char *ptr; +#ifdef TMATE + if (!searchstr) + return; +#endif + if (*searchstr == '\0') return; - utf8flag = options_get_number(&wp->window->options, "utf8"); - wrapflag = options_get_number(&wp->window->options, "wrap-search"); - searchlen = screen_write_strlen(utf8flag, "%s", searchstr); + wrapflag = options_get_number(wp->window->options, "wrap-search"); + searchlen = screen_write_strlen("%s", searchstr); screen_init(&ss, searchlen, 1, 0); screen_write_start(&ctx, NULL, &ss); memcpy(&gc, &grid_default_cell, sizeof gc); - screen_write_nputs(&ctx, -1, &gc, utf8flag, "%s", searchstr); + screen_write_nputs(&ctx, -1, &gc, "%s", searchstr); screen_write_stop(&ctx); fx = data->cx; @@ -1195,7 +1206,7 @@ window_copy_write_line(struct window_pane *wp, struct screen_write_ctx *ctx, { struct window_copy_mode_data *data = wp->modedata; struct screen *s = &data->screen; - struct options *oo = &wp->window->options; + struct options *oo = wp->window->options; struct grid_cell gc; char hdr[512]; size_t last, xoff = 0, size = 0, limit; @@ -1204,20 +1215,37 @@ window_copy_write_line(struct window_pane *wp, struct screen_write_ctx *ctx, last = screen_size_y(s) - 1; if (py == 0) { +#ifdef TMATE + if (data->inputtype != WINDOW_COPY_PASSWORD) { +#endif size = xsnprintf(hdr, sizeof hdr, "[%u/%u]", data->oy, screen_hsize(data->backing)); if (size > screen_size_x(s)) size = screen_size_x(s); screen_write_cursormove(ctx, screen_size_x(s) - size, 0); screen_write_puts(ctx, &gc, "%s", hdr); +#ifdef TMATE + } +#endif } else if (py == last && data->inputtype != WINDOW_COPY_OFF) { limit = sizeof hdr; if (limit > screen_size_x(s) + 1) limit = screen_size_x(s) + 1; if (data->inputtype == WINDOW_COPY_NUMERICPREFIX) { xoff = size = xsnprintf(hdr, limit, - "Repeat: %u", data->numprefix); + "Repeat: %d", data->numprefix); } else { +#ifdef TMATE + if (data->inputtype == WINDOW_COPY_PASSWORD) { + int password_len = strlen(data->inputstr); + xoff = size = xsnprintf(hdr, sizeof hdr, "%s: ", data->inputprompt); + memset(hdr+xoff, '*', password_len); + xoff += password_len; + size += password_len; + hdr[xoff] = '\0'; + } + else +#endif xoff = size = xsnprintf(hdr, limit, "%s: %s", data->inputprompt, data->inputstr); } @@ -1328,7 +1356,7 @@ window_copy_update_selection(struct window_pane *wp, int may_redraw) { struct window_copy_mode_data *data = wp->modedata; struct screen *s = &data->screen; - struct options *oo = &wp->window->options; + struct options *oo = wp->window->options; struct grid_cell gc; u_int sx, sy, ty, cy; @@ -1428,7 +1456,7 @@ window_copy_get_selection(struct window_pane *wp, size_t *len) * bottom-right-most, regardless of copy direction. If it is vi, also * keep bottom-right-most character. */ - keys = options_get_number(&wp->window->options, "mode-keys"); + keys = options_get_number(wp->window->options, "mode-keys"); if (data->rectflag) { /* * Need to ignore the column with the cursor in it, which for @@ -1487,7 +1515,7 @@ window_copy_copy_buffer(struct window_pane *wp, const char *bufname, void *buf, { struct screen_write_ctx ctx; - if (options_get_number(&global_options, "set-clipboard")) { + if (options_get_number(global_options, "set-clipboard")) { screen_write_start(&ctx, wp, NULL); screen_write_setselection(&ctx, buf, len); screen_write_stop(&ctx); @@ -1511,11 +1539,11 @@ window_copy_copy_pipe(struct window_pane *wp, struct session *sess, if (buf == NULL) return; - ft = format_create(); + ft = format_create(NULL, 0); format_defaults(ft, NULL, sess, NULL, wp); expanded = format_expand(ft, arg); - job = job_run(expanded, sess, NULL, NULL, NULL); + job = job_run(expanded, sess, NULL, NULL, NULL, NULL); bufferevent_write(job->event, buf, len); free(expanded); @@ -1542,42 +1570,42 @@ window_copy_append_selection(struct window_pane *wp, const char *bufname) { char *buf; struct paste_buffer *pb; - size_t len; + const char *bufdata; + size_t len, bufsize; struct screen_write_ctx ctx; buf = window_copy_get_selection(wp, &len); if (buf == NULL) return; - if (options_get_number(&global_options, "set-clipboard")) { + if (options_get_number(global_options, "set-clipboard")) { screen_write_start(&ctx, wp, NULL); screen_write_setselection(&ctx, buf, len); screen_write_stop(&ctx); } - if (bufname == NULL || *bufname == '\0') { - pb = paste_get_top(); - if (pb != NULL) - bufname = pb->name; - } else + if (bufname == NULL || *bufname == '\0') + pb = paste_get_top(&bufname); + else pb = paste_get_name(bufname); if (pb != NULL) { - buf = xrealloc(buf, len + pb->size); - memmove(buf + pb->size, buf, len); - memcpy(buf, pb->data, pb->size); - len += pb->size; + bufdata = paste_buffer_data(pb, &bufsize); + buf = xrealloc(buf, len + bufsize); + memmove(buf + bufsize, buf, len); + memcpy(buf, bufdata, bufsize); + len += bufsize; } if (paste_set(buf, len, bufname, NULL) != 0) free(buf); } void -window_copy_copy_line(struct window_pane *wp, - char **buf, size_t *off, u_int sy, u_int sx, u_int ex) +window_copy_copy_line(struct window_pane *wp, char **buf, size_t *off, u_int sy, + u_int sx, u_int ex) { struct window_copy_mode_data *data = wp->modedata; struct grid *gd = data->backing->grid; - const struct grid_cell *gc; + struct grid_cell gc; struct grid_line *gl; struct utf8_data ud; u_int i, xx, wrapped = 0; @@ -1606,11 +1634,11 @@ window_copy_copy_line(struct window_pane *wp, if (sx < ex) { for (i = sx; i < ex; i++) { - gc = grid_peek_cell(gd, i, sy); - if (gc->flags & GRID_FLAG_PADDING) + grid_get_cell(gd, i, sy, &gc); + if (gc.flags & GRID_FLAG_PADDING) continue; - grid_cell_get(gc, &ud); - if (ud.size == 1 && (gc->attr & GRID_ATTR_CHARSET)) { + utf8_copy(&ud, &gc.data); + if (ud.size == 1 && (gc.attr & GRID_ATTR_CHARSET)) { s = tty_acs_get(NULL, ud.data[0]); if (s != NULL && strlen(s) <= sizeof ud.data) { ud.size = strlen(s); @@ -1649,16 +1677,17 @@ int window_copy_in_set(struct window_pane *wp, u_int px, u_int py, const char *set) { struct window_copy_mode_data *data = wp->modedata; - const struct grid_cell *gc; - struct utf8_data ud; + struct grid_cell gc; + const struct utf8_data *ud; - gc = grid_peek_cell(data->backing->grid, px, py); - grid_cell_get(gc, &ud); - if (ud.size != 1 || gc->flags & GRID_FLAG_PADDING) + grid_get_cell(data->backing->grid, px, py, &gc); + + ud = &gc.data; + if (ud->size != 1 || (gc.flags & GRID_FLAG_PADDING)) return (0); - if (*ud.data == 0x00 || *ud.data == 0x7f) + if (*ud->data == 0x00 || *ud->data == 0x7f) return (0); - return (strchr(set, *ud.data) != NULL); + return (strchr(set, *ud->data) != NULL); } u_int @@ -1666,8 +1695,7 @@ window_copy_find_length(struct window_pane *wp, u_int py) { struct window_copy_mode_data *data = wp->modedata; struct screen *s = data->backing; - const struct grid_cell *gc; - struct utf8_data ud; + struct grid_cell gc; u_int px; /* @@ -1680,9 +1708,8 @@ window_copy_find_length(struct window_pane *wp, u_int py) if (px > screen_size_x(s)) px = screen_size_x(s); while (px > 0) { - gc = grid_peek_cell(s->grid, px - 1, py); - grid_cell_get(gc, &ud); - if (ud.size != 1 || *ud.data != ' ') + grid_get_cell(s->grid, px - 1, py, &gc); + if (gc.data.size != 1 || *gc.data.data != ' ') break; px--; } @@ -1716,17 +1743,15 @@ window_copy_cursor_back_to_indentation(struct window_pane *wp) { struct window_copy_mode_data *data = wp->modedata; u_int px, py, xx; - const struct grid_cell *gc; - struct utf8_data ud; + struct grid_cell gc; px = 0; py = screen_hsize(data->backing) + data->cy - data->oy; xx = window_copy_find_length(wp, py); while (px < xx) { - gc = grid_peek_cell(data->backing->grid, px, py); - grid_cell_get(gc, &ud); - if (ud.size != 1 || *ud.data != ' ') + grid_get_cell(data->backing->grid, px, py, &gc); + if (gc.data.size != 1 || *gc.data.data != ' ') break; px++; } @@ -1809,11 +1834,13 @@ void window_copy_cursor_left(struct window_pane *wp) { struct window_copy_mode_data *data = wp->modedata; + u_int py; - if (data->cx == 0) { + py = screen_hsize(data->backing) + data->cy - data->oy; + if (data->cx == 0 && py > 0) { window_copy_cursor_up(wp, 0); window_copy_cursor_end_of_line(wp); - } else { + } else if (data->cx > 0) { window_copy_update_cursor(wp, data->cx - 1, data->cy); if (window_copy_update_selection(wp, 1)) window_copy_redraw_lines(wp, data->cy, 1); @@ -1824,19 +1851,20 @@ void window_copy_cursor_right(struct window_pane *wp) { struct window_copy_mode_data *data = wp->modedata; - u_int px, py; + u_int px, py, yy; + py = screen_hsize(data->backing) + data->cy - data->oy; + yy = screen_hsize(data->backing) + screen_size_y(data->backing) - 1; if (data->screen.sel.flag && data->rectflag) px = screen_size_x(&data->screen); else { - py = screen_hsize(data->backing) + data->cy - data->oy; px = window_copy_find_length(wp, py); } - if (data->cx >= px) { + if (data->cx >= px && py < yy) { window_copy_cursor_start_of_line(wp); window_copy_cursor_down(wp, 0); - } else { + } else if (data->cx < px) { window_copy_update_cursor(wp, data->cx + 1, data->cy); if (window_copy_update_selection(wp, 1)) window_copy_redraw_lines(wp, data->cy, 1); @@ -1940,8 +1968,7 @@ window_copy_cursor_jump(struct window_pane *wp) { struct window_copy_mode_data *data = wp->modedata; struct screen *back_s = data->backing; - const struct grid_cell *gc; - struct utf8_data ud; + struct grid_cell gc; u_int px, py, xx; px = data->cx + 1; @@ -1949,10 +1976,9 @@ window_copy_cursor_jump(struct window_pane *wp) xx = window_copy_find_length(wp, py); while (px < xx) { - gc = grid_peek_cell(back_s->grid, px, py); - grid_cell_get(gc, &ud); - if (!(gc->flags & GRID_FLAG_PADDING) && - ud.size == 1 && *ud.data == data->jumpchar) { + grid_get_cell(back_s->grid, px, py, &gc); + if (!(gc.flags & GRID_FLAG_PADDING) && + gc.data.size == 1 && *gc.data.data == data->jumpchar) { window_copy_update_cursor(wp, px, data->cy); if (window_copy_update_selection(wp, 1)) window_copy_redraw_lines(wp, data->cy, 1); @@ -1967,8 +1993,7 @@ window_copy_cursor_jump_back(struct window_pane *wp) { struct window_copy_mode_data *data = wp->modedata; struct screen *back_s = data->backing; - const struct grid_cell *gc; - struct utf8_data ud; + struct grid_cell gc; u_int px, py; px = data->cx; @@ -1978,10 +2003,9 @@ window_copy_cursor_jump_back(struct window_pane *wp) px--; for (;;) { - gc = grid_peek_cell(back_s->grid, px, py); - grid_cell_get(gc, &ud); - if (!(gc->flags & GRID_FLAG_PADDING) && - ud.size == 1 && *ud.data == data->jumpchar) { + grid_get_cell(back_s->grid, px, py, &gc); + if (!(gc.flags & GRID_FLAG_PADDING) && + gc.data.size == 1 && *gc.data.data == data->jumpchar) { window_copy_update_cursor(wp, px, data->cy); if (window_copy_update_selection(wp, 1)) window_copy_redraw_lines(wp, data->cy, 1); @@ -1994,23 +2018,21 @@ window_copy_cursor_jump_back(struct window_pane *wp) } void -window_copy_cursor_jump_to(struct window_pane *wp) +window_copy_cursor_jump_to(struct window_pane *wp, int jump_again) { struct window_copy_mode_data *data = wp->modedata; struct screen *back_s = data->backing; - const struct grid_cell *gc; - struct utf8_data ud; + struct grid_cell gc; u_int px, py, xx; - px = data->cx + 1; + px = data->cx + 1 + jump_again; py = screen_hsize(back_s) + data->cy - data->oy; xx = window_copy_find_length(wp, py); while (px < xx) { - gc = grid_peek_cell(back_s->grid, px, py); - grid_cell_get(gc, &ud); - if (!(gc->flags & GRID_FLAG_PADDING) && - ud.size == 1 && *ud.data == data->jumpchar) { + grid_get_cell(back_s->grid, px, py, &gc); + if (!(gc.flags & GRID_FLAG_PADDING) && + gc.data.size == 1 && *gc.data.data == data->jumpchar) { window_copy_update_cursor(wp, px - 1, data->cy); if (window_copy_update_selection(wp, 1)) window_copy_redraw_lines(wp, data->cy, 1); @@ -2021,12 +2043,11 @@ window_copy_cursor_jump_to(struct window_pane *wp) } void -window_copy_cursor_jump_to_back(struct window_pane *wp) +window_copy_cursor_jump_to_back(struct window_pane *wp, int jump_again) { struct window_copy_mode_data *data = wp->modedata; struct screen *back_s = data->backing; - const struct grid_cell *gc; - struct utf8_data ud; + struct grid_cell gc; u_int px, py; px = data->cx; @@ -2035,11 +2056,13 @@ window_copy_cursor_jump_to_back(struct window_pane *wp) if (px > 0) px--; + if (jump_again && px > 0) + px--; + for (;;) { - gc = grid_peek_cell(back_s->grid, px, py); - grid_cell_get(gc, &ud); - if (!(gc->flags & GRID_FLAG_PADDING) && - ud.size == 1 && *ud.data == data->jumpchar) { + grid_get_cell(back_s->grid, px, py, &gc); + if (!(gc.flags & GRID_FLAG_PADDING) && + gc.data.size == 1 && *gc.data.data == data->jumpchar) { window_copy_update_cursor(wp, px + 1, data->cy); if (window_copy_update_selection(wp, 1)) window_copy_redraw_lines(wp, data->cy, 1); @@ -2098,7 +2121,7 @@ window_copy_cursor_next_word_end(struct window_pane *wp, const char *separators) { struct window_copy_mode_data *data = wp->modedata; - struct options *oo = &wp->window->options; + struct options *oo = wp->window->options; struct screen *back_s = data->backing; u_int px, py, xx, yy; int keys, expected = 1; @@ -2243,6 +2266,16 @@ window_copy_scroll_down(struct window_pane *wp, u_int ny) screen_write_stop(&ctx); } +int +window_copy_scroll_position(struct window_pane *wp) +{ + struct window_copy_mode_data *data = wp->modedata; + + if (wp->mode != &window_copy_mode) + return (-1); + return (data->oy); +} + void window_copy_rectangle_toggle(struct window_pane *wp) { @@ -2259,3 +2292,45 @@ window_copy_rectangle_toggle(struct window_pane *wp) window_copy_update_selection(wp, 1); window_copy_redraw_screen(wp); } + +void +window_copy_start_drag(struct client *c, struct mouse_event *m) +{ + struct window_pane *wp; + u_int x, y; + + wp = cmd_mouse_pane(m, NULL, NULL); + if (wp == NULL || wp->mode != &window_copy_mode) + return; + + if (cmd_mouse_at(wp, m, &x, &y, 1) != 0) + return; + + c->tty.mouse_drag_update = window_copy_drag_update; + c->tty.mouse_drag_release = NULL; /* will fire MouseUp key */ + + window_copy_update_cursor(wp, x, y); + window_copy_start_selection(wp); + window_copy_redraw_screen(wp); +} + +void +window_copy_drag_update(__unused struct client *c, struct mouse_event *m) +{ + struct window_pane *wp; + struct window_copy_mode_data *data; + u_int x, y, old_cy; + + wp = cmd_mouse_pane(m, NULL, NULL); + if (wp == NULL || wp->mode != &window_copy_mode) + return; + data = wp->modedata; + + if (cmd_mouse_at(wp, m, &x, &y, 0) != 0) + return; + old_cy = data->cy; + + window_copy_update_cursor(wp, x, y); + if (window_copy_update_selection(wp, 1)) + window_copy_redraw_selection(wp, old_cy); +} diff --git a/window-copy.h b/window-copy.h new file mode 100644 index 00000000..29e74ad8 --- /dev/null +++ b/window-copy.h @@ -0,0 +1,87 @@ +#ifndef WINDOW_COPY_H +#define WINDOW_COPY_H + +#include "tmux.h" + +enum window_copy_input_type { + WINDOW_COPY_OFF, + WINDOW_COPY_NAMEDBUFFER, + WINDOW_COPY_NUMERICPREFIX, + WINDOW_COPY_SEARCHUP, + WINDOW_COPY_SEARCHDOWN, + WINDOW_COPY_JUMPFORWARD, + WINDOW_COPY_JUMPBACK, + WINDOW_COPY_JUMPTOFORWARD, + WINDOW_COPY_JUMPTOBACK, + WINDOW_COPY_GOTOLINE, +#ifdef TMATE + WINDOW_COPY_PASSWORD, +#endif +}; + +/* + * Copy-mode's visible screen (the "screen" field) is filled from one of + * two sources: the original contents of the pane (used when we + * actually enter via the "copy-mode" command, to copy the contents of + * the current pane), or else a series of lines containing the output + * from an output-writing tmux command (such as any of the "show-*" or + * "list-*" commands). + * + * In either case, the full content of the copy-mode grid is pointed at + * by the "backing" field, and is copied into "screen" as needed (that + * is, when scrolling occurs). When copy-mode is backed by a pane, + * backing points directly at that pane's screen structure (&wp->base); + * when backed by a list of output-lines from a command, it points at + * a newly-allocated screen structure (which is deallocated when the + * mode ends). + */ + +#ifdef TMATE +typedef void (*copy_password_callback)(const char *password, void *private); +#endif + +struct window_copy_mode_data { + struct screen screen; + + struct screen *backing; + int backing_written; /* backing display started */ + + struct mode_key_data mdata; + + u_int oy; + + u_int selx; + u_int sely; + + int rectflag; /* in rectangle copy mode? */ + int scroll_exit; /* exit on scroll to end? */ + + u_int cx; + u_int cy; + + u_int lastcx; /* position in last line w/ content */ + u_int lastsx; /* size of last line w/ content */ + + enum window_copy_input_type inputtype; + const char *inputprompt; + char *inputstr; + int inputexit; + + int numprefix; + + enum window_copy_input_type searchtype; + char *searchstr; + + enum window_copy_input_type jumptype; + char jumpchar; + +#ifdef TMATE + copy_password_callback password_cb; + void *password_cb_private; +#endif +}; + +extern int window_copy_update_selection(struct window_pane *, int); +extern void window_copy_redraw_screen(struct window_pane *); + +#endif diff --git a/window.c b/window.c index fff2cfc3..45ec7790 100644 --- a/window.c +++ b/window.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2007 Nicholas Marriott + * Copyright (c) 2007 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -28,6 +29,7 @@ #include #include "tmux.h" +#include "tmate.h" /* * Each window is attached to a number of panes, each of which is a pty. This @@ -61,7 +63,15 @@ void window_pane_timer_callback(int, short, void *); void window_pane_read_callback(struct bufferevent *, void *); void window_pane_error_callback(struct bufferevent *, short, void *); -struct window_pane *window_pane_choose_best(struct window_pane_list *); +struct window_pane *window_pane_choose_best(struct window_pane **, u_int); + +RB_GENERATE(windows, window, entry, window_cmp); + +int +window_cmp(struct window *w1, struct window *w2) +{ + return (w1->id - w2->id); +} RB_GENERATE(winlinks, winlink, entry, winlink_cmp); @@ -244,71 +254,75 @@ winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl) } } -int -window_index(struct window *s, u_int *i) +struct window * +window_find_by_id_str(const char *s) { - for (*i = 0; *i < ARRAY_LENGTH(&windows); (*i)++) { - if (s == ARRAY_ITEM(&windows, *i)) - return (0); - } - return (-1); + const char *errstr; + u_int id; + + if (*s != '@') + return (NULL); + + id = strtonum(s + 1, 0, UINT_MAX, &errstr); + if (errstr != NULL) + return (NULL); + return (window_find_by_id(id)); } struct window * window_find_by_id(u_int id) { - struct window *w; - u_int i; + struct window w; - for (i = 0; i < ARRAY_LENGTH(&windows); i++) { - w = ARRAY_ITEM(&windows, i); - if (w != NULL && w->id == id) - return (w); - } - return (NULL); + w.id = id; + return (RB_FIND(windows, &windows, &w)); +} + +void +window_update_activity(struct window *w) +{ + gettimeofday(&w->activity_time, NULL); + alerts_queue(w, WINDOW_ACTIVITY); } struct window * window_create1(u_int sx, u_int sy) { struct window *w; - u_int i; w = xcalloc(1, sizeof *w); - w->id = next_window_id++; w->name = NULL; w->flags = 0; TAILQ_INIT(&w->panes); w->active = NULL; +#ifdef TMATE + w->tmate_last_sync_active_pane = NULL; +#endif + w->lastlayout = -1; w->layout_root = NULL; w->sx = sx; w->sy = sy; - options_init(&w->options, &global_w_options); - if (options_get_number(&w->options, "automatic-rename")) - queue_window_name(w); + w->options = options_create(global_w_options); - for (i = 0; i < ARRAY_LENGTH(&windows); i++) { - if (ARRAY_ITEM(&windows, i) == NULL) { - ARRAY_SET(&windows, i, w); - break; - } - } - if (i == ARRAY_LENGTH(&windows)) - ARRAY_ADD(&windows, w); w->references = 0; + w->id = next_window_id++; + RB_INSERT(windows, &windows, w); + + window_update_activity(w); + return (w); } struct window * window_create(const char *name, int argc, char **argv, const char *path, - const char *shell, int cwd, struct environ *env, struct termios *tio, - u_int sx, u_int sy, u_int hlimit, char **cause) + const char *shell, const char *cwd, struct environ *env, + struct termios *tio, u_int sx, u_int sy, u_int hlimit, char **cause) { struct window *w; struct window_pane *wp; @@ -326,7 +340,7 @@ window_create(const char *name, int argc, char **argv, const char *path, w->active = TAILQ_FIRST(&w->panes); if (name != NULL) { w->name = xstrdup(name); - options_set_number(&w->options, "automatic-rename", 0); + options_set_number(w->options, "automatic-rename", 0); } else w->name = default_window_name(w); @@ -336,23 +350,21 @@ window_create(const char *name, int argc, char **argv, const char *path, void window_destroy(struct window *w) { - u_int i; - - window_unzoom(w); - - if (window_index(w, &i) != 0) - fatalx("index not found"); - ARRAY_SET(&windows, i, NULL); - while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL) - ARRAY_TRUNC(&windows, 1); + RB_REMOVE(windows, &windows, w); if (w->layout_root != NULL) - layout_free(w); + layout_free_cell(w->layout_root); + if (w->saved_layout_root != NULL) + layout_free_cell(w->saved_layout_root); + free(w->old_layout); - if (event_initialized(&w->name_timer)) - evtimer_del(&w->name_timer); + if (event_initialized(&w->name_event)) + evtimer_del(&w->name_event); - options_free(&w->options); + if (event_initialized(&w->alerts_timer)) + evtimer_del(&w->alerts_timer); + + options_free(w->options); window_destroy_panes(w); @@ -373,9 +385,22 @@ window_remove_ref(struct window *w) void window_set_name(struct window *w, const char *new_name) { +#ifdef TMATE + /* + * We don't want to sync the layout too much. + * We might want to have some sort of timer for when to + * sync the layout. + */ + if (!strcmp(w->name, new_name)) + return; +#endif + free(w->name); w->name = xstrdup(new_name); notify_window_renamed(w); +#ifdef TMATE + tmate_sync_layout(); +#endif } void @@ -385,6 +410,18 @@ window_resize(struct window *w, u_int sx, u_int sy) w->sy = sy; } +int +window_has_pane(struct window *w, struct window_pane *wp) +{ + struct window_pane *wp1; + + TAILQ_FOREACH(wp1, &w->panes, entry) { + if (wp1 == wp) + return (1); + } + return (0); +} + int window_set_active_pane(struct window *w, struct window_pane *wp) { @@ -400,9 +437,34 @@ window_set_active_pane(struct window *w, struct window_pane *wp) return (1); } w->active->active_point = next_active_point++; + w->active->flags |= PANE_CHANGED; return (1); } +void +window_redraw_active_switch(struct window *w, struct window_pane *wp) +{ + const struct grid_cell *agc, *wgc; + + if (wp == w->active) + return; + + /* + * If window-style and window-active-style are the same, we don't need + * to redraw panes when switching active panes. Otherwise, if the + * active or inactive pane do not have a custom style, they will need + * to be redrawn. + */ + agc = options_get_style(w->options, "window-active-style"); + wgc = options_get_style(w->options, "window-style"); + if (style_equal(agc, wgc)) + return; + if (style_equal(&grid_default_cell, &w->active->colgc)) + w->active->flags |= PANE_REDRAW; + if (style_equal(&grid_default_cell, &wp->colgc)) + wp->flags |= PANE_REDRAW; +} + struct window_pane * window_get_active_at(struct window *w, u_int x, u_int y) { @@ -420,16 +482,6 @@ window_get_active_at(struct window *w, u_int x, u_int y) return (NULL); } -void -window_set_active_at(struct window *w, u_int x, u_int y) -{ - struct window_pane *wp; - - wp = window_get_active_at(w, x, y); - if (wp != NULL && wp != w->active) - window_set_active_pane(w, wp); -} - struct window_pane * window_find_string(struct window *w, const char *s) { @@ -506,6 +558,7 @@ window_unzoom(struct window *w) w->flags &= ~WINDOW_ZOOMED; layout_free(w); w->layout_root = w->saved_layout_root; + w->saved_layout_root = NULL; TAILQ_FOREACH(wp, &w->panes, entry) { wp->layout_cell = wp->saved_layout_cell; @@ -533,6 +586,9 @@ window_add_pane(struct window *w, u_int hlimit) void window_lost_pane(struct window *w, struct window_pane *wp) { + if (wp == marked_pane.wp) + server_clear_marked(); + if (wp == w->active) { w->active = w->last; w->last = NULL; @@ -541,6 +597,8 @@ window_lost_pane(struct window *w, struct window_pane *wp) if (w->active == NULL) w->active = TAILQ_NEXT(wp, entry); } + if (w->active != NULL) + w->active->flags |= PANE_CHANGED; } else if (wp == w->last) w->last = NULL; } @@ -560,7 +618,7 @@ window_pane_at_index(struct window *w, u_int idx) struct window_pane *wp; u_int n; - n = options_get_number(&w->options, "pane-base-index"); + n = options_get_number(w->options, "pane-base-index"); TAILQ_FOREACH(wp, &w->panes, entry) { if (n == idx) return (wp); @@ -598,7 +656,7 @@ window_pane_index(struct window_pane *wp, u_int *i) struct window_pane *wq; struct window *w = wp->window; - *i = options_get_number(&w->options, "pane-base-index"); + *i = options_get_number(w->options, "pane-base-index"); TAILQ_FOREACH(wq, &w->panes, entry) { if (wp == wq) { return (0); @@ -633,11 +691,11 @@ window_destroy_panes(struct window *w) } } -/* Return list of printable window flag symbols. No flags is just a space. */ +/* Retuns the printable flags on a window, empty string if no flags set. */ char * window_printable_flags(struct session *s, struct winlink *wl) { - char flags[BUFSIZ]; + char flags[32]; int pos; pos = 0; @@ -651,15 +709,29 @@ window_printable_flags(struct session *s, struct winlink *wl) flags[pos++] = '*'; if (wl == TAILQ_FIRST(&s->lastw)) flags[pos++] = '-'; + if (server_check_marked() && wl == marked_pane.wl) + flags[pos++] = 'M'; if (wl->window->flags & WINDOW_ZOOMED) flags[pos++] = 'Z'; - if (pos == 0) - flags[pos++] = ' '; flags[pos] = '\0'; return (xstrdup(flags)); } -/* Find pane in global tree by id. */ +struct window_pane * +window_pane_find_by_id_str(const char *s) +{ + const char *errstr; + u_int id; + + if (*s != '%') + return (NULL); + + id = strtonum(s + 1, 0, UINT_MAX, &errstr); + if (errstr != NULL) + return (NULL); + return (window_pane_find_by_id(id)); +} + struct window_pane * window_pane_find_by_id(u_int id) { @@ -673,6 +745,7 @@ struct window_pane * window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit) { struct window_pane *wp; + char host[HOST_NAME_MAX + 1]; wp = xcalloc(1, sizeof *wp); wp->window = w; @@ -683,7 +756,7 @@ window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit) wp->argc = 0; wp->argv = NULL; wp->shell = NULL; - wp->cwd = -1; + wp->cwd = NULL; wp->fd = -1; wp->event = NULL; @@ -702,11 +775,20 @@ window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit) wp->pipe_off = 0; wp->pipe_event = NULL; +#ifdef TMATE + wp->tmate_off = 0; +#endif + wp->saved_grid = NULL; + memcpy(&wp->colgc, &grid_default_cell, sizeof wp->colgc); + screen_init(&wp->base, sx, sy, hlimit); wp->screen = &wp->base; + if (gethostname(host, sizeof host) == 0) + screen_set_title(&wp->base, host); + input_init(wp); return (wp); @@ -717,8 +799,8 @@ window_pane_destroy(struct window_pane *wp) { window_pane_reset_mode(wp); - if (event_initialized(&wp->changes_timer)) - evtimer_del(&wp->changes_timer); + if (event_initialized(&wp->timer)) + evtimer_del(&wp->timer); if (wp->fd != -1) { #ifdef HAVE_UTEMPTER @@ -741,7 +823,7 @@ window_pane_destroy(struct window_pane *wp) RB_REMOVE(window_pane_tree, &all_window_panes, wp); - close(wp->cwd); + free((void *)wp->cwd); free(wp->shell); cmd_free_argv(wp->argc, wp->argv); free(wp); @@ -749,12 +831,12 @@ window_pane_destroy(struct window_pane *wp) int window_pane_spawn(struct window_pane *wp, int argc, char **argv, - const char *path, const char *shell, int cwd, struct environ *env, + const char *path, const char *shell, const char *cwd, struct environ *env, struct termios *tio, char **cause) { struct winsize ws; - char *argv0, *cmd, **argvp, paneid[16]; - const char *ptr, *first; + char *argv0, *cmd, **argvp; + const char *ptr, *first, *home; struct termios tio2; #ifdef HAVE_UTEMPTER char s[32]; @@ -774,9 +856,9 @@ window_pane_spawn(struct window_pane *wp, int argc, char **argv, free(wp->shell); wp->shell = xstrdup(shell); } - if (cwd != -1) { - close(wp->cwd); - wp->cwd = dup(cwd); + if (cwd != NULL) { + free((void *)wp->cwd); + wp->cwd = xstrdup(cwd); } cmd = cmd_stringify_argv(wp->argc, wp->argv); @@ -795,8 +877,10 @@ window_pane_spawn(struct window_pane *wp, int argc, char **argv, free(cmd); return (-1); case 0: - if (fchdir(wp->cwd) != 0) - chdir("/"); + if (chdir(wp->cwd) != 0) { + if ((home = find_home()) == NULL || chdir(home) != 0) + chdir("/"); + } if (tcgetattr(STDIN_FILENO, &tio2) != 0) fatal("tcgetattr failed"); @@ -804,8 +888,7 @@ window_pane_spawn(struct window_pane *wp, int argc, char **argv, memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc); tio2.c_cc[VERASE] = '\177'; #ifdef IUTF8 - if (options_get_number(&wp->window->options, "utf8")) - tio2.c_iflag |= IUTF8; + tio2.c_iflag |= IUTF8; #endif if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0) fatal("tcgetattr failed"); @@ -813,9 +896,8 @@ window_pane_spawn(struct window_pane *wp, int argc, char **argv, closefrom(STDERR_FILENO + 1); if (path != NULL) - environ_set(env, "PATH", path); - xsnprintf(paneid, sizeof paneid, "%%%u", wp->id); - environ_set(env, "TMUX_PANE", paneid); + environ_set(env, "PATH", "%s", path); + environ_set(env, "TMUX_PANE", "%%%u", wp->id); environ_push(env); clear_signals(1); @@ -856,12 +938,15 @@ window_pane_spawn(struct window_pane *wp, int argc, char **argv, #ifdef HAVE_UTEMPTER xsnprintf(s, sizeof s, "tmux(%lu).%%%u", (long) getpid(), wp->id); utempter_add_record(wp->fd, s); + kill(getpid(), SIGCHLD); #endif setblocking(wp->fd, 0); wp->event = bufferevent_new(wp->fd, window_pane_read_callback, NULL, window_pane_error_callback, wp); + + bufferevent_setwatermark(wp->event, EV_READ, 0, READ_SIZE); bufferevent_enable(wp->event, EV_READ|EV_WRITE); free(cmd); @@ -869,74 +954,74 @@ window_pane_spawn(struct window_pane *wp, int argc, char **argv, } void -window_pane_timer_start(struct window_pane *wp) +window_pane_timer_callback(__unused int fd, __unused short events, void *data) { - struct timeval tv; - - tv.tv_sec = 0; - tv.tv_usec = 1000; - - evtimer_del(&wp->changes_timer); - evtimer_set(&wp->changes_timer, window_pane_timer_callback, wp); - evtimer_add(&wp->changes_timer, &tv); + window_pane_read_callback(NULL, data); } void -window_pane_timer_callback(unused int fd, unused short events, void *data) +window_pane_read_callback(__unused struct bufferevent *bufev, void *data) { struct window_pane *wp = data; - struct window *w = wp->window; - u_int interval, trigger; + struct evbuffer *evb = wp->event->input; + char *new_data; + size_t new_size, available; + struct client *c; + struct timeval tv; - interval = options_get_number(&w->options, "c0-change-interval"); - trigger = options_get_number(&w->options, "c0-change-trigger"); + if (event_initialized(&wp->timer)) + evtimer_del(&wp->timer); - if (wp->changes_redraw++ == interval) { - wp->flags |= PANE_REDRAW; - wp->changes_redraw = 0; + log_debug("%%%u has %zu bytes", wp->id, EVBUFFER_LENGTH(evb)); + + TAILQ_FOREACH(c, &clients, entry) { + if (!tty_client_ready(c, wp)) + continue; + + available = EVBUFFER_LENGTH(c->tty.event->output); + if (available > READ_BACKOFF) + goto start_timer; } - if (trigger == 0 || wp->changes < trigger) { - wp->flags |= PANE_REDRAW; - wp->flags &= ~PANE_DROP; - } else - window_pane_timer_start(wp); - wp->changes = 0; -} - -void -window_pane_read_callback(unused struct bufferevent *bufev, void *data) -{ - struct window_pane *wp = data; - char *new_data; - size_t new_size; - - new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off; + new_size = EVBUFFER_LENGTH(evb) - wp->pipe_off; if (wp->pipe_fd != -1 && new_size > 0) { - new_data = EVBUFFER_DATA(wp->event->input); + new_data = EVBUFFER_DATA(evb) + wp->pipe_off; bufferevent_write(wp->pipe_event, new_data, new_size); } +#ifdef TMATE + new_size = EVBUFFER_LENGTH(wp->event->input) - wp->tmate_off; + new_data = EVBUFFER_DATA(wp->event->input) + wp->tmate_off; + if (new_size > 0) + tmate_pty_data(wp, new_data, new_size); +#endif + input_parse(wp); - wp->pipe_off = EVBUFFER_LENGTH(wp->event->input); + wp->pipe_off = EVBUFFER_LENGTH(evb); +#ifdef TMATE + wp->tmate_off = EVBUFFER_LENGTH(evb); +#endif + return; - /* - * If we get here, we're not outputting anymore, so set the silence - * flag on the window. - */ - wp->window->flags |= WINDOW_SILENCE; - if (gettimeofday(&wp->window->silence_timer, NULL) != 0) - fatal("gettimeofday failed."); +start_timer: + log_debug("%%%u backing off (%s %zu > %d)", wp->id, c->ttyname, + available, READ_BACKOFF); + + tv.tv_sec = 0; + tv.tv_usec = READ_TIME; + + evtimer_set(&wp->timer, window_pane_timer_callback, wp); + evtimer_add(&wp->timer, &tv); } void -window_pane_error_callback( - unused struct bufferevent *bufev, unused short what, void *data) +window_pane_error_callback(__unused struct bufferevent *bufev, + __unused short what, void *data) { struct window_pane *wp = data; - server_destroy_pane(wp); + server_destroy_pane(wp, 1); } void @@ -967,7 +1052,7 @@ window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc, if (wp->saved_grid != NULL) return; - if (!options_get_number(&wp->window->options, "alternate-screen")) + if (!options_get_number(wp->window->options, "alternate-screen")) return; sx = screen_size_x(s); sy = screen_size_y(s); @@ -997,7 +1082,7 @@ window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc, if (wp->saved_grid == NULL) return; - if (!options_get_number(&wp->window->options, "alternate-screen")) + if (!options_get_number(wp->window->options, "alternate-screen")) return; sx = screen_size_x(s); sy = screen_size_y(s); @@ -1046,7 +1131,9 @@ window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode) if ((s = wp->mode->init(wp)) != NULL) wp->screen = s; - wp->flags |= PANE_REDRAW; + wp->flags |= (PANE_REDRAW|PANE_CHANGED); + + server_status_window(wp->window); return (0); } @@ -1060,56 +1147,49 @@ window_pane_reset_mode(struct window_pane *wp) wp->mode = NULL; wp->screen = &wp->base; - wp->flags |= PANE_REDRAW; + wp->flags |= (PANE_REDRAW|PANE_CHANGED); + + server_status_window(wp->window); + +#ifdef TMATE + tmate_sync_copy_mode(wp); +#endif } void -window_pane_key(struct window_pane *wp, struct session *sess, int key) +window_pane_key(struct window_pane *wp, struct client *c, struct session *s, + key_code key, struct mouse_event *m) { struct window_pane *wp2; + if (KEYC_IS_MOUSE(key) && m == NULL) + return; + if (wp->mode != NULL) { if (wp->mode->key != NULL) - wp->mode->key(wp, sess, key); + wp->mode->key(wp, c, s, key, m); return; } if (wp->fd == -1 || wp->flags & PANE_INPUTOFF) return; - input_key(wp, key); - if (options_get_number(&wp->window->options, "synchronize-panes")) { + input_key(wp, key, m); + + if (KEYC_IS_MOUSE(key)) + return; + if (options_get_number(wp->window->options, "synchronize-panes")) { TAILQ_FOREACH(wp2, &wp->window->panes, entry) { if (wp2 == wp || wp2->mode != NULL) continue; - if (wp2->fd != -1 && window_pane_visible(wp2)) - input_key(wp2, key); + if (wp2->fd == -1 || wp2->flags & PANE_INPUTOFF) + continue; + if (window_pane_visible(wp2)) + input_key(wp2, key, NULL); } } } -void -window_pane_mouse(struct window_pane *wp, struct session *sess, - struct mouse_event *m) -{ - if (!window_pane_visible(wp)) - return; - - if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx) - return; - if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy) - return; - m->x -= wp->xoff; - m->y -= wp->yoff; - - if (wp->mode != NULL) { - if (wp->mode->mouse != NULL && - options_get_number(&wp->window->options, "mode-mouse")) - wp->mode->mouse(wp, sess, m); - } else if (wp->fd != -1) - input_mouse(wp, sess, m); -} - int window_pane_visible(struct window_pane *wp) { @@ -1152,17 +1232,17 @@ window_pane_search(struct window_pane *wp, const char *searchstr, /* Get MRU pane from a list. */ struct window_pane * -window_pane_choose_best(struct window_pane_list *list) +window_pane_choose_best(struct window_pane **list, u_int size) { struct window_pane *next, *best; u_int i; - if (ARRAY_LENGTH(list) == 0) + if (size == 0) return (NULL); - best = ARRAY_FIRST(list); - for (i = 1; i < ARRAY_LENGTH(list); i++) { - next = ARRAY_ITEM(list, i); + best = list[0]; + for (i = 1; i < size; i++) { + next = list[i]; if (next->active_point > best->active_point) best = next; } @@ -1176,14 +1256,15 @@ window_pane_choose_best(struct window_pane_list *list) struct window_pane * window_pane_find_up(struct window_pane *wp) { - struct window_pane *next, *best; - u_int edge, left, right, end; - struct window_pane_list list; + struct window_pane *next, *best, **list; + u_int edge, left, right, end, size; int found; if (wp == NULL || !window_pane_visible(wp)) return (NULL); - ARRAY_INIT(&list); + + list = NULL; + size = 0; edge = wp->yoff; if (edge == 0) @@ -1206,12 +1287,14 @@ window_pane_find_up(struct window_pane *wp) found = 1; else if (end >= left && end <= right) found = 1; - if (found) - ARRAY_ADD(&list, next); + if (!found) + continue; + list = xreallocarray(list, size + 1, sizeof *list); + list[size++] = next; } - best = window_pane_choose_best(&list); - ARRAY_FREE(&list); + best = window_pane_choose_best(list, size); + free(list); return (best); } @@ -1219,14 +1302,15 @@ window_pane_find_up(struct window_pane *wp) struct window_pane * window_pane_find_down(struct window_pane *wp) { - struct window_pane *next, *best; - u_int edge, left, right, end; - struct window_pane_list list; + struct window_pane *next, *best, **list; + u_int edge, left, right, end, size; int found; if (wp == NULL || !window_pane_visible(wp)) return (NULL); - ARRAY_INIT(&list); + + list = NULL; + size = 0; edge = wp->yoff + wp->sy + 1; if (edge >= wp->window->sy) @@ -1249,12 +1333,14 @@ window_pane_find_down(struct window_pane *wp) found = 1; else if (end >= left && end <= right) found = 1; - if (found) - ARRAY_ADD(&list, next); + if (!found) + continue; + list = xreallocarray(list, size + 1, sizeof *list); + list[size++] = next; } - best = window_pane_choose_best(&list); - ARRAY_FREE(&list); + best = window_pane_choose_best(list, size); + free(list); return (best); } @@ -1262,14 +1348,15 @@ window_pane_find_down(struct window_pane *wp) struct window_pane * window_pane_find_left(struct window_pane *wp) { - struct window_pane *next, *best; - u_int edge, top, bottom, end; - struct window_pane_list list; + struct window_pane *next, *best, **list; + u_int edge, top, bottom, end, size; int found; if (wp == NULL || !window_pane_visible(wp)) return (NULL); - ARRAY_INIT(&list); + + list = NULL; + size = 0; edge = wp->xoff; if (edge == 0) @@ -1292,12 +1379,14 @@ window_pane_find_left(struct window_pane *wp) found = 1; else if (end >= top && end <= bottom) found = 1; - if (found) - ARRAY_ADD(&list, next); + if (!found) + continue; + list = xreallocarray(list, size + 1, sizeof *list); + list[size++] = next; } - best = window_pane_choose_best(&list); - ARRAY_FREE(&list); + best = window_pane_choose_best(list, size); + free(list); return (best); } @@ -1305,14 +1394,15 @@ window_pane_find_left(struct window_pane *wp) struct window_pane * window_pane_find_right(struct window_pane *wp) { - struct window_pane *next, *best; - u_int edge, top, bottom, end; - struct window_pane_list list; + struct window_pane *next, *best, **list; + u_int edge, top, bottom, end, size; int found; if (wp == NULL || !window_pane_visible(wp)) return (NULL); - ARRAY_INIT(&list); + + list = NULL; + size = 0; edge = wp->xoff + wp->sx + 1; if (edge >= wp->window->sx) @@ -1335,12 +1425,14 @@ window_pane_find_right(struct window_pane *wp) found = 1; else if (end >= top && end <= bottom) found = 1; - if (found) - ARRAY_ADD(&list, next); + if (!found) + continue; + list = xreallocarray(list, size + 1, sizeof *list); + list[size++] = next; } - best = window_pane_choose_best(&list); - ARRAY_FREE(&list); + best = window_pane_choose_best(list, size); + free(list); return (best); } @@ -1348,27 +1440,44 @@ window_pane_find_right(struct window_pane *wp) void winlink_clear_flags(struct winlink *wl) { - struct winlink *wm; struct session *s; - struct window *w; - u_int i; + struct winlink *wl_loop; - for (i = 0; i < ARRAY_LENGTH(&windows); i++) { - if ((w = ARRAY_ITEM(&windows, i)) == NULL) - continue; - - RB_FOREACH(s, sessions, &sessions) { - if ((wm = session_has(s, w)) == NULL) + RB_FOREACH(s, sessions, &sessions) { + RB_FOREACH(wl_loop, winlinks, &s->windows) { + if (wl_loop->window != wl->window) + continue; + if ((wl_loop->flags & WINLINK_ALERTFLAGS) == 0) continue; - if (wm->window != wl->window) - continue; - if ((wm->flags & WINLINK_ALERTFLAGS) == 0) - continue; - - wm->flags &= ~WINLINK_ALERTFLAGS; - wm->window->flags &= ~WINDOW_ALERTFLAGS; + wl_loop->flags &= ~WINLINK_ALERTFLAGS; + wl_loop->window->flags &= ~WINDOW_ALERTFLAGS; server_status_session(s); } } } + +int +winlink_shuffle_up(struct session *s, struct winlink *wl) +{ + int idx, last; + + idx = wl->idx + 1; + + /* Find the next free index. */ + for (last = idx; last < INT_MAX; last++) { + if (winlink_find_by_index(&s->windows, last) == NULL) + break; + } + if (last == INT_MAX) + return (-1); + + /* Move everything from last - 1 to idx up a bit. */ + for (; last > idx; last--) { + wl = winlink_find_by_index(&s->windows, last - 1); + server_link_window(s, wl, s, last, 0, 0, NULL); + server_unlink_window(s, wl); + } + + return (idx); +} diff --git a/www/images/tmux3.png b/www/images/tmux3.png deleted file mode 100644 index 689a2833..00000000 Binary files a/www/images/tmux3.png and /dev/null differ diff --git a/www/images/tmux4.png b/www/images/tmux4.png deleted file mode 100644 index 9e3da3f1..00000000 Binary files a/www/images/tmux4.png and /dev/null differ diff --git a/www/images/tmux5.png b/www/images/tmux5.png deleted file mode 100644 index 83cd7e15..00000000 Binary files a/www/images/tmux5.png and /dev/null differ diff --git a/www/index.html.in b/www/index.html.in deleted file mode 100644 index e9ec46bd..00000000 --- a/www/index.html.in +++ /dev/null @@ -1,64 +0,0 @@ - - - - - tmux - - - -
- -
- -

tmux is a terminal multiplexer

- -

What is a terminal multiplexer? It lets you switch easily between -several programs in one terminal, detach them (they keep running in the -background) and reattach them to a different terminal. And do a lot more. See - -the manual.

- -

-Download tmux %%RELEASE%% -(changelog) or - -get the development version. -tmux is hosted on -SourceForge -and needs -libevent -and -ncurses.

- -

For support contact the -tmux-users@lists.sf.net -mailing list or IRC channel -#tmux on freenode.

- -

There are some programs to use with tmux - -on GitHub and a -book on tmux.

- -
- Screenshot - Screenshot - Screenshot -
-
-
- - diff --git a/www/main.css b/www/main.css deleted file mode 100644 index 28b58424..00000000 --- a/www/main.css +++ /dev/null @@ -1,50 +0,0 @@ -body { - font-family: Sans-Serif; - font-size: 10pt; - background-color: white; -} -#body-wrapper { - overflow: auto; -} -#upper-left-title { - font-size:xx-large; - margin-top: 0; -} -#left-menu li { - list-style: none; - margin-top: 1em; -} -.menu-headings { - border-top: 1px solid black; - border-bottom: 1px solid black; - font-weight: bold; - padding: 0.5em; -} -#left-menu-container { - padding-right: 0.5em; - margin-top: 0.5em; - margin-bottom: 0.5em; - margin-right: 0.5em; - border-right: 3px solid black; - text-align: right; - width: 12em; - float: left; -} -#main-content-wrapper { - margin-left: 2em; - margin-right: 4em; -} -#main-content-wrapper li { - list-style: disc; - margin-left: 12em -} -#screenshots { - text-align: center; - margin: 1em; - margin-left: 10em; -} -#screenshots img { - text-align: center; - margin: 0.5em; -} - diff --git a/xmalloc.c b/xmalloc.c index b1570a3a..90af1e2f 100644 --- a/xmalloc.c +++ b/xmalloc.c @@ -1,148 +1,139 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2004 Nicholas Marriott + * Author: Tatu Ylonen + * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland + * All rights reserved + * Versions of malloc and friends that check their results, and never return + * failure (they call fatal if they encounter an error). * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER - * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING - * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * As far as I am concerned, the code I have written for this software + * can be used freely for any purpose. Any derived versions of this + * software must be clearly marked as such, and if the derived work is + * incompatible with the protocol description in the RFC file, it must be + * called by a name other than "ssh" or "Secure Shell". */ -#include - +#include +#include #include +#include #include #include #include "tmux.h" -char * -xstrdup(const char *s) +void * +xmalloc(size_t size) { - char *ptr; - size_t len; + void *ptr; - len = strlen(s) + 1; - ptr = xmalloc(len); - - strlcpy(ptr, s, len); - return (ptr); + if (size == 0) + fatal("xmalloc: zero size"); + ptr = malloc(size); + if (ptr == NULL) + fatal("xmalloc: allocating %zu bytes: %s", + size, strerror(errno)); + return ptr; } void * xcalloc(size_t nmemb, size_t size) { - void *ptr; + void *ptr; if (size == 0 || nmemb == 0) - fatalx("zero size"); - if (SIZE_MAX / nmemb < size) - fatalx("nmemb * size > SIZE_MAX"); - if ((ptr = calloc(nmemb, size)) == NULL) - fatal("xcalloc failed"); - - return (ptr); + fatal("xcalloc: zero size"); + ptr = calloc(nmemb, size); + if (ptr == NULL) + fatal("xcalloc: allocating %zu * %zu bytes: %s", + nmemb, size, strerror(errno)); + return ptr; } void * -xmalloc(size_t size) +xrealloc(void *ptr, size_t size) { - void *ptr; - - if (size == 0) - fatalx("zero size"); - if ((ptr = malloc(size)) == NULL) - fatal("xmalloc failed"); - - return (ptr); + return xreallocarray(ptr, 1, size); } void * -xrealloc(void *oldptr, size_t newsize) +xreallocarray(void *ptr, size_t nmemb, size_t size) { - void *newptr; + void *new_ptr; - if (newsize == 0) - fatalx("zero size"); - if ((newptr = realloc(oldptr, newsize)) == NULL) - fatal("xrealloc failed"); - - return (newptr); + if (nmemb == 0 || size == 0) + fatal("xreallocarray: zero size"); + new_ptr = reallocarray(ptr, nmemb, size); + if (new_ptr == NULL) + fatal("xreallocarray: allocating %zu * %zu bytes: %s", + nmemb, size, strerror(errno)); + return new_ptr; } -void * -xreallocarray(void *oldptr, size_t nmemb, size_t size) +char * +xstrdup(const char *str) { - size_t newsize = nmemb * size; - void *newptr; + char *cp; - if (newsize == 0) - fatalx("zero size"); - if (SIZE_MAX / nmemb < size) - fatalx("nmemb * size > SIZE_MAX"); - if ((newptr = realloc(oldptr, newsize)) == NULL) - fatal("xreallocarray failed"); - - return (newptr); + if ((cp = strdup(str)) == NULL) + fatal("xstrdup: %s", strerror(errno)); + return cp; } int xasprintf(char **ret, const char *fmt, ...) { va_list ap; - int i; + int i; va_start(ap, fmt); i = xvasprintf(ret, fmt, ap); va_end(ap); - return (i); + return i; } +__attribute__((__format__(__printf__, 2, 0))) int xvasprintf(char **ret, const char *fmt, va_list ap) { - int i; + int i; i = vasprintf(ret, fmt, ap); - if (i < 0 || *ret == NULL) - fatal("xvasprintf failed"); - return (i); + if (i < 0 || *ret == NULL) + fatal("xasprintf: %s", strerror(errno)); + + return i; } int -xsnprintf(char *buf, size_t len, const char *fmt, ...) +xsnprintf(char *str, size_t len, const char *fmt, ...) { va_list ap; - int i; + int i; va_start(ap, fmt); - i = xvsnprintf(buf, len, fmt, ap); + i = xvsnprintf(str, len, fmt, ap); va_end(ap); - return (i); + return i; } +__attribute__((__format__(__printf__, 3, 0))) int -xvsnprintf(char *buf, size_t len, const char *fmt, va_list ap) +xvsnprintf(char *str, size_t len, const char *fmt, va_list ap) { - int i; + int i; if (len > INT_MAX) - fatalx("len > INT_MAX"); + fatal("xsnprintf: len > INT_MAX"); - i = vsnprintf(buf, len, fmt, ap); - if (i < 0) - fatal("vsnprintf failed"); + i = vsnprintf(str, len, fmt, ap); - return (i); + if (i < 0 || i >= (int)len) + fatal("xsnprintf: overflow"); + + return i; } diff --git a/xmalloc.h b/xmalloc.h new file mode 100644 index 00000000..0360b0d9 --- /dev/null +++ b/xmalloc.h @@ -0,0 +1,44 @@ +/* $OpenBSD$ */ + +/* + * Author: Tatu Ylonen + * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland + * All rights reserved + * Created: Mon Mar 20 22:09:17 1995 ylo + * + * Versions of malloc and friends that check their results, and never return + * failure (they call fatal if they encounter an error). + * + * As far as I am concerned, the code I have written for this software + * can be used freely for any purpose. Any derived versions of this + * software must be clearly marked as such, and if the derived work is + * incompatible with the protocol description in the RFC file, it must be + * called by a name other than "ssh" or "Secure Shell". + */ + +#ifndef XMALLOC_H +#define XMALLOC_H + +#if !defined(__bounded__) +# define __bounded__(x, y, z) +#endif + +void *xmalloc(size_t); +void *xcalloc(size_t, size_t); +void *xrealloc(void *, size_t); +void *xreallocarray(void *, size_t, size_t); +char *xstrdup(const char *); +int xasprintf(char **, const char *, ...) + __attribute__((__format__ (printf, 2, 3))) + __attribute__((__nonnull__ (2))); +int xvasprintf(char **, const char *, va_list) + __attribute__((__nonnull__ (2))); +int xsnprintf(char *, size_t, const char *, ...) + __attribute__((__format__ (printf, 3, 4))) + __attribute__((__nonnull__ (3))) + __attribute__((__bounded__ (__string__, 1, 2))); +int xvsnprintf(char *, size_t, const char *, va_list) + __attribute__((__nonnull__ (3))) + __attribute__((__bounded__ (__string__, 1, 2))); + +#endif /* XMALLOC_H */ diff --git a/xterm-keys.c b/xterm-keys.c index 5ca75d89..7bfefd15 100644 --- a/xterm-keys.c +++ b/xterm-keys.c @@ -1,7 +1,7 @@ /* $OpenBSD$ */ /* - * Copyright (c) 2009 Nicholas Marriott + * Copyright (c) 2009 Nicholas Marriott * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -40,11 +40,12 @@ * We accept any but always output the latter (it comes first in the table). */ -int xterm_keys_match(const char *, const char *, size_t, size_t *, u_int *); -int xterm_keys_modifiers(const char *, size_t, size_t *, u_int *); +int xterm_keys_match(const char *, const char *, size_t, size_t *, + key_code *); +int xterm_keys_modifiers(const char *, size_t, size_t *, key_code *); struct xterm_keys_entry { - int key; + key_code key; const char *template; }; @@ -115,11 +116,13 @@ const struct xterm_keys_entry xterm_keys_table[] = { */ int xterm_keys_match(const char *template, const char *buf, size_t len, - size_t *size, u_int *modifiers) + size_t *size, key_code *modifiers) { size_t pos; int retval; + *modifiers = 0; + if (len == 0) return (0); @@ -146,7 +149,8 @@ xterm_keys_match(const char *template, const char *buf, size_t len, /* Find modifiers from buffer. */ int -xterm_keys_modifiers(const char *buf, size_t len, size_t *pos, u_int *modifiers) +xterm_keys_modifiers(const char *buf, size_t len, size_t *pos, + key_code *modifiers) { u_int flags; @@ -177,11 +181,12 @@ xterm_keys_modifiers(const char *buf, size_t len, size_t *pos, u_int *modifiers) * key), -1 for not found, 1 for partial match. */ int -xterm_keys_find(const char *buf, size_t len, size_t *size, int *key) +xterm_keys_find(const char *buf, size_t len, size_t *size, key_code *key) { const struct xterm_keys_entry *entry; - u_int i, modifiers; + u_int i; int matched; + key_code modifiers; for (i = 0; i < nitems(xterm_keys_table); i++) { entry = &xterm_keys_table[i]; @@ -199,11 +204,11 @@ xterm_keys_find(const char *buf, size_t len, size_t *size, int *key) /* Lookup a key number from the table. */ char * -xterm_keys_lookup(int key) +xterm_keys_lookup(key_code key) { const struct xterm_keys_entry *entry; u_int i; - int modifiers; + key_code modifiers; char *out; modifiers = 1;