Ejemplo n.º 1
0
static void get_domain(const struct phishcheck* pchk,struct string* dest,struct string* host)
{
	char* domain;
	char* tld = strrchr(host->data,'.');
	if(!tld) {
		cli_dbgmsg("Phishcheck: Encountered a host without a tld? (%s)\n",host->data);
		string_assign(dest,host);
		return;
	}
	if(isCountryCode(pchk,tld+1)) {
		const char* countrycode = tld+1;
		tld = rfind(host->data,'.',tld-host->data-1);
		if(!tld) {
			cli_dbgmsg("Phishcheck: Weird, a name with only 2 levels (%s)\n",
				host->data);
			string_assign(dest,host);
			return;
		}
		if(!isTLD(pchk,tld+1,countrycode-tld-1)) {
			string_assign_ref(dest,host,tld+1);
			return;/*it was a name like: subdomain.domain.uk, return domain.uk*/
		}
	}
	/*we need to strip one more level, this is the actual domain*/
	domain = rfind(host->data,'.',tld-host->data-1);
	if(!domain) {
		string_assign(dest,host);
		return;/* it was like sourceforge.net?*/
	}
	string_assign_ref(dest,host,domain+1);
}
Ejemplo n.º 2
0
void update(void* instance)
{
  InstancePtr inst = (InstancePtr) instance;
  MyInstancePtr my = inst->my;

  int len          = inst->in_audio->len;
  double* samples  = inst->in_audio->samples;
  double thr       = trim_double(inst->in_thr->number, 0, 1);
  int is_beat;

  if (strcmp(my->algo.text, inst->in_algo->text) != 0
      || my->drv == 0)
    {
      string_assign(&my->algo, inst->in_algo);

      if (my->drv) {
        if (my->drv->inst) {
          my->drv->destroy(my->drv->inst);
        }
        free(my->drv);
      }
      
      if (strcmp(inst->in_algo->text, "exp") == 0) {
        my->drv = exp_get_driver();
      }
      else if (strcmp(inst->in_algo->text, "avg") == 0) {
        my->drv = av_get_driver();
      }
      else {
        char buffer[128];
        snprintf(buffer, sizeof(buffer),
                 "Unsupported algo: '%s', using 'exp' instead",
                 my->algo.text);
            
        s_log(0, buffer);
        my->drv = exp_get_driver();
      }

      my->drv->inst = my->drv->create(HIST_SIZE, LOCAL_SIZE);
    }

  assert(my->drv);
  assert(my->drv->inst);
  
  is_beat = my->drv->is_beat(my->drv->inst, samples, len, thr);

  if (is_beat)
    inst->out_b->number = 1.0;
  else
    inst->out_b->number = 0;

}
Ejemplo n.º 3
0
void string_erase(String *sp)
{
    string_assign(sp, 0);
}
Ejemplo n.º 4
0
void _type_copy_string(const void* cpv_first, const void* cpv_second, void* pv_output)
{
    assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL);
    string_assign((string_t*)cpv_first, (string_t*)cpv_second);
    *(bool_t*)pv_output = true;
}
Ejemplo n.º 5
0
void show_process_info() {
	vtab_t table;
	int header_rows = 2;
	int header_cols = 1;
	int data_rows;
	int show_sub_categories = (verbose || (num_pids == 1));
	if (show_sub_categories) {
		data_rows = PROCESS_MEMINFO_ROWS;
	} else {
		data_rows = num_pids;
	}
	// Add two extra rows for a horizontal rule followed by a total row
	// Add one extra data column for a total column
	init_table(&table, header_rows, header_cols, data_rows + 2, num_nodes + 1);
	int total_col_ix = header_cols + num_nodes;
	int total_row_ix = header_rows + data_rows + 1;
	string_assign(&table, total_row_ix, 0, "Total");
	if (show_sub_categories) {
		// Assign left header column label for each row in table
		for (int row = 0; (row < PROCESS_MEMINFO_ROWS); row++) {
			string_assign(&table, (header_rows + row), 0, process_meminfo[row].label);
		}
	} else {
		string_assign(&table, 0, 0, "PID");
		repchar_assign(&table, 1, 0, '-');
		printf("\nPer-node process memory usage (in MBs)\n");
	}
	// Set left header column width and left justify it
	set_col_width(&table, 0, 16);
	set_col_justification(&table, 0, COL_JUSTIFY_LEFT);
	// Set up "Node <N>" column headers over data columns, plus "Total" column
	for (int node_ix = 0; (node_ix <= num_nodes); node_ix++) {
		int col = header_cols + node_ix;
		// Assign header row label and horizontal line for this column...
		string_assign(&table, 0, col, node_header[node_ix]);
		repchar_assign(&table, 1, col, '-');
		// Set column width, decimal places, and right justify data
		set_col_width(&table, col, 16);
		int decimal_places = 2;
		if (compress_display) {
			decimal_places = 0;
		}
		set_col_decimal_places(&table, col, decimal_places);
		set_col_justification(&table, col, COL_JUSTIFY_RIGHT);
	}
	// Initialize data in table to all zeros
	zero_table_data(&table, CELL_TYPE_DOUBLE);
	// If (show_sub_categories), show individual process tables for each PID,
	// Otherwise show one big table of process total lines from all the PIDs.
	for (int pid_ix = 0; (pid_ix < num_pids); pid_ix++) {
		int pid = pid_array[pid_ix];
		if (show_sub_categories) {
			printf("\nPer-node process memory usage (in MBs) for PID %d (%s)\n", pid, command_name_for_pid(pid));
			if (pid_ix > 0) {
				// Re-initialize show_sub_categories table, because we re-use it for each PID.
				zero_table_data(&table, CELL_TYPE_DOUBLE);
			}
		} else {
			// Put this row's "PID (cmd)" label in left header column for this PID total row
			char tmp_buf[64];
			snprintf(tmp_buf, sizeof(tmp_buf), "%d (%s)", pid, command_name_for_pid(pid));
			char *p = strdup(tmp_buf);
			if (p == NULL) {
				perror("malloc failed line: " STRINGIFY(__LINE__));
				exit(EXIT_FAILURE);
			}
			string_assign(&table, header_rows + pid_ix, 0, p);
			set_cell_flag(&table, header_rows + pid_ix, 0, CELL_FLAG_FREEABLE);
		}
		// Open numa_map for this PID to get per-node data
		char fname[64];
		snprintf(fname, sizeof(fname), "/proc/%d/numa_maps", pid);
		char buf[BUF_SIZE];
		FILE *fs = fopen(fname, "r");
		if (!fs) {
			sprintf(buf, "Can't read /proc/%d/numa_maps", pid);
			perror(buf);
			continue;
		}
		// Add up sub-category memory used from each node.  Must go line by line
		// through the numa_map figuring out which category memory, node, and the
		// amount.
		while (fgets(buf, BUF_SIZE, fs)) {
			int category = PROCESS_PRIVATE_INDEX;	// init category to the catch-all...
			const char *delimiters = " \t\r\n";
			char *p = strtok(buf, delimiters);
			while (p) {
				// If the memory category for this line is still the catch-all
				// (i.e.  private), then see if the current token is a special
				// keyword for a specific memory sub-category.
				if (category == PROCESS_PRIVATE_INDEX) {
					for (int ix = 0; (ix < PROCESS_PRIVATE_INDEX); ix++) {
						if (!strncmp(p, process_meminfo[ix].token, strlen(process_meminfo[ix].token))) {
							category = ix;
							break;
						}
					}
				}
				// If the current token is a per-node pages quantity, parse the
				// node number and accumulate the number of pages in the specific
				// category (and also add to the total).
				if (p[0] == 'N') {
					int node_num = (int)strtol(&p[1], &p, 10);
					if (p[0] != '=') {
						perror("node value parse error");
						exit(EXIT_FAILURE);
					}
					double value = (double)strtol(&p[1], &p, 10);
					double multiplier = page_size_in_bytes;
					if (category == PROCESS_HUGE_INDEX) {
						multiplier = huge_page_size_in_bytes;
					}
					value *= multiplier;
					value /= (double)MEGABYTE;
					// Add value to data cell, total_col, and total_row
					int tmp_row;
					if (show_sub_categories) {
						tmp_row = header_rows + category;
					} else {
						tmp_row = header_rows + pid_ix;
					}
					int tmp_col = header_cols + node_num;
					double_addto(&table, tmp_row, tmp_col, value);
					double_addto(&table, tmp_row, total_col_ix, value);
					double_addto(&table, total_row_ix, tmp_col, value);
					double_addto(&table, total_row_ix, total_col_ix, value);
				}
				// Get next token on the line
				p = strtok(NULL, delimiters);
			}
		}
		// Currently, a non-root user can open some numa_map files successfully
		// without error, but can't actually read the contents -- despite the
		// 444 file permissions.  So, use ferror() to check here to see if we
		// actually got a read error, and if so, alert the user so they know
		// not to trust the zero in the table.
		if (ferror(fs)) {
			sprintf(buf, "Can't read /proc/%d/numa_maps", pid);
			perror(buf);
		}
		fclose(fs);
		// If showing individual tables, or we just added the last total line,
		// prepare the table for display and display it...
		if ((show_sub_categories) || (pid_ix + 1 == num_pids)) {
			// Crompress display column widths, if requested
			if (compress_display) {
				for (int col = 0; (col < header_cols + num_nodes + 1); col++) {
					auto_set_col_width(&table, col, 4, 16);
				}
			} else {
				// Since not compressing the display, allow the left header
				// column to be wider.  Otherwise, sometimes process command
				// name instance numbers can be truncated in an annoying way.
				auto_set_col_width(&table, 0, 16, 24);
			}
			// Put dashes above Total line...
			set_row_flag(&table, total_row_ix - 1, COL_FLAG_ALWAYS_SHOW);
			for (int col = 0; (col < header_cols + num_nodes + 1); col++) {
				repchar_assign(&table, total_row_ix - 1, col, '-');
			}
			// Optionally sort the table data
			if (sort_table) {
				int sort_col;
				if ((sort_table_node < 0) || (sort_table_node >= num_nodes)) {
					sort_col = total_col_ix;
				} else {
					sort_col = header_cols + node_ix_map[sort_table_node];
				}
				sort_rows_descending_by_col(&table, header_rows, header_rows + data_rows - 1, sort_col);
			}
			// Actually show the table
			display_table(&table, screen_width, 0, 0, show_zero_data, show_zero_data);
		}
	}			// END OF FOR_EACH-PID loop
	free_table(&table);
}				// show_process_info()
Ejemplo n.º 6
0
void show_info_from_system_file(char *file, meminfo_p meminfo, int meminfo_rows, int tok_offset) {
	// Setup and init table
	vtab_t table;
	int header_rows = 2 - compatibility_mode;
	int header_cols = 1;
	// Add an extra data column for a total column
	init_table(&table, header_rows, header_cols, meminfo_rows, num_nodes + 1);
	int total_col_ix = header_cols + num_nodes;
	// Insert token mapping in hash table and assign left header column label for each row in table
	init_hash_table();
	for (int row = 0; (row < meminfo_rows); row++) {
		hash_insert(meminfo[row].token, meminfo[row].index);
		if (compatibility_mode) {
			string_assign(&table, (header_rows + row), 0, meminfo[row].token);
		} else {
			string_assign(&table, (header_rows + row), 0, meminfo[row].label);
		}
	}
	// printf("There are %d table hash collisions.\n", hash_collisions);
	// Set left header column width and left justify it
	set_col_width(&table, 0, 16);
	set_col_justification(&table, 0, COL_JUSTIFY_LEFT);
	// Open /sys/devices/system/node/node?/<file> for each node and store data
	// in table.  If not compatibility_mode, do approximately first third of
	// this loop also for (node_ix == num_nodes) to get "Total" column header.
	for (int node_ix = 0; (node_ix < (num_nodes + (1 - compatibility_mode))); node_ix++) {
		int col = header_cols + node_ix;
		// Assign header row label and horizontal line for this column...
		string_assign(&table, 0, col, node_header[node_ix]);
		if (!compatibility_mode) {
			repchar_assign(&table, 1, col, '-');
			int decimal_places = 2;
			if (compress_display) {
				decimal_places = 0;
			}
			set_col_decimal_places(&table, col, decimal_places);
		}
		// Set column width and right justify data
		set_col_width(&table, col, 16);
		set_col_justification(&table, col, COL_JUSTIFY_RIGHT);
		if (node_ix == num_nodes) {
			break;
		}
		// Open /sys/.../node<N>/numstast file for this node...
		char buf[SMALL_BUF_SIZE];
		char fname[64];
		snprintf(fname, sizeof(fname), "/sys/devices/system/node/node%d/%s", node_ix_map[node_ix], file);
		FILE *fs = fopen(fname, "r");
		if (!fs) {
			sprintf(buf, "cannot open %s", fname);
			perror(buf);
			exit(EXIT_FAILURE);
		}
		// Get table values for this node...
		while (fgets(buf, SMALL_BUF_SIZE, fs)) {
			char *tok[64];
			int tokens = 0;
			const char *delimiters = " \t\r\n:";
			char *p = strtok(buf, delimiters);
			if (p == NULL) {
				continue;	// Skip blank lines;
			}
			while (p) {
				tok[tokens++] = p;
				p = strtok(NULL, delimiters);
			}
			// example line from numastat file: "numa_miss 16463"
			// example line from meminfo  file: "Node 3 Inactive:  210680 kB"
			int index = hash_lookup(tok[0 + tok_offset]);
			if (index < 0) {
				printf("Token %s not in hash table.\n", tok[0]);
			} else {
				double value = (double)atol(tok[1 + tok_offset]);
				if (!compatibility_mode) {
					double multiplier = 1.0;
					if (tokens < 4) {
						multiplier = page_size_in_bytes;
					} else if (!strncmp("HugePages", tok[2], 9)) {
						multiplier = huge_page_size_in_bytes;
					} else if (!strncmp("kB", tok[4], 2)) {
						multiplier = KILOBYTE;
					}
					value *= multiplier;
					value /= (double)MEGABYTE;
				}
				double_assign(&table, header_rows + index, col, value);
				double_addto(&table, header_rows + index, total_col_ix, value);
			}
		}
		fclose(fs);
	}
	// Crompress display column widths, if requested
	if (compress_display) {
		for (int col = 0; (col < header_cols + num_nodes + 1); col++) {
			auto_set_col_width(&table, col, 4, 16);
		}
	}
	// Optionally sort the table data
	if (sort_table) {
		int sort_col;
		if ((sort_table_node < 0) || (sort_table_node >= num_nodes)) {
			sort_col = total_col_ix;
		} else {
			sort_col = header_cols + node_ix_map[sort_table_node];
		}
		sort_rows_descending_by_col(&table, header_rows, header_rows + meminfo_rows - 1, sort_col);
	}
	// Actually display the table now, doing line-folding as necessary
	display_table(&table, screen_width, 0, 0, show_zero_data, show_zero_data);
	free_table(&table);
}
Ejemplo n.º 7
0
void assign(TypeInstanceID dst,TypeInstanceID src)
{
	string_assign((StringType*) dst, (const StringType*) src);
}
Ejemplo n.º 8
0
void update(void* instance)
{
    InstancePtr inst      = (InstancePtr) instance;
    MyInstancePtr my      = inst->my;
    int    options        = trim_int(inst->in_options->number, 0, INT_MAX);
    int    on_top         = options & 1;
    int    frame          = options & 2;
    int    mirrorx        = options & 4;
    int    mirrory        = options & 8;
    int    invert         = options & 16;
    int    monitor        = trim_int(inst->in_monitor->number, 0, 3);
    int    win_xsize      = trim_int(inst->in_xsize->number, 0, 2048);
    int    win_ysize      = trim_int(inst->in_ysize->number, 0, 2048);
    double brightness     = trim_double(inst->in_brightness->number, 0, 1);
    double contrast       = trim_double(inst->in_contrast->number, 0, 4);
    double gamma          = trim_double(inst->in_gamma->number, 0.01, 4);
    int    fb_xsize       = inst->in_in->xsize;
    int    fb_ysize       = inst->in_in->ysize;

    int result;
    char buffer[TEMP_BUF_SIZE] = "";
    const char* driver_name = inst->in_driver->text;
    const char* server_name = inst->in_server->text;

    if (strcmp(driver_name, my->driver_name->text) != 0 ||
            strcmp(server_name, my->server_name->text) != 0 ||
            my->drv == 0)
    {
        if (my->drv != 0)
        {
            get_drv_window_pos(my->drv, &my->win_xpos,&my->win_ypos);

            output_close(my->driver_name->text, my->server_name->text);
            my->drv = 0;
        }

        string_assign(my->driver_name, inst->in_driver);
        string_assign(my->server_name, inst->in_server);

        my->drv = output_open(driver_name, server_name);
        if (my->drv == 0)
            return;
    }

    assert(my->drv != 0);

    if (my->drv->inst == 0)
    {
        /** initialize the driver **/
        my->drv->inst = my->drv->new_instance(server_name,
                                              my->win_xpos, my->win_ypos,
                                              my->width, my->height,
                                              s_mmx_supported,
                                              buffer, sizeof(buffer));

        if (my->drv->inst == 0)
        {
            s_log(0, buffer);
            output_close(driver_name, server_name);

            my->drv = 0;
            return;
        }

        // reset parameters to make sure they are initialized below
        my->frame   = -1;
        my->monitor = -1;
        my->on_top  = -1;
    }

    assert(my->drv != 0);
    assert(my->drv->inst != 0);

    if (on_top != my->on_top && my->drv->always_on_top)
    {
        int res = my->drv->always_on_top(my->drv->inst,
                                         on_top,
                                         buffer, sizeof(buffer));
        if (!res)
            s_log(0, buffer);

        my->on_top  = on_top;
    }

    if (monitor != my->monitor && my->drv->to_monitor)
    {
        int res = my->drv->to_monitor(my->drv->inst,
                                      monitor,
                                      buffer, sizeof(buffer));
        if (!res)
            s_log(0, buffer);

        my->monitor = monitor;
    }

    if (win_xsize == 0 || win_ysize == 0)
    {
        win_xsize = fb_xsize;
        win_ysize = fb_ysize;
    }

    //  if (win_xsize != my->width || win_ysize != my->height)
    {
        int res = my->drv->resize(my->drv->inst, win_xsize, win_ysize,
                                  buffer, sizeof(buffer));

        if (!res)
        {
            char msg[128];
            snprintf(msg, sizeof(msg), "Could not resize: %s", buffer);
            s_log(0, msg);
        }

        my->width   = win_xsize;
        my->height  = win_ysize;
    }

    if (frame != my->frame && my->drv->frame)
    {
        int res = my->drv->frame(my->drv->inst,
                                 frame,
                                 buffer, sizeof(buffer));

        if (!res)
            s_log(0, buffer);
        my->frame = frame;
    }

    {
        struct blit_params params;
        params.mirrorx    = mirrorx;
        params.mirrory    = mirrory;
        params.brightness = brightness;
        params.contrast   = contrast;
        params.gamma      = gamma;
        params.invert     = invert;

        result = my->drv->blit(my->drv->inst,
                               (const uint8_t*) inst->in_in->framebuffer,
                               inst->in_in->xsize,
                               inst->in_in->ysize,
                               &params,
                               buffer, sizeof(buffer));
    }

    if (!result)
    {
        s_log(0, buffer);
    }
}