Subversion Repositories SvarDOS

Rev

Rev 1980 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/*
723 bttr 2
 * PKG - SvarDOS package manager
219 mateuszvis 3
 *
240 mateuszvis 4
 * PUBLISHED UNDER THE TERMS OF THE MIT LICENSE
5
 *
1681 mateusz.vi 6
 * COPYRIGHT (C) 2016-2024 MATEUSZ VISTE, ALL RIGHTS RESERVED.
240 mateuszvis 7
 *
219 mateuszvis 8
 * Permission is hereby granted, free of charge, to any person obtaining a
9
 * copy of this software and associated documentation files (the "Software"),
10
 * to deal in the Software without restriction, including without limitation
11
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
 * and/or sell copies of the Software, and to permit persons to whom the
13
 * Software is furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
240 mateuszvis 23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24
 * DEALINGS IN THE SOFTWARE.
219 mateuszvis 25
 */
26
 
27
 
1966 mateusz.vi 28
#include <stdio.h>    /* FILE */
219 mateuszvis 29
#include <stdlib.h>   /* malloc() and friends */
2235 bernd.boec 30
#include <string.h>
31
#include <strings.h>  /* strcasecmp() */
219 mateuszvis 32
 
613 mateuszvis 33
#include "svarlang.lib/svarlang.h"
1959 mateusz.vi 34
#include "healthck.h"
1681 mateusz.vi 35
#include "crc32.h"
757 mateusz.vi 36
#include "helpers.h"
219 mateuszvis 37
#include "libunzip.h"
38
#include "pkginst.h"
39
#include "pkgrem.h"
242 mateuszvis 40
#include "showinst.h"
296 mateuszvis 41
#include "unzip.h"
219 mateuszvis 42
#include "version.h"
43
 
44
 
45
enum ACTIONTYPES {
46
  ACTION_INSTALL,
47
  ACTION_REMOVE,
242 mateuszvis 48
  ACTION_LISTFILES,
250 mateuszvis 49
  ACTION_LISTLOCAL,
1959 mateusz.vi 50
  ACTION_HEALTHCHECK,
296 mateuszvis 51
  ACTION_UNZIP,
1681 mateusz.vi 52
  ACTION_CRC32,
219 mateuszvis 53
  ACTION_HELP
54
};
55
 
56
 
57
static int showhelp(void) {
1962 mateusz.vi 58
  outputnl("PKG ver " PVER " Copyright (C) " PDATE " Mateusz Viste");
59
  outputnl("");
60
  outputnl(svarlang_str(1, 0)); /* "PKG is the SvarDOS package manager." */
61
  outputnl("");
62
  outputnl(svarlang_str(1, 19)); /* "Usage:" */
63
  outputnl("");
64
  outputnl(svarlang_str(1, 20)); /* "pkg install package.svp */
65
  outputnl(svarlang_str(1, 21)); /* "pkg update package.svp" */
66
  outputnl(svarlang_str(1, 22)); /* "pkg rm package" */
67
  outputnl(svarlang_str(1, 23)); /* "pkg files package" */
68
  outputnl(svarlang_str(1, 24)); /* "pkg list [filter]" */
69
  outputnl(svarlang_str(1, 25)); /* "pkg check [pkg]" */
70
  outputnl(svarlang_str(1, 26)); /* "pkg check+ [pkg]" */
71
  outputnl(svarlang_str(1, 27)); /* "pkg unzip file.zip" */
72
  outputnl(svarlang_str(1, 29)); /* "pkg listzip file.zip" */
73
  outputnl(svarlang_str(1, 28)); /* "pkg crc32 file" */
74
  outputnl("");
75
  outputnl(svarlang_str(1, 40)); /* "PKG is published under the MIT license." */
76
  outputnl(svarlang_str(1, 41)); /* "It is configured through %DOSDIR%\CFG\PKG.CFG" */
219 mateuszvis 77
  return(1);
78
}
79
 
80
 
1978 mateusz.vi 81
static enum ACTIONTYPES parsearg(int argc, char * const *argv, unsigned char *flags) {
82
  *flags = 0;
83
 
219 mateuszvis 84
  /* look for valid actions */
250 mateuszvis 85
  if ((argc == 3) && (strcasecmp(argv[1], "install") == 0)) {
226 mateuszvis 86
    return(ACTION_INSTALL);
1980 mateusz.vi 87
  } else if ((argc == 3) && (strcasecmp(argv[1], "inowarn") == 0)) {
88
    /* hidden action used by the SvarDOS installer to avoid onscreen warnings
89
     * during system installation */
90
    *flags = PKGINST_HIDEWARN;
91
    return(ACTION_INSTALL);
265 mateuszvis 92
  } else if ((argc == 3) && (strcasecmp(argv[1], "update") == 0)) {
1978 mateusz.vi 93
    *flags = PKGINST_UPDATE;
94
    return(ACTION_INSTALL);
1962 mateusz.vi 95
  } else if ((argc == 3) && (strcasecmp(argv[1], "rm") == 0)) {
226 mateuszvis 96
    return(ACTION_REMOVE);
1962 mateusz.vi 97
  } else if ((argc == 3) && (strcasecmp(argv[1], "files") == 0)) {
242 mateuszvis 98
    return(ACTION_LISTFILES);
1962 mateusz.vi 99
  } else if ((argc >= 2) && (argc <= 3) && (strcasecmp(argv[1], "list") == 0)) {
250 mateuszvis 100
    return(ACTION_LISTLOCAL);
1962 mateusz.vi 101
  } else if ((argc >= 2) && (argc <= 3) && (strcasecmp(argv[1], "check") == 0)) {
1959 mateusz.vi 102
    return(ACTION_HEALTHCHECK);
1962 mateusz.vi 103
  } else if ((argc >= 2) && (argc <= 3) && (strcasecmp(argv[1], "check+") == 0)) {
1978 mateusz.vi 104
    *flags = 1; /* extended check */
105
    return(ACTION_HEALTHCHECK);
296 mateuszvis 106
  } else if ((argc == 3) && (strcasecmp(argv[1], "unzip") == 0)) {
107
    return(ACTION_UNZIP);
1962 mateusz.vi 108
  } else if ((argc == 3) && (strcasecmp(argv[1], "ziplist") == 0)) {
1978 mateusz.vi 109
    *flags = 1; /* list only */
110
    return(ACTION_UNZIP);
1681 mateusz.vi 111
  } else if ((argc == 3) && (strcasecmp(argv[1], "crc32") == 0)) {
112
    return(ACTION_CRC32);
226 mateuszvis 113
  } else {
114
    return(ACTION_HELP);
219 mateuszvis 115
  }
116
}
117
 
118
 
1964 mateusz.vi 119
static int pkginst(const char *file, int flags, const char *dosdir, const struct customdirs *dirlist, char bootdrive, unsigned char *buff15k) {
261 mateuszvis 120
  char pkgname[9];
1880 mateusz.vi 121
  int res = 1;
219 mateuszvis 122
  struct ziplist *zipfileidx;
123
  FILE *zipfilefd;
1880 mateusz.vi 124
 
125
  /* prepare the zip file for installation and fetch package's name */
1892 mateusz.vi 126
  zipfileidx = pkginstall_preparepackage(pkgname, file, flags, &zipfilefd, dosdir, dirlist, bootdrive);
219 mateuszvis 127
  if (zipfileidx != NULL) {
1880 mateusz.vi 128
 
129
    /* package name must be all lower-case for further file matching in the zip file */
130
    strlwr(pkgname);
131
 
627 mateuszvis 132
    /* remove the old version of the package if we are UPDATING it */
133
    res = 0;
134
    if (flags & PKGINST_UPDATE) res = pkgrem(pkgname, dosdir);
1880 mateusz.vi 135
 
1980 mateusz.vi 136
    if (res == 0) res = pkginstall_installpackage(pkgname, dosdir, dirlist, zipfileidx, zipfilefd, bootdrive, buff15k, flags);
293 mateuszvis 137
    zip_freelist(&zipfileidx);
1980 mateusz.vi 138
 
139
    fclose(zipfilefd);
219 mateuszvis 140
  }
261 mateuszvis 141
 
293 mateuszvis 142
  return(res);
219 mateuszvis 143
}
144
 
145
 
1681 mateusz.vi 146
/* pkg crc32 file */
1964 mateusz.vi 147
static int crcfile(const char *fname, unsigned char *buff4k) {
1681 mateusz.vi 148
  FILE *fd;
149
  unsigned long crc;
150
  unsigned int len;
151
 
152
  fd = fopen(fname, "rb");
153
  if (fd == NULL) {
1963 mateusz.vi 154
    outputnl(svarlang_str(10, 1)); /* failed to open file */
1681 mateusz.vi 155
    return(1);
156
  }
157
 
1964 mateusz.vi 158
  crc = CRC32_INITVAL;
1681 mateusz.vi 159
 
160
  for (;;) {
1964 mateusz.vi 161
    len = fread(buff4k, 1, 4096, fd);
1681 mateusz.vi 162
    if (len == 0) break;
1964 mateusz.vi 163
    crc32_feed(&crc, buff4k, len);
1681 mateusz.vi 164
  }
165
  fclose(fd);
166
 
167
  crc32_finish(&crc);
168
 
1964 mateusz.vi 169
  crc32tostring(buff4k, crc);
170
  outputnl(buff4k);
1681 mateusz.vi 171
 
172
  return(0);
173
}
174
 
175
 
219 mateuszvis 176
int main(int argc, char **argv) {
1964 mateusz.vi 177
  static unsigned char buff15k[15 * 1024];
260 mateuszvis 178
  int res = 1;
219 mateuszvis 179
  enum ACTIONTYPES action;
1978 mateusz.vi 180
  unsigned char actionflags;
239 mateuszvis 181
  const char *dosdir;
219 mateuszvis 182
  struct customdirs *dirlist;
1892 mateusz.vi 183
  char bootdrive;
219 mateuszvis 184
 
1602 mateusz.vi 185
  svarlang_autoload_exepath(argv[0], getenv("LANG"));   /* NLS init */
244 mateuszvis 186
 
1978 mateusz.vi 187
  action = parsearg(argc, argv, &actionflags);
1111 bttr 188
  switch (action) {
189
    case ACTION_HELP:
190
      res = showhelp();
191
      goto GAMEOVER;
192
    case ACTION_UNZIP:
1978 mateusz.vi 193
      res = unzip(argv[2], actionflags, buff15k);
1111 bttr 194
      goto GAMEOVER;
1681 mateusz.vi 195
    case ACTION_CRC32:
1964 mateusz.vi 196
      res = crcfile(argv[2], buff15k);
1681 mateusz.vi 197
      goto GAMEOVER;
244 mateuszvis 198
  }
219 mateuszvis 199
 
238 mateuszvis 200
  /* read the DOSDIR environment variable */
201
  dosdir = getenv("DOSDIR");
202
  if (dosdir == NULL) {
1963 mateusz.vi 203
    outputnl(svarlang_str(2, 2)); /* "%DOSDIR% not set! You should make it point to the SvarDOS main directory." */
204
    outputnl(svarlang_str(2, 3)); /* "Example: SET DOSDIR=C:\SVARDOS" */
244 mateuszvis 205
    goto GAMEOVER;
238 mateuszvis 206
  }
219 mateuszvis 207
 
208
  /* load configuration */
1892 mateusz.vi 209
  if (loadconf(dosdir, &dirlist, &bootdrive) != 0) goto GAMEOVER;
219 mateuszvis 210
 
211
  switch (action) {
212
    case ACTION_INSTALL:
1978 mateusz.vi 213
      res = pkginst(argv[2], actionflags, dosdir, dirlist, bootdrive, buff15k);
219 mateuszvis 214
      break;
215
    case ACTION_REMOVE:
225 mateuszvis 216
      res = pkgrem(argv[2], dosdir);
219 mateuszvis 217
      break;
242 mateuszvis 218
    case ACTION_LISTFILES:
219
      res = listfilesofpkg(argv[2], dosdir);
220
      break;
250 mateuszvis 221
    case ACTION_LISTLOCAL:
222
      res = showinstalledpkgs((argc == 3)?argv[2]:NULL, dosdir);
223
      break;
1959 mateusz.vi 224
    case ACTION_HEALTHCHECK:
1978 mateusz.vi 225
      res = healthcheck(buff15k, (argc == 3)?argv[2]:NULL, dosdir, actionflags);
1958 mateusz.vi 226
      break;
219 mateuszvis 227
    default:
228
      res = showhelp();
229
      break;
230
  }
231
 
244 mateuszvis 232
  GAMEOVER:
219 mateuszvis 233
  if (res != 0) return(1);
234
  return(0);
235
}