Subversion Repositories SvarDOS

Rev

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

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