Subversion Repositories SvarDOS

Rev

Rev 436 | Rev 1731 | 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
 *
570 mateuszvis 4
 * Copyright (C) 2021-2022 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
 
95
/* remove a variable from environment, if present. returns 0 on success, non-zero if variable not found */
96
int env_dropvar(unsigned short env_seg, const char *varname) {
97
  unsigned short blocksz, traillen;
98
  unsigned short len;
99
  char far *varptr = env_lookup(env_seg, varname);
100
 
101
  /* if variable not found in environment, quit now */
102
  if (varptr == NULL) return(-1);
103
 
104
  for (len = 0; varptr[len] != 0; len++); /* compute length of variable (without trailing null) */
105
  blocksz = env_allocsz(env_seg);           /* total environment size */
106
  traillen = blocksz - (FP_OFF(varptr) + len + 1); /* how much bytes are present after the variable */
107
  _fmemset(varptr, 0, len);               /* zero out the variable */
108
  if (traillen != 0) {
109
    _fmemmove(varptr, varptr + len + 1, traillen); /* move rest of memory */
110
  }
111
  return(0);
112
}
113
 
114
 
115
/* Writes a variable to environment block. The variable must in the form
116
 * "VARNAME=value". If variable is already present in the environment block,
117
 * then the value will be updated. If the new value is empty, then the
118
 * existing variable (if any) is removed.
119
 * VARNAME *MUST* be all-uppercase.
120
 *
121
 * This function returns:
122
 *   ENV_SUCCESS = success
123
 *   ENV_NOTENOM = not enough available space in memory block
124
 *   ENV_INVSYNT = invalid syntax
125
 */
126
int env_setvar(unsigned short env_seg, const char *v) {
127
  unsigned short envlen;
128
  unsigned short envfree;
129
  unsigned short vlen, veqpos;
130
  char far *env = MK_FP(env_seg, 0);
131
 
132
  /* remove variable from environment, if already set */
133
  env_dropvar(env_seg, v);
134
 
135
  /* compute v length and find the position of the eq sign */
136
  veqpos = 0xffff;
137
  for (vlen = 0; v[vlen] != 0; vlen++) {
138
    if (v[vlen] == '=') {
139
      if (veqpos != 0xffff) return(ENV_INVSYNT); /* equal sign is forbidden in value */
140
      veqpos = vlen;
141
    }
142
  }
143
 
144
  /* if variable empty, stop here */
145
  if (veqpos == vlen - 1) return(ENV_SUCCESS);
146
 
147
  /* compute current size of the environment */
148
  for (envlen = 0; env[envlen] != 0; envlen++) {
149
    while (env[envlen] != 0) envlen++; /* consume a string */
150
  }
151
 
152
  /* compute free space available in environment */
153
  envfree = env_allocsz(env_seg);
154
  envfree -= envlen;
155
  envfree -= 1; /* 1 byte for the environment's NULL terminator */
156
 
157
  /* do I have enough env space for the new var? */
158
  if (envfree < vlen + 1) return(ENV_NOTENOM);
159
 
160
  /* write the new variable (with its NULL terminator) to environment tail */
161
  _fmemcpy(env + envlen, v, vlen + 1);
162
 
163
  /* add the environment's NULL terminator */
164
  env[envlen + vlen + 1] = 0;
165
 
166
  return(ENV_SUCCESS);
167
}