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