--- freem/src/cmd_zedit.c 2025/05/19 01:08:12 1.1 +++ freem/src/cmd_zedit.c 2025/05/19 21:29:29 1.4 @@ -1,7 +1,5 @@ - /* - * - * $Id: cmd_zedit.c,v 1.1 2025/05/19 01:08:12 snw Exp $ + * $Id: cmd_zedit.c,v 1.4 2025/05/19 21:29:29 snw Exp $ * Implementation of the ZEDIT command * * @@ -26,6 +24,15 @@ * along with FreeM. If not, see . * * $Log: cmd_zedit.c,v $ + * Revision 1.4 2025/05/19 21:29:29 snw + * Add basic tab completion to direct mode + * + * Revision 1.3 2025/05/19 17:57:20 snw + * Extend ZEDIT to create the specified routine if it does not yet exist + * + * Revision 1.2 2025/05/19 02:03:31 snw + * Reverse-engineer and document argumented ZPRINT (thanks to D. Wicksell) + * * Revision 1.1 2025/05/19 01:08:12 snw * Add ZEDIT command (properly this time) * @@ -35,6 +42,7 @@ **/ #include +#include #include #include #include "mpsdef.h" @@ -48,13 +56,16 @@ MRESULT cmd_zedit(MACTION *ra) char editor_command[PATHLEN]; char *tgt_editor; short free_editor = FALSE; - + FILE *fp; + if (restricted_mode) { + /* we never shell out in restricted mode */ retval = NOSTAND; goto done; } if (rou_name[0] == EOL) { + /* there was no current routine */ retval = NOPGM; goto done; } @@ -64,13 +75,18 @@ MRESULT cmd_zedit(MACTION *ra) NULLPTRCHK(tgt_editor,"cmd_zedit"); snprintf (tgt_editor, STRLEN - 1, "/usr/bin/vi"); + + /* had to allocate the editor path on the heap, + so we need to free it later */ free_editor = TRUE; } if (*codptr == SP || *codptr == EOL) { + /* argumentless ZEDIT edits the current routine */ stcpy (tgt_routine, rou_name); } else { + /* argumented ZEDIT edits a specified routine */ expr (NAME); if (varnam[0] == '^') { @@ -94,21 +110,30 @@ MRESULT cmd_zedit(MACTION *ra) stcnv_m2c (tgt_routine); if ((rtn_get_path (tgt_routine, pth)) == FALSE) { - retval = NOPGM; - goto done; + /* the routine could not be found */ + if ((fp = fopen (pth, "w")) == NULL) { + retval = PROTECT; + goto done; + } + + fprintf (fp, "%s ; Created by FreeM Administrator\n QUIT\n", tgt_routine); + fclose (fp); } snprintf (editor_command, sizeof (editor_command) - 1, "%s %s", tgt_editor, pth); - + + /* make the environment friendly to running external programs */ sig_attach (SIGUSR1, SIG_IGN); set_io (UNIX); system (editor_command); + /* back into mumps */ set_io (MUMPS); sig_attach (SIGUSR1, &oncld); retval = OK; + codptr++; done: if (free_editor) free (tgt_editor);