1/*
2 * Shell-like utility functions
3 *
4 * Copyright 2005, Broadcom Corporation
5 * All Rights Reserved.
6 *
7 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
11 *
12 * $Id: shutils.h,v 1.1.1.1 2008/07/21 09:20:23 james26_jang Exp $
13 */
14
15#ifndef _shutils_h_
16#define _shutils_h_
17
18/*
19 * Reads file and returns contents
20 * @param	fd	file descriptor
21 * @return	contents of file or NULL if an error occurred
22 */
23extern char * fd2str(int fd);
24
25/*
26 * Reads file and returns contents
27 * @param	path	path to file
28 * @return	contents of file or NULL if an error occurred
29 */
30extern char * file2str(const char *path);
31
32/*
33 * Waits for a file descriptor to become available for reading or unblocked signal
34 * @param	fd	file descriptor
35 * @param	timeout	seconds to wait before timing out or 0 for no timeout
36 * @return	1 if descriptor changed status or 0 if timed out or -1 on error
37 */
38extern int waitfor(int fd, int timeout);
39
40/*
41 * Concatenates NULL-terminated list of arguments into a single
42 * commmand and executes it
43 * @param	argv	argument list
44 * @param	path	NULL, ">output", or ">>output"
45 * @param	timeout	seconds to wait before timing out or 0 for no timeout
46 * @param	ppid	NULL to wait for child termination or pointer to pid
47 * @return	return value of executed command or errno
48 */
49extern int _eval(char *const argv[], char *path, int timeout, pid_t *ppid);
50
51/*
52 * Concatenates NULL-terminated list of arguments into a single
53 * commmand and executes it
54 * @param	argv	argument list
55 * @return	stdout of executed command or NULL if an error occurred
56 */
57extern char * _backtick(char *const argv[]);
58
59/*
60 * Kills process whose PID is stored in plaintext in pidfile
61 * @param	pidfile	PID file
62 * @return	0 on success and errno on failure
63 */
64extern int kill_pidfile(char *pidfile);
65
66/*
67 * fread() with automatic retry on syscall interrupt
68 * @param	ptr	location to store to
69 * @param	size	size of each element of data
70 * @param	nmemb	number of elements
71 * @param	stream	file stream
72 * @return	number of items successfully read
73 */
74extern int safe_fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
75
76/*
77 * fwrite() with automatic retry on syscall interrupt
78 * @param	ptr	location to read from
79 * @param	size	size of each element of data
80 * @param	nmemb	number of elements
81 * @param	stream	file stream
82 * @return	number of items successfully written
83 */
84extern int safe_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
85
86/*
87 * Convert Ethernet address string representation to binary data
88 * @param	a	string in xx:xx:xx:xx:xx:xx notation
89 * @param	e	binary data
90 * @return	TRUE if conversion was successful and FALSE otherwise
91 */
92extern int ether_atoe(const char *a, unsigned char *e);
93
94/*
95 * Convert Ethernet address binary data to string representation
96 * @param	e	binary data
97 * @param	a	string in xx:xx:xx:xx:xx:xx notation
98 * @return	a
99 */
100extern char * ether_etoa(const unsigned char *e, char *a);
101
102/*
103 * Concatenate two strings together into a caller supplied buffer
104 * @param	s1	first string
105 * @param	s2	second string
106 * @param	buf	buffer large enough to hold both strings
107 * @return	buf
108 */
109static inline char * strcat_r(const char *s1, const char *s2, char *buf)
110{
111	strcpy(buf, s1);
112	strcat(buf, s2);
113	return buf;
114}
115
116/*
117 * Parse the unit and subunit from an interface string such as wlXX or wlXX.YY
118 *
119 * @param	ifname	interface string to parse
120 * @param	unit	pointer to return the unit number, may pass NULL
121 * @param	subunit	pointer to return the subunit number, may pass NULL
122 * @return	Returns 0 if the string ends with digits or digits.digits, -1 otherwise.
123 *		If ifname ends in digits.digits, then unit and subuint are set
124 *		to the first and second values respectively. If ifname ends
125 *		in just digits, unit is set to the value, and subunit is set
126 *		to -1. On error both unit and subunit are -1. NULL may be passed
127 *		for unit and/or subuint to ignore the value.
128 */
129extern int get_ifname_unit(const char* ifname, int *unit, int *subunit);
130
131/*
132 * Set the ip configuration index given the eth name
133 * Updates both wlXX_ipconfig_index and lanYY_ifname.
134 *
135 * @param	eth_ifname 	pointer to eth interface name
136 * @return	0 if successful -1 if not.
137 */
138 extern int set_ipconfig_index(char *eth_ifname,int index);
139
140/*
141 * Get the ip configuration index if it exists given the
142 * eth name.
143 *
144 * @param	wl_ifname 	pointer to eth interface name
145 * @return	index or -1 if not found
146 */
147extern int get_ipconfig_index(char *eth_ifname);
148
149/*
150 * Get interfaces belonging to a specific bridge.
151 *
152 * @param	bridge_name 	pointer to bridge interface name
153 * @return	list on interfaces beloging to the bridge
154 */
155extern char *
156get_bridged_interfaces(char *bridge_name);
157
158/*
159		remove_from_list
160		Remove the specified word from the list.
161
162		@param name word to be removed from the list
163		@param list List to modify
164		@param listsize Max size the list can occupy
165
166		@return	error code
167*/
168extern int remove_from_list( char *name, char *list, int listsize );
169
170/*
171		add_to_list
172		Add the specified interface(string) to the list as long as
173		it will fit in the space left in the list.
174
175		@param name Name of interface to be added to the list
176		@param list List to modify
177		@param listsize Max size the list can occupy
178
179		@return	error code
180*/
181extern int add_to_list( char *name, char *list, int listsize );
182
183extern int nvifname_to_osifname( const char *nvifname, char *osifname_buf,
184								int osifname_buf_len );
185extern int osifname_to_nvifname( const char *osifname, char *nvifname_buf,
186								int nvifname_buf_len );
187
188/* Check for a blank character; that is, a space or a tab */
189#define isblank(c) ((c) == ' ' || (c) == '\t')
190
191/* Strip trailing CR/NL from string <s> */
192#define chomp(s) ({ \
193	char *c = (s) + strlen((s)) - 1; \
194	while ((c > (s)) && (*c == '\n' || *c == '\r')) \
195		*c-- = '\0'; \
196	s; \
197})
198
199/* Simple version of _backtick() */
200#define backtick(cmd, args...) ({ \
201	char *argv[] = { cmd, ## args, NULL }; \
202	_backtick(argv); \
203})
204
205/* Simple version of _eval() (no timeout and wait for child termination) */
206#define eval(cmd, args...) ({ \
207	char *argv[] = { cmd, ## args, NULL }; \
208	_eval(argv, ">/dev/null", 0, NULL); \
209})
210
211#define ceval(cmd, args...) ({ \
212	char *argv[] = { cmd, ## args, NULL }; \
213	_eval(argv, ">/dev/console", 0, NULL); \
214})
215
216/* Copy each token in wordlist delimited by space into word */
217#define foreach(word, wordlist, next) \
218	for (next = &wordlist[strspn(wordlist, " ")], \
219	     strncpy(word, next, sizeof(word)), \
220	     word[strcspn(word, " ")] = '\0', \
221	     word[sizeof(word) - 1] = '\0', \
222	     next = strchr(next, ' '); \
223	     strlen(word); \
224	     next = next ? &next[strspn(next, " ")] : "", \
225	     strncpy(word, next, sizeof(word)), \
226	     word[strcspn(word, " ")] = '\0', \
227	     word[sizeof(word) - 1] = '\0', \
228	     next = strchr(next, ' '))
229
230/* Return NUL instead of NULL if undefined */
231#define safe_getenv(s) (getenv(s) ? : "")
232
233#ifdef linux
234/* Print directly to the console */
235
236#define cprintf(fmt, args...) do { \
237	FILE *fp = fopen("/dev/console", "w"); \
238	if (fp) { \
239		fprintf(fp, fmt, ## args); \
240		fclose(fp); \
241	} \
242} while (0)
243#endif
244
245/* Debug print */
246#ifdef DEBUG
247#define dprintf(fmt, args...) cprintf("%s: " fmt, __FUNCTION__, ## args)
248#else
249#define dprintf(fmt, args...)
250#endif /*DEBUG*/
251
252
253#endif /* _shutils_h_ */
254