okeyla |
09-21-2005 09:39 PM |
adding UI to a console program (with source)
I have a shell script ,
and would like to add a UI on it. (I use FLTK)
Some buttons on UI will command to the console program.
The response of the console program will be displayed on UI.
Many recommand pipe is suitable.
Since it is uni-directional , so i have to establish 2 pipes.
UI -> pipe2 -> console program
<- pipe1 <-
User just face to UI and don't care about the console.
The following is my codes.
Now i do coding of the reading part , but cannot make it.
Could someone give me a hand?
Code:
[menu.sh]
#!/bin/bash
let "loop=0"
while test $loop == 0
do
echo "Please choose the letter of the game you want to run and enter"
echo "------------------------------------------------------"
echo "a. Checking files"
echo "b. PWD"
echo "c. Adding string to file"
echo "------------------------------------------------------"
echo "q. quit"
echo "choice?"
read choice
case $choice in q)
let "loop=1"
esac
case $choice in a)
clear
echo "You are checking the files of this directory!"
ls -al
esac
case $choice in b)
clear
echo "You are checking the current directory"
pwd
esac
case $choice in c)
clear
echo "You are adding string to a file"
echo "I am number1" >> text.txt
esac
done
[fltk_popen_demo.cxx]
// demo use of popen() and Fl::add_fd() - erco 10/04/04
#include <stdio.h>
#include <stdlib.h>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Multi_Browser.H>
#include <unistd.h>
FILE *G_fp = NULL;
void HandleFD(int fd, void *data) {
Fl_Multi_Browser *brow = (Fl_Multi_Browser*)data;
char s[1024];
if ( fgets(s, 1023, G_fp) == NULL ) {
Fl::remove_fd(fileno(G_fp));
pclose(G_fp);
return;
}
brow->add(s);
}
int main() {
system("sh runme1.sh");
Fl_Window win(600,600);
Fl_Multi_Browser brow(10,10,580,580);
if ( ( G_fp = popen("cat < pipe1", "r" ) ) == NULL ) {
perror("popen failed");
return(1);
}
Fl::add_fd(fileno(G_fp), HandleFD, (void*)&brow);
win.resizable(brow);
win.show();
return(Fl::run());
}
[runme1.sh]
#!/bin/sh
if [ ! -e pipe1 ];then
mknod pipe1 p
fi
if [ ! -e pipe2 ];then
mknod pipe2 p
fi
sh ./menu.sh >pipe1 <pipe2
|