Initial revision
This commit is contained in:
60
lib/utils/Makefile
Normal file
60
lib/utils/Makefile
Normal file
@@ -0,0 +1,60 @@
|
||||
# Makefile for lib/utils.
|
||||
|
||||
CFLAGS = -O -D_MINIX -D_POSIX_SOURCE
|
||||
CC1 = $(CC) $(CFLAGS) -c
|
||||
|
||||
LIBUTILS = ../libutils.a
|
||||
all: $(LIBUTILS)
|
||||
|
||||
OBJECTS = \
|
||||
$(LIBUTILS)(tick_delay.o) \
|
||||
$(LIBUTILS)(get_mon_prm.o) \
|
||||
$(LIBUTILS)(env_parse.o) \
|
||||
$(LIBUTILS)(env_panic.o) \
|
||||
$(LIBUTILS)(env_prefix.o) \
|
||||
$(LIBUTILS)(fkey_ctl.o) \
|
||||
$(LIBUTILS)(get_proc_nr.o) \
|
||||
$(LIBUTILS)(srvr_assert.o) \
|
||||
$(LIBUTILS)(srvr_panic.o) \
|
||||
$(LIBUTILS)(srvr_report.o) \
|
||||
$(LIBUTILS)(taskcall.o) \
|
||||
|
||||
|
||||
$(LIBUTILS): $(OBJECTS)
|
||||
aal cr $@ *.o
|
||||
rm *.o
|
||||
|
||||
$(LIBUTILS)(tick_delay.o): tick_delay.c
|
||||
$(CC1) tick_delay.c
|
||||
|
||||
$(LIBUTILS)(get_mon_prm.o): get_mon_prm.c
|
||||
$(CC1) get_mon_prm.c
|
||||
|
||||
$(LIBUTILS)(env_parse.o): env_parse.c
|
||||
$(CC1) env_parse.c
|
||||
|
||||
$(LIBUTILS)(env_prefix.o): env_prefix.c
|
||||
$(CC1) env_prefix.c
|
||||
|
||||
$(LIBUTILS)(env_panic.o): env_panic.c
|
||||
$(CC1) env_panic.c
|
||||
|
||||
$(LIBUTILS)(fkey_ctl.o): fkey_ctl.c
|
||||
$(CC1) fkey_ctl.c
|
||||
|
||||
$(LIBUTILS)(get_proc_nr.o): get_proc_nr.c
|
||||
$(CC1) get_proc_nr.c
|
||||
|
||||
$(LIBUTILS)(srvr_assert.o): srvr_assert.c
|
||||
$(CC1) srvr_assert.c
|
||||
|
||||
$(LIBUTILS)(srvr_panic.o): srvr_panic.c
|
||||
$(CC1) srvr_panic.c
|
||||
|
||||
$(LIBUTILS)(srvr_report.o): srvr_report.c
|
||||
$(CC1) srvr_report.c
|
||||
|
||||
$(LIBUTILS)(taskcall.o): taskcall.c
|
||||
$(CC1) taskcall.c
|
||||
|
||||
|
||||
19
lib/utils/env_panic.c
Normal file
19
lib/utils/env_panic.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "utils.h"
|
||||
#include <string.h>
|
||||
|
||||
/*=========================================================================*
|
||||
* env_panic *
|
||||
*=========================================================================*/
|
||||
PUBLIC void env_panic(key)
|
||||
char *key; /* environment variable whose value is bogus */
|
||||
{
|
||||
static char value[EP_BUF_SIZE] = "<unknown>";
|
||||
int s;
|
||||
if ((s=sys_getkenv(key, strlen(key), value, sizeof(value))) == 0) {
|
||||
if (s != ESRCH) /* only error allowed */
|
||||
printf("WARNING: sys_getkenv() failed in env_panic(): %d\n", s);
|
||||
}
|
||||
printf("Bad environment setting: '%s = %s'\n", key, value);
|
||||
server_panic("","", NO_NUM);
|
||||
}
|
||||
|
||||
92
lib/utils/env_parse.c
Normal file
92
lib/utils/env_parse.c
Normal file
@@ -0,0 +1,92 @@
|
||||
#include "utils.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*=========================================================================*
|
||||
* env_parse *
|
||||
*=========================================================================*/
|
||||
PUBLIC int env_parse(env, fmt, field, param, min, max)
|
||||
char *env; /* environment variable to inspect */
|
||||
char *fmt; /* template to parse it with */
|
||||
int field; /* field number of value to return */
|
||||
long *param; /* address of parameter to get */
|
||||
long min, max; /* minimum and maximum values for the parameter */
|
||||
{
|
||||
/* Parse an environment variable setting, something like "DPETH0=300:3".
|
||||
* Panic if the parsing fails. Return EP_UNSET if the environment variable
|
||||
* is not set, EP_OFF if it is set to "off", EP_ON if set to "on" or a
|
||||
* field is left blank, or EP_SET if a field is given (return value through
|
||||
* *param). Punctuation may be used in the environment and format string,
|
||||
* fields in the environment string may be empty, and punctuation may be
|
||||
* missing to skip fields. The format string contains characters 'd', 'o',
|
||||
* 'x' and 'c' to indicate that 10, 8, 16, or 0 is used as the last argument
|
||||
* to strtol(). A '*' means that a field should be skipped. If the format
|
||||
* string contains something like "\4" then the string is repeated 4 characters
|
||||
* to the left.
|
||||
*/
|
||||
char *val, *end;
|
||||
char value[EP_BUF_SIZE];
|
||||
char PUNCT[] = ":,;.";
|
||||
long newpar;
|
||||
int s, i = 0, radix, r;
|
||||
|
||||
#if DEAD_CODE
|
||||
if ((s=sys_getkenv(env, strlen(env), value, sizeof(value))) != 0) {
|
||||
#endif
|
||||
if ((s=get_mon_param(env, value, sizeof(value))) != 0) {
|
||||
if (s == ESRCH) return(EP_UNSET); /* only error allowed */
|
||||
printf("WARNING: sys_getkenv() failed in env_parse(): %d\n",s);
|
||||
return(EP_EGETKENV);
|
||||
}
|
||||
val = value;
|
||||
if (strcmp(val, "off") == 0) return(EP_OFF);
|
||||
if (strcmp(val, "on") == 0) return(EP_ON);
|
||||
|
||||
r = EP_ON;
|
||||
for (;;) {
|
||||
while (*val == ' ') val++; /* skip spaces */
|
||||
if (*val == 0) return(r); /* the proper exit point */
|
||||
if (*fmt == 0) break; /* too many values */
|
||||
|
||||
if (strchr(PUNCT, *val) != NULL) {
|
||||
/* Time to go to the next field. */
|
||||
if (strchr(PUNCT, *fmt) != NULL) i++;
|
||||
if (*fmt++ == *val) val++;
|
||||
if (*fmt < 32) fmt -= *fmt; /* step back? */
|
||||
} else {
|
||||
/* Environment contains a value, get it. */
|
||||
switch (*fmt) {
|
||||
case '*': radix = -1; break;
|
||||
case 'd': radix = 10; break;
|
||||
case 'o': radix = 010; break;
|
||||
case 'x': radix = 0x10; break;
|
||||
case 'c': radix = 0; break;
|
||||
default: goto badenv;
|
||||
}
|
||||
|
||||
if (radix < 0) {
|
||||
/* Skip. */
|
||||
while (strchr(PUNCT, *val) == NULL) val++;
|
||||
continue;
|
||||
} else {
|
||||
/* A number. */
|
||||
newpar = strtol(val, &end, radix);
|
||||
|
||||
if (end == val) break; /* not a number */
|
||||
val = end;
|
||||
}
|
||||
|
||||
if (i == field) {
|
||||
/* The field requested. */
|
||||
if (newpar < min || newpar > max) break;
|
||||
*param = newpar;
|
||||
r = EP_SET;
|
||||
}
|
||||
}
|
||||
}
|
||||
badenv:
|
||||
env_panic(env);
|
||||
}
|
||||
|
||||
|
||||
29
lib/utils/env_prefix.c
Normal file
29
lib/utils/env_prefix.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "utils.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/*=========================================================================*
|
||||
* env_prefix *
|
||||
*=========================================================================*/
|
||||
PUBLIC int env_prefix(env, prefix)
|
||||
char *env; /* environment variable to inspect */
|
||||
char *prefix; /* prefix to test for */
|
||||
{
|
||||
/* An environment setting may be prefixed by a word, usually "pci".
|
||||
* Return TRUE if a given prefix is used.
|
||||
*/
|
||||
char value[EP_BUF_SIZE];
|
||||
char punct[] = ":,;.";
|
||||
int s;
|
||||
size_t n;
|
||||
|
||||
if ((s = sys_getkenv(env, strlen(env), value, sizeof(value))) != 0) {
|
||||
if (s != ESRCH) /* only error allowed */
|
||||
printf("WARNING: sys_getkenv() failed in env_prefix(): %d\n", s);
|
||||
}
|
||||
n = strlen(prefix);
|
||||
return(value != NULL
|
||||
&& strncmp(value, prefix, n) == 0
|
||||
&& strchr(punct, value[n]) != NULL);
|
||||
}
|
||||
|
||||
21
lib/utils/fkey_ctl.c
Normal file
21
lib/utils/fkey_ctl.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "utils.h"
|
||||
|
||||
/*===========================================================================*
|
||||
* fkey_ctl *
|
||||
*===========================================================================*/
|
||||
PUBLIC int fkey_ctl(fkey_code, enable_disable)
|
||||
int fkey_code; /* function key code it concerns */
|
||||
int enable_disable; /* enable or disable notifications */
|
||||
{
|
||||
/* Send a message to the TTY server to request notifications for function
|
||||
* key presses or to disable notifications. Enabling succeeds unless the key
|
||||
* is already bound to another process. Disabling only succeeds if the key is
|
||||
* bound to the current process.
|
||||
*/
|
||||
message m;
|
||||
m.FKEY_CODE = fkey_code;
|
||||
m.FKEY_ENABLE = enable_disable;
|
||||
return(_taskcall(TTY, FKEY_CONTROL, &m));
|
||||
}
|
||||
|
||||
|
||||
67
lib/utils/get_mon_prm.c
Normal file
67
lib/utils/get_mon_prm.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "utils.h"
|
||||
#include <minix/config.h>
|
||||
#include <string.h>
|
||||
|
||||
FORWARD _PROTOTYPE( char *find_key, (const char *params, const char *key));
|
||||
|
||||
/*===========================================================================*
|
||||
* get_mon_param *
|
||||
*===========================================================================*/
|
||||
PUBLIC int get_mon_param(key, value, max_len)
|
||||
char *key; /* which key to look up */
|
||||
char *value; /* where to store value */
|
||||
int max_len; /* maximum length of value */
|
||||
{
|
||||
message m;
|
||||
static char mon_params[128*sizeof(char *)]; /* copy parameters here */
|
||||
char *key_value;
|
||||
int s;
|
||||
|
||||
if (key != NULL) {
|
||||
/* Get copy of boot monitor parameters. */
|
||||
m.m_type = SYS_GETINFO;
|
||||
m.I_REQUEST = GET_MONPARAMS;
|
||||
m.I_PROC_NR = SELF;
|
||||
m.I_VAL_LEN = sizeof(mon_params);
|
||||
m.I_VAL_PTR = mon_params;
|
||||
if ((s=_taskcall(SYSTASK, SYS_GETINFO, &m)) != OK) {
|
||||
printf("SYS_GETINFO: %d (size %u)\n", s, sizeof(mon_params));
|
||||
return(s);
|
||||
}
|
||||
|
||||
/* We got a copy, now search requested key. */
|
||||
if ((key_value = find_key(mon_params, key)) == NULL)
|
||||
return(ESRCH);
|
||||
|
||||
/* Value found, make the actual copy (as far as possible). */
|
||||
strncpy(value, key_value, max_len);
|
||||
|
||||
/* See if it fits in the client's buffer. */
|
||||
if ((strlen(key_value)+1) > max_len) return(E2BIG);
|
||||
return(OK);
|
||||
}
|
||||
return(EINVAL);
|
||||
}
|
||||
|
||||
|
||||
/*==========================================================================*
|
||||
* find_key *
|
||||
*==========================================================================*/
|
||||
PRIVATE char *find_key(params,name)
|
||||
const char *params;
|
||||
const char *name;
|
||||
{
|
||||
register const char *namep;
|
||||
register char *envp;
|
||||
|
||||
for (envp = (char *) params; *envp != 0;) {
|
||||
for (namep = name; *namep != 0 && *namep == *envp; namep++, envp++)
|
||||
;
|
||||
if (*namep == '\0' && *envp == '=')
|
||||
return(envp + 1);
|
||||
while (*envp++ != 0)
|
||||
;
|
||||
}
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
33
lib/utils/get_proc_nr.c
Normal file
33
lib/utils/get_proc_nr.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "utils.h"
|
||||
#include <minix/config.h>
|
||||
#include <timers.h>
|
||||
#include "../../kernel/const.h"
|
||||
#include "../../kernel/type.h"
|
||||
#include "../../kernel/proc.h"
|
||||
|
||||
/*===========================================================================*
|
||||
* get_proc_nr *
|
||||
*===========================================================================*/
|
||||
PUBLIC int get_proc_nr(proc_nr, proc_name)
|
||||
int *proc_nr; /* store process number here */
|
||||
char *proc_name; /* lookup process by name */
|
||||
{
|
||||
struct proc proc;
|
||||
message m;
|
||||
int s;
|
||||
if (proc_name != NULL) { /* lookup by name */
|
||||
|
||||
} else { /* get own process number */
|
||||
m.m_type = SYS_GETINFO;
|
||||
m.I_REQUEST = GET_PROC;
|
||||
m.I_PROC_NR = SELF;
|
||||
m.I_KEY_LEN = SELF;
|
||||
m.I_VAL_PTR = (char *) &proc;
|
||||
if ((s=_taskcall(SYSTASK, SYS_GETINFO, &m)) != OK)
|
||||
return(s);
|
||||
*proc_nr = proc.p_nr;
|
||||
}
|
||||
return(OK);
|
||||
}
|
||||
|
||||
|
||||
38
lib/utils/srvr_assert.c
Normal file
38
lib/utils/srvr_assert.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "utils.h"
|
||||
|
||||
/* This file contains two very related procedures for debugging purposes:
|
||||
* server_assert_failed
|
||||
* server_compare_failed
|
||||
* Also see <minix/serverassert.h>.
|
||||
*/
|
||||
|
||||
#if !NDEBUG
|
||||
/*=========================================================================*
|
||||
* server_assert_failed *
|
||||
*=========================================================================*/
|
||||
PUBLIC void server_assert_failed(file, line, what)
|
||||
char *file;
|
||||
int line;
|
||||
char *what;
|
||||
{
|
||||
printf("server panic at %s(%d): assertion \"%s\" failed\n",
|
||||
file, line, what);
|
||||
server_panic(NULL, NULL, NO_NUM);
|
||||
}
|
||||
|
||||
/*=========================================================================*
|
||||
* server_compare_failed *
|
||||
*=========================================================================*/
|
||||
PUBLIC void server_compare_failed(file, line, lhs, what, rhs)
|
||||
char *file;
|
||||
int line;
|
||||
int lhs;
|
||||
char *what;
|
||||
int rhs;
|
||||
{
|
||||
printf("server panic at %s(%d): compare (%d) %s (%d) failed\n",
|
||||
file, line, lhs, what, rhs);
|
||||
server_panic(NULL, NULL, NO_NUM);
|
||||
}
|
||||
#endif /* !NDEBUG */
|
||||
|
||||
35
lib/utils/srvr_panic.c
Normal file
35
lib/utils/srvr_panic.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "utils.h"
|
||||
#include <unistd.h> /* need RBT_PANIC flag */
|
||||
|
||||
PRIVATE int panicking; /* inhibits recursive panics */
|
||||
|
||||
/*===========================================================================*
|
||||
* server_panic *
|
||||
*===========================================================================*/
|
||||
PUBLIC void server_panic(who, mess, num)
|
||||
char *who; /* server identification */
|
||||
char *mess; /* message format string */
|
||||
int num; /* number to go with format string */
|
||||
{
|
||||
/* Something awful has happened. Panics are caused when an internal
|
||||
* inconsistency is detected, e.g., a programming error or illegal
|
||||
* value of a defined constant.
|
||||
*/
|
||||
message m;
|
||||
|
||||
if (panicking) return; /* already a panic */
|
||||
panicking = TRUE; /* prevent recursive panics */
|
||||
if (NULL != who && NULL != mess) {
|
||||
if (num != NO_NUM) {
|
||||
printf("Panic in %s: %s: %d\n", who, mess, num);
|
||||
} else {
|
||||
printf("Panic in %s: %s\n", who, mess);
|
||||
}
|
||||
}
|
||||
|
||||
m.m_type = SYS_EXIT;
|
||||
m.EXIT_STATUS = 1;
|
||||
_taskcall(SYSTASK, SYS_EXIT, &m);
|
||||
/* never reached */
|
||||
}
|
||||
|
||||
20
lib/utils/srvr_report.c
Normal file
20
lib/utils/srvr_report.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#include "utils.h"
|
||||
|
||||
/*===========================================================================*
|
||||
* server_report *
|
||||
*===========================================================================*/
|
||||
PUBLIC void server_report(who, mess, num)
|
||||
char *who; /* server identification */
|
||||
char *mess; /* message format to print */
|
||||
int num; /* number to go with the message */
|
||||
{
|
||||
/* Display a message for a server. */
|
||||
|
||||
if (num != NO_NUM) {
|
||||
printf("%s: %s %d\n", who, mess, num);
|
||||
} else {
|
||||
printf("%s: %s\n", who, mess);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
20
lib/utils/taskcall.c
Normal file
20
lib/utils/taskcall.c
Normal file
@@ -0,0 +1,20 @@
|
||||
/* _taskcall() is the same as _syscall() except it returns negative error
|
||||
* codes directly and not in errno. This is a better interface for MM and
|
||||
* FS.
|
||||
*/
|
||||
|
||||
#include <lib.h>
|
||||
#include <minix/syslib.h>
|
||||
|
||||
PUBLIC int _taskcall(who, syscallnr, msgptr)
|
||||
int who;
|
||||
int syscallnr;
|
||||
register message *msgptr;
|
||||
{
|
||||
int status;
|
||||
|
||||
msgptr->m_type = syscallnr;
|
||||
status = _sendrec(who, msgptr);
|
||||
if (status != 0) return(status);
|
||||
return(msgptr->m_type);
|
||||
}
|
||||
28
lib/utils/tick_delay.c
Normal file
28
lib/utils/tick_delay.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "utils.h"
|
||||
|
||||
/*===========================================================================*
|
||||
* tick_delay *
|
||||
*===========================================================================*/
|
||||
PUBLIC int tick_delay(ticks)
|
||||
long ticks; /* number of ticks to wait */
|
||||
{
|
||||
message m;
|
||||
int s;
|
||||
|
||||
if (ticks <= 0) return; /* check for robustness */
|
||||
|
||||
m.m_type = SYS_SYNCALRM; /* request a synchronous alarm */
|
||||
m.ALRM_PROC_NR = SELF; /* SELF means this process nr */
|
||||
m.ALRM_EXP_TIME = ticks; /* request message after ticks */
|
||||
m.ALRM_ABS_TIME = 0; /* ticks are relative to now */
|
||||
s = _taskcall(SYSTASK, SYS_SYNCALRM, &m);
|
||||
|
||||
if (OK == s) receive(HARDWARE,&m); /* await synchronous alarm */
|
||||
return(s);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
8
lib/utils/utils.h
Normal file
8
lib/utils/utils.h
Normal file
@@ -0,0 +1,8 @@
|
||||
/* utils.h - System library utilities. */
|
||||
|
||||
#define _SYSTEM
|
||||
|
||||
#include <lib.h> /* common to all libraries */
|
||||
#include <minix/com.h> /* need task numbers + message types */
|
||||
#include <minix/syslib.h> /* need sendrec, _taskcall, etc */
|
||||
#include <minix/utils.h> /* prototypes in this library */
|
||||
Reference in New Issue
Block a user