Subversion Repositories SvarDOS

Rev

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

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