Subversion Repositories SvarDOS

Rev

Rev 1893 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/*
994 mateusz.vi 2
 * This file is part of pkg (SvarDOS package manager)
1678 mateusz.vi 3
 * Copyright (C) 2012-2024 Mateusz Viste
219 mateuszvis 4
 *
5
 * It contains a few helper function...
6
 */
7
 
8
 
9
#include <ctype.h>    /* tolower() */
240 mateuszvis 10
#include <direct.h>   /* provides the mkdir() prototype */
219 mateuszvis 11
#include <string.h>   /* */
12
#include <stdio.h>    /* sprintf() */
13
#include <stdlib.h>   /* atoi() */
14
#include <sys/stat.h> /* mkdir() */
15
 
256 mateuszvis 16
#include "trim.h"
219 mateuszvis 17
#include "helpers.h"
18
 
19
 
1893 mateusz.vi 20
 
21
/* outputs a NUL-terminated string to stdout */
22
void output(const char *s) {
23
  _asm {
24
    push ds
25
    push es
26
    /* get length of s into CX */
27
    mov ax, 0x4000 /* ah=DOS "write to file" and AL=0 for NUL matching */
28
    lds dx, s      /* set DS:DX to string (required for later) */
29
    push ds
30
    pop es         /* make sure es=ds (scasb uses es) */
31
    mov di, dx     /* set di to string (for NULL matching) */
32
    mov cx, 0xffff /* preset cx to 65535 (-1) */
33
    cld            /* clear DF so scasb increments DI */
34
    repne scasb    /* cmp al, es:[di], inc di, dec cx until match found */
35
    /* CX contains (65535 - strlen(s)) now */
36
    not cx         /* reverse all bits so I get (strlen(s) + 1) */
37
    dec cx         /* this is CX length */
38
    jz WRITEDONE   /* do nothing for empty strings */
39
 
40
    /* output by writing to stdout */
41
    /* mov ah, 0x40 */  /* DOS 2+ -- write to file via handle */
42
    xor bh, bh
43
    mov bl, 1 /* set handle (1=stdout 2=stderr) */
44
    /* mov cx, xxx */ /* write CX bytes */
45
    /* mov dx, s   */ /* DS:DX is the source of bytes to "write" */
46
    int 0x21
47
    WRITEDONE:
48
    pop es
49
    pop ds
50
  }
51
}
52
 
53
 
1959 mateusz.vi 54
void outputnl(const char *s) {
55
  output(s);
56
  output("\r\n");
57
}
58
 
59
 
219 mateuszvis 60
/* change all / to \ in a string */
61
void slash2backslash(char *str) {
62
  int x;
63
  for (x = 0; str[x] != 0; x++) {
64
    if (str[x] == '/') str[x] = '\\';
65
  }
66
}
67
 
68
 
1678 mateusz.vi 69
/* trim CRC from a filename and returns a pointer to the CRC part.
70
 * this is used to parse filename lines from LSM files */
71
char *trimfnamecrc(char *fname) {
72
  while (*fname) {
73
    if (*fname == '?') {
74
      *fname = 0;
75
      return(fname + 1);
76
    }
77
    fname++;
78
  }
1959 mateusz.vi 79
  return(NULL);
1678 mateusz.vi 80
}
81
 
82
 
219 mateuszvis 83
void removeDoubleBackslashes(char *str) {
84
  char *curpos;
85
  int x;
86
  for (;;) {
251 mateuszvis 87
    curpos = strstr(str, "\\\\");
219 mateuszvis 88
    if (curpos == NULL) return; /* job done */
89
    for (x = 1; curpos[x] != 0; x++) {
90
      curpos[x - 1] = curpos[x];
91
    }
92
    curpos[x - 1] = 0;
93
  }
94
}
95
 
96
 
97
/* Find the first occurrence of find in s, ignore case. */
98
char *fdnpkg_strcasestr(const char *s, const char *find) {
99
  char c, sc;
100
  size_t len;
101
  if ((c = *find++) != 0) {
102
    c = tolower((unsigned char)c);
103
    len = strlen(find);
104
    do {
105
      do {
106
        if ((sc = *s++) == 0) return(NULL);
107
      } while ((char)tolower((unsigned char)sc) != c);
108
    } while (strncasecmp(s, find, len) != 0);
109
    s--;
110
  }
111
  return((char *)s);
112
}
113
 
114
 
115
/* Creates directories recursively */
116
void mkpath(char *dirs) {
117
  int x;
118
  char savechar;
119
  for (x = 0; dirs[x] != 0; x++) {
120
    if (((dirs[x] == '/') || (dirs[x] == '\\')) && (x > 0)) {
121
      if (dirs[x - 1] != ':') { /* avoid d:\ stuff */
122
        savechar = dirs[x];
123
        dirs[x] = 0;
124
        /* make the dir */
240 mateuszvis 125
        mkdir(dirs);
219 mateuszvis 126
        dirs[x] = savechar;
127
      }
128
    }
129
  }
130
}
131
 
132
 
133
/* returns a pointer to the start of the filename, out of a path\to\file string, and
134
   fills respath with the local folder where the file should be placed. */
1892 mateusz.vi 135
char *computelocalpath(char *longfilename, char *respath, const char *dosdir, const struct customdirs *dirlist, char bootdrive) {
219 mateuszvis 136
  int x, lastsep = 0, firstsep = -1;
137
  char savedchar;
138
  char *shortfilename, *pathstart;
139
  pathstart = longfilename;
140
  for (x = 0; longfilename[x] != 0; x++) {
141
    if ((longfilename[x] == '/') || (longfilename[x] == '\\')) {
142
      lastsep = x;
143
      if (firstsep < 0) firstsep = x;
144
    }
145
  }
1878 mateusz.vi 146
  /* if it's a file without any directory, then it goes to BOOTDRIVE:\ (COMMAND.COM, KERNEL.SYS...) */
994 mateusz.vi 147
  if (firstsep < 0) {
1892 mateusz.vi 148
    sprintf(respath, "%c:\\", bootdrive);
994 mateusz.vi 149
    return(longfilename);
150
  }
151
  /* */
219 mateuszvis 152
  shortfilename = &longfilename[lastsep + 1];
153
  /* look for possible custom path */
154
  if (firstsep > 0) {
155
    savedchar = longfilename[firstsep];
156
    longfilename[firstsep] = 0;
157
    for (; dirlist != NULL; dirlist = dirlist->next) {
158
      if (fdnpkg_strcasestr(longfilename, dirlist->name) == longfilename) { /* found! */
159
        /* sprintf(respath, "%s\\%s", dirlist->location, &longfilename[firstsep + 1]); */
160
        pathstart = &longfilename[firstsep + 1];
161
        dosdir = dirlist->location;
162
        break;
163
      }
164
    }
165
    longfilename[firstsep] = savedchar; /* restore longfilename as it was */
166
  }
167
  /* apply the default (DOSDIR) path */
168
  savedchar = longfilename[lastsep + 1];
169
  longfilename[lastsep + 1] = 0;
170
  sprintf(respath, "%s\\%s", dosdir, pathstart);
171
  slash2backslash(respath);
172
  removeDoubleBackslashes(respath);
173
  longfilename[lastsep + 1] = savedchar;
174
  return(shortfilename);
175
}
176
 
177
 
178
/* analyzes a filename string and returns the pointer to the file's extension
179
 * (which can be empty) */
180
char *getfext(char *fname) {
181
  char *res = NULL;
182
  for (; *fname != 0; fname++) {
183
    if (*fname == '.') res = fname + 1;
184
  }
185
  /* if no dot found, then point to the string's null terminator */
186
  if (res == NULL) return(fname);
187
  return(res);
188
}
248 mateuszvis 189
 
190
 
191
/* reads a line from a "token = value" file, returns 0 on success
192
 * val (if not NULL) is updated with a pointer to the "value" part
193
 * delim is the delimiter char (typically ':' or '=' but can be anything) */
194
int freadtokval(FILE *fd, char *line, size_t maxlen, char **val, char delim) {
195
  int bytebuff, linelen = 0;
196
  if (val != NULL) *val = NULL;
197
  for (;;) {
198
    bytebuff = fgetc(fd);
199
    if (bytebuff == EOF) {
200
      if (linelen == 0) return(-1);
201
      break;
202
    }
203
    if (bytebuff < 0) return(-1);
204
    if ((*val == NULL) && (bytebuff == delim)) {
205
      line[linelen++] = 0;
206
      *val = line + linelen;
207
      continue;
208
    }
209
    if (bytebuff == '\r') continue; /* ignore CR */
210
    if (bytebuff == '\n') break;
211
    if (linelen < maxlen - 1) line[linelen++] = bytebuff;
212
  }
213
  /* terminate line and trim token and value (if any) */
214
  line[linelen] = 0;
215
  trim(line);
216
  if ((val != NULL) && (*val != NULL)) trim(*val);
217
  return(0);
218
}