| 1 |
/* |
| 2 |
* Copyright(C) 1999-2020, 2024 National Technology & Engineering Solutions |
| 3 |
* of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 4 |
* NTESS, the U.S. Government retains certain rights in this software. |
| 5 |
* |
| 6 |
* See packages/seacas/LICENSE for details |
| 7 |
*/ |
| 8 |
/* |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "fortranc.h" |
| 12 |
#include "getline_int.h" |
| 13 |
#include <stdio.h> |
| 14 |
#include <string.h> |
| 15 |
#include <unistd.h> |
| 16 |
|
| 17 |
static int my_getline(char *s, int len); |
| 18 |
static char *copy_string(char *dest, char const *source, long int elements) |
| 19 |
{ |
| 20 |
char *d; |
| 21 |
for (d = dest; d + 1 < dest + elements && *source; d++, source++) { |
| 22 |
*d = *source; |
| 23 |
} |
| 24 |
*d = '\0'; |
| 25 |
return d; |
| 26 |
} |
| 27 |
|
| 28 |
#if defined(ADDC_) |
| 29 |
void exread_(char *prompt, char *input, FTNINT *iostat, long int PromptLength, long int InputLength) |
| 30 |
#else |
| 31 |
void exread(char *prompt, char *input, FTNINT *iostat, long int PromptLength, long int InputLength) |
| 32 |
#endif |
| 33 |
|
| 34 |
/* |
| 35 |
************************************************************************ |
| 36 |
|
| 37 |
C DESCRIPTION: |
| 38 |
C This routine prompts, reads, and echos from the standard input |
| 39 |
C device. For an interactive job, this would prompt for input from |
| 40 |
C the terminal and read (with echo) from the keyboard. For a batch |
| 41 |
C job, this would read from the main input file and echo to the |
| 42 |
C log file with the prompt string as a prefix. This routine should |
| 43 |
C assume the burden of assuring that the standard input and output |
| 44 |
C devices are properly opened. |
| 45 |
|
| 46 |
C FORMAL PARAMETERS: |
| 47 |
C PROMPT CHARACTER Prompt String |
| 48 |
C INPUT CHARACTER Input String |
| 49 |
C IOSTAT INTEGER I/O Status ( -1 = EOF, 0 = normal ) |
| 50 |
|
| 51 |
************************************************************************ |
| 52 |
*/ |
| 53 |
{ |
| 54 |
static int debug = 0; |
| 55 |
if (debug == 1 || isatty(0) == 0 || isatty(1) == 0) { |
| 56 |
int icnt; |
| 57 |
|
| 58 |
(void)write(1, prompt, PromptLength); |
| 59 |
icnt = my_getline(input, InputLength); |
| 60 |
|
| 61 |
/* Next evaluate the error status. */ |
| 62 |
/* For icnt < 0 indicate an error condition. */ |
| 63 |
*iostat = (icnt >= 0) ? 0 : -1; |
| 64 |
} |
| 65 |
else { |
| 66 |
static char internal_prompt[128]; |
| 67 |
char *p = NULL; |
| 68 |
|
| 69 |
/* Fill line with blanks... */ |
| 70 |
int dlen = InputLength; |
| 71 |
char *ds = input; |
| 72 |
while (dlen-- > 0) /* Blank out the entire string. */ |
| 73 |
*ds++ = ' '; |
| 74 |
|
| 75 |
copy_string(internal_prompt, prompt, 128); |
| 76 |
internal_prompt[PromptLength - 1] = ' '; |
| 77 |
internal_prompt[PromptLength] = '\0'; |
| 78 |
|
| 79 |
p = getline_int(internal_prompt); |
| 80 |
|
| 81 |
if (p) { |
| 82 |
gl_histadd(p); |
| 83 |
/* Strip the trailing \n */ |
| 84 |
p[strlen(p) - 1] = '\0'; |
| 85 |
|
| 86 |
size_t i = 0; |
| 87 |
while (i < strlen(p) && i < (size_t)InputLength) { |
| 88 |
input[i] = p[i]; |
| 89 |
++i; |
| 90 |
} |
| 91 |
*iostat = 0; |
| 92 |
} |
| 93 |
else { |
| 94 |
*iostat = -1; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
static int my_getline(char *s, int len) |
| 100 |
{ |
| 101 |
char c; |
| 102 |
int dlen; |
| 103 |
char nl = '\n'; |
| 104 |
|
| 105 |
char *ds; /* A dummy argument used in nulling out s. */ |
| 106 |
|
| 107 |
dlen = len; |
| 108 |
ds = s; |
| 109 |
while (dlen-- > 0) /* Blank out the entire string. */ |
| 110 |
*ds++ = ' '; |
| 111 |
|
| 112 |
dlen = len; /* Now take care of business. */ |
| 113 |
for (; dlen > 0; dlen--) { |
| 114 |
if (read(0, &c, 1) != 1) { |
| 115 |
if (dlen == len) { |
| 116 |
(void)write(1, &nl, 1); /* We've encountered the End of File */ |
| 117 |
return (-1); |
| 118 |
} |
| 119 |
else |
| 120 |
return (len - dlen + 1); |
| 121 |
} |
| 122 |
else if (c == '\n' || c == '\r') |
| 123 |
return (len - dlen + 1); |
| 124 |
|
| 125 |
*s++ = c; |
| 126 |
} |
| 127 |
/* If we get this far, we've read 'len' characters without hitting the |
| 128 |
end of the line. Read until get end-of-line or end-of-file |
| 129 |
*/ |
| 130 |
while (read(0, &c, 1) == 1 && c != '\n' && c != '\r') |
| 131 |
; |
| 132 |
return (len - dlen + 1); |
| 133 |
} |