void load_configfile_wrapper(char *config_file) { /* load configurationfile (if any) */ if (load_global_config) do_load_config(-1, NULL, CONFIG_FILE); if (config_file) { do_load_config(-1, NULL, config_file); } else { int path_max = find_path_max(); char *path = mymalloc(path_max + 1); char *home = getenv("HOME"); struct passwd *pp = getuserinfo(); if (home) snprintf(path, path_max, "%s/.multitailrc", home); else snprintf(path, path_max, "%s/.multitailrc", pp -> pw_dir); do_load_config(-1, NULL, path); myfree(path); } }
int get_vmsize(pid_t pid) { int vmsize = -1; #if defined(linux) FILE *fh; int path_max = find_path_max(); char *path = mymalloc(path_max); assert(pid > 1); snprintf(path, path_max, "/proc/%d/stat", pid); fh = fopen(path, "r"); if (fh) { char *dummystr = mymalloc(path_max); char dummychar; int dummy; if (fscanf(fh, "%d %s %c %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", &dummy, dummystr, &dummychar, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &dummy, &vmsize) != 23) vmsize = -1; fclose(fh); myfree(dummystr); } myfree(path); #endif return vmsize; }
void scrollback_savefile(buffer *pbuf) { char *file = NULL; NEWWIN *mywin = create_popup(8, 40); win_header(mywin, "Save buffer to file"); mvwprintw(mywin -> win, 4, 2, "Select file"); file = edit_string(mywin, 5, 2, 40, find_path_max(), 0, NULL, HELP_SCROLLBACK_SAVEFILE_ENTER_FILENAME, -1, &cmdfile_h, NULL); if (file) { FILE *fh = fopen(file, "w"); if (fh) { int loop; for(loop=0; loop<pbuf -> curpos; loop++) { if ((pbuf -> be)[loop].Bline) { char display = 1; char *error; int dummy = -1; regmatch_t *pmatch = NULL; /* check filter */ if (!IS_MARKERLINE((pbuf -> be)[loop].pi)) { (void)check_filter((pbuf -> be)[loop].pi, (pbuf -> be)[loop].Bline, &pmatch, &error, &dummy, 0, &display); if (error) { fprintf(fh, "%s\n", error); myfree(error); } } if (display) { fprintf(fh, "%s\n", USE_IF_SET((pbuf -> be)[loop].Bline, "")); } if (pmatch) myfree(pmatch); } } fclose(fh); } else { error_popup("Save scrollback buffer", -1, "Cannot write to file, reason: %s", strerror(errno)); } } delete_popup(mywin); }
char * select_file(char *input, int what_help) { char **list = NULL, *isdir = NULL, *path = NULL; char *new_fname = NULL; struct stat64 statbuf; int list_n, index; int strbufsize = find_path_max(); list_n = match_files(input, &path, &list, &isdir); if (list_n == 0) { myfree(path); flash(); return NULL; } index = selection_box((void **)list, isdir, list_n, SEL_FILES, what_help, NULL); if (index != -1) { new_fname = (char *)mymalloc(strbufsize + 1); snprintf(new_fname, strbufsize, "%s%s", path, list[index]); if (stat64(new_fname, &statbuf) == -1) { myfree(new_fname); new_fname = NULL; flash(); } else { if (S_ISDIR(statbuf.st_mode)) { strncat(new_fname, "/", strbufsize); } } } delete_array(list, list_n); myfree(isdir); myfree(path); return new_fname; }
int selection_box(void **list, char *needs_mark, int nlines, selbox_type_t type, int what_help, char *heading) { NEWWIN *mywin; int wlines = min(nlines, (max_y - 1) - 4); int total_win_size = wlines + 4; int win_width = max(32, max_x / 3); int wcols = win_width - 4; int pos = 0, ppos = -1, offs = 0, poffs = -1; int loop = 0, sel = -1; char first = 1; char *dummy = (char *)mymalloc(wcols + 1); int path_max = find_path_max(); char *selstr = (char *)mymalloc(path_max + 1), selfound = 0; selstr[0] = 0x00; mywin = create_popup(total_win_size, win_width); for(;;) { int c; /* draw list */ if (pos != ppos) { int entries_left = (nlines - pos); werase(mywin -> win); if (heading) win_header(mywin, heading); else if (type == SEL_WIN) win_header(mywin, "Select window"); else if (type == SEL_SUBWIN) win_header(mywin, "Select subwindow"); else if (type == SEL_FILES) win_header(mywin, "Select file"); else if (type == SEL_CSCHEME) win_header(mywin, "Select color scheme"); else if (type == SEL_HISTORY) win_header(mywin, "Select string from history"); for(loop=0; loop<min(entries_left, wlines); loop++) { char invert = generate_string(dummy, list, type, wcols, loop + pos); if (loop == offs) ui_inverse_on(mywin); if (invert) color_on(mywin, find_colorpair(COLOR_YELLOW, -1, 0)); if (needs_mark && needs_mark[loop + pos]) mvwprintw(mywin -> win, loop + 2, 1, "*"); mvwprintw(mywin -> win, loop + 2, 2, "%s", dummy); if (invert) color_off(mywin, find_colorpair(COLOR_YELLOW, -1, 0)); if (loop == offs) ui_inverse_off(mywin); } draw_border(mywin); ppos = pos; poffs = offs; } else if (poffs != offs) { int yellow_cp = find_colorpair(COLOR_YELLOW, -1, 0); char invert = generate_string(dummy, list, type, wcols, poffs + pos); if (invert) color_on(mywin, yellow_cp); mvwprintw(mywin -> win, poffs + 2, 2, "%s", dummy); if (invert) color_off(mywin, yellow_cp); invert = generate_string(dummy, list, type, wcols, offs + pos); ui_inverse_on(mywin); if (invert) color_on(mywin, yellow_cp); if (needs_mark && needs_mark[offs + pos]) mvwprintw(mywin -> win, loop + 2, 1, "*"); mvwprintw(mywin -> win, offs + 2, 2, "%s", dummy); if (invert) color_off(mywin, yellow_cp); ui_inverse_off(mywin); poffs = offs; } if (first) { first = 0; color_on(mywin, find_colorpair(COLOR_GREEN, -1, 0)); mvwprintw(mywin -> win, total_win_size - 2, 2, "Press ^G to abort"); color_off(mywin, find_colorpair(COLOR_GREEN, -1, 0)); } else { int loop, len = strlen(selstr); for(loop=0; loop<wcols; loop++) mvwprintw(mywin -> win, total_win_size - 2, 1 + loop, " "); if (!selfound) color_on(mywin, find_colorpair(COLOR_RED, -1, 0)); mvwprintw(mywin -> win, total_win_size - 2, 1, "%s", &selstr[max(0, len - wcols)]); if (!selfound) color_off(mywin, find_colorpair(COLOR_RED, -1, 0)); } mydoupdate(); c = wait_for_keypress(what_help, 0, mywin, 1); if (c == KEY_UP) { if ((offs + pos) > 0) { if (offs) offs--; else pos--; } else { wrong_key(); } } else if (c == KEY_DOWN) { if ((pos + offs) < (nlines-1)) { if (offs < (wlines-1)) offs++; else pos++; } else { wrong_key(); } } else if (c == KEY_NPAGE) { if ((pos + offs) < (nlines - 1)) { pos += min(wlines, (nlines - 1) - (pos + offs)); } else { wrong_key(); } } else if (c == KEY_PPAGE) { if ((pos + offs - wlines) >= 0) { if (pos > wlines) { pos -= wlines; } else { pos -= (wlines - offs); offs = 0; } } else if (offs > 0) { offs = 0; } else if (pos > 0) { pos = 0; } else { wrong_key(); } } else if (c == KEY_ENTER || c == 13 || c == 10) { sel = pos + offs; break; } else if (c == abort_key || c == -1) { break; } else if ((c > 31 && c != 127) || (c == KEY_BACKSPACE)) { int index, curlen; curlen = strlen(selstr); if (c == KEY_BACKSPACE) { if (curlen > 0) selstr[curlen - 1] = 0x00; else wrong_key(); } else if (curlen < path_max) { selstr[curlen] = c; selstr[curlen + 1] = 0x00; } else wrong_key(); curlen = strlen(selstr); if (curlen > 0) { index = find_sb_string(list, type, nlines, selstr); if (index != -1) { ppos = -1; sel = pos = index; selfound = 1; } else { selfound = 0; } } } else { wrong_key(); } } delete_popup(mywin); myfree(dummy); myfree(selstr); return sel; }
int match_files(char *search_for, char **path, char ***found, char **isdir) { DIR *dir; struct dirent *entry; char *cur_dir = mymalloc(find_path_max() + 1); char *fname; char **list = NULL; int nfound = 0; size_t fname_size; int path_len; int s1, s2; char *slash = strrchr(search_for, '/'); if (slash) { fname = mystrdup(slash + 1); *(slash + 1) = 0x00; *path = mystrdup(search_for); } else { *path = mystrdup("./"); fname = mystrdup(search_for); } fname_size = strlen(fname); path_len = strlen(*path); dir = opendir(*path); if (!dir) { free(cur_dir); return 0; } memcpy(cur_dir, *path, path_len + 1); while((entry = readdir(dir)) != NULL) { if ((fname_size == 0 || strncmp(entry -> d_name, fname, fname_size) == 0) && strcmp(entry -> d_name, ".") != 0 && strcmp(entry -> d_name, "..") != 0) { struct stat finfo; /* get filename */ list = (char **)myrealloc(list, (nfound + 1) * sizeof(char *)); list[nfound] = mystrdup(entry -> d_name); /* check if the file is a directory */ *isdir = (char *)myrealloc(*isdir, (nfound + 1) * sizeof(char)); strncpy(&cur_dir[path_len], entry -> d_name, max(0,find_path_max() - path_len)); if (stat(cur_dir, &finfo) == -1) { if (errno != ENOENT) /* file did not disappear? then something is very wrong */ error_exit(TRUE, FALSE, "Error while invoking stat() %s.\n", cur_dir); } (*isdir)[nfound] = S_ISDIR(finfo.st_mode)?1:0; nfound++; } } if (closedir(dir) == -1) error_exit(TRUE, FALSE, "closedir() failed\n"); /* qsort( (void *)list, (size_t)nfound, sizeof(char *), compare_filenames); */ for(s1=0; s1<(nfound - 1); s1++) { for(s2=s1+1; s2<nfound; s2++) { if (strcasecmp(list[s2], list[s1]) < 0) { char *fdummy = list[s1], ddummy = (*isdir)[s1]; list[s1] = list[s2]; (*isdir)[s1] = (*isdir)[s2]; list[s2] = fdummy; (*isdir)[s2] = ddummy; } } } *found = list; myfree(fname); myfree(cur_dir); return nfound; }