Subversion Repositories SvarDOS

Rev

Rev 239 | Rev 241 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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