diff --git a/BUGS b/BUGS index 4d707fe..2c5df6a 100644 --- a/BUGS +++ b/BUGS @@ -122,51 +122,6 @@ Known bugs in calc: Calc shell scripts do not read from stdin properly, 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 us know! See the above for details on how to report and were to Email your bug reports and hopefully patches to fix them. diff --git a/CHANGES b/CHANGES index 5fe9b37..020ad5d 100644 --- a/CHANGES +++ b/CHANGES @@ -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. @@ -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 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: diff --git a/Makefile.ship b/Makefile.ship index ae5c92c..30a2e12 100644 --- a/Makefile.ship +++ b/Makefile.ship @@ -1153,7 +1153,7 @@ EXT= # The default calc versions # -VERSION= 2.12.9.1 +VERSION= 2.13.0.0 # Names of shared libraries with versions # @@ -2823,7 +2823,7 @@ have_unistd.h: ${MAKE_FILE} ${Q} echo '' >> $@ ${Q} echo '/* do we have ? */' >> $@ -${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 \ echo '#undef HAVE_UNISTD_H /* no */' >> $@; \ elif echo '#include ' | ${CC} -E - ${S}; then \ @@ -2859,7 +2859,7 @@ have_string.h: ${MAKE_FILE} ${Q} echo '' >> $@ ${Q} echo '/* do we have ? */' >> $@ -${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 \ echo '#undef HAVE_STRING_H /* no */' >> $@; \ elif echo '#include ' | ${CC} -E - ${S}; then \ @@ -3581,7 +3581,7 @@ have_urandom.h: ${MAKE_FILE} ${Q} echo '' >> $@ ${Q} echo '/* do we have /dev/urandom? */' >> $@ -${Q} if [ X"${HAVE_URANDOM}" = X"YES" ]; then \ - echo '#define $@ /* yes */' >> $@; \ + echo '#define HAVE_URANDOM /* yes */' >> $@; \ elif [ X"${HAVE_URANDOM}" = X"NO" ]; then \ echo '#undef HAVE_URANDOM /* no */' >> $@; \ 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' ${Q} ${RM} -rf 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_MISC_SRC} win32 ${Q} ${CP} ${MAKE_FILE} win32/${MAKE_FILE} diff --git a/calc.c b/calc.c index d52efb3..566f4d2 100644 --- a/calc.c +++ b/calc.c @@ -123,6 +123,11 @@ main(int argc, char **argv) * parse args */ 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'; cmdlen = 0; @@ -399,19 +404,35 @@ main(int argc, char **argv) } } 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) { len = strlen(cp); if (len == 0) { fprintf(stderr, "Null" - " filename!"); - exit(17); + " filename!\n"); + exit(18); } if (cmdlen + len + 2 > MAXCMD) { fprintf(stderr, "Commands too" - " long"); - exit(18); + " long\n"); + exit(19); } /* XXX - what if *cp = '\''? */ *bp++ = '\''; @@ -423,10 +444,10 @@ main(int argc, char **argv) } else { do { if (cmdlen > MAXCMD) { - fprintf(stderr, - "Commands" - " too long"); - exit(19); + fprintf(stderr, + "Commands" + " too long\n"); + exit(20); } *bp++ = *cp++; cmdlen++; @@ -439,7 +460,10 @@ main(int argc, char **argv) *bp++ = ';'; *bp = '\0'; cmdlen++; - s_flag = TRUE; /* -f implies -s */ + + /* -f implies -s */ + s_flag = TRUE; + maxindex = index + 1; break; case 's': @@ -462,7 +486,7 @@ main(int argc, char **argv) "usage: %s ... -f filename\n" "1st cscript line: #/path/to/calc ... -s -f\n", program, program); - exit(20); + exit(21); } if (havearg) break; @@ -488,7 +512,7 @@ main(int argc, char **argv) fprintf(stderr, "%s: commands too long\n", program); - exit(21); + exit(22); } strlcpy(cmdbuf + cmdlen, cp, cplen+1); 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; /* @@ -799,7 +823,7 @@ calc_interrupt(char *fmt, ...) * don't call libcalc_call_me_last() -- we might loop * and besides ... this is an unusual internal error case */ - exit(22); + exit(24); } } diff --git a/calc.h b/calc.h index c015f02..0ac7e1a 100644 --- a/calc.h +++ b/calc.h @@ -216,6 +216,7 @@ EXTERN int havecommands; /* TRUE if have cmd args) */ EXTERN char *program; /* our name */ EXTERN char *base_name; /* basename of our name */ 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 BOOL inputwait; /* TRUE if in a terminal input wait */ diff --git a/calc.man b/calc.man index 644f337..97b1840 100644 --- a/calc.man +++ b/calc.man @@ -37,6 +37,7 @@ calc \- arbitrary precision calculator .RB [ -D\ \&calc_debug[:resource_debug[:user_debug]] ] .br .RB [ \-e ] +.RB [ \-f\ \&filename ] .RB [ \-h ] .RB [ \-i ] .RB [ \-m\ \&mode ] @@ -50,11 +51,12 @@ calc \- arbitrary precision calculator .RB [ [\-\-]\ calc_cmd\ \&.\|.\|. ] .in -5n .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 -\& -.br -CALC OPTIONS + .PP .TP @@ -218,48 +220,55 @@ Ignore any environment variables on startup. The getenv() builtin will still return values, however. .TP -.B \-f +.BR \-f " filename" This flag is normally only with calc shell scripts. .sp 1 This flag is required when using calc in .BR "shell script mode" . It must be at the end of the initial .B #! -line of the script -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: +line of the script, as in: .sp 1 .in +5n .nf -\fI#!${BINDIR}/calc\fP\ [other_flags\ \&...] \fB\-s\fP \fB\-f\fP +\fI#!${BINDIR}/calc\fP\ [optional_other_flags\ \&...] \fB\-f\fP .fi .in -5n .sp 1 the rest of the file will be processed in .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 .B "SHELL SCRIPT MODE" section of this man page below for details. .sp 1 -The actual form of this flag is: +While the actual form of this flag is: .sp 1 .in +5n -.B \-f +.BR \-f filename .in -5n .sp 1 -On systems that treat an executable that begins with +for systems that treat an executable that begins with .B #! as a script, the path of the executable is appended by the kernel as the final argument to the exec() system call. @@ -270,12 +279,13 @@ flag at the very end of the line. .sp 1 It is possible use -.B \-f\ filename +.B \-f +filename on the command line: .sp 1 .in +5n .nf -\fIcalc\fP\ [other_flags\ \&...] \fB\-s\fP \fB\-f\fP filename +\fIcalc\fP\ [optional_other_flags\ \&...] \fB\-f\fP filename .fi .in -5n .sp 1 @@ -283,28 +293,14 @@ This will cause calc to process lines in .B filename in .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 -In addition, -.B \-d -and -.B \-p -are implied if -.B \-i -is not given. +The use of +.B \-f +filename +implies the +.B \-s +flag. .TP .B \-h @@ -491,16 +487,16 @@ This is useful when entering negative values on the command line as in: .in +5n .nf calc \-p \-\- \-1 - -7 -.sp 1 .fi .in -5n +.sp 1 .PP -\& -.br -CALC COMMAND LINE +.SH CALC COMMAND LINE + .PP + With no .I calc_cmd arguments, @@ -638,10 +634,10 @@ to parse interactive commands, flushes data on standard input. .PP -\& -.br -CALC STARTUP FILES +.SH CALC STARTUP FILES + .PP + Normally on startup, if the environment variable .B $CALCRC @@ -678,10 +674,12 @@ files is also disabled as if .B \-q was given. -\& -.br -CALC FILE SEARCH PATH .PP + +.SH CALC FILE SEARCH PATH + +.PP + If the environment variable .B $CALCPATH is undefined, or if it @@ -758,24 +756,24 @@ help config .fi .in -5n .sp 1 + .PP -\& -.br -SHELL SCRIPT MODE +.SH SHELL SCRIPT MODE + .PP + If the first line of an executable file begins .B #! followed by the absolute pathname of the .B calc -program and the first line ends with the two flags -.B \-s +program and the first line ends with the flag .B \-f as in: .sp 1 .in +5n .nf -\fI#!${BINDIR}/calc\fP\ [other_flags\ \&...] \fB\-s\fP \fB\-f\fP +\fI#!${BINDIR}/calc\fP\ [optional_other_flags\ \&...] \fB\-f\fP .fi .in -5n .sp 1 @@ -786,11 +784,11 @@ Note that .B \-f must at the end of the initial ``#!'' line. Any other optional -.B "other_flags" +.B "optional_other_flags" must come before the -.B \-s -.BR \-f . +.B \-f +flag. .sp 1 In .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 whitespace and ending at the next newline is treated as a comment. Any optional -.B "other_flags" +.B "optional_other_flags" will be parsed first followed by the later lines within the script itself. .sp 1 @@ -821,7 +819,7 @@ the file .sp 1 .in +5n .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 */ argc = argv(); @@ -894,12 +892,13 @@ whereas .in -5n .sp 1 will not. + .PP -\& -.br -DATA TYPES +.SH DATA TYPES + .PP + Fundamental builtin data types include integers, real numbers, rational numbers, complex numbers and strings. .PP @@ -922,12 +921,13 @@ help obj .br show objfuncs .in -1.0i + .PP -\& -.br -VARIABLES +.SH VARIABLES + .PP + Variables in \fIcalc\fP are typeless. 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. @@ -950,12 +950,13 @@ help list .br show globals .in -1.0i + .PP -\& -.br -INPUT/OUTPUT +.SH INPUT/OUTPUT + .PP + A leading ``0x'' implies a hexadecimal value, a leading ``0b'' implies a binary value, and a ``0'' followed by a digit implies an octal value. @@ -985,12 +986,13 @@ command: .in 1.0i help file .in -1.0i + .PP -\& -.br -CALC LANGUAGE +.SH CALC LANGUAGE + .PP + The \fIcalc\fP language is a C-like language. The language includes commands such as variable declarations, expressions, tests, labels, loops, file operations, function calls. @@ -1017,10 +1019,13 @@ help operator .br help config .in -1.0i + .PP + .SH FILES -\& -.br + +.PP + .PD 0 .TP 5 ${BINDIR}/calc @@ -1062,9 +1067,13 @@ custom resource files ${CUSTOMHELPDIR}/* custom help files .sp 1 + +.PP + .SH ENVIRONMENT -\& -.br + +.PP + .PD 0 .TP 5 CALCPATH @@ -1123,9 +1132,13 @@ Location of the calc custom help directory. .sp Default value: ${CUSTOMHELPDIR} .sp + +.PP + .SH CREDIT -\& -.br + +.PP + The main chunk of .B calc was written by David I. Bell. @@ -1154,10 +1167,13 @@ public domain arbitrarily precision routines which was posted to the net around 1984. By now, there is almost no recognizable code left from that original source. -.sp + +.PP + .SH "COPYING / CALC GNU LESSER GENERAL PUBLIC LICENSE" -\& -.sp + +.PP + Calc is open software, and is covered under version 2.1 of the GNU Lesser General Public License. You are @@ -1217,10 +1233,13 @@ Copyright (C) 1999-2021 Landon Curt Noll .sp and is covered under version 2.1 GNU Lesser General Public License. -.sp + +.PP + .SH "CALC QUESTIONS" -\& -.br + +.PP + If you have a simple general question about calc, send Email to: .sp .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. .sp Please be patient as we cannot always respond to Email messages quickly. -.sp + +.PP + .SH "BUG REPORTS / BUG FIXES" -\& -.br -.sp + +.PP + Send bug reports and bug fixes to: .sp .in +0.5i @@ -1346,11 +1367,13 @@ help bugs for more information about bug reporting. .sp Please be patient as we cannot always respond to Email messages quickly. -.sp + +.PP + .SH "CONTRIBUTING CODE TO CALC" -\& -.br -.sp + +.PP + .I Calc is open source. Contributions of code are welcome. @@ -1473,10 +1496,13 @@ you have that somewhere in the subject line! .sp Please be patient as we cannot always respond to Email messages quickly. -.sp + +.PP + .SH "CALC WEB SITE" -\& -.br + +.PP + Landon Noll maintains the .B calc web site is located at: diff --git a/cscript/4dsphere.calc b/cscript/4dsphere.calc index 8508e27..912807d 100644 --- a/cscript/4dsphere.calc +++ b/cscript/4dsphere.calc @@ -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 * + * Copyright (C) 2001,2014,2019,2021 Landon Curt Noll + * * usage: * 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 * - * Copyright (C) 2001,2014,2019 Landon Curt Noll - * * 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 * as published by the Free Software Foundation. diff --git a/cscript/fproduct.calc b/cscript/fproduct.calc index 1f7be5b..22327bf 100644 --- a/cscript/fproduct.calc +++ b/cscript/fproduct.calc @@ -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 * + * Copyright (C) 2001,2014,2019,2021 Landon Curt Noll + * * usage: * fproduct filename term [term ...] * * filename where to write the product, use - for stdout * 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 * the terms of the version 2.1 of the GNU Lesser General Public License * as published by the Free Software Foundation. diff --git a/cscript/mersenne.calc b/cscript/mersenne.calc index 0d49ef5..0f04446 100644 --- a/cscript/mersenne.calc +++ b/cscript/mersenne.calc @@ -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 * - * 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 * the terms of the version 2.1 of the GNU Lesser General Public License diff --git a/cscript/piforever.calc b/cscript/piforever.calc index 3b4d035..bb721d6 100644 --- a/cscript/piforever.calc +++ b/cscript/piforever.calc @@ -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) * diff --git a/cscript/plus.calc b/cscript/plus.calc index f750af9..1c0aa4d 100644 --- a/cscript/plus.calc +++ b/cscript/plus.calc @@ -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 * - * 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 * 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. */ sum = 0; -for (i=1; i < argv(); ++i) { +for (i=1; i < argc; ++i) { sum += eval(argv(i)); } print sum; diff --git a/cscript/powerterm.calc b/cscript/powerterm.calc index 992133d..59edd29 100644 --- a/cscript/powerterm.calc +++ b/cscript/powerterm.calc @@ -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 * + * Copyright (C) 2001,2014,2019,2021 Landon Curt Noll + * * usage: * powerterm [base_limit] value * * base_limit largest base we will consider (def: 10000) * 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 * the powerterm of the version 2.1 of the GNU Lesser General Public License diff --git a/cscript/simple.calc b/cscript/simple.calc index 95ffa7f..a30618a 100644 --- a/cscript/simple.calc +++ b/cscript/simple.calc @@ -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 * - * 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 * the terms of the version 2.1 of the GNU Lesser General Public License diff --git a/cscript/square.calc b/cscript/square.calc index b1e3d4c..540c124 100644 --- a/cscript/square.calc +++ b/cscript/square.calc @@ -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 * - * 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 * the terms of the version 2.1 of the GNU Lesser General Public License @@ -48,6 +48,8 @@ * 1522756 * ln(2) * ~.48045301391820142467 + * 2i + * -4 * config("mode","frac"), bernoulli(10) * 25/4356 * config("mode", "hex"), 0xff diff --git a/custom/Makefile b/custom/Makefile index eded67f..bcc53d4 100644 --- a/custom/Makefile +++ b/custom/Makefile @@ -458,7 +458,7 @@ EXT= # The default calc versions # -VERSION= 2.12.9.1 +VERSION= 2.13.0.0 # Names of shared libraries with versions # diff --git a/custom/Makefile.head b/custom/Makefile.head index 89091aa..c9e4d89 100644 --- a/custom/Makefile.head +++ b/custom/Makefile.head @@ -458,7 +458,7 @@ EXT= # The default calc versions # -VERSION= 2.12.9.1 +VERSION= 2.13.0.0 # Names of shared libraries with versions # diff --git a/func.c b/func.c index 00788eb..c03cf7b 100644 --- a/func.c +++ b/func.c @@ -8464,11 +8464,23 @@ f_argv(int count, VALUE **vals) /*NOTREACHED*/ } - /* return the n-th argv string */ + /* determine the arg value of the argv() function */ 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_str = makestring(strdup(argv_value[arg])); + result.v_str = makestring(strdup(argv_value[arg-1])); } else { result.v_type = V_NULL; } diff --git a/help/argv b/help/argv index 0f4e425..d292106 100644 --- a/help/argv +++ b/help/argv @@ -11,14 +11,33 @@ TYPES DESCRIPTION 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 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 - to the 1st argv string, and argv(argv()-1) will refer to the last. - This differs from the way the param() builtin works. + to the name of the program. If the -f filename argument is used, + 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. However, if calc is invoked with -s, all non-dashed options will @@ -27,22 +46,26 @@ DESCRIPTION calc -i 2+2 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: calc -i -s 2+2 - will cause calc to interactive mode. The argv() builtin will return 1 - and argv(0) will return the string "2+2". + will cause calc to interactive mode. The argv() builtin will return 2 + and argv(1) will return the string "2+2". EXAMPLE $ calc -s a bb ccc ; argc = argv(); - ; for (i = 0; i < argc; i++) print "argv[": i : '] = "': argv(i) : '"'; - argv[0] = "a" - argv[1] = "bb" - argv[2] = "ccc" + ; print "argc =", argc; + argc = 4 + ; print 'argv[0] = "': argv(0) : '"'; + argv[0] = "calc" + ; for (i = 1; i < argc; i++) print "argv[": i : '] = "': argv(i) : '"'; + argv[1] = "a" + argv[2] = "bb" + argv[3] = "ccc" LIMITS 0 <= n < 2^31 @@ -53,7 +76,7 @@ LINK LIBRARY SEE ALSO 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 ## the terms of the version 2.1 of the GNU Lesser General Public License diff --git a/help/script b/help/script index 84cf770..191b165 100644 --- a/help/script +++ b/help/script @@ -123,7 +123,7 @@ Calc shell scripts calc "-q -s" $* << + global i, n, s; n = argv(); - for (i = 0; i < n; i++) + for (i = 1; i < n; i++) s += eval(argv(i)); print "sum =", s; + @@ -133,20 +133,24 @@ Calc shell scripts ./addall2 2 3 4 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", - argv(2)= "4". As there is only one calc process involved and + in the options, calc starts with argv(1) = "2", argv(2) = "3", + argv(3)= "4". As there is only one calc process involved and the eval() function accepts as argument any string that - represents the body of a calc function, the strings argv(0), - argv(1), ... could evaluate to any value types for which the + represents the body of a calc function, the strings argv(1), + argv(2), ... could evaluate to any value types for which the additions to be performed are defined, and variables defined in 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 same thing may be done more efficiently by using calc as an interpreter. Assuming the full path for calc is /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; n = argv(); for (i = 1; i < n; i++) @@ -161,9 +165,9 @@ Calc shell scripts 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". Another kind of script that can be useful is sqrts1: diff --git a/lib_calc.c b/lib_calc.c index c7587f5..a4a0f9f 100644 --- a/lib_calc.c +++ b/lib_calc.c @@ -119,6 +119,7 @@ char *program = "calc"; /* our name */ char *base_name = "calc"; /* basename of our name */ char cmdbuf[MAXCMD+1+1+1]; /* command line expression + "\n\0" + guard */ 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(); + /* + * free script_name + */ + if (script_name != NULL) { + free(script_name); + script_name = NULL; + } + /* * restore all changed descriptor states */ diff --git a/lib_calc.h b/lib_calc.h index 0ff023f..48d7969 100644 --- a/lib_calc.h +++ b/lib_calc.h @@ -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 * diff --git a/version.c b/version.c index 613fcaa..b395ac2 100644 --- a/version.c +++ b/version.c @@ -52,9 +52,9 @@ static char *program; #define MAJOR_VER 2 /* major library version */ -#define MINOR_VER 12 /* minor library version */ -#define MAJOR_PATCH 9 /* major software version level */ -#define MINOR_PATCH 1 /* minor software version level */ +#define MINOR_VER 13 /* minor library version */ +#define MAJOR_PATCH 0 /* major software version level */ +#define MINOR_PATCH 0 /* minor software version level */ /* diff --git a/win32.mkdef b/win32.mkdef index 7aa6950..8278409 100644 --- a/win32.mkdef +++ b/win32.mkdef @@ -54,6 +54,7 @@ USE_READLINE=-DUSE_READLINE READLINE_LIB= READLINE_INCLUDE= CALCPAGER=less.exe +CCBAN=-DUNBAN EXT=.exe