File: main.cc

package info (click to toggle)
sdcc 3.5.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 59,964 kB
  • ctags: 66,696
  • sloc: ansic: 422,129; cpp: 49,032; makefile: 46,849; sh: 28,496; perl: 12,125; asm: 11,748; yacc: 6,698; lisp: 1,630; lex: 747; python: 660; awk: 495; sed: 89
file content (109 lines) | stat: -rw-r--r-- 2,250 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/******************************************************************************
 * to emulate the serial input and output of an 8051 controller               *
 * main.cc - the main stuff                                                   *
 ******************************************************************************/
#include "ddconfig.h"

#include <sys/types.h>
#include <iostream>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>

#if defined(HAVE_GETOPT_H)
# include <getopt.h>
#endif

#include "fileio.hh"
#include "frontend.hh"
#include "posix_signal.hh"


// globals
int doloop = 1;

// the signal handler
void HandleSig(int info)
{
	doloop = 0;
}


// usage
void PrintUsage(char *progname)
{
std::cout << "Usage: " << progname << " [-i <filename>] [-o <filename>] [-h]\n";
std::cout << "-i <filename>\t<filename> is the pipe to the controllers' serial input\n";
std::cout << "-o <filename>\t<filename> is the pipe to the controllers' serial output\n";
std::cout << "-h\t\tshow the help\n";
std::cout << "\nTim Hurman - t.hurman@virgin.net\n";
exit(0);
}


// the main function
int main(int argc, char **argv)
{
	char *string = new char[MAX_SIZ];
	extern char *optarg;
	int errflg=0;
	int c;
	char *infile = DEF_INFILE;
	char *outfile = DEF_OUTFILE;

	// sort out any command line params
	while ((c = getopt(argc, argv, "i:o:h")) != EOF)
	switch(c) {
	case 'i':
		infile = optarg;
		break;
	case 'o':
		outfile = optarg;
		break;
	case 'h':
		errflg++;
		break;
	default:
		std::cerr << "Invalid or unknown switch\n";
		errflg++;
		break;
	}

	// was there a problem
	if(errflg)
		PrintUsage(argv[0]);

	// the main objects needed
	FileIO *fobj = new FileIO(infile, outfile);
	Viewer *view = new Viewer();
	SigHandler *sig = new SigHandler();

	// add a signal handler for ^C
	sig->SetSignal(SIGINT, HandleSig);

	// set the timeout for waiting for a char
	while(doloop)
	{
		string[0] = view->GetChInWin();
		if(string[0] == 4)
			break;

		if(string[0] != 0)
			fobj->SendByte(string[0]);
		
		if(fobj->RecvStr(string) > 0)
			view->AddStrOutWin(string);

		usleep(5000);
	}

	delete fobj;
	delete view;
	delete sig;
	delete string;
	return(0);
}