2 * Copyright (C) 2004-2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file dbadm.c simple attribute setting utility for the mysql selector */
25 //#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
29 static int att_win_lines
, att_win_cols
;
30 static int att_format_lines
, att_format_cols
;
31 static int atts_modified
, refresh_file
= 1;
32 static int n_choices
, choice_len
;
37 static char **choices
;
40 void para_log(__a_unused
int ll
, __a_unused
const char *fmt
,...)
44 static int client_cmd(const char *cmd
)
47 int ret
, fds
[3] = {0, 1, 0};
48 char *cmdline
= make_message(BINDIR
"/para_client %s", cmd
);
49 ret
= para_exec_cmdline_pid(&pid
, cmdline
, fds
);
56 static char **get_all_atts(int *num_atts
)
58 int fd
= client_cmd("laa");
60 char **ret
= NULL
, *buf
;
70 buf
= para_malloc(MAXLINE
* sizeof(char));
71 while (fgets(buf
, MAXLINE
- 1, f
) && *buf
) {
72 size_t n
= strlen(buf
);
74 if (choice_len
< n
- 1)
76 ret
= para_realloc(ret
, (*num_atts
+ 1) * sizeof(char*));
77 ret
[*num_atts
] = para_strdup(buf
);
84 static char *get_atts(char *filename
)
86 int n
, fd
, bufsize
= (n_choices
* (choice_len
+ 1) + 10) * sizeof(char);
87 char *cmd
= make_message("la %s", filename
), *buf
;
93 buf
= para_malloc(bufsize
* sizeof(char));
94 n
= read(fd
, buf
, bufsize
- 1);
95 if (n
<= 0 ||strstr(buf
, "Not contained in database")) {
102 static void _item_init(__a_unused MENU
* menu
)
104 // static int subsequent_run;
112 for (i
= 0; i
< n_choices
; i
++)
113 set_item_value(my_items
[i
], FALSE
);
114 while (sscanf(p
, "%s%n", att
, &n
) > 0) {
115 //mvprintw(LINES - 4, 0, "aaaitem.");
117 for (i
= 0; i
< n_choices
; i
++) {
118 if (!strcmp(item_name(my_items
[i
]), att
)) {
119 set_item_value(my_items
[i
], TRUE
);
139 static void init_colors(void)
142 init_pair(COLOR_FRAME
, COLOR_BLUE
, COLOR_BLACK
);
143 init_pair(COLOR_FILENAME
, COLOR_MAGENTA
, COLOR_BLACK
);
144 init_pair(COLOR_ACTIVE_ITEM
, COLOR_RED
, COLOR_WHITE
);
145 init_pair(COLOR_INACTIVE_ITEM
, COLOR_WHITE
, COLOR_BLACK
);
148 static int commit_changes(char *filename
)
152 char buf
[MAXLINE
] = "para_client sa ";
154 for (i
= 0; i
< n_choices
; ++i
) {
155 strcat(buf
, item_name(my_items
[i
]));
156 if (item_value(my_items
[i
])) {
157 // printf("%s\n", item_name(my_items[i]));
162 strcat(buf
, filename
);
163 //printf("old atts: %s\n", atts);
164 //printf("%s\n", buf);
168 static char *get_current_filename(void)
170 char *bn
= NULL
, *buf
= para_malloc(MAXLINE
* sizeof(char));
173 fd
= client_cmd("sc 1");
176 ret
= read(fd
, buf
, MAXLINE
- 1);
180 bn
= para_basename(buf
);
187 static void print_filename(char *filename
)
189 char *tmp
= strdup(filename
);
190 int maxlen
= att_win_cols
- 2;
192 wattron(att_win
, COLOR_PAIR(COLOR_FILENAME
));
193 if (strlen(filename
) > maxlen
)
195 wmove(att_win
, 1, 1);
197 mvwprintw(att_win
, 1, 1, "%s", tmp
);
198 wattron(att_win
, COLOR_PAIR(COLOR_FRAME
));
199 mvwaddch(att_win
, 2, att_win_cols
- 1, ACS_RTEE
);
203 static int com_refresh_file(char *filename
) {
206 filename
= get_current_filename();
209 atts
= get_atts(filename
);
212 print_filename(filename
);
216 static int init_curses(void)
218 /* Initialize curses */
221 if (LINES
<= OFFSET
+ 4)
224 att_format_cols
= (COLS
- 2 * OFFSET
- 3) / (choice_len
+ 1);
225 if (att_format_cols
< 1)
227 att_format_lines
= (n_choices
- 1) / att_format_cols
+ 1;
228 if (att_format_lines
+ OFFSET
+ 4 > LINES
)
229 att_format_lines
= LINES
- OFFSET
- 4;
231 att_win_lines
= att_format_lines
+ 4;
232 att_win_cols
= (choice_len
+ 1) * att_format_cols
+ 1;
233 if (att_win_lines
+ OFFSET
> LINES
)
234 att_win_lines
= LINES
- OFFSET
- 1;
235 if (att_win_cols
+ 2 * OFFSET
> COLS
)
236 att_win_cols
= COLS
- 2 * OFFSET
+ 1;
237 //printf ("%i:%i, %i:%i\n", att_format_lines, att_format_cols, att_win_lines, att_win_cols); fflush(stdout); sleep(2);
241 keypad(stdscr
, TRUE
);
246 int main(int argc
, char *argv
[])
248 int c
, i
, ret
= EXIT_FAILURE
;
249 MENU
*my_menu
= NULL
;
252 choices
= get_all_atts(&n_choices
);
253 if (!choices
|| n_choices
<= 0)
256 filename
= get_current_filename();
260 filename
= strdup(argv
[1]);
261 atts
= get_atts(filename
);
264 if (init_curses() < 0)
267 /* Initialize items */
268 my_items
= (ITEM
**)calloc(n_choices
+ 1, sizeof(ITEM
*));
269 for (i
= 0; i
< n_choices
; ++i
)
270 my_items
[i
] = new_item(choices
[i
], "");
272 my_menu
= new_menu(my_items
);
273 set_item_init(my_menu
, _item_init
);
274 /* Make the menu multi valued */
275 menu_opts_off(my_menu
, O_ONEVALUE
);
276 /* Set menu option not to show the description */
277 menu_opts_off(my_menu
, O_SHOWDESC
);
279 /* Create the window to be associated with the menu */
280 att_win
= newwin(att_win_lines
, att_win_cols
, OFFSET
, OFFSET
);
281 keypad(att_win
, TRUE
);
282 /* Set main window and sub window */
283 set_menu_win(my_menu
, att_win
);
284 set_menu_sub(my_menu
, derwin(att_win
, att_win_lines
- 4,
285 att_win_cols
- 2, 3, 1));
286 set_menu_format(my_menu
, att_format_lines
, att_format_cols
);
287 //set_menu_format(my_menu, 5, 1);
288 set_menu_mark(my_menu
, "");
290 /* Print a border around the main window and print a title */
291 wattron(att_win
, COLOR_PAIR(COLOR_FRAME
));
293 mvwhline(att_win
, 2, 1, ACS_HLINE
, att_win_cols
- 2);
294 mvwaddch(att_win
, 2, 0, ACS_LTEE
);
295 print_filename(filename
);
296 set_menu_fore(my_menu
, COLOR_PAIR(COLOR_ACTIVE_ITEM
) | A_REVERSE
);
297 set_menu_back(my_menu
, COLOR_PAIR(COLOR_INACTIVE_ITEM
));
306 menu_driver(my_menu
, REQ_DOWN_ITEM
);
309 menu_driver(my_menu
, REQ_UP_ITEM
);
312 menu_driver(my_menu
, REQ_LEFT_ITEM
);
315 menu_driver(my_menu
, REQ_RIGHT_ITEM
);
322 com_refresh_file(filename
);
328 menu_driver(my_menu
, REQ_TOGGLE_ITEM
);
335 commit_changes(filename
);
337 printf("Attributes unchanged\n");
344 free_item(my_items
[0]);
345 free_item(my_items
[1]);
347 for (i
= 0; i
< n_choices
; i
++)