1 /* cmd-valid-package-name.c:
2  *
3  ****************************************************************
4  * Copyright (C) 2003 Tom Lord
5  *
6  * See the file "COPYING" for further information about
7  * the copyright and warranty status of this work.
8  */
9 
10 
11 #include "config-options.h"
12 #include "hackerlab/cmd/main.h"
13 #include "tla/libarch/namespace.h"
14 #include "tla/libarch/cmd-valid-package-name.h"
15 
16 
17 
18 static t_uchar * usage = "[options] name";
19 static t_uchar * version_string = (cfg__std__package " from regexps.com\n"
20                                    "\n"
21                                    "Copyright 2003 Tom Lord\n"
22                                    "\n"
23                                    "This is free software; see the source for copying conditions.\n"
24                                    "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n"
25                                    "PARTICULAR PURPOSE.\n"
26                                    "\n"
27                                    "Report bugs to " cfg__tla_bug_mail ".\n"
28                                    "\n"
29                                    cfg__std__release_id_string
30                                    "\n");
31 
32 #define OPTS(OP) \
33   OP (opt_help_msg, "h", "help", 0, \
34       "Display a help message and exit.") \
35   OP (opt_long_help, "H", 0, 0, \
36       "Display a verbose help message and exit.") \
37   OP (opt_version, "V", "version", 0, \
38       "Display a release identifier string\n" \
39       "and exit.") \
40   OP (opt_errname, "e", "errname", 1, \
41       "specify program name for errors") \
42   OP (opt_archive, 0, "archive", 0, \
43       "require and explicit archive") \
44   OP (opt_no_archive, 0, "no-archive", 0, \
45       "prohibit and explicit archive") \
46   OP (opt_category, "c", "category", 0, \
47       "require a category") \
48   OP (opt_package, "p", "package", 0, \
49       "require category, permit branch") \
50   OP (opt_vsn, "v", "vsn", 0, \
51       "require a version number") \
52   OP (opt_level, "l", "patch-level", 0, \
53       "require a patch level") \
54   OP (opt_lvl, 0, "lvl", 0, \
55       "synonym for --patch-level") \
56   OP (opt_tolerant, "t", "tolerant", 0, \
57       "tolerate more specific names")
58 
59 t_uchar arch_cmd_valid_package_name_help[] = ("test a package name for validity\n"
60                                               "Exit with status 0 if PACKAGE is a valid package name,\n"
61                                               "status 1 otherwise.\n"
62                                               "\n"
63                                               "By default, require a basename or basename plus branch label.\n"
64                                               "\n"
65                                               "Options -v and -l cancel -b and vice versa.\n"
66                                               "\n"
67                                               "Option -l implies -v.\n"
68                                               "\n"
69                                               "If an error name is specified (-e or --errname), then invalid\n"
70                                               "names cause an error message on stdout.  Otherwise, the exit\n"
71                                               "status is the only output.\n"
72                                               "\n"
73                                               "By default, validation is strict.  For example, -b checks\n"
74                                               "for a valid basename and does not permit a branch label\n"
75                                               "or version number.\n"
76                                               "\n"
77                                               "With -t, more specific names are permitted.  For example, -b\n"
78                                               "will permit a branch name, version number, and patch level.\n");
79 
80 enum options
81 {
82   OPTS (OPT_ENUM)
83 };
84 
85 static struct opt_desc opts[] =
86 {
87   OPTS (OPT_DESC)
88     {-1, 0, 0, 0, 0}
89 };
90 
91 
92 
93 int
arch_cmd_valid_package_name(t_uchar * program_name,int argc,char * argv[])94 arch_cmd_valid_package_name (t_uchar * program_name, int argc, char * argv[])
95 {
96   int o;
97   struct opt_parsed * option;
98   t_uchar * errname;
99   enum arch_valid_package_name_archive archive_disposition;
100   enum arch_valid_package_name_archive package_name_type;
101   char * type_name;
102   int tolerant;
103   int result;
104 
105   errname = 0;
106   archive_disposition = arch_maybe_archive;
107   package_name_type = arch_req_package;
108   type_name = "package";
109   tolerant = 0;
110 
111   safe_buffer_fd (1, 0, O_WRONLY, 0);
112 
113   option = 0;
114 
115   while (1)
116     {
117       o = opt_standard (lim_use_must_malloc, &option, opts, &argc, argv, program_name, usage, version_string, arch_cmd_valid_package_name_help, opt_help_msg, opt_long_help, opt_version);
118       if (o == opt_none)
119         break;
120       switch (o)
121         {
122         default:
123           safe_printfmt (2, "unhandled option `%s'\n", option->opt_string);
124           panic ("internal error parsing arguments");
125 
126         usage_error:
127           opt_usage (2, argv[0], program_name, usage, 1);
128           exit (1);
129 
130           /* bogus_arg: */
131           safe_printfmt (2, "ill-formed argument for `%s' (`%s')\n", option->opt_string, option->arg_string);
132           goto usage_error;
133 
134 
135         case opt_errname:
136           {
137             errname = str_save (0, option->arg_string);
138             break;
139           }
140 
141         case opt_archive:
142           {
143             archive_disposition = arch_req_archive;
144             break;
145           }
146 
147         case opt_no_archive:
148           {
149             archive_disposition = arch_no_archive;
150             break;
151           }
152 
153         case opt_category:
154           {
155             package_name_type = arch_req_category;
156             type_name = "category";
157             break;
158           }
159 
160         case opt_package:
161           {
162             package_name_type = arch_req_package;
163             type_name = "package";
164             break;
165           }
166 
167         case opt_vsn:
168           {
169             package_name_type = arch_req_version;
170             type_name = "version";
171             break;
172           }
173 
174         case opt_level:
175         case opt_lvl:
176           {
177             package_name_type = arch_req_patch_level;
178             type_name = "patch-level";
179             break;
180           }
181 
182         case opt_tolerant:
183           {
184             tolerant = 1;
185             break;
186           }
187         }
188     }
189 
190   if (argc != 2)
191     goto usage_error;
192 
193   result = arch_valid_package_name (argv[1], archive_disposition, package_name_type, tolerant);
194 
195   if (!result)
196     {
197       if (errname)
198         {
199           safe_printfmt (2, "%s: invalid %s id (%s)\n", errname, type_name, argv[1]);
200         }
201       exit (1);
202     }
203 
204   return 0;
205 }
206 
207 
208 
209 
210 /* tag: Tom Lord Mon May 12 11:13:10 2003 (valid-package-name.c)
211  */
212