1 /* Copyright (C) 1987-2002 Free Software Foundation, Inc.
3 This file is part of the GNU Readline Library, a library for
4 reading lines of text with interactive input and history editing.
6 The GNU Readline Library is free software; you can redistribute it
7 and/or modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2, or
9 (at your option) any later version.
11 The GNU Readline Library is distributed in the hope that it will be
12 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 The GNU General Public License is often shipped with GNU software, and
17 is generally kept in a file called COPYING or LICENSE. If you do not
18 have a copy of the license, write to the Free Software Foundation,
19 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
21 /* fileman.c -- A tiny application which demonstrates how to use the
22 GNU Readline library. This application interactively allows users
23 to manipulate files and their modes. */
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_FILE_H
31 # include <sys/file.h>
43 #if defined (HAVE_STRING_H)
45 #else /* !HAVE_STRING_H */
47 #endif /* !HAVE_STRING_H */
53 #ifdef READLINE_LIBRARY
54 # include "readline.h"
57 # include <readline/readline.h>
58 # include <readline/history.h>
61 extern char *xmalloc ();
63 /* The names of functions that actually do the manipulation. */
64 int com_list
PARAMS((char *));
65 int com_view
PARAMS((char *));
66 int com_rename
PARAMS((char *));
67 int com_stat
PARAMS((char *));
68 int com_pwd
PARAMS((char *));
69 int com_delete
PARAMS((char *));
70 int com_help
PARAMS((char *));
71 int com_cd
PARAMS((char *));
72 int com_quit
PARAMS((char *));
74 /* A structure which contains information on the commands this program
78 char *name
; /* User printable name of the function. */
79 rl_icpfunc_t
*func
; /* Function to call to do the job. */
80 char *doc
; /* Documentation for this function. */
83 COMMAND commands
[] = {
84 { "cd", com_cd
, "Change to directory DIR" },
85 { "delete", com_delete
, "Delete FILE" },
86 { "help", com_help
, "Display this text" },
87 { "?", com_help
, "Synonym for `help'" },
88 { "list", com_list
, "List files in DIR" },
89 { "ls", com_list
, "Synonym for `list'" },
90 { "pwd", com_pwd
, "Print the current working directory" },
91 { "quit", com_quit
, "Quit using Fileman" },
92 { "rename", com_rename
, "Rename FILE to NEWNAME" },
93 { "stat", com_stat
, "Print out statistics on FILE" },
94 { "view", com_view
, "View the contents of FILE" },
95 { (char *)NULL
, (rl_icpfunc_t
*)NULL
, (char *)NULL
}
98 /* Forward declarations. */
100 COMMAND
*find_command ();
102 /* The name of this program, as taken from argv[0]. */
105 /* When non-zero, this global means the user is done using this program. */
114 r
= xmalloc (strlen (s
) + 1);
127 initialize_readline (); /* Bind our completer. */
129 /* Loop reading and executing lines until the user quits. */
132 line
= readline ("FileMan: ");
137 /* Remove leading and trailing whitespace from the line.
138 Then, if there is anything left, add it to the history list
140 s
= stripwhite (line
);
153 /* Execute a command line. */
162 /* Isolate the command word. */
164 while (line
[i
] && whitespace (line
[i
]))
168 while (line
[i
] && !whitespace (line
[i
]))
174 command
= find_command (word
);
178 fprintf (stderr
, "%s: No such command for FileMan.\n", word
);
182 /* Get argument to command, if any. */
183 while (whitespace (line
[i
]))
188 /* Call the function. */
189 return ((*(command
->func
)) (word
));
192 /* Look up NAME as the name of a command, and return a pointer to that
193 command. Return a NULL pointer if NAME isn't a command name. */
200 for (i
= 0; commands
[i
].name
; i
++)
201 if (strcmp (name
, commands
[i
].name
) == 0)
202 return (&commands
[i
]);
204 return ((COMMAND
*)NULL
);
207 /* Strip whitespace from the start and end of STRING. Return a pointer
213 register char *s
, *t
;
215 for (s
= string
; whitespace (*s
); s
++)
221 t
= s
+ strlen (s
) - 1;
222 while (t
> s
&& whitespace (*t
))
229 /* **************************************************************** */
231 /* Interface to Readline Completion */
233 /* **************************************************************** */
235 char *command_generator
PARAMS((const char *, int));
236 char **fileman_completion
PARAMS((const char *, int, int));
238 /* Tell the GNU Readline library how to complete. We want to try to complete
239 on command names if this is the first word in the line, or on filenames
241 initialize_readline ()
243 /* Allow conditional parsing of the ~/.inputrc file. */
244 rl_readline_name
= "FileMan";
246 /* Tell the completer that we want a crack first. */
247 rl_attempted_completion_function
= fileman_completion
;
250 /* Attempt to complete on the contents of TEXT. START and END bound the
251 region of rl_line_buffer that contains the word to complete. TEXT is
252 the word to complete. We can use the entire contents of rl_line_buffer
253 in case we want to do some simple parsing. Return the array of matches,
254 or NULL if there aren't any. */
256 fileman_completion (text
, start
, end
)
262 matches
= (char **)NULL
;
264 /* If this word is at the start of the line, then it is a command
265 to complete. Otherwise it is the name of a file in the current
268 matches
= rl_completion_matches (text
, command_generator
);
273 /* Generator function for command completion. STATE lets us know whether
274 to start from scratch; without any state (i.e. STATE == 0), then we
275 start at the top of the list. */
277 command_generator (text
, state
)
281 static int list_index
, len
;
284 /* If this is a new word to complete, initialize now. This includes
285 saving the length of TEXT for efficiency, and initializing the index
293 /* Return the next name which partially matches from the command list. */
294 while (name
= commands
[list_index
].name
)
298 if (strncmp (name
, text
, len
) == 0)
299 return (dupstr(name
));
302 /* If no names matched, then return NULL. */
303 return ((char *)NULL
);
306 /* **************************************************************** */
308 /* FileMan Commands */
310 /* **************************************************************** */
312 /* String to pass to system (). This is for the LIST, VIEW and RENAME
314 static char syscom
[1024];
316 /* List the file(s) named in arg. */
323 sprintf (syscom
, "ls -FClg %s", arg
);
324 return (system (syscom
));
330 if (!valid_argument ("view", arg
))
333 #if defined (__MSDOS__)
334 /* more.com doesn't grok slashes in pathnames */
335 sprintf (syscom
, "less %s", arg
);
337 sprintf (syscom
, "more %s", arg
);
339 return (system (syscom
));
345 too_dangerous ("rename");
354 if (!valid_argument ("stat", arg
))
357 if (stat (arg
, &finfo
) == -1)
363 printf ("Statistics for `%s':\n", arg
);
365 printf ("%s has %d link%s, and is %d byte%s in length.\n",
368 (finfo
.st_nlink
== 1) ? "" : "s",
370 (finfo
.st_size
== 1) ? "" : "s");
371 printf ("Inode Last Change at: %s", ctime (&finfo
.st_ctime
));
372 printf (" Last access at: %s", ctime (&finfo
.st_atime
));
373 printf (" Last modified at: %s", ctime (&finfo
.st_mtime
));
380 too_dangerous ("delete");
384 /* Print out help for ARG, or for all of the commands if ARG is
392 for (i
= 0; commands
[i
].name
; i
++)
394 if (!*arg
|| (strcmp (arg
, commands
[i
].name
) == 0))
396 printf ("%s\t\t%s.\n", commands
[i
].name
, commands
[i
].doc
);
403 printf ("No commands match `%s'. Possibilties are:\n", arg
);
405 for (i
= 0; commands
[i
].name
; i
++)
407 /* Print in six columns. */
414 printf ("%s\t", commands
[i
].name
);
424 /* Change to the directory ARG. */
428 if (chdir (arg
) == -1)
438 /* Print out the current working directory. */
444 s
= getcwd (dir
, sizeof(dir
) - 1);
447 printf ("Error getting pwd: %s\n", dir
);
451 printf ("Current directory is %s\n", dir
);
455 /* The user wishes to quit using this program. Just set DONE non-zero. */
463 /* Function which tells you that you can't do this. */
464 too_dangerous (caller
)
468 "%s: Too dangerous for me to distribute. Write it yourself.\n",
472 /* Return non-zero if ARG is a valid argument for CALLER, else print
473 an error message and return zero. */
475 valid_argument (caller
, arg
)
480 fprintf (stderr
, "%s: Argument required.\n", caller
);