Skip to content

Instantly share code, notes, and snippets.

@MichaelDimmitt
Last active November 9, 2023 14:49
Show Gist options
  • Save MichaelDimmitt/4663473dd9e3a1a43044a5aab9ba80c3 to your computer and use it in GitHub Desktop.
Save MichaelDimmitt/4663473dd9e3a1a43044a5aab9ba80c3 to your computer and use it in GitHub Desktop.
ideating how to get operating system changes

The original goal of this gist was to provide the same behavior to the "history" command in bash which tells you all of the commands you have written in your bash session, but tells the history of commands you added when using the elixir repl iex. The expected result is to print out all commands and their associated line numbers.

tldr: The instinct is to jump right in and solve the problem. But this document is an effort to create a framework of thinking on how to approach the problem and deal with all failure scenerios until it is proven truly impossible. Hopefully when a similar situation pops up I can utilize the same framework and skip or speed up this step of generating strategies to solve the problem.

Operation "track operating system changes"

Operation "track operating system changes" when in an elixir process.

With this information, I can achieve my original goal of finding where the operating system stores the up arrow history text when in an iex terminal session.

How my brain is trying to solve this problem:

  1. Look for a solution in elixir.
    1. Solve via config file and using mix.exs https://gist.github.com/sushant12/38c8e01ab7a3673ca45abe82d1ffe3c0
    2. Add some middlewhere
      1. where we intercept the input
      2. write the command lists to a log each time
      3. open and read from that file.
    3. Interrogate the pid to get the history object
      (sadly it only gives line numbers and values not command list, see below):
iex>(Process.info self)|> Enum.at(5)|> elem(1)|> Enum.at(6)|> elem(1)|> IEx.History.each(&IO.inspect/1)

tried and got bored, so moving on ...

  1. look for proof that the problem cant be solved in the current enviornment of an elixir iex process.

... skip

  1. Look for a solution in erlang.

... skip.

  1. look for proof that the problem cant be solved in the current enviornment of an erlang shell.erl context.

... skip.

... so now I am on step 5.

  1. Hope that a builtin exists in bash that can get me the information that I need
    a. Does bash have any advantage looking into the operating system than any other languages?
    b. How the heck does the history command even work?
    c. Look at a list of all builtin bash commands, do any seem to serve my purpose?
    d. Look for executables

    1. that make use of c drivers
    2. that interact with the unix system
    3. that I can run in bash to get the info I need.
  2. Look at the Application "Terminal" a. does it maintain its own state: cache/other?
    does it store shell history for a user to up arrow.

  3. If no "c" based driver exists how would you solve this in "c"?
    a. Kernel extensions (kexts), IOKit, and drivers.
    b. You can find more information on how to make these items here:

    1. https://stackoverflow.com/questions/1326855/where-can-i-systematically-study-how-to-write-mac-os-x-device-drivers
    2. (note to self) Buy all 3 volumes:
      a. Volume 1: https://www.amazon.com/MacOS-iOS-Internals-User-Mode/dp/099105556X
      b. Volume 2: https://www.amazon.com/MacOS-iOS-Internals-II-Kernel/dp/0991055578
    3. what the heck is a kext?
    4. get list of all callable system operations that can potentially get the information that we want.
  4. Learn more about operating systems.
    a. potentially there is not a library that tells us this info b. potentially there is a side effect that we can view the presence of that accomplishes the goal.

  5. Question myself:
    a. Did I miss something?
    b. Has this document achieved functional completeness?

Future:

Once this knowledge is solved the following becomes possible (living document)

  1. find where the operating system stores the up arrow history text when in an iex terminal session.
  2. find a way to intercept iex terminal output on <tab> using some sort of trap ... so that we can sort by arity.
    a. another solution:
    1. what if we could grab it first,
    2. write it to a file.
    3. clear the terminal.
    4. write it from the file in the sorted manner.

Other cool bash stuff:

# You can monitor any operation with as many jobs as you want. 
# Use the builtin time and bash give tell you a timestamp when all the jobs related to all git processes finish. 
# Monitor other pids by replacing git with whatever you want like "Cypress". 

# usage: time wait_for_all_of_any_process_type

wait_for_all_of_any_process_type () {
  echo -n loading 2>&1;
  while
    echo -n . 2>&1;
    ps e | grep -v grep | grep git > /dev/null;
  do
    sleep 1;
  done;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment