Subversion Repositories SvarDOS

Rev

Rev 1681 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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