Subversion Repositories SvarDOS

Rev

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

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