Subversion Repositories SvarDOS

Rev

Rev 436 | Rev 1731 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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