Subversion Repositories SvarDOS

Rev

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

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