Skip to content

Commit 1f54777

Browse files
committed
WIP: $POST_PROMPT_COMMAND
1 parent a47afee commit 1f54777

15 files changed

+50
-10
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ History of Yash
22

33
## Yash 2.57 (Unreleased)
44

5+
- Added support for the "$POST_PROMPT_COMMAND" variable, whose value
6+
is executed after reading a command line in the interactive shell.
57
- [line-editing] Fixed the spurious error message printed when
68
completing after `git config alias.` with the nounset shell option
79
enabled.

NEWS.ja

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Yash 更新履歴
22

33
## Yash 2.57 (未リリース)
44

5+
- "$POST_PROMPT_COMMAND" に対応。対話モードでコマンドを読むごとに
6+
変数の値が実行される
57
- [行編集] nounset オプション有効時に `git config alias.` に続けて
68
補完をしようとするとエラーが出るのを修正
79
- [行編集] カーソルがバックスラッシュの直後にある時に補完をすると

doc/_set.txt

+1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ This option enables the link:posix.html[POSIXly-correct mode].
202202
When this option is disabled, the <<so-xtrace,x-trace option>> is temporarily
203203
disabled while the shell is executing commands defined in the
204204
link:params.html#sv-command_not_found_handler[+COMMAND_NOT_FOUND_HANDLER+],
205+
link:params.html#sv-post_prompt_command[+POST_PROMPT_COMMAND+],
205206
link:params.html#sv-prompt_command[+PROMPT_COMMAND+], or
206207
link:params.html#sv-yash_after_cd[+YASH_AFTER_CD+] variable.
207208

doc/interact.txt

+10-3
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,16 @@ variables can be defined with a name prefixed with +YASH_+ (e.g.
178178
link:params.html#sv-yash_ps1[+YASH_PS1+]). This allows using a different
179179
prompt string than that in the POSIXly-correct mode.
180180

181-
When the shell is not in the link:posix.html[POSIXly-correct mode],
182-
the value of the link:params.html#sv-prompt_command[+PROMPT_COMMAND+ variable]
183-
is executed before each prompt.
181+
When the shell is not in the link:posix.html[POSIXly-correct mode]:
182+
183+
- The value of the link:params.html#sv-prompt_command[+PROMPT_COMMAND+ variable]
184+
is executed before each prompt.
185+
- The value of the
186+
link:params.html#sv-post_prompt_command[+POST_PROMPT_COMMAND+ variable] is
187+
executed after each line is input. While the execution, the
188+
link:params.html#sv-command[+COMMAND+ variable] is set to the just input
189+
line. You can even modify the variable to manipulate the command to be
190+
executed. If you unset the variable, the command will not be executed.
184191

185192
[[history]]
186193
== Command history

doc/ja/_set.txt

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ link:syntax.html#for[For ループ]が{zwsp}link:exec.html#function[関数]の
134134
[[so-traceall]]trace-all::
135135
このオプションは、補助コマンド実行中も <<so-xtrace,x-trace オプション>>を機能させるかどうかを指定します。補助コマンドとは、
136136
link:params.html#sv-command_not_found_handler[+COMMAND_NOT_FOUND_HANDLER+]、
137+
link:params.html#sv-post_prompt_command[+POST_PROMPT_COMMAND+]、
137138
link:params.html#sv-prompt_command[+PROMPT_COMMAND+]、および
138139
link:params.html#sv-yash_after_cd[+YASH_AFTER_CD+]
139140
変数の値として定義され、特定のタイミングで解釈・実行されるコマンドです。

doc/ja/interact.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ link:lineedit.html#prediction[コマンドライン推定]を使用している
9898

9999
link:posix.html[POSIX 準拠モード]でないときは、上記の変数は名前に +YASH_+ を付けた名前 (例えば link:params.html#sv-yash_ps1[+YASH_PS1+]) で定義することもできます。これにより、POSIX 準拠モードとは異なるプロンプトを使い分けることができます。
100100

101-
link:posix.html[POSIX 準拠モード]でないときは、プロンプトを出す前に link:params.html#sv-prompt_command[+PROMPT_COMMAND+ 変数]の値がコマンドとして実行されます。
101+
link:posix.html[POSIX 準拠モード]でないときは、以下の変数が評価されます。
102+
103+
- プロンプトを出す前に link:params.html#sv-prompt_command[+PROMPT_COMMAND+ 変数]の値がコマンドとして実行されます。
104+
- コマンドが一行分入力されるたびに link:params.html#sv-post_prompt_command[+POST_PROMPT_COMMAND+ 変数]の値がコマンドとして実行されます。実行中は入力されたコマンドが link:params.html#sv-command[+COMMAND+ 変数]に代入されます。この変数の値を変更することで実行されるコマンドを改変することもできます。変数を削除するとコマンドは実行されません。
102105

103106
[[history]]
104107
== コマンド履歴

doc/ja/params.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,14 @@ link:_getopts.html[Getopts 組込みコマンド]で引数付きのオプショ
163163
[[sv-path]]+PATH+::
164164
この変数は、{zwsp}link:exec.html#search[コマンドの検索時]にコマンドのありかを示すパスを指定します。
165165

166+
[[sv-post_prompt_command]]+POST_PROMPT_COMMAND+::
167+
link:posix.html[POSIX 準拠モード]でない{zwsp}link:interact.html[対話モード]のシェルにおいて、シェルがコマンドを一行読み込むたびに、この変数の値がコマンドとして解釈・実行されます。詳細はlink:interact.html#prompt[プロンプト]を参照してください。
168+
166169
[[sv-ppid]]+PPID+::
167170
この変数の値は、シェルの親プロセスのプロセス ID を表す正の整数です。この変数はシェルの起動時に初期化されます。この変数の値は{zwsp}link:exec.html#subshell[サブシェル]においても変わりません。
168171

169172
[[sv-prompt_command]]+PROMPT_COMMAND+::
170-
link:posix.html[POSIX 準拠モード]でない{zwsp}link:interact.html[対話モード]のシェルにおいて、シェルが各コマンドのプロンプトを出す直前に、この変数の値がコマンドとして解釈・実行されます。これは、プロンプトを出す直前に毎回
173+
link:posix.html[POSIX 準拠モード]でない{zwsp}link:interact.html[対話モード]のシェルにおいて、シェルが各コマンドのlink:interact.html#prompt[プロンプト]を出す直前に、この変数の値がコマンドとして解釈・実行されます。これは、プロンプトを出す直前に毎回
171174
ifdef::basebackend-html[]
172175
pass:[<code><a href="_eval.html">eval</a> -i -- "${PROMPT_COMMAND-}"</code>]
173176
endif::basebackend-html[]

doc/ja/posix.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ POSIX 準拠モードを有効にすると、yash は POSIX の規定にでき
3535
- link:builtin.html#types[任意組込みコマンドおよび拡張組込みコマンド]は実行できません。
3636
- いくつかの{zwsp}link:builtin.html[組込みコマンド]で特定のオプションが使えなくなるなど挙動が変わります。特に、長いオプションは使えなくなります。
3737
- link:interact.html[対話モード]でないとき、{zwsp}link:builtin.html#types[特殊組込みコマンド]のオプションやオペランドの使い方が間違っているとシェルは直ちに終了します。また特殊組込みコマンドで代入エラーやリダイレクトエラーが発生したときも直ちに終了します。
38-
- link:interact.html[対話モード]のプロンプトを出す前に link:params.html#sv-prompt_command[+PROMPT_COMMAND+ 変数]の値を実行しません。{zwsp}link:params.html#sv-ps1[+PS1+ 変数]・{zwsp}link:params.html#sv-ps2[+PS2+ 変数]・{zwsp}link:params.html#sv-ps4[+PS4+ 変数]の値の解釈の仕方が違います。{zwsp}link:params.html#sv-yash_ps1[+YASH_PS1+] など +YASH_+ で始まる名前のプロンプト変数は使用されません。
38+
- link:interact.html[対話モード]のプロンプトを出す前後に link:params.html#sv-prompt_command[+PROMPT_COMMAND+ 変数]および link:params.html#sv-post_prompt_command[+POST_PROMPT_COMMAND+ 変数]の値を実行しません。{zwsp}link:params.html#sv-ps1[+PS1+ 変数]・{zwsp}link:params.html#sv-ps2[+PS2+ 変数]・{zwsp}link:params.html#sv-ps4[+PS4+ 変数]の値の解釈の仕方が違います。{zwsp}link:params.html#sv-yash_ps1[+YASH_PS1+] など +YASH_+ で始まる名前のプロンプト変数は使用されません。
3939
- link:interact.html#mailcheck[メールチェック]において、ファイルが更新されている場合はファイルが空でも新着メールメッセージを出力します。
4040
4141
// vim: set filetype=asciidoc expandtab:

doc/params.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,12 @@ This variable is initialized to +1+ when the shell is started.
284284
This variable specifies paths that are searched for a command in
285285
link:exec.html#search[command search].
286286

287+
[[sv-post_prompt_command]]+POST_PROMPT_COMMAND+::
288+
The shell interprets and executes the value of this variable after reading
289+
each command if the shell is link:interact.html[interactive] and not in
290+
the link:posix.html[POSIXly-correct mode].
291+
See link:interact.html#prompt[prompt] for details.
292+
287293
[[sv-ppid]]+PPID+::
288294
The value of this variable is the process ID of the shell's parent process,
289295
which is a positive integer.
@@ -293,7 +299,7 @@ link:exec.html#subshell[subshell].
293299

294300
[[sv-prompt_command]]+PROMPT_COMMAND+::
295301
The shell interprets and executes the value of this variable before printing
296-
each command prompt if the shell is link:interact.html[interactive] and not in
302+
each command link:interact.html#prompt[prompt] if the shell is link:interact.html[interactive] and not in
297303
the link:posix.html[POSIXly-correct mode].
298304
This behavior is equivalent to executing the command
299305
ifdef::basebackend-html[]

doc/posix.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ When the POSIXly-correct mode is enabled:
8787
arguments or when an error occurs in assignment or redirection with a
8888
special built-in.
8989
- An link:interact.html[interactive] shell does not execute the
90-
link:params.html#sv-prompt_command[+PROMPT_COMMAND+ variable] before
91-
printing a prompt.
90+
link:params.html#sv-prompt_command[+PROMPT_COMMAND+] or
91+
link:params.html#sv-post_prompt_command[+POST_PROMPT_COMMAND+ variable]
92+
before and after a prompt, respectively.
9293
The values of the link:params.html#sv-ps1[+PS1+],
9394
link:params.html#sv-ps2[+PS2+], and link:params.html#sv-ps4[+PS4+] variables
9495
are parsed differently.

input.c

+2
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ inputresult_T input_interactive(struct xwcsbuf_T *buf, void *inputinfo)
253253
* `handle_sigchld' must not be called from any other function until it is
254254
* called from `wait_for_input' during the line-editing. */
255255

256+
// FIXME
257+
256258
#if YASH_ENABLE_LINEEDIT
257259
/* read a line using line editing */
258260
if (info->fileinfo->fd == STDIN_FILENO

share/initialization/common

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ export SHLVL
199199
# initialize event handlers
200200
COMMAND_NOT_FOUND_HANDLER=()
201201
PROMPT_COMMAND=()
202+
POST_PROMPT_COMMAND=()
202203
YASH_AFTER_CD=()
203204

204205
# define prompt

tests/prompt-y.tst

+10
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ $
102102
$
103103
__ERR__
104104

105+
test_Oe 'POST_PROMPT_COMMAND is ignored in POSIX mode' -i +m
106+
POST_PROMPT_COMMAND='echo not printed'; echo >&2
107+
echo >&2; exit
108+
__IN__
109+
$
110+
$
111+
__ERR__
112+
105113
)
106114

107115
test_e 'YASH_PSx precedes PSx (non-POSIX)' -i +m
@@ -193,6 +201,8 @@ $
193201
123$ 1
194202
__ERR__
195203

204+
# FIXME post-prompt command
205+
196206
test_e '\$ in PS1 and PS2 (non-root)' -i +m
197207
PS1='\$ ' PS2='\$_'; echo >&2
198208
e\

tests/run-test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export YASH_LOADPATH= # ignore default yashrc
8080
unset -v CDPATH COLUMNS COMMAND_NOT_FOUND_HANDLER DIRSTACK ECHO_STYLE ENV
8181
unset -v FCEDIT HANDLED HISTFILE HISTRMDUP HISTSIZE HOME IFS LC_ALL
8282
unset -v LC_COLLATE LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME LINES MAIL
83-
unset -v MAILCHECK MAILPATH NLSPATH OLDPWD PROMPT_COMMAND
83+
unset -v MAILCHECK MAILPATH NLSPATH OLDPWD POST_PROMPT_COMMAND PROMPT_COMMAND
8484
unset -v PS1 PS1R PS1S PS2 PS2R PS2S PS3 PS3R PS3S PS4 PS4R PS4S
8585
unset -v RANDOM TERM YASH_AFTER_CD YASH_LE_TIMEOUT YASH_VERSION
8686
unset -v A B C D E F G H I J K L M N O P Q R S T U V W X Y Z _

variable.h

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ extern char **environ;
5757
#define VAR_OPTARG "OPTARG"
5858
#define VAR_OPTIND "OPTIND"
5959
#define VAR_PATH "PATH"
60+
#define VAR_POST_PROMPT_COMMAND "POST_PROMPT_COMMAND"
6061
#define VAR_PPID "PPID"
6162
#define VAR_PROMPT_COMMAND "PROMPT_COMMAND"
6263
#define VAR_PS1 "PS1"

0 commit comments

Comments
 (0)