]>
Commit | Line | Data |
---|---|---|
e246ba5f NS |
1 | /* |
2 | * Copyright (c) 2003 Silicon Graphics, Inc. All Rights Reserved. | |
dfc130f3 | 3 | * |
e246ba5f NS |
4 | * This program is free software; you can redistribute it and/or modify it |
5 | * under the terms of version 2 of the GNU General Public License as | |
6 | * published by the Free Software Foundation. | |
dfc130f3 | 7 | * |
e246ba5f NS |
8 | * This program is distributed in the hope that it would be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
dfc130f3 | 11 | * |
e246ba5f NS |
12 | * Further, this software is distributed without any warranty that it is |
13 | * free of the rightful claim of any third person regarding infringement | |
14 | * or the like. Any license provided herein, whether implied or | |
15 | * otherwise, applies only to this software file. Patent licenses, if | |
16 | * any, provided herein do not apply to combinations of this program with | |
17 | * other software, or any other product whatsoever. | |
dfc130f3 | 18 | * |
e246ba5f NS |
19 | * You should have received a copy of the GNU General Public License along |
20 | * with this program; if not, write the Free Software Foundation, Inc., 59 | |
21 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. | |
dfc130f3 | 22 | * |
e246ba5f NS |
23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
24 | * Mountain View, CA 94043, or: | |
dfc130f3 RC |
25 | * |
26 | * http://www.sgi.com | |
27 | * | |
28 | * For further information regarding this notice, see: | |
29 | * | |
e246ba5f NS |
30 | * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ |
31 | */ | |
dfc130f3 | 32 | |
e246ba5f NS |
33 | #include <libxfs.h> |
34 | #include "input.h" | |
35 | #include "init.h" | |
36 | ||
37 | #ifdef ENABLE_READLINE | |
38 | # include <readline/history.h> | |
39 | # include <readline/readline.h> | |
40 | #else | |
41 | # define MAXREADLINESZ 1024 | |
42 | static char * | |
43 | readline(char *prompt) | |
44 | { | |
45 | char *p, *line = malloc(MAXREADLINESZ); | |
46 | ||
47 | if (!line) | |
48 | return NULL; | |
49 | printf(prompt); | |
50 | fflush(stdout); | |
51 | if (!fgets(line, MAXREADLINESZ, stdin)) { | |
52 | free(line); | |
53 | return NULL; | |
54 | } | |
55 | p = line + strlen(line); | |
56 | if (p != line && p[-1] == '\n') | |
57 | p[-1] = '\0'; | |
58 | return line; | |
59 | } | |
60 | static void add_history(char *line) { } | |
61 | # undef MAXREADLINESZ | |
62 | #endif | |
63 | ||
64 | char * | |
65 | fetchline(void) | |
66 | { | |
67 | static char prompt[FILENAME_MAX + 1]; | |
68 | char *line; | |
69 | ||
70 | if (!prompt[0]) | |
71 | snprintf(prompt, sizeof(prompt), "%s> ", progname); | |
72 | line = readline(prompt); | |
73 | if (line && *line) | |
74 | add_history(line); | |
75 | return line; | |
76 | } | |
77 | ||
78 | char ** | |
79 | breakline( | |
80 | char *input, | |
81 | int *count) | |
82 | { | |
83 | int c = 0; | |
84 | char *p; | |
85 | char **rval = calloc(sizeof(char *), 1); | |
86 | ||
87 | while ((p = strsep(&input, " ")) != NULL) { | |
88 | if (!*p) | |
89 | continue; | |
90 | c++; | |
91 | rval = realloc(rval, sizeof(*rval) * (c + 1)); | |
92 | rval[c - 1] = p; | |
93 | rval[c] = NULL; | |
94 | } | |
95 | *count = c; | |
96 | return rval; | |
97 | } | |
98 | ||
99 | void | |
100 | doneline( | |
101 | char *input, | |
102 | char **vec) | |
103 | { | |
104 | free(input); | |
105 | free(vec); | |
106 | } |