示例#1
0
文件: history.c 项目: pyer/forth
/* Add the contents of FILENAME to the history list, a line at a time.
   If FILENAME is NULL, then read from ~/.history.  Returns 0 if
   successful, or errno if not. */
int read_history (void)
{
  return (read_history_range (NULL, 0, -1));
}
示例#2
0
char *getline()
{
    char *prompt;
    int len;
	char *home;
	int  fdh;

    /* If initialization hasn't been done, do it now:
     *  - We don't want TAB completion
     */
    if (!done_init) {
        rl_bind_key('\t', rl_insert);

		/* get history from file */
		home = getenv("FVWM_USERDIR");
		h_file = safemalloc(strlen(home) + sizeof(HISTFILE) + 1);
		strcpy(h_file, home);
		strcat(h_file, HISTFILE);
		if( access( h_file, F_OK)  < 0) {
		  /* if it doesn't exist create it */
		  fdh = creat( h_file, S_IRUSR | S_IWUSR );
		  if( fdh != -1 ) {
			close( fdh );
		  }
		} else {
		  read_history_range( h_file, 0, HISTSIZE );
		}
		done_init = 1;
  }

    /* Empty out the previous info */
    len = 0;
    *cmd = '\0';
    prompt = PS1;

    while (1) {
        int linelen = 0;

        /* If the buffer has already been allocated, free the memory. */
        if (line != (char *)NULL)
            free(line);

  /* Get a line from the user. */
        line  = readline(prompt);
        if (line == NULL)
            return (NULL);

        /* Make sure we have enough space for the new line */
        linelen = strlen(line);
        if (len + linelen > MAX_COMMAND_SIZE-2 ) {
		  fprintf( stderr, "line too long %d chars max %d \a\n",
				   len+linelen, MAX_COMMAND_SIZE-2 );
		  strncat(cmd, line, MAX_COMMAND_SIZE-len-2);
		  add_history(cmd);
		  break;
        }

        /* Copy the new line onto the end of the current line */
        strcat(cmd, line);

        /* If the current line doesn't end with a backslash, we're done */
        len = strlen(cmd);
        if (cmd[len-1] != '\\')
            break;

        /* Otherwise, remove it and wait for more (add a space if needed) */
        prompt = PS2;
        cmd[len-1] = (cmd[len-2]==' ' || cmd[len-2]=='\t') ? '\0' : ' ';
  }

    /* If the command has any text in it, save it on the history. */
    if (*cmd != '\0') {
	  add_history(cmd);
	  append_history( 1,h_file );
	  history_truncate_file( h_file, HISTSIZE );
	}

    cmd[len]   = '\n';
    cmd[len+1] = '\0';

    return (cmd);
}