File: main.cc

package info (click to toggle)
sdcc 4.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 100,120 kB
  • sloc: ansic: 935,524; cpp: 75,055; makefile: 57,615; sh: 30,106; asm: 14,243; perl: 12,136; yacc: 7,297; lisp: 1,672; python: 815; lex: 781; awk: 498; sed: 89
file content (439 lines) | stat: -rw-r--r-- 7,847 bytes parent folder | download
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/******************************************************************************
 * 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"

Viewer *view;
FileIO *fobj;

struct auto_answer_t {
  char *pattern;
  int p_len;
  char *answer;
  int a_len;
};

enum
 {
   aa_len= 100
 };


struct auto_answer_t aa[aa_len];
int max_pattern_len= 0;
int aa_p_num= 0;
int aa_a_num= 0;
char *aa_queue;
int aa_q_len= 0;


void
init_aa()
{
  int i;
  for (i= 0; i < aa_len; i++)
    {
      aa[i].pattern= 0;
      aa[i].p_len= 0;
      aa[i].answer= 0;
      aa[i].a_len= 0;
    }
}

void
start_aa()
{
  if (max_pattern_len > 0)
    {
      aa_queue= (char*)malloc(max_pattern_len);
      aa_queue[0]= 0;
    }
  else
    aa_queue= NULL;
}

void
new_aa_pattern(char *s, int len)
{
  if (aa_p_num < aa_len)
    {
      aa[aa_p_num].pattern= s;
      aa[aa_p_num].p_len= len;
      aa_p_num++;
      if (len > max_pattern_len)
	max_pattern_len= len;
    }
}

void
new_aa_answer(char *s, int len)
{
  if (aa_a_num < aa_len)
    {
      aa[aa_a_num].answer= s;
      aa[aa_a_num].a_len= len;
      aa_a_num++;
    }
}

// 0: no match
// 1: partial match
// 2: full match
int
check_pattern(char *p, int pl, char *q, int ql)
{
  int m= pl, i;
  if (ql < m)
    m= ql;
  if (m == 0)
    return 0;
  for (i= 0; i<m; i++)
    {
      if (p[i] != q[i])
	break;
    }
  if (i != m)
    return 0;
  if (pl == ql)
    return 2;
  return 1;
}

struct auto_answer_t *
get_aa(int *partial)
{
  
  return 0;
}

void
shift_queue()
{
  int i;
  if (aa_queue)
    {
      for (i= 1; i<aa_q_len; i++)
	aa_queue[i-1]= aa_queue[i];
      aa_queue[--aa_q_len]= 0;
    }
}

void
aa_proc(char c)
{
  int i, p;
  // add to queue
  if (!aa_queue)
    return;
  if (aa_q_len >= max_pattern_len)
    shift_queue();
  aa_queue[aa_q_len++]= c;
  // search
  for (i=p= 0; i < aa_p_num; i++)
    {
      int m= check_pattern(aa[i].pattern, aa[i].p_len, aa_queue, aa_q_len);
      if (m == 2)
	{
	  // found a full match
	  int j;
	  for (j=0; j < aa[i].a_len; j++)
	    {
	      fobj->SendByte(aa[i].answer[j]);
	    }
	  shift_queue();
	  return;
	}
      if (m == 1)
	p++;
    }
  if (!p)
    shift_queue();
}

// globals
int doloop = 1;

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

int
hex2bin(char h)
{
  int cv= 0;
  if ((h >= '0') && (h <= '9'))
    cv= h-'0';
  else if ((h >= 'A') && (h <= 'F'))
    cv= 10 + h-'A';
  else if ((h >= 'a') && (h <= 'f'))
    cv= 10 + h-'a';
  return cv;
}

/* Convert C escapes into bin chars */

char *
unesc_cstr(char *s, int *len)
{
  char *d= (char*)malloc(strlen(s));
  int i, j;
  d[j= 0]= 0;
  for (i= 0; s[i]!=0; i++)
    {
      if (s[i]=='\\')
	{
	  if (s[++i]==0)
	    break;
	  switch (s[i])
	    {
	    case 'a': d[j++]= '\a'; d[j]= 0; break;
	    case 'b': d[j++]= '\b'; d[j]= 0; break;
	    case 'e': d[j++]= 27; d[j]= 0; break;
	    case 'f': d[j++]= '\f'; d[j]= 0; break;
	    case 'n': d[j++]= '\n'; d[j]= 0; break;
	    case 'r': d[j++]= '\r'; d[j]= 0; break;
	    case 't': d[j++]= '\t'; d[j]= 0; break;
	    case 'v': d[j++]= '\v'; d[j]= 0; break;
	    case '\\': d[j++]= '\\'; d[j]= 0; break;
	    case '\'': d[j++]= '\''; d[j]= 0; break;
	    case '\"': d[j++]= '\"'; d[j]= 0; break;
	    case '?': d[j++]= '?'; d[j]= 0; break;
	    case 'x':
	      {
		if (s[++i]==0)
		  break;
		int n= 0, l= strspn(&s[i], "0123456789abcdefABCDEF"), v= 0, cv;
		do
		  {
		    cv= hex2bin(s[i]);
		    v= (v*16) + cv;
		    if (s[++i]==0)
		      break;
		  }
		while (++n < l);
		d[j++]= v&0xff;
		d[j]= 0;
		i--;
		break;
	      }
	    case 'u':
	      {
		if (s[++i]==0)
		  break;
		int n= 0, l= 4, v= 0, cv;
		do
		  {
		    cv= hex2bin(s[i]);
		    v= (v*16) + cv;
		    if (s[++i]==0)
		      break;
		  }
		while (++n < l);
		d[j++]= v&0xff;
		d[j]= 0;
		i--;
		break;
	      }
	    case 'U': 
	      {
		if (s[++i]==0)
		  break;
		int n= 0, l= 8, v= 0, cv;
		do
		  {
		    cv= hex2bin(s[i]);
		    v= (v*16) + cv;
		    if (s[++i]==0)
		      break;
		  }
		while (++n < l);
		d[j++]= v&0xff;
		d[j]= 0;
		i--;
		break;
	      }
	    case '0': case '1': case '2': case '3':
	    case '4': case '5': case '6': case '7':
	      {
		int n= 0, l= strspn(&s[i], "01234567"), v= 0;
		if (l>3)
		  l= 3;
		do
		  {
		    v= (v*8) + (s[i]-'0');
		    if (s[++i]==0)
		      break;
		  }
		while (++n < l);
		d[j++]= v&0xff;
		d[j]= 0;
		i--;
		break;
	      }
	    default:
	      d[j++]= s[i];
	      d[j]= 0;
	      break;
	    }
	  if (s[i]==0)
	    break;
	}
      else
	{
	  d[j++]= s[i];
	  d[j]= 0;
	}
    }
  if (len)
    *len= j;
  return d;
}

// usage
void PrintUsage(char *progname)
{
  std::cout << "Usage: " << progname << " [-i <filename>] [-o <filename>] [-hIO]\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 << "-I \t\thexa filter on input\n";
  std::cout << "-O \t\thexa filter on output\n";
  std::cout << "-L n\t\tSet line length of hex dump in output panel (def=8)\n";
  std::cout << "-a string\tPattern for autoanswer\n";
  std::cout << "-A string\tAnswer for autoanswer\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, l= 8;
  enum filter_t fi= flt_none, fo= flt_none;
  const char *infile = DEF_INFILE;
  const char *outfile = DEF_OUTFILE;
  
  // sort out any command line params
  while ((c = getopt(argc, argv, "i:o:hOIL:a:A:")) != EOF)
    switch(c) {
    case 'i':
      infile = optarg;
      break;
    case 'o':
      outfile = optarg;
      break;
    case 'I':
      fi= flt_hex;
      break;
    case 'O':
      fo= flt_hex;
      break;
    case 'L':
      l= strtol(optarg, 0, 0);
      break;
    case 'h':
      errflg++;
      break;
    case 'a':
      {
	int l;
	char *s= unesc_cstr(optarg, &l);
	new_aa_pattern(s, l);
	break;
      }
    case 'A':
      {
	int l;
	char *s= unesc_cstr(optarg, &l);
	new_aa_answer(s, l);
	break;
      }
    default:
      std::cerr << "Invalid or unknown switch\n";
      errflg++;
      break;
    }
  
  // was there a problem
  if(errflg)
    PrintUsage(argv[0]);
  
  // the main objects needed
  view = new Viewer();
  fobj = new FileIO(infile, outfile);
  SigHandler *sig = new SigHandler();
  
  view->iflt_mode(fi);
  view->oflt_mode(fo);
  view->set_length(l);
  
  // add a signal handler for ^C
  sig->SetSignal(SIGINT, HandleSig);

  start_aa();
  // set the timeout for waiting for a char
  fd_set s;
  while(doloop)
    {
      int ret, i;
      FD_ZERO(&s);
      FD_SET(fileno(stdin), &s);
      FD_SET(fobj->infile_id(), &s);

      i= select(FD_SETSIZE, &s, NULL, NULL, NULL);
      if (i >= 0)
	{
	  if (FD_ISSET(fileno(stdin), &s))
	    {
	      ret= view->GetChInWin(&string[0]);
	      if (ret > 0)
		{
		  fobj->SendByte(string[0]);
		}
	      else if (ret < 0)
		break;
	    }

	  if (FD_ISSET(fobj->infile_id(), &s))
	    {
	      if (fobj->RecvByte(string) > 0)
		{
		  aa_proc(string[0]);
		  view->AddChOutWin(string[0]);
		}
	    }
	}
      //usleep(5000);
    }
  
  delete fobj;
  delete view;
  delete sig;
  delete string;
  return(0);
}