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