+++ /dev/null
-/*
- * Copyright (C) 2004-2006 Andre Noll <maan@systemlinux.org>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
-
-/** \file dbadm.c simple attribute setting utility for the mysql selector */
-
-#include "para.h"
-#include <menu.h>
-#include "string.h"
-
-//#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
-#define MAXLINE 255
-
-#define OFFSET 4
-static int att_win_lines, att_win_cols;
-static int att_format_lines, att_format_cols;
-static int atts_modified, refresh_file = 1;
-static int n_choices, choice_len;
-static char *atts;
-ITEM **my_items;
-WINDOW *att_win;
-
-static char **choices;
-
-/* no looging */
-void para_log(__a_unused int ll, __a_unused const char *fmt,...)
-{
-}
-
-static int client_cmd(const char *cmd)
-{
- pid_t pid;
- int ret, fds[3] = {0, 1, 0};
- char *cmdline = make_message(BINDIR "/para_client %s", cmd);
- ret = para_exec_cmdline_pid(&pid, cmdline, fds);
- free(cmdline);
- if (ret < 0)
- return -1;
- return fds[1];
-}
-
-static char **get_all_atts(int *num_atts)
-{
- int fd = client_cmd("laa");
- FILE *f;
- char **ret = NULL, *buf;
-
- if (fd < 0)
- return NULL;
- f = fdopen(fd, "r");
- if (!f) {
- close(fd);
- return NULL;
- }
- *num_atts = 0;
- buf = para_malloc(MAXLINE * sizeof(char));
- while (fgets(buf, MAXLINE - 1, f) && *buf) {
- size_t n = strlen(buf);
- buf[n - 1] = '\0';
- if (choice_len < n - 1)
- choice_len = n - 1;
- ret = para_realloc(ret, (*num_atts + 1) * sizeof(char*));
- ret[*num_atts] = para_strdup(buf);
- *num_atts += 1;
- }
- free(buf);
- return ret;
-}
-
-static char *get_atts(char *filename)
-{
- int n, fd, bufsize = (n_choices * (choice_len + 1) + 10) * sizeof(char);
- char *cmd = make_message("la %s", filename), *buf;
-
- fd = client_cmd(cmd);
- free(cmd);
- if (fd < 0)
- return NULL;
- buf = para_malloc(bufsize * sizeof(char));
- n = read(fd, buf, bufsize - 1);
- if (n <= 0 ||strstr(buf, "Not contained in database")) {
- free(buf);
- return NULL;
- };
- return buf;
-}
-
-static void _item_init(__a_unused MENU* menu)
-{
-// static int subsequent_run;
- int i, n;
- char *p = atts;
- char att[MAXLINE];
-
- if (!refresh_file)
- return;
- refresh_file = 0;
- for (i = 0; i < n_choices; i++)
- set_item_value(my_items[i], FALSE);
- while (sscanf(p, "%s%n", att, &n) > 0) {
- //mvprintw(LINES - 4, 0, "aaaitem.");
- p += n + 1;
- for (i = 0; i < n_choices; i++) {
- if (!strcmp(item_name(my_items[i]), att)) {
- set_item_value(my_items[i], TRUE);
- break;
- }
- }
- }
-}
-
-struct color_pair {
- int bg;
- int fg;
-};
-
-enum {
- COLOR_DUMMY,
- COLOR_FRAME,
- COLOR_FILENAME,
- COLOR_ACTIVE_ITEM,
- COLOR_INACTIVE_ITEM
-};
-
-static void init_colors(void)
-{
- start_color();
- init_pair(COLOR_FRAME, COLOR_BLUE, COLOR_BLACK);
- init_pair(COLOR_FILENAME, COLOR_MAGENTA, COLOR_BLACK);
- init_pair(COLOR_ACTIVE_ITEM, COLOR_RED, COLOR_WHITE);
- init_pair(COLOR_INACTIVE_ITEM, COLOR_WHITE, COLOR_BLACK);
-}
-
-static int commit_changes(char *filename)
-{
-// ITEM **items;
- int i;
- char buf[MAXLINE] = "para_client sa ";
- int fds[3] = {0, 0, 0};
- pid_t pid;
-
- for (i = 0; i < n_choices; ++i) {
- strcat(buf, item_name(my_items[i]));
- if (item_value(my_items[i])) {
- // printf("%s\n", item_name(my_items[i]));
- strcat(buf, "+ ");
- } else
- strcat(buf, "- ");
- }
- strcat(buf, filename);
- //printf("old atts: %s\n", atts);
- //printf("%s\n", buf);
- return para_exec_cmdline_pid(&pid, buf, fds);
-}
-
-static char *get_current_filename(void)
-{
- char *bn = NULL, *buf = para_malloc(MAXLINE * sizeof(char));
- int ret, fd;
-
- fd = client_cmd("sc 1");
- if (fd < 0)
- return NULL;
- ret = read(fd, buf, MAXLINE - 1);
- if (ret <= 0)
- goto out;
- buf[ret] = '\0';
- bn = para_basename(buf);
- free(buf);
-out:
- close(fd);
- return bn;
-}
-
-static void print_filename(char *filename)
-{
- char *tmp = strdup(filename);
- int maxlen = att_win_cols - 2;
-
- wattron(att_win, COLOR_PAIR(COLOR_FILENAME));
- if (strlen(filename) > maxlen)
- tmp[maxlen] = '\0';
- wmove(att_win, 1, 1);
- clrtoeol();
- mvwprintw(att_win, 1, 1, "%s", tmp);
- wattron(att_win, COLOR_PAIR(COLOR_FRAME));
- mvwaddch(att_win, 2, att_win_cols - 1, ACS_RTEE);
- free(tmp);
-}
-
-static int com_refresh_file(char *filename) {
-
-
- filename = get_current_filename();
- if (!filename)
- return -1;
- atts = get_atts(filename);
- if (!atts)
- return -1;
- print_filename(filename);
- return 1;
-}
-
-static int init_curses(void)
-{
- /* Initialize curses */
- initscr();
-
- if (LINES <= OFFSET + 4)
- return -1;
-
- att_format_cols = (COLS - 2 * OFFSET - 3) / (choice_len + 1);
- if (att_format_cols < 1)
- return -1;
- att_format_lines = (n_choices - 1) / att_format_cols + 1;
- if (att_format_lines + OFFSET + 4 > LINES)
- att_format_lines = LINES - OFFSET - 4;
-
- att_win_lines = att_format_lines + 4;
- att_win_cols = (choice_len + 1) * att_format_cols + 1;
- if (att_win_lines + OFFSET > LINES)
- att_win_lines = LINES - OFFSET - 1;
- if (att_win_cols + 2 * OFFSET > COLS)
- att_win_cols = COLS - 2 * OFFSET + 1;
- //printf ("%i:%i, %i:%i\n", att_format_lines, att_format_cols, att_win_lines, att_win_cols); fflush(stdout); sleep(2);
-
- cbreak();
- noecho();
- keypad(stdscr, TRUE);
- init_colors();
- return 1;
-}
-
-int main(int argc, char *argv[])
-{
- int c, i, ret = EXIT_FAILURE;
- MENU *my_menu = NULL;
- char *filename;
-
- choices = get_all_atts(&n_choices);
- if (!choices || n_choices <= 0)
- exit(EXIT_FAILURE);
- if (argc < 2) {
- filename = get_current_filename();
- if (!filename)
- exit(EXIT_FAILURE);
- } else
- filename = strdup(argv[1]);
- atts = get_atts(filename);
- if (!atts)
- goto out;
- if (init_curses() < 0)
- goto out;
-
- /* Initialize items */
- my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
- for (i = 0; i < n_choices; ++i)
- my_items[i] = new_item(choices[i], "");
-
- my_menu = new_menu(my_items);
- set_item_init(my_menu, _item_init);
- /* Make the menu multi valued */
- menu_opts_off(my_menu, O_ONEVALUE);
- /* Set menu option not to show the description */
- menu_opts_off(my_menu, O_SHOWDESC);
-
- /* Create the window to be associated with the menu */
- att_win = newwin(att_win_lines, att_win_cols, OFFSET, OFFSET);
- keypad(att_win, TRUE);
- /* Set main window and sub window */
- set_menu_win(my_menu, att_win);
- set_menu_sub(my_menu, derwin(att_win, att_win_lines - 4,
- att_win_cols - 2, 3, 1));
- set_menu_format(my_menu, att_format_lines, att_format_cols);
- //set_menu_format(my_menu, 5, 1);
- set_menu_mark(my_menu, "");
-
- /* Print a border around the main window and print a title */
- wattron(att_win, COLOR_PAIR(COLOR_FRAME));
- box(att_win, 0, 0);
- mvwhline(att_win, 2, 1, ACS_HLINE , att_win_cols - 2);
- mvwaddch(att_win, 2, 0, ACS_LTEE);
- print_filename(filename);
- set_menu_fore(my_menu, COLOR_PAIR(COLOR_ACTIVE_ITEM) | A_REVERSE);
- set_menu_back(my_menu, COLOR_PAIR(COLOR_INACTIVE_ITEM));
- refresh();
- post_menu(my_menu);
-
-repeat:
- wrefresh(att_win);
- c = getch();
- switch(c) {
- case KEY_DOWN:
- menu_driver(my_menu, REQ_DOWN_ITEM);
- goto repeat;
- case KEY_UP:
- menu_driver(my_menu, REQ_UP_ITEM);
- goto repeat;
- case KEY_LEFT:
- menu_driver(my_menu, REQ_LEFT_ITEM);
- goto repeat;
- case KEY_RIGHT:
- menu_driver(my_menu, REQ_RIGHT_ITEM);
- goto repeat;
-
- case 'q':
- ret = EXIT_SUCCESS;
- goto out;
- case 'r':
- com_refresh_file(filename);
- refresh_file = 1;
- _item_init(NULL);
- goto repeat;
-
- case ' ':
- menu_driver(my_menu, REQ_TOGGLE_ITEM);
- atts_modified = 1;
- goto repeat;
- /* Enter */
- case 10:
- if (atts_modified)
- commit_changes(filename);
- goto out;
- default:
- goto repeat;
- }
-out:
- if (my_items) {
- free_item(my_items[0]);
- free_item(my_items[1]);
- }
- for (i = 0; i < n_choices; i++)
- free(choices[i]);
- free(choices);
- if (my_menu)
- free_menu(my_menu);
- if (atts)
- free(atts);
- if (filename)
- free(filename);
- endwin();
- exit(ret);
-}
-