manexamp.c revision 75406
1121259Sphk/* manexamp.c -- The examples which appear in the documentation are here. */ 2121259Sphk 3121259Sphk#include <stdio.h> 4121259Sphk#include <readline/readline.h> 5121259Sphk 6121259Sphk/* **************************************************************** */ 7121259Sphk/* */ 8121259Sphk/* How to Emulate gets () */ 9121259Sphk/* */ 10121259Sphk/* **************************************************************** */ 11121259Sphk 12121259Sphk/* A static variable for holding the line. */ 13121259Sphkstatic char *line_read = (char *)NULL; 14121259Sphk 15121259Sphk/* Read a string, and return a pointer to it. Returns NULL on EOF. */ 16121259Sphkchar * 17121259Sphkrl_gets () 18121259Sphk{ 19121259Sphk /* If the buffer has already been allocated, return the memory 20121259Sphk to the free pool. */ 21121259Sphk if (line_read) 22121259Sphk { 23121259Sphk free (line_read); 24121259Sphk line_read = (char *)NULL; 25121259Sphk } 26121259Sphk 27121259Sphk /* Get a line from the user. */ 28121259Sphk line_read = readline (""); 29121259Sphk 30121259Sphk /* If the line has any text in it, save it on the history. */ 31121259Sphk if (line_read && *line_read) 32121259Sphk add_history (line_read); 33121259Sphk 34121259Sphk return (line_read); 35121259Sphk} 36121259Sphk 37175360Ssobomax/* **************************************************************** */ 38121259Sphk/* */ 39121259Sphk/* Writing a Function to be Called by Readline. */ 40121259Sphk/* */ 41121259Sphk/* **************************************************************** */ 42121259Sphk 43121259Sphk/* Invert the case of the COUNT following characters. */ 44121259Sphkinvert_case_line (count, key) 45121259Sphk int count, key; 46121259Sphk{ 47121259Sphk register int start, end; 48121259Sphk 49121259Sphk start = rl_point; 50121259Sphk 51121259Sphk if (count < 0) 52121259Sphk { 53121259Sphk direction = -1; 54121259Sphk count = -count; 55121259Sphk } 56121259Sphk else 57121259Sphk direction = 1; 58121259Sphk 59121259Sphk /* Find the end of the range to modify. */ 60121259Sphk end = start + (count * direction); 61121259Sphk 62121259Sphk /* Force it to be within range. */ 63121259Sphk if (end > rl_end) 64121259Sphk end = rl_end; 65121259Sphk else if (end < 0) 66121259Sphk end = -1; 67121259Sphk 68121259Sphk if (start > end) 69121259Sphk { 70121259Sphk int temp = start; 71121259Sphk start = end; 72121259Sphk end = temp; 73121259Sphk } 74121259Sphk 75121259Sphk 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} 93