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