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 ";
153 int fds
[3] = {0, 0, 0};
156 for (i
= 0; i
< n_choices
; ++i
) {
157 strcat(buf
, item_name(my_items
[i
]));
158 if (item_value(my_items
[i
])) {
159 // printf("%s\n", item_name(my_items[i]));
164 strcat(buf
, filename
);
165 //printf("old atts: %s\n", atts);
166 //printf("%s\n", buf);
167 return para_exec_cmdline_pid(&pid
, buf
, fds
);
170 static char *get_current_filename(void)
172 char *bn
= NULL
, *buf
= para_malloc(MAXLINE
* sizeof(char));
175 fd
= client_cmd("sc 1");
178 ret
= read(fd
, buf
, MAXLINE
- 1);
182 bn
= para_basename(buf
);
189 static void print_filename(char *filename
)
191 char *tmp
= strdup(filename
);
192 int maxlen
= att_win_cols
- 2;
194 wattron(att_win
, COLOR_PAIR(COLOR_FILENAME
));
195 if (strlen(filename
) > maxlen
)
197 wmove(att_win
, 1, 1);
199 mvwprintw(att_win
, 1, 1, "%s", tmp
);
200 wattron(att_win
, COLOR_PAIR(COLOR_FRAME
));
201 mvwaddch(att_win
, 2, att_win_cols
- 1, ACS_RTEE
);
205 static int com_refresh_file(char *filename
) {
208 filename
= get_current_filename();
211 atts
= get_atts(filename
);
214 print_filename(filename
);
218 static int init_curses(void)
220 /* Initialize curses */
223 if (LINES
<= OFFSET
+ 4)
226 att_format_cols
= (COLS
- 2 * OFFSET
- 3) / (choice_len
+ 1);
227 if (att_format_cols
< 1)
229 att_format_lines
= (n_choices
- 1) / att_format_cols
+ 1;
230 if (att_format_lines
+ OFFSET
+ 4 > LINES
)
231 att_format_lines
= LINES
- OFFSET
- 4;
233 att_win_lines
= att_format_lines
+ 4;
234 att_win_cols
= (choice_len
+ 1) * att_format_cols
+ 1;
235 if (att_win_lines
+ OFFSET
> LINES
)
236 att_win_lines
= LINES
- OFFSET
- 1;
237 if (att_win_cols
+ 2 * OFFSET
> COLS
)
238 att_win_cols
= COLS
- 2 * OFFSET
+ 1;
239 //printf ("%i:%i, %i:%i\n", att_format_lines, att_format_cols, att_win_lines, att_win_cols); fflush(stdout); sleep(2);
243 keypad(stdscr
, TRUE
);
248 int main(int argc
, char *argv
[])
250 int c
, i
, ret
= EXIT_FAILURE
;
251 MENU
*my_menu
= NULL
;
254 choices
= get_all_atts(&n_choices
);
255 if (!choices
|| n_choices
<= 0)
258 filename
= get_current_filename();
262 filename
= strdup(argv
[1]);
263 atts
= get_atts(filename
);
266 if (init_curses() < 0)
269 /* Initialize items */
270 my_items
= (ITEM
**)calloc(n_choices
+ 1, sizeof(ITEM
*));
271 for (i
= 0; i
< n_choices
; ++i
)
272 my_items
[i
] = new_item(choices
[i
], "");
274 my_menu
= new_menu(my_items
);
275 set_item_init(my_menu
, _item_init
);
276 /* Make the menu multi valued */
277 menu_opts_off(my_menu
, O_ONEVALUE
);
278 /* Set menu option not to show the description */
279 menu_opts_off(my_menu
, O_SHOWDESC
);
281 /* Create the window to be associated with the menu */
282 att_win
= newwin(att_win_lines
, att_win_cols
, OFFSET
, OFFSET
);
283 keypad(att_win
, TRUE
);
284 /* Set main window and sub window */
285 set_menu_win(my_menu
, att_win
);
286 set_menu_sub(my_menu
, derwin(att_win
, att_win_lines
- 4,
287 att_win_cols
- 2, 3, 1));
288 set_menu_format(my_menu
, att_format_lines
, att_format_cols
);
289 //set_menu_format(my_menu, 5, 1);
290 set_menu_mark(my_menu
, "");
292 /* Print a border around the main window and print a title */
293 wattron(att_win
, COLOR_PAIR(COLOR_FRAME
));
295 mvwhline(att_win
, 2, 1, ACS_HLINE
, att_win_cols
- 2);
296 mvwaddch(att_win
, 2, 0, ACS_LTEE
);
297 print_filename(filename
);
298 set_menu_fore(my_menu
, COLOR_PAIR(COLOR_ACTIVE_ITEM
) | A_REVERSE
);
299 set_menu_back(my_menu
, COLOR_PAIR(COLOR_INACTIVE_ITEM
));
308 menu_driver(my_menu
, REQ_DOWN_ITEM
);
311 menu_driver(my_menu
, REQ_UP_ITEM
);
314 menu_driver(my_menu
, REQ_LEFT_ITEM
);
317 menu_driver(my_menu
, REQ_RIGHT_ITEM
);
324 com_refresh_file(filename
);
330 menu_driver(my_menu
, REQ_TOGGLE_ITEM
);
336 commit_changes(filename
);
343 free_item(my_items
[0]);
344 free_item(my_items
[1]);
346 for (i
= 0; i
< n_choices
; i
++)