Subversion Repositories SvarDOS

Rev

Rev 1732 | 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
 *
1731 mateusz.vi 4
 * Copyright (C) 2021-2024 Mateusz Viste
421 mateuszvis 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 (;;) {
1985 mateusz.vi 41
    /* end of environment block? */
42
    if (*env == 0) return(NULL);
361 mateuszvis 43
    /* is this it? */
44
    for (i = 0;; i++) {
45
      if ((varname[i] == '=') || (varname[i] == 0)) {
46
        if (env[i] == '=') return(env); /* FOUND! */
47
        break; /* else look for next string */
48
      }
49
      if (varname[i] != env[i]) break;
50
    }
51
    /* move env to end of current string */
52
    while (*env != 0) env++;
1985 mateusz.vi 53
    /* move to next variable */
361 mateuszvis 54
    env++;
55
  }
56
}
57
 
58
 
436 mateuszvis 59
/* almost identical to env_lookup(), but instead of returning a pointer
60
 * to the 'NAME=value' string, it returns a pointer to value (or NULL if
61
 * var not found) */
62
char far *env_lookup_val(unsigned short env_seg, const char *varname) {
63
  char far *r = env_lookup(env_seg, varname);
64
  if (r == NULL) return(NULL);
65
  /* find '=' or end of string */
66
  for (;;) {
67
    if (*r == '=') return(r + 1);
68
    if (*r == 0) return(r);
69
    r++;
70
  }
71
}
72
 
73
 
570 mateuszvis 74
/* locates the value of env variable varname and copies it to result, up to
75
 * ressz bytes (incl. the NULL terminator). returns the length of the value on
76
 * success, 0 if var not found or couldn't fit in ressz). */
77
unsigned short env_lookup_valcopy(char *res, unsigned short ressz, unsigned short env_seg, const char *varname) {
78
  unsigned short i;
79
  char far *v = env_lookup_val(env_seg, varname);
80
  if (v == NULL) return(0);
81
  for (i = 0;; i++) {
82
    if (ressz-- == 0) return(0);
83
    res[i] = v[i];
84
    if (res[i] == 0) return(i);
85
  }
86
}
87
 
88
 
361 mateuszvis 89
/* returns the size, in bytes, of the allocated environment block */
90
unsigned short env_allocsz(unsigned short env_seg) {
91
  unsigned short far *mcbsz = MK_FP(env_seg - 1, 3); /* block size is a word at offset +3 in the MCB */
92
  return(*mcbsz * 16); /* return size in bytes, not paragraphs */
93
}
94
 
95
 
1731 mateusz.vi 96
/* return currently used space (length, in bytes) of the environment */
97
unsigned short env_getcurlen(unsigned short env_seg) {
98
  char far *env = MK_FP(env_seg, 0);
99
  unsigned short envlen;
100
  for (envlen = 0; env[envlen] != 0; envlen++) {
101
    while (env[envlen] != 0) envlen++; /* consume a string */
102
  }
103
  return(envlen);
104
}
105
 
106
 
361 mateuszvis 107
/* remove a variable from environment, if present. returns 0 on success, non-zero if variable not found */
108
int env_dropvar(unsigned short env_seg, const char *varname) {
109
  unsigned short blocksz, traillen;
110
  unsigned short len;
111
  char far *varptr = env_lookup(env_seg, varname);
112
 
113
  /* if variable not found in environment, quit now */
114
  if (varptr == NULL) return(-1);
115
 
116
  for (len = 0; varptr[len] != 0; len++); /* compute length of variable (without trailing null) */
117
  blocksz = env_allocsz(env_seg);           /* total environment size */
118
  traillen = blocksz - (FP_OFF(varptr) + len + 1); /* how much bytes are present after the variable */
119
  _fmemset(varptr, 0, len);               /* zero out the variable */
120
  if (traillen != 0) {
121
    _fmemmove(varptr, varptr + len + 1, traillen); /* move rest of memory */
122
  }
123
  return(0);
124
}
125
 
126
 
127
/* Writes a variable to environment block. The variable must in the form
128
 * "VARNAME=value". If variable is already present in the environment block,
129
 * then the value will be updated. If the new value is empty, then the
130
 * existing variable (if any) is removed.
131
 * VARNAME *MUST* be all-uppercase.
132
 *
133
 * This function returns:
134
 *   ENV_SUCCESS = success
135
 *   ENV_NOTENOM = not enough available space in memory block
136
 *   ENV_INVSYNT = invalid syntax
137
 */
138
int env_setvar(unsigned short env_seg, const char *v) {
139
  unsigned short envlen;
140
  unsigned short vlen, veqpos;
141
  char far *env = MK_FP(env_seg, 0);
1732 mateusz.vi 142
  char far *alreadyexists;
143
  unsigned short alreadyexistslen;
361 mateuszvis 144
 
145
  /* compute v length and find the position of the eq sign */
146
  veqpos = 0xffff;
147
  for (vlen = 0; v[vlen] != 0; vlen++) {
148
    if (v[vlen] == '=') {
149
      if (veqpos != 0xffff) return(ENV_INVSYNT); /* equal sign is forbidden in value */
150
      veqpos = vlen;
151
    }
152
  }
153
 
1731 mateusz.vi 154
  /* get length of the current environment */
155
  envlen = env_getcurlen(env_seg);
361 mateuszvis 156
 
1732 mateusz.vi 157
  /* does the variable already exist? */
158
  alreadyexists = env_lookup(env_seg, v);
159
  alreadyexistslen = 0;
160
  if (alreadyexists != NULL) {
161
    while (alreadyexists[alreadyexistslen] != 0) alreadyexistslen++;
162
  }
163
 
361 mateuszvis 164
  /* do I have enough env space for the new var? */
1732 mateusz.vi 165
  if (envlen + vlen + 3 - alreadyexistslen >= env_allocsz(env_seg)) {
166
    return(ENV_NOTENOM);
167
  }
361 mateuszvis 168
 
1732 mateusz.vi 169
  /* remove variable from environment if already set and recompute environment's length */
170
  env_dropvar(env_seg, v);
171
  envlen = env_getcurlen(env_seg);
172
 
173
  /* if variable empty, stop here */
174
  if (veqpos == vlen - 1) return(ENV_SUCCESS);
175
 
361 mateuszvis 176
  /* write the new variable (with its NULL terminator) to environment tail */
177
  _fmemcpy(env + envlen, v, vlen + 1);
178
 
179
  /* add the environment's NULL terminator */
180
  env[envlen + vlen + 1] = 0;
181
 
182
  return(ENV_SUCCESS);
183
}