1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
|
/*
* history.c: stuff to handle command line history
*
*
* Written By Michael Sandrof
*
* Copyright(c) 1990
*
* See the COPYRIGHT file, or do a HELP IRCII COPYRIGHT
*/
#ifndef lint
static char rcsid[] = "@(#)$Id: history.c,v 1.1.1.1 1999/07/16 21:21:42 toast Exp $";
#endif
#include "irc.h"
#include "ircaux.h"
#include "vars.h"
#include "history.h"
#include "output.h"
#include "input.h"
static int parse_history();
static FILE *hist_file = (FILE *)NULL;
typedef struct HistoryStru
{
int number;
char *stuff;
struct HistoryStru *next;
struct HistoryStru *prev;
} History;
/* command_history: pointer to head of command_history list */
static History *command_history_head = (History *)NULL;
static History *command_history_tail = (History *)NULL;
static History *command_history_pos = (History *)NULL;
/* hist_size: the current size of the command_history array */
static int hist_size = 0;
/* hist_count: the absolute counter for the history list */
static int hist_count = 0;
/*
* last_dir: the direction (next or previous) of the last get_from_history()
* call.... reset by add to history
*/
static int last_dir = -1;
/*
* file_pos: The position in the history file of the last history entry
* zwooped out by get_from_history_file()... look there for how this is used
*/
static off_t file_pos = 0;
/*
* history pointer
*/
static History *tmp = (History *)NULL;
/*
* history_match: using wild_match(), this finds the latest match in the
* history file and returns it as the function result. Returns null if there
* is no match. Note that this sticks a '*' at the end if one is not already
* there.
*/
static char *
history_match(match)
char *match;
{
char *ptr;
char *match_str = (char *)NULL;
if (*(match + strlen(match) - 1) == '*')
malloc_strcpy(&match_str, match);
else
{
match_str = (char *) new_malloc(strlen(match) + 2);
strcpy(match_str, match);
strcat(match_str, "*");
}
if (hist_file)
{
if (last_dir == -1)
fseek(hist_file, 0L, 2);
else
fseek(hist_file, file_pos, 0);
while (rfgets(buffer, BIG_BUFFER_SIZE, hist_file))
{
parse_history(buffer, &ptr);
if (wild_match(match_str, ptr))
{
new_free(&match_str);
*(ptr + strlen(ptr) - 1) = '\0';
file_pos = ftell(hist_file);
last_dir = PREV;
return (ptr);
}
}
file_pos = 0;
}
if (!hist_file && get_int_var(HISTORY_VAR))
{
if ((last_dir == -1) || (tmp == (History *)NULL))
tmp = command_history_head;
else
tmp = tmp->next;
for (; tmp; tmp = tmp->next)
if (wild_match(match_str, tmp->stuff))
{
new_free(&match_str);
last_dir = PREV;
return (tmp->stuff);
}
}
last_dir = -1;
new_free(&match_str);
return (char *)NULL;
}
/*
* add_to_history_file: This adds the given line of text to the end of the
* history file using cnt as the history index number.
*/
static void
add_to_history_file(cnt, line)
int cnt;
char *line;
{
if (hist_file)
{
fseek(hist_file, 0L, 2);
fprintf(hist_file, "%d: %s\n", cnt, line);
fflush(hist_file);
file_pos = ftell(hist_file);
}
}
static void
add_to_history_list(cnt, stuff)
int cnt;
char *stuff;
{
History *new;
if (get_int_var(HISTORY_VAR) == 0)
return;
if ((hist_size == get_int_var(HISTORY_VAR)) && command_history_tail)
{
if (hist_size == 1)
{
malloc_strcpy(&command_history_tail->stuff, stuff);
return;
}
new = command_history_tail;
command_history_tail = command_history_tail->prev;
command_history_tail->next = (History *)NULL;
new_free(&new->stuff);
new_free(&new);
if (command_history_tail == (History *)NULL)
command_history_head = (History *)NULL;
}
else
hist_size++;
new = (History *) new_malloc(sizeof(History));
new->stuff = (char *)NULL;
new->number = cnt;
new->next = command_history_head;
new->prev = (History *)NULL;
malloc_strcpy(&(new->stuff), stuff);
if (command_history_head)
command_history_head->prev = new;
command_history_head = new;
if (command_history_tail == (History *)NULL)
command_history_tail = new;
command_history_pos = (History *)NULL;
}
/*
* set_history_file: this sets the file to be used by the command history
* function to whatever you send as file. This expands twiddles and opens
* the file if all is well
*/
void
set_history_file(file)
char *file;
{
char *ptr;
int i,
cnt;
History *tmp;
if (file)
{
#ifdef DAEMON_UID
if (getuid() == DAEMON_UID)
{
say("You are not permitted to use a HISTORY_FILE");
set_string_var(HISTORY_FILE_VAR, (char *)NULL);
return;
}
#endif /* DAEMON_UID */
if ((ptr = expand_twiddle(file)) == (char *)NULL)
{
say("Bad filename: %s",file);
set_string_var(HISTORY_FILE_VAR, (char *)NULL);
return;
}
set_string_var(HISTORY_FILE_VAR, ptr);
if (hist_file)
fclose(hist_file);
if ((hist_file = fopen(ptr, "w+")) == (FILE *)NULL)
{
say("Unable to open %s: %s", ptr, strerror(errno));
set_string_var(HISTORY_FILE_VAR, (char *)NULL);
hist_file = (FILE *)NULL;
}
else if (hist_size)
{
cnt = hist_size;
tmp = command_history_tail;
for (i = 0; i < cnt; i++, tmp = tmp->prev)
add_to_history_file(tmp->number, tmp->stuff);
}
new_free(&ptr);
}
else if (hist_file)
{
fclose(hist_file);
hist_file = (FILE *)NULL;
}
}
/*
* set_history_size: adjusts the size of the command_history to be size. If
* the array is not yet allocated, it is set to size and all the entries
* nulled. If it exists, it is resized to the new size with a realloc. Any
* new entries are nulled.
*/
void
set_history_size(size)
int size;
{
int i,
cnt;
History *ptr;
if (size < hist_size)
{
cnt = hist_size - size;
for (i = 0; i < cnt; i++)
{
ptr = command_history_tail;
command_history_tail = ptr->prev;
new_free(&(ptr->stuff));
new_free(&ptr);
}
if (command_history_tail == (History *)NULL)
command_history_head = (History *)NULL;
else
command_history_tail->next = (History *)NULL;
hist_size = size;
}
}
/*
* parse_history: given a string of the form "number: stuff", this returns
* the number as an integer and points ret to stuff
*/
static int
parse_history(buffer, ret)
char *buffer;
char **ret;
{
char *ptr;
int entry;
if ((ptr = index(buffer, ':')) != NULL)
{
entry = atoi(buffer);
*ret = ptr + 2;
return (entry);
}
*ret = (char *)NULL;
return -1;
}
/*
* add_to_history: adds the given line to the history array. The history
* array is a circular buffer, and add_to_history handles all that stuff. It
* automagically allocted and deallocated memory as needed
*/
void
add_to_history(line)
char *line;
{
char *ptr;
if (line && *line)
while (line)
{
if ((ptr = sindex(line, "\n\r")) != NULL)
*(ptr++) = '\0';
add_to_history_list(hist_count, line);
add_to_history_file(hist_count, line);
last_dir = PREV;
hist_count++;
line = ptr;
}
}
static char *
get_from_history_file(which)
int which;
{
char *ptr;
if (last_dir == -1)
last_dir = which;
else if (last_dir != which)
{
last_dir = which;
get_from_history(which);
}
fseek(hist_file, file_pos, 0);
if (which == NEXT)
{
if (!fgets(buffer, BIG_BUFFER_SIZE, hist_file))
{
file_pos = 0L;
fseek(hist_file, 0L, 0);
if (!fgets(buffer, BIG_BUFFER_SIZE, hist_file))
return (char *)NULL;
}
}
else if (!rfgets(buffer, BIG_BUFFER_SIZE, hist_file))
{
fseek(hist_file, 0L, 2);
file_pos = ftell(hist_file);
if (!rfgets(buffer, BIG_BUFFER_SIZE, hist_file))
return (char *)NULL;
}
file_pos = ftell(hist_file);
buffer[strlen(buffer) - 1] = '\0';
parse_history(buffer, &ptr);
return (ptr);
}
static char *
get_from_history_buffer(which)
int which;
{
if ((get_int_var(HISTORY_VAR) == 0) || (hist_size == 0))
return (char *)NULL;
/*
* if (last_dir != which) { last_dir = which; get_from_history(which); }
*/
if (which == NEXT)
{
if (command_history_pos)
{
if (command_history_pos->prev)
command_history_pos = command_history_pos->prev;
else
command_history_pos = command_history_tail;
}
else
{
add_to_history(get_input());
command_history_pos = command_history_tail;
}
return (command_history_pos->stuff);
}
else
{
if (command_history_pos)
{
if (command_history_pos->next)
command_history_pos = command_history_pos->next;
else
command_history_pos = command_history_head;
}
else
{
add_to_history(get_input());
command_history_pos = command_history_head;
}
return (command_history_pos->stuff);
}
}
char *
get_from_history(which)
int which;
{
char *str = (char *)NULL;
if (get_string_var(HISTORY_FILE_VAR))
str = get_from_history_file(which);
if (!str)
str = get_from_history_buffer(which);
return str;
}
/* history: the /HISTORY command, shows the command history buffer. */
/*ARGSUSED*/
void
history(command, args, subargs)
char *command,
*args,
*subargs;
{
int cnt,
max;
char *value;
History *tmp;
say("Command History:");
if (hist_file)
{
if ((value = next_arg(args, &args)) != NULL)
{
if ((max = atoi(value)) > hist_count)
max = 0;
else
max = hist_count - max + 1;
}
else
max = 0;
fseek(hist_file, 0L, 0);
while (--max > 0)
fgets(buffer, BIG_BUFFER_SIZE, hist_file);
while (fgets(buffer, BIG_BUFFER_SIZE, hist_file))
{
buffer[strlen(buffer) - 1] = '\0';
put_it("%s", buffer);
}
}
else if (get_int_var(HISTORY_VAR))
{
if ((value = next_arg(args, &args)) != NULL)
{
if ((max = atoi(value)) > get_int_var(HISTORY_VAR))
max = get_int_var(HISTORY_VAR);
}
else
max = get_int_var(HISTORY_VAR);
for (tmp = command_history_tail, cnt = 0; tmp && (cnt < max);
tmp = tmp->prev, cnt++)
put_it("%d: %s", tmp->number, tmp->stuff);
}
}
/*
* do_history: This finds the given history entry in either the history file,
* or the in memory history buffer (if no history file is given). It then
* returns the found entry as its function value, or null if the entry is not
* found for some reason. Note that this routine mallocs the string returned
*/
char *
do_history(com, rest)
char *com,
*rest;
{
int hist_num;
char *ptr,
*ret = (char *)NULL;
static char *last_com = (char *)NULL;
if (!com || !*com)
{
if (last_com)
com = last_com;
else
com = empty_string;
}
else
/* last_dir = -1; */
malloc_strcpy(&last_com, com);
if (!is_number(com))
{
if ((ptr = history_match(com)) != NULL)
{
if (rest && *rest)
{
ret = (char *) new_malloc(strlen(ptr) +
strlen(rest) + 2);
strcpy(ret, ptr);
strcat(ret, " ");
strcat(ret, rest);
}
else
malloc_strcpy(&ret, ptr);
}
else
say("No Match");
return (ret);
}
hist_num = atoi(com);
if (hist_file)
{
fseek(hist_file, 0L, 0);
while (fgets(buffer, BIG_BUFFER_SIZE, hist_file))
if (parse_history(buffer, &ptr) == hist_num)
{
*(ptr + strlen(ptr) - 1) = '\0';
if (rest && *rest)
{
ret = (char *) new_malloc(strlen(ptr)
+ strlen(rest) + 2);
strcpy(ret, ptr);
strcat(ret, " ");
strcat(ret, rest);
}
else
malloc_strcpy(&ret, ptr);
last_dir = PREV;
file_pos = ftell(hist_file);
return (ret);
}
say("No such history entry: %d", hist_num);
file_pos = 0;
}
else
{
History *tmp;
for (tmp = command_history_head; tmp; tmp = tmp->next)
if (tmp->number == hist_num)
{
if (rest && *rest)
{
ret = (char *)
new_malloc(strlen(tmp->stuff) +
strlen(rest) + 2);
strcpy(ret, tmp->stuff);
strcat(ret, " ");
strcat(ret, rest);
}
else
malloc_strcpy(&ret, tmp->stuff);
return (ret);
}
say("No such history entry: %d", hist_num);
}
return (char *)NULL;
}
|