newLISP Fan Club

Forum => newLISP in the real world => Topic started by: kosh on May 30, 2010, 08:17:33 AM

Title: [patch] "exec" to enable a binary string in pipe
Post by: kosh on May 30, 2010, 08:17:33 AM
syntax: (exec str-process)
syntax: (exec str-process [str-stdin])


Current exec function second syntax (p_exec:nl-filesys.c) is unable use a binary string.

There is caused by strlen function which treat as a NULL terminator character.



newLISP's cell string has own length. I think this problem can be avoided, if use it.



For instance, patch is here:



--- nl-filesys.orig.c. Mon Apr 19 07:29:16 2010
+++ nl-filesys.c Sun May 30 23:07:09 2010
@@ -958,10 +957,11 @@
 
 CELL * p_exec(CELL * params)
 {
-CELL * lineList;
+  CELL * cell;
 char * line;
-char * command, * data;
+  char * command;
 FILE * handle;
+  size_t nwrite;
 
 params = getString(params, &command);
 if(params == nilCell)
@@ -969,21 +969,26 @@
  if((handle = popen(command , "r")) == NULL)
  return(nilCell);
 
- lineList = getCell(CELL_EXPRESSION);
+      cell = getCell(CELL_EXPRESSION);
  while((line = readStreamLine(&readLineStream, handle)) != NULL)
- addList(lineList, stuffString(line));
+        addList(cell, stuffString(line));
 
  pclose(handle);
- return(lineList);
+      return(cell);
  }
     
-getString(params, &data);
+  /* getString(params, &data); */
+  cell = evaluateExpression(params);
+  if (cell->type != CELL_STRING)
+    return(errorProcExt(ERR_STRING_EXPECTED, params));
 
 if((handle = popen(command, "w")) == NULL)
  return(nilCell);
 
-if(fwrite(data, sizeof(char), strlen(data), handle) < strlen(data))
- return(nilCell);
+  /* if(fwrite(data, sizeof(char), strlen(data), handle) < strlen(data)) */
+  nwrite = fwrite((char *)cell->contents, sizeof(char), cell->aux -1, handle);
+  if (nwrite < (cell->aux -1))
+    return nilCell;
 
 pclose(handle);
 return(trueCell);


Now, exec function enable binary string.


[~/src/newlisp-10.2.4]$ ./newlisp -C
newLISP v.10.2.4 on Win32 IPv4, execute 'newlisp -h' for more info.

> (exec "sha1sum -" (get-url "http://www.newlisp.org/downloads/UTF-8_win32/newlisp.exe"))
db2c8f9161993bd965dd8efd75d3aa6f22f71c6f *-
true
> !curl http://www.newlisp.org/downloads/UTF-8_win32/SHA1.txt
SHA1(newlisp.dll)= 145bb1a411dd12c9e43ea052014038fd383f95ed
SHA1(newlisp.exe)= db2c8f9161993bd965dd8efd75d3aa6f22f71c6f
Title: Re: [patch] "exec" to enable a binary string in pipe
Post by: TedWalther on May 30, 2010, 02:42:14 PM
I like your idea and hope Lutz includes it.



However, I don't think this will fix your problem.  Exec passes that string to the shell, which in turns parses the command line.  But all shells I know of are written in C and take C style strings, so at the first byte, it will stop processing anyway.



I support your fix, because it is conceivable that one day someone will compile newlisp to use some other shell than the standard one for exec, possibly even one that has proper strings, not just C style strings.  Then it would work.
Title: Re: [patch] "exec" to enable a binary string in pipe
Post by: TedWalther on May 30, 2010, 02:45:06 PM
Actually, I can see how this would be helpful with "here" documents.  So it would be a useful patch right now.