Subversion Repositories SvarDOS

Rev

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
 *
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 (;;) {
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
 
570 mateuszvis 73
/* locates the value of env variable varname and copies it to result, up to
74
 * ressz bytes (incl. the NULL terminator). returns the length of the value on
75
 * success, 0 if var not found or couldn't fit in ressz). */
76
unsigned short env_lookup_valcopy(char *res, unsigned short ressz, unsigned short env_seg, const char *varname) {
77
  unsigned short i;
78
  char far *v = env_lookup_val(env_seg, varname);
79
  if (v == NULL) return(0);
80
  for (i = 0;; i++) {
81
    if (ressz-- == 0) return(0);
82
    res[i] = v[i];
83
    if (res[i] == 0) return(i);
84
  }
85
}
86
 
87
 
361 mateuszvis 88
/* returns the size, in bytes, of the allocated environment block */
89
unsigned short env_allocsz(unsigned short env_seg) {
90
  unsigned short far *mcbsz = MK_FP(env_seg - 1, 3); /* block size is a word at offset +3 in the MCB */
91
  return(*mcbsz * 16); /* return size in bytes, not paragraphs */
92
}
93
 
94
 
1731 mateusz.vi 95
/* return currently used space (length, in bytes) of the environment */
96
unsigned short env_getcurlen(unsigned short env_seg) {
97
  char far *env = MK_FP(env_seg, 0);
98
  unsigned short envlen;
99
  for (envlen = 0; env[envlen] != 0; envlen++) {
100
    while (env[envlen] != 0) envlen++; /* consume a string */
101
  }
102
  return(envlen);
103
}
104
 
105
 
361 mateuszvis 106
/* remove a variable from environment, if present. returns 0 on success, non-zero if variable not found */
107
int env_dropvar(unsigned short env_seg, const char *varname) {
108
  unsigned short blocksz, traillen;
109
  unsigned short len;
110
  char far *varptr = env_lookup(env_seg, varname);
111
 
112
  /* if variable not found in environment, quit now */
113
  if (varptr == NULL) return(-1);
114
 
115
  for (len = 0; varptr[len] != 0; len++); /* compute length of variable (without trailing null) */
116
  blocksz = env_allocsz(env_seg);           /* total environment size */
117
  traillen = blocksz - (FP_OFF(varptr) + len + 1); /* how much bytes are present after the variable */
118
  _fmemset(varptr, 0, len);               /* zero out the variable */
119
  if (traillen != 0) {
120
    _fmemmove(varptr, varptr + len + 1, traillen); /* move rest of memory */
121
  }
122
  return(0);
123
}
124
 
125
 
126
/* Writes a variable to environment block. The variable must in the form
127
 * "VARNAME=value". If variable is already present in the environment block,
128
 * then the value will be updated. If the new value is empty, then the
129
 * existing variable (if any) is removed.
130
 * VARNAME *MUST* be all-uppercase.
131
 *
132
 * This function returns:
133
 *   ENV_SUCCESS = success
134
 *   ENV_NOTENOM = not enough available space in memory block
135
 *   ENV_INVSYNT = invalid syntax
136
 */
137
int env_setvar(unsigned short env_seg, const char *v) {
138
  unsigned short envlen;
139
  unsigned short vlen, veqpos;
140
  char far *env = MK_FP(env_seg, 0);
141
 
142
  /* remove variable from environment, if already set */
143
  env_dropvar(env_seg, v);
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
 
154
  /* if variable empty, stop here */
155
  if (veqpos == vlen - 1) return(ENV_SUCCESS);
156
 
1731 mateusz.vi 157
  /* get length of the current environment */
158
  envlen = env_getcurlen(env_seg);
361 mateuszvis 159
 
160
  /* do I have enough env space for the new var? */
1731 mateusz.vi 161
  if (envlen + vlen + 2 >= env_allocsz(env_seg)) return(ENV_NOTENOM);
361 mateuszvis 162
 
163
  /* write the new variable (with its NULL terminator) to environment tail */
164
  _fmemcpy(env + envlen, v, vlen + 1);
165
 
166
  /* add the environment's NULL terminator */
167
  env[envlen + vlen + 1] = 0;
168
 
169
  return(ENV_SUCCESS);
170
}