Subversion Repositories SvarDOS

Rev

Rev 238 | Rev 240 | 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
236 mateuszvis 3
 * Copyright (C) 2015-2021 Mateusz Viste
219 mateuszvis 4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
 * IN THE SOFTWARE.
22
 */
23
 
24
 
25
#include <stdio.h>    /* printf() */
26
#include <stdlib.h>   /* malloc() and friends */
27
#include <string.h>   /* strcasecmp() */
28
 
238 mateuszvis 29
#include "kprintf.h"
219 mateuszvis 30
#include "libunzip.h"
31
#include "pkginst.h"
32
#include "pkgrem.h"
33
#include "version.h"
34
 
35
 
36
enum ACTIONTYPES {
37
  ACTION_INSTALL,
38
  ACTION_REMOVE,
39
  ACTION_HELP
40
};
41
 
42
 
43
static int showhelp(void) {
237 mateuszvis 44
  printf("PKGINST ver " PVER " Copyright (C) " PDATE " Mateusz Viste\n"
219 mateuszvis 45
         "\n"
237 mateuszvis 46
         "PKGINST is the package installer for SvarDOS.\n"
219 mateuszvis 47
         "\n"
237 mateuszvis 48
         "Usage: PKGINST install package.zip\n"
49
         "       PKGINST remove package\n"
219 mateuszvis 50
         "\n"
237 mateuszvis 51
         "PKGINST is published under the MIT license. It uses a PKGINST.CFG configuration\n"
52
         "file located in the directory pointed by %%PKGCFG%%\n"
219 mateuszvis 53
         );
54
  return(1);
55
}
56
 
57
 
58
static enum ACTIONTYPES parsearg(int argc, char **argv) {
59
  /* I expect exactly 2 arguments (ie argc == 3) */
60
  if (argc != 3) return(ACTION_HELP);
226 mateuszvis 61
 
219 mateuszvis 62
  /* look for valid actions */
63
  if (strcasecmp(argv[1], "install") == 0) {
226 mateuszvis 64
    return(ACTION_INSTALL);
219 mateuszvis 65
  } else if (strcasecmp(argv[1], "remove") == 0) {
226 mateuszvis 66
    return(ACTION_REMOVE);
67
  } else {
68
    return(ACTION_HELP);
219 mateuszvis 69
  }
70
}
71
 
72
 
237 mateuszvis 73
static int pkginst(const char *file, int flags, const char *dosdir, const struct customdirs *dirlist) {
219 mateuszvis 74
  char pkgname[32];
75
  int t, lastpathdelim = -1, u = 0;
76
  struct ziplist *zipfileidx;
77
  FILE *zipfilefd;
78
  for (t = 0; file[t] != 0; t++) {
79
    if ((file[t] == '/') || (file[t] == '\\')) lastpathdelim = t;
80
  }
81
  /* copy the filename into pkgname (without path elements) */
82
  for (t = lastpathdelim + 1; file[t] != 0; t++) pkgname[u++] = file[t];
83
  pkgname[u] = 0; /* terminate the string */
84
  /* truncate the file's extension (.zip) */
85
  for (t = u; t > 0; t--) {
86
    if (pkgname[t] == '.') {
87
      pkgname[t] = 0;
88
      break;
89
    }
90
  }
91
  /* prepare the zip file and install it */
236 mateuszvis 92
  zipfileidx = pkginstall_preparepackage(pkgname, file, flags, &zipfilefd, dosdir, dirlist);
219 mateuszvis 93
  if (zipfileidx != NULL) {
94
    int res = 0;
225 mateuszvis 95
    if (pkginstall_installpackage(pkgname, dosdir, dirlist, zipfileidx, zipfilefd) != 0) res = 1;
219 mateuszvis 96
    fclose(zipfilefd);
97
    return(res);
98
  } else {
99
    fclose(zipfilefd);
100
    return(1);
101
  }
102
}
103
 
104
 
105
int main(int argc, char **argv) {
106
  int res, flags;
107
  enum ACTIONTYPES action;
239 mateuszvis 108
  const char *dosdir;
219 mateuszvis 109
  struct customdirs *dirlist;
110
 
111
  action = parsearg(argc, argv);
112
  if (action == ACTION_HELP) return(showhelp());
113
 
238 mateuszvis 114
  /* read the DOSDIR environment variable */
115
  dosdir = getenv("DOSDIR");
116
  if (dosdir == NULL) {
117
    kitten_puts(2, 2, "%DOSDIR% not set! You should make it point to the FreeDOS main directory.");
118
    kitten_puts(2, 3, "Example: SET DOSDIR=C:\\FDOS");
119
    return(-1);
120
  }
219 mateuszvis 121
 
122
  /* load configuration */
123
  flags = 0;
124
  dirlist = NULL;
237 mateuszvis 125
  if (loadconf(dosdir, &dirlist, &flags) != 0) return(5);
219 mateuszvis 126
 
127
  switch (action) {
128
    case ACTION_INSTALL:
236 mateuszvis 129
      res = pkginst(argv[2], flags, dosdir, dirlist);
219 mateuszvis 130
      break;
131
    case ACTION_REMOVE:
225 mateuszvis 132
      res = pkgrem(argv[2], dosdir);
219 mateuszvis 133
      break;
134
    default:
135
      res = showhelp();
136
      break;
137
  }
138
 
139
  if (res != 0) return(1);
140
  return(0);
141
}