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

Re: [wip patch] new zsh/attr module



On Tue, 3 Mar 2009 15:43:29 +0100 (CET)
Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
> I've written some basic entries for the builtins, it doesn't seem to show 
> up properly in man zshmodules though, in the summary list at the top it 
> doesn't appear, but it does appear below with the details.

It might be just a dependency problem; it all seems to be there in my case.

> It's trivial if you know how the standard builtin handling code works, 
> less so otherwise :). I will look into it later. I think I want a -h
> option for not dereferencing symlinks.

You need a string containing "h" where the builtin is declared and to test
OPT_ISSET(ops,'h') in the function for the builtin.

> I didn't quite figure out how to best set an array parameter, so for now I 
> still only handle one file, and the zlistattr function sets the whole 
> string with nulls in the parameter, so you have to use ${(0)REPLY} to 
> split it. I looked a little at bin_read since i know read can set an array 
> parameter, but it is probably not very well suited as an easy to 
> understand example since it does so many other things too.

The use of setaparam() in stat.c is probably a good example, it's doing
something very similar, and there are some hash examples down there, too.
Remember all the bits come from permanently allocated memory.

> I think I got this right now, but I haven't tested the case where the 
> stuff isn't found.

It looks OK.  I'll see if I can find a Solaris machine left over to try it
on when I've committed the patch at the bottom.

> It looks like I have to re-metafy the string when using zwarnnam, I pass 
> in the parameter that says the buffer is large enough, since it did 
> contain the metafied version of the string some milliseconds earlier. That 
> should work, right?

It's better to pass in the right length in case of embedded NULs---there
shouldn't be any since the filesystem calls wouldn't handle them, but it's
more consistent.  That's an easy change.

Something strange happened with some of the whitespace in the patch so I
applied various bits by hand.

There are various minor tweaks below: expand the filename in completion in
case it's got a ~ in it; reformat the .distfiles (I've been gradually doing
this to all of them since one file per line is much easier to maintain);
handle lengths when metafying; don't report errors unless system calls
returned a negative value; remove an unused variable.

===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Zsh/Command/_zattr,v
retrieving revision 1.1
diff -u -r1.1 _zattr
--- Completion/Zsh/Command/_zattr	3 Mar 2009 15:04:27 -0000	1.1
+++ Completion/Zsh/Command/_zattr	3 Mar 2009 16:24:48 -0000
@@ -29,6 +29,6 @@
 esac
 
 if [[ $state = attrs ]]; then
-  zlistattr $line[1] REPLY
+  zlistattr $~line[1] REPLY
   _wanted attrs expl 'attribute' compadd ${(0)REPLY}
 fi
Index: Doc/Zsh/.distfiles
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/.distfiles,v
retrieving revision 1.19
diff -u -r1.19 .distfiles
--- Doc/Zsh/.distfiles	3 Mar 2009 15:04:28 -0000	1.19
+++ Doc/Zsh/.distfiles	3 Mar 2009 16:24:48 -0000
@@ -1,21 +1,72 @@
 DISTFILES_SRC='
-    .cvsignore .distfiles
-    arith.yo          builtins.yo       calsys.yo         compat.yo
-    compctl.yo        compsys.yo        compwid.yo        cond.yo
-    contrib.yo        exec.yo           expn.yo           filelist.yo
-    files.yo          func.yo           grammar.yo        index.yo
-    intro.yo          invoke.yo         jobs.yo           manmodmenu.yo
-    manual.yo         metafaq.yo        mod_attr.yo       mod_cap.yo
-    mod_clone.yo
-    mod_compctl.yo    mod_complete.yo   mod_complist.yo   mod_computil.yo
-    mod_curses.yo     mod_datetime.yo   mod_deltochar.yo  mod_example.yo
-    mod_files.yo      mod_langinfo.yo   modlist.yo        mod_mapfile.yo
-    mod_mathfunc.yo   modmenu.yo        mod_newuser.yo    mod_parameter.yo
-    mod_pcre.yo       mod_regex.yo      mod_sched.yo      mod_socket.yo
-    mod_stat.yo       mod_system.yo     mod_tcp.yo        mod_termcap.yo
-    mod_terminfo.yo   modules.yo        mod_zftp.yo       mod_zleparameter.yo
-    mod_zle.yo        mod_zprof.yo      mod_zpty.yo       mod_zselect.yo
-    mod_zutil.yo      options.yo        params.yo         prompt.yo
-    redirect.yo       restricted.yo     roadmap.yo        seealso.yo
-    tcpsys.yo         zftpsys.yo        zle.yo
+.cvsignore
+.distfiles
+arith.yo
+builtins.yo
+calsys.yo
+compat.yo
+compctl.yo
+compsys.yo
+compwid.yo
+cond.yo
+contrib.yo
+exec.yo
+expn.yo
+filelist.yo
+files.yo
+func.yo
+grammar.yo
+index.yo
+intro.yo
+invoke.yo
+jobs.yo
+manmodmenu.yo
+manual.yo
+metafaq.yo
+mod_attr.yo
+mod_cap.yo
+mod_clone.yo
+mod_compctl.yo
+mod_complete.yo
+mod_complist.yo
+mod_computil.yo
+mod_curses.yo
+mod_datetime.yo
+mod_deltochar.yo
+mod_example.yo
+mod_files.yo
+mod_langinfo.yo
+modlist.yo
+mod_mapfile.yo
+mod_mathfunc.yo
+modmenu.yo
+mod_newuser.yo
+mod_parameter.yo
+mod_pcre.yo
+mod_regex.yo
+mod_sched.yo
+mod_socket.yo
+mod_stat.yo
+mod_system.yo
+mod_tcp.yo
+mod_termcap.yo
+mod_terminfo.yo
+modules.yo
+mod_zftp.yo
+mod_zleparameter.yo
+mod_zle.yo
+mod_zprof.yo
+mod_zpty.yo
+mod_zselect.yo
+mod_zutil.yo
+options.yo
+params.yo
+prompt.yo
+redirect.yo
+restricted.yo
+roadmap.yo
+seealso.yo
+tcpsys.yo
+zftpsys.yo
+zle.yo
 '
Index: Src/Modules/attr.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/attr.c,v
retrieving revision 1.1
diff -u -r1.1 attr.c
--- Src/Modules/attr.c	3 Mar 2009 15:04:28 -0000	1.1
+++ Src/Modules/attr.c	3 Mar 2009 16:24:48 -0000
@@ -37,10 +37,10 @@
 bin_getattr(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func))
 {
     int ret = 0;
-    int len;
+    int len, slen;
     char value[256];
 
-    unmetafy(*argv, NULL);
+    unmetafy(*argv, &slen);
     unmetafy(*(argv+1), NULL);
     if (listxattr(*argv, NULL, 0) > 0) {
         if (0 < (len = getxattr(*argv, *(argv+1), value, 255))) {
@@ -51,8 +51,8 @@
                 else
                     printf("%s\n", value);
             }
-        } else {
-            zwarnnam(nam, "%s: %e", metafy(*argv, -1, META_NOALLOC), errno);
+        } else if (len < 0) {
+            zwarnnam(nam, "%s: %e", metafy(*argv, slen, META_NOALLOC), errno);
             ret = 1;
         }
     }
@@ -62,13 +62,13 @@
 static int
 bin_setattr(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func))
 {
-    int ret = 0;
- 
-    unmetafy(*argv, NULL);
+    int ret = 0, slen;
+
+    unmetafy(*argv, &slen);
     unmetafy(*(argv+1), NULL);
     unmetafy(*(argv+2), NULL);
     if (setxattr(*argv, *(argv+1), *(argv+2), strlen(*(argv+2)), 0)) {
-        zwarnnam(nam, "%s: %e", metafy(*argv, -1, META_NOALLOC), errno);
+        zwarnnam(nam, "%s: %e", metafy(*argv, slen, META_NOALLOC), errno);
         ret = 1;
     }
     return ret;
@@ -77,25 +77,25 @@
 static int
 bin_delattr(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func))
 {
-    int ret = 0;
- 
-    unmetafy(*argv, NULL);
+    int ret = 0, slen;
+
+    unmetafy(*argv, &slen);
     unmetafy(*(argv+1), NULL);
     if (removexattr(*argv, *(argv+1))) {
-        zwarnnam(nam, "%s: %e", metafy(*argv, -1, META_NOALLOC), errno);
+        zwarnnam(nam, "%s: %e", metafy(*argv, slen, META_NOALLOC), errno);
         ret = 1;
     }
     return ret;
 }
- 
+
 static int
 bin_listattr(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func))
 {
     int ret = 0;
-    int len, i = 1;
+    int len, slen;
     char value[256];
 
-    unmetafy(*argv, NULL);
+    unmetafy(*argv, &slen);
     if (0 < (len = listxattr(*argv, value, 256))) {
         if (len < 256) {
             char *p = value;
@@ -106,8 +106,8 @@
                 p += strlen(p) + 1;
             }
         }
-    } else {
-        zwarnnam(nam, "%s: %e", metafy(*argv, -1, META_NOALLOC), errno);
+    } else if (len < 0) {
+        zwarnnam(nam, "%s: %e", metafy(*argv, slen, META_NOALLOC), errno);
         ret = 1;
     }
     return ret;



-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070



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