1 /* $NetBSD: prompt.c,v 1.3 2007/02/22 05:31:54 thorpej Exp $ */
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <lib/libsa/stand.h>
33 #include <lib/libkern/libkern.h>
35 #include <machine/param.h>
36 #include <machine/bfs.h>
42 #define LOG_MASK (LOG_SIZE - 1)
43 uint8_t __log
[LOG_SIZE
];
46 #define CMDBUF_HISTORY_MAX 8
50 char buf
[CMDBUF_SIZE
];
52 } __cmd_buf
[CMDBUF_HISTORY_MAX
];
54 struct cmd_buf
*__cmd
;
57 void prompt_reset(void);
58 void prompt_input(int);
59 extern void __putchar(int);
67 while (/*CONSTCOND*/1)
68 if ((c
= getchar()) != 0)
80 __cmd
->buf
[__cmd
->cur
] = c
;
81 __cmd
->cur
= (__cmd
->cur
>= CMDBUF_SIZE
- 1) ? __cmd
->cur
:
83 if (__cmd
->cur
>= __cmd
->max
)
84 __cmd
->max
= __cmd
->cur
;
98 __cmd
->buf
[--__cmd
->cur
] = 0;
103 __cmd
->cur
= __cmd
->min
;
105 printf("%s", PROMPT
);
108 __cmd
->cur
= __cmd
->max
;
111 if (__cmd
->cur
== __cmd
->min
)
117 if (__cmd
->cur
== __cmd
->max
)
123 if (__cmd
->cur
== __cmd
->max
)
125 for (i
= __cmd
->cur
; i
< __cmd
->max
; i
++)
126 __cmd
->buf
[i
] = __cmd
->buf
[i
+ 1];
127 __cmd
->buf
[i
] = '\0';
130 case 11: /* Ctrl k */
131 for (i
= __cmd
->cur
; i
< __cmd
->max
; i
++) {
133 __cmd
->max
= __cmd
->cur
;
137 case 14: /* Ctrl n */
138 __cur_cmd
= (__cur_cmd
+ 1) & 0x7;
140 case 16: /* Ctrl p */
141 __cur_cmd
= (__cur_cmd
- 1) & 0x7;
143 __cmd
= &__cmd_buf
[__cur_cmd
];
144 __cmd
->cur
= __cmd
->max
;
145 case 12: /* Ctrl l */
149 printf("%s%s", PROMPT
, __cmd
->buf
);
158 __cur_cmd
= (__cur_cmd
+ 1) & 0x7;
159 __cmd
= &__cmd_buf
[__cur_cmd
];
160 memset(__cmd
, 0, sizeof *__cmd
);
165 prompt_yesno(int interactive
)
171 /* block until user input */
172 while (/*CONSTCOND*/1) {
173 if ((i
= getchar()) == 0)
175 if (i
== 'N' || i
== 'n')
177 if (i
== 'Y' || i
== 'y')
188 __log
[__log_cnt
++] = c
;
189 __log_cnt
&= LOG_MASK
;
193 cmd_log_save(int argc
, char *argp
[], int interactive
)
197 if (bfs_init(&bfs
) != 0) {
198 printf("no BFS partition.\n");
202 if (bfs_file_write(bfs
, "boot.log", __log
, LOG_SIZE
) != 0)
203 printf("BFS write failed.\n");