2
* Copyright (C) 2013 Intel Corporation
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
25
/* Very simple history storage for easy usage of tool */
27
#define HISTORY_DEPTH 40
29
static char lines[HISTORY_DEPTH][LINE_SIZE];
30
static int last_line = 0;
31
static int history_size = 0;
33
/* TODO: Storing history not implemented yet */
34
void history_store(const char *filename)
38
/* Restoring history from file */
39
void history_restore(const char *filename)
42
FILE *f = fopen(filename, "rt");
48
if (fgets(line, 1000, f) != NULL) {
51
while (l > 0 && isspace(line[--l]))
55
history_add_line(line);
63
/* Add new line to history buffer */
64
void history_add_line(const char *line)
66
if (line == NULL || strlen(line) == 0)
69
if (strcmp(line, lines[last_line]) == 0)
72
last_line = (last_line + 1) % HISTORY_DEPTH;
73
strncpy(&lines[last_line][0], line, LINE_SIZE - 1);
74
if (history_size < HISTORY_DEPTH)
79
* Get n-th line from history
82
* return -1 if there is no such line
84
int history_get_line(int n, char *buf, int buf_size)
89
if (n >= history_size || buf_size == 0 || n < 0)
93
&lines[(HISTORY_DEPTH + last_line - n) % HISTORY_DEPTH][0],
95
buf[buf_size - 1] = 0;