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 * private helper functions
61 */
63 static size_t
64 input_readline(sdb_input_t *input)
65 {
66 const char *prompt = "sysdb=> ";
67 char *line;
69 size_t len;
71 if (input->query_len)
72 prompt = "sysdb-> ";
74 line = readline(prompt);
76 if (! line)
77 return 0;
79 len = strlen(line) + 1;
81 sdb_strbuf_append(input->input, line);
82 sdb_strbuf_append(input->input, "\n");
83 free(line);
84 return len;
85 } /* input_readline */
87 /*
88 * API
89 */
91 ssize_t
92 sdb_input_readline(sdb_input_t *input, char *buf,
93 int *n_chars, size_t max_chars)
94 {
95 size_t len;
97 len = sdb_strbuf_len(input->input) - input->tokenizer_pos;
99 if (! len) {
100 size_t n = input_readline(input);
101 if (! n) {
102 *n_chars = 0; /* YY_NULL */
103 return 0;
104 }
105 len += n;
106 }
108 len = (len < max_chars) ? len : max_chars;
109 strncpy(buf, sdb_strbuf_string(input->input) + input->tokenizer_pos, len);
110 input->tokenizer_pos += len;
111 *n_chars = (int)len;
113 return (ssize_t)len;
114 } /* sdb_input_readline */
116 int
117 sdb_input_exec_query(sdb_input_t *input)
118 {
119 char *query = sdb_command_exec(input);
121 HIST_ENTRY *current_hist;
122 const char *hist_line = NULL;
124 if (! query)
125 return -1;
127 current_hist = current_history();
128 if (current_hist)
129 hist_line = current_hist->line;
131 if (*query != ' ')
132 if ((! hist_line) || strcmp(hist_line, query))
133 add_history(query);
134 free(query);
135 return 0;
136 } /* sdb_input_exec_query */
138 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */