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