~arivigo/scalc-devel

Add command to print contents of stack v1 REJECTED

Steven Guikal: 1
 Add command to print contents of stack

 7 files changed, 34 insertions(+), 11 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~arivigo/scalc-devel/patches/23314/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH] Add command to print contents of stack Export this patch

Fixes #18
---
 Makefile |  4 ++--
 scalc.1  |  5 ++++-
 scalc.c  |  9 ++-------
 stack.c  | 16 +++++++++++++++-
 stack.h  |  1 +
 util.c   |  9 +++++++++
 util.h   |  1 +
 7 files changed, 34 insertions(+), 11 deletions(-)
 create mode 100644 util.c
 create mode 100644 util.h

diff --git a/Makefile b/Makefile
index c44394b..1657847 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@

include config.mk

SRC = mem.c op.c scalc.c stack.c strlcpy.c
SRC = mem.c op.c scalc.c stack.c strlcpy.c util.c
OBJ = ${SRC:%.c=%.o}

all: options scalc
@@ -40,7 +40,7 @@ clean:

dist: clean
	mkdir -p scalc-${VERSION}
	cp -R LICENSE Makefile README.md config.def.h config.mk mem.h op.h \
	cp -R LICENSE Makefile README.md config.def.h config.mk mem.h op.h util.h \
	   stack.h strlcpy.h scalc.1 ${SRC} scalc-${VERSION}
	tar -cf scalc-${VERSION}.tar scalc-${VERSION}
	gzip scalc-${VERSION}.tar
diff --git a/scalc.1 b/scalc.1
index 3ac4fb5..2ab3d01 100644
--- a/scalc.1
+++ b/scalc.1
@@ -46,6 +46,9 @@ Shows the last element in the stack.
Quits
.B scalc.
.TP
.B :s
Shows all the elements in the stack, bottom to top.
.TP
.BI :sav " reg"
Saves the value on the top of the stack to register
.I reg
@@ -131,4 +134,4 @@ Reading from a pipeline:
.RE
.fi
.SH SEE ALSO
.BR bc (1), dc (1)
\ No newline at end of file
.BR bc (1), dc (1)
diff --git a/scalc.c b/scalc.c
index 6977602..ccb147c 100644
--- a/scalc.c
+++ b/scalc.c
@@ -11,6 +11,7 @@
#include "op.h"
#include "stack.h"
#include "strlcpy.h"
#include "util.h"

#define CMD_ID_SIZE 32

@@ -34,7 +35,6 @@ typedef struct {
static void die(const char *fmt, ...);
static void usage(void);

static void print_num(double num);
static void run_cmd(Stack *stack, const char *expr);
static int apply_op(double *dx, const OpReg *op_ptr, Stack *stack);
static void eval_math(const char *expr, Stack *stack);
@@ -52,6 +52,7 @@ static const CmdReg cmd_defs[] = {
	{ ":sav", CMD_MEM, { .mem = mem_set } },
	{ ":swp", CMD_STK, { .stk = stack_swap } },
	{ ":q", CMD_CMD, { .cmd = cmd_quit } },
	{ ":s", CMD_STK, { .stk = stack_show } },
	{ "", CMD_NULL, { .cmd = NULL } }
};

@@ -75,12 +76,6 @@ usage(void)
	die("usage: scalc [-v]");
}

static void
print_num(double num)
{
	printf("%." SCALC_PREC "f\n", num);
}

static void
run_cmd(Stack *stack, const char *expr)
{
diff --git a/stack.c b/stack.c
index c9fa9da..9dac957 100644
--- a/stack.c
+++ b/stack.c
@@ -1,9 +1,10 @@
/* See LICENSE file for copyright and license details. */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "stack.h"
#include "util.h"

enum {
	STK_SUCCESS,
@@ -114,6 +115,19 @@ stack_swap(Stack *stack)
	return 0;
}

int
stack_show(Stack *stack)
{
	if (stack->sp < 0) {
		stack_err = STK_ERR_STACK_MIN;
		return -1;
	}
	for (int i = 0; i <= stack->sp; i ++) {
		print_num(stack->elems[i]);
	}
	return 0;
}

const char *
stack_errmsg(void)
{
diff --git a/stack.h b/stack.h
index c7a9c24..c4779f0 100644
--- a/stack.h
+++ b/stack.h
@@ -15,4 +15,5 @@ int stack_drop(Stack *stack);
int stack_dup(Stack *stack);
int stack_peek(double *dest, Stack stack);
int stack_swap(Stack *stack);
int stack_show(Stack *stack);
const char *stack_errmsg(void);
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..677d165
--- /dev/null
+++ b/util.c
@@ -0,0 +1,9 @@
#include <stdio.h>

#include "config.h"

void
print_num(double num)
{
    printf("%." SCALC_PREC "f\n", num);
}
diff --git a/util.h b/util.h
new file mode 100644
index 0000000..296f097
--- /dev/null
+++ b/util.h
@@ -0,0 +1 @@
void print_num(double num);
-- 
2.32.0

Hi!