2014-11-02 14:16:26 +01:00
|
|
|
/****************************************************************************
|
2014-11-11 19:34:00 +01:00
|
|
|
* apps/interpreters/bas/bas_fs.c
|
2014-11-02 14:16:26 +01:00
|
|
|
* BASIC file system interface.
|
|
|
|
*
|
|
|
|
* Copyright (c) 1999-2014 Michael Haardt
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* Adapted to NuttX and re-released under a 3-clause BSD license:
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
|
|
|
* Authors: Alan Carvalho de Assis <Alan Carvalho de Assis>
|
|
|
|
* Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-27 22:37:19 +01:00
|
|
|
#include <nuttx/config.h>
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2014-11-02 20:54:12 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2014-11-09 15:19:51 +01:00
|
|
|
#include <nuttx/ascii.h>
|
2014-11-10 22:14:25 +01:00
|
|
|
#include <nuttx/vt100.h>
|
2014-11-09 15:19:51 +01:00
|
|
|
|
2014-11-11 19:34:00 +01:00
|
|
|
#include "bas_vt100.h"
|
|
|
|
#include "bas_fs.h"
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
#define LINEWIDTH 80
|
|
|
|
#define COLWIDTH 14
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-03 15:33:39 +01:00
|
|
|
#define _(String) String
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
static struct FileStream **g_file;
|
|
|
|
static int g_capacity;
|
|
|
|
static int g_used;
|
2014-11-08 16:08:20 +01:00
|
|
|
static const int g_open_mode[4] = { 0, O_RDONLY, O_WRONLY, O_RDWR };
|
|
|
|
static char g_errmsgbuf[80];
|
|
|
|
|
2015-05-24 01:08:35 +02:00
|
|
|
#ifdef CONFIG_INTERPRETER_BAS_VT100
|
2014-11-10 22:14:25 +01:00
|
|
|
static const uint8_t g_vt100_colormap[8] =
|
|
|
|
{
|
|
|
|
VT100_BLACK, VT100_BLUE, VT100_GREEN, VT100_CYAN,
|
|
|
|
VT100_RED, VT100_MAGENTA, VT100_YELLOW, VT100_WHITE
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
const char *FS_errmsg;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int size(int dev)
|
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
if (dev >= g_capacity)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
int i;
|
|
|
|
struct FileStream **n;
|
|
|
|
|
2014-11-02 14:16:26 +01:00
|
|
|
n = (struct FileStream **)
|
2014-11-08 15:40:12 +01:00
|
|
|
realloc(g_file, (dev + 1) * sizeof(struct FileStream *));
|
2014-11-02 02:27:54 +01:00
|
|
|
if (n == (struct FileStream **)0)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
g_file = n;
|
|
|
|
for (i = g_capacity; i <= dev; ++i)
|
2014-11-02 15:49:20 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
g_file[i] = (struct FileStream *)0;
|
2014-11-02 15:49:20 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
g_capacity = dev + 1;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
static int opened(int dev, int mode)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
int fd = -1;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (dev < 0 || dev >= g_capacity || g_file[dev] == (struct FileStream *)0)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
snprintf(g_errmsgbuf, sizeof(g_errmsgbuf), _("channel #%d not open"),
|
2014-11-02 02:27:54 +01:00
|
|
|
dev);
|
2014-11-08 16:08:20 +01:00
|
|
|
FS_errmsg = g_errmsgbuf;
|
2014-11-02 02:27:54 +01:00
|
|
|
return -1;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if (mode == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
switch (mode)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
case 0:
|
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
fd = g_file[dev]->outfd;
|
2014-11-02 02:27:54 +01:00
|
|
|
if (fd == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
snprintf(g_errmsgbuf, sizeof(g_errmsgbuf),
|
2014-11-02 14:16:26 +01:00
|
|
|
_("channel #%d not opened for writing"), dev);
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 14:16:26 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
case 1:
|
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
fd = g_file[dev]->infd;
|
2014-11-02 02:27:54 +01:00
|
|
|
if (fd == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
snprintf(g_errmsgbuf, sizeof(g_errmsgbuf),
|
2014-11-02 14:16:26 +01:00
|
|
|
_("channel #%d not opened for reading"), dev);
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 14:16:26 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case 2:
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
fd = g_file[dev]->randomfd;
|
2014-11-02 02:27:54 +01:00
|
|
|
if (fd == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
snprintf(g_errmsgbuf, sizeof(g_errmsgbuf),
|
2014-11-02 14:16:26 +01:00
|
|
|
_("channel #%d not opened for random access"), dev);
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 14:16:26 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case 3:
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
fd = g_file[dev]->binaryfd;
|
2014-11-02 02:27:54 +01:00
|
|
|
if (fd == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
snprintf(g_errmsgbuf, sizeof(g_errmsgbuf),
|
2014-11-02 14:16:26 +01:00
|
|
|
_("channel #%d not opened for binary access"), dev);
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 14:16:26 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case 4:
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
fd = (g_file[dev]->randomfd != -1 ? g_file[dev]->randomfd : g_file[dev]->binaryfd);
|
2014-11-02 02:27:54 +01:00
|
|
|
if (fd == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
snprintf(g_errmsgbuf, sizeof(g_errmsgbuf),
|
2014-11-02 14:16:26 +01:00
|
|
|
_("channel #%d not opened for random or binary access"),
|
|
|
|
dev);
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 14:16:26 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd == -1)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
FS_errmsg = g_errmsgbuf;
|
2014-11-02 02:27:54 +01:00
|
|
|
return -1;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
else
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
static int refill(int dev)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct FileStream *f;
|
|
|
|
ssize_t len;
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
f = g_file[dev];
|
2014-11-02 02:27:54 +01:00
|
|
|
f->inSize = 0;
|
|
|
|
len = read(f->infd, f->inBuf, sizeof(f->inBuf));
|
|
|
|
if (len <= 0)
|
|
|
|
{
|
|
|
|
f->inCapacity = 0;
|
|
|
|
FS_errmsg = (len == -1 ? strerror(errno) : (const char *)0);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
else
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
f->inCapacity = len;
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
static int edit(int chn, int onl)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
struct FileStream *f = g_file[chn];
|
2014-11-02 02:27:54 +01:00
|
|
|
char *buf = f->inBuf;
|
2014-10-27 14:53:12 +01:00
|
|
|
char ch;
|
|
|
|
int r;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
for (buf = f->inBuf; buf < (f->inBuf + f->inCapacity); ++buf)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
if (*buf >= '\0' && *buf < ' ')
|
|
|
|
{
|
|
|
|
FS_putChar(chn, '^');
|
|
|
|
FS_putChar(chn, *buf ? (*buf + 'a' - 1) : '@');
|
|
|
|
}
|
|
|
|
else
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
FS_putChar(chn, *buf);
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
do
|
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_flush(chn);
|
|
|
|
if ((r = read(f->infd, &ch, 1)) == -1)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
f->inCapacity = 0;
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (r == 0 || (f->inCapacity == 0 && ch == 4))
|
|
|
|
{
|
|
|
|
FS_errmsg = (char *)0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-09 15:19:51 +01:00
|
|
|
/* Check for backspace
|
|
|
|
*
|
|
|
|
* There are several notions of backspace, for an elaborate summary see
|
|
|
|
* http://www.ibb.net/~anne/keyboard.html. There is no clean solution.
|
|
|
|
* Here both DEL and backspace are treated like backspace here. The
|
|
|
|
* Unix/Linux screen terminal by default outputs DEL (0x7f) when the
|
|
|
|
* backspace key is pressed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ch == ASCII_BS || ch == ASCII_DEL)
|
2014-11-07 16:06:00 +01:00
|
|
|
{
|
|
|
|
if (f->inCapacity)
|
|
|
|
{
|
2015-05-24 01:08:35 +02:00
|
|
|
#ifdef CONFIG_INTERPRETER_BAS_VT100
|
2014-11-07 16:06:00 +01:00
|
|
|
/* Could use vt100_clrtoeol */
|
|
|
|
#endif
|
|
|
|
/* Is the previous character in the buffer 2 character escape sequence? */
|
|
|
|
|
|
|
|
if (f->inBuf[f->inCapacity - 1] >= '\0' &&
|
|
|
|
f->inBuf[f->inCapacity - 1] < ' ')
|
|
|
|
{
|
|
|
|
/* Yes.. erase two characters */
|
|
|
|
|
|
|
|
FS_putChars(chn, "\b\b \b\b");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Yes.. erase one characters */
|
|
|
|
|
|
|
|
FS_putChars(chn, "\b \b");
|
|
|
|
}
|
|
|
|
|
|
|
|
--f->inCapacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((f->inCapacity + 1) < sizeof(f->inBuf))
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
2014-11-07 00:04:40 +01:00
|
|
|
#ifdef CONFIG_EOL_IS_BOTH_CRLF
|
|
|
|
/* Ignore carriage returns that may accompany a CRLF sequence. */
|
2014-11-06 20:35:15 +01:00
|
|
|
|
|
|
|
if (ch != '\r')
|
2014-11-07 00:04:40 +01:00
|
|
|
#endif
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
2014-11-06 20:35:15 +01:00
|
|
|
/* Is this a new line character */
|
|
|
|
|
2014-11-07 00:04:40 +01:00
|
|
|
#ifdef CONFIG_EOL_IS_CR
|
|
|
|
if (ch != '\r')
|
|
|
|
#elif defined(CONFIG_EOL_IS_LF)
|
2014-11-06 20:35:15 +01:00
|
|
|
if (ch != '\n')
|
2014-11-07 00:04:40 +01:00
|
|
|
#elif defined(CONFIG_EOL_IS_EITHER_CRLF)
|
|
|
|
if (ch != '\n' && ch != '\r' )
|
|
|
|
#endif
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
2014-11-06 20:35:15 +01:00
|
|
|
/* No.. escape control characters other than newline and
|
|
|
|
* carriage return
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ch >= '\0' && ch < ' ')
|
|
|
|
{
|
|
|
|
FS_putChar(chn, '^');
|
|
|
|
FS_putChar(chn, ch ? (ch + 'a' - 1) : '@');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Output normal, printable characters */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FS_putChar(chn, ch);
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
}
|
2014-11-06 20:35:15 +01:00
|
|
|
|
2014-11-07 00:04:40 +01:00
|
|
|
/* It is a newline */
|
2014-11-06 20:35:15 +01:00
|
|
|
|
2014-11-07 00:04:40 +01:00
|
|
|
else
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-07 00:04:40 +01:00
|
|
|
/* Echo the newline (or not). We always use newline
|
|
|
|
* termination when talking to the host.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (onl)
|
|
|
|
{
|
|
|
|
FS_putChar(chn, '\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(CONFIG_EOL_IS_CR) || defined(CONFIG_EOL_IS_EITHER_CRLF)
|
|
|
|
/* If the host is talking to us with CR line terminations,
|
|
|
|
* switch to use LF internally.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ch = '\n';
|
|
|
|
#endif
|
2014-11-02 14:16:26 +01:00
|
|
|
}
|
2014-11-06 20:22:21 +01:00
|
|
|
|
2014-11-06 20:35:15 +01:00
|
|
|
f->inBuf[f->inCapacity++] = ch;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 14:59:56 +01:00
|
|
|
}
|
2014-11-03 15:19:14 +01:00
|
|
|
while (ch != '\n');
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
static int cls(int chn)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2015-05-24 01:08:35 +02:00
|
|
|
#ifdef CONFIG_INTERPRETER_BAS_VT100
|
2014-11-07 16:06:00 +01:00
|
|
|
vt100_clrscreen(chn);
|
2014-11-11 16:07:16 +01:00
|
|
|
vt100_cursorhome(chn);
|
2014-11-07 16:06:00 +01:00
|
|
|
return 0;
|
|
|
|
#else
|
2014-11-03 15:19:14 +01:00
|
|
|
FS_errmsg = _("Clear screen operation not implemented");
|
2014-10-27 14:53:12 +01:00
|
|
|
return -1;
|
2014-11-07 16:06:00 +01:00
|
|
|
#endif
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
static int locate(int chn, int line, int column)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2015-05-24 01:08:35 +02:00
|
|
|
#ifdef CONFIG_INTERPRETER_BAS_VT100
|
2014-11-07 16:06:00 +01:00
|
|
|
vt100_setcursor(chn, line, column);
|
|
|
|
return 0;
|
|
|
|
#else
|
2014-11-03 15:19:14 +01:00
|
|
|
FS_errmsg = _("Set cursor position operation not implement");
|
2014-10-27 14:53:12 +01:00
|
|
|
return -1;
|
2014-11-07 16:06:00 +01:00
|
|
|
#endif
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
static int colour(int chn, int foreground, int background)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2015-05-24 01:08:35 +02:00
|
|
|
#ifdef CONFIG_INTERPRETER_BAS_VT100
|
2014-11-10 22:14:25 +01:00
|
|
|
if (foreground >= 0)
|
|
|
|
{
|
|
|
|
vt100_foreground_color(chn, foreground);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (background >= 0)
|
|
|
|
{
|
|
|
|
vt100_background_color(chn, background);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
2014-11-03 15:19:14 +01:00
|
|
|
FS_errmsg = _("Set color operation no implemented");
|
2014-10-27 14:53:12 +01:00
|
|
|
return -1;
|
2014-11-10 22:14:25 +01:00
|
|
|
#endif
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
static int resetcolour(int chn)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2015-05-24 01:08:35 +02:00
|
|
|
#ifdef CONFIG_INTERPRETER_BAS_VT100
|
2014-11-10 22:14:25 +01:00
|
|
|
vt100_foreground_color(chn, VT100_DEFAULT);
|
|
|
|
vt100_background_color(chn, VT100_DEFAULT);
|
2014-11-03 15:19:14 +01:00
|
|
|
#endif
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int FS_opendev(int chn, int infd, int outfd)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
if (size(chn) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[chn] != (struct FileStream *)0)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = _("channel already open");
|
2014-10-27 14:53:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
g_file[chn] = malloc(sizeof(struct FileStream));
|
|
|
|
g_file[chn]->dev = 1;
|
2014-11-02 14:59:56 +01:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
2014-11-08 15:40:12 +01:00
|
|
|
g_file[chn]->tty = (infd == 0 ? isatty(infd) && isatty(outfd) : 0);
|
2014-11-02 14:59:56 +01:00
|
|
|
#else
|
2014-11-08 15:40:12 +01:00
|
|
|
g_file[chn]->tty = 1;
|
2014-11-02 14:59:56 +01:00
|
|
|
#endif
|
2014-11-08 15:40:12 +01:00
|
|
|
g_file[chn]->recLength = 1;
|
|
|
|
g_file[chn]->infd = infd;
|
|
|
|
g_file[chn]->inSize = 0;
|
|
|
|
g_file[chn]->inCapacity = 0;
|
|
|
|
g_file[chn]->outfd = outfd;
|
|
|
|
g_file[chn]->outPos = 0;
|
|
|
|
g_file[chn]->outLineWidth = LINEWIDTH;
|
|
|
|
g_file[chn]->outColWidth = COLWIDTH;
|
|
|
|
g_file[chn]->outCapacity = sizeof(g_file[chn]->outBuf);
|
|
|
|
g_file[chn]->outSize = 0;
|
|
|
|
g_file[chn]->outforeground = -1;
|
|
|
|
g_file[chn]->outbackground = -1;
|
|
|
|
g_file[chn]->randomfd = -1;
|
|
|
|
g_file[chn]->binaryfd = -1;
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = (const char *)0;
|
2014-11-08 15:40:12 +01:00
|
|
|
++g_used;
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_openin(const char *name)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
int chn, fd;
|
|
|
|
|
|
|
|
if ((fd = open(name, O_RDONLY)) == -1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
for (chn = 0; chn < g_capacity; ++chn)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[chn] == (struct FileStream *)0)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (size(chn) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
g_file[chn] = malloc(sizeof(struct FileStream));
|
|
|
|
g_file[chn]->recLength = 1;
|
|
|
|
g_file[chn]->dev = 0;
|
|
|
|
g_file[chn]->tty = 0;
|
|
|
|
g_file[chn]->infd = fd;
|
|
|
|
g_file[chn]->inSize = 0;
|
|
|
|
g_file[chn]->inCapacity = 0;
|
|
|
|
g_file[chn]->outfd = -1;
|
|
|
|
g_file[chn]->randomfd = -1;
|
|
|
|
g_file[chn]->binaryfd = -1;
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = (const char *)0;
|
2014-11-08 15:40:12 +01:00
|
|
|
++g_used;
|
2014-10-27 14:53:12 +01:00
|
|
|
return chn;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_openinChn(int chn, const char *name, int mode)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
mode_t fl;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (size(chn) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[chn] != (struct FileStream *)0)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = _("channel already open");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
fl = g_open_mode[mode];
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-02 14:16:26 +01:00
|
|
|
/* Serial devices on Linux should be opened non-blocking, otherwise the
|
|
|
|
* open() may block already. Named pipes can not be opened non-blocking in
|
|
|
|
* write-only mode, so first try non-blocking, then blocking. */
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if ((fd = open(name, fl | O_NONBLOCK)) == -1)
|
|
|
|
{
|
|
|
|
if (errno != ENXIO || (fd = open(name, fl)) == -1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fcntl(fd, F_SETFL, (long)fl) == -1)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
close(fd);
|
2014-10-27 14:53:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
g_file[chn] = malloc(sizeof(struct FileStream));
|
|
|
|
g_file[chn]->recLength = 1;
|
|
|
|
g_file[chn]->dev = 0;
|
|
|
|
g_file[chn]->tty = 0;
|
|
|
|
g_file[chn]->infd = fd;
|
|
|
|
g_file[chn]->inSize = 0;
|
|
|
|
g_file[chn]->inCapacity = 0;
|
|
|
|
g_file[chn]->outfd = -1;
|
|
|
|
g_file[chn]->randomfd = -1;
|
|
|
|
g_file[chn]->binaryfd = -1;
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = (const char *)0;
|
2014-11-08 15:40:12 +01:00
|
|
|
++g_used;
|
2014-10-27 14:53:12 +01:00
|
|
|
return chn;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_openout(const char *name)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
int chn, fd;
|
|
|
|
|
|
|
|
if ((fd = open(name, O_WRONLY | O_TRUNC | O_CREAT, 0666)) == -1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
for (chn = 0; chn < g_capacity; ++chn)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[chn] == (struct FileStream *)0)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if (size(chn) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
g_file[chn] = malloc(sizeof(struct FileStream));
|
|
|
|
g_file[chn]->recLength = 1;
|
|
|
|
g_file[chn]->dev = 0;
|
|
|
|
g_file[chn]->tty = 0;
|
|
|
|
g_file[chn]->infd = -1;
|
|
|
|
g_file[chn]->outfd = fd;
|
|
|
|
g_file[chn]->outPos = 0;
|
|
|
|
g_file[chn]->outLineWidth = LINEWIDTH;
|
|
|
|
g_file[chn]->outColWidth = COLWIDTH;
|
|
|
|
g_file[chn]->outSize = 0;
|
|
|
|
g_file[chn]->outCapacity = sizeof(g_file[chn]->outBuf);
|
|
|
|
g_file[chn]->randomfd = -1;
|
|
|
|
g_file[chn]->binaryfd = -1;
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = (const char *)0;
|
2014-11-08 15:40:12 +01:00
|
|
|
++g_used;
|
2014-10-27 14:53:12 +01:00
|
|
|
return chn;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_openoutChn(int chn, const char *name, int mode, int append)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
mode_t fl;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (size(chn) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[chn] != (struct FileStream *)0)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = _("channel already open");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
fl = g_open_mode[mode] | (append ? O_APPEND : 0);
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
/* Serial devices on Linux should be opened non-blocking, otherwise the */
|
2014-10-27 14:53:12 +01:00
|
|
|
/* open() may block already. Named pipes can not be opened non-blocking */
|
2014-11-02 02:27:54 +01:00
|
|
|
/* in write-only mode, so first try non-blocking, then blocking. */
|
|
|
|
|
|
|
|
fd = open(name, fl | O_CREAT | (append ? 0 : O_TRUNC) | O_NONBLOCK, 0666);
|
2014-11-02 14:16:26 +01:00
|
|
|
if (fd == -1)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
if (errno != ENXIO ||
|
|
|
|
(fd = open(name, fl | O_CREAT | (append ? 0 : O_TRUNC), 0666)) == -1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fcntl(fd, F_SETFL, (long)fl) == -1)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
close(fd);
|
2014-10-27 14:53:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
g_file[chn] = malloc(sizeof(struct FileStream));
|
|
|
|
g_file[chn]->recLength = 1;
|
|
|
|
g_file[chn]->dev = 0;
|
|
|
|
g_file[chn]->tty = 0;
|
|
|
|
g_file[chn]->infd = -1;
|
|
|
|
g_file[chn]->outfd = fd;
|
|
|
|
g_file[chn]->outPos = 0;
|
|
|
|
g_file[chn]->outLineWidth = LINEWIDTH;
|
|
|
|
g_file[chn]->outColWidth = COLWIDTH;
|
|
|
|
g_file[chn]->outSize = 0;
|
|
|
|
g_file[chn]->outCapacity = sizeof(g_file[chn]->outBuf);
|
|
|
|
g_file[chn]->randomfd = -1;
|
|
|
|
g_file[chn]->binaryfd = -1;
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = (const char *)0;
|
2014-11-08 15:40:12 +01:00
|
|
|
++g_used;
|
2014-10-27 14:53:12 +01:00
|
|
|
return chn;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_openrandomChn(int chn, const char *name, int mode, int recLength)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
assert(chn >= 0);
|
|
|
|
assert(name != (const char *)0);
|
|
|
|
assert(recLength > 0);
|
|
|
|
if (size(chn) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[chn] != (struct FileStream *)0)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = _("channel already open");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if ((fd = open(name, g_open_mode[mode] | O_CREAT, 0666)) == -1)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
g_file[chn] = malloc(sizeof(struct FileStream));
|
|
|
|
g_file[chn]->recLength = recLength;
|
|
|
|
g_file[chn]->dev = 0;
|
|
|
|
g_file[chn]->tty = 0;
|
|
|
|
g_file[chn]->infd = -1;
|
|
|
|
g_file[chn]->outfd = -1;
|
|
|
|
g_file[chn]->randomfd = fd;
|
|
|
|
g_file[chn]->recBuf = malloc(recLength);
|
|
|
|
memset(g_file[chn]->recBuf, 0, recLength);
|
|
|
|
StringField_new(&g_file[chn]->field);
|
|
|
|
g_file[chn]->binaryfd = -1;
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = (const char *)0;
|
2014-11-08 15:40:12 +01:00
|
|
|
++g_used;
|
2014-10-27 14:53:12 +01:00
|
|
|
return chn;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_openbinaryChn(int chn, const char *name, int mode)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
assert(chn >= 0);
|
|
|
|
assert(name != (const char *)0);
|
|
|
|
if (size(chn) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[chn] != (struct FileStream *)0)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = _("channel already open");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if ((fd = open(name, g_open_mode[mode] | O_CREAT, 0666)) == -1)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-08 15:40:12 +01:00
|
|
|
|
|
|
|
g_file[chn] = malloc(sizeof(struct FileStream));
|
|
|
|
g_file[chn]->recLength = 1;
|
|
|
|
g_file[chn]->dev = 0;
|
|
|
|
g_file[chn]->tty = 0;
|
|
|
|
g_file[chn]->infd = -1;
|
|
|
|
g_file[chn]->outfd = -1;
|
|
|
|
g_file[chn]->randomfd = -1;
|
|
|
|
g_file[chn]->binaryfd = fd;
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = (const char *)0;
|
2014-11-08 15:40:12 +01:00
|
|
|
++g_used;
|
2014-10-27 14:53:12 +01:00
|
|
|
return chn;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_freechn(void)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
for (i = 0; i < g_capacity && g_file[i]; ++i);
|
2014-11-02 02:27:54 +01:00
|
|
|
if (size(i) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return i;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_flush(int dev)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
ssize_t written;
|
|
|
|
size_t offset;
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[dev] == (struct FileStream *)0)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = _("channel not open");
|
2014-10-27 14:53:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
offset = 0;
|
2014-11-08 15:40:12 +01:00
|
|
|
while (offset < g_file[dev]->outSize)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
written =
|
2014-11-08 15:40:12 +01:00
|
|
|
write(g_file[dev]->outfd, g_file[dev]->outBuf + offset,
|
|
|
|
g_file[dev]->outSize - offset);
|
2014-11-02 02:27:54 +01:00
|
|
|
if (written == -1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
offset += written;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
}
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
g_file[dev]->outSize = 0;
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = (const char *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_close(int dev)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[dev] == (struct FileStream *)0)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = _("channel not open");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[dev]->outfd >= 0)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[dev]->tty &&
|
|
|
|
(g_file[dev]->outforeground != -1 || g_file[dev]->outbackground != -1))
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
resetcolour(dev);
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_flush(dev);
|
2014-11-08 15:40:12 +01:00
|
|
|
close(g_file[dev]->outfd);
|
2014-11-02 02:27:54 +01:00
|
|
|
}
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[dev]->randomfd >= 0)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
StringField_destroy(&g_file[dev]->field);
|
|
|
|
free(g_file[dev]->recBuf);
|
|
|
|
close(g_file[dev]->randomfd);
|
2014-11-02 02:27:54 +01:00
|
|
|
}
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[dev]->binaryfd >= 0)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
close(g_file[dev]->binaryfd);
|
2014-11-02 02:27:54 +01:00
|
|
|
}
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[dev]->infd >= 0)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
close(g_file[dev]->infd);
|
2014-11-02 14:16:26 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
free(g_file[dev]);
|
|
|
|
g_file[dev] = (struct FileStream *)0;
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = (const char *)0;
|
2014-11-08 15:40:12 +01:00
|
|
|
if (--g_used == 0)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
free(g_file);
|
|
|
|
g_file = (struct FileStream **)0;
|
|
|
|
g_capacity = 0;
|
2014-11-02 02:27:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-02 14:59:56 +01:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
2014-11-02 02:27:54 +01:00
|
|
|
int FS_istty(int chn)
|
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
return (g_file[chn] && g_file[chn]->tty);
|
2014-11-02 02:27:54 +01:00
|
|
|
}
|
2014-11-02 14:59:56 +01:00
|
|
|
#endif
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_lock(int chn, off_t offset, off_t length, int mode, int w)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct flock recordLock;
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[chn] == (struct FileStream *)0)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = _("channel not open");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if ((fd = g_file[chn]->infd) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
if ((fd = g_file[chn]->outfd) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
if ((fd = g_file[chn]->randomfd) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
if ((fd = g_file[chn]->binaryfd) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
recordLock.l_whence = SEEK_SET;
|
|
|
|
recordLock.l_start = offset;
|
|
|
|
recordLock.l_len = length;
|
2014-10-27 14:53:12 +01:00
|
|
|
switch (mode)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
case FS_LOCK_SHARED:
|
|
|
|
recordLock.l_type = F_RDLCK;
|
|
|
|
break;
|
2014-11-02 14:16:26 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
case FS_LOCK_EXCLUSIVE:
|
|
|
|
recordLock.l_type = F_WRLCK;
|
|
|
|
break;
|
2014-11-02 14:16:26 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
case FS_LOCK_NONE:
|
|
|
|
recordLock.l_type = F_UNLCK;
|
|
|
|
break;
|
2014-11-02 14:16:26 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fcntl(fd, w ? F_SETLKW : F_SETLK, &recordLock) == -1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_truncate(int chn)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-06 20:22:21 +01:00
|
|
|
#ifdef CONFIG_INTERPRETER_BAS_HAVE_FTRUNCATE
|
2014-10-27 14:53:12 +01:00
|
|
|
int fd;
|
|
|
|
off_t o;
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[chn] == (struct FileStream *)0)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = _("channel not open");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if ((fd = g_file[chn]->infd) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
if ((fd = g_file[chn]->outfd) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
if ((fd = g_file[chn]->randomfd) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
if ((fd = g_file[chn]->binaryfd) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if ((o = lseek(fd, 0, SEEK_CUR)) == (off_t) - 1 || ftruncate(fd, o + 1) == -1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
2014-11-06 20:22:21 +01:00
|
|
|
#else
|
|
|
|
FS_errmsg = strerror(ENOSYS);
|
|
|
|
return -1;
|
|
|
|
#endif
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
void FS_shellmode(int dev)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
void FS_fsmode(int chn)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
void FS_xonxoff(int chn, int on)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-03 15:19:14 +01:00
|
|
|
/* Not implemented */
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_put(int chn)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
ssize_t offset, written;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(chn, 2) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
offset = 0;
|
2014-11-08 15:40:12 +01:00
|
|
|
while (offset < g_file[chn]->recLength)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
written =
|
2014-11-08 15:40:12 +01:00
|
|
|
write(g_file[chn]->randomfd, g_file[chn]->recBuf + offset,
|
|
|
|
g_file[chn]->recLength - offset);
|
2014-11-02 02:27:54 +01:00
|
|
|
if (written == -1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
offset += written;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
FS_errmsg = (const char *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_putChar(int dev, char ch)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct FileStream *f;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(dev, 0) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
f = g_file[dev];
|
2014-11-02 02:27:54 +01:00
|
|
|
if (ch == '\n')
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
f->outPos = 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if (ch == '\b' && f->outPos)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
--f->outPos;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if (f->outSize + 2 >= f->outCapacity && FS_flush(dev) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if (f->outLineWidth && f->outPos == f->outLineWidth)
|
|
|
|
{
|
2014-11-03 15:19:14 +01:00
|
|
|
f->outBuf[f->outSize++] = '\n';
|
2014-11-02 02:27:54 +01:00
|
|
|
f->outPos = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-03 15:19:14 +01:00
|
|
|
f->outBuf[f->outSize++] = ch;
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if (ch != '\n' && ch != '\b')
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
++f->outPos;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
FS_errmsg = (const char *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_putChars(int dev, const char *chars)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
while (*chars)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
if (FS_putChar(dev, *chars++) == -1)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_putString(int dev, const struct String *s)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
size_t len = s->length;
|
|
|
|
const char *c = s->character;
|
|
|
|
|
|
|
|
while (len)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
if (FS_putChar(dev, *c++) == -1)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
--len;
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_putItem(int dev, const struct String *s)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct FileStream *f;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(dev, 0) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
f = g_file[dev];
|
2014-11-02 02:27:54 +01:00
|
|
|
if (f->outPos && f->outPos + s->length > f->outLineWidth)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
FS_nextline(dev);
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return FS_putString(dev, s);
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_putbinaryString(int chn, const struct String *s)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(chn, 3) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if (s->length &&
|
2014-11-08 15:40:12 +01:00
|
|
|
write(g_file[chn]->binaryfd, s->character, s->length) != s->length)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_putbinaryInteger(int chn, long int x)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
char s[sizeof(long int)];
|
|
|
|
int i;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(chn, 3) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
for (i = 0; i < sizeof(x); ++i, x >>= 8)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
s[i] = (x & 0xff);
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (write(g_file[chn]->binaryfd, s, sizeof(s)) != sizeof(s))
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_putbinaryReal(int chn, double x)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(chn, 3) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (write(g_file[chn]->binaryfd, &x, sizeof(x)) != sizeof(x))
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_getbinaryString(int chn, struct String *s)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
ssize_t len;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(chn, 3) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if (s->length &&
|
2014-11-08 15:40:12 +01:00
|
|
|
(len = read(g_file[chn]->binaryfd, s->character, s->length)) != s->length)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
if (len == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
else
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
FS_errmsg = _("End of g_file");
|
2014-11-02 14:16:26 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_getbinaryInteger(int chn, long int *x)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
char s[sizeof(long int)];
|
|
|
|
int i;
|
|
|
|
ssize_t len;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(chn, 3) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if ((len = read(g_file[chn]->binaryfd, s, sizeof(s))) != sizeof(s))
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
if (len == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
else
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = _("End of file");
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*x = (s[sizeof(x) - 1] < 0) ? -1 : 0;
|
|
|
|
for (i = sizeof(s) - 1; i >= 0; --i)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
*x = (*x << 8) | (s[i] & 0xff);
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_getbinaryReal(int chn, double *x)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
ssize_t len;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(chn, 3) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if ((len = read(g_file[chn]->binaryfd, x, sizeof(*x))) != sizeof(*x))
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
if (len == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
else
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = _("End of file");
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_nextcol(int dev)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct FileStream *f;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(dev, 0) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
f = g_file[dev];
|
2014-11-02 02:27:54 +01:00
|
|
|
if (f->outPos % f->outColWidth
|
|
|
|
&& f->outLineWidth
|
|
|
|
&& ((f->outPos / f->outColWidth + 2) * f->outColWidth) > f->outLineWidth)
|
|
|
|
{
|
|
|
|
return FS_putChar(dev, '\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(f->outPos % f->outColWidth) && FS_putChar(dev, ' ') == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
while (f->outPos % f->outColWidth)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
if (FS_putChar(dev, ' ') == -1)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_nextline(int dev)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct FileStream *f;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(dev, 0) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
f = g_file[dev];
|
2014-11-02 02:27:54 +01:00
|
|
|
if (f->outPos && FS_putChar(dev, '\n') == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_tab(int dev, int position)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
struct FileStream *f = g_file[dev];
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if (f->outLineWidth && position >= f->outLineWidth)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
position = f->outLineWidth - 1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
while (f->outPos < (position - 1))
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
if (FS_putChar(dev, ' ') == -1)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_width(int dev, int width)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(dev, 0) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if (width < 0)
|
|
|
|
{
|
|
|
|
FS_errmsg = _("negative width");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
g_file[dev]->outLineWidth = width;
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_zone(int dev, int zone)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(dev, 0) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if (zone <= 0)
|
|
|
|
{
|
|
|
|
FS_errmsg = _("non-positive zone width");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
g_file[dev]->outColWidth = zone;
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_cls(int chn)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct FileStream *f;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(chn, 0) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
f = g_file[chn];
|
2014-10-27 14:53:12 +01:00
|
|
|
if (!f->tty)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = _("not a terminal");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cls(chn) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if (FS_flush(chn) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
f->outPos = 0;
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_locate(int chn, int line, int column)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct FileStream *f;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(chn, 0) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
f = g_file[chn];
|
2014-10-27 14:53:12 +01:00
|
|
|
if (!f->tty)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = _("not a terminal");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (locate(chn, line, column) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if (FS_flush(chn) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
f->outPos = column - 1;
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_colour(int chn, int foreground, int background)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct FileStream *f;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(chn, 0) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
f = g_file[chn];
|
2014-10-27 14:53:12 +01:00
|
|
|
if (!f->tty)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = _("not a terminal");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (colour(chn, foreground, background) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
f->outforeground = foreground;
|
|
|
|
f->outbackground = background;
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_getChar(int dev)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct FileStream *f;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(dev, 1) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
f = g_file[dev];
|
2014-11-02 02:27:54 +01:00
|
|
|
if (f->inSize == f->inCapacity && refill(dev) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = (const char *)0;
|
|
|
|
if (f->inSize + 1 == f->inCapacity)
|
|
|
|
{
|
|
|
|
char ch = f->inBuf[f->inSize];
|
|
|
|
|
|
|
|
f->inSize = f->inCapacity = 0;
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
else
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return f->inBuf[f->inSize++];
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_get(int chn)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
ssize_t offset, rd;
|
|
|
|
|
|
|
|
if (opened(chn, 2) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
offset = 0;
|
2014-11-08 15:40:12 +01:00
|
|
|
while (offset < g_file[chn]->recLength)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
rd =
|
2014-11-08 15:40:12 +01:00
|
|
|
read(g_file[chn]->randomfd, g_file[chn]->recBuf + offset,
|
|
|
|
g_file[chn]->recLength - offset);
|
2014-11-02 02:27:54 +01:00
|
|
|
if (rd == -1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
offset += rd;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
FS_errmsg = (const char *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_inkeyChar(int dev, int ms)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct FileStream *f;
|
|
|
|
char c;
|
|
|
|
ssize_t len;
|
2014-11-03 16:11:25 +01:00
|
|
|
#ifdef CONFIG_INTERPRETER_BAS_USE_SELECT
|
2014-10-27 14:53:12 +01:00
|
|
|
fd_set just_infd;
|
|
|
|
struct timeval timeout;
|
|
|
|
#endif
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(dev, 1) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
f = g_file[dev];
|
2014-11-02 02:27:54 +01:00
|
|
|
if (f->inSize < f->inCapacity)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return f->inBuf[f->inSize++];
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-03 16:11:25 +01:00
|
|
|
#ifdef CONFIG_INTERPRETER_BAS_USE_SELECT
|
2014-10-27 14:53:12 +01:00
|
|
|
FD_ZERO(&just_infd);
|
2014-11-02 02:27:54 +01:00
|
|
|
FD_SET(f->infd, &just_infd);
|
|
|
|
timeout.tv_sec = ms / 1000;
|
|
|
|
timeout.tv_usec = (ms % 1000) * 1000;
|
|
|
|
switch (select(f->infd + 1, &just_infd, (fd_set *) 0, (fd_set *) 0, &timeout))
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
FS_errmsg = (const char *)0;
|
|
|
|
len = read(f->infd, &c, 1);
|
|
|
|
return (len == 1 ? c : -1);
|
|
|
|
}
|
2014-11-02 14:16:26 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case 0:
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = (const char *)0;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 14:16:26 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
case -1:
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 14:16:26 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#else
|
|
|
|
FS_errmsg = (const char *)0;
|
|
|
|
len = read(f->infd, &c, 1);
|
2014-11-02 14:59:56 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (len == -1)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = strerror(errno);
|
2014-10-27 14:53:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
return (len == 1 ? c : -1);
|
2014-10-27 14:53:12 +01:00
|
|
|
#endif
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
void FS_sleep(double s)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct timespec p;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
p.tv_sec = floor(s);
|
|
|
|
p.tv_nsec = 1000000000 * (s - floor(s));
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
nanosleep(&p, (struct timespec *)0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_eof(int chn)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct FileStream *f;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(chn, 1) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
f = g_file[chn];
|
2014-11-02 02:27:54 +01:00
|
|
|
if (f->inSize == f->inCapacity && refill(chn) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
long int FS_loc(int chn)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
int fd;
|
2014-11-02 02:27:54 +01:00
|
|
|
off_t cur, offset = 0;
|
|
|
|
|
|
|
|
if (opened(chn, -1) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[chn]->infd != -1)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
fd = g_file[chn]->infd;
|
|
|
|
offset = -g_file[chn]->inCapacity + g_file[chn]->inSize;
|
2014-11-02 02:27:54 +01:00
|
|
|
}
|
2014-11-08 15:40:12 +01:00
|
|
|
else if (g_file[chn]->outfd != -1)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
fd = g_file[chn]->outfd;
|
|
|
|
offset = g_file[chn]->outSize;
|
2014-11-02 02:27:54 +01:00
|
|
|
}
|
2014-11-08 15:40:12 +01:00
|
|
|
else if (g_file[chn]->randomfd != -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
fd = g_file[chn]->randomfd;
|
2014-11-02 14:16:26 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
else
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
fd = g_file[chn]->binaryfd;
|
2014-11-02 14:16:26 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
assert(fd != -1);
|
|
|
|
if ((cur = lseek(fd, 0, SEEK_CUR)) == -1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
return (cur + offset) / g_file[chn]->recLength;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
long int FS_lof(int chn)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 20:54:12 +01:00
|
|
|
off_t curpos;
|
|
|
|
off_t endpos;
|
2014-10-27 14:53:12 +01:00
|
|
|
int fd;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(chn, -1) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[chn]->infd != -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
fd = g_file[chn]->infd;
|
2014-11-02 14:16:26 +01:00
|
|
|
}
|
2014-11-08 15:40:12 +01:00
|
|
|
else if (g_file[chn]->outfd != -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
fd = g_file[chn]->outfd;
|
2014-11-02 14:16:26 +01:00
|
|
|
}
|
2014-11-08 15:40:12 +01:00
|
|
|
else if (g_file[chn]->randomfd != -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
fd = g_file[chn]->randomfd;
|
2014-11-02 14:16:26 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
else
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
fd = g_file[chn]->binaryfd;
|
2014-11-02 14:16:26 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
assert(fd != -1);
|
2014-11-02 20:54:12 +01:00
|
|
|
|
|
|
|
/* Get the size of the file */
|
|
|
|
/* Save the current file position */
|
|
|
|
|
|
|
|
curpos = lseek(fd, 0, SEEK_CUR);
|
|
|
|
if (curpos == (off_t)-1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the position at the end of the file */
|
|
|
|
|
|
|
|
endpos = lseek(fd, 0, SEEK_END);
|
|
|
|
if (endpos == (off_t)-1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore the file position */
|
|
|
|
|
|
|
|
curpos = lseek(fd, curpos, SEEK_SET);
|
|
|
|
if (curpos == (off_t)-1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-03 19:03:42 +02:00
|
|
|
return (long int)(endpos / g_file[chn]->recLength);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
long int FS_recLength(int chn)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
if (opened(chn, 2) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
return g_file[chn]->recLength;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
void FS_field(int chn, struct String *s, long int position, long int length)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
assert(g_file[chn]);
|
|
|
|
String_joinField(s, &g_file[chn]->field, g_file[chn]->recBuf + position, length);
|
2014-11-02 02:27:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int FS_seek(int chn, long int record)
|
|
|
|
{
|
|
|
|
if (opened(chn, 2) != -1)
|
|
|
|
{
|
|
|
|
if (lseek
|
2014-11-08 15:40:12 +01:00
|
|
|
(g_file[chn]->randomfd, (off_t) record * g_file[chn]->recLength,
|
2014-11-02 02:27:54 +01:00
|
|
|
SEEK_SET) != -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
}
|
|
|
|
else if (opened(chn, 4) != -1)
|
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
if (lseek(g_file[chn]->binaryfd, (off_t) record, SEEK_SET) != -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_appendToString(int chn, struct String *s, int onl)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
size_t new;
|
|
|
|
char *n;
|
2014-11-08 15:40:12 +01:00
|
|
|
struct FileStream *f = g_file[chn];
|
2014-10-27 14:53:12 +01:00
|
|
|
int c;
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if (f->tty && f->inSize == f->inCapacity)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
if (edit(chn, onl) == -1)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
return (FS_errmsg ? -1 : 0);
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
do
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
n = f->inBuf + f->inSize;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (n == f->inBuf + f->inCapacity)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
c = *n++;
|
|
|
|
if (c == '\n')
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
new = n - (f->inBuf + f->inSize);
|
|
|
|
if (new)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
size_t offset = s->length;
|
|
|
|
|
|
|
|
if (String_size(s, offset + new) == -1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(s->character + offset, f->inBuf + f->inSize, new);
|
|
|
|
f->inSize += new;
|
|
|
|
if (*(n - 1) == '\n')
|
|
|
|
{
|
|
|
|
if (f->inSize == f->inCapacity)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
f->inSize = f->inCapacity = 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((c = FS_getChar(chn)) >= 0)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
|
|
|
String_appendChar(s, c);
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
if (s->length >= 2 && s->character[s->length - 2] == '\r')
|
|
|
|
{
|
|
|
|
s->character[s->length - 2] = '\n';
|
|
|
|
--s->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
while (c != -1);
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return (FS_errmsg ? -1 : 0);
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
void FS_closefiles(void)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
/* Example each entry in the g_files[] arrary */
|
|
|
|
|
|
|
|
for (i = 0; i < g_capacity; ++i)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
/* Has this entry been allocated? Is it a file? Or a device? */
|
|
|
|
|
|
|
|
if (g_file[i] && !g_file[i]->dev)
|
2014-11-02 14:16:26 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
/* It is an open file, close it */
|
|
|
|
|
2014-11-02 14:16:26 +01:00
|
|
|
FS_close(i);
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_charpos(int chn)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 15:40:12 +01:00
|
|
|
if (g_file[chn] == (struct FileStream *)0)
|
2014-11-02 02:27:54 +01:00
|
|
|
{
|
|
|
|
FS_errmsg = _("channel not open");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-08 15:40:12 +01:00
|
|
|
return (g_file[chn]->outPos);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_copy(const char *from, const char *to)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
int infd, outfd;
|
2014-10-27 14:53:12 +01:00
|
|
|
char buf[4096];
|
2014-11-02 02:27:54 +01:00
|
|
|
ssize_t inlen, outlen = -1;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if ((infd = open(from, O_RDONLY)) == -1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:27:54 +01:00
|
|
|
if ((outfd = open(to, O_WRONLY | O_CREAT | O_TRUNC, 0666)) == -1)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
while ((inlen = read(infd, &buf, sizeof(buf))) > 0)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
ssize_t off = 0;
|
|
|
|
|
|
|
|
while (inlen && (outlen = write(outfd, &buf + off, inlen)) > 0)
|
|
|
|
{
|
|
|
|
off += outlen;
|
|
|
|
inlen -= outlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outlen == -1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
close(infd);
|
|
|
|
close(outfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inlen == -1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
2014-10-27 14:53:12 +01:00
|
|
|
close(infd);
|
|
|
|
close(outfd);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
if (close(infd) == -1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
close(outfd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (close(outfd) == -1)
|
|
|
|
{
|
|
|
|
FS_errmsg = strerror(errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_portInput(int address)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = _("Direct port access not available");
|
2014-10-27 14:53:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_memInput(int address)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = _("Direct memory access not available");
|
2014-10-27 14:53:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_portOutput(int address, int value)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = _("Direct port access not available");
|
2014-10-27 14:53:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:27:54 +01:00
|
|
|
|
|
|
|
int FS_memOutput(int address, int value)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:27:54 +01:00
|
|
|
FS_errmsg = _("Direct memory access not available");
|
2014-10-27 14:53:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|