home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / gdb-4.12.tar.gz / gdb-4.12.tar / gdb-4.12 / readline / examples / fileman.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  10KB  |  446 lines

  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. */
  4.  
  5. #include <stdio.h>
  6. #include <readline/readline.h>
  7. #include <readline/history.h>
  8. #include <sys/types.h>
  9. #include <sys/file.h>
  10. #include <sys/stat.h>
  11. #include <sys/errno.h>
  12.  
  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 ();
  16.  
  17. /* A structure which contains information on the commands this program
  18.    can understand. */
  19.  
  20. typedef struct {
  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.  */
  24. } COMMAND;
  25.  
  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 }
  39. };
  40.  
  41. /* The name of this program, as taken from argv[0]. */
  42. char *progname;
  43.  
  44. /* When non-zero, this global means the user is done using this program. */
  45. int done = 0;
  46.  
  47. main (argc, argv)
  48.      int argc;
  49.      char **argv;
  50. {
  51.   progname = argv[0];
  52.  
  53.   initialize_readline ();    /* Bind our completer. */
  54.  
  55.   /* Loop reading and executing lines until the user quits. */
  56.   while (!done)
  57.     {
  58.       char *line;
  59.  
  60.       line = readline ("FileMan: ");
  61.  
  62.       if (!line)
  63.     {
  64.       done = 1;        /* Encountered EOF at top level. */
  65.     }
  66.       else
  67.     {
  68.       /* Remove leading and trailing whitespace from the line.
  69.          Then, if there is anything left, add it to the history list
  70.          and execute it. */
  71.       stripwhite (line);
  72.  
  73.       if (*line)
  74.         {
  75.           add_history (line);
  76.           execute_line (line);
  77.         }
  78.     }
  79.  
  80.       if (line)
  81.     free (line);
  82.     }
  83.   exit (0);
  84. }
  85.  
  86. /* Execute a command line. */
  87. execute_line (line)
  88.      char *line;
  89. {
  90.   register int i;
  91.   COMMAND *find_command (), *command;
  92.   char *word;
  93.  
  94.   /* Isolate the command word. */
  95.   i = 0;
  96.   while (line[i] && !whitespace (line[i]))
  97.     i++;
  98.  
  99.   word = line;
  100.  
  101.   if (line[i])
  102.     line[i++] = '\0';
  103.  
  104.   command = find_command (word);
  105.  
  106.   if (!command)
  107.     {
  108.       fprintf (stderr, "%s: No such command for FileMan.\n", word);
  109.       return;
  110.     }
  111.  
  112.   /* Get argument to command, if any. */
  113.   while (whitespace (line[i]))
  114.     i++;
  115.  
  116.   word = line + i;
  117.  
  118.   /* Call the function. */
  119.   (*(command->func)) (word);
  120. }
  121.  
  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. */
  124. COMMAND *
  125. find_command (name)
  126.      char *name;
  127. {
  128.   register int i;
  129.  
  130.   for (i = 0; commands[i].name; i++)
  131.     if (strcmp (name, commands[i].name) == 0)
  132.       return (&commands[i]);
  133.  
  134.   return ((COMMAND *)NULL);
  135. }
  136.  
  137. /* Strip whitespace from the start and end of STRING. */
  138. stripwhite (string)
  139.      char *string;
  140. {
  141.   register int i = 0;
  142.  
  143.   while (whitespace (string[i]))
  144.     i++;
  145.  
  146.   if (i)
  147.     strcpy (string, string + i);
  148.  
  149.   i = strlen (string) - 1;
  150.  
  151.   while (i > 0 && whitespace (string[i]))
  152.     i--;
  153.  
  154.   string[++i] = '\0';
  155. }
  156.  
  157. /* **************************************************************** */
  158. /*                                                                  */
  159. /*                  Interface to Readline Completion                */
  160. /*                                                                  */
  161. /* **************************************************************** */
  162.  
  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
  165.    if not. */
  166. initialize_readline ()
  167. {
  168.   char **fileman_completion ();
  169.  
  170.   /* Allow conditional parsing of the ~/.inputrc file. */
  171.   rl_readline_name = "FileMan";
  172.  
  173.   /* Tell the completer that we want a crack first. */
  174.   rl_attempted_completion_function = (Function *)fileman_completion;
  175. }
  176.  
  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. */
  181. char **
  182. fileman_completion (text, start, end)
  183.      char *text;
  184.      int start, end;
  185. {
  186.   char **matches;
  187.   char *command_generator ();
  188.  
  189.   matches = (char **)NULL;
  190.  
  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
  193.      directory. */
  194.   if (start == 0)
  195.     matches = completion_matches (text, command_generator);
  196.  
  197.   return (matches);
  198. }
  199.  
  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. */
  203. char *
  204. command_generator (text, state)
  205.      char *text;
  206.      int state;
  207. {
  208.   static int list_index, len;
  209.   char *name;
  210.  
  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
  213.      variable to 0. */
  214.   if (!state)
  215.     {
  216.       list_index = 0;
  217.       len = strlen (text);
  218.     }
  219.  
  220.   /* Return the next name which partially matches from the command list. */
  221.   while (name = commands[list_index].name)
  222.     {
  223.       list_index++;
  224.  
  225.       if (strncmp (name, text, len) == 0)
  226.     return (name);
  227.     }
  228.  
  229.   /* If no names matched, then return NULL. */
  230.   return ((char *)NULL);
  231. }
  232.  
  233. /* **************************************************************** */
  234. /*                                                                  */
  235. /*                       FileMan Commands                           */
  236. /*                                                                  */
  237. /* **************************************************************** */
  238.  
  239. /* String to pass to system ().  This is for the LIST, VIEW and RENAME
  240.    commands. */
  241. static char syscom[1024];
  242.  
  243. /* List the file(s) named in arg. */
  244. com_list (arg)
  245.      char *arg;
  246. {
  247.   if (!arg)
  248.     arg = "*";
  249.  
  250. #ifdef __GO32__
  251.   sprintf (syscom, "ls -lp %s", arg);
  252. #else
  253.   sprintf (syscom, "ls -FClg %s", arg);
  254. #endif
  255.   system (syscom);
  256. }
  257.  
  258. com_view (arg)
  259.      char *arg;
  260. {
  261.   if (!valid_argument ("view", arg))
  262.     return;
  263.  
  264.   sprintf (syscom, "cat %s | more", arg);
  265.   system (syscom);
  266. }
  267.  
  268. com_rename (arg)
  269.      char *arg;
  270. {
  271.   too_dangerous ("rename");
  272. }
  273.  
  274. com_stat (arg)
  275.      char *arg;
  276. {
  277.   struct stat finfo;
  278.  
  279.   if (!valid_argument ("stat", arg))
  280.     return;
  281.  
  282.   if (stat (arg, &finfo) == -1)
  283.     {
  284.       perror (arg);
  285.       return;
  286.     }
  287.  
  288.   printf ("Statistics for `%s':\n", arg);
  289.  
  290.   printf ("%s has %d link%s, and is %d bytes in length.\n", arg,
  291.       finfo.st_nlink, (finfo.st_nlink == 1) ? "" : "s",  finfo.st_size);
  292.   printf ("      Created on: %s", ctime (&finfo.st_ctime));
  293.   printf ("  Last access at: %s", ctime (&finfo.st_atime));
  294.   printf ("Last modified at: %s", ctime (&finfo.st_mtime));
  295. }
  296.  
  297. com_delete (arg)
  298.      char *arg;
  299. {
  300.   too_dangerous ("delete");
  301. }
  302.  
  303. /* Print out help for ARG, or for all of the commands if ARG is
  304.    not present. */
  305. com_help (arg)
  306.      char *arg;
  307. {
  308.   register int i;
  309.   int printed = 0;
  310.  
  311.   for (i = 0; commands[i].name; i++)
  312.     {
  313.       if (!*arg || (strcmp (arg, commands[i].name) == 0))
  314.     {
  315.       printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
  316.       printed++;
  317.     }
  318.     }
  319.  
  320.   if (!printed)
  321.     {
  322.       printf ("No commands match `%s'.  Possibilties are:\n", arg);
  323.  
  324.       for (i = 0; commands[i].name; i++)
  325.     {
  326.       /* Print in six columns. */
  327.       if (printed == 6)
  328.         {
  329.           printed = 0;
  330.           printf ("\n");
  331.         }
  332.  
  333.       printf ("%s\t", commands[i].name);
  334.       printed++;
  335.     }
  336.  
  337.       if (printed)
  338.     printf ("\n");
  339.     }
  340. }
  341.  
  342. /* Change to the directory ARG. */
  343. com_cd (arg)
  344.      char *arg;
  345. {
  346.   if (chdir (arg) == -1)
  347.     perror (arg);
  348.  
  349.   com_pwd ("");
  350. }
  351.  
  352. /* Print out the current working directory. */
  353. com_pwd (ignore)
  354.      char *ignore;
  355. {
  356.   char dir[1024];
  357.  
  358.   (void) getwd (dir);
  359.  
  360.   printf ("Current directory is %s\n", dir);
  361. }
  362.  
  363. /* The user wishes to quit using this program.  Just set DONE non-zero. */
  364. com_quit (arg)
  365.      char *arg;
  366. {
  367.   done = 1;
  368. }
  369.  
  370. /* Function which tells you that you can't do this. */
  371. too_dangerous (caller)
  372.      char *caller;
  373. {
  374.   fprintf (stderr,
  375.        "%s: Too dangerous for me to distribute.  Write it yourself.\n",
  376.        caller);
  377. }
  378.  
  379. /* Return non-zero if ARG is a valid argument for CALLER, else print
  380.    an error message and return zero. */
  381. int
  382. valid_argument (caller, arg)
  383.      char *caller, *arg;
  384. {
  385.   if (!arg || !*arg)
  386.     {
  387.       fprintf (stderr, "%s: Argument required.\n", caller);
  388.       return (0);
  389.     }
  390.  
  391.   return (1);
  392. }
  393.  
  394.  
  395. /*
  396.  * Local variables:
  397.  * compile-command: "cc -g -I../.. -L.. -o fileman fileman.c -lreadline -ltermcap"
  398.  * end:
  399.  */
  400.  
  401. #ifdef __GO32__
  402. /* **************************************************************** */
  403. /*                                    */
  404. /*            xmalloc and xrealloc ()                     */
  405. /*                                    */
  406. /* **************************************************************** */
  407.  
  408. static void memory_error_and_abort ();
  409.  
  410. char *
  411. xmalloc (bytes)
  412.      int bytes;
  413. {
  414.   char *temp = (char *)malloc (bytes);
  415.  
  416.   if (!temp)
  417.     memory_error_and_abort ();
  418.   return (temp);
  419. }
  420.  
  421. char *
  422. xrealloc (pointer, bytes)
  423.      char *pointer;
  424.      int bytes;
  425. {
  426.   char *temp;
  427.  
  428.   if (!pointer)
  429.     temp = (char *)xmalloc (bytes);
  430.   else
  431.     temp = (char *)realloc (pointer, bytes);
  432.  
  433.   if (!temp)
  434.     memory_error_and_abort ();
  435.  
  436.   return (temp);
  437. }
  438.  
  439. static void
  440. memory_error_and_abort ()
  441. {
  442.   fprintf (stderr, "xmalloc: Out of virtual memory!\n");
  443.   abort ();
  444. }
  445. #endif
  446.