examples/fileman.c: Reindent to project coding style

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson 2018-12-25 19:37:42 +01:00
parent 8e34e4e417
commit 8566eb8384

View File

@ -5,7 +5,7 @@
NOTE: this was taken from the GNU Readline documentation and ported NOTE: this was taken from the GNU Readline documentation and ported
to libedit. A command to output the history list was added. to libedit. A command to output the history list was added.
*/ */
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
@ -21,18 +21,18 @@
#include "editline.h" #include "editline.h"
void * xmalloc (size_t size); void *xmalloc(size_t size);
void too_dangerous (char *caller); void too_dangerous(char *caller);
void initialize_readline (); void initialize_readline();
int execute_line (char *line); int execute_line(char *line);
int valid_argument (char *caller, char *arg); int valid_argument(char *caller, char *arg);
typedef int rl_icpfunc_t (char *); typedef int rl_icpfunc_t(char *);
/* The names of functions that actually do the manipulation. */ /* The names of functions that actually do the manipulation. */
int com_list (char *); int com_list(char *);
int com_view (char *); int com_view(char *);
int com_history (char *); int com_history(char *);
int com_rename(char *); int com_rename(char *);
int com_stat(char *); int com_stat(char *);
int com_pwd(char *); int com_pwd(char *);
@ -51,24 +51,24 @@ typedef struct {
} COMMAND; } COMMAND;
COMMAND commands[] = { COMMAND commands[] = {
{ "cd", com_cd, "Change to directory DIR" }, {"cd", com_cd, "Change to directory DIR"},
{ "delete", com_delete, "Delete FILE" }, {"delete", com_delete, "Delete FILE"},
{ "help", com_help, "Display this text" }, {"help", com_help, "Display this text"},
{ "?", com_help, "Synonym for `help'" }, {"?", com_help, "Synonym for `help'"},
{ "list", com_list, "List files in DIR" }, {"list", com_list, "List files in DIR"},
{ "ls", com_list, "Synonym for `list'" }, {"ls", com_list, "Synonym for `list'"},
{ "pwd", com_pwd, "Print the current working directory" }, {"pwd", com_pwd, "Print the current working directory"},
{ "quit", com_quit, "Quit using Fileman" }, {"quit", com_quit, "Quit using Fileman"},
{ "rename", com_rename, "Rename FILE to NEWNAME" }, {"rename", com_rename, "Rename FILE to NEWNAME"},
{ "stat", com_stat, "Print out statistics on FILE" }, {"stat", com_stat, "Print out statistics on FILE"},
{ "view", com_view, "View the contents of FILE" }, {"view", com_view, "View the contents of FILE"},
{ "history", com_history, "List editline history" }, {"history", com_history, "List editline history"},
{ (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL } {(char *)NULL, (rl_icpfunc_t *) NULL, (char *)NULL}
}; };
/* Forward declarations. */ /* Forward declarations. */
char *stripwhite (); char *stripwhite();
COMMAND *find_command (); COMMAND *find_command();
/* The name of this program, as taken from argv[0]. */ /* The name of this program, as taken from argv[0]. */
char *progname; char *progname;
@ -76,18 +76,16 @@ char *progname;
/* When non-zero, this means the user is done using this program. */ /* When non-zero, this means the user is done using this program. */
int done; int done;
char * char *dupstr(char *s)
dupstr (char* s)
{ {
char *r; char *r;
r = xmalloc (strlen (s) + 1); r = xmalloc(strlen(s) + 1);
strcpy (r, s); strcpy(r, s);
return (r); return (r);
} }
int int main(int argc __attribute__ ((__unused__)), char **argv)
main (int argc __attribute__((__unused__)), char **argv)
{ {
char *line, *s; char *line, *s;
@ -98,9 +96,8 @@ main (int argc __attribute__((__unused__)), char **argv)
initialize_readline(); /* Bind our completer. */ initialize_readline(); /* Bind our completer. */
/* Loop reading and executing lines until the user quits. */ /* Loop reading and executing lines until the user quits. */
for ( ; done == 0; ) for (; done == 0;) {
{ line = readline("FileMan: ");
line = readline ("FileMan: ");
if (!line) if (!line)
break; break;
@ -112,7 +109,7 @@ main (int argc __attribute__((__unused__)), char **argv)
#if 0 #if 0
if (*s) { if (*s) {
char* expansion; char *expansion;
int result; int result;
result = history_expand(s, &expansion); result = history_expand(s, &expansion);
@ -129,14 +126,13 @@ main (int argc __attribute__((__unused__)), char **argv)
#endif #endif
free(line); free(line);
} }
exit (0); exit(0);
return 0; return 0;
} }
/* Execute a command line. */ /* Execute a command line. */
int int execute_line(char *line)
execute_line (char *line)
{ {
register int i; register int i;
COMMAND *command; COMMAND *command;
@ -144,26 +140,25 @@ execute_line (char *line)
/* Isolate the command word. */ /* Isolate the command word. */
i = 0; i = 0;
while (line[i] && isspace (line[i])) while (line[i] && isspace(line[i]))
i++; i++;
word = line + i; word = line + i;
while (line[i] && !isspace (line[i])) while (line[i] && !isspace(line[i]))
i++; i++;
if (line[i]) if (line[i])
line[i++] = '\0'; line[i++] = '\0';
command = find_command (word); command = find_command(word);
if (!command) if (!command) {
{ fprintf(stderr, "%s: No such command for FileMan.\n", word);
fprintf (stderr, "%s: No such command for FileMan.\n", word);
return (-1); return (-1);
} }
/* Get argument to command, if any. */ /* Get argument to command, if any. */
while (isspace (line[i])) while (isspace(line[i]))
i++; i++;
word = line + i; word = line + i;
@ -174,33 +169,30 @@ execute_line (char *line)
/* Look up NAME as the name of a command, and return a pointer to that /* Look up NAME as the name of a command, and return a pointer to that
command. Return a NULL pointer if NAME isn't a command name. */ command. Return a NULL pointer if NAME isn't a command name. */
COMMAND * COMMAND *find_command(char *name)
find_command (char *name)
{ {
register int i; register int i;
for (i = 0; commands[i].name; i++) for (i = 0; commands[i].name; i++)
if (strcmp (name, commands[i].name) == 0) if (strcmp(name, commands[i].name) == 0)
return (&commands[i]); return (&commands[i]);
return ((COMMAND *)NULL); return ((COMMAND *) NULL);
} }
/* Strip whitespace from the start and end of STRING. Return a pointer /* Strip whitespace from the start and end of STRING. Return a pointer
into STRING. */ into STRING. */
char * char *stripwhite(char *string)
stripwhite (char *string)
{ {
register char *s, *t; register char *s, *t;
for (s = string; isspace (*s); s++) for (s = string; isspace(*s); s++) ;
;
if (*s == 0) if (*s == 0)
return (s); return (s);
t = s + strlen (s) - 1; t = s + strlen(s) - 1;
while (t > s && isspace (*t)) while (t > s && isspace(*t))
t--; t--;
*++t = '\0'; *++t = '\0';
@ -219,8 +211,7 @@ char **fileman_completion(const char *, int, int);
/* Tell the GNU Readline library how to complete. We want to try to /* Tell the GNU Readline library how to complete. We want to try to
complete on command names if this is the first word in the line, or complete on command names if this is the first word in the line, or
on filenames if not. */ on filenames if not. */
void void initialize_readline()
initialize_readline ()
{ {
/* Allow conditional parsing of the ~/.inputrc file. */ /* Allow conditional parsing of the ~/.inputrc file. */
rl_readline_name = "FileMan"; rl_readline_name = "FileMan";
@ -234,8 +225,8 @@ initialize_readline ()
complete. TEXT is the word to complete. We can use the entire complete. TEXT is the word to complete. We can use the entire
contents of rl_line_buffer in case we want to do some simple contents of rl_line_buffer in case we want to do some simple
parsing. Returnthe array of matches, or NULL if there aren't any. */ parsing. Returnthe array of matches, or NULL if there aren't any. */
char ** char **fileman_completion(const char *text, int start, int end
fileman_completion (const char* text, int start, int end __attribute__((__unused__))) __attribute__ ((__unused__)))
{ {
char **matches; char **matches;
@ -245,7 +236,7 @@ fileman_completion (const char* text, int start, int end __attribute__((__unused
to complete. Otherwise it is the name of a file in the current to complete. Otherwise it is the name of a file in the current
directory. */ directory. */
if (start == 0) if (start == 0)
matches = rl_completion_matches (text, command_generator); matches = rl_completion_matches(text, command_generator);
return (matches); return (matches);
} }
@ -253,10 +244,9 @@ fileman_completion (const char* text, int start, int end __attribute__((__unused
/* Generator function for command completion. STATE lets us /* Generator function for command completion. STATE lets us
know whether to start from scratch; without any state know whether to start from scratch; without any state
(i.e. STATE == 0), then we start at the top of the list. */ (i.e. STATE == 0), then we start at the top of the list. */
char * char *command_generator(text, state)
command_generator (text, state) const char *text;
const char *text; int state;
int state;
{ {
static int list_index, len; static int list_index, len;
char *name; char *name;
@ -264,19 +254,17 @@ command_generator (text, state)
/* If this is a new word to complete, initialize now. This /* If this is a new word to complete, initialize now. This
includes saving the length of TEXT for efficiency, and includes saving the length of TEXT for efficiency, and
initializing the index variable to 0. */ initializing the index variable to 0. */
if (!state) if (!state) {
{
list_index = 0; list_index = 0;
len = strlen (text); len = strlen(text);
} }
/* Return the next name which partially matches from the /* Return the next name which partially matches from the
command list. */ command list. */
while ((name = commands[list_index].name)) while ((name = commands[list_index].name)) {
{
list_index++; list_index++;
if (strncmp (name, text, len) == 0) if (strncmp(name, text, len) == 0)
return (dupstr(name)); return (dupstr(name));
} }
@ -295,34 +283,30 @@ command_generator (text, state)
static char syscom[1024]; static char syscom[1024];
/* List the file(s) named in arg. */ /* List the file(s) named in arg. */
int int com_list(char *arg)
com_list (char *arg)
{ {
if (!arg) if (!arg)
arg = ""; arg = "";
sprintf (syscom, "ls -FClg %s", arg); sprintf(syscom, "ls -FClg %s", arg);
return (system (syscom)); return (system(syscom));
} }
int int com_view(char *arg)
com_view (char *arg)
{ {
if (!valid_argument ("view", arg)) if (!valid_argument("view", arg))
return 1; return 1;
sprintf (syscom, "more %s", arg); sprintf(syscom, "more %s", arg);
return (system (syscom)); return (system(syscom));
} }
int int com_history(char *arg __attribute__ ((__unused__)))
com_history(char* arg __attribute__((__unused__)))
{ {
const char *he; const char *he;
/* rewind history */ /* rewind history */
while (el_prev_hist()) while (el_prev_hist()) ;
;
for (he = el_next_hist(); he != NULL; he = el_next_hist()) for (he = el_next_hist(); he != NULL; he = el_next_hist())
printf("%s\n", he); printf("%s\n", he);
@ -330,158 +314,142 @@ com_history(char* arg __attribute__((__unused__)))
return 0; return 0;
} }
int int com_rename(char *arg __attribute__ ((__unused__)))
com_rename (char *arg __attribute__((__unused__)))
{ {
too_dangerous ("rename"); too_dangerous("rename");
return (1); return (1);
} }
int int com_stat(char *arg)
com_stat (char *arg)
{ {
struct stat finfo; struct stat finfo;
if (!valid_argument ("stat", arg)) if (!valid_argument("stat", arg))
return (1); return (1);
if (stat (arg, &finfo) == -1) if (stat(arg, &finfo) == -1) {
{ perror(arg);
perror (arg);
return (1); return (1);
} }
printf ("Statistics for `%s':\n", arg); printf("Statistics for `%s':\n", arg);
printf ("%s has %ld link%s, and is %lld byte%s in length.\n", arg, printf("%s has %ld link%s, and is %lld byte%s in length.\n", arg,
(long) finfo.st_nlink, (long)finfo.st_nlink,
(finfo.st_nlink == 1) ? "" : "s", (finfo.st_nlink == 1) ? "" : "s",
(long long) finfo.st_size, (long long)finfo.st_size, (finfo.st_size == 1) ? "" : "s");
(finfo.st_size == 1) ? "" : "s"); printf("Inode Last Change at: %s", ctime(&finfo.st_ctime));
printf ("Inode Last Change at: %s", ctime (&finfo.st_ctime)); printf(" Last access at: %s", ctime(&finfo.st_atime));
printf (" Last access at: %s", ctime (&finfo.st_atime)); printf(" Last modified at: %s", ctime(&finfo.st_mtime));
printf (" Last modified at: %s", ctime (&finfo.st_mtime));
return (0); return (0);
} }
int int com_delete(char *arg __attribute__ ((__unused__)))
com_delete (char *arg __attribute__((__unused__)))
{ {
too_dangerous ("delete"); too_dangerous("delete");
return (1); return (1);
} }
/* Print out help for ARG, or for all of the commands if ARG is /* Print out help for ARG, or for all of the commands if ARG is
not present. */ not present. */
int int com_help(char *arg)
com_help (char *arg)
{ {
register int i; register int i;
int printed = 0; int printed = 0;
for (i = 0; commands[i].name; i++) for (i = 0; commands[i].name; i++) {
{ if (!*arg || (strcmp(arg, commands[i].name) == 0)) {
if (!*arg || (strcmp (arg, commands[i].name) == 0)) printf("%s\t\t%s.\n", commands[i].name,
{ commands[i].doc);
printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
printed++; printed++;
} }
} }
if (!printed) if (!printed) {
{ printf("No commands match `%s'. Possibilties are:\n", arg);
printf ("No commands match `%s'. Possibilties are:\n", arg);
for (i = 0; commands[i].name; i++) for (i = 0; commands[i].name; i++) {
{
/* Print in six columns. */ /* Print in six columns. */
if (printed == 6) if (printed == 6) {
{
printed = 0; printed = 0;
printf ("\n"); printf("\n");
} }
printf ("%s\t", commands[i].name); printf("%s\t", commands[i].name);
printed++; printed++;
} }
if (printed) if (printed)
printf ("\n"); printf("\n");
} }
return (0); return (0);
} }
/* Change to the directory ARG. */ /* Change to the directory ARG. */
int int com_cd(char *arg)
com_cd (char *arg)
{ {
if (chdir (arg) == -1) if (chdir(arg) == -1) {
{ perror(arg);
perror (arg);
return 1; return 1;
} }
com_pwd (""); com_pwd("");
return (0); return (0);
} }
/* Print out the current working directory. */ /* Print out the current working directory. */
int int com_pwd(char *ignore __attribute__ ((__unused__)))
com_pwd (char* ignore __attribute__((__unused__)))
{ {
char dir[1024], *s; char dir[1024], *s;
s = (char*)getcwd(dir, sizeof(dir) - 1); s = (char *)getcwd(dir, sizeof(dir) - 1);
if (s == 0) if (s == 0) {
{ printf("Error getting pwd: %s\n", dir);
printf ("Error getting pwd: %s\n", dir);
return 1; return 1;
} }
printf ("Current directory is %s\n", dir); printf("Current directory is %s\n", dir);
return 0; return 0;
} }
/* The user wishes to quit using this program. Just set DONE /* The user wishes to quit using this program. Just set DONE
non-zero. */ non-zero. */
int int com_quit(char *arg __attribute__ ((__unused__)))
com_quit (char *arg __attribute__((__unused__)))
{ {
done = 1; done = 1;
return (0); return (0);
} }
/* Function which tells you that you can't do this. */ /* Function which tells you that you can't do this. */
void void too_dangerous(char *caller)
too_dangerous (char *caller)
{ {
fprintf (stderr, fprintf(stderr, "%s: Too dangerous for me to distribute.\n", caller);
"%s: Too dangerous for me to distribute.\n", fprintf(stderr, "Write it yourself.\n");
caller);
fprintf (stderr, "Write it yourself.\n");
} }
/* Return non-zero if ARG is a valid argument for CALLER, /* Return non-zero if ARG is a valid argument for CALLER,
else print an error message and return zero. */ else print an error message and return zero. */
int int valid_argument(char *caller, char *arg)
valid_argument (char *caller, char *arg)
{ {
if (!arg || !*arg) if (!arg || !*arg) {
{ fprintf(stderr, "%s: Argument required.\n", caller);
fprintf (stderr, "%s: Argument required.\n", caller);
return (0); return (0);
} }
return (1); return (1);
} }
void * void *xmalloc(size_t size)
xmalloc (size_t size)
{ {
register void *value = (void*)malloc(size); register void *value = (void *)malloc(size);
if (value == 0) if (value == 0)
fprintf(stderr, "virtual memory exhausted"); fprintf(stderr, "virtual memory exhausted");
return value; return value;
} }
/**
* Local Variables:
* c-file-style: "k&r"
* c-basic-offset: 4
* End:
*/