1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
/*
* Copyright (c) 1993-2020 Paul Mattes.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the names of Paul Mattes nor the names of his contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY PAUL MATTES "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL PAUL MATTES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* query.c
* The Query() action.
*/
#include "globals.h"
#include "appres.h"
#include "actions.h"
#include "codepage.h"
#include "copyright.h"
#include "ctlrc.h"
#include "host.h"
#include "lazya.h"
#include "names.h"
#include "popups.h"
#include "query.h"
#include "split_host.h"
#include "telnet.h"
#include "task.h"
#include "trace.h"
#include "screentrace.h"
#include "unicodec.h"
#include "utf8.h"
#include "utils.h"
#include "varbuf.h"
/* Globals */
/* Statics */
/* The current set of queries. */
static query_t *queries;
static size_t num_queries;
static const char *
query_terminal_name(void)
{
return termtype;
}
static const char *
query_build(void)
{
return build;
}
static const char *
get_connect_time(void)
{
time_t t, td;
long dy, hr, mn, sc;
if (!CONNECTED) {
return NULL;
}
time(&t);
td = t - ns_time;
dy = (long)(td / (3600 * 24));
hr = (dy % (3600 * 24)) / 3600;
mn = (td % 3600) / 60;
sc = td % 60;
return (dy > 0)?
lazyaf("%ud%02u:%02u:%02u", dy, hr, mn, sc):
lazyaf("%02u:%02u:%02u", hr, mn, sc);
}
static const char *
get_codepage(void)
{
char *sbcs = lazyaf("%s sbcs gcsgid %u cpgid %u",
get_canonical_codepage(),
(unsigned short)((cgcsgid >> 16) & 0xffff),
(unsigned short)(cgcsgid & 0xffff));
return dbcs? lazyaf("%s dbcs gcsgid %u cpgid %u",
sbcs,
(unsigned short)((cgcsgid_dbcs >> 16) & 0xffff),
(unsigned short)(cgcsgid_dbcs & 0xffff)):
sbcs;
}
static const char *
get_codepages(void)
{
cpname_t *c = get_cpnames();
varbuf_t r;
int i, j;
char *sep = "";
vb_init(&r);
for (i = 0; c[i].name != NULL; i++) {
vb_appendf(&r, "%s%s %cbcs", sep, c[i].name, c[i].dbcs? 'd': 's');
sep = "\n";
for (j = 0; j < c[i].num_aliases; j++) {
vb_appendf(&r, " %s", c[i].aliases[j]);
}
}
free_cpnames(c);
return lazya(vb_consume(&r));
}
static const char *
get_proxy(void)
{
const char *ptype = net_proxy_type();
const char *user = net_proxy_user();
return (ptype != NULL)?
lazyaf("%s %s %s%s%s",
ptype,
net_proxy_host(),
net_proxy_port(),
(user != NULL)? " ": "",
(user != NULL)? user: ""):
NULL;
}
static const char *
get_rx(void)
{
if (!CONNECTED) {
return NULL;
}
return IN_3270?
lazyaf("records %u bytes %u", ns_rrcvd, ns_brcvd):
lazyaf("bytes %u", ns_brcvd);
}
static const char *
get_screentracefile(void)
{
if (!toggled(SCREEN_TRACE)) {
return NULL;
}
return trace_get_screentrace_name();
}
static const char *
get_tasks(void)
{
char *r = task_get_tasks();
size_t sl = strlen(r);
if (sl > 0 && r[sl - 1] == '\n') {
r[sl - 1] = '\0';
}
return lazya(r);
}
static const char *
get_tracefile(void)
{
if (!toggled(TRACING)) {
return NULL;
}
return tracefile_name;
}
static const char *
get_tx(void)
{
if (!CONNECTED) {
return NULL;
}
return IN_3270?
lazyaf("records %u bytes %u", ns_rsent, ns_bsent):
lazyaf("bytes %u", ns_bsent);
}
const char *
get_about(void)
{
return lazyaf("%s\nCopyright 1989-%s by Paul Mattes, GTRC and others.",
build, cyear);
}
/* Common code for Query() and Show() actions. */
bool
query_common(const char *name, ia_t ia, unsigned argc, const char **argv)
{
size_t i;
size_t sl;
action_debug(name, ia, argc, argv);
if (check_argc(name, argc, 0, 1) < 0) {
return false;
}
switch (argc) {
case 0:
for (i = 0; i < num_queries; i++) {
if (!queries[i].hidden) {
const char *s = (queries[i].fn? (*queries[i].fn)():
queries[i].string);
if (s == NULL) {
s = "";
}
if (queries[i].specific && strcmp(s, "")) {
s = "...";
}
action_output("%s:%s%s", queries[i].name, *s? " ": "", s);
}
}
break;
case 1:
sl = strlen(argv[0]);
for (i = 0; i < num_queries; i++) {
if (!strncasecmp(argv[0], queries[i].name, sl)) {
const char *s;
if (strlen(queries[i].name) > sl &&
queries[i + 1].name != NULL &&
!strncasecmp(argv[0], queries[i + 1].name, sl)) {
popup_an_error("%s: Ambiguous parameter", name);
return false;
}
if (queries[i].fn) {
s = (*queries[i].fn)();
} else {
s = queries[i].string;
}
if (s == NULL) {
s = "";
}
action_output("%s\n", s);
return true;
}
}
popup_an_error("%s: Unknown parameter", name);
return false;
}
return true;
}
/* Compare two queries by name. */
static int
query_compare(const void *a, const void *b)
{
const query_t *qa = (query_t *)a;
const query_t *qb = (query_t *)b;
return strcmp(qa->name, qb->name);
}
bool
Query_action(ia_t ia, unsigned argc, const char **argv)
{
return query_common(AnQuery, ia, argc, argv);
}
bool
Show_action(ia_t ia, unsigned argc, const char **argv)
{
return query_common(AnShow, ia, argc, argv);
}
/**
* Register a set of queries.
*/
void
register_queries(query_t new_queries[], size_t count)
{
query_t *q = Malloc(sizeof(query_t) * (num_queries + count));
memcpy(q, queries, sizeof(query_t) * num_queries);
memcpy(q + num_queries, new_queries, sizeof(query_t) * count);
qsort(q, num_queries + count, sizeof(query_t), query_compare);
Free(queries);
queries = q;
num_queries += count;
}
/**
* Query module registration.
*/
void
query_register(void)
{
static action_table_t actions[] = {
{ AnQuery, Query_action, 0 },
{ AnShow, Show_action, 0 }
};
static query_t base_queries[] = {
{ KwAbout, get_about, NULL, false, true },
{ KwActions, all_actions, NULL, false, true },
{ KwBindPluName, net_query_bind_plu_name, NULL, false, false },
{ KwBuildOptions, build_options, NULL, false, false },
{ KwConnectionState, net_query_connection_state, NULL, false, false },
{ KwConnectTime, get_connect_time, NULL, false, false },
{ KwCodePage, get_codepage, NULL, false, false },
{ KwCodePages, get_codepages, NULL, false, true },
{ KwCopyright, show_copyright, NULL, false, true },
{ KwCursor, ctlr_query_cursor, NULL, true, false },
{ KwCursor1, ctlr_query_cursor1, NULL, false, false },
{ KwFormatted, ctlr_query_formatted, NULL, false, false },
{ KwHost, net_query_host, NULL, false, false },
{ KwLocalEncoding, get_codeset, NULL, false, false },
{ KwLuName, net_query_lu_name, NULL, false, false },
{ KwModel, NULL, full_model_name, true, false },
{ KwPrefixes, host_prefixes, NULL, false, false },
{ KwProxy, get_proxy, NULL, false, false },
{ KwScreenCurSize, ctlr_query_cur_size_old, NULL, true, false },
{ KwScreenMaxSize, ctlr_query_max_size_old, NULL, true, false },
{ KwScreenSizeCurrent, ctlr_query_cur_size, NULL, false, false },
{ KwScreenSizeMax, ctlr_query_max_size, NULL, false, false },
{ KwScreenTraceFile, get_screentracefile, NULL, false, false },
{ KwSsl, net_query_tls, NULL, true, false },
{ KwStatsRx, get_rx, NULL, false, false },
{ KwStatsTx, get_tx, NULL, false, false },
{ KwTasks, get_tasks, NULL, false, true },
{ KwTelnetMyOptions, net_myopts, NULL, false, false },
{ KwTelnetHostOptions, net_hisopts, NULL, false, false },
{ KwTerminalName, query_terminal_name, NULL, false, false },
{ KwTraceFile, get_tracefile, NULL, false, false },
{ KwTls, net_query_tls, NULL, false, false },
{ KwTlsCertInfo, net_server_cert_info, NULL, false, true },
{ KwTlsProvider, net_sio_provider, NULL, false, false },
{ KwTlsSessionInfo, net_session_info, NULL, false, true },
{ KwTn3270eOptions, tn3270e_current_opts, NULL, false, false },
{ KwVersion, query_build, NULL, false, false }
};
/* Register actions.*/
register_actions(actions, array_count(actions));
/* Register queries. */
register_queries(base_queries, array_count(base_queries));
}
|