Subversion Repositories SvarDOS

Rev

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