Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
list_readers.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file list_readers.c
12 * @brief Implements the OP_PCSC_LIST_READERS opcode (conditional build).
13 *
14 * Lists available PC/SC reader names for a given context. On success returns
15 * an array of strings. When PCSC support is disabled at build time, returns an
16 * empty array after consuming its argument to keep the stack balanced.
17 *
18 * OP_PCSC_LIST_READERS: (ctx_id:int) -> array<string>
19 *
20 * - Pops: ctx_id.
21 * - Pushes: array of reader names on success; empty array on error/disabled.
22 */
23
24/* PCSC list_readers */
26#ifdef FUN_WITH_PCSC
27 /* Pop context id; return [] if anything goes wrong */
28 Value vid = pop_value(vm);
29 int id = (int)vid.i;
31
33 if (!e) {
35 break;
36 }
37
38 DWORD sz = 0;
39 LONG rv = SCardListReaders(e->ctx, NULL, NULL, &sz);
40 if (rv != SCARD_S_SUCCESS || sz == 0) {
42 break;
43 }
44
45 char *msz = (char *)malloc(sz);
46 if (!msz) {
48 break;
49 }
50
51 rv = SCardListReaders(e->ctx, NULL, msz, &sz);
52 if (rv != SCARD_S_SUCCESS) {
53 free(msz);
55 break;
56 }
57
59 int count = 0;
60 char *p = msz;
61 while (*p && count < (int)(sizeof(vals) / sizeof(vals[0]))) {
62 size_t n = strlen(p);
63 vals[count++] = make_string(p);
64 p += n + 1;
65 }
67 for (int i = 0; i < count; ++i)
68 free_value(vals[i]);
70
71 if (arr.type != VAL_ARRAY) {
73 } else {
74 push_value(vm, arr);
75 }
76#else
77 /* Fallback (no PCSC): consume ctx id and return [] to keep the stack consistent */
78 Value vid = pop_value(vm);
79 free_value(vid);
81#endif
82 break;
83}
@ OP_PCSC_LIST_READERS
Definition bytecode.h:186
array_clear & arr
Definition clear.c:38
LONG rv
Definition connect.c:53
pcsc_ctx_entry * e
Definition connect.c:38
int n
Definition insert.c:41
free(msz)
free_value(vid)
char * msz
Value * vals
Definition make_array.c:39
int count
Definition parser.c:263
static pcsc_ctx_entry * pcsc_get_ctx(int id)
Lookup a context slot by id.
Definition pcsc.c:88
const char * p
Definition read_file.c:37
long sz
Definition read_file.c:50
Tagged union representing a Fun value.
Definition value.h:68
int64_t i
Definition value.h:71
Definition pcsc.c:35
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
Value make_array_from_values(const Value *vals, int count)
Create an array Value by copying items from an input span.
Definition value.c:142
@ VAL_ARRAY
Definition value.h:55
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580
static void push_value(VM *vm, Value v)
Push a Value onto the VM operand stack.
Definition vm.c:564