Subversion Repositories SvarDOS

Rev

Rev 421 | Rev 570 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
421 mateuszvis 1
/* This file is part of the SvarCOM project and is published under the terms
2
 * of the MIT license.
3
 *
4
 * Copyright (C) 2021 Mateusz Viste
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
 * and/or sell copies of the Software, and to permit persons to whom the
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
23
 */
24
 
361 mateuszvis 25
/*
26
 * routines used to manipulate the environment block
27
 */
28
 
29
#include <i86.h>
30
#include <string.h>
31
 
32
#include "env.h"
33
 
34
/* looks for varname in environment block and returns a far ptr to it if
35
 * found, NULL otherwise. varname MUST be in upper-case and MUST be terminated
36
 * by either a = sign or a NULL terminator */
37
char far *env_lookup(unsigned short env_seg, const char *varname) {
38
  char far *env = MK_FP(env_seg, 0);
39
  int i;
40
  for (;;) {
41
    /* is this it? */
42
    for (i = 0;; i++) {
43
      if ((varname[i] == '=') || (varname[i] == 0)) {
44
        if (env[i] == '=') return(env); /* FOUND! */
45
        break; /* else look for next string */
46
      }
47
      if (varname[i] != env[i]) break;
48
    }
49
    /* move env to end of current string */
50
    while (*env != 0) env++;
51
    /* if there's another trailing zero, then that is the end of environment */
52
    env++;
53
    if (*env == 0) return(NULL);
54
  }
55
}
56
 
57
 
436 mateuszvis 58
/* almost identical to env_lookup(), but instead of returning a pointer
59
 * to the 'NAME=value' string, it returns a pointer to value (or NULL if
60
 * var not found) */
61
char far *env_lookup_val(unsigned short env_seg, const char *varname) {
62
  char far *r = env_lookup(env_seg, varname);
63
  if (r == NULL) return(NULL);
64
  /* find '=' or end of string */
65
  for (;;) {
66
    if (*r == '=') return(r + 1);
67
    if (*r == 0) return(r);
68
    r++;
69
  }
70
}
71
 
72
 
361 mateuszvis 73
/* returns the size, in bytes, of the allocated environment block */
74
unsigned short env_allocsz(unsigned short env_seg) {
75
  unsigned short far *mcbsz = MK_FP(env_seg - 1, 3); /* block size is a word at offset +3 in the MCB */
76
  return(*mcbsz * 16); /* return size in bytes, not paragraphs */
77
}
78
 
79
 
80
/* remove a variable from environment, if present. returns 0 on success, non-zero if variable not found */
81
int env_dropvar(unsigned short env_seg, const char *varname) {
82
  unsigned short blocksz, traillen;
83
  unsigned short len;
84
  char far *varptr = env_lookup(env_seg, varname);
85
 
86
  /* if variable not found in environment, quit now */
87
  if (varptr == NULL) return(-1);
88
 
89
  for (len = 0; varptr[len] != 0; len++); /* compute length of variable (without trailing null) */
90
  blocksz = env_allocsz(env_seg);           /* total environment size */
91
  traillen = blocksz - (FP_OFF(varptr) + len + 1); /* how much bytes are present after the variable */
92
  _fmemset(varptr, 0, len);               /* zero out the variable */
93
  if (traillen != 0) {
94
    _fmemmove(varptr, varptr + len + 1, traillen); /* move rest of memory */
95
  }
96
  return(0);
97
}
98
 
99
 
100
/* Writes a variable to environment block. The variable must in the form
101
 * "VARNAME=value". If variable is already present in the environment block,
102
 * then the value will be updated. If the new value is empty, then the
103
 * existing variable (if any) is removed.
104
 * VARNAME *MUST* be all-uppercase.
105
 *
106
 * This function returns:
107
 *   ENV_SUCCESS = success
108
 *   ENV_NOTENOM = not enough available space in memory block
109
 *   ENV_INVSYNT = invalid syntax
110
 */
111
int env_setvar(unsigned short env_seg, const char *v) {
112
  unsigned short envlen;
113
  unsigned short envfree;
114
  unsigned short vlen, veqpos;
115
  char far *env = MK_FP(env_seg, 0);
116
 
117
  /* remove variable from environment, if already set */
118
  env_dropvar(env_seg, v);
119
 
120
  /* compute v length and find the position of the eq sign */
121
  veqpos = 0xffff;
122
  for (vlen = 0; v[vlen] != 0; vlen++) {
123
    if (v[vlen] == '=') {
124
      if (veqpos != 0xffff) return(ENV_INVSYNT); /* equal sign is forbidden in value */
125
      veqpos = vlen;
126
    }
127
  }
128
 
129
  /* if variable empty, stop here */
130
  if (veqpos == vlen - 1) return(ENV_SUCCESS);
131
 
132
  /* compute current size of the environment */
133
  for (envlen = 0; env[envlen] != 0; envlen++) {
134
    while (env[envlen] != 0) envlen++; /* consume a string */
135
  }
136
 
137
  /* compute free space available in environment */
138
  envfree = env_allocsz(env_seg);
139
  envfree -= envlen;
140
  envfree -= 1; /* 1 byte for the environment's NULL terminator */
141
 
142
  /* do I have enough env space for the new var? */
143
  if (envfree < vlen + 1) return(ENV_NOTENOM);
144
 
145
  /* write the new variable (with its NULL terminator) to environment tail */
146
  _fmemcpy(env + envlen, v, vlen + 1);
147
 
148
  /* add the environment's NULL terminator */
149
  env[envlen + vlen + 1] = 0;
150
 
151
  return(ENV_SUCCESS);
152
}