commit e369e7830e1e80d7e3dd56b62b1e52f3bf7ea1c4
parent 27174b019aec4aa377f7d51ff0753366756422a6
Author: William Casarin <jb55@jb55.com>
Date: Sat, 16 Dec 2017 23:22:16 -0800
fix -p in repl
Diffstat:
5 files changed, 11 insertions(+), 11 deletions(-)
diff --git a/Makefile b/Makefile
@@ -23,7 +23,7 @@ install: $(BIN)
cp $(BIN) $(PREFIX)/bin
$(BIN): $(OBJS) bcalc.c
- $(CC) -Ideps -o $@ bcalc.c $(OBJS)
+ $(CC) $(CFLAGS) -Ideps -o $@ bcalc.c $(OBJS)
clean:
rm -f $(GEN)
diff --git a/bcalc.c b/bcalc.c
@@ -7,6 +7,7 @@
extern int yylex();
extern int yyparse();
extern enum unit g_output_format;
+extern int g_print_unit;
void yyerror(const char* s);
@@ -57,16 +58,12 @@ int main(int argc, char *argv[argc]) {
command_free(&cmd);
g_output_format = settings.format;
+ g_print_unit = settings.print_unit;
do {
yyparse();
} while(!feof(stdin));
- if (settings.print_unit)
- printf(" %s\n", unit_name(settings.format));
- else
- putchar('\n');
-
return 0;
}
diff --git a/num.c b/num.c
@@ -130,18 +130,20 @@ trim_zeros (char *s, int n)
}
void
-num_print(struct num *num, enum unit format) {
+num_print(struct num *num, enum unit format, int print_unit) {
static char buffer[255];
int64_t msat_multiple = unit_msat_multiple(format);
- if (format) {
- }
double d = num->type == TYPE_FLOAT? num->floatval : (double)num->intval;
d /= (double)msat_multiple;
sprintf (buffer, "%.11f", d);
trim_zeros(buffer, 12);
printf("%s", buffer);
+
+ if (print_unit)
+ printf(" %s", unit_name(format));
+ printf("\n");
}
diff --git a/num.h b/num.h
@@ -44,7 +44,7 @@ void num_mul(struct num *dst, struct num *a, struct num *b);
void num_div(struct num *dst, struct num *a, struct num *b);
void num_assign(struct num *dst, struct num *a);
void num_init(struct num *num);
-void num_print(struct num *num, enum unit unit);
+void num_print(struct num *num, enum unit unit, int print_unit);
char *unit_name(enum unit unit);
#endif /* UNIT_H */
diff --git a/parser.y b/parser.y
@@ -6,6 +6,7 @@
#include "num.h"
enum unit g_output_format;
+int g_print_unit;
%}
@@ -36,7 +37,7 @@ calc:
;
line: T_NEWLINE
- | expr T_NEWLINE { num_print(&$1, g_output_format); }
+ | expr T_NEWLINE { num_print(&$1, g_output_format, g_print_unit); }
;
expr: T_INT T_UNIT { num_init(&$$);