Subversion Repositories SvarDOS

Rev

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