23 #define ALIAS_FILE_OPERATOR NMSG_ETCDIR "/nmsg.opalias"
24 #define ALIAS_FILE_GROUP NMSG_ETCDIR "/nmsg.gralias"
26 #define ALIAS_SZ_INIT 16
27 #define MAX_LINE_SZ 1024
34 static int nmsg_alias_initialized = 0;
35 static struct nmsg_alias alias_operator;
36 static struct nmsg_alias alias_group;
40 static nmsg_res alias_init(
struct nmsg_alias *,
const char *fname);
41 static nmsg_res alias_resize(
struct nmsg_alias *,
unsigned n);
42 static void alias_free(
struct nmsg_alias *);
48 struct nmsg_alias *al = NULL;
57 if (key <= al->max_idx)
58 return (al->value[key]);
65 struct nmsg_alias *al = NULL;
74 for (
unsigned i = 0; i <= al->max_idx; i++)
75 if (al->value[i] != NULL &&
76 strcasecmp(value, al->value[i]) == 0)
83 _nmsg_alias_init(
void) {
86 if (nmsg_alias_initialized == 0) {
87 res = alias_init(&alias_operator, ALIAS_FILE_OPERATOR);
91 res = alias_init(&alias_group, ALIAS_FILE_GROUP);
95 nmsg_alias_initialized = 1;
102 _nmsg_alias_fini(
void) {
103 if (nmsg_alias_initialized == 1) {
104 alias_free(&alias_operator);
105 alias_free(&alias_group);
106 nmsg_alias_initialized = 0;
113 alias_init(
struct nmsg_alias *al,
const char *fname) {
115 char line[MAX_LINE_SZ];
116 char *saveptr, *str_key, *str_value;
123 al->value = malloc(
sizeof(*(al->value)) * (ALIAS_SZ_INIT + 1));
124 if (al->value == NULL)
126 al->max_idx = ALIAS_SZ_INIT;
127 for (
unsigned i = 0; i <= al->max_idx; i++)
130 fp = fopen(fname,
"r");
135 while (fgets(line,
sizeof(line), fp) != NULL) {
136 str_key = strtok_r(line,
" \t", &saveptr);
137 str_value = strtok_r(NULL,
" \t\n", &saveptr);
138 if (str_key == NULL || str_value == NULL) {
143 key = (unsigned) strtoul(str_key, &t, 0);
149 if (key > al->max_idx) {
156 al->value[key] = strdup(str_value);
164 alias_resize(
struct nmsg_alias *al,
unsigned n) {
170 if (n > al->max_idx) {
171 max_idx = al->max_idx * 2;
176 al->value = realloc(al->value, (max_idx + 1) *
sizeof(*(al->value)));
177 if (al->value == NULL) {
182 for (
unsigned i = al->max_idx + 1; i <= max_idx; i++)
184 al->max_idx = max_idx;
190 alias_free(
struct nmsg_alias *al) {
191 for (
unsigned i = 0; i <= al->max_idx; i++)
192 if (al->value[i] != NULL)
unsigned nmsg_alias_by_value(nmsg_alias_e ae, const char *value)
Look up an alias by name.
operator ID -> operator name
const char * nmsg_alias_by_key(nmsg_alias_e ae, unsigned key)
Look up an alias by key.