2014-05-13 08:45:59 +00:00
|
|
|
/**
|
|
|
|
* rofi
|
|
|
|
*
|
|
|
|
* MIT/X11 License
|
|
|
|
* Copyright 2013-2014 Qball Cow <qball@gmpclient.org>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include "rofi.h"
|
|
|
|
#include "history.h"
|
|
|
|
|
2014-05-22 07:33:32 +00:00
|
|
|
#define HISTORY_NAME_LENGTH 256
|
|
|
|
#define HISTORY_MAX_ENTRIES 25
|
2014-05-13 08:45:59 +00:00
|
|
|
|
|
|
|
typedef struct __element
|
|
|
|
{
|
|
|
|
long int index;
|
|
|
|
char name[HISTORY_NAME_LENGTH];
|
|
|
|
}_element;
|
|
|
|
|
|
|
|
static int __element_sort_func ( const void *ea, const void *eb )
|
|
|
|
{
|
|
|
|
_element *a = *(_element * *) ea;
|
|
|
|
_element *b = *(_element * *) eb;
|
|
|
|
return b->index - a->index;
|
|
|
|
}
|
|
|
|
|
2014-05-22 07:33:32 +00:00
|
|
|
static void __history_write_element_list ( FILE *fd, _element **list, unsigned int length )
|
2014-05-13 08:45:59 +00:00
|
|
|
{
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( list == NULL ) {
|
2014-05-13 08:45:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Sort the list before writing out.
|
|
|
|
qsort ( list, length, sizeof ( _element* ), __element_sort_func );
|
|
|
|
|
2014-05-25 15:55:27 +00:00
|
|
|
// Get minimum index.
|
2014-05-25 21:32:06 +00:00
|
|
|
int min_value = list[length - 1]->index;
|
2014-05-25 15:55:27 +00:00
|
|
|
|
2014-05-13 08:45:59 +00:00
|
|
|
// Set the max length of the list.
|
2014-05-22 07:33:32 +00:00
|
|
|
length = ( length > HISTORY_MAX_ENTRIES ) ? HISTORY_MAX_ENTRIES : length;
|
2014-05-13 08:45:59 +00:00
|
|
|
|
|
|
|
// Write out entries.
|
2014-06-04 19:29:23 +00:00
|
|
|
for ( unsigned int iter = 0; iter < length; iter++ ) {
|
2014-05-25 21:32:06 +00:00
|
|
|
fprintf ( fd, "%ld %s\n", list[iter]->index - min_value, list[iter]->name );
|
2014-05-22 07:33:32 +00:00
|
|
|
}
|
2014-05-13 08:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
|
|
|
|
{
|
2014-05-22 07:33:32 +00:00
|
|
|
char buffer[HISTORY_NAME_LENGTH + 16];
|
2014-05-13 08:45:59 +00:00
|
|
|
_element **retv = NULL;
|
|
|
|
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( length == NULL ) {
|
2014-05-13 08:45:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*length = 0;
|
|
|
|
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( fd == NULL ) {
|
2014-05-13 08:45:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-06-04 19:29:23 +00:00
|
|
|
while ( fgets ( buffer, HISTORY_NAME_LENGTH + 16, fd ) != NULL ) {
|
2014-05-17 21:06:45 +00:00
|
|
|
char * start = NULL;
|
2014-05-13 08:45:59 +00:00
|
|
|
// Skip empty lines.
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( strlen ( buffer ) == 0 ) {
|
2014-05-13 08:45:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-05-26 07:19:58 +00:00
|
|
|
// Resize and check.
|
|
|
|
_element **tr = realloc ( retv, ( *length + 2 ) * sizeof ( _element* ) );
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( tr == NULL ) {
|
2014-05-26 07:19:58 +00:00
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
retv = tr;
|
|
|
|
|
2014-05-22 07:33:32 +00:00
|
|
|
retv[( *length )] = malloc ( sizeof ( _element ) );
|
2014-05-13 08:45:59 +00:00
|
|
|
// remove trailing \n
|
|
|
|
buffer[strlen ( buffer ) - 1] = '\0';
|
|
|
|
// Parse the number of times.
|
2014-05-22 07:33:32 +00:00
|
|
|
retv[( *length )]->index = strtol ( buffer, &start, 10 );
|
|
|
|
strncpy ( retv[( *length )]->name, ( start + 1 ), HISTORY_NAME_LENGTH );
|
2014-05-13 08:45:59 +00:00
|
|
|
// Force trailing '\0'
|
2014-05-22 07:33:32 +00:00
|
|
|
retv[( *length )]->name[HISTORY_NAME_LENGTH - 1] = '\0';
|
|
|
|
retv[( *length ) + 1] = NULL;
|
2014-05-13 08:45:59 +00:00
|
|
|
|
2014-05-22 07:33:32 +00:00
|
|
|
( *length )++;
|
2014-05-13 08:45:59 +00:00
|
|
|
}
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void history_set ( const char *filename, const char *entry )
|
|
|
|
{
|
2014-06-05 19:55:47 +00:00
|
|
|
if ( config.disable_history ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-22 07:33:32 +00:00
|
|
|
int found = 0;
|
2014-05-13 08:45:59 +00:00
|
|
|
unsigned int curr = 0;
|
|
|
|
unsigned int length = 0;
|
2014-05-22 07:33:32 +00:00
|
|
|
_element **list = NULL;
|
2014-05-13 08:45:59 +00:00
|
|
|
// Open file for reading and writing.
|
2014-05-22 07:33:32 +00:00
|
|
|
FILE *fd = fopen ( filename, "a+" );
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( fd == NULL ) {
|
2014-05-22 07:33:32 +00:00
|
|
|
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
|
|
|
|
return;
|
2014-05-13 08:45:59 +00:00
|
|
|
}
|
|
|
|
// Get list.
|
2014-05-22 07:33:32 +00:00
|
|
|
list = __history_get_element_list ( fd, &length );
|
|
|
|
|
|
|
|
// Look if the entry exists.
|
2014-06-04 19:29:23 +00:00
|
|
|
for ( unsigned int iter = 0; !found && iter < length; iter++ ) {
|
|
|
|
if ( strcmp ( list[iter]->name, entry ) == 0 ) {
|
2014-05-22 07:33:32 +00:00
|
|
|
curr = iter;
|
2014-05-13 08:45:59 +00:00
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( found ) {
|
2014-05-13 08:45:59 +00:00
|
|
|
// If exists, increment list index number
|
|
|
|
list[curr]->index++;
|
2014-05-22 07:33:32 +00:00
|
|
|
}
|
2014-06-04 19:29:23 +00:00
|
|
|
else{
|
2014-05-13 08:45:59 +00:00
|
|
|
// If not exists, add it.
|
|
|
|
// Increase list by one
|
2014-05-26 07:19:58 +00:00
|
|
|
_element **tr = realloc ( list, ( length + 2 ) * sizeof ( _element* ) );
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( tr != NULL ) {
|
2014-05-26 07:19:58 +00:00
|
|
|
list = tr;
|
|
|
|
list[length] = malloc ( sizeof ( _element ) );
|
|
|
|
// Copy name
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( list[length] != NULL ) {
|
2014-05-26 07:19:58 +00:00
|
|
|
strncpy ( list[length]->name, entry, HISTORY_NAME_LENGTH );
|
|
|
|
list[length]->name[HISTORY_NAME_LENGTH - 1] = '\0';
|
|
|
|
// set # hits
|
|
|
|
list[length]->index = 1;
|
|
|
|
|
|
|
|
length++;
|
|
|
|
list[length] = NULL;
|
|
|
|
}
|
|
|
|
}
|
2014-05-13 08:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rewind.
|
2014-05-22 07:33:32 +00:00
|
|
|
fseek ( fd, 0L, SEEK_SET );
|
2014-05-13 08:45:59 +00:00
|
|
|
// Clear file.
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( ftruncate ( fileno ( fd ), 0 ) == 0 ) {
|
2014-05-13 20:11:42 +00:00
|
|
|
// Write list.
|
2014-05-22 07:33:32 +00:00
|
|
|
__history_write_element_list ( fd, list, length );
|
|
|
|
}
|
2014-06-04 19:29:23 +00:00
|
|
|
else{
|
2014-05-22 07:33:32 +00:00
|
|
|
fprintf ( stderr, "Failed to truncate file: %s\n", strerror ( errno ) );
|
2014-05-13 20:11:42 +00:00
|
|
|
}
|
2014-05-13 08:45:59 +00:00
|
|
|
// Free the list.
|
2014-06-04 19:29:23 +00:00
|
|
|
for ( unsigned int iter = 0; iter < length; iter++ ) {
|
2014-05-26 07:19:58 +00:00
|
|
|
free ( list[iter] );
|
2014-05-13 08:45:59 +00:00
|
|
|
}
|
2014-05-26 07:19:58 +00:00
|
|
|
free ( list );
|
2014-05-13 08:45:59 +00:00
|
|
|
// Close file.
|
2014-05-22 07:33:32 +00:00
|
|
|
fclose ( fd );
|
2014-05-13 08:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void history_remove ( const char *filename, const char *entry )
|
|
|
|
{
|
2014-06-05 19:55:47 +00:00
|
|
|
if ( config.disable_history ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-22 07:33:32 +00:00
|
|
|
_element ** list = NULL;
|
|
|
|
int found = 0;
|
|
|
|
unsigned int curr = 0;
|
|
|
|
unsigned int length = 0;
|
2014-05-13 08:45:59 +00:00
|
|
|
// Open file for reading and writing.
|
2014-05-22 07:33:32 +00:00
|
|
|
FILE *fd = fopen ( filename, "a+" );
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( fd == NULL ) {
|
2014-05-22 07:33:32 +00:00
|
|
|
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
|
|
|
|
return;
|
2014-05-13 08:45:59 +00:00
|
|
|
}
|
|
|
|
// Get list.
|
2014-05-22 07:33:32 +00:00
|
|
|
list = __history_get_element_list ( fd, &length );
|
|
|
|
|
|
|
|
// Find entry.
|
2014-06-04 19:29:23 +00:00
|
|
|
for ( unsigned int iter = 0; !found && iter < length; iter++ ) {
|
|
|
|
if ( strcmp ( list[iter]->name, entry ) == 0 ) {
|
2014-05-22 07:33:32 +00:00
|
|
|
curr = iter;
|
2014-05-13 08:45:59 +00:00
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If found, remove it and write out new file.
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( found ) {
|
2014-05-13 08:45:59 +00:00
|
|
|
// Remove the entry.
|
2014-05-22 07:33:32 +00:00
|
|
|
free ( list[curr] );
|
2014-05-13 08:45:59 +00:00
|
|
|
// Swap last to here (if list is size 1, we just swap empty sets).
|
2014-05-22 07:33:32 +00:00
|
|
|
list[curr] = list[length - 1];
|
2014-05-13 08:45:59 +00:00
|
|
|
// Empty last.
|
2014-05-22 07:33:32 +00:00
|
|
|
list[length - 1] = NULL;
|
2014-05-13 08:45:59 +00:00
|
|
|
length--;
|
|
|
|
|
|
|
|
// Rewind.
|
2014-05-22 07:33:32 +00:00
|
|
|
fseek ( fd, 0L, SEEK_SET );
|
2014-05-13 08:45:59 +00:00
|
|
|
// Clear list.
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( ftruncate ( fileno ( fd ), 0 ) == 0 ) {
|
2014-05-13 20:11:42 +00:00
|
|
|
// Write list.
|
2014-05-22 07:33:32 +00:00
|
|
|
__history_write_element_list ( fd, list, length );
|
|
|
|
}
|
2014-06-04 19:29:23 +00:00
|
|
|
else{
|
2014-05-22 07:33:32 +00:00
|
|
|
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
|
2014-05-13 20:11:42 +00:00
|
|
|
}
|
2014-05-13 08:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Free the list.
|
2014-06-04 19:29:23 +00:00
|
|
|
for ( unsigned int iter = 0; iter < length; iter++ ) {
|
2014-05-27 06:42:21 +00:00
|
|
|
free ( list[iter] );
|
2014-05-22 07:33:32 +00:00
|
|
|
}
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( list != NULL ) {
|
2014-05-22 07:33:32 +00:00
|
|
|
free ( list );
|
2014-05-13 08:45:59 +00:00
|
|
|
}
|
|
|
|
// Close file.
|
2014-05-22 07:33:32 +00:00
|
|
|
fclose ( fd );
|
2014-05-13 08:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char ** history_get_list ( const char *filename, unsigned int *length )
|
|
|
|
{
|
2014-06-05 19:55:47 +00:00
|
|
|
if ( config.disable_history ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-17 21:06:45 +00:00
|
|
|
_element **list = NULL;
|
2014-05-22 07:33:32 +00:00
|
|
|
char **retv = NULL;
|
2014-05-13 08:45:59 +00:00
|
|
|
// Open file.
|
2014-05-22 07:33:32 +00:00
|
|
|
FILE *fd = fopen ( filename, "r" );
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( fd == NULL ) {
|
2014-05-27 06:42:21 +00:00
|
|
|
// File that does not exists is not an error, so ignore it.
|
|
|
|
// Everything else? panic.
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( errno != ENOENT ) {
|
2014-05-22 07:33:32 +00:00
|
|
|
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
|
2014-05-13 08:45:59 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Get list.
|
2014-05-22 07:33:32 +00:00
|
|
|
list = __history_get_element_list ( fd, length );
|
2014-05-13 08:45:59 +00:00
|
|
|
|
2014-05-22 07:33:32 +00:00
|
|
|
// Copy list in right format.
|
2014-05-27 06:42:21 +00:00
|
|
|
// Lists are always short, so performance should not be an issue.
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( ( *length ) > 0 ) {
|
2014-05-22 07:33:32 +00:00
|
|
|
retv = malloc ( ( ( *length ) + 1 ) * sizeof ( char * ) );
|
2014-06-04 19:29:23 +00:00
|
|
|
for ( int iter = 0; iter < ( *length ); iter++ ) {
|
2014-05-22 07:33:32 +00:00
|
|
|
retv[iter] = strdup ( list[iter]->name );
|
|
|
|
free ( list[iter] );
|
2014-05-13 08:45:59 +00:00
|
|
|
}
|
2014-05-22 07:33:32 +00:00
|
|
|
retv[( *length )] = NULL;
|
|
|
|
free ( list );
|
2014-05-13 08:45:59 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 07:33:32 +00:00
|
|
|
fclose ( fd );
|
2014-05-13 08:45:59 +00:00
|
|
|
return retv;
|
|
|
|
}
|