Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

autoloading modules



-----BEGIN PGP SIGNED MESSAGE-----

I came up with this cool function yesterday:

function zmodautoload
{
  if [[ $# != 2 ]]; then
    echo >&2 "zmodautoload: wrong number of arguments"
    return 1
  fi
  eval "function $1 { if zmodload $2; then unfunction $1; $1 \$@; fi; }"
}

In order to support autoloading of modules that provide multiple builtins,
it would be helped by an option to zmodload that says "load this module,
but keep quiet if it's already loaded".  The patch below adds such an
option, -i (for idempotent).  To make use of it in zmodautoload, change
"zmodload" to "zmodload -i" above.

Zoltan, what is "zmodload -f" for?

 -zefram

      Index: Doc/zshbuiltins.man
      ===================================================================
      RCS file: /home/zefram/usr/cvsroot/zsh/Doc/zshbuiltins.man,v
      retrieving revision 1.13
      diff -c -r1.13 zshbuiltins.man
      *** zshbuiltins.man	1996/11/23 01:18:50	1.13
      --- zshbuiltins.man	1996/11/23 21:57:21
      ***************
      *** 1199,1205 ****
        \fBwhich\fP [ \-\fBpam\fP ] \fIname\fP ...
        Same as \fBwhence \-c\fP.
        %IFDYN%.TP
      ! %IFDYN%\fBzmodload \fP[\fB-u\fP] \fIname
        %IFDYN%Load (or, with
        %IFDYN%.BR -u ,
        %IFDYN%unload) a binary module.
      --- 1199,1205 ----
        \fBwhich\fP [ \-\fBpam\fP ] \fIname\fP ...
        Same as \fBwhence \-c\fP.
        %IFDYN%.TP
      ! %IFDYN%\fBzmodload \fP[\-\fBfiu\fP] \fIname
        %IFDYN%Load (or, with
        %IFDYN%.BR -u ,
        %IFDYN%unload) a binary module.
      ***************
      *** 1209,1214 ****
      --- 1209,1220 ----
        %IFDYN%.IR name .
        %IFDYN%.RS
        %IFDYN%.PP
      + %IFDYN%If the module to be loaded is already loaded, the behaviour
      + %IFDYN%depends on the options.  If the \fB\-i\fP option is given,
      + %IFDYN%the duplicate module is ignored.  Otherwise, if the \fB\-f\fP
      + %IFDYN%option is given, an attempt is made to load the module again.
      + %IFDYN%Otherwise (if neither option is given), it is an error.
      + %IFDYN%.PP
        %IFDYN%The
        %IFDYN%.IR name d
        %IFDYN%module is searched for in the same way a command is, using
      ***************
      *** 1228,1232 ****
      --- 1234,1240 ----
        %IFDYN%.I name
        %IFDYN%must be given that was given when the module was loaded, but it is not
        %IFDYN%necessary for the module to exist in the filesystem.
      + %IFDYN%The \fB\-i\fP option suppresses the error if the module is already
      + %IFDYN%unloaded (or was never loaded).
        %IFDYN%.RE
        .RE
      Index: Src/hashtable.h
      ===================================================================
      RCS file: /home/zefram/usr/cvsroot/zsh/Src/hashtable.h,v
      retrieving revision 1.21
      diff -c -r1.21 hashtable.h
      *** hashtable.h	1996/11/15 00:30:54	1.21
      --- hashtable.h	1996/11/23 21:58:48
      ***************
      *** 304,310 ****
        #endif
        
        #ifdef DYNAMIC
      !     {NULL, "zmodload", 0, bin_zmodload, 0, -1, 0, "uf", NULL},
        #endif
            {NULL, "popd", 0, bin_cd, 0, 2, BIN_POPD, NULL, NULL},
            {NULL, "print", BINF_PRINTOPTS, bin_print, 0, -1, BIN_PRINT, "RDPnrslzNu0123456789pioOcm-", NULL},
      --- 304,310 ----
        #endif
        
        #ifdef DYNAMIC
      !     {NULL, "zmodload", 0, bin_zmodload, 0, -1, 0, "ufi", NULL},
        #endif
            {NULL, "popd", 0, bin_cd, 0, 2, BIN_POPD, NULL, NULL},
            {NULL, "print", BINF_PRINTOPTS, bin_print, 0, -1, BIN_PRINT, "RDPnrslzNu0123456789pioOcm-", NULL},
      Index: Src/module.c
      ===================================================================
      RCS file: /home/zefram/usr/cvsroot/zsh/Src/module.c,v
      retrieving revision 1.12
      diff -c -r1.12 module.c
      *** module.c	1996/11/23 01:18:00	1.12
      --- module.c	1996/11/23 21:59:45
      ***************
      *** 168,179 ****
        	    }
        	}
        	if (ops['u']) {
      ! 	    if (! found) {
        		zwarnnam(nam, "no such module %s", *args, 0);
        		ret++;
        	    }
        	} else if (found) {
      ! 	    if (! ops['f']) {
        		zwarnnam(nam, "module %s already loaded.", *args, 0);
        		ret++;
        	    } else if ((handle = load_module(*args)) != m->handle) {
      --- 168,181 ----
        	    }
        	}
        	if (ops['u']) {
      ! 	    if (!found && !ops['i']) {
        		zwarnnam(nam, "no such module %s", *args, 0);
        		ret++;
        	    }
        	} else if (found) {
      ! 	    if(ops['i'])
      ! 		;
      ! 	    else if(!ops['f']) {
        		zwarnnam(nam, "module %s already loaded.", *args, 0);
        		ret++;
        	    } else if ((handle = load_module(*args)) != m->handle) {

-----BEGIN PGP SIGNATURE-----
Version: 2.6.2

iQCVAwUBMpd2+nD/+HJTpU/hAQFlEAP/TQpSXPbcyTPjibllIlSHmXBmqAkRWvO0
KszHN+5MW5UJQUQqwHIheOxfB8lbAZbcKXCD6jMClf7rzrqCYnwdp4l8VIgp6ra8
Rd/e+4fRgmubXlV6PcYlMLANR4Heyx9BjNVdjINopgd2koKnDHk+/XyjgOt3Jesm
PokPoq24rX4=
=5Sh9
-----END PGP SIGNATURE-----



Messages sorted by: Reverse Date, Date, Thread, Author