Subversion Repositories SvarDOS

Rev

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

Rev 397 Rev 421
-
 
1
/* This file is part of the SvarCOM project and is published under the terms
-
 
2
 * of the MIT license.
-
 
3
 *
-
 
4
 * Copyright (C) 2021 Mateusz Viste
-
 
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
 
1
/*
25
/*
2
 * routines used to manipulate the environment block
26
 * routines used to manipulate the environment block
3
 */
27
 */
4
 
28
 
5
#include <i86.h>
29
#include <i86.h>
6
#include <string.h>
30
#include <string.h>
7
 
31
 
8
#include "env.h"
32
#include "env.h"
9
 
33
 
10
/* 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
11
 * 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
12
 * by either a = sign or a NULL terminator */
36
 * by either a = sign or a NULL terminator */
13
char far *env_lookup(unsigned short env_seg, const char *varname) {
37
char far *env_lookup(unsigned short env_seg, const char *varname) {
14
  char far *env = MK_FP(env_seg, 0);
38
  char far *env = MK_FP(env_seg, 0);
15
  int i;
39
  int i;
16
  for (;;) {
40
  for (;;) {
17
    /* is this it? */
41
    /* is this it? */
18
    for (i = 0;; i++) {
42
    for (i = 0;; i++) {
19
      if ((varname[i] == '=') || (varname[i] == 0)) {
43
      if ((varname[i] == '=') || (varname[i] == 0)) {
20
        if (env[i] == '=') return(env); /* FOUND! */
44
        if (env[i] == '=') return(env); /* FOUND! */
21
        break; /* else look for next string */
45
        break; /* else look for next string */
22
      }
46
      }
23
      if (varname[i] != env[i]) break;
47
      if (varname[i] != env[i]) break;
24
    }
48
    }
25
    /* move env to end of current string */
49
    /* move env to end of current string */
26
    while (*env != 0) env++;
50
    while (*env != 0) env++;
27
    /* 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 */
28
    env++;
52
    env++;
29
    if (*env == 0) return(NULL);
53
    if (*env == 0) return(NULL);
30
  }
54
  }
31
}
55
}
32
 
56
 
33
 
57
 
34
/* returns the size, in bytes, of the allocated environment block */
58
/* returns the size, in bytes, of the allocated environment block */
35
unsigned short env_allocsz(unsigned short env_seg) {
59
unsigned short env_allocsz(unsigned short env_seg) {
36
  unsigned short far *mcbsz = MK_FP(env_seg - 1, 3); /* block size is a word at offset +3 in the MCB */
60
  unsigned short far *mcbsz = MK_FP(env_seg - 1, 3); /* block size is a word at offset +3 in the MCB */
37
  return(*mcbsz * 16); /* return size in bytes, not paragraphs */
61
  return(*mcbsz * 16); /* return size in bytes, not paragraphs */
38
}
62
}
39
 
63
 
40
 
64
 
41
/* remove a variable from environment, if present. returns 0 on success, non-zero if variable not found */
65
/* remove a variable from environment, if present. returns 0 on success, non-zero if variable not found */
42
int env_dropvar(unsigned short env_seg, const char *varname) {
66
int env_dropvar(unsigned short env_seg, const char *varname) {
43
  unsigned short blocksz, traillen;
67
  unsigned short blocksz, traillen;
44
  unsigned short len;
68
  unsigned short len;
45
  char far *varptr = env_lookup(env_seg, varname);
69
  char far *varptr = env_lookup(env_seg, varname);
46
 
70
 
47
  /* if variable not found in environment, quit now */
71
  /* if variable not found in environment, quit now */
48
  if (varptr == NULL) return(-1);
72
  if (varptr == NULL) return(-1);
49
 
73
 
50
  for (len = 0; varptr[len] != 0; len++); /* compute length of variable (without trailing null) */
74
  for (len = 0; varptr[len] != 0; len++); /* compute length of variable (without trailing null) */
51
  blocksz = env_allocsz(env_seg);           /* total environment size */
75
  blocksz = env_allocsz(env_seg);           /* total environment size */
52
  traillen = blocksz - (FP_OFF(varptr) + len + 1); /* how much bytes are present after the variable */
76
  traillen = blocksz - (FP_OFF(varptr) + len + 1); /* how much bytes are present after the variable */
53
  _fmemset(varptr, 0, len);               /* zero out the variable */
77
  _fmemset(varptr, 0, len);               /* zero out the variable */
54
  if (traillen != 0) {
78
  if (traillen != 0) {
55
    _fmemmove(varptr, varptr + len + 1, traillen); /* move rest of memory */
79
    _fmemmove(varptr, varptr + len + 1, traillen); /* move rest of memory */
56
  }
80
  }
57
  return(0);
81
  return(0);
58
}
82
}
59
 
83
 
60
 
84
 
61
/* Writes a variable to environment block. The variable must in the form
85
/* Writes a variable to environment block. The variable must in the form
62
 * "VARNAME=value". If variable is already present in the environment block,
86
 * "VARNAME=value". If variable is already present in the environment block,
63
 * then the value will be updated. If the new value is empty, then the
87
 * then the value will be updated. If the new value is empty, then the
64
 * existing variable (if any) is removed.
88
 * existing variable (if any) is removed.
65
 * VARNAME *MUST* be all-uppercase.
89
 * VARNAME *MUST* be all-uppercase.
66
 *
90
 *
67
 * This function returns:
91
 * This function returns:
68
 *   ENV_SUCCESS = success
92
 *   ENV_SUCCESS = success
69
 *   ENV_NOTENOM = not enough available space in memory block
93
 *   ENV_NOTENOM = not enough available space in memory block
70
 *   ENV_INVSYNT = invalid syntax
94
 *   ENV_INVSYNT = invalid syntax
71
 */
95
 */
72
int env_setvar(unsigned short env_seg, const char *v) {
96
int env_setvar(unsigned short env_seg, const char *v) {
73
  unsigned short envlen;
97
  unsigned short envlen;
74
  unsigned short envfree;
98
  unsigned short envfree;
75
  unsigned short vlen, veqpos;
99
  unsigned short vlen, veqpos;
76
  char far *env = MK_FP(env_seg, 0);
100
  char far *env = MK_FP(env_seg, 0);
77
 
101
 
78
  /* remove variable from environment, if already set */
102
  /* remove variable from environment, if already set */
79
  env_dropvar(env_seg, v);
103
  env_dropvar(env_seg, v);
80
 
104
 
81
  /* compute v length and find the position of the eq sign */
105
  /* compute v length and find the position of the eq sign */
82
  veqpos = 0xffff;
106
  veqpos = 0xffff;
83
  for (vlen = 0; v[vlen] != 0; vlen++) {
107
  for (vlen = 0; v[vlen] != 0; vlen++) {
84
    if (v[vlen] == '=') {
108
    if (v[vlen] == '=') {
85
      if (veqpos != 0xffff) return(ENV_INVSYNT); /* equal sign is forbidden in value */
109
      if (veqpos != 0xffff) return(ENV_INVSYNT); /* equal sign is forbidden in value */
86
      veqpos = vlen;
110
      veqpos = vlen;
87
    }
111
    }
88
  }
112
  }
89
 
113
 
90
  /* if variable empty, stop here */
114
  /* if variable empty, stop here */
91
  if (veqpos == vlen - 1) return(ENV_SUCCESS);
115
  if (veqpos == vlen - 1) return(ENV_SUCCESS);
92
 
116
 
93
  /* compute current size of the environment */
117
  /* compute current size of the environment */
94
  for (envlen = 0; env[envlen] != 0; envlen++) {
118
  for (envlen = 0; env[envlen] != 0; envlen++) {
95
    while (env[envlen] != 0) envlen++; /* consume a string */
119
    while (env[envlen] != 0) envlen++; /* consume a string */
96
  }
120
  }
97
 
121
 
98
  /* compute free space available in environment */
122
  /* compute free space available in environment */
99
  envfree = env_allocsz(env_seg);
123
  envfree = env_allocsz(env_seg);
100
  envfree -= envlen;
124
  envfree -= envlen;
101
  envfree -= 1; /* 1 byte for the environment's NULL terminator */
125
  envfree -= 1; /* 1 byte for the environment's NULL terminator */
102
 
126
 
103
  /* do I have enough env space for the new var? */
127
  /* do I have enough env space for the new var? */
104
  if (envfree < vlen + 1) return(ENV_NOTENOM);
128
  if (envfree < vlen + 1) return(ENV_NOTENOM);
105
 
129
 
106
  /* write the new variable (with its NULL terminator) to environment tail */
130
  /* write the new variable (with its NULL terminator) to environment tail */
107
  _fmemcpy(env + envlen, v, vlen + 1);
131
  _fmemcpy(env + envlen, v, vlen + 1);
108
 
132
 
109
  /* add the environment's NULL terminator */
133
  /* add the environment's NULL terminator */
110
  env[envlen + vlen + 1] = 0;
134
  env[envlen + vlen + 1] = 0;
111
 
135
 
112
  return(ENV_SUCCESS);
136
  return(ENV_SUCCESS);
113
}
137
}
114
 
138