mg: specify a different startup file

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

mg: specify a different startup file

Mark Lumsden-3
This diff allows mg(1) to specify an alternative startup file on
the fly when starting mg:

$ mg -u ~/.mg2

I decided to use "u" since that is what emacs uses to load
another user's init file:

https://www.gnu.org/software/emacs/manual/html_node/emacs/Init-File.html

Perhaps "f" would be better but that is being used to set mg modes

Also, I decided not to load the default startup file if the
alternative is not found since it seems more obvious that something
is wrong that way.

Any comments/ok?

Mark

Index: def.h
===================================================================
RCS file: /cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.166
diff -u -p -u -p -r1.166 def.h
--- def.h 9 Feb 2020 10:13:13 -0000 1.166
+++ def.h 21 Feb 2021 12:55:41 -0000
@@ -471,7 +471,7 @@ int ffputbuf(FILE *, struct buffer *,
  int ffgetline(FILE *, char *, int, int *);
  int fbackupfile(const char *);
  char *adjustname(const char *, int);
-char *startupfile(char *);
+char *startupfile(char *, char *);
  int copy(char *, char *);
  struct list *make_file_list(char *);
  int fisdir(const char *);
Index: fileio.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/fileio.c,v
retrieving revision 1.106
diff -u -p -u -p -r1.106 fileio.c
--- fileio.c 22 Jun 2019 10:21:57 -0000 1.106
+++ fileio.c 21 Feb 2021 12:55:41 -0000
@@ -330,7 +330,7 @@ adjustname(const char *fn, int slashslas
   * to the startup file name.
   */
  char *
-startupfile(char *suffix)
+startupfile(char *suffix, char *conffile)
  {
  static char file[NFILEN];
  char *home;
@@ -339,7 +339,9 @@ startupfile(char *suffix)
  if ((home = getenv("HOME")) == NULL || *home == '\0')
  goto nohome;

- if (suffix == NULL) {
+ if (conffile != NULL) {
+ (void)strncpy(file, conffile, NFILEN);
+ } else if (suffix == NULL) {
  ret = snprintf(file, sizeof(file), _PATH_MG_STARTUP, home);
  if (ret < 0 || ret >= sizeof(file))
  return (NULL);
Index: main.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/main.c,v
retrieving revision 1.87
diff -u -p -u -p -r1.87 main.c
--- main.c 22 Jun 2019 15:38:15 -0000 1.87
+++ main.c 21 Feb 2021 12:55:41 -0000
@@ -48,7 +48,8 @@ extern void     closetags(void);
  static __dead void
  usage()
  {
- fprintf(stderr, "usage: %s [-nR] [-f mode] [+number] [file ...]\n",
+ fprintf(stderr, "usage: %s [-nR] [-f mode] [-u file] [+number] "
+    "[file ...]\n",
     __progname);
  exit(1);
  }
@@ -56,7 +57,7 @@ usage()
  int
  main(int argc, char **argv)
  {
- char *cp, *init_fcn_name = NULL;
+ char *cp, *conffile = NULL, *init_fcn_name = NULL;
  PF init_fcn = NULL;
  int o, i, nfiles;
  int   nobackups = 0;
@@ -66,7 +67,7 @@ main(int argc, char **argv)
     NULL) == -1)
  err(1, "pledge");

- while ((o = getopt(argc, argv, "nRf:")) != -1)
+ while ((o = getopt(argc, argv, "nRf:u:")) != -1)
  switch (o) {
  case 'R':
  allbro = 1;
@@ -80,6 +81,9 @@ main(int argc, char **argv)
     "initial function");
  init_fcn_name = optarg;
  break;
+ case 'u':
+ conffile = optarg;
+ break;
  default:
  usage();
  }
@@ -129,7 +133,7 @@ main(int argc, char **argv)
  update(CMODE);

  /* user startup file. */
- if ((cp = startupfile(NULL)) != NULL)
+ if ((cp = startupfile(NULL, conffile)) != NULL)
  (void)load(cp);

  /*
Index: mg.1
===================================================================
RCS file: /cvs/src/usr.bin/mg/mg.1,v
retrieving revision 1.118
diff -u -p -u -p -r1.118 mg.1
--- mg.1 8 Nov 2019 19:54:40 -0000 1.118
+++ mg.1 21 Feb 2021 12:55:42 -0000
@@ -11,6 +11,7 @@
  .Nm mg
  .Op Fl nR
  .Op Fl f Ar mode
+.Op Fl u Ar file
  .Op + Ns Ar number
  .Op Ar
  .Sh DESCRIPTION
@@ -38,6 +39,11 @@ line of the file, +-2 will be second las
  Run the mode command for all buffers created from
  arguments on the command line, including the
  scratch buffer and all files.
+.It Fl u Ar file
+Use
+.Ar file
+as the startup file, instead of the default
+.Pa ~/.mg .
  .It Fl n
  Turn off backup file generation.
  .It Fl R
Index: ttykbd.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/ttykbd.c,v
retrieving revision 1.19
diff -u -p -u -p -r1.19 ttykbd.c
--- ttykbd.c 17 Dec 2017 14:37:57 -0000 1.19
+++ ttykbd.c 21 Feb 2021 12:55:42 -0000
@@ -58,7 +58,8 @@ ttykeymapinit(void)
  dobindkey(fundamental_map, "delete-char", key_dc);

  if ((cp = getenv("TERM"))) {
- if (((cp = startupfile(cp)) != NULL) && (load(cp) != TRUE))
+ if (((cp = startupfile(cp, NULL)) != NULL) &&
+    (load(cp) != TRUE))
  ewprintf("Error reading key initialization file");
  }
  if (keypad_xmit)