1*57843c15Sdsl /* $NetBSD: globalcmds.c,v 1.15 2009/11/01 22:08:14 dsl Exp $ */
27fc29b61Sjwise
37fc29b61Sjwise /*-
4299c3534Sjwise * Copyright (c) 1999
5299c3534Sjwise * The NetBSD Foundation, Inc. All rights reserved.
67fc29b61Sjwise *
77fc29b61Sjwise * Redistribution and use in source and binary forms, with or without
87fc29b61Sjwise * modification, are permitted provided that the following conditions
97fc29b61Sjwise * are met:
107fc29b61Sjwise * 1. Redistributions of source code must retain the above copyright
117fc29b61Sjwise * notice, this list of conditions and the following disclaimer.
127fc29b61Sjwise * 2. Redistributions in binary form must reproduce the above copyright
137fc29b61Sjwise * notice, this list of conditions and the following disclaimer in the
147fc29b61Sjwise * documentation and/or other materials provided with the distribution.
15299c3534Sjwise * 3. All advertising materials mentioning features or use of this software
16299c3534Sjwise * must display the following acknowledgement:
17299c3534Sjwise * This product includes software developed by the NetBSD Foundation.
18299c3534Sjwise * 4. Neither the name of the Foundation nor the names of its contributors
19299c3534Sjwise * may be used to endorse or promote products derived from this software
20299c3534Sjwise * without specific prior written permission.
217fc29b61Sjwise *
227fc29b61Sjwise * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
237fc29b61Sjwise * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
247fc29b61Sjwise * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
257fc29b61Sjwise * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
267fc29b61Sjwise * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
277fc29b61Sjwise * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
287fc29b61Sjwise * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
297fc29b61Sjwise * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
307fc29b61Sjwise * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
317fc29b61Sjwise * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
327fc29b61Sjwise * SUCH DAMAGE.
337fc29b61Sjwise */
347fc29b61Sjwise
35fc391547Sad #include <sys/cdefs.h>
36fc391547Sad #ifndef lint
37*57843c15Sdsl __RCSID("$NetBSD: globalcmds.c,v 1.15 2009/11/01 22:08:14 dsl Exp $");
38fc391547Sad #endif /* not lint */
39fc391547Sad
407fc29b61Sjwise #include <curses.h>
417fc29b61Sjwise #include <stdlib.h>
4200a4e0e7Slukem #include <string.h>
437fc29b61Sjwise #include <unistd.h>
44828483a7Ssimonb
457fc29b61Sjwise #include "systat.h"
467fc29b61Sjwise #include "extern.h"
477fc29b61Sjwise
48fc391547Sad static char *shortname(const char *, const char *);
49d7b4ec92Sitojun
50d7b4ec92Sitojun static char *
shortname(const char * key,const char * s)51fc391547Sad shortname(const char *key, const char *s)
52d7b4ec92Sitojun {
53d7b4ec92Sitojun char *p, *q;
545af29647Sdsl size_t len;
55d7b4ec92Sitojun
56d7b4ec92Sitojun if (key == NULL) {
57d7b4ec92Sitojun if ((p = strdup(s)) == NULL)
58d7b4ec92Sitojun return NULL;
59d7b4ec92Sitojun q = strchr(p, '.');
60d7b4ec92Sitojun if (q && strlen(q) > 1) {
61d7b4ec92Sitojun q[1] = '*';
62d7b4ec92Sitojun q[2] = '\0';
63d7b4ec92Sitojun }
64d7b4ec92Sitojun return p;
655af29647Sdsl }
665af29647Sdsl
675af29647Sdsl len = strlen(key);
685af29647Sdsl if (strncmp(key, s, len) == 0 && s[len] == '.') {
695af29647Sdsl p = strdup(s + len + 1);
705f2d0b66Sitojun if (!p)
715f2d0b66Sitojun return NULL;
72d7b4ec92Sitojun return p;
735af29647Sdsl }
74d7b4ec92Sitojun return NULL;
75d7b4ec92Sitojun }
76d7b4ec92Sitojun
777fc29b61Sjwise void
global_help(char * args)78fc391547Sad global_help(char *args)
797fc29b61Sjwise {
807fc29b61Sjwise int col, len;
817fc29b61Sjwise struct mode *p;
82a6843277Sdsl char *name, *prev;
837fc29b61Sjwise
847fc29b61Sjwise move(CMDLINE, col = 0);
85a6843277Sdsl name = prev = NULL;
867fc29b61Sjwise for (p = modes; p->c_name; p++) {
87a6843277Sdsl if ((name = shortname(args, p->c_name)) == NULL)
88d7b4ec92Sitojun continue;
89a6843277Sdsl if (name && prev && strcmp(name, prev) == 0) {
90a6843277Sdsl free(name);
91a6843277Sdsl name = NULL;
92d7b4ec92Sitojun continue;
93d7b4ec92Sitojun }
94a6843277Sdsl len = strlen(name);
957fc29b61Sjwise if (col + len > COLS)
967fc29b61Sjwise break;
97*57843c15Sdsl addstr(name);
98*57843c15Sdsl col += len + 1;
997fc29b61Sjwise if (col + 1 < COLS)
1007fc29b61Sjwise addch(' ');
101d7b4ec92Sitojun if (prev)
102d7b4ec92Sitojun free(prev);
103a6843277Sdsl prev = name;
104a6843277Sdsl name = NULL;
105d7b4ec92Sitojun }
106d7b4ec92Sitojun if (col == 0 && args) {
107d7b4ec92Sitojun standout();
108e7a39c4fSlukem if ((int)strlen(args) < COLS - 25)
1094bed5400Sitojun printw("help: no matches for `%s.*'", args);
1104bed5400Sitojun else
1114bed5400Sitojun printw("help: no matches");
112d7b4ec92Sitojun standend();
1137fc29b61Sjwise }
1147fc29b61Sjwise clrtoeol();
115a6843277Sdsl if (name)
116a6843277Sdsl free(name);
117d7b4ec92Sitojun if (prev)
118d7b4ec92Sitojun free(prev);
1197fc29b61Sjwise }
1207fc29b61Sjwise
1217fc29b61Sjwise void
global_interval(char * args)122fc391547Sad global_interval(char *args)
123099afa19Sjwise {
124099afa19Sjwise int interval;
125099afa19Sjwise
126099afa19Sjwise if (!args) {
127099afa19Sjwise interval = 5;
128099afa19Sjwise } else {
129099afa19Sjwise interval = atoi(args);
130099afa19Sjwise }
131099afa19Sjwise
132099afa19Sjwise if (interval <= 0) {
133099afa19Sjwise error("%d: bad interval.", interval);
134099afa19Sjwise return;
135099afa19Sjwise }
136099afa19Sjwise
137099afa19Sjwise naptime = interval;
138099afa19Sjwise display(0);
139099afa19Sjwise status();
140099afa19Sjwise }
141099afa19Sjwise
142099afa19Sjwise
143099afa19Sjwise void
global_load(char * args)144fc391547Sad global_load(char *args)
1457fc29b61Sjwise {
1467fc29b61Sjwise (void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
1477fc29b61Sjwise mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
1487fc29b61Sjwise avenrun[0], avenrun[1], avenrun[2]);
1497fc29b61Sjwise clrtoeol();
1507fc29b61Sjwise }
1517fc29b61Sjwise
1527fc29b61Sjwise void
global_quit(char * args)153fc391547Sad global_quit(char *args)
1547fc29b61Sjwise {
1557fc29b61Sjwise die(0);
1567fc29b61Sjwise }
1577fc29b61Sjwise
1587fc29b61Sjwise void
global_stop(char * args)159fc391547Sad global_stop(char *args)
1607fc29b61Sjwise {
161eb8fd502Smycroft timeout(-1);
1627fc29b61Sjwise mvaddstr(CMDLINE, 0, "Refresh disabled.");
1637fc29b61Sjwise clrtoeol();
1647fc29b61Sjwise }
165