summaryrefslogtreecommitdiff
path: root/src/unshar.c
blob: cb4f4927841f64bf2df6a8ab021290fed1dd734f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/* Handle so called `shell archives'.

   Copyright (C) 1994, 1995, 1996, 2002, 2005,
   2007 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/* Unpackage one or more shell archive files.  The `unshar' program is a
   filter which removes the front part of a file and passes the rest to
   the `sh' command.  It understands phrases like "cut here", and also
   knows about shell comment characters and the Unix commands `echo',
   `cat', and `sed'.  */

#include "system.h"
#include "getopt.h"
#include "basename.h"
#include "error.h"
#include "exit.h"
#include "stpcpy.h"
#include "xgetcwd.h"

#if HAVE_LOCALE_H
# include <locale.h>
#else
# define setlocale(Category, Locale)
#endif
#include "gettext.h"
#define _(str) gettext (str)

/* Buffer size for shell process input.  */
#define SHELL_BUFFER_SIZE 8196

#define EOL '\n'

/* The name this program was run with. */
const char *program_name;

/* If non-zero, display usage information and exit.  */
static int show_help = 0;

/* If non-zero, print the version on standard output and exit.  */
static int show_version = 0;

static int pass_c_flag = 0;
static int continue_reading = 0;
static const char *exit_string = "exit 0";
static size_t exit_string_length;
static char *current_directory;

/*-------------------------------------------------------------------.
| Match the leftmost part of a string.  Returns 1 if initial	     |
| characters of DATA match PATTERN exactly; else 0.  This was	     |
| formerly a function.  But because we always have a constant string |
| as the seconf argument and the length of the second argument is a  |
| lot of shorter than the buffer the first argument is pointing at,  |
| we simply use `memcmp'.  And one more point: even if the `memcmp'  |
| function does not work correct for 8 bit characters it does not    |
| matter here.  We are only interested in equal or not equal	     |
| information.							     |
`-------------------------------------------------------------------*/

#define starting_with(data, pattern)					    \
  (memcmp (data, pattern, sizeof (pattern) - 1) == 0)

static FILE* load_stdin (char **p_name_buf);

/*-------------------------------------------------------------------------.
| For a DATA string and a PATTERN containing one or more embedded	   |
| asterisks (matching any number of characters), return non-zero if the	   |
| match succeeds, and set RESULT_ARRAY[I] to the characters matched by the |
| I'th *.								   |
`-------------------------------------------------------------------------*/

static int
matched_by (data, pattern, result_array)
     const char *data;
     const char *pattern;
     char **result_array;
{
  const char *pattern_cursor = NULL;
  const char *data_cursor = NULL;
  char *result_cursor = NULL;
  int number_of_results = 0;

  while (1)
    if (*pattern == '*')
      {
	pattern_cursor = ++pattern;
	data_cursor = data;
	result_cursor = result_array[number_of_results++];
	*result_cursor = '\0';
      }
    else if (*data == *pattern)
      {
	if (*pattern == '\0')
	  /* The pattern matches.  */
	  return 1;

	pattern++;
	data++;
      }
    else
      {
	if (*data == '\0')
	  /* The pattern fails: no more data.  */
	  return 0;

	if (pattern_cursor == NULL)
	  /* The pattern fails: no star to adjust.  */
	  return 0;

	/* Restart pattern after star.  */

	pattern = pattern_cursor;
	*result_cursor++ = *data_cursor;
	*result_cursor = '\0';

	/* Rescan after copied char.  */

	data = ++data_cursor;
      }
}

/*------------------------------------------------------------------------.
| Associated with a given file NAME, position FILE at the start of the	  |
| shell command portion of a shell archive file.  Scan file from position |
| START.								  |
`------------------------------------------------------------------------*/

static int
find_archive (name, file, start)
     const char *name;
     FILE *file;
     off_t start;
{
  char buffer[BUFSIZ];
  off_t position;

  /* Results from star matcher.  */

  static char res1[BUFSIZ], res2[BUFSIZ], res3[BUFSIZ], res4[BUFSIZ];
  static char *result[] = {res1, res2, res3, res4};

  fseeko (file, start, SEEK_SET);

  while (1)
    {

      /* Record position of the start of this line.  */

      position = ftello (file);

      /* Read next line, fail if no more and no previous process.  */

      if (!fgets (buffer, BUFSIZ, file))
	{
	  if (!start)
	    error (0, 0, _("Found no shell commands in %s"), name);
	  return 0;
	}

      /* Bail out if we see C preprocessor commands or C comments.  */

      if (starting_with (buffer, "#include")
	  || starting_with (buffer, "# include")
	  || starting_with (buffer, "#define")
	  || starting_with (buffer, "# define")
	  || starting_with (buffer, "#ifdef")
	  || starting_with (buffer, "# ifdef")
	  || starting_with (buffer, "#ifndef")
	  || starting_with (buffer, "# ifndef")
	  || starting_with (buffer, "/*"))
	{
	  error (0, 0, _("%s looks like raw C code, not a shell archive"),
		 name);
	  return 0;
	}

      /* Does this line start with a shell command or comment.  */

      if (starting_with (buffer, "#")
	  || starting_with (buffer, ":")
	  || starting_with (buffer, "echo ")
	  || starting_with (buffer, "sed ")
	  || starting_with (buffer, "cat ")
	  || starting_with (buffer, "if "))
	{
	  fseeko (file, position, SEEK_SET);
	  return 1;
	}

      /* Does this line say "Cut here".  */

      if (matched_by (buffer, "*CUT*HERE*", result) ||
	  matched_by (buffer, "*cut*here*", result) ||
	  matched_by (buffer, "*TEAR*HERE*", result) ||
	  matched_by (buffer, "*tear*here*", result) ||
	  matched_by (buffer, "*CUT*CUT*", result) ||
	  matched_by (buffer, "*cut*cut*", result))
	{

	  /* Read next line after "cut here", skipping blank lines.  */

	  while (1)
	    {
	      position = ftello (file);

	      if (!fgets (buffer, BUFSIZ, file))
		{
		  error (0, 0, _("Found no shell commands after `cut' in %s"),
			 name);
		  return 0;
		}

	      if (*buffer != '\n')
		break;
	    }

	  /* Win if line starts with a comment character of lower case
	     letter.  */

	  if (*buffer == '#' || *buffer == ':'
	      || (('a' <= *buffer) && ('z' >= *buffer)))
	    {
	      fseeko (file, position, SEEK_SET);
	      return 1;
	    }

	  /* Cut here message lied to us.  */

	  error (0, 0, _("%s is probably not a shell archive"), name);
	  error (0, 0, _("The `cut' line was followed by: %s"), buffer);
	  return 0;
	}
    }
}

/*-----------------------------------------------------------------.
| Unarchive a shar file provided on file NAME.  The file itself is |
| provided on the already opened FILE.				   |
`-----------------------------------------------------------------*/

static void
unarchive_shar_file (name, file)
     const char *name;
     FILE *file;
{
  char buffer[SHELL_BUFFER_SIZE];
  FILE *shell_process;
  off_t current_position = 0;
  char *more_to_read;

  while (find_archive (name, file, current_position))
    {
      printf ("%s:\n", name);
      shell_process = popen (pass_c_flag ? "sh -s - -c" : "sh", "w");
      if (!shell_process)
	error (EXIT_FAILURE, errno, _("Starting `sh' process"));

      if (!continue_reading)
	{
	  size_t len;

	  while ((len = fread (buffer, 1, SHELL_BUFFER_SIZE, file)) != 0)
	    fwrite (buffer, 1, len, shell_process);
#if 0
	  /* Don't know whether a test is appropriate here.  */
	  if (ferror (shell_process) != 0)
	    fwrite (buffer, length, 1, shell_process);
#endif
	  pclose (shell_process);
	  break;
	}
      else
	{
	  while (more_to_read = fgets (buffer, SHELL_BUFFER_SIZE, file),
		 more_to_read != NULL)
	    {
	      fputs (buffer, shell_process);
	      if (!strncmp (exit_string, buffer, exit_string_length))
		break;
	    }
	  pclose (shell_process);

	  if (more_to_read)
	    current_position = ftello (file);
	  else
	    break;
	}
    }
}

/*-----------------------------.
| Explain how to use program.  |
`-----------------------------*/

static void
usage (status)
     int status;
{
  if (status != EXIT_SUCCESS)
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
	     program_name);
  else
    {
      printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
      fputs (_("\
Mandatory arguments to long options are mandatory for short options too.\n\
\n\
  -d, --directory=DIRECTORY   change to DIRECTORY before unpacking\n\
  -c, --overwrite             pass -c to shar script for overwriting files\n\
  -e, --exit-0                same as `--split-at=\"exit 0\"'\n\
  -E, --split-at=STRING       split concatenated shars after STRING\n\
  -f, --force                 same as `-c'\n\
      --help                  display this help and exit\n\
      --version               output version information and exit\n\
\n\
If no FILE, standard input is read.\n"),
	     stdout);
      /* TRANSLATORS: add the contact address for your translation team! */
      printf (_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
    }
  exit (status);
}

/*--------------------------------------.
| Decode options and launch execution.  |
`--------------------------------------*/

static const struct option long_options[] =
{
  {"directory", required_argument, NULL, 'd'},
  {"exit-0", no_argument, NULL, 'e'},
  {"force", no_argument, NULL, 'f'},
  {"overwrite", no_argument, NULL, 'c'},
  {"split-at", required_argument, NULL, 'E'},

  {"help", no_argument, &show_help, 1},
  {"version", no_argument, &show_version, 1},

  { NULL, 0, NULL, 0 },
};

int
main (argc, argv)
     int argc;
     char *const *argv;
{
  FILE *file;
  char* name_buffer = NULL;
  int optchar;

  program_name = argv[0];
  setlocale (LC_ALL, "");

  /* Set the text message domain.  */
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

#ifdef __MSDOS__
  setbuf (stdout, NULL);
  setbuf (stderr, NULL);
#endif

  if (current_directory = xgetcwd (), !current_directory)
    error (EXIT_FAILURE, errno, _("Cannot get current directory name"));

  /* Process options.  */

  while (optchar = getopt_long (argc, argv, "E:cd:ef", long_options, NULL),
	 optchar != EOF)
    switch (optchar)
      {
      case '\0':
	break;

      case 'c':
      case 'f':
	pass_c_flag = 1;
	break;

      case 'd':
	if (chdir (optarg) == -1)
	  error (2, 0, _("Cannot chdir to `%s'"), optarg);
	break;

      case 'E':
	exit_string = optarg;
	/* Fall through.  */

      case 'e':
	continue_reading = 1;
	exit_string_length = strlen (exit_string);
	break;

      default:
	usage (EXIT_FAILURE);
      }

  if (show_version)
    {
      printf ("%s (GNU %s) %s\n", basename (program_name), PACKAGE, VERSION);
      /* xgettext: no-wrap */
      printf (_("Copyright (C) %s Free Software Foundation, Inc.\n\
This is free software; see the source for copying conditions.  There is NO\n\
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
"),
	      "1994, 1995, 1996, 2005");
      exit (EXIT_SUCCESS);
    }

  if (show_help)
    usage (EXIT_SUCCESS);

  if (optind < argc)
    {
      size_t buflen = 0;
      argv += optind;

      for (;;)
        {
          char* arg = *(argv++);
          size_t sz;
          if (arg == NULL)
            break;

          sz = strlen (current_directory) + strlen (arg) + 2;
          if (sz > buflen)
            {
              buflen = sz + 32;
              name_buffer = (name_buffer == NULL)
                ? malloc (buflen)
                : realloc (name_buffer, buflen);
              if (name_buffer == NULL)
                error (EXIT_FAILURE, ENOMEM, _("allocate file name buffer"));
            }

          if (*arg == '/')
            strcpy (name_buffer, arg);
          else
            {
              char *cp = stpcpy (name_buffer, current_directory);
              *cp++ = '/';
              strcpy (cp, arg);
            }
          if (file = fopen (name_buffer, "r"), !file)
            error (EXIT_FAILURE, errno, name_buffer);
          unarchive_shar_file (name_buffer, file);
          fclose (file);
        }
    }
  else
    {
      file = load_stdin (&name_buffer);

      unarchive_shar_file (_("standard input"), file);

      fclose (file);
      unlink (name_buffer);
    }

  exit (EXIT_SUCCESS);
}


static FILE*
load_stdin (char **p_name_buf)
{
  static const char z_tmpfile[] = "unsh.XXXXXX";
  char *pz_fname;
  FILE *fp;

  /*
   * FIXME: actually configure this stuff:
   */
#if defined(_SC_PAGESIZE)
  long pg_sz = sysconf (_SC_PAGESIZE);
#elif defined(_SC_PAGE_SIZE)
  long pg_sz = sysconf (_SC_PAGE_SIZE);
#elif defined(HAVE_GETPAGESIZE)
  long pg_sz = getpagesize();
#else
# define pg_sz 8192
#endif

  {
    size_t name_size;
    char *pz_tmp = getenv ("TMPDIR");

    if (pz_tmp == NULL)
      pz_tmp = "/tmp";

    name_size = strlen (pz_tmp) + sizeof (z_tmpfile) + 1;
    *p_name_buf = pz_fname = malloc (name_size);

    if (pz_fname == NULL)
      error (EXIT_FAILURE, ENOMEM, _("allocate file name buffer"));

    sprintf (pz_fname, "%s/%s", pz_tmp, z_tmpfile);
  }

  {
    int fd = mkstemp (pz_fname);
    if (fd < 0)
      error (EXIT_FAILURE, errno, pz_fname);

    fp = fdopen (fd, "w+");
  }

  if (fp == NULL)
    error (EXIT_FAILURE, errno, pz_fname);

  {
    char *buf = malloc (pg_sz);
    size_t size_read;

    if (buf == NULL)
      error (EXIT_FAILURE, ENOMEM, _("allocate file buffer"));

    while (size_read = fread (buf, 1, pg_sz, stdin),
           size_read != 0)
      fwrite (buf, size_read, 1, fp);

    free (buf);
  }

  rewind (fp);

  return fp;
}

/*
 * Local Variables:
 * mode: C
 * c-file-style: "gnu"
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 * end of agen5/autogen.c */