File:  [Coherent Logic Development] / freem / src / cmd_write.c
Revision 1.5: download - view: text, annotated - select for diffs
Mon Mar 24 04:05:36 2025 UTC (8 days, 20 hours ago) by snw
Branches: MAIN
CVS tags: v0-62-3, HEAD
Replace crlf with frm_crlf to avoid symbol conflict with readline on OS/2

/*
 *   $Id: cmd_write.c,v 1.5 2025/03/24 04:05:36 snw Exp $
 *    Implementation of the WRITE command
 *
 *  
 *   Author: Serena Willis <snw@coherent-logic.com>
 *    Copyright (C) 1998 MUG Deutschland
 *    Copyright (C) 2023 Coherent Logic Development LLC
 *
 *
 *   This file is part of FreeM.
 *
 *   FreeM is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   FreeM is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Affero Public License for more details.
 *
 *   You should have received a copy of the GNU Affero Public License
 *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.
 *
 *   $Log: cmd_write.c,v $
 *   Revision 1.5  2025/03/24 04:05:36  snw
 *   Replace crlf with frm_crlf to avoid symbol conflict with readline on OS/2
 *
 *   Revision 1.4  2025/03/22 18:43:54  snw
 *   Make STRLEN 255 chars and add BIGSTR macro for larger buffers
 *
 *   Revision 1.3  2025/03/09 19:14:24  snw
 *   First phase of REUSE compliance and header reformat
 *
 *
 * SPDX-FileCopyrightText:  (C) 2025 Coherent Logic Development LLC
 * SPDX-License-Identifier: AGPL-3.0-or-later
 **/

#include <string.h>
#include <stdlib.h>
#include "mpsdef.h"
#include "mcommand.h"
#if !defined(MSDOS)
# include "io_socket.h"
#endif

MRESULT cmd_write(MACTION *ra, int *i)
{
    char vn[255];
    char *wr_terminator = (char *) malloc (STRLEN * sizeof (char));
    char *sw_buf = (char *) malloc (BIGSTR * sizeof (char));
    
    NULLPTRCHK(wr_terminator,"cmd_write");
    NULLPTRCHK(sw_buf,"cmd_write");

    if (io > FIRSTSCK) msck_get_terminator (io, wr_terminator);
    
    if (io != HOME && devopen[io] == 'r' && io < FIRSTSCK) {
        free (sw_buf);
        return NOWRITE;
    }
    
    if ((*i = (*codptr)) == SP || *i == EOL) {
        return ARGLIST;
    }
    
writeproc:
    
    switch (*i)
    {
        
        case '!':
            
            if (io < FIRSTSCK) {
                
                if (frm_crlf[io]) {
                    write_m ("\012\201");
                }
                else {
                    write_m ("\015\012\201");
                }
                
            }
            else {                
                /* WRITE ! for sockets */
                strcat (sw_buf, wr_terminator);                
            }
            
            break;
            
        case '#':
            
            if (io < FIRSTSCK) {
                write_m ("\015\014\201");
            }
            else {
                return SYNTERR;
            }
            
            break;
            
        case '?':
            
            codptr++;
            
            expr (STRING);
            
            if (merr ()) return merr ();
            
            if (io < FIRSTSCK) {
                write_t ((short) intexpr (argptr));
            }
            else {
                return SYNTERR;
            }
            
            break;
            
        case '/':
            codptr++;
            
            expr (NAME);
            if (merr ()) return merr ();
            
            if (io < FIRSTSCK) {
                write_f (varnam);
            }
            else {
                return SYNTERR;
            }
            
            codptr++;
            
            break;
            
        case '*':
            codptr++;
            
            expr (STRING);            
            if (merr ()) return merr ();
            
            argptr[0] = (char) UNSIGN (intexpr (argptr));
            argptr[1] = EOL;
            
            /* special treatment for EOL as long as we don't have 8 bit */
            if (argptr[0] == EOL) {
                
                mcmnd = '*';
                
                if (io < FIRSTSCK) {
                    m_output (argptr);
                }
                else {
                    /* EOL handling for socket devices */
                }
                mcmnd = WRITE;
                
            }
            else {
                
                if (io < FIRSTSCK) {
                    write_m (argptr);
                }
                else {
                    /* non-EOL char-code handling for sockets */
                }
                
            }
            
            break;
            
        default:
            expr (STRING);            
            if (merr ()) return merr ();
            
            if (io < FIRSTSCK) {
                write_m (argptr);
            }
            else {
                /* regular output for socket devices */
                stcpy (vn, argptr);
                stcnv_m2c (vn);
                strcat (sw_buf, vn);
            }
    }
    
    if ((*i == '!') || (*i == '#')) {
        if ((*i = *++codptr) == '!' || *i == '#' || *i == '?') goto writeproc;
    }
    
    if (io >= FIRSTSCK) {
        msck_write (io, sw_buf, strlen (sw_buf));
    }
    
    free (sw_buf);
    free (wr_terminator);
    
    *ra = RA_CONTINUE;
    return OK;    
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>