2 * Copyright (C) 2004-2006 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file dbadm.c simple attribute setting utility for the mysql selector */
13 //#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
17 static int att_win_lines
, att_win_cols
;
18 static int att_format_lines
, att_format_cols
;
19 static int atts_modified
, refresh_file
= 1;
20 static int n_choices
, choice_len
;
25 static char **choices
;
28 void para_log(__a_unused
int ll
, __a_unused
const char *fmt
,...)
32 static int client_cmd(const char *cmd
)
35 int ret
, fds
[3] = {0, 1, 0};
36 char *cmdline
= make_message(BINDIR
"/para_client %s", cmd
);
37 ret
= para_exec_cmdline_pid(&pid
, cmdline
, fds
);
44 static char **get_all_atts(int *num_atts
)
46 int fd
= client_cmd("laa");
48 char **ret
= NULL
, *buf
;
58 buf
= para_malloc(MAXLINE
* sizeof(char));
59 while (fgets(buf
, MAXLINE
- 1, f
) && *buf
) {
60 size_t n
= strlen(buf
);
62 if (choice_len
< n
- 1)
64 ret
= para_realloc(ret
, (*num_atts
+ 1) * sizeof(char*));
65 ret
[*num_atts
] = para_strdup(buf
);
72 static char *get_atts(char *filename
)
74 int n
, fd
, bufsize
= (n_choices
* (choice_len
+ 1) + 10) * sizeof(char);
75 char *cmd
= make_message("la %s", filename
), *buf
;
81 buf
= para_malloc(bufsize
* sizeof(char));
82 n
= read(fd
, buf
, bufsize
- 1);
83 if (n
<= 0 ||strstr(buf
, "Not contained in database")) {
90 static void _item_init(__a_unused MENU
* menu
)
92 // static int subsequent_run;
100 for (i
= 0; i
< n_choices
; i
++)
101 set_item_value(my_items
[i
], FALSE
);
102 while (sscanf(p
, "%s%n", att
, &n
) > 0) {
103 //mvprintw(LINES - 4, 0, "aaaitem.");
105 for (i
= 0; i
< n_choices
; i
++) {
106 if (!strcmp(item_name(my_items
[i
]), att
)) {
107 set_item_value(my_items
[i
], TRUE
);
127 static void init_colors(void)
130 init_pair(COLOR_FRAME
, COLOR_BLUE
, COLOR_BLACK
);
131 init_pair(COLOR_FILENAME
, COLOR_MAGENTA
, COLOR_BLACK
);
132 init_pair(COLOR_ACTIVE_ITEM
, COLOR_RED
, COLOR_WHITE
);
133 init_pair(COLOR_INACTIVE_ITEM
, COLOR_WHITE
, COLOR_BLACK
);
136 static int commit_changes(char *filename
)
140 char buf
[MAXLINE
] = "para_client sa ";
141 int fds
[3] = {0, 0, 0};
144 for (i
= 0; i
< n_choices
; ++i
) {
145 strcat(buf
, item_name(my_items
[i
]));
146 if (item_value(my_items
[i
])) {
147 // printf("%s\n", item_name(my_items[i]));
152 strcat(buf
, filename
);
153 //printf("old atts: %s\n", atts);
154 //printf("%s\n", buf);
155 return para_exec_cmdline_pid(&pid
, buf
, fds
);
158 static char *get_current_filename(void)
160 char *bn
= NULL
, *buf
= para_malloc(MAXLINE
* sizeof(char));
163 fd
= client_cmd("sc 1");
166 ret
= read(fd
, buf
, MAXLINE
- 1);
170 bn
= para_basename(buf
);
177 static void print_filename(char *filename
)
179 char *tmp
= strdup(filename
);
180 int maxlen
= att_win_cols
- 2;
182 wattron(att_win
, COLOR_PAIR(COLOR_FILENAME
));
183 if (strlen(filename
) > maxlen
)
185 wmove(att_win
, 1, 1);
187 mvwprintw(att_win
, 1, 1, "%s", tmp
);
188 wattron(att_win
, COLOR_PAIR(COLOR_FRAME
));
189 mvwaddch(att_win
, 2, att_win_cols
- 1, ACS_RTEE
);
193 static int com_refresh_file(char *filename
) {
196 filename
= get_current_filename();
199 atts
= get_atts(filename
);
202 print_filename(filename
);
206 static int init_curses(void)
208 /* Initialize curses */
211 if (LINES
<= OFFSET
+ 4)
214 att_format_cols
= (COLS
- 2 * OFFSET
- 3) / (choice_len
+ 1);
215 if (att_format_cols
< 1)
217 att_format_lines
= (n_choices
- 1) / att_format_cols
+ 1;
218 if (att_format_lines
+ OFFSET
+ 4 > LINES
)
219 att_format_lines
= LINES
- OFFSET
- 4;
221 att_win_lines
= att_format_lines
+ 4;
222 att_win_cols
= (choice_len
+ 1) * att_format_cols
+ 1;
223 if (att_win_lines
+ OFFSET
> LINES
)
224 att_win_lines
= LINES
- OFFSET
- 1;
225 if (att_win_cols
+ 2 * OFFSET
> COLS
)
226 att_win_cols
= COLS
- 2 * OFFSET
+ 1;
227 //printf ("%i:%i, %i:%i\n", att_format_lines, att_format_cols, att_win_lines, att_win_cols); fflush(stdout); sleep(2);
231 keypad(stdscr
, TRUE
);
236 int main(int argc
, char *argv
[])
238 int c
, i
, ret
= EXIT_FAILURE
;
239 MENU
*my_menu
= NULL
;
242 choices
= get_all_atts(&n_choices
);
243 if (!choices
|| n_choices
<= 0)
246 filename
= get_current_filename();
250 filename
= strdup(argv
[1]);
251 atts
= get_atts(filename
);
254 if (init_curses() < 0)
257 /* Initialize items */
258 my_items
= (ITEM
**)calloc(n_choices
+ 1, sizeof(ITEM
*));
259 for (i
= 0; i
< n_choices
; ++i
)
260 my_items
[i
] = new_item(choices
[i
], "");
262 my_menu
= new_menu(my_items
);
263 set_item_init(my_menu
, _item_init
);
264 /* Make the menu multi valued */
265 menu_opts_off(my_menu
, O_ONEVALUE
);
266 /* Set menu option not to show the description */
267 menu_opts_off(my_menu
, O_SHOWDESC
);
269 /* Create the window to be associated with the menu */
270 att_win
= newwin(att_win_lines
, att_win_cols
, OFFSET
, OFFSET
);
271 keypad(att_win
, TRUE
);
272 /* Set main window and sub window */
273 set_menu_win(my_menu
, att_win
);
274 set_menu_sub(my_menu
, derwin(att_win
, att_win_lines
- 4,
275 att_win_cols
- 2, 3, 1));
276 set_menu_format(my_menu
, att_format_lines
, att_format_cols
);
277 //set_menu_format(my_menu, 5, 1);
278 set_menu_mark(my_menu
, "");
280 /* Print a border around the main window and print a title */
281 wattron(att_win
, COLOR_PAIR(COLOR_FRAME
));
283 mvwhline(att_win
, 2, 1, ACS_HLINE
, att_win_cols
- 2);
284 mvwaddch(att_win
, 2, 0, ACS_LTEE
);
285 print_filename(filename
);
286 set_menu_fore(my_menu
, COLOR_PAIR(COLOR_ACTIVE_ITEM
) | A_REVERSE
);
287 set_menu_back(my_menu
, COLOR_PAIR(COLOR_INACTIVE_ITEM
));
296 menu_driver(my_menu
, REQ_DOWN_ITEM
);
299 menu_driver(my_menu
, REQ_UP_ITEM
);
302 menu_driver(my_menu
, REQ_LEFT_ITEM
);
305 menu_driver(my_menu
, REQ_RIGHT_ITEM
);
312 com_refresh_file(filename
);
318 menu_driver(my_menu
, REQ_TOGGLE_ITEM
);
324 commit_changes(filename
);
331 free_item(my_items
[0]);
332 free_item(my_items
[1]);
334 for (i
= 0; i
< n_choices
; i
++)