Skip to content

Canonical vs Non-canonical input mode for Terminal

Interesting read:

Canonical Input:

  • Buffered input
  • CTRL+D (EOF) is processed, the buffer is returned to the program, and if this buffer is empty, read() will interpret that as an actual EOF

Non-canonical Input:

  • Input is immediately returned to pending read()
  • If there are no pending read(), the input buffer is still used to hold the data

Buffered input enables the editing in shells like bash, where you can go back n characters, delete a character, add one etc

EOF handling behavior in canonical input means that when a buffer is returned with a CTRL-D and nothing is in the buffer (if it is the only character available then EOF is generated by read()). This is quite interesting because if the program doesnt care about EOFs (i.e. if it doesnt stop reading on EOF), you can type stuff after a EOF and they get read anyway.

Buffer is used in both modes, its just that no editing is available in non-canonical mode

Posted in Rant. Tagged with , , .