Skip to content

Commit

Permalink
implement @SYSTEM function
Browse files Browse the repository at this point in the history
This allows you to collect the output of commands in cells. Previously
This had to be done with a macro, but it's very convenient to write
@value(@System("query value")).
  • Loading branch information
taviso committed May 26, 2023
1 parent 8a7992a commit 5660440
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion atfuncs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LDLIBS=

all: atfuncs.a

atfuncs.a: date.o weekday.o product.o atfuncs.o
atfuncs.a: date.o weekday.o product.o atfuncs.o system.o
$(AR) r $@ $^

clean:
Expand Down
2 changes: 2 additions & 0 deletions atfuncs/atfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ void init_at_funcs()
// Setup any @functions here.
functions[AT_WEEKDAY] = (atfunc_t) at_weekday;
functions[AT_PRODUCT] = (atfunc_t) at_product;
functions[AT_SYSTEM] = (atfunc_t) at_system;

// You can pass 1 or more parameters to @PRODUCT().
fn_numargs[AT_PRODUCT] = AT_ARGS_VARIABLE | 1;
fn_numargs[AT_SYSTEM] = 1;
}
10 changes: 8 additions & 2 deletions atfuncs/atfuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@ extern int16_t encode_date(int16_t *datenums);

extern int16_t check_three_numbers();
extern int16_t check_one_number();
extern int16_t check_one_string();
extern int16_t get_integer();
extern int16_t push_integer(uint16_t val);
extern int16_t push_one();
extern int16_t push_zero();
extern int16_t drop_one_push_err();
extern int16_t drop_one_push_stack_string(char *str);
extern char * peek_string(int16_t n);
extern void mod_real_d();
extern void int_real_d();
extern void drop_one();
extern void swap_TOS();

// @PRODUCT replaced the previously unimplemented @CALL
// Replaced previously unimplemented functions
#define AT_CALL AT_PRODUCT
#define AT_TERM2 AT_SYSTEM

enum {
AT_NA,
Expand Down Expand Up @@ -162,7 +167,7 @@ enum {
AT_PMT2,
AT_PV2,
AT_FV2,
AT_TERM2,
AT_SYSTEM,
AT_NUM_FUNCS,
};

Expand All @@ -173,6 +178,7 @@ extern int8_t fn_numargs[AT_NUM_FUNCS];

int16_t at_date();
int16_t at_weekday();
int16_t at_system();
int16_t at_product(int16_t cnt);

void init_at_funcs();
Expand Down
37 changes: 37 additions & 0 deletions atfuncs/system.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stddef.h>

#include "lottypes.h"
#include "lotfuncs.h"
#include "lotdefs.h"
#include "atfuncs.h"

int16_t at_system()
{
FILE *cmd;
char buf[512] = {0};

if (!check_one_string()) {
return 0;
}

cmd = popen(peek_string(0), "r");

if (cmd == NULL) {
return drop_one_push_err();
}

// We can read at most 512 bytes of output.
fscanf(cmd, "%511[^\n]", buf);

// We return @ERR if the command returned failure.
if (pclose(cmd) != EXIT_SUCCESS) {
return drop_one_push_err();
}

// Return the output as a string.
return drop_one_push_stack_string(buf);
}
2 changes: 2 additions & 0 deletions globalize.lst
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,5 @@ display_filename
filename_length
mac_nobreak
mac_str
drop_one_push_stack_string
drop_one_push_err
2 changes: 1 addition & 1 deletion res/l123txt3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2767,7 +2767,7 @@
130 "PMT2"
131 "PV2"
132 "FV2"
133 "TERM2"
133 "SYSTEM"
134 "?"

=group 21, 256 bytes
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,10 @@ function check_calculations()
verify_result_contents '@UPPER("hello")' "HELLO"
verify_result_contents '@WEEKDAY(@DATE(2001,12,25))' "3"
verify_result_contents '@PRODUCT(1,2,3)' "6"
verify_result_contents '@ISERR(@SYSTEM("false"))' "1"
verify_result_contents '@ISERR(@SYSTEM("true"))' "0"
verify_result_contents '@SYSTEM("printf XyZzY")' "XyZzY"
verify_result_contents '@VALUE(@SYSTEM("echo 32+6 | bc"))' "38"
}

function check_crash_bugs()
Expand Down

0 comments on commit 5660440

Please sign in to comment.