2012-02-01 20:07:57 +01:00
|
|
|
|
/****************************************************************************
|
2012-10-04 02:11:05 +02:00
|
|
|
|
* apps/system/readline/readline.c
|
2012-02-01 20:07:57 +01:00
|
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-02-01 20:07:57 +01:00
|
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-02-01 20:07:57 +01:00
|
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
|
* under the License.
|
2012-02-01 20:07:57 +01:00
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Included Files
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2023-10-13 12:42:20 +02:00
|
|
|
|
#include <stdlib.h>
|
2012-03-30 18:08:28 +02:00
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
|
#include "system/readline.h"
|
2012-02-01 20:07:57 +01:00
|
|
|
|
|
2023-10-13 12:42:20 +02:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Pre-processor Definitions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/* Maximum size of one command line (telnet or serial) */
|
|
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_LINELEN
|
|
|
|
|
# define CONFIG_NSH_LINELEN 80
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-02-01 20:07:57 +01:00
|
|
|
|
/****************************************************************************
|
2015-10-03 00:20:33 +02:00
|
|
|
|
* Public Functions
|
2012-02-01 20:07:57 +01:00
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: readline
|
|
|
|
|
*
|
2023-10-13 12:42:20 +02:00
|
|
|
|
* readline will read a line from the terminal and return it, using
|
|
|
|
|
* prompt as a prompt. If prompt is NULL or the empty string, no prompt
|
|
|
|
|
* is issued. The line returned is allocated with malloc(3);
|
|
|
|
|
* the caller must free it when finished. The line re‐turned has the
|
|
|
|
|
* final newline removed, so only the text of the line remains.
|
2012-02-01 20:07:57 +01:00
|
|
|
|
*
|
2015-10-03 01:33:30 +02:00
|
|
|
|
****************************************************************************/
|
2012-02-01 20:07:57 +01:00
|
|
|
|
|
2023-10-13 12:42:20 +02:00
|
|
|
|
FAR char *readline(FAR const char *prompt)
|
2012-02-01 20:07:57 +01:00
|
|
|
|
{
|
2023-10-13 12:42:20 +02:00
|
|
|
|
FAR char *line = malloc(CONFIG_NSH_LINELEN);
|
2023-09-22 13:37:51 +02:00
|
|
|
|
|
2023-10-13 12:42:20 +02:00
|
|
|
|
if (line != NULL)
|
|
|
|
|
{
|
|
|
|
|
#ifdef CONFIG_READLINE_TABCOMPLETION
|
|
|
|
|
FAR const char *orig = readline_prompt(prompt);
|
|
|
|
|
#endif
|
|
|
|
|
if (readline_fd(line, CONFIG_NSH_LINELEN,
|
|
|
|
|
STDIN_FILENO, STDOUT_FILENO) == 0)
|
|
|
|
|
{
|
|
|
|
|
free(line);
|
|
|
|
|
line = NULL;
|
|
|
|
|
}
|
2012-02-01 20:07:57 +01:00
|
|
|
|
|
2023-10-13 12:42:20 +02:00
|
|
|
|
#ifdef CONFIG_READLINE_TABCOMPLETION
|
|
|
|
|
readline_prompt(orig);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2023-09-22 13:37:51 +02:00
|
|
|
|
|
2023-10-13 12:42:20 +02:00
|
|
|
|
return line;
|
2012-02-01 20:07:57 +01:00
|
|
|
|
}
|