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

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

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