Author: allanjude (doc committer)
Date: Tue Jul 21 05:03:59 2015
New Revision: 285734
URL: https://svnweb.freebsd.org/changeset/base/285734

Log:
  Fix some issues with the application of libxo to ls(1)
  
  * Add whitespace trimming to some fields (username, group, size, inode, 
blocks) to avoid whitespace in JSON strings
  * fix -m mode, was invalid JSON (repeated keys), and was missing outer array 
container
  * in -n mode, numeric uids and gids were returned as strings
  
  Approved by:  eadler (mentor)
  Sponsored by: ScaleEngine Inc.
  Differential Revision:        https://reviews.freebsd.org/D2854

Modified:
  head/bin/ls/ls.c
  head/bin/ls/ls.h
  head/bin/ls/print.c

Modified: head/bin/ls/ls.c
==============================================================================
--- head/bin/ls/ls.c    Tue Jul 21 03:18:53 2015        (r285733)
+++ head/bin/ls/ls.c    Tue Jul 21 05:03:59 2015        (r285734)
@@ -119,7 +119,7 @@ static int f_nofollow;              /* don't follow 
        int f_nonprint;         /* show unprintables as ? */
 static int f_nosort;           /* don't sort output */
        int f_notabs;           /* don't use tab-separated multi-col output */
-static int f_numericonly;      /* don't convert uid/gid to name */
+       int f_numericonly;      /* don't convert uid/gid to name */
        int f_octal;            /* show unprintables as \xxx */
        int f_octal_escape;     /* like f_octal but use C escapes if possible */
 static int f_recursive;                /* ls subdirectories also */
@@ -195,6 +195,7 @@ main(int argc, char *argv[])
        if (argc < 0)
                return (1);
        xo_set_flags(NULL, XOF_COLUMNS);
+       xo_set_version(LS_XO_VERSION);
 
        while ((ch = getopt(argc, argv,
            "1ABCD:FGHILPRSTUWXZabcdfghiklmnopqrstuwxy,")) != -1) {

Modified: head/bin/ls/ls.h
==============================================================================
--- head/bin/ls/ls.h    Tue Jul 21 03:18:53 2015        (r285733)
+++ head/bin/ls/ls.h    Tue Jul 21 05:03:59 2015        (r285734)
@@ -37,6 +37,8 @@
 
 #define HUMANVALSTR_LEN        5
 
+#define LS_XO_VERSION  "1"
+
 extern long blocksize;         /* block size units */
 
 extern int f_accesstime;       /* use time of last access */
@@ -58,6 +60,7 @@ extern int f_statustime;      /* use time of 
 extern int f_thousands;                /* show file sizes with thousands 
separators */
 extern char *f_timeformat;     /* user-specified time format */
 extern int f_notabs;           /* don't use tab-separated multi-col output */
+extern int f_numericonly;      /* don't convert uid/gid to name */
 extern int f_type;             /* add type character for non-regular files */
 #ifdef COLORLS
 extern int f_color;            /* add type in color for non-regular files */

Modified: head/bin/ls/print.c
==============================================================================
--- head/bin/ls/print.c Tue Jul 21 03:18:53 2015        (r285733)
+++ head/bin/ls/print.c Tue Jul 21 05:03:59 2015        (r285734)
@@ -171,7 +171,7 @@ printlong(const DISPLAY *dp)
 
        xo_open_list("entry");
        for (p = dp->list; p; p = p->fts_link) {
-               char *name;
+               char *name, *type;
                if (IS_NOPRINT(p))
                        continue;
                xo_open_instance("entry");
@@ -180,22 +180,46 @@ printlong(const DISPLAY *dp)
                if (name)
                    xo_emit("{ke:name/%hs}", name);
                if (f_inode)
-                       xo_emit("{:inode/%*ju} ",
+                       xo_emit("{t:inode/%*ju} ",
                            dp->s_inode, (uintmax_t)sp->st_ino);
                if (f_size)
-                       xo_emit("{:blocks/%*jd} ",
+                       xo_emit("{t:blocks/%*jd} ",
                            dp->s_block, howmany(sp->st_blocks, blocksize));
                strmode(sp->st_mode, buf);
                aclmode(buf, p);
                np = p->fts_pointer;
                xo_attr("value", "%03o", (int) sp->st_mode & ALLPERMS);
-               xo_emit("{t:mode/%s} {:links/%*u} {:user/%-*s}  {:group/%-*s}  
",
-                       buf, dp->s_nlink, sp->st_nlink,
-                       dp->s_user, np->user, dp->s_group, np->group);
+               if (f_numericonly) {
+                       xo_emit("{t:mode/%s}{e:mode_octal/%03o} {t:links/%*u} 
{td:user/%-*s}{e:user/%ju}  {td:group/%-*s}{e:group/%ju}  ",
+                               buf, (int) sp->st_mode & ALLPERMS, dp->s_nlink, 
sp->st_nlink,
+                               dp->s_user, np->user, sp->st_uid, dp->s_group, 
np->group, sp->st_gid);
+               } else {
+                       xo_emit("{t:mode/%s}{e:mode_octal/%03o} {t:links/%*u} 
{t:user/%-*s}  {t:group/%-*s}  ",
+                               buf, (int) sp->st_mode & ALLPERMS, dp->s_nlink, 
sp->st_nlink,
+                               dp->s_user, np->user, dp->s_group, np->group);
+               }
+               if (S_ISBLK(sp->st_mode))
+                       asprintf(&type, "block");
+               if (S_ISCHR(sp->st_mode))
+                       asprintf(&type, "character");
+               if (S_ISDIR(sp->st_mode))
+                       asprintf(&type, "directory");
+               if (S_ISFIFO(sp->st_mode))
+                       asprintf(&type, "fifo");
+               if (S_ISLNK(sp->st_mode))
+                       asprintf(&type, "symlink");
+               if (S_ISREG(sp->st_mode))
+                       asprintf(&type, "regular");
+               if (S_ISSOCK(sp->st_mode))
+                       asprintf(&type, "socket");
+               if (S_ISWHT(sp->st_mode))
+                       asprintf(&type, "whiteout");
+               xo_emit("{e:type/%s}", type);
+               free(type);
                if (f_flags)
                        xo_emit("{:flags/%-*s} ", dp->s_flags, np->flags);
                if (f_label)
-                       xo_emit("{:label/%-*s} ", dp->s_label, np->label);
+                       xo_emit("{t:label/%-*s} ", dp->s_label, np->label);
                if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
                        printdev(dp->s_size, sp->st_rdev);
                else
@@ -238,6 +262,7 @@ printstream(const DISPLAY *dp)
        FTSENT *p;
        int chcnt;
 
+       xo_open_list("entry");
        for (p = dp->list, chcnt = 0; p; p = p->fts_link) {
                if (p->fts_number == NO_PRINT)
                        continue;
@@ -247,12 +272,15 @@ printstream(const DISPLAY *dp)
                        xo_emit("\n");
                        chcnt = 0;
                }
+               xo_open_instance("file");
                chcnt += printaname(p, dp->s_inode, dp->s_block);
+               xo_close_instance("file");
                if (p->fts_link) {
                        xo_emit(", ");
                        chcnt += 2;
                }
        }
+       xo_close_list("entry");
        if (chcnt)
                xo_emit("\n");
 }
@@ -369,10 +397,10 @@ printaname(const FTSENT *p, u_long inode
        sp = p->fts_statp;
        chcnt = 0;
        if (f_inode)
-               chcnt += xo_emit("{:inode/%*ju} ",
+               chcnt += xo_emit("{t:inode/%*ju} ",
                    (int)inodefield, (uintmax_t)sp->st_ino);
        if (f_size)
-               chcnt += xo_emit("{:size/%*jd} ",
+               chcnt += xo_emit("{t:size/%*jd} ",
                    (int)sizefield, howmany(sp->st_blocks, blocksize));
 #ifdef COLORLS
        if (f_color)
@@ -425,9 +453,11 @@ printtime(const char *field, time_t ftim
                format = d_first ? "%e %b  %Y" : "%b %e  %Y";
        strftime(longstring, sizeof(longstring), format, localtime(&ftime));
 
-       snprintf(fmt, sizeof(fmt), "{:%s/%%hs} ", field);
+       snprintf(fmt, sizeof(fmt), "{d:%s/%%hs} ", field);
        xo_attr("value", "%ld", (long) ftime);
        xo_emit(fmt, longstring);
+       snprintf(fmt, sizeof(fmt), "{en:%s/%%ld} ", field);
+       xo_emit(fmt, (long) ftime);
 }
 
 static int
_______________________________________________
[email protected] mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "[email protected]"

Reply via email to