File: optutil.c

package info (click to toggle)
xcopilot 1%3A0.6.6
  • links: PTS
  • area: contrib
  • in suites: potato, slink
  • size: 3,252 kB
  • ctags: 3,406
  • sloc: ansic: 37,932; cpp: 1,918; sh: 329; makefile: 68
file content (129 lines) | stat: -rw-r--r-- 4,119 bytes parent folder | download
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
/* option utilities library
   Copyright 1998 Gene McCulley <mcculley@cuspy.com>, Cuspy Solutions, 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 2 of the License, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "config.h"
#include "optutil.h"

XtResource *OU_GetResources(const ResourceOption *ros, unsigned num_ros)
{
  unsigned k;

  XtResource *resources = (XtResource *)XtMalloc(sizeof(XtResource) * num_ros);
  for (k = 0; k < num_ros; k++)
    resources[k] = ros[k].resource;

  return resources;
}

XrmOptionDescRec *OU_GetOptions(const ResourceOption *ros, unsigned num_ros)
{
  unsigned k;

  XrmOptionDescRec *options =
    (XrmOptionDescRec *)XtMalloc(sizeof(XrmOptionDescRec) * num_ros);
  for (k = 0; k < num_ros; k++)
    options[k] = ros[k].option;

  return options;
}

void OU_DumpOptions(const ResourceOption *ros, unsigned num_ros, FILE *fp)
{
  unsigned j;
  unsigned biggest_option = 0;
  unsigned biggest_description = 0;
  const unsigned spacing = 2; /* Spacing between categories in help output. */
  const char format_template[] = "%%-%us%%-%us";
  char format[sizeof(format_template)]; /* Here we are assuming that the result
                                           is never bigger than the template. */

  for (j = 0; j < num_ros; j++) {
    unsigned option_length = strlen(ros[j].option.option) +
      (ros[j].parameters ? strlen(ros[j].parameters) + 1 : 0);
                               /* The 1 here is a space added between the
                                  option and its parameter description, if it
                                  has one. */

    if (option_length > biggest_option)
      biggest_option = option_length;
    if (strlen(ros[j].help) > biggest_description)
      biggest_description = strlen(ros[j].help);
  }

  assert(sprintf(format, format_template, biggest_option + spacing,
                 biggest_description + spacing) < (int)(sizeof(format)));
  fprintf(fp, format, "Option", "Description");
  fprintf(fp, "Default\n");
  for (j = 0; j < num_ros; j++) {
    char *whole_option = (char *)alloca(biggest_option + 1);

    sprintf(whole_option, "%s %s", ros[j].option.option,
            ros[j].parameters ? ros[j].parameters : "");
    fprintf(fp, format, whole_option, ros[j].help);
    if (ros[j].resource.default_type == XtRString) {
      fprintf(fp, "%s\n", (char *)ros[j].resource.default_addr);
    }
    else {
      fprintf(fp, "\nerror: unknown type %s for %s at %s:%d\n",
              ros[j].resource.default_type, ros[j].option.option, __FILE__,
              __LINE__);
      return;
    }
  }
}

void OU_GetEnvironment(const char *varname, char **argv[], int *argc)
{
  char *env_args;

  if (!(env_args = getenv(varname)))
    return;
  else {
    unsigned num_args = 0;
    char **env_argv = NULL;
    char *arg;

    while ((arg = strsep(&env_args, " \t\n\r\f\v"))) {
      if (strlen(arg)) {
        num_args++;
        env_argv = (char **)realloc(env_argv, sizeof(char *) * num_args);
        env_argv[num_args - 1] = arg;
      }
    }

    if (num_args) {
      char **new_argv = (char **)malloc(sizeof(char *) * (num_args + *argc) +
                                        1);
      unsigned i;

      for (i = 0; i < (unsigned)*argc; i++)
        new_argv[i] = (*argv)[i];

      for (i = *argc; i < num_args + *argc; i++)
        new_argv[i] = env_argv[i - *argc];

      new_argv[i] = NULL;

      *argv = new_argv;
      *argc = num_args + *argc;
    }
  }
}