Skip to content

Commit 4bf26d9

Browse files
committed
cu: implement the cu exit logic like top cmd
using the local cu_globals_s instance to manange the cu exit procedure Signed-off-by: guoshichao <[email protected]>
1 parent d76ab4f commit 4bf26d9

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

system/cu/cu_main.c

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ enum parity_mode
7979
* Private Data
8080
****************************************************************************/
8181

82-
static struct cu_globals_s g_cu;
83-
8482
/****************************************************************************
8583
* Public Data
8684
****************************************************************************/
@@ -124,9 +122,10 @@ static FAR void *cu_listener(FAR void *parameter)
124122
return NULL;
125123
}
126124

127-
static void sigint(int sig)
125+
static void cu_exit(int signo, FAR siginfo_t *siginfo, FAR void *context)
128126
{
129-
g_cu.force_exit = true;
127+
FAR struct cu_globals_s *cu = siginfo->si_user;
128+
cu->force_exit = true;
130129
}
131130

132131
#ifdef CONFIG_SERIAL_TERMIOS
@@ -276,8 +275,8 @@ int main(int argc, FAR char *argv[])
276275
{
277276
pthread_attr_t attr;
278277
struct sigaction sa;
278+
struct cu_globals_s cu;
279279
FAR const char *devname = CONFIG_SYSTEM_CUTERM_DEFAULT_DEVICE;
280-
FAR struct cu_globals_s *cu = &g_cu;
281280
#ifdef CONFIG_SERIAL_TERMIOS
282281
int baudrate = CONFIG_SYSTEM_CUTERM_DEFAULT_BAUD;
283282
enum parity_mode parity = PARITY_NONE;
@@ -292,14 +291,21 @@ int main(int argc, FAR char *argv[])
292291

293292
/* Initialize global data */
294293

295-
memset(cu, 0, sizeof(*cu));
296-
cu->escape = '~';
294+
memset(&cu, 0, sizeof(struct cu_globals_s));
295+
cu.escape = '~';
297296

298297
/* Install signal handlers */
299298

300299
memset(&sa, 0, sizeof(sa));
301-
sa.sa_handler = sigint;
302-
sigaction(SIGINT, &sa, NULL);
300+
sa.sa_user = &cu;
301+
sa.sa_sigaction = cu_exit;
302+
sigemptyset(&sa.sa_mask);
303+
if (sigaction(SIGINT, &sa, NULL) < 0)
304+
{
305+
cu_error("cu_main: ERROR during setup cu_exit sigaction(): %d\n",
306+
errno);
307+
return EXIT_FAILURE;
308+
}
303309

304310
optind = 0; /* Global that needs to be reset in FLAT mode */
305311
while ((option = getopt(argc, argv, "l:s:ceE:fho?")) != ERROR)
@@ -333,7 +339,7 @@ int main(int argc, FAR char *argv[])
333339
break;
334340

335341
case 'E':
336-
cu->escape = atoi(optarg);
342+
cu.escape = atoi(optarg);
337343
break;
338344

339345
case 'h':
@@ -356,8 +362,8 @@ int main(int argc, FAR char *argv[])
356362

357363
/* Open the serial device for reading and writing */
358364

359-
cu->devfd = open(devname, O_RDWR);
360-
if (cu->devfd < 0)
365+
cu.devfd = open(devname, O_RDWR);
366+
if (cu.devfd < 0)
361367
{
362368
cu_error("cu_main: ERROR: Failed to open %s for writing: %d\n",
363369
devname, errno);
@@ -366,9 +372,9 @@ int main(int argc, FAR char *argv[])
366372

367373
/* Remember serial device termios attributes */
368374

369-
if (isatty(cu->devfd))
375+
if (isatty(cu.devfd))
370376
{
371-
ret = tcgetattr(cu->devfd, &cu->devtio);
377+
ret = tcgetattr(cu.devfd, &cu.devtio);
372378
if (ret)
373379
{
374380
cu_error("cu_main: ERROR during tcgetattr(): %d\n", errno);
@@ -382,30 +388,30 @@ int main(int argc, FAR char *argv[])
382388

383389
if (isatty(STDERR_FILENO))
384390
{
385-
cu->stdfd = STDERR_FILENO;
391+
cu.stdfd = STDERR_FILENO;
386392
}
387393
else if (isatty(STDOUT_FILENO))
388394
{
389-
cu->stdfd = STDOUT_FILENO;
395+
cu.stdfd = STDOUT_FILENO;
390396
}
391397
else if (isatty(STDIN_FILENO))
392398
{
393-
cu->stdfd = STDIN_FILENO;
399+
cu.stdfd = STDIN_FILENO;
394400
}
395401
else
396402
{
397-
cu->stdfd = -1;
403+
cu.stdfd = -1;
398404
}
399405

400-
if (cu->stdfd >= 0)
406+
if (cu.stdfd >= 0)
401407
{
402-
tcgetattr(cu->stdfd, &cu->stdtio);
408+
tcgetattr(cu.stdfd, &cu.stdtio);
403409
}
404410

405411
#ifdef CONFIG_SERIAL_TERMIOS
406-
if (set_termios(cu, baudrate, parity, rtscts, nocrlf) != 0)
412+
if (set_termios(&cu, baudrate, parity, rtscts, nocrlf) != 0)
407413
#else
408-
if (set_termios(cu, nocrlf) != 0)
414+
if (set_termios(&cu, nocrlf) != 0)
409415
#endif
410416
{
411417
goto errout_with_devfd_retrieve;
@@ -424,7 +430,7 @@ int main(int argc, FAR char *argv[])
424430

425431
attr.priority = CONFIG_SYSTEM_CUTERM_PRIORITY;
426432

427-
ret = pthread_create(&cu->listener, &attr, cu_listener, cu);
433+
ret = pthread_create(&cu.listener, &attr, cu_listener, &cu);
428434
pthread_attr_destroy(&attr);
429435
if (ret != 0)
430436
{
@@ -434,7 +440,7 @@ int main(int argc, FAR char *argv[])
434440

435441
/* Send messages and get responses -- forever */
436442

437-
while (!cu->force_exit)
443+
while (!cu.force_exit)
438444
{
439445
char ch;
440446

@@ -443,7 +449,7 @@ int main(int argc, FAR char *argv[])
443449
continue;
444450
}
445451

446-
if (start_of_line == 1 && ch == cu->escape)
452+
if (start_of_line == 1 && ch == cu.escape)
447453
{
448454
/* We've seen and escape (~) character, echo it to local
449455
* terminal and read the next char from serial
@@ -456,16 +462,16 @@ int main(int argc, FAR char *argv[])
456462
continue;
457463
}
458464

459-
if (ch == cu->escape)
465+
if (ch == cu.escape)
460466
{
461467
/* Escaping a tilde: handle like normal char */
462468

463-
write(cu->devfd, &ch, 1);
469+
write(cu.devfd, &ch, 1);
464470
continue;
465471
}
466472
else
467473
{
468-
if (cu_cmd(cu, ch) == 1)
474+
if (cu_cmd(&cu, ch) == 1)
469475
{
470476
break;
471477
}
@@ -474,7 +480,7 @@ int main(int argc, FAR char *argv[])
474480

475481
/* Normal character */
476482

477-
write(cu->devfd, &ch, 1);
483+
write(cu.devfd, &ch, 1);
478484

479485
/* Determine if we are now at the start of a new line or not */
480486

@@ -488,15 +494,15 @@ int main(int argc, FAR char *argv[])
488494
}
489495
}
490496

491-
pthread_cancel(cu->listener);
497+
pthread_cancel(cu.listener);
492498
exitval = EXIT_SUCCESS;
493499

494500
/* Error exits */
495501

496502
errout_with_devfd_retrieve:
497-
retrieve_termios(cu);
503+
retrieve_termios(&cu);
498504
errout_with_devfd:
499-
close(cu->devfd);
505+
close(cu.devfd);
500506
errout_with_devinit:
501507
return exitval;
502508
}

0 commit comments

Comments
 (0)