Subversion Repositories SvarDOS

Rev

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

Rev 260 Rev 261
1
/*
1
/*
2
 * PKGINST - SvarDOS package installer
2
 * PKGINST - SvarDOS package installer
3
 *
3
 *
4
 * PUBLISHED UNDER THE TERMS OF THE MIT LICENSE
4
 * PUBLISHED UNDER THE TERMS OF THE MIT LICENSE
5
 *
5
 *
6
 * COPYRIGHT (C) 2016-2021 MATEUSZ VISTE, ALL RIGHTS RESERVED.
6
 * COPYRIGHT (C) 2016-2021 MATEUSZ VISTE, ALL RIGHTS RESERVED.
7
 *
7
 *
8
 * 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
9
 * copy of this software and associated documentation files (the "Software"),
9
 * copy of this software and associated documentation files (the "Software"),
10
 * to deal in the Software without restriction, including without limitation
10
 * to deal in the Software without restriction, including without limitation
11
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
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
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:
13
 * Software is furnished to do so, subject to the following conditions:
14
 *
14
 *
15
 * 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
16
 * all copies or substantial portions of the Software.
16
 * all copies or substantial portions of the Software.
17
 *
17
 *
18
 * 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
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * 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
21
 * 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
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24
 * DEALINGS IN THE SOFTWARE.
24
 * DEALINGS IN THE SOFTWARE.
25
 */
25
 */
26
 
26
 
27
 
27
 
28
#include <stdio.h>    /* printf() */
28
#include <stdio.h>    /* printf() */
29
#include <stdlib.h>   /* malloc() and friends */
29
#include <stdlib.h>   /* malloc() and friends */
30
#include <string.h>   /* strcasecmp() */
30
#include <string.h>   /* strcasecmp() */
31
 
31
 
32
#include "kitten/kitten.h"
32
#include "kitten/kitten.h"
33
#include "kprintf.h"
33
#include "kprintf.h"
34
#include "libunzip.h"
34
#include "libunzip.h"
35
#include "pkginst.h"
35
#include "pkginst.h"
36
#include "pkgrem.h"
36
#include "pkgrem.h"
37
#include "showinst.h"
37
#include "showinst.h"
38
#include "version.h"
38
#include "version.h"
39
 
39
 
40
 
40
 
41
enum ACTIONTYPES {
41
enum ACTIONTYPES {
42
  ACTION_INSTALL,
42
  ACTION_INSTALL,
43
  ACTION_REMOVE,
43
  ACTION_REMOVE,
44
  ACTION_LISTFILES,
44
  ACTION_LISTFILES,
45
  ACTION_LISTLOCAL,
45
  ACTION_LISTLOCAL,
46
  ACTION_HELP
46
  ACTION_HELP
47
};
47
};
48
 
48
 
49
 
49
 
50
static int showhelp(void) {
50
static int showhelp(void) {
51
  printf("PKGINST ver " PVER " Copyright (C) " PDATE " Mateusz Viste\n"
51
  printf("PKGINST ver " PVER " Copyright (C) " PDATE " Mateusz Viste\n"
52
         "\n"
52
         "\n"
53
         "PKGINST is the package installer for SvarDOS.\n"
53
         "PKGINST is the package installer for SvarDOS.\n"
54
         "\n"
54
         "\n"
55
         "Usage: PKGINST install package.zip\n"
55
         "Usage: PKGINST install package.zip\n"
56
         "       PKGINST remove package\n"
56
         "       PKGINST remove package\n"
57
         "       PKGINST listfiles package\n"
57
         "       PKGINST listfiles package\n"
58
         "       PKGINST listlocal [filter]\n"
58
         "       PKGINST listlocal [filter]\n"
59
         "\n"
59
         "\n"
60
         "PKGINST is published under the MIT license. It uses a configuration file\n"
60
         "PKGINST is published under the MIT license. It uses a configuration file\n"
61
         "located at %%DOSDIR%%\\CFG\\PKGINST.CFG\n"
61
         "located at %%DOSDIR%%\\CFG\\PKGINST.CFG\n"
62
         );
62
         );
63
  return(1);
63
  return(1);
64
}
64
}
65
 
65
 
66
 
66
 
67
static enum ACTIONTYPES parsearg(int argc, char * const *argv) {
67
static enum ACTIONTYPES parsearg(int argc, char * const *argv) {
68
  /* look for valid actions */
68
  /* look for valid actions */
69
  if ((argc == 3) && (strcasecmp(argv[1], "install") == 0)) {
69
  if ((argc == 3) && (strcasecmp(argv[1], "install") == 0)) {
70
    return(ACTION_INSTALL);
70
    return(ACTION_INSTALL);
71
  } else if ((argc == 3) && (strcasecmp(argv[1], "remove") == 0)) {
71
  } else if ((argc == 3) && (strcasecmp(argv[1], "remove") == 0)) {
72
    return(ACTION_REMOVE);
72
    return(ACTION_REMOVE);
73
  } else if ((argc == 3) && (strcasecmp(argv[1], "listfiles") == 0)) {
73
  } else if ((argc == 3) && (strcasecmp(argv[1], "listfiles") == 0)) {
74
    return(ACTION_LISTFILES);
74
    return(ACTION_LISTFILES);
75
  } else if ((argc >= 2) && (argc <= 3) && (strcasecmp(argv[1], "listlocal") == 0)) {
75
  } else if ((argc >= 2) && (argc <= 3) && (strcasecmp(argv[1], "listlocal") == 0)) {
76
    return(ACTION_LISTLOCAL);
76
    return(ACTION_LISTLOCAL);
77
  } else {
77
  } else {
78
    return(ACTION_HELP);
78
    return(ACTION_HELP);
79
  }
79
  }
80
}
80
}
81
 
81
 
82
 
82
 
83
static int pkginst(const char *file, int flags, const char *dosdir, const struct customdirs *dirlist) {
83
static int pkginst(const char *file, int flags, const char *dosdir, const struct customdirs *dirlist) {
84
  char pkgname[32];
84
  char pkgname[9];
85
  int t, lastpathdelim = -1, u = 0;
85
  int t, lastpathdelim = -1, lastdot = -1;
86
  struct ziplist *zipfileidx;
86
  struct ziplist *zipfileidx;
87
  FILE *zipfilefd;
87
  FILE *zipfilefd;
-
 
88
  /* copy the filename into pkgname (without path elements and without extension) */
88
  for (t = 0; file[t] != 0; t++) {
89
  for (t = 0; file[t] != 0; t++) {
89
    if ((file[t] == '/') || (file[t] == '\\')) lastpathdelim = t;
90
    switch (file[t]) {
90
  }
91
      case '/':
91
  /* copy the filename into pkgname (without path elements) */
-
 
92
  for (t = lastpathdelim + 1; file[t] != 0; t++) pkgname[u++] = file[t];
-
 
93
  pkgname[u] = 0; /* terminate the string */
92
      case '\\':
94
  /* truncate the file's extension (.zip) */
93
        lastpathdelim = t;
95
  for (t = u; t > 0; t--) {
94
        break;
96
    if (pkgname[t] == '.') {
95
      case '.':
97
      pkgname[t] = 0;
96
        lastdot = t;
98
      break;
97
        break;
99
    }
98
    }
100
  }
99
  }
-
 
100
  if (lastdot < lastpathdelim) lastdot = t; /* a dot before last path delimiters is not an extension prefix */
-
 
101
  t = lastdot - (lastpathdelim + 1);
-
 
102
  if (t + 1 > sizeof(pkgname)) {
-
 
103
    kitten_puts(3, 24, "ERROR: package name too long");
-
 
104
    return(1);
-
 
105
  }
-
 
106
  memcpy(pkgname, file + lastpathdelim + 1, t);
-
 
107
  pkgname[t] = 0;
101
  /* prepare the zip file and install it */
108
  /* prepare the zip file and install it */
102
  zipfileidx = pkginstall_preparepackage(pkgname, file, flags, &zipfilefd, dosdir, dirlist);
109
  zipfileidx = pkginstall_preparepackage(pkgname, file, flags, &zipfilefd, dosdir, dirlist);
103
  if (zipfileidx != NULL) {
110
  if (zipfileidx != NULL) {
104
    int res = 0;
-
 
105
    if (pkginstall_installpackage(pkgname, dosdir, dirlist, zipfileidx, zipfilefd) != 0) res = 1;
111
    t = pkginstall_installpackage(pkgname, dosdir, dirlist, zipfileidx, zipfilefd);
106
    fclose(zipfilefd);
112
    fclose(zipfilefd);
107
    return(res);
-
 
108
  } else {
-
 
109
    fclose(zipfilefd);
-
 
110
    return(1);
113
    return(t);
111
  }
114
  }
-
 
115
 
-
 
116
  fclose(zipfilefd);
-
 
117
  return(1);
112
}
118
}
113
 
119
 
114
 
120
 
115
int main(int argc, char **argv) {
121
int main(int argc, char **argv) {
116
  int res = 1;
122
  int res = 1;
117
  enum ACTIONTYPES action;
123
  enum ACTIONTYPES action;
118
  const char *dosdir;
124
  const char *dosdir;
119
  struct customdirs *dirlist;
125
  struct customdirs *dirlist;
120
 
126
 
121
  kittenopen("pkginst"); /* NLS init */
127
  kittenopen("pkginst"); /* NLS init */
122
 
128
 
123
  action = parsearg(argc, argv);
129
  action = parsearg(argc, argv);
124
  if (action == ACTION_HELP) {
130
  if (action == ACTION_HELP) {
125
    showhelp();
131
    showhelp();
126
    goto GAMEOVER;
132
    goto GAMEOVER;
127
  }
133
  }
128
 
134
 
129
  /* read the DOSDIR environment variable */
135
  /* read the DOSDIR environment variable */
130
  dosdir = getenv("DOSDIR");
136
  dosdir = getenv("DOSDIR");
131
  if (dosdir == NULL) {
137
  if (dosdir == NULL) {
132
    kitten_puts(2, 2, "%DOSDIR% not set! You should make it point to the FreeDOS main directory.");
138
    kitten_puts(2, 2, "%DOSDIR% not set! You should make it point to the FreeDOS main directory.");
133
    kitten_puts(2, 3, "Example: SET DOSDIR=C:\\FDOS");
139
    kitten_puts(2, 3, "Example: SET DOSDIR=C:\\FDOS");
134
    goto GAMEOVER;
140
    goto GAMEOVER;
135
  }
141
  }
136
 
142
 
137
  /* load configuration */
143
  /* load configuration */
138
  if (loadconf(dosdir, &dirlist) != 0) goto GAMEOVER;
144
  if (loadconf(dosdir, &dirlist) != 0) goto GAMEOVER;
139
 
145
 
140
  switch (action) {
146
  switch (action) {
141
    case ACTION_INSTALL:
147
    case ACTION_INSTALL:
142
      res = pkginst(argv[2], 0, dosdir, dirlist);
148
      res = pkginst(argv[2], 0, dosdir, dirlist);
143
      break;
149
      break;
144
    case ACTION_REMOVE:
150
    case ACTION_REMOVE:
145
      res = pkgrem(argv[2], dosdir);
151
      res = pkgrem(argv[2], dosdir);
146
      break;
152
      break;
147
    case ACTION_LISTFILES:
153
    case ACTION_LISTFILES:
148
      res = listfilesofpkg(argv[2], dosdir);
154
      res = listfilesofpkg(argv[2], dosdir);
149
      break;
155
      break;
150
    case ACTION_LISTLOCAL:
156
    case ACTION_LISTLOCAL:
151
      res = showinstalledpkgs((argc == 3)?argv[2]:NULL, dosdir);
157
      res = showinstalledpkgs((argc == 3)?argv[2]:NULL, dosdir);
152
      break;
158
      break;
153
    default:
159
    default:
154
      res = showhelp();
160
      res = showhelp();
155
      break;
161
      break;
156
  }
162
  }
157
 
163
 
158
  GAMEOVER:
164
  GAMEOVER:
159
  kittenclose(); /* NLS de-init */
165
  kittenclose(); /* NLS de-init */
160
  if (res != 0) return(1);
166
  if (res != 0) return(1);
161
  return(0);
167
  return(0);
162
}
168
}
163
 
169