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

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

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