Subversion Repositories SvarDOS

Rev

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