Touched all sources to ease import of readline 2.2.1
[external/binutils.git] / readline / examples / manexamp.c
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 do_gets ()
19 {
20   /* If the buffer has already been allocated, return the memory
21      to the free pool. */
22   if (line_read != (char *)NULL)
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 /*                                                                  */
41 /*        Writing a Function to be Called by Readline.              */
42 /*                                                                  */
43 /* **************************************************************** */
44
45 /* Invert the case of the COUNT following characters. */
46 invert_case_line (count, key)
47      int count, key;
48 {
49   register int start, end;
50
51   start = rl_point;
52
53   if (count < 0)
54     {
55       direction = -1;
56       count = -count;
57     }
58   else
59     direction = 1;
60       
61   /* Find the end of the range to modify. */
62   end = start + (count * direction);
63
64   /* Force it to be within range. */
65   if (end > rl_end)
66     end = rl_end;
67   else if (end < 0)
68     end = -1;
69
70   if (start > end)
71     {
72       int temp = start;
73       start = end;
74       end = temp;
75     }
76
77   if (start == end)
78     return;
79
80   /* Tell readline that we are modifying the line, so save the undo
81      information. */
82   rl_modifying (start, end);
83
84   for (; start != end; start += direction)
85     {
86       if (uppercase_p (rl_line_buffer[start]))
87         rl_line_buffer[start] = to_lower (rl_line_buffer[start]);
88       else if (lowercase_p (rl_line_buffer[start]))
89         rl_line_buffer[start] = to_upper (rl_line_buffer[start]);
90     }
91
92   /* Move point to on top of the last character changed. */
93   rl_point = end - direction;
94 }
95
96