1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
|
/*
* input - nested input source file reader
*
* Copyright (C) 1999-2006 David I. Bell
*
* 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.
*
* Calc is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* A copy of version 2.1 of the GNU Lesser General Public License is
* distributed with calc under the filename COPYING-LGPL. You should have
* received a copy with calc; if not, write to Free Software Foundation, Inc.
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* @(#) $Revision: 29.11 $
* @(#) $Id: input.c,v 29.11 2006/05/19 15:26:10 chongo Exp $
* @(#) $Source: /usr/local/src/cmd/calc/RCS/input.c,v $
*
* Under source code control: 1990/02/15 01:48:16
* File existed as early as: before 1990
*
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
*/
/*
* Nested input source file reader.
* For terminal input, this also provides a simple command stack.
*/
#include <stdio.h>
#include <ctype.h>
#if !defined(_WIN32)
# include <pwd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include "have_unistd.h"
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif
#if defined(__MSDOS__)
#include <limits.h>
#define _fullpath(f,n,s) (_fixpath(n,f),f)
#define _MAX_PATH PATH_MAX
#endif
#include "calc.h"
#include "conf.h"
#include "hist.h"
extern int stdin_tty; /* TRUE if stdin is a tty */
extern FILE *f_open(char *name, char *mode);
extern FILE *curstream(void);
#define TTYSIZE 100 /* reallocation size for terminal buffers */
#define MAXDEPTH 10 /* maximum depth of input */
#define IS_READ 1 /* reading normally */
#define IS_REREAD 2 /* reread current character */
#define chartoint(ch) ((ch) & 0xff) /* make sure char is not negative */
#define READSET_ALLOC 8 /* readset to allocate chunk size */
typedef struct {
int i_state; /* state (read, reread) */
int i_char; /* currently read char */
long i_line; /* line number */
char *i_cp; /* pointer to string character to be read */
char *i_str; /* start of string copy to be read, or NULL */
long i_num; /* number of string characters remaining */
char *i_ttystr; /* current character of tty line (or NULL) */
FILE *i_fp; /* current file for input (if not NULL) */
char *i_name; /* file name if known */
} INPUT;
/* files that calc has read or included */
typedef struct {
int active; /* != 0 => active entry, 0 => unused entry */
char *name; /* name used to read file */
char *path; /* real path used to open file */
struct stat inode; /* inode information for file */
} READSET;
static READSET *readset = NULL; /* array of files read */
static int maxreadset = 0; /* length of readset */
static int linesize; /* current max size of input line */
static char *linebuf; /* current input line buffer */
static char *prompt; /* current prompt for terminal */
static BOOL noprompt; /* TRUE if should not print prompt */
static int depth; /* current input depth */
static INPUT *cip; /* current input source */
static INPUT inputs[MAXDEPTH]; /* input sources */
static int openfile(char *name);
static int ttychar(void);
static int isinoderead(struct stat *sbuf);
static int findfreeread(void);
static int addreadset(char *name, char *path, struct stat *sbuf);
static char *homeexpand(char *name);
/*
* Open an input file by possibly searching through a path list
* and also possibly applying the specified extension. For example:
*
* opensearchfile("barf", ".:/tmp", ".c", rd_once)
*
* searches in order for the files:
*
* "./barf", "./barf.c", "/tmp/barf", and "/tmp/barf.c".
*
* Returns -1 if we could not open a file or error.
* Returns 1 if file was opened and added to/updated in the readset
* Returns 0 if file was already in the readset and reopen was 0.
*
* given:
* name file name to be read
* pathlist list of colon separated paths (or NULL)
* extension extra extension to try (or NULL)
* rd_once TRUE => do not reread a file
*/
int
opensearchfile(char *name, char *pathlist, char *extension, int rd_once)
{
int i;
char *cp;
char *path; /* name being searched for */
struct stat statbuf; /* stat of the path */
size_t namelen; /* length of name */
size_t extlen; /* length of the extension if non-NULL or 0 */
size_t pathlen; /* length of the pathlist if non-NULL or 0 */
/* firewall */
if (name == NULL) {
math_error("NULL name given to opensearchfile");
/*NOTREACHED*/
}
/*
* We ignore the pathlist of the file is absolute, dot-relative
* or tilde-relative or if there is no search path.
*/
if (name[0] == PATHCHAR ||
name[0] == HOMECHAR ||
(name[0] == DOTCHAR && name[1] == '\0') ||
(name[0] == DOTCHAR && name[1] == DOTCHAR && name[2] == '\0') ||
(name[0] == DOTCHAR && name[1] == PATHCHAR) ||
(name[0] == DOTCHAR && name[1] == DOTCHAR && name[2] == PATHCHAR) ||
pathlist == NULL) {
pathlist = "";
}
/*
* allocate storage for the longest name being searched for
*
* We will allocate more than we could ever want/need.
* The longest we could ever need would be:
*
* pathlist (as a single long string)
* /
* name
* .
* extension
* \0
* guard byte
*/
namelen = strlen(name);
if (extension != NULL) {
extlen = strlen(extension);
} else {
extlen = 0;
}
pathlen = strlen(pathlist);
path = malloc(pathlen+1 + 1 + namelen+1 + extlen+1 + 1 + 1);
if (path == NULL) {
math_error("Cannot allocate filename path buffer");
/*NOTREACHED*/
}
/*
* Don't try the extension if the filename already contains it.
*/
if (extension != NULL && namelen >= extlen &&
strcmp(&name[namelen-extlen], extension) == 0) {
extension = NULL;
}
/*
* Search through the path list for the file
*/
pathlist--;
do {
pathlist++;
cp = path;
while (*pathlist && (*pathlist != LISTCHAR))
*cp++ = *pathlist++;
if (cp != path)
*cp++ = PATHCHAR;
strncpy(cp, name, namelen+1);
i = openfile(path);
if ((i < 0) &&
(extension != NULL && extension[0] != '\0')) {
strcat(path, extension);
i = openfile(path);
}
} while ((i < 0) && *pathlist);
/* examine what our search produced */
if (i < 0) {
free(path);
return i;
}
if (cip->i_fp == NULL) {
/* cannot find a file to open */
free(path);
return -3;
}
if (fstat(fileno(cip->i_fp), &statbuf) < 0) {
/* unable to fstat the open file */
free(path);
return -4;
}
/* note if we will reopen a file and if that is allowed */
if (rd_once == TRUE && isinoderead(&statbuf) >= 0) {
/* file is in readset and reopen is false */
closeinput();
free(path);
return 1;
}
/* add this name to the readset if allowed */
if (addreadset(name, path, &statbuf) < 0) {
/* cannot add to readset */
closeinput();
free(path);
return -1;
}
/* file was added to/updated in readset */
free(path);
return 0;
}
/*
* f_pathopen - open an absolute filename, or a relative filename along a search path
*
* Open a file by possibly searching through a path list. For example:
*
* f_pathopen("curds", ".:/tmp:~/pub", "r", NULL)
*
* searches in order for a file that it can open for reading:
*
* "./curds", "/tmp/curds", "~/pub/curds"
*
* NOTE: ~ is expanded accordingly (see homeexpand() below).
*
* However is the file is /absolue/path/name or a ./dot/relative/name, or
* a ~/home/dir/name, or a ~user/home/name, then the pathlist is ignored
* we just attempt to open name.
*
* and opens the first one that exists and allows the mode.
*
* name file name to be read
* mode fopen() mode argument (one of "r", "w", "a", "r+", "w+", "a+")
* pathlist list of colon separated paths (or NULL)
* openpath if non-NULL, and file was opened, set to malloced path used to open
*
* returns:
* open file stream, NULL ==> file was not found or error
* If file was open and openpath was non-NULL, changed to point to path used to open
*/
FILE *
f_pathopen(char *name, char *mode, char *pathlist, char **openpath)
{
char *cp;
char *path; /* name being searched for */
size_t namelen; /* length of name */
size_t pathlen; /* length of the pathlist if non-NULL or 0 */
FILE *ret; /* return open stream or NULL */
/* firewall */
if (name == NULL) {
math_error("NULL name given to f_pathopen");
/*NOTREACHED*/
}
if (mode == NULL) {
math_error("NULL mode given to f_pathopen");
/*NOTREACHED*/
}
/*
* We ignore the pathlist of the file is absolute, dot-relative
* or tilde-relative or if there is no search path.
*/
if (name[0] == PATHCHAR ||
name[0] == HOMECHAR ||
(name[0] == DOTCHAR && name[1] == '\0') ||
(name[0] == DOTCHAR && name[1] == DOTCHAR && name[2] == '\0') ||
(name[0] == DOTCHAR && name[1] == PATHCHAR) ||
(name[0] == DOTCHAR && name[1] == DOTCHAR && name[2] == PATHCHAR) ||
pathlist == NULL) {
pathlist = "";
}
/*
* allocate storage for the longest name being searched for
*
* We will allocate more than we could ever want/need.
* The longest we could ever need would be:
*
* pathlist (as a single long string)
* /
* name
* \0
* guard byte
*/
namelen = strlen(name);
pathlen = strlen(pathlist);
path = malloc(pathlen+1 + 1 + namelen+1 + 1 + 1);
if (path == NULL) {
math_error("Cannot allocate f_pathopen buffer");
/*NOTREACHED*/
}
/*
* Search through the path list for the file
*/
pathlist--;
do {
pathlist++;
cp = path;
while (*pathlist && (*pathlist != LISTCHAR))
*cp++ = *pathlist++;
if (cp != path)
*cp++ = PATHCHAR;
strncpy(cp, name, namelen+1);
ret = f_open(path, mode);
} while ((ret == NULL) && *pathlist);
/* if caller wants to know the path, malloc it and return it */
if (openpath != NULL && ret != NULL) {
if (path[0] == HOMECHAR) {
*openpath = homeexpand(path);
} else {
*openpath = strdup(path);
}
if (*openpath == NULL) {
free(path);
fclose(ret);
math_error("cannot malloc return openpath buffer");
/*NOTREACHED*/
}
}
free(path);
/* return open file or NULL */
return ret;
}
/*
* Given a filename with a leading ~, expand it into a home directory for
* that user. This function will malloc the space for the expanded path.
*
* If the path is just ~, or begins with ~/, expand it to the home
* directory of the current user. If the environment variable $HOME
* is known, it will be used, otherwise the password file will be
* consulted.
*
* If the path is just ~username, or ~username/, expand it to the home
* directory of that user by looking it up in the password file.
*
* If the password file must be consulted and the username is not found
* a NULL pointer is returned.
*
* given:
* name a filename with a leading ~
*/
static char *
homeexpand(char *name)
{
#if defined(_WIN32)
return NULL;
#else /* Windoz free systems */
struct passwd *ent; /* password entry */
char *home2; /* fullpath of the home directory */
char *fullpath; /* the malloced expanded path */
char *after; /* after the ~user or ~ */
char *username; /* extracted username */
size_t fullpath_len; /* length of fullpath */
/* firewall */
if (name[0] != HOMECHAR)
return NULL;
/*
* obtain the home directory component
*/
switch (name[1]) {
case PATHCHAR: /* ~/... */
case '\0': /* ~ */
home2 = home;
after = name+1;
break;
default: /* ~username or ~username/... */
/* extract the username after the ~ */
after = (char *)strchr(name+2, PATHCHAR);
if (after == NULL) {
/* path is just ~username */
ent = (struct passwd *)getpwnam(name+1);
if (ent == NULL) {
/* unknown user */
return NULL;
}
/* just malloc the home directory and return it */
fullpath_len = strlen(ent->pw_dir);
fullpath = (char *)malloc(fullpath_len+1);
if (fullpath == NULL) {
return NULL;
}
strncpy(fullpath, ent->pw_dir, fullpath_len+1);
return fullpath;
}
username = (char *) malloc(after-name + 1 + 1);
if (username == NULL) {
/* failed to malloc username */
return NULL;
}
strncpy(username, name+1, after-name-1);
username[after-name-1] = '\0';
/* get that user's home directory */
ent = (struct passwd *)getpwnam(username);
free(username);
if (ent == NULL) {
/* unknown user */
return NULL;
}
home2 = ent->pw_dir;
break;
}
/*
* build the fullpath given the home directory
*/
fullpath = (char *)malloc(strlen(home2)+strlen(after)+1);
if (fullpath == NULL) {
return NULL;
}
sprintf(fullpath, "%s%s", home2, after);
return fullpath;
#endif /* Windoz free systems */
}
/*
* f_open - ~-expand a filename and fopen() it
*
* given:
* name the filename to open
* mode fopen() mode argument (one of "r", "w", "a", "r+", "w+", "a+")
*/
FILE *
f_open(char *name, char *mode)
{
FILE *fp; /* open file descriptor */
char *fullname; /* file name with HOMECHAR expansion */
/*
* be sore we are allowed to open a file in this mode
*/
if (!allow_read && !allow_write) {
/* no reads and no writes means no opens! */
if (run_state > RUN_BEGIN) {
fprintf(stderr,
"open of %s mode %s - %s\n", name, mode,
"open for read or write disallowed by -m\n");
}
return NULL;
} else if (!allow_read && strchr(mode, 'r') != NULL) {
/* reading new files disallowed */
if (run_state > RUN_BEGIN) {
fprintf(stderr,
"open of %s mode %s - %s\n", name, mode,
"open for read disallowed by -m\n");
}
return NULL;
} else if (!allow_write &&
(strchr(mode, 'w') != NULL ||
strchr(mode, 'a') != NULL ||
strchr(mode, '+') != NULL)) {
/* writing new files disallowed */
if (run_state > RUN_BEGIN) {
fprintf(stderr,
"open of %s mode %s - %s\n", name, mode,
"open for write disallowed by -m\n");
}
return NULL;
}
/*
* expand ~ if needed
*/
if (name[0] == HOMECHAR) {
fullname = homeexpand(name);
if (fullname == NULL)
return NULL;
fp = fopen(fullname, mode);
free(fullname);
} else {
fp = fopen(name, mode);
}
return fp;
}
/*
* Setup for reading from a input file.
* Returns -1 if file could not be opened.
*
* given:
* name file name to be read
*/
static int
openfile(char *name)
{
FILE *fp; /* open file descriptor */
size_t namelen;
if (depth >= MAXDEPTH)
return -2;
fp = f_open(name, "r");
if (fp == NULL)
return -1;
cip = inputs + depth++;
cip->i_state = IS_READ;
cip->i_char = '\0';
cip->i_str = NULL;
cip->i_ttystr = NULL;
cip->i_fp = fp;
cip->i_line = 1;
namelen = strlen(name);
cip->i_name = (char *)malloc(namelen+1);
if (cip->i_name == NULL) {
return -1;
}
strncpy(cip->i_name, name, namelen+1);
return 0;
}
/*
* Return the current input file stream, or NULL if none.
*/
FILE *
curstream(void)
{
if (depth <= 0 || depth > MAXDEPTH)
return NULL;
return cip->i_fp;
}
/*
* Open a string for scanning, num characters to be read.
* String is copied into local memory so it can be trashed afterwards.
* Returns -1 if cannot open string.
*
* given:
* str string to be opened
* num lengh of string to open
*/
int
openstring(char *str, size_t num)
{
char *cp; /* copied string */
if ((depth >= MAXDEPTH) || (str == NULL))
return -2;
cp = (char *) malloc(num + 1);
if (cp == NULL)
return -1;
strncpy(cp, str, num);
cp[num] = '\0'; /* firewall */
cip = inputs + depth++;
cip->i_state = IS_READ;
cip->i_char = '\0';
cip->i_cp = cp;
cip->i_str = cp;
cip->i_num = num;
cip->i_fp = NULL;
cip->i_name = NULL;
cip->i_ttystr = NULL;
cip->i_line = 1;
return 0;
}
/*
* Set to read input from the terminal.
* Returns -1 if there is no more depth for input.
*/
int
openterminal(void)
{
if (depth >= MAXDEPTH)
return -2;
cip = inputs + depth++;
cip->i_state = IS_READ;
cip->i_char = '\0';
cip->i_str = NULL;
cip->i_ttystr = NULL;
cip->i_fp = NULL;
cip->i_name = NULL;
cip->i_line = 1;
return 0;
}
/*
* Close the current input source.
*/
void
closeinput(void)
{
if (depth <= 0)
return;
if (cip->i_str)
free(cip->i_str);
if (cip->i_fp)
fclose(cip->i_fp);
if (cip->i_name)
free(cip->i_name);
depth--;
cip = depth ? &inputs[depth - 1] : NULL;
}
/*
* Reset the input sources back to the initial state.
*/
void
resetinput(void)
{
while (depth > 0)
closeinput();
noprompt = FALSE;
}
/*
* Set the prompt for terminal input.
*/
void
setprompt(char *str)
{
prompt = str;
noprompt = FALSE;
}
/*
* Read the next character from the current input source.
* End of file closes current input source, and returns EOF character.
*/
int
nextchar(void)
{
int ch; /* current input character */
if (depth == 0) /* input finished */
return EOF;
if (cip->i_state == IS_REREAD) { /* rereading current char */
ch = cip->i_char;
cip->i_state = IS_READ;
if (ch == '\n')
cip->i_line++;
return ch;
}
if (cip->i_str) { /* from string */
if (cip->i_num) {
ch = chartoint(*cip->i_cp++);
cip->i_num--;
} else {
ch = EOF;
}
} else if (cip->i_fp) { /* from file */
ch = fgetc(cip->i_fp);
} else if (!stdin_tty) { /* from file */
ch = fgetc(stdin);
} else { /* from terminal */
ch = ttychar();
}
if (depth > 0)
cip->i_char = ch; /* save for rereads */
if (ch == '\n')
cip->i_line++;
return ch;
}
/*
* Read in the next line of input from the current input source.
* The line is terminated with a null character, and does not contain
* the final newline character. The returned string is only valid
* until the next such call, and so must be copied if necessary.
* Returns NULL on end of file.
*/
char *
nextline(void)
{
char *cp;
int ch;
int len;
cp = linebuf;
if (linesize == 0) {
cp = (char *)malloc(TTYSIZE + 1);
if (cp == NULL) {
math_error("Cannot allocate line buffer");
/*NOTREACHED*/
}
linebuf = cp;
linesize = TTYSIZE;
}
len = 0;
for (;;) {
noprompt = TRUE;
ch = nextchar();
noprompt = FALSE;
if (ch == EOF)
return NULL;
if (ch == '\0')
continue;
if (ch == '\n')
break;
if (len >= linesize) {
cp = (char *)realloc(cp, linesize + TTYSIZE + 1);
if (cp == NULL) {
math_error("Cannot realloc line buffer");
/*NOTREACHED*/
}
linebuf = cp;
linesize += TTYSIZE;
}
cp[len++] = (char)ch;
}
cp[len] = '\0';
return linebuf;
}
/*
* Read the next character from the terminal.
* The routines in the history module are called so that the user
* can use a command history and emacs-like editing of the line.
*/
static int
ttychar(void)
{
int ch; /* current char */
int len; /* length of current command */
static char charbuf[1024];
/*
* If we have more to read from the saved command line, then do that.
* When we see a newline character, then clear the pointer so we will
* read a new line on the next call.
*/
if (cip->i_ttystr) {
ch = chartoint(*cip->i_ttystr++);
if (ch == '\n')
cip->i_ttystr = NULL;
return ch;
}
/*
* We need another complete line.
*/
abortlevel = 0;
inputwait = TRUE;
len = hist_getline(noprompt ? "" : prompt, charbuf, sizeof(charbuf));
if (len == 0) {
inputwait = FALSE;
return EOF;
}
inputwait = FALSE;
/*
* Handle shell escape if present
*/
if (charbuf[0] == '!') { /* do a shell command */
char *cmd;
cmd = charbuf + 1;
if (*cmd == '\0' || *cmd == '\n')
cmd = shell;
if (allow_exec) {
if (conf->calc_debug & CALCDBG_SYSTEM) {
printf("%s\n", cmd);
}
system(cmd);
} else {
fprintf(stderr, "execution disallowed by -m flag\n");
}
return '\n';
}
hist_saveline(charbuf, len);
/*
* Return the first character of the line, and set up to
* return the rest of it with later calls.
*/
ch = chartoint(charbuf[0]);
if (ch != '\n')
cip->i_ttystr = charbuf + 1;
return ch;
}
/*
* Return whether or not the input source is the terminal.
*/
BOOL
inputisterminal(void)
{
return ((depth <= 0) || ((cip->i_str == NULL) && (cip->i_fp == NULL)));
}
/*
* Return depth of current input source
*/
int
inputlevel(void)
{
return depth - 1;
}
/*
* Return the name of the current input file.
* Returns NULL for terminal or strings.
*/
char *
inputname(void)
{
if (depth <= 0)
return NULL;
return cip->i_name;
}
/*
* Return the current line number.
*/
long
linenumber(void)
{
if (depth > 0)
return cip->i_line;
return 1;
}
/*
* Restore the next character to be read again on the next nextchar call.
*/
void
reread(void)
{
if ((depth <= 0) || (cip->i_state == IS_REREAD))
return;
cip->i_state = IS_REREAD;
if (cip->i_char == '\n')
cip->i_line--;
}
/*
* Process all startup files found in the $CALCRC path.
*/
void
runrcfiles(void)
{
char path[MAX_CALCRC+1+1]; /* name being searched for */
char *cp;
char *p;
/* execute each file in the list */
while (calcrc != NULL && *calcrc) {
cp = calcrc;
calcrc = (char *) strchr(calcrc + 1, LISTCHAR);
/* load file name into the path */
if (calcrc == NULL) {
strncpy(path, cp, MAX_CALCRC+1);
} else {
strncpy(path, cp, calcrc - cp);
path[calcrc - cp] = '\0';
}
/* find the start of the path */
p = (path[0] == ':') ? path + 1 : path;
if (p[0] == '\0') {
continue;
}
/* process the current file in the list */
if (openfile(p) < 0) {
/* Unable to open rcfile */
if (c_flag && !d_flag)
fprintf(stderr,
"Unable to open rcfile \"%s\"\n", p);
continue;
}
getcommands(FALSE);
closeinput();
}
}
/*
* isinoderead - determine if we have read a given dev/inode
*
* This function returns the index of the readset element that matches
* a given device/inode, -1 otherwise.
*
*
* WIN32 NOTE:
*
* This function does not work under WIN32. The sbuf->st_ino is always
* zero because the FAT and NTFS filesystems do not support inodes.
* They also don't support links, which is why you need this function
* under UNIX. For WIN32, use _fullpath() to determine if you have
* already opened a file.
*
* given:
* sbuf stat of the inode in question
*/
static int
isinoderead(struct stat *sbuf)
{
int i;
#if defined(_WIN32) || defined(__MSDOS__)
char fullpathname[_MAX_PATH];
#endif
/* deal with the empty case */
if (readset == NULL || maxreadset <= 0) {
/* readset is empty */
return -1;
}
/* scan the entire readset */
for (i=0; i < maxreadset; ++i) {
#if defined(_WIN32) || defined(__MSDOS__)
if (readset[i].active &&
strcasecmp(readset[i].path,
_fullpath(fullpathname,cip->i_name,
_MAX_PATH)) == 0) {
/* found a match */
return i;
}
#else /* Windoz free systems */
if (readset[i].active &&
sbuf->st_dev == readset[i].inode.st_dev &&
sbuf->st_ino == readset[i].inode.st_ino) {
/* found a match */
return i;
}
#endif /* Windoz free systems */
}
/* no match found */
return -1;
}
/*
* findfreeread - find the next free readset element
*
* This function will return the index of the next free readset element.
* If needed, this function will allocate new readset elements.
*
* This function returns the index of the next free element, or -1.
*/
static int
findfreeread(void)
{
int i;
/* deal with an empty readset case */
if (readset == NULL || maxreadset <= 0) {
/* malloc a new readset */
readset = (READSET *)malloc((READSET_ALLOC+1)*sizeof(READSET));
if (readset == NULL) {
return -1;
}
maxreadset = READSET_ALLOC;
for (i=0; i < READSET_ALLOC; ++i) {
readset[i].active = 0;
}
/* return first entry */
return 0;
}
/* try to find a free readset entry */
for (i=0; i < maxreadset; ++i) {
if (readset[i].active == 0) {
/* found a free readset entry */
return i;
}
}
/* all readset entries are in use, allocate more */
readset = (READSET *)realloc(readset,
(maxreadset+READSET_ALLOC) * sizeof(READSET));
if (readset == NULL) {
return -1;
}
for (i=0; i < READSET_ALLOC; ++i) {
readset[i+maxreadset].active = 0;
}
maxreadset += READSET_ALLOC;
/* return the first newly allocated free entry */
return maxreadset-READSET_ALLOC;
}
/*
* addreadset - add a entry to the readset array if it is not already there
*
* This function attempts to add a file into the readset. If the readset
* has an entry with a matching dev/inode, then that entry is updated with
* the new name and path. If no such readset entry is found, a new entry
* is added.
*
* This function returns the index of the readset entry, or -1 if error.
*
* given:
* name name given to read or include
* path full pathname of file
* sbuf stat of the path
*/
static int
addreadset(char *name, char *path, struct stat *sbuf)
{
int ret; /* index to return */
size_t name_len; /* length of read set name */
size_t path_len; /* length of path to read set name */
/* find the inode */
ret = isinoderead(sbuf);
if (ret < 0) {
/* not in readset, find a free node */
ret = findfreeread();
if (ret < 0) {
/* cannot find/form a free readset entry */
return -1;
}
} else {
/* found an readset entry, free old readset data */
if (readset[ret].name != NULL) {
free(readset[ret].name);
}
if (readset[ret].path != NULL) {
free(readset[ret].path);
}
}
/* load our information into the readset entry */
name_len = strlen(name);
readset[ret].name = (char *)malloc(name_len+1);
if (readset[ret].name == NULL) {
return -1;
}
strncpy(readset[ret].name, name, name_len+1);
#if defined(_WIN32) || defined(__MSDOS__)
/*
* For WIN32, _fullpath expands the path to a fully qualified
* path name, which under WIN32 FAT and NTFS is unique, just
* like UNIX inodes. _fullpath also allocated the memory for
* this new longer path name.
*/
{
char fullpathname[_MAX_PATH];
readset[ret].path = _fullpath(fullpathname, path, _MAX_PATH);
}
#else /* Windoz free systems */
path_len = strlen(path);
readset[ret].path = (char *)malloc(path_len+1);
if (readset[ret].path == NULL) {
return -1;
}
strncpy(readset[ret].path, path, path_len+1);
#endif /* Windoz free systems */
readset[ret].inode = *sbuf;
readset[ret].active = 1;
/* return index of the newly added entry */
return ret;
}
|