Reorder functions _lttng_cmd_* functions in bash completion
[lttng-tools.git] / src / bin / lttng / utils.c
CommitLineData
f3ed775e
DG
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
f3ed775e
DG
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
4c462e79 18#define _GNU_SOURCE
f3ed775e 19#include <stdlib.h>
679b4943 20#include <ctype.h>
3badf2bf 21#include <limits.h>
f3ed775e 22
db758600 23#include <common/error.h>
feb0f3e5 24#include <common/utils.h>
f3ed775e 25
beb8c75a 26#include "conf.h"
679b4943 27#include "utils.h"
f3ed775e 28
f3ed775e
DG
29/*
30 * get_session_name
31 *
32 * Return allocated string with the session name found in the config
33 * directory.
34 */
35char *get_session_name(void)
36{
37 char *path, *session_name = NULL;
38
39 /* Get path to config file */
feb0f3e5 40 path = utils_get_home_dir();
f3ed775e
DG
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) {
58a97671 48 goto error;
f3ed775e
DG
49 }
50
3183dbb0 51 DBG2("Config file path found: %s", path);
cd80958d 52 DBG("Session name found: %s", session_name);
f3ed775e 53 return session_name;
3183dbb0
DG
54
55error:
56 return NULL;
f3ed775e 57}
679b4943
SM
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 */
66void 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}
8ce58bad
MD
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)
88static inline
89unsigned 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)
104static inline
105unsigned 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
120static __attribute__((unused))
121unsigned 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
157static __attribute__((unused))
158unsigned 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
188static
189unsigned 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 */
202int 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 */
214int 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 */
226int 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.085679 seconds and 5 git commands to generate.