add: more samples and make them work
This commit is contained in:
@@ -156,6 +156,59 @@ pub extern "C" fn rpg_dsply_f64(f: f64) {
|
||||
let _ = out.flush();
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// rpg_dsply_read — display a prompt and read an integer response from stdin
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Display `prompt` (a null-terminated C string) with a `DSPLY` prefix, then
|
||||
/// read one line from stdin and parse it as a signed 64-bit integer. The
|
||||
/// parsed value is written through `response`.
|
||||
///
|
||||
/// This implements the three-operand form of the `DSPLY` opcode:
|
||||
/// ```rpg
|
||||
/// Dsply prompt ' ' response_var;
|
||||
/// ```
|
||||
/// where the third operand is the variable that receives the operator's reply.
|
||||
///
|
||||
/// If stdin is exhausted (EOF) or the line cannot be parsed as an integer the
|
||||
/// response is left unchanged.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// * `prompt` must be a valid null-terminated C string (or null, treated as
|
||||
/// an empty string).
|
||||
/// * `response` must be a valid, aligned, writable pointer to an `i64`.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn rpg_dsply_read(
|
||||
prompt: *const std::os::raw::c_char,
|
||||
response: *mut i64,
|
||||
) {
|
||||
use std::io::BufRead;
|
||||
|
||||
// Display the prompt.
|
||||
let text = if prompt.is_null() {
|
||||
std::borrow::Cow::Borrowed("")
|
||||
} else {
|
||||
unsafe { CStr::from_ptr(prompt).to_string_lossy() }
|
||||
};
|
||||
{
|
||||
let stdout = io::stdout();
|
||||
let mut out = stdout.lock();
|
||||
let _ = writeln!(out, "DSPLY {}", text);
|
||||
let _ = out.flush();
|
||||
}
|
||||
|
||||
// Read one line from stdin and parse it as i64.
|
||||
let stdin = io::stdin();
|
||||
let mut line = String::new();
|
||||
if stdin.lock().read_line(&mut line).is_ok() {
|
||||
let trimmed = line.trim();
|
||||
if let Ok(n) = trimmed.parse::<i64>() {
|
||||
unsafe { *response = n; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// rpg_halt — abnormal termination
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user