-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
216 lines (177 loc) · 4.78 KB
/
common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* MOC - music on console
* Copyright (C) 2004 - 2005 Damian Pietras <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <assert.h>
#include "server.h"
#include "interface.h"
#include "log.h"
#include "common.h"
#include "options.h"
#include "lists.h"
static int im_server = 0; /* Am I the server? */
void error (const char *format, ...)
{
va_list va;
char msg[256];
va_start (va, format);
vsnprintf (msg, sizeof(msg), format, va);
msg[sizeof(msg) - 1] = 0;
va_end (va);
if (im_server)
server_error (msg);
else
interface_error (msg);
}
/* End program with a message. Use when an error occurs and we can't recover. */
void fatal (const char *format, ...)
{
va_list va;
char msg[256];
va_start (va, format);
vsnprintf (msg, sizeof(msg), format, va);
msg[sizeof(msg) - 1] = 0;
fprintf (stderr, "\nFATAL_ERROR: %s\n\n", msg);
logit ("FATAL ERROR: %s", msg);
va_end (va);
exit (EXIT_FATAL);
}
void *xmalloc (const size_t size)
{
void *p;
if ((p = malloc(size)) == NULL)
fatal ("Can't allocate memory!");
return p;
}
void *xcalloc (size_t nmemb, size_t size)
{
void *p;
if ((p = calloc(nmemb, size)) == NULL)
fatal ("Can't allocate memory!");
return p;
}
void *xrealloc (void *ptr, const size_t size)
{
void *p;
if ((p = realloc(ptr, size)) == NULL && size != 0)
fatal ("Can't allocate memory!");
return p;
}
char *xstrdup (const char *s)
{
char *n;
if (s && (n = strdup(s)) == NULL)
fatal ("Can't allocate memory!");
return s ? n : NULL;
}
/* Read and return a line from 'stream' in a dynamically allocated buffer.
* Return NULL at end of file. */
char *xgetline (FILE *stream)
{
static char buffer[64];
char *result;
lists_t_strs *line;
line = lists_strs_new (4);
do {
if (!fgets (buffer, sizeof (buffer), stream))
break;
lists_strs_append (line, buffer);
} while (buffer[strlen (buffer) - 1] != '\n');
result = lists_strs_cat (line);
lists_strs_free (line);
return result;
}
void set_me_server ()
{
im_server = 1;
}
char *str_repl (char *target, const char *oldstr, const char *newstr)
{
size_t oldstr_len = strlen(oldstr);
size_t newstr_len = strlen(newstr);
size_t target_len = strlen(target);
size_t target_max = target_len;
size_t s, p;
char *needle;
for (s = 0; (needle = strstr(target + s, oldstr)) != NULL; s = p + newstr_len) {
target_len += newstr_len - oldstr_len;
p = needle - target;
if (target_len + 1 > target_max) {
target_max = (target_len + 1 > target_max * 2) ? target_len + 1 : target_max * 2;
target = xrealloc(target, target_max);
}
memmove(target + p + newstr_len, target + p + oldstr_len, target_len - p - newstr_len + 1);
memcpy(target + p, newstr, newstr_len);
}
target = xrealloc(target, target_len + 1);
return target;
}
/* Return true iff the argument would be a syntactically valid symbol.
* (Note that the so-called "peculiar indentifiers" are disallowed here.) */
bool is_valid_symbol (const char *candidate)
{
size_t len;
bool result;
const char *first = "+-.0123456789@";
const char *valid = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789"
"@?!.+-*/<=>:$%^&_~";
result = false;
len = strlen (candidate);
if (len > 0 && len == strspn (candidate, valid) &&
index (first, candidate[0]) == NULL)
result = true;
return result;
}
/* Return path to a file in MOC config directory. NOT THREAD SAFE */
char *create_file_name (const char *file)
{
char *home_dir;
static char fname[PATH_MAX];
char *moc_dir = options_get_str ("MOCDir");
if (moc_dir[0] == '~') {
if (!(home_dir = getenv("HOME")))
fatal ("No HOME environmential variable.");
if (snprintf(fname, sizeof(fname), "%s/%s/%s", home_dir,
(moc_dir[1] == '/') ? moc_dir + 2 : moc_dir + 1,
file)
>= (int)sizeof(fname))
fatal ("Path too long.");
}
else if (snprintf(fname, sizeof(fname), "%s/%s", moc_dir, file)
>= (int)sizeof(fname))
fatal ("Path too long.");
return fname;
}
/* Convert time in second to min:sec text format. buff must be 6 chars long. */
void sec_to_min (char *buff, const int seconds)
{
assert (seconds >= 0);
if (seconds < 6000) {
/* the time is less than 99:59 */
int min, sec;
min = seconds / 60;
sec = seconds % 60;
snprintf (buff, 6, "%02d:%02d", min, sec);
}
else if (seconds < 10000 * 60)
/* the time is less than 9999 minutes */
snprintf (buff, 6, "%4dm", seconds/60);
else
strcpy (buff, "!!!!!");
}