Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Change codebase to haskell :P

  • Loading branch information...
commit 65e4b1458a719ff3ee07f90a06b1dfa7a2e0dae7 1 parent eb1e19b
@steveWang authored
Showing with 44 additions and 145 deletions.
  1. +1 −1  Makefile
  2. +0 −144 src/bifurcate.C
  3. +43 −0 src/bifurcate.hs
View
2  Makefile
@@ -1,4 +1,4 @@
all:
- g++ src/bifurcate.C -o bifurcate
+ ghc src/bifurcate.hs -o ./bifurcate -threaded
clean:
rm -f bifurcate
View
144 src/bifurcate.C
@@ -1,144 +0,0 @@
-/** bifurcate.C, by Steve Wang
- * Copyright 2012
- * Basically, take an input stream and pipe it to multiple outputs.
- **/
-
-/* Read from stdin, then write to multiple streams. */
-#include <iostream>
-#include <cstring>
-#include <list>
-#include <cstdio>
-
-#define HELP_STRING \
-"Usage: bifurcate type targets...\n\
-Types:\n\
- -a Open the specified file(s) for appending.\n\
- -w Open the specified file(s) for writing\n\
- (WARNING: will overwrite file contents).\n\
- -p Pipe to the specified process id(s). Note: only supported\n\
- on flavors of Unix that support the procfs.\n\
- -c Run the specified commands.\n\
- -h Print this help message.\n\
-\n\
-targets corresponds to possible inputs.\n\
-"
-
-using namespace std;
-
-enum OutputType
-{
- NONE = 0,
- WRITE, // write to file; >
- APPEND, // append to file; >>
- PID, // pipe to existing PID; |
- COMMAND, // spawn process via command and provide as stdin.
- HELP
-};
-
-typedef unsigned int uint;
-
-
-void print_help()
-{
- puts(HELP_STRING);
-}
-
-
-OutputType specifies_output(char *str)
-{
- uint len = strlen(str);
- if (!(len == 2 && str[0] == '-'))
- {
- return NONE;
- }
-
- switch (str[1])
- {
- case 'p':
- return PID;
- case 'w':
- return WRITE;
- case 'a':
- return APPEND;
- case 'h':
- return HELP;
- default:
- return NONE;
- }
-}
-
-
-ostream *open_file(char *str, OutputType t)
-{
- return NULL;
-}
-
-
-ostream *parse_pid(char *str)
-{
- return NULL;
-}
-
-
-ostream *spawn(char *str)
-{
- return NULL;
-}
-
-
-int add_stream(list<ostream*> *streams, char *str, OutputType t)
-{
- switch (t)
- {
- case WRITE:
- case APPEND:
- streams->push_front(open_file(str, t));
- break;
- case PID:
- streams->push_front(parse_pid(str));
- break;
- case COMMAND:
- streams->push_front(spawn(str));
- break;
- }
- return 0;
-}
-
-
-void bifurcate(istream *input, list<ostream*> *streams)
-{
- list<ostream*>::iterator
- iter = streams->begin(),
- end = streams->end();
-
- char c;
- uint available = input->rdbuf()->in_avail();
- while (!input->eof())
- {
- input->get(c);
-
- for (list<ostream*>::iterator i = iter; i != end; i++)
- {
- (*i)->put(c);
- }
- }
-}
-
-
-int main(int c, char **v)
-{
- istream *input = &cin;
- list<ostream*> *streams = new list<ostream*>();
- OutputType t = PID;
- while (c--)
- {
- (t = specifies_output(*v)) || add_stream(streams, *v, t);
- if (t == HELP)
- {
- print_help();
- return 0;
- }
- ++v;
- }
- bifurcate(input, streams);
-}
View
43 src/bifurcate.hs
@@ -0,0 +1,43 @@
+import System.Environment
+import System.Process
+import System.IO
+
+-- TODO : wait until all files are closed.
+
+process :: String -> String -> IO Handle
+process "a" fname = do
+ x <- openFile fname AppendMode
+ return x
+process "w" fname = do
+ x <- openFile fname WriteMode
+ return x
+process "c" cmd = do
+ x <- createProcess (shell cmd) {std_in = CreatePipe}
+ let (Just ihandle, _, _, _) = x in
+ return ihandle
+
+collect :: String -> [String] -> [IO Handle]
+collect t [] = []
+collect t (('-' : hd) : tl) = parseArgs (hd : tl)
+collect t (hd : tl) = ((process t hd) : (collect t tl))
+
+parseArgs :: [String] -> [IO Handle]
+parseArgs [] = []
+parseArgs (('-': hd) : tl) = collect hd tl
+
+multicast :: [IO Handle] -> String -> IO ()
+multicast [] c = do
+ return ()
+multicast [x] c = do
+ hdl <- x
+ hPutStr hdl c
+multicast (x : xs) c = do
+ hdl <- x
+ tmp <- hPutStr hdl c
+ multicast xs c
+
+main :: IO ()
+main = do
+ args <- getArgs
+ c <- getContents
+ multicast (parseArgs args) c
Please sign in to comment.
Something went wrong with that request. Please try again.