Subversion Repositories SvarDOS

Compare Revisions

Ignore whitespace Rev 360 → Rev 361

/svarcom/cmd/set.c
5,6 → 5,9
* varname can also contain spaces
*/
 
#include "env.h"
 
 
static int cmd_set(int argc, char const **argv, unsigned short env_seg, const char far *cmdline) {
char far *env = MK_FP(env_seg, 0);
char buff[256];
22,10 → 25,10
}
} else if ((argc == 2) && (imatch(argv[1], "/?"))) {
puts("TODO: help screen"); /* TODO */
} else { /* do not rely on argv, SET has its own rules... */
} else { /* set variable (do not rely on argv, SET has its own rules...) */
const char far *ptr;
char buff[256];
int i;
unsigned short i;
/* locate the first space */
for (ptr = cmdline; *ptr != ' '; ptr++);
/* now locate the first non-space: that's where the variable name begins */
39,16 → 42,8
i++;
}
 
/* if not an = sign, then error */
if (*ptr != '=') goto syntax_err;
 
/* add the eq sign to buff */
buff[i++] = '=';
ptr++;
 
/* copy value now, but make sure it contains no '=' sign */
/* copy value now */
while (*ptr != '\r') {
if (*ptr == '=') goto syntax_err;
buff[i++] = *ptr;
ptr++;
}
56,10 → 51,10
/* terminate buff */
buff[i] = 0;
 
/* TODO add it to environment */
puts(buff);
puts("TODO: ACTUALLY ADD TO ENV");
 
/* commit variable to environment */
i = env_setvar(env_seg, buff);
if (i == ENV_INVSYNT) goto syntax_err;
if (i == ENV_NOTENOM) puts("Not enough available space within the environment block");
}
return(-1);
 
/svarcom/env.c
0,0 → 1,113
/*
* routines used to manipulate the environment block
*/
 
#include <i86.h>
#include <string.h>
 
#include "env.h"
 
/* looks for varname in environment block and returns a far ptr to it if
* found, NULL otherwise. varname MUST be in upper-case and MUST be terminated
* by either a = sign or a NULL terminator */
char far *env_lookup(unsigned short env_seg, const char *varname) {
char far *env = MK_FP(env_seg, 0);
int i;
for (;;) {
/* is this it? */
for (i = 0;; i++) {
if ((varname[i] == '=') || (varname[i] == 0)) {
if (env[i] == '=') return(env); /* FOUND! */
break; /* else look for next string */
}
if (varname[i] != env[i]) break;
}
/* move env to end of current string */
while (*env != 0) env++;
/* if there's another trailing zero, then that is the end of environment */
env++;
if (*env == 0) return(NULL);
}
}
 
 
/* returns the size, in bytes, of the allocated environment block */
unsigned short env_allocsz(unsigned short env_seg) {
unsigned short far *mcbsz = MK_FP(env_seg - 1, 3); /* block size is a word at offset +3 in the MCB */
return(*mcbsz * 16); /* return size in bytes, not paragraphs */
}
 
 
/* remove a variable from environment, if present. returns 0 on success, non-zero if variable not found */
int env_dropvar(unsigned short env_seg, const char *varname) {
unsigned short blocksz, traillen;
unsigned short len;
char far *varptr = env_lookup(env_seg, varname);
 
/* if variable not found in environment, quit now */
if (varptr == NULL) return(-1);
 
for (len = 0; varptr[len] != 0; len++); /* compute length of variable (without trailing null) */
blocksz = env_allocsz(env_seg); /* total environment size */
traillen = blocksz - (FP_OFF(varptr) + len + 1); /* how much bytes are present after the variable */
_fmemset(varptr, 0, len); /* zero out the variable */
if (traillen != 0) {
_fmemmove(varptr, varptr + len + 1, traillen); /* move rest of memory */
}
return(0);
}
 
 
/* Writes a variable to environment block. The variable must in the form
* "VARNAME=value". If variable is already present in the environment block,
* then the value will be updated. If the new value is empty, then the
* existing variable (if any) is removed.
* VARNAME *MUST* be all-uppercase.
*
* This function returns:
* ENV_SUCCESS = success
* ENV_NOTENOM = not enough available space in memory block
* ENV_INVSYNT = invalid syntax
*/
int env_setvar(unsigned short env_seg, const char *v) {
unsigned short envlen;
unsigned short envfree;
unsigned short vlen, veqpos;
char far *env = MK_FP(env_seg, 0);
 
/* remove variable from environment, if already set */
env_dropvar(env_seg, v);
 
/* compute v length and find the position of the eq sign */
veqpos = 0xffff;
for (vlen = 0; v[vlen] != 0; vlen++) {
if (v[vlen] == '=') {
if (veqpos != 0xffff) return(ENV_INVSYNT); /* equal sign is forbidden in value */
veqpos = vlen;
}
}
 
/* if variable empty, stop here */
if (veqpos == vlen - 1) return(ENV_SUCCESS);
 
/* compute current size of the environment */
for (envlen = 0; env[envlen] != 0; envlen++) {
while (env[envlen] != 0) envlen++; /* consume a string */
}
 
/* compute free space available in environment */
envfree = env_allocsz(env_seg);
envfree -= envlen;
envfree -= 1; /* 1 byte for the environment's NULL terminator */
 
/* do I have enough env space for the new var? */
if (envfree < vlen + 1) return(ENV_NOTENOM);
 
/* write the new variable (with its NULL terminator) to environment tail */
_fmemcpy(env + envlen, v, vlen + 1);
 
/* add the environment's NULL terminator */
env[envlen + vlen + 1] = 0;
 
return(ENV_SUCCESS);
}
/svarcom/env.h
0,0 → 1,36
/*
* routines used to manipulate the environment block
* Copyright (C) 2021, Mateusz Viste
*/
 
#ifndef ENV_H
#define ENV_H
 
/* looks for varname in environment block and returns a far ptr to it if
* found, NULL otherwise. varname MUST be in upper-case and MUST be terminated
* by either a = sign or a NULL terminator */
char far *env_lookup(unsigned short env_seg, const char *varname);
 
/* returns the size, in bytes, of the allocated environment block */
unsigned short env_allocsz(unsigned short env_seg);
 
/* remove a variable from environment, if present. returns 0 on success, non-zero if variable not found */
int env_dropvar(unsigned short env_seg, const char *varname);
 
#define ENV_SUCCESS 0
#define ENV_NOTENOM -1
#define ENV_INVSYNT -2
 
/* Writes a variable to environment block. The variable must in the form
* "varname=value". If variable is already present in the environment block,
* then the value will be updated. If the new value is empty, then the
* existing variable (if any) is removed.
*
* This function returns:
* ENV_SUCCESS = success
* ENV_NOTENOM = not enough available space in memory block
* ENV_INVSYNT = invalid syntax
*/
int env_setvar(unsigned short env_seg, const char *v);
 
#endif
/svarcom/makefile
20,8 → 20,8
 
all: command.com
 
command.com: rmod.h command.c cmd.c rmodinit.c helpers.c cmd\*.c
wcl $(CFLAGS) command.c cmd.c rmodinit.c helpers.c
command.com: rmod.h command.c cmd.c env.c rmodinit.c helpers.c cmd\*.c
wcl $(CFLAGS) command.c cmd.c env.c rmodinit.c helpers.c
 
rmod.h: file2c.com rmod.com
file2c rmod.com rmod.h rmod