Code

Merged branch 'master' of git://git.tokkee.org/sysdb.
[sysdb.git] / src / tools / sysdb / input.c
1 /*
2  * SysDB - src/tools/sysdb/input.c
3  * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "tools/sysdb/input.h"
33 #include "tools/sysdb/command.h"
35 #include "utils/strbuf.h"
37 #include <stdio.h>
38 #include <stdlib.h>
40 #include <string.h>
42 #if HAVE_EDITLINE_READLINE_H
43 #       include <editline/readline.h>
44 #       if HAVE_EDITLINE_HISTORY_H
45 #               include <editline/history.h>
46 #       endif
47 #elif HAVE_READLINE_READLINE_H
48 #       include <readline/readline.h>
49 #       if HAVE_READLINE_HISTORY_H
50 #               include <readline/history.h>
51 #       endif
52 #elif HAVE_READLINE_H
53 #       include <readline.h>
54 #       if HAVE_HISTORY_H
55 #               include <history.h>
56 #       endif
57 #endif /* READLINEs */
59 /*
60  * public variables
61  */
63 sdb_input_t *sysdb_input = NULL;
65 /*
66  * private helper functions
67  */
69 static size_t
70 input_readline(void)
71 {
72         const char *prompt = "sysdb=> ";
73         char *line;
75         size_t len;
77         if (sysdb_input->query_len)
78                 prompt = "sysdb-> ";
80         line = readline(prompt);
82         if (! line)
83                 return 0;
85         len = strlen(line) + 1;
87         sdb_strbuf_append(sysdb_input->input, line);
88         sdb_strbuf_append(sysdb_input->input, "\n");
89         free(line);
90         return len;
91 } /* input_readline */
93 /*
94  * public API
95  */
97 int
98 sdb_input_init(sdb_input_t *input)
99 {
100         /* register input handler */
101         sysdb_input = input;
102         return 0;
103 } /* sdb_input_init */
105 ssize_t
106 sdb_input_readline(char *buf, int *n_chars, size_t max_chars)
108         const char *data;
109         size_t len;
111         len = sdb_strbuf_len(sysdb_input->input) - sysdb_input->tokenizer_pos;
113         if (! len) {
114                 size_t n = input_readline();
115                 if (! n) {
116                         *n_chars = 0; /* YY_NULL */
117                         return 0;
118                 }
119                 len += n;
120         }
122         len = (len < max_chars) ? len : max_chars;
123         data = sdb_strbuf_string(sysdb_input->input);
124         data += sysdb_input->tokenizer_pos;
125         strncpy(buf, data, len);
127         sysdb_input->tokenizer_pos += len;
128         *n_chars = (int)len;
129         return (ssize_t)len;
130 } /* sdb_input_readline */
132 int
133 sdb_input_exec_query()
135         char *query = sdb_command_exec(sysdb_input);
137         HIST_ENTRY *current_hist;
138         const char *hist_line = NULL;
140         if (! query)
141                 return -1;
143         current_hist = current_history();
144         if (current_hist)
145                 hist_line = current_hist->line;
147         if (*query != ' ')
148                 if ((! hist_line) || strcmp(hist_line, query))
149                         add_history(query);
150         free(query);
151         return 0;
152 } /* sdb_input_exec_query */
154 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */