1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <[email protected]>
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
15 #include "cmdline.h"
16 #include "cmdline_private.h"
17 #include "cmdline_socket.h"
18
19 struct cmdline *
cmdline_file_new(cmdline_parse_ctx_t * ctx,const char * prompt,const char * path)20 cmdline_file_new(cmdline_parse_ctx_t *ctx, const char *prompt, const char *path)
21 {
22 int fd;
23
24 /* everything else is checked in cmdline_new() */
25 if (!path)
26 return NULL;
27
28 fd = open(path, O_RDONLY, 0);
29 if (fd < 0) {
30 dprintf("open() failed\n");
31 return NULL;
32 }
33 return cmdline_new(ctx, prompt, fd, -1);
34 }
35
36 struct cmdline *
cmdline_stdin_new(cmdline_parse_ctx_t * ctx,const char * prompt)37 cmdline_stdin_new(cmdline_parse_ctx_t *ctx, const char *prompt)
38 {
39 struct cmdline *cl;
40
41 cl = cmdline_new(ctx, prompt, 0, 1);
42
43 if (cl != NULL)
44 terminal_adjust(cl);
45
46 return cl;
47 }
48
49 void
cmdline_stdin_exit(struct cmdline * cl)50 cmdline_stdin_exit(struct cmdline *cl)
51 {
52 if (cl == NULL)
53 return;
54
55 terminal_restore(cl);
56 cmdline_free(cl);
57 }
58