Subversion Repositories SvarDOS

Rev

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

Rev 1602 Rev 1681
1
/*
1
/*
2
 * PKG - SvarDOS package manager
2
 * PKG - SvarDOS package manager
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-2023 MATEUSZ VISTE, ALL RIGHTS RESERVED.
6
 * COPYRIGHT (C) 2016-2024 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 "svarlang.lib/svarlang.h"
32
#include "svarlang.lib/svarlang.h"
-
 
33
#include "crc32.h"
33
#include "helpers.h"
34
#include "helpers.h"
34
#include "kprintf.h"
35
#include "kprintf.h"
35
#include "libunzip.h"
36
#include "libunzip.h"
36
#include "pkginst.h"
37
#include "pkginst.h"
37
#include "pkgrem.h"
38
#include "pkgrem.h"
38
#include "showinst.h"
39
#include "showinst.h"
39
#include "unzip.h"
40
#include "unzip.h"
40
#include "version.h"
41
#include "version.h"
41
 
42
 
42
 
43
 
43
enum ACTIONTYPES {
44
enum ACTIONTYPES {
44
  ACTION_INSTALL,
45
  ACTION_INSTALL,
45
  ACTION_UPDATE,
46
  ACTION_UPDATE,
46
  ACTION_REMOVE,
47
  ACTION_REMOVE,
47
  ACTION_LISTFILES,
48
  ACTION_LISTFILES,
48
  ACTION_LISTLOCAL,
49
  ACTION_LISTLOCAL,
49
  ACTION_UNZIP,
50
  ACTION_UNZIP,
-
 
51
  ACTION_CRC32,
50
  ACTION_HELP
52
  ACTION_HELP
51
};
53
};
52
 
54
 
53
 
55
 
54
static int showhelp(void) {
56
static int showhelp(void) {
55
  puts("PKG ver " PVER " Copyright (C) " PDATE " Mateusz Viste");
57
  puts("PKG ver " PVER " Copyright (C) " PDATE " Mateusz Viste");
56
  puts("");
58
  puts("");
57
  puts(svarlang_str(1, 0)); /* "PKG is the SvarDOS package manager." */
59
  puts(svarlang_str(1, 0)); /* "PKG is the SvarDOS package manager." */
58
  puts("");
60
  puts("");
59
  puts(svarlang_str(1, 20)); /* "Usage: pkg install package.svp */
61
  puts(svarlang_str(1, 20)); /* "Usage: pkg install package.svp */
60
  puts(svarlang_str(1, 21)); /* "       pkg update package.svp" */
62
  puts(svarlang_str(1, 21)); /* "       pkg update package.svp" */
61
  puts(svarlang_str(1, 22)); /* "       pkg remove package" */
63
  puts(svarlang_str(1, 22)); /* "       pkg remove package" */
62
  puts(svarlang_str(1, 23)); /* "       pkg listfiles package" */
64
  puts(svarlang_str(1, 23)); /* "       pkg listfiles package" */
63
  puts(svarlang_str(1, 24)); /* "       pkg listlocal [filter]" */
65
  puts(svarlang_str(1, 24)); /* "       pkg listlocal [filter]" */
64
  puts(svarlang_str(1, 27)); /* "       pkg unzip file.zip" */
66
  puts(svarlang_str(1, 27)); /* "       pkg unzip file.zip" */
-
 
67
  puts(svarlang_str(1, 28)); /* "       pkg crc32 file" */
65
  puts("");
68
  puts("");
66
  puts(svarlang_str(1, 25)); /* "PKG is published under the MIT license." */
69
  puts(svarlang_str(1, 25)); /* "PKG is published under the MIT license." */
67
  puts(svarlang_str(1, 26)); /* "It is configured through %DOSDIR%\CFG\PKG.CFG" */
70
  puts(svarlang_str(1, 26)); /* "It is configured through %DOSDIR%\CFG\PKG.CFG" */
68
  return(1);
71
  return(1);
69
}
72
}
70
 
73
 
71
 
74
 
72
static enum ACTIONTYPES parsearg(int argc, char * const *argv) {
75
static enum ACTIONTYPES parsearg(int argc, char * const *argv) {
73
  /* look for valid actions */
76
  /* look for valid actions */
74
  if ((argc == 3) && (strcasecmp(argv[1], "install") == 0)) {
77
  if ((argc == 3) && (strcasecmp(argv[1], "install") == 0)) {
75
    return(ACTION_INSTALL);
78
    return(ACTION_INSTALL);
76
  } else if ((argc == 3) && (strcasecmp(argv[1], "update") == 0)) {
79
  } else if ((argc == 3) && (strcasecmp(argv[1], "update") == 0)) {
77
    return(ACTION_UPDATE);
80
    return(ACTION_UPDATE);
78
  } else if ((argc == 3) && (strcasecmp(argv[1], "remove") == 0)) {
81
  } else if ((argc == 3) && (strcasecmp(argv[1], "remove") == 0)) {
79
    return(ACTION_REMOVE);
82
    return(ACTION_REMOVE);
80
  } else if ((argc == 3) && (strcasecmp(argv[1], "listfiles") == 0)) {
83
  } else if ((argc == 3) && (strcasecmp(argv[1], "listfiles") == 0)) {
81
    return(ACTION_LISTFILES);
84
    return(ACTION_LISTFILES);
82
  } else if ((argc >= 2) && (argc <= 3) && (strcasecmp(argv[1], "listlocal") == 0)) {
85
  } else if ((argc >= 2) && (argc <= 3) && (strcasecmp(argv[1], "listlocal") == 0)) {
83
    return(ACTION_LISTLOCAL);
86
    return(ACTION_LISTLOCAL);
84
  } else if ((argc == 3) && (strcasecmp(argv[1], "unzip") == 0)) {
87
  } else if ((argc == 3) && (strcasecmp(argv[1], "unzip") == 0)) {
85
    return(ACTION_UNZIP);
88
    return(ACTION_UNZIP);
-
 
89
  } else if ((argc == 3) && (strcasecmp(argv[1], "crc32") == 0)) {
-
 
90
    return(ACTION_CRC32);
86
  } else {
91
  } else {
87
    return(ACTION_HELP);
92
    return(ACTION_HELP);
88
  }
93
  }
89
}
94
}
90
 
95
 
91
 
96
 
92
static int pkginst(const char *file, int flags, const char *dosdir, const struct customdirs *dirlist) {
97
static int pkginst(const char *file, int flags, const char *dosdir, const struct customdirs *dirlist) {
93
  char pkgname[9];
98
  char pkgname[9];
94
  int t, lastpathdelim = -1, lastdot = -1, res = 1;
99
  int t, lastpathdelim = -1, lastdot = -1, res = 1;
95
  struct ziplist *zipfileidx;
100
  struct ziplist *zipfileidx;
96
  FILE *zipfilefd;
101
  FILE *zipfilefd;
97
  /* copy the filename into pkgname (without path elements and without extension) */
102
  /* copy the filename into pkgname (without path elements and without extension) */
98
  for (t = 0; file[t] != 0; t++) {
103
  for (t = 0; file[t] != 0; t++) {
99
    switch (file[t]) {
104
    switch (file[t]) {
100
      case '/':
105
      case '/':
101
      case '\\':
106
      case '\\':
102
        lastpathdelim = t;
107
        lastpathdelim = t;
103
        break;
108
        break;
104
      case '.':
109
      case '.':
105
        lastdot = t;
110
        lastdot = t;
106
        break;
111
        break;
107
    }
112
    }
108
  }
113
  }
109
  if (lastdot <= lastpathdelim) lastdot = t; /* a dot before last path delimiters is not an extension prefix */
114
  if (lastdot <= lastpathdelim) lastdot = t; /* a dot before last path delimiters is not an extension prefix */
110
  t = lastdot - (lastpathdelim + 1);
115
  t = lastdot - (lastpathdelim + 1);
111
  if (t + 1 > sizeof(pkgname)) {
116
  if (t + 1 > sizeof(pkgname)) {
112
    puts(svarlang_str(3, 24)); /* "ERROR: package name too long" */
117
    puts(svarlang_str(3, 24)); /* "ERROR: package name too long" */
113
    return(1);
118
    return(1);
114
  }
119
  }
115
  memcpy(pkgname, file + lastpathdelim + 1, t);
120
  memcpy(pkgname, file + lastpathdelim + 1, t);
116
  pkgname[t] = 0;
121
  pkgname[t] = 0;
117
  strlwr(pkgname); /* package name must be all lower-case for further file matching in the zip file */
122
  strlwr(pkgname); /* package name must be all lower-case for further file matching in the zip file */
118
  /* prepare the zip file and install it */
123
  /* prepare the zip file and install it */
119
  zipfileidx = pkginstall_preparepackage(pkgname, file, flags, &zipfilefd, dosdir, dirlist);
124
  zipfileidx = pkginstall_preparepackage(pkgname, file, flags, &zipfilefd, dosdir, dirlist);
120
  if (zipfileidx != NULL) {
125
  if (zipfileidx != NULL) {
121
    /* remove the old version of the package if we are UPDATING it */
126
    /* remove the old version of the package if we are UPDATING it */
122
    res = 0;
127
    res = 0;
123
    if (flags & PKGINST_UPDATE) res = pkgrem(pkgname, dosdir);
128
    if (flags & PKGINST_UPDATE) res = pkgrem(pkgname, dosdir);
124
    if (res == 0) res = pkginstall_installpackage(pkgname, dosdir, dirlist, zipfileidx, zipfilefd);
129
    if (res == 0) res = pkginstall_installpackage(pkgname, dosdir, dirlist, zipfileidx, zipfilefd);
125
    zip_freelist(&zipfileidx);
130
    zip_freelist(&zipfileidx);
126
  }
131
  }
127
 
132
 
128
  fclose(zipfilefd);
133
  fclose(zipfilefd);
129
  return(res);
134
  return(res);
130
}
135
}
131
 
136
 
132
 
137
 
-
 
138
/* pkg crc32 file */
-
 
139
static int crcfile(const char *fname) {
-
 
140
  FILE *fd;
-
 
141
  unsigned long crc;
-
 
142
  unsigned char buff[512];
-
 
143
  unsigned int len;
-
 
144
 
-
 
145
  fd = fopen(fname, "rb");
-
 
146
  if (fd == NULL) {
-
 
147
    puts(svarlang_str(10, 1)); /* failed to open file */
-
 
148
    return(1);
-
 
149
  }
-
 
150
 
-
 
151
  crc = crc32_init();
-
 
152
 
-
 
153
  for (;;) {
-
 
154
    len = fread(buff, 1, sizeof(buff), fd);
-
 
155
    if (len == 0) break;
-
 
156
    crc32_feed(&crc, buff, len);
-
 
157
  }
-
 
158
  fclose(fd);
-
 
159
 
-
 
160
  crc32_finish(&crc);
-
 
161
 
-
 
162
  printf("%08lX", crc);
-
 
163
  puts("");
-
 
164
 
-
 
165
  return(0);
-
 
166
}
-
 
167
 
-
 
168
 
133
int main(int argc, char **argv) {
169
int main(int argc, char **argv) {
134
  int res = 1;
170
  int res = 1;
135
  enum ACTIONTYPES action;
171
  enum ACTIONTYPES action;
136
  const char *dosdir;
172
  const char *dosdir;
137
  struct customdirs *dirlist;
173
  struct customdirs *dirlist;
138
 
174
 
139
  svarlang_autoload_exepath(argv[0], getenv("LANG"));   /* NLS init */
175
  svarlang_autoload_exepath(argv[0], getenv("LANG"));   /* NLS init */
140
 
176
 
141
  action = parsearg(argc, argv);
177
  action = parsearg(argc, argv);
142
  switch (action) {
178
  switch (action) {
143
    case ACTION_HELP:
179
    case ACTION_HELP:
144
      res = showhelp();
180
      res = showhelp();
145
      goto GAMEOVER;
181
      goto GAMEOVER;
146
      break;
182
      break;
147
    case ACTION_UNZIP:
183
    case ACTION_UNZIP:
148
      res = unzip(argv[2]);
184
      res = unzip(argv[2]);
149
      goto GAMEOVER;
185
      goto GAMEOVER;
-
 
186
      break;
-
 
187
    case ACTION_CRC32:
-
 
188
      res = crcfile(argv[2]);
-
 
189
      goto GAMEOVER;
150
      break;
190
      break;
151
  }
191
  }
152
 
192
 
153
  /* read the DOSDIR environment variable */
193
  /* read the DOSDIR environment variable */
154
  dosdir = getenv("DOSDIR");
194
  dosdir = getenv("DOSDIR");
155
  if (dosdir == NULL) {
195
  if (dosdir == NULL) {
156
    puts(svarlang_str(2, 2)); /* "%DOSDIR% not set! You should make it point to the SvarDOS main directory." */
196
    puts(svarlang_str(2, 2)); /* "%DOSDIR% not set! You should make it point to the SvarDOS main directory." */
157
    puts(svarlang_str(2, 3)); /* "Example: SET DOSDIR=C:\SVARDOS" */
197
    puts(svarlang_str(2, 3)); /* "Example: SET DOSDIR=C:\SVARDOS" */
158
    goto GAMEOVER;
198
    goto GAMEOVER;
159
  }
199
  }
160
 
200
 
161
  /* load configuration */
201
  /* load configuration */
162
  if (loadconf(dosdir, &dirlist) != 0) goto GAMEOVER;
202
  if (loadconf(dosdir, &dirlist) != 0) goto GAMEOVER;
163
 
203
 
164
  switch (action) {
204
  switch (action) {
165
    case ACTION_UPDATE:
205
    case ACTION_UPDATE:
166
    case ACTION_INSTALL:
206
    case ACTION_INSTALL:
167
      res = pkginst(argv[2], (action == ACTION_UPDATE)?PKGINST_UPDATE:0, dosdir, dirlist);
207
      res = pkginst(argv[2], (action == ACTION_UPDATE)?PKGINST_UPDATE:0, dosdir, dirlist);
168
      break;
208
      break;
169
    case ACTION_REMOVE:
209
    case ACTION_REMOVE:
170
      res = pkgrem(argv[2], dosdir);
210
      res = pkgrem(argv[2], dosdir);
171
      break;
211
      break;
172
    case ACTION_LISTFILES:
212
    case ACTION_LISTFILES:
173
      res = listfilesofpkg(argv[2], dosdir);
213
      res = listfilesofpkg(argv[2], dosdir);
174
      break;
214
      break;
175
    case ACTION_LISTLOCAL:
215
    case ACTION_LISTLOCAL:
176
      res = showinstalledpkgs((argc == 3)?argv[2]:NULL, dosdir);
216
      res = showinstalledpkgs((argc == 3)?argv[2]:NULL, dosdir);
177
      break;
217
      break;
178
    default:
218
    default:
179
      res = showhelp();
219
      res = showhelp();
180
      break;
220
      break;
181
  }
221
  }
182
 
222
 
183
  GAMEOVER:
223
  GAMEOVER:
184
  if (res != 0) return(1);
224
  if (res != 0) return(1);
185
  return(0);
225
  return(0);
186
}
226
}
187
 
227