File:  [Coherent Logic Development] / freem / src / cmd_zedit.c
Revision 1.2: download - view: text, annotated - select for diffs
Mon May 19 02:03:31 2025 UTC (5 months, 1 week ago) by snw
Branches: MAIN
CVS tags: HEAD
Reverse-engineer and document argumented ZPRINT (thanks to D. Wicksell)

    1: 
    2: /*
    3:  *
    4:  *   $Id: cmd_zedit.c,v 1.2 2025/05/19 02:03:31 snw Exp $
    5:  *    Implementation of the ZEDIT command
    6:  *
    7:  *  
    8:  *   Author: Serena Willis <snw@coherent-logic.com>
    9:  *    Copyright (C) 1998 MUG Deutschland
   10:  *    Copyright (C) 2025 Coherent Logic Development LLC
   11:  *
   12:  *
   13:  *   This file is part of FreeM.
   14:  *
   15:  *   FreeM is free software: you can redistribute it and/or modify
   16:  *   it under the terms of the GNU Affero Public License as published by
   17:  *   the Free Software Foundation, either version 3 of the License, or
   18:  *   (at your option) any later version.
   19:  *
   20:  *   FreeM is distributed in the hope that it will be useful,
   21:  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   22:  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   23:  *   GNU Affero Public License for more details.
   24:  *
   25:  *   You should have received a copy of the GNU Affero Public License
   26:  *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.
   27:  *
   28:  *   $Log: cmd_zedit.c,v $
   29:  *   Revision 1.2  2025/05/19 02:03:31  snw
   30:  *   Reverse-engineer and document argumented ZPRINT (thanks to D. Wicksell)
   31:  *
   32:  *   Revision 1.1  2025/05/19 01:08:12  snw
   33:  *   Add ZEDIT command (properly this time)
   34:  *
   35:  *
   36:  * SPDX-FileCopyrightText:  (C) 2025 Coherent Logic Development LLC
   37:  * SPDX-License-Identifier: AGPL-3.0-or-later
   38:  **/
   39: 
   40: #include <string.h>
   41: #include <stdlib.h>
   42: #include <ctype.h>
   43: #include "mpsdef.h"
   44: #include "mcommand.h"
   45: 
   46: MRESULT cmd_zedit(MACTION *ra)
   47: {    
   48:     MRESULT retval;
   49:     char pth[PATHLEN];
   50:     char tgt_routine[PATHLEN];    
   51:     char editor_command[PATHLEN];
   52:     char *tgt_editor;
   53:     short free_editor = FALSE;
   54: 
   55:     if (restricted_mode) {
   56:         /* we never shell out in restricted mode */
   57:         retval = NOSTAND;
   58:         goto done;
   59:     }
   60: 
   61:     if (rou_name[0] == EOL) {
   62:         /* there was no current routine */
   63:         retval = NOPGM;
   64:         goto done;
   65:     }        
   66:     
   67:     if ((tgt_editor = getenv ("EDITOR")) == NULL) {
   68:         tgt_editor = (char *) malloc (STRLEN * sizeof (char));
   69:         NULLPTRCHK(tgt_editor,"cmd_zedit");
   70:         
   71:         snprintf (tgt_editor, STRLEN - 1, "/usr/bin/vi");
   72: 
   73:         /* had to allocate the editor path on the heap,
   74:            so we need to free it later */         
   75:         free_editor = TRUE;
   76:     }
   77:     
   78:     if (*codptr == SP || *codptr == EOL) {
   79:         /* argumentless ZEDIT edits the current routine */
   80:         stcpy (tgt_routine, rou_name);
   81:     }
   82:     else {
   83:         /* argumented ZEDIT edits a specified routine */
   84:         expr (NAME);
   85:         
   86:         if (varnam[0] == '^') {
   87:             retval = GLOBER;
   88:             goto done;
   89:         }
   90: 
   91:         if (varnam[0] == '$') {
   92:             retval = INVREF;
   93:             goto done;
   94:         }
   95: 
   96:         if (merr ()) {
   97:             retval = merr ();
   98:             goto done;
   99:         }
  100:         
  101:         stcpy (tgt_routine, varnam);
  102:     }
  103: 
  104:     stcnv_m2c (tgt_routine);
  105:     
  106:     if ((rtn_get_path (tgt_routine, pth)) == FALSE) {
  107:         /* the routine could not be found */
  108:         retval = NOPGM;
  109:         goto done;
  110:     }
  111:     
  112:     snprintf (editor_command, sizeof (editor_command) - 1, "%s %s", tgt_editor, pth);
  113: 
  114:     /* make the environment friendly to running external programs */
  115:     sig_attach (SIGUSR1, SIG_IGN);
  116:     set_io (UNIX);
  117: 
  118:     system (editor_command);
  119: 
  120:     /* back into mumps */
  121:     set_io (MUMPS);
  122:     sig_attach (SIGUSR1, &oncld);
  123: 
  124:     retval = OK;
  125:     
  126: done:
  127:     if (free_editor) free (tgt_editor);
  128:     *ra = RA_CONTINUE;
  129:     return retval;        
  130: }

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