OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [tags/] [gdb/] [gdb-6.8/] [gdb-6.8.openrisc-2.1/] [readline/] [examples/] [fileman.c] - Diff between revs 24 and 33

Only display areas with differences | Details | Blame | View Log

Rev 24 Rev 33
/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
 
 
   This file is part of the GNU Readline Library, a library for
   This file is part of the GNU Readline Library, a library for
   reading lines of text with interactive input and history editing.
   reading lines of text with interactive input and history editing.
 
 
   The GNU Readline Library is free software; you can redistribute it
   The GNU Readline Library is free software; you can redistribute it
   and/or modify it under the terms of the GNU General Public License
   and/or modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2, or
   as published by the Free Software Foundation; either version 2, or
   (at your option) any later version.
   (at your option) any later version.
 
 
   The GNU Readline Library is distributed in the hope that it will be
   The GNU Readline Library is distributed in the hope that it will be
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   GNU General Public License for more details.
 
 
   The GNU General Public License is often shipped with GNU software, and
   The GNU General Public License is often shipped with GNU software, and
   is generally kept in a file called COPYING or LICENSE.  If you do not
   is generally kept in a file called COPYING or LICENSE.  If you do not
   have a copy of the license, write to the Free Software Foundation,
   have a copy of the license, write to the Free Software Foundation,
   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
 
 
/* fileman.c -- A tiny application which demonstrates how to use the
/* fileman.c -- A tiny application which demonstrates how to use the
   GNU Readline library.  This application interactively allows users
   GNU Readline library.  This application interactively allows users
   to manipulate files and their modes. */
   to manipulate files and their modes. */
 
 
#ifdef HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
#  include <config.h>
#  include <config.h>
#endif
#endif
 
 
#include <sys/types.h>
#include <sys/types.h>
#ifdef HAVE_SYS_FILE_H
#ifdef HAVE_SYS_FILE_H
#  include <sys/file.h>
#  include <sys/file.h>
#endif
#endif
#include <sys/stat.h>
#include <sys/stat.h>
 
 
#ifdef HAVE_UNISTD_H
#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#  include <unistd.h>
#endif
#endif
 
 
#include <fcntl.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdio.h>
#include <errno.h>
#include <errno.h>
 
 
#if defined (HAVE_STRING_H)
#if defined (HAVE_STRING_H)
#  include <string.h>
#  include <string.h>
#else /* !HAVE_STRING_H */
#else /* !HAVE_STRING_H */
#  include <strings.h>
#  include <strings.h>
#endif /* !HAVE_STRING_H */
#endif /* !HAVE_STRING_H */
 
 
#ifdef HAVE_STDLIB_H
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#  include <stdlib.h>
#endif
#endif
 
 
#ifdef READLINE_LIBRARY
#ifdef READLINE_LIBRARY
#  include "readline.h"
#  include "readline.h"
#  include "history.h"
#  include "history.h"
#else
#else
#  include <readline/readline.h>
#  include <readline/readline.h>
#  include <readline/history.h>
#  include <readline/history.h>
#endif
#endif
 
 
extern char *xmalloc ();
extern char *xmalloc ();
 
 
/* The names of functions that actually do the manipulation. */
/* The names of functions that actually do the manipulation. */
int com_list PARAMS((char *));
int com_list PARAMS((char *));
int com_view PARAMS((char *));
int com_view PARAMS((char *));
int com_rename PARAMS((char *));
int com_rename PARAMS((char *));
int com_stat PARAMS((char *));
int com_stat PARAMS((char *));
int com_pwd PARAMS((char *));
int com_pwd PARAMS((char *));
int com_delete PARAMS((char *));
int com_delete PARAMS((char *));
int com_help PARAMS((char *));
int com_help PARAMS((char *));
int com_cd PARAMS((char *));
int com_cd PARAMS((char *));
int com_quit PARAMS((char *));
int com_quit PARAMS((char *));
 
 
/* A structure which contains information on the commands this program
/* A structure which contains information on the commands this program
   can understand. */
   can understand. */
 
 
typedef struct {
typedef struct {
  char *name;                   /* User printable name of the function. */
  char *name;                   /* User printable name of the function. */
  rl_icpfunc_t *func;           /* Function to call to do the job. */
  rl_icpfunc_t *func;           /* Function to call to do the job. */
  char *doc;                    /* Documentation for this function.  */
  char *doc;                    /* Documentation for this function.  */
} 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" },
  { (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;
 
 
/* When non-zero, this global means the user is done using this program. */
/* When non-zero, this global means the user is done using this program. */
int done;
int done;
 
 
char *
char *
dupstr (s)
dupstr (s)
     char *s;
     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);
}
}
 
 
main (argc, argv)
main (argc, argv)
     int argc;
     int argc;
     char **argv;
     char **argv;
{
{
  char *line, *s;
  char *line, *s;
 
 
  progname = argv[0];
  progname = argv[0];
 
 
  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;
 
 
      /* Remove leading and trailing whitespace from the line.
      /* Remove leading and trailing whitespace from the line.
         Then, if there is anything left, add it to the history list
         Then, if there is anything left, add it to the history list
         and execute it. */
         and execute it. */
      s = stripwhite (line);
      s = stripwhite (line);
 
 
      if (*s)
      if (*s)
        {
        {
          add_history (s);
          add_history (s);
          execute_line (s);
          execute_line (s);
        }
        }
 
 
      free (line);
      free (line);
    }
    }
  exit (0);
  exit (0);
}
}
 
 
/* Execute a command line. */
/* Execute a command line. */
int
int
execute_line (line)
execute_line (line)
     char *line;
     char *line;
{
{
  register int i;
  register int i;
  COMMAND *command;
  COMMAND *command;
  char *word;
  char *word;
 
 
  /* Isolate the command word. */
  /* Isolate the command word. */
  i = 0;
  i = 0;
  while (line[i] && whitespace (line[i]))
  while (line[i] && whitespace (line[i]))
    i++;
    i++;
  word = line + i;
  word = line + i;
 
 
  while (line[i] && !whitespace (line[i]))
  while (line[i] && !whitespace (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 (whitespace (line[i]))
  while (whitespace (line[i]))
    i++;
    i++;
 
 
  word = line + i;
  word = line + i;
 
 
  /* Call the function. */
  /* Call the function. */
  return ((*(command->func)) (word));
  return ((*(command->func)) (word));
}
}
 
 
/* 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 (name)
find_command (name)
     char *name;
     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 (string)
stripwhite (string)
     char *string;
     char *string;
{
{
  register char *s, *t;
  register char *s, *t;
 
 
  for (s = string; whitespace (*s); s++)
  for (s = string; whitespace (*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 && whitespace (*t))
  while (t > s && whitespace (*t))
    t--;
    t--;
  *++t = '\0';
  *++t = '\0';
 
 
  return s;
  return s;
}
}
 
 
/* **************************************************************** */
/* **************************************************************** */
/*                                                                  */
/*                                                                  */
/*                  Interface to Readline Completion                */
/*                  Interface to Readline Completion                */
/*                                                                  */
/*                                                                  */
/* **************************************************************** */
/* **************************************************************** */
 
 
char *command_generator PARAMS((const char *, int));
char *command_generator PARAMS((const char *, int));
char **fileman_completion PARAMS((const char *, int, int));
char **fileman_completion PARAMS((const char *, int, int));
 
 
/* Tell the GNU Readline library how to complete.  We want to try to complete
/* 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 on filenames
   on command names if this is the first word in the line, or on filenames
   if not. */
   if not. */
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";
 
 
  /* Tell the completer that we want a crack first. */
  /* Tell the completer that we want a crack first. */
  rl_attempted_completion_function = fileman_completion;
  rl_attempted_completion_function = fileman_completion;
}
}
 
 
/* Attempt to complete on the contents of TEXT.  START and END bound the
/* Attempt to complete on the contents of TEXT.  START and END bound the
   region of rl_line_buffer that contains the word to complete.  TEXT is
   region of rl_line_buffer that contains the word to complete.  TEXT is
   the word to complete.  We can use the entire contents of rl_line_buffer
   the word to complete.  We can use the entire contents of rl_line_buffer
   in case we want to do some simple parsing.  Return the array of matches,
   in case we want to do some simple parsing.  Return the array of matches,
   or NULL if there aren't any. */
   or NULL if there aren't any. */
char **
char **
fileman_completion (text, start, end)
fileman_completion (text, start, end)
     const char *text;
     const char *text;
     int start, end;
     int start, end;
{
{
  char **matches;
  char **matches;
 
 
  matches = (char **)NULL;
  matches = (char **)NULL;
 
 
  /* If this word is at the start of the line, then it is a command
  /* If this word is at the start of the line, then it is a command
     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);
}
}
 
 
/* Generator function for command completion.  STATE lets us know whether
/* Generator function for command completion.  STATE lets us know whether
   to start from scratch; without any state (i.e. STATE == 0), then we
   to start from scratch; without any state (i.e. STATE == 0), then we
   start at the top of the list. */
   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;
 
 
  /* If this is a new word to complete, initialize now.  This includes
  /* If this is a new word to complete, initialize now.  This includes
     saving the length of TEXT for efficiency, and initializing the index
     saving the length of TEXT for efficiency, and initializing the index
     variable to 0. */
     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 command list. */
  /* Return the next name which partially matches from the 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));
    }
    }
 
 
  /* If no names matched, then return NULL. */
  /* If no names matched, then return NULL. */
  return ((char *)NULL);
  return ((char *)NULL);
}
}
 
 
/* **************************************************************** */
/* **************************************************************** */
/*                                                                  */
/*                                                                  */
/*                       FileMan Commands                           */
/*                       FileMan Commands                           */
/*                                                                  */
/*                                                                  */
/* **************************************************************** */
/* **************************************************************** */
 
 
/* String to pass to system ().  This is for the LIST, VIEW and RENAME
/* String to pass to system ().  This is for the LIST, VIEW and RENAME
   commands. */
   commands. */
static char syscom[1024];
static char syscom[1024];
 
 
/* List the file(s) named in arg. */
/* List the file(s) named in arg. */
com_list (arg)
com_list (arg)
     char *arg;
     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));
}
}
 
 
com_view (arg)
com_view (arg)
     char *arg;
     char *arg;
{
{
  if (!valid_argument ("view", arg))
  if (!valid_argument ("view", arg))
    return 1;
    return 1;
 
 
#if defined (__MSDOS__)
#if defined (__MSDOS__)
  /* more.com doesn't grok slashes in pathnames */
  /* more.com doesn't grok slashes in pathnames */
  sprintf (syscom, "less %s", arg);
  sprintf (syscom, "less %s", arg);
#else
#else
  sprintf (syscom, "more %s", arg);
  sprintf (syscom, "more %s", arg);
#endif
#endif
  return (system (syscom));
  return (system (syscom));
}
}
 
 
com_rename (arg)
com_rename (arg)
     char *arg;
     char *arg;
{
{
  too_dangerous ("rename");
  too_dangerous ("rename");
  return (1);
  return (1);
}
}
 
 
com_stat (arg)
com_stat (arg)
     char *arg;
     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 %d link%s, and is %d byte%s in length.\n",
  printf ("%s has %d link%s, and is %d byte%s in length.\n",
          arg,
          arg,
          finfo.st_nlink,
          finfo.st_nlink,
          (finfo.st_nlink == 1) ? "" : "s",
          (finfo.st_nlink == 1) ? "" : "s",
          finfo.st_size,
          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);
}
}
 
 
com_delete (arg)
com_delete (arg)
     char *arg;
     char *arg;
{
{
  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. */
com_help (arg)
com_help (arg)
     char *arg;
     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. */
com_cd (arg)
com_cd (arg)
     char *arg;
     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. */
com_pwd (ignore)
com_pwd (ignore)
     char *ignore;
     char *ignore;
{
{
  char dir[1024], *s;
  char dir[1024], *s;
 
 
  s = getcwd (dir, sizeof(dir) - 1);
  s = 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 non-zero. */
/* The user wishes to quit using this program.  Just set DONE non-zero. */
com_quit (arg)
com_quit (arg)
     char *arg;
     char *arg;
{
{
  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. */
too_dangerous (caller)
too_dangerous (caller)
     char *caller;
     char *caller;
{
{
  fprintf (stderr,
  fprintf (stderr,
           "%s: Too dangerous for me to distribute.  Write it yourself.\n",
           "%s: Too dangerous for me to distribute.  Write it yourself.\n",
           caller);
           caller);
}
}
 
 
/* Return non-zero if ARG is a valid argument for CALLER, else print
/* Return non-zero if ARG is a valid argument for CALLER, else print
   an error message and return zero. */
   an error message and return zero. */
int
int
valid_argument (caller, arg)
valid_argument (caller, arg)
     char *caller, *arg;
     char *caller, *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);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.