Subversion Repositories SvarDOS

Rev

Rev 1958 | Rev 1961 | Go to most recent revision | 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
 
28
#include <stdio.h>    /* printf() */
29
#include <stdlib.h>   /* malloc() and friends */
30
#include <string.h>   /* strcasecmp() */
31
 
613 mateuszvis 32
#include "svarlang.lib/svarlang.h"
1959 mateusz.vi 33
#include "healthck.h"
1681 mateusz.vi 34
#include "crc32.h"
757 mateusz.vi 35
#include "helpers.h"
238 mateuszvis 36
#include "kprintf.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,
265 mateuszvis 47
  ACTION_UPDATE,
219 mateuszvis 48
  ACTION_REMOVE,
242 mateuszvis 49
  ACTION_LISTFILES,
250 mateuszvis 50
  ACTION_LISTLOCAL,
1959 mateusz.vi 51
  ACTION_HEALTHCHECK,
296 mateuszvis 52
  ACTION_UNZIP,
1889 mateusz.vi 53
  ACTION_LISTZIP,
1681 mateusz.vi 54
  ACTION_CRC32,
219 mateuszvis 55
  ACTION_HELP
56
};
57
 
58
 
59
static int showhelp(void) {
272 mateuszvis 60
  puts("PKG ver " PVER " Copyright (C) " PDATE " Mateusz Viste");
61
  puts("");
723 bttr 62
  puts(svarlang_str(1, 0)); /* "PKG is the SvarDOS package manager." */
272 mateuszvis 63
  puts("");
1958 mateusz.vi 64
  puts(svarlang_str(1, 19)); /* "Usage:" */
272 mateuszvis 65
  puts("");
1958 mateusz.vi 66
  puts(svarlang_str(1, 20)); /* "pkg install package.svp */
67
  puts(svarlang_str(1, 21)); /* "pkg update package.svp" */
68
  puts(svarlang_str(1, 22)); /* "pkg del package" */
69
  puts(svarlang_str(1, 23)); /* "pkg listfiles package" */
70
  puts(svarlang_str(1, 24)); /* "pkg listlocal [filter]" */
1959 mateusz.vi 71
  puts(svarlang_str(1, 25)); /* "pkg healthcheck [pkg]" */
1958 mateusz.vi 72
  puts(svarlang_str(1, 27)); /* "pkg unzip file.zip" */
73
  puts(svarlang_str(1, 29)); /* "pkg listzip file.zip" */
74
  puts(svarlang_str(1, 28)); /* "pkg crc32 file" */
75
  puts("");
1682 mateusz.vi 76
  puts(svarlang_str(1, 40)); /* "PKG is published under the MIT license." */
77
  puts(svarlang_str(1, 41)); /* "It is configured through %DOSDIR%\CFG\PKG.CFG" */
219 mateuszvis 78
  return(1);
79
}
80
 
81
 
250 mateuszvis 82
static enum ACTIONTYPES parsearg(int argc, char * const *argv) {
219 mateuszvis 83
  /* look for valid actions */
250 mateuszvis 84
  if ((argc == 3) && (strcasecmp(argv[1], "install") == 0)) {
226 mateuszvis 85
    return(ACTION_INSTALL);
265 mateuszvis 86
  } else if ((argc == 3) && (strcasecmp(argv[1], "update") == 0)) {
87
    return(ACTION_UPDATE);
1956 mateusz.vi 88
  } else if ((argc == 3) && (strcasecmp(argv[1], "del") == 0)) {
226 mateuszvis 89
    return(ACTION_REMOVE);
250 mateuszvis 90
  } else if ((argc == 3) && (strcasecmp(argv[1], "listfiles") == 0)) {
242 mateuszvis 91
    return(ACTION_LISTFILES);
250 mateuszvis 92
  } else if ((argc >= 2) && (argc <= 3) && (strcasecmp(argv[1], "listlocal") == 0)) {
93
    return(ACTION_LISTLOCAL);
1959 mateusz.vi 94
  } else if ((argc >= 2) && (argc <= 3) && (strcasecmp(argv[1], "healthcheck") == 0)) {
95
    return(ACTION_HEALTHCHECK);
296 mateuszvis 96
  } else if ((argc == 3) && (strcasecmp(argv[1], "unzip") == 0)) {
97
    return(ACTION_UNZIP);
1889 mateusz.vi 98
  } else if ((argc == 3) && (strcasecmp(argv[1], "listzip") == 0)) {
99
    return(ACTION_LISTZIP);
1681 mateusz.vi 100
  } else if ((argc == 3) && (strcasecmp(argv[1], "crc32") == 0)) {
101
    return(ACTION_CRC32);
226 mateuszvis 102
  } else {
103
    return(ACTION_HELP);
219 mateuszvis 104
  }
105
}
106
 
107
 
1892 mateusz.vi 108
static int pkginst(const char *file, int flags, const char *dosdir, const struct customdirs *dirlist, char bootdrive) {
261 mateuszvis 109
  char pkgname[9];
1880 mateusz.vi 110
  int res = 1;
219 mateuszvis 111
  struct ziplist *zipfileidx;
112
  FILE *zipfilefd;
1880 mateusz.vi 113
 
114
  /* prepare the zip file for installation and fetch package's name */
1892 mateusz.vi 115
  zipfileidx = pkginstall_preparepackage(pkgname, file, flags, &zipfilefd, dosdir, dirlist, bootdrive);
219 mateuszvis 116
  if (zipfileidx != NULL) {
1880 mateusz.vi 117
 
118
    /* package name must be all lower-case for further file matching in the zip file */
119
    strlwr(pkgname);
120
 
627 mateuszvis 121
    /* remove the old version of the package if we are UPDATING it */
122
    res = 0;
123
    if (flags & PKGINST_UPDATE) res = pkgrem(pkgname, dosdir);
1880 mateusz.vi 124
 
1892 mateusz.vi 125
    if (res == 0) res = pkginstall_installpackage(pkgname, dosdir, dirlist, zipfileidx, zipfilefd, bootdrive);
293 mateuszvis 126
    zip_freelist(&zipfileidx);
219 mateuszvis 127
  }
261 mateuszvis 128
 
129
  fclose(zipfilefd);
293 mateuszvis 130
  return(res);
219 mateuszvis 131
}
132
 
133
 
1681 mateusz.vi 134
/* pkg crc32 file */
135
static int crcfile(const char *fname) {
136
  FILE *fd;
137
  unsigned long crc;
138
  unsigned char buff[512];
139
  unsigned int len;
140
 
141
  fd = fopen(fname, "rb");
142
  if (fd == NULL) {
143
    puts(svarlang_str(10, 1)); /* failed to open file */
144
    return(1);
145
  }
146
 
147
  crc = crc32_init();
148
 
149
  for (;;) {
150
    len = fread(buff, 1, sizeof(buff), fd);
151
    if (len == 0) break;
152
    crc32_feed(&crc, buff, len);
153
  }
154
  fclose(fd);
155
 
156
  crc32_finish(&crc);
157
 
158
  printf("%08lX", crc);
159
  puts("");
160
 
161
  return(0);
162
}
163
 
164
 
219 mateuszvis 165
int main(int argc, char **argv) {
260 mateuszvis 166
  int res = 1;
219 mateuszvis 167
  enum ACTIONTYPES action;
239 mateuszvis 168
  const char *dosdir;
219 mateuszvis 169
  struct customdirs *dirlist;
1892 mateusz.vi 170
  char bootdrive;
219 mateuszvis 171
 
1602 mateusz.vi 172
  svarlang_autoload_exepath(argv[0], getenv("LANG"));   /* NLS init */
244 mateuszvis 173
 
219 mateuszvis 174
  action = parsearg(argc, argv);
1111 bttr 175
  switch (action) {
176
    case ACTION_HELP:
177
      res = showhelp();
178
      goto GAMEOVER;
179
    case ACTION_UNZIP:
1889 mateusz.vi 180
      res = unzip(argv[2], 0);
1111 bttr 181
      goto GAMEOVER;
1889 mateusz.vi 182
    case ACTION_LISTZIP:
183
      res = unzip(argv[2], 1);
184
      goto GAMEOVER;
1681 mateusz.vi 185
    case ACTION_CRC32:
186
      res = crcfile(argv[2]);
187
      goto GAMEOVER;
244 mateuszvis 188
  }
219 mateuszvis 189
 
238 mateuszvis 190
  /* read the DOSDIR environment variable */
191
  dosdir = getenv("DOSDIR");
192
  if (dosdir == NULL) {
727 bttr 193
    puts(svarlang_str(2, 2)); /* "%DOSDIR% not set! You should make it point to the SvarDOS main directory." */
194
    puts(svarlang_str(2, 3)); /* "Example: SET DOSDIR=C:\SVARDOS" */
244 mateuszvis 195
    goto GAMEOVER;
238 mateuszvis 196
  }
219 mateuszvis 197
 
198
  /* load configuration */
1892 mateusz.vi 199
  if (loadconf(dosdir, &dirlist, &bootdrive) != 0) goto GAMEOVER;
219 mateuszvis 200
 
201
  switch (action) {
265 mateuszvis 202
    case ACTION_UPDATE:
219 mateuszvis 203
    case ACTION_INSTALL:
1892 mateusz.vi 204
      res = pkginst(argv[2], (action == ACTION_UPDATE)?PKGINST_UPDATE:0, dosdir, dirlist, bootdrive);
219 mateuszvis 205
      break;
206
    case ACTION_REMOVE:
225 mateuszvis 207
      res = pkgrem(argv[2], dosdir);
219 mateuszvis 208
      break;
242 mateuszvis 209
    case ACTION_LISTFILES:
210
      res = listfilesofpkg(argv[2], dosdir);
211
      break;
250 mateuszvis 212
    case ACTION_LISTLOCAL:
213
      res = showinstalledpkgs((argc == 3)?argv[2]:NULL, dosdir);
214
      break;
1959 mateusz.vi 215
    case ACTION_HEALTHCHECK:
216
      res = healthcheck((argc == 3)?argv[2]:NULL, dosdir);
1958 mateusz.vi 217
      break;
219 mateuszvis 218
    default:
219
      res = showhelp();
220
      break;
221
  }
222
 
244 mateuszvis 223
  GAMEOVER:
219 mateuszvis 224
  if (res != 0) return(1);
225
  return(0);
226
}