int main(int argc, char** argv)
{
+ int ret = 0;
int force = 0;
OPTBEGIN { case 'f': force = 1; break; } OPTEND;
if (argc == 0)
XA_OPEN = XInternAtom(x.display, "OPEN", 0);
XA_DONE = XInternAtom(x.display, "DONE", 0);
Window registrar = start_registrar(&x);
- if (registrar == None)
+ if (registrar != None)
{
- fprintf(stderr, "Failed to contact registrar.\n");
- return 1;
- }
- /* Loop over files and send an OPEN message for each one. */
- for (int i = 0; i < argc; i++)
- {
- char* addr = strrchr(argv[i], ':');
- if (addr)
+ /* Loop over files and send an OPEN message for each one. */
+ for (int i = 0; i < argc; i++)
{
- *addr = '\0', addr++;
+ char* addr = strrchr(argv[i], ':');
+ if (addr)
+ {
+ *addr = '\0', addr++;
+ }
+ edit_file(&x, registrar, argv[i], (addr ? addr : "0"), force);
}
- edit_file(&x, registrar, argv[i], (addr ? addr : "0"), force);
+ XSync(x.display, False);
+ }
+ else
+ {
+ fprintf(stderr, "Failed to contact registrar.\n");
+ ret = 1;
}
- XSync(x.display, False);
}
- return 0;
+ return ret;
}
}
regmatch_t matches[2] = {{0},{0}};
- if (regexec(®ex, str, nelem(matches), matches, 0) < 0)
- {
- return str;
- }
- else if (matches[1].rm_so > 0)
+ int result = regexec(®ex, str, nelem(matches), matches, 0);
+ if ((result == 0) && (matches[1].rm_so > 0))
{
char* var = strndup(str+matches[1].rm_so, matches[1].rm_eo-matches[1].rm_so);
char* val = getvar(var);
strncat(exp, str, matches[0].rm_so);
strcat(exp, val);
strcat(exp, str + matches[0].rm_eo);
- return eval(exp);
- } else {
- return str;
+ str = eval(exp);
}
+ return str;
}
/******************************************************************************/
bool matches(char* var, char* patt)
{
+ bool ret = false;
regex_t regex = {0};
regmatch_t matches[10] = {{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}};
if (regcomp(®ex, patt, REG_EXTENDED) == 0)
setenv((char[]){ 'M', ('0' + i), 0 }, matchval, 1);
free(matchval);
}
- return (err == 0);
+ ret = (err == 0);
}
- return false;
+ return ret;
}
bool var_is(char* var, char* val)
bool var_isdir(char* var)
{
+ bool ret = false;
struct stat st = {0};
char* path = eval(var);
- if ((stat(path, &st) < 0) && (errno == ENOENT))
- {
- return false;
- }
- else if (S_ISDIR(st.st_mode))
+ if (!stat(path, &st) && !errno && S_ISDIR(st.st_mode))
{
setenv("dir", var, 1);
- return true;
- }
- else
- {
- return false;
+ ret = true;
}
+ return ret;
}
bool var_isfile(char* var)
{
+ bool ret = false;
struct stat st = {0};
char* path = eval(var);
- if ((stat(eval(var), &st) < 0) && (errno == ENOENT))
- {
- return false;
- }
- else if (!S_ISDIR(st.st_mode))
+ if (!stat(eval(var), &st) && !errno && !S_ISDIR(st.st_mode))
{
setenv("file", path, 1);
- return true;
- }
- else
- {
- return false;
+ ret = true;
}
+ return ret;
}
bool var_set(char* var, char* val)
bool exec(char* cmd)
{
+ bool ret = false;
int pid, status;
- if ((pid = fork()) < 0)
+ if ((pid = fork()) >= 0)
{
- return false;
- }
- if (pid == 0)
- {
- runcmd(cmd);
- }
- else
- {
- waitpid(pid, &status, 0);
- return (status == 0);
+ if (pid == 0)
+ {
+ runcmd(cmd);
+ }
+ else
+ {
+ waitpid(pid, &status, 0);
+ ret = (status == 0);
+ }
}
- return false;
+ return ret;
}
bool launch(char* cmd)
{
+ bool ret = false;
int pid = fork();
if (pid > 0)
{
- return true;
+ ret = true;
}
else if (pid == 0)
{
runcmd(cmd);
}
- return false;
+ return ret;
}
bool apply_rule(Rule* rule)
{
+ bool ret = false;
switch (rule->type)
{
- case COMPLETE: exit(0);
- case MATCHES: return matches(rule->arg1, rule->arg2);
- case IS: return var_is(rule->arg1, rule->arg2);
- case ISSET: return var_isset(rule->arg1);
- case ISDIR: return var_isdir(rule->arg1);
- case ISFILE: return var_isfile(rule->arg1);
- case SET: return var_set(rule->arg1, rule->arg2);
- case UNSET: return var_unset(rule->arg1);
- case FINDFILE: return find_file(rule->arg1);
- case EXEC: return exec(rule->arg1);
- case LAUNCH: return launch(rule->arg1);
+ case COMPLETE: exit(0); break;
+ case MATCHES: ret = matches(rule->arg1, rule->arg2); break;
+ case IS: ret = var_is(rule->arg1, rule->arg2); break;
+ case ISSET: ret = var_isset(rule->arg1); break;
+ case ISDIR: ret = var_isdir(rule->arg1); break;
+ case ISFILE: ret = var_isfile(rule->arg1); break;
+ case SET: ret = var_set(rule->arg1, rule->arg2); break;
+ case UNSET: ret = var_unset(rule->arg1); break;
+ case FINDFILE: ret = find_file(rule->arg1); break;
+ case EXEC: ret = exec(rule->arg1); break;
+ case LAUNCH: ret = launch(rule->arg1); break;
}
- return false;
+ return ret;
}
/******************************************************************************/
char* type2str(int type)
{
+ char* ret = "UNKNOWN";
switch (type)
{
- case COMPLETE: return "COMPLETE";
- case MATCHES: return "MATCHES";
- case IS: return "IS";
- case ISSET: return "ISSET";
- case ISDIR: return "ISDIR";
- case ISFILE: return "ISFILE";
- case SET: return "SET";
- case UNSET: return "UNSET";
- case FINDFILE: return "FINDFILE";
- case EXEC: return "EXEC";
- case LAUNCH: return "LAUNCH";
+ case COMPLETE: ret = "COMPLETE"; break;
+ case MATCHES: ret = "MATCHES"; break;
+ case IS: ret = "IS"; break;
+ case ISSET: ret = "ISSET"; break;
+ case ISDIR: ret = "ISDIR"; break;
+ case ISFILE: ret = "ISFILE"; break;
+ case SET: ret = "SET"; break;
+ case UNSET: ret = "UNSET"; break;
+ case FINDFILE: ret = "FINDFILE"; break;
+ case EXEC: ret = "EXEC"; break;
+ case LAUNCH: ret = "LAUNCH"; break;
}
- return "UNKNOWN";
+ return ret;
}
int main(int argc, char** argv)
XftFont* x11_font_load(XConf* x, char* name)
{
- /* init the library and the base font pattern */
- if (!FcInit())
- {
- return NULL;
- }
- FcPattern* pattern = FcNameParse((FcChar8 *)name);
- if (!pattern)
- {
- return NULL;
- }
- /* load the base font */
- FcResult result;
- FcPattern* match = XftFontMatch(x->display, x->screen, pattern, &result);
XftFont* font = NULL;
- if (match)
+ if (FcInit())
{
- font = XftFontOpenPattern(x->display, match);
+ FcPattern* pattern = FcNameParse((FcChar8 *)name);
+ if (pattern)
+ {
+ /* load the base font */
+ FcResult result;
+ FcPattern* match = XftFontMatch(x->display, x->screen, pattern, &result);
+ if (match)
+ {
+ font = XftFontOpenPattern(x->display, match);
+ }
+ FcPatternDestroy(pattern);
+ FcPatternDestroy(match);
+ }
}
- FcPatternDestroy(pattern);
- FcPatternDestroy(match);
return font;
}
static struct XSel* selfetch(Atom atom)
{
+ struct XSel* ret = NULL;
for (unsigned int i = 0; i < (sizeof(Selections) / sizeof(Selections[0])); i++)
{
if (atom == Selections[i].atom)
{
- return &Selections[i];
+ ret = &Selections[i];
+ break;
}
}
- return NULL;
+ return ret;
}
static void xselclear(XConf* x, XEvent* e)
int x11_sel_get(XConf* x, int selid, void(*cbfn)(char*))
{
+ int ret = 0;
struct XSel* sel = &(Selections[selid]);
- if (sel->callback) return 0;
- Window owner = XGetSelectionOwner(x->display, sel->atom);
- if (owner == x->self)
+ if (!sel->callback)
{
- cbfn(sel->text);
- }
- else if (owner != None)
- {
- sel->callback = cbfn;
- XConvertSelection(x->display, sel->atom, SelTarget, sel->atom, x->self, CurrentTime);
+ Window owner = XGetSelectionOwner(x->display, sel->atom);
+ if (owner == x->self)
+ {
+ cbfn(sel->text);
+ }
+ else if (owner != None)
+ {
+ sel->callback = cbfn;
+ XConvertSelection(x->display, sel->atom, SelTarget, sel->atom, x->self, CurrentTime);
+ }
+ ret = 1;
}
- return 1;
+ return ret;
}
int x11_sel_set(XConf* x, int selid, char* str)
{
+ int ret;
struct XSel* sel = &(Selections[selid]);
if (!sel || !str || !*str)
{
free(str);
- return 0;
+ ret = 0;
}
else
{
sel->text = str;
XSetSelectionOwner(x->display, sel->atom, x->self, CurrentTime);
- return 1;
+ ret = 1;
}
+ return ret;
}
void x11_sel_quit(XConf* x, XEvent* e)