File:  [Coherent Logic Development] / freem / src / cmd_zedit.c
Revision 1.6: download - view: text, annotated - select for diffs
Tue May 20 16:20:42 2025 UTC (2 months, 1 week ago) by snw
Branches: MAIN
CVS tags: HEAD
Update ROUTINE SSVN after ZEDIT and ZSAVE

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

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