Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
input_line.c File Reference

Implements the OP_INPUT_LINE opcode for interactive console input. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

 if (has_prompt)
 if (hidden)
 if (!buf)
 while ((ch=fgetc(stdin)) !=EOF)
 if (len+1 >=cap)
 push_value (vm, make_string(buf))
 free (buf)
 if (tcgetattr(STDIN_FILENO, &t)==0)

Variables

case OP_INPUT_LINE
int hidden = (inst.operand & 2) ? 1 : 0
int echo_disabled = 0
size_t cap = 128
size_t len = 0
char * buf = (char *)malloc(cap)
int ch
push_done __pad0__
restore_echo_and_break __pad1__

Detailed Description

Implements the OP_INPUT_LINE opcode for interactive console input.

This snippet handles the OP_INPUT_LINE instruction in the VM dispatch. It can optionally print a prompt (taken from the stack) and can read input in a hidden mode (terminal echo disabled) suitable for passwords.

Operand bits (inst.operand):

  • bit0 (1): Has prompt. When set, the top of the stack is popped and converted to string, printed without a trailing newline.
  • bit1 (2): Hidden input. When set, terminal echo is temporarily disabled while reading the line (best-effort, platform dependent).

Stack effects:

  • If bit0 is set: pop(prompt)
  • Always: push(result_string)

Behavior:

  • Converts an optional prompt Value to string using value_to_string_alloc, prints it to stdout without a newline, and flushes the stream.
  • If hidden is requested, disables terminal echo (POSIX termios or Win32 console modes) before reading.
  • Reads a single line from stdin, accepting both "\n" and "\r\n" endings.
  • Restores terminal echo if it was disabled and, when a prompt was printed, emits a newline for a better UX.
  • Pushes the captured line as a Fun string (never NULL; empty string on failure or EOF).

Errors and corner cases:

  • Memory allocation failures are reported to stderr; an empty string is pushed in such cases to keep execution flowing.
  • If echo toggling fails, input still proceeds with echo enabled.
  • EOF before any character yields an empty string.

Example: // Bytecode: [optional PUSH prompt], OP_INPUT_LINE(operand) // operand bit0=1 (has prompt), bit1=2 (hidden) can be combined // Stack before (bit0=1): ["Enter password: "] // Stack after: ["user-typed-line"]

Definition in file input_line.c.

Function Documentation

◆ free()

free(buf)
Here is the caller graph for this function:

◆ if() [1/5]

if(!buf)

Definition at line 104 of file input_line.c.

Here is the call graph for this function:

◆ if() [2/5]

if(has_prompt)

Definition at line 59 of file input_line.c.

Here is the call graph for this function:

◆ if() [3/5]

if(hidden)

Definition at line 87 of file input_line.c.

◆ if() [4/5]

if(len+1 >=cap)

Definition at line 139 of file input_line.c.

Here is the call graph for this function:

◆ if() [5/5]

if(tcgetattr(STDIN_FILENO, &t) = = 0)

Definition at line 176 of file input_line.c.

◆ push_value()

push_value(vm,
make_string(buf) )
Here is the caller graph for this function:

◆ while()

while((ch=fgetc(stdin)) ! = EOF)

Definition at line 112 of file input_line.c.

Here is the call graph for this function:

Variable Documentation

◆ __pad0__

push_done __pad0__

Definition at line 155 of file input_line.c.

◆ __pad1__

restore_echo_and_break __pad1__

Definition at line 186 of file input_line.c.

◆ buf

buf[len] = (char *)malloc(cap)

Definition at line 103 of file input_line.c.

◆ cap

size_t cap = 128

Definition at line 101 of file input_line.c.

◆ ch

int ch

Definition at line 111 of file input_line.c.

◆ echo_disabled

int echo_disabled = 0

Definition at line 72 of file input_line.c.

◆ hidden

int hidden = (inst.operand & 2) ? 1 : 0

Definition at line 58 of file input_line.c.

◆ len

size_t len = 0

Definition at line 102 of file input_line.c.

◆ OP_INPUT_LINE

case OP_INPUT_LINE

Definition at line 52 of file input_line.c.