]> git.ozlabs.org Git - yaboot.git/blob - second/cmdline.c
Avoid stack smash in parseing the vendor specific options.
[yaboot.git] / second / cmdline.c
1 /*
2  *  cmdline.c - Prompt handling
3  *
4  *  Copyright (C) 2001, 2002 Ethan Benson
5  *
6  *  Adapted from SILO
7  *
8  *  Copyright (C) 1996 Maurizio Plaza
9  *                1996 Jakub Jelinek
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #include "types.h"
27 #include "stdarg.h"
28 #include "prom.h"
29 #include "string.h"
30 #include "cfg.h"
31
32 #define CMD_LENG        512
33 char cbuff[CMD_LENG];
34 char passwdbuff[CMD_LENG];
35 extern int useconf;
36
37 void cmdinit()
38 {
39     cbuff[0] = 0;
40     passwdbuff[0] = 0;
41 }
42
43 void cmdedit (void (*tabfunc) (void), int password)
44 {
45      int x, c;
46      char *buff = password ? passwdbuff : cbuff;
47      for (x = 0; x < CMD_LENG - 1; x++) {
48           if (buff[x] == 0)
49                break;
50           else if (password)
51                prom_printf("*");
52      }
53      if (!password)
54           prom_printf(buff, x);
55
56      for (;;) {
57           c = prom_getchar ();
58           if (c == -1)
59                break;
60           if (c == '\n' || c == '\r') {
61                break;
62           }
63           if (c == '\t' && !x && tabfunc)
64                (*tabfunc) ();
65           if (c == '\b' || c == 0x7F) {
66                if (x > 0) {
67                     --x;
68                     buff[x] = 0;
69                     prom_printf("\b \b");
70                }
71           } else if ((c & 0xE0) != 0) {
72                if (x < CMD_LENG - 1) {
73                     buff[x] = c;
74                     buff[x + 1] = 0;
75                     if (password)
76                          prom_printf("*");
77                     else
78                          prom_printf(buff + x);
79                     x++;
80                }
81                if (x == 1 && !password && useconf) {
82                     if (cfg_get_flag (cbuff, "single-key"))
83                          break;
84                }
85           }
86      }
87      buff[x] = 0;
88 }