Example #1
0
/*
 * Run the Device/Target Group Layout dialog
 */
void devTgtGrpLayoutDialog(CDKSCREEN *main_cdk_screen) {
    CDKSWINDOW *alua_info = 0;
    char *swindow_info[MAX_ALUA_LAYOUT_LINES] = {NULL};
    int i = 0, line_pos = 0;
    char dir_name[MAX_SYSFS_PATH_SIZE] = {0},
            tmp_buff[MAX_SYSFS_ATTR_SIZE] = {0};
    DIR *dev_grp_dir_stream = NULL, *tgt_grp_dir_stream = NULL,
            *tgt_dir_stream = NULL, *dev_dir_stream = NULL;
    struct dirent *dev_grp_dir_entry = NULL, *tgt_grp_dir_entry = NULL,
            *tgt_dir_entry = NULL, *dev_dir_entry = NULL;

    /* Setup scrolling window widget */
    alua_info = newCDKSwindow(main_cdk_screen, CENTER, CENTER,
            (ALUA_LAYOUT_ROWS + 2), (ALUA_LAYOUT_COLS + 2),
            "<C></31/B>SCST ALUA Device/Target Group Layout\n",
            MAX_ALUA_LAYOUT_LINES, TRUE, FALSE);
    if (!alua_info) {
        errorDialog(main_cdk_screen, SWINDOW_ERR_MSG, NULL);
        return;
    }
    setCDKSwindowBackgroundAttrib(alua_info, COLOR_DIALOG_TEXT);
    setCDKSwindowBoxAttribute(alua_info, COLOR_DIALOG_BOX);

    line_pos = 0;
    while (1) {
        /* We'll start with the SCST device groups */
        snprintf(dir_name, MAX_SYSFS_PATH_SIZE,
                "%s/device_groups", SYSFS_SCST_TGT);
        if ((dev_grp_dir_stream = opendir(dir_name)) == NULL) {
            if (line_pos < MAX_ALUA_LAYOUT_LINES) {
                SAFE_ASPRINTF(&swindow_info[line_pos], "opendir(): %s",
                        strerror(errno));
                line_pos++;
            }
            break;
        }
        while ((dev_grp_dir_entry = readdir(dev_grp_dir_stream)) != NULL) {
            /* The device group names are directories; skip '.' and '..' */
            if ((dev_grp_dir_entry->d_type == DT_DIR) &&
                    (strcmp(dev_grp_dir_entry->d_name, ".") != 0) &&
                    (strcmp(dev_grp_dir_entry->d_name, "..") != 0)) {
                if (line_pos < MAX_ALUA_LAYOUT_LINES) {
                    SAFE_ASPRINTF(&swindow_info[line_pos],
                            "</B>Device Group:<!B> %s",
                            dev_grp_dir_entry->d_name);
                    line_pos++;
                }

                /* Now get all of the device names for this device group */
                snprintf(dir_name, MAX_SYSFS_PATH_SIZE,
                        "%s/device_groups/%s/devices",
                        SYSFS_SCST_TGT, dev_grp_dir_entry->d_name);
                if ((dev_dir_stream = opendir(dir_name)) == NULL) {
                    if (line_pos < MAX_ALUA_LAYOUT_LINES) {
                        SAFE_ASPRINTF(&swindow_info[line_pos],
                                "opendir(): %s", strerror(errno));
                        line_pos++;
                    }
                    break;
                }
                while ((dev_dir_entry = readdir(dev_dir_stream)) != NULL) {
                    /* The device names are links */
                    if (dev_dir_entry->d_type == DT_LNK) {
                        if (line_pos < MAX_ALUA_LAYOUT_LINES) {
                            SAFE_ASPRINTF(&swindow_info[line_pos],
                                    "\t</B>Device:<!B> %s",
                                    dev_dir_entry->d_name);
                            line_pos++;
                        }
                    }
                }
                closedir(dev_dir_stream);

                /* Now get all of the target groups for this device group */
                snprintf(dir_name, MAX_SYSFS_PATH_SIZE,
                        "%s/device_groups/%s/target_groups", SYSFS_SCST_TGT,
                        dev_grp_dir_entry->d_name);
                if ((tgt_grp_dir_stream = opendir(dir_name)) == NULL) {
                    if (line_pos < MAX_ALUA_LAYOUT_LINES) {
                        SAFE_ASPRINTF(&swindow_info[line_pos], "opendir(): %s",
                                strerror(errno));
                        line_pos++;
                    }
                    break;
                }
                while ((tgt_grp_dir_entry = readdir(tgt_grp_dir_stream)) !=
                        NULL) {
                    /* The target group names are directories;
                     * skip '.' and '..' */
                    if ((tgt_grp_dir_entry->d_type == DT_DIR) &&
                            (strcmp(tgt_grp_dir_entry->d_name, ".") != 0) &&
                            (strcmp(tgt_grp_dir_entry->d_name, "..") != 0)) {
                        if (line_pos < MAX_ALUA_LAYOUT_LINES) {
                            snprintf(dir_name, MAX_SYSFS_PATH_SIZE,
                            "%s/device_groups/%s/target_groups/%s/group_id",
                            SYSFS_SCST_TGT, dev_grp_dir_entry->d_name,
                                    tgt_grp_dir_entry->d_name);
                            readAttribute(dir_name, tmp_buff);
                            SAFE_ASPRINTF(&swindow_info[line_pos],
                                    "\t</B>Target Group:<!B> %s (Group ID: %s)",
                                    tgt_grp_dir_entry->d_name, tmp_buff);
                            line_pos++;
                        }

                        /* Loop over each target name for the
                         * current target group */
                        snprintf(dir_name, MAX_SYSFS_PATH_SIZE,
                                "%s/device_groups/%s/target_groups/%s",
                                SYSFS_SCST_TGT, dev_grp_dir_entry->d_name,
                                tgt_grp_dir_entry->d_name);
                        if ((tgt_dir_stream = opendir(dir_name)) == NULL) {
                            if (line_pos < MAX_ALUA_LAYOUT_LINES) {
                                SAFE_ASPRINTF(&swindow_info[line_pos],
                                        "opendir(): %s", strerror(errno));
                                line_pos++;
                            }
                            break;
                        }
                        while ((tgt_dir_entry = readdir(tgt_dir_stream)) !=
                                NULL) {
                            /* The target names are links (if local),
                             * or directories (if remote); skip '.' and '..' */
                            if (((tgt_dir_entry->d_type == DT_DIR) &&
                                    (strcmp(tgt_dir_entry->d_name,
                                    ".") != 0) &&
                                    (strcmp(tgt_dir_entry->d_name,
                                    "..") != 0)) ||
                                    (tgt_dir_entry->d_type == DT_LNK)) {
                                if (line_pos < MAX_ALUA_LAYOUT_LINES) {
                                    snprintf(dir_name, MAX_SYSFS_PATH_SIZE,
                                            "%s/device_groups/%s/"
                                            "target_groups/%s/%s/rel_tgt_id",
                                            SYSFS_SCST_TGT,
                                            dev_grp_dir_entry->d_name,
                                            tgt_grp_dir_entry->d_name,
                                            tgt_dir_entry->d_name);
                                    readAttribute(dir_name, tmp_buff);
                                    SAFE_ASPRINTF(&swindow_info[line_pos],
                                            "\t\t</B>Target:<!B> %s "
                                            "(Rel Tgt ID: %s)",
                                            tgt_dir_entry->d_name, tmp_buff);
                                    line_pos++;
                                }
                            }
                        }
                        closedir(tgt_dir_stream);
                    }
                }
                closedir(tgt_grp_dir_stream);

                /* Print a blank line to separate targets */
                if (line_pos < MAX_ALUA_LAYOUT_LINES) {
                    SAFE_ASPRINTF(&swindow_info[line_pos], " ");
                    line_pos++;
                }
            }
        }
        closedir(dev_grp_dir_stream);
        break;
    }

    /* Add a message to the bottom explaining how to close the dialog */
    if (line_pos < MAX_ALUA_LAYOUT_LINES) {
        SAFE_ASPRINTF(&swindow_info[line_pos], " ");
        line_pos++;
    }
    if (line_pos < MAX_ALUA_LAYOUT_LINES) {
        SAFE_ASPRINTF(&swindow_info[line_pos], CONTINUE_MSG);
        line_pos++;
    }

    /* Set the scrolling window content */
    setCDKSwindowContents(alua_info, swindow_info, line_pos);

    /* The 'g' makes the swindow widget scroll to the top, then activate */
    injectCDKSwindow(alua_info, 'g');
    activateCDKSwindow(alua_info, 0);

    /* We fell through -- the user exited the widget, but we don't care how */
    destroyCDKSwindow(alua_info);

    /* Done */
    for (i = 0; i < MAX_ALUA_LAYOUT_LINES; i++)
        FREE_NULL(swindow_info[i]);
    return;
}
Example #2
0
int main() {
	int logi, ch, y, x;

	display_log_type = INFO;
	log_init(&log);
	log_register_message_callback(&log, message_logged);

	current_action = NO_ACTION;

	initscr();
	create_windows();
	cdk_screen = initCDKScreen(log_window);
	log_swindow = newCDKSwindow(cdk_screen, 0, 0, log_lines, log_cols, "", 255, 0, 0);

	draw_stdscr();
	draw_url_input();
	draw_commands();

	select_form();

	raw();
	noecho();
	keypad(stdscr, TRUE);

	while ((ch = wgetch(input_window)) != '' || current_action & CANCEL_SHOWING)
		if (ch == KEY_LEFT)
			form_driver(url_form, REQ_LEFT_CHAR);
		else if (ch == KEY_RIGHT)
			form_driver(url_form, REQ_RIGHT_CHAR);
		else if (ch == KEY_BACKSPACE || ch == 127) {
			getyx(input_window, y, x);

			if (x == FIELD_START + 1) {
				form_driver(url_form, REQ_SCR_HBHALF);
				form_driver(url_form, REQ_END_LINE);
			}

			form_driver(url_form, REQ_LEFT_CHAR);
			form_driver(url_form, REQ_DEL_CHAR);
		} else if (ch == KEY_DC)
			form_driver(url_form, REQ_DEL_CHAR);
		else if (ch == KEY_HOME)
			form_driver(url_form, REQ_BEG_FIELD);
		else if (ch == KEY_END)
			form_driver(url_form, REQ_END_FIELD);
		else if (ch == KEY_UP || ch == KEY_DOWN)
			injectCDKSwindow(log_swindow, ch);
		else if (ch == '')
			cleanCDKSwindow(log_swindow);
		else if (ch == '	')
			change_display_log_type(INFO);
		else if (ch == '')
			change_display_log_type(DETAILS);
		else if (ch == '\n' && current_action & URL_INPUTTING) {
			int ret;

			form_driver(url_form, REQ_END_FIELD);
			form_driver(url_form, 'a');
			form_driver(url_form, REQ_VALIDATION);
			strncpy(url, field_buffer(url_field[0], 0), FIELD_BUF_SIZE);
			url[FIELD_BUF_SIZE - 1] = '\0';
			*strrchr(url, 'a') = '\0';

			http_url_init(&http_url);
			if (ret = http_url_parse(&http_url, url)) {
				char *message;

				switch (ret) {
					case INVALID_SERVER_AUTHORITY:
						message = "Invalid server authority";
						break;
					case INVALID_SCHEME:
						message = "Invalid scheme";
						break;
					case INVALID_PORT:
						message = "Invalid port";
						break;
					default:
						message = "Invalid URI";
						break;
				}

				log_printf(&log, INFO, "http_url_parse", "%s", message);

				draw_url_input();
				draw_commands();
			} else {
				draw_method_selection();
				draw_cancel();
			}
		} else if (tolower(ch) == 'g' && current_action & METHOD_SELECTING) {
			http_init(&http, &http_url, GET, NULL, 0, 3, &log);
			pthread_create(&http_thread, NULL, http_run_thread, &http);
			draw_cancel();
		} else if (tolower(ch) == 'h' && current_action & METHOD_SELECTING) {
			http_init(&http, &http_url, HEAD, NULL, 0, 3, &log);
			pthread_create(&http_thread, NULL, http_run_thread, &http);
			draw_cancel();
		} else if (tolower(ch) == 'p' && current_action & METHOD_SELECTING) {
			draw_post_input();
			draw_cancel();
			select_form();
		} else if (ch == '\n' && current_action & POST_INPUTTING) {
			form_driver(url_form, REQ_END_FIELD);
			form_driver(url_form, 'a');
			form_driver(url_form, REQ_VALIDATION);
			strncpy(post, field_buffer(url_field[0], 0), FIELD_BUF_SIZE);
			post[FIELD_BUF_SIZE - 1] = '\0';
			*strrchr(post, 'a') = '\0';

			http_init(&http, &http_url, POST, post, strlen(post), 3, &log);
			pthread_create(&http_thread, NULL, http_run_thread, &http);
			draw_cancel();
		} else if (ch == '\n' && current_action & FILE_INPUTTING) {
			form_driver(url_form, REQ_END_FIELD);
			form_driver(url_form, 'a');
			form_driver(url_form, REQ_VALIDATION);
			strncpy(file, field_buffer(url_field[0], 0), FIELD_BUF_SIZE);
			file[FIELD_BUF_SIZE - 1] = '\0';
			*strrchr(file, 'a') = '\0';

			pthread_mutex_lock(&input_mutex);
			inputted = 1;
			pthread_cond_broadcast(&input_cond);
			pthread_mutex_unlock(&input_mutex);
		} else if ((ch == 'y' || ch == 'Y') && current_action & OVERWRITE_INPUTTING) {
			do_overwrite = 1;

			pthread_mutex_lock(&input_mutex);
			inputted = 1;
			pthread_cond_broadcast(&input_cond);
			pthread_mutex_unlock(&input_mutex);
		} else if ((ch == 'n' || ch == 'N') && current_action & OVERWRITE_INPUTTING) {
			do_overwrite = 0;
			
			pthread_mutex_lock(&input_mutex);
			inputted = 1;
			pthread_cond_broadcast(&input_cond);
			pthread_mutex_unlock(&input_mutex);
		}else if (ch == '') {
			if (http.status != DISCONNECTED)
				http_disconnect(&http);
			draw_url_input();
			draw_commands();
			select_form();
		} else
			form_driver(url_form, ch);

	delete_input_form();
	destroyCDKSwindow(log_swindow);
	destroyCDKScreen(cdk_screen);
	endwin();

	pthread_mutex_destroy(&input_mutex);
	pthread_cond_destroy(&input_cond);

	return 0;
}