GC old libarchive/bsdtar.
[dragonfly/port-amd64.git] / contrib / libreadline / examples / manexamp.c
blob3496efa00e7612bb24d31f66697cef176f4c15a5
1 /* manexamp.c -- The examples which appear in the documentation are here. */
3 #include <stdio.h>
4 #include <readline/readline.h>
7 /* **************************************************************** */
8 /* */
9 * How to Emulate gets () */
10 /* */
11 /* **************************************************************** */
13 /* A static variable for holding the line. */
14 static char *line_read = (char *)NULL;
16 /* Read a string, and return a pointer to it. Returns NULL on EOF. */
17 char *
18 rl_gets ()
20 /* If the buffer has already been allocated, return the memory
21 to the free pool. */
22 if (line_read)
24 free (line_read);
25 line_read = (char *)NULL;
28 /* Get a line from the user. */
29 line_read = readline ("");
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);
35 return (line_read);
38 /* **************************************************************** */
39 /* */
40 /* Writing a Function to be Called by Readline. */
41 /* */
42 /* **************************************************************** */
44 /* Invert the case of the COUNT following characters. */
45 invert_case_line (count, key)
46 int count, key;
48 register int start, end;
50 start = rl_point;
52 if (count < 0)
54 direction = -1;
55 count = -count;
57 else
58 direction = 1;
60 /* Find the end of the range to modify. */
61 end = start + (count * direction);
63 /* Force it to be within range. */
64 if (end > rl_end)
65 end = rl_end;
66 else if (end < 0)
67 end = -1;
69 if (start > end)
71 int temp = start;
72 start = end;
73 end = temp;
76 if (start == end)
77 return;
79 /* Tell readline that we are modifying the line, so save the undo
80 information. */
81 rl_modifying (start, end);
83 for (; start != end; start += direction)
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]);
91 /* Move point to on top of the last character changed. */
92 rl_point = end - direction;