Annotation of freem/src/cmd_zedit.c, revision 1.2

1.1       snw         1: 
                      2: /*
                      3:  *
1.2     ! snw         4:  *   $Id: cmd_zedit.c,v 1.1 2025/05/19 01:08:12 snw Exp $
1.1       snw         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:  *
1.2     ! snw        28:  *   $Log: cmd_zedit.c,v $
        !            29:  *   Revision 1.1  2025/05/19 01:08:12  snw
        !            30:  *   Add ZEDIT command (properly this time)
        !            31:  *
1.1       snw        32:  *
                     33:  * SPDX-FileCopyrightText:  (C) 2025 Coherent Logic Development LLC
                     34:  * SPDX-License-Identifier: AGPL-3.0-or-later
                     35:  **/
                     36: 
                     37: #include <string.h>
                     38: #include <stdlib.h>
                     39: #include <ctype.h>
                     40: #include "mpsdef.h"
                     41: #include "mcommand.h"
                     42: 
                     43: MRESULT cmd_zedit(MACTION *ra)
                     44: {    
                     45:     MRESULT retval;
                     46:     char pth[PATHLEN];
                     47:     char tgt_routine[PATHLEN];    
                     48:     char editor_command[PATHLEN];
                     49:     char *tgt_editor;
                     50:     short free_editor = FALSE;
                     51: 
                     52:     if (restricted_mode) {
1.2     ! snw        53:         /* we never shell out in restricted mode */
1.1       snw        54:         retval = NOSTAND;
                     55:         goto done;
                     56:     }
                     57: 
                     58:     if (rou_name[0] == EOL) {
1.2     ! snw        59:         /* there was no current routine */
1.1       snw        60:         retval = NOPGM;
                     61:         goto done;
                     62:     }        
                     63:     
                     64:     if ((tgt_editor = getenv ("EDITOR")) == NULL) {
                     65:         tgt_editor = (char *) malloc (STRLEN * sizeof (char));
                     66:         NULLPTRCHK(tgt_editor,"cmd_zedit");
                     67:         
                     68:         snprintf (tgt_editor, STRLEN - 1, "/usr/bin/vi");
1.2     ! snw        69: 
        !            70:         /* had to allocate the editor path on the heap,
        !            71:            so we need to free it later */         
1.1       snw        72:         free_editor = TRUE;
                     73:     }
                     74:     
                     75:     if (*codptr == SP || *codptr == EOL) {
1.2     ! snw        76:         /* argumentless ZEDIT edits the current routine */
1.1       snw        77:         stcpy (tgt_routine, rou_name);
                     78:     }
                     79:     else {
1.2     ! snw        80:         /* argumented ZEDIT edits a specified routine */
1.1       snw        81:         expr (NAME);
                     82:         
                     83:         if (varnam[0] == '^') {
                     84:             retval = GLOBER;
                     85:             goto done;
                     86:         }
                     87: 
                     88:         if (varnam[0] == '$') {
                     89:             retval = INVREF;
                     90:             goto done;
                     91:         }
                     92: 
                     93:         if (merr ()) {
                     94:             retval = merr ();
                     95:             goto done;
                     96:         }
                     97:         
                     98:         stcpy (tgt_routine, varnam);
                     99:     }
                    100: 
                    101:     stcnv_m2c (tgt_routine);
                    102:     
                    103:     if ((rtn_get_path (tgt_routine, pth)) == FALSE) {
1.2     ! snw       104:         /* the routine could not be found */
1.1       snw       105:         retval = NOPGM;
                    106:         goto done;
                    107:     }
                    108:     
                    109:     snprintf (editor_command, sizeof (editor_command) - 1, "%s %s", tgt_editor, pth);
1.2     ! snw       110: 
        !           111:     /* make the environment friendly to running external programs */
1.1       snw       112:     sig_attach (SIGUSR1, SIG_IGN);
                    113:     set_io (UNIX);
                    114: 
                    115:     system (editor_command);
                    116: 
1.2     ! snw       117:     /* back into mumps */
1.1       snw       118:     set_io (MUMPS);
                    119:     sig_attach (SIGUSR1, &oncld);
                    120: 
                    121:     retval = OK;
                    122:     
                    123: done:
                    124:     if (free_editor) free (tgt_editor);
                    125:     *ra = RA_CONTINUE;
                    126:     return retval;        
                    127: }

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