dcalc.c (gdcalc-2.20) | : | dcalc.c (gdcalc-3.0) | ||
---|---|---|---|---|
skipping to change at line 23 | skipping to change at line 23 | |||
it under the terms of the GNU General Public License as published by | it under the terms of the GNU General Public License as published by | |||
the Free Software Foundation; either version 2 of the License, or | the Free Software Foundation; either version 2 of the License, or | |||
(at your option) any later version. | (at your option) any later version. | |||
This program is distributed in the hope that it will be useful, | This program is distributed in the hope that it will be useful, | |||
but WITHOUT ANY WARRANTY; without even the implied warranty of | but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
GNU General Public License for more details. | GNU General Public License for more details. | |||
You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | |||
along with this program; see the file COPYING. If not, write to | along with this program; see the file COPYING. If not, write to | |||
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | the Free Software Foundation, Inc., 51 Franklin Street, Fifth | |||
Boston, MA 02111-1307, USA. | Floor, Boston, MA 02110-1301 USA | |||
*/ | */ | |||
#include <stdio.h> | #include <stdio.h> | |||
#include <math.h> | #include <math.h> | |||
#include <ctype.h> | #include <ctype.h> | |||
#include <string.h> | #include <string.h> | |||
#include <stdlib.h> | #include <stdlib.h> | |||
#include <unistd.h> /* for pipe() */ | #include <unistd.h> /* for pipe() */ | |||
#include <sys/types.h> /* for wait */ | ||||
#include <sys/wait.h> /* for wait */ | ||||
#include <time.h> /* for localtime */ | #include <time.h> /* for localtime */ | |||
#include <float.h> /* for DBL_MAX */ | #include <float.h> /* for DBL_MAX */ | |||
extern int errno; | extern int errno; | |||
#ifdef HAVE_CONFIG_H /* gtk & wx */ | #ifdef HAVE_CONFIG_H /* gtk & wx */ | |||
# include "config.h" | # include "config.h" | |||
#endif | #endif | |||
#include "dcalc.h" | #include "dcalc.h" | |||
skipping to change at line 302 | skipping to change at line 300 | |||
else | else | |||
sprintf(s, "%32.1lx", x); | sprintf(s, "%32.1lx", x); | |||
break; | break; | |||
} | } | |||
} else { | } else { | |||
if (xf == 0.0) { | if (xf == 0.0) { | |||
strcpy(s, "0.0"); | strcpy(s, "0.0"); | |||
return; | return; | |||
} | } | |||
if (floatmode == SCI) { | if (floatmode == SCIFORMAT) { | |||
sprintf(format, "%%32.%dE", decplaces); | sprintf(format, "%%32.%dE", decplaces); | |||
sprintf(s, format, xf); | sprintf(s, format, xf); | |||
} else if (floatmode == ENG) { | } else if (floatmode == ENGFORMAT) { | |||
double mant; | double mant; | |||
int isNeg = 0, exp; | int isNeg = 0, exp; | |||
char *e, outb[80]; | char *e, outb[80]; | |||
if (xf < 0.0) | if (xf < 0.0) | |||
isNeg = 1; | isNeg = 1; | |||
mant = fabs(xf); | mant = fabs(xf); | |||
exp = log10(mant); | exp = log10(mant); | |||
exp = exp / 3; | exp = exp / 3; | |||
exp = exp * 3; | exp = exp * 3; | |||
skipping to change at line 332 | skipping to change at line 330 | |||
mant = -mant; | mant = -mant; | |||
sprintf(outb, "%32.2E", pow(10.0, exp)); | sprintf(outb, "%32.2E", pow(10.0, exp)); | |||
e = strchr(outb, 'E'); | e = strchr(outb, 'E'); | |||
if (e) { | if (e) { | |||
sprintf(format, "%%32.%df%%s", decplaces); | sprintf(format, "%%32.%df%%s", decplaces); | |||
sprintf(s, format, mant, e); | sprintf(s, format, mant, e); | |||
} else { | } else { | |||
sprintf(format, "%%32.%dg", decplaces); | sprintf(format, "%%32.%dg", decplaces); | |||
sprintf(s, format, xf); | sprintf(s, format, xf); | |||
} | } | |||
} else { /* FIX */ | } else { /* FIXFORMAT */ | |||
double lowerLimit = pow(10.0, -decplaces); | double lowerLimit = pow(10.0, -decplaces); | |||
#ifdef EBOOKMAN | #ifdef EBOOKMAN | |||
double upperlimit = 1.0e15 / pow(10.0, decplaces); | double upperlimit = 1.0e15 / pow(10.0, decplaces); | |||
#else | #else | |||
double upperlimit = 1.0e25 / pow(10.0, decplaces); | double upperlimit = 1.0e25 / pow(10.0, decplaces); | |||
#endif | #endif | |||
if ((fabs(xf) > upperlimit) || (fabs(xf) < lowerLimit)) { | if ((fabs(xf) > upperlimit) || (fabs(xf) < lowerLimit)) { | |||
sprintf(format, "%%32.%dE", decplaces); | sprintf(format, "%%32.%dE", decplaces); | |||
} else { | } else { | |||
sprintf(format, "%%32.%df", decplaces); | sprintf(format, "%%32.%df", decplaces); | |||
skipping to change at line 380 | skipping to change at line 378 | |||
case DEC: | case DEC: | |||
sep = COMMA; | sep = COMMA; | |||
interval = 3; | interval = 3; | |||
break; | break; | |||
case HEX: | case HEX: | |||
sep = SPACE; | sep = SPACE; | |||
interval = 2; | interval = 2; | |||
strcpy(prefix, "0x"); | strcpy(prefix, "0x"); | |||
break; | break; | |||
} | } | |||
} else { /* FIX or ENG */ | } else { /* FIXFORMAT or ENGFORMAT */ | |||
sep = COMMA; | sep = COMMA; | |||
interval = 3; | interval = 3; | |||
} | } | |||
/* Find first non-blank ... */ | /* Find first non-blank ... */ | |||
s = string; | s = string; | |||
while (*s && isspace(*s)) s++; | while (*s && isspace(*s)) s++; | |||
if (*s == '-') { | if (*s == '-') { | |||
negative = TRUE; | negative = TRUE; | |||
*s++ = SPACE; | *s++ = SPACE; | |||
skipping to change at line 754 | skipping to change at line 752 | |||
dispnums(); | dispnums(); | |||
dispregs(); | dispregs(); | |||
prinbase(); | prinbase(); | |||
} | } | |||
return 0; | return 0; | |||
} | } | |||
static | static | |||
void change_floatmode(COMMAND c) { | void change_floatmode(COMMAND c) { | |||
last_was_fin_key = 0; | last_was_fin_key = 0; | |||
if (c != floatmode) { | if ((c != floatmode) || (mode == PROG)) { | |||
char buf[80]; | char buf[80]; | |||
floatmode = (c == SCIFORMAT? SCI: c); | if (mode == PROG) mode = SCI; | |||
floatmode = c; | ||||
stop_entering(); | stop_entering(); | |||
os_raw_mode(0); | os_raw_mode(0); | |||
max_digits = 20; | max_digits = 20; | |||
sprintf(buf, "%s notation", | sprintf(buf, "%s notation", | |||
(c==ENG)? "Engineering": | (c==ENGFORMAT)? "Engineering": | |||
(c==SCIFORMAT)? "Scientific": | (c==SCIFORMAT)? "Scientific": | |||
"Fixed point"); | "Fixed point"); | |||
msg(buf); | msg(buf); | |||
dispnums(); | dispnums(); | |||
dispregs(); | dispregs(); | |||
prinbase(); | prinbase(); | |||
} | } | |||
} | } | |||
int | int | |||
exec_eng() { | exec_sciformat() { | |||
isInverted(); | isInverted(); | |||
last_was_fin_key = 0; | last_was_fin_key = 0; | |||
change_floatmode(ENG); | change_floatmode(SCIFORMAT); | |||
return 0; | return 0; | |||
} | } | |||
int | int | |||
exec_fix() { | exec_eng() { | |||
isInverted(); | isInverted(); | |||
last_was_fin_key = 0; | last_was_fin_key = 0; | |||
change_floatmode(FIX); | change_floatmode(ENGFORMAT); | |||
return 0; | return 0; | |||
} | } | |||
int | int | |||
exec_sciformat() { | exec_fix() { | |||
isInverted(); | isInverted(); | |||
last_was_fin_key = 0; | last_was_fin_key = 0; | |||
change_floatmode(SCIFORMAT); | change_floatmode(FIXFORMAT); | |||
return 0; | return 0; | |||
} | } | |||
int | int | |||
exec_places() { | exec_places() { | |||
long tempx; | long tempx; | |||
isInverted(); | isInverted(); | |||
last_was_fin_key = 0; | last_was_fin_key = 0; | |||
if (((tempx = places("Enter number of decimal places")) >= 0) && | if (((tempx = places("Enter number of decimal places")) >= 0) && | |||
skipping to change at line 825 | skipping to change at line 824 | |||
dispregs(); | dispregs(); | |||
prinbase(); | prinbase(); | |||
} | } | |||
return 0; | return 0; | |||
} | } | |||
int | int | |||
exec_ip() { | exec_ip() { | |||
isInverted(); | isInverted(); | |||
last_was_fin_key = 0; | last_was_fin_key = 0; | |||
if (mode != PROG || intmode != IP) { | if ((intmode != IP) || (mode != PROG)) { | |||
long tempx; | long tempx; | |||
mode = PROG; | ||||
tempx = pop(); | tempx = pop(); | |||
push(tempx); | push(tempx); | |||
mode = PROG; | ||||
os_raw_mode(0); | os_raw_mode(0); | |||
max_digits = 15; | max_digits = 15; | |||
intmode = IP; | intmode = IP; | |||
msg("IP address mode"); | msg("IP address mode"); | |||
dispnums(); | dispnums(); | |||
dispregs(); | dispregs(); | |||
prinbase(); | prinbase(); | |||
} | } | |||
return 0; | return 0; | |||
} | } | |||
int | int | |||
exec_bin() { | exec_bin() { | |||
isInverted(); | isInverted(); | |||
last_was_fin_key = 0; | last_was_fin_key = 0; | |||
if (mode != PROG || intmode != BIN) { | if ((intmode != BIN) || (mode != PROG)) { | |||
long tempx; | long tempx; | |||
mode = PROG; | ||||
tempx = pop(); | tempx = pop(); | |||
push(tempx); | push(tempx); | |||
mode = PROG; | ||||
os_raw_mode(0); | os_raw_mode(0); | |||
max_digits = 32; | max_digits = 32; | |||
intmode = BIN; | intmode = BIN; | |||
msg("Binary mode"); | msg("Binary mode"); | |||
dispnums(); | dispnums(); | |||
dispregs(); | dispregs(); | |||
prinbase(); | prinbase(); | |||
} | } | |||
return 0; | return 0; | |||
} | } | |||
int | int | |||
exec_oct() { | exec_oct() { | |||
isInverted(); | isInverted(); | |||
last_was_fin_key = 0; | last_was_fin_key = 0; | |||
if (mode != PROG || intmode != OCT) { | if ((intmode != OCT) || (mode != PROG)) { | |||
long tempx; | long tempx; | |||
mode = PROG; | ||||
tempx = pop(); | tempx = pop(); | |||
push(tempx); | push(tempx); | |||
intmode = OCT; | intmode = OCT; | |||
mode = PROG; | ||||
os_raw_mode(0); | os_raw_mode(0); | |||
max_digits = 11; | max_digits = 11; | |||
msg("Octal mode"); | msg("Octal mode"); | |||
dispnums(); | dispnums(); | |||
dispregs(); | dispregs(); | |||
prinbase(); | prinbase(); | |||
} | } | |||
return 0; | return 0; | |||
} | } | |||
int | int | |||
exec_dec() { | exec_dec() { | |||
isInverted(); | isInverted(); | |||
last_was_fin_key = 0; | last_was_fin_key = 0; | |||
if (mode != PROG || intmode != DEC) { | if ((intmode != DEC) || (mode != PROG)) { | |||
long tempx; | long tempx; | |||
mode = PROG; | ||||
tempx = pop(); | tempx = pop(); | |||
push(tempx); | push(tempx); | |||
mode = PROG; | ||||
intmode = DEC; | intmode = DEC; | |||
os_raw_mode(0); | os_raw_mode(0); | |||
max_digits = 10; | max_digits = 10; | |||
msg("Decimal mode"); | msg("Decimal mode"); | |||
dispnums(); | dispnums(); | |||
dispregs(); | dispregs(); | |||
prinbase(); | prinbase(); | |||
} | } | |||
return 0; | return 0; | |||
} | } | |||
int | int | |||
exec_hex() { | exec_hex() { | |||
isInverted(); | isInverted(); | |||
last_was_fin_key = 0; | last_was_fin_key = 0; | |||
if (mode != PROG || intmode != HEX) { | if ((intmode != HEX) || (mode != PROG)) { | |||
long tempx; | long tempx; | |||
mode = PROG; | ||||
tempx = pop(); | tempx = pop(); | |||
push(tempx); | push(tempx); | |||
mode = PROG; | ||||
intmode = HEX; | intmode = HEX; | |||
os_raw_mode(0); | os_raw_mode(0); | |||
max_digits = 8; | max_digits = 8; | |||
msg("Hexadecimal mode"); | msg("Hexadecimal mode"); | |||
dispnums(); | dispnums(); | |||
dispregs(); | dispregs(); | |||
prinbase(); | prinbase(); | |||
} | } | |||
return 0; | return 0; | |||
skipping to change at line 937 | skipping to change at line 936 | |||
void change_fin_sci_stat(COMMAND c) { | void change_fin_sci_stat(COMMAND c) { | |||
last_was_fin_key = 0; | last_was_fin_key = 0; | |||
if (mode != c) { | if (mode != c) { | |||
char buf[80]; | char buf[80]; | |||
mode = c; | mode = c; | |||
stop_entering(); | stop_entering(); | |||
os_raw_mode(0); | os_raw_mode(0); | |||
max_digits = 20; | max_digits = 20; | |||
sprintf(buf, "%s mode", (c==FIN)? "Financial": (c==SCI)? "Scientific": " Statistics"); | sprintf(buf, "%s mode", (c==FIN)? "Financial": (c==SCI)? "Scientific": " Statistics"); | |||
if (c == FIN) { | if (c == FIN) { | |||
if (floatmode != FIX) | if (floatmode != FIXFORMAT) | |||
exec_fix(); | exec_fix(); | |||
if (decplaces != 2) | ||||
decplaces = 2; | ||||
dispnums(); | dispnums(); | |||
} | } | |||
msg(buf); | msg(buf); | |||
dispnums(); | dispnums(); | |||
dispregs(); | dispregs(); | |||
prinbase(); | prinbase(); | |||
} | } | |||
} | } | |||
skipping to change at line 2033 | skipping to change at line 2030 | |||
} else { /* convert hours to h.mmss */ | } else { /* convert hours to h.mmss */ | |||
long h, m, s; | long h, m, s; | |||
h = tempxf; | h = tempxf; | |||
tempxf = (tempxf - h) * 60 + 1E-10; | tempxf = (tempxf - h) * 60 + 1E-10; | |||
m = tempxf; | m = tempxf; | |||
s = (tempxf - m) * 60 + 1E-10; | s = (tempxf - m) * 60 + 1E-10; | |||
tempxf = h + m / 100.0 + s / 10000.0; | tempxf = h + m / 100.0 + s / 10000.0; | |||
if (decplaces < 4) | if (decplaces < 4) | |||
decplaces = 4; | decplaces = 4; | |||
if (floatmode != FIX) | if (floatmode != FIXFORMAT) | |||
exec_fix(); | exec_fix(); | |||
msg("hours -> HH.MMSS"); | msg("hours -> HH.MMSS"); | |||
} | } | |||
if (sign) | if (sign) | |||
tempxf = -tempxf; | tempxf = -tempxf; | |||
pushf(tempxf); | pushf(tempxf); | |||
dispnums(); | dispnums(); | |||
return 0; | return 0; | |||
} | } | |||
skipping to change at line 2489 | skipping to change at line 2486 | |||
if (m <= 2) { | if (m <= 2) { | |||
x = 0; | x = 0; | |||
z = y - 1; | z = y - 1; | |||
} else { | } else { | |||
x = (long) (0.4*m + 2.3); | x = (long) (0.4*m + 2.3); | |||
z = y; | z = y; | |||
} | } | |||
return 365*y + 31 *(m - 1) + d + (int)(z/4) - x; | return 365*y + 31 *(m - 1) + d + (int)(z/4) - x; | |||
} | } | |||
static int dayFunction30(double f, int i) { | static double date_plus_days(double start, double days) { | |||
static int was30or31; | struct tm tm, *new_time; | |||
int y, m, d, a1; | long y, m, d; | |||
time_t new_time_secs; | ||||
f += 0.00005; /* round up */ | double retval; | |||
y = f; | ||||
f = (f - y) * 100; | ||||
m = f; | ||||
f = (f - m) * 100; | ||||
d = f; | ||||
if (i == 1) { | start += 0.00005; /* round up */ | |||
if (d == 31) { | y = start; | |||
a1 = 30; | start = (start - y) * 100; | |||
} else { | m = start; | |||
a1 = d; | start = (start - m) * 100; | |||
} | d = start; | |||
if (d == 30 || d == 31) { | ||||
was30or31 = 1; | tm.tm_sec = 0; | |||
} else { | tm.tm_min = 0; | |||
was30or31 = 0; | tm.tm_hour = 0; | |||
} | tm.tm_mday = d; | |||
} else { | tm.tm_mon = m; | |||
if (d == 31) { | tm.tm_year = y; | |||
if (was30or31) { | new_time_secs = mktime(&tm) + days * 24 * 60 * 60; | |||
a1 = 30; | new_time = localtime(&new_time_secs); | |||
} else { | retval = new_time->tm_year + new_time->tm_mon / 100.0 + new_time->tm_mday / | |||
a1 = d; | 10000.0; | |||
} | return(retval); | |||
} else { | ||||
a1 = d; | ||||
} | ||||
} | ||||
return 360*y + 30*m + a1; | ||||
} | } | |||
/* static void financial(COMMAND c) { */ | /* static void financial(COMMAND c) { */ | |||
/* | /* | |||
* Financial function register allocation ... | * Financial function register allocation ... | |||
* | * | |||
* 5 n | * 5 n | |||
* 6 interest | * 6 interest | |||
* 7 present value | * 7 present value | |||
* 8 payment | * 8 payment | |||
skipping to change at line 3129 | skipping to change at line 3115 | |||
isInverted(); | isInverted(); | |||
if (mode == PROG) | if (mode == PROG) | |||
return 0; | return 0; | |||
c = dialog("Clear all finance registers?" DIALOG_LETTERS); | c = dialog("Clear all finance registers?" DIALOG_LETTERS); | |||
if (toupper(c) == 'Y') { | if (toupper(c) == 'Y') { | |||
int i; | int i; | |||
for (i=5; i< 10; i++) | for (i=5; i< 10; i++) | |||
regf[i] = 0; | regf[i] = 0; | |||
msg("Fin registers cleared"); | msg("Fin registers cleared"); | |||
dispregs(); | ||||
} | } | |||
return 0; | return 0; | |||
} | } | |||
int | int | |||
exec_begin() { | exec_begin() { | |||
isInverted(); | isInverted(); | |||
if (mode == PROG) | if (mode == PROG) | |||
return 0; | return 0; | |||
skipping to change at line 3159 | skipping to change at line 3146 | |||
int | int | |||
exec_dys() { | exec_dys() { | |||
double tempxf, tempyf; | double tempxf, tempyf; | |||
if (mode == PROG) | if (mode == PROG) | |||
return 0; | return 0; | |||
save_x(); | save_x(); | |||
tempxf = popf(); | tempxf = popf(); | |||
tempyf = popf(); | tempyf = popf(); | |||
pushf(tempyf); | ||||
if (isInverted()) { | if (isInverted()) { | |||
tempyf = dayFunction30(tempxf,1); | tempxf = date_plus_days(tempyf, tempxf); | |||
tempxf = dayFunction30(tempyf,2) - tempyf; | msg("date (x) plus days (y) calculated"); | |||
} else { | } else { | |||
tempxf = dayFunction(tempyf) - dayFunction(tempxf); | tempxf = dayFunction(tempyf) - dayFunction(tempxf); | |||
msg("days between two dates calculated"); | ||||
} | } | |||
msg("days between two dates calculated"); | ||||
if (decplaces < 4) | if (decplaces < 4) | |||
decplaces = 4; | decplaces = 4; | |||
if (floatmode != FIX) | if (floatmode != FIXFORMAT) | |||
exec_fix(); | exec_fix(); | |||
pushf(tempxf); | pushf(tempxf); | |||
dispnums(); | dispnums(); | |||
return 0; | return 0; | |||
} | } | |||
int | int | |||
exec_tdy() { | exec_tdy() { | |||
double tempxf; | double tempxf; | |||
struct tm *tmbuf; | struct tm *tmbuf; | |||
skipping to change at line 3194 | skipping to change at line 3183 | |||
save_x(); | save_x(); | |||
t = time(NULL); | t = time(NULL); | |||
tmbuf = localtime(&t); | tmbuf = localtime(&t); | |||
tempxf = tmbuf->tm_year + 1900; | tempxf = tmbuf->tm_year + 1900; | |||
tempxf += (tmbuf->tm_mon + 1) / 100.0; | tempxf += (tmbuf->tm_mon + 1) / 100.0; | |||
tempxf += tmbuf->tm_mday / 10000.0; | tempxf += tmbuf->tm_mday / 10000.0; | |||
pushf(tempxf); | pushf(tempxf); | |||
if (decplaces != 4) | if (decplaces != 4) | |||
decplaces = 4; | decplaces = 4; | |||
if (floatmode != FIX) | if (floatmode != FIXFORMAT) | |||
exec_fix(); | exec_fix(); | |||
dispnums(); | dispnums(); | |||
msg("Today's date"); | msg("Today's date"); | |||
return 0; | return 0; | |||
} | } | |||
int | int | |||
exec_times12() { | exec_times12() { | |||
double tempxf; | double tempxf; | |||
skipping to change at line 3238 | skipping to change at line 3227 | |||
save_x(); | save_x(); | |||
tempxf = popf(); | tempxf = popf(); | |||
tempxf /= 12.0; | tempxf /= 12.0; | |||
pushf(tempxf); | pushf(tempxf); | |||
dispnums(); | dispnums(); | |||
return 0; | return 0; | |||
} | } | |||
static void process_digit(COMMAND c) { | static void process_digit(COMMAND c) { | |||
switch (c) { | switch (toupper(c)) { | |||
case BACKSPACE: | case BACKSPACE: | |||
case '0': | case '0': | |||
case '1': | case '1': | |||
echo_char(c); | echo_char(c); | |||
break; | break; | |||
case '2': | case '2': | |||
case '3': | case '3': | |||
case '4': | case '4': | |||
case '5': | case '5': | |||
skipping to change at line 3314 | skipping to change at line 3303 | |||
else | else | |||
echo_char(c); | echo_char(c); | |||
break; | break; | |||
default: | default: | |||
put_a_char(BELL); | put_a_char(BELL); | |||
break; | break; | |||
} | } | |||
} | } | |||
/* trim all white space from buf (for units) */ | ||||
void trim(char const *s) { | ||||
char *d = (char *)s; | ||||
while (*s) { | ||||
if (!isspace(*s)) | ||||
*d++ = *s; | ||||
s++; | ||||
} | ||||
*d = 0; | ||||
} | ||||
void convertX(char const *from, char const *to) { | ||||
#ifdef unix | ||||
int fd[2], pid; | ||||
#endif | ||||
if (mode == PROG) { | ||||
msg("No conversions in PROG mode"); | ||||
return; | ||||
} | ||||
trim(from); | ||||
trim(to); | ||||
stop_entering(); | ||||
#ifdef unix | ||||
if (pipe(fd) < 0) | ||||
msg("Can't open a pipe."); | ||||
else { | ||||
double oldX = xfReg; | ||||
char *argv[5]; | ||||
char fromarg[80]; | ||||
BOOLEAN is_neg = FALSE; | ||||
if (fabs(oldX) < 0.0001) | ||||
oldX = 1.0; | ||||
if (oldX < 0.0) { | ||||
is_neg = TRUE; | ||||
oldX = -oldX; | ||||
} | ||||
sprintf(fromarg, "%f %s", oldX, from); | ||||
argv[0] = "/usr/bin/units"; | ||||
argv[1] = "-o%.16g"; | ||||
argv[2] = fromarg; | ||||
argv[3] = (char *)to; | ||||
argv[4] = 0; | ||||
if ((pid = vfork()) == -1) { | ||||
msg("Fork failed."); | ||||
} else if (pid == 0) { /* child - put stdout on pipe */ | ||||
close(fd[0]); /* child does not need to read from pipe */ | ||||
close(1); /* set stdout to the pipe */ | ||||
if (dup(fd[1]) == -1) _exit(1); | ||||
close(fd[1]); | ||||
execvp(argv[0], argv); | ||||
_exit(1); /* abandon hope all ye who get here */ | ||||
} else { /* PARENT - read from pipe */ | ||||
FILE *p; | ||||
close(fd[1]); /* parent does not write to pipe */ | ||||
if ((p = fdopen(fd[0], "r")) == NULL) { | ||||
msg("Can't read from pipe"); | ||||
} else { | ||||
double newX; | ||||
int numRead = 0, status, bytesRead = 0; | ||||
char inbuf[80]; | ||||
while (fgets(inbuf, 80, p) != NULL) { | ||||
bytesRead += strlen(inbuf); | ||||
if (!numRead) | ||||
numRead = sscanf(inbuf, " * %lf", &newX); | ||||
} | ||||
wait(&status); | ||||
if (is_neg) | ||||
newX = -newX; | ||||
if (numRead == 1 && | ||||
WIFEXITED(status) && | ||||
WEXITSTATUS(status) == 0) { | ||||
double xf; | ||||
xf = popf(); | ||||
pushf(newX); | ||||
if (fabs(xf) < 0.0001) | ||||
sprintf(inbuf, "Conversion factor %s to %s", from, to); | ||||
else | ||||
sprintf(inbuf, "Converted %s to %s", from, to); | ||||
msg(inbuf); | ||||
dispnums(); | ||||
} else if (bytesRead) | ||||
/* Note: if you are tempted to catch units's error | ||||
message be aware that as of version 1.55 it | ||||
does not use stderr! */ | ||||
msg("Bad units"); | ||||
else | ||||
msg("Can't find the units program - please install it!"); | ||||
fclose(p); /* no need to close fd[1] too */ | ||||
} | ||||
} | ||||
} | ||||
#else | ||||
msg("Conversions not supported here"); | ||||
#endif | ||||
} | ||||
/* void storeOp(int op, int i) { */ | /* void storeOp(int op, int i) { */ | |||
static void | static void | |||
stoplus(int i) { | stoplus(int i) { | |||
if ((i >= 0) && (i < NUMREGS)) { | if ((i >= 0) && (i < NUMREGS)) { | |||
char buf[80], *s; | char buf[80], *s; | |||
if (mode == PROG) | if (mode == PROG) | |||
reg[i] += xiReg; | reg[i] += xiReg; | |||
else | else | |||
regf[i] += xfReg; | regf[i] += xfReg; | |||
skipping to change at line 3678 | skipping to change at line 3560 | |||
if (isInverted()) { | if (isInverted()) { | |||
c = dialog("Clear all registers?" DIALOG_LETTERS); | c = dialog("Clear all registers?" DIALOG_LETTERS); | |||
if (toupper(c) == 'Y') { | if (toupper(c) == 'Y') { | |||
int i; | int i; | |||
for (i=0; i< NUMREGS; i++) | for (i=0; i< NUMREGS; i++) | |||
if (mode == PROG) | if (mode == PROG) | |||
reg[i] = 0; | reg[i] = 0; | |||
else | else | |||
regf[i] = 0; | regf[i] = 0; | |||
msg("Registers cleared"); | msg("Registers cleared"); | |||
dispregs(); | ||||
} | } | |||
} else { | } else { | |||
last_was_fin_key = 0; | last_was_fin_key = 0; | |||
if (mode == PROG) { | if (mode == PROG) { | |||
xiReg = yiReg = ziReg = tiReg = liReg = 0; | xiReg = yiReg = ziReg = tiReg = liReg = 0; | |||
} else { | } else { | |||
xfReg = yfReg = zfReg = tfReg = lfReg = 0; | xfReg = yfReg = zfReg = tfReg = lfReg = 0; | |||
} | } | |||
lift_needed = FALSE; | lift_needed = FALSE; | |||
dispnums(); | dispnums(); | |||
skipping to change at line 4062 | skipping to change at line 3945 | |||
exec_inv() { | exec_inv() { | |||
last_was_fin_key = 0; | last_was_fin_key = 0; | |||
invert = !invert; | invert = !invert; | |||
print_inv(); | print_inv(); | |||
clear_msg(); | clear_msg(); | |||
return 0; | return 0; | |||
} | } | |||
int process(COMMAND c) { | int process(COMMAND c) { | |||
clear_msg(); | clear_msg(); | |||
#ifdef HAS_ALGEBRAIC_MODE | #ifdef HAS_ALGEBRAIC_MODE | |||
if (algebraic_mode) | if (algebraic_mode) | |||
if (c >= ' ' && c <= '~') { | if (c >= ' ' && c <= '~') { | |||
last_was_fin_key = 0; | last_was_fin_key = 0; | |||
echo_char(c); | echo_char(c); | |||
return 0; | return 0; | |||
} /* else a command */ | } /* else a command */ | |||
#endif | #endif | |||
/* ASCII MODE - almost any character can be echo'ed */ | /* ASCII MODE - almost any character can be echo'ed */ | |||
skipping to change at line 4090 | skipping to change at line 3972 | |||
exec_inv(); | exec_inv(); | |||
return 0; | return 0; | |||
} | } | |||
if (c == BACKSPACE && isInverted()) { | if (c == BACKSPACE && isInverted()) { | |||
exec_clx(); | exec_clx(); | |||
return 0; | return 0; | |||
} | } | |||
if (((c >= '0') && (c <= '9')) || | if (((c >= '0') && (c <= '9')) || | |||
((c >= 'a') && (c <= 'f')) || | ||||
((c >= 'A') && (c <= 'F')) || | ((c >= 'A') && (c <= 'F')) || | |||
(c == BACKSPACE) || | (c == BACKSPACE) || | |||
(c == '.')) { | (c == '.')) { | |||
process_digit(c); | process_digit(c); | |||
last_was_fin_key = 0; | last_was_fin_key = 0; | |||
return 0; | return 0; | |||
} | } | |||
switch (c) { | switch (c) { | |||
case CHS: exec_chs(); break; | case CHS: exec_chs(); break; | |||
case PLUS: exec_plus(); break; | case PLUS: exec_plus(); break; | |||
case MINUS: exec_minus(); break; | case MINUS: exec_minus(); break; | |||
case DIVIDE: exec_divide(); break; | case DIVIDE: exec_divide(); break; | |||
case MULT: exec_mult(); break; | case MULT: exec_mult(); break; | |||
case dAND: exec_and(); break; | case dAND: exec_and(); break; | |||
case dOR: exec_or(); break; | case dOR: exec_or(); break; | |||
case dXOR: exec_xor(); break; | case dXOR: exec_xor(); break; | |||
case dNOT: exec_not(); break; | case dNOT: exec_not(); break; | |||
case MODULUS: exec_modulus(); break; | case MODULUS: exec_modulus(); break; | |||
case '%': | ||||
case PERCENT: exec_percent(); break; | case PERCENT: exec_percent(); break; | |||
case PERCENTCH: exec_percentch(); break; | case PERCENTCH: exec_percentch(); break; | |||
case YTOX: exec_ytox(); break; | case YTOX: exec_ytox(); break; | |||
case SHIFTL: exec_shiftl(); break; | case SHIFTL: exec_shiftl(); break; | |||
case SHIFTR: exec_shiftr(); break; | case SHIFTR: exec_shiftr(); break; | |||
case SHIFTYL: exec_shiftyl(); break; | case SHIFTYL: exec_shiftyl(); break; | |||
case SHIFTYR: exec_shiftyr(); break; | case SHIFTYR: exec_shiftyr(); break; | |||
case PRIMEF: exec_primef(); break; | case PRIMEF: exec_primef(); break; | |||
case RECI: exec_reci(); break; | case RECI: exec_reci(); break; | |||
case SQR: exec_sqr(); break; | case SQR: exec_sqr(); break; | |||
skipping to change at line 4139 | skipping to change at line 4023 | |||
case DIVIDE12: exec_divide12(); break; | case DIVIDE12: exec_divide12(); break; | |||
case BEGIN: exec_begin(); break; | case BEGIN: exec_begin(); break; | |||
case DYS: exec_dys(); break; | case DYS: exec_dys(); break; | |||
case TDY: exec_tdy(); break; | case TDY: exec_tdy(); break; | |||
case ASCIIM: exec_asciim(); break; | case ASCIIM: exec_asciim(); break; | |||
case BIN: exec_bin(); break; | case BIN: exec_bin(); break; | |||
case IP: exec_ip(); break; | case IP: exec_ip(); break; | |||
case OCT: exec_oct(); break; | case OCT: exec_oct(); break; | |||
case DEC: exec_dec(); break; | case DEC: exec_dec(); break; | |||
case HEX: exec_hex(); break; | case HEX: exec_hex(); break; | |||
case FIX: exec_fix(); break; | case FIXFORMAT: exec_fix(); break; | |||
case ENG: exec_eng(); break; | case ENGFORMAT: exec_eng(); break; | |||
case SCIFORMAT: exec_sciformat(); break; | case SCIFORMAT: exec_sciformat(); break; | |||
case PLACES: exec_places(); break; | case PLACES: exec_places(); break; | |||
case FIN: exec_fin(); break; | case FIN: exec_fin(); break; | |||
case STAT: exec_stat(); break; | case STAT: exec_stat(); break; | |||
case SCI: exec_sci(); break; | case SCI: exec_sci(); break; | |||
case PROG: exec_prog(); break; | case PROG: exec_prog(); break; | |||
case ROLLDOWN: exec_rolldown(); break; | case ROLLDOWN: exec_rolldown(); break; | |||
case 'x': | ||||
case 'X': | ||||
case CLX: exec_clx(); break; | case CLX: exec_clx(); break; | |||
case CLR: exec_clr(); break; | case CLR: exec_clr(); break; | |||
case DEGREE: exec_degree(); break; | case DEGREE: exec_degree(); break; | |||
case RADIAN: exec_radian(); break; | case RADIAN: exec_radian(); break; | |||
case E: exec_e(); break; | case E: exec_e(); break; | |||
case PI: exec_pi(); break; | case PI: exec_pi(); break; | |||
case SIN: exec_sin(); break; | case SIN: exec_sin(); break; | |||
case COS: exec_cos(); break; | case COS: exec_cos(); break; | |||
case TAN: exec_tan(); break; | case TAN: exec_tan(); break; | |||
case SINH: exec_sinh(); break; | case SINH: exec_sinh(); break; | |||
skipping to change at line 4184 | skipping to change at line 4070 | |||
case S_DEV: exec_s_dev(); break; | case S_DEV: exec_s_dev(); break; | |||
case SUM: exec_sum(); break; | case SUM: exec_sum(); break; | |||
case FACT: exec_fact(); break; | case FACT: exec_fact(); break; | |||
case COMB: exec_comb(); break; | case COMB: exec_comb(); break; | |||
case PERM: exec_perm(); break; | case PERM: exec_perm(); break; | |||
case LR: exec_lr(); break; | case LR: exec_lr(); break; | |||
case CALCY: exec_calcy(); break; | case CALCY: exec_calcy(); break; | |||
case ENTER: exec_enter(); break; | case ENTER: exec_enter(); break; | |||
case LASTX: exec_lastx(); break; | case LASTX: exec_lastx(); break; | |||
case QUIT: c = exec_quit(); break; | case QUIT: c = exec_quit(); break; | |||
case 'r': | ||||
case 'R': | ||||
case RECALL: exec_recall(); break; | case RECALL: exec_recall(); break; | |||
case 's': | ||||
case 'S': | ||||
case STORE: exec_store(); break; | case STORE: exec_store(); break; | |||
case STOPLUS: exec_stoplus(); break; | case STOPLUS: exec_stoplus(); break; | |||
case STOMINUS: exec_stominus(); break; | case STOMINUS: exec_stominus(); break; | |||
case STOMULTIPLY: exec_stomultiply(); break; | case STOMULTIPLY: exec_stomultiply(); break; | |||
case STODIVIDE: exec_stodivide(); break; | case STODIVIDE: exec_stodivide(); break; | |||
case 'y': | ||||
case 'Y': | ||||
case XNY: exec_xny(); break; | case XNY: exec_xny(); break; | |||
case 'l': | ||||
case 'L': | ||||
case XNL: exec_xnl(); break; | case XNL: exec_xnl(); break; | |||
case 't': | ||||
case 'T': | ||||
case XNT: exec_xnt(); break; | case XNT: exec_xnt(); break; | |||
case 'z': | ||||
case 'Z': | ||||
case XNZ: exec_xnz(); break; | case XNZ: exec_xnz(); break; | |||
case HELP: exec_help(); break; | case HELP: exec_help(); break; | |||
case REGISTER: exec_reg(); break; | case REGISTER: exec_reg(); break; | |||
#ifdef HAS_ALGEBRAIC_MODE | #ifdef HAS_ALGEBRAIC_MODE | |||
case RPN: exec_rpn(); break; | case RPN: exec_rpn(); break; | |||
case ALGEBRAIC: exec_algebraic(); break; | case ALGEBRAIC: exec_algebraic(); break; | |||
case EVAL: exec_eval(); break; | case EVAL: exec_eval(); break; | |||
case '=': | ||||
case EQUALS: exec_equals(); break; | case EQUALS: exec_equals(); break; | |||
#endif | #endif | |||
case NOP: | case NOP: | |||
break; | break; | |||
default: | default: | |||
last_was_fin_key = 0; | last_was_fin_key = 0; | |||
put_a_char(BELL); | put_a_char(BELL); | |||
break; | break; | |||
} | } | |||
skipping to change at line 4252 | skipping to change at line 4151 | |||
strcmp(b, "asci")==0? ASCIIM: | strcmp(b, "asci")==0? ASCIIM: | |||
strcmp(b, "ip")==0? IP: | strcmp(b, "ip")==0? IP: | |||
strcmp(b, "bin")==0? BIN: | strcmp(b, "bin")==0? BIN: | |||
strcmp(b, "oct")==0? OCT: | strcmp(b, "oct")==0? OCT: | |||
strcmp(b, "dec")==0? DEC: | strcmp(b, "dec")==0? DEC: | |||
strcmp(b, "hex")==0? HEX: DEC; | strcmp(b, "hex")==0? HEX: DEC; | |||
} | } | |||
static void getFloatmode(char *b) { | static void getFloatmode(char *b) { | |||
floatmode = | floatmode = | |||
strcmp(b, "FIX")==0? FIX: | strcmp(b, "FIX")==0? FIXFORMAT: | |||
strcmp(b, "ENG")==0? ENG: | strcmp(b, "ENG")==0? ENGFORMAT: | |||
strcmp(b, "SCI")==0? SCI: FIX; | strcmp(b, "SCI")==0? SCIFORMAT: FIXFORMAT; | |||
} | } | |||
#endif | #endif | |||
static void readSettings() { | static void readSettings() { | |||
#ifdef EBOOKMAN | #ifdef EBOOKMAN | |||
readGuiSettings(NULL); | readGuiSettings(NULL); | |||
return; | return; | |||
#else | #else | |||
FILE *f = NULL; | FILE *f = NULL; | |||
char *homeDir = NULL; | char *homeDir = NULL; | |||
skipping to change at line 4380 | skipping to change at line 4279 | |||
mode==STAT? "stat": | mode==STAT? "stat": | |||
mode==PROG? "prog":"sci"); | mode==PROG? "prog":"sci"); | |||
fprintf(f, "intmode %s\n", | fprintf(f, "intmode %s\n", | |||
intmode==ASCIIM? "asci": | intmode==ASCIIM? "asci": | |||
intmode==IP? "ip": | intmode==IP? "ip": | |||
intmode==BIN? "bin": | intmode==BIN? "bin": | |||
intmode==OCT? "oct": | intmode==OCT? "oct": | |||
intmode==DEC? "dec": | intmode==DEC? "dec": | |||
intmode==HEX? "hex": "dec"); | intmode==HEX? "hex": "dec"); | |||
fprintf(f, "floatmode %s\n", | fprintf(f, "floatmode %s\n", | |||
floatmode==FIX? "FIX": "ENG"); | floatmode==FIXFORMAT? "FIX": "ENG"); | |||
saveGuiSettings(f); | saveGuiSettings(f); | |||
/* Rev 2.7 on: */ | /* Rev 2.7 on: */ | |||
fprintf(f, "annuitymode %d\n", finPayAt0? 1: 0); | fprintf(f, "annuitymode %d\n", finPayAt0? 1: 0); | |||
/* Rev 2.8 on: */ | /* Rev 2.8 on: */ | |||
#ifdef HAS_ALGEBRAIC_MODE | #ifdef HAS_ALGEBRAIC_MODE | |||
fprintf(f, "algebraicmode %d\n", algebraic_mode? 1: 0); | fprintf(f, "algebraicmode %d\n", algebraic_mode? 1: 0); | |||
fprintf(f, "lasteval \"%s\"\n", last_eval); | fprintf(f, "lasteval \"%s\"\n", last_eval); | |||
#endif | #endif | |||
fclose(f); | fclose(f); | |||
} | } | |||
skipping to change at line 4411 | skipping to change at line 4310 | |||
xiReg = yiReg = ziReg = tiReg = liReg = 0; | xiReg = yiReg = ziReg = tiReg = liReg = 0; | |||
xfReg = yfReg = zfReg = tfReg = lfReg = 0.0; | xfReg = yfReg = zfReg = tfReg = lfReg = 0.0; | |||
decplaces = 2; | decplaces = 2; | |||
#if EBOOKMAN | #if EBOOKMAN | |||
mode = FIN; | mode = FIN; | |||
#else | #else | |||
mode = SCI; | mode = SCI; | |||
#endif | #endif | |||
intmode = DEC; | intmode = DEC; | |||
floatmode = FIX; | floatmode = FIXFORMAT; | |||
entering = FALSE; | entering = FALSE; | |||
lift_needed = TRUE; | lift_needed = TRUE; | |||
invert = FALSE; | invert = FALSE; | |||
degree = TRUE; | degree = TRUE; | |||
last_was_fin_key = FALSE; | last_was_fin_key = FALSE; | |||
inptr = inbuf; | inptr = inbuf; | |||
*inptr = ASCIIZ; | *inptr = ASCIIZ; | |||
max_digits = 20; | max_digits = 20; | |||
pi = 4.0 * atan(1.0); | pi = 4.0 * atan(1.0); | |||
d_to_r = pi / 180.0; | d_to_r = pi / 180.0; | |||
End of changes. 60 change blocks. | ||||
186 lines changed or deleted | 86 lines changed or added |