~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
cvs/emstar/link/liblink/link_opt.c


  1 /*
  2  *
  3  * Copyright (c) 2003 The Regents of the University of California.  All 
  4  * rights reserved.
  5  *
  6  * Redistribution and use in source and binary forms, with or without
  7  * modification, are permitted provided that the following conditions
  8  * are met:
  9  *
 10  * - Redistributions of source code must retain the above copyright
 11  *   notice, this list of conditions and the following disclaimer.
 12  *
 13  * - Neither the name of the University nor the names of its
 14  *   contributors may be used to endorse or promote products derived
 15  *   from this software without specific prior written permission.
 16  *
 17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
 18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 20  * PARTICULAR  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
 21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 25  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28  *
 29  */
 30 
 31 /*
 32  *  link_opt.c
 33  *
 34  *  Options parsing and manilpulation for link devices
 35  *
 36  *  Author: cerpa
 37  *
 38  *  $Id: link_opt.c,v 1.8 2005-05-18 05:54:17 girod Exp $
 39  */
 40 
 41 #include "liblink_i.h"
 42 #include <libmisc/misc.h>
 43 
 44 
 45 /*
 46  *  Simple command line parser functions
 47  */ 
 48 
 49 char *link_parse_uses(int *argc, char **argv, char *default_name)
 50 {
 51   char *uses = misc_parse_out_option(argc, argv, "uses", 'U');
 52   if (uses == NULL) uses = default_name;
 53   return uses;
 54 }
 55 
 56 
 57 char *link_parse_provides(int *argc, char **argv, char *default_name)
 58 {
 59   char *provides = misc_parse_out_option(argc, argv, "provides", 'P');
 60   if (provides == NULL) provides = default_name;
 61   return provides;
 62 }
 63 
 64 
 65 char **link_parse_provides_multi(int *argc, char **argv, char *default_name)
 66 {
 67   char *provides = link_parse_provides(argc, argv, default_name);
 68   return misc_parse_csv_to_vector(provides);
 69 }
 70 
 71 
 72 char **link_parse_uses_multi(int *argc, char **argv, char *default_name)
 73 {
 74   char *uses = link_parse_uses(argc, argv, default_name);
 75   return misc_parse_csv_to_vector(uses);
 76 }
 77 
 78 
 79 char *link_parse_if_class(int *argc, char **argv, char *default_class)
 80 {
 81   char *class = misc_parse_out_option(argc, argv, "ifclass", 'I');
 82   if (class == NULL) class = default_class;
 83   return class;
 84 }
 85 
 86 
 87 char *link_parse_uses_class(int *argc, char **argv, char *default_class)
 88 {
 89   char *class = misc_parse_out_option(argc, argv, "uses_class", 0);
 90   if (class == NULL) class = default_class;
 91   return class;
 92 }
 93 
 94 /*
 95  *  Full helper interface
 96  */
 97 
 98 int link_addstring(link_opts_t *opts, char *string)
 99 {
100   char buf[FUSD_MAX_NAME_LENGTH];
101   
102   if (opts == NULL) {
103     elog(LOG_WARNING, "Passing invalid (null) arguments!");
104     return -EINVAL;  
105   }
106   
107   /* concatenate the device name with the new string */
108   sprintf(buf, "%s/%s", opts->name, string);
109   
110   /* replace the old device name with the new name */
111   if ((opts->name = strdup(buf)) == NULL) {
112     elog(LOG_WARNING, "Insufficient memory, allocation failed!");  
113     return -ENOMEM;
114   }
115   
116   return 0;
117 }
118 
119 
120 int link_addone(link_opts_t **opts, char *string)
121 {
122   if (opts == NULL) {
123     elog(LOG_WARNING, "Passing invalid (null) arguments!");
124     return -EINVAL;  
125   }
126   
127   /* allocate memory for the opts pointer */
128   if (((*opts) = calloc(1, sizeof(link_opts_t))) == NULL) {
129     elog(LOG_WARNING, "Insufficient memory, allocation failed!");  
130     return -ENOMEM; 
131   }
132     
133   /* copy the device name */
134   if (((*opts)->name = strdup(string)) == NULL) {
135     elog(LOG_WARNING, "Insufficient memory, allocation failed!");  
136     return -ENOMEM;
137   }
138 
139   return 0;  
140 }
141 
142 
143 static int link_parse(link_opts_t **opts, int *argc, char **argv, 
144                       char *option_str, char option_char, char esc,
145                       char *dev_name)
146 {
147   char *string = NULL, *name = NULL;
148   int i, num_args = 0;
149   
150   /* get the argument string for this option */
151   string = misc_parse_out_option(argc, argv, option_str, option_char);
152   
153   /* find the total number of arguments for this option */
154   num_args = misc_num_args(string, esc);
155   
156   /* allocate memory for the array of opts pointers */
157   if (((*opts) = calloc(num_args, sizeof(link_opts_t))) == NULL) {
158      elog(LOG_WARNING, "Insufficient memory, allocation failed!");  
159      return -ENOMEM; 
160   }
161   
162   for (i = 0; (string = misc_parse_string(string, esc, &name)) != NULL; 
163        i++) {
164     
165     /* copy the device name string */
166     if ((((*opts)+i)->name = strdup(name)) == NULL) {
167       elog(LOG_WARNING, "Insufficient memory, allocation failed!");  
168       return -ENOMEM;
169     }
170     
171     if(name)
172       link_addstring(((*opts)+i), dev_name);
173   }
174   
175   return num_args;
176 }
177 
178 void link_config(link_ifs_t *ifs, int *argc, char **argv)
179 { 
180   /* check arguments */
181   if (!ifs || !argc || !argv) {
182     elog(LOG_WARNING, "Passing invalid (NULL) arguments");
183     return;  
184   }
185   
186   /* parse the rest of the options */
187   
188   /* the default policy of links used and exported, is to just name the
189    * directory. this is because the link libraries will use that 
190    * directory as the root to use/create multiple other devices (e.g. 
191    * data, status, command) when a client uses/registers the link 
192    */
193   ifs->num_used = link_parse(&ifs->uses, argc, argv, "uses", 0, ' ', NULL);
194   ifs->num_provided = link_parse(&ifs->provides, argc, argv, "provides",
195                                  0, ' ', NULL);
196 }
197 
198 
199 void link_usage(char **usage, char **expl) 
200 {
201   *usage = "[--uses <input_if>] [--provides <output_if>]";
202   *expl =
203     " --uses <input_if>:      Sets the interface(s) to connect to.\n"
204     "\n"
205     " --provides <output_if>:  Sets the interface(s) that is(are) provided.\n";
206 }
207 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.