Subversion Repositories SvarDOS

Rev

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

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