Subversion Repositories SvarDOS

Rev

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

Rev Author Line No. Line
219 mateuszvis 1
/*
238 mateuszvis 2
 * PKGINST - 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
 
238 mateuszvis 32
#include "kprintf.h"
219 mateuszvis 33
#include "libunzip.h"
34
#include "pkginst.h"
35
#include "pkgrem.h"
36
#include "version.h"
37
 
38
 
39
enum ACTIONTYPES {
40
  ACTION_INSTALL,
41
  ACTION_REMOVE,
42
  ACTION_HELP
43
};
44
 
45
 
46
static int showhelp(void) {
237 mateuszvis 47
  printf("PKGINST ver " PVER " Copyright (C) " PDATE " Mateusz Viste\n"
219 mateuszvis 48
         "\n"
237 mateuszvis 49
         "PKGINST is the package installer for SvarDOS.\n"
219 mateuszvis 50
         "\n"
237 mateuszvis 51
         "Usage: PKGINST install package.zip\n"
52
         "       PKGINST remove package\n"
219 mateuszvis 53
         "\n"
237 mateuszvis 54
         "PKGINST is published under the MIT license. It uses a PKGINST.CFG configuration\n"
55
         "file located in the directory pointed by %%PKGCFG%%\n"
219 mateuszvis 56
         );
57
  return(1);
58
}
59
 
60
 
61
static enum ACTIONTYPES parsearg(int argc, char **argv) {
62
  /* I expect exactly 2 arguments (ie argc == 3) */
63
  if (argc != 3) return(ACTION_HELP);
226 mateuszvis 64
 
219 mateuszvis 65
  /* look for valid actions */
66
  if (strcasecmp(argv[1], "install") == 0) {
226 mateuszvis 67
    return(ACTION_INSTALL);
219 mateuszvis 68
  } else if (strcasecmp(argv[1], "remove") == 0) {
226 mateuszvis 69
    return(ACTION_REMOVE);
70
  } else {
71
    return(ACTION_HELP);
219 mateuszvis 72
  }
73
}
74
 
75
 
237 mateuszvis 76
static int pkginst(const char *file, int flags, const char *dosdir, const struct customdirs *dirlist) {
219 mateuszvis 77
  char pkgname[32];
78
  int t, lastpathdelim = -1, u = 0;
79
  struct ziplist *zipfileidx;
80
  FILE *zipfilefd;
81
  for (t = 0; file[t] != 0; t++) {
82
    if ((file[t] == '/') || (file[t] == '\\')) lastpathdelim = t;
83
  }
84
  /* copy the filename into pkgname (without path elements) */
85
  for (t = lastpathdelim + 1; file[t] != 0; t++) pkgname[u++] = file[t];
86
  pkgname[u] = 0; /* terminate the string */
87
  /* truncate the file's extension (.zip) */
88
  for (t = u; t > 0; t--) {
89
    if (pkgname[t] == '.') {
90
      pkgname[t] = 0;
91
      break;
92
    }
93
  }
94
  /* prepare the zip file and install it */
236 mateuszvis 95
  zipfileidx = pkginstall_preparepackage(pkgname, file, flags, &zipfilefd, dosdir, dirlist);
219 mateuszvis 96
  if (zipfileidx != NULL) {
97
    int res = 0;
225 mateuszvis 98
    if (pkginstall_installpackage(pkgname, dosdir, dirlist, zipfileidx, zipfilefd) != 0) res = 1;
219 mateuszvis 99
    fclose(zipfilefd);
100
    return(res);
101
  } else {
102
    fclose(zipfilefd);
103
    return(1);
104
  }
105
}
106
 
107
 
108
int main(int argc, char **argv) {
109
  int res, flags;
110
  enum ACTIONTYPES action;
239 mateuszvis 111
  const char *dosdir;
219 mateuszvis 112
  struct customdirs *dirlist;
113
 
114
  action = parsearg(argc, argv);
115
  if (action == ACTION_HELP) return(showhelp());
116
 
238 mateuszvis 117
  /* read the DOSDIR environment variable */
118
  dosdir = getenv("DOSDIR");
119
  if (dosdir == NULL) {
120
    kitten_puts(2, 2, "%DOSDIR% not set! You should make it point to the FreeDOS main directory.");
121
    kitten_puts(2, 3, "Example: SET DOSDIR=C:\\FDOS");
122
    return(-1);
123
  }
219 mateuszvis 124
 
125
  /* load configuration */
126
  flags = 0;
127
  dirlist = NULL;
237 mateuszvis 128
  if (loadconf(dosdir, &dirlist, &flags) != 0) return(5);
219 mateuszvis 129
 
130
  switch (action) {
131
    case ACTION_INSTALL:
236 mateuszvis 132
      res = pkginst(argv[2], flags, dosdir, dirlist);
219 mateuszvis 133
      break;
134
    case ACTION_REMOVE:
225 mateuszvis 135
      res = pkgrem(argv[2], dosdir);
219 mateuszvis 136
      break;
137
    default:
138
      res = showhelp();
139
      break;
140
  }
141
 
142
  if (res != 0) return(1);
143
  return(0);
144
}