Thursday | 21 NOV 2024
[ previous ]
[ next ]

Redirecting Output in NSH

Title:
Date: 2024-04-10
Tags:  

I added a redirect operator to NSH so I can send the output of a command to a file.

> CT MD TIME > /tmp/test.txt

Not super happy with this code but it works.

            LAST.POSITION = DCOUNT(SERIES,@AM)
            LAST.COMMAND = SERIES<LAST.POSITION>
*
            IF COUNT(LAST.COMMAND,'>') = 1 THEN
               EXPORT.AVAILABLE = TRUE
               EXPORT.PATH = TRIM(FIELD(LAST.COMMAND,'>',2))
*
               SERIES<LAST.POSITION> = TRIM(FIELD(LAST.COMMAND,'>',1))
               EXECUTE SERIES CAPTURING EXPORT.RESULTS
*
               CONVERT @AM TO CHAR(10) IN EXPORT.RESULTS
*
               EXPORT.RESULTS = CHANGE(EXPORT.RESULTS,HOME.POSITION,'')
               EXPORT.RESULTS = CHANGE(EXPORT.RESULTS,ERASE.SCREEN,'')
*
               OPENSEQ EXPORT.PATH TO EXPORT.FILE ELSE
                  CREATE EXPORT.FILE ELSE
                     PRINT 'Failed to create file: ' : EXPORT.PATH
                     EXPORT.AVAILABLE = FALSE
                  END
               END
*
               IF EXPORT.AVAILABLE THEN
                  WEOFSEQ EXPORT.FILE
*
                  WRITEBLK EXPORT.RESULTS ON EXPORT.FILE ELSE
                     PRINT 'Failed to write file: ' : EXPORT.FILE
                  END
*
                  FLUSH EXPORT.FILE ELSE NULL
                  CLOSESEQ EXPORT.FILE
               END
            END
*