/* * model.c -- dtlaunch without X: matching, the calculator, snippets, programs * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. */ #include #include #include #include #include #include #include #include #include #include "model.h" /* ---- matching ---------------------------------------------------------- */ int mx_match(const char *query, const char *text) { const char *t = text; int score = 0, run = 0, first = 1; if (*query == '\0') return 1; for (; *query != '\0'; query++) { int q = tolower((unsigned char)*query); int adjacent = 1; if (q == ' ') continue; while (*t != '\0' && tolower((unsigned char)*t) != q) { t++; adjacent = 0; } if (*t == '\0') return 0; if (first && t == text) score += 20; /* the very start */ else if (t > text && !isalnum((unsigned char)t[-1])) score += 8; /* the start of a word */ run = adjacent && !first ? run + 1 : 0; score += 1 + 3 * run; /* letters in a row */ first = 0; t++; } /* a shorter text is the better match for the same letters */ score += 10 - (int)(strlen(text) > 10 ? 10 : strlen(text)); return score > 0 ? score : 1; } /* ---- the calculator: recursive descent --------------------------------- */ typedef struct { const char *p; int error; } Calc; static double expr(Calc *c); static void skip(Calc *c) { while (*c->p == ' ' || *c->p == '\t') c->p++; } static double primary(Calc *c) { double v; char *end; skip(c); if (*c->p == '(') { c->p++; v = expr(c); skip(c); if (*c->p != ')') c->error = 1; else c->p++; return v; } if (*c->p == '-') { c->p++; return -primary(c); } if (*c->p == '+') { c->p++; return primary(c); } if (!isdigit((unsigned char)*c->p) && *c->p != '.') { c->error = 1; return 0; } errno = 0; v = strtod(c->p, &end); if (end == c->p || errno != 0) c->error = 1; c->p = end; return v; } static double power(Calc *c) { double b = primary(c); skip(c); if (*c->p == '^') { c->p++; return pow(b, power(c)); /* right associative */ } return b; } static double term(Calc *c) { double v = power(c), r; for (;;) { skip(c); if (*c->p == '*' || *c->p == '/' || *c->p == '%') { char op = *c->p++; r = power(c); if (op == '*') v *= r; else if (r == 0) c->error = 1; else v = op == '/' ? v / r : fmod(v, r); } else return v; } } static double expr(Calc *c) { double v = term(c); for (;;) { skip(c); if (*c->p == '+') { c->p++; v += term(c); } else if (*c->p == '-') { c->p++; v -= term(c); } else return v; } } int mx_calc(const char *s, double *out) { Calc c; double v; int ops = 0; const char *q; while (*s == ' ') s++; if (*s == '=') s++; else { /* without '=', only text that is clearly a calculation counts: * digits and at least one operator, nothing else */ for (q = s; *q != '\0'; q++) { if (strchr("+-*/%^", *q) != NULL && q != s) ops++; else if (!isdigit((unsigned char)*q) && strchr(" .()+-", *q) == NULL) return -1; } if (ops == 0) return -1; } c.p = s; c.error = 0; v = expr(&c); skip(&c); if (c.error || *c.p != '\0' || isnan(v) || isinf(v)) return -1; *out = v; return 0; } void mx_calc_format(double v, char *buf, size_t len) { char *p; (void)snprintf(buf, len, "%.10f", v); if (strchr(buf, '.') == NULL) return; p = buf + strlen(buf) - 1; while (p > buf && *p == '0') *p-- = '\0'; if (*p == '.') *p = '\0'; if (strcmp(buf, "-0") == 0) (void)snprintf(buf, len, "0"); } /* ---- snippets ---------------------------------------------------------- */ char * mx_unescape(const char *s) { char *r = malloc(strlen(s) + 1), *d = r; if (r == NULL) return NULL; for (; *s != '\0'; s++) { if (*s == '\\' && s[1] != '\0') { s++; *d++ = *s == 'n' ? '\n' : *s == 't' ? '\t' : *s; } else *d++ = *s; } *d = '\0'; return r; } int mx_snippets_load(const char *path, MxSnippet **out, size_t *n) { FILE *f; char line[4096], *tab; MxSnippet *v = NULL, *nv; size_t cnt = 0; *out = NULL; *n = 0; if ((f = fopen(path, "r")) == NULL) return errno == ENOENT ? 0 : -1; while (fgets(line, sizeof line, f) != NULL) { line[strcspn(line, "\r\n")] = '\0'; if (line[0] == '#' || (tab = strchr(line, '\t')) == NULL || tab == line) continue; *tab = '\0'; if ((nv = realloc(v, (cnt + 1) * sizeof *v)) == NULL) break; v = nv; v[cnt].key = strdup(line); v[cnt].text = mx_unescape(tab + 1); if (v[cnt].key == NULL || v[cnt].text == NULL) { free(v[cnt].key); free(v[cnt].text); break; } cnt++; } (void)fclose(f); *out = v; *n = cnt; return 0; } void mx_snippets_free(MxSnippet *s, size_t n) { size_t i; for (i = 0; i < n; i++) { free(s[i].key); free(s[i].text); } free(s); } /* ---- programs in $PATH ------------------------------------------------- */ static int cmpstr(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); } int mx_path_programs(const char *path, char ***out, size_t *n) { char *copy, *dir, *save = NULL, file[4096]; char **v = NULL, **nv; size_t cnt = 0, cap = 0, i, j; struct dirent *de; struct stat st; DIR *d; *out = NULL; *n = 0; if (path == NULL || (copy = strdup(path)) == NULL) return 0; for (dir = strtok_r(copy, ":", &save); dir != NULL; dir = strtok_r(NULL, ":", &save)) { if ((d = opendir(dir)) == NULL) continue; while ((de = readdir(d)) != NULL) { if (de->d_name[0] == '.') continue; (void)snprintf(file, sizeof file, "%s/%s", dir, de->d_name); if (stat(file, &st) != 0 || !S_ISREG(st.st_mode) || access(file, X_OK) != 0) continue; if (cnt == cap) { cap = cap ? cap * 2 : 256; if ((nv = realloc(v, cap * sizeof *v)) == NULL) break; v = nv; } if ((v[cnt] = strdup(de->d_name)) != NULL) cnt++; } (void)closedir(d); } free(copy); if (cnt > 1) qsort(v, cnt, sizeof *v, cmpstr); /* duplicates: the first one in $PATH wins, the name is the same */ for (i = j = 0; i < cnt; i++) { if (j > 0 && strcmp(v[i], v[j - 1]) == 0) free(v[i]); else v[j++] = v[i]; } *out = v; *n = j; return 0; } void mx_strv_free(char **v, size_t n) { size_t i; for (i = 0; i < n; i++) free(v[i]); free(v); }