1 | /* manexamp.c -- The examples which appear in the documentation are here. */ |
---|
2 | |
---|
3 | #include <stdio.h> |
---|
4 | #include <readline/readline.h> |
---|
5 | |
---|
6 | /* **************************************************************** */ |
---|
7 | /* */ |
---|
8 | /* How to Emulate gets () */ |
---|
9 | /* */ |
---|
10 | /* **************************************************************** */ |
---|
11 | |
---|
12 | /* A static variable for holding the line. */ |
---|
13 | static char *line_read = (char *)NULL; |
---|
14 | |
---|
15 | /* Read a string, and return a pointer to it. Returns NULL on EOF. */ |
---|
16 | char * |
---|
17 | rl_gets () |
---|
18 | { |
---|
19 | /* If the buffer has already been allocated, return the memory |
---|
20 | to the free pool. */ |
---|
21 | if (line_read) |
---|
22 | { |
---|
23 | free (line_read); |
---|
24 | line_read = (char *)NULL; |
---|
25 | } |
---|
26 | |
---|
27 | /* Get a line from the user. */ |
---|
28 | line_read = readline (""); |
---|
29 | |
---|
30 | /* If the line has any text in it, save it on the history. */ |
---|
31 | if (line_read && *line_read) |
---|
32 | add_history (line_read); |
---|
33 | |
---|
34 | return (line_read); |
---|
35 | } |
---|
36 | |
---|
37 | /* **************************************************************** */ |
---|
38 | /* */ |
---|
39 | /* Writing a Function to be Called by Readline. */ |
---|
40 | /* */ |
---|
41 | /* **************************************************************** */ |
---|
42 | |
---|
43 | /* Invert the case of the COUNT following characters. */ |
---|
44 | invert_case_line (count, key) |
---|
45 | int count, key; |
---|
46 | { |
---|
47 | register int start, end; |
---|
48 | |
---|
49 | start = rl_point; |
---|
50 | |
---|
51 | if (count < 0) |
---|
52 | { |
---|
53 | direction = -1; |
---|
54 | count = -count; |
---|
55 | } |
---|
56 | else |
---|
57 | direction = 1; |
---|
58 | |
---|
59 | /* Find the end of the range to modify. */ |
---|
60 | end = start + (count * direction); |
---|
61 | |
---|
62 | /* Force it to be within range. */ |
---|
63 | if (end > rl_end) |
---|
64 | end = rl_end; |
---|
65 | else if (end < 0) |
---|
66 | end = -1; |
---|
67 | |
---|
68 | if (start > end) |
---|
69 | { |
---|
70 | int temp = start; |
---|
71 | start = end; |
---|
72 | end = temp; |
---|
73 | } |
---|
74 | |
---|
75 | if (start == end) |
---|
76 | return; |
---|
77 | |
---|
78 | /* Tell readline that we are modifying the line, so save the undo |
---|
79 | information. */ |
---|
80 | rl_modifying (start, end); |
---|
81 | |
---|
82 | for (; start != end; start += direction) |
---|
83 | { |
---|
84 | if (_rl_uppercase_p (rl_line_buffer[start])) |
---|
85 | rl_line_buffer[start] = _rl_to_lower (rl_line_buffer[start]); |
---|
86 | else if (_rl_lowercase_p (rl_line_buffer[start])) |
---|
87 | rl_line_buffer[start] = _rl_to_upper (rl_line_buffer[start]); |
---|
88 | } |
---|
89 | |
---|
90 | /* Move point to on top of the last character changed. */ |
---|
91 | rl_point = end - direction; |
---|
92 | } |
---|