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