5293a1745c6d74766557aa691fc51900bc3dc442
[fw/sdcc] / sim / ucsim / gui.src / serio.src / main.cc
1 /******************************************************************************
2  * to emulate the serial input and output of an 8051 controller               *
3  * main.cc - the main stuff                                                   *
4  ******************************************************************************/
5 #include <sys/types.h>
6 #include <iostream>
7 #include <stdlib.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <signal.h>
14
15 #if defined(HAVE_GETOPT_H)
16 # include <getopt.h>
17 #endif
18
19 #include "fileio.hh"
20 #include "frontend.hh"
21 #include "posix_signal.hh"
22
23
24 // globals
25 int doloop = 1;
26
27 // the signal handler
28 void HandleSig(int info)
29 {
30         doloop = 0;
31 }
32
33
34 // usage
35 void PrintUsage(char *progname)
36 {
37 std::cout << "Usage: " << progname << " [-i <filename>] [-o <filename>] [-h]\n";
38 std::cout << "-i <filename>\t<filename> is the pipe to the controllers' serial input\n";
39 std::cout << "-o <filename>\t<filename> is the pipe to the controllers' serial output\n";
40 std::cout << "-h\t\tshow the help\n";
41 std::cout << "\nTim Hurman - t.hurman@virgin.net\n";
42 exit(0);
43 }
44
45
46 // the main function
47 int main(int argc, char **argv)
48 {
49         char *string = new char[MAX_SIZ];
50         extern char *optarg;
51         int errflg=0;
52         int c;
53         char *infile = DEF_INFILE;
54         char *outfile = DEF_OUTFILE;
55
56         // sort out any command line params
57         while ((c = getopt(argc, argv, "i:o:h")) != EOF)
58         switch(c) {
59         case 'i':
60                 infile = optarg;
61                 break;
62         case 'o':
63                 outfile = optarg;
64                 break;
65         case 'h':
66                 errflg++;
67                 break;
68         default:
69                 std::cerr << "Invalid or unknown switch\n";
70                 errflg++;
71                 break;
72         }
73
74         // was there a problem
75         if(errflg)
76                 PrintUsage(argv[0]);
77
78         // the main objects needed
79         FileIO *fobj = new FileIO(infile, outfile);
80         Viewer *view = new Viewer();
81         SigHandler *sig = new SigHandler();
82
83         // add a signal handler for ^C
84         sig->SetSignal(SIGINT, HandleSig);
85
86         // set the timeout for waiting for a char
87         while(doloop)
88         {
89                 string[0] = view->GetChInWin();
90                 if(string[0] == 4)
91                         break;
92
93                 if(string[0] != 0)
94                         fobj->SendByte(string[0]);
95
96                 if(fobj->RecvStr(string) > 0)
97                         view->AddStrOutWin(string);
98
99                 usleep(5000);
100         }
101
102         delete fobj;
103         delete view;
104         delete sig;
105         delete string;
106         return(0);
107 }