Subversion Repositories SvarDOS

Rev

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

Rev Author Line No. Line
219 mateuszvis 1
/*
236 mateuszvis 2
 * PKGINST - lightweigth SvarDOS package installer
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
 
29
#include "libunzip.h"
30
#include "pkginst.h"
31
#include "pkgrem.h"
32
#include "readenv.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;
237 mateuszvis 108
  char *dosdir;
219 mateuszvis 109
  struct customdirs *dirlist;
110
 
111
  action = parsearg(argc, argv);
112
  if (action == ACTION_HELP) return(showhelp());
113
 
114
  /* read all necessary environment variables */
237 mateuszvis 115
  if (readenv(&dosdir) != 0) return(1);
219 mateuszvis 116
 
117
  /* load configuration */
118
  flags = 0;
119
  dirlist = NULL;
237 mateuszvis 120
  if (loadconf(dosdir, &dirlist, &flags) != 0) return(5);
219 mateuszvis 121
 
122
  switch (action) {
123
    case ACTION_INSTALL:
236 mateuszvis 124
      res = pkginst(argv[2], flags, dosdir, dirlist);
219 mateuszvis 125
      break;
126
    case ACTION_REMOVE:
225 mateuszvis 127
      res = pkgrem(argv[2], dosdir);
219 mateuszvis 128
      break;
129
    default:
130
      res = showhelp();
131
      break;
132
  }
133
 
134
  if (res != 0) return(1);
135
  return(0);
136
}