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. */
6 #include <readline/readline.h>
7 #include <readline/history.h>
11 #include <sys/errno.h>
13 /* The names of functions that actually do the manipulation. */
14 int com_list (), com_view (), com_rename (), com_stat (), com_pwd ();
15 int com_delete (), com_help (), com_cd (), com_quit ();
17 /* A structure which contains information on the commands this program
21 char *name
; /* User printable name of the function. */
22 Function
*func
; /* Function to call to do the job. */
23 char *doc
; /* Documentation for this function. */
26 COMMAND commands
[] = {
27 { "cd", com_cd
, "Change to directory DIR" },
28 { "delete", com_delete
, "Delete FILE" },
29 { "help", com_help
, "Display this text" },
30 { "?", com_help
, "Synonym for `help'" },
31 { "list", com_list
, "List files in DIR" },
32 { "ls", com_list
, "Synonym for `list'" },
33 { "pwd", com_pwd
, "Print the current working directory" },
34 { "quit", com_quit
, "Quit using Fileman" },
35 { "rename", com_rename
, "Rename FILE to NEWNAME" },
36 { "stat", com_stat
, "Print out statistics on FILE" },
37 { "view", com_view
, "View the contents of FILE" },
38 { (char *)NULL
, (Function
*)NULL
, (char *)NULL
}
41 /* The name of this program, as taken from argv[0]. */
44 /* When non-zero, this global means the user is done using this program. */
53 initialize_readline (); /* Bind our completer. */
55 /* Loop reading and executing lines until the user quits. */
60 line
= readline ("FileMan: ");
64 done
= 1; /* Encountered EOF at top level. */
68 /* Remove leading and trailing whitespace from the line.
69 Then, if there is anything left, add it to the history list
86 /* Execute a command line. */
91 COMMAND
*find_command (), *command
;
94 /* Isolate the command word. */
96 while (line
[i
] && !whitespace (line
[i
]))
104 command
= find_command (word
);
108 fprintf (stderr
, "%s: No such command for FileMan.\n", word
);
112 /* Get argument to command, if any. */
113 while (whitespace (line
[i
]))
118 /* Call the function. */
119 (*(command
->func
)) (word
);
122 /* Look up NAME as the name of a command, and return a pointer to that
123 command. Return a NULL pointer if NAME isn't a command name. */
130 for (i
= 0; commands
[i
].name
; i
++)
131 if (strcmp (name
, commands
[i
].name
) == 0)
132 return (&commands
[i
]);
134 return ((COMMAND
*)NULL
);
137 /* Strip whitespace from the start and end of STRING. */
143 while (whitespace (string
[i
]))
147 strcpy (string
, string
+ i
);
149 i
= strlen (string
) - 1;
151 while (i
> 0 && whitespace (string
[i
]))
157 /* **************************************************************** */
159 /* Interface to Readline Completion */
161 /* **************************************************************** */
163 /* Tell the GNU Readline library how to complete. We want to try to complete
164 on command names if this is the first word in the line, or on filenames
166 initialize_readline ()
168 char **fileman_completion ();
170 /* Allow conditional parsing of the ~/.inputrc file. */
171 rl_readline_name
= "FileMan";
173 /* Tell the completer that we want a crack first. */
174 rl_attempted_completion_function
= (Function
*)fileman_completion
;
177 /* Attempt to complete on the contents of TEXT. START and END show the
178 region of TEXT that contains the word to complete. We can use the
179 entire line in case we want to do some simple parsing. Return the
180 array of matches, or NULL if there aren't any. */
182 fileman_completion (text
, start
, end
)
187 char *command_generator ();
189 matches
= (char **)NULL
;
191 /* If this word is at the start of the line, then it is a command
192 to complete. Otherwise it is the name of a file in the current
195 matches
= completion_matches (text
, command_generator
);
200 /* Generator function for command completion. STATE lets us know whether
201 to start from scratch; without any state (i.e. STATE == 0), then we
202 start at the top of the list. */
204 command_generator (text
, state
)
208 static int list_index
, len
;
211 /* If this is a new word to complete, initialize now. This includes
212 saving the length of TEXT for efficiency, and initializing the index
220 /* Return the next name which partially matches from the command list. */
221 while (name
= commands
[list_index
].name
)
225 if (strncmp (name
, text
, len
) == 0)
229 /* If no names matched, then return NULL. */
230 return ((char *)NULL
);
233 /* **************************************************************** */
235 /* FileMan Commands */
237 /* **************************************************************** */
239 /* String to pass to system (). This is for the LIST, VIEW and RENAME
241 static char syscom
[1024];
243 /* List the file(s) named in arg. */
250 sprintf (syscom
, "ls -FClg %s", arg
);
257 if (!valid_argument ("view", arg
))
260 sprintf (syscom
, "cat %s | more", arg
);
267 too_dangerous ("rename");
275 if (!valid_argument ("stat", arg
))
278 if (stat (arg
, &finfo
) == -1)
284 printf ("Statistics for `%s':\n", arg
);
286 printf ("%s has %d link%s, and is %d bytes in length.\n", arg
,
287 finfo
.st_nlink
, (finfo
.st_nlink
== 1) ? "" : "s", finfo
.st_size
);
288 printf (" Created on: %s", ctime (&finfo
.st_ctime
));
289 printf (" Last access at: %s", ctime (&finfo
.st_atime
));
290 printf ("Last modified at: %s", ctime (&finfo
.st_mtime
));
296 too_dangerous ("delete");
299 /* Print out help for ARG, or for all of the commands if ARG is
307 for (i
= 0; commands
[i
].name
; i
++)
309 if (!*arg
|| (strcmp (arg
, commands
[i
].name
) == 0))
311 printf ("%s\t\t%s.\n", commands
[i
].name
, commands
[i
].doc
);
318 printf ("No commands match `%s'. Possibilties are:\n", arg
);
320 for (i
= 0; commands
[i
].name
; i
++)
322 /* Print in six columns. */
329 printf ("%s\t", commands
[i
].name
);
338 /* Change to the directory ARG. */
342 if (chdir (arg
) == -1)
348 /* Print out the current working directory. */
356 printf ("Current directory is %s\n", dir
);
359 /* The user wishes to quit using this program. Just set DONE non-zero. */
366 /* Function which tells you that you can't do this. */
367 too_dangerous (caller
)
371 "%s: Too dangerous for me to distribute. Write it yourself.\n",
375 /* Return non-zero if ARG is a valid argument for CALLER, else print
376 an error message and return zero. */
378 valid_argument (caller
, arg
)
383 fprintf (stderr
, "%s: Argument required.\n", caller
);
393 * compile-command: "cc -g -I../.. -L.. -o fileman fileman.c -lreadline -ltermcap"