| 1 |
/* |
| 2 |
* The Regina Rexx Interpreter |
| 3 |
* Copyright (C) 1992-1994 Anders Christensen <[email protected]> |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Library General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Library General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Library General Public |
| 16 |
* License along with this library; if not, write to the Free |
| 17 |
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* This module is a real pain, since file I/O is one of the features |
| 22 |
* that varies most between different platforms. And what makes it |
| 23 |
* even more of a pain, is the fact that it must be coordinated with |
| 24 |
* the handling of the condition NOTREADY. Anyway, here are the |
| 25 |
* decisions set up before (well ... during) the implementation that |
| 26 |
* guide how this this thing is supposed to work. |
| 27 |
* |
| 28 |
* There are four kind of routines, structured in four levels: |
| 29 |
* |
| 30 |
* (1)---------+ (2)--------+ |
| 31 |
* | builtin | ----> | general | B C library |
| 32 |
* | functions | A | routines | ----> Routines |
| 33 |
* +-----------+ +----------+ |
| 34 |
* | | |
| 35 |
* | | |
| 36 |
* | V (3)--------+ |
| 37 |
* +----------------->+---> | Error | |
| 38 |
* | routines | |
| 39 |
* +----------+ |
| 40 |
* |
| 41 |
* 1) Builtin functions, these has the "std_" prefix which is standard |
| 42 |
* for all buildin functions. The task for these functions are to |
| 43 |
* process parameters, call (2) which specializes on operations (like |
| 44 |
* read, write, position etc), and return a decent answer back to its |
| 45 |
* caller. There is one routine in this level for each of the |
| 46 |
* functions in the library of built-in functions. Most of them are |
| 47 |
* std_* functions, but there are a few others too. |
| 48 |
* |
| 49 |
* 2) These are general operations for reading, writing, positioning, |
| 50 |
* etc. They may call the C library routines directly, or |
| 51 |
* indirectly, through calls to (3). The interface (A) between (1) |
| 52 |
* and (2) is based on the local structure fileboxptr and strengs. |
| 53 |
* There are one function in this level for each of the basic |
| 54 |
* operations needed to be performed on a file. Opening, closing, |
| 55 |
* reading a line, writing a line, line positioning, reading chars, |
| 56 |
* writing chars, positioning chars, counting lines, counting |
| 57 |
* chars, etc. The interface (B) to the C library routines uses |
| 58 |
* FILE* and char* for its operations. |
| 59 |
* |
| 60 |
* 3) General routines to perform 'trivial' tasks. In this level, |
| 61 |
* things like retriving Rexx's file table entries are implemented, |
| 62 |
* and all the errorhandling. These are called from both the two |
| 63 |
* previous levels. |
| 64 |
* |
| 65 |
* There are three standard files, called "<stdin>", "<stdout>" and |
| 66 |
* "<stderr>" (note that the "<" and ">" are part of the filename.) |
| 67 |
* These are handles for the equivalent Unix standard files. This |
| 68 |
* might cause problems if you actually do want a file calls that, or |
| 69 |
* if one of these files is closed, and the more information is |
| 70 |
* written to it (I can easily visulize Users trying to delete such a |
| 71 |
* file :-)) So the standard files -- having set flag SURVIVOR -- will |
| 72 |
* never be closed or reopened. |
| 73 |
* |
| 74 |
* Error_file is called by that routine which actually discovers the |
| 75 |
* problem. If it is an CALL ON condition, it will set the FLAG_FAKE |
| 76 |
* flag, which all other routines will check for. |
| 77 |
*/ |
| 78 |
|
| 79 |
/* |
| 80 |
* Bug in LCC complier wchar.h that incorrectly says it defines stat struct |
| 81 |
* but doesn't |
| 82 |
*/ |
| 83 |
#if defined(__LCC__) |
| 84 |
# include <sys/stat.h> |
| 85 |
#endif |
| 86 |
|
| 87 |
#include "rexx.h" |
| 88 |
#include <errno.h> |
| 89 |
#include <stdio.h> |
| 90 |
#include <string.h> |
| 91 |
#ifdef HAVE_ASSERT_H |
| 92 |
# include <assert.h> |
| 93 |
#endif |
| 94 |
#ifdef HAVE_LIMITS_H |
| 95 |
# include <limits.h> |
| 96 |
#endif |
| 97 |
#include <time.h> |
| 98 |
#if defined(VMS) |
| 99 |
# include <stat.h> |
| 100 |
#elif defined(OS2) |
| 101 |
# include <sys/stat.h> |
| 102 |
# ifdef HAVE_UNISTD_H |
| 103 |
# include <unistd.h> |
| 104 |
# endif |
| 105 |
#elif (defined(__WATCOMC__) && !defined(__QNX__)) || defined(_MSC_VER) || defined(__LCC__) |
| 106 |
# include <sys/stat.h> /* MH 10-06-96 */ |
| 107 |
# include <fcntl.h> /* MH 10-06-96 */ |
| 108 |
# ifdef HAVE_UNISTD_H |
| 109 |
# include <unistd.h> /* MH 10-06-96 */ |
| 110 |
# endif |
| 111 |
# if defined(_MSC_VER) && !defined(__WINS__) |
| 112 |
# include <io.h> |
| 113 |
# endif |
| 114 |
#elif defined(WIN32) && defined(__IBMC__) /* LM 26-02-99 */ |
| 115 |
# include <io.h> |
| 116 |
# include <sys/stat.h> |
| 117 |
# include <fcntl.h> |
| 118 |
#elif defined(MAC) |
| 119 |
# include "mac.h" |
| 120 |
#else |
| 121 |
# include <sys/stat.h> |
| 122 |
# ifdef HAVE_PWD_H |
| 123 |
# include <pwd.h> |
| 124 |
#endif |
| 125 |
# ifdef HAVE_GRP_H |
| 126 |
# include <grp.h> |
| 127 |
# endif |
| 128 |
# include <fcntl.h> |
| 129 |
# ifdef HAVE_UNISTD_H |
| 130 |
# include <unistd.h> |
| 131 |
# endif |
| 132 |
#endif |
| 133 |
|
| 134 |
#ifdef HAVE_DIRECT_H |
| 135 |
# include <direct.h> |
| 136 |
#endif |
| 137 |
|
| 138 |
#ifdef __EMX__ |
| 139 |
# include <io.h> |
| 140 |
#endif |
| 141 |
|
| 142 |
#if defined(WIN32) |
| 143 |
# ifdef _MSC_VER |
| 144 |
# if _MSC_VER >= 1100 |
| 145 |
/* Stupid MSC can't compile own headers without warning at least in VC 5.0 */ |
| 146 |
# pragma warning(disable: 4115 4201 4214 4514) |
| 147 |
# endif |
| 148 |
# endif |
| 149 |
# include <windows.h> |
| 150 |
# ifdef _MSC_VER |
| 151 |
# if _MSC_VER >= 1100 |
| 152 |
# pragma warning(default: 4115 4201 4214) |
| 153 |
# endif |
| 154 |
# endif |
| 155 |
# if defined(__WATCOMC__) || defined(__BORLANDC__) |
| 156 |
# include <io.h> |
| 157 |
# endif |
| 158 |
#endif |
| 159 |
|
| 160 |
#ifndef min |
| 161 |
# define min(a,b) (((a) < (b)) ? (a) : (b)) |
| 162 |
#endif |
| 163 |
|
| 164 |
/* |
| 165 |
* Stuff for FSTAT |
| 166 |
*/ |
| 167 |
#if defined(S_IFDIR) && !defined(S_ISDIR) |
| 168 |
# define S_ISDIR(mode) (((mode) & S_IFDIR)==S_IFDIR) |
| 169 |
#endif |
| 170 |
#if defined(S_IFREG) && !defined(S_ISREG) |
| 171 |
# define S_ISREG(mode) (((mode) & S_IFREG)==S_IFREG) |
| 172 |
#endif |
| 173 |
#if defined(S_IFCHR) && !defined(S_ISCHR) |
| 174 |
# define S_ISCHR(mode) (((mode) & S_IFCHR)==S_IFCHR) |
| 175 |
#endif |
| 176 |
#if defined(S_IFBLK) && !defined(S_ISBLK) |
| 177 |
# define S_ISBLK(mode) (((mode) & S_IFBLK)==S_IFBLK) |
| 178 |
#endif |
| 179 |
#if defined(S_IFLNK) && !defined(S_ISLNK) |
| 180 |
# define S_ISLNK(mode) (((mode) & S_IFLNK)==S_IFLNK) |
| 181 |
#endif |
| 182 |
#if defined(S_IFFIFO) && !defined(S_ISFIFO) |
| 183 |
# define S_ISFIFO(mode) (((mode) & S_IFFIFO)==S_IFFIFO) |
| 184 |
#endif |
| 185 |
#if defined(S_IFSOCK) && !defined(S_ISSOCK) |
| 186 |
# define S_ISSOCK(mode) (((mode) & S_IFSOCK)==S_IFSOCK) |
| 187 |
#endif |
| 188 |
#if defined(S_IFNAM) && !defined(S_ISNAM) |
| 189 |
# define S_ISNAM(mode) (((mode) & S_IFNAM)==S_IFNAM) |
| 190 |
#endif |
| 191 |
|
| 192 |
#if !defined(ACCESSPERMS) |
| 193 |
# if defined(S_IRWXU) && defined(S_IRWXG) && defined(S_IRWXO) |
| 194 |
# define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) |
| 195 |
# else |
| 196 |
# if defined(S_IREAD) && defined(S_IWRITE) && defined(S_IEXEC) |
| 197 |
# define ACCESSPERMS (S_IREAD|S_IWRITE|S_IEXEC) |
| 198 |
# else |
| 199 |
# define ACCESSPERMS (0xfff) |
| 200 |
# endif |
| 201 |
# endif |
| 202 |
#endif |
| 203 |
/* |
| 204 |
* The macrodefinition below defines the various modes in which a |
| 205 |
* file may be opened. These modes are: |
| 206 |
* |
| 207 |
* READ - Open for readonly access. The current read position |
| 208 |
* is set to the the start of the file. If the file does |
| 209 |
* not exist, report an error. |
| 210 |
* |
| 211 |
* WRITE - Open for read and write. The current read position is |
| 212 |
* set to the start of the file, while the current write |
| 213 |
* position is set to EOF. If file does not exist, create |
| 214 |
* it. If file does exist, use existing data. |
| 215 |
* |
| 216 |
* UPDATE - The combined operation of READ and WRITE, but if file |
| 217 |
* does not exist, issue an error. |
| 218 |
* |
| 219 |
* APPEND - Open in APPEND mode, i.e. open for writeonly, position |
| 220 |
* at the End-Of-File, and (if possible) open in a mode |
| 221 |
* that disallows positioning. The file will be a transient |
| 222 |
* file. If the file does not exist, create it. |
| 223 |
* |
| 224 |
* CREATE - Open for write, but if the file does exist, truncate |
| 225 |
* it at the beginning after opening it. |
| 226 |
*/ |
| 227 |
#define ACCESS_NONE 0 |
| 228 |
#define ACCESS_READ 1 |
| 229 |
#define ACCESS_WRITE 2 |
| 230 |
#define ACCESS_UPDATE 3 |
| 231 |
#define ACCESS_APPEND 4 |
| 232 |
#define ACCESS_CREATE 5 |
| 233 |
#define ACCESS_STREAM_APPEND 6 |
| 234 |
#define ACCESS_STREAM_REPLACE 7 |
| 235 |
|
| 236 |
/* |
| 237 |
* These macros is used to set the value of the 'oper' in the filebox |
| 238 |
* data structure. If last operation on a file was a read or a write, |
| 239 |
* set 'oper' to OPER_READ or OPER_WRITE, respectively. If last |
| 240 |
* operation was repositioning or flushing, use OPER_NONE. See |
| 241 |
* description of 'oper' field in definition of 'filebox'. |
| 242 |
*/ |
| 243 |
#define OPER_NONE 0 |
| 244 |
#define OPER_READ 1 |
| 245 |
#define OPER_WRITE 2 |
| 246 |
|
| 247 |
/* |
| 248 |
* Flags, carrying information about files. The 'flag' field in the |
| 249 |
* 'filebox' structure is set to values matching these defintions. The |
| 250 |
* meaning of each of these flags is: |
| 251 |
* |
| 252 |
* PERSIST - Set if file is persistent, if unset, file is treated |
| 253 |
* as a transient file. |
| 254 |
* EOF - Currently not in use |
| 255 |
* READ - File has been opened for read access. |
| 256 |
* WRITE - File has been opened for write access. |
| 257 |
* CREATE - Currently not in use |
| 258 |
* ERROR - Set if the file is in error state. If operations are |
| 259 |
* attempted performed on files in state error, the |
| 260 |
* NOTREADY condition will in general be raised, and the |
| 261 |
* operation will fail. |
| 262 |
* SURVIVOR - Set for certain special files; the default streams, which |
| 263 |
* is not really to be closed or reopened. |
| 264 |
* FAKE - Meaningful only if ERROR is set. If FAKE is set, and |
| 265 |
* an operation on the file is attempted, the operation is |
| 266 |
* 'faked' (NOTREADY is not triggered, and the result returned |
| 267 |
* for write operations does not report that the output was |
| 268 |
* not written. |
| 269 |
* WREOF - Current write position is at EOF. If line output is |
| 270 |
* performed, there is no need to truncate the file. |
| 271 |
* RDEOF - Current read position is at EOF. Reading EOF raises the |
| 272 |
* NOTREADY condition, but does not put the file into error |
| 273 |
* state. |
| 274 |
* AFTER_RDEOF - Bit of a hack here. This flag is set after an attempt |
| 275 |
* (Added by MH) is made to read a stream once the RDEOF flag is set. |
| 276 |
* The reason for this is that all the "read" stream |
| 277 |
* functions; LINEIN, LINES, CHARIN, etc set the RDEOF |
| 278 |
* flag at the point that they determine a RDEOF has |
| 279 |
* occurred. This is usually at the end of the function. |
| 280 |
* Therefore a LINEIN that reads EOF sets RDEOF and a |
| 281 |
* subsequent call to STREAM(stream,'S') will return |
| 282 |
* NOTREADY. This to me is logical, but the behaviour |
| 283 |
* of other interpreters is that the first call to |
| 284 |
* STREAM(stream,'S') after reaching EOF should still return |
| 285 |
* READY. Only when ANOTHER "read" stream function is |
| 286 |
* called does STREAM(stream,'S') return NOTREADY. |
| 287 |
* SWAPPED - This flag is set if the file is currently swapped out, that |
| 288 |
* is, the file is closed in order to let another file use |
| 289 |
* the system's file table sloth freed when the file was |
| 290 |
* temporarily closed. |
| 291 |
*/ |
| 292 |
#define FLAG_PERSIST 0x0001 |
| 293 |
#define FLAG_EOF 0x0002 |
| 294 |
#define FLAG_READ 0x0004 |
| 295 |
#define FLAG_WRITE 0x0008 |
| 296 |
#define FLAG_CREATE 0x0010 |
| 297 |
#define FLAG_ERROR 0x0020 |
| 298 |
#define FLAG_SURVIVOR 0x0040 |
| 299 |
#define FLAG_FAKE 0x0080 |
| 300 |
#define FLAG_WREOF 0x0100 |
| 301 |
#define FLAG_RDEOF 0x0200 |
| 302 |
#define FLAG_SWAPPED 0x0400 |
| 303 |
#define FLAG_AFTER_RDEOF 0x0800 |
| 304 |
|
| 305 |
/* |
| 306 |
* So, what is the big difference between FAKE and ERROR. Well, when a |
| 307 |
* file gets it ERROR flag set, it signalizes that the file is in |
| 308 |
* error state, and that no fileoperations should be performed on it. |
| 309 |
* The FAKE flag is only meaningful when the ERROR flag is set. If set |
| 310 |
* the FAKE flag tells that file operations should be faked in order to |
| 311 |
* give the user the impression that everything is OK, while if FAKE is |
| 312 |
* not set, errors are returned. |
| 313 |
* |
| 314 |
* The clue is that if a statement contains several operations on one |
| 315 |
* file, and the first operation bombs, CALL ON NOTREADY will not take |
| 316 |
* effect before the next statement boundary at the same procedural |
| 317 |
* level So, for the rest of the file operations until that statement |
| 318 |
* has finished, the FAKE flag is set, and signalizes that OK result |
| 319 |
* should be returned whenever positioning or write is performed, and |
| 320 |
* that NOTREADY should not be raised again. |
| 321 |
* |
| 322 |
* The reason for the RDEOF flag is that reading beyond EOF is not really |
| 323 |
* a capital crime, and a lot of programmers are likely to do that, and |
| 324 |
* expect things to be OK after repositioning current read position to |
| 325 |
* another part of the file. If a file is put into ERROR state, it has |
| 326 |
* to be explicitly reset in order to do any useful to it afterwards. |
| 327 |
* Therefore, if EOF is seen on input, RDEOF is set, and NOTREADY is |
| 328 |
* raised, but the file is not put into ERROR state. |
| 329 |
*/ |
| 330 |
|
| 331 |
/* |
| 332 |
* The following macros defines symbolic names to the commands available |
| 333 |
* in the Rexx built-in function STREAM(). The meaning of each of these |
| 334 |
* commands are: |
| 335 |
* |
| 336 |
* READ - Opens the file with the corresponding mode. For a deeper |
| 337 |
* WRITE description of each of these modes, see the defininition |
| 338 |
* APPEND of the ACCESS_* macros. STREAM() is used to explicitly |
| 339 |
* UPDATE open a file, while Rexx is totally happy with the |
| 340 |
* CREATE traditional implicit opening, i.e. that the file is |
| 341 |
* opened for the needed access at the time when it is |
| 342 |
* first used. If the file to be opened is already open, |
| 343 |
* it will first be closed, and then opened in the |
| 344 |
* specified mode. |
| 345 |
* |
| 346 |
* CLOSE - Closes a file, works for any type of access. But if |
| 347 |
* the file is a default stream, it will not be closed. |
| 348 |
* Default streams should not be closed. |
| 349 |
* |
| 350 |
* FLUSH - Performs flushing on the file. Actually, I'm not so |
| 351 |
* sure whether that is very interesting, since flushing |
| 352 |
* is always performed after a write, anyway. Though, it |
| 353 |
* might become an important function if the automatic |
| 354 |
* flushing after write is removed (e.g. to improve speed). |
| 355 |
* |
| 356 |
* STATUS - Returns status information associated with the file as |
| 357 |
* a human readable string. The information returned is the |
| 358 |
* internal information that Rexx stores in the Rexx file |
| 359 |
* table entry for that file. Use FSTAT to get information |
| 360 |
* about the file from the operating system. See the |
| 361 |
* function 'getrexxstatus()' for more information about |
| 362 |
* the layout of the returned string. |
| 363 |
* |
| 364 |
* FSTAT - Returns status information associated with the file as |
| 365 |
* a human readable string. The information returned is the |
| 366 |
* information normally returned by the stat() system call |
| 367 |
* under Unix (i.e. size, dates, access modes, etc). Use |
| 368 |
* STATUS to get Rexx's information about the file. See |
| 369 |
* the function 'getstatus()' for more information about |
| 370 |
* the layout of the string returned. |
| 371 |
* |
| 372 |
* RESET - Resets the file after an error. Of course, this will |
| 373 |
* only work for files which are 'resettable'. If the error |
| 374 |
* is too serious, resetting will help little to fix the |
| 375 |
* problem. E.g. writing beyond end-of-file can easily be |
| 376 |
* fixed by RESET, trying to use a file which is named |
| 377 |
* by an invalid syntax can not be correctly reset. |
| 378 |
* |
| 379 |
* READABLE - Checks that the file in question is available in the |
| 380 |
* WRITABLE mode given, for the user that is executing the script. |
| 381 |
* EXECUTABLE I.e. READABLE will return '1' for a file, if the file |
| 382 |
* is readable for the user, else '0' is returned. Note |
| 383 |
* that FSTAT returns the information about the accessmodes |
| 384 |
* for a file, these returns the 'accessmode' which is |
| 385 |
* relevant for a particular user. Also note that if your |
| 386 |
* machine are using suid-bit (i.e. Unix), this function |
| 387 |
* will check for the real uid, not the effective uid. |
| 388 |
* Consequently, it may not give the wanted result for |
| 389 |
* suid rexx scripts, see the Unix access() function. (And |
| 390 |
* anyway, suid scripts are a _very_ bad idea under Unix, |
| 391 |
* so this is probably not a problem ... :-) |
| 392 |
*/ |
| 393 |
#define COMMAND_NONE 0 |
| 394 |
#define COMMAND_READ 1 |
| 395 |
#define COMMAND_WRITE 2 |
| 396 |
#define COMMAND_APPEND 3 |
| 397 |
#define COMMAND_UPDATE 4 |
| 398 |
#define COMMAND_CREATE 5 |
| 399 |
#define COMMAND_CLOSE 6 |
| 400 |
#define COMMAND_FLUSH 7 |
| 401 |
#define COMMAND_STATUS 8 |
| 402 |
#define COMMAND_FSTAT 9 |
| 403 |
#define COMMAND_RESET 10 |
| 404 |
#define COMMAND_READABLE 11 |
| 405 |
#define COMMAND_WRITEABLE 12 |
| 406 |
#define COMMAND_EXECUTABLE 13 |
| 407 |
#define COMMAND_LIST 14 |
| 408 |
#define COMMAND_QUERY_DATETIME 15 |
| 409 |
#define COMMAND_QUERY_EXISTS 16 |
| 410 |
#define COMMAND_QUERY_HANDLE 17 |
| 411 |
#define COMMAND_QUERY_SEEK 18 |
| 412 |
#define COMMAND_QUERY_SIZE 19 |
| 413 |
#define COMMAND_QUERY_STREAMTYPE 20 |
| 414 |
#define COMMAND_QUERY_TIMESTAMP 21 |
| 415 |
#define COMMAND_QUERY_CREATETIME 22 |
| 416 |
#define COMMAND_QUERY_MODIFYTIME 23 |
| 417 |
#define COMMAND_QUERY_ACCESSTIME 24 |
| 418 |
#define COMMAND_QUERY_POSITION 25 |
| 419 |
#define COMMAND_QUERY 26 |
| 420 |
#define COMMAND_QUERY_POSITION_READ 27 |
| 421 |
#define COMMAND_QUERY_POSITION_WRITE 28 |
| 422 |
#define COMMAND_QUERY_POSITION_SYS 29 |
| 423 |
#define COMMAND_QUERY_POSITION_READ_CHAR 30 |
| 424 |
#define COMMAND_QUERY_POSITION_READ_LINE 31 |
| 425 |
#define COMMAND_QUERY_POSITION_WRITE_CHAR 32 |
| 426 |
#define COMMAND_QUERY_POSITION_WRITE_LINE 33 |
| 427 |
#define COMMAND_OPEN 34 |
| 428 |
#define COMMAND_OPEN_READ 35 |
| 429 |
#define COMMAND_OPEN_WRITE 36 |
| 430 |
#define COMMAND_OPEN_BOTH 37 |
| 431 |
#define COMMAND_OPEN_BOTH_APPEND 38 |
| 432 |
#define COMMAND_OPEN_BOTH_REPLACE 39 |
| 433 |
#define COMMAND_OPEN_WRITE_APPEND 40 |
| 434 |
#define COMMAND_OPEN_WRITE_REPLACE 41 |
| 435 |
#define COMMAND_SEEK 42 |
| 436 |
#define COMMAND_POSITION 43 |
| 437 |
|
| 438 |
#define STREAMTYPE_UNKNOWN 0 |
| 439 |
#define STREAMTYPE_PERSISTENT 1 |
| 440 |
#define STREAMTYPE_TRANSIENT 2 |
| 441 |
/* |
| 442 |
* Define TRUE_TRL_IO, if you want the I/O system to be even more like |
| 443 |
* TRL. It will try to mimic the behaviour in TRL exactly. Note that if |
| 444 |
* you _do_ define this, you might experience a degrade in runtime |
| 445 |
* performance. |
| 446 |
*/ |
| 447 |
#define TRUE_TRL_IO |
| 448 |
|
| 449 |
/* |
| 450 |
* There are two ways to report an error for file I/O operations. Either |
| 451 |
* as an "error" or as a "warning". Both will raise the NOTREADY |
| 452 |
* condition, but only ERROR will actually put the file into ERROR mode. |
| 453 |
* Warnings are used for e.g. EOF while reading. Both are implemented |
| 454 |
* by the same routine. |
| 455 |
*/ |
| 456 |
#define file_error(a,b,c) handle_file_error(TSD,a,b,c,1) |
| 457 |
#define file_warning(a,b,c) handle_file_error(TSD,a,b,c,0) |
| 458 |
|
| 459 |
/* |
| 460 |
* CASE_SENSITIVE_FILENAMES is used to determine if internal file |
| 461 |
* pointers respect the case of files and treat "ABC" as a different |
| 462 |
* file to "abc". |
| 463 |
*/ |
| 464 |
#ifdef UNIX |
| 465 |
# define CASE_SENSITIVE_FILENAMES |
| 466 |
#endif |
| 467 |
/* |
| 468 |
* Regina truncates a file when repositioning by the use of a line |
| 469 |
* count. That is, if the file has ten lines, and you use the BIF |
| 470 |
* lineout(file,,4), it will be truncated after the fourth line. |
| 471 |
* Truncating is not performed for character repositioning. |
| 472 |
* |
| 473 |
* If you don't want truncating after line repositioning, undefine |
| 474 |
* the macro HAVE_FTRUNCATE in config.h. Also, if your system doesn't |
| 475 |
* have ftruncate(), undefine HAVE_FTRUNCATE, and survive without the |
| 476 |
* truncating. |
| 477 |
* |
| 478 |
* The function ftruncate() is a BSDism; if you have trouble finding |
| 479 |
* it, try linking with -lbsd or -lucb or something like that. Since |
| 480 |
* it is not a standard POSIX feature, some machines may generate |
| 481 |
* warnings during compilation. Let's help these machines ... |
| 482 |
*/ |
| 483 |
#if defined(FIX_PROTOS) && defined(HAVE_FTRUNCATE) |
| 484 |
# if defined(ultrix) |
| 485 |
int ftruncate( int fd, int length ) ; |
| 486 |
# endif |
| 487 |
#endif |
| 488 |
|
| 489 |
/* |
| 490 |
* Since development of Ultrix has ceased, and they never managed to |
| 491 |
* fix a few things, we want to define a few things, just in order |
| 492 |
* to kill a few warnings ... |
| 493 |
*/ |
| 494 |
#if defined(FIX_PROTOS) && defined(FIX_ALL_PROTOS) && defined(ultrix) |
| 495 |
int fstat( int fd, struct stat *buf ) ; |
| 496 |
int stat( char *path, struct stat *buf ) ; |
| 497 |
#endif |
| 498 |
|
| 499 |
#include "regina64.h" |
| 500 |
|
| 501 |
#if defined(WIN32) |
| 502 |
/* |
| 503 |
* Work around for bug in WIN32 stat() function; can't have trailing slash |
| 504 |
*/ |
| 505 |
# if defined(HAVE__STATI64) |
| 506 |
static int rx_w32_stat( const char *path, struct _stati64 *buffer ) |
| 507 |
{ |
| 508 |
int lastpos; |
| 509 |
int rc; |
| 510 |
char tmpstring[REXX_PATH_MAX ]; |
| 511 |
|
| 512 |
rc = _stati64( path, buffer ); |
| 513 |
if ( rc != 0 && errno == ENOENT ) |
| 514 |
{ |
| 515 |
if ( path ) |
| 516 |
{ |
| 517 |
lastpos = strlen( path ) - 1; |
| 518 |
if ( path[lastpos] == '\\' || path[lastpos] == '/') |
| 519 |
{ |
| 520 |
memcpy( tmpstring, path, lastpos ) ; |
| 521 |
tmpstring[lastpos] = '\0'; |
| 522 |
rc = _stati64( tmpstring, buffer ); |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
return rc; |
| 527 |
} |
| 528 |
# else |
| 529 |
static int rx_w32_stat( const char *path, struct _stat *buffer ) |
| 530 |
{ |
| 531 |
int lastpos; |
| 532 |
int rc; |
| 533 |
char tmpstring[REXX_PATH_MAX ]; |
| 534 |
|
| 535 |
rc = stat( path, buffer ); |
| 536 |
if ( rc != 0 && errno == ENOENT ) |
| 537 |
{ |
| 538 |
if ( path ) |
| 539 |
{ |
| 540 |
lastpos = strlen( path ) - 1; |
| 541 |
if ( path[lastpos] == '\\'|| path[lastpos] == '/') |
| 542 |
{ |
| 543 |
memcpy( tmpstring, path, lastpos ) ; |
| 544 |
tmpstring[lastpos+1] = '\0'; |
| 545 |
rc = stat( tmpstring, buffer ); |
| 546 |
} |
| 547 |
} |
| 548 |
} |
| 549 |
return rc; |
| 550 |
} |
| 551 |
# endif |
| 552 |
#endif |
| 553 |
|
| 554 |
/* |
| 555 |
* Here comes another 'sunshine-story' ... Since SunOS don't have |
| 556 |
* a decent set of include-files in the standard version of the OS, |
| 557 |
* their <stdio.h> don't define these macros. Instead, Sun seems to |
| 558 |
* survive with the old custom of using the numberic values of these |
| 559 |
* macros directly. If compiled with "SunKlugdes" defined, try to |
| 560 |
* fix this. |
| 561 |
* |
| 562 |
* If you are using gcc on a Sun, you may want to run the program |
| 563 |
* fixincludes that comes with gcc. It will fix this more permanently. |
| 564 |
* At least one recent version of GCC for VMS doesn't have this |
| 565 |
|
| 566 |
*/ |
| 567 |
#if defined(SunKludges) || (defined(__GNUC__) && defined(VMS)) |
| 568 |
# define SEEK_SET 0 |
| 569 |
# define SEEK_CUR 1 |
| 570 |
# define SEEK_END 2 |
| 571 |
#endif |
| 572 |
|
| 573 |
/* |
| 574 |
* Some machines don't defined these ... they should! |
| 575 |
*/ |
| 576 |
#if defined(VMS) || defined(_MSC_VER) || (defined(WIN32) && defined(__IBMC__)) || (defined(WIN32) && defined(__BORLANDC__)) || defined(__LCC__) |
| 577 |
# define F_OK 0 |
| 578 |
# define X_OK 1 |
| 579 |
# define W_OK 2 |
| 580 |
# define R_OK 4 |
| 581 |
#endif |
| 582 |
|
| 583 |
/* |
| 584 |
* Here is the datastructure in which to store the information about |
| 585 |
* open files. The storage format of the file table is discussed |
| 586 |
* elsewhere. The fields used to handle storing are 'next' and 'prev' |
| 587 |
* which is used to implement a double linked list of files having |
| 588 |
* the same hashfunc; and 'newer' and 'older' which are used to maintain |
| 589 |
* a large double linked list of all files in order of the most |
| 590 |
* recently used file. |
| 591 |
* |
| 592 |
* The other fields are: |
| 593 |
* |
| 594 |
* fileptr - Pointer to the filehandle use by the system when |
| 595 |
* accessing the file through the normal I/O calls. |
| 596 |
* If this pointer is NULL, it means that the file is |
| 597 |
* not currently open. |
| 598 |
* oper - Holds the value that tells whether the most recent |
| 599 |
* operation on this file was a read or a write. This has |
| 600 |
* importance for flushing, since a read can't imediately |
| 601 |
* follow a write (or vice versa) without a flush (or |
| 602 |
* a repositioning) inbetween. Takes the values OPER_READ, |
| 603 |
* OPER_WRITE or OPER_NONE (signalizes that most recent |
| 604 |
* operation can be followed by either read or write). |
| 605 |
* flag - Bitfield that holds information about the file. The |
| 606 |
* significance of the various fields are described by |
| 607 |
* the FLAG_* macros. |
| 608 |
* error - Most recently 'errno' code for this file. It could have |
| 609 |
* been stored into 'errmsg' instead, but that would require |
| 610 |
* copying of data which might not be used. If undefined, |
| 611 |
* it will have the value 0. |
| 612 |
* readpos - The current read position in the file, as a character |
| 613 |
* position. Note that this is in 'C-syntax', i.e. the |
| 614 |
* first character in the file is numbered "0". A value of |
| 615 |
* -1 means that the value is unknown or undefined. |
| 616 |
* readline - The line number of the current read position, which must |
| 617 |
* be positive if define. A value of zero means that the |
| 618 |
* line number is undefined or unknown. If current read |
| 619 |
* position is at an EOL, 'readline' refers to the line |
| 620 |
* preceding the EOL. |
| 621 |
* writepos - Similar to 'readpos' but for current write position. |
| 622 |
* writeline - Similar to 'readline' but for current write position. |
| 623 |
* filename0 - Pointer to string containing the filename assiciated |
| 624 |
* with this file. This string is garanteed to have an |
| 625 |
* ASCII NUL following the last character, so it can be |
| 626 |
* used directly in file operations. This field *must* |
| 627 |
* be defined. |
| 628 |
* errmsg - Error message associated with the file. Some errors are |
| 629 |
* not trapped during call to system routines, and these |
| 630 |
* does not have an error message defined by the opsys. |
| 631 |
* E.g. when positioning current read position after EOF. |
| 632 |
* This field stores errormessages for these situations. |
| 633 |
* If undefined, it will be a NULL pointer. |
| 634 |
* |
| 635 |
* Both errmsg and error can not be defined simultaneously. |
| 636 |
*/ |
| 637 |
|
| 638 |
typedef struct fileboxtype *fileboxptr ; |
| 639 |
typedef const struct fileboxtype *cfileboxptr ; |
| 640 |
typedef struct fileboxtype { |
| 641 |
FILE *fileptr ; |
| 642 |
unsigned char oper ; |
| 643 |
// size_t readpos, writepos, thispos ; |
| 644 |
rx_64 readpos, writepos, thispos ; |
| 645 |
rx_64 readline, writeline, linesleft ; |
| 646 |
int flag, error; |
| 647 |
fileboxptr prev, next ; /* within a filehash entry */ |
| 648 |
fileboxptr newer, older ; |
| 649 |
streng *filename0 ; |
| 650 |
streng *errmsg ; |
| 651 |
} filebox ; |
| 652 |
|
| 653 |
/* POSIX denies read and write operations on streams without intermediate |
| 654 |
* fflush, fseek, fsetpos or rewind (list from EMX). We use the following |
| 655 |
* macros to switch directly before an I/O operation. "Useful" fseeks should |
| 656 |
* be error checked. This is not necessary here since the following operation |
| 657 |
* will fault in case of an error. |
| 658 |
*/ |
| 659 |
|
| 660 |
#define SWITCH_OPER_READ(fptr) {if (fptr->oper==OPER_WRITE) \ |
| 661 |
rx_fseek(fptr->fileptr,0l,SEEK_CUR); \ |
| 662 |
fptr->oper=OPER_READ;} |
| 663 |
#define SWITCH_OPER_WRITE(fptr) {if (fptr->oper==OPER_READ) \ |
| 664 |
rx_fseek(fptr->fileptr,0l,SEEK_CUR); \ |
| 665 |
fptr->oper=OPER_WRITE;} |
| 666 |
|
| 667 |
typedef struct |
| 668 |
{ /* fil_tsd: static variables of this module (thread-safe) */ |
| 669 |
/* |
| 670 |
* The following two pointers are pointers into the doble linked list |
| 671 |
* of all files in the file table. They points to the most recently |
| 672 |
* used file, and the least recently used open file. Note that the latter |
| 673 |
* of these are _not_ the same as the last file in the list. If the |
| 674 |
* Rexx' file table contains more files than the system's file table |
| 675 |
* can contain, 'lrufile' will point to the last open file in the double |
| 676 |
* linked list. Files further out in the list are 'swapped' out. |
| 677 |
*/ |
| 678 |
fileboxptr mrufile; |
| 679 |
|
| 680 |
fileboxptr stdio_ptr[6]; |
| 681 |
void * rdarea; |
| 682 |
fileboxptr filehash[131]; |
| 683 |
int rol_size ; /* readoneline() */ |
| 684 |
char * rol_string ; /* readoneline() */ |
| 685 |
int got_eof ; /* readkbdline() */ |
| 686 |
} fil_tsd_t; /* thread-specific but only needed by this module. see |
| 687 |
* init_filetable |
| 688 |
*/ |
| 689 |
/* |
| 690 |
* Structure to define stream types; names and whether persistent,transient or unknown |
| 691 |
*/ |
| 692 |
typedef struct |
| 693 |
{ |
| 694 |
int streamtype; |
| 695 |
char *streamname; |
| 696 |
} stream_type_t; |
| 697 |
|
| 698 |
#define STREAMTYPE_DIRECTORY 1 |
| 699 |
#define STREAMTYPE_CHARACTERSPECIAL 2 |
| 700 |
#define STREAMTYPE_BLOCKSPECIAL 3 |
| 701 |
#define STREAMTYPE_REGULARFILE 4 |
| 702 |
#define STREAMTYPE_FIFO 5 |
| 703 |
#define STREAMTYPE_SYMBOLICLINK 6 |
| 704 |
#define STREAMTYPE_SOCKET 7 |
| 705 |
#define STREAMTYPE_SPECIALNAME 8 |
| 706 |
|
| 707 |
static const stream_type_t stream_types[] = |
| 708 |
{ |
| 709 |
{ STREAMTYPE_UNKNOWN , "" }, |
| 710 |
{ STREAMTYPE_UNKNOWN , " Directory" }, |
| 711 |
{ STREAMTYPE_PERSISTENT," CharacterSpecial" }, |
| 712 |
{ STREAMTYPE_PERSISTENT," BlockSpecial" }, |
| 713 |
{ STREAMTYPE_PERSISTENT," RegularFile" }, |
| 714 |
{ STREAMTYPE_UNKNOWN , " FIFO" }, |
| 715 |
{ STREAMTYPE_UNKNOWN , " SymbolicLink" }, |
| 716 |
{ STREAMTYPE_UNKNOWN , " Socket" }, |
| 717 |
{ STREAMTYPE_UNKNOWN , " SpecialName" }, |
| 718 |
}; |
| 719 |
|
| 720 |
static rx_64 positioncharfile( tsd_t *TSD, const char *bif, int argno, fileboxptr fileptr, int oper, rx_64 where, int from ); |
| 721 |
static rx_64 positionfile( tsd_t *TSD, const char *bif, int argno, fileboxptr ptr, int oper, rx_64 lineno, int from ); |
| 722 |
static void handle_file_error( tsd_t *TSD, fileboxptr ptr, int rc, const char *errmsg, int level) ; |
| 723 |
static int flush_output( tsd_t *TSD, fileboxptr ptr ); |
| 724 |
static const char *translateTilde( tsd_t *TSD, const char *src ); |
| 725 |
int my_rx_stat( tsd_t *TSD, const char *src, struct stat *statbuf ); |
| 726 |
|
| 727 |
/* |
| 728 |
* Based on the st_mode filed returned from stat(), determine the Regina "stream type". |
| 729 |
* The returned value is an index into stream_types array, which allows the caller to |
| 730 |
* determine if the stream is persistent, transient or unknown, and also allows the |
| 731 |
* user to look up the stream type name. |
| 732 |
* Added to resolve 802114. |
| 733 |
*/ |
| 734 |
static int determine_stream_type( int mode ) |
| 735 |
{ |
| 736 |
#ifdef S_ISDIR |
| 737 |
if ( S_ISDIR(mode) ) |
| 738 |
return STREAMTYPE_DIRECTORY; |
| 739 |
#endif |
| 740 |
#ifdef S_ISCHR |
| 741 |
if ( S_ISCHR(mode) ) |
| 742 |
return STREAMTYPE_CHARACTERSPECIAL; |
| 743 |
#endif |
| 744 |
#ifdef S_ISBLK |
| 745 |
if ( S_ISBLK(mode) ) |
| 746 |
return STREAMTYPE_BLOCKSPECIAL; |
| 747 |
#endif |
| 748 |
#ifdef S_ISREG |
| 749 |
if ( S_ISREG(mode) ) |
| 750 |
return STREAMTYPE_REGULARFILE; |
| 751 |
#endif |
| 752 |
#ifdef S_ISFIFO |
| 753 |
if ( S_ISFIFO(mode) ) |
| 754 |
return STREAMTYPE_FIFO; |
| 755 |
#endif |
| 756 |
#ifdef S_ISLNK |
| 757 |
if ( S_ISLNK(mode) ) |
| 758 |
return STREAMTYPE_SYMBOLICLINK; |
| 759 |
#endif |
| 760 |
#ifdef S_ISSOCK |
| 761 |
if ( S_ISSOCK(mode) ) |
| 762 |
return STREAMTYPE_SOCKET; |
| 763 |
#endif |
| 764 |
#ifdef S_ISNAM |
| 765 |
if ( S_ISNAM(mode) ) |
| 766 |
return STREAMTYPE_SPECIALNAME; |
| 767 |
#endif |
| 768 |
return STREAMTYPE_UNKNOWN; |
| 769 |
} |
| 770 |
|
| 771 |
/* |
| 772 |
* Marks all entries in the filetable. Used only by the memory |
| 773 |
* management. Does not really change anything, so you can in general |
| 774 |
* forget this one. This routine is called from memory.c in order to |
| 775 |
* mark all statically defined data in this file. |
| 776 |
*/ |
| 777 |
#ifdef TRACEMEM |
| 778 |
void mark_filetable( const tsd_t *TSD ) |
| 779 |
{ |
| 780 |
fileboxptr ptr=NULL ; |
| 781 |
fil_tsd_t *ft; |
| 782 |
|
| 783 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 784 |
for (ptr=ft->mrufile; ptr; ptr=ptr->older) |
| 785 |
{ |
| 786 |
markmemory( ptr, TRC_FILEPTR ) ; |
| 787 |
markmemory( ptr->filename0, TRC_FILEPTR ) ; |
| 788 |
if (ptr->errmsg) |
| 789 |
markmemory( ptr->errmsg, TRC_FILEPTR ) ; |
| 790 |
} |
| 791 |
|
| 792 |
if (ft->rdarea) |
| 793 |
markmemory( ft->rdarea, TRC_FILEPTR ) ; |
| 794 |
|
| 795 |
} |
| 796 |
#endif /* TRACEMEM */ |
| 797 |
|
| 798 |
#if defined(WIN32) && defined(_MSC_VER) |
| 799 |
/* |
| 800 |
* This is a replacement fo the BSD ftruncate() function. |
| 801 |
* The code in this function was written by Les Moull. |
| 802 |
*/ |
| 803 |
|
| 804 |
int ftruncate( int fd, long pos ) |
| 805 |
{ |
| 806 |
HANDLE h = (HANDLE)_get_osfhandle( fd ) ; |
| 807 |
|
| 808 |
if (SetFilePointer( h, pos, NULL, FILE_BEGIN) == 0xFFFFFFFF) |
| 809 |
return -1; |
| 810 |
|
| 811 |
if ( !SetEndOfFile( h ) ) |
| 812 |
return -1; |
| 813 |
|
| 814 |
return 0; |
| 815 |
} |
| 816 |
#endif |
| 817 |
|
| 818 |
#if defined(__WATCOMC__) && defined(__QNX__) |
| 819 |
# define ftruncate( fd, pos ) ltrunc( fd, pos, SEEK_SET ) |
| 820 |
#endif |
| 821 |
|
| 822 |
/* |
| 823 |
* This command maps the string 'cmd' into a number which is to be |
| 824 |
* interpreted according to the settings of the COMMAND_ macros. |
| 825 |
* The input strings must be one of the valid command, or else the |
| 826 |
* COMMAND_NONE value is returned. |
| 827 |
* |
| 828 |
* Well, this routine should really have been implemented differently, |
| 829 |
* since sequential searching through a list of strings is not very |
| 830 |
* efficient. But still, it is not so many entries in the list, and |
| 831 |
* this function is not going to be called often, so I suppose it |
| 832 |
* doesn't matter too much. Ideallistic, it should be rewritten to |
| 833 |
* a binary search. |
| 834 |
*/ |
| 835 |
|
| 836 |
static char get_command( streng *cmd ) |
| 837 |
{ |
| 838 |
Str_upper(cmd); |
| 839 |
|
| 840 |
if (cmd->len>=5 && !memcmp(cmd->value, "QUERY", 5)) |
| 841 |
return COMMAND_QUERY ; |
| 842 |
if (cmd->len>=4 && !memcmp(cmd->value, "OPEN", 4)) |
| 843 |
return COMMAND_OPEN ; |
| 844 |
if (cmd->len==5 && !memcmp(cmd->value, "CLOSE", 5)) |
| 845 |
return COMMAND_CLOSE ; |
| 846 |
if (cmd->len==5 && !memcmp(cmd->value, "FLUSH", 5)) |
| 847 |
return COMMAND_FLUSH ; |
| 848 |
if (cmd->len==6 && !memcmp(cmd->value, "STATUS", 6)) |
| 849 |
return COMMAND_STATUS ; |
| 850 |
if (cmd->len==5 && !memcmp(cmd->value, "FSTAT", 5)) |
| 851 |
return COMMAND_FSTAT ; |
| 852 |
if (cmd->len==5 && !memcmp(cmd->value, "RESET", 5)) |
| 853 |
return COMMAND_RESET ; |
| 854 |
if (cmd->len==8 && !memcmp(cmd->value, "READABLE", 8)) |
| 855 |
return COMMAND_READABLE ; |
| 856 |
if (cmd->len==8 && !memcmp(cmd->value, "WRITABLE", 8)) |
| 857 |
return COMMAND_WRITEABLE ; |
| 858 |
if (cmd->len==8 && !memcmp(cmd->value, "WRITEABLE", 9)) |
| 859 |
return COMMAND_WRITEABLE ; |
| 860 |
if (cmd->len==10 && !memcmp(cmd->value, "EXECUTABLE", 10)) |
| 861 |
return COMMAND_EXECUTABLE ; |
| 862 |
if (cmd->len>=4 && !memcmp(cmd->value, "SEEK", 4)) |
| 863 |
return COMMAND_SEEK ; |
| 864 |
if (cmd->len>=8 && !memcmp(cmd->value, "POSITION", 8)) |
| 865 |
return COMMAND_POSITION ; |
| 866 |
if (cmd->len==4 && !memcmp(cmd->value, "READ", 4)) |
| 867 |
return COMMAND_READ ; |
| 868 |
if (cmd->len==5 && !memcmp(cmd->value, "WRITE", 5)) |
| 869 |
return COMMAND_WRITE ; |
| 870 |
if (cmd->len==6 && !memcmp(cmd->value, "APPEND", 6)) |
| 871 |
return COMMAND_APPEND ; |
| 872 |
if (cmd->len==6 && !memcmp(cmd->value, "UPDATE", 6)) |
| 873 |
return COMMAND_UPDATE ; |
| 874 |
if (cmd->len==6 && !memcmp(cmd->value, "CREATE", 6)) |
| 875 |
return COMMAND_CREATE ; |
| 876 |
if (cmd->len==4 && !memcmp(cmd->value, "LIST", 4)) /* not implemented */ |
| 877 |
return COMMAND_LIST ; |
| 878 |
return COMMAND_NONE ; |
| 879 |
} |
| 880 |
|
| 881 |
static char get_querycommand( const streng *cmd ) |
| 882 |
{ |
| 883 |
if (cmd->len==6 && !memcmp(cmd->value, "EXISTS", 6)) |
| 884 |
return COMMAND_QUERY_EXISTS ; |
| 885 |
if (cmd->len==4 && !memcmp(cmd->value, "SIZE", 4)) |
| 886 |
return COMMAND_QUERY_SIZE ; |
| 887 |
if (cmd->len==8 && !memcmp(cmd->value, "DATETIME", 8)) |
| 888 |
return COMMAND_QUERY_DATETIME ; |
| 889 |
if (cmd->len>=4 && !memcmp(cmd->value, "SEEK", 4)) |
| 890 |
return COMMAND_QUERY_SEEK ; |
| 891 |
if (cmd->len>=8 && !memcmp(cmd->value, "POSITION", 8)) |
| 892 |
return COMMAND_QUERY_POSITION ; |
| 893 |
if (cmd->len==10 && !memcmp(cmd->value, "STREAMTYPE", 10)) |
| 894 |
return COMMAND_QUERY_STREAMTYPE ; |
| 895 |
if (cmd->len==9 && !memcmp(cmd->value, "TIMESTAMP", 9)) |
| 896 |
return COMMAND_QUERY_TIMESTAMP ; |
| 897 |
if (cmd->len==10 && !memcmp(cmd->value, "CREATETIME", 10)) |
| 898 |
return COMMAND_QUERY_CREATETIME; |
| 899 |
if (cmd->len==10 && !memcmp(cmd->value, "MODIFYTIME", 10)) |
| 900 |
return COMMAND_QUERY_MODIFYTIME; |
| 901 |
if (cmd->len==10 && !memcmp(cmd->value, "ACCESSTIME", 10)) |
| 902 |
return COMMAND_QUERY_ACCESSTIME; |
| 903 |
if (cmd->len==6 && !memcmp(cmd->value, "HANDLE", 6)) |
| 904 |
return COMMAND_QUERY_HANDLE ; |
| 905 |
return COMMAND_NONE ; |
| 906 |
} |
| 907 |
|
| 908 |
static char get_querypositioncommand( const streng *cmd ) |
| 909 |
{ |
| 910 |
if (cmd->len>=4 && !memcmp(cmd->value, "READ", 4)) |
| 911 |
return COMMAND_QUERY_POSITION_READ ; |
| 912 |
if (cmd->len>=5 && !memcmp(cmd->value, "WRITE", 5)) |
| 913 |
return COMMAND_QUERY_POSITION_WRITE ; |
| 914 |
if (cmd->len==3 && !memcmp(cmd->value, "SYS", 3)) |
| 915 |
return COMMAND_QUERY_POSITION_SYS ; |
| 916 |
return COMMAND_NONE ; |
| 917 |
} |
| 918 |
|
| 919 |
static char get_querypositionreadcommand( const streng *cmd ) |
| 920 |
{ |
| 921 |
if (cmd->len==4 && !memcmp(cmd->value, "CHAR", 4)) |
| 922 |
return COMMAND_QUERY_POSITION_READ_CHAR ; |
| 923 |
if (cmd->len==4 && !memcmp(cmd->value, "LINE", 4)) |
| 924 |
return COMMAND_QUERY_POSITION_READ_LINE ; |
| 925 |
if (cmd->len==0) |
| 926 |
return COMMAND_QUERY_POSITION_READ_CHAR ; |
| 927 |
return COMMAND_NONE ; |
| 928 |
} |
| 929 |
|
| 930 |
static char get_querypositionwritecommand( const streng *cmd ) |
| 931 |
{ |
| 932 |
if (cmd->len==4 && !memcmp(cmd->value, "CHAR", 4)) |
| 933 |
return COMMAND_QUERY_POSITION_WRITE_CHAR ; |
| 934 |
if (cmd->len==4 && !memcmp(cmd->value, "LINE", 4)) |
| 935 |
return COMMAND_QUERY_POSITION_WRITE_LINE ; |
| 936 |
if (cmd->len==0) |
| 937 |
return COMMAND_QUERY_POSITION_WRITE_CHAR ; |
| 938 |
return COMMAND_NONE ; |
| 939 |
} |
| 940 |
|
| 941 |
static char get_opencommand( const streng *cmd ) |
| 942 |
{ |
| 943 |
if (cmd->len>=4 && !memcmp(cmd->value, "BOTH", 4)) |
| 944 |
return COMMAND_OPEN_BOTH ; |
| 945 |
if (cmd->len==4 && !memcmp(cmd->value, "READ", 4)) |
| 946 |
return COMMAND_OPEN_READ ; |
| 947 |
if (cmd->len>=5 && !memcmp(cmd->value, "WRITE", 5)) |
| 948 |
return COMMAND_OPEN_WRITE ; |
| 949 |
if (cmd->len==0) |
| 950 |
return COMMAND_OPEN_BOTH ; |
| 951 |
return COMMAND_NONE ; |
| 952 |
} |
| 953 |
|
| 954 |
static char get_opencommandboth( const streng *cmd ) |
| 955 |
{ |
| 956 |
if (cmd->len==6 && !memcmp(cmd->value, "APPEND", 6)) |
| 957 |
return COMMAND_OPEN_BOTH_APPEND ; |
| 958 |
if (cmd->len==7 && !memcmp(cmd->value, "REPLACE", 7)) |
| 959 |
return COMMAND_OPEN_BOTH_REPLACE ; |
| 960 |
if (cmd->len==0) |
| 961 |
return COMMAND_OPEN_BOTH ; |
| 962 |
return COMMAND_NONE ; |
| 963 |
} |
| 964 |
|
| 965 |
static char get_opencommandwrite( const streng *cmd ) |
| 966 |
{ |
| 967 |
if (cmd->len==6 && !memcmp(cmd->value, "APPEND", 6)) |
| 968 |
return COMMAND_OPEN_WRITE_APPEND ; |
| 969 |
if (cmd->len==7 && !memcmp(cmd->value, "REPLACE", 7)) |
| 970 |
return COMMAND_OPEN_WRITE_REPLACE ; |
| 971 |
if (cmd->len==0) |
| 972 |
return COMMAND_OPEN_WRITE ; |
| 973 |
return COMMAND_NONE ; |
| 974 |
} |
| 975 |
|
| 976 |
|
| 977 |
/* ==================================================================== */ |
| 978 |
/* level 3 routines */ |
| 979 |
|
| 980 |
/* |
| 981 |
* Returns the fileboxptr of a file, if is has already been opened. |
| 982 |
* If it does not exist in Rexx's file table, a NULL pointer is |
| 983 |
* returned in stead. It is easy to change the datastruction in |
| 984 |
* which the file table is stored. |
| 985 |
* |
| 986 |
* If using VMS, or another opsys that has a caseinsensitive file |
| 987 |
* system, maybe it should disregard the case of the filename. In |
| 988 |
* general, maybe it should 'normalize' the file name before storing |
| 989 |
* it in the file table (do we sence an upcoming namei() :-) |
| 990 |
*/ |
| 991 |
|
| 992 |
#define FILEHASH_SIZE (sizeof(((fil_tsd_t*)0)->filehash) / \ |
| 993 |
sizeof(((fil_tsd_t*)0)->filehash[0])) |
| 994 |
|
| 995 |
#ifdef CASE_SENSITIVE_FILENAMES |
| 996 |
#define filehashvalue(strng) (hashvalue(strng->value, strng->len) % FILEHASH_SIZE) |
| 997 |
#else |
| 998 |
#define filehashvalue(strng) (hashvalue_ic(strng->value, strng->len) % FILEHASH_SIZE) |
| 999 |
#endif |
| 1000 |
|
| 1001 |
static void removefileptr( const tsd_t *TSD, cfileboxptr ptr ) |
| 1002 |
{ |
| 1003 |
fil_tsd_t *ft; |
| 1004 |
|
| 1005 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 1006 |
|
| 1007 |
if (ft->mrufile==ptr) |
| 1008 |
ft->mrufile = ptr->older ; |
| 1009 |
|
| 1010 |
if (ptr->older) |
| 1011 |
ptr->older->newer = ptr->newer ; |
| 1012 |
|
| 1013 |
if (ptr->newer) |
| 1014 |
ptr->newer->older = ptr->older ; |
| 1015 |
|
| 1016 |
if (ptr->next) |
| 1017 |
ptr->next->prev = ptr->prev ; |
| 1018 |
|
| 1019 |
if (ptr->prev) |
| 1020 |
ptr->prev->next = ptr->next ; |
| 1021 |
else |
| 1022 |
ft->filehash[filehashvalue(ptr->filename0)] = ptr->next ; |
| 1023 |
} |
| 1024 |
|
| 1025 |
/* enterfileptr initializes a fileboxptr. It must be allocated and the |
| 1026 |
* following fields must already been set: |
| 1027 |
* errmsg, error, fileptr, flag, filename0 |
| 1028 |
*/ |
| 1029 |
static void enterfileptr( const tsd_t *TSD, fileboxptr ptr ) |
| 1030 |
{ |
| 1031 |
int hashval=0 ; |
| 1032 |
fil_tsd_t *ft; |
| 1033 |
|
| 1034 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 1035 |
|
| 1036 |
/* |
| 1037 |
* First, get the magic number for this file. Note that when we're |
| 1038 |
* doing hashing like this, we *may* get trouble on machines that |
| 1039 |
* don't differ between upper and lower case letters in filenames. |
| 1040 |
*/ |
| 1041 |
hashval = filehashvalue(ptr->filename0) ; |
| 1042 |
/* |
| 1043 |
* Then, link it into the list of values having the same hashvalue |
| 1044 |
*/ |
| 1045 |
ptr->next = ft->filehash[hashval] ; |
| 1046 |
if (ptr->next) |
| 1047 |
ptr->next->prev = ptr ; |
| 1048 |
ft->filehash[hashval] = ptr ; |
| 1049 |
ptr->prev = NULL ; |
| 1050 |
|
| 1051 |
/* |
| 1052 |
* Then, link it into the 'global' list of files, sorted by how |
| 1053 |
* recently they have been used. |
| 1054 |
*/ |
| 1055 |
ptr->older = ft->mrufile ; |
| 1056 |
if (ptr->older) |
| 1057 |
ptr->older->newer = ptr ; |
| 1058 |
ptr->newer = NULL ; |
| 1059 |
ft->mrufile = ptr ; |
| 1060 |
|
| 1061 |
ptr->readline = 0 ; |
| 1062 |
ptr->linesleft = 0 ; |
| 1063 |
ptr->writeline = 0 ; |
| 1064 |
ptr->thispos = (size_t) EOF ; |
| 1065 |
ptr->readpos = (size_t) EOF ; |
| 1066 |
ptr->writepos = (size_t) EOF ; |
| 1067 |
ptr->oper = OPER_NONE; |
| 1068 |
} |
| 1069 |
|
| 1070 |
/* swapout_file swaps out one closeable file. The state persists. Use |
| 1071 |
* swapin_file to reuse it. |
| 1072 |
* The given fileboxptr MUST NOT be swapped out. It indicates a file which |
| 1073 |
* should be swapped in after this operation. dont_swap may be NULL. |
| 1074 |
*/ |
| 1075 |
static void swapout_file( tsd_t *TSD, fileboxptr dont_swap ) |
| 1076 |
{ |
| 1077 |
fil_tsd_t *ft; |
| 1078 |
fileboxptr start, run, found; |
| 1079 |
|
| 1080 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 1081 |
/* |
| 1082 |
* Too many open files simultaneously, we have to close one down |
| 1083 |
* in order to free one file descriptor, but only if there actually |
| 1084 |
* are some files that can be closed down. |
| 1085 |
*/ |
| 1086 |
found = NULL; |
| 1087 |
|
| 1088 |
if ( ( start = dont_swap ) == NULL ) /* any start point is better than */ |
| 1089 |
start = ft->mrufile ; /* mru head. We need the opposite */ |
| 1090 |
|
| 1091 |
/* first try finding an older file */ |
| 1092 |
for ( run = start ; run ; run = run->older ) |
| 1093 |
{ |
| 1094 |
if ( ( (run->flag & ( FLAG_SURVIVOR | FLAG_SWAPPED ) ) == 0 ) && |
| 1095 |
( run->fileptr != NULL ) && |
| 1096 |
( run != dont_swap ) ) |
| 1097 |
found = run ; /* continue looking for an older file */ |
| 1098 |
} |
| 1099 |
|
| 1100 |
/* if !found, try finding a more recent swapable file */ |
| 1101 |
if ( found == NULL ) |
| 1102 |
{ |
| 1103 |
for ( run = start ; run ; run = run->newer ) |
| 1104 |
{ |
| 1105 |
if ( ( (run->flag & ( FLAG_SURVIVOR | FLAG_SWAPPED ) ) == 0 ) && |
| 1106 |
( run->fileptr != NULL ) && |
| 1107 |
( run != dont_swap ) ) |
| 1108 |
{ |
| 1109 |
found = run ; /* least newer swapable file */ |
| 1110 |
break; |
| 1111 |
} |
| 1112 |
} |
| 1113 |
} |
| 1114 |
if ( found == NULL ) |
| 1115 |
exiterror( ERR_SYSTEM_FAILURE, 0 ) ; |
| 1116 |
|
| 1117 |
flush_output( TSD, found ); |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
/* swapout_all swaps out all closeable files. The states persist. Use |
| 1122 |
* swapin_file to reuse them. |
| 1123 |
* This function is useful when exiting the external's interface. |
| 1124 |
*/ |
| 1125 |
static void swapout_all( tsd_t *TSD ) |
| 1126 |
{ |
| 1127 |
fil_tsd_t *ft; |
| 1128 |
fileboxptr run; |
| 1129 |
|
| 1130 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 1131 |
|
| 1132 |
for ( run = ft->mrufile ; run ; run = run->older ) |
| 1133 |
{ |
| 1134 |
flush_output( TSD, run ); |
| 1135 |
} |
| 1136 |
} |
| 1137 |
|
| 1138 |
#ifdef VMS |
| 1139 |
static const char *acc_mode[] = { "r", "r+", "a" } ; |
| 1140 |
#else |
| 1141 |
static const char *acc_mode[] = { "rb", "r+b", "ab" } ; |
| 1142 |
#endif |
| 1143 |
|
| 1144 |
#define ACCMODE_READ 0 |
| 1145 |
#define ACCMODE_RDWRT 1 |
| 1146 |
#define ACCMODE_WRITE 2 |
| 1147 |
#define ACCMODE_NONE 3 |
| 1148 |
|
| 1149 |
static void swapin_file( tsd_t *TSD, fileboxptr ptr ) |
| 1150 |
{ |
| 1151 |
int faccess=0, itmp=0 ; |
| 1152 |
|
| 1153 |
/* |
| 1154 |
* First, just try to reopen the file, we _might_ have a vacant |
| 1155 |
* entry in the system file table, so, use that. |
| 1156 |
*/ |
| 1157 |
itmp = (ptr->flag & (FLAG_READ | FLAG_WRITE)) ; |
| 1158 |
if (itmp==(FLAG_READ | FLAG_WRITE)) |
| 1159 |
faccess = ACCMODE_RDWRT ; |
| 1160 |
else if (itmp==(FLAG_READ)) |
| 1161 |
faccess = ACCMODE_READ ; |
| 1162 |
else if (itmp==(FLAG_WRITE)) |
| 1163 |
faccess = ACCMODE_WRITE ; |
| 1164 |
else |
| 1165 |
faccess = ACCMODE_NONE ; |
| 1166 |
|
| 1167 |
if (faccess == ACCMODE_NONE) |
| 1168 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ; |
| 1169 |
|
| 1170 |
tryagain: |
| 1171 |
#ifdef SGIkludges |
| 1172 |
errno = EMFILE ; |
| 1173 |
#else |
| 1174 |
errno = 0 ; |
| 1175 |
#endif |
| 1176 |
ptr->fileptr = fopen( ptr->filename0->value, acc_mode[faccess] ) ; |
| 1177 |
if ((!ptr->fileptr) && (errno == EMFILE)) |
| 1178 |
{ |
| 1179 |
swapout_file( TSD, ptr ) ; |
| 1180 |
goto tryagain ; |
| 1181 |
} |
| 1182 |
|
| 1183 |
ptr->flag &= ~(FLAG_SWAPPED) ; |
| 1184 |
if (ptr->fileptr==NULL) |
| 1185 |
file_error( ptr, errno, NULL ) ; |
| 1186 |
else |
| 1187 |
{ |
| 1188 |
if ( ptr->thispos == (size_t) EOF ) |
| 1189 |
rx_fseek( ptr->fileptr, 0, SEEK_SET ) ; |
| 1190 |
else |
| 1191 |
rx_fseek( ptr->fileptr, ptr->thispos, SEEK_SET ) ; |
| 1192 |
/* |
| 1193 |
* If the swapped-in file was already after EOF; ie |
| 1194 |
* ptr->flag contained FLAG_RDEOF, then force eof() |
| 1195 |
* to return true. |
| 1196 |
*/ |
| 1197 |
if ( ptr->flag & FLAG_RDEOF ) |
| 1198 |
{ |
| 1199 |
rx_fseek( ptr->fileptr, 0, SEEK_END ); |
| 1200 |
fgetc( ptr->fileptr ); |
| 1201 |
} |
| 1202 |
} |
| 1203 |
} |
| 1204 |
|
| 1205 |
/* |
| 1206 |
* Compares two filesnames, either with ignoring or respecting the letter case. |
| 1207 |
*/ |
| 1208 |
int filename_cmp( const streng *name1, const streng *name2 ) |
| 1209 |
{ |
| 1210 |
#ifdef CASE_SENSITIVE_FILENAMES |
| 1211 |
return Str_cmp( name1, name2 ); |
| 1212 |
#else |
| 1213 |
return Str_ccmp( name1, name2 ); |
| 1214 |
#endif |
| 1215 |
} |
| 1216 |
|
| 1217 |
static fileboxptr getfileptr( tsd_t *TSD, const streng *name ) |
| 1218 |
{ |
| 1219 |
int hashval ; |
| 1220 |
fileboxptr ptr ; |
| 1221 |
fil_tsd_t *ft; |
| 1222 |
|
| 1223 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 1224 |
|
| 1225 |
hashval = filehashvalue( name ) ; |
| 1226 |
/* |
| 1227 |
* First, try to find the correct file in this slot of the |
| 1228 |
* hash table. If one is found, ptr points to it, else, ptr is set |
| 1229 |
* to NULL |
| 1230 |
*/ |
| 1231 |
for (ptr=ft->filehash[hashval];ptr;ptr=ptr->next) |
| 1232 |
{ |
| 1233 |
if ( !filename_cmp( name, ptr->filename0 ) ) |
| 1234 |
break ; |
| 1235 |
} |
| 1236 |
/* |
| 1237 |
* In order not to create any problems later, just return with NULL |
| 1238 |
* (signifying that no file was found) if that is the case. Then we may |
| 1239 |
* able to assume that ptr _is_ set later. |
| 1240 |
*/ |
| 1241 |
if (!ptr) |
| 1242 |
return NULL ; |
| 1243 |
|
| 1244 |
/* |
| 1245 |
* Now, put the file in front of the list of files stored by how |
| 1246 |
* recently they were used. We assume that any access to a file is |
| 1247 |
* equivalent to the file being used. |
| 1248 |
*/ |
| 1249 |
if ( ptr != ft->mrufile ) |
| 1250 |
{ |
| 1251 |
if ( ptr->newer ) |
| 1252 |
{ |
| 1253 |
ptr->newer->older = ptr->older ; |
| 1254 |
} |
| 1255 |
if ( ptr->older ) |
| 1256 |
{ |
| 1257 |
ptr->older->newer = ptr->newer ; |
| 1258 |
} |
| 1259 |
|
| 1260 |
ptr->older = ft->mrufile ; |
| 1261 |
ptr->newer = NULL ; |
| 1262 |
ft->mrufile->newer = ptr ; |
| 1263 |
ft->mrufile = ptr ; |
| 1264 |
} |
| 1265 |
if ( ptr != ft->filehash[ hashval ] ) |
| 1266 |
{ |
| 1267 |
/* Why the hell do we use ft->mrufile? Is it useful for anything? |
| 1268 |
* The following code speeds up getfileptr much more. |
| 1269 |
* If the current file pointer (ptr) is not the first file pointer |
| 1270 |
* in the list for this hash value; reposition it so that it is |
| 1271 |
* at the front of the list, and move the current file pointer |
| 1272 |
* that is at the front of the list after ptr. |
| 1273 |
* Fixes bug 594553 |
| 1274 |
*/ |
| 1275 |
if ( ptr->next ) |
| 1276 |
ptr->next->prev = ptr->prev ; |
| 1277 |
if ( ptr->prev ) |
| 1278 |
ptr->prev->next = ptr->next ; |
| 1279 |
|
| 1280 |
ptr->prev = NULL; |
| 1281 |
ptr->next = ft->filehash[ hashval ]; |
| 1282 |
ft->filehash[ hashval ]->prev = ptr; |
| 1283 |
ft->filehash[ hashval ] = ptr ; |
| 1284 |
} |
| 1285 |
|
| 1286 |
/* |
| 1287 |
* If this file has been swapped out, we have to reopen it, so we can |
| 1288 |
* continue to access it. |
| 1289 |
*/ |
| 1290 |
if (ptr->flag & FLAG_SWAPPED) |
| 1291 |
swapin_file( TSD, ptr ) ; |
| 1292 |
|
| 1293 |
return ptr ; |
| 1294 |
} |
| 1295 |
|
| 1296 |
|
| 1297 |
static void flush_input( cfileboxptr dummy ) |
| 1298 |
{ |
| 1299 |
dummy = dummy; /* keep compiler happy */ |
| 1300 |
return ; |
| 1301 |
} |
| 1302 |
|
| 1303 |
|
| 1304 |
/* |
| 1305 |
* flush_output does close the file. The purpose of this function is to free |
| 1306 |
* up space for opening another file while maintaining all state information about |
| 1307 |
* this file if/when it is needed again. |
| 1308 |
* Returns -1 in case of an error, 0 on success. |
| 1309 |
*/ |
| 1310 |
static int flush_output( tsd_t *TSD, fileboxptr ptr ) |
| 1311 |
{ |
| 1312 |
int h; |
| 1313 |
|
| 1314 |
errno = 0; |
| 1315 |
|
| 1316 |
if ( ptr->fileptr == NULL || ptr->flag & FLAG_SWAPPED ) |
| 1317 |
return 0; |
| 1318 |
|
| 1319 |
if ( ptr->flag & FLAG_SURVIVOR ) |
| 1320 |
{ |
| 1321 |
if ( ptr->flag & FLAG_WRITE ) |
| 1322 |
{ |
| 1323 |
if ( fflush( ptr->fileptr ) != 0 ) |
| 1324 |
{ |
| 1325 |
file_error( ptr, errno, NULL ); |
| 1326 |
return -1; |
| 1327 |
} |
| 1328 |
} |
| 1329 |
return 0; |
| 1330 |
} |
| 1331 |
|
| 1332 |
if ( fflush( ptr->fileptr ) != 0 ) |
| 1333 |
{ |
| 1334 |
h = errno; |
| 1335 |
fclose( ptr->fileptr ); |
| 1336 |
ptr->fileptr = NULL; |
| 1337 |
ptr->flag |= FLAG_SWAPPED; |
| 1338 |
file_error( ptr, h, NULL ); |
| 1339 |
return -1; |
| 1340 |
} |
| 1341 |
if ( fclose( ptr->fileptr ) == EOF ) |
| 1342 |
{ |
| 1343 |
h = errno; |
| 1344 |
ptr->fileptr = NULL; |
| 1345 |
ptr->flag |= FLAG_SWAPPED; |
| 1346 |
file_error( ptr, h, NULL ); |
| 1347 |
return -1; |
| 1348 |
} |
| 1349 |
|
| 1350 |
ptr->fileptr = NULL; |
| 1351 |
ptr->flag |= FLAG_SWAPPED; |
| 1352 |
return 0; |
| 1353 |
} |
| 1354 |
|
| 1355 |
/* |
| 1356 |
* Sets up the internal filetable for REXX, and initializes it with |
| 1357 |
* the three standard files under Unix, stderr, stdout og and stdin. |
| 1358 |
* Should only be called once, from the main routine. We should also |
| 1359 |
* add code to register the routine for marking memory from this |
| 1360 |
* routine. |
| 1361 |
* |
| 1362 |
* As a shortcut to access these three default files, there is a |
| 1363 |
* variable 'stdio_ptr' which contains pointers to them. This allows |
| 1364 |
* for quick access to the default streams. |
| 1365 |
* The function returns 1 on success, 0 if memory is short. |
| 1366 |
* |
| 1367 |
* IMPORTANT! |
| 1368 |
* The entry for stdin must be the same as the following #define for |
| 1369 |
* DEFAULT_STDIN_INDEX below. Never changes it. |
| 1370 |
* This assumption is used in readkbdline(). |
| 1371 |
*/ |
| 1372 |
#define DEFAULT_STDIN_INDEX 0 |
| 1373 |
#define DEFAULT_STDOUT_INDEX 1 |
| 1374 |
#define DEFAULT_STDERR_INDEX 2 |
| 1375 |
int init_filetable( tsd_t *TSD ) |
| 1376 |
{ |
| 1377 |
int i=0 ; |
| 1378 |
fil_tsd_t *ft; |
| 1379 |
|
| 1380 |
if (TSD->fil_tsd != NULL) |
| 1381 |
return(1); |
| 1382 |
|
| 1383 |
if ( ( TSD->fil_tsd = MallocTSD( sizeof(fil_tsd_t) ) ) == NULL ) |
| 1384 |
return(0); |
| 1385 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 1386 |
memset( ft, 0, sizeof(fil_tsd_t) ); |
| 1387 |
|
| 1388 |
for ( i = 0; i < 6; i++ ) |
| 1389 |
{ |
| 1390 |
ft->stdio_ptr[i] = (fileboxptr)MallocTSD( sizeof( filebox )) ; |
| 1391 |
ft->stdio_ptr[i]->errmsg = NULL ; |
| 1392 |
ft->stdio_ptr[i]->error = 0 ; |
| 1393 |
} |
| 1394 |
|
| 1395 |
ft->stdio_ptr[0]->fileptr = ft->stdio_ptr[3]->fileptr = stdin ; |
| 1396 |
ft->stdio_ptr[1]->fileptr = ft->stdio_ptr[4]->fileptr = stdout ; |
| 1397 |
ft->stdio_ptr[2]->fileptr = ft->stdio_ptr[5]->fileptr = stderr ; |
| 1398 |
|
| 1399 |
ft->stdio_ptr[0]->flag = ft->stdio_ptr[3]->flag = ( FLAG_SURVIVOR + FLAG_READ ) ; |
| 1400 |
ft->stdio_ptr[1]->flag = ft->stdio_ptr[4]->flag = ( FLAG_SURVIVOR + FLAG_WRITE ) ; |
| 1401 |
ft->stdio_ptr[2]->flag = ft->stdio_ptr[5]->flag = ( FLAG_SURVIVOR + FLAG_WRITE ) ; |
| 1402 |
|
| 1403 |
ft->stdio_ptr[0]->filename0 = Str_crestrTSD( "<stdin>" ) ; |
| 1404 |
ft->stdio_ptr[1]->filename0 = Str_crestrTSD( "<stdout>" ) ; |
| 1405 |
ft->stdio_ptr[2]->filename0 = Str_crestrTSD( "<stderr>" ) ; |
| 1406 |
ft->stdio_ptr[3]->filename0 = Str_crestrTSD( "stdin" ) ; |
| 1407 |
ft->stdio_ptr[4]->filename0 = Str_crestrTSD( "stdout" ) ; |
| 1408 |
ft->stdio_ptr[5]->filename0 = Str_crestrTSD( "stderr" ) ; |
| 1409 |
|
| 1410 |
for (i=0; i<6; i++) |
| 1411 |
enterfileptr( TSD, ft->stdio_ptr[i] ) ; |
| 1412 |
|
| 1413 |
return(1); |
| 1414 |
} |
| 1415 |
|
| 1416 |
void purge_filetable( tsd_t *TSD ) |
| 1417 |
{ |
| 1418 |
fileboxptr ptr1, ptr2, save_ptr1, save_ptr2 ; |
| 1419 |
int i; |
| 1420 |
fil_tsd_t *ft; |
| 1421 |
|
| 1422 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 1423 |
/* Naming this the "removal loop". */ |
| 1424 |
for ( ptr1=ft->mrufile; ptr1; ) |
| 1425 |
{ |
| 1426 |
save_ptr1 = ptr1->older ; |
| 1427 |
for ( ptr2=ptr1; ptr2; ) |
| 1428 |
{ |
| 1429 |
save_ptr2 = ptr2->next ; /* this was moved from third parm of loop |
| 1430 |
so that it did not address the free'd |
| 1431 |
memory. See if statement below. */ |
| 1432 |
/* |
| 1433 |
* If this is one of the default streams, don't let it be closed. |
| 1434 |
* These file shall stay open, whatever happens. |
| 1435 |
*/ |
| 1436 |
/* |
| 1437 |
* JH 19991105 if was modified to include the next 5 statements. Originally, |
| 1438 |
* the file was not closed, but all other references to it were deleted. In |
| 1439 |
* situations where one *.exe invokes Rexx mutiple times, subsequent calls to |
| 1440 |
* the standard streams caused an error. (getfileptr() failed, the file name |
| 1441 |
* for stdio_ptr[?] comes up blank.) |
| 1442 |
*/ |
| 1443 |
if (!(ptr2->flag & FLAG_SURVIVOR) |
| 1444 |
&& ptr2->fileptr) |
| 1445 |
{ |
| 1446 |
fclose( ptr2->fileptr ) ; |
| 1447 |
|
| 1448 |
removefileptr( TSD, ptr2 ) ; |
| 1449 |
|
| 1450 |
if (ptr2->errmsg) |
| 1451 |
Free_stringTSD( ptr2->errmsg ) ; |
| 1452 |
|
| 1453 |
Free_stringTSD( ptr2->filename0 ) ; |
| 1454 |
FreeTSD( ptr2 ) ; |
| 1455 |
} |
| 1456 |
ptr2 = save_ptr2 ; |
| 1457 |
} |
| 1458 |
ptr1 = save_ptr1 ; |
| 1459 |
} |
| 1460 |
|
| 1461 |
ft->mrufile = NULL; |
| 1462 |
|
| 1463 |
/* |
| 1464 |
* Now lets be absolutely paranoid, and remove all entries from the |
| 1465 |
* filehash table... |
| 1466 |
*/ |
| 1467 |
memset( ft->filehash, 0, sizeof(ft->filehash) ); |
| 1468 |
/* |
| 1469 |
* JH 19991105 The following loop was added to re-instate the std streams into the |
| 1470 |
* hash table. It seems easier to do this then to muck around with reseting the pointers |
| 1471 |
* as the fileboxptr's are deleted. Cannot modify the loop above to look at filenames |
| 1472 |
* before removing from filehas table, it might be pointing to a fileboxptr that got removed |
| 1473 |
* by the "removal loop". |
| 1474 |
*/ |
| 1475 |
for (i=0; i<6; i++) |
| 1476 |
{ |
| 1477 |
enterfileptr( TSD, ft->stdio_ptr[i] ) ; |
| 1478 |
} |
| 1479 |
|
| 1480 |
#if 0 |
| 1481 |
// can't free this as the next call to RexxStart() expects this memory to be here |
| 1482 |
if ( TSD->fil_tsd ) |
| 1483 |
{ |
| 1484 |
FreeTSD( TSD->fil_tsd ); |
| 1485 |
TSD->fil_tsd = NULL; |
| 1486 |
} |
| 1487 |
#endif |
| 1488 |
} |
| 1489 |
|
| 1490 |
/* |
| 1491 |
* checkProperStreamName raises 40.27 if errno describes an error leading |
| 1492 |
* to the assumption that the filename was malformed according to ANSI 9.2.1. |
| 1493 |
*/ |
| 1494 |
static void checkProperStreamName( tsd_t *TSD, streng *kill, const char *fn, |
| 1495 |
int eno ) |
| 1496 |
{ |
| 1497 |
static const int bad[] = { |
| 1498 |
#if defined(ENAMETOOLONG) |
| 1499 |
ENAMETOOLONG, |
| 1500 |
#endif |
| 1501 |
0 |
| 1502 |
}; |
| 1503 |
|
| 1504 |
int i; |
| 1505 |
|
| 1506 |
for ( i = 0; bad[i] != 0; i++ ) |
| 1507 |
{ |
| 1508 |
if ( eno == bad[i] ) |
| 1509 |
{ |
| 1510 |
/* |
| 1511 |
* ANSI 9.2.1 wants us to raise 40.27 if stream is malformed. |
| 1512 |
* Feel free to provide more errno values describing this situation. |
| 1513 |
*/ |
| 1514 |
if ( kill ) |
| 1515 |
Free_stringTSD( kill ) ; |
| 1516 |
exiterror( ERR_INCORRECT_CALL, 27, BIFname( TSD ), fn ); |
| 1517 |
} |
| 1518 |
} |
| 1519 |
} |
| 1520 |
|
| 1521 |
/* |
| 1522 |
* Sets the proper error conditions for the file, including providing a |
| 1523 |
* a hook into the CALL/SIGNAL ON system. Now, we also like to set some |
| 1524 |
* other information, like the status of the file (taken from rc). |
| 1525 |
* |
| 1526 |
* First parameter is the file to operate on, the second and third |
| 1527 |
* parameters are the error message to set (they can't both be defined), |
| 1528 |
* and the last parameter is the level of 'severity'. If set, the file |
| 1529 |
* is thrown into error state. |
| 1530 |
*/ |
| 1531 |
static void handle_file_error( tsd_t *TSD, fileboxptr ptr, int rc, const char *errmsg, int level) |
| 1532 |
{ |
| 1533 |
trap *traps=NULL ; |
| 1534 |
|
| 1535 |
assert( !(rc && errmsg) ) ; |
| 1536 |
|
| 1537 |
if ((ptr->flag & FLAG_ERROR) && (ptr->flag & FLAG_FAKE)) |
| 1538 |
{ |
| 1539 |
/* |
| 1540 |
* If we are faking for this file already, don't bother to do anything |
| 1541 |
* more. In particular, we do not want to set a new error, since that |
| 1542 |
* will in general only overwrite the old (and probably more relevant) |
| 1543 |
* error message. However, faking are _only_ done when NOTREADY is |
| 1544 |
* being trapped. |
| 1545 |
*/ |
| 1546 |
return ; |
| 1547 |
} |
| 1548 |
else |
| 1549 |
{ |
| 1550 |
/* |
| 1551 |
* If the file is not already in error, set the ERROR flag, and record |
| 1552 |
* the error message. Also, clear the FAKE flag. This flag is only |
| 1553 |
* defined when the ERROR flag is set, and we don't want any old |
| 1554 |
* values laying around (it will be set later if needed). |
| 1555 |
*/ |
| 1556 |
if (level) |
| 1557 |
{ |
| 1558 |
ptr->flag &= ~FLAG_FAKE ; |
| 1559 |
ptr->flag |= FLAG_ERROR ; |
| 1560 |
} |
| 1561 |
else if (ptr->flag & FLAG_RDEOF) |
| 1562 |
{ |
| 1563 |
/* |
| 1564 |
* If the file was in RDEOF state; ie EOF was read on the file |
| 1565 |
* set the AFTER_RDEOF flag to ensure STREAM(stream,'S') works |
| 1566 |
* like other interpreters. |
| 1567 |
*/ |
| 1568 |
ptr->flag |= FLAG_AFTER_RDEOF; |
| 1569 |
} |
| 1570 |
|
| 1571 |
checkProperStreamName( TSD, ptr->errmsg, Str_val( ptr->filename0 ), rc ); |
| 1572 |
|
| 1573 |
/* |
| 1574 |
* Set the error message, but only if one was given. This routine |
| 1575 |
* can be called _without_ any errormessage, and if so, keep the |
| 1576 |
* old one (if any) |
| 1577 |
*/ |
| 1578 |
if (rc || errmsg) |
| 1579 |
{ |
| 1580 |
if (ptr->errmsg) |
| 1581 |
Free_stringTSD( ptr->errmsg ) ; |
| 1582 |
|
| 1583 |
ptr->error = rc ; |
| 1584 |
if ( errmsg ) |
| 1585 |
ptr->errmsg = Str_creTSD( errmsg ) ; |
| 1586 |
else |
| 1587 |
#ifdef WIN32 |
| 1588 |
{ |
| 1589 |
/* |
| 1590 |
* For Win32 always get the last error, and store that; |
| 1591 |
* it seems to be more meaningful than strerror( errno ) |
| 1592 |
* Address bug ???? FIXME |
| 1593 |
*/ |
| 1594 |
CHAR LastError[256]; |
| 1595 |
ULONG last_error = GetLastError(); |
| 1596 |
if ( last_error ) |
| 1597 |
{ |
| 1598 |
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT), LastError, 256, NULL ) ; |
| 1599 |
ptr->errmsg = Str_creTSD( LastError ) ; |
| 1600 |
} |
| 1601 |
else |
| 1602 |
ptr->errmsg = NULL; |
| 1603 |
} |
| 1604 |
#else |
| 1605 |
ptr->errmsg = NULL; |
| 1606 |
#endif |
| 1607 |
} |
| 1608 |
|
| 1609 |
/* |
| 1610 |
* OK, the file has been put into ERROR state, now we must check |
| 1611 |
* to see if we should raise the NOTREADY condition. If NOTREADY |
| 1612 |
* is not currently enabled, don't bother to try to raise it. |
| 1613 |
*/ |
| 1614 |
traps = gettraps( TSD, TSD->currlevel ) ; |
| 1615 |
if (traps[SIGNAL_NOTREADY].on_off) |
| 1616 |
{ |
| 1617 |
/* |
| 1618 |
* The NOTREADY condition is being trapped; set the FAKE flag |
| 1619 |
* so that we don't create more errors for this file. But _only_ |
| 1620 |
* set the FAKE flag if NOTREADY is trapped by method CALL. |
| 1621 |
* Then raise the condition ... |
| 1622 |
*/ |
| 1623 |
if (!traps[SIGNAL_NOTREADY].invoked) |
| 1624 |
ptr->flag |= FLAG_FAKE ; |
| 1625 |
|
| 1626 |
condition_hook(TSD,SIGNAL_NOTREADY,rc+100,0,-1,Str_dupTSD(ptr->filename0),NULL); |
| 1627 |
} |
| 1628 |
} |
| 1629 |
} |
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
/* |
| 1634 |
* This routine is supposed to be called when the condition is triggered |
| 1635 |
* by method CALL. From the time the condition is raised until the CALL is |
| 1636 |
* is triggered, I/O to the file is faked. But before the condition handler |
| 1637 |
* is called, we try to tidy things up a bit. |
| 1638 |
* |
| 1639 |
* At least, we have to clear the FAKE flag. Other 'nice' things to do |
| 1640 |
* is to clear error indicator in the file pointer, and to reset the |
| 1641 |
* file in general. The ERROR state is not cleared, _unless_ the file |
| 1642 |
* is one of the default streams. |
| 1643 |
*/ |
| 1644 |
|
| 1645 |
void fixup_file( tsd_t *TSD, const streng *filename ) |
| 1646 |
{ |
| 1647 |
fileboxptr ptr=NULL ; |
| 1648 |
|
| 1649 |
if ( filename ) |
| 1650 |
{ |
| 1651 |
/* |
| 1652 |
* filename will be NULL when condition_hook() called with a NULL description |
| 1653 |
* argument. This happens when NOTREADY occurs when pulling from an external |
| 1654 |
* queue with a timeout and the timeout expires. |
| 1655 |
*/ |
| 1656 |
ptr = getfileptr( TSD, filename ) ; |
| 1657 |
if (ptr) |
| 1658 |
{ |
| 1659 |
/* |
| 1660 |
* If the file is open, try to clear it, first clear the error |
| 1661 |
* indicator, and then try to fseek() to a 'safe' point. If the |
| 1662 |
* seeking didn't work out, don't bother, it was worth a try. |
| 1663 |
*/ |
| 1664 |
if (ptr->fileptr) |
| 1665 |
{ |
| 1666 |
clearerr( ptr->fileptr ) ; |
| 1667 |
if ( ptr->flag & FLAG_PERSIST ) |
| 1668 |
rx_fseek( ptr->fileptr, 0, SEEK_SET ) ; |
| 1669 |
ptr->thispos = 0 ; |
| 1670 |
ptr->oper = OPER_NONE ; |
| 1671 |
} |
| 1672 |
|
| 1673 |
if (ptr->flag & FLAG_SURVIVOR) |
| 1674 |
{ |
| 1675 |
ptr->flag &= ~(FLAG_ERROR) ; |
| 1676 |
/* |
| 1677 |
* MHES Added following 4 flag resets - 30-11-2004 |
| 1678 |
*/ |
| 1679 |
ptr->flag &= ~(FLAG_RDEOF) ; |
| 1680 |
ptr->flag &= ~(FLAG_WREOF) ; |
| 1681 |
ptr->flag &= ~(FLAG_AFTER_RDEOF) ; |
| 1682 |
} |
| 1683 |
|
| 1684 |
ptr->flag &= ~(FLAG_FAKE) ; |
| 1685 |
} |
| 1686 |
} |
| 1687 |
} |
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
/* |
| 1693 |
* This is stupid ... if the file exists, but is in error mode, we |
| 1694 |
* shall not close it, but leave it open, so that the rest of the |
| 1695 |
* operations on this file in this statement don't trip. Same happens |
| 1696 |
* if we are not able to close it properly. Oh well ... |
| 1697 |
* |
| 1698 |
* On second thoughts ... Faking only applies for input and output. |
| 1699 |
* So closing doesn't have to be faked. Remove the file, whatever |
| 1700 |
* happens. |
| 1701 |
*/ |
| 1702 |
void closefile( tsd_t *TSD, const streng *name ) |
| 1703 |
{ |
| 1704 |
fileboxptr ptr=NULL ; |
| 1705 |
|
| 1706 |
/* If it isn't open, don't try to close it ... */ |
| 1707 |
ptr = getfileptr( TSD, name ) ; |
| 1708 |
if (ptr) |
| 1709 |
{ |
| 1710 |
/* |
| 1711 |
* If this is one of the default streams, don't let it be closed. |
| 1712 |
* These file shall stay open, whatever happens. |
| 1713 |
*/ |
| 1714 |
if (ptr->flag & FLAG_SURVIVOR) |
| 1715 |
return ; |
| 1716 |
|
| 1717 |
/* |
| 1718 |
* If the fileptr seems to point to something ... close it. We |
| 1719 |
* really don't want to leak file table slots. Actually, we should |
| 1720 |
* check that the close was ok, and not let the fileptr go unless |
| 1721 |
* we know that it was really closed (and released for new use). |
| 1722 |
* Previously, it only closed when file was not in error. I don't |
| 1723 |
* know what is the correct action, but this seems to be the most |
| 1724 |
* sensible ... |
| 1725 |
*/ |
| 1726 |
if (ptr->fileptr) |
| 1727 |
fclose( ptr->fileptr ) ; |
| 1728 |
|
| 1729 |
removefileptr( TSD, ptr ) ; |
| 1730 |
|
| 1731 |
if (ptr->errmsg) |
| 1732 |
Free_stringTSD( ptr->errmsg ) ; |
| 1733 |
|
| 1734 |
Free_stringTSD( ptr->filename0 ) ; |
| 1735 |
FreeTSD( ptr ) ; |
| 1736 |
} |
| 1737 |
} |
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
/* |
| 1743 |
* This function is called when we need some kind of access to a file |
| 1744 |
* but don't (yet) have it. It will only be called when we want to |
| 1745 |
* open a file implicitly, e.g. it is open for reading, and it has then |
| 1746 |
* been named in a output function. |
| 1747 |
* |
| 1748 |
* This is rather primitive ... but this function can only be called |
| 1749 |
* when the file is open for read, and we want to open it for write; |
| 1750 |
* or if the file i open for write, and we want to open it for read. |
| 1751 |
* So I think this will suffice. It ignores the 'access' parameter |
| 1752 |
* And just assumes that the file must be opened in both read and |
| 1753 |
* write. |
| 1754 |
* |
| 1755 |
* To improve on this function, we ought to do a lot more checking, |
| 1756 |
* e.g. that the 'access' wanted are required, and that the file is |
| 1757 |
* already open in some kind of mode. If this don't hold, we probably |
| 1758 |
* have an error condition. |
| 1759 |
* |
| 1760 |
* We should also check another thing, that the new file which is opened |
| 1761 |
* is in fact the same file that we closed. Perferably, we should open |
| 1762 |
* the new file, then check the device and inode of both the old and |
| 1763 |
* new file to see whether they are the same (using stat()). If they |
| 1764 |
* are not the same, the reopening should fail. As it is implemented |
| 1765 |
* now, the Unix method for temporary files (open it, remove it, |
| 1766 |
* use it, and then close it) will fail; and we loose access to the |
| 1767 |
* original file too. |
| 1768 |
*/ |
| 1769 |
static void reopen_file( tsd_t *TSD, fileboxptr ptr ) |
| 1770 |
{ |
| 1771 |
if (!ptr) |
| 1772 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ; |
| 1773 |
|
| 1774 |
/* |
| 1775 |
* We can not reopen the default streams, that makes no sence. If |
| 1776 |
* tried, report an error. |
| 1777 |
*/ |
| 1778 |
if (ptr->flag & FLAG_SURVIVOR) |
| 1779 |
{ |
| 1780 |
file_error( ptr, 0, "Invalid operation on default stream" ) ; |
| 1781 |
return ; |
| 1782 |
} |
| 1783 |
|
| 1784 |
/* |
| 1785 |
* Close the old file, and try to reopen the new file. There is the |
| 1786 |
* same problem here as in closefile(); if closing didn't work (for |
| 1787 |
* some mysterious reason), the system's file table should become |
| 1788 |
* full. Better checking might be required. |
| 1789 |
*/ |
| 1790 |
errno = 0 ; |
| 1791 |
fclose( ptr->fileptr ) ; |
| 1792 |
#ifdef VMS |
| 1793 |
ptr->fileptr = fopen( ptr->filename0->value, "r+" ) ; |
| 1794 |
#else |
| 1795 |
ptr->fileptr = fopen( ptr->filename0->value, "r+b" ) ; |
| 1796 |
#endif |
| 1797 |
if (ptr->fileptr==NULL) |
| 1798 |
{ |
| 1799 |
file_error( ptr, errno, NULL ) ; |
| 1800 |
return ; |
| 1801 |
} |
| 1802 |
ptr->oper = OPER_NONE ; |
| 1803 |
|
| 1804 |
/* |
| 1805 |
* We definitively want to set the close-on-exec flag. Suppose |
| 1806 |
* an output file has not been flushed, and we execute a command. |
| 1807 |
* This might perform an exec() and then a system(), which _will_ |
| 1808 |
* flush all files (close them). The result is that the file might |
| 1809 |
* be flushed twice ... not good. |
| 1810 |
* |
| 1811 |
* This don't work on VMS ... but the file system on VMS is so |
| 1812 |
* different anyway, so it will probably not create any problems. |
| 1813 |
* Besides, we don't do exec() and system() on VMS. |
| 1814 |
*/ |
| 1815 |
#if !defined(VMS) && !defined(MAC) && !defined(OS2) && !defined(DOS) && !defined(__WATCOMC__) && !defined(_MSC_VER) && !(defined(WIN32) && defined(__IBMC__)) && !defined(__MINGW32__) && !defined(__BORLANDC__) && !defined(__EPOC32__) && !defined(__LCC__) && !defined(SKYOS) |
| 1816 |
if (ptr && ptr->fileptr) |
| 1817 |
{ |
| 1818 |
int flags, fno ; |
| 1819 |
fno = fileno( ptr->fileptr ) ; |
| 1820 |
assert( fno >= -1) ; |
| 1821 |
flags = fcntl( fno, F_GETFD ) ; |
| 1822 |
flags |= FD_CLOEXEC ; |
| 1823 |
if (fcntl( fno, F_SETFD, flags)== -1) |
| 1824 |
exiterror( ERR_SYSTEM_FAILURE, 1, strerror(errno) ) ; |
| 1825 |
} |
| 1826 |
#endif |
| 1827 |
|
| 1828 |
/* |
| 1829 |
* If readposition is EOF (=illegal), then we "probably" needed to |
| 1830 |
* open it in read mode. Set the current read position to the start |
| 1831 |
* of the file. |
| 1832 |
*/ |
| 1833 |
if (ptr->readpos == (size_t) EOF) |
| 1834 |
{ |
| 1835 |
ptr->readline = 1 ; |
| 1836 |
ptr->linesleft = 0 ; |
| 1837 |
ptr->readpos = 0 ; |
| 1838 |
ptr->thispos = 0 ; |
| 1839 |
if ( ptr->flag & FLAG_PERSIST ) |
| 1840 |
rx_fseek( ptr->fileptr, 0, SEEK_SET ) ; |
| 1841 |
} |
| 1842 |
|
| 1843 |
/* |
| 1844 |
* Then do the same thing for write access. We always set this to the |
| 1845 |
* end-of-file -- the default -- even though there are other write |
| 1846 |
* modes available. If the file is implicitly open in write mode, |
| 1847 |
* then the current write position should be set to the default |
| 1848 |
* value. |
| 1849 |
*/ |
| 1850 |
if (ptr->writepos == (size_t) EOF) |
| 1851 |
{ |
| 1852 |
ptr->writeline = 0 ; |
| 1853 |
if ( ptr->flag & FLAG_PERSIST ) |
| 1854 |
rx_fseek( ptr->fileptr, 0, SEEK_END ) ; |
| 1855 |
ptr->writepos = rx_ftell( ptr->fileptr ) ; |
| 1856 |
ptr->thispos = ptr->writepos ; |
| 1857 |
} |
| 1858 |
|
| 1859 |
/* |
| 1860 |
* Then, at last, do some simple bookkeeping, set both read and |
| 1861 |
* write access, and clear any previous problem. |
| 1862 |
*/ |
| 1863 |
ptr->flag = FLAG_READ | FLAG_WRITE | FLAG_PERSIST ; |
| 1864 |
ptr->error = 0 ; |
| 1865 |
if (ptr->errmsg) |
| 1866 |
Free_stringTSD(ptr->errmsg) ; |
| 1867 |
|
| 1868 |
ptr->errmsg = NULL ; |
| 1869 |
} |
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
/* |
| 1874 |
* This function explicitly opens a file. It will be called if the user |
| 1875 |
* has called the built-in function STREAM() in order to open a file |
| 1876 |
* in a particular mode. It will also be called if the file is not |
| 1877 |
* previously open, and is used in a read or write operation. |
| 1878 |
* |
| 1879 |
* It takes two parameters, the name of the file to open, and the |
| 1880 |
* mode in which it is to be opened. The mode has a value which is |
| 1881 |
* matched by the ACCESS_ macros defined earlier. |
| 1882 |
* |
| 1883 |
* If the file is actually open in advance, then we close it before we |
| 1884 |
* do any other operations. If the user is interested in the file in |
| 1885 |
* one particular mode, he is probably not interested in any previous |
| 1886 |
* modes. |
| 1887 |
*/ |
| 1888 |
static fileboxptr openfile( tsd_t *TSD, const streng *name, int faccess ) |
| 1889 |
{ |
| 1890 |
fileboxptr ptr=NULL ; |
| 1891 |
long lpos=0L ; |
| 1892 |
|
| 1893 |
/* |
| 1894 |
* First check wether this file is already open, and use that open |
| 1895 |
* file if possible. However, that may not be possible, since we |
| 1896 |
* may want to use the file for another operation now. So, if the |
| 1897 |
* file _is_ open, check to see if access is right. |
| 1898 |
*/ |
| 1899 |
ptr = getfileptr( TSD, name ) ; |
| 1900 |
if (ptr) |
| 1901 |
{ |
| 1902 |
if (ptr->flag & FLAG_SURVIVOR) |
| 1903 |
{ |
| 1904 |
file_error( ptr, 0, "Can't open a default stream" ) ; |
| 1905 |
return ptr ; |
| 1906 |
} |
| 1907 |
closefile( TSD, name ) ; |
| 1908 |
} |
| 1909 |
|
| 1910 |
/* |
| 1911 |
* Now, get a new file table entry, and fill in the various |
| 1912 |
* field with appropriate (i.e. default) values. |
| 1913 |
*/ |
| 1914 |
ptr = (fileboxptr)MallocTSD( sizeof(filebox) ) ; |
| 1915 |
ptr->filename0 = Str_dupstrTSD( name ) ; |
| 1916 |
ptr->flag = 0 ; |
| 1917 |
ptr->error = 0 ; |
| 1918 |
ptr->errmsg = NULL ; |
| 1919 |
ptr->readline = 0 ; |
| 1920 |
ptr->linesleft = 0 ; |
| 1921 |
ptr->writeline = 0 ; |
| 1922 |
ptr->thispos = (size_t) EOF ; |
| 1923 |
ptr->readpos = (size_t) EOF ; |
| 1924 |
ptr->writepos = (size_t) EOF ; |
| 1925 |
ptr->oper = OPER_NONE; |
| 1926 |
|
| 1927 |
/* |
| 1928 |
* suppose we tried to open, but didn't manage, well, stuff it into |
| 1929 |
* the file table, we might want to retrieve information about it |
| 1930 |
* later on. _And_ we need to know about the problem if the file |
| 1931 |
* I/O is to be faked later on. |
| 1932 |
*/ |
| 1933 |
enterfileptr( TSD, ptr ) ; |
| 1934 |
name = ptr->filename0 ; |
| 1935 |
goto try_to_open ; |
| 1936 |
|
| 1937 |
kill_one_file: |
| 1938 |
swapout_file( TSD, ptr ) ; |
| 1939 |
|
| 1940 |
try_to_open: |
| 1941 |
/* |
| 1942 |
* In most of these, we have to check that the file opened is really |
| 1943 |
* a persistent file. We should not take that for granted. |
| 1944 |
*/ |
| 1945 |
errno = 0 ; |
| 1946 |
if (faccess==ACCESS_READ) |
| 1947 |
{ |
| 1948 |
#ifdef VMS |
| 1949 |
if ((ptr->fileptr = fopen( name->value, "r" )) != NULL) |
| 1950 |
#else |
| 1951 |
if ((ptr->fileptr = fopen( name->value, "rb" )) != NULL) |
| 1952 |
#endif |
| 1953 |
{ |
| 1954 |
ptr->flag = FLAG_READ | FLAG_PERSIST ; |
| 1955 |
ptr->readline = 1 ; |
| 1956 |
ptr->linesleft = 0 ; |
| 1957 |
ptr->thispos = ptr->readpos = 0 ; |
| 1958 |
} |
| 1959 |
else if (errno==EMFILE) |
| 1960 |
goto kill_one_file ; |
| 1961 |
else |
| 1962 |
{ |
| 1963 |
file_error( ptr, errno, NULL ) ; |
| 1964 |
} |
| 1965 |
} |
| 1966 |
else if (faccess==ACCESS_WRITE) |
| 1967 |
{ |
| 1968 |
/* |
| 1969 |
* This is really a problem. If opened in mode "w", it will |
| 1970 |
* truncate the file if it did exist. If opened int mode "r+", |
| 1971 |
* it will fail if the file did not exist. So we try to |
| 1972 |
* combine the two. |
| 1973 |
*/ |
| 1974 |
ptr->flag = FLAG_READ ; |
| 1975 |
#ifdef VMS |
| 1976 |
ptr->fileptr = fopen( name->value, "r+" ) ; |
| 1977 |
#else |
| 1978 |
ptr->fileptr = fopen( name->value, "r+b" ) ; |
| 1979 |
#endif |
| 1980 |
errno = 0 ; |
| 1981 |
if (!ptr->fileptr) |
| 1982 |
#ifdef VMS |
| 1983 |
ptr->fileptr = fopen( name->value, "w+" ) ; |
| 1984 |
#else |
| 1985 |
ptr->fileptr = fopen( name->value, "w+b" ) ; |
| 1986 |
#endif |
| 1987 |
|
| 1988 |
errno = 0 ; |
| 1989 |
if (!ptr->fileptr) |
| 1990 |
{ |
| 1991 |
#ifdef SGIkludges |
| 1992 |
errno = EMFILE ; |
| 1993 |
#else |
| 1994 |
errno = 0 ; |
| 1995 |
#endif |
| 1996 |
#ifdef VMS |
| 1997 |
ptr->fileptr = fopen( name->value, "w" ) ; |
| 1998 |
#else |
| 1999 |
ptr->fileptr = fopen( name->value, "wb" ) ; |
| 2000 |
#endif |
| 2001 |
ptr->flag &= 0 ; |
| 2002 |
} |
| 2003 |
|
| 2004 |
/* |
| 2005 |
* Then set the current read and write positions to the start and |
| 2006 |
* the end of the file, respectively. When we first open the file |
| 2007 |
* we can quickly determine readpos, writepos and readline, but |
| 2008 |
* writeline is expensive to determine, because we have to read |
| 2009 |
* the whole file to determine this. So we don't do this when we |
| 2010 |
* open the file, because we may never use it. Instead we set the |
| 2011 |
* value of writeline to 0 to indicate that the actual position |
| 2012 |
* is unknown. When we do want to use writeline, we have to |
| 2013 |
* determine it then. |
| 2014 |
*/ |
| 2015 |
if (ptr->fileptr) |
| 2016 |
{ |
| 2017 |
ptr->flag |= FLAG_WRITE | FLAG_PERSIST ; |
| 2018 |
rx_fseek( ptr->fileptr, 0, SEEK_END ) ; |
| 2019 |
lpos = rx_ftell( ptr->fileptr ) ; |
| 2020 |
ptr->thispos = ptr->writepos = lpos ; |
| 2021 |
ptr->writeline = 0 ; |
| 2022 |
ptr->readpos = 0 ; |
| 2023 |
ptr->readline = 1 ; |
| 2024 |
ptr->linesleft = 0 ; |
| 2025 |
} |
| 2026 |
else if (errno==EMFILE) |
| 2027 |
goto kill_one_file ; |
| 2028 |
else |
| 2029 |
file_error( ptr, errno, NULL ) ; |
| 2030 |
} |
| 2031 |
else if (faccess==ACCESS_APPEND) |
| 2032 |
{ |
| 2033 |
/* |
| 2034 |
* In append mode, the file is opened as a transient file, all |
| 2035 |
* writing must be done at the end of the file. It is not |
| 2036 |
* possible to perform reading on the file. Useful for files |
| 2037 |
* to which you have write, but not read access (e.g. logfiles). |
| 2038 |
*/ |
| 2039 |
#ifdef VMS |
| 2040 |
if ((ptr->fileptr = fopen( name->value, "a" )) != NULL) |
| 2041 |
#else |
| 2042 |
if ((ptr->fileptr = fopen( name->value, "ab" )) != NULL) |
| 2043 |
#endif |
| 2044 |
{ |
| 2045 |
ptr->flag = FLAG_WRITE | FLAG_WREOF ; |
| 2046 |
} |
| 2047 |
else if (errno==EMFILE) |
| 2048 |
goto kill_one_file ; |
| 2049 |
else |
| 2050 |
file_error( ptr, errno, NULL ) ; |
| 2051 |
} |
| 2052 |
else if (faccess==ACCESS_STREAM_APPEND) |
| 2053 |
{ |
| 2054 |
/* |
| 2055 |
* In "stream" append mode, the file is opened as a persistent file, all |
| 2056 |
* writing must be done at the end of the file. It is not |
| 2057 |
* possible to perform reading on the file. Useful for files |
| 2058 |
* to which you have write, but not read access (e.g. logfiles). |
| 2059 |
*/ |
| 2060 |
#ifdef VMS |
| 2061 |
if ((ptr->fileptr = fopen( name->value, "a" )) != NULL) |
| 2062 |
#else |
| 2063 |
if ((ptr->fileptr = fopen( name->value, "ab" )) != NULL) |
| 2064 |
#endif |
| 2065 |
{ |
| 2066 |
ptr->flag = FLAG_WRITE | FLAG_WREOF | FLAG_PERSIST; |
| 2067 |
if ( ptr->flag & FLAG_PERSIST ) |
| 2068 |
rx_fseek( ptr->fileptr, 0, SEEK_END ) ; |
| 2069 |
lpos = rx_ftell( ptr->fileptr ) ; |
| 2070 |
ptr->thispos = ptr->writepos = lpos ; |
| 2071 |
ptr->writeline = 0 ; /* unknown position */ |
| 2072 |
ptr->readpos = 0 ; |
| 2073 |
ptr->readline = 1 ; |
| 2074 |
ptr->linesleft = 0 ; |
| 2075 |
} |
| 2076 |
else if (errno==EMFILE) |
| 2077 |
goto kill_one_file ; |
| 2078 |
else |
| 2079 |
file_error( ptr, errno, NULL ) ; |
| 2080 |
} |
| 2081 |
else if (faccess==ACCESS_STREAM_REPLACE) |
| 2082 |
{ |
| 2083 |
/* |
| 2084 |
* The file is created if it didn't exist, and if it did exist |
| 2085 |
* it is truncated and the file pointers set to the start of file. |
| 2086 |
*/ |
| 2087 |
#ifdef VMS |
| 2088 |
if ((ptr->fileptr = fopen( name->value, "w+" )) != NULL) |
| 2089 |
#else |
| 2090 |
if ((ptr->fileptr = fopen( name->value, "w+b" )) != NULL) |
| 2091 |
#endif |
| 2092 |
{ |
| 2093 |
ptr->flag = FLAG_WRITE | FLAG_READ | FLAG_WREOF | FLAG_RDEOF | |
| 2094 |
FLAG_PERSIST ; |
| 2095 |
ptr->writeline = ptr->readline = 1 ; |
| 2096 |
ptr->linesleft = 0 ; |
| 2097 |
ptr->readpos = ptr->writepos = ptr->thispos = 0 ; |
| 2098 |
} |
| 2099 |
else if (errno==EMFILE) |
| 2100 |
goto kill_one_file ; |
| 2101 |
else |
| 2102 |
file_error( ptr, errno, NULL ) ; |
| 2103 |
} |
| 2104 |
else if (faccess==ACCESS_UPDATE) |
| 2105 |
{ |
| 2106 |
/* |
| 2107 |
* Like read access, but it will not create the file if it didn't |
| 2108 |
* already exist. Instead, an error is reported. |
| 2109 |
*/ |
| 2110 |
#ifdef VMS |
| 2111 |
if ((ptr->fileptr = fopen( name->value, "r+" )) != NULL) |
| 2112 |
#else |
| 2113 |
if ((ptr->fileptr = fopen( name->value, "r+b" )) != NULL) |
| 2114 |
#endif |
| 2115 |
{ |
| 2116 |
ptr->flag = FLAG_WRITE | FLAG_READ | FLAG_PERSIST ; |
| 2117 |
ptr->readline = 0 ; |
| 2118 |
ptr->linesleft = 0 ; |
| 2119 |
ptr->writeline = 0 ; /* unknown */ |
| 2120 |
} |
| 2121 |
else if (errno==EMFILE) |
| 2122 |
goto kill_one_file ; |
| 2123 |
else |
| 2124 |
file_error( ptr, errno, NULL ) ; |
| 2125 |
} |
| 2126 |
else if (faccess==ACCESS_CREATE) |
| 2127 |
{ |
| 2128 |
/* |
| 2129 |
* The file is created if it didn't exist, and if it did exist |
| 2130 |
* it is truncated. |
| 2131 |
*/ |
| 2132 |
#ifdef VMS |
| 2133 |
if ((ptr->fileptr = fopen( name->value, "w+" )) != NULL) |
| 2134 |
#else |
| 2135 |
if ((ptr->fileptr = fopen( name->value, "w+b" )) != NULL) |
| 2136 |
#endif |
| 2137 |
{ |
| 2138 |
ptr->flag = FLAG_WRITE | FLAG_READ | FLAG_WREOF | FLAG_RDEOF | |
| 2139 |
FLAG_PERSIST ; |
| 2140 |
ptr->writeline = ptr->readline = 1 ; |
| 2141 |
ptr->linesleft = 0 ; |
| 2142 |
ptr->readpos = ptr->writepos = ptr->thispos = 0 ; |
| 2143 |
} |
| 2144 |
else if (errno==EMFILE) |
| 2145 |
goto kill_one_file ; |
| 2146 |
else |
| 2147 |
file_error( ptr, errno, NULL ) ; |
| 2148 |
} |
| 2149 |
else |
| 2150 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ; |
| 2151 |
|
| 2152 |
#if !defined(VMS) && !defined(MAC) && !defined(OS2) && !defined(DOS) && !defined(__WATCOMC__) && !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__BORLANDC__) && !defined(__EPOC32__) && !defined(__LCC__) && !defined(SKYOS) |
| 2153 |
/* |
| 2154 |
* Then we check to see if this is a transient or persistent file. |
| 2155 |
* We can remove a 'persistent' setting, but add one, since we |
| 2156 |
* sometimes want to access a persistent file as transient (append). |
| 2157 |
*/ |
| 2158 |
if (ptr->fileptr) |
| 2159 |
{ |
| 2160 |
int fno, rc ; |
| 2161 |
struct rx_stat_buf statbuf ; |
| 2162 |
errno = 0 ; |
| 2163 |
fno = fileno(ptr->fileptr) ; |
| 2164 |
rc = rx_fstat( fno, &statbuf ) ; |
| 2165 |
if (rc==0 && !S_ISREG(statbuf.st_mode)) |
| 2166 |
ptr->flag &= ~(FLAG_PERSIST) ; |
| 2167 |
else if (rc!=0) |
| 2168 |
file_error( ptr, errno, NULL ) ; |
| 2169 |
} |
| 2170 |
|
| 2171 |
/* |
| 2172 |
* As with reopen_file(), we want to set the close-on-exec flag, |
| 2173 |
* see reopen_file for more information. |
| 2174 |
*/ |
| 2175 |
if (ptr->fileptr) |
| 2176 |
{ |
| 2177 |
int flags, fno ; |
| 2178 |
fno = fileno(ptr->fileptr) ; |
| 2179 |
assert( fno >= -1) ; |
| 2180 |
flags = fcntl( fno, F_GETFD ) ; |
| 2181 |
flags |= FD_CLOEXEC ; |
| 2182 |
if (fcntl( fno, F_SETFD, flags)== -1) |
| 2183 |
exiterror( ERR_SYSTEM_FAILURE, 1, strerror(errno) ) ; |
| 2184 |
} |
| 2185 |
#endif |
| 2186 |
|
| 2187 |
return (ptr) ; |
| 2188 |
} |
| 2189 |
|
| 2190 |
|
| 2191 |
|
| 2192 |
|
| 2193 |
/* ------------------------------------------------------------------- */ |
| 2194 |
/* High level utility routines */ |
| 2195 |
|
| 2196 |
|
| 2197 |
|
| 2198 |
|
| 2199 |
/* |
| 2200 |
* This function is really just an interface to the function getfileptr(). |
| 2201 |
* It takes a (possible) filename, and retrieves the corresponding |
| 2202 |
* Rexx file table entry. If the file does not exist, it is opened in |
| 2203 |
* the mode indicated by 'open_mode'. If it does exist, this routine |
| 2204 |
* verifies that it it has been opened in a mode corresponding to |
| 2205 |
* 'access' (OPER_READ or OPER_WRITE). |
| 2206 |
* |
| 2207 |
* If the file does not exist, it is opened in either normal read |
| 2208 |
* or normal write. This correcspinds to an "implicit" file open |
| 2209 |
* in Rexx. |
| 2210 |
*/ |
| 2211 |
static fileboxptr get_file_ptr( tsd_t *TSD, const streng *name, int faccess, int open_mode ) |
| 2212 |
{ |
| 2213 |
fileboxptr ptr=NULL ; |
| 2214 |
|
| 2215 |
ptr = getfileptr( TSD, name ) ; |
| 2216 |
if (ptr==NULL) |
| 2217 |
return openfile( TSD, name, open_mode ) ; |
| 2218 |
|
| 2219 |
if (ptr->flag & FLAG_ERROR) |
| 2220 |
return ptr ; |
| 2221 |
|
| 2222 |
if (faccess==OPER_READ && (!(ptr->flag & FLAG_READ))) |
| 2223 |
reopen_file( TSD, ptr ) ; |
| 2224 |
else if (faccess==OPER_WRITE && (!(ptr->flag & FLAG_WRITE))) |
| 2225 |
reopen_file( TSD, ptr ) ; |
| 2226 |
|
| 2227 |
return ptr ; |
| 2228 |
} |
| 2229 |
|
| 2230 |
|
| 2231 |
|
| 2232 |
/* |
| 2233 |
* This routine reads one complete line from the file indicated by |
| 2234 |
* the file table entry 'ptr'. Or rather, it read from the current |
| 2235 |
* read position, until and including the first EOL mark, and returns |
| 2236 |
* that. If the EOL mark is implemented as certain characters, they are |
| 2237 |
* not returned. It closely corresponds to the LINEIN() built-in |
| 2238 |
* function. |
| 2239 |
* |
| 2240 |
* What is the upper limit for the size that we might read in? It's |
| 2241 |
* best not to have any limit, so the method is the following: A |
| 2242 |
* temporary area is used for storing the data read from the file. |
| 2243 |
* We never know the size needed until the EOL mark is found. So |
| 2244 |
* just read the data into the temporary area. If the EOL is found, |
| 2245 |
* then we know the size, and we can transfer the data into a 'streng' |
| 2246 |
* of suitable size. If the temporary area is too small, allocate |
| 2247 |
* an area twice the size, and copy the data over. Afterwards, keep the |
| 2248 |
* new area as the temporary area. |
| 2249 |
* |
| 2250 |
* This way, we normally use little memory, but we are still able to |
| 2251 |
* read as large lines as the memory allows, if it is needed. |
| 2252 |
* |
| 2253 |
* No error condition is raised if noerrors is set. Instead, NULL is returned. |
| 2254 |
*/ |
| 2255 |
static streng *readoneline( tsd_t *TSD, fileboxptr ptr ) |
| 2256 |
{ |
| 2257 |
int i=0, j=0, eolf=0, eolchars=1 ; |
| 2258 |
/*#if !defined(UNIX)*/ |
| 2259 |
int k=0; |
| 2260 |
/*#endif*/ |
| 2261 |
streng *ret=NULL ; |
| 2262 |
fil_tsd_t *ft; |
| 2263 |
|
| 2264 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 2265 |
|
| 2266 |
/* |
| 2267 |
* First verify that we actually have a file that is not in an |
| 2268 |
* ERROR state. If so, don't perform any operations. |
| 2269 |
*/ |
| 2270 |
if ( ptr->flag & FLAG_ERROR ) |
| 2271 |
{ |
| 2272 |
if (!(ptr->flag & FLAG_FAKE)) |
| 2273 |
file_error( ptr, 0, NULL ) ; |
| 2274 |
|
| 2275 |
return nullstringptr() ; |
| 2276 |
} |
| 2277 |
|
| 2278 |
/* |
| 2279 |
* If we have an EOF from the last linein() then cause the NOTREADY |
| 2280 |
* condition now. |
| 2281 |
*/ |
| 2282 |
if ( ptr->flag & FLAG_RDEOF ) |
| 2283 |
{ |
| 2284 |
file_warning( ptr, 0, "EOF on line input" ) ; |
| 2285 |
} |
| 2286 |
|
| 2287 |
/* |
| 2288 |
* If the string is not yet allocated, allocate it, and use an |
| 2289 |
* initial size of 512 bytes. This can be increased during runtime. |
| 2290 |
* Using higher initial sizes will waste allocation time on modern systems |
| 2291 |
* with page sizes around 4KB. 512 fits most cases at its best. |
| 2292 |
|
| 2293 |
* MH 4 Sep 02 We should read into the ft->rol_string 512 bytes at a |
| 2294 |
* time and search through the buffer for the EOL. getc() for every |
| 2295 |
* character is innefficient, however (at least on Linux) getc() is |
| 2296 |
* implemented as a read( 4096 ), and then returns each character |
| 2297 |
* from some internal buffer. |
| 2298 |
*/ |
| 2299 |
if (!ft->rol_string) |
| 2300 |
{ |
| 2301 |
ft->rol_string = (char *)MallocTSD( (ft->rol_size=512) ) ; |
| 2302 |
#ifdef TRACEMEM |
| 2303 |
ft->rdarea = ft->rol_string ; |
| 2304 |
#endif |
| 2305 |
} |
| 2306 |
|
| 2307 |
/* |
| 2308 |
if (ptr->fileptr==stdin) |
| 2309 |
fcntl( stdin, F_SETFL, O_NONBLOCK | fcntl(stdin,F_GETFL)) ; |
| 2310 |
*/ |
| 2311 |
errno = 0 ; |
| 2312 |
SWITCH_OPER_READ(ptr); |
| 2313 |
/* |
| 2314 |
* Switch to the current readpos setting in case the current position |
| 2315 |
* is based on the write position. |
| 2316 |
*/ |
| 2317 |
ptr->thispos = ptr->readpos; |
| 2318 |
if ( ptr->flag & FLAG_PERSIST ) |
| 2319 |
rx_fseek(ptr->fileptr,ptr->thispos,SEEK_SET); |
| 2320 |
for (i=0; ; i++) |
| 2321 |
{ |
| 2322 |
j = getc(ptr->fileptr); |
| 2323 |
if (j == REGINA_EOL) |
| 2324 |
{ |
| 2325 |
eolf = REGINA_EOL; |
| 2326 |
break; |
| 2327 |
} |
| 2328 |
/*#if !defined(UNIX) && !defined(MAC)*/ |
| 2329 |
/* |
| 2330 |
* MH 25042003 - all platforms can read lines with |
| 2331 |
* CRLF, LF, or CR - consistent with ObjectRexx |
| 2332 |
*/ |
| 2333 |
if (j == REGINA_CR) |
| 2334 |
{ |
| 2335 |
k = getc(ptr->fileptr); |
| 2336 |
if (k == REGINA_EOL) |
| 2337 |
{ |
| 2338 |
eolf = REGINA_EOL; |
| 2339 |
eolchars = 2; |
| 2340 |
break; |
| 2341 |
} |
| 2342 |
else |
| 2343 |
{ |
| 2344 |
ungetc(k,ptr->fileptr); |
| 2345 |
eolf = REGINA_EOL; |
| 2346 |
break; |
| 2347 |
} |
| 2348 |
} |
| 2349 |
/*#endif*/ |
| 2350 |
/* |
| 2351 |
* If we hit end-of-file, handle it carefully, and terminate the |
| 2352 |
* reading. Note that this means that there we have read an |
| 2353 |
* incomplete last line, so return what we've got, and report |
| 2354 |
* an NOTREADY condition. (Partly, I disagree, but this is how |
| 2355 |
* TRL defines it ... Case I: Programmer using Emacs forgets to |
| 2356 |
* add a EOL after the last line; Rexx triggers NOTREADY when |
| 2357 |
* reading that last, incomplete line. |
| 2358 |
|
| 2359 |
* MH 4-Sep-2002 - change in semantics happened a while ago. |
| 2360 |
* We treat incomplete line as a "normal" line, and NOTREADY |
| 2361 |
* is NOT raised when reading an incomplete line. |
| 2362 |
*/ |
| 2363 |
if (j==EOF) |
| 2364 |
{ |
| 2365 |
ptr->flag |= FLAG_RDEOF ; |
| 2366 |
/* file_warning( ptr, 0, "EOF on line input" ) ; */ |
| 2367 |
break ; |
| 2368 |
} |
| 2369 |
|
| 2370 |
/* |
| 2371 |
* We are trying to avoid any limits other than memory-imposed |
| 2372 |
* limits. So if the buffer size that we currently have are too |
| 2373 |
* small, double it, and hide the operation from the rest of the |
| 2374 |
* interpreter. |
| 2375 |
*/ |
| 2376 |
if (i>=ft->rol_size) |
| 2377 |
{ |
| 2378 |
char *tmpstring ; |
| 2379 |
|
| 2380 |
assert( i == ft->rol_size ) ; |
| 2381 |
tmpstring = (char *)MallocTSD( 2*ft->rol_size+10 ) ; |
| 2382 |
memcpy( tmpstring, ft->rol_string, ft->rol_size ) ; |
| 2383 |
FreeTSD( ft->rol_string ) ; |
| 2384 |
ft->rol_string = tmpstring ; |
| 2385 |
ft->rol_size *= 2 ; |
| 2386 |
#ifdef TRACEMEM |
| 2387 |
ft->rdarea = ft->rol_string ; |
| 2388 |
#endif |
| 2389 |
} |
| 2390 |
|
| 2391 |
/* |
| 2392 |
* Just an ordinary character ... append it to the buffer |
| 2393 |
*/ |
| 2394 |
ft->rol_string[i] = (char) j ; |
| 2395 |
} |
| 2396 |
/* |
| 2397 |
* Attempt to set the read pointer and the current file |
| 2398 |
* pointer based on the length of the line we just read. |
| 2399 |
*/ |
| 2400 |
#if 1 /* really MH */ |
| 2401 |
if ( ptr->thispos == ptr->readpos ) |
| 2402 |
{ |
| 2403 |
if ( ptr->thispos == (size_t) EOF ) |
| 2404 |
{ |
| 2405 |
errno = 0 ; |
| 2406 |
ptr->thispos = ptr->readpos = rx_ftell( ptr->fileptr ) ; |
| 2407 |
} |
| 2408 |
else |
| 2409 |
{ |
| 2410 |
ptr->thispos += (i - (j==EOF)) + eolchars ; |
| 2411 |
ptr->readpos = ptr->thispos ; |
| 2412 |
} |
| 2413 |
} |
| 2414 |
else |
| 2415 |
{ |
| 2416 |
errno = 0 ; |
| 2417 |
ptr->thispos = ptr->readpos = rx_ftell( ptr->fileptr ) ; |
| 2418 |
} |
| 2419 |
#else |
| 2420 |
if (ptr->thispos != (size_t) EOF) |
| 2421 |
ptr->thispos += (i - (j==EOF)) + eolchars ; |
| 2422 |
|
| 2423 |
if (ptr->readpos != (size_t) EOF) |
| 2424 |
ptr->readpos = ptr->thispos ; |
| 2425 |
#endif |
| 2426 |
/* |
| 2427 |
* If we did read a complete line, we have to increment the line |
| 2428 |
* count for the current read pointer of this file. This part of |
| 2429 |
* the code is a bit Unix-ish. It will have to be reworked for |
| 2430 |
* other types of operating systems. |
| 2431 |
*/ |
| 2432 |
if ((eolf==REGINA_EOL) && (ptr->readline > 0)) |
| 2433 |
{ |
| 2434 |
#if 1 /* really MH */ |
| 2435 |
ptr->readline += 1 ; /* only if we actually saw the "\n" !!! */ |
| 2436 |
#else |
| 2437 |
ptr->readline += eolchars ; /* only if we actually saw the "\n" !!! */ |
| 2438 |
#endif |
| 2439 |
if (ptr->linesleft) |
| 2440 |
ptr->linesleft-- ; |
| 2441 |
} |
| 2442 |
/* |
| 2443 |
* A bit of a hack here. Because countlines() determines if any lines |
| 2444 |
* are left in the stream by using the feof() function, we have to |
| 2445 |
* attempt to read the EOF after each line, and set the file's state |
| 2446 |
* to EOF. If the character read is not EOF, then put it back on |
| 2447 |
* the stream to be read later. |
| 2448 |
* Only do this for persistent streams!! |
| 2449 |
*/ |
| 2450 |
if ( ptr->flag & FLAG_PERSIST |
| 2451 |
&& !feof( ptr->fileptr ) ) |
| 2452 |
{ |
| 2453 |
int ch0; |
| 2454 |
ch0 = getc(ptr->fileptr); |
| 2455 |
if (feof(ptr->fileptr)) |
| 2456 |
{ |
| 2457 |
ptr->flag |= FLAG_RDEOF ; |
| 2458 |
/* file_warning( ptr, 0, "EOF on line input" ) ; */ |
| 2459 |
} |
| 2460 |
else |
| 2461 |
{ |
| 2462 |
ungetc(ch0,ptr->fileptr); |
| 2463 |
} |
| 2464 |
} |
| 2465 |
|
| 2466 |
/* |
| 2467 |
* Wrap up the data that was read, and return it as a 'streng'. |
| 2468 |
* |
| 2469 |
*/ |
| 2470 |
/* if (i>1000) i = 1000 ; */ |
| 2471 |
ret = Str_makeTSD( i ) ; |
| 2472 |
memcpy( ret->value, ft->rol_string, ret->len=i ) ; |
| 2473 |
return ret ; |
| 2474 |
} |
| 2475 |
|
| 2476 |
|
| 2477 |
static rx_64 positionfile_SEEK_SET( tsd_t *TSD, const char *bif, int argno, fileboxptr ptr, int oper, rx_64 lineno ) |
| 2478 |
{ |
| 2479 |
int ch=0x00; |
| 2480 |
rx_64 from_line=0, tmp=0 ; |
| 2481 |
rx_64 from_char=0L ; |
| 2482 |
rx_64 ret; |
| 2483 |
|
| 2484 |
/* |
| 2485 |
* We know the line number of at most three positions in the file: |
| 2486 |
* the start of the file, the write position and the read position. |
| 2487 |
* If the file is open only for reading or writing, we know at most |
| 2488 |
* two positions. And in addition, the read and/or the write |
| 2489 |
* position may be be invalid (i.e. previous operation was |
| 2490 |
* character oriented). But at least, we know the line number of |
| 2491 |
* one position, the start of the file, which is the first line. |
| 2492 |
* |
| 2493 |
* The best method seems to be: First start with the start of file |
| 2494 |
* and then see if using the read or the write position instead is |
| 2495 |
* a better deal. There is one drawback ... we assume that all lines |
| 2496 |
* are equally long. That assumption is probably not too bad for text |
| 2497 |
* files, but it may create unnecessary overhead for 'peculiar' files |
| 2498 |
*/ |
| 2499 |
from_line = 1 ; |
| 2500 |
from_char = 0 ; |
| 2501 |
|
| 2502 |
if ( oper & OPER_READ |
| 2503 |
&& ptr->flag & FLAG_PERSIST ) |
| 2504 |
{ |
| 2505 |
if ( rx_fseek( ptr->fileptr, ptr->readpos, SEEK_SET ) ) |
| 2506 |
{ |
| 2507 |
file_error( ptr, errno, NULL ) ; |
| 2508 |
return 0; |
| 2509 |
} |
| 2510 |
ptr->thispos = ptr->readpos ; |
| 2511 |
} |
| 2512 |
if ( oper & OPER_WRITE |
| 2513 |
&& ptr->flag & FLAG_PERSIST ) |
| 2514 |
{ |
| 2515 |
if ( rx_fseek( ptr->fileptr, ptr->writepos, SEEK_SET ) ) |
| 2516 |
{ |
| 2517 |
file_error( ptr, errno, NULL ) ; |
| 2518 |
return 0; |
| 2519 |
} |
| 2520 |
ptr->thispos = ptr->writepos ; |
| 2521 |
} |
| 2522 |
|
| 2523 |
/* |
| 2524 |
* First, let's check to see if we gain anything from using the |
| 2525 |
* read position instead. If the distance from the current read |
| 2526 |
* position to the wanted line (counted in number of lines) is smaller |
| 2527 |
* than the number of lines from the first line to the wanted line, |
| 2528 |
* use the current read position in stead. But only if the current |
| 2529 |
* read position is defined. |
| 2530 |
*/ |
| 2531 |
if ((ptr->flag & FLAG_READ) && (ptr->readline > 0)) |
| 2532 |
{ |
| 2533 |
assert( ptr->readpos != (size_t) EOF) ; |
| 2534 |
tmp = ptr->readline - lineno ; |
| 2535 |
if (tmp<0) |
| 2536 |
tmp = (-tmp) ; |
| 2537 |
|
| 2538 |
if (tmp < (lineno - from_line)) |
| 2539 |
{ |
| 2540 |
from_line = ptr->readline ; |
| 2541 |
from_char = ptr->readpos ; |
| 2542 |
} |
| 2543 |
} |
| 2544 |
|
| 2545 |
/* |
| 2546 |
* Then, we check to see whether we can gain even more if we use |
| 2547 |
* the current write position of the file instead. |
| 2548 |
*/ |
| 2549 |
if ((ptr->flag & FLAG_WRITE) && (ptr->writeline > 0)) |
| 2550 |
{ |
| 2551 |
assert( ptr->writepos != (size_t) EOF ) ; |
| 2552 |
tmp = ptr->writeline - lineno ; |
| 2553 |
if (tmp<0) |
| 2554 |
tmp = (-tmp) ; |
| 2555 |
|
| 2556 |
if (tmp < (lineno - from_line)) |
| 2557 |
{ |
| 2558 |
from_line = ptr->writeline ; |
| 2559 |
from_char = ptr->writepos ; |
| 2560 |
} |
| 2561 |
} |
| 2562 |
|
| 2563 |
/* |
| 2564 |
* By now, the variables from_line, and from_char should contain |
| 2565 |
* the optimal starting point from where a seek for the 'lineno' |
| 2566 |
* line in the file can start, so first, move there. An in addition, |
| 2567 |
* it should be the known position which is closest to the wanted |
| 2568 |
* line. |
| 2569 |
*/ |
| 2570 |
if (from_char != (long) ptr->thispos) |
| 2571 |
{ |
| 2572 |
errno = 0 ; |
| 2573 |
if ( ptr->flag & FLAG_PERSIST |
| 2574 |
&& rx_fseek( ptr->fileptr, from_char, SEEK_SET )) |
| 2575 |
{ |
| 2576 |
file_error( ptr, errno, NULL ) ; |
| 2577 |
return 0; |
| 2578 |
} |
| 2579 |
ptr->oper = OPER_NONE; |
| 2580 |
ptr->thispos = from_char ; |
| 2581 |
} |
| 2582 |
assert( from_char == rx_ftell(ptr->fileptr) ) ; |
| 2583 |
|
| 2584 |
/* |
| 2585 |
* Now we are positioned at the right spot, so seek forwards or |
| 2586 |
* backwards until we reach the correct line. Actually, the method |
| 2587 |
* we are going to use may seem a bit strange at first. First we |
| 2588 |
* seek forward until we pass the line, and then we seek backwards |
| 2589 |
* until we reach the line and at the end we back up to the first |
| 2590 |
* preceding end-of-line marker. This may seem awkward, but it is |
| 2591 |
* fairly simple. And in addition, it will always position us at |
| 2592 |
* the _start_ of the wanted line. |
| 2593 |
*/ |
| 2594 |
once_more: |
| 2595 |
while ((lineno>from_line)) /* seek forward */ |
| 2596 |
{ |
| 2597 |
SWITCH_OPER_READ(ptr); |
| 2598 |
for (;((ch=getc(ptr->fileptr))!=EOF)&&(ch!=REGINA_EOL);from_char++) ; |
| 2599 |
if (ch==REGINA_EOL) |
| 2600 |
from_line++ ; |
| 2601 |
else |
| 2602 |
break ; |
| 2603 |
} |
| 2604 |
|
| 2605 |
/* |
| 2606 |
* Then we seek backwards until we reach the line. The backwards |
| 2607 |
* movement is _really_ awkward, so perhaps we should read in 512 |
| 2608 |
* bytes, and analyse the data in it instead? Indeed, another |
| 2609 |
* algoritm should be chosen. Maybe later ... |
| 2610 |
*/ |
| 2611 |
while (lineno<=from_line && from_char>0) |
| 2612 |
{ |
| 2613 |
errno = 0 ; |
| 2614 |
if ( ptr->flag & FLAG_PERSIST |
| 2615 |
&& rx_fseek(ptr->fileptr, -1, SEEK_CUR)) |
| 2616 |
{ |
| 2617 |
/* |
| 2618 |
* Should this happen? Only if someone overwrites EOF chars in |
| 2619 |
* the file, but that _may_ happend ... Report error for |
| 2620 |
* any errors from the fseek and ftell. If we hit the start of |
| 2621 |
* the file, reset from_line check whether we are _below_ lineno |
| 2622 |
* If so, jump back and seek from the start (then we *must* |
| 2623 |
* start at line 1, since the data we've got are illegal). |
| 2624 |
* |
| 2625 |
* It will also happen if we are seeking backwards for the |
| 2626 |
* first line. |
| 2627 |
*/ |
| 2628 |
errno = 0 ; |
| 2629 |
if (rx_fseek(ptr->fileptr,0,SEEK_SET)) |
| 2630 |
{ |
| 2631 |
file_error( ptr, errno, NULL ) ; |
| 2632 |
return 0; |
| 2633 |
} |
| 2634 |
ptr->oper = OPER_NONE; |
| 2635 |
|
| 2636 |
from_line = 1 ; |
| 2637 |
ptr->thispos = 0 ; |
| 2638 |
if (from_line<lineno) |
| 2639 |
{ |
| 2640 |
ptr->readline = (-1); |
| 2641 |
ptr->writeline = 0; /* unknown */ |
| 2642 |
goto once_more ; |
| 2643 |
} |
| 2644 |
|
| 2645 |
break ; /* we were looking for the first line ... how lucky :-) */ |
| 2646 |
} |
| 2647 |
|
| 2648 |
/* |
| 2649 |
* After seeking one character backwards, we must read the character |
| 2650 |
* that we just skipped over. Do that, and test whether it is |
| 2651 |
* a end-of-line character. |
| 2652 |
*/ |
| 2653 |
SWITCH_OPER_READ(ptr); |
| 2654 |
ch = getc(ptr->fileptr) ; |
| 2655 |
if (ch==REGINA_EOL) |
| 2656 |
{ |
| 2657 |
if (lineno==from_line) |
| 2658 |
break ; |
| 2659 |
|
| 2660 |
from_line-- ; |
| 2661 |
} |
| 2662 |
|
| 2663 |
/* |
| 2664 |
* Then we move backwards once more, in order to compensate for |
| 2665 |
* reading the character. Sigh, we are really doing a lot of |
| 2666 |
* forward and backward reading, arn't we? |
| 2667 |
*/ |
| 2668 |
errno = 0 ; |
| 2669 |
if ( ptr->flag & FLAG_PERSIST |
| 2670 |
&& rx_fseek(ptr->fileptr, -1, SEEK_CUR)) |
| 2671 |
{ |
| 2672 |
file_error( ptr, errno, NULL ) ; |
| 2673 |
return 0; |
| 2674 |
} |
| 2675 |
ptr->oper = OPER_NONE; |
| 2676 |
} |
| 2677 |
|
| 2678 |
/* |
| 2679 |
* Now we are almost finished. We just have to set the correct |
| 2680 |
* information in the Rexx file table entry. |
| 2681 |
*/ |
| 2682 |
ptr->thispos = rx_ftell( ptr->fileptr ) ; |
| 2683 |
if (oper & OPER_READ) |
| 2684 |
{ |
| 2685 |
ptr->readline = from_line ; /* was lineno */ |
| 2686 |
ptr->readpos = ptr->thispos ; |
| 2687 |
ptr->flag &= ~(FLAG_RDEOF) ; |
| 2688 |
ptr->flag &= ~(FLAG_AFTER_RDEOF) ; |
| 2689 |
} |
| 2690 |
if (oper & OPER_WRITE) |
| 2691 |
{ |
| 2692 |
ptr->writeline = from_line ; /* was lineno */ |
| 2693 |
ptr->writepos = ptr->thispos ; |
| 2694 |
ptr->flag &= ~(FLAG_WREOF) ; |
| 2695 |
} |
| 2696 |
|
| 2697 |
if (oper & OPER_READ) |
| 2698 |
ret = ptr->readline ; |
| 2699 |
else |
| 2700 |
ret = ptr->writeline ; |
| 2701 |
return ret; |
| 2702 |
} |
| 2703 |
|
| 2704 |
static rx_64 positionfile_SEEK_CUR( tsd_t *TSD, const char *bif, int argno, fileboxptr ptr, int oper, rx_64 lineno, rx_64 from_line, rx_64 from_char ) |
| 2705 |
{ |
| 2706 |
rx_64 tmp,ret; |
| 2707 |
|
| 2708 |
/* |
| 2709 |
* Do simple checks first. |
| 2710 |
* If we are seeking back before the first line, then set: |
| 2711 |
* READ: |
| 2712 |
* ptr->readline = 1 |
| 2713 |
* ptr->readpos = 1 |
| 2714 |
* return 1 |
| 2715 |
* WRITE: |
| 2716 |
* ptr->writeline = 1 |
| 2717 |
* ptr->writepos = 0 |
| 2718 |
* return 0 |
| 2719 |
*/ |
| 2720 |
tmp = from_line + lineno; |
| 2721 |
|
| 2722 |
if ( tmp < 1 ) |
| 2723 |
{ |
| 2724 |
/* |
| 2725 |
* We have positioned to before the first line |
| 2726 |
* |
| 2727 |
*/ |
| 2728 |
rx_fseek( ptr->fileptr, 0L, SEEK_SET ); |
| 2729 |
ptr->thispos = rx_ftell( ptr->fileptr ); |
| 2730 |
if ( oper == OPER_READ ) |
| 2731 |
{ |
| 2732 |
ptr->readline = 1; |
| 2733 |
ptr->readpos = 1; |
| 2734 |
ptr->oper = OPER_READ; |
| 2735 |
return 1; |
| 2736 |
} |
| 2737 |
else |
| 2738 |
{ |
| 2739 |
ptr->writeline = 1; |
| 2740 |
ptr->writepos = 0; |
| 2741 |
ptr->oper = OPER_WRITE; |
| 2742 |
return 0; |
| 2743 |
} |
| 2744 |
} |
| 2745 |
/* |
| 2746 |
* We now have an absolute line number from the +ve relative line |
| 2747 |
* number, so use the absolute positioning code. |
| 2748 |
*/ |
| 2749 |
ret = positionfile_SEEK_SET( TSD, bif, argno, ptr, oper, tmp ); |
| 2750 |
return ret; |
| 2751 |
} |
| 2752 |
|
| 2753 |
static rx_64 positionfile_SEEK_END( tsd_t *TSD, const char *bif, int argno, fileboxptr ptr, int oper, rx_64 lineno ) |
| 2754 |
{ |
| 2755 |
/* |
| 2756 |
* This function does file positioning on a line basis from the |
| 2757 |
* end of the file. |
| 2758 |
* There is not a lot of optimisation we can do reading backwards. |
| 2759 |
* We first need to determine the last line; does it end in an EOL, |
| 2760 |
* or is it incomplete; ie does not end in EOL. We treat an incomplete |
| 2761 |
* last line as a line. |
| 2762 |
* Our initial attempt at this will position the file at the |
| 2763 |
* end, and read backwards; ie getc(ptr->fileptr), seek() back 2 chars, |
| 2764 |
* until the start of file, or we have the specified number of lines. |
| 2765 |
*/ |
| 2766 |
rx_64 here, next, save_pos, i, ret, this_lineno, num_lines; |
| 2767 |
size_t bret; |
| 2768 |
char buf[512]; |
| 2769 |
int found = 0; |
| 2770 |
|
| 2771 |
SWITCH_OPER_READ(ptr); |
| 2772 |
/* |
| 2773 |
* First, get the size of the file. We can only do positioning on |
| 2774 |
* persistant files... |
| 2775 |
*/ |
| 2776 |
if ( ! (ptr->flag & FLAG_PERSIST) ) |
| 2777 |
{ |
| 2778 |
file_error( ptr, 0, "Cannot position on transient stream" ); |
| 2779 |
return 0; |
| 2780 |
} |
| 2781 |
if ( rx_fseek( ptr->fileptr, 0L, SEEK_END ) ) |
| 2782 |
{ |
| 2783 |
file_error( ptr, errno, NULL ) ; |
| 2784 |
return 0; |
| 2785 |
} |
| 2786 |
|
| 2787 |
here = rx_ftell( ptr->fileptr ); |
| 2788 |
/* |
| 2789 |
* Seek backwards one character and read the character. If the last |
| 2790 |
* character is REGINA_EOL, then DON'T treat this as a new line. |
| 2791 |
*/ |
| 2792 |
if ( rx_fseek( ptr->fileptr, -1L, SEEK_CUR ) ) |
| 2793 |
{ |
| 2794 |
file_error( ptr, errno, NULL ) ; |
| 2795 |
return 0; |
| 2796 |
} |
| 2797 |
buf[0] = (char)getc( ptr->fileptr ); |
| 2798 |
if ( buf[0] == REGINA_EOL ) |
| 2799 |
num_lines = 0; |
| 2800 |
else |
| 2801 |
num_lines = 1; |
| 2802 |
/* |
| 2803 |
* Move the file pointer back to after the last character |
| 2804 |
*/ |
| 2805 |
if ( rx_fseek( ptr->fileptr, 0L, SEEK_END ) ) |
| 2806 |
{ |
| 2807 |
file_error( ptr, errno, NULL ) ; |
| 2808 |
return 0; |
| 2809 |
} |
| 2810 |
|
| 2811 |
/* |
| 2812 |
* We have to read backwards in the file until we reach a known point. |
| 2813 |
* The only point which we can guarantee is known is the start of the |
| 2814 |
* file. We can't use ptr->(read/write)line as we really don't know |
| 2815 |
* how many lines are in the file (we may have appended several earlier). |
| 2816 |
* An incomplete last line of a file (ie no CRLF/LF) is treated as a complete |
| 2817 |
* line. |
| 2818 |
*/ |
| 2819 |
for ( ; ; ) |
| 2820 |
{ |
| 2821 |
/* |
| 2822 |
* Determine where we want to move the file pointer backwards |
| 2823 |
* into the file, and then move the file pointer there ready for |
| 2824 |
* reading a buffer. |
| 2825 |
*/ |
| 2826 |
next = min( 512, here ); |
| 2827 |
if ( rx_fseek( ptr->fileptr, -next, SEEK_CUR ) ) |
| 2828 |
{ |
| 2829 |
file_error( ptr, errno, NULL ) ; |
| 2830 |
return 0; |
| 2831 |
} |
| 2832 |
/* |
| 2833 |
* Save our current position; start of the buffer being read |
| 2834 |
*/ |
| 2835 |
save_pos = rx_ftell( ptr->fileptr ); |
| 2836 |
/* |
| 2837 |
* Read a buffer, this moves the file pointer forward to the |
| 2838 |
* end of the buffer |
| 2839 |
*/ |
| 2840 |
|
| 2841 |
bret = fread( buf, sizeof(char), next, ptr->fileptr ); |
| 2842 |
if ( bret != next |
| 2843 |
&& bret != EOF ) |
| 2844 |
{ |
| 2845 |
file_error( ptr, errno, NULL ) ; |
| 2846 |
return 0; |
| 2847 |
} |
| 2848 |
/* |
| 2849 |
* Count the number of lines from the end of the buffer |
| 2850 |
*/ |
| 2851 |
for ( i = next-1; i >= 0; i-- ) |
| 2852 |
{ |
| 2853 |
if ( buf[i] == REGINA_EOL ) |
| 2854 |
{ |
| 2855 |
num_lines++; |
| 2856 |
if ( num_lines > lineno |
| 2857 |
&& !found ) |
| 2858 |
{ |
| 2859 |
/* |
| 2860 |
* Calculate the actual char file position of the EOL |
| 2861 |
*/ |
| 2862 |
/* |
| 2863 |
* the +1 is to point at the character AFTER the EOL; the |
| 2864 |
* first character of the next line. |
| 2865 |
*/ |
| 2866 |
ptr->thispos = save_pos + i + 1; |
| 2867 |
found = 1; |
| 2868 |
} |
| 2869 |
} |
| 2870 |
} |
| 2871 |
/* |
| 2872 |
* move the file pointer back to the start of the buffer just |
| 2873 |
* read, so the next fseek() backwards is from the START of the |
| 2874 |
* buffer. |
| 2875 |
*/ |
| 2876 |
if ( rx_fseek( ptr->fileptr, save_pos, SEEK_SET ) ) |
| 2877 |
{ |
| 2878 |
file_error( ptr, errno, NULL ) ; |
| 2879 |
return 0; |
| 2880 |
} |
| 2881 |
/* |
| 2882 |
* Calculate our own file position |
| 2883 |
*/ |
| 2884 |
here -= next; |
| 2885 |
if ( here == 0 ) |
| 2886 |
break; |
| 2887 |
} |
| 2888 |
/* |
| 2889 |
* We are at the start of the file. If we haven't found our lineno |
| 2890 |
* (because it was greater than the number of lines in the file), |
| 2891 |
* that's where we stay. |
| 2892 |
*/ |
| 2893 |
if ( found ) |
| 2894 |
{ |
| 2895 |
/* ptr->thispos already set */ |
| 2896 |
this_lineno = 1 + (num_lines - lineno); |
| 2897 |
} |
| 2898 |
else |
| 2899 |
{ |
| 2900 |
ptr->thispos = 0; |
| 2901 |
this_lineno = 1; |
| 2902 |
} |
| 2903 |
|
| 2904 |
/* |
| 2905 |
* Now we are almost finished. We just have to set the correct |
| 2906 |
* information in the Rexx file table entry. |
| 2907 |
*/ |
| 2908 |
if ( rx_fseek( ptr->fileptr, ptr->thispos, SEEK_SET ) ) |
| 2909 |
{ |
| 2910 |
file_error( ptr, errno, NULL ) ; |
| 2911 |
return 0; |
| 2912 |
} |
| 2913 |
if ( oper & OPER_READ ) |
| 2914 |
{ |
| 2915 |
ptr->readline = this_lineno; |
| 2916 |
ptr->readpos = ptr->thispos; |
| 2917 |
ptr->flag &= ~(FLAG_RDEOF); |
| 2918 |
ptr->flag &= ~(FLAG_AFTER_RDEOF); |
| 2919 |
} |
| 2920 |
if ( oper & OPER_WRITE ) |
| 2921 |
{ |
| 2922 |
ptr->writeline = this_lineno; |
| 2923 |
ptr->writepos = ptr->thispos ; |
| 2924 |
ptr->flag &= ~(FLAG_WREOF) ; |
| 2925 |
} |
| 2926 |
/* |
| 2927 |
* We just counted the number of lines between the end of the file and |
| 2928 |
* our current position, so we know how many lines are left. |
| 2929 |
* The number of lines counted is one more than actually left in the file. |
| 2930 |
*/ |
| 2931 |
ptr->linesleft = num_lines-1; |
| 2932 |
if ( oper & OPER_READ ) |
| 2933 |
ret = ptr->readline ; |
| 2934 |
else |
| 2935 |
ret = ptr->writeline ; |
| 2936 |
return ret; |
| 2937 |
} |
| 2938 |
|
| 2939 |
/* |
| 2940 |
* This routine will position the current read or write position |
| 2941 |
* of a file, to the start of a particular line. The file to be |
| 2942 |
* operated on is 'ptr', the pointer to manipulate is indicated |
| 2943 |
* by 'oper' (either OPER_READ or OPER_WRITE or both), and the linenumber |
| 2944 |
* to position at is 'lineno'. |
| 2945 |
* 'from' specifies if the positioning is done as an absolute position SEEK_SET, |
| 2946 |
* a relative position from the current position SEEK_CUR, or relative to the |
| 2947 |
* end of file: SEEK_END. |
| 2948 |
* |
| 2949 |
* There are (at least) two ways to do the backup of the current |
| 2950 |
* position in the file. First to backup to the start of the file |
| 2951 |
* and then to seek forward, or to seek backwards from the current |
| 2952 |
* position of the file. |
| 2953 |
* |
| 2954 |
* Perhaps the first is best for the standard case, and the second |
| 2955 |
* should be activated when the line-parameter is negative ... ? |
| 2956 |
*/ |
| 2957 |
|
| 2958 |
static rx_64 positionfile( tsd_t *TSD, const char *bif, int argno, fileboxptr ptr, int oper, rx_64 lineno, int from ) |
| 2959 |
{ |
| 2960 |
rx_64 from_line=0; |
| 2961 |
rx_64 from_char=0; |
| 2962 |
rx_64 ret=0; |
| 2963 |
|
| 2964 |
/* |
| 2965 |
* If file is in ERROR state, don't touch it. |
| 2966 |
*/ |
| 2967 |
if (ptr->flag & FLAG_ERROR) |
| 2968 |
{ |
| 2969 |
if (!(ptr->flag & FLAG_FAKE)) |
| 2970 |
file_error( ptr, 0, NULL ) ; |
| 2971 |
return 0; |
| 2972 |
} |
| 2973 |
|
| 2974 |
/* |
| 2975 |
* If this isn't a persistent file, then report an error. We can only |
| 2976 |
* perform repositioning in persistent files. |
| 2977 |
*/ |
| 2978 |
|
| 2979 |
if (!(ptr->flag & FLAG_PERSIST )) |
| 2980 |
exiterror( ERR_INCORRECT_CALL, 42, bif, tmpstr_of( TSD, ptr->filename0 ) ) ; |
| 2981 |
|
| 2982 |
/* |
| 2983 |
* If the operation is READ, but the file is not open for READ, |
| 2984 |
* return an error. |
| 2985 |
*/ |
| 2986 |
if ((oper&OPER_READ) && !(ptr->flag & FLAG_READ)) |
| 2987 |
exiterror( ERR_INCORRECT_CALL, 921, bif, argno, "READ" ) ; |
| 2988 |
/* |
| 2989 |
* If the operation is WRITE, but the file is not open for WRITE, |
| 2990 |
* return an error. |
| 2991 |
*/ |
| 2992 |
if ( (oper&OPER_WRITE) && !(ptr->flag & FLAG_WRITE) ) |
| 2993 |
exiterror( ERR_INCORRECT_CALL, 921, bif, argno, "WRITE" ) ; |
| 2994 |
|
| 2995 |
/* |
| 2996 |
* If we do any repositioning, then make the old estimate of lines |
| 2997 |
* left to read invalid. This is not really needed in all cases, but |
| 2998 |
* it is a good start. And you _may_ even want to recalculate the |
| 2999 |
* number of lines left! |
| 3000 |
*/ |
| 3001 |
if ( ptr->linesleft > 0 ) |
| 3002 |
ptr->linesleft = 0 ; |
| 3003 |
|
| 3004 |
if ( ptr->thispos == (size_t) EOF ) |
| 3005 |
{ |
| 3006 |
errno = 0 ; |
| 3007 |
ptr->thispos = rx_ftell( ptr->fileptr ) ; |
| 3008 |
} |
| 3009 |
|
| 3010 |
/* |
| 3011 |
* So, what we are going to do depends partly on whether we are moving |
| 3012 |
* the read or the write position of the file. We may even be as |
| 3013 |
* lucky as not to have to move anything ... :-) First we can clear |
| 3014 |
* the EOF flag, if set. Repositioning will clean up any EOF state. |
| 3015 |
*/ |
| 3016 |
if (oper & OPER_READ) |
| 3017 |
{ |
| 3018 |
ptr->flag &= ~(FLAG_RDEOF) ; |
| 3019 |
ptr->flag &= ~(FLAG_AFTER_RDEOF) ; |
| 3020 |
} |
| 3021 |
if (oper & OPER_WRITE) |
| 3022 |
ptr->flag &= ~(FLAG_WREOF) ; |
| 3023 |
|
| 3024 |
/* |
| 3025 |
* Positioning by line in a forwards direction is always going to be more efficient |
| 3026 |
* starting at the current line position and reading forward. |
| 3027 |
* Positioning by line in a backwards direction may be more efficient to start at the |
| 3028 |
* beginning of the file and read forwards, rather than reading backwards; it |
| 3029 |
* depends on how far back we are positioning. |
| 3030 |
*/ |
| 3031 |
switch( from ) |
| 3032 |
{ |
| 3033 |
case SEEK_CUR: /* position relative to current position */ |
| 3034 |
if ( oper & OPER_READ ) |
| 3035 |
{ |
| 3036 |
if ( ptr->readline > 0 ) |
| 3037 |
{ |
| 3038 |
from_line = ptr->readline ; |
| 3039 |
from_char = ptr->readpos ; |
| 3040 |
ret = positionfile_SEEK_CUR( TSD, bif, argno, ptr, OPER_READ, lineno, from_line, from_char ); |
| 3041 |
} |
| 3042 |
else |
| 3043 |
{ |
| 3044 |
/* |
| 3045 |
* If the readpos is set, then we can (inefficiently) determine the line |
| 3046 |
* position by starting at the beginning of the file. |
| 3047 |
*/ |
| 3048 |
if ( ptr->readpos != (size_t) -1 ) |
| 3049 |
{ |
| 3050 |
/* |
| 3051 |
* FIXME: We need a mechanism to convert to readpos into a readline; |
| 3052 |
* positionfile_SEEK_SET() doesn't do it for us. |
| 3053 |
* |
| 3054 |
*/ |
| 3055 |
errno = 2; |
| 3056 |
ret = -1; |
| 3057 |
break; |
| 3058 |
} |
| 3059 |
else |
| 3060 |
{ |
| 3061 |
/* |
| 3062 |
* ERROR:2 |
| 3063 |
* Can't seek lines relatively if there is no current read char position |
| 3064 |
*/ |
| 3065 |
errno = 2; |
| 3066 |
ret = -1; |
| 3067 |
break; |
| 3068 |
} |
| 3069 |
} |
| 3070 |
} |
| 3071 |
if ( oper & OPER_WRITE ) |
| 3072 |
{ |
| 3073 |
if ( ptr->writeline > 0 ) |
| 3074 |
{ |
| 3075 |
from_line = ptr->writeline ; |
| 3076 |
from_char = ptr->writepos ; |
| 3077 |
ret = positionfile_SEEK_CUR( TSD, bif, argno, ptr, OPER_WRITE, lineno, from_line, from_char ); |
| 3078 |
} |
| 3079 |
else |
| 3080 |
{ |
| 3081 |
/* |
| 3082 |
* If the writepos is set, then we can (inefficiently) determine the line |
| 3083 |
* position by starting at the beginning of the file. |
| 3084 |
*/ |
| 3085 |
if ( ptr->writepos != (size_t) -1 ) |
| 3086 |
{ |
| 3087 |
/* |
| 3088 |
* FIXME: We need a mechanism to convert to writepos into a writeline; |
| 3089 |
* positionfile_SEEK_SET() doesn't do it for us. |
| 3090 |
* |
| 3091 |
*/ |
| 3092 |
errno = 2; |
| 3093 |
ret = -1; |
| 3094 |
break; |
| 3095 |
} |
| 3096 |
else |
| 3097 |
{ |
| 3098 |
/* |
| 3099 |
* ERROR:2 |
| 3100 |
* Can't seek lines relatively if there is no current write char position |
| 3101 |
*/ |
| 3102 |
errno = 2; |
| 3103 |
ret = -1; |
| 3104 |
break; |
| 3105 |
} |
| 3106 |
} |
| 3107 |
} |
| 3108 |
/* |
| 3109 |
* Now we are almost finished. We just have to set the correct |
| 3110 |
* information in the Rexx file table entry. |
| 3111 |
*/ |
| 3112 |
if ( (oper & OPER_READ) && (oper & OPER_WRITE) ) |
| 3113 |
{ |
| 3114 |
ptr->oper = OPER_NONE; |
| 3115 |
} |
| 3116 |
if ( oper & OPER_READ ) |
| 3117 |
{ |
| 3118 |
ptr->flag &= ~(FLAG_RDEOF) ; |
| 3119 |
ptr->flag &= ~(FLAG_AFTER_RDEOF) ; |
| 3120 |
} |
| 3121 |
if ( oper & OPER_WRITE ) |
| 3122 |
{ |
| 3123 |
ptr->flag &= ~(FLAG_WREOF) ; |
| 3124 |
} |
| 3125 |
break; |
| 3126 |
case SEEK_END: /* position relative to end of file */ |
| 3127 |
ret = positionfile_SEEK_END( TSD, bif, argno, ptr, oper, lineno ); |
| 3128 |
break; |
| 3129 |
case SEEK_SET: /* position absolute */ |
| 3130 |
ret = positionfile_SEEK_SET( TSD, bif, argno, ptr, oper, lineno ); |
| 3131 |
default: /* should not get here */ |
| 3132 |
break; |
| 3133 |
} |
| 3134 |
return ret; |
| 3135 |
} |
| 3136 |
|
| 3137 |
|
| 3138 |
|
| 3139 |
/* |
| 3140 |
* I wish every function would be as easy as this! Basically, it |
| 3141 |
* only contain simple error checking, and a direct positioning. |
| 3142 |
* it is called by the built-in function CHARIN() and CHAROUT() |
| 3143 |
* in order to position the current read or write position at the |
| 3144 |
* correct place in the file. |
| 3145 |
*/ |
| 3146 |
static rx_64 positioncharfile( tsd_t *TSD, const char *bif, int argno, fileboxptr fileptr, int oper, rx_64 where, int from ) |
| 3147 |
{ |
| 3148 |
rx_64 where_read=0,where_write=0; |
| 3149 |
/* |
| 3150 |
* If the file is in state ERROR, don't touch it! Since we are not |
| 3151 |
* to return any data, don't bother about the state of FAKE. |
| 3152 |
*/ |
| 3153 |
if (fileptr->flag & FLAG_ERROR) |
| 3154 |
{ |
| 3155 |
if (!(fileptr->flag & FLAG_FAKE)) |
| 3156 |
file_error( fileptr, 0, NULL ) ; |
| 3157 |
return 0; |
| 3158 |
} |
| 3159 |
|
| 3160 |
/* |
| 3161 |
* If the file is not persistent, then positioning is not allowed. |
| 3162 |
* Give the appropriate error for this. |
| 3163 |
*/ |
| 3164 |
if (!(fileptr->flag & FLAG_PERSIST)) |
| 3165 |
exiterror( ERR_INCORRECT_CALL, 42, bif, tmpstr_of( TSD, fileptr->filename0 ) ) ; |
| 3166 |
/* |
| 3167 |
* If the operation is READ, but the file is not open for READ, |
| 3168 |
* return an error. |
| 3169 |
*/ |
| 3170 |
if ((oper&OPER_READ) && !(fileptr->flag & FLAG_READ)) |
| 3171 |
exiterror( ERR_INCORRECT_CALL, 921, bif, argno, "READ" ) ; |
| 3172 |
/* |
| 3173 |
* If the operation is WRITE, but the file is not open for WRITE, |
| 3174 |
* return an error. |
| 3175 |
*/ |
| 3176 |
if ((oper&OPER_WRITE) && !(fileptr->flag & FLAG_WRITE)) |
| 3177 |
exiterror( ERR_INCORRECT_CALL, 921, bif, argno, "WRITE" ) ; |
| 3178 |
|
| 3179 |
#ifdef TRUE_TRL_IO |
| 3180 |
/* |
| 3181 |
* TRL says that positioning the read position to after the last |
| 3182 |
* character in the file, is an error. Unix allows it, and gives |
| 3183 |
* an EOF at the next reading. So, we have to handle this as a |
| 3184 |
* special case ... Check that the new position is valid. |
| 3185 |
* |
| 3186 |
* Should we give "Incorrect call to routine" when the character |
| 3187 |
* position is greater than the size of the file? Perhaps we should |
| 3188 |
* raise the NOTREADY condition instead? |
| 3189 |
*/ |
| 3190 |
{ |
| 3191 |
rx_64 oldp, endp ; |
| 3192 |
|
| 3193 |
oldp = rx_ftell( fileptr->fileptr ) ; |
| 3194 |
rx_fseek(fileptr->fileptr, 0, SEEK_END) ; |
| 3195 |
endp = rx_ftell( fileptr->fileptr ) ; |
| 3196 |
rx_fseek( fileptr->fileptr, oldp, SEEK_SET ) ; |
| 3197 |
fileptr->oper = OPER_NONE; |
| 3198 |
|
| 3199 |
/* |
| 3200 |
* Determine the value of "where" depending on the starting |
| 3201 |
* location determined by "from". "where" is passed in in an |
| 3202 |
* external format; ie 1 based, internally it must be 0 based |
| 3203 |
*/ |
| 3204 |
switch(from) |
| 3205 |
{ |
| 3206 |
case SEEK_CUR: |
| 3207 |
if ( oper & OPER_READ ) |
| 3208 |
where_read = 1 + where + fileptr->readpos; |
| 3209 |
if ( oper & OPER_WRITE ) |
| 3210 |
where_write = 1 + where + fileptr->writepos; |
| 3211 |
break; |
| 3212 |
case SEEK_END: |
| 3213 |
if ( oper & OPER_READ ) |
| 3214 |
where_read = endp - where; |
| 3215 |
#if SEEK_TO_EOF_FOR_WRITE_IS_AT_EOF |
| 3216 |
if ( oper & OPER_WRITE ) |
| 3217 |
where_write = endp - where; |
| 3218 |
#else |
| 3219 |
if ( oper & OPER_WRITE ) |
| 3220 |
where_write = 1 + endp - where; |
| 3221 |
#endif |
| 3222 |
break; |
| 3223 |
default: /* SEEK_SET */ |
| 3224 |
if ( oper & OPER_READ ) |
| 3225 |
where_read = where; |
| 3226 |
if ( oper & OPER_WRITE ) |
| 3227 |
where_write = where; |
| 3228 |
break; |
| 3229 |
} |
| 3230 |
if ( oper & OPER_READ ) |
| 3231 |
{ |
| 3232 |
if ( where_read < 1 ) |
| 3233 |
{ |
| 3234 |
file_error( fileptr, 0, "Repositioning before start of file" ) ; |
| 3235 |
return 0; |
| 3236 |
} |
| 3237 |
if ( endp < where_read ) |
| 3238 |
{ |
| 3239 |
file_error( fileptr, 0, "Repositioning at or after EOF" ) ; |
| 3240 |
return 0; |
| 3241 |
} |
| 3242 |
} |
| 3243 |
if ( oper & OPER_WRITE ) |
| 3244 |
{ |
| 3245 |
if ( where_write < 1 ) |
| 3246 |
{ |
| 3247 |
file_error( fileptr, 0, "Repositioning before start of file" ) ; |
| 3248 |
return 0; |
| 3249 |
} |
| 3250 |
if ( (endp+1) < where_write ) |
| 3251 |
{ |
| 3252 |
file_error( fileptr, 0, "Repositioning after EOF" ) ; |
| 3253 |
return 0; |
| 3254 |
} |
| 3255 |
} |
| 3256 |
} |
| 3257 |
#endif |
| 3258 |
|
| 3259 |
/* |
| 3260 |
* Then do the actual positioning. Remember to clear errno first. |
| 3261 |
* Previously, this code tested afterwards to see if ftell() |
| 3262 |
* returned the same position that fseek() tried to set. Surely, that |
| 3263 |
* must be unnecessary? |
| 3264 |
* We need to reposition using both the read and write postions (if |
| 3265 |
* required). |
| 3266 |
*/ |
| 3267 |
errno = 0 ; |
| 3268 |
/* |
| 3269 |
* Position the real file pointer to the write or read pointers |
| 3270 |
* calculated. The "thispos" member is set to the last seek |
| 3271 |
* executed. READ is done last as this is probably the most |
| 3272 |
* likely use of character positioning, hence it may be slightly |
| 3273 |
* more efficient. |
| 3274 |
*/ |
| 3275 |
if ( oper & OPER_WRITE ) |
| 3276 |
{ |
| 3277 |
if ( rx_fseek(fileptr->fileptr,(where_write-1),SEEK_SET ) ) |
| 3278 |
{ |
| 3279 |
file_error( fileptr, errno, NULL ) ; |
| 3280 |
return 0; |
| 3281 |
} |
| 3282 |
fileptr->thispos = where_write ; /* this was where-1; is that correct ?*/ |
| 3283 |
} |
| 3284 |
if ( oper & OPER_READ ) |
| 3285 |
{ |
| 3286 |
if ( rx_fseek(fileptr->fileptr,(where_read-1),SEEK_SET) ) |
| 3287 |
{ |
| 3288 |
file_error( fileptr, errno, NULL ) ; |
| 3289 |
return 0; |
| 3290 |
} |
| 3291 |
fileptr->thispos = where_read ; /* this was where-1; is that correct ?*/ |
| 3292 |
} |
| 3293 |
fileptr->oper = OPER_NONE; |
| 3294 |
|
| 3295 |
/* |
| 3296 |
* Then we have to update the file pointers in the entry in our |
| 3297 |
* file table. |
| 3298 |
* |
| 3299 |
* Clear the end-of-file flag. Even if we *did* position to the |
| 3300 |
* end of file, we don't want to discover that until we actually |
| 3301 |
* _read_ data that is _off_ the end-of-file. |
| 3302 |
*/ |
| 3303 |
|
| 3304 |
if (oper & OPER_READ) |
| 3305 |
{ |
| 3306 |
fileptr->readpos = where_read-1 ; |
| 3307 |
fileptr->flag &= ~(FLAG_RDEOF) ; |
| 3308 |
fileptr->flag &= ~(FLAG_AFTER_RDEOF) ; |
| 3309 |
} |
| 3310 |
if (oper & OPER_WRITE) |
| 3311 |
{ |
| 3312 |
fileptr->writepos = where_write-1 ; |
| 3313 |
fileptr->flag &= ~(FLAG_WREOF) ; |
| 3314 |
} |
| 3315 |
if (oper == OPER_NONE) |
| 3316 |
file_error( fileptr, 0, NULL ) ; |
| 3317 |
|
| 3318 |
/* |
| 3319 |
* We have moved the file pointer by a number of characters which |
| 3320 |
* may have spanned any number of lines. So we have no idea which |
| 3321 |
* line we are in the file, so we need to invalidate the |
| 3322 |
* read or write line position. |
| 3323 |
*/ |
| 3324 |
if (oper & OPER_READ) |
| 3325 |
fileptr->readline = 0 ; |
| 3326 |
if (oper & OPER_WRITE) |
| 3327 |
fileptr->writeline = 0 ; |
| 3328 |
|
| 3329 |
/* |
| 3330 |
* Return the new position of the file pointer. If both file |
| 3331 |
* pointers were set, then readpos and writepos are the same, so |
| 3332 |
* the following test is valid. |
| 3333 |
*/ |
| 3334 |
if (oper & OPER_READ) |
| 3335 |
return fileptr->readpos + 1; /* external representation */ |
| 3336 |
else |
| 3337 |
return fileptr->writepos + 1; /* external representation */ |
| 3338 |
} |
| 3339 |
|
| 3340 |
|
| 3341 |
|
| 3342 |
/* |
| 3343 |
* This routine reads a string of data from a file indicated by |
| 3344 |
* the Rexx file table entry 'ptr'. The read starts at the current |
| 3345 |
* read position, and the length will be 'length' characters. |
| 3346 |
* |
| 3347 |
* Then, what if the data to be read are more than what is possible |
| 3348 |
* to store in one string; let's say length=100,000, and the size of |
| 3349 |
* length in a string is 16 bit. Well, That should return an error |
| 3350 |
* in Str_makeTSD(), but maybe we should handle it more elegantly? |
| 3351 |
* |
| 3352 |
* No file_error() is thrown if noerrors is set. |
| 3353 |
*/ |
| 3354 |
static streng *readbytes( tsd_t *TSD, fileboxptr fileptr, size_t length, int noerrors ) |
| 3355 |
{ |
| 3356 |
size_t didread=0 ; |
| 3357 |
streng *retvalue=NULL ; |
| 3358 |
|
| 3359 |
/* |
| 3360 |
* If state is ERROR, then refuse to handle the file further. |
| 3361 |
* If the state was 'only' EOF, then don't bother, the length of |
| 3362 |
* the file might have increased since last attempt to read. |
| 3363 |
*/ |
| 3364 |
if (fileptr->flag & FLAG_ERROR) |
| 3365 |
{ |
| 3366 |
if (!noerrors && !(fileptr->flag & FLAG_FAKE)) |
| 3367 |
file_error( fileptr, 0, NULL ) ; |
| 3368 |
return nullstringptr() ; |
| 3369 |
} |
| 3370 |
|
| 3371 |
assert( fileptr->flag & FLAG_READ ) ; |
| 3372 |
|
| 3373 |
/* |
| 3374 |
* If we are not at the current read position, we have to |
| 3375 |
* seek to the correct position, but first we have to the validity |
| 3376 |
* of these positions. |
| 3377 |
*/ |
| 3378 |
if (fileptr->flag & FLAG_PERSIST) |
| 3379 |
{ |
| 3380 |
if (fileptr->thispos != fileptr->readpos) |
| 3381 |
{ |
| 3382 |
errno = 0 ; |
| 3383 |
if ( fileptr->flag & FLAG_PERSIST |
| 3384 |
&& rx_fseek(fileptr->fileptr, fileptr->readpos, SEEK_SET )) |
| 3385 |
{ |
| 3386 |
if (!noerrors) |
| 3387 |
file_error( fileptr, errno, NULL ) ; |
| 3388 |
return nullstringptr() ; |
| 3389 |
} |
| 3390 |
fileptr->thispos = fileptr->readpos ; |
| 3391 |
fileptr->oper = OPER_NONE ; |
| 3392 |
} |
| 3393 |
} |
| 3394 |
|
| 3395 |
/* |
| 3396 |
* The joy of POSIX ... If a file is open for input and output, it |
| 3397 |
* must be flushed when changing between the two. Therefore, check |
| 3398 |
* the type of the last operation. Actually, this are not very likely |
| 3399 |
* since that situation would in general have been handled above. |
| 3400 |
*/ |
| 3401 |
if (fileptr->oper==OPER_WRITE) |
| 3402 |
{ |
| 3403 |
errno = 0 ; |
| 3404 |
if ( fileptr->flag & FLAG_PERSIST |
| 3405 |
&& rx_fseek( fileptr->fileptr, 0L, SEEK_CUR )) |
| 3406 |
{ |
| 3407 |
/* Hey, how could this have happened?!?! NFS down? */ |
| 3408 |
if (!noerrors) |
| 3409 |
file_error( fileptr, errno, NULL ) ; |
| 3410 |
return nullstringptr() ; |
| 3411 |
} |
| 3412 |
fileptr->oper = OPER_NONE ; |
| 3413 |
} |
| 3414 |
|
| 3415 |
/* |
| 3416 |
* Lets get ready for the big event. First allocate enough space to |
| 3417 |
* hold the data we are hoping to be able to read. Then read it |
| 3418 |
* directly into the string. |
| 3419 |
*/ |
| 3420 |
retvalue = Str_makeTSD(length+1) ; |
| 3421 |
errno = 0 ; |
| 3422 |
didread = fread( retvalue->value, 1, length, fileptr->fileptr ) ; |
| 3423 |
fileptr->oper = OPER_READ; |
| 3424 |
|
| 3425 |
/* |
| 3426 |
* Variable 'read' contains the number of items (=bytes) read, or |
| 3427 |
* it contains EOF if an error occurred. Handle the error the |
| 3428 |
* normal way; i.e. trigger file_error and return nothing. |
| 3429 |
*/ |
| 3430 |
if (didread==EOF) |
| 3431 |
{ |
| 3432 |
if (!noerrors) |
| 3433 |
file_error( fileptr, errno, NULL ) ; |
| 3434 |
return nullstringptr() ; |
| 3435 |
} |
| 3436 |
|
| 3437 |
/* |
| 3438 |
* What if we didn't manage to read all the data? Well, return what |
| 3439 |
* we got, but still trigger an error, since EOF should be |
| 3440 |
* considered a NOTREADY condition. However, we try to handle EOF |
| 3441 |
* a bit more elegantly than other errors, since lots of programmers |
| 3442 |
* are probably not bothering about EOF; an EOF condition should be |
| 3443 |
* able to be reset using a file positioning. |
| 3444 |
*/ |
| 3445 |
assert( 0<=didread && didread<=length ) ; /* It'd better be! */ |
| 3446 |
retvalue->len = didread ; |
| 3447 |
if (didread<length) |
| 3448 |
{ |
| 3449 |
if (!noerrors) |
| 3450 |
file_warning( fileptr, 0, "EOF on char input" ) ; |
| 3451 |
fileptr->flag |= FLAG_RDEOF ; |
| 3452 |
} |
| 3453 |
else |
| 3454 |
{ |
| 3455 |
fileptr->flag &= ~FLAG_RDEOF ; |
| 3456 |
fileptr->flag &= ~FLAG_AFTER_RDEOF ; |
| 3457 |
} |
| 3458 |
|
| 3459 |
/* |
| 3460 |
* Then, at the end, we have to set the pointers and counter to |
| 3461 |
* the correct values |
| 3462 |
*/ |
| 3463 |
fileptr->thispos += didread ; |
| 3464 |
fileptr->readpos += didread ; |
| 3465 |
fileptr->readline = (-1) ; |
| 3466 |
fileptr->linesleft = 0 ; |
| 3467 |
|
| 3468 |
return retvalue ; |
| 3469 |
} |
| 3470 |
|
| 3471 |
|
| 3472 |
|
| 3473 |
/* |
| 3474 |
* This routines write a string to a file pointed to by the Rexx file |
| 3475 |
* table entry 'fileptr'. The string to be written is 'string', and the |
| 3476 |
* length of the write is implicitly given as the length of 'string' |
| 3477 |
* |
| 3478 |
* This routine is called from the Rexx built-in function CHAROUT(). |
| 3479 |
* It is a fairly straight forward implementation. |
| 3480 |
* |
| 3481 |
* No file_error() is thrown if noerrors is set. |
| 3482 |
*/ |
| 3483 |
static size_t writebytes( tsd_t *TSD, fileboxptr fileptr, const streng *string, int noerrors ) |
| 3484 |
{ |
| 3485 |
#ifdef WIN32 |
| 3486 |
int rc=0; |
| 3487 |
#endif |
| 3488 |
long todo, done, written=0 ; |
| 3489 |
const char *buf ; |
| 3490 |
|
| 3491 |
/* |
| 3492 |
* First, if this file is in state ERROR, don't touch it, what to |
| 3493 |
* return depends on whether the file is in state FAKE. |
| 3494 |
*/ |
| 3495 |
if ( fileptr->flag & FLAG_ERROR ) |
| 3496 |
{ |
| 3497 |
if ( fileptr->flag & FLAG_FAKE ) |
| 3498 |
return string->len ; |
| 3499 |
else |
| 3500 |
{ |
| 3501 |
if (!noerrors) |
| 3502 |
file_error( fileptr, 0, NULL ) ; |
| 3503 |
if (fileptr->flag & FLAG_FAKE) |
| 3504 |
return string->len ; |
| 3505 |
|
| 3506 |
return 0 ; |
| 3507 |
} |
| 3508 |
} |
| 3509 |
/* |
| 3510 |
* If we are not at the current write position, we have to |
| 3511 |
* seek to the correct position |
| 3512 |
*/ |
| 3513 |
if (fileptr->thispos != fileptr->writepos) |
| 3514 |
{ |
| 3515 |
errno = 0 ; |
| 3516 |
if ( fileptr->flag & FLAG_PERSIST |
| 3517 |
&& rx_fseek(fileptr->fileptr, fileptr->writepos, SEEK_SET )) |
| 3518 |
{ |
| 3519 |
if (!noerrors) |
| 3520 |
file_error( fileptr, errno, NULL ) ; |
| 3521 |
return 0 ; |
| 3522 |
} |
| 3523 |
fileptr->thispos = fileptr->writepos ; |
| 3524 |
fileptr->oper = OPER_NONE ; |
| 3525 |
} |
| 3526 |
|
| 3527 |
/* |
| 3528 |
* If previous operation on this file was a read, we have to flush |
| 3529 |
* the file before we can perform any write operations. This will |
| 3530 |
* seldom happen, since it is in general handled above. |
| 3531 |
*/ |
| 3532 |
if (fileptr->oper == OPER_READ) |
| 3533 |
{ |
| 3534 |
errno = 0 ; |
| 3535 |
if ( fileptr->flag & FLAG_PERSIST |
| 3536 |
&& rx_fseek(fileptr->fileptr, 0, SEEK_CUR)) |
| 3537 |
{ |
| 3538 |
if (!noerrors) |
| 3539 |
file_error( fileptr, errno, NULL ) ; |
| 3540 |
return (fileptr->flag & FLAG_FAKE) ? string->len : 0 ; |
| 3541 |
} |
| 3542 |
fileptr->oper = OPER_NONE ; |
| 3543 |
} |
| 3544 |
|
| 3545 |
/* |
| 3546 |
* Here comes the actual writing. This also works when the length |
| 3547 |
* of string is zero. |
| 3548 |
*/ |
| 3549 |
errno = 0 ; |
| 3550 |
buf = string->value ; |
| 3551 |
todo = string->len ; |
| 3552 |
fileptr->oper = OPER_WRITE ; |
| 3553 |
do |
| 3554 |
{ |
| 3555 |
#ifdef WIN32 |
| 3556 |
done = fwrite( buf, 1, todo, fileptr->fileptr ) ; |
| 3557 |
#else |
| 3558 |
done = fwrite( buf, 1, todo, fileptr->fileptr ) ; |
| 3559 |
#endif |
| 3560 |
/* |
| 3561 |
* Win32 has a bug with fwrite and disk full. If the size of the |
| 3562 |
* chunk to write is < 4096 and the disk fills up, then you don't get |
| 3563 |
* an error indication. So flush the stream if the size of data is |
| 3564 |
* < 4096 and test the result of fflush(). Bug 731664 |
| 3565 |
*/ |
| 3566 |
#ifdef WIN32 |
| 3567 |
if (string->len < 4096 ) |
| 3568 |
rc = fflush( fileptr->fileptr ); |
| 3569 |
if (done < 0 || rc != 0 ) |
| 3570 |
#else |
| 3571 |
if (done < 0) |
| 3572 |
#endif |
| 3573 |
{ |
| 3574 |
written = -1 ; |
| 3575 |
break ; |
| 3576 |
} else if (done == 0) |
| 3577 |
break; |
| 3578 |
else |
| 3579 |
written += done ; |
| 3580 |
buf += done ; |
| 3581 |
todo -= done ; |
| 3582 |
} while ( ( todo > 0 ) && noerrors ) ; |
| 3583 |
|
| 3584 |
/* |
| 3585 |
* Here comes the error checking. Note that this function will |
| 3586 |
* return the number of elements written, it will never return |
| 3587 |
* EOF as fread can, since the problems surrounding EOF can not |
| 3588 |
* occur in this operation. Therefore, report a fullfleged error |
| 3589 |
* whenever rc is less than the length of string. |
| 3590 |
*/ |
| 3591 |
assert( 0<=written && written<=string->len ) ; |
| 3592 |
if (written < string->len ) |
| 3593 |
{ |
| 3594 |
if (!noerrors) |
| 3595 |
file_error( fileptr, errno, NULL ) ; |
| 3596 |
} |
| 3597 |
else |
| 3598 |
{ |
| 3599 |
/* |
| 3600 |
* If the operation was successful, then we set misc status |
| 3601 |
* information about the file, and the counters and pointers. |
| 3602 |
*/ |
| 3603 |
fileptr->writeline = 0 ; |
| 3604 |
fileptr->flag &= ~FLAG_RDEOF ; |
| 3605 |
fileptr->flag &= ~FLAG_AFTER_RDEOF ; |
| 3606 |
fileptr->thispos += written ; |
| 3607 |
fileptr->writepos += written ; |
| 3608 |
|
| 3609 |
fflush( fileptr->fileptr ) ; |
| 3610 |
fileptr->oper = OPER_NONE; |
| 3611 |
} |
| 3612 |
|
| 3613 |
return written ; |
| 3614 |
} |
| 3615 |
|
| 3616 |
|
| 3617 |
/* |
| 3618 |
* This routine calculates the number of bytes remaining in the file, |
| 3619 |
* i.e the number of bytes from the current read position until the |
| 3620 |
* end-of-file. It is, of course, called from the Rexx built-in |
| 3621 |
* function CHARS() |
| 3622 |
*/ |
| 3623 |
|
| 3624 |
static rx_64 calc_chars_left( tsd_t *TSD, fileboxptr ptr ) |
| 3625 |
{ |
| 3626 |
rx_64 left=0 ; |
| 3627 |
rx_64 oldpoint=0L, newpoint=0L ; |
| 3628 |
|
| 3629 |
if (!(ptr->flag & FLAG_READ)) |
| 3630 |
return 0 ; |
| 3631 |
|
| 3632 |
/* |
| 3633 |
* First, determine whether this file is in ERROR state. If so, we |
| 3634 |
* don't want to touch it. Whether or not the file is in FAKE state |
| 3635 |
* is fairly irrelevant in this situation |
| 3636 |
*/ |
| 3637 |
if ( ptr->flag & FLAG_ERROR ) |
| 3638 |
{ |
| 3639 |
if (!(ptr->flag & FLAG_FAKE)) |
| 3640 |
file_error( ptr, 0, NULL ) ; |
| 3641 |
return 0 ; |
| 3642 |
} |
| 3643 |
|
| 3644 |
/* |
| 3645 |
* If this is not a persistent file, then we have no means of finding |
| 3646 |
* out how much of the file is available. Then, return 1 if we are not |
| 3647 |
* at the end-of-file, and 0 otherwise. |
| 3648 |
*/ |
| 3649 |
if (!(ptr->flag & FLAG_PERSIST)) |
| 3650 |
{ |
| 3651 |
#if 0 |
| 3652 |
left = ( !(ptr->flag & FLAG_RDEOF)) ; |
| 3653 |
#else |
| 3654 |
struct rx_stat_buf finfo; |
| 3655 |
int fno; |
| 3656 |
|
| 3657 |
fno = fileno( ptr->fileptr ) ; |
| 3658 |
rx_fstat( fno, &finfo ); |
| 3659 |
left = finfo.st_size; |
| 3660 |
#endif |
| 3661 |
} |
| 3662 |
else |
| 3663 |
{ |
| 3664 |
/* |
| 3665 |
* This is a persistent file, which is not in error state. OK, then |
| 3666 |
* we must record the current point, fseek to the end-of-file, |
| 3667 |
* ftell to get that position, and fseek back to where we started. |
| 3668 |
* And we have to check for errors everywhere ... sigh. |
| 3669 |
* |
| 3670 |
* First, record the current position in the file. |
| 3671 |
*/ |
| 3672 |
errno = 0 ; |
| 3673 |
oldpoint = rx_ftell( ptr->fileptr ) ; |
| 3674 |
if (oldpoint==EOF) |
| 3675 |
{ |
| 3676 |
file_error( ptr, errno, NULL ) ; |
| 3677 |
return 0 ; |
| 3678 |
} |
| 3679 |
|
| 3680 |
/* |
| 3681 |
* Then, move the current position to the end-of-file |
| 3682 |
*/ |
| 3683 |
errno = 0 ; |
| 3684 |
if (rx_fseek(ptr->fileptr, 0L, SEEK_END)) |
| 3685 |
{ |
| 3686 |
file_error( ptr, errno, NULL ) ; |
| 3687 |
return 0 ; |
| 3688 |
} |
| 3689 |
ptr->oper = OPER_NONE; |
| 3690 |
|
| 3691 |
/* |
| 3692 |
* And record the position of the end-of-file |
| 3693 |
*/ |
| 3694 |
errno = 0 ; |
| 3695 |
newpoint = rx_ftell( ptr->fileptr ) ; |
| 3696 |
if (newpoint==EOF) |
| 3697 |
{ |
| 3698 |
file_error( ptr, errno, NULL ) ; |
| 3699 |
return 0 ; |
| 3700 |
} |
| 3701 |
|
| 3702 |
/* |
| 3703 |
* And, at last, position back to the place where we started. |
| 3704 |
* Actually, this may not be necessary, since we _can_ leave the |
| 3705 |
* current position at the end-of-file. After all, the next read |
| 3706 |
* or write _will_ position back correctly. However, let's be |
| 3707 |
* nice ... |
| 3708 |
*/ |
| 3709 |
errno = 0 ; |
| 3710 |
if (rx_fseek(ptr->fileptr, oldpoint, SEEK_SET)) |
| 3711 |
{ |
| 3712 |
file_error( ptr, errno, NULL ) ; |
| 3713 |
return 0 ; |
| 3714 |
} |
| 3715 |
|
| 3716 |
/* |
| 3717 |
* Then we have some accounting to do; calculate the size of the |
| 3718 |
* last part of the file. And also set oper to NONE, we _have_ |
| 3719 |
* done a repositioning ... actually, several :-) |
| 3720 |
*/ |
| 3721 |
left = newpoint - ptr->readpos ; |
| 3722 |
/* left = newpoint - oldpoint ; */ /* YURI - wrong */ |
| 3723 |
ptr->oper = OPER_NONE ; |
| 3724 |
} |
| 3725 |
|
| 3726 |
return left ; |
| 3727 |
} |
| 3728 |
|
| 3729 |
|
| 3730 |
/* |
| 3731 |
* This routine counts the complete lines remaining in the file |
| 3732 |
* pointed to by the Rexx file table entry 'ptr'. The count starts |
| 3733 |
* at the current read or write position, and the current line will be counted |
| 3734 |
* even if the current read position points to the middle of a line. |
| 3735 |
* The last line will only be counted if it was actually terminated |
| 3736 |
* by a EOL marker. If the current line is the last line, but it was |
| 3737 |
* not explicitly terminated by a EOL marker, zero is returned. |
| 3738 |
*/ |
| 3739 |
static rx_64 countlines( tsd_t *TSD, fileboxptr ptr, int actual, int oper ) |
| 3740 |
{ |
| 3741 |
long oldpoint=0L ; |
| 3742 |
int left=0, ch=0; |
| 3743 |
int prevch=-1 ; |
| 3744 |
|
| 3745 |
/* |
| 3746 |
* If this file is in ERROR state, we really don't want to try to |
| 3747 |
* operate on it. Just report an error, and return 0. |
| 3748 |
*/ |
| 3749 |
if ( ptr->flag & FLAG_ERROR ) |
| 3750 |
{ |
| 3751 |
if (!(ptr->flag & FLAG_FAKE)) |
| 3752 |
file_error( ptr, 0, NULL ) ; |
| 3753 |
return 0 ; |
| 3754 |
} |
| 3755 |
|
| 3756 |
/* |
| 3757 |
* Counting lines requires us to reposition in the file. However, |
| 3758 |
* we can not reposition in transient files. If this is not a |
| 3759 |
* persistent file, don't do any repositioning, just return one |
| 3760 |
* for any situation where we are not sure whether there are more |
| 3761 |
* data or not (i.e. unless we are sure that there are no more data, |
| 3762 |
* return "1" |
| 3763 |
*/ |
| 3764 |
if (!(ptr->flag & FLAG_PERSIST)) |
| 3765 |
return (!feof(ptr->fileptr)) ; |
| 3766 |
else if (!actual) |
| 3767 |
{ |
| 3768 |
/* |
| 3769 |
* We just want to know of there are any lines left for a persistent |
| 3770 |
* file. First check if we have reached EOF. Return 0 if so. |
| 3771 |
* Next check if the file size is 0; if so then return 0, otherwise |
| 3772 |
* there is at least 1 line left; return 1 |
| 3773 |
*/ |
| 3774 |
if ( feof(ptr->fileptr ) ) |
| 3775 |
return 0; |
| 3776 |
else |
| 3777 |
{ |
| 3778 |
struct rx_stat_buf buffer ; |
| 3779 |
int fno; |
| 3780 |
memset( &buffer, 0, sizeof(buffer) ); |
| 3781 |
fno = fileno( ptr->fileptr ) ; |
| 3782 |
rx_fstat( fno, &buffer ) ; |
| 3783 |
return ((buffer.st_size==0) ? 0 : 1); |
| 3784 |
} |
| 3785 |
} |
| 3786 |
else |
| 3787 |
{ |
| 3788 |
/* |
| 3789 |
* Take advantage of the cached value of the lines left in the |
| 3790 |
* file |
| 3791 |
*/ |
| 3792 |
if (ptr->linesleft) |
| 3793 |
return ptr->linesleft ; |
| 3794 |
|
| 3795 |
/* |
| 3796 |
* If, however, this is a persistent file, we have to read from |
| 3797 |
* the current read position to the end-of-file, and count all |
| 3798 |
* the lines. First, make sure that we position at the current |
| 3799 |
* read position. |
| 3800 |
*/ |
| 3801 |
errno = 0 ; |
| 3802 |
oldpoint = rx_ftell( ptr->fileptr ) ; |
| 3803 |
if (oldpoint==EOF) |
| 3804 |
{ |
| 3805 |
file_error( ptr, errno, NULL ) ; |
| 3806 |
return 0 ; |
| 3807 |
} |
| 3808 |
|
| 3809 |
/* |
| 3810 |
* Then read the rest of the file, and keep a count of all the files |
| 3811 |
* read in the process. |
| 3812 |
*/ |
| 3813 |
SWITCH_OPER_READ(ptr); |
| 3814 |
/* |
| 3815 |
* Switch to the current read or write pos setting |
| 3816 |
*/ |
| 3817 |
if ( oper == OPER_READ ) |
| 3818 |
ptr->thispos = ptr->readpos; |
| 3819 |
else |
| 3820 |
ptr->thispos = ptr->writepos; |
| 3821 |
rx_fseek(ptr->fileptr,ptr->thispos,SEEK_SET); |
| 3822 |
#if defined(UNIX) || defined(MAC) |
| 3823 |
for(left=0;((ch=getc(ptr->fileptr))!=EOF);) |
| 3824 |
{ |
| 3825 |
if (ch==REGINA_EOL) |
| 3826 |
left++ ; |
| 3827 |
prevch = ch; |
| 3828 |
} |
| 3829 |
if (prevch != REGINA_EOL |
| 3830 |
&& prevch != -1) |
| 3831 |
left++; |
| 3832 |
#else |
| 3833 |
for(left=0;;) |
| 3834 |
{ |
| 3835 |
ch = getc(ptr->fileptr); |
| 3836 |
if (ch == EOF) |
| 3837 |
break; |
| 3838 |
if ( ch == REGINA_CR) |
| 3839 |
left++ ; |
| 3840 |
else |
| 3841 |
{ |
| 3842 |
if ( ch == REGINA_EOL && prevch != REGINA_CR) |
| 3843 |
left++ ; |
| 3844 |
} |
| 3845 |
prevch = ch; |
| 3846 |
} |
| 3847 |
if (prevch != REGINA_EOL |
| 3848 |
&& prevch != REGINA_CR |
| 3849 |
&& prevch != -1) |
| 3850 |
left++; |
| 3851 |
#endif |
| 3852 |
|
| 3853 |
/* |
| 3854 |
* At the end, try to reposition back to the old current read |
| 3855 |
* position, and report an error if that attempt failed. |
| 3856 |
*/ |
| 3857 |
errno = 0 ; |
| 3858 |
if ( ptr->flag & FLAG_PERSIST |
| 3859 |
&& rx_fseek(ptr->fileptr, oldpoint, SEEK_SET)) |
| 3860 |
{ |
| 3861 |
file_error( ptr, errno, NULL ) ; |
| 3862 |
return 0 ; |
| 3863 |
} |
| 3864 |
ptr->oper = OPER_NONE; |
| 3865 |
ptr->linesleft = left ; |
| 3866 |
} |
| 3867 |
return left ; |
| 3868 |
} |
| 3869 |
|
| 3870 |
|
| 3871 |
|
| 3872 |
/* |
| 3873 |
* This routine writes a line to the file indicated by 'ptr'. The line |
| 3874 |
* to be written is 'data', and it will be terminated by an extra |
| 3875 |
* EOL marker after the charactrers in 'data'. |
| 3876 |
*/ |
| 3877 |
static int writeoneline( tsd_t *TSD, fileboxptr ptr, const streng *data ) |
| 3878 |
{ |
| 3879 |
const char *i=NULL ; |
| 3880 |
int num_eol_chars=0; |
| 3881 |
|
| 3882 |
/* |
| 3883 |
* First, make sure that the file is not in ERROR state. If it is |
| 3884 |
* report an error, and return a result depending on whether this |
| 3885 |
* file is to be faked. |
| 3886 |
*/ |
| 3887 |
if (ptr->flag & FLAG_ERROR) |
| 3888 |
{ |
| 3889 |
if (ptr->flag & FLAG_FAKE) |
| 3890 |
return 0 ; |
| 3891 |
else |
| 3892 |
{ |
| 3893 |
file_error( ptr, 0, NULL ) ; |
| 3894 |
if (ptr->flag & FLAG_FAKE) |
| 3895 |
return 0 ; |
| 3896 |
return 1 ; |
| 3897 |
} |
| 3898 |
} |
| 3899 |
|
| 3900 |
/* |
| 3901 |
* If we are to write a new line, we ought to truncate the file after |
| 3902 |
* that line. Or rather, we truncate the file at the start of the |
| 3903 |
* new line, before we write it out. But only if we have the non-POSIX |
| 3904 |
* function ftruncate() available. And not if we are already there. |
| 3905 |
*/ |
| 3906 |
#if defined(HAVE_FTRUNCATE) |
| 3907 |
if ( get_options_flag( TSD->currlevel, EXT_LINEOUTTRUNC ) ) |
| 3908 |
{ |
| 3909 |
if (ptr->oper != OPER_WRITE && !(ptr->flag & (FLAG_WREOF)) && |
| 3910 |
(ptr->flag & FLAG_PERSIST)) |
| 3911 |
{ |
| 3912 |
int fno ; |
| 3913 |
errno = 0 ; |
| 3914 |
SWITCH_OPER_WRITE(ptr); /* Maybe, ftruncate is a write operation in |
| 3915 |
* the meaning of POSIX. This shouldn't do |
| 3916 |
* any harm in other systems. |
| 3917 |
*/ |
| 3918 |
|
| 3919 |
fno = fileno( ptr->fileptr ) ; |
| 3920 |
if (ftruncate( fno, ptr->writepos) == -1) |
| 3921 |
{ |
| 3922 |
file_error( ptr, errno, NULL ) ; |
| 3923 |
return !(ptr->flag & FLAG_FAKE) ; |
| 3924 |
} |
| 3925 |
if ( ptr->flag & FLAG_PERSIST ) |
| 3926 |
rx_fseek( ptr->fileptr, 0, SEEK_END ) ; |
| 3927 |
ptr->oper = OPER_NONE; |
| 3928 |
ptr->thispos = ptr->writepos = rx_ftell( ptr->fileptr ) ; |
| 3929 |
if (ptr->readpos>ptr->thispos && ptr->readpos!= (size_t) EOF) |
| 3930 |
{ |
| 3931 |
ptr->readpos = ptr->thispos ; |
| 3932 |
ptr->readline = 0 ; |
| 3933 |
ptr->linesleft = 0 ; |
| 3934 |
} |
| 3935 |
} |
| 3936 |
} |
| 3937 |
#endif |
| 3938 |
|
| 3939 |
/* |
| 3940 |
* Then, output the characters in 'data', and sense any problem. |
| 3941 |
* If there is a problem, report an error |
| 3942 |
*/ |
| 3943 |
errno = 0 ; |
| 3944 |
SWITCH_OPER_WRITE(ptr); |
| 3945 |
for (i=data->value; i<Str_end(data); i++) |
| 3946 |
{ |
| 3947 |
if (putc( *i, ptr->fileptr)==EOF) |
| 3948 |
{ |
| 3949 |
file_error( ptr, errno, NULL ) ; |
| 3950 |
return 1 ; |
| 3951 |
} |
| 3952 |
} |
| 3953 |
|
| 3954 |
/* |
| 3955 |
* After all the data has been written out, we have to explicitly |
| 3956 |
* terminate the file with an end-of-line marker. Under Unix this |
| 3957 |
* is the single character EOL. Under Macintosh this is the single |
| 3958 |
* character CR, and all others it is CR and EOL. |
| 3959 |
*/ |
| 3960 |
#if !defined(UNIX) |
| 3961 |
SWITCH_OPER_WRITE(ptr); |
| 3962 |
if (putc( REGINA_CR, ptr->fileptr)==EOF) |
| 3963 |
{ |
| 3964 |
file_error( ptr, errno, NULL ) ; |
| 3965 |
return 1 ; |
| 3966 |
} |
| 3967 |
num_eol_chars++; |
| 3968 |
#endif |
| 3969 |
#if !defined(MAC) |
| 3970 |
SWITCH_OPER_WRITE(ptr); |
| 3971 |
if (putc( REGINA_EOL, ptr->fileptr)==EOF) |
| 3972 |
{ |
| 3973 |
file_error( ptr, errno, NULL ) ; |
| 3974 |
return 1 ; |
| 3975 |
} |
| 3976 |
num_eol_chars++; |
| 3977 |
#endif |
| 3978 |
|
| 3979 |
/* |
| 3980 |
* Then we have to update the counters and pointers in the Rexx |
| 3981 |
* file table entry. We must do that in order to be able to keep |
| 3982 |
* track of where we are. |
| 3983 |
*/ |
| 3984 |
ptr->thispos += data->len + num_eol_chars ; /* fix 736578 */ |
| 3985 |
ptr->writepos = ptr->thispos ; |
| 3986 |
ptr->oper = OPER_WRITE ; |
| 3987 |
|
| 3988 |
/* |
| 3989 |
* FIXME - under what circumstances will writeline be 0 ? |
| 3990 |
* If it hasn't been determined by what calls this function, the |
| 3991 |
* a) the calling function(s) should determine the line number, or |
| 3992 |
* b) the current line number should be determined here. |
| 3993 |
*/ |
| 3994 |
if (ptr->writeline) |
| 3995 |
ptr->writeline++ ; |
| 3996 |
|
| 3997 |
ptr->flag |= FLAG_WREOF ; |
| 3998 |
|
| 3999 |
/* |
| 4000 |
* At the end, we flush the data. We do this in order to avoid |
| 4001 |
* surprises later. Maybe we shouldn't do that, since it may force |
| 4002 |
* a systemcall, which might give away the timeslice and decrease |
| 4003 |
* system time. So you might want to remove this call ... at your |
| 4004 |
* own risk :-) |
| 4005 |
*/ |
| 4006 |
errno = 0 ; |
| 4007 |
if (fflush( ptr->fileptr )) |
| 4008 |
{ |
| 4009 |
file_error( ptr, errno, NULL ) ; |
| 4010 |
return 1 ; |
| 4011 |
} |
| 4012 |
|
| 4013 |
return 0 ; |
| 4014 |
} |
| 4015 |
|
| 4016 |
/* |
| 4017 |
* This routine is a way of retrieving the information returned by the |
| 4018 |
* standard Unix call stat(). It takes the name of a file as parameter, |
| 4019 |
* and return information about that file. This is not standard Rexx, |
| 4020 |
* but quite useful. It is accessed through the built-in function |
| 4021 |
* STREAM(), command 'FSTAT' |
| 4022 |
* This is now also used for the "standard" STREAM() options. |
| 4023 |
* *Persistent will be set to 1 if the stream's type is a file. The setting |
| 4024 |
* happens on success and if Persistent isn't NULL. |
| 4025 |
*/ |
| 4026 |
static streng *getstatus( tsd_t *TSD, const streng *filename , int subcommand ) |
| 4027 |
{ |
| 4028 |
fileboxptr ptr=NULL ; |
| 4029 |
int rc=0 ; |
| 4030 |
int fno=0 ; |
| 4031 |
rx_64 pos_read = -2L, pos_write = -2L, line_read = -2L, line_write = -2; |
| 4032 |
int streamtype = STREAMTYPE_UNKNOWN; |
| 4033 |
streng *result=NULL ; |
| 4034 |
struct rx_stat_buf buffer ; |
| 4035 |
struct tm tmdata, *tmptr ; |
| 4036 |
char *fn=NULL; |
| 4037 |
#if 0 |
| 4038 |
static const char *fmt = "%02d-%02d-%02d %02d:%02d:%02d" ; |
| 4039 |
static const char *iso = "%04d-%02d-%02d %02d:%02d:%02d" ; |
| 4040 |
#endif |
| 4041 |
static const char *streamdesc[] = { "UNKNOWN", "PERSISTENT", "TRANSIENT" }; |
| 4042 |
char tmppwd[50]; |
| 4043 |
char tmpgrp[50]; |
| 4044 |
char *ptmppwd=tmppwd,*ptmpgrp=tmpgrp; |
| 4045 |
#if !(defined(VMS) || defined(MAC) || defined(OS2) || defined(DOS) || (defined (__WATCOMC__) && !defined(__QNX__)) || defined(_MSC_VER) || (defined(WIN32) && defined(__IBMC__)) || defined(__MINGW32__) || defined(__BORLANDC__) || defined(__EPOC32__) || defined(__LCC__)) |
| 4046 |
struct passwd *ppwd; |
| 4047 |
struct group *pgrp; |
| 4048 |
#endif |
| 4049 |
|
| 4050 |
memset( &buffer, 0, sizeof(buffer) ); |
| 4051 |
/* |
| 4052 |
* Nul terminate the input filename string, as stat() will barf if |
| 4053 |
* it isn't and other functions stuff up! |
| 4054 |
*/ |
| 4055 |
fn = str_ofTSD(filename); |
| 4056 |
/* |
| 4057 |
* First get the Rexx file table entry associated with the file, |
| 4058 |
* and then call stat() for that file. If the file is already open, |
| 4059 |
* then call fstat, since that will in general be a 'safer' way |
| 4060 |
* to be sure that it is _really_ the file that is open in Rexx. |
| 4061 |
*/ |
| 4062 |
ptr = getfileptr( TSD, filename ) ; |
| 4063 |
if (ptr && ptr->fileptr) |
| 4064 |
{ |
| 4065 |
fno = fileno( ptr->fileptr ) ; |
| 4066 |
rc = rx_fstat( fno, &buffer ) ; |
| 4067 |
if (ptr->flag & FLAG_PERSIST) |
| 4068 |
streamtype = STREAMTYPE_PERSISTENT; |
| 4069 |
else |
| 4070 |
streamtype = STREAMTYPE_TRANSIENT; |
| 4071 |
pos_read = ptr->readpos; |
| 4072 |
pos_write = ptr->writepos; |
| 4073 |
line_read = ptr->readline; |
| 4074 |
line_write = ptr->writeline; |
| 4075 |
} |
| 4076 |
else |
| 4077 |
{ |
| 4078 |
/* |
| 4079 |
* To be consistent with other functions when determining persistence, |
| 4080 |
* we need to check for a "regular" file. Everything other than a |
| 4081 |
* "regular" file in transient. |
| 4082 |
* If we don't have S_ISREG macro, then revert to a simple check; if the |
| 4083 |
* stream is a directory it is transent; ugly! |
| 4084 |
*/ |
| 4085 |
rc = my_rx_stat( TSD, fn, &buffer ) ; |
| 4086 |
if ( rc != 0 ) |
| 4087 |
streamtype = STREAMTYPE_UNKNOWN; |
| 4088 |
else |
| 4089 |
{ |
| 4090 |
/* |
| 4091 |
* Resolves 802114. |
| 4092 |
*/ |
| 4093 |
streamtype = stream_types[determine_stream_type( buffer.st_mode )].streamtype; |
| 4094 |
} |
| 4095 |
} |
| 4096 |
|
| 4097 |
/* |
| 4098 |
* If we were able to retrieve any useful information, store it |
| 4099 |
* in a string of suitable length, and return that string. |
| 4100 |
* If the filename does not exist, always return an empty string. |
| 4101 |
*/ |
| 4102 |
if ( rc == -1 ) |
| 4103 |
{ |
| 4104 |
if ( fn ) |
| 4105 |
FreeTSD( fn ); |
| 4106 |
checkProperStreamName( TSD, NULL, (const char *) tmpstr_of( TSD, filename ), errno ); |
| 4107 |
return nullstringptr(); |
| 4108 |
} |
| 4109 |
switch ( subcommand ) |
| 4110 |
{ |
| 4111 |
case COMMAND_FSTAT: /* return information about symbolic link if a symbolic link */ |
| 4112 |
#ifdef HAVE_LSTAT |
| 4113 |
/* |
| 4114 |
* If we have lstat(), use it to gather details, this is the only |
| 4115 |
* way to determine if the file is a symlink. |
| 4116 |
*/ |
| 4117 |
lstat(fn, &buffer) ; |
| 4118 |
#endif |
| 4119 |
#if defined(VMS) || defined(MAC) || defined(OS2) || defined(DOS) || (defined (__WATCOMC__) && !defined(__QNX__)) || defined(_MSC_VER) || (defined(WIN32) && defined(__IBMC__)) || defined(__MINGW32__) || defined(__BORLANDC__) || defined(__EPOC32__) || defined(__LCC__) |
| 4120 |
ptmppwd = "USER"; |
| 4121 |
ptmpgrp = "GROUP"; |
| 4122 |
#else |
| 4123 |
ppwd = getpwuid( buffer.st_uid ); |
| 4124 |
if ( ppwd ) |
| 4125 |
ptmppwd = ppwd->pw_name; |
| 4126 |
else |
| 4127 |
sprintf( tmppwd, "%d", buffer.st_uid ); |
| 4128 |
|
| 4129 |
pgrp = getgrgid( buffer.st_gid ); |
| 4130 |
if ( pgrp ) |
| 4131 |
ptmpgrp = pgrp->gr_name; |
| 4132 |
else |
| 4133 |
sprintf( tmpgrp, "%d", buffer.st_gid ); |
| 4134 |
#endif |
| 4135 |
result = Str_makeTSD( 128 ) ; |
| 4136 |
#if defined(HAVE_I64U) |
| 4137 |
sprintf( result->value, |
| 4138 |
"%ld %ld %03o %d %s %s %I64u", |
| 4139 |
(long)(buffer.st_dev), (long)(buffer.st_ino), |
| 4140 |
buffer.st_mode & ACCESSPERMS, buffer.st_nlink, |
| 4141 |
ptmppwd, ptmpgrp, |
| 4142 |
buffer.st_size ) ; |
| 4143 |
#else |
| 4144 |
if ( sizeof(rx_64) > 4 ) |
| 4145 |
sprintf( result->value, |
| 4146 |
"%ld %ld %03o %d %s %s %lld", |
| 4147 |
(long)(buffer.st_dev), (long)(buffer.st_ino), |
| 4148 |
buffer.st_mode & ACCESSPERMS, buffer.st_nlink, |
| 4149 |
ptmppwd, ptmpgrp, |
| 4150 |
(rx_64)(buffer.st_size) ) ; |
| 4151 |
else |
| 4152 |
sprintf( result->value, |
| 4153 |
"%ld %ld %03o %d %s %s %ld", |
| 4154 |
(long)(buffer.st_dev), (long)(buffer.st_ino), |
| 4155 |
buffer.st_mode & ACCESSPERMS, buffer.st_nlink, |
| 4156 |
ptmppwd, ptmpgrp, |
| 4157 |
(long)(buffer.st_size) ) ; |
| 4158 |
#endif |
| 4159 |
/* |
| 4160 |
* Append the stream type name... |
| 4161 |
*/ |
| 4162 |
strcat( result->value, stream_types[determine_stream_type( buffer.st_mode )].streamname ); |
| 4163 |
break; |
| 4164 |
case COMMAND_QUERY_EXISTS: |
| 4165 |
if ( streamtype == STREAMTYPE_TRANSIENT ) |
| 4166 |
{ |
| 4167 |
result = nullstringptr(); |
| 4168 |
} |
| 4169 |
else |
| 4170 |
{ |
| 4171 |
result = Str_makeTSD( REXX_PATH_MAX ); |
| 4172 |
my_fullpath( TSD, result->value, fn ); |
| 4173 |
result->len = strlen( result->value ); |
| 4174 |
} |
| 4175 |
break; |
| 4176 |
case COMMAND_QUERY_SIZE: |
| 4177 |
if ( streamtype == STREAMTYPE_TRANSIENT ) |
| 4178 |
{ |
| 4179 |
result = nullstringptr() ; |
| 4180 |
} |
| 4181 |
else |
| 4182 |
{ |
| 4183 |
result = Str_makeTSD( 50 ) ; |
| 4184 |
#if defined(HAVE_I64U) |
| 4185 |
sprintf( result->value, "%I64u", buffer.st_size ) ; |
| 4186 |
#else |
| 4187 |
if ( sizeof(rx_64) > 4 ) |
| 4188 |
sprintf( result->value, "%lld", (rx_64)(buffer.st_size) ) ; |
| 4189 |
else |
| 4190 |
sprintf( result->value, "%ld", (long)(buffer.st_size) ) ; |
| 4191 |
#endif |
| 4192 |
} |
| 4193 |
break; |
| 4194 |
case COMMAND_QUERY_HANDLE: |
| 4195 |
if (fno) |
| 4196 |
{ |
| 4197 |
result = Str_makeTSD( 10 ) ; |
| 4198 |
sprintf( result->value, "%d", fno ) ; |
| 4199 |
} |
| 4200 |
else |
| 4201 |
result = nullstringptr() ; |
| 4202 |
break; |
| 4203 |
case COMMAND_QUERY_STREAMTYPE: |
| 4204 |
result = Str_makeTSD( 12 ) ; |
| 4205 |
sprintf( result->value, "%s", streamdesc[streamtype] ) ; |
| 4206 |
break; |
| 4207 |
case COMMAND_QUERY_DATETIME: |
| 4208 |
if ( streamtype == STREAMTYPE_TRANSIENT ) |
| 4209 |
{ |
| 4210 |
result = nullstringptr() ; |
| 4211 |
} |
| 4212 |
else |
| 4213 |
{ |
| 4214 |
time_t num64; |
| 4215 |
num64 = buffer.st_mtime; |
| 4216 |
if ( ( tmptr = localtime( &num64 ) ) != NULL ) |
| 4217 |
tmdata = *tmptr; |
| 4218 |
else |
| 4219 |
memset(&tmdata,0,sizeof(tmdata)); /* what shall we do in this case? */ |
| 4220 |
result = Str_makeTSD( 20 ) ; |
| 4221 |
#if 0 |
| 4222 |
sprintf( result->value, fmt, tmdata.tm_mon+1, tmdata.tm_mday, |
| 4223 |
(tmdata.tm_year % 100), tmdata.tm_hour, tmdata.tm_min, |
| 4224 |
tmdata.tm_sec ) ; |
| 4225 |
#else |
| 4226 |
strftime( result->value, 20, "%m-%d-%y %H:%M:%S", &tmdata ); |
| 4227 |
#endif |
| 4228 |
} |
| 4229 |
break; |
| 4230 |
case COMMAND_QUERY_TIMESTAMP: |
| 4231 |
if ( streamtype == STREAMTYPE_TRANSIENT ) |
| 4232 |
{ |
| 4233 |
result = nullstringptr() ; |
| 4234 |
} |
| 4235 |
else |
| 4236 |
{ |
| 4237 |
time_t num64; |
| 4238 |
num64 = buffer.st_mtime; |
| 4239 |
if ( ( tmptr = localtime( &num64 ) ) != NULL ) |
| 4240 |
tmdata = *tmptr; |
| 4241 |
else |
| 4242 |
memset( &tmdata, 0, sizeof(tmdata) ); /* what shall we do in this case? */ |
| 4243 |
result = Str_makeTSD( 20 ) ; |
| 4244 |
#if 0 |
| 4245 |
sprintf( result->value, iso, tmdata.tm_year+1900, tmdata.tm_mon+1, |
| 4246 |
tmdata.tm_mday, |
| 4247 |
tmdata.tm_hour, tmdata.tm_min, |
| 4248 |
tmdata.tm_sec ) ; |
| 4249 |
#else |
| 4250 |
strftime( result->value, 20, "%Y-%m-%d %H:%M:%S", &tmdata ); |
| 4251 |
#endif |
| 4252 |
} |
| 4253 |
break; |
| 4254 |
case COMMAND_QUERY_CREATETIME: |
| 4255 |
if ( streamtype == STREAMTYPE_TRANSIENT ) |
| 4256 |
{ |
| 4257 |
result = nullstringptr() ; |
| 4258 |
} |
| 4259 |
else |
| 4260 |
{ |
| 4261 |
result = Str_makeTSD( 20 ) ; |
| 4262 |
#if defined(HAVE_I64U) |
| 4263 |
sprintf( result->value, "%I64u", buffer.st_ctime ); |
| 4264 |
#else |
| 4265 |
if ( sizeof(off_t) > 4 ) |
| 4266 |
sprintf( result->value, "%lld", buffer.st_ctime ); |
| 4267 |
else |
| 4268 |
sprintf( result->value, "%ld", buffer.st_ctime); |
| 4269 |
#endif |
| 4270 |
} |
| 4271 |
break; |
| 4272 |
case COMMAND_QUERY_MODIFYTIME: |
| 4273 |
if ( streamtype == STREAMTYPE_TRANSIENT ) |
| 4274 |
{ |
| 4275 |
result = nullstringptr() ; |
| 4276 |
} |
| 4277 |
else |
| 4278 |
{ |
| 4279 |
result = Str_makeTSD( 20 ) ; |
| 4280 |
#if defined(HAVE_I64U) |
| 4281 |
sprintf( result->value, "%I64u", buffer.st_mtime ); |
| 4282 |
#else |
| 4283 |
if ( sizeof(off_t) > 4 ) |
| 4284 |
sprintf( result->value, "%lld", buffer.st_mtime ); |
| 4285 |
else |
| 4286 |
sprintf( result->value, "%ld", buffer.st_mtime); |
| 4287 |
#endif |
| 4288 |
} |
| 4289 |
break; |
| 4290 |
case COMMAND_QUERY_ACCESSTIME: |
| 4291 |
if ( streamtype == STREAMTYPE_TRANSIENT ) |
| 4292 |
{ |
| 4293 |
result = nullstringptr() ; |
| 4294 |
} |
| 4295 |
else |
| 4296 |
{ |
| 4297 |
result = Str_makeTSD( 20 ) ; |
| 4298 |
#if defined(HAVE_I64U) |
| 4299 |
sprintf( result->value, "%I64u", buffer.st_atime ); |
| 4300 |
#else |
| 4301 |
if ( sizeof(off_t) > 4 ) |
| 4302 |
sprintf( result->value, "%lld", buffer.st_atime ); |
| 4303 |
else |
| 4304 |
sprintf( result->value, "%ld", buffer.st_atime); |
| 4305 |
#endif |
| 4306 |
} |
| 4307 |
break; |
| 4308 |
case COMMAND_QUERY_POSITION_READ_CHAR: |
| 4309 |
case COMMAND_QUERY_POSITION_SYS: |
| 4310 |
if (pos_read != (-2)) |
| 4311 |
{ |
| 4312 |
result = Str_makeTSD( 50 ) ; |
| 4313 |
#if defined(HAVE_I64U) |
| 4314 |
sprintf( result->value, "%I64u", pos_read + 1 ); |
| 4315 |
#else |
| 4316 |
if ( sizeof(off_t) > 4 ) |
| 4317 |
sprintf( result->value, "%lld", pos_read + 1 ); |
| 4318 |
else |
| 4319 |
sprintf( result->value, "%ld", (long)(pos_read + 1) ); |
| 4320 |
#endif |
| 4321 |
} |
| 4322 |
else |
| 4323 |
result = nullstringptr() ; |
| 4324 |
break; |
| 4325 |
case COMMAND_QUERY_POSITION_WRITE_CHAR: |
| 4326 |
if (pos_write != (-2)) |
| 4327 |
{ |
| 4328 |
result = Str_makeTSD( 50 ) ; |
| 4329 |
#if defined(HAVE_I64U) |
| 4330 |
sprintf( result->value, "%I64u", pos_write + 1 ); |
| 4331 |
#else |
| 4332 |
if ( sizeof(off_t) > 4 ) |
| 4333 |
sprintf( result->value, "%lld", pos_write + 1 ); |
| 4334 |
else |
| 4335 |
sprintf( result->value, "%ld", (long)(pos_write + 1) ); |
| 4336 |
#endif |
| 4337 |
} |
| 4338 |
else |
| 4339 |
result = nullstringptr() ; |
| 4340 |
break; |
| 4341 |
case COMMAND_QUERY_POSITION_READ_LINE: |
| 4342 |
if (line_read != (-2)) |
| 4343 |
{ |
| 4344 |
result = Str_makeTSD( 50 ) ; |
| 4345 |
#if defined(HAVE_I64U) |
| 4346 |
sprintf( result->value, "%I64u", line_read ); |
| 4347 |
#else |
| 4348 |
if ( sizeof(off_t) > 4 ) |
| 4349 |
sprintf( result->value, "%lld", line_read ); |
| 4350 |
else |
| 4351 |
sprintf( result->value, "%ld", (long)line_read ); |
| 4352 |
#endif |
| 4353 |
} |
| 4354 |
else |
| 4355 |
result = nullstringptr() ; |
| 4356 |
break; |
| 4357 |
case COMMAND_QUERY_POSITION_WRITE_LINE: |
| 4358 |
if ( line_write == 0 ) |
| 4359 |
{ |
| 4360 |
rx_64 here; |
| 4361 |
rx_64 char_count; |
| 4362 |
int ch; |
| 4363 |
/* |
| 4364 |
* When a file is first opened for both read and |
| 4365 |
* write (default for implicit open), it is inexpensive |
| 4366 |
* to determine pos_read, pos_write and line_read, but |
| 4367 |
* is very expensive to determine line_write, so we |
| 4368 |
* don't do it. It is set to 0 indicating that we don't |
| 4369 |
* know the current write position. So to reduce the |
| 4370 |
* cost when we may never use it, we have to pay the |
| 4371 |
* price the first time we need the value; this is |
| 4372 |
* one of the times we pay the price! |
| 4373 |
*/ |
| 4374 |
result = Str_makeTSD( 50 ) ; |
| 4375 |
/* |
| 4376 |
* We can't use countlines(), so do our our counting |
| 4377 |
* of lines form the beginning of the file to the current |
| 4378 |
* write pos... |
| 4379 |
*/ |
| 4380 |
here = rx_ftell( ptr->fileptr ); |
| 4381 |
rx_fseek( ptr->fileptr, 0L, SEEK_SET ); |
| 4382 |
SWITCH_OPER_READ(ptr); |
| 4383 |
for( char_count = 0, line_write = 0; char_count < (long) ptr->writepos; char_count++ ) |
| 4384 |
{ |
| 4385 |
ch = getc( ptr->fileptr ); |
| 4386 |
if ( ch == EOF ) |
| 4387 |
break; |
| 4388 |
if ( ch == REGINA_EOL ) |
| 4389 |
line_write++; |
| 4390 |
} |
| 4391 |
#if defined(HAVE_I64U) |
| 4392 |
sprintf( result->value, "%I64u", line_write + 1 ); |
| 4393 |
#else |
| 4394 |
if ( sizeof(off_t) > 4 ) |
| 4395 |
sprintf( result->value, "%lld", line_write + 1 ) ; |
| 4396 |
else |
| 4397 |
sprintf( result->value, "%ld", (long)(line_write + 1) ) ; |
| 4398 |
#endif |
| 4399 |
rx_fseek( ptr->fileptr, here, SEEK_SET ); |
| 4400 |
} |
| 4401 |
else if (line_write != (-2)) |
| 4402 |
{ |
| 4403 |
result = Str_makeTSD( 50 ) ; |
| 4404 |
#if defined(HAVE_I64U) |
| 4405 |
sprintf( result->value, "%I64u", line_write ); |
| 4406 |
#else |
| 4407 |
if ( sizeof(off_t) > 4 ) |
| 4408 |
sprintf( result->value, "%lld", line_write ) ; |
| 4409 |
else |
| 4410 |
sprintf( result->value, "%ld", (long)line_write ) ; |
| 4411 |
#endif |
| 4412 |
} |
| 4413 |
else |
| 4414 |
result = nullstringptr() ; |
| 4415 |
break; |
| 4416 |
} |
| 4417 |
result->len = strlen( result->value ) ; |
| 4418 |
|
| 4419 |
if ( fn ) |
| 4420 |
FreeTSD( fn ); |
| 4421 |
return result ; |
| 4422 |
} |
| 4423 |
|
| 4424 |
|
| 4425 |
/* |
| 4426 |
* This little sweet routine returns information stored in the Rexx |
| 4427 |
* file table entry about the named file 'filename'. It is perhaps more |
| 4428 |
* of a debugging function than a Rexx function. It is accessed by the |
| 4429 |
* Rexx built-in function STREAM(), command 'STATUS'. One of the nice |
| 4430 |
* pieces of information this function returns is whether a file is |
| 4431 |
* transient or persistent. |
| 4432 |
* |
| 4433 |
* This is really a simple function, just retrieve the Rexx file |
| 4434 |
* table entry, and store the information in that entry into a string |
| 4435 |
* and return that string. |
| 4436 |
* |
| 4437 |
* The difference between getrexxstatus() and getstatus() is that |
| 4438 |
* that former returns information stored in Rexx's datastructures, |
| 4439 |
* while the latter return information about the file stored in and |
| 4440 |
* managed by the operating system |
| 4441 |
*/ |
| 4442 |
static streng *getrexxstatus( const tsd_t *TSD, cfileboxptr ptr ) |
| 4443 |
{ |
| 4444 |
streng *result=NULL ; |
| 4445 |
|
| 4446 |
if (ptr==NULL) |
| 4447 |
return nullstringptr() ; |
| 4448 |
|
| 4449 |
result = Str_makeTSD(64) ; /* Ought to be enough */ |
| 4450 |
result->value[0] = 0x00 ; |
| 4451 |
|
| 4452 |
if ((ptr->flag & FLAG_READ) && (ptr->flag & FLAG_WRITE)) |
| 4453 |
strcat( result->value, "READ/WRITE" ) ; |
| 4454 |
else if (ptr->flag & FLAG_READ) |
| 4455 |
strcat( result->value, "READ" ) ; |
| 4456 |
else if (ptr->flag & FLAG_WRITE) |
| 4457 |
strcat( result->value, "WRITE" ) ; |
| 4458 |
else |
| 4459 |
strcat( result->value, "NONE" ) ; |
| 4460 |
|
| 4461 |
#if defined(HAVE_I64U) |
| 4462 |
sprintf( result->value + strlen(result->value), |
| 4463 |
" READ: char=%I64u line=%I64u WRITE: char=%I64u line=%I64u %s", |
| 4464 |
(ptr->readpos+1), ptr->readline, |
| 4465 |
(ptr->writepos+1), ptr->writeline, |
| 4466 |
(ptr->flag & FLAG_PERSIST) ? "PERSISTENT" : "TRANSIENT" ) ; |
| 4467 |
#else |
| 4468 |
if ( sizeof(rx_64) > 4 ) |
| 4469 |
sprintf( result->value + strlen(result->value), |
| 4470 |
" READ: char=%lld line=%lld WRITE: char=%lld line=%lld %s", |
| 4471 |
(ptr->readpos+1), ptr->readline, |
| 4472 |
(ptr->writepos+1), ptr->writeline, |
| 4473 |
(ptr->flag & FLAG_PERSIST) ? "PERSISTENT" : "TRANSIENT" ) ; |
| 4474 |
else |
| 4475 |
sprintf( result->value + strlen(result->value), |
| 4476 |
" READ: char=%ld line=%ld WRITE: char=%ld line=%ld %s", |
| 4477 |
(long)(ptr->readpos+1), (long)ptr->readline, |
| 4478 |
(long)(ptr->writepos+1), (long)ptr->writeline, |
| 4479 |
(ptr->flag & FLAG_PERSIST) ? "PERSISTENT" : "TRANSIENT" ) ; |
| 4480 |
#endif |
| 4481 |
|
| 4482 |
result->len = strlen(result->value) ; |
| 4483 |
return result ; |
| 4484 |
} |
| 4485 |
|
| 4486 |
|
| 4487 |
/* |
| 4488 |
* This routine parses the remainder of the parameters passed to the |
| 4489 |
* Stream(,'C','QUERY...') function. |
| 4490 |
*/ |
| 4491 |
static streng *getquery( tsd_t *TSD, const streng *filename , const streng *subcommand) |
| 4492 |
{ |
| 4493 |
streng *result=NULL, *psub=NULL, *psubsub=NULL ; |
| 4494 |
char oper = 0; |
| 4495 |
char seek_oper = 0; |
| 4496 |
|
| 4497 |
/* |
| 4498 |
* Get the subcommand to QUERY |
| 4499 |
*/ |
| 4500 |
oper = get_querycommand( subcommand ); |
| 4501 |
switch ( oper ) |
| 4502 |
{ |
| 4503 |
case COMMAND_QUERY_DATETIME : |
| 4504 |
case COMMAND_QUERY_TIMESTAMP : |
| 4505 |
case COMMAND_QUERY_CREATETIME : |
| 4506 |
case COMMAND_QUERY_MODIFYTIME : |
| 4507 |
case COMMAND_QUERY_ACCESSTIME : |
| 4508 |
case COMMAND_QUERY_EXISTS : |
| 4509 |
case COMMAND_QUERY_HANDLE : |
| 4510 |
case COMMAND_QUERY_SIZE : |
| 4511 |
case COMMAND_QUERY_STREAMTYPE : |
| 4512 |
result = getstatus( TSD, filename, oper ); |
| 4513 |
break; |
| 4514 |
case COMMAND_QUERY_SEEK : |
| 4515 |
case COMMAND_QUERY_POSITION : |
| 4516 |
if ( oper == COMMAND_QUERY_SEEK ) |
| 4517 |
{ |
| 4518 |
psub = Str_nodupTSD( subcommand, 4, subcommand->len - 4 ); |
| 4519 |
seek_oper = 1; |
| 4520 |
} |
| 4521 |
else |
| 4522 |
{ |
| 4523 |
psub = Str_nodupTSD( subcommand, 8, subcommand->len - 8 ); |
| 4524 |
seek_oper = 0; |
| 4525 |
} |
| 4526 |
psub = Str_strp( psub, ' ', STRIP_LEADING); |
| 4527 |
oper = get_querypositioncommand( psub ); |
| 4528 |
switch ( oper ) |
| 4529 |
{ |
| 4530 |
case COMMAND_QUERY_POSITION_SYS : |
| 4531 |
result = getstatus(TSD, filename, oper ); |
| 4532 |
break; |
| 4533 |
case COMMAND_QUERY_POSITION_READ : |
| 4534 |
psubsub = Str_nodupTSD( psub, 4, psub->len - 4 ); |
| 4535 |
psubsub = Str_strp( psubsub, ' ', STRIP_LEADING); |
| 4536 |
oper = get_querypositionreadcommand( psubsub ); |
| 4537 |
switch( oper ) |
| 4538 |
{ |
| 4539 |
case COMMAND_QUERY_POSITION_READ_CHAR: |
| 4540 |
case COMMAND_QUERY_POSITION_READ_LINE: |
| 4541 |
result = getstatus( TSD, filename, oper ); |
| 4542 |
break; |
| 4543 |
default: |
| 4544 |
exiterror( ERR_STREAM_COMMAND, 1, (seek_oper)?"QUERY SEEK READ":"QUERY POSITION READ", "CHAR LINE ''", tmpstr_of( TSD, psubsub ) ) ; |
| 4545 |
break; |
| 4546 |
} |
| 4547 |
break; |
| 4548 |
case COMMAND_QUERY_POSITION_WRITE : |
| 4549 |
psubsub = Str_nodupTSD( psub, 5, psub->len - 5 ); |
| 4550 |
psubsub = Str_strp( psubsub, ' ', STRIP_LEADING); |
| 4551 |
oper = get_querypositionwritecommand( psubsub ); |
| 4552 |
switch( oper ) |
| 4553 |
{ |
| 4554 |
case COMMAND_QUERY_POSITION_WRITE_CHAR: |
| 4555 |
case COMMAND_QUERY_POSITION_WRITE_LINE: |
| 4556 |
result = getstatus( TSD, filename, oper ); |
| 4557 |
break; |
| 4558 |
default: |
| 4559 |
exiterror( ERR_STREAM_COMMAND, 1, (seek_oper)?"QUERY SEEK WRITE":"QUERY POSITION WRITE", "CHAR LINE ''", tmpstr_of( TSD, psubsub ) ) ; |
| 4560 |
break; |
| 4561 |
} |
| 4562 |
break; |
| 4563 |
default: |
| 4564 |
exiterror( ERR_STREAM_COMMAND, 1, (seek_oper)?"QUERY SEEK":"QUERY POSITION", "READ WRITE SYS", tmpstr_of( TSD, psub ) ) ; |
| 4565 |
break; |
| 4566 |
} |
| 4567 |
Free_stringTSD(psub); |
| 4568 |
break; |
| 4569 |
default: |
| 4570 |
exiterror( ERR_STREAM_COMMAND, 1, "QUERY", "DATETIME TIMESTAMP CREATETIME MODIFYTIME ACCESSTIME EXISTS HANDLE SIZE STREAMTYPE SEEK POSITION", tmpstr_of( TSD, subcommand ) ) ; |
| 4571 |
break; |
| 4572 |
} |
| 4573 |
|
| 4574 |
return result ; |
| 4575 |
} |
| 4576 |
|
| 4577 |
/* |
| 4578 |
* This routine parses the remainder of the parameters passed to the |
| 4579 |
* Stream(,'C','OPEN...') function. |
| 4580 |
*/ |
| 4581 |
static streng *getopen( tsd_t *TSD, const streng *filename , const streng *subcommand) |
| 4582 |
{ |
| 4583 |
fileboxptr ptr=NULL ; |
| 4584 |
streng *result=NULL, *psub=NULL ; |
| 4585 |
char oper = 0; |
| 4586 |
char buf[20]; |
| 4587 |
|
| 4588 |
/* |
| 4589 |
* Get the subcommand to OPEN |
| 4590 |
*/ |
| 4591 |
oper = get_opencommand( subcommand ); |
| 4592 |
switch ( oper ) |
| 4593 |
{ |
| 4594 |
case COMMAND_OPEN_BOTH : |
| 4595 |
if ( subcommand->len >= 4 |
| 4596 |
&& memcmp(subcommand->value, "BOTH", 4) == 0 ) |
| 4597 |
psub = Str_nodupTSD( subcommand, 4, subcommand->len - 4 ); |
| 4598 |
else |
| 4599 |
psub = Str_dupTSD( subcommand ); |
| 4600 |
psub = Str_strp( psub, ' ', STRIP_LEADING); |
| 4601 |
oper = get_opencommandboth( psub ); |
| 4602 |
if ( TSD->restricted ) |
| 4603 |
exiterror( ERR_RESTRICTED, 4 ) ; |
| 4604 |
switch ( oper ) |
| 4605 |
{ |
| 4606 |
case COMMAND_OPEN_BOTH : |
| 4607 |
closefile( TSD, filename ) ; |
| 4608 |
ptr = openfile( TSD, filename, ACCESS_WRITE ) ; |
| 4609 |
break; |
| 4610 |
case COMMAND_OPEN_BOTH_APPEND : |
| 4611 |
closefile( TSD, filename ) ; |
| 4612 |
ptr = openfile( TSD, filename, ACCESS_STREAM_APPEND ) ; |
| 4613 |
break; |
| 4614 |
case COMMAND_OPEN_BOTH_REPLACE : |
| 4615 |
closefile( TSD, filename ) ; |
| 4616 |
ptr = openfile( TSD, filename, ACCESS_STREAM_REPLACE ) ; |
| 4617 |
break; |
| 4618 |
default: |
| 4619 |
exiterror( ERR_STREAM_COMMAND, 1, "OPEN BOTH", "APPEND REPLACE ''", tmpstr_of( TSD, psub ) ) ; |
| 4620 |
break; |
| 4621 |
} |
| 4622 |
Free_stringTSD(psub); |
| 4623 |
if (ptr->fileptr) |
| 4624 |
result = Str_creTSD( "READY:" ) ; |
| 4625 |
else |
| 4626 |
{ |
| 4627 |
sprintf(buf,"ERROR:%d",errno); |
| 4628 |
result = Str_creTSD( buf ) ; |
| 4629 |
} |
| 4630 |
break; |
| 4631 |
case COMMAND_OPEN_READ : |
| 4632 |
closefile( TSD, filename ) ; |
| 4633 |
ptr = openfile( TSD, filename, ACCESS_READ ) ; |
| 4634 |
if (ptr->fileptr) |
| 4635 |
result = Str_creTSD( "READY:" ) ; |
| 4636 |
else |
| 4637 |
{ |
| 4638 |
sprintf(buf,"ERROR:%d",errno); |
| 4639 |
result = Str_creTSD( buf ) ; |
| 4640 |
} |
| 4641 |
break; |
| 4642 |
case COMMAND_OPEN_WRITE : |
| 4643 |
if ( TSD->restricted ) |
| 4644 |
exiterror( ERR_RESTRICTED, 4 ) ; |
| 4645 |
psub = Str_nodupTSD( subcommand, 5, subcommand->len - 5 ); |
| 4646 |
psub = Str_strp( psub, ' ', STRIP_LEADING); |
| 4647 |
oper = get_opencommandwrite( psub ); |
| 4648 |
|
| 4649 |
switch ( oper ) |
| 4650 |
{ |
| 4651 |
case COMMAND_OPEN_WRITE : |
| 4652 |
closefile( TSD, filename ) ; |
| 4653 |
ptr = openfile( TSD, filename, ACCESS_WRITE ) ; |
| 4654 |
break; |
| 4655 |
case COMMAND_OPEN_WRITE_APPEND : |
| 4656 |
closefile( TSD, filename ) ; |
| 4657 |
ptr = openfile( TSD, filename, ACCESS_STREAM_APPEND ) ; |
| 4658 |
break; |
| 4659 |
case COMMAND_OPEN_WRITE_REPLACE : |
| 4660 |
closefile( TSD, filename ) ; |
| 4661 |
ptr = openfile( TSD, filename, ACCESS_STREAM_REPLACE ) ; |
| 4662 |
break; |
| 4663 |
default: |
| 4664 |
exiterror( ERR_STREAM_COMMAND, 1, "OPEN WRITE", "APPEND REPLACE ''", tmpstr_of( TSD, psub ) ) ; |
| 4665 |
break; |
| 4666 |
} |
| 4667 |
Free_stringTSD(psub); |
| 4668 |
if (ptr->fileptr) |
| 4669 |
result = Str_creTSD( "READY:" ) ; |
| 4670 |
else |
| 4671 |
{ |
| 4672 |
sprintf(buf,"ERROR:%d",errno); |
| 4673 |
result = Str_creTSD( buf ) ; |
| 4674 |
} |
| 4675 |
break; |
| 4676 |
default: |
| 4677 |
exiterror( ERR_STREAM_COMMAND, 1, "OPEN", "BOTH READ WRITE ''", tmpstr_of( TSD, subcommand ) ) ; |
| 4678 |
break; |
| 4679 |
} |
| 4680 |
|
| 4681 |
return result ; |
| 4682 |
} |
| 4683 |
|
| 4684 |
|
| 4685 |
static streng *getseek( tsd_t *TSD, const streng *filename, const streng *cmd ) |
| 4686 |
{ |
| 4687 |
#define STATE_START 0 |
| 4688 |
#define STATE_WORD 1 |
| 4689 |
#define STATE_DELIM 2 |
| 4690 |
char *word[5] = {NULL,NULL,NULL,NULL}; |
| 4691 |
char *str; |
| 4692 |
char *offset=NULL; |
| 4693 |
int i,j=0; |
| 4694 |
int state=STATE_START; |
| 4695 |
int seek_by_line=0; |
| 4696 |
int seek_type=0; |
| 4697 |
int seek_sign=0; |
| 4698 |
rx_64 seek_offset=0,pos=0; |
| 4699 |
int pos_type=OPER_NONE,num_params=0; |
| 4700 |
int str_start=0,str_end=(-1),words; |
| 4701 |
fileboxptr ptr; |
| 4702 |
streng *result=NULL; |
| 4703 |
char buf[20]; |
| 4704 |
|
| 4705 |
str = str_ofTSD(cmd); |
| 4706 |
words = 4; |
| 4707 |
for (i=0;i<Str_len(cmd);i++) |
| 4708 |
{ |
| 4709 |
switch(state) |
| 4710 |
{ |
| 4711 |
case STATE_START: |
| 4712 |
if (*(str+i) == ' ') |
| 4713 |
{ |
| 4714 |
state = STATE_DELIM; |
| 4715 |
break; |
| 4716 |
} |
| 4717 |
if ( j < 3 ) |
| 4718 |
word[j] = str+str_start; |
| 4719 |
j++; |
| 4720 |
if (str_end != (-1)) |
| 4721 |
{ |
| 4722 |
*(str+str_end) = '\0'; |
| 4723 |
} |
| 4724 |
state = STATE_WORD; |
| 4725 |
break; |
| 4726 |
case STATE_WORD: |
| 4727 |
if (*(str+i) == ' ') |
| 4728 |
{ |
| 4729 |
state = STATE_DELIM; |
| 4730 |
str_end = i; |
| 4731 |
str_start = str_end + 1; |
| 4732 |
break; |
| 4733 |
} |
| 4734 |
break; |
| 4735 |
case STATE_DELIM: |
| 4736 |
state = STATE_WORD; |
| 4737 |
if (*(str+i) == ' ') |
| 4738 |
{ |
| 4739 |
state = STATE_DELIM; |
| 4740 |
} |
| 4741 |
if (state == STATE_WORD) |
| 4742 |
{ |
| 4743 |
if ( j < 3 ) |
| 4744 |
word[j] = str+str_start; |
| 4745 |
j++; |
| 4746 |
if (str_end != (-1)) |
| 4747 |
{ |
| 4748 |
*(str+str_end) = '\0'; |
| 4749 |
} |
| 4750 |
} |
| 4751 |
break; |
| 4752 |
} |
| 4753 |
} |
| 4754 |
num_params = j; |
| 4755 |
if (num_params < 1) |
| 4756 |
exiterror( ERR_INCORRECT_CALL, 922, "STREAM", 3, 2, num_params+1 ); |
| 4757 |
if (num_params > 3) |
| 4758 |
exiterror( ERR_INCORRECT_CALL, 923, "STREAM", 3, 4, num_params+1 ); |
| 4759 |
|
| 4760 |
switch( num_params ) |
| 4761 |
{ |
| 4762 |
case 3: |
| 4763 |
if (strcmp(word[2],"CHAR") == 0) |
| 4764 |
seek_by_line = 0; |
| 4765 |
else |
| 4766 |
{ |
| 4767 |
if (strcmp(word[2],"LINE") == 0) |
| 4768 |
seek_by_line = 1; |
| 4769 |
else |
| 4770 |
exiterror( ERR_INCORRECT_CALL, 924, "STREAM", 3, "CHAR LINE", word[2] ); |
| 4771 |
} |
| 4772 |
/* meant to fall through */ |
| 4773 |
case 2: |
| 4774 |
/* |
| 4775 |
* 2 params(to SEEK), last one (word[1]) could be READ/WRITE or CHAR/LINE |
| 4776 |
*/ |
| 4777 |
if (strcmp(word[1],"READ") == 0) |
| 4778 |
pos_type = OPER_READ; |
| 4779 |
else if (strcmp(word[1],"WRITE") == 0) |
| 4780 |
pos_type = OPER_WRITE; |
| 4781 |
else if (strcmp(word[1],"CHAR") == 0) |
| 4782 |
seek_by_line = 0; |
| 4783 |
else if (strcmp(word[1],"LINE") == 0) |
| 4784 |
seek_by_line = 1; |
| 4785 |
else |
| 4786 |
exiterror( ERR_INCORRECT_CALL, 924, "STREAM", 3, "READ WRITE CHAR LINE", word[1] ); |
| 4787 |
} |
| 4788 |
/* |
| 4789 |
* Determine the position type if not supplied prior |
| 4790 |
*/ |
| 4791 |
if ( pos_type == OPER_NONE ) |
| 4792 |
{ |
| 4793 |
ptr = getfileptr( TSD, filename ) ; |
| 4794 |
if ( ptr != NULL ) |
| 4795 |
{ |
| 4796 |
if ( ptr->flag & FLAG_READ ) |
| 4797 |
pos_type |= OPER_READ; |
| 4798 |
if ( ptr->flag & FLAG_WRITE ) |
| 4799 |
pos_type |= OPER_WRITE; |
| 4800 |
} |
| 4801 |
} |
| 4802 |
offset = word[0]; |
| 4803 |
switch(*offset) |
| 4804 |
{ |
| 4805 |
case '=': |
| 4806 |
seek_type = SEEK_SET; |
| 4807 |
offset++; |
| 4808 |
break; |
| 4809 |
case '-': |
| 4810 |
seek_type = SEEK_CUR; |
| 4811 |
seek_sign = 1; |
| 4812 |
offset++; |
| 4813 |
break; |
| 4814 |
case '+': |
| 4815 |
seek_type = SEEK_CUR; |
| 4816 |
seek_sign = 0; |
| 4817 |
offset++; |
| 4818 |
break; |
| 4819 |
case '<': |
| 4820 |
seek_type = SEEK_END; |
| 4821 |
offset++; |
| 4822 |
break; |
| 4823 |
default: |
| 4824 |
seek_type = SEEK_SET; |
| 4825 |
break; |
| 4826 |
} |
| 4827 |
for (i=0;i<(int)strlen(offset);i++) |
| 4828 |
{ |
| 4829 |
if (!rx_isdigit(*(offset+i))) |
| 4830 |
exiterror( ERR_INCORRECT_CALL, 924, "STREAM", 3, "n, +n, -n, =n or <n", word[0] ); |
| 4831 |
} |
| 4832 |
#if defined(HAVE_ATOLL) |
| 4833 |
seek_offset = atoll(offset); |
| 4834 |
#elif defined(HAVE__ATOI64) |
| 4835 |
seek_offset = _atoi64(offset); |
| 4836 |
#else |
| 4837 |
seek_offset = atol(offset); |
| 4838 |
#endif |
| 4839 |
if (seek_sign) /* negative */ |
| 4840 |
seek_offset *= -1; |
| 4841 |
ptr = get_file_ptr( TSD, filename, pos_type, (pos_type&OPER_WRITE) ? ACCESS_WRITE : ACCESS_READ ) ; |
| 4842 |
if (!ptr) |
| 4843 |
{ |
| 4844 |
sprintf(buf,"ERROR:%d",errno); |
| 4845 |
result = Str_creTSD( buf ) ; |
| 4846 |
} |
| 4847 |
if (seek_by_line) /* position by line */ |
| 4848 |
pos = positionfile( TSD, "STREAM", 3, ptr, pos_type, seek_offset, seek_type ) ; |
| 4849 |
else |
| 4850 |
pos = positioncharfile( TSD, "STREAM", 3, ptr, pos_type, seek_offset, seek_type ) ; |
| 4851 |
if ( pos >= 0 ) |
| 4852 |
{ |
| 4853 |
result = Str_makeTSD( 20 ) ; /* should be enough digits */ |
| 4854 |
#if defined(HAVE_I64U) |
| 4855 |
sprintf(result->value, "%I64u", pos ); |
| 4856 |
#else |
| 4857 |
if ( sizeof(rx_64) > 4 ) |
| 4858 |
sprintf(result->value, "%lld", pos ); |
| 4859 |
else |
| 4860 |
sprintf(result->value, "%ld", (long)pos ); |
| 4861 |
#endif |
| 4862 |
Str_len( result ) = strlen( result->value ); |
| 4863 |
} |
| 4864 |
else |
| 4865 |
{ |
| 4866 |
sprintf(buf,"ERROR:%d",errno); |
| 4867 |
result = Str_creTSD( buf ) ; |
| 4868 |
} |
| 4869 |
FreeTSD(str); |
| 4870 |
return result ; |
| 4871 |
} |
| 4872 |
|
| 4873 |
|
| 4874 |
|
| 4875 |
/* ------------------------------------------------------------------- */ |
| 4876 |
/* Rexx builtin functions (level 3) */ |
| 4877 |
/* |
| 4878 |
* This part consists of one function for each of the Rexx builtin |
| 4879 |
* functions that operates on filesystem I/O |
| 4880 |
*/ |
| 4881 |
|
| 4882 |
|
| 4883 |
/* |
| 4884 |
* This routine implements the Rexx built-in function CHARS(). It is |
| 4885 |
* really quite simple, little more than a wrap-around to the |
| 4886 |
* function calc_chars_left. |
| 4887 |
*/ |
| 4888 |
streng *std_chars( tsd_t *TSD, cparamboxptr parms ) |
| 4889 |
{ |
| 4890 |
char opt = 'N'; |
| 4891 |
streng *string=NULL ; |
| 4892 |
fileboxptr ptr=NULL ; |
| 4893 |
int was_closed=0; |
| 4894 |
rx_64 result=0 ; |
| 4895 |
fil_tsd_t *ft; |
| 4896 |
|
| 4897 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 4898 |
|
| 4899 |
/* Syntax: chars([filename]) */ |
| 4900 |
checkparam( parms, 0, 2 , "CHARS" ) ; |
| 4901 |
|
| 4902 |
if (parms&&parms->next&&parms->next->value) |
| 4903 |
opt = getoptionchar( TSD, parms->next->value, "CHARS", 2, "CN", "" ) ; |
| 4904 |
|
| 4905 |
string = (parms->value && parms->value->len) ? parms->value : ft->stdio_ptr[0]->filename0 ; |
| 4906 |
/* |
| 4907 |
* Get a pointer to the Rexx file table entry of the file, and |
| 4908 |
* calculate the number of characters left. |
| 4909 |
*/ |
| 4910 |
ptr = getfileptr( TSD, string ) ; |
| 4911 |
was_closed = (ptr==NULL) ; |
| 4912 |
if (!ptr) |
| 4913 |
ptr = get_file_ptr( TSD, string, OPER_READ, ACCESS_READ ) ; |
| 4914 |
|
| 4915 |
result = calc_chars_left( TSD, ptr ) ; |
| 4916 |
if (was_closed) |
| 4917 |
closefile( TSD, string ) ; |
| 4918 |
|
| 4919 |
return rx64_to_streng( TSD, result ) ; |
| 4920 |
} |
| 4921 |
|
| 4922 |
|
| 4923 |
|
| 4924 |
/* |
| 4925 |
* Implements the Rexx builtin function charin(). This function takes |
| 4926 |
* three parameters, and they are treated pretty straight forward |
| 4927 |
* according to TRL. If called with no start position, and a length of |
| 4928 |
* zero, it may be used to do some fancy work (flushing I/O?), although |
| 4929 |
* that is probably more needed for output :-) Note that the file in |
| 4930 |
* entered into the file table in this case, so it might be used to |
| 4931 |
* explicitly open a file for reading. However, consider using stream() |
| 4932 |
* to do this, it's a much cleaner approach! |
| 4933 |
*/ |
| 4934 |
streng *std_charin( tsd_t *TSD, cparamboxptr parms ) |
| 4935 |
{ |
| 4936 |
streng *filename=NULL, *result=NULL ; |
| 4937 |
fileboxptr ptr=NULL ; |
| 4938 |
size_t length=0 ; |
| 4939 |
rx_64 start=0 ; |
| 4940 |
fil_tsd_t *ft; |
| 4941 |
|
| 4942 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 4943 |
|
| 4944 |
/* Syntax: charin([filename][,[start][,length]]) */ |
| 4945 |
checkparam( parms, 0, 3 , "CHARIN" ) ; |
| 4946 |
|
| 4947 |
/* |
| 4948 |
* First, let's get the information about the file from the |
| 4949 |
* file table, and open it in the correct mode if is not already |
| 4950 |
* availble as such. |
| 4951 |
*/ |
| 4952 |
filename = (parms->value && parms->value->len) ? (parms->value) : ft->stdio_ptr[0]->filename0 ; |
| 4953 |
ptr = get_file_ptr( TSD, filename, OPER_READ, ACCESS_READ ) ; |
| 4954 |
|
| 4955 |
/* |
| 4956 |
* Then, get the starting point, or set it to zero. |
| 4957 |
*/ |
| 4958 |
parms = parms->next ; |
| 4959 |
if ((parms)&&(parms->value)) |
| 4960 |
start = atoposrx64( TSD, parms->value, "CHARIN", 2 ) ; |
| 4961 |
else |
| 4962 |
start = 0 ; |
| 4963 |
|
| 4964 |
/* |
| 4965 |
* At last, get the length, or use the default value one. |
| 4966 |
*/ |
| 4967 |
if (parms) |
| 4968 |
parms = parms->next ; |
| 4969 |
|
| 4970 |
if ((parms)&&(parms->value)) |
| 4971 |
length = atozpos( TSD, parms->value, "CHARIN", 3 ) ; |
| 4972 |
else |
| 4973 |
length = 1 ; |
| 4974 |
|
| 4975 |
/* |
| 4976 |
* Position current position in file if necessary |
| 4977 |
*/ |
| 4978 |
if (start) |
| 4979 |
positioncharfile( TSD, "CHARIN", 2, ptr, OPER_READ, start, SEEK_SET ) ; |
| 4980 |
|
| 4981 |
if (length) |
| 4982 |
result = readbytes( TSD, ptr, length, 0 ) ; |
| 4983 |
else |
| 4984 |
{ |
| 4985 |
if (!start) |
| 4986 |
flush_input( ptr ) ; /* Whatever happens ... */ |
| 4987 |
result = nullstringptr() ; |
| 4988 |
} |
| 4989 |
|
| 4990 |
return result ; |
| 4991 |
} |
| 4992 |
|
| 4993 |
|
| 4994 |
|
| 4995 |
/* |
| 4996 |
* This function implements the Rexx built-in function CHAROUT(). It |
| 4997 |
* is basically a wrap-around for the two functions that perform |
| 4998 |
* character repositioning in a file; and writes out characters. |
| 4999 |
*/ |
| 5000 |
|
| 5001 |
streng *std_charout( tsd_t *TSD, cparamboxptr parms ) |
| 5002 |
{ |
| 5003 |
streng *filename=NULL, *string=NULL ; |
| 5004 |
int length=0 ; |
| 5005 |
long pos=0 ; |
| 5006 |
fileboxptr ptr=NULL ; |
| 5007 |
fil_tsd_t *ft; |
| 5008 |
|
| 5009 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 5010 |
|
| 5011 |
if ( TSD->restricted ) |
| 5012 |
exiterror( ERR_RESTRICTED, 1, "CHAROUT" ) ; |
| 5013 |
|
| 5014 |
/* Syntax: charout([filename][,[string][,start]]) */ |
| 5015 |
checkparam( parms, 0, 3 , "CHAROUT" ) ; |
| 5016 |
|
| 5017 |
filename = (parms->value && parms->value->len) ? (parms->value) : ft->stdio_ptr[1]->filename0 ; |
| 5018 |
|
| 5019 |
/* Read the data to be written, if any */ |
| 5020 |
parms = parms->next ; |
| 5021 |
if (parms && parms->value ) |
| 5022 |
string = parms->value ; |
| 5023 |
else |
| 5024 |
string = NULL ; |
| 5025 |
|
| 5026 |
/* Read the position to start writing, is any */ |
| 5027 |
if (parms) |
| 5028 |
parms = parms->next ; |
| 5029 |
|
| 5030 |
if ( parms && parms->value ) |
| 5031 |
pos = atopos( TSD, parms->value, "CHAROUT", 3 ) ; |
| 5032 |
else |
| 5033 |
pos = 0 ; |
| 5034 |
|
| 5035 |
ptr = get_file_ptr( TSD, filename, OPER_WRITE, ACCESS_WRITE ) ; |
| 5036 |
|
| 5037 |
/* |
| 5038 |
* If we are to position the write position somewhere, do that first. |
| 5039 |
*/ |
| 5040 |
if (pos) |
| 5041 |
positioncharfile( TSD, "CHAROUT", 3, ptr, OPER_WRITE, pos, SEEK_SET ) ; |
| 5042 |
|
| 5043 |
/* |
| 5044 |
* Then, write the actual data, or flush output if neither data nor |
| 5045 |
* position was given. |
| 5046 |
*/ |
| 5047 |
if (string) |
| 5048 |
length = string->len - writebytes( TSD, ptr, string, 0 ) ; |
| 5049 |
else |
| 5050 |
{ |
| 5051 |
length = 0; |
| 5052 |
if ( !pos ) |
| 5053 |
{ |
| 5054 |
/* |
| 5055 |
* flush_output() will swap out the file and close it, but leave ALL positions |
| 5056 |
* intact |
| 5057 |
* We need to set the write positions to end of file (NOT EOF) |
| 5058 |
* See ANSI 9.7.2, 9.7.5, A.5.8.9 |
| 5059 |
* For efficiency sake, we will have to set writeline = 0 :-( |
| 5060 |
* We do this BEFORE flush_output() otherwise we won't have a ptr->fileptr! |
| 5061 |
*/ |
| 5062 |
if ( ptr->flag & FLAG_PERSIST ) |
| 5063 |
{ |
| 5064 |
rx_fseek( ptr->fileptr, 0, SEEK_END ) ; |
| 5065 |
ptr->writepos = rx_ftell( ptr->fileptr ) ; |
| 5066 |
} |
| 5067 |
else |
| 5068 |
ptr->writepos = 0; |
| 5069 |
ptr->writeline = 0; |
| 5070 |
|
| 5071 |
if ( flush_output( TSD, ptr ) == -1 ) |
| 5072 |
length = 1; /* simulate (at least) 1 byte not written */ |
| 5073 |
} |
| 5074 |
} |
| 5075 |
|
| 5076 |
return int_to_streng( TSD, length ) ; |
| 5077 |
} |
| 5078 |
|
| 5079 |
|
| 5080 |
|
| 5081 |
/* |
| 5082 |
* Simple routine that implements the Rexx built-in function LINES(). |
| 5083 |
* Really just a wrap-around to the countlines() routine. |
| 5084 |
*/ |
| 5085 |
|
| 5086 |
streng *std_lines( tsd_t *TSD, cparamboxptr parms ) |
| 5087 |
{ |
| 5088 |
char opt = 'N'; |
| 5089 |
fileboxptr ptr=NULL ; |
| 5090 |
streng *filename=NULL ; |
| 5091 |
int was_closed=0; |
| 5092 |
rx_64 result=0 ; |
| 5093 |
int actual; |
| 5094 |
fil_tsd_t *ft; |
| 5095 |
|
| 5096 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 5097 |
|
| 5098 |
/* Syntax: lines([filename][,C|N]) */ |
| 5099 |
checkparam( parms, 0, 2 , "LINES" ) ; |
| 5100 |
|
| 5101 |
if (parms&&parms->next&&parms->next->value) |
| 5102 |
opt = getoptionchar( TSD, parms->next->value, "LINES", 2, "CN", "" ) ; |
| 5103 |
|
| 5104 |
/* |
| 5105 |
* Get the name of the file (use defaults if necessary), and get |
| 5106 |
* a pointer to the entry of that file from the file table |
| 5107 |
*/ |
| 5108 |
if (parms->value |
| 5109 |
&& parms->value->len) |
| 5110 |
filename = parms->value ; |
| 5111 |
else |
| 5112 |
filename = ft->stdio_ptr[0]->filename0 ; |
| 5113 |
|
| 5114 |
/* |
| 5115 |
* Try to get the Rexx file table entry, if it doesn't work, then |
| 5116 |
* try again ... and a bit harder |
| 5117 |
*/ |
| 5118 |
ptr = getfileptr( TSD, filename ) ; |
| 5119 |
was_closed = (ptr==NULL) ; |
| 5120 |
if (!ptr) |
| 5121 |
ptr = get_file_ptr( TSD, filename, OPER_READ, ACCESS_READ ) ; |
| 5122 |
|
| 5123 |
/* |
| 5124 |
* If 'C' is passed, we ALWAYS get the actual number of lines |
| 5125 |
* If 'N' is passed (or unset), we get the actual number of lines |
| 5126 |
* unless FAST_LINES_BIF is set (default is set) |
| 5127 |
* Bug: 1000227 |
| 5128 |
*/ |
| 5129 |
if ( opt == 'C' ) |
| 5130 |
actual = 1; |
| 5131 |
else |
| 5132 |
{ |
| 5133 |
if ( get_options_flag( TSD->currlevel, EXT_FAST_LINES_BIF_DEFAULT ) ) |
| 5134 |
actual = 0; |
| 5135 |
else |
| 5136 |
actual = 1; |
| 5137 |
} |
| 5138 |
result = countlines( TSD, ptr, actual, OPER_READ ) ; |
| 5139 |
|
| 5140 |
if (was_closed) |
| 5141 |
closefile( TSD, filename ) ; |
| 5142 |
|
| 5143 |
|
| 5144 |
return rx64_to_streng( TSD, result ) ; |
| 5145 |
} |
| 5146 |
|
| 5147 |
|
| 5148 |
|
| 5149 |
/* |
| 5150 |
* The Rexx built-in function LINEIN() reads a line from a file. |
| 5151 |
* The actual reading is performed in 'readoneline', while this routine |
| 5152 |
* takes care of range checking of parameters, and decides which |
| 5153 |
* lower level routines to call. |
| 5154 |
*/ |
| 5155 |
|
| 5156 |
streng *std_linein( tsd_t *TSD, cparamboxptr parms ) |
| 5157 |
{ |
| 5158 |
streng *filename=NULL, *res=NULL ; |
| 5159 |
fileboxptr ptr=NULL ; |
| 5160 |
int count=0, line=0 ; |
| 5161 |
fil_tsd_t *ft; |
| 5162 |
|
| 5163 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 5164 |
|
| 5165 |
/* Syntax: linein([filename][,[line][,count]]) */ |
| 5166 |
checkparam( parms, 0, 3 , "LINEIN" ) ; |
| 5167 |
|
| 5168 |
/* |
| 5169 |
* First get the name of the file, or use the appropriate default |
| 5170 |
*/ |
| 5171 |
if (parms->value |
| 5172 |
&& parms->value->len) |
| 5173 |
filename = parms->value ; |
| 5174 |
else |
| 5175 |
filename = ft->stdio_ptr[0]->filename0 ; |
| 5176 |
|
| 5177 |
/* |
| 5178 |
* Then get the line number at which the read it to start, or set |
| 5179 |
* set it to zero if none was specified. |
| 5180 |
*/ |
| 5181 |
if (parms) |
| 5182 |
parms = parms->next ; |
| 5183 |
|
| 5184 |
if (parms && parms->value) |
| 5185 |
line = atopos( TSD, parms->value, "LINEIN", 2 ) ; |
| 5186 |
else |
| 5187 |
line = 0 ; /* Illegal value */ |
| 5188 |
|
| 5189 |
/* |
| 5190 |
* And at last, read the count, which can be only 0 or 1, and which |
| 5191 |
* is the number of lines to read. |
| 5192 |
*/ |
| 5193 |
if (parms) |
| 5194 |
parms = parms->next ; |
| 5195 |
|
| 5196 |
if (parms && parms->value) |
| 5197 |
{ |
| 5198 |
count = atozpos( TSD, parms->value, "LINEIN", 3 ) ; |
| 5199 |
if (count!=0 && count!=1) |
| 5200 |
exiterror( ERR_INCORRECT_CALL, 39, "LINEIN", tmpstr_of( TSD, parms->value ) ) ; |
| 5201 |
} |
| 5202 |
else |
| 5203 |
count = 1 ; /* The default */ |
| 5204 |
|
| 5205 |
/* |
| 5206 |
* Now, get the pointer to the entry in the file table that contains |
| 5207 |
* information about this file, or make it automatically create |
| 5208 |
* an entry if one didn't exist. |
| 5209 |
*/ |
| 5210 |
ptr = get_file_ptr( TSD, filename, OPER_READ, ACCESS_READ ) ; |
| 5211 |
|
| 5212 |
/* |
| 5213 |
* If line was specified, we must reposition the current read |
| 5214 |
* position of the file. |
| 5215 |
*/ |
| 5216 |
if (line) |
| 5217 |
positionfile( TSD, "LINEIN", 2, ptr, OPER_READ, line, SEEK_SET ) ; |
| 5218 |
|
| 5219 |
/* |
| 5220 |
* As the last thing, read in the data. If no data was wanted, skip it |
| 5221 |
* but call flushing if line wasn't specified either. |
| 5222 |
*/ |
| 5223 |
if (count) |
| 5224 |
res = readoneline( TSD, ptr ) ; |
| 5225 |
else |
| 5226 |
{ |
| 5227 |
if (!line) |
| 5228 |
flush_input( ptr ) ; |
| 5229 |
res = nullstringptr() ; |
| 5230 |
} |
| 5231 |
|
| 5232 |
return res ; |
| 5233 |
} |
| 5234 |
|
| 5235 |
|
| 5236 |
|
| 5237 |
|
| 5238 |
/* |
| 5239 |
* This function is a wrap-around for the Rexx built-in function |
| 5240 |
* LINEOUT(). It performs parameter checking and decides which lower |
| 5241 |
* level routines to call. |
| 5242 |
*/ |
| 5243 |
|
| 5244 |
streng *std_lineout( tsd_t *TSD, cparamboxptr parms ) |
| 5245 |
{ |
| 5246 |
streng *string=NULL, *file=NULL ; |
| 5247 |
int lineno=0, result=0 ; |
| 5248 |
fileboxptr ptr=NULL ; |
| 5249 |
fil_tsd_t *ft; |
| 5250 |
|
| 5251 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 5252 |
|
| 5253 |
if ( TSD->restricted ) |
| 5254 |
exiterror( ERR_RESTRICTED, 1, "LINEOUT" ) ; |
| 5255 |
|
| 5256 |
/* Syntax: lineout([filename][,[string][,line]]) */ |
| 5257 |
checkparam( parms, 0, 3 , "LINEOUT" ) ; |
| 5258 |
|
| 5259 |
/* |
| 5260 |
* First get the pointer for the file to operate on. If omitted, |
| 5261 |
* use the standard output stream |
| 5262 |
*/ |
| 5263 |
if (parms->value |
| 5264 |
&& parms->value->len) |
| 5265 |
file = parms->value ; |
| 5266 |
else |
| 5267 |
file = ft->stdio_ptr[1]->filename0 ; |
| 5268 |
/* |
| 5269 |
* The file pointer is needed in ALL circumstances! |
| 5270 |
*/ |
| 5271 |
ptr = get_file_ptr( TSD, file, OPER_WRITE, ACCESS_WRITE ) ; |
| 5272 |
|
| 5273 |
/* |
| 5274 |
* Then, get the data to be written, if any. |
| 5275 |
*/ |
| 5276 |
if (parms) |
| 5277 |
parms = parms->next ; |
| 5278 |
|
| 5279 |
if (parms && parms->value) |
| 5280 |
string = parms->value ; |
| 5281 |
else |
| 5282 |
string = NULL ; |
| 5283 |
|
| 5284 |
/* |
| 5285 |
* At last, we must find the line number of the file to write. We |
| 5286 |
* must position the file at this line before the write. |
| 5287 |
*/ |
| 5288 |
if (parms) |
| 5289 |
parms = parms->next ; |
| 5290 |
|
| 5291 |
if (parms && parms->value) |
| 5292 |
lineno = atopos( TSD, parms->value, "LINEOUT", 3 ) ; |
| 5293 |
else |
| 5294 |
lineno = 0 ; /* illegal value */ |
| 5295 |
|
| 5296 |
/* |
| 5297 |
* First, let's reposition the file if necessary. |
| 5298 |
*/ |
| 5299 |
if (lineno) |
| 5300 |
positionfile( TSD, "LINEOUT", 2, ptr, OPER_WRITE, lineno, SEEK_SET ) ; |
| 5301 |
|
| 5302 |
/* |
| 5303 |
* And then, we write out the data. If there are not data, it may have |
| 5304 |
* been just positioning. However, if there are neither data nor |
| 5305 |
* a linenumber, something magic may happen. |
| 5306 |
*/ |
| 5307 |
if (string) |
| 5308 |
result = writeoneline( TSD, ptr, string ) ; |
| 5309 |
else |
| 5310 |
{ |
| 5311 |
if ( !lineno ) |
| 5312 |
{ |
| 5313 |
/* |
| 5314 |
* flush_output() will swap out the file and close it, but leave ALL positions |
| 5315 |
* intact |
| 5316 |
* We need to set the write positions to end of file (NOT EOF) |
| 5317 |
* See ANSI 9.7.2, 9.7.5, A.5.8.9 |
| 5318 |
* For efficiency sake, we will have to set writeline = 0 :-( |
| 5319 |
* We do this BEFORE flush_output() otherwise we won't have a ptr->fileptr! |
| 5320 |
*/ |
| 5321 |
if ( ptr->flag & FLAG_PERSIST ) |
| 5322 |
{ |
| 5323 |
/* |
| 5324 |
* When using LINEOUT() to close a file and that file is NOT writeable, ptr->fileptr |
| 5325 |
* gets set to NULL, so guard against this |
| 5326 |
*/ |
| 5327 |
if ( ptr->fileptr ) |
| 5328 |
{ |
| 5329 |
rx_fseek( ptr->fileptr, 0, SEEK_END ) ; |
| 5330 |
ptr->writepos = rx_ftell( ptr->fileptr ) ; |
| 5331 |
} |
| 5332 |
} |
| 5333 |
else |
| 5334 |
ptr->writepos = 0; |
| 5335 |
ptr->writeline = 0; |
| 5336 |
/* |
| 5337 |
* ANSI states that a file is not necessarily closed in this case. |
| 5338 |
* Position of file pointers is explicitly stated in ANSI and if the |
| 5339 |
* file is NOT closed they cause breakage. |
| 5340 |
* Therefore implement ANSI in STRICT_ANSI mode and normal behaviour |
| 5341 |
* (that does not cause breakage) in "regina" mode. |
| 5342 |
* MH 22/06/2004 - after non-conclusive discussions on ANSI mailing list |
| 5343 |
*/ |
| 5344 |
if ( get_options_flag( TSD->currlevel, EXT_STRICT_ANSI ) ) |
| 5345 |
{ |
| 5346 |
flush_output( TSD, ptr ); |
| 5347 |
} |
| 5348 |
else |
| 5349 |
{ |
| 5350 |
closefile( TSD, file ) ; |
| 5351 |
} |
| 5352 |
} |
| 5353 |
result = 0; |
| 5354 |
} |
| 5355 |
|
| 5356 |
return int_to_streng( TSD, result ) ; |
| 5357 |
} |
| 5358 |
|
| 5359 |
|
| 5360 |
|
| 5361 |
|
| 5362 |
/* |
| 5363 |
* This function checks whether a particular file is accessable by |
| 5364 |
* the user in a certain mode, which may be read, write or execute. |
| 5365 |
* Unfortunately, this function differs a bit from the functionality |
| 5366 |
* of several others. It explicitly checks a file, so that if the |
| 5367 |
* file didn't exist in advance, it is _not_ opened. And even _if_ |
| 5368 |
* the file existed, the file in the file system is checked, not the |
| 5369 |
* file opened by Regina. The two may differ slightly under certain |
| 5370 |
* circumstances. |
| 5371 |
*/ |
| 5372 |
|
| 5373 |
static int is_accessable( const tsd_t *TSD, const streng *filename, int mode ) |
| 5374 |
{ |
| 5375 |
int res=0 ; |
| 5376 |
char *fn ; |
| 5377 |
|
| 5378 |
fn = str_ofTSD( filename ) ; |
| 5379 |
/* |
| 5380 |
* First, call access() with the 'correct' parameters, and store |
| 5381 |
* the result in 'res'. If 'mode' had an "impossible" value, give |
| 5382 |
* an error. |
| 5383 |
*/ |
| 5384 |
#if defined(WIN32) && ( defined(__IBMC__) || defined(__LCC__) ) |
| 5385 |
{ |
| 5386 |
DWORD Attrib; |
| 5387 |
res=-1; |
| 5388 |
Attrib=GetFileAttributes(fn); |
| 5389 |
if (Attrib==(DWORD)-1) |
| 5390 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ; |
| 5391 |
if ((Attrib&FILE_ATTRIBUTE_DIRECTORY)!=FILE_ATTRIBUTE_DIRECTORY) |
| 5392 |
{ |
| 5393 |
if ((mode == COMMAND_READABLE) && ((Attrib&FILE_ATTRIBUTE_READONLY)==FILE_ATTRIBUTE_READONLY)) |
| 5394 |
res = 0 ; |
| 5395 |
else if ((mode == COMMAND_WRITEABLE) || (mode == COMMAND_EXECUTABLE)) |
| 5396 |
res = 0 ; |
| 5397 |
} |
| 5398 |
} |
| 5399 |
#else |
| 5400 |
if (mode == COMMAND_READABLE) |
| 5401 |
res = access( fn, R_OK ) ; |
| 5402 |
else if (mode == COMMAND_WRITEABLE) |
| 5403 |
res = access( fn, W_OK ) ; |
| 5404 |
else if (mode == COMMAND_EXECUTABLE) |
| 5405 |
res = access( fn, X_OK ) ; |
| 5406 |
else |
| 5407 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ; |
| 5408 |
#endif |
| 5409 |
|
| 5410 |
/* |
| 5411 |
* Perhaps we should analyze the output a bit before returning? |
| 5412 |
* If res==EACCES, that is not really an error, while other errno |
| 5413 |
* code _do_ signify an error. However ... since the return code |
| 5414 |
* a boolean variable, just return it. |
| 5415 |
*/ |
| 5416 |
FreeTSD(fn) ; |
| 5417 |
return (res==0) ; |
| 5418 |
} |
| 5419 |
|
| 5420 |
|
| 5421 |
|
| 5422 |
/* |
| 5423 |
* This little function implements the RESET command of the Rexx |
| 5424 |
* built-in function STREAM(). Basically, most of the job is done in |
| 5425 |
* the function 'fixup_file()'. Except from removing the ERROR flag. |
| 5426 |
* The 'fixup_file()' function is intended for fixing the file at the |
| 5427 |
* start of a condition handler for the NOTREADY condition. |
| 5428 |
* |
| 5429 |
* The value returned from this function is either "READY" or "UNKNOWN", |
| 5430 |
* and reflects the philosophy that the file _is_ fixed, unless it |
| 5431 |
* is impossible to open it. Of course, that may be a false READY, |
| 5432 |
* since the actual _problem_ might not have been fixed, but at least |
| 5433 |
* you have another try at the problem. |
| 5434 |
*/ |
| 5435 |
|
| 5436 |
static streng *reset_file( tsd_t *TSD, fileboxptr fileptr ) |
| 5437 |
{ |
| 5438 |
if (!fileptr) |
| 5439 |
return nullstringptr() ; |
| 5440 |
|
| 5441 |
fixup_file( TSD, fileptr->filename0 ) ; |
| 5442 |
fileptr->flag &= ~(FLAG_ERROR | FLAG_FAKE) ; |
| 5443 |
|
| 5444 |
if (fileptr->fileptr) |
| 5445 |
return Str_creTSD( "READY" ) ; /* Per definition */ |
| 5446 |
else |
| 5447 |
return Str_creTSD( "UNKNOWN" ) ; |
| 5448 |
} |
| 5449 |
|
| 5450 |
|
| 5451 |
|
| 5452 |
/* |
| 5453 |
* The built-in function STREAM() is new in TRL2. It is supposed to be |
| 5454 |
* a sort of all-round function for just about anything having to do with |
| 5455 |
* files. The details of its specification in TRL2 leaves a lot of room |
| 5456 |
* for the implementors. Two of the options to this command -- the Status |
| 5457 |
* and Description options are treated as defined by TRL, the Command |
| 5458 |
* option takes several command, defined by the COMMAND_ macros. |
| 5459 |
*/ |
| 5460 |
streng* std_stream( tsd_t *TSD, cparamboxptr parms ) |
| 5461 |
{ |
| 5462 |
char oper=' ' ; |
| 5463 |
streng *command=NULL, *result=NULL, *filename=NULL, *psub=NULL ; |
| 5464 |
fileboxptr ptr=NULL ; |
| 5465 |
|
| 5466 |
/* Syntax: stream(filename[,[oper][,command ...]]) */ |
| 5467 |
if ((!parms)||(!parms->value)) |
| 5468 |
exiterror( ERR_INCORRECT_CALL, 5, "STREAM", 1 ) ; |
| 5469 |
checkparam( parms, 1, 3 , "STREAM" ) ; |
| 5470 |
|
| 5471 |
/* |
| 5472 |
* Get the filepointer to Rexx's file table, but make sure that the |
| 5473 |
* file is not in any way created if it didn't exist. |
| 5474 |
*/ |
| 5475 |
filename = Str_dupstrTSD( parms->value ); |
| 5476 |
ptr = getfileptr( TSD, filename ) ; |
| 5477 |
/* |
| 5478 |
* Read the 'operation'. This is really just an 'option'. The |
| 5479 |
* default option is 'S'. |
| 5480 |
*/ |
| 5481 |
parms = parms->next ; |
| 5482 |
if (parms && parms->value) |
| 5483 |
oper = getoptionchar( TSD, parms->value, "STREAM", 2, "CSD", "" ) ; |
| 5484 |
else |
| 5485 |
oper = 'S' ; |
| 5486 |
|
| 5487 |
/* |
| 5488 |
* If the operation was 'C', we _must_ have a third parameter, on the |
| 5489 |
* other hand, if it was not 'C', we must never have a third parameter. |
| 5490 |
* Make sure that these rules are followed. |
| 5491 |
*/ |
| 5492 |
command = NULL ; |
| 5493 |
if (oper=='C') |
| 5494 |
{ |
| 5495 |
parms = parms->next ; |
| 5496 |
if (parms && parms->value) |
| 5497 |
command = parms->value ; |
| 5498 |
else |
| 5499 |
exiterror( ERR_INCORRECT_CALL, 3, "STREAM", 3 ) ; |
| 5500 |
} |
| 5501 |
else |
| 5502 |
if (parms && parms->next && parms->next->value) |
| 5503 |
exiterror( ERR_INCORRECT_CALL, 4, "STREAM", 2 ) ; |
| 5504 |
|
| 5505 |
/* |
| 5506 |
* Here comes the main loop. |
| 5507 |
*/ |
| 5508 |
result = NULL ; |
| 5509 |
switch ( oper ) |
| 5510 |
{ |
| 5511 |
case 'C': |
| 5512 |
/* |
| 5513 |
* Read the command, and 'translate' it into an integer which |
| 5514 |
* describes it, see the implementation of get_command(), and |
| 5515 |
* the COMMAND_ macros. The first of these are rather simple, |
| 5516 |
* in fact, they could probably be compressed to save some |
| 5517 |
* space. |
| 5518 |
*/ |
| 5519 |
command = Str_strp( command, ' ', STRIP_BOTH ); |
| 5520 |
oper = get_command( command ) ; |
| 5521 |
switch(oper) |
| 5522 |
{ |
| 5523 |
case COMMAND_READ: /* Regina only - use OPEN READ */ |
| 5524 |
closefile( TSD, filename ) ; |
| 5525 |
ptr = openfile( TSD, filename, ACCESS_READ ) ; |
| 5526 |
break; |
| 5527 |
case COMMAND_WRITE: /* Regina only - use OPEN WRITE */ |
| 5528 |
closefile( TSD, filename ) ; |
| 5529 |
ptr = openfile( TSD, filename, ACCESS_WRITE ) ; |
| 5530 |
break; |
| 5531 |
case COMMAND_APPEND: /* Regina only - use OPEN WRITE APPEND */ |
| 5532 |
closefile( TSD, filename ) ; |
| 5533 |
ptr = openfile( TSD, filename, ACCESS_APPEND ) ; |
| 5534 |
break; |
| 5535 |
case COMMAND_UPDATE: /* Regina only - use OPEN BOTH */ |
| 5536 |
closefile( TSD, filename ) ; |
| 5537 |
ptr = openfile( TSD, filename, ACCESS_UPDATE ) ; |
| 5538 |
break; |
| 5539 |
case COMMAND_CREATE: /* Regina only - use OPEN WRITE REPLACE */ |
| 5540 |
closefile( TSD, filename ) ; |
| 5541 |
ptr = openfile( TSD, filename, ACCESS_CREATE ) ; |
| 5542 |
break; |
| 5543 |
case COMMAND_CLOSE: |
| 5544 |
/* |
| 5545 |
* The file is always unknown after is has been closed. Does |
| 5546 |
* that sound convincing, or does it sound like I didn't feel |
| 5547 |
* to implement the rest of this ... ? |
| 5548 |
*/ |
| 5549 |
closefile( TSD, filename ) ; |
| 5550 |
result = Str_creTSD( "UNKNOWN" ) ; |
| 5551 |
break ; |
| 5552 |
case COMMAND_FLUSH: |
| 5553 |
/* |
| 5554 |
* Flush the file. Actually, this might not be needed, since |
| 5555 |
* the functions that write out data may contain explicit |
| 5556 |
* calls to fflush() |
| 5557 |
*/ |
| 5558 |
ptr = getfileptr( TSD, filename ) ; |
| 5559 |
if (ptr && ptr->fileptr) |
| 5560 |
{ |
| 5561 |
errno = 0 ; |
| 5562 |
if (fflush( ptr->fileptr)) |
| 5563 |
{ |
| 5564 |
file_error( ptr, errno, NULL ) ; |
| 5565 |
result = Str_creTSD( "ERROR" ) ; |
| 5566 |
} |
| 5567 |
else |
| 5568 |
result = Str_creTSD( "READY" ) ; |
| 5569 |
} |
| 5570 |
else if (ptr) |
| 5571 |
result = Str_creTSD( "ERROR" ) ; |
| 5572 |
else |
| 5573 |
result = Str_creTSD( "UNKNOWN" ) ; |
| 5574 |
break ; |
| 5575 |
case COMMAND_STATUS: |
| 5576 |
ptr = getfileptr( TSD, filename ) ; |
| 5577 |
result = getrexxstatus( TSD, ptr ) ; |
| 5578 |
break; |
| 5579 |
case COMMAND_FSTAT: |
| 5580 |
result = getstatus( TSD, filename, COMMAND_FSTAT ); |
| 5581 |
break; |
| 5582 |
case COMMAND_RESET: |
| 5583 |
ptr = getfileptr( TSD, filename ) ; |
| 5584 |
result = reset_file( TSD, ptr ) ; |
| 5585 |
break; |
| 5586 |
case COMMAND_READABLE: |
| 5587 |
case COMMAND_WRITEABLE: |
| 5588 |
case COMMAND_EXECUTABLE: |
| 5589 |
result = int_to_streng( TSD, is_accessable( TSD, filename, oper )) ; |
| 5590 |
break; |
| 5591 |
case COMMAND_QUERY: |
| 5592 |
/* |
| 5593 |
* We have to further parse the remainder of the command |
| 5594 |
* to determine what sub-command has been passed. |
| 5595 |
*/ |
| 5596 |
psub = Str_nodupTSD( command , 5, command->len - 5); |
| 5597 |
psub = Str_strp( psub, ' ', STRIP_LEADING); |
| 5598 |
result = getquery( TSD, filename, psub ) ; |
| 5599 |
Free_stringTSD(psub); |
| 5600 |
break; |
| 5601 |
case COMMAND_OPEN: |
| 5602 |
/* |
| 5603 |
* We have to further parse the remainder of the command |
| 5604 |
* to determine what sub-command has been passed. |
| 5605 |
*/ |
| 5606 |
psub = Str_nodupTSD( command , 4, command->len - 4); |
| 5607 |
psub = Str_strp( psub, ' ', STRIP_LEADING); |
| 5608 |
result = getopen( TSD, filename, psub ) ; |
| 5609 |
Free_stringTSD(psub); |
| 5610 |
break; |
| 5611 |
case COMMAND_SEEK: |
| 5612 |
psub = Str_nodupTSD( command , 4, command->len - 4); |
| 5613 |
psub = Str_strp( psub, ' ', STRIP_LEADING); |
| 5614 |
result = getseek( TSD, filename, psub ) ; |
| 5615 |
Free_stringTSD(psub); |
| 5616 |
break; |
| 5617 |
case COMMAND_POSITION: |
| 5618 |
psub = Str_nodupTSD( command , 8, command->len - 8); |
| 5619 |
psub = Str_strp( psub, ' ', STRIP_LEADING); |
| 5620 |
result = getseek( TSD, filename, psub ) ; |
| 5621 |
Free_stringTSD(psub); |
| 5622 |
break; |
| 5623 |
default: |
| 5624 |
exiterror( ERR_STREAM_COMMAND, 3, "CLOSE FLUSH OPEN POSITION QUERY RESET SEEK STATUS EXECUTABLE FSTAT READABLE WRITABLE", tmpstr_of( TSD, command ) ) ; |
| 5625 |
break; |
| 5626 |
} |
| 5627 |
break ; |
| 5628 |
|
| 5629 |
case 'D': |
| 5630 |
/* |
| 5631 |
* Get a description of the most recent error for this file |
| 5632 |
*/ |
| 5633 |
if (ptr) |
| 5634 |
{ |
| 5635 |
if (ptr->errmsg) |
| 5636 |
result = Str_dupTSD(ptr->errmsg) ; |
| 5637 |
else if (ptr->error) |
| 5638 |
result = Str_creTSD( strerror(ptr->error) ) ; |
| 5639 |
} |
| 5640 |
break ; |
| 5641 |
|
| 5642 |
case 'S': |
| 5643 |
/* |
| 5644 |
* Get a simple status for the file in question. If the file |
| 5645 |
* doesn't exist in Rexx's tables, UNKNOWN is returned. If the |
| 5646 |
* file is in error state, return ERROR, else return READY, |
| 5647 |
* unless current read position is at EOF, in which case |
| 5648 |
* NOTREADY is return. Note that ERROR and NOTREADY are the |
| 5649 |
* two states that will raise the NOTREADY condition. |
| 5650 |
*/ |
| 5651 |
if (ptr) |
| 5652 |
{ |
| 5653 |
if (ptr->flag & FLAG_ERROR) |
| 5654 |
{ |
| 5655 |
result = Str_creTSD( "ERROR" ) ; |
| 5656 |
} |
| 5657 |
#if 1 /* really MH */ |
| 5658 |
else if (ptr->flag & FLAG_AFTER_RDEOF) |
| 5659 |
{ |
| 5660 |
result = Str_creTSD( "NOTREADY" ) ; |
| 5661 |
} |
| 5662 |
#else |
| 5663 |
else if (ptr->flag & FLAG_RDEOF) |
| 5664 |
{ |
| 5665 |
result = Str_creTSD( "NOTREADY" ) ; |
| 5666 |
} |
| 5667 |
#endif |
| 5668 |
else |
| 5669 |
{ |
| 5670 |
result = Str_creTSD( "READY" ) ; |
| 5671 |
} |
| 5672 |
} |
| 5673 |
else |
| 5674 |
result = Str_creTSD( "UNKNOWN" ) ; |
| 5675 |
|
| 5676 |
break ; |
| 5677 |
|
| 5678 |
default: |
| 5679 |
exiterror( ERR_INTERPRETER_FAILURE, 1, __FILE__, __LINE__, "" ) ; |
| 5680 |
} |
| 5681 |
|
| 5682 |
if (result==NULL) |
| 5683 |
result = nullstringptr() ; |
| 5684 |
|
| 5685 |
Free_stringTSD(filename); |
| 5686 |
return result ; |
| 5687 |
} |
| 5688 |
|
| 5689 |
|
| 5690 |
|
| 5691 |
/* |
| 5692 |
* This routine will traverse the list of open files, and dump relevant |
| 5693 |
* information about each of them. Really a debugging routine. It is |
| 5694 |
* not available when Regina is compiled with optimalization. |
| 5695 |
*/ |
| 5696 |
#ifndef NDEBUG |
| 5697 |
streng *dbg_dumpfiles( tsd_t *TSD, cparamboxptr parms ) |
| 5698 |
{ |
| 5699 |
fileboxptr ptr ; |
| 5700 |
int i=0, fno ; |
| 5701 |
char string[11] ; |
| 5702 |
fil_tsd_t *ft; |
| 5703 |
char opt='N' ; |
| 5704 |
int slot; |
| 5705 |
|
| 5706 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 5707 |
|
| 5708 |
checkparam( parms, 0, 2 , "DUMPFILES" ) ; |
| 5709 |
|
| 5710 |
if ( parms && parms->value ) |
| 5711 |
opt = getoptionchar( TSD, parms->value, "DUMPFILES", 1, "LN", "" ) ; |
| 5712 |
|
| 5713 |
if ( parms ) |
| 5714 |
parms = parms->next ; |
| 5715 |
|
| 5716 |
if ( parms && parms->value ) |
| 5717 |
slot = atopos( TSD, parms->value, "DUMPFILES", 2 ) ; |
| 5718 |
else |
| 5719 |
slot = -1 ; /* dump all slots */ |
| 5720 |
|
| 5721 |
if (TSD->stddump == NULL) |
| 5722 |
return nullstringptr() ; |
| 5723 |
|
| 5724 |
if ( opt == 'N' ) |
| 5725 |
{ |
| 5726 |
fprintf(TSD->stddump, |
| 5727 |
" Read Write\n" ) ; |
| 5728 |
fprintf(TSD->stddump, |
| 5729 |
"File Filename Flags line char line char\n"); |
| 5730 |
|
| 5731 |
for ( ptr = ft->mrufile; ptr ; ptr = ptr->older ) |
| 5732 |
{ |
| 5733 |
fno = fileno( ptr->fileptr ) ; |
| 5734 |
fprintf( TSD->stddump,"%4d %-30s", fno, ptr->filename0->value); |
| 5735 |
i = 0 ; |
| 5736 |
|
| 5737 |
string[0] = (char) (( ptr->flag & FLAG_READ ) ? 'r' : ' ') ; |
| 5738 |
string[1] = (char) (( ptr->flag & FLAG_WRITE ) ? 'w' : ' ') ; |
| 5739 |
string[2] = (char) (( ptr->flag & FLAG_PERSIST ) ? 'p' : 't') ; |
| 5740 |
string[3] = (char) (( ptr->flag & FLAG_RDEOF ) ? 'R' : ' ') ; |
| 5741 |
string[4] = (char) (( ptr->flag & FLAG_AFTER_RDEOF ) ? 'A' : ' ') ; |
| 5742 |
string[5] = (char) (( ptr->flag & FLAG_WREOF ) ? 'W' : ' ') ; |
| 5743 |
string[6] = (char) (( ptr->flag & FLAG_SURVIVOR ) ? 'S' : ' ') ; |
| 5744 |
string[7] = (char) (( ptr->flag & FLAG_ERROR ) ? 'E' : ' ') ; |
| 5745 |
string[8] = (char) (((ptr->flag & FLAG_FAKE) && (ptr->flag & FLAG_ERROR) ) ? 'F' : ' ') ; |
| 5746 |
string[9] = 0x00 ; |
| 5747 |
|
| 5748 |
#if defined(HAVE_I64U) |
| 5749 |
fprintf( TSD->stddump, " %8s %4I64u %4I64u %4I64u %4I64u\n", string, |
| 5750 |
ptr->readline, ptr->readpos, |
| 5751 |
ptr->writeline,ptr->writepos ) ; |
| 5752 |
#else |
| 5753 |
if ( sizeof(rx_64) > 4 ) |
| 5754 |
fprintf( TSD->stddump, " %8s %4lld %4lld %4lld %4lld\n", string, |
| 5755 |
ptr->readline, ptr->readpos, |
| 5756 |
ptr->writeline,ptr->writepos ) ; |
| 5757 |
else |
| 5758 |
fprintf( TSD->stddump, " %8s %4ld %4ld %4ld %4ld\n", string, |
| 5759 |
(long)(ptr->readline), (long)(ptr->readpos), |
| 5760 |
(long)(ptr->writeline),(long)(ptr->writepos) ) ; |
| 5761 |
#endif |
| 5762 |
if (ptr->flag & FLAG_ERROR) |
| 5763 |
{ |
| 5764 |
if (ptr->errmsg) |
| 5765 |
fprintf(TSD->stddump, " ==> %s\n", ptr->errmsg->value ) ; |
| 5766 |
else if (ptr->error) |
| 5767 |
fprintf(TSD->stddump, " ==> %s\n", strerror( ptr->error )) ; |
| 5768 |
} |
| 5769 |
} |
| 5770 |
fprintf( TSD->stddump," r=read, w=write, p=persistent, t=transient, e=eof\n"); |
| 5771 |
fprintf( TSD->stddump," R=read-eof, W=write-eof, S=special, E=error, F=fake\n"); |
| 5772 |
} |
| 5773 |
else |
| 5774 |
{ |
| 5775 |
int start, end; |
| 5776 |
if ( slot == -1 ) |
| 5777 |
{ |
| 5778 |
start = 0; |
| 5779 |
end = 131; |
| 5780 |
} |
| 5781 |
else |
| 5782 |
{ |
| 5783 |
start = slot; |
| 5784 |
end = start + 1; |
| 5785 |
} |
| 5786 |
for ( i = start; i < end; i++ ) |
| 5787 |
{ |
| 5788 |
ptr = ft->filehash[i]; |
| 5789 |
if ( ptr ) |
| 5790 |
{ |
| 5791 |
fno = 0; |
| 5792 |
if ( ptr->fileptr ) |
| 5793 |
fno = fileno( ptr->fileptr ) ; |
| 5794 |
fprintf( TSD->stddump,"Slot: %3d: %4d %8lx prev: %8lx next: %8lx %-30s\n", |
| 5795 |
i, fno, (long)ptr, (long)ptr->prev, (long)ptr->next, ptr->filename0->value); |
| 5796 |
for ( ptr = ptr->next; ptr; ptr = ptr->next ) |
| 5797 |
{ |
| 5798 |
fno = 0; |
| 5799 |
if ( ptr->fileptr ) |
| 5800 |
fno = fileno( ptr->fileptr ) ; |
| 5801 |
fprintf( TSD->stddump," %4d %8lx prev: %8lx next: %8lx %-30s\n", |
| 5802 |
fno, (long)ptr, (long)ptr->prev, (long)ptr->next, ptr->filename0->value ); |
| 5803 |
} |
| 5804 |
} |
| 5805 |
else |
| 5806 |
{ |
| 5807 |
fprintf( TSD->stddump,"Slot: %3d is empty\n", i ); |
| 5808 |
} |
| 5809 |
} |
| 5810 |
} |
| 5811 |
|
| 5812 |
return nullstringptr() ; |
| 5813 |
} |
| 5814 |
#endif |
| 5815 |
|
| 5816 |
|
| 5817 |
|
| 5818 |
|
| 5819 |
/* |
| 5820 |
* Read from stdin using readoneline() |
| 5821 |
*/ |
| 5822 |
streng *readkbdline( tsd_t *TSD ) |
| 5823 |
{ |
| 5824 |
fil_tsd_t *ft; |
| 5825 |
|
| 5826 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 5827 |
return readoneline( TSD, ft->stdio_ptr[DEFAULT_STDIN_INDEX] ); |
| 5828 |
} |
| 5829 |
|
| 5830 |
void *addr_reopen_file( tsd_t *TSD, const streng *filename, char code, |
| 5831 |
int iserror ) |
| 5832 |
/* This is the open routine for the ADDRESS WITH-redirection. filename is |
| 5833 |
* the name of the file. code is either 'r' for "READ", |
| 5834 |
* 'A' for "WRITE APPEND", 'R' for "WRITE REPLACE". In case of READ |
| 5835 |
* already opened files will be reused. In case of APPEND or REPLACE the |
| 5836 |
* files are (re-)opened. An internal structure for files is returned and |
| 5837 |
* should be used for calls to addr_io_file. |
| 5838 |
* An already opened file for write can't be used. See J18PUB.pdf, 5.5.1. |
| 5839 |
* The return value may be NULL in case of an error. A NOTREADY condition |
| 5840 |
* may have been raised in this case. |
| 5841 |
* filename may be NULL for a default file. |
| 5842 |
* iserror can be set or not. If set, stderr instead of stdout should be used. |
| 5843 |
*/ |
| 5844 |
{ |
| 5845 |
fileboxptr ptr; |
| 5846 |
fil_tsd_t *ft; |
| 5847 |
|
| 5848 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 5849 |
|
| 5850 |
iserror = ( iserror ) ? 1 : 0; |
| 5851 |
switch ( code ) |
| 5852 |
{ |
| 5853 |
case 'r': |
| 5854 |
if ( ( filename == NULL ) || ( Str_len( filename ) == 0 ) ) |
| 5855 |
return ft->stdio_ptr[DEFAULT_STDIN_INDEX]; |
| 5856 |
ptr = get_file_ptr( TSD, filename, OPER_READ, ACCESS_READ ); |
| 5857 |
if ( ptr != NULL ) |
| 5858 |
ptr->readpos = 0; |
| 5859 |
break; |
| 5860 |
|
| 5861 |
case 'A': |
| 5862 |
if ( ( filename == NULL ) || ( Str_len( filename ) == 0 ) ) |
| 5863 |
return ft->stdio_ptr[DEFAULT_STDOUT_INDEX + iserror]; |
| 5864 |
if ( ( ptr = getfileptr( TSD, filename ) ) != NULL ) |
| 5865 |
{ |
| 5866 |
if ( ptr->flag & FLAG_SURVIVOR ) |
| 5867 |
return get_file_ptr( TSD, filename, OPER_WRITE, ACCESS_WRITE ); |
| 5868 |
} |
| 5869 |
closefile( TSD, filename ); |
| 5870 |
ptr = openfile( TSD, filename, ACCESS_STREAM_APPEND ); |
| 5871 |
break; |
| 5872 |
|
| 5873 |
case 'R': |
| 5874 |
if ( ( filename == NULL ) || ( Str_len( filename ) == 0 ) ) |
| 5875 |
return ft->stdio_ptr[DEFAULT_STDOUT_INDEX + iserror]; |
| 5876 |
if ( ( ptr = getfileptr( TSD, filename ) ) != NULL ) |
| 5877 |
{ |
| 5878 |
if ( ptr->flag & FLAG_SURVIVOR ) |
| 5879 |
return get_file_ptr( TSD, filename, OPER_WRITE, ACCESS_WRITE ); |
| 5880 |
} |
| 5881 |
closefile( TSD, filename ); |
| 5882 |
ptr = openfile( TSD, filename, ACCESS_STREAM_REPLACE ); |
| 5883 |
break; |
| 5884 |
|
| 5885 |
default: |
| 5886 |
ptr = NULL; |
| 5887 |
break; |
| 5888 |
} |
| 5889 |
|
| 5890 |
if ( ( ptr != NULL ) && ( ptr->fileptr == NULL ) ) |
| 5891 |
ptr = NULL; |
| 5892 |
|
| 5893 |
return ptr; |
| 5894 |
} |
| 5895 |
|
| 5896 |
streng *addr_io_file( tsd_t *TSD, void *fileptr, const streng *buffer ) |
| 5897 |
/* This is the working routine for the ADDRESS WITH-redirection. fileptr is |
| 5898 |
* the return value of addr_reopen_file. buffer must be NULL for a read |
| 5899 |
* operation or a filled buffer. |
| 5900 |
* The return value is NULL in case of a write operation or in case of EOF |
| 5901 |
* while reading. |
| 5902 |
* All IO is done by charin/charout. |
| 5903 |
* A NOTREADY condition won't be raised. |
| 5904 |
*/ |
| 5905 |
{ |
| 5906 |
streng *retval = NULL ; |
| 5907 |
|
| 5908 |
if ( fileptr == NULL ) /* fixes bug 806948 */ |
| 5909 |
return retval; |
| 5910 |
|
| 5911 |
if ( buffer == NULL ) |
| 5912 |
retval = readbytes( TSD, (fileboxptr)fileptr, 0x1000, 1 ) ; |
| 5913 |
else |
| 5914 |
writebytes( TSD, (fileboxptr)fileptr, buffer, 1 ) ; |
| 5915 |
|
| 5916 |
return( retval ) ; |
| 5917 |
} |
| 5918 |
|
| 5919 |
void addr_reset_file( tsd_t *TSD, void *fileptr ) |
| 5920 |
/* This is the "close" routine for the ADDRESS WITH-redirection. We don't close |
| 5921 |
* the stream, we simply perform a reset, that's enough to restart reading |
| 5922 |
* or writing. |
| 5923 |
*/ |
| 5924 |
{ |
| 5925 |
fileboxptr ptr = (fileboxptr)fileptr; |
| 5926 |
|
| 5927 |
if ( fileptr == NULL ) /* fixes bug 806948 */ |
| 5928 |
return; |
| 5929 |
|
| 5930 |
if ( ptr->fileptr ) |
| 5931 |
{ |
| 5932 |
clearerr( ptr->fileptr ); |
| 5933 |
if ( ptr->flag & FLAG_PERSIST ) |
| 5934 |
rx_fseek( ptr->fileptr, 0, SEEK_SET ); |
| 5935 |
ptr->thispos = 0; |
| 5936 |
ptr->oper = OPER_NONE; |
| 5937 |
} |
| 5938 |
|
| 5939 |
if ( ptr->flag & FLAG_SURVIVOR ) |
| 5940 |
ptr->flag &= ~FLAG_ERROR; |
| 5941 |
|
| 5942 |
ptr->flag &= ~FLAG_FAKE; |
| 5943 |
} |
| 5944 |
|
| 5945 |
|
| 5946 |
streng *addr_file_info( tsd_t *TSD, const streng *source, int defchannel ) |
| 5947 |
/* |
| 5948 |
* addr_file_info is a helper for the ADDRESS WITH-redirection. source is the |
| 5949 |
* name of the file and may be NULL for a default channel. The channel's number |
| 5950 |
* is used then, 0 = stdin, 1 = stdout, 2 = stderr. |
| 5951 |
* The return name is a fresh copy of the qualified name of the file. |
| 5952 |
*/ |
| 5953 |
{ |
| 5954 |
fil_tsd_t *ft; |
| 5955 |
streng *result; |
| 5956 |
fileboxptr p; |
| 5957 |
|
| 5958 |
ft = (fil_tsd_t *)TSD->fil_tsd; |
| 5959 |
|
| 5960 |
/* |
| 5961 |
* We don't know anything about the default channels. So just return our |
| 5962 |
* internal name. |
| 5963 |
*/ |
| 5964 |
if ( source == NULL ) |
| 5965 |
return Str_dupstrTSD( ft->stdio_ptr[ defchannel ]->filename0 ); |
| 5966 |
|
| 5967 |
/* |
| 5968 |
* Check for well known devices, the SURVIVORs. Return a comparable name |
| 5969 |
* which makes sure "<stdin>" is equal to "stdin". |
| 5970 |
*/ |
| 5971 |
if ( ( p = getfileptr( TSD, source ) ) != NULL ) |
| 5972 |
{ |
| 5973 |
if ( p->flag & FLAG_SURVIVOR ) |
| 5974 |
{ |
| 5975 |
if ( p->fileptr == stdin ) |
| 5976 |
defchannel = 0; |
| 5977 |
else if ( p->fileptr == stdout ) |
| 5978 |
defchannel = 1; |
| 5979 |
else |
| 5980 |
defchannel = 2; |
| 5981 |
|
| 5982 |
return Str_dupstrTSD( ft->stdio_ptr[ defchannel ]->filename0 ); |
| 5983 |
} |
| 5984 |
} |
| 5985 |
|
| 5986 |
/* |
| 5987 |
* Even in case of character devices a name resolution makes sense, e.g. |
| 5988 |
* "tty" may expand to "/dev/tty" or the fifo "local" may expand to |
| 5989 |
* "/somethere/local". |
| 5990 |
*/ |
| 5991 |
result = Str_makeTSD( REXX_PATH_MAX ); |
| 5992 |
my_fullpathstreng( TSD, result->value, source ); |
| 5993 |
result->len = strlen( result->value ); |
| 5994 |
|
| 5995 |
return result; |
| 5996 |
} |
| 5997 |
|
| 5998 |
|
| 5999 |
/* |
| 6000 |
* This routine is not really interesting. You should use the STREAM() |
| 6001 |
* built-in function for greater portability and functionality. It is |
| 6002 |
* left in the code for portability reasons. |
| 6003 |
*/ |
| 6004 |
streng *unx_open( tsd_t *TSD, cparamboxptr parms ) |
| 6005 |
{ |
| 6006 |
fileboxptr ptr=NULL ; |
| 6007 |
char ch=' ' ; |
| 6008 |
int iaccess=ACCESS_NONE ; |
| 6009 |
|
| 6010 |
checkparam( parms, 1, 2 , "OPEN" ) ; |
| 6011 |
|
| 6012 |
if ((parms->next)&&(parms->next->value)) |
| 6013 |
{ |
| 6014 |
ch = getoptionchar( TSD, parms->next->value, "OPEN", 2, "RW", "" ) ; |
| 6015 |
if ( ch == 'R' ) /* bja */ |
| 6016 |
iaccess = ACCESS_READ ; |
| 6017 |
else if ( ch == 'W' ) /* bja */ |
| 6018 |
iaccess = ACCESS_WRITE ; |
| 6019 |
else |
| 6020 |
assert( 0 ) ; |
| 6021 |
} |
| 6022 |
else |
| 6023 |
iaccess = ACCESS_READ ; |
| 6024 |
|
| 6025 |
ptr = openfile( TSD, parms->value, iaccess ) ; |
| 6026 |
|
| 6027 |
return int_to_streng( TSD,( ptr && ptr->fileptr )) ; |
| 6028 |
} |
| 6029 |
|
| 6030 |
|
| 6031 |
/* |
| 6032 |
* This routine is not really interesting. You should use the CLOSE |
| 6033 |
* command of the STREAM() built-in function for greater portability |
| 6034 |
* and compatibility. It is left in the code only for compatibility |
| 6035 |
* reasons. |
| 6036 |
*/ |
| 6037 |
streng *unx_close( tsd_t *TSD, cparamboxptr parms ) |
| 6038 |
{ |
| 6039 |
fileboxptr ptr=NULL ; |
| 6040 |
|
| 6041 |
checkparam( parms, 1, 1 , "CLOSE" ) ; |
| 6042 |
ptr = getfileptr( TSD, parms->value ) ; |
| 6043 |
closefile( TSD, parms->value ) ; |
| 6044 |
|
| 6045 |
return int_to_streng( TSD, ptr!=NULL ) ; |
| 6046 |
} |
| 6047 |
|
| 6048 |
|
| 6049 |
/* |
| 6050 |
* a function called exists that checks if a file with a certain name |
| 6051 |
* exists. This function was taken from the ARexx API. |
| 6052 |
*/ |
| 6053 |
streng *arexx_exists( tsd_t *TSD, cparamboxptr parms ) |
| 6054 |
{ |
| 6055 |
char *name; |
| 6056 |
streng *retval; |
| 6057 |
struct rx_stat_buf st; |
| 6058 |
|
| 6059 |
checkparam( parms, 1, 1, "EXISTS" ) ; |
| 6060 |
|
| 6061 |
name = str_of( TSD, parms->value ) ; |
| 6062 |
retval = int_to_streng( TSD, my_rx_stat( TSD, name, &st ) != -1 ) ; |
| 6063 |
Free_TSD( TSD, name ) ; |
| 6064 |
|
| 6065 |
return retval; |
| 6066 |
} |
| 6067 |
|
| 6068 |
/* |
| 6069 |
* get_external_routine_file opens a file in binary mode and returns the |
| 6070 |
* fully qualified path name on success. NULL is returned otherwise. |
| 6071 |
* The opened file pointer is returned in *fp. |
| 6072 |
*/ |
| 6073 |
static streng *get_external_routine_file( const tsd_t *TSD, |
| 6074 |
const char *inname, FILE **fp ) |
| 6075 |
{ |
| 6076 |
char buf[3 * REXX_PATH_MAX + 1]; |
| 6077 |
|
| 6078 |
#ifdef VMS |
| 6079 |
*fp = fopen( inname, "r" ); |
| 6080 |
#else |
| 6081 |
*fp = fopen( inname, "rb" ); |
| 6082 |
#endif |
| 6083 |
if ( *fp == NULL ) |
| 6084 |
return NULL; |
| 6085 |
|
| 6086 |
my_fullpath( TSD, buf, inname ); |
| 6087 |
|
| 6088 |
return Str_crestrTSD( buf ); |
| 6089 |
} |
| 6090 |
|
| 6091 |
/* |
| 6092 |
* See get_external_routine for comments. This function processes one path |
| 6093 |
* element which is passed in path. |
| 6094 |
* suffixes is either NULL or the list of extra suffixes which should be |
| 6095 |
* tested. *fp must be NULL on entry. |
| 6096 |
* path may be NULL if no further directory processing shall happen. |
| 6097 |
*/ |
| 6098 |
static streng *get_external_routine_path( const tsd_t *TSD, |
| 6099 |
const char *inname, FILE **fp, |
| 6100 |
const char *path, |
| 6101 |
const char *suffixes, |
| 6102 |
int emptySuffixAllowed ) |
| 6103 |
{ |
| 6104 |
char outname[REXX_PATH_MAX+1]; |
| 6105 |
int i,ilen,hlen; |
| 6106 |
streng *retval; |
| 6107 |
static const char *default_suffixes = "rexx,rex,cmd,rx"; |
| 6108 |
const char *suffixlist[2]; |
| 6109 |
const char *suffix; |
| 6110 |
int suffixlen; |
| 6111 |
|
| 6112 |
ilen = strlen( inname ); |
| 6113 |
if ( !path ) |
| 6114 |
hlen = 0; |
| 6115 |
else |
| 6116 |
hlen = strlen( path ); |
| 6117 |
if ( !hlen ) |
| 6118 |
{ |
| 6119 |
if ( ilen > REXX_PATH_MAX ) |
| 6120 |
return NULL; |
| 6121 |
strcpy( outname, inname ); |
| 6122 |
} |
| 6123 |
else |
| 6124 |
{ |
| 6125 |
if ( ( strchr( FILE_SEPARATORS, inname[0] ) == NULL ) && |
| 6126 |
( strchr( FILE_SEPARATORS, path[hlen - 1] ) == NULL ) ) |
| 6127 |
{ |
| 6128 |
if ( ilen + hlen + 1 > REXX_PATH_MAX ) |
| 6129 |
return NULL; |
| 6130 |
strcpy( outname, path ); |
| 6131 |
strcat( outname, FILE_SEPARATOR_STR ); |
| 6132 |
strcat( outname, inname ); |
| 6133 |
} |
| 6134 |
else |
| 6135 |
{ |
| 6136 |
if ( ilen + hlen > REXX_PATH_MAX ) |
| 6137 |
return NULL; |
| 6138 |
strcpy( outname, path ); |
| 6139 |
strcat( outname, inname ); |
| 6140 |
} |
| 6141 |
} |
| 6142 |
|
| 6143 |
/* |
| 6144 |
* The filename is constructed. Try without fiddling with suffixes first. |
| 6145 |
*/ |
| 6146 |
if ( emptySuffixAllowed ) |
| 6147 |
{ |
| 6148 |
if ( ( retval = get_external_routine_file( TSD, outname, fp ) ) != NULL ) |
| 6149 |
return retval; |
| 6150 |
} |
| 6151 |
|
| 6152 |
/* |
| 6153 |
* Next try the supplied suffix list, then try the default list. |
| 6154 |
* First check if a known extension exists, after every check do the |
| 6155 |
* application. |
| 6156 |
*/ |
| 6157 |
|
| 6158 |
suffixlist[0] = suffixes; |
| 6159 |
suffixlist[1] = default_suffixes; |
| 6160 |
ilen = strlen( outname ); |
| 6161 |
#define IsDelim(c) ( ( (c) == ',' ) || ( (c) == '.' ) || \ |
| 6162 |
( (c) == PATH_SEPARATOR ) || rx_isspace(c) ) |
| 6163 |
for ( i = 0; i < 2; i++ ) |
| 6164 |
{ |
| 6165 |
suffixes = suffixlist[i]; |
| 6166 |
|
| 6167 |
while ( suffixes ) |
| 6168 |
{ |
| 6169 |
while ( IsDelim(*suffixes) ) |
| 6170 |
suffixes++; |
| 6171 |
if ( *suffixes == '\0' ) |
| 6172 |
break; |
| 6173 |
|
| 6174 |
for ( suffixlen = 1; !IsDelim(suffixes[suffixlen]); suffixlen++ ) |
| 6175 |
if ( suffixes[suffixlen] == '\0' ) |
| 6176 |
break; |
| 6177 |
|
| 6178 |
suffix = suffixes; |
| 6179 |
suffixes += suffixlen; |
| 6180 |
|
| 6181 |
if ( suffixlen + 1 > ilen ) |
| 6182 |
continue; |
| 6183 |
if ( outname[ ilen - suffixlen - 1 ] != '.' ) |
| 6184 |
continue; |
| 6185 |
#ifdef CASE_SENSITIVE_FILENAMES |
| 6186 |
if ( memcmp( suffix, outname + ilen - suffixlen, suffixlen - 1 ) ) |
| 6187 |
#else |
| 6188 |
if ( mem_cmpic( suffix, outname + ilen - suffixlen, suffixlen - 1 ) ) |
| 6189 |
#endif |
| 6190 |
continue; |
| 6191 |
|
| 6192 |
/* |
| 6193 |
* A matching suffix forces us to terminate every further seeking a |
| 6194 |
* proper file. |
| 6195 |
*/ |
| 6196 |
if ( !emptySuffixAllowed ) |
| 6197 |
return get_external_routine_file( TSD, outname, fp ); |
| 6198 |
return NULL; |
| 6199 |
} |
| 6200 |
} |
| 6201 |
|
| 6202 |
/* |
| 6203 |
* Try the extensions. |
| 6204 |
*/ |
| 6205 |
for ( i = 0; i < 2; i++ ) |
| 6206 |
{ |
| 6207 |
suffixes = suffixlist[i]; |
| 6208 |
|
| 6209 |
while ( suffixes ) |
| 6210 |
{ |
| 6211 |
while ( IsDelim(*suffixes) ) |
| 6212 |
suffixes++; |
| 6213 |
if ( *suffixes == '\0' ) |
| 6214 |
break; |
| 6215 |
|
| 6216 |
for ( suffixlen = 1; !IsDelim(suffixes[suffixlen]); suffixlen++ ) |
| 6217 |
if ( suffixes[suffixlen] == '\0' ) |
| 6218 |
break; |
| 6219 |
|
| 6220 |
suffix = suffixes; |
| 6221 |
suffixes += suffixlen; |
| 6222 |
|
| 6223 |
if ( suffixlen + 1 + ilen > REXX_PATH_MAX ) |
| 6224 |
continue; |
| 6225 |
outname[ ilen ] = '.'; |
| 6226 |
memcpy( outname + ilen + 1, suffix, suffixlen ); |
| 6227 |
outname[ilen + 1 + suffixlen] = '\0'; |
| 6228 |
if ( ( retval = get_external_routine_file( TSD, outname, fp ) ) != |
| 6229 |
NULL ) |
| 6230 |
return retval; |
| 6231 |
} |
| 6232 |
} |
| 6233 |
|
| 6234 |
#undef Delim |
| 6235 |
return NULL; |
| 6236 |
} |
| 6237 |
|
| 6238 |
/* |
| 6239 |
* See get_external_routine for comments. This function processes a list of |
| 6240 |
* path elements delimited by the path separator which is passed in paths. |
| 6241 |
* suffixes is either NULL or the list of extra suffixes which should be |
| 6242 |
* tested. *fp must be NULL on entry. |
| 6243 |
* paths will be destroyed. |
| 6244 |
*/ |
| 6245 |
static streng *get_external_routine_paths( const tsd_t *TSD, |
| 6246 |
const char *inname, FILE **fp, |
| 6247 |
char *paths, const char *suffixes, |
| 6248 |
int emptySuffixAllowed ) |
| 6249 |
{ |
| 6250 |
char *path; |
| 6251 |
streng *retval; |
| 6252 |
|
| 6253 |
if ( *paths == '\0' ) |
| 6254 |
return NULL; |
| 6255 |
|
| 6256 |
while ( paths ) |
| 6257 |
{ |
| 6258 |
path = paths; |
| 6259 |
paths = strchr( paths, PATH_SEPARATOR ); |
| 6260 |
if ( paths != NULL ) |
| 6261 |
*paths++ = '\0'; |
| 6262 |
|
| 6263 |
if ( *path == '\0') |
| 6264 |
{ |
| 6265 |
/* |
| 6266 |
* An empty string is counted as "." in unix systems and ignored in |
| 6267 |
* all other systems. |
| 6268 |
*/ |
| 6269 |
#ifdef UNIX |
| 6270 |
path = "."; |
| 6271 |
#else |
| 6272 |
continue; |
| 6273 |
#endif |
| 6274 |
} |
| 6275 |
|
| 6276 |
retval = get_external_routine_path( TSD, inname, fp, path, suffixes, |
| 6277 |
emptySuffixAllowed ); |
| 6278 |
|
| 6279 |
if ( retval ) |
| 6280 |
return retval; |
| 6281 |
} |
| 6282 |
|
| 6283 |
return NULL; |
| 6284 |
} |
| 6285 |
|
| 6286 |
/* |
| 6287 |
* get_external_routine searches for a script called inname. Some paths are |
| 6288 |
* search if the file is not found and an extension may be added if no file is |
| 6289 |
* found. |
| 6290 |
* |
| 6291 |
* On success *fp is set to the opened (binary) file and the return value is |
| 6292 |
* the fully qualified file name. If no file was found the return value is |
| 6293 |
* NULL and *fp will be NULL, too. |
| 6294 |
* The returned file name is extended by a terminating '\0' without counting |
| 6295 |
* is in the string's length. |
| 6296 |
* |
| 6297 |
* This is the search algorithm: |
| 6298 |
* |
| 6299 |
* First of all we process the environment variable REGINA_MACROS. If no file |
| 6300 |
* is found we proceed with the current directory and then with the environment |
| 6301 |
* variable PATH. The semantics of the use of REGINA_MACROS and PATH are the |
| 6302 |
* same, and the search in the current directory is omitted for the superuser |
| 6303 |
* in unix systems for security reasons. The current directory must be |
| 6304 |
* specified explicitely by the superuser. |
| 6305 |
* When processing an environment variable the content is split into the |
| 6306 |
* different paths and each path is processed separately. |
| 6307 |
* Note that the search algorithm to this point is ignored if the script name |
| 6308 |
* contains a file path specification. eg. If "CALL .\MYPROG" is called, then |
| 6309 |
* no searching of REGINA_MACROS or PATH is done; only the concatenation of |
| 6310 |
* suffixes is carried out. |
| 6311 |
* |
| 6312 |
* For each file name and path element a concatenated file name is created. If |
| 6313 |
* a known file extension is part of the file name only this file is searched, |
| 6314 |
* otherwise the file name is extended by the extensions "<empty>", ".rexx", |
| 6315 |
* ".rex", ".cmd", ".rx" in this order. The file name case is ignored on |
| 6316 |
* systems that ignore the character case for normal file operations like DOS, |
| 6317 |
* Windows, OS/2. |
| 6318 |
* |
| 6319 |
* The first matching file terminates the whole algorithm and the found file |
| 6320 |
* is returned. |
| 6321 |
* |
| 6322 |
* The environment variable REGINA_SUFFIXES extends the list of known suffixes |
| 6323 |
* as specified above, and is inserted after the "<empty"> extension in the |
| 6324 |
* process. REGINA_SUFFIXES has to contain a space or comma separated list of |
| 6325 |
* extensions, a dot in front of each entry is allowed, e.g. |
| 6326 |
* ".macro,.mac,regina" or "macro mac regina" |
| 6327 |
* |
| 6328 |
* Note that it is planned to extend the list of known suffixes by ".rxc" in |
| 6329 |
* version 3.4 to allow for seemless integration of precompiled macros. |
| 6330 |
*/ |
| 6331 |
streng *get_external_routine( const tsd_t *TSD, const char *inname, FILE **fp ) |
| 6332 |
{ |
| 6333 |
streng *retval=NULL; |
| 6334 |
char *paths; |
| 6335 |
char *suffixes; |
| 6336 |
|
| 6337 |
*fp = NULL; |
| 6338 |
|
| 6339 |
suffixes = mygetenv( TSD, "REGINA_SUFFIXES", NULL, 0 ); |
| 6340 |
|
| 6341 |
if ( strpbrk( inname, FILE_SEPARATORS ) != NULL ) |
| 6342 |
{ |
| 6343 |
retval = get_external_routine_path( TSD, inname, fp, NULL, suffixes, 1 ); |
| 6344 |
if ( retval ) |
| 6345 |
{ |
| 6346 |
if ( suffixes ) |
| 6347 |
FreeTSD( suffixes ); |
| 6348 |
return retval; |
| 6349 |
} |
| 6350 |
return NULL; |
| 6351 |
} |
| 6352 |
|
| 6353 |
if ( ( paths = mygetenv( TSD, "REGINA_MACROS", NULL, 0 ) ) != NULL ) |
| 6354 |
{ |
| 6355 |
retval = get_external_routine_paths( TSD, inname, fp, paths, suffixes, 1 ); |
| 6356 |
FreeTSD( paths ); |
| 6357 |
if ( retval ) |
| 6358 |
{ |
| 6359 |
if ( suffixes ) |
| 6360 |
FreeTSD( suffixes ); |
| 6361 |
return retval; |
| 6362 |
} |
| 6363 |
} |
| 6364 |
|
| 6365 |
paths = "."; |
| 6366 |
#ifdef UNIX |
| 6367 |
if ( geteuid() == 0 ) |
| 6368 |
paths = NULL; |
| 6369 |
#endif |
| 6370 |
if ( paths ) |
| 6371 |
{ |
| 6372 |
retval = get_external_routine_path( TSD, inname, fp, paths, suffixes, 1 ); |
| 6373 |
if ( retval ) |
| 6374 |
{ |
| 6375 |
if ( suffixes ) |
| 6376 |
FreeTSD( suffixes ); |
| 6377 |
return retval; |
| 6378 |
} |
| 6379 |
} |
| 6380 |
|
| 6381 |
if ( ( paths = mygetenv( TSD, "PATH", NULL, 0 ) ) != NULL ) |
| 6382 |
{ |
| 6383 |
retval = get_external_routine_paths( TSD, inname, fp, paths, suffixes, 0 ); |
| 6384 |
FreeTSD( paths ); |
| 6385 |
} |
| 6386 |
|
| 6387 |
if ( suffixes ) |
| 6388 |
FreeTSD( suffixes ); |
| 6389 |
return retval; |
| 6390 |
} |
| 6391 |
|
| 6392 |
/* |
| 6393 |
* find_shared_library is used for HP/UX purpose only. |
| 6394 |
* It looks for the file inname in the content of the environment variable |
| 6395 |
* inenv and puts the result into retname. retname has to have a size of |
| 6396 |
* at least REXX_PATH_MAX+1. |
| 6397 |
* retname becomes inname if no other file is found. |
| 6398 |
*/ |
| 6399 |
void find_shared_library(const tsd_t *TSD, const char *inname, const char *inenv, char *retname) |
| 6400 |
{ |
| 6401 |
char *paths; |
| 6402 |
char outname[REXX_PATH_MAX+1]; |
| 6403 |
char *env_path; |
| 6404 |
|
| 6405 |
strcpy( retname, inname ); |
| 6406 |
env_path = mygetenv( TSD, inenv, NULL, 0 ); /* fixes bug 595293 */ |
| 6407 |
if ( !env_path ) |
| 6408 |
return; |
| 6409 |
paths = env_path; |
| 6410 |
while ( paths && *paths ) |
| 6411 |
{ |
| 6412 |
int pathlen; |
| 6413 |
char *sep; |
| 6414 |
|
| 6415 |
sep = strchr( paths, PATH_SEPARATOR ); |
| 6416 |
pathlen = sep ? sep-paths : strlen( paths ); |
| 6417 |
strncpy( outname, paths, pathlen ); |
| 6418 |
outname[pathlen] = 0; |
| 6419 |
|
| 6420 |
if ( ( pathlen > 0 ) && ( outname[pathlen-1] != FILE_SEPARATOR ) ) |
| 6421 |
strcat( outname, FILE_SEPARATOR_STR ); |
| 6422 |
strcat( outname, inname ); |
| 6423 |
paths = sep ? sep+1 : 0; /* set up for next pass */ |
| 6424 |
if ( access( outname,F_OK ) == 0) |
| 6425 |
{ |
| 6426 |
strcpy( retname,outname ); |
| 6427 |
break; |
| 6428 |
} |
| 6429 |
} |
| 6430 |
FreeTSD( env_path ); |
| 6431 |
return; |
| 6432 |
} |
| 6433 |
|
| 6434 |
/* CloseOpenFiles closes all scripting input files and it closes all opened |
| 6435 |
* STREAM files without destroying the associated informations. |
| 6436 |
* Bug 982062: Added FilePtrDisposition to allow for purging fileptr |
| 6437 |
* table if requested. |
| 6438 |
*/ |
| 6439 |
void CloseOpenFiles( const tsd_t *TSD, FilePtrDisposition fpd ) |
| 6440 |
{ |
| 6441 |
sysinfobox *ptr; |
| 6442 |
|
| 6443 |
if ( fpd == fpdRETAIN ) |
| 6444 |
{ |
| 6445 |
ptr = TSD->systeminfo; |
| 6446 |
while (ptr) |
| 6447 |
{ |
| 6448 |
if (ptr->input_fp) |
| 6449 |
{ |
| 6450 |
fclose(ptr->input_fp); |
| 6451 |
ptr->input_fp = NULL; |
| 6452 |
} |
| 6453 |
ptr = ptr->previous; |
| 6454 |
} |
| 6455 |
/* |
| 6456 |
* Cheat about the const-state. |
| 6457 |
*/ |
| 6458 |
swapout_all( ( tsd_t *) TSD ); |
| 6459 |
} |
| 6460 |
else |
| 6461 |
{ |
| 6462 |
purge_filetable( ( tsd_t *) TSD ); |
| 6463 |
} |
| 6464 |
return; |
| 6465 |
} |
| 6466 |
|
| 6467 |
streng *ConfigStreamQualified( tsd_t *TSD, const streng *name ) |
| 6468 |
{ |
| 6469 |
char *fn=NULL; |
| 6470 |
streng *result=NULL ; |
| 6471 |
|
| 6472 |
/* |
| 6473 |
* Nul terminate the input filename string, as stat() will barf if |
| 6474 |
* it isn't and other functions stuff up! |
| 6475 |
*/ |
| 6476 |
fn = str_ofTSD(name); |
| 6477 |
|
| 6478 |
result = Str_makeTSD( REXX_PATH_MAX ); |
| 6479 |
if ( my_fullpath( TSD, result->value, fn ) == -1 ) |
| 6480 |
{ |
| 6481 |
/* |
| 6482 |
* my_fullpath failed, so split the supplied file into filename |
| 6483 |
* and directory. Then look for directory and append the filename |
| 6484 |
* TODO - |
| 6485 |
*/ |
| 6486 |
} |
| 6487 |
result->len = strlen( result->value ); |
| 6488 |
return( result ); |
| 6489 |
} |
| 6490 |
|
| 6491 |
#if defined(HAVE__FULLPATH) || defined(__EMX__) |
| 6492 |
/* |
| 6493 |
* my_fullpath tries to get the fully qualified name of a file or directory |
| 6494 |
* even if it doesn't exist. It tries to return a reasonable value even if |
| 6495 |
* a path element is missing. |
| 6496 |
* The return value is 0 on success, -1 in case of a severe error. |
| 6497 |
*/ |
| 6498 |
int my_fullpath( tsd_t *TSD, char *dst, const char *src ) |
| 6499 |
{ |
| 6500 |
char *copy=NULL; |
| 6501 |
int rc = 0, len; |
| 6502 |
# if defined(__EMX__) |
| 6503 |
int i; |
| 6504 |
|
| 6505 |
if ( _fullpath( dst, src, REXX_PATH_MAX ) == -1) |
| 6506 |
strcpy( dst, src ); |
| 6507 |
/* |
| 6508 |
* Convert / back to \. |
| 6509 |
*/ |
| 6510 |
len = strlen( dst ); |
| 6511 |
for ( i = 0; i < len; i++ ) |
| 6512 |
{ |
| 6513 |
if ( dst[i] == '/' ) |
| 6514 |
dst[i] = '\\'; |
| 6515 |
} |
| 6516 |
# else |
| 6517 |
/* attempt to get the full path with the supplied source */ |
| 6518 |
if ( _fullpath( dst, src, REXX_PATH_MAX ) == NULL ) |
| 6519 |
{ |
| 6520 |
# if defined(WIN32) |
| 6521 |
/* now try by removing trailing slash */ |
| 6522 |
/* this code may not actually be needed */ |
| 6523 |
len = strlen( src ) - 1; |
| 6524 |
if ( src[len] == '\\' || src[len] == '/') |
| 6525 |
{ |
| 6526 |
copy = (char *)MallocTSD( len+2 ) ; |
| 6527 |
strcpy( copy, src ); |
| 6528 |
copy[len] = '\0'; |
| 6529 |
if ( _fullpath( dst, copy, REXX_PATH_MAX ) == NULL ) |
| 6530 |
{ |
| 6531 |
strcpy( dst, src ); |
| 6532 |
rc = -1; |
| 6533 |
} |
| 6534 |
} |
| 6535 |
else |
| 6536 |
{ |
| 6537 |
strcpy( dst, src ); |
| 6538 |
rc = -1; |
| 6539 |
} |
| 6540 |
if ( copy ) |
| 6541 |
FreeTSD( copy ); |
| 6542 |
# else |
| 6543 |
strcpy( dst, src ); |
| 6544 |
rc = -1; |
| 6545 |
# endif |
| 6546 |
} |
| 6547 |
# endif |
| 6548 |
return rc; |
| 6549 |
} |
| 6550 |
#elif defined(HAVE__TRUENAME) |
| 6551 |
|
| 6552 |
int my_fullpath( tsd_t *TSD, char *dst, const char *src ) |
| 6553 |
{ |
| 6554 |
_truename( src, dst ); |
| 6555 |
|
| 6556 |
return 0; |
| 6557 |
} |
| 6558 |
#elif defined(HAVE_REALPATH) |
| 6559 |
|
| 6560 |
int my_fullpath( tsd_t *TSD, char *dst, const char *src ) |
| 6561 |
{ |
| 6562 |
const char *source=NULL; |
| 6563 |
/* hack for leading ~/ */ |
| 6564 |
source = translateTilde( TSD, src ); |
| 6565 |
realpath( source, dst ); |
| 6566 |
if ( source != src ) |
| 6567 |
{ |
| 6568 |
FreeTSD( source ); |
| 6569 |
} |
| 6570 |
|
| 6571 |
return 0; |
| 6572 |
} |
| 6573 |
#elif defined(VMS) |
| 6574 |
# include <ssdef.h> |
| 6575 |
# include <rmsdef.h> |
| 6576 |
# include <descrip.h> |
| 6577 |
|
| 6578 |
int my_fullpath( tsd_t *TSD, char *dst, const char *src ) |
| 6579 |
{ |
| 6580 |
char *s; |
| 6581 |
int status, context = 0; |
| 6582 |
struct dsc$descriptor_d result_dx = {0, DSC$K_DTYPE_T, DSC$K_CLASS_D, 0}; |
| 6583 |
struct dsc$descriptor_d finddesc_dx = {0, DSC$K_DTYPE_T, DSC$K_CLASS_D, 0}; |
| 6584 |
|
| 6585 |
finddesc_dx.dsc$a_pointer = (char *)src; /* You may need to cast this */ |
| 6586 |
finddesc_dx.dsc$w_length = strlen(src); |
| 6587 |
status = lib$find_file( &finddesc_dx, &result_dx, &context, 0, 0, 0, 0 ); |
| 6588 |
if ( status == RMS$_NORMAL ) |
| 6589 |
{ |
| 6590 |
memcpy(dst,result_dx.dsc$a_pointer,result_dx.dsc$w_length); |
| 6591 |
*(dst+result_dx.dsc$w_length) = '\0'; |
| 6592 |
} |
| 6593 |
else |
| 6594 |
strcpy(dst,src); |
| 6595 |
lib$find_file_end(&context); |
| 6596 |
str$free1_dx(&result_dx); |
| 6597 |
return(0); |
| 6598 |
} |
| 6599 |
#else /* neither _FULLPATH, _TRUENAME, REALNAME, VMS */ |
| 6600 |
|
| 6601 |
int my_fullpath( tsd_t *TSD, char *dst, const char *src ) |
| 6602 |
{ |
| 6603 |
char tmp[REXX_PATH_MAX+1]; |
| 6604 |
char curr_path[REXX_PATH_MAX+1]; |
| 6605 |
char path[REXX_PATH_MAX+1]; |
| 6606 |
char fname[REXX_PATH_MAX+1]; |
| 6607 |
int i = 0, len = -1, retval; |
| 6608 |
struct rx_stat_buf stat_buf; |
| 6609 |
|
| 6610 |
getcwd(curr_path,REXX_PATH_MAX); |
| 6611 |
strcpy(tmp,src); |
| 6612 |
/* |
| 6613 |
* First determine if the supplied filename is a directory. |
| 6614 |
*/ |
| 6615 |
# if defined(DJGPP) || defined(__WINS__) || defined(__EPOC32__) |
| 6616 |
for ( i = 0; i < strlen( tmp ); i++ ) |
| 6617 |
if ( tmp[ i ] == '\\' ) |
| 6618 |
tmp[ i ] = '/'; |
| 6619 |
# endif |
| 6620 |
if ((rx_stat(tmp,&stat_buf) == 0) |
| 6621 |
&& (stat_buf.st_mode & S_IFMT) == S_IFDIR) |
| 6622 |
{ |
| 6623 |
strcpy(path,tmp); |
| 6624 |
strcpy(fname,""); |
| 6625 |
} |
| 6626 |
else /* here if the file doesn't exist or is not a directory */ |
| 6627 |
{ |
| 6628 |
for (i=strlen(tmp),len=-1;i>-1;i--) |
| 6629 |
{ |
| 6630 |
if (tmp[i] == '/') |
| 6631 |
{ |
| 6632 |
len = i; |
| 6633 |
break; |
| 6634 |
} |
| 6635 |
} |
| 6636 |
switch(len) |
| 6637 |
{ |
| 6638 |
case (-1): |
| 6639 |
getcwd(path,REXX_PATH_MAX); |
| 6640 |
strcpy(fname,tmp); |
| 6641 |
break; |
| 6642 |
case 0: |
| 6643 |
strcpy(path,tmp); |
| 6644 |
path[1] = '\0'; |
| 6645 |
strcpy(fname,tmp+1+len); |
| 6646 |
break; |
| 6647 |
default: |
| 6648 |
strcpy(path,tmp); |
| 6649 |
path[len] = '\0'; |
| 6650 |
strcpy(fname,tmp+1+len); |
| 6651 |
break; |
| 6652 |
} |
| 6653 |
} |
| 6654 |
/* |
| 6655 |
* Change directory to the supplied path, if possible and store the |
| 6656 |
* expanded path. |
| 6657 |
* If an error, restore the current path. |
| 6658 |
*/ |
| 6659 |
if (chdir(path) != 0) |
| 6660 |
{ |
| 6661 |
retval = -1; |
| 6662 |
} |
| 6663 |
else |
| 6664 |
{ |
| 6665 |
getcwd(path,REXX_PATH_MAX); |
| 6666 |
retval = 0; |
| 6667 |
} |
| 6668 |
chdir(curr_path); |
| 6669 |
/* |
| 6670 |
* Append the OS directory character to the path if it doesn't already |
| 6671 |
* end in the character. |
| 6672 |
*/ |
| 6673 |
len = strlen(path); |
| 6674 |
if (len > 0) |
| 6675 |
{ |
| 6676 |
# if defined(__WINS__) || defined(__EPOC32__) |
| 6677 |
if ( path[ len - 1 ] != '\\' |
| 6678 |
# else |
| 6679 |
if ( path[ len - 1 ] != '/' |
| 6680 |
# endif |
| 6681 |
&& strlen( fname ) != 0 ) |
| 6682 |
{ |
| 6683 |
strcat(path,"/"); |
| 6684 |
len++; |
| 6685 |
} |
| 6686 |
# if defined(DJGPP) || defined(__WINS__) || defined(__EPOC32__) |
| 6687 |
for ( i = 0; i < len; i++ ) |
| 6688 |
if ( path[ i ] == '/' ) |
| 6689 |
path[ i ] = '\\'; |
| 6690 |
# endif |
| 6691 |
} |
| 6692 |
strcpy(dst,path); |
| 6693 |
strcat(dst,fname); |
| 6694 |
|
| 6695 |
return retval; |
| 6696 |
} |
| 6697 |
#endif |
| 6698 |
|
| 6699 |
int my_fullpathstreng( const tsd_t *TSD, char *dst, const streng *src ) |
| 6700 |
{ |
| 6701 |
char *copy; |
| 6702 |
int retval; |
| 6703 |
|
| 6704 |
copy = str_ofTSD( src ); |
| 6705 |
retval = my_fullpath( TSD, dst, copy ); |
| 6706 |
FreeTSD( copy ); |
| 6707 |
|
| 6708 |
return retval; |
| 6709 |
} |
| 6710 |
|
| 6711 |
#if !defined(HAVE__SPLITPATH2) && !defined(HAVE__SPLITPATH) && !defined(__EMX__) && !defined(DJGPP) |
| 6712 |
int my_splitpath2( const char *in, char *out, char **drive, char **dir, char **name, char **ext ) |
| 6713 |
{ |
| 6714 |
int inlen = strlen(in); |
| 6715 |
int last_slash_pos=-1,last_dot_pos=-1,last_pos=0,i=0; |
| 6716 |
|
| 6717 |
for (i=0;i<inlen;i++) |
| 6718 |
{ |
| 6719 |
if ( *(in+i) == '/' || *(in+i) == '\\' ) |
| 6720 |
last_slash_pos = i; |
| 6721 |
else if ( *(in+i) == '.' ) |
| 6722 |
last_dot_pos = i; |
| 6723 |
} |
| 6724 |
/* |
| 6725 |
* drive is always empty ! |
| 6726 |
*/ |
| 6727 |
out[0] = '\0'; |
| 6728 |
*drive = out; |
| 6729 |
|
| 6730 |
*ext = out+1; |
| 6731 |
if (last_dot_pos > last_slash_pos) |
| 6732 |
{ |
| 6733 |
strcpy(*ext,in+last_dot_pos); |
| 6734 |
last_pos = 2 + (inlen - last_dot_pos); |
| 6735 |
inlen = last_dot_pos; |
| 6736 |
} |
| 6737 |
else |
| 6738 |
{ |
| 6739 |
**ext = '\0'; |
| 6740 |
last_pos = 2; |
| 6741 |
} |
| 6742 |
*dir = out+last_pos; |
| 6743 |
/* |
| 6744 |
* If there is a path component (last_slash_pos not -1), then copy |
| 6745 |
* from the start of the in string to the last_slash_pos to out[1] |
| 6746 |
*/ |
| 6747 |
if (last_slash_pos != -1) |
| 6748 |
{ |
| 6749 |
memcpy(*dir, in, last_slash_pos + 1); |
| 6750 |
last_pos += last_slash_pos + 1; |
| 6751 |
out[last_pos++] = '\0'; |
| 6752 |
*name = out+last_pos; |
| 6753 |
memcpy(*name, in+last_slash_pos+1,(inlen - last_slash_pos - 1) ); |
| 6754 |
out[last_pos + (inlen - last_slash_pos - 1)] = '\0'; |
| 6755 |
} |
| 6756 |
else |
| 6757 |
{ |
| 6758 |
**dir = '\0'; |
| 6759 |
last_pos++; |
| 6760 |
*name = out+last_pos; |
| 6761 |
memcpy(*name, in, inlen); |
| 6762 |
*(*name+inlen) = '\0'; |
| 6763 |
} |
| 6764 |
return(0); |
| 6765 |
} |
| 6766 |
#endif |
| 6767 |
|
| 6768 |
/* |
| 6769 |
* Enable ~ to be translated to $HOME under linux |
| 6770 |
* The caller is responsible for freeing the return value ONLY if |
| 6771 |
* the return value is NOT the same as the input parameter |
| 6772 |
*/ |
| 6773 |
#ifdef UNIX |
| 6774 |
static const char *translateTilde( tsd_t *TSD, const char *src ) |
| 6775 |
{ |
| 6776 |
int len = strlen( src ); |
| 6777 |
char *copy = (char *)src; |
| 6778 |
if ( ( len > 1 && strncmp( src, "~/", 2 ) == 0 ) || ( len == 1 && strncmp( src, "~", 1 ) == 0) ) |
| 6779 |
{ |
| 6780 |
char *env = getenv( "HOME" ); |
| 6781 |
if ( env != NULL ) |
| 6782 |
{ |
| 6783 |
int len2 = strlen( env ); |
| 6784 |
copy = (char *)MallocTSD( len+len2+2 ) ; |
| 6785 |
strcpy( copy, env ); |
| 6786 |
strcat( copy, src+1 ); |
| 6787 |
} |
| 6788 |
else |
| 6789 |
{ |
| 6790 |
copy = (char *)src; |
| 6791 |
} |
| 6792 |
} |
| 6793 |
else |
| 6794 |
{ |
| 6795 |
copy = (char *)src; |
| 6796 |
} |
| 6797 |
return copy; |
| 6798 |
} |
| 6799 |
int my_rx_stat( tsd_t *TSD, const char *src, struct stat *statbuf ) |
| 6800 |
{ |
| 6801 |
int rc; |
| 6802 |
char *source = (char *)translateTilde( TSD, src ); |
| 6803 |
|
| 6804 |
rc = rx_stat( source, statbuf ) ; |
| 6805 |
if ( source != src ) |
| 6806 |
{ |
| 6807 |
FreeTSD( source ); |
| 6808 |
} |
| 6809 |
return rc; |
| 6810 |
} |
| 6811 |
#else |
| 6812 |
static const char *translateTilde( tsd_t *TSD, const char *src ) |
| 6813 |
{ |
| 6814 |
return src; |
| 6815 |
} |
| 6816 |
|
| 6817 |
int my_rx_stat( tsd_t *TSD, const char *src, struct stat *statbuf ) |
| 6818 |
{ |
| 6819 |
return rx_stat( src, statbuf ) ; |
| 6820 |
} |
| 6821 |
#endif |