cmdline: use SPDX tags
[dpdk.git] / lib / librte_cmdline / cmdline_socket.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
4  * All rights reserved.
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <inttypes.h>
13 #include <fcntl.h>
14 #include <termios.h>
15
16 #include "cmdline_parse.h"
17 #include "cmdline_rdline.h"
18 #include "cmdline_socket.h"
19 #include "cmdline.h"
20
21 struct cmdline *
22 cmdline_file_new(cmdline_parse_ctx_t *ctx, const char *prompt, const char *path)
23 {
24         int fd;
25
26         /* everything else is checked in cmdline_new() */
27         if (!path)
28                 return NULL;
29
30         fd = open(path, O_RDONLY, 0);
31         if (fd < 0) {
32                 dprintf("open() failed\n");
33                 return NULL;
34         }
35         return cmdline_new(ctx, prompt, fd, -1);
36 }
37
38 struct cmdline *
39 cmdline_stdin_new(cmdline_parse_ctx_t *ctx, const char *prompt)
40 {
41         struct cmdline *cl;
42         struct termios oldterm, term;
43
44         tcgetattr(0, &oldterm);
45         memcpy(&term, &oldterm, sizeof(term));
46         term.c_lflag &= ~(ICANON | ECHO | ISIG);
47         tcsetattr(0, TCSANOW, &term);
48         setbuf(stdin, NULL);
49
50         cl = cmdline_new(ctx, prompt, 0, 1);
51
52         if (cl)
53                 memcpy(&cl->oldterm, &oldterm, sizeof(term));
54
55         return cl;
56 }
57
58 void
59 cmdline_stdin_exit(struct cmdline *cl)
60 {
61         if (!cl)
62                 return;
63
64         tcsetattr(fileno(stdin), TCSANOW, &cl->oldterm);
65 }