121308Sache/* manexamp.c -- The examples which appear in the documentation are here. */
221308Sache
3119610Sache/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
4119610Sache
5119610Sache   This file is part of the GNU Readline Library, a library for
6119610Sache   reading lines of text with interactive input and history editing.
7119610Sache
8119610Sache   The GNU Readline Library is free software; you can redistribute it
9119610Sache   and/or modify it under the terms of the GNU General Public License
10119610Sache   as published by the Free Software Foundation; either version 2, or
11119610Sache   (at your option) any later version.
12119610Sache
13119610Sache   The GNU Readline Library is distributed in the hope that it will be
14119610Sache   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15119610Sache   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16119610Sache   GNU General Public License for more details.
17119610Sache
18119610Sache   The GNU General Public License is often shipped with GNU software, and
19119610Sache   is generally kept in a file called COPYING or LICENSE.  If you do not
20119610Sache   have a copy of the license, write to the Free Software Foundation,
21119610Sache   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22119610Sache
2321308Sache#include <stdio.h>
2421308Sache#include <readline/readline.h>
2521308Sache
2621308Sache/* **************************************************************** */
2721308Sache/*                                                                  */
2875406Sache/*   			How to Emulate gets ()			    */
2921308Sache/*                                                                  */
3021308Sache/* **************************************************************** */
3121308Sache
3221308Sache/* A static variable for holding the line. */
3321308Sachestatic char *line_read = (char *)NULL;
3421308Sache
3521308Sache/* Read a string, and return a pointer to it.  Returns NULL on EOF. */
3621308Sachechar *
3721308Sacherl_gets ()
3821308Sache{
3921308Sache  /* If the buffer has already been allocated, return the memory
4021308Sache     to the free pool. */
4121308Sache  if (line_read)
4221308Sache    {
4321308Sache      free (line_read);
4421308Sache      line_read = (char *)NULL;
4521308Sache    }
4621308Sache
4721308Sache  /* Get a line from the user. */
4821308Sache  line_read = readline ("");
4921308Sache
5021308Sache  /* If the line has any text in it, save it on the history. */
5121308Sache  if (line_read && *line_read)
5221308Sache    add_history (line_read);
5321308Sache
5421308Sache  return (line_read);
5521308Sache}
5621308Sache
5721308Sache/* **************************************************************** */
5821308Sache/*                                                                  */
5921308Sache/*        Writing a Function to be Called by Readline.              */
6021308Sache/*                                                                  */
6121308Sache/* **************************************************************** */
6221308Sache
6321308Sache/* Invert the case of the COUNT following characters. */
6421308Sacheinvert_case_line (count, key)
6521308Sache     int count, key;
6621308Sache{
6721308Sache  register int start, end;
6821308Sache
6921308Sache  start = rl_point;
7021308Sache
7121308Sache  if (count < 0)
7221308Sache    {
7321308Sache      direction = -1;
7421308Sache      count = -count;
7521308Sache    }
7621308Sache  else
7721308Sache    direction = 1;
7821308Sache
7921308Sache  /* Find the end of the range to modify. */
8021308Sache  end = start + (count * direction);
8121308Sache
8221308Sache  /* Force it to be within range. */
8321308Sache  if (end > rl_end)
8421308Sache    end = rl_end;
8521308Sache  else if (end < 0)
8621308Sache    end = -1;
8721308Sache
8821308Sache  if (start > end)
8921308Sache    {
9021308Sache      int temp = start;
9121308Sache      start = end;
9221308Sache      end = temp;
9321308Sache    }
9421308Sache
9521308Sache  if (start == end)
9621308Sache    return;
9721308Sache
9821308Sache  /* Tell readline that we are modifying the line, so save the undo
9921308Sache     information. */
10021308Sache  rl_modifying (start, end);
10121308Sache
10221308Sache  for (; start != end; start += direction)
10321308Sache    {
10475406Sache      if (_rl_uppercase_p (rl_line_buffer[start]))
10575406Sache	rl_line_buffer[start] = _rl_to_lower (rl_line_buffer[start]);
10675406Sache      else if (_rl_lowercase_p (rl_line_buffer[start]))
10775406Sache	rl_line_buffer[start] = _rl_to_upper (rl_line_buffer[start]);
10821308Sache    }
10921308Sache
11021308Sache  /* Move point to on top of the last character changed. */
11121308Sache  rl_point = end - direction;
11221308Sache}
113