Hi,

`cut -a ARRAY ...` puts its last line of output in ARRAY[0] and discards any
other elements ARRAY used to have. I tried 3 alternatives:

Alternative A: put last line of output in ARRAY[0] but preserve any other
elements.

Alternative B: empty out the array then put lines of output in successive array
elements.

Alternative C: apply patch A and then patch B so lines are put in successive
elements but if there are less lines than there were elements originally then
preserve those that weren't overwritten.

I have no feeling for which is the most useful, but in any case help should say
what -a does.

Patches are against version 5.3.0(1)-rc2[dbe4256d].
patch-B is not signal-safe, it was just a quick test. arrayind_t ind needs to be
external and zeroed out in the main reading loop.

Cheers ... Duncan.
diff --git a/builtins/common.c b/builtins/common.c
index a681cee4..4628c44f 100644
--- a/builtins/common.c
+++ b/builtins/common.c
@@ -1008,7 +1008,7 @@ builtin_find_indexed_array (char *array_name, int flags)
   else if (invisible_p (entry))
     VUNSETATTR (entry, att_invisible); /* no longer invisible */
 
-  if (flags & 1)
+  if ((flags & 5) == 1)
     array_flush (array_cell (entry));
 
   return entry;
diff --git a/examples/loadables/cut.c b/examples/loadables/cut.c
index b3b8a477..dd0c2c4b 100644
--- a/examples/loadables/cut.c
+++ b/examples/loadables/cut.c
@@ -518,7 +518,7 @@ cut_internal (int which, WORD_LIST *list)
 #if defined (ARRAY_VARS)
   if (array_name)
     {
-      v = builtin_find_indexed_array (array_name, 1);
+      v = builtin_find_indexed_array (array_name, 5);
       if (v == 0)
        {
          free (poslist);
@@ -561,7 +561,7 @@ lcut_builtin (WORD_LIST *list)
 int
 cut_builtin (WORD_LIST *list)
 {
-  return (cut_internal (1, list));
+  return (cut_internal (5, list));
 }
 
 char *lcut_doc[] = {
diff --git a/examples/loadables/cut.c b/examples/loadables/cut.c
index b3b8a477..a7f6024c 100644
--- a/examples/loadables/cut.c
+++ b/examples/loadables/cut.c
@@ -60,6 +60,8 @@ struct cutop
   struct cutpos *poslist;
 };
 
+static arrayind_t ind;
+
 static int
 poscmp (const void *a, const void *b)
 {
@@ -145,7 +147,6 @@ getlist (char *arg, struct cutpos **opp)
 static int
 cutbytes (SHELL_VAR *v, char *line, struct cutop *ops)
 {
-  arrayind_t ind;
   char *buf, *bmap;
   size_t llen;
   int i, b, n, s, e;
@@ -177,7 +178,6 @@ cutbytes (SHELL_VAR *v, char *line, struct cutop *ops)
 
   if (v)
     {
-      ind = 0;
 #if defined (ARRAY_VARS)
       bind_array_element (v, ind, buf, 0);
 #endif
@@ -195,7 +195,6 @@ cutbytes (SHELL_VAR *v, char *line, struct cutop *ops)
 static int
 cutchars (SHELL_VAR *v, char *line, struct cutop *ops)
 {
-  arrayind_t ind;
   char *buf, *bmap;
   wchar_t *wbuf, *wb2;
   size_t llen, wlen;
@@ -247,7 +246,6 @@ cutchars (SHELL_VAR *v, char *line, struct cutop *ops)
 
   if (v)
     {
-      ind = 0;
 #if defined (ARRAY_VARS)
       bind_array_element (v, ind, buf, 0);
 #endif
@@ -269,12 +267,10 @@ cutchars (SHELL_VAR *v, char *line, struct cutop *ops)
 static int
 cutfields (SHELL_VAR *v, char *line, struct cutop *ops)
 {
-  arrayind_t ind;
   char *buf, *bmap, *field, **fields, delim[2];
   size_t llen, fsize;
   int i, b, n, s, e, nf;
 
-  ind = 0;
 
   delim[0] = ops->delim;
   delim[1] = '\0';
@@ -436,6 +432,7 @@ cut_internal (int which, WORD_LIST *list)
   struct cutpos *poslist;
 
   v = 0;
+  ind = 0;
   rval = EXECUTION_SUCCESS;
 
   cutflags = 0;

Reply via email to