Subversion Repositories SvarDOS

Rev

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

Rev 236 Rev 237
1
/*
1
/*
2
 * PKGINST - lightweigth SvarDOS package installer
2
 * PKGINST - lightweigth SvarDOS package installer
3
 * Copyright (C) 2015-2021 Mateusz Viste
3
 * Copyright (C) 2015-2021 Mateusz Viste
4
 *
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
11
 *
12
 * The above copyright notice and this permission notice shall be included in
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
13
 * all copies or substantial portions of the Software.
14
 *
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
 * IN THE SOFTWARE.
21
 * IN THE SOFTWARE.
22
 */
22
 */
23
 
23
 
24
 
24
 
25
#include <stdio.h>    /* printf() */
25
#include <stdio.h>    /* printf() */
26
#include <stdlib.h>   /* malloc() and friends */
26
#include <stdlib.h>   /* malloc() and friends */
27
#include <string.h>   /* strcasecmp() */
27
#include <string.h>   /* strcasecmp() */
28
 
28
 
29
#include "libunzip.h"
29
#include "libunzip.h"
30
#include "pkginst.h"
30
#include "pkginst.h"
31
#include "pkgrem.h"
31
#include "pkgrem.h"
32
#include "readenv.h"
32
#include "readenv.h"
33
#include "version.h"
33
#include "version.h"
34
 
34
 
35
 
35
 
36
enum ACTIONTYPES {
36
enum ACTIONTYPES {
37
  ACTION_INSTALL,
37
  ACTION_INSTALL,
38
  ACTION_REMOVE,
38
  ACTION_REMOVE,
39
  ACTION_HELP
39
  ACTION_HELP
40
};
40
};
41
 
41
 
42
 
42
 
43
static int showhelp(void) {
43
static int showhelp(void) {
44
  printf("FDINST v" PVER " Copyright (C) " PDATE " Mateusz Viste\n"
44
  printf("PKGINST ver " PVER " Copyright (C) " PDATE " Mateusz Viste\n"
45
         "\n"
45
         "\n"
46
         "FDINST is a lightweigth package installer for FreeDOS. It is an alternative\n"
46
         "PKGINST is the package installer for SvarDOS.\n"
47
         "to FDNPKG, when only basic, local install/remove actions are necessary. FDINST\n"
-
 
48
         "is a 16-bit, 8086-compatible application running in real mode.\n"
-
 
49
         "\n"
47
         "\n"
50
         "Usage: FDINST install package.zip\n"
48
         "Usage: PKGINST install package.zip\n"
51
         "       FDINST remove package\n"
49
         "       PKGINST remove package\n"
52
         "\n"
50
         "\n"
53
         "FDINST is published under the MIT license, and shares most of its source code\n"
51
         "PKGINST is published under the MIT license. It uses a PKGINST.CFG configuration\n"
54
         "with FDNPKG to guarantee consistent behaviour of both tools. It also uses\n"
52
         "file located in the directory pointed by %%PKGCFG%%\n"
55
         "FDNPKG's configuration file.\n"
-
 
56
         );
53
         );
57
  return(1);
54
  return(1);
58
}
55
}
59
 
56
 
60
 
57
 
61
static enum ACTIONTYPES parsearg(int argc, char **argv) {
58
static enum ACTIONTYPES parsearg(int argc, char **argv) {
62
  /* I expect exactly 2 arguments (ie argc == 3) */
59
  /* I expect exactly 2 arguments (ie argc == 3) */
63
  if (argc != 3) return(ACTION_HELP);
60
  if (argc != 3) return(ACTION_HELP);
64
 
61
 
65
  /* look for valid actions */
62
  /* look for valid actions */
66
  if (strcasecmp(argv[1], "install") == 0) {
63
  if (strcasecmp(argv[1], "install") == 0) {
67
    return(ACTION_INSTALL);
64
    return(ACTION_INSTALL);
68
  } else if (strcasecmp(argv[1], "remove") == 0) {
65
  } else if (strcasecmp(argv[1], "remove") == 0) {
69
    return(ACTION_REMOVE);
66
    return(ACTION_REMOVE);
70
  } else {
67
  } else {
71
    return(ACTION_HELP);
68
    return(ACTION_HELP);
72
  }
69
  }
73
}
70
}
74
 
71
 
75
 
72
 
76
static int pkginst(const char *file, int flags, const char *dosdir, struct customdirs *dirlist) {
73
static int pkginst(const char *file, int flags, const char *dosdir, const struct customdirs *dirlist) {
77
  char pkgname[32];
74
  char pkgname[32];
78
  int t, lastpathdelim = -1, u = 0;
75
  int t, lastpathdelim = -1, u = 0;
79
  struct ziplist *zipfileidx;
76
  struct ziplist *zipfileidx;
80
  FILE *zipfilefd;
77
  FILE *zipfilefd;
81
  for (t = 0; file[t] != 0; t++) {
78
  for (t = 0; file[t] != 0; t++) {
82
    if ((file[t] == '/') || (file[t] == '\\')) lastpathdelim = t;
79
    if ((file[t] == '/') || (file[t] == '\\')) lastpathdelim = t;
83
  }
80
  }
84
  /* copy the filename into pkgname (without path elements) */
81
  /* copy the filename into pkgname (without path elements) */
85
  for (t = lastpathdelim + 1; file[t] != 0; t++) pkgname[u++] = file[t];
82
  for (t = lastpathdelim + 1; file[t] != 0; t++) pkgname[u++] = file[t];
86
  pkgname[u] = 0; /* terminate the string */
83
  pkgname[u] = 0; /* terminate the string */
87
  /* truncate the file's extension (.zip) */
84
  /* truncate the file's extension (.zip) */
88
  for (t = u; t > 0; t--) {
85
  for (t = u; t > 0; t--) {
89
    if (pkgname[t] == '.') {
86
    if (pkgname[t] == '.') {
90
      pkgname[t] = 0;
87
      pkgname[t] = 0;
91
      break;
88
      break;
92
    }
89
    }
93
  }
90
  }
94
  /* prepare the zip file and install it */
91
  /* prepare the zip file and install it */
95
  zipfileidx = pkginstall_preparepackage(pkgname, file, flags, &zipfilefd, dosdir, dirlist);
92
  zipfileidx = pkginstall_preparepackage(pkgname, file, flags, &zipfilefd, dosdir, dirlist);
96
  if (zipfileidx != NULL) {
93
  if (zipfileidx != NULL) {
97
    int res = 0;
94
    int res = 0;
98
    if (pkginstall_installpackage(pkgname, dosdir, dirlist, zipfileidx, zipfilefd) != 0) res = 1;
95
    if (pkginstall_installpackage(pkgname, dosdir, dirlist, zipfileidx, zipfilefd) != 0) res = 1;
99
    fclose(zipfilefd);
96
    fclose(zipfilefd);
100
    return(res);
97
    return(res);
101
  } else {
98
  } else {
102
    fclose(zipfilefd);
99
    fclose(zipfilefd);
103
    return(1);
100
    return(1);
104
  }
101
  }
105
}
102
}
106
 
103
 
107
 
104
 
108
int main(int argc, char **argv) {
105
int main(int argc, char **argv) {
109
  int res, flags;
106
  int res, flags;
110
  enum ACTIONTYPES action;
107
  enum ACTIONTYPES action;
111
  char *dosdir, *cfgfile;
108
  char *dosdir;
112
  struct customdirs *dirlist;
109
  struct customdirs *dirlist;
113
 
110
 
114
  action = parsearg(argc, argv);
111
  action = parsearg(argc, argv);
115
  if (action == ACTION_HELP) return(showhelp());
112
  if (action == ACTION_HELP) return(showhelp());
116
 
113
 
117
  /* allocate some bits for cfg file's location */
-
 
118
  cfgfile = malloc(256);
-
 
119
  if (cfgfile == NULL) {
-
 
120
    puts("ERROR: Out of memory");
-
 
121
    return(1);
-
 
122
  }
-
 
123
 
-
 
124
  /* read all necessary environment variables */
114
  /* read all necessary environment variables */
125
  if (readenv(&dosdir, cfgfile, 256) != 0) {
115
  if (readenv(&dosdir) != 0) return(1);
126
    free(cfgfile);
-
 
127
    return(1);
-
 
128
  }
-
 
129
 
116
 
130
  /* load configuration */
117
  /* load configuration */
131
  flags = 0;
118
  flags = 0;
132
  dirlist = NULL;
119
  dirlist = NULL;
133
  if (loadconf(cfgfile, &dirlist, &flags) < 0) return(5);
120
  if (loadconf(dosdir, &dirlist, &flags) != 0) return(5);
134
 
-
 
135
  /* free the cfgfile buffer, I won't need the config file's location any more */
-
 
136
  free(cfgfile);
-
 
137
  cfgfile = NULL;
-
 
138
 
121
 
139
  switch (action) {
122
  switch (action) {
140
    case ACTION_INSTALL:
123
    case ACTION_INSTALL:
141
      res = pkginst(argv[2], flags, dosdir, dirlist);
124
      res = pkginst(argv[2], flags, dosdir, dirlist);
142
      break;
125
      break;
143
    case ACTION_REMOVE:
126
    case ACTION_REMOVE:
144
      res = pkgrem(argv[2], dosdir);
127
      res = pkgrem(argv[2], dosdir);
145
      break;
128
      break;
146
    default:
129
    default:
147
      res = showhelp();
130
      res = showhelp();
148
      break;
131
      break;
149
  }
132
  }
150
 
133
 
151
  if (res != 0) return(1);
134
  if (res != 0) return(1);
152
  return(0);
135
  return(0);
153
}
136
}
154
 
137