void LocationManager::positionAt(std::size_t offset, int *line, int *column, QString *filename) const { int ppline, ppcolumn; line_table.positionAt(offset, &ppline, &ppcolumn); int base_line; extract_line((int) line_table[ppline-1], &base_line, filename); int line2, column2; location_table.positionAt((int) line_table[ppline-1], &line2, &column2); location_table.positionAt(offset, line, column); *line = base_line + *line - line2 - 1; }
// perform FTP PWD command, prints current directory void command_pwd(CLIENT_INFO * client_info) { char line[BUFFER_SIZE] = { 0 }; client_read_line(client_info->fd, client_info->buf, &client_info->buffer_pos); extract_line(line, client_info->buf, &client_info->buffer_pos); int len = strlen(line); if (len != 3) { send_code(client_info->fd, 500); return; } send_code_param(client_info->fd, 257, client_info->dir); }
// perform FTP SYST command, identify as a standard UNIX FTP server void command_syst(CLIENT_INFO * client_info) { char line[BUFFER_SIZE] = { 0 }; client_read_line(client_info->fd, client_info->buf, &client_info->buffer_pos); extract_line(line, client_info->buf, &client_info->buffer_pos); int len = strlen(line); if (len != 4) { send_code(client_info->fd, 500); return; } send_code_param(client_info->fd, 215, "UNIX Type: L8"); }
// perform FTP USER command, take any username as valid void command_user(CLIENT_INFO * client_info) { char line[BUFFER_SIZE] = { 0 }; client_read_line(client_info->fd, client_info->buf, &client_info->buffer_pos); extract_line(line, client_info->buf, &client_info->buffer_pos); int len = strlen(line); if (len < 6) { send_code(client_info->fd, 500); return; } send_code(client_info->fd, 331); }
static recel_line extract_line(recel_tracer *t) { return (recel_line){ .x = (t->x * 2) + ((1 - t->dy) / 2 + (1 - t->dx) / 2), .y = (t->y * 2) + ((1 - t->dy) / 2 + (1 + t->dx) / 2), .n = (t->n + 1) * 2, .dx = t->dx, .dy = t->dy }; } recel_line recel_tracer_next_hv_line(recel_tracer *t) { recel_line l = extract_line(t); recel_tracer_next(t); return l; }
// perform FTP PASV command, prepare for passive data connection void command_pasv(CLIENT_INFO * client_info) { char line[BUFFER_SIZE] = { 0 }; client_read_line(client_info->fd, client_info->buf, &client_info->buffer_pos); extract_line(line, client_info->buf, &client_info->buffer_pos); int len = strlen(line); if (len != 4) { send_code(client_info->fd, 500); return; } if (client_info->passive_fd != 0) { close(client_info->passive_fd); client_info->passive_fd = 0; } struct sockaddr_in s; socklen_t l; l = sizeof(s); getsockname(client_info->fd, (struct sockaddr *)&s, &l); char * ip = inet_ntoa(s.sin_addr); if (! ip) epicfail("inet_ntoa"); client_info->data_connection_mode = CONN_MODE_PASSIVE; client_info->passive_fd = create_tcp_server_socket(ip, 0); l = sizeof(s); getsockname(client_info->passive_fd, (struct sockaddr *)&s, &l); int port = ntohs(s.sin_port); int ip1, ip2, ip3, ip4, port1, port2; if (sscanf(ip, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4) != 4) epicfail("command_pasv"); port1 = port >> 8; port2 = port & 0xff; char p[64]; sprintf(p, "%d,%d,%d,%d,%d,%d", ip1, ip2, ip3, ip4, port1, port2); send_code_param(client_info->fd, 227, p); }
char * LineSplitter::read() { /* is there enough data left in the buffer to read another line? */ if (memchr(remaining.first, '\n', remaining.second) == NULL) { /* no: read more data from the Source */ remaining = source.read(); if (remaining.second == 0) /* end of file */ return NULL; } assert(remaining.second > 0); Source<char>::Range range = remaining; std::pair<unsigned, unsigned> bounds = extract_line(range.first, range.second); source.consume(bounds.second); remaining.first += bounds.second; remaining.second -= bounds.second; if (bounds.first >= range.second) { /* last line, not terminated by a line feed: copy to local buffer, because we want to append the null byte */ char *line = last.get(range.second + 1); if (line == NULL) /* allocation has failed */ return NULL; memcpy(line, range.first, range.second); line[range.second] = 0; return line; } else { /* there is space left for the null byte */ range.first[bounds.first] = 0; return range.first; } }
// perform FTP PORT command, prepare for active data connection void command_port(CLIENT_INFO * client_info) { char line[BUFFER_SIZE] = { 0 }; client_read_line(client_info->fd, client_info->buf, &client_info->buffer_pos); extract_line(line, client_info->buf, &client_info->buffer_pos); int ip1, ip2, ip3, ip4, port1, port2; if (sscanf(line + 5, "%d,%d,%d,%d,%d,%d", &ip1, &ip2, &ip3, &ip4, &port1, &port2) != 6) { send_code(client_info->fd, 500); return; } client_info->data_connection_mode = CONN_MODE_ACTIVE; memset(&(client_info->active_addr), 0, sizeof(client_info->active_addr)); client_info->active_addr.sin_family = AF_INET; char buf_addr[32]; sprintf(buf_addr, "%d.%d.%d.%d", ip1, ip2, ip3, ip4); inet_pton(AF_INET, buf_addr, &(client_info->active_addr.sin_addr)); client_info->active_addr.sin_port = htons((port1 << 8) + port2); send_code(client_info->fd, 200); }
static int parse_header(struct saxs_document *doc, const struct line *firstline, const struct line *lastline) { assert_valid_lineset(firstline, lastline); while (firstline != lastline) { /* * Example line: * " #### G N O M --- Version 4.6 ####" * ^^^ */ if (strstr(firstline->line_buffer, "G N O M")) { saxs_document_add_property(doc, "creator", "GNOM"); saxs_document_add_property(doc, "creator-version", extract(firstline, "Version")); } /* * Example line: * "Run title: sphere" * "Run title: Lysozyme, high angles (>.22) 46 mg/ml, small angles (<.22) 15 mg/" */ else if (strstr(firstline->line_buffer, "Run title")) /* * Contrary to any other place, here we want everything after the delimiter, * not just the token up to the next whitespace. */ saxs_document_add_property(doc, "title", extract_line(firstline, ":")); /* * Example lines: * " Number of points omitted at the beginning: 9" * ^ * " Number of points omitted at the end: 1100" * ^^^^ * These lines are not present if '0' points are omitted. */ else if (strstr(firstline->line_buffer, "omitted at the beginning")) saxs_document_add_property(doc, "leading-points-omitted", extract(firstline, ":")); else if (strstr(firstline->line_buffer, "omitted at the end")) saxs_document_add_property(doc, "trailing-points-omitted", extract(firstline, ":")); /* * Example line: * " ******* Input file(s) : lyz_014.dat" * ^^^^^^^^^^^ */ else if (strstr(firstline->line_buffer, "Input file")) saxs_document_add_property(doc, "parent", extract(firstline, ":")); /* * Example lines: * " Condition P(rmin) = 0 is used. " * " Condition P(rmax) = 0 is used. " * * No need to extract anything, the lines are omitted if not used. */ else if (strstr(firstline->line_buffer, "Condition P(rmin)")) saxs_document_add_property(doc, "condition-r-min-zero", "true"); else if (strstr(firstline->line_buffer, "Condition P(rmax)")) saxs_document_add_property(doc, "condition-r-max-zero", "true"); /* * Example lines: * "Number of real space points is too large! Modified to NR = 215" * ^^^ * If the number of points was not modified, no line is printed. */ else if (strstr(firstline->line_buffer, "Number of real space points")) saxs_document_add_property(doc, "real-space-points", extract(firstline, "=")); /* * Example line: * " Warning: Dmax*Smin = 4.090 is greater than Pi" */ else if (strstr(firstline->line_buffer, "greater than Pi")) saxs_document_add_property(doc, "warning-dmax*smin-greater-than-pi", "true"); /* * Example line: * " Real space range : from 0.00 to 10.00" * * Assumption: 'from' is always 0.0, then 'to' denotes Dmax. */ else if (strstr(firstline->line_buffer, "Real space range")) saxs_document_add_property(doc, "real-space-range", extract(firstline, "to")); /* * Example line: * " Highest ALPHA (theor) : 0.182E+03 JOB = 0" * ^^^^^^^^^ */ else if (strstr(firstline->line_buffer, "Highest ALPHA (theor)")) saxs_document_add_property(doc, "highest-alpha-theor", extract(firstline, ":")); /* * Example line: * " Current ALPHA : 0.195E-18 Rg : 0.118E+01 I(0) : 0.332E+02" * ^^^^^^^^^ */ else if (strstr(firstline->line_buffer, "Current ALPHA")) saxs_document_add_property(doc, "current-alpha", extract(firstline, ":")); /* * Example line: * " Total estimate : 0.251 which is A BAD solution" * ^^^^^ */ else if (strstr(firstline->line_buffer, "Total estimate")) saxs_document_add_property(doc, "total-estimate", extract(firstline, ":")); // FIXME-1: properly handle 4.6 and 5.0 file versions. // FIXME-2: first-point, last-point only work if there was only one input file, // if there are multiple, things get messy. else if (strstr(firstline->line_buffer, "First data point used")) saxs_document_add_property(doc, "first-point", extract(firstline, ":")); else if (strstr(firstline->line_buffer, "Last data point used")) saxs_document_add_property(doc, "last-point", extract(firstline, ":")); else if (strstr(firstline->line_buffer, "Reciprocal space Rg")) saxs_document_add_property(doc, "reciprocal-space-rg", extract(firstline, ":")); else if (strstr(firstline->line_buffer, "Reciprocal space I(0)")) saxs_document_add_property(doc, "reciprocal-space-I0", extract(firstline, ":")); else if (strstr(firstline->line_buffer, "Real space Rg")) saxs_document_add_property(doc, "real-space-rg", extract(firstline, ":")); else if (strstr(firstline->line_buffer, "Real space I(0)")) saxs_document_add_property(doc, "real-space-I0", extract(firstline, ":")); else if (strstr(firstline->line_buffer, "Total Estimate")) saxs_document_add_property(doc, "total-estimate", extract(firstline, ":")); firstline = firstline->next; } return 0; }
/* * If edit_widget is NULL then we are called from the mc menu, * otherwise we are called from the mcedit menu. */ void user_menu_cmd (struct WEdit *edit_widget) { char *p; char *data, **entries; int max_cols, menu_lines, menu_limit; int col, i, accept_entry = 1; int selected, old_patterns; Listbox *listbox; if (!vfs_current_is_local ()){ message (1, MSG_ERROR, _(" Cannot execute commands on non-local filesystems")); return; } menu = g_strdup (edit_widget ? CEDIT_LOCAL_MENU : MC_LOCAL_MENU); if (!exist_file (menu) || !menu_file_own (menu)){ g_free (menu); menu = concat_dir_and_file \ (home_dir, edit_widget ? CEDIT_HOME_MENU : MC_HOME_MENU); if (!exist_file (menu)){ g_free (menu); menu = concat_dir_and_file \ (mc_home, edit_widget ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU); } } if ((data = load_file (menu)) == NULL){ message (1, MSG_ERROR, _(" Cannot open file %s \n %s "), menu, unix_error_string (errno)); g_free (menu); menu = NULL; return; } max_cols = 0; selected = 0; menu_limit = 0; entries = 0; /* Parse the menu file */ old_patterns = easy_patterns; p = check_patterns (data); for (menu_lines = col = 0; *p; p++){ if (menu_lines >= menu_limit){ char ** new_entries; menu_limit += MAX_ENTRIES; new_entries = g_realloc (entries, sizeof (new_entries[0]) * menu_limit); if (new_entries == 0) break; entries = new_entries; new_entries += menu_limit; while (--new_entries >= &entries[menu_lines]) *new_entries = 0; } if (col == 0 && !entries [menu_lines]){ if (*p == '#'){ /* A commented menu entry */ accept_entry = 1; } else if (*p == '+'){ if (*(p+1) == '='){ /* Combined adding and default */ p = test_line (edit_widget, p + 1, &accept_entry); if (selected == 0 && accept_entry) selected = menu_lines; } else { /* A condition for adding the entry */ p = test_line (edit_widget, p, &accept_entry); } } else if (*p == '='){ if (*(p+1) == '+'){ /* Combined adding and default */ p = test_line (edit_widget, p + 1, &accept_entry); if (selected == 0 && accept_entry) selected = menu_lines; } else { /* A condition for making the entry default */ i = 1; p = test_line (edit_widget, p, &i); if (selected == 0 && i) selected = menu_lines; } } else if (*p != ' ' && *p != '\t' && is_printable (*p)) { /* A menu entry title line */ if (accept_entry) entries [menu_lines] = p; else accept_entry = 1; } } if (*p == '\n'){ if (entries [menu_lines]){ menu_lines++; accept_entry = 1; } max_cols = max (max_cols, col); col = 0; } else { if (*p == '\t') *p = ' '; col++; } } if (menu_lines == 0) { message (1, MSG_ERROR, _(" No suitable entries found in %s "), menu); } else { max_cols = min (max (max_cols, col), MAX_ENTRY_LEN); /* Create listbox */ listbox = create_listbox_window (max_cols+2, menu_lines, _(" User menu "), "[Menu File Edit]"); /* insert all the items found */ for (i = 0; i < menu_lines; i++) { p = entries [i]; LISTBOX_APPEND_TEXT (listbox, (unsigned char) p[0], extract_line (p, p + MAX_ENTRY_LEN), p ); } /* Select the default entry */ listbox_select_by_number (listbox->list, selected); selected = run_listbox (listbox); if (selected >= 0) execute_menu_command (edit_widget, entries [selected]); do_refresh (); } easy_patterns = old_patterns; g_free (menu); menu = NULL; g_free (entries); g_free (data); }
gboolean user_menu_cmd (struct WEdit * edit_widget, const char *menu_file, int selected_entry) { char *p; char *data, **entries; int max_cols, menu_lines, menu_limit; int col, i, accept_entry = 1; int selected, old_patterns; gboolean res = FALSE; gboolean interactive = TRUE; if (!vfs_current_is_local ()) { message (D_ERROR, MSG_ERROR, "%s", _("Cannot execute commands on non-local filesystems")); return FALSE; } if (menu_file != NULL) menu = g_strdup (menu_file); else menu = g_strdup (edit_widget ? EDIT_LOCAL_MENU : MC_LOCAL_MENU); if (!exist_file (menu) || !menu_file_own (menu)) { if (menu_file != NULL) { message (D_ERROR, MSG_ERROR, _("Cannot open file %s\n%s"), menu, unix_error_string (errno)); MC_PTR_FREE (menu); return FALSE; } g_free (menu); if (edit_widget) menu = mc_config_get_full_path (EDIT_HOME_MENU); else menu = mc_config_get_full_path (MC_USERMENU_FILE); if (!exist_file (menu)) { g_free (menu); menu = mc_build_filename (mc_config_get_home_dir (), edit_widget ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU, NULL); if (!exist_file (menu)) { g_free (menu); menu = mc_build_filename (mc_global.sysconfig_dir, edit_widget ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU, NULL); if (!exist_file (menu)) { g_free (menu); menu = mc_build_filename (mc_global.share_data_dir, edit_widget ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU, NULL); } } } } if (!g_file_get_contents (menu, &data, NULL, NULL)) { message (D_ERROR, MSG_ERROR, _("Cannot open file%s\n%s"), menu, unix_error_string (errno)); MC_PTR_FREE (menu); return FALSE; } max_cols = 0; selected = 0; menu_limit = 0; entries = 0; /* Parse the menu file */ old_patterns = easy_patterns; p = check_patterns (data); for (menu_lines = col = 0; *p; str_next_char (&p)) { if (menu_lines >= menu_limit) { char **new_entries; menu_limit += MAX_ENTRIES; new_entries = g_try_realloc (entries, sizeof (new_entries[0]) * menu_limit); if (new_entries == NULL) break; entries = new_entries; new_entries += menu_limit; while (--new_entries >= &entries[menu_lines]) *new_entries = NULL; } if (col == 0 && !entries[menu_lines]) { if (*p == '#') { /* show prompt if first line of external script is #interactive */ if (selected_entry >= 0 && strncmp (p, "#silent", 7) == 0) interactive = FALSE; /* A commented menu entry */ accept_entry = 1; } else if (*p == '+') { if (*(p + 1) == '=') { /* Combined adding and default */ p = test_line (edit_widget, p + 1, &accept_entry); if (selected == 0 && accept_entry) selected = menu_lines; } else { /* A condition for adding the entry */ p = test_line (edit_widget, p, &accept_entry); } } else if (*p == '=') { if (*(p + 1) == '+') { /* Combined adding and default */ p = test_line (edit_widget, p + 1, &accept_entry); if (selected == 0 && accept_entry) selected = menu_lines; } else { /* A condition for making the entry default */ i = 1; p = test_line (edit_widget, p, &i); if (selected == 0 && i) selected = menu_lines; } } else if (*p != ' ' && *p != '\t' && str_isprint (p)) { /* A menu entry title line */ if (accept_entry) entries[menu_lines] = p; else accept_entry = 1; } } if (*p == '\n') { if (entries[menu_lines]) { menu_lines++; accept_entry = 1; } max_cols = max (max_cols, col); col = 0; } else { if (*p == '\t') *p = ' '; col++; } } if (menu_lines == 0) { message (D_ERROR, MSG_ERROR, _("No suitable entries found in %s"), menu); res = FALSE; } else { if (selected_entry >= 0) selected = selected_entry; else { Listbox *listbox; max_cols = min (max (max_cols, col), MAX_ENTRY_LEN); /* Create listbox */ listbox = create_listbox_window (menu_lines, max_cols + 2, _("User menu"), "[Menu File Edit]"); /* insert all the items found */ for (i = 0; i < menu_lines; i++) { p = entries[i]; LISTBOX_APPEND_TEXT (listbox, (unsigned char) p[0], extract_line (p, p + MAX_ENTRY_LEN), p); } /* Select the default entry */ listbox_select_entry (listbox->list, selected); selected = run_listbox (listbox); } if (selected >= 0) { execute_menu_command (edit_widget, entries[selected], interactive); res = TRUE; } do_refresh (); } easy_patterns = old_patterns; MC_PTR_FREE (menu); g_free (entries); g_free (data); return res; }
void user_menu_cmd (void) { char *menu, *p; int col, i, accept_entry = 1; int selected, old_patterns; Listbox *listbox; if (!vfs_current_is_local ()){ message (1, _(" Oops... "), _(" I can't run programs while logged on a non local directory ")); return; } menu = strdup (MC_LOCAL_MENU); if (!exist_file (menu) || !menu_file_own (menu)){ free (menu); #ifdef OS2_NT menu = strdup("NC.MNU"); if (!exist_file (menu)) #endif { menu = concat_dir_and_file (home_dir, MC_HOME_MENU); if (!exist_file (menu)){ free (menu); menu = concat_dir_and_file (mc_home, MC_GLOBAL_MENU); } } } if ((data = load_file (menu)) == NULL){ message (1, MSG_ERROR, _(" Can't open file %s \n %s "), menu, unix_error_string (errno)); free (menu); return; } free (menu); max_cols = 0; for (i = 0; i < MAX_ENTRIES; i++) entries [i] = 0; selected = 0; /* Parse the menu file */ old_patterns = easy_patterns; p = check_patterns (data); for (menu_lines = col = 0; *p; p++){ if (col == 0 && !entries [menu_lines]){ if (*p == '#'){ /* A commented menu entry */ accept_entry = 1; } else if (*p == '+'){ if (*(p+1) == '='){ /* Combined adding and default */ char *q = p++; p = test_line (q, &accept_entry); if (selected == 0 && accept_entry) selected = menu_lines; } else { /* A condition for adding the entry */ p = test_line (p, &accept_entry); } } else if (*p == '='){ if (*(p+1) == '+'){ char *q = p++; /* Combined adding and default */ p = test_line (q, &accept_entry); if (selected == 0 && accept_entry) selected = menu_lines; } else { /* A condition for making the entry default */ i = 1; p = test_line (p, &i); if (selected == 0 && i) selected = menu_lines; } } else if (*p > ' ' && *p < 127){ /* A menu entry title line */ if (accept_entry) entries [menu_lines] = p; else accept_entry = 1; } } if (menu_lines == MAX_ENTRIES) break; if (*p == '\t') *p = ' '; col++; if (*p == '\n'){ if (entries [menu_lines]){ menu_lines++; accept_entry = 1; } max_cols = max (max_cols, col); col = 0; } } max_cols = min (max (max_cols, col), MAX_ENTRY_LEN); /* Create listbox */ listbox = create_listbox_window (max_cols+2, menu_lines, _(" User menu "), "[Menu File Edit]"); /* insert all the items found */ for (i = 0; i < menu_lines; i++) LISTBOX_APPEND_TEXT (listbox, entries [i][0], extract_line (entries [i], entries [i]+MAX_ENTRY_LEN), entries [i]); /* Select the default entry */ listbox_select_by_number (listbox->list, selected); selected = run_listbox (listbox); if (selected >= 0) execute_menu_command (entries [selected]); easy_patterns = old_patterns; do_refresh (); free (data); }
// reads a line from the client buffer void get_line(int fd, char * line, char * buf, int * buffer_pos) { client_read_line(fd, buf, buffer_pos); extract_line(line, buf, buffer_pos); }
// perform FTP LIST command, sends a directory listing to the client void command_list(CLIENT_INFO * client_info) { char line[BUFFER_SIZE] = { 0 }; client_read_line(client_info->fd, client_info->buf, &client_info->buffer_pos); extract_line(line, client_info->buf, &client_info->buffer_pos); int len = strlen(line); char * path = NULL; if (len != 4) { char * p = line + 5; if (*p == '-') { while (*p && (*p != ' ')) p++; } while (*p && (*p == ' ')) p++; path = p; } char dirbuf[PATH_MAX + 1] = { 0 }; snprintf(dirbuf, PATH_MAX, "%s%s", basedir, client_info->dir); if (path) { if (path[0] == '/') { snprintf(dirbuf, PATH_MAX, "%s%s", basedir, path); } else { strcat(dirbuf, "/"); strcat(dirbuf, path); } } char realpathbuf[PATH_MAX + 1] = { 0 }; if (! realpath(dirbuf, realpathbuf)) { send_code(client_info->fd, 550); return; } strcpy(dirbuf, realpathbuf); if (dirbuf[strlen(dirbuf) - 1] != '/') strncat(dirbuf, "/", PATH_MAX); DIR * dirp = opendir(dirbuf); if (! dirp) { send_code(client_info->fd, 550); return; } send_code(client_info->fd, 150); open_data_connection(client_info); while (1) { struct dirent * entry = readdir(dirp); if (! entry) break; char filenamebuf[PATH_MAX + 1] = { 0 }; snprintf(filenamebuf, PATH_MAX, "%s%s", dirbuf, entry->d_name); struct stat s; if (stat(filenamebuf, &s) == -1) { // cannot stat continue; } char buf[PATH_MAX + 128 + 1] = { 0 }; strmode(s.st_mode, buf); unsigned int size = (unsigned int)s.st_size; char date[64]; struct tm * ts = localtime(&s.st_mtime); strftime(date, 64, "%b %e %Y", ts); sprintf(buf + 11, "%3d %-8d %-8d %8u %s %s\r\n", s.st_nlink, (int)s.st_uid, (int)s.st_gid, size, date, entry->d_name); data_connection_write_string(client_info, buf); } close_data_connection(client_info); send_code(client_info->fd, 226); }