Subversion Repositories SvarDOS

Rev

Rev 1111 | Rev 1681 | 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
 *
1602 mateusz.vi 6
 * COPYRIGHT (C) 2016-2023 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"
757 mateusz.vi 33
#include "helpers.h"
238 mateuszvis 34
#include "kprintf.h"
219 mateuszvis 35
#include "libunzip.h"
36
#include "pkginst.h"
37
#include "pkgrem.h"
242 mateuszvis 38
#include "showinst.h"
296 mateuszvis 39
#include "unzip.h"
219 mateuszvis 40
#include "version.h"
41
 
42
 
43
enum ACTIONTYPES {
44
  ACTION_INSTALL,
265 mateuszvis 45
  ACTION_UPDATE,
219 mateuszvis 46
  ACTION_REMOVE,
242 mateuszvis 47
  ACTION_LISTFILES,
250 mateuszvis 48
  ACTION_LISTLOCAL,
296 mateuszvis 49
  ACTION_UNZIP,
219 mateuszvis 50
  ACTION_HELP
51
};
52
 
53
 
54
static int showhelp(void) {
272 mateuszvis 55
  puts("PKG ver " PVER " Copyright (C) " PDATE " Mateusz Viste");
56
  puts("");
723 bttr 57
  puts(svarlang_str(1, 0)); /* "PKG is the SvarDOS package manager." */
272 mateuszvis 58
  puts("");
669 mateusz.vi 59
  puts(svarlang_str(1, 20)); /* "Usage: pkg install package.svp */
60
  puts(svarlang_str(1, 21)); /* "       pkg update package.svp" */
613 mateuszvis 61
  puts(svarlang_str(1, 22)); /* "       pkg remove package" */
62
  puts(svarlang_str(1, 23)); /* "       pkg listfiles package" */
63
  puts(svarlang_str(1, 24)); /* "       pkg listlocal [filter]" */
64
  puts(svarlang_str(1, 27)); /* "       pkg unzip file.zip" */
272 mateuszvis 65
  puts("");
613 mateuszvis 66
  puts(svarlang_str(1, 25)); /* "PKG is published under the MIT license." */
67
  puts(svarlang_str(1, 26)); /* "It is configured through %DOSDIR%\CFG\PKG.CFG" */
219 mateuszvis 68
  return(1);
69
}
70
 
71
 
250 mateuszvis 72
static enum ACTIONTYPES parsearg(int argc, char * const *argv) {
219 mateuszvis 73
  /* look for valid actions */
250 mateuszvis 74
  if ((argc == 3) && (strcasecmp(argv[1], "install") == 0)) {
226 mateuszvis 75
    return(ACTION_INSTALL);
265 mateuszvis 76
  } else if ((argc == 3) && (strcasecmp(argv[1], "update") == 0)) {
77
    return(ACTION_UPDATE);
250 mateuszvis 78
  } else if ((argc == 3) && (strcasecmp(argv[1], "remove") == 0)) {
226 mateuszvis 79
    return(ACTION_REMOVE);
250 mateuszvis 80
  } else if ((argc == 3) && (strcasecmp(argv[1], "listfiles") == 0)) {
242 mateuszvis 81
    return(ACTION_LISTFILES);
250 mateuszvis 82
  } else if ((argc >= 2) && (argc <= 3) && (strcasecmp(argv[1], "listlocal") == 0)) {
83
    return(ACTION_LISTLOCAL);
296 mateuszvis 84
  } else if ((argc == 3) && (strcasecmp(argv[1], "unzip") == 0)) {
85
    return(ACTION_UNZIP);
226 mateuszvis 86
  } else {
87
    return(ACTION_HELP);
219 mateuszvis 88
  }
89
}
90
 
91
 
237 mateuszvis 92
static int pkginst(const char *file, int flags, const char *dosdir, const struct customdirs *dirlist) {
261 mateuszvis 93
  char pkgname[9];
293 mateuszvis 94
  int t, lastpathdelim = -1, lastdot = -1, res = 1;
219 mateuszvis 95
  struct ziplist *zipfileidx;
96
  FILE *zipfilefd;
261 mateuszvis 97
  /* copy the filename into pkgname (without path elements and without extension) */
219 mateuszvis 98
  for (t = 0; file[t] != 0; t++) {
261 mateuszvis 99
    switch (file[t]) {
100
      case '/':
101
      case '\\':
102
        lastpathdelim = t;
103
        break;
104
      case '.':
105
        lastdot = t;
106
        break;
219 mateuszvis 107
    }
108
  }
264 mateuszvis 109
  if (lastdot <= lastpathdelim) lastdot = t; /* a dot before last path delimiters is not an extension prefix */
261 mateuszvis 110
  t = lastdot - (lastpathdelim + 1);
111
  if (t + 1 > sizeof(pkgname)) {
613 mateuszvis 112
    puts(svarlang_str(3, 24)); /* "ERROR: package name too long" */
261 mateuszvis 113
    return(1);
114
  }
115
  memcpy(pkgname, file + lastpathdelim + 1, t);
116
  pkgname[t] = 0;
995 mateusz.vi 117
  strlwr(pkgname); /* package name must be all lower-case for further file matching in the zip file */
219 mateuszvis 118
  /* prepare the zip file and install it */
236 mateuszvis 119
  zipfileidx = pkginstall_preparepackage(pkgname, file, flags, &zipfilefd, dosdir, dirlist);
219 mateuszvis 120
  if (zipfileidx != NULL) {
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);
124
    if (res == 0) res = pkginstall_installpackage(pkgname, dosdir, dirlist, zipfileidx, zipfilefd);
293 mateuszvis 125
    zip_freelist(&zipfileidx);
219 mateuszvis 126
  }
261 mateuszvis 127
 
128
  fclose(zipfilefd);
293 mateuszvis 129
  return(res);
219 mateuszvis 130
}
131
 
132
 
133
int main(int argc, char **argv) {
260 mateuszvis 134
  int res = 1;
219 mateuszvis 135
  enum ACTIONTYPES action;
239 mateuszvis 136
  const char *dosdir;
219 mateuszvis 137
  struct customdirs *dirlist;
138
 
1602 mateusz.vi 139
  svarlang_autoload_exepath(argv[0], getenv("LANG"));   /* NLS init */
244 mateuszvis 140
 
219 mateuszvis 141
  action = parsearg(argc, argv);
1111 bttr 142
  switch (action) {
143
    case ACTION_HELP:
144
      res = showhelp();
145
      goto GAMEOVER;
146
      break;
147
    case ACTION_UNZIP:
148
      res = unzip(argv[2]);
149
      goto GAMEOVER;
150
      break;
244 mateuszvis 151
  }
219 mateuszvis 152
 
238 mateuszvis 153
  /* read the DOSDIR environment variable */
154
  dosdir = getenv("DOSDIR");
155
  if (dosdir == NULL) {
727 bttr 156
    puts(svarlang_str(2, 2)); /* "%DOSDIR% not set! You should make it point to the SvarDOS main directory." */
157
    puts(svarlang_str(2, 3)); /* "Example: SET DOSDIR=C:\SVARDOS" */
244 mateuszvis 158
    goto GAMEOVER;
238 mateuszvis 159
  }
219 mateuszvis 160
 
161
  /* load configuration */
260 mateuszvis 162
  if (loadconf(dosdir, &dirlist) != 0) goto GAMEOVER;
219 mateuszvis 163
 
164
  switch (action) {
265 mateuszvis 165
    case ACTION_UPDATE:
219 mateuszvis 166
    case ACTION_INSTALL:
265 mateuszvis 167
      res = pkginst(argv[2], (action == ACTION_UPDATE)?PKGINST_UPDATE:0, dosdir, dirlist);
219 mateuszvis 168
      break;
169
    case ACTION_REMOVE:
225 mateuszvis 170
      res = pkgrem(argv[2], dosdir);
219 mateuszvis 171
      break;
242 mateuszvis 172
    case ACTION_LISTFILES:
173
      res = listfilesofpkg(argv[2], dosdir);
174
      break;
250 mateuszvis 175
    case ACTION_LISTLOCAL:
176
      res = showinstalledpkgs((argc == 3)?argv[2]:NULL, dosdir);
177
      break;
219 mateuszvis 178
    default:
179
      res = showhelp();
180
      break;
181
  }
182
 
244 mateuszvis 183
  GAMEOVER:
219 mateuszvis 184
  if (res != 0) return(1);
185
  return(0);
186
}