Skip to content

Commit

Permalink
Enter bc mode with -b sans args
Browse files Browse the repository at this point in the history
  • Loading branch information
jarun committed Oct 2, 2018
1 parent c0957f9 commit 5f6a41f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ $ make strip install

```
usage: bcal [-c N] [-f FORMAT] [-s bytes] [expr]
[N [unit]] [-b expr] [-m] [-d] [-h]
[N [unit]] [-b [expr]] [-m] [-d] [-h]
Storage expression calculator.
Expand Down Expand Up @@ -148,7 +148,7 @@ optional arguments:
LBA = 50, MH = 0x12, MS = 0
default MAX_HEAD: 16, default MAX_SECTOR: 63
-s bytes sector size [default 512]
-b expr evaluate expression in bc
-b [expr] enter bc mode or evaluate expression in bc
-m show minimal output (e.g. decimal bytes)
-d enable debug information and logs
-h show this help, storage sizes and exit
Expand Down
6 changes: 3 additions & 3 deletions bcal.1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.SH NAME
bcal \- Storage expression calculator.
.SH SYNOPSIS
.B bcal [-c N] [-f FORMAT] [-s bytes] [expr] [N [unit]] [-b expr] [-m] [-d] [-h]
.B bcal [-c N] [-f FORMAT] [-s bytes] [expr] [N [unit]] [-b [expr]] [-m] [-d] [-h]
.SH DESCRIPTION
.B bcal
(Byte CALculator) is a command-line utility to help with numerical calculations and expressions involving data storage units, addressing, base conversion etc. It invokes GNU \fBbc\fR for non-storage expressions.
Expand Down Expand Up @@ -79,8 +79,8 @@ Omitted values, (other than MAX_HEAD and MAX_SECTOR) are considered 0. Default M
.BI "-s=" bytes
Sector size in bytes. Default value is 512.
.TP
.BI "-b=" expr
Evaluate expression in bc.
.BI "-b=" [expr]
Start in \fIbc\fR mode. If expression is provided, evaluate in \fIbc\fR and quit.
.TP
.BI "-m"
Show minimal output (e.g. decimal bytes).
Expand Down
19 changes: 13 additions & 6 deletions src/bcal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ static void prompt_help()
static void usage()
{
printf("usage: bcal [-c N] [-f FORMAT] [-s bytes] [expr]\n\
[N [unit]] [-b expr] [-m] [-d] [-h]\n\n\
[N [unit]] [-b [expr]] [-m] [-d] [-h]\n\n\
Storage expression calculator.\n\n\
positional arguments:\n\
expr evaluate storage arithmetic expression\n\
Expand Down Expand Up @@ -1107,7 +1107,7 @@ optional arguments:\n\
LBA = 50, MH = 0x12, MS = 0\n\
default MAX_HEAD: 16, default MAX_SECTOR: 63\n\
-s bytes sector size [default 512]\n\
-b expr evaluate expression in bc\n\
-b [expr] enter bc mode or evaluate expression in bc\n\
-m show minimal output (e.g. decimal bytes)\n\
-d enable debug information and logs\n\
-h show this help, storage sizes and exit\n\n");
Expand Down Expand Up @@ -1936,7 +1936,7 @@ int main(int argc, char **argv)
opterr = 0;
rl_bind_key('\t', rl_insert);

while ((opt = getopt(argc, argv, "b:c:df:hms:")) != -1) {
while ((opt = getopt(argc, argv, "bc:df:hms:")) != -1) {
switch (opt) {
case 'c':
{
Expand Down Expand Up @@ -1980,7 +1980,9 @@ int main(int argc, char **argv)
sectorsz = strtoul_b(optarg);
break;
case 'b':
return try_bc(optarg);
cfg.bcmode = 1;
strncpy(prompt, "bc> ", 5);
break;
case 'd':
cfg.loglvl = DEBUG;
log(DEBUG, "bcal v%s\n", VERSION);
Expand All @@ -1991,6 +1993,7 @@ int main(int argc, char **argv)
usage();
return 0;
default:
log(ERROR, "invalid option \'%c\'\n\n", (char)optopt);
usage();
return -1;
}
Expand Down Expand Up @@ -2108,8 +2111,12 @@ int main(int argc, char **argv)

/*Arithmetic operation*/
if (argc - optind == 1) {
curexpr = argv[optind];
return evaluate(argv[optind], sectorsz);
if (cfg.bcmode)
return try_bc(argv[optind]);
else {
curexpr = argv[optind];
return evaluate(argv[optind], sectorsz);
}
}

return -1;
Expand Down

0 comments on commit 5f6a41f

Please sign in to comment.