Subversion Repositories SvarDOS

Rev

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