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