199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation.
399a2dd95SBruce Richardson * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
499a2dd95SBruce Richardson * All rights reserved.
599a2dd95SBruce Richardson */
699a2dd95SBruce Richardson
799a2dd95SBruce Richardson #include <stdio.h>
899a2dd95SBruce Richardson #include <unistd.h>
999a2dd95SBruce Richardson #include <fcntl.h>
1099a2dd95SBruce Richardson
1199a2dd95SBruce Richardson #include "cmdline.h"
1299a2dd95SBruce Richardson #include "cmdline_private.h"
1399a2dd95SBruce Richardson #include "cmdline_socket.h"
1499a2dd95SBruce Richardson
1599a2dd95SBruce Richardson struct cmdline *
cmdline_file_new(cmdline_parse_ctx_t * ctx,const char * prompt,const char * path)1699a2dd95SBruce Richardson cmdline_file_new(cmdline_parse_ctx_t *ctx, const char *prompt, const char *path)
1799a2dd95SBruce Richardson {
1899a2dd95SBruce Richardson int fd;
1999a2dd95SBruce Richardson
2099a2dd95SBruce Richardson /* everything else is checked in cmdline_new() */
2199a2dd95SBruce Richardson if (!path)
2299a2dd95SBruce Richardson return NULL;
2399a2dd95SBruce Richardson
2499a2dd95SBruce Richardson fd = open(path, O_RDONLY, 0);
2599a2dd95SBruce Richardson if (fd < 0) {
2699a2dd95SBruce Richardson dprintf("open() failed\n");
2799a2dd95SBruce Richardson return NULL;
2899a2dd95SBruce Richardson }
2999a2dd95SBruce Richardson return cmdline_new(ctx, prompt, fd, -1);
3099a2dd95SBruce Richardson }
3199a2dd95SBruce Richardson
3299a2dd95SBruce Richardson struct cmdline *
cmdline_stdin_new(cmdline_parse_ctx_t * ctx,const char * prompt)3399a2dd95SBruce Richardson cmdline_stdin_new(cmdline_parse_ctx_t *ctx, const char *prompt)
3499a2dd95SBruce Richardson {
3599a2dd95SBruce Richardson struct cmdline *cl;
3699a2dd95SBruce Richardson
3799a2dd95SBruce Richardson cl = cmdline_new(ctx, prompt, 0, 1);
3899a2dd95SBruce Richardson
3999a2dd95SBruce Richardson if (cl != NULL)
4099a2dd95SBruce Richardson terminal_adjust(cl);
4199a2dd95SBruce Richardson
4299a2dd95SBruce Richardson return cl;
4399a2dd95SBruce Richardson }
4499a2dd95SBruce Richardson
4599a2dd95SBruce Richardson void
cmdline_stdin_exit(struct cmdline * cl)4699a2dd95SBruce Richardson cmdline_stdin_exit(struct cmdline *cl)
4799a2dd95SBruce Richardson {
4899a2dd95SBruce Richardson if (cl == NULL)
4999a2dd95SBruce Richardson return;
5099a2dd95SBruce Richardson
5199a2dd95SBruce Richardson terminal_restore(cl);
52*6ad06203SZhihong Peng cmdline_free(cl);
5399a2dd95SBruce Richardson }
54