Subversion Repositories SvarDOS

Rev

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

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