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

1.1       snw         1: /*
1.6     ! snw         2:  *   $Id: cmd_zedit.c,v 1.5 2025/05/20 14:36:06 snw Exp $
1.1       snw         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:  *
1.2       snw        26:  *   $Log: cmd_zedit.c,v $
1.6     ! snw        27:  *   Revision 1.5  2025/05/20 14:36:06  snw
        !            28:  *   Documentation updates; raise ZCMMND instead of NOSTAND for restricted_mode restrictions
        !            29:  *
1.5       snw        30:  *   Revision 1.4  2025/05/19 21:29:29  snw
                     31:  *   Add basic tab completion to direct mode
                     32:  *
1.4       snw        33:  *   Revision 1.3  2025/05/19 17:57:20  snw
                     34:  *   Extend ZEDIT to create the specified routine if it does not yet exist
                     35:  *
1.3       snw        36:  *   Revision 1.2  2025/05/19 02:03:31  snw
                     37:  *   Reverse-engineer and document argumented ZPRINT (thanks to D. Wicksell)
                     38:  *
1.2       snw        39:  *   Revision 1.1  2025/05/19 01:08:12  snw
                     40:  *   Add ZEDIT command (properly this time)
                     41:  *
1.1       snw        42:  *
                     43:  * SPDX-FileCopyrightText:  (C) 2025 Coherent Logic Development LLC
                     44:  * SPDX-License-Identifier: AGPL-3.0-or-later
                     45:  **/
                     46: 
                     47: #include <string.h>
1.3       snw        48: #include <stdio.h>
1.1       snw        49: #include <stdlib.h>
                     50: #include <ctype.h>
                     51: #include "mpsdef.h"
                     52: #include "mcommand.h"
                     53: 
                     54: MRESULT cmd_zedit(MACTION *ra)
                     55: {    
                     56:     MRESULT retval;
                     57:     char pth[PATHLEN];
                     58:     char tgt_routine[PATHLEN];    
                     59:     char editor_command[PATHLEN];
                     60:     char *tgt_editor;
                     61:     short free_editor = FALSE;
1.3       snw        62:     FILE *fp;
                     63:     
1.1       snw        64:     if (restricted_mode) {
1.2       snw        65:         /* we never shell out in restricted mode */
1.5       snw        66:         retval = CMMND;
1.1       snw        67:         goto done;
                     68:     }
                     69: 
                     70:     if (rou_name[0] == EOL) {
1.2       snw        71:         /* there was no current routine */
1.1       snw        72:         retval = NOPGM;
                     73:         goto done;
                     74:     }        
                     75:     
                     76:     if ((tgt_editor = getenv ("EDITOR")) == NULL) {
                     77:         tgt_editor = (char *) malloc (STRLEN * sizeof (char));
                     78:         NULLPTRCHK(tgt_editor,"cmd_zedit");
                     79:         
                     80:         snprintf (tgt_editor, STRLEN - 1, "/usr/bin/vi");
1.2       snw        81: 
                     82:         /* had to allocate the editor path on the heap,
                     83:            so we need to free it later */         
1.1       snw        84:         free_editor = TRUE;
                     85:     }
                     86:     
                     87:     if (*codptr == SP || *codptr == EOL) {
1.2       snw        88:         /* argumentless ZEDIT edits the current routine */
1.1       snw        89:         stcpy (tgt_routine, rou_name);
                     90:     }
                     91:     else {
1.2       snw        92:         /* argumented ZEDIT edits a specified routine */
1.1       snw        93:         expr (NAME);
                     94:         
                     95:         if (varnam[0] == '^') {
                     96:             retval = GLOBER;
                     97:             goto done;
                     98:         }
                     99: 
                    100:         if (varnam[0] == '$') {
                    101:             retval = INVREF;
                    102:             goto done;
                    103:         }
                    104: 
                    105:         if (merr ()) {
                    106:             retval = merr ();
                    107:             goto done;
                    108:         }
                    109:         
                    110:         stcpy (tgt_routine, varnam);
                    111:     }
                    112: 
                    113:     stcnv_m2c (tgt_routine);
                    114:     
                    115:     if ((rtn_get_path (tgt_routine, pth)) == FALSE) {
1.2       snw       116:         /* the routine could not be found */
1.3       snw       117:         if ((fp = fopen (pth, "w")) == NULL) {
                    118:             retval = PROTECT;
                    119:             goto done;
                    120:         }
                    121: 
                    122:         fprintf (fp, "%s ; Created by FreeM Administrator\n    QUIT\n", tgt_routine);
                    123:         fclose (fp);
1.1       snw       124:     }
                    125:     
                    126:     snprintf (editor_command, sizeof (editor_command) - 1, "%s %s", tgt_editor, pth);
1.2       snw       127: 
                    128:     /* make the environment friendly to running external programs */
1.1       snw       129:     sig_attach (SIGUSR1, SIG_IGN);
                    130:     set_io (UNIX);
                    131: 
                    132:     system (editor_command);
                    133: 
1.6     ! snw       134:     ssvn_routine_update ();
        !           135:     
1.2       snw       136:     /* back into mumps */
1.1       snw       137:     set_io (MUMPS);
                    138:     sig_attach (SIGUSR1, &oncld);
                    139: 
                    140:     retval = OK;
1.3       snw       141:     codptr++;
1.1       snw       142:     
                    143: done:
                    144:     if (free_editor) free (tgt_editor);
                    145:     *ra = RA_CONTINUE;
                    146:     return retval;        
                    147: }

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