1 /* fileman.c -- A tiny application which demonstrates how to use the
2 GNU Readline library. This application interactively allows users
3 to manipulate files and their modes. */
11 #include <readline/readline.h>
12 #include <readline/history.h>
14 extern char *getwd ();
15 extern char *xmalloc ();
17 /* The names of functions that actually do the manipulation. */
18 int com_list (), com_view (), com_rename (), com_stat (), com_pwd ();
19 int com_delete (), com_help (), com_cd (), com_quit ();
21 /* A structure which contains information on the commands this program
25 char *name
; /* User printable name of the function. */
26 Function
*func
; /* Function to call to do the job. */
27 char *doc
; /* Documentation for this function. */
30 COMMAND commands
[] = {
31 { "cd", com_cd
, "Change to directory DIR" },
32 { "delete", com_delete
, "Delete FILE" },
33 { "help", com_help
, "Display this text" },
34 { "?", com_help
, "Synonym for `help'" },
35 { "list", com_list
, "List files in DIR" },
36 { "ls", com_list
, "Synonym for `list'" },
37 { "pwd", com_pwd
, "Print the current working directory" },
38 { "quit", com_quit
, "Quit using Fileman" },
39 { "rename", com_rename
, "Rename FILE to NEWNAME" },
40 { "stat", com_stat
, "Print out statistics on FILE" },
41 { "view", com_view
, "View the contents of FILE" },
42 { (char *)NULL
, (Function
*)NULL
, (char *)NULL
}
45 /* Forward declarations. */
47 COMMAND
*find_command ();
49 /* The name of this program, as taken from argv[0]. */
52 /* When non-zero, this global means the user is done using this program. */
61 r
= xmalloc (strlen (s
) + 1);
74 initialize_readline (); /* Bind our completer. */
76 /* Loop reading and executing lines until the user quits. */
79 line
= readline ("FileMan: ");
84 /* Remove leading and trailing whitespace from the line.
85 Then, if there is anything left, add it to the history list
87 s
= stripwhite (line
);
100 /* Execute a command line. */
109 /* Isolate the command word. */
111 while (line
[i
] && whitespace (line
[i
]))
115 while (line
[i
] && !whitespace (line
[i
]))
121 command
= find_command (word
);
125 fprintf (stderr
, "%s: No such command for FileMan.\n", word
);
129 /* Get argument to command, if any. */
130 while (whitespace (line
[i
]))
135 /* Call the function. */
136 return ((*(command
->func
)) (word
));
139 /* Look up NAME as the name of a command, and return a pointer to that
140 command. Return a NULL pointer if NAME isn't a command name. */
147 for (i
= 0; commands
[i
].name
; i
++)
148 if (strcmp (name
, commands
[i
].name
) == 0)
149 return (&commands
[i
]);
151 return ((COMMAND
*)NULL
);
154 /* Strip whitespace from the start and end of STRING. Return a pointer
160 register char *s
, *t
;
162 for (s
= string
; whitespace (*s
); s
++)
168 t
= s
+ strlen (s
) - 1;
169 while (t
> s
&& whitespace (*t
))
176 /* **************************************************************** */
178 /* Interface to Readline Completion */
180 /* **************************************************************** */
182 char *command_generator ();
183 char **fileman_completion ();
185 /* Tell the GNU Readline library how to complete. We want to try to complete
186 on command names if this is the first word in the line, or on filenames
188 initialize_readline ()
190 /* Allow conditional parsing of the ~/.inputrc file. */
191 rl_readline_name
= "FileMan";
193 /* Tell the completer that we want a crack first. */
194 rl_attempted_completion_function
= (CPPFunction
*)fileman_completion
;
197 /* Attempt to complete on the contents of TEXT. START and END bound the
198 region of rl_line_buffer that contains the word to complete. TEXT is
199 the word to complete. We can use the entire contents of rl_line_buffer
200 in case we want to do some simple parsing. Return the array of matches,
201 or NULL if there aren't any. */
203 fileman_completion (text
, start
, end
)
209 matches
= (char **)NULL
;
211 /* If this word is at the start of the line, then it is a command
212 to complete. Otherwise it is the name of a file in the current
215 matches
= completion_matches (text
, command_generator
);
220 /* Generator function for command completion. STATE lets us know whether
221 to start from scratch; without any state (i.e. STATE == 0), then we
222 start at the top of the list. */
224 command_generator (text
, state
)
228 static int list_index
, len
;
231 /* If this is a new word to complete, initialize now. This includes
232 saving the length of TEXT for efficiency, and initializing the index
240 /* Return the next name which partially matches from the command list. */
241 while (name
= commands
[list_index
].name
)
245 if (strncmp (name
, text
, len
) == 0)
246 return (dupstr(name
));
249 /* If no names matched, then return NULL. */
250 return ((char *)NULL
);
253 /* **************************************************************** */
255 /* FileMan Commands */
257 /* **************************************************************** */
259 /* String to pass to system (). This is for the LIST, VIEW and RENAME
261 static char syscom
[1024];
263 /* List the file(s) named in arg. */
270 sprintf (syscom
, "ls -FClg %s", arg
);
271 return (system (syscom
));
277 if (!valid_argument ("view", arg
))
280 sprintf (syscom
, "more %s", arg
);
281 return (system (syscom
));
287 too_dangerous ("rename");
296 if (!valid_argument ("stat", arg
))
299 if (stat (arg
, &finfo
) == -1)
305 printf ("Statistics for `%s':\n", arg
);
307 printf ("%s has %d link%s, and is %d byte%s in length.\n", arg
,
309 (finfo
.st_nlink
== 1) ? "" : "s",
311 (finfo
.st_size
== 1) ? "" : "s");
312 printf ("Inode Last Change at: %s", ctime (&finfo
.st_ctime
));
313 printf (" Last access at: %s", ctime (&finfo
.st_atime
));
314 printf (" Last modified at: %s", ctime (&finfo
.st_mtime
));
321 too_dangerous ("delete");
325 /* Print out help for ARG, or for all of the commands if ARG is
333 for (i
= 0; commands
[i
].name
; i
++)
335 if (!*arg
|| (strcmp (arg
, commands
[i
].name
) == 0))
337 printf ("%s\t\t%s.\n", commands
[i
].name
, commands
[i
].doc
);
344 printf ("No commands match `%s'. Possibilties are:\n", arg
);
346 for (i
= 0; commands
[i
].name
; i
++)
348 /* Print in six columns. */
355 printf ("%s\t", commands
[i
].name
);
365 /* Change to the directory ARG. */
369 if (chdir (arg
) == -1)
379 /* Print out the current working directory. */
388 printf ("Error getting pwd: %s\n", dir
);
392 printf ("Current directory is %s\n", dir
);
396 /* The user wishes to quit using this program. Just set DONE non-zero. */
404 /* Function which tells you that you can't do this. */
405 too_dangerous (caller
)
409 "%s: Too dangerous for me to distribute. Write it yourself.\n",
413 /* Return non-zero if ARG is a valid argument for CALLER, else print
414 an error message and return zero. */
416 valid_argument (caller
, arg
)
421 fprintf (stderr
, "%s: Argument required.\n", caller
);