Reorder functions _lttng_cmd_* functions in bash completion
[lttng-tools.git] / src / bin / lttng / utils.c
1 /*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <stdlib.h>
20 #include <ctype.h>
21 #include <limits.h>
22
23 #include <common/error.h>
24 #include <common/utils.h>
25
26 #include "conf.h"
27 #include "utils.h"
28
29 /*
30 * get_session_name
31 *
32 * Return allocated string with the session name found in the config
33 * directory.
34 */
35 char *get_session_name(void)
36 {
37 char *path, *session_name = NULL;
38
39 /* Get path to config file */
40 path = utils_get_home_dir();
41 if (path == NULL) {
42 goto error;
43 }
44
45 /* Get session name from config */
46 session_name = config_read_session_name(path);
47 if (session_name == NULL) {
48 goto error;
49 }
50
51 DBG2("Config file path found: %s", path);
52 DBG("Session name found: %s", session_name);
53 return session_name;
54
55 error:
56 return NULL;
57 }
58
59
60 /*
61 * list_cmd_options
62 *
63 * Prints a simple list of the options available to a command. This is intended
64 * to be easily parsed for bash completion.
65 */
66 void list_cmd_options(FILE *ofp, struct poptOption *options)
67 {
68 int i;
69 struct poptOption *option = NULL;
70
71 for (i = 0; options[i].longName != NULL; i++) {
72 option = &options[i];
73
74 fprintf(ofp, "--%s\n", option->longName);
75
76 if (isprint(option->shortName)) {
77 fprintf(ofp, "-%c\n", option->shortName);
78 }
79 }
80 }
81
82 /*
83 * fls: returns the position of the most significant bit.
84 * Returns 0 if no bit is set, else returns the position of the most
85 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
86 */
87 #if defined(__i386) || defined(__x86_64)
88 static inline
89 unsigned int fls_u32(uint32_t x)
90 {
91 int r;
92
93 asm("bsrl %1,%0\n\t"
94 "jnz 1f\n\t"
95 "movl $-1,%0\n\t"
96 "1:\n\t"
97 : "=r" (r) : "rm" (x));
98 return r + 1;
99 }
100 #define HAS_FLS_U32
101 #endif
102
103 #if defined(__x86_64)
104 static inline
105 unsigned int fls_u64(uint64_t x)
106 {
107 long r;
108
109 asm("bsrq %1,%0\n\t"
110 "jnz 1f\n\t"
111 "movq $-1,%0\n\t"
112 "1:\n\t"
113 : "=r" (r) : "rm" (x));
114 return r + 1;
115 }
116 #define HAS_FLS_U64
117 #endif
118
119 #ifndef HAS_FLS_U64
120 static __attribute__((unused))
121 unsigned int fls_u64(uint64_t x)
122 {
123 unsigned int r = 64;
124
125 if (!x)
126 return 0;
127
128 if (!(x & 0xFFFFFFFF00000000ULL)) {
129 x <<= 32;
130 r -= 32;
131 }
132 if (!(x & 0xFFFF000000000000ULL)) {
133 x <<= 16;
134 r -= 16;
135 }
136 if (!(x & 0xFF00000000000000ULL)) {
137 x <<= 8;
138 r -= 8;
139 }
140 if (!(x & 0xF000000000000000ULL)) {
141 x <<= 4;
142 r -= 4;
143 }
144 if (!(x & 0xC000000000000000ULL)) {
145 x <<= 2;
146 r -= 2;
147 }
148 if (!(x & 0x8000000000000000ULL)) {
149 x <<= 1;
150 r -= 1;
151 }
152 return r;
153 }
154 #endif
155
156 #ifndef HAS_FLS_U32
157 static __attribute__((unused))
158 unsigned int fls_u32(uint32_t x)
159 {
160 unsigned int r = 32;
161
162 if (!x)
163 return 0;
164 if (!(x & 0xFFFF0000U)) {
165 x <<= 16;
166 r -= 16;
167 }
168 if (!(x & 0xFF000000U)) {
169 x <<= 8;
170 r -= 8;
171 }
172 if (!(x & 0xF0000000U)) {
173 x <<= 4;
174 r -= 4;
175 }
176 if (!(x & 0xC0000000U)) {
177 x <<= 2;
178 r -= 2;
179 }
180 if (!(x & 0x80000000U)) {
181 x <<= 1;
182 r -= 1;
183 }
184 return r;
185 }
186 #endif
187
188 static
189 unsigned int fls_ulong(unsigned long x)
190 {
191 #if (CAA_BITS_PER_LONG == 32)
192 return fls_u32(x);
193 #else
194 return fls_u64(x);
195 #endif
196 }
197
198 /*
199 * Return the minimum order for which x <= (1UL << order).
200 * Return -1 if x is 0.
201 */
202 int get_count_order_u32(uint32_t x)
203 {
204 if (!x)
205 return -1;
206
207 return fls_u32(x - 1);
208 }
209
210 /*
211 * Return the minimum order for which x <= (1UL << order).
212 * Return -1 if x is 0.
213 */
214 int get_count_order_u64(uint64_t x)
215 {
216 if (!x)
217 return -1;
218
219 return fls_u64(x - 1);
220 }
221
222 /*
223 * Return the minimum order for which x <= (1UL << order).
224 * Return -1 if x is 0.
225 */
226 int get_count_order_ulong(unsigned long x)
227 {
228 if (!x)
229 return -1;
230
231 return fls_ulong(x - 1);
232 }
This page took 0.035736 seconds and 6 git commands to generate.