OSDN Git Service

Make run-test.sh POSIXly-portable
[yash/yash.git] / input.h
1 /* Yash: yet another shell */
2 /* input.h: functions for input of command line */
3 /* (C) 2007-2018 magicant */
4
5 /* This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18
19 #ifndef YASH_INPUT_H
20 #define YASH_INPUT_H
21
22 #include <stdlib.h>
23 #include <wchar.h>
24
25
26 struct promptset_T {
27     wchar_t *main, *right, *styler, *predict;
28 };
29
30 #define PROMPT_RESET L"\\fD"
31
32 extern struct promptset_T get_prompt(int type);
33 static inline void free_prompt(struct promptset_T prompt);
34 extern void print_prompt(const wchar_t *s)
35     __attribute__((nonnull));
36 extern _Bool unset_nonblocking(int fd);
37
38
39 /* Frees the specified prompt set. */
40 void free_prompt(struct promptset_T prompt)
41 {
42     free(prompt.main);
43     free(prompt.right);
44     free(prompt.styler);
45     free(prompt.predict);
46 }
47
48
49 /* The result type of `inputfunc_T'. */
50 typedef enum inputresult_T {
51     INPUT_OK,           /* A line was read successfully. */
52     INPUT_EOF,          /* The end of file was reached. */
53     INPUT_INTERRUPTED,  /* SIGINT was received (interactive shell only) */
54     INPUT_ERROR,        /* Other error was encountered. */
55 } inputresult_T;
56
57 struct xwcsbuf_T;
58 struct input_file_info_T;
59 extern inputresult_T read_input(
60         struct xwcsbuf_T *buf, struct input_file_info_T *info, _Bool trap)
61     __attribute__((nonnull));
62
63 /* The type of input functions.
64  * An input function reads input and appends it to buffer `buf'.
65  * Input is done line-wise: the buffer contents are always terminated by a
66  * newline character (L'\n') except when the end of file is reached and the last
67  * line does not have a newline.
68  * An input function should not read more than one line at a time, as commands
69  * (which may contain alias definitions) should be executed as soon as possible,
70  * before the next line is parsed.
71  * The result is indicated by a value of the `inputresult_T' type. If the return
72  * value is other than INPUT_OK, the buffer is unchanged.
73  * The input function may be called even after it returned a value other than
74  * INPUT_OK. */
75 typedef inputresult_T inputfunc_T(struct xwcsbuf_T *buf, void *inputinfo);
76
77 /* input functions */
78 extern inputresult_T input_wcs(struct xwcsbuf_T *buf, void *inputinfo)
79     __attribute__((nonnull));
80 extern inputresult_T input_file(struct xwcsbuf_T *buf, void *inputinfo)
81     __attribute__((nonnull));
82 extern inputresult_T input_interactive(struct xwcsbuf_T *buf, void *inputinfo)
83     __attribute__((nonnull));
84
85 /* to be used as `inputinfo' for `input_wcs' */
86 struct input_wcs_info_T {
87     const wchar_t *src;  /* the input source code */
88 };
89
90 /* to be used as `inputinfo' for `input_file' */
91 struct input_file_info_T {
92     int fd;
93     mbstate_t state;
94     size_t bufpos, bufmax, bufsize;
95     char buf[];
96 };
97 /* `bufsize' is the size of `buf', which must be at least one byte. */
98
99 /* to be used as `inputinfo' for `input_interactive' */
100 struct input_interactive_info_T {
101     struct input_file_info_T *fileinfo;
102     int prompttype;
103 };
104
105
106 #endif /* YASH_INPUT_H */
107
108
109 /* vim: set ts=8 sts=4 sw=4 noet tw=80: */