xref: /openbsd-src/gnu/lib/libreadline/examples/manexamp.c (revision 15b117eae8ea0caffec2fbd5cbd366a5ccee20ab)
11acd27e7Smillert /* manexamp.c -- The examples which appear in the documentation are here. */
21acd27e7Smillert 
3*15b117eaSkettenis /* Copyright (C) 1987-2002 Free Software Foundation, Inc.
4*15b117eaSkettenis 
5*15b117eaSkettenis    This file is part of the GNU Readline Library, a library for
6*15b117eaSkettenis    reading lines of text with interactive input and history editing.
7*15b117eaSkettenis 
8*15b117eaSkettenis    The GNU Readline Library is free software; you can redistribute it
9*15b117eaSkettenis    and/or modify it under the terms of the GNU General Public License
10*15b117eaSkettenis    as published by the Free Software Foundation; either version 2, or
11*15b117eaSkettenis    (at your option) any later version.
12*15b117eaSkettenis 
13*15b117eaSkettenis    The GNU Readline Library is distributed in the hope that it will be
14*15b117eaSkettenis    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15*15b117eaSkettenis    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*15b117eaSkettenis    GNU General Public License for more details.
17*15b117eaSkettenis 
18*15b117eaSkettenis    The GNU General Public License is often shipped with GNU software, and
19*15b117eaSkettenis    is generally kept in a file called COPYING or LICENSE.  If you do not
20*15b117eaSkettenis    have a copy of the license, write to the Free Software Foundation,
21*15b117eaSkettenis    59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22*15b117eaSkettenis 
231acd27e7Smillert #include <stdio.h>
241acd27e7Smillert #include <readline/readline.h>
251acd27e7Smillert 
261acd27e7Smillert /* **************************************************************** */
271acd27e7Smillert /*                                                                  */
28*15b117eaSkettenis /*   			How to Emulate gets ()			    */
291acd27e7Smillert /*                                                                  */
301acd27e7Smillert /* **************************************************************** */
311acd27e7Smillert 
321acd27e7Smillert /* A static variable for holding the line. */
331acd27e7Smillert static char *line_read = (char *)NULL;
341acd27e7Smillert 
351acd27e7Smillert /* Read a string, and return a pointer to it.  Returns NULL on EOF. */
361acd27e7Smillert char *
rl_gets()371acd27e7Smillert rl_gets ()
381acd27e7Smillert {
391acd27e7Smillert   /* If the buffer has already been allocated, return the memory
401acd27e7Smillert      to the free pool. */
411acd27e7Smillert   if (line_read)
421acd27e7Smillert     {
431acd27e7Smillert       free (line_read);
441acd27e7Smillert       line_read = (char *)NULL;
451acd27e7Smillert     }
461acd27e7Smillert 
471acd27e7Smillert   /* Get a line from the user. */
481acd27e7Smillert   line_read = readline ("");
491acd27e7Smillert 
501acd27e7Smillert   /* If the line has any text in it, save it on the history. */
511acd27e7Smillert   if (line_read && *line_read)
521acd27e7Smillert     add_history (line_read);
531acd27e7Smillert 
541acd27e7Smillert   return (line_read);
551acd27e7Smillert }
561acd27e7Smillert 
571acd27e7Smillert /* **************************************************************** */
581acd27e7Smillert /*                                                                  */
591acd27e7Smillert /*        Writing a Function to be Called by Readline.              */
601acd27e7Smillert /*                                                                  */
611acd27e7Smillert /* **************************************************************** */
621acd27e7Smillert 
631acd27e7Smillert /* Invert the case of the COUNT following characters. */
invert_case_line(count,key)641acd27e7Smillert invert_case_line (count, key)
651acd27e7Smillert      int count, key;
661acd27e7Smillert {
671acd27e7Smillert   register int start, end;
681acd27e7Smillert 
691acd27e7Smillert   start = rl_point;
701acd27e7Smillert 
711acd27e7Smillert   if (count < 0)
721acd27e7Smillert     {
731acd27e7Smillert       direction = -1;
741acd27e7Smillert       count = -count;
751acd27e7Smillert     }
761acd27e7Smillert   else
771acd27e7Smillert     direction = 1;
781acd27e7Smillert 
791acd27e7Smillert   /* Find the end of the range to modify. */
801acd27e7Smillert   end = start + (count * direction);
811acd27e7Smillert 
821acd27e7Smillert   /* Force it to be within range. */
831acd27e7Smillert   if (end > rl_end)
841acd27e7Smillert     end = rl_end;
851acd27e7Smillert   else if (end < 0)
861acd27e7Smillert     end = -1;
871acd27e7Smillert 
881acd27e7Smillert   if (start > end)
891acd27e7Smillert     {
901acd27e7Smillert       int temp = start;
911acd27e7Smillert       start = end;
921acd27e7Smillert       end = temp;
931acd27e7Smillert     }
941acd27e7Smillert 
951acd27e7Smillert   if (start == end)
961acd27e7Smillert     return;
971acd27e7Smillert 
981acd27e7Smillert   /* Tell readline that we are modifying the line, so save the undo
991acd27e7Smillert      information. */
1001acd27e7Smillert   rl_modifying (start, end);
1011acd27e7Smillert 
1021acd27e7Smillert   for (; start != end; start += direction)
1031acd27e7Smillert     {
104*15b117eaSkettenis       if (_rl_uppercase_p (rl_line_buffer[start]))
105*15b117eaSkettenis 	rl_line_buffer[start] = _rl_to_lower (rl_line_buffer[start]);
106*15b117eaSkettenis       else if (_rl_lowercase_p (rl_line_buffer[start]))
107*15b117eaSkettenis 	rl_line_buffer[start] = _rl_to_upper (rl_line_buffer[start]);
1081acd27e7Smillert     }
1091acd27e7Smillert 
1101acd27e7Smillert   /* Move point to on top of the last character changed. */
1111acd27e7Smillert   rl_point = end - direction;
1121acd27e7Smillert }
113