Re: Reading pipe from *standard-input*?

Jean Louis <[email protected]> Mon, 27 Mar 2017 16:47:45 +0300
Newsgroups gmane.lisp.clisp.general
Message-ID <[email protected]>
Thank you Sam.

Now I have changed it so that it is somewhat more elegant. Even those
setfs are bothering me.

(defparameter memdir "/run/shm/")
(defparameter tmp-file "rcd-wrs-XXXXXX")
(defparameter tmp-org (format nil "~a~a.org" memdir tmp-file))
(defparameter tmp-md (format nil "~a~a.md" memdir tmp-file))
(load "/home/data1/protected/Programming/git/RCDBusiness/lib/lisp/streamtools.lisp")
;; (require "syscalls")

(defun main ()
  (let ((input '())
        (output '()))
    (with-open-file (out tmp-org :direction :output :external-format "utf-8")
      (loop for line = (read-line *standard-input* nil nil)
         while line do
           (write-line line out)))
    (shell (format nil "emacs -Q -l ~~/.emacs.d/elpa/org-20170210/org-autoloads.el \"~a\" --batch -f org-mode -f org-md-export-to-markdown --kill" tmp-org))
    (setf output
            (with-open-file (stream tmp-md :direction :input :external-format "utf-8")
              (loop for line = (read-line stream nil nil)
                 while line collect line)))
    (setf output (format nil "~{~a~^~%~}" output))
    (setf output (slurp-stream-io-command "/usr/bin/discount_markdown -F 0x4" output))
    (princ output))
  (exit))

;; ;; (saveinitmem "org2html" :quiet t :init-function 'main :verbose nil :norc t :documentation "Converts Org standard input into markdown" :executable t)

(main)


On Mon, Mar 27, 2017 at 08:33:54AM -0400, Sam Steingold wrote:
> > * Jean Louis <[email protected]> [2017-03-24 00:00:23 +0300]:
> >
> > Also I am sure there is more elegant reading from *standard-input* and
> > writing to *standard-output* then this kind of list making and
> > converting to string. If somebody knows, let me know.
> 
> this is a common idiom:
> 
>   (let ((input (loop for line = (read-line in nil nil)
>                   while line collect line)))
>     ...)
> 
> >     (setf input (format nil "~{~a~^~%~}" (reverse input)))
> >     (with-open-file (stream tmp-org :direction :output :if-exists :supersede :if-does-not-exist :create :external-format "utf-8")
> >       (princ input stream))
> 
> this is a very poor idiom.
> not only your last line is not NL-terminated, but you are creating the
> full input string in memory.
> 
> this is the more common approach:
> 
> (with-open-file (out tmp-org :direction :output external-format "utf-8")
>   (dolist (line input)
>     (write-line line out)))
> 
> -- 
> Sam Steingold (http://sds.podval.org/) on darwin Ns 10.3.1504
> http://steingoldpsychology.com http://www.childpsy.net http://mideasttruth.com
> https://ffii.org http://thereligionofpeace.com http://iris.org.il
> A number problem "solved" with floats turns into 1.9999999999999998
> problems.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
clisp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/clisp-list