Release v2.13.0.0

The following are the changes in this release:

    Fixed typo (missing quotes) in the env rule.

    Fixed intendation problem in CHANGES.

    Combined 2.12.9.1 changes into the 2.12.8.2 to 2.12.9.0
    range, and thus renamed the range to 2.12.8.2 to 2.12.9.1.

    Fixed issues related to building Makefile.simple.

    Fixed how the Makefile variable MANPATH is set for macOS.

    Added a bunch of information to the near bottom of HOWTO.INSTALL
    on calc Makefiles.  This information discusses the various
    Makefiles found in the calc source.

    Added comments in various calc Makefiles about their origin.
    In particular, for Makefiles that are constructed such as
    Makefile.simple, custom/Makefile and custom/Makefile.simple
    there are comments about how they were made.

    For all calc Makefiles, including those in sub-directories,
    near the top there is now a line of the form:

	# SRC: ... some message about the origin ...

    Fixed how the calc(1) man page is installed under macOS.

    Fixed how calc man page in ${CATDIR} is formed.

    Fixed how Makefile.simple is formed.

    Fixed the #! calc script argument processing.  The initial #!
    line must end in a -f.  For example, if calc is in /usr/local/bin/calc,
    then the following would be the first line of a calc script:

	#!/usr/local/bin/calc -f
	...

    It is common that -q be usde with a calc script, so assuming the
    same /usr/local/bin/calc path:

	#!/usr/local/bin/calc -q -f
	...

    Use of -s in the #! first line of a calc script is not needed
    since -f implies -f.

    The argv() will now return values more typical of C's main().
    Before it returned one less than the number of arguments.  Now,
    for example, when calc is given 2 args, argv() will return 3.

    The value of argv(0) will be the path to calc, or in the
    case of a #! calc cscript, it will return the name of the script.

    Updated the calc man page and help/argv to reflect the
    above changes.

    Improved the formatting of the calc man page.

    Fixed the formation of the win32 sub-directory via the win32_hsrc
    Makefile rule.

    Due to incompatible changes to the argv() function, and #! calc
    scripts, we are setting the version to the next minor number:

	2.13.0
This commit is contained in:
Landon Curt Noll
2021-03-27 05:55:50 -07:00
parent ca0aaa0c3a
commit 04861939fc
23 changed files with 312 additions and 210 deletions

45
BUGS
View File

@@ -122,51 +122,6 @@ Known bugs in calc:
Calc shell scripts do not read from stdin properly, Calc shell scripts do not read from stdin properly,
we all as a number of the cscript examples. we all as a number of the cscript examples.
The argv() function is behaving differently
when run in calc shell script mode:
When calc is run as:
calc -s a bb ccc
and given this input on the command prompt:
print "config(\"program\")=", config("program");
print "argv()=", argv();
argc = argv();
for (i=0; i < argc; ++i) {
print "argv(":i:")=", argv(i);
}
calc prints:
config("program")= calc
argv()= 3
argv(0)= a
argv(1)= bb
argv(2)= ccc
but when it is run as a script called ./simple:
#!/usr/local/bin/calc -q -s -f
print "config(\"program\")=", config("program");
print "argv()=", argv();
argc = argv();
for (i=0; i < argc; ++i) {
print "argv(":i:")=", argv(i);
}
under Linux prints:
config("program")= /usr/bin/calc
argv()= 4
argv(0)= ./simple
argv(1)= a
argv(2)= bb
argv(3)= ccc
and under macOS simply enters into interactive mode.
We are sure some more bugs exist. When you find them, please let We are sure some more bugs exist. When you find them, please let
us know! See the above for details on how to report and were to us know! See the above for details on how to report and were to
Email your bug reports and hopefully patches to fix them. Email your bug reports and hopefully patches to fix them.

38
CHANGES
View File

@@ -1,4 +1,4 @@
The following are the changes from calc version 2.12.9.2 to date: The following are the changes from calc version 2.13.0 to date:
Fixed typo (missing quotes) in the env rule. Fixed typo (missing quotes) in the env rule.
@@ -31,6 +31,42 @@ The following are the changes from calc version 2.12.9.2 to date:
Fixed how Makefile.simple is formed. Fixed how Makefile.simple is formed.
Fixed the #! calc script argument processing. The initial #!
line must end in a -f. For example, if calc is in /usr/local/bin/calc,
then the following would be the first line of a calc script:
#!/usr/local/bin/calc -f
...
It is common that -q be usde with a calc script, so assuming the
same /usr/local/bin/calc path:
#!/usr/local/bin/calc -q -f
...
Use of -s in the #! first line of a calc script is not needed
since -f implies -f.
The argv() will now return values more typical of C's main().
Before it returned one less than the number of arguments. Now,
for example, when calc is given 2 args, argv() will return 3.
The value of argv(0) will be the path to calc, or in the
case of a #! calc cscript, it will return the name of the script.
Updated the calc man page and help/argv to reflect the
above changes.
Improved the formatting of the calc man page.
Fixed the formation of the win32 sub-directory via the win32_hsrc
Makefile rule.
Due to incompatible changes to the argv() function, and #! calc
scripts, we are setting the version to the next minor number:
2.13.0
The following are the changes from calc version 2.12.8.2 to 2.12.9.1: The following are the changes from calc version 2.12.8.2 to 2.12.9.1:

View File

@@ -1153,7 +1153,7 @@ EXT=
# The default calc versions # The default calc versions
# #
VERSION= 2.12.9.1 VERSION= 2.13.0.0
# Names of shared libraries with versions # Names of shared libraries with versions
# #
@@ -2823,7 +2823,7 @@ have_unistd.h: ${MAKE_FILE}
${Q} echo '' >> $@ ${Q} echo '' >> $@
${Q} echo '/* do we have <unistd.h>? */' >> $@ ${Q} echo '/* do we have <unistd.h>? */' >> $@
-${Q} if [ X"${HAVE_UNISTD_H}" = X"YES" ]; then \ -${Q} if [ X"${HAVE_UNISTD_H}" = X"YES" ]; then \
echo '#define $@ /* yes */' >> $@; \ echo '#define HAVE_UNISTD_H /* yes */' >> $@; \
elif [ X"${HAVE_UNISTD_H}" = X"NO" ]; then \ elif [ X"${HAVE_UNISTD_H}" = X"NO" ]; then \
echo '#undef HAVE_UNISTD_H /* no */' >> $@; \ echo '#undef HAVE_UNISTD_H /* no */' >> $@; \
elif echo '#include <unistd.h>' | ${CC} -E - ${S}; then \ elif echo '#include <unistd.h>' | ${CC} -E - ${S}; then \
@@ -2859,7 +2859,7 @@ have_string.h: ${MAKE_FILE}
${Q} echo '' >> $@ ${Q} echo '' >> $@
${Q} echo '/* do we have <string.h>? */' >> $@ ${Q} echo '/* do we have <string.h>? */' >> $@
-${Q} if [ X"${HAVE_STRING_H}" = X"YES" ]; then \ -${Q} if [ X"${HAVE_STRING_H}" = X"YES" ]; then \
echo '#define $@ /* yes */' >> $@; \ echo '#define HAVE_STRING_H /* yes */' >> $@; \
elif [ X"${HAVE_STRING_H}" = X"NO" ]; then \ elif [ X"${HAVE_STRING_H}" = X"NO" ]; then \
echo '#undef HAVE_STRING_H /* no */' >> $@; \ echo '#undef HAVE_STRING_H /* no */' >> $@; \
elif echo '#include <string.h>' | ${CC} -E - ${S}; then \ elif echo '#include <string.h>' | ${CC} -E - ${S}; then \
@@ -3581,7 +3581,7 @@ have_urandom.h: ${MAKE_FILE}
${Q} echo '' >> $@ ${Q} echo '' >> $@
${Q} echo '/* do we have /dev/urandom? */' >> $@ ${Q} echo '/* do we have /dev/urandom? */' >> $@
-${Q} if [ X"${HAVE_URANDOM}" = X"YES" ]; then \ -${Q} if [ X"${HAVE_URANDOM}" = X"YES" ]; then \
echo '#define $@ /* yes */' >> $@; \ echo '#define HAVE_URANDOM /* yes */' >> $@; \
elif [ X"${HAVE_URANDOM}" = X"NO" ]; then \ elif [ X"${HAVE_URANDOM}" = X"NO" ]; then \
echo '#undef HAVE_URANDOM /* no */' >> $@; \ echo '#undef HAVE_URANDOM /* no */' >> $@; \
elif [ -r /dev/urandom ] 2>/dev/null; then \ elif [ -r /dev/urandom ] 2>/dev/null; then \
@@ -3971,10 +3971,11 @@ have_strlcat.h: have_strlcat.c banned.h have_ban_pragma.h ${MAKE_FILE}
# #
### ###
win32_hsrc: win32.mkdef ${MAKE_FILE} win32_hsrc: win32.mkdef banned.h have_ban_pragma.h alloc.h ${MAKE_FILE}
${H} echo 'forming win32 directory' ${H} echo 'forming win32 directory'
${Q} ${RM} -rf win32 ${Q} ${RM} -rf win32
${Q} ${MKDIR} -p win32 ${Q} ${MKDIR} -p win32
${Q} ${CP} banned.h have_ban_pragma.h alloc.h win32
${Q} ${CP} ${UTIL_C_SRC} win32 ${Q} ${CP} ${UTIL_C_SRC} win32
${Q} ${CP} ${UTIL_MISC_SRC} win32 ${Q} ${CP} ${UTIL_MISC_SRC} win32
${Q} ${CP} ${MAKE_FILE} win32/${MAKE_FILE} ${Q} ${CP} ${MAKE_FILE} win32/${MAKE_FILE}

50
calc.c
View File

@@ -123,6 +123,11 @@ main(int argc, char **argv)
* parse args * parse args
*/ */
program = argv[0]; program = argv[0];
script_name = strdup(argv[0]);
if (script_name == NULL) {
fprintf(stderr, "%s: failed to strdup(argv[0])\n", program);
exit(1);
}
cmdbuf[0] = '\0'; cmdbuf[0] = '\0';
cmdlen = 0; cmdlen = 0;
@@ -399,19 +404,35 @@ main(int argc, char **argv)
} }
} }
bp = cmdbuf + cmdlen; bp = cmdbuf + cmdlen;
/*
* duplicate -f filename arg
* as a new script_name value
*/
if (script_name != NULL) {
free(script_name);
}
script_name = NULL;
script_name = strdup(cp);
if (script_name == NULL) {
fprintf(stderr,
"strdup(-f argument)"
"failed\n");
exit(17);
}
/* process -f filename arg */
if (haveendstr) { if (haveendstr) {
len = strlen(cp); len = strlen(cp);
if (len == 0) { if (len == 0) {
fprintf(stderr, fprintf(stderr,
"Null" "Null"
" filename!"); " filename!\n");
exit(17); exit(18);
} }
if (cmdlen + len + 2 > MAXCMD) { if (cmdlen + len + 2 > MAXCMD) {
fprintf(stderr, fprintf(stderr,
"Commands too" "Commands too"
" long"); " long\n");
exit(18); exit(19);
} }
/* XXX - what if *cp = '\''? */ /* XXX - what if *cp = '\''? */
*bp++ = '\''; *bp++ = '\'';
@@ -423,10 +444,10 @@ main(int argc, char **argv)
} else { } else {
do { do {
if (cmdlen > MAXCMD) { if (cmdlen > MAXCMD) {
fprintf(stderr, fprintf(stderr,
"Commands" "Commands"
" too long"); " too long\n");
exit(19); exit(20);
} }
*bp++ = *cp++; *bp++ = *cp++;
cmdlen++; cmdlen++;
@@ -439,7 +460,10 @@ main(int argc, char **argv)
*bp++ = ';'; *bp++ = ';';
*bp = '\0'; *bp = '\0';
cmdlen++; cmdlen++;
s_flag = TRUE; /* -f implies -s */
/* -f implies -s */
s_flag = TRUE;
maxindex = index + 1;
break; break;
case 's': case 's':
@@ -462,7 +486,7 @@ main(int argc, char **argv)
"usage: %s ... -f filename\n" "usage: %s ... -f filename\n"
"1st cscript line: #/path/to/calc ... -s -f\n", "1st cscript line: #/path/to/calc ... -s -f\n",
program, program); program, program);
exit(20); exit(21);
} }
if (havearg) if (havearg)
break; break;
@@ -488,7 +512,7 @@ main(int argc, char **argv)
fprintf(stderr, fprintf(stderr,
"%s: commands too long\n", "%s: commands too long\n",
program); program);
exit(21); exit(22);
} }
strlcpy(cmdbuf + cmdlen, cp, cplen+1); strlcpy(cmdbuf + cmdlen, cp, cplen+1);
cmdbuf[newcmdlen] = '\0'; cmdbuf[newcmdlen] = '\0';
@@ -511,7 +535,7 @@ main(int argc, char **argv)
} }
} }
argc_value = argc - maxindex; argc_value = argc - maxindex + 1;
argv_value = argv + maxindex; argv_value = argv + maxindex;
/* /*
@@ -799,7 +823,7 @@ calc_interrupt(char *fmt, ...)
* don't call libcalc_call_me_last() -- we might loop * don't call libcalc_call_me_last() -- we might loop
* and besides ... this is an unusual internal error case * and besides ... this is an unusual internal error case
*/ */
exit(22); exit(24);
} }
} }

1
calc.h
View File

@@ -216,6 +216,7 @@ EXTERN int havecommands; /* TRUE if have cmd args) */
EXTERN char *program; /* our name */ EXTERN char *program; /* our name */
EXTERN char *base_name; /* basename of our name */ EXTERN char *base_name; /* basename of our name */
EXTERN char cmdbuf[]; /* command line expression */ EXTERN char cmdbuf[]; /* command line expression */
EXTERN char *script_name; /* program name or -f filename arg or NULL */
EXTERN int abortlevel; /* current level of aborts */ EXTERN int abortlevel; /* current level of aborts */
EXTERN BOOL inputwait; /* TRUE if in a terminal input wait */ EXTERN BOOL inputwait; /* TRUE if in a terminal input wait */

226
calc.man
View File

@@ -37,6 +37,7 @@ calc \- arbitrary precision calculator
.RB [ -D\ \&calc_debug[:resource_debug[:user_debug]] ] .RB [ -D\ \&calc_debug[:resource_debug[:user_debug]] ]
.br .br
.RB [ \-e ] .RB [ \-e ]
.RB [ \-f\ \&filename ]
.RB [ \-h ] .RB [ \-h ]
.RB [ \-i ] .RB [ \-i ]
.RB [ \-m\ \&mode ] .RB [ \-m\ \&mode ]
@@ -50,11 +51,12 @@ calc \- arbitrary precision calculator
.RB [ [\-\-]\ calc_cmd\ \&.\|.\|. ] .RB [ [\-\-]\ calc_cmd\ \&.\|.\|. ]
.in -5n .in -5n
.sp .sp
\fI#!${BINDIR}/calc\fP\ [other_flags\ \&...] \fB\-s\fP \fB\-f\fP \fI#!${BINDIR}/calc\fP\ [optional_other_flags\ \&...] \fB\-f\fP
.PP
.SH DESCRIPTION .SH DESCRIPTION
\&
.br
CALC OPTIONS
.PP .PP
.TP .TP
@@ -218,48 +220,55 @@ Ignore any environment variables on startup.
The getenv() builtin will still return values, however. The getenv() builtin will still return values, however.
.TP .TP
.B \-f .BR \-f " filename"
This flag is normally only with calc shell scripts. This flag is normally only with calc shell scripts.
.sp 1 .sp 1
This flag is required when using calc in This flag is required when using calc in
.BR "shell script mode" . .BR "shell script mode" .
It must be at the end of the initial It must be at the end of the initial
.B #! .B #!
line of the script line of the script, as in:
and must be immediately preceded by the
.B \-s
flag.
.sp 1
If the first line of an executable file begins
.B #!
followed by the absolute pathname of the
.B calc
program and if the first line ends with the two flags
.B \-s
.B \-f
as in:
.sp 1 .sp 1
.in +5n .in +5n
.nf .nf
\fI#!${BINDIR}/calc\fP\ [other_flags\ \&...] \fB\-s\fP \fB\-f\fP \fI#!${BINDIR}/calc\fP\ [optional_other_flags\ \&...] \fB\-f\fP
.fi .fi
.in -5n .in -5n
.sp 1 .sp 1
the rest of the file will be processed in the rest of the file will be processed in
.BR "shell script mode" . .BR "shell script mode" .
.sp 1
A common flag to use, prior to the
.B \-f
on the #! line is the
.B \-q
flag.
For example:
.sp 1
.in +5n
.nf
\fI#!${BINDIR}/calc\fP \fB\-q\fP \fB\-f\fP
.fi
.in -5n
.sp 1
This allows the script to run independely of
startup scripts.
.sp 1
See See
.B "SHELL SCRIPT MODE" .B "SHELL SCRIPT MODE"
section of this man page section of this man page
below for details. below for details.
.sp 1 .sp 1
The actual form of this flag is: While the actual form of this flag is:
.sp 1 .sp 1
.in +5n .in +5n
.B \-f .BR \-f
filename filename
.in -5n .in -5n
.sp 1 .sp 1
On systems that treat an executable that begins with for systems that treat an executable that begins with
.B #! .B #!
as a script, the path of the executable is appended by the kernel as a script, the path of the executable is appended by the kernel
as the final argument to the exec() system call. as the final argument to the exec() system call.
@@ -270,12 +279,13 @@ flag at the very end of the
line. line.
.sp 1 .sp 1
It is possible use It is possible use
.B \-f\ filename .B \-f
filename
on the command line: on the command line:
.sp 1 .sp 1
.in +5n .in +5n
.nf .nf
\fIcalc\fP\ [other_flags\ \&...] \fB\-s\fP \fB\-f\fP filename \fIcalc\fP\ [optional_other_flags\ \&...] \fB\-f\fP filename
.fi .fi
.in -5n .in -5n
.sp 1 .sp 1
@@ -283,28 +293,14 @@ This will cause calc to process lines in
.B filename .B filename
in in
.BR "shell script mode" . .BR "shell script mode" .
.sp 1
Use of
.B \-f
implies
.BR \-s .
However in a calc shell script,
one must include
.B \-f
before
.B \-s
on the initial
.B #!
line.
.sp 1 .sp 1
In addition, The use of
.B \-d .B \-f
and filename
.B \-p implies the
are implied if .B \-s
.B \-i flag.
is not given.
.TP .TP
.B \-h .B \-h
@@ -491,16 +487,16 @@ This is useful when entering negative values on the command line as in:
.in +5n .in +5n
.nf .nf
calc \-p \-\- \-1 - -7 calc \-p \-\- \-1 - -7
.sp 1
.fi .fi
.in -5n .in -5n
.sp 1
.PP .PP
\& .SH CALC COMMAND LINE
.br
CALC COMMAND LINE
.PP .PP
With no With no
.I calc_cmd .I calc_cmd
arguments, arguments,
@@ -638,10 +634,10 @@ to parse interactive commands, flushes data on standard input.
.PP .PP
\& .SH CALC STARTUP FILES
.br
CALC STARTUP FILES
.PP .PP
Normally on startup, Normally on startup,
if the environment variable if the environment variable
.B $CALCRC .B $CALCRC
@@ -678,10 +674,12 @@ files is also disabled as if
.B \-q .B \-q
was given. was given.
\&
.br
CALC FILE SEARCH PATH
.PP .PP
.SH CALC FILE SEARCH PATH
.PP
If the environment variable If the environment variable
.B $CALCPATH .B $CALCPATH
is undefined, or if it is undefined, or if it
@@ -758,24 +756,24 @@ help config
.fi .fi
.in -5n .in -5n
.sp 1 .sp 1
.PP .PP
\& .SH SHELL SCRIPT MODE
.br
SHELL SCRIPT MODE
.PP .PP
If the first line of an executable file begins If the first line of an executable file begins
.B #! .B #!
followed by the absolute pathname of the followed by the absolute pathname of the
.B calc .B calc
program and the first line ends with the two flags program and the first line ends with the flag
.B \-s
.B \-f .B \-f
as in: as in:
.sp 1 .sp 1
.in +5n .in +5n
.nf .nf
\fI#!${BINDIR}/calc\fP\ [other_flags\ \&...] \fB\-s\fP \fB\-f\fP \fI#!${BINDIR}/calc\fP\ [optional_other_flags\ \&...] \fB\-f\fP
.fi .fi
.in -5n .in -5n
.sp 1 .sp 1
@@ -786,11 +784,11 @@ Note that
.B \-f .B \-f
must at the end of the initial ``#!'' line. must at the end of the initial ``#!'' line.
Any other optional Any other optional
.B "other_flags" .B "optional_other_flags"
must come before must come before
the the
.B \-s .B \-f
.BR \-f . flag.
.sp 1 .sp 1
In In
.B "shell script mode" .B "shell script mode"
@@ -799,7 +797,7 @@ executed as if they were in a file being processed by a read
command, except that a "command" beginning with '#' followed by command, except that a "command" beginning with '#' followed by
whitespace and ending at the next newline is treated as a comment. whitespace and ending at the next newline is treated as a comment.
Any optional Any optional
.B "other_flags" .B "optional_other_flags"
will be parsed first followed by will be parsed first followed by
the later lines within the script itself. the later lines within the script itself.
.sp 1 .sp 1
@@ -821,7 +819,7 @@ the file
.sp 1 .sp 1
.in +5n .in +5n
.nf .nf
\fI#!${BINDIR}/calc\fP\ \&\fB\-q\fP \&\fB\-s\fP \fB\-f\fP \fI#!${BINDIR}/calc\fP\ \&\fB\-q\fP \fB\-f\fP
/* setup */ /* setup */
argc = argv(); argc = argv();
@@ -894,12 +892,13 @@ whereas
.in -5n .in -5n
.sp 1 .sp 1
will not. will not.
.PP .PP
\& .SH DATA TYPES
.br
DATA TYPES
.PP .PP
Fundamental builtin data types include integers, real numbers, Fundamental builtin data types include integers, real numbers,
rational numbers, complex numbers and strings. rational numbers, complex numbers and strings.
.PP .PP
@@ -922,12 +921,13 @@ help obj
.br .br
show objfuncs show objfuncs
.in -1.0i .in -1.0i
.PP .PP
\& .SH VARIABLES
.br
VARIABLES
.PP .PP
Variables in \fIcalc\fP are typeless. Variables in \fIcalc\fP are typeless.
In other words, the fundamental type of a variable is determined by its content. In other words, the fundamental type of a variable is determined by its content.
Before a variable is assigned a value it has the value of zero. Before a variable is assigned a value it has the value of zero.
@@ -950,12 +950,13 @@ help list
.br .br
show globals show globals
.in -1.0i .in -1.0i
.PP .PP
\& .SH INPUT/OUTPUT
.br
INPUT/OUTPUT
.PP .PP
A leading ``0x'' implies a hexadecimal value, A leading ``0x'' implies a hexadecimal value,
a leading ``0b'' implies a binary value, a leading ``0b'' implies a binary value,
and a ``0'' followed by a digit implies an octal value. and a ``0'' followed by a digit implies an octal value.
@@ -985,12 +986,13 @@ command:
.in 1.0i .in 1.0i
help file help file
.in -1.0i .in -1.0i
.PP .PP
\& .SH CALC LANGUAGE
.br
CALC LANGUAGE
.PP .PP
The \fIcalc\fP language is a C-like language. The \fIcalc\fP language is a C-like language.
The language includes commands such as variable declarations, The language includes commands such as variable declarations,
expressions, tests, labels, loops, file operations, function calls. expressions, tests, labels, loops, file operations, function calls.
@@ -1017,10 +1019,13 @@ help operator
.br .br
help config help config
.in -1.0i .in -1.0i
.PP .PP
.SH FILES .SH FILES
\&
.br .PP
.PD 0 .PD 0
.TP 5 .TP 5
${BINDIR}/calc ${BINDIR}/calc
@@ -1062,9 +1067,13 @@ custom resource files
${CUSTOMHELPDIR}/* ${CUSTOMHELPDIR}/*
custom help files custom help files
.sp 1 .sp 1
.PP
.SH ENVIRONMENT .SH ENVIRONMENT
\&
.br .PP
.PD 0 .PD 0
.TP 5 .TP 5
CALCPATH CALCPATH
@@ -1123,9 +1132,13 @@ Location of the calc custom help directory.
.sp .sp
Default value: ${CUSTOMHELPDIR} Default value: ${CUSTOMHELPDIR}
.sp .sp
.PP
.SH CREDIT .SH CREDIT
\&
.br .PP
The main chunk of The main chunk of
.B calc .B calc
was written by David I. Bell. was written by David I. Bell.
@@ -1154,10 +1167,13 @@ public domain arbitrarily precision routines which was posted
to the net around 1984. to the net around 1984.
By now, there is almost no recognizable By now, there is almost no recognizable
code left from that original source. code left from that original source.
.sp
.PP
.SH "COPYING / CALC GNU LESSER GENERAL PUBLIC LICENSE" .SH "COPYING / CALC GNU LESSER GENERAL PUBLIC LICENSE"
\&
.sp .PP
Calc is open software, and is Calc is open software, and is
covered under version 2.1 of the GNU Lesser General Public License. covered under version 2.1 of the GNU Lesser General Public License.
You are You are
@@ -1217,10 +1233,13 @@ Copyright (C) 1999-2021 Landon Curt Noll
.sp .sp
and is covered under version 2.1 GNU Lesser General and is covered under version 2.1 GNU Lesser General
Public License. Public License.
.sp
.PP
.SH "CALC QUESTIONS" .SH "CALC QUESTIONS"
\&
.br .PP
If you have a simple general question about calc, send Email to: If you have a simple general question about calc, send Email to:
.sp .sp
.in +0.5i .in +0.5i
@@ -1276,11 +1295,13 @@ nor can we do your homework, nor can
we do much more than answer short general questions about calc. we do much more than answer short general questions about calc.
.sp .sp
Please be patient as we cannot always respond to Email messages quickly. Please be patient as we cannot always respond to Email messages quickly.
.sp
.PP
.SH "BUG REPORTS / BUG FIXES" .SH "BUG REPORTS / BUG FIXES"
\&
.br .PP
.sp
Send bug reports and bug fixes to: Send bug reports and bug fixes to:
.sp .sp
.in +0.5i .in +0.5i
@@ -1346,11 +1367,13 @@ help bugs
for more information about bug reporting. for more information about bug reporting.
.sp .sp
Please be patient as we cannot always respond to Email messages quickly. Please be patient as we cannot always respond to Email messages quickly.
.sp
.PP
.SH "CONTRIBUTING CODE TO CALC" .SH "CONTRIBUTING CODE TO CALC"
\&
.br .PP
.sp
.I Calc .I Calc
is open source. is open source.
Contributions of code are welcome. Contributions of code are welcome.
@@ -1473,10 +1496,13 @@ you have that
somewhere in the subject line! somewhere in the subject line!
.sp .sp
Please be patient as we cannot always respond to Email messages quickly. Please be patient as we cannot always respond to Email messages quickly.
.sp
.PP
.SH "CALC WEB SITE" .SH "CALC WEB SITE"
\&
.br .PP
Landon Noll maintains the Landon Noll maintains the
.B calc .B calc
web site is located at: web site is located at:

View File

@@ -1,7 +1,9 @@
#!/usr/local/src/bin/calc/calc -q -s -f #!/usr/local/src/bin/calc/calc -q -f
/* /*
* 4dsphere - determine if 6 points lie on the surface of a sphere in R^4 * 4dsphere - determine if 6 points lie on the surface of a sphere in R^4
* *
* Copyright (C) 2001,2014,2019,2021 Landon Curt Noll
*
* usage: * usage:
* 4dsphere x0 y0 z0 w0 x1 y1 z1 w1 ... x5 y5 z5 w5 * 4dsphere x0 y0 z0 w0 x1 y1 z1 w1 ... x5 y5 z5 w5
* *
@@ -10,8 +12,6 @@
* ... ... * ... ...
* x5 y5 z5 w5 point 5 in R^4 * x5 y5 z5 w5 point 5 in R^4
* *
* Copyright (C) 2001,2014,2019 Landon Curt Noll
*
* Calc is open software; you can redistribute it and/or modify it under * Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License * the terms of the version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation. * as published by the Free Software Foundation.

View File

@@ -1,15 +1,15 @@
#!/usr/local/src/bin/calc/calc -q -s -f #!/usr/local/src/bin/calc/calc -q -f
/* /*
* fproduct - write the big Endian product of terms to a file * fproduct - write the big Endian product of terms to a file
* *
* Copyright (C) 2001,2014,2019,2021 Landon Curt Noll
*
* usage: * usage:
* fproduct filename term [term ...] * fproduct filename term [term ...]
* *
* filename where to write the product, use - for stdout * filename where to write the product, use - for stdout
* term ... terms to multiply * term ... terms to multiply
* *
* Copyright (C) 2001,2014,2019 Landon Curt Noll
*
* Calc is open software; you can redistribute it and/or modify it under * Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License * the terms of the version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation. * as published by the Free Software Foundation.

View File

@@ -1,8 +1,8 @@
#!/usr/local/src/bin/calc/calc -q -s -f #!/usr/local/src/bin/calc/calc -q -f
/* /*
* mersenne - print the value of a mersenne number * mersenne - print the value of a mersenne number
* *
* Copyright (C) 1999-2007,2014,2019 Landon Curt Noll * Copyright (C) 1999-2007,2014,2019,2021 Landon Curt Noll
* *
* Calc is open software; you can redistribute it and/or modify it under * Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License * the terms of the version 2.1 of the GNU Lesser General Public License

View File

@@ -1,4 +1,4 @@
#!/usr/local/src/bin/calc/calc -q -s -f #!/usr/local/src/bin/calc/calc -q -f
/* /*
* piforever - print digits of pi forever (or as long as your mem/CPU allow) * piforever - print digits of pi forever (or as long as your mem/CPU allow)
* *

View File

@@ -1,8 +1,8 @@
#!/usr/local/src/bin/calc/calc -q -s -f #!/usr/local/src/bin/calc/calc -q -f
/* /*
* plus - add two or more arguments together * plus - add two or more arguments together
* *
* Copyright (C) 1999-2007,2014,2019 Landon Curt Noll * Copyright (C) 1999-2007,2014,2019,2021 Landon Curt Noll
* *
* Calc is open software; you can redistribute it and/or modify it under * Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License * the terms of the version 2.1 of the GNU Lesser General Public License
@@ -47,7 +47,7 @@ if (argc < 2) {
* Since args are strings, we must eval them before using them numerically. * Since args are strings, we must eval them before using them numerically.
*/ */
sum = 0; sum = 0;
for (i=1; i < argv(); ++i) { for (i=1; i < argc; ++i) {
sum += eval(argv(i)); sum += eval(argv(i));
} }
print sum; print sum;

View File

@@ -1,14 +1,22 @@
#!/usr/local/src/bin/calc/calc -q -s -f #!/usr/local/src/bin/calc/calc -q -f
/* /*
* powerterm - print the argument as a sum of powers of integers * powerterm - print the argument as a sum of powers of integers
* *
* Copyright (C) 2001,2014,2019,2021 Landon Curt Noll
*
* usage: * usage:
* powerterm [base_limit] value * powerterm [base_limit] value
* *
* base_limit largest base we will consider (def: 10000) * base_limit largest base we will consider (def: 10000)
* value value to convert into sums of powers of integers * value value to convert into sums of powers of integers
* *
* Copyright (C) 2001,2014,2019 Landon Curt Noll * Example:
*
* powerterm 5 1000000
*
* prints:
*
* 4^10 - 3^10 + 5^6 - 4^6 - 4^5 - 2^5
* *
* Calc is open software; you can redistribute it and/or modify it under * Calc is open software; you can redistribute it and/or modify it under
* the powerterm of the version 2.1 of the GNU Lesser General Public License * the powerterm of the version 2.1 of the GNU Lesser General Public License

View File

@@ -1,8 +1,8 @@
#!/usr/local/src/bin/calc/calc -q -s -f #!/usr/local/src/bin/calc/calc -q -f
/* /*
* simple - an example of a simple calc shell script * simple - an example of a simple calc shell script
* *
* Copyright (C) 1999-2007,2014,2019 Landon Curt Noll * Copyright (C) 1999-2007,2014,2019,2021 Landon Curt Noll
* *
* Calc is open software; you can redistribute it and/or modify it under * Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License * the terms of the version 2.1 of the GNU Lesser General Public License

View File

@@ -1,8 +1,8 @@
#!/usr/local/src/bin/calc/calc -q -s -f #!/usr/local/src/bin/calc/calc -q -p -f
/* /*
* sqaure - print the squares of input values * sqaure - print the squares of input values
* *
* Copyright (C) 2000-2007,2014,2019 Ernest Bowen * Copyright (C) 2000-2007,2014,2019,2021 Ernest Bowen
* *
* Calc is open software; you can redistribute it and/or modify it under * Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License * the terms of the version 2.1 of the GNU Lesser General Public License
@@ -48,6 +48,8 @@
* 1522756 * 1522756
* ln(2) * ln(2)
* ~.48045301391820142467 * ~.48045301391820142467
* 2i
* -4
* config("mode","frac"), bernoulli(10) * config("mode","frac"), bernoulli(10)
* 25/4356 * 25/4356
* config("mode", "hex"), 0xff * config("mode", "hex"), 0xff

View File

@@ -458,7 +458,7 @@ EXT=
# The default calc versions # The default calc versions
# #
VERSION= 2.12.9.1 VERSION= 2.13.0.0
# Names of shared libraries with versions # Names of shared libraries with versions
# #

View File

@@ -458,7 +458,7 @@ EXT=
# The default calc versions # The default calc versions
# #
VERSION= 2.12.9.1 VERSION= 2.13.0.0
# Names of shared libraries with versions # Names of shared libraries with versions
# #

18
func.c
View File

@@ -8464,11 +8464,23 @@ f_argv(int count, VALUE **vals)
/*NOTREACHED*/ /*NOTREACHED*/
} }
/* return the n-th argv string */ /* determine the arg value of the argv() function */
arg = qtoi(vals[0]->v_num); arg = qtoi(vals[0]->v_num);
if (arg < argc_value && argv_value[arg] != NULL) {
/* argv(0) is program or script_name if -f filename was used */
if (arg == 0) {
if (script_name == NULL) {
/* paranoia */
result.v_type = V_NULL;
} else {
result.v_type = V_STR;
result.v_str = makenewstring(script_name);
}
/* return the n-th argv string */
} else if (arg < argc_value && argv_value[arg-1] != NULL) {
result.v_type = V_STR; result.v_type = V_STR;
result.v_str = makestring(strdup(argv_value[arg])); result.v_str = makestring(strdup(argv_value[arg-1]));
} else { } else {
result.v_type = V_NULL; result.v_type = V_NULL;
} }

View File

@@ -11,14 +11,33 @@ TYPES
DESCRIPTION DESCRIPTION
Without args, this builtin returns the number of calc command line Without args, this builtin returns the number of calc command line
strings available. strings available, including the program or script name.
If the numeric arg is supplied, then the corresponding command line If the numeric arg is supplied, then the corresponding command line
string is return, if it exists. Otherwise a nul() value is returned. string is return, if it exists. Otherwise a nul() value is returned.
In keeping with the argc/argv convention of C, argv(0) will refer In keeping with the argc/argv convention of C, argv(0) will refer
to the 1st argv string, and argv(argv()-1) will refer to the last. to the name of the program. If the -f filename argument is used,
This differs from the way the param() builtin works. then argv(0) will refer to the filename. In the case of a #! script,
assuming the calc is located in /usr/local/bin/calc, the first
line of a calc script would be:
#!/usr/local/bin/calc -f
...
then argv(0) will refer to the filename of the script.
As it is common to use -q in a calc script, and using that same
/usr/local/bin/calc location:
#!/usr/local/bin/calc -q -f
...
and again, argv(0) will refer to the filename of the script.
The 1st argument to calc will be argv(1), and argv(argv()-1)
will refer to the last. This differs from the way the param()
builtin works.
By default, calc will evaluate all of its command line arguments. By default, calc will evaluate all of its command line arguments.
However, if calc is invoked with -s, all non-dashed options will However, if calc is invoked with -s, all non-dashed options will
@@ -27,22 +46,26 @@ DESCRIPTION
calc -i 2+2 calc -i 2+2
will cause calc to print 4 and enter interactive mode. In this case will cause calc to print 4 and enter interactive mode. In this case
argv() will return 0. argv() will return 1.
On the other hand: On the other hand:
calc -i -s 2+2 calc -i -s 2+2
will cause calc to interactive mode. The argv() builtin will return 1 will cause calc to interactive mode. The argv() builtin will return 2
and argv(0) will return the string "2+2". and argv(1) will return the string "2+2".
EXAMPLE EXAMPLE
$ calc -s a bb ccc $ calc -s a bb ccc
; argc = argv(); ; argc = argv();
; for (i = 0; i < argc; i++) print "argv[": i : '] = "': argv(i) : '"'; ; print "argc =", argc;
argv[0] = "a" argc = 4
argv[1] = "bb" ; print 'argv[0] = "': argv(0) : '"';
argv[2] = "ccc" argv[0] = "calc"
; for (i = 1; i < argc; i++) print "argv[": i : '] = "': argv(i) : '"';
argv[1] = "a"
argv[2] = "bb"
argv[3] = "ccc"
LIMITS LIMITS
0 <= n < 2^31 0 <= n < 2^31
@@ -53,7 +76,7 @@ LINK LIBRARY
SEE ALSO SEE ALSO
param, system, usage param, system, usage
## Copyright (C) 1999-2006 Landon Curt Noll ## Copyright (C) 1999-2006,2021 Landon Curt Noll
## ##
## Calc is open software; you can redistribute it and/or modify it under ## Calc is open software; you can redistribute it and/or modify it under
## the terms of the version 2.1 of the GNU Lesser General Public License ## the terms of the version 2.1 of the GNU Lesser General Public License

View File

@@ -123,7 +123,7 @@ Calc shell scripts
calc "-q -s" $* << + calc "-q -s" $* << +
global i, n, s; global i, n, s;
n = argv(); n = argv();
for (i = 0; i < n; i++) for (i = 1; i < n; i++)
s += eval(argv(i)); s += eval(argv(i));
print "sum =", s; print "sum =", s;
+ +
@@ -133,20 +133,24 @@ Calc shell scripts
./addall2 2 3 4 ./addall2 2 3 4
the $* in this script expands to 2 3 4, and because of the "-s" the $* in this script expands to 2 3 4, and because of the "-s"
in the options, calc starts with argv(0) = "2", argv(1) = "3", in the options, calc starts with argv(1) = "2", argv(2) = "3",
argv(2)= "4". As there is only one calc process involved and argv(3)= "4". As there is only one calc process involved and
the eval() function accepts as argument any string that the eval() function accepts as argument any string that
represents the body of a calc function, the strings argv(0), represents the body of a calc function, the strings argv(1),
argv(1), ... could evaluate to any value types for which the argv(2), ... could evaluate to any value types for which the
additions to be performed are defined, and variables defined in additions to be performed are defined, and variables defined in
one argv() can be used in later arguments. one argv() can be used in later arguments.
In case you are wondering, argv(0) returns the program
or calc script name. In the case of the above example,
argv(0) = "./addall2".
For systems that support interpreter files, essentially the For systems that support interpreter files, essentially the
same thing may be done more efficiently by using calc as an same thing may be done more efficiently by using calc as an
interpreter. Assuming the full path for calc is interpreter. Assuming the full path for calc is
/usr/local/bin/calc, one could use the file addall3 with contents /usr/local/bin/calc, one could use the file addall3 with contents
#!/usr/bin/calc -q -s -f #!/usr/bin/calc -q -f
global i, n, s; global i, n, s;
n = argv(); n = argv();
for (i = 1; i < n; i++) for (i = 1; i < n; i++)
@@ -161,9 +165,9 @@ Calc shell scripts
After the command: After the command:
addall3 2 3 4 ./addall3 2 3 4
the arguments calc receives are argv(0) = "addall3", argv(1) = the arguments calc receives are argv(0) = "./addall3", argv(1) =
"2", argv(3) = "3", argv(4) = "4". "2", argv(3) = "3", argv(4) = "4".
Another kind of script that can be useful is sqrts1: Another kind of script that can be useful is sqrts1:

View File

@@ -119,6 +119,7 @@ char *program = "calc"; /* our name */
char *base_name = "calc"; /* basename of our name */ char *base_name = "calc"; /* basename of our name */
char cmdbuf[MAXCMD+1+1+1]; /* command line expression + "\n\0" + guard */ char cmdbuf[MAXCMD+1+1+1]; /* command line expression + "\n\0" + guard */
run run_state = RUN_ZERO; /* calc startup run state */ run run_state = RUN_ZERO; /* calc startup run state */
char *script_name = NULL; /* program name or -f filename arg or NULL */
/* /*
@@ -614,6 +615,14 @@ libcalc_call_me_last(void)
*/ */
random_libcalc_cleanup(); random_libcalc_cleanup();
/*
* free script_name
*/
if (script_name != NULL) {
free(script_name);
script_name = NULL;
}
/* /*
* restore all changed descriptor states * restore all changed descriptor states
*/ */

View File

@@ -1,5 +1,5 @@
/* /*
* math_error - a simple libcalc math error routine * lib_calc - calc link library initialization and shutdown routines
* *
* Copyright (C) 1999-2007,2014,2021 Landon Curt Noll * Copyright (C) 1999-2007,2014,2021 Landon Curt Noll
* *

View File

@@ -52,9 +52,9 @@ static char *program;
#define MAJOR_VER 2 /* major library version */ #define MAJOR_VER 2 /* major library version */
#define MINOR_VER 12 /* minor library version */ #define MINOR_VER 13 /* minor library version */
#define MAJOR_PATCH 9 /* major software version level */ #define MAJOR_PATCH 0 /* major software version level */
#define MINOR_PATCH 1 /* minor software version level */ #define MINOR_PATCH 0 /* minor software version level */
/* /*

View File

@@ -54,6 +54,7 @@ USE_READLINE=-DUSE_READLINE
READLINE_LIB= READLINE_LIB=
READLINE_INCLUDE= READLINE_INCLUDE=
CALCPAGER=less.exe CALCPAGER=less.exe
CCBAN=-DUNBAN
EXT=.exe EXT=.exe