summaryrefslogtreecommitdiff
blob: 2711c3b7fd4ad09af7621ac5a005538502c00f05 (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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/*
 * parse.c
 *
 * Parser for Gentoo style rc-scripts.
 *
 * Copyright (C) 2004,2005 Martin Schlemmer <azarah@nosferatu.za.org>
 *
 *
 *      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 version 2 of the License.
 *
 *      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.,
 *      675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Header$
 */

#include <errno.h>
#include <string.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/poll.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>

#include "rcscripts.h"

#define READ_PIPE			0
#define WRITE_PIPE			1

/* _pipe[0] is used to send data to the parent (thus the parent only use the
 * read pipe, and the child uses the write pipe)
 * _pipe[1] is used to send data to the child (thus the child only use the read
 * pipe, and the parent uses the write pipe)
 */
#define PARENT_READ_PIPE(_pipe)		(_pipe[0][READ_PIPE])
#define PARENT_WRITE_PIPE(_pipe)	(_pipe[1][WRITE_PIPE])
#define CHILD_READ_PIPE(_pipe)		(_pipe[1][READ_PIPE])
#define CHILD_WRITE_PIPE(_pipe)		(_pipe[0][WRITE_PIPE])

#define PARSE_BUFFER_SIZE		256

static size_t parse_rcscript (char *scriptname, dyn_buf_t *data);

static size_t parse_print_start (dyn_buf_t *data);
static size_t parse_print_header (char *scriptname, dyn_buf_t *data);
static size_t parse_print_body (char *scriptname, dyn_buf_t *data);

/* Return count on success, -1 on error.  If it was critical, errno will be set. */
size_t
parse_rcscript (char *scriptname, dyn_buf_t *data)
{
  regex_data_t tmp_data;
  dyn_buf_t *dynbuf = NULL;
  char *buf = NULL;
  size_t write_count = 0;
  size_t tmp_count;

  if (!check_arg_dyn_buf (data))
    return -1;

  if (!check_arg_str (scriptname))
    return -1;

  dynbuf = new_dyn_buf_mmap_file (scriptname);
  if (NULL == dynbuf)
    {
      DBG_MSG ("Could not open '%s' for reading!\n", gbasename (scriptname));
      return -1;
    }

  DBG_MSG ("Parsing '%s'.\n", gbasename (scriptname));

  tmp_count = parse_print_header (gbasename (scriptname), data);
  if (-1 == tmp_count)
    {
      DBG_MSG ("Failed to call parse_print_header()!\n");
      goto error;
    }
  write_count += tmp_count;

  while (NULL != (buf = read_line_dyn_buf(dynbuf)))
    {
      /* Check for lines with comments, and skip them */
      DO_REGEX (tmp_data, buf, "^[ \t]*#", error);
      if (REGEX_MATCH (tmp_data))
	{
	  free (buf);
	  continue;
	}

      /* If the line contains 'depend()', call parse_print_body () and break */
      DO_REGEX (tmp_data, buf, "depend[ \t]*()[ \t]*{?", error);
      if (REGEX_MATCH (tmp_data))
	{
	  DBG_MSG ("Got 'depend()' function.\n");

	  tmp_count = parse_print_body (gbasename (scriptname), data);
	  if (-1 == tmp_count)
	    {
	      DBG_MSG ("Failed to call parse_print_body()!\n");
	      goto error;
	    }

	  write_count += tmp_count;

	  /* This is the last loop */
	  free (buf);
	  break;
	}

      free (buf);
    }

  /* read_line_dyn_buf() returned NULL with errno set */
  if ((NULL == buf) && (0 != errno))
    {
      DBG_MSG ("Failed to read line from dynamic buffer!\n");
      free_dyn_buf (dynbuf);

      return -1;
    }

  free_dyn_buf (dynbuf);

  return write_count;

error:
  if (NULL != buf)
    free (buf);
  if (NULL != dynbuf)
    free_dyn_buf (dynbuf);

  return -1;
}


size_t
generate_stage1 (dyn_buf_t *data)
{
  rcscript_info_t *info;
  size_t write_count = 0;
  size_t tmp_count;

  if (!check_arg_dyn_buf (data))
    return -1;

  write_count = parse_print_start (data);
  if (-1 == write_count)
    {
      DBG_MSG ("Failed to call parse_print_start()!\n");
      return -1;
    }

  list_for_each_entry (info, &rcscript_list, node)
    {
      tmp_count = parse_rcscript (info->filename, data);
      if (-1 == tmp_count)
	{
	  DBG_MSG ("Failed to parse '%s'!\n", gbasename (info->filename));

	  /* If 'errno' is set, it is critical (hopefully) */
	  if (0 != errno)
	    return -1;
	}
      else
	{
	  write_count += tmp_count;
	}
    }

  return write_count;
}

/* Empty signal handler for SIGPIPE */
static void
sig_handler (int signum)
{
  return;
}

/* Returns data's lenght on success, else -1 on error. */
size_t
generate_stage2 (dyn_buf_t *data)
{
  int pipe_fds[2][2] = { {0, 0}, {0, 0} };
  pid_t child_pid;
  size_t write_count = 0;
  int old_errno = 0;

  if (!check_arg_dyn_buf (data))
    return -1;

  /* Pipe to send data to parent */
  if (-1 == pipe (pipe_fds[0]))
    {
      DBG_MSG ("Failed to open pipe!\n");
      goto error;
    }
  /* Pipe to send data to child */
  if (-1 == pipe (pipe_fds[1]))
    {
      DBG_MSG ("Failed to open pipe!\n");
      /* Close parent_pfds */
      goto error;
    }

  child_pid = fork ();
  if (-1 == child_pid)
    {
      DBG_MSG ("Failed to fork()!\n");
      /* Close all pipes */
      goto error;
    }
  if (0 == child_pid)
    {
      /***
       ***   In child
       ***/

      char *const argv[] = {
	"bash",
	"--noprofile",
	"--norc",
	"--",
	NULL
      };

      /* Close the sides of the pipes we do not use */
      close (PARENT_WRITE_PIPE (pipe_fds));
      close (PARENT_READ_PIPE (pipe_fds));

      /* dup2 child side read pipe to STDIN */
      dup2 (CHILD_READ_PIPE (pipe_fds), STDIN_FILENO);
      /* dup2 child side write pipe to STDOUT */
      dup2 (CHILD_WRITE_PIPE (pipe_fds), STDOUT_FILENO);

      /* We need to be in RCSCRIPTS_INITDDIR for 'before'/'after' '*' to work */
      if (-1 == chdir (RCSCRIPTS_INITDDIR))
	{
	  DBG_MSG ("Failed to chdir to '%s'!\n", RCSCRIPTS_INITDDIR);
	  exit (EXIT_FAILURE);
	}

      if (-1 == execv (SHELL_PARSER, argv))
	{
	  DBG_MSG ("Failed to execv %s!\n", SHELL_PARSER);
	  exit (EXIT_FAILURE);
	}
    }
  else
    {
      /***
       ***   In parent
       ***/

      dyn_buf_t *stage1_data;
      struct sigaction act_new;
      struct sigaction act_old;
      struct pollfd poll_fds[2];
      int status = 0;

      DBG_MSG ("Child pid = %i\n", child_pid);

      /* Set signal handler for SIGPIPE to empty in case bash errors
       * out.  It will then close the write pipe, and instead of us
       * getting SIGPIPE, we can handle the write error like normal.
       */
      memset (&act_new, 0x00, sizeof (act_new));
      act_new.sa_handler = (void (*)(int)) sig_handler;
      sigemptyset (&act_new.sa_mask);
      act_new.sa_flags = 0;
      sigaction (SIGPIPE, &act_new, &act_old);

      /* Close the sides of the pipes we do not use */
      close (CHILD_WRITE_PIPE (pipe_fds));
      CHILD_WRITE_PIPE (pipe_fds) = 0;
      close (CHILD_READ_PIPE (pipe_fds));
      CHILD_READ_PIPE (pipe_fds) = 0;

      stage1_data = new_dyn_buf ();
      if (NULL == stage1_data)
	{
	  DBG_MSG ("Failed to allocate dynamic buffer!\n");
	  goto error;
	}

      /* Pipe parse_rcscripts() to bash */
      if (-1 == generate_stage1 (stage1_data))
	{
	  DBG_MSG ("Failed to generate stage1!\n");
	  goto error;
	}

#if 0
      int tmp_fd = open ("bar", O_CREAT | O_TRUNC | O_RDWR, 0600);
      write (tmp_fd, stage1_data->data, stage1_data->wr_index);
      close (tmp_fd);
#endif

      do
	{
	  int tmp_count = 0;
	  int do_write = 0;
	  int do_read = 0;

	  /* Check if we can write or read */
	  poll_fds[WRITE_PIPE].fd = PARENT_WRITE_PIPE (pipe_fds);
	  poll_fds[WRITE_PIPE].events = POLLOUT;
	  poll_fds[READ_PIPE].fd = PARENT_READ_PIPE (pipe_fds);
	  poll_fds[READ_PIPE].events = POLLIN | POLLPRI;
	  if (!dyn_buf_rd_eof (stage1_data))
	    {
	      poll (poll_fds, 2, -1);
	      if (poll_fds[WRITE_PIPE].revents & POLLOUT)
		do_write = 1;
	    }
	  else
	    {
	      poll (&(poll_fds[READ_PIPE]), 1, -1);
	    }
	  if ((poll_fds[READ_PIPE].revents & POLLIN)
	      || (poll_fds[READ_PIPE].revents & POLLPRI))
	    do_read = 1;

	  do
	    {
	      /* While we can write, or there is nothing to
	       * read, keep feeding the write pipe */
	      if ((dyn_buf_rd_eof (stage1_data))
		  || (1 == do_read)
		  || (1 != do_write))
		break;

	      tmp_count = read_dyn_buf_to_fd (PARENT_WRITE_PIPE (pipe_fds),
					      stage1_data, PARSE_BUFFER_SIZE);
	      if ((-1 == tmp_count) && (EINTR != errno))
		{
		  DBG_MSG ("Error writing to PARENT_WRITE_PIPE!\n");
		  goto failed;
		}
	      /* We were interrupted, try to write again */
	      if (-1 == tmp_count)
		{
		  errno = 0;
		  /* Make sure we retry */
		  tmp_count = 1;
		  continue;
		}

	      /* Close the write pipe if we done
	       * writing to get a EOF signaled to
	       * bash */
	      if (dyn_buf_rd_eof (stage1_data))
		{
		  close (PARENT_WRITE_PIPE (pipe_fds));
		  PARENT_WRITE_PIPE (pipe_fds) = 0;
		}
	    }
	  while ((tmp_count > 0) && (!dyn_buf_rd_eof (stage1_data)));

	  /* Reset tmp_count for below read loop */
	  tmp_count = 0;

	  do
	    {
	      if (1 != do_read)
		continue;

	      tmp_count = write_dyn_buf_from_fd (PARENT_READ_PIPE (pipe_fds),
						 data, PARSE_BUFFER_SIZE);
	      if ((-1 == tmp_count) && (EINTR != errno))
		{
		  DBG_MSG ("Error reading PARENT_READ_PIPE!\n");
		  goto failed;
		}
	      /* We were interrupted, try to read again */
	      if ((-1 == tmp_count) || (0 == tmp_count))
		{
		  errno = 0;
		  continue;
		}

	      write_count += tmp_count;
	    }
	  while (tmp_count > 0);
	}
      while (!(poll_fds[READ_PIPE].revents & POLLHUP));

failed:
      /* Set old_errno to disable child exit code checking below */
      if (0 != errno)
	old_errno = errno;

      free_dyn_buf (stage1_data);

      if (0 != PARENT_WRITE_PIPE (pipe_fds))
	close (PARENT_WRITE_PIPE (pipe_fds));
      close (PARENT_READ_PIPE (pipe_fds));

      /* Restore the old signal handler for SIGPIPE */
      sigaction (SIGPIPE, &act_old, NULL);

      /* Wait for bash to finish */
      waitpid (child_pid, &status, 0);
      /* If old_errno is set, we had an error in the read loop, so do
       * not worry about the child's exit code */
      if (0 == old_errno)
	{
	  if ((!WIFEXITED (status)) || (0 != WEXITSTATUS (status)))
	    {
	      /* FIXME: better errno ? */
	      errno = ECANCELED;
	      DBG_MSG ("Bash failed with status 0x%x!\n", status);

	      return -1;
	    }
	}
      else
	{
	  /* Right, we had an error, so set errno, and exit */
	  errno = old_errno;
	  return -1;
	}
    }

  return write_count;

  /* Close parent side pipes */
error:
  /* Close all pipes */
  old_errno = errno;
  if (0 != CHILD_READ_PIPE (pipe_fds))
    close (CHILD_READ_PIPE (pipe_fds));
  if (0 != CHILD_WRITE_PIPE (pipe_fds))
    close (CHILD_WRITE_PIPE (pipe_fds));
  if (0 != PARENT_READ_PIPE (pipe_fds))
    close (PARENT_READ_PIPE (pipe_fds));
  if (0 != PARENT_WRITE_PIPE (pipe_fds))
    close (PARENT_WRITE_PIPE (pipe_fds));
  /* close() might have changed it */
  errno = old_errno;

  return -1;
}

int
write_legacy_stage3 (FILE * output)
{
  service_info_t *info;
  char *service;
  int count;
  int sindex = 0;
  int dep_count;
  int i;

  if (!check_arg_fp (output))
    return -1;

  fprintf (output, "rc_type_ineed=2\n");
  fprintf (output, "rc_type_needsme=3\n");
  fprintf (output, "rc_type_iuse=4\n");
  fprintf (output, "rc_type_usesme=5\n");
  fprintf (output, "rc_type_ibefore=6\n");
  fprintf (output, "rc_type_iafter=7\n");
  fprintf (output, "rc_type_broken=8\n");
  fprintf (output, "rc_type_mtime=9\n");
  fprintf (output, "rc_index_scale=10\n\n");
  fprintf (output, "declare -a RC_DEPEND_TREE\n\n");

  list_for_each_entry (info, &service_info_list, node)
    {
      sindex++;
    }
  if (0 == sindex)
    {
      EERROR ("No services to generate dependency tree for!\n");
      return -1;
    }

  fprintf (output, "RC_DEPEND_TREE[0]=%i\n\n", sindex);

  sindex = 1;

  list_for_each_entry (info, &service_info_list, node)
    {
      fprintf (output, "RC_DEPEND_TREE[%i]=\"%s\"\n", sindex * 10, info->name);

      for (i = 0; i <= BROKEN; i++)
	{
	  dep_count = 0;

	  fprintf (output, "RC_DEPEND_TREE[%i+%i]=", (sindex * 10), (i + 2));

	  str_list_for_each_item (info->depend_info[i], service, count)
	    {
	      if (0 == dep_count)
		fprintf (output, "\"%s", service);
	      else
		fprintf (output, " %s", service);

	      dep_count++;
	    }

	  if (dep_count > 0)
	    fprintf (output, "\"\n");
	  else
	    fprintf (output, "\n");
	}

      fprintf (output, "RC_DEPEND_TREE[%i+9]=\"%li\"\n\n",
	       sindex * 10, info->mtime);
      sindex++;
    }

  fprintf (output, "RC_GOT_DEPTREE_INFO=\"yes\"\n");

  info = service_get_virtual ("logger");
  if (NULL == info)
    {
      DBG_MSG ("No service provides the 'logger' logger virtual!\n");
      fprintf (output, "\nLOGGER_SERVICE=\n");
    }
  else
    {
      fprintf (output, "\nLOGGER_SERVICE=\"%s\"\n", info->name);
    }


  return 0;
}

int
parse_cache (const dyn_buf_t *data)
{
  service_info_t *info;
  service_type_t type = ALL_SERVICE_TYPE_T;
  rcscript_info_t *rs_info;
  char *buf = NULL;
  char *rc_name = NULL;
  char *str_ptr;
  char *token;
  char *field;
  int retval;

  if (!check_arg_dyn_buf ((dyn_buf_t *) data))
    goto error;

  while (NULL != (buf = read_line_dyn_buf ((dyn_buf_t *) data)))
    {
      str_ptr = buf;

      /* Strip leading spaces/tabs */
      while ((str_ptr[0] == ' ') || (str_ptr[0] == '\t'))
	str_ptr++;

      /* Get FIELD name and FIELD value */
      token = strsep (&str_ptr, " ");

      /* FIELD name empty/bogus? */
      if ((!check_str (token))
	  /* We got an empty FIELD value */
	  || (!check_str (str_ptr)))
	{
	  errno = EMSGSIZE;
	  DBG_MSG ("Parsing stopped due to short read!\n");

	  goto error;
	}

      if (0 == strcmp (token, FIELD_RCSCRIPT))
	{
	  DBG_MSG ("Field = '%s', value = '%s'\n", token, str_ptr);

	  /* Add the service to the list, and initialize all data */
	  retval = service_add (str_ptr);
	  if (-1 == retval)
	    {
	      DBG_MSG ("Failed to add %s to service list!\n", str_ptr);
	      goto error;
	    }

	  info = service_get_info (str_ptr);
	  if (NULL == info)
	    {
	      DBG_MSG ("Failed to get info for '%s'!\n", str_ptr);
	      goto error;
	    }
	  /* Save the rc-script name for next passes of loop */
	  rc_name = info->name;

	  goto _continue;
	}

      if (NULL == rc_name)
	{
	  DBG_MSG ("Other fields should come after '%s'!\n", FIELD_RCSCRIPT);
	  goto error;
	}

      if (0 == strcmp (token, FIELD_NEED))
	type = NEED;
      else if (0 == strcmp (token, FIELD_USE))
	type = USE;
      else if (0 == strcmp (token, FIELD_BEFORE))
	type = BEFORE;
      else if (0 == strcmp (token, FIELD_AFTER))
	type = AFTER;
      else if (0 == strcmp (token, FIELD_PROVIDE))
	type = PROVIDE;
      else if (0 == strcmp (token, FIELD_FAILED))
	{
	  type = BROKEN;

	  /* FIXME: Need to think about what to do syntax BROKEN
	   * services */
	  EWARN ("'%s' has syntax errors, please correct!\n", rc_name);
	}

      if (type < ALL_SERVICE_TYPE_T)
	{
	  /* Get the first value *
	   * As the values are passed to a bash function, and we
	   * then use 'echo $*' to parse them, they should only
	   * have one space between each value ... */
	  token = strsep (&str_ptr, " ");

	  /* Get the correct type name */
	  field = service_type_names[type];

	  while (NULL != token)
	    {
	      DBG_MSG ("Field = '%s', service = '%s', value = '%s'\n",
		       field, rc_name, token);

	      retval = service_add_dependency (rc_name, token, type);
	      if (-1 == retval)
		{
		  DBG_MSG
		   ("Failed to add dependency '%s' to service '%s', type '%s'!\n",
		    token, rc_name, field);
		  goto error;
		}

	      /* Get the next value (if any) */
	      token = strsep (&str_ptr, " ");
	    }

	  goto _continue;
	}

      /* Fall through */
      DBG_MSG ("Unknown FIELD in data!\n");

_continue:
      type = ALL_SERVICE_TYPE_T;
      free (buf);
      /* Do not free 'rc_name', as it should be consistant
       * across loops */
    }

  /* read_line_dyn_buf() returned NULL with errno set */
  if ((NULL == buf) && (0 != errno))
    {
      DBG_MSG ("Failed to read line from dynamic buffer!\n");
      return -1;
    }

  /* Set the mtimes
   * FIXME: Can drop this when we no longer need write_legacy_stage3() */
  list_for_each_entry (rs_info, &rcscript_list, node)
    {
      rc_name = gbasename (rs_info->filename);
      if (NULL == service_get_info (rc_name))
	continue;

      retval = service_set_mtime (rc_name, rs_info->mtime);
      if (-1 == retval)
	{
	  DBG_MSG ("Failed to set mtime for service '%s'!\n", rc_name);
	  return -1;
	}
    }

  return 0;

error:
  free (buf);

  return -1;
}

size_t
parse_print_start (dyn_buf_t *data)
{
  size_t write_count;

  if (!check_arg_dyn_buf (data))
    return -1;

  write_count =
   sprintf_dyn_buf (data,
		    ". /sbin/functions.sh\n"
		    "[ -e /etc/rc.conf ] && . /etc/rc.conf\n"
		    "\n"
		    /*      "set -e\n" */
		    "\n");

  return write_count;
}

size_t
parse_print_header (char *scriptname, dyn_buf_t *data)
{
  size_t write_count;

  if (!check_arg_dyn_buf (data))
    return -1;

  write_count =
   sprintf_dyn_buf (data,
		    "#*** %s ***\n"
		    "\n"
		    "myservice=\"%s\"\n"
		    "echo \"RCSCRIPT ${myservice}\"\n"
		    "\n", scriptname, scriptname);

  return write_count;
}

size_t
parse_print_body (char *scriptname, dyn_buf_t *data)
{
  size_t write_count;
  char *buf = NULL;
  char *str_ptr;
  char *base;
  char *ext;

  if (!check_arg_dyn_buf (data))
    return -1;

  buf = xstrndup (scriptname, strlen (scriptname));
  if (NULL == buf)
    return -1;

  /*
   * Rather do the next block in C than bash, in case we want to
   * use ash or another shell in the place of bash
   */

  /* bash: base="${myservice%%.*}" */
  base = buf;
  str_ptr = strchr (buf, '.');
  if (NULL != str_ptr)
    {
      str_ptr[0] = '\0';
      str_ptr++;
    }
  else
    {
      str_ptr = buf;
    }
  /* bash: ext="${myservice##*.}" */
  ext = strrchr (str_ptr, '.');
  if (NULL == ext)
    ext = str_ptr;

  write_count =
   sprintf_dyn_buf (data,
		    "\n"
		    "(\n"
		    "  # Get settings for rc-script ...\n"
		    "  [ -e \"/etc/conf.d/${myservice}\" ] && \\\n"
		    "  	. \"/etc/conf.d/${myservice}\"\n"
		    "  [ -e /etc/conf.d/net ] && \\\n"
		    "  [ \"%s\" = \"net\" ] && \\\n"
		    "  [ \"%s\" != \"${myservice}\" ] && \\\n"
		    "  	. /etc/conf.d/net\n"
		    "  depend() {\n"
		    "    return 0\n"
		    "  }\n"
		    "  \n"
		    "  # Actual depend() function ...\n"
		    "  (\n"
		    "    set -e\n"
		    "    . \"/etc/init.d/%s\" >/dev/null 2>&1\n"
		    "    set +e\n"
		    "    \n"
		    "    need() {\n"
		    "      [ \"$#\" -gt 0 ] && echo \"NEED $*\"; return 0\n"
		    "    }\n"
		    "    \n"
		    "    use() {\n"
		    "      [ \"$#\" -gt 0 ] && echo \"USE $*\"; return 0\n"
		    "    }\n"
		    "    \n"
		    "    before() {\n"
		    "      [ \"$#\" -gt 0 ] && echo \"BEFORE $*\"; return 0\n"
		    "    }\n"
		    "    \n"
		    "    after() {\n"
		    "      [ \"$#\" -gt 0 ] && echo \"AFTER $*\"; return 0\n"
		    "    }\n"
		    "    \n"
		    "    provide() {\n"
		    "      [ \"$#\" -gt 0 ] && echo \"PROVIDE $*\"; return 0\n"
		    "    }\n"
		    "    \n"
		    "    depend\n"
		    "  ) || echo \"FAILED ${myservice}\"\n"
		    ")\n" "\n\n", base, ext, scriptname);

  return write_count;
}