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