Subversion Repositories SvarDOS

Rev

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

Rev 221 Rev 225
1
/*
1
/*
2
 * FDINST - lightweigth FreeDOS package installer
2
 * FDINST - lightweigth FreeDOS package installer
3
 * Copyright (C) 2015-2017 Mateusz Viste
3
 * Copyright (C) 2015-2017 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("FDINST v" 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
         "FDINST is a lightweigth package installer for FreeDOS. It is an alternative\n"
47
         "to FDNPKG, when only basic, local install/remove actions are necessary. FDINST\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"
48
         "is a 16-bit, 8086-compatible application running in real mode.\n"
49
         "\n"
49
         "\n"
50
         "Usage: FDINST install package.zip\n"
50
         "Usage: FDINST install package.zip\n"
51
         "       FDINST remove package\n"
51
         "       FDINST remove package\n"
52
         "\n"
52
         "\n"
53
         "FDINST is published under the MIT license, and shares most of its source code\n"
53
         "FDINST is published under the MIT license, and shares most of its source code\n"
54
         "with FDNPKG to guarantee consistent behaviour of both tools. It also uses\n"
54
         "with FDNPKG to guarantee consistent behaviour of both tools. It also uses\n"
55
         "FDNPKG's configuration file.\n"
55
         "FDNPKG's configuration file.\n"
56
         );
56
         );
57
  return(1);
57
  return(1);
58
}
58
}
59
 
59
 
60
 
60
 
61
static enum ACTIONTYPES parsearg(int argc, char **argv) {
61
static enum ACTIONTYPES parsearg(int argc, char **argv) {
62
  int extpos, i;
62
  int extpos, i;
63
  enum ACTIONTYPES res = ACTION_HELP;
63
  enum ACTIONTYPES res = ACTION_HELP;
64
  /* I expect exactly 2 arguments (ie argc == 3) */
64
  /* I expect exactly 2 arguments (ie argc == 3) */
65
  if (argc != 3) return(ACTION_HELP);
65
  if (argc != 3) return(ACTION_HELP);
66
  /* look for valid actions */
66
  /* look for valid actions */
67
  if (strcasecmp(argv[1], "install") == 0) {
67
  if (strcasecmp(argv[1], "install") == 0) {
68
    res = ACTION_INSTALL;
68
    res = ACTION_INSTALL;
69
  } else if (strcasecmp(argv[1], "remove") == 0) {
69
  } else if (strcasecmp(argv[1], "remove") == 0) {
70
    res = ACTION_REMOVE;
70
    res = ACTION_REMOVE;
71
  }
71
  }
72
  /* the argument should never be empty */
72
  /* the argument should never be empty */
73
  if (argv[2][0] == 0) return(ACTION_INSTALL);
73
  if (argv[2][0] == 0) return(ACTION_INSTALL);
74
  /* for 'install', validate that the extension is '.zip' */
74
  /* for 'install', validate that the extension is '.zip' */
75
  if (res == ACTION_INSTALL) {
75
  if (res == ACTION_INSTALL) {
76
    /* find where the file's extension starts */
76
    /* find where the file's extension starts */
77
    extpos = 0;
77
    extpos = 0;
78
    for (i = 0; argv[2][i] != 0; i++) {
78
    for (i = 0; argv[2][i] != 0; i++) {
79
      if (argv[2][i] == '.') extpos = i + 1;
79
      if (argv[2][i] == '.') extpos = i + 1;
80
    }
80
    }
81
    if (extpos == 0) return(ACTION_HELP);
81
    if (extpos == 0) return(ACTION_HELP);
82
  }
82
  }
83
  /* return the result */
83
  /* return the result */
84
  return(res);
84
  return(res);
85
}
85
}
86
 
86
 
87
 
87
 
88
static int pkginst(char *file, int flags, char *dosdir, char *tempdir, struct customdirs *dirlist, char *mapdrv) {
88
static int pkginst(char *file, int flags, char *dosdir, char *tempdir, struct customdirs *dirlist) {
89
  char pkgname[32];
89
  char pkgname[32];
90
  int t, lastpathdelim = -1, u = 0;
90
  int t, lastpathdelim = -1, u = 0;
91
  char *buffmem1k;
91
  char *buffmem1k;
92
  struct ziplist *zipfileidx;
92
  struct ziplist *zipfileidx;
93
  FILE *zipfilefd;
93
  FILE *zipfilefd;
94
  for (t = 0; file[t] != 0; t++) {
94
  for (t = 0; file[t] != 0; t++) {
95
    if ((file[t] == '/') || (file[t] == '\\')) lastpathdelim = t;
95
    if ((file[t] == '/') || (file[t] == '\\')) lastpathdelim = t;
96
  }
96
  }
97
  /* copy the filename into pkgname (without path elements) */
97
  /* copy the filename into pkgname (without path elements) */
98
  for (t = lastpathdelim + 1; file[t] != 0; t++) pkgname[u++] = file[t];
98
  for (t = lastpathdelim + 1; file[t] != 0; t++) pkgname[u++] = file[t];
99
  pkgname[u] = 0; /* terminate the string */
99
  pkgname[u] = 0; /* terminate the string */
100
  /* truncate the file's extension (.zip) */
100
  /* truncate the file's extension (.zip) */
101
  for (t = u; t > 0; t--) {
101
  for (t = u; t > 0; t--) {
102
    if (pkgname[t] == '.') {
102
    if (pkgname[t] == '.') {
103
      pkgname[t] = 0;
103
      pkgname[t] = 0;
104
      break;
104
      break;
105
    }
105
    }
106
  }
106
  }
107
  /* allocate some memory for pkginst_preparepackage() to do its job */
107
  /* allocate some memory for pkginst_preparepackage() to do its job */
108
  buffmem1k = malloc(1024);
108
  buffmem1k = malloc(1024);
109
  if (buffmem1k == NULL) {
109
  if (buffmem1k == NULL) {
110
    puts("ERROR: Out of memory");
110
    puts("ERROR: Out of memory");
111
    return(1);
111
    return(1);
112
  }
112
  }
113
  /* prepare the zip file and install it */
113
  /* prepare the zip file and install it */
114
  zipfileidx = pkginstall_preparepackage(NULL, pkgname, tempdir, file, flags, NULL, &zipfilefd, NULL, 0, NULL, dosdir, dirlist, buffmem1k, mapdrv);
114
  zipfileidx = pkginstall_preparepackage(pkgname, file, flags, &zipfilefd, dosdir, dirlist, buffmem1k);
115
  free(buffmem1k);
115
  free(buffmem1k);
116
  if (zipfileidx != NULL) {
116
  if (zipfileidx != NULL) {
117
    int res = 0;
117
    int res = 0;
118
    if (pkginstall_installpackage(pkgname, dosdir, dirlist, zipfileidx, zipfilefd, mapdrv) != 0) res = 1;
118
    if (pkginstall_installpackage(pkgname, dosdir, dirlist, zipfileidx, zipfilefd) != 0) res = 1;
119
    fclose(zipfilefd);
119
    fclose(zipfilefd);
120
    return(res);
120
    return(res);
121
  } else {
121
  } else {
122
    fclose(zipfilefd);
122
    fclose(zipfilefd);
123
    return(1);
123
    return(1);
124
  }
124
  }
125
}
125
}
126
 
126
 
127
 
127
 
128
int main(int argc, char **argv) {
128
int main(int argc, char **argv) {
129
  int res, flags;
129
  int res, flags;
130
  enum ACTIONTYPES action;
130
  enum ACTIONTYPES action;
131
  char *dosdir, *tempdir, *cfgfile;
131
  char *dosdir, *tempdir, *cfgfile;
132
  struct customdirs *dirlist;
132
  struct customdirs *dirlist;
133
  char *mapdrv = "";
-
 
134
 
133
 
135
  action = parsearg(argc, argv);
134
  action = parsearg(argc, argv);
136
  if (action == ACTION_HELP) return(showhelp());
135
  if (action == ACTION_HELP) return(showhelp());
137
 
136
 
138
  /* allocate some bits for cfg file's location */
137
  /* allocate some bits for cfg file's location */
139
  cfgfile = malloc(256);
138
  cfgfile = malloc(256);
140
  if (cfgfile == NULL) {
139
  if (cfgfile == NULL) {
141
    puts("ERROR: Out of memory");
140
    puts("ERROR: Out of memory");
142
    return(1);
141
    return(1);
143
  }
142
  }
144
 
143
 
145
  /* read all necessary environment variables */
144
  /* read all necessary environment variables */
146
  if (readenv(&dosdir, &tempdir, cfgfile, 256) != 0) {
145
  if (readenv(&dosdir, &tempdir, cfgfile, 256) != 0) {
147
    free(cfgfile);
146
    free(cfgfile);
148
    return(1);
147
    return(1);
149
  }
148
  }
150
 
149
 
151
  /* load configuration */
150
  /* load configuration */
152
  flags = 0;
151
  flags = 0;
153
  dirlist = NULL;
152
  dirlist = NULL;
154
  if (loadconf(cfgfile, NULL, 0, NULL, NULL, &dirlist, &flags, NULL, NULL, &mapdrv) < 0) return(5);
153
  if (loadconf(cfgfile, &dirlist, &flags) < 0) return(5);
155
 
154
 
156
  /* free the cfgfile buffer, I won't need the config file's location any more */
155
  /* free the cfgfile buffer, I won't need the config file's location any more */
157
  free(cfgfile);
156
  free(cfgfile);
158
  cfgfile = NULL;
157
  cfgfile = NULL;
159
 
158
 
160
  switch (action) {
159
  switch (action) {
161
    case ACTION_INSTALL:
160
    case ACTION_INSTALL:
162
      res = pkginst(argv[2], flags, dosdir, tempdir, dirlist, mapdrv);
161
      res = pkginst(argv[2], flags, dosdir, tempdir, dirlist);
163
      break;
162
      break;
164
    case ACTION_REMOVE:
163
    case ACTION_REMOVE:
165
      res = pkgrem(argv[2], dosdir, mapdrv);
164
      res = pkgrem(argv[2], dosdir);
166
      break;
165
      break;
167
    default:
166
    default:
168
      res = showhelp();
167
      res = showhelp();
169
      break;
168
      break;
170
  }
169
  }
171
 
170
 
172
  if (res != 0) return(1);
171
  if (res != 0) return(1);
173
  return(0);
172
  return(0);
174
}
173
}
175
 
174