]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/libreadline/examples/manexamp.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / libreadline / examples / manexamp.c
1 /* manexamp.c -- The examples which appear in the documentation are here. */
2
3 /* Copyright (C) 1987-2002 Free Software Foundation, Inc.
4
5    This file is part of the GNU Readline Library, a library for
6    reading lines of text with interactive input and history editing.
7
8    The GNU Readline Library is free software; you can redistribute it
9    and/or modify it under the terms of the GNU General Public License
10    as published by the Free Software Foundation; either version 2, or
11    (at your option) any later version.
12
13    The GNU Readline Library is distributed in the hope that it will be
14    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    The GNU General Public License is often shipped with GNU software, and
19    is generally kept in a file called COPYING or LICENSE.  If you do not
20    have a copy of the license, write to the Free Software Foundation,
21    59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22
23 #include <stdio.h>
24 #include <readline/readline.h>
25
26 /* **************************************************************** */
27 /*                                                                  */
28 /*                      How to Emulate gets ()                      */
29 /*                                                                  */
30 /* **************************************************************** */
31
32 /* A static variable for holding the line. */
33 static char *line_read = (char *)NULL;
34
35 /* Read a string, and return a pointer to it.  Returns NULL on EOF. */
36 char *
37 rl_gets ()
38 {
39   /* If the buffer has already been allocated, return the memory
40      to the free pool. */
41   if (line_read)
42     {
43       free (line_read);
44       line_read = (char *)NULL;
45     }
46
47   /* Get a line from the user. */
48   line_read = readline ("");
49
50   /* If the line has any text in it, save it on the history. */
51   if (line_read && *line_read)
52     add_history (line_read);
53
54   return (line_read);
55 }
56
57 /* **************************************************************** */
58 /*                                                                  */
59 /*        Writing a Function to be Called by Readline.              */
60 /*                                                                  */
61 /* **************************************************************** */
62
63 /* Invert the case of the COUNT following characters. */
64 invert_case_line (count, key)
65      int count, key;
66 {
67   register int start, end;
68
69   start = rl_point;
70
71   if (count < 0)
72     {
73       direction = -1;
74       count = -count;
75     }
76   else
77     direction = 1;
78       
79   /* Find the end of the range to modify. */
80   end = start + (count * direction);
81
82   /* Force it to be within range. */
83   if (end > rl_end)
84     end = rl_end;
85   else if (end < 0)
86     end = -1;
87
88   if (start > end)
89     {
90       int temp = start;
91       start = end;
92       end = temp;
93     }
94
95   if (start == end)
96     return;
97
98   /* Tell readline that we are modifying the line, so save the undo
99      information. */
100   rl_modifying (start, end);
101
102   for (; start != end; start += direction)
103     {
104       if (_rl_uppercase_p (rl_line_buffer[start]))
105         rl_line_buffer[start] = _rl_to_lower (rl_line_buffer[start]);
106       else if (_rl_lowercase_p (rl_line_buffer[start]))
107         rl_line_buffer[start] = _rl_to_upper (rl_line_buffer[start]);
108     }
109
110   /* Move point to on top of the last character changed. */
111   rl_point = end - direction;
112 }