File:  [Coherent Logic Development] / freem / src / cmd_zedit.c
Revision 1.1: download - view: text, annotated - select for diffs
Mon May 19 01:08:12 2025 UTC (2 weeks, 5 days ago) by snw
Branches: MAIN
CVS tags: HEAD
Add ZEDIT command (properly this time)


/*
 *
 *   $Id: cmd_zedit.c,v 1.1 2025/05/19 01:08:12 snw Exp $
 *    Implementation of the ZEDIT command
 *
 *  
 *   Author: Serena Willis <snw@coherent-logic.com>
 *    Copyright (C) 1998 MUG Deutschland
 *    Copyright (C) 2025 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_zedit.c,v $
 *   Revision 1.1  2025/05/19 01:08:12  snw
 *   Add ZEDIT command (properly this time)
 *
 *
 * SPDX-FileCopyrightText:  (C) 2025 Coherent Logic Development LLC
 * SPDX-License-Identifier: AGPL-3.0-or-later
 **/

#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "mpsdef.h"
#include "mcommand.h"

MRESULT cmd_zedit(MACTION *ra)
{    
    MRESULT retval;
    char pth[PATHLEN];
    char tgt_routine[PATHLEN];    
    char editor_command[PATHLEN];
    char *tgt_editor;
    short free_editor = FALSE;

    if (restricted_mode) {
        retval = NOSTAND;
        goto done;
    }

    if (rou_name[0] == EOL) {
        retval = NOPGM;
        goto done;
    }        
    
    if ((tgt_editor = getenv ("EDITOR")) == NULL) {
        tgt_editor = (char *) malloc (STRLEN * sizeof (char));
        NULLPTRCHK(tgt_editor,"cmd_zedit");
        
        snprintf (tgt_editor, STRLEN - 1, "/usr/bin/vi");
        free_editor = TRUE;
    }
    
    if (*codptr == SP || *codptr == EOL) {
        stcpy (tgt_routine, rou_name);
    }
    else {
        expr (NAME);
        
        if (varnam[0] == '^') {
            retval = GLOBER;
            goto done;
        }

        if (varnam[0] == '$') {
            retval = INVREF;
            goto done;
        }

        if (merr ()) {
            retval = merr ();
            goto done;
        }
        
        stcpy (tgt_routine, varnam);
    }

    stcnv_m2c (tgt_routine);
    
    if ((rtn_get_path (tgt_routine, pth)) == FALSE) {
        retval = NOPGM;
        goto done;
    }
    
    snprintf (editor_command, sizeof (editor_command) - 1, "%s %s", tgt_editor, pth);
    
    sig_attach (SIGUSR1, SIG_IGN);
    set_io (UNIX);

    system (editor_command);

    set_io (MUMPS);
    sig_attach (SIGUSR1, &oncld);

    retval = OK;
    
done:
    if (free_editor) free (tgt_editor);
    *ra = RA_CONTINUE;
    return retval;        
}

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