Implementing `HISTTIMEFORMAT` in Ksh
dnewhall opened this issue · 0 comments
dnewhall commented
bash has a HISTTIMEFORMAT
variable that saves the timestamp for each interactive command that is run and displays the timestamp using its value as a format string when listing the history. This is pretty useful sometimes and could be added to Ksh.
After reading the source, here's what I think needs to be changed to implement this (though am unsure of some details). Presumably someday I'll have the time to code this up and test it, but in the meantime, I'm going to leave this explanation here:
- First, increment the history file version number (
HIST_VERSION
inhistory.h
), so the magic number sequence would now be \201\02 - In
edit/history.c
, add a new byte sequence starting with \203 (since \201 is the undo, and \202 is to set the command number) and continuing for 9 bytes. The bytes following the \203 are the bytes representing the time_t value for the time the following command was run. I think the functionshist_eof
,hist_trim
, andhist_write
are the big ones that need to be changed for this. - Add code that saves the UNIX time when a command is executed using the new byte sequence format before the command is executed by the shell. I'm guessing this would go in either
sh_eval
orsh_exec
insh/xev.c
? - As far as I can tell, all history functions operate on the history file itself, not any buffer in memory?
hist_list
and co. inedit/history.c
would then need to be modified to skip over the new \203 sequences when retrieving/saving parts of the command history. - Add code in
bltins/hist.c
sohist -l
(theb_hist
function) can checkHISTTIMEFORMAT
, retrive the timestamps, and display them using its value as the format.
(Steps 1, 2, and (to a lesser extent) 5 are pretty straightforward, steps 3 and 4 are the ones where my knowledge of the source appears to fail me.)