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